tarsub.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * tar archive manipulation functions
  11. */
  12. #include <u.h>
  13. #include <libc.h>
  14. #include <ctype.h>
  15. #include "tar.h"
  16. enum {
  17. Blocksxfr = 32,
  18. };
  19. /* exports */
  20. char *thisnm, *lastnm;
  21. /* private data */
  22. static uint64_t outoff = 0; /* maintained by newarch, writetar */
  23. unsigned
  24. checksum(Hblock *hp)
  25. {
  26. int i;
  27. uint8_t *cp, *csum, *end;
  28. i = ' ' * sizeof hp->chksum; /* pretend blank chksum field */
  29. csum = (uint8_t *)hp->chksum;
  30. end = &hp->dummy[Tblock];
  31. /*
  32. * Unixware gets this wrong; it adds *signed* chars.
  33. * i += (Uflag? *(schar *)cp: *cp);
  34. */
  35. for (cp = hp->dummy; cp < csum; )
  36. i += *cp++;
  37. /* skip checksum field */
  38. for (cp += sizeof hp->chksum; cp < end; )
  39. i += *cp++;
  40. return i;
  41. }
  42. void
  43. readtar(int in, char *buffer, int32_t size)
  44. {
  45. int i;
  46. unsigned bytes;
  47. bytes = i = readn(in, buffer, size);
  48. if (i <= 0)
  49. sysfatal("archive read error: %r");
  50. if (bytes % Tblock != 0)
  51. sysfatal("archive blocksize error");
  52. if (bytes != size) {
  53. /*
  54. * buffering would be screwed up by only partially
  55. * filling tbuf, yet this might be the last (short)
  56. * record in a tar disk archive, so just zero the rest.
  57. */
  58. fprint(2, "%s: warning: short archive block\n", argv0);
  59. memset(buffer + bytes, '\0', size - bytes);
  60. }
  61. }
  62. void
  63. newarch(void)
  64. {
  65. outoff = 0;
  66. }
  67. uint64_t
  68. writetar(int outf, char *buffer, uint32_t size)
  69. {
  70. if (write(outf, buffer, size) < size) {
  71. fprint(2, "%s: archive write error: %r\n", argv0);
  72. fprint(2, "%s: archive seek offset: %llu\n", argv0, outoff);
  73. exits("write");
  74. }
  75. outoff += size;
  76. return outoff;
  77. }
  78. uint32_t
  79. otoi(char *s)
  80. {
  81. int c;
  82. uint32_t ul = 0;
  83. while (isascii(*s) && isspace(*s))
  84. s++;
  85. while ((c = *s++) >= '0' && c <= '7') {
  86. ul <<= 3;
  87. ul |= c - '0';
  88. }
  89. return ul;
  90. }
  91. int
  92. getdir(Hblock *hp, int in, int64_t *lenp)
  93. {
  94. *lenp = 0;
  95. readtar(in, (char*)hp, Tblock);
  96. if (hp->name[0] == '\0') { /* zero block indicates end-of-archive */
  97. lastnm = strdup(thisnm);
  98. return 0;
  99. }
  100. *lenp = otoi(hp->size);
  101. if (otoi(hp->chksum) != checksum(hp))
  102. sysfatal("directory checksum error");
  103. if (lastnm != nil)
  104. free(lastnm);
  105. lastnm = thisnm;
  106. thisnm = strdup(hp->name);
  107. return 1;
  108. }
  109. uint64_t
  110. passtar(Hblock *hp, int in, int outf, int64_t len)
  111. {
  112. uint32_t bytes;
  113. int64_t off;
  114. uint64_t blks;
  115. char bigbuf[Blocksxfr*Tblock]; /* 2*(8192 == MAXFDATA) */
  116. off = outoff;
  117. if (islink(hp->linkflag))
  118. return off;
  119. for (blks = TAPEBLKS((uint64_t)len); blks >= Blocksxfr;
  120. blks -= Blocksxfr) {
  121. readtar(in, bigbuf, sizeof bigbuf);
  122. off = writetar(outf, bigbuf, sizeof bigbuf);
  123. }
  124. if (blks > 0) {
  125. bytes = blks*Tblock;
  126. readtar(in, bigbuf, bytes);
  127. off = writetar(outf, bigbuf, bytes);
  128. }
  129. return off;
  130. }
  131. void
  132. putempty(int out)
  133. {
  134. static char buf[Tblock];
  135. writetar(out, buf, sizeof buf);
  136. }
  137. /* emit zero blocks at end */
  138. int
  139. closeout(int outf, char *, int prflag)
  140. {
  141. if (outf < 0)
  142. return -1;
  143. putempty(outf);
  144. putempty(outf);
  145. if (lastnm && prflag)
  146. fprint(2, " %s\n", lastnm);
  147. close(outf); /* guaranteed to succeed on plan 9 */
  148. return -1;
  149. }