tar.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /*
  2. * tar - `tape archiver', actually usable on any medium.
  3. * POSIX "ustar" compliant when extracting, and by default when creating.
  4. * this tar attempts to read and write multiple Tblock-byte blocks
  5. * at once to and from the filesystem, and does not copy blocks
  6. * around internally.
  7. */
  8. #include <u.h>
  9. #include <libc.h>
  10. #include <fcall.h> /* for %M */
  11. #include <String.h>
  12. /*
  13. * modified versions of those in libc.h; scans only the first arg for
  14. * keyletters and options.
  15. */
  16. #define TARGBEGIN {\
  17. (argv0 || (argv0 = *argv)), argv++, argc--;\
  18. if (argv[0]) {\
  19. char *_args, *_argt;\
  20. Rune _argc;\
  21. _args = &argv[0][0];\
  22. _argc = 0;\
  23. while(*_args && (_args += chartorune(&_argc, _args)))\
  24. switch(_argc)
  25. #define TARGEND SET(_argt); USED(_argt);USED(_argc);USED(_args); \
  26. argc--, argv++; } \
  27. USED(argv); USED(argc); }
  28. #define TARGC() (_argc)
  29. #define ROUNDUP(a, b) (((a) + (b) - 1)/(b))
  30. #define BYTES2TBLKS(bytes) ROUNDUP(bytes, Tblock)
  31. /* read big-endian binary integers; args must be (uchar *) */
  32. #define G2BEBYTE(x) (((x)[0]<<8) | (x)[1])
  33. #define G3BEBYTE(x) (((x)[0]<<16) | ((x)[1]<<8) | (x)[2])
  34. #define G4BEBYTE(x) (((x)[0]<<24) | ((x)[1]<<16) | ((x)[2]<<8) | (x)[3])
  35. #define G8BEBYTE(x) (((vlong)G4BEBYTE(x)<<32) | (u32int)G4BEBYTE((x)+4))
  36. typedef vlong Off;
  37. typedef char *(*Refill)(int ar, char *bufs, int justhdr);
  38. enum { Stdin, Stdout, Stderr };
  39. enum { Rd, Wr }; /* pipe fd-array indices */
  40. enum { Output, Input };
  41. enum { None, Toc, Xtract, Replace };
  42. enum { Alldata, Justnxthdr };
  43. enum {
  44. Tblock = 512,
  45. Namsiz = 100,
  46. Maxpfx = 155, /* from POSIX */
  47. Maxname = Namsiz + 1 + Maxpfx,
  48. Binsize = 0x80, /* flag in size[0], from gnu: positive binary size */
  49. Binnegsz = 0xff, /* flag in size[0]: negative binary size */
  50. Nblock = 40, /* maximum blocksize */
  51. Dblock = 20, /* default blocksize */
  52. Debug = 0,
  53. };
  54. /* POSIX link flags */
  55. enum {
  56. LF_PLAIN1 = '\0',
  57. LF_PLAIN2 = '0',
  58. LF_LINK = '1',
  59. LF_SYMLINK1 = '2',
  60. LF_SYMLINK2 = 's', /* 4BSD used this */
  61. LF_CHR = '3',
  62. LF_BLK = '4',
  63. LF_DIR = '5',
  64. LF_FIFO = '6',
  65. LF_CONTIG = '7',
  66. /* 'A' - 'Z' are reserved for custom implementations */
  67. };
  68. #define islink(lf) (isreallink(lf) || issymlink(lf))
  69. #define isreallink(lf) ((lf) == LF_LINK)
  70. #define issymlink(lf) ((lf) == LF_SYMLINK1 || (lf) == LF_SYMLINK2)
  71. typedef union {
  72. uchar data[Tblock];
  73. struct {
  74. char name[Namsiz];
  75. char mode[8];
  76. char uid[8];
  77. char gid[8];
  78. char size[12];
  79. char mtime[12];
  80. char chksum[8];
  81. char linkflag;
  82. char linkname[Namsiz];
  83. /* rest are defined by POSIX's ustar format; see p1003.2b */
  84. char magic[6]; /* "ustar" */
  85. char version[2];
  86. char uname[32];
  87. char gname[32];
  88. char devmajor[8];
  89. char devminor[8];
  90. char prefix[Maxpfx]; /* if non-null, path= prefix "/" name */
  91. };
  92. } Hdr;
  93. typedef struct {
  94. char *comp;
  95. char *decomp;
  96. char *sfx[4];
  97. } Compress;
  98. static Compress comps[] = {
  99. "gzip", "gunzip", { ".tar.gz", ".tgz" }, /* default */
  100. "compress", "uncompress", { ".tar.Z", ".tz" },
  101. "bzip2", "bunzip2", { ".tar.bz", ".tbz",
  102. ".tar.bz2",".tbz2" },
  103. };
  104. typedef struct {
  105. int kid;
  106. int fd; /* original fd */
  107. int rfd; /* replacement fd */
  108. int input;
  109. int open;
  110. } Pushstate;
  111. #define OTHER(rdwr) (rdwr == Rd? Wr: Rd)
  112. static int debug;
  113. static int fixednblock;
  114. static int verb;
  115. static int posix = 1;
  116. static int docreate;
  117. static int aruid;
  118. static int argid;
  119. static int relative = 1;
  120. static int settime;
  121. static int verbose;
  122. static int docompress;
  123. static int keepexisting;
  124. static int ignerrs; /* flag: ignore i/o errors if possible */
  125. static Off blkoff; /* offset of the current archive block (not Tblock) */
  126. static Off nexthdr;
  127. static int nblock = Dblock;
  128. static char *usefile, *arname = "archive";
  129. static char origdir[Maxname*2];
  130. static Hdr *tpblk, *endblk;
  131. static Hdr *curblk;
  132. static void
  133. usage(void)
  134. {
  135. fprint(2, "usage: %s {crtx}[PRTfgikmpuvz] [archive] file1 file2...\n",
  136. argv0);
  137. exits("usage");
  138. }
  139. /* I/O, with error retry or exit */
  140. static int
  141. cope(char *name, int fd, void *buf, long len, Off off)
  142. {
  143. fprint(2, "%s: %serror reading %s: %r\n", argv0,
  144. (ignerrs? "ignoring ": ""), name);
  145. if (!ignerrs)
  146. exits("read error");
  147. /* pretend we read len bytes of zeroes */
  148. memset(buf, 0, len);
  149. if (off >= 0) /* seekable? */
  150. seek(fd, off + len, 0);
  151. return len;
  152. }
  153. static int
  154. eread(char *name, int fd, void *buf, long len)
  155. {
  156. int rd;
  157. Off off;
  158. off = seek(fd, 0, 1); /* for coping with errors */
  159. rd = read(fd, buf, len);
  160. if (rd < 0)
  161. rd = cope(name, fd, buf, len, off);
  162. return rd;
  163. }
  164. static int
  165. ereadn(char *name, int fd, void *buf, long len)
  166. {
  167. int rd;
  168. Off off;
  169. off = seek(fd, 0, 1);
  170. rd = readn(fd, buf, len);
  171. if (rd < 0)
  172. rd = cope(name, fd, buf, len, off);
  173. return rd;
  174. }
  175. static int
  176. ewrite(char *name, int fd, void *buf, long len)
  177. {
  178. int rd;
  179. werrstr("");
  180. rd = write(fd, buf, len);
  181. if (rd != len)
  182. sysfatal("error writing %s: %r", name);
  183. return rd;
  184. }
  185. /* compression */
  186. static Compress *
  187. compmethod(char *name)
  188. {
  189. int i, nmlen = strlen(name), sfxlen;
  190. Compress *cp;
  191. for (cp = comps; cp < comps + nelem(comps); cp++)
  192. for (i = 0; i < nelem(cp->sfx) && cp->sfx[i]; i++) {
  193. sfxlen = strlen(cp->sfx[i]);
  194. if (nmlen > sfxlen &&
  195. strcmp(cp->sfx[i], name + nmlen - sfxlen) == 0)
  196. return cp;
  197. }
  198. return docompress? comps: nil;
  199. }
  200. /*
  201. * push a filter, cmd, onto fd. if input, it's an input descriptor.
  202. * returns a descriptor to replace fd, or -1 on error.
  203. */
  204. static int
  205. push(int fd, char *cmd, int input, Pushstate *ps)
  206. {
  207. int nfd, pifds[2];
  208. String *s;
  209. ps->open = 0;
  210. ps->fd = fd;
  211. ps->input = input;
  212. if (fd < 0 || pipe(pifds) < 0)
  213. return -1;
  214. ps->kid = fork();
  215. switch (ps->kid) {
  216. case -1:
  217. return -1;
  218. case 0:
  219. if (input)
  220. dup(pifds[Wr], Stdout);
  221. else
  222. dup(pifds[Rd], Stdin);
  223. close(pifds[input? Rd: Wr]);
  224. dup(fd, (input? Stdin: Stdout));
  225. s = s_new();
  226. if (cmd[0] != '/')
  227. s_append(s, "/bin/");
  228. s_append(s, cmd);
  229. execl(s_to_c(s), cmd, nil);
  230. sysfatal("can't exec %s: %r", cmd);
  231. default:
  232. nfd = pifds[input? Rd: Wr];
  233. close(pifds[input? Wr: Rd]);
  234. break;
  235. }
  236. ps->rfd = nfd;
  237. ps->open = 1;
  238. return nfd;
  239. }
  240. static char *
  241. pushclose(Pushstate *ps)
  242. {
  243. Waitmsg *wm;
  244. if (ps->fd < 0 || ps->rfd < 0 || !ps->open)
  245. return "not open";
  246. close(ps->rfd);
  247. ps->rfd = -1;
  248. ps->open = 0;
  249. while ((wm = wait()) != nil && wm->pid != ps->kid)
  250. continue;
  251. return wm? wm->msg: nil;
  252. }
  253. /*
  254. * block-buffer management
  255. */
  256. static void
  257. initblks(void)
  258. {
  259. free(tpblk);
  260. tpblk = malloc(Tblock * nblock);
  261. assert(tpblk != nil);
  262. endblk = tpblk + nblock;
  263. }
  264. /*
  265. * (re)fill block buffers from archive. `justhdr' means we don't care
  266. * about the data before the next header block.
  267. */
  268. static char *
  269. refill(int ar, char *bufs, int justhdr)
  270. {
  271. int i, n;
  272. unsigned bytes = Tblock * nblock;
  273. static int done, first = 1, seekable;
  274. if (done)
  275. return nil;
  276. blkoff = seek(ar, 0, 1); /* note position for `tar r' */
  277. if (first)
  278. seekable = blkoff >= 0;
  279. /* try to size non-pipe input at first read */
  280. if (first && usefile && !fixednblock) {
  281. n = eread(arname, ar, bufs, bytes);
  282. if (n == 0)
  283. sysfatal("EOF reading archive: %r");
  284. i = n;
  285. if (i % Tblock != 0)
  286. sysfatal("archive block size (%d) error", i);
  287. i /= Tblock;
  288. if (i != nblock) {
  289. nblock = i;
  290. fprint(2, "%s: blocking = %d\n", argv0, nblock);
  291. endblk = (Hdr *)bufs + nblock;
  292. bytes = n;
  293. }
  294. } else if (justhdr && seekable && nexthdr - blkoff >= bytes) {
  295. /* optimisation for huge archive members on seekable media */
  296. if (seek(ar, bytes, 1) < 0)
  297. sysfatal("can't seek on archive: %r");
  298. n = bytes;
  299. } else
  300. n = ereadn(arname, ar, bufs, bytes);
  301. first = 0;
  302. if (n == 0)
  303. sysfatal("unexpected EOF reading archive");
  304. if (n % Tblock != 0)
  305. sysfatal("partial block read from archive");
  306. if (n != bytes) {
  307. done = 1;
  308. memset(bufs + n, 0, bytes - n);
  309. }
  310. return bufs;
  311. }
  312. static Hdr *
  313. getblk(int ar, Refill rfp, int justhdr)
  314. {
  315. if (curblk == nil || curblk >= endblk) { /* input block exhausted? */
  316. if (rfp != nil && (*rfp)(ar, (char *)tpblk, justhdr) == nil)
  317. return nil;
  318. curblk = tpblk;
  319. }
  320. return curblk++;
  321. }
  322. static Hdr *
  323. getblkrd(int ar, int justhdr)
  324. {
  325. return getblk(ar, refill, justhdr);
  326. }
  327. static Hdr *
  328. getblke(int ar)
  329. {
  330. return getblk(ar, nil, Alldata);
  331. }
  332. static Hdr *
  333. getblkz(int ar)
  334. {
  335. Hdr *hp = getblke(ar);
  336. if (hp != nil)
  337. memset(hp->data, 0, Tblock);
  338. return hp;
  339. }
  340. /*
  341. * how many block buffers are available, starting at the address
  342. * just returned by getblk*?
  343. */
  344. static int
  345. gothowmany(int max)
  346. {
  347. int n = endblk - (curblk - 1);
  348. return n > max? max: n;
  349. }
  350. /*
  351. * indicate that one is done with the last block obtained from getblke
  352. * and it is now available to be written into the archive.
  353. */
  354. static void
  355. putlastblk(int ar)
  356. {
  357. unsigned bytes = Tblock * nblock;
  358. /* if writing end-of-archive, aid compression (good hygiene too) */
  359. if (curblk < endblk)
  360. memset(curblk, 0, (char *)endblk - (char *)curblk);
  361. ewrite(arname, ar, tpblk, bytes);
  362. }
  363. static void
  364. putblk(int ar)
  365. {
  366. if (curblk >= endblk)
  367. putlastblk(ar);
  368. }
  369. static void
  370. putbackblk(int ar)
  371. {
  372. curblk--;
  373. USED(ar);
  374. }
  375. static void
  376. putreadblks(int ar, int blks)
  377. {
  378. curblk += blks - 1;
  379. USED(ar);
  380. }
  381. static void
  382. putblkmany(int ar, int blks)
  383. {
  384. assert(blks > 0);
  385. curblk += blks - 1;
  386. putblk(ar);
  387. }
  388. /*
  389. * common routines
  390. */
  391. /*
  392. * modifies hp->chksum but restores it; important for the last block of the
  393. * old archive when updating with `tar rf archive'
  394. */
  395. static long
  396. chksum(Hdr *hp)
  397. {
  398. int n = Tblock;
  399. long i = 0;
  400. uchar *cp = hp->data;
  401. char oldsum[sizeof hp->chksum];
  402. memmove(oldsum, hp->chksum, sizeof oldsum);
  403. memset(hp->chksum, ' ', sizeof hp->chksum);
  404. while (n-- > 0)
  405. i += *cp++;
  406. memmove(hp->chksum, oldsum, sizeof oldsum);
  407. return i;
  408. }
  409. static int
  410. isustar(Hdr *hp)
  411. {
  412. return strcmp(hp->magic, "ustar") == 0;
  413. }
  414. /*
  415. * s is at most n bytes long, but need not be NUL-terminated.
  416. * if shorter than n bytes, all bytes after the first NUL must also
  417. * be NUL.
  418. */
  419. static int
  420. strnlen(char *s, int n)
  421. {
  422. return s[n - 1] != '\0'? n: strlen(s);
  423. }
  424. /* set fullname from header */
  425. static char *
  426. name(Hdr *hp)
  427. {
  428. int pfxlen, namlen;
  429. static char fullnamebuf[2+Maxname+1]; /* 2+ for ./ on relative names */
  430. char *fullname;
  431. fullname = fullnamebuf+2;
  432. namlen = strnlen(hp->name, sizeof hp->name);
  433. if (hp->prefix[0] == '\0' || !isustar(hp)) { /* old-style name? */
  434. memmove(fullname, hp->name, namlen);
  435. fullname[namlen] = '\0';
  436. return fullname;
  437. }
  438. /* name is in two pieces */
  439. pfxlen = strnlen(hp->prefix, sizeof hp->prefix);
  440. memmove(fullname, hp->prefix, pfxlen);
  441. fullname[pfxlen] = '/';
  442. memmove(fullname + pfxlen + 1, hp->name, namlen);
  443. fullname[pfxlen + 1 + namlen] = '\0';
  444. return fullname;
  445. }
  446. static int
  447. isdir(Hdr *hp)
  448. {
  449. /* the mode test is ugly but sometimes necessary */
  450. return hp->linkflag == LF_DIR ||
  451. strrchr(name(hp), '\0')[-1] == '/' ||
  452. (strtoul(hp->mode, nil, 8)&0170000) == 040000;
  453. }
  454. static int
  455. eotar(Hdr *hp)
  456. {
  457. return name(hp)[0] == '\0';
  458. }
  459. /*
  460. static uvlong
  461. getbe(uchar *src, int size)
  462. {
  463. uvlong vl = 0;
  464. while (size-- > 0) {
  465. vl <<= 8;
  466. vl |= *src++;
  467. }
  468. return vl;
  469. }
  470. */
  471. static void
  472. putbe(uchar *dest, uvlong vl, int size)
  473. {
  474. for (dest += size; size-- > 0; vl >>= 8)
  475. *--dest = vl;
  476. }
  477. /*
  478. * return the nominal size from the header block, which is not always the
  479. * size in the archive (the archive size may be zero for some file types
  480. * regardless of the nominal size).
  481. *
  482. * gnu and freebsd tars are now recording vlongs as big-endian binary
  483. * with a flag in byte 0 to indicate this, which permits file sizes up to
  484. * 2^64-1 (actually 2^80-1 but our file sizes are vlongs) rather than 2^33-1.
  485. */
  486. static Off
  487. hdrsize(Hdr *hp)
  488. {
  489. uchar *p;
  490. if((uchar)hp->size[0] == Binnegsz) {
  491. fprint(2, "%s: %s: negative length, which is insane\n",
  492. argv0, name(hp));
  493. return 0;
  494. } else if((uchar)hp->size[0] == Binsize) {
  495. p = (uchar *)hp->size + sizeof hp->size - 1 -
  496. sizeof(vlong); /* -1 for terminating space */
  497. return G8BEBYTE(p);
  498. } else
  499. return strtoull(hp->size, nil, 8);
  500. }
  501. /*
  502. * return the number of bytes recorded in the archive.
  503. */
  504. static Off
  505. arsize(Hdr *hp)
  506. {
  507. if(isdir(hp) || islink(hp->linkflag))
  508. return 0;
  509. return hdrsize(hp);
  510. }
  511. static Hdr *
  512. readhdr(int ar)
  513. {
  514. long hdrcksum;
  515. Hdr *hp;
  516. hp = getblkrd(ar, Alldata);
  517. if (hp == nil)
  518. sysfatal("unexpected EOF instead of archive header");
  519. if (eotar(hp)) /* end-of-archive block? */
  520. return nil;
  521. hdrcksum = strtoul(hp->chksum, nil, 8);
  522. if (chksum(hp) != hdrcksum)
  523. sysfatal("bad archive header checksum: name %.64s...",
  524. hp->name);
  525. nexthdr += Tblock*(1 + BYTES2TBLKS(arsize(hp)));
  526. return hp;
  527. }
  528. /*
  529. * tar r[c]
  530. */
  531. /*
  532. * if name is longer than Namsiz bytes, try to split it at a slash and fit the
  533. * pieces into hp->prefix and hp->name.
  534. */
  535. static int
  536. putfullname(Hdr *hp, char *name)
  537. {
  538. int namlen, pfxlen;
  539. char *sl, *osl;
  540. String *slname = nil;
  541. if (isdir(hp)) {
  542. slname = s_new();
  543. s_append(slname, name);
  544. s_append(slname, "/"); /* posix requires this */
  545. name = s_to_c(slname);
  546. }
  547. namlen = strlen(name);
  548. if (namlen <= Namsiz) {
  549. strncpy(hp->name, name, Namsiz);
  550. hp->prefix[0] = '\0'; /* ustar paranoia */
  551. return 0;
  552. }
  553. if (!posix || namlen > Maxname) {
  554. fprint(2, "%s: name too long for tar header: %s\n",
  555. argv0, name);
  556. return -1;
  557. }
  558. /*
  559. * try various splits until one results in pieces that fit into the
  560. * appropriate fields of the header. look for slashes from right
  561. * to left, in the hopes of putting the largest part of the name into
  562. * hp->prefix, which is larger than hp->name.
  563. */
  564. sl = strrchr(name, '/');
  565. while (sl != nil) {
  566. pfxlen = sl - name;
  567. if (pfxlen <= sizeof hp->prefix && namlen-1 - pfxlen <= Namsiz)
  568. break;
  569. osl = sl;
  570. *osl = '\0';
  571. sl = strrchr(name, '/');
  572. *osl = '/';
  573. }
  574. if (sl == nil) {
  575. fprint(2, "%s: name can't be split to fit tar header: %s\n",
  576. argv0, name);
  577. return -1;
  578. }
  579. *sl = '\0';
  580. strncpy(hp->prefix, name, sizeof hp->prefix);
  581. *sl++ = '/';
  582. strncpy(hp->name, sl, sizeof hp->name);
  583. if (slname)
  584. s_free(slname);
  585. return 0;
  586. }
  587. static int
  588. mkhdr(Hdr *hp, Dir *dir, char *file)
  589. {
  590. /*
  591. * some of these fields run together, so we format them left-to-right
  592. * and don't use snprint.
  593. */
  594. sprint(hp->mode, "%6lo ", dir->mode & 0777);
  595. sprint(hp->uid, "%6o ", aruid);
  596. sprint(hp->gid, "%6o ", argid);
  597. if (dir->length >= (Off)1<<32) {
  598. static int printed;
  599. if (!printed) {
  600. printed = 1;
  601. fprint(2, "%s: storing large sizes in \"base 256\"\n", argv0);
  602. }
  603. hp->size[0] = Binsize;
  604. /* emit so-called `base 256' representation of size */
  605. putbe((uchar *)hp->size+1, dir->length, sizeof hp->size - 2);
  606. hp->size[sizeof hp->size - 1] = ' ';
  607. } else
  608. sprint(hp->size, "%11lluo ", dir->length);
  609. sprint(hp->mtime, "%11luo ", dir->mtime);
  610. hp->linkflag = (dir->mode&DMDIR? LF_DIR: LF_PLAIN1);
  611. putfullname(hp, file);
  612. if (posix) {
  613. strncpy(hp->magic, "ustar", sizeof hp->magic);
  614. strncpy(hp->version, "00", sizeof hp->version);
  615. strncpy(hp->uname, dir->uid, sizeof hp->uname);
  616. strncpy(hp->gname, dir->gid, sizeof hp->gname);
  617. }
  618. sprint(hp->chksum, "%6luo", chksum(hp));
  619. return 0;
  620. }
  621. static void addtoar(int ar, char *file, char *shortf);
  622. static void
  623. addtreetoar(int ar, char *file, char *shortf, int fd)
  624. {
  625. int n;
  626. Dir *dent, *dirents;
  627. String *name = s_new();
  628. n = dirreadall(fd, &dirents);
  629. if (n < 0)
  630. fprint(2, "%s: dirreadall %s: %r\n", argv0, file);
  631. close(fd);
  632. if (n <= 0)
  633. return;
  634. if (chdir(shortf) < 0)
  635. sysfatal("chdir %s: %r", file);
  636. if (Debug)
  637. fprint(2, "chdir %s\t# %s\n", shortf, file);
  638. for (dent = dirents; dent < dirents + n; dent++) {
  639. s_reset(name);
  640. s_append(name, file);
  641. s_append(name, "/");
  642. s_append(name, dent->name);
  643. addtoar(ar, s_to_c(name), dent->name);
  644. }
  645. s_free(name);
  646. free(dirents);
  647. /*
  648. * this assumes that shortf is just one component, which is true
  649. * during directory descent, but not necessarily true of command-line
  650. * arguments. Our caller (or addtoar's) must reset the working
  651. * directory if necessary.
  652. */
  653. if (chdir("..") < 0)
  654. sysfatal("chdir %s/..: %r", file);
  655. if (Debug)
  656. fprint(2, "chdir ..\n");
  657. }
  658. static void
  659. addtoar(int ar, char *file, char *shortf)
  660. {
  661. int n, fd, isdir;
  662. long bytes, blksread;
  663. ulong blksleft;
  664. Hdr *hbp;
  665. Dir *dir;
  666. String *name = nil;
  667. if (shortf[0] == '#') {
  668. name = s_new();
  669. s_append(name, "./");
  670. s_append(name, shortf);
  671. shortf = s_to_c(name);
  672. }
  673. if (Debug)
  674. fprint(2, "opening %s # %s\n", shortf, file);
  675. fd = open(shortf, OREAD);
  676. if (fd < 0) {
  677. fprint(2, "%s: can't open %s: %r\n", argv0, file);
  678. if (name)
  679. s_free(name);
  680. return;
  681. }
  682. dir = dirfstat(fd);
  683. if (dir == nil)
  684. sysfatal("can't fstat %s: %r", file);
  685. hbp = getblkz(ar);
  686. isdir = (dir->qid.type & QTDIR) != 0;
  687. if (mkhdr(hbp, dir, file) < 0) {
  688. putbackblk(ar);
  689. free(dir);
  690. close(fd);
  691. if (name)
  692. s_free(name);
  693. return;
  694. }
  695. putblk(ar);
  696. blksleft = BYTES2TBLKS(dir->length);
  697. free(dir);
  698. if (isdir)
  699. addtreetoar(ar, file, shortf, fd);
  700. else {
  701. for (; blksleft > 0; blksleft -= blksread) {
  702. hbp = getblke(ar);
  703. blksread = gothowmany(blksleft);
  704. assert(blksread >= 0);
  705. bytes = blksread * Tblock;
  706. n = ereadn(file, fd, hbp->data, bytes);
  707. assert(n >= 0);
  708. /*
  709. * ignore EOF. zero any partial block to aid
  710. * compression and emergency recovery of data.
  711. */
  712. if (n < Tblock)
  713. memset(hbp->data + n, 0, bytes - n);
  714. putblkmany(ar, blksread);
  715. }
  716. close(fd);
  717. if (verbose)
  718. fprint(2, "%s\n", file);
  719. }
  720. if (name)
  721. s_free(name);
  722. }
  723. static char *
  724. replace(char **argv)
  725. {
  726. int i, ar;
  727. ulong blksleft, blksread;
  728. Off bytes;
  729. Hdr *hp;
  730. Compress *comp = nil;
  731. Pushstate ps;
  732. if (usefile && docreate) {
  733. ar = create(usefile, OWRITE, 0666);
  734. if (docompress)
  735. comp = compmethod(usefile);
  736. } else if (usefile)
  737. ar = open(usefile, ORDWR);
  738. else
  739. ar = Stdout;
  740. if (comp)
  741. ar = push(ar, comp->comp, Output, &ps);
  742. if (ar < 0)
  743. sysfatal("can't open archive %s: %r", usefile);
  744. if (usefile && !docreate) {
  745. /* skip quickly to the end */
  746. while ((hp = readhdr(ar)) != nil) {
  747. bytes = arsize(hp);
  748. for (blksleft = BYTES2TBLKS(bytes);
  749. blksleft > 0 && getblkrd(ar, Justnxthdr) != nil;
  750. blksleft -= blksread) {
  751. blksread = gothowmany(blksleft);
  752. putreadblks(ar, blksread);
  753. }
  754. }
  755. /*
  756. * we have just read the end-of-archive Tblock.
  757. * now seek back over the (big) archive block containing it,
  758. * and back up curblk ptr over end-of-archive Tblock in memory.
  759. */
  760. if (seek(ar, blkoff, 0) < 0)
  761. sysfatal("can't seek back over end-of-archive: %r");
  762. curblk--;
  763. }
  764. for (i = 0; argv[i] != nil; i++) {
  765. addtoar(ar, argv[i], argv[i]);
  766. chdir(origdir); /* for correctness & profiling */
  767. }
  768. /* write end-of-archive marker */
  769. getblkz(ar);
  770. putblk(ar);
  771. getblkz(ar);
  772. putlastblk(ar);
  773. if (comp)
  774. return pushclose(&ps);
  775. if (ar > Stderr)
  776. close(ar);
  777. return nil;
  778. }
  779. /*
  780. * tar [xt]
  781. */
  782. /* is pfx a file-name prefix of name? */
  783. static int
  784. prefix(char *name, char *pfx)
  785. {
  786. int pfxlen = strlen(pfx);
  787. char clpfx[Maxname+1];
  788. if (pfxlen > Maxname)
  789. return 0;
  790. strcpy(clpfx, pfx);
  791. cleanname(clpfx);
  792. return strncmp(clpfx, name, pfxlen) == 0 &&
  793. (name[pfxlen] == '\0' || name[pfxlen] == '/');
  794. }
  795. static int
  796. match(char *name, char **argv)
  797. {
  798. int i;
  799. char clname[Maxname+1];
  800. if (argv[0] == nil)
  801. return 1;
  802. strcpy(clname, name);
  803. cleanname(clname);
  804. for (i = 0; argv[i] != nil; i++)
  805. if (prefix(clname, argv[i]))
  806. return 1;
  807. return 0;
  808. }
  809. static void
  810. cantcreate(char *s, int mode)
  811. {
  812. int len;
  813. static char *last;
  814. /*
  815. * Always print about files. Only print about directories
  816. * we haven't printed about. (Assumes archive is ordered
  817. * nicely.)
  818. */
  819. if(mode&DMDIR){
  820. if(last){
  821. /* already printed this directory */
  822. if(strcmp(s, last) == 0)
  823. return;
  824. /* printed a higher directory, so printed this one */
  825. len = strlen(s);
  826. if(memcmp(s, last, len) == 0 && last[len] == '/')
  827. return;
  828. }
  829. /* save */
  830. free(last);
  831. last = strdup(s);
  832. }
  833. fprint(2, "%s: can't create %s: %r\n", argv0, s);
  834. }
  835. static int
  836. makedir(char *s)
  837. {
  838. int f;
  839. if (access(s, AEXIST) == 0)
  840. return -1;
  841. f = create(s, OREAD, DMDIR | 0777);
  842. if (f >= 0)
  843. close(f);
  844. else
  845. cantcreate(s, DMDIR);
  846. return f;
  847. }
  848. static int
  849. mkpdirs(char *s)
  850. {
  851. int err;
  852. char *p;
  853. p = s;
  854. err = 0;
  855. while (!err && (p = strchr(p+1, '/')) != nil) {
  856. *p = '\0';
  857. err = (access(s, AEXIST) < 0 && makedir(s) < 0);
  858. *p = '/';
  859. }
  860. return -err;
  861. }
  862. /* Call access but preserve the error string. */
  863. static int
  864. xaccess(char *name, int mode)
  865. {
  866. char err[ERRMAX];
  867. int rv;
  868. err[0] = 0;
  869. errstr(err, sizeof err);
  870. rv = access(name, mode);
  871. errstr(err, sizeof err);
  872. return rv;
  873. }
  874. static int
  875. openfname(Hdr *hp, char *fname, int dir, int mode)
  876. {
  877. int fd;
  878. fd = -1;
  879. cleanname(fname);
  880. switch (hp->linkflag) {
  881. case LF_LINK:
  882. case LF_SYMLINK1:
  883. case LF_SYMLINK2:
  884. fprint(2, "%s: can't make (sym)link %s\n",
  885. argv0, fname);
  886. break;
  887. case LF_FIFO:
  888. fprint(2, "%s: can't make fifo %s\n", argv0, fname);
  889. break;
  890. default:
  891. if (!keepexisting || access(fname, AEXIST) < 0) {
  892. int rw = (dir? OREAD: OWRITE);
  893. fd = create(fname, rw, mode);
  894. if (fd < 0) {
  895. mkpdirs(fname);
  896. fd = create(fname, rw, mode);
  897. }
  898. if (fd < 0 && (!dir || xaccess(fname, AEXIST) < 0))
  899. cantcreate(fname, mode);
  900. }
  901. if (fd >= 0 && verbose)
  902. fprint(2, "%s\n", fname);
  903. break;
  904. }
  905. return fd;
  906. }
  907. /* copy from archive to file system (or nowhere for table-of-contents) */
  908. static void
  909. copyfromar(int ar, int fd, char *fname, ulong blksleft, Off bytes)
  910. {
  911. int wrbytes;
  912. ulong blksread;
  913. Hdr *hbp;
  914. if (blksleft == 0 || bytes < 0)
  915. bytes = 0;
  916. for (; blksleft > 0; blksleft -= blksread) {
  917. hbp = getblkrd(ar, (fd >= 0? Alldata: Justnxthdr));
  918. if (hbp == nil)
  919. sysfatal("unexpected EOF on archive extracting %s",
  920. fname);
  921. blksread = gothowmany(blksleft);
  922. if (blksread <= 0) {
  923. fprint(2, "%s: got %ld blocks reading %s!\n",
  924. argv0, blksread, fname);
  925. blksread = 0;
  926. }
  927. wrbytes = Tblock*blksread;
  928. assert(bytes >= 0);
  929. if(wrbytes > bytes)
  930. wrbytes = bytes;
  931. assert(wrbytes >= 0);
  932. if (fd >= 0)
  933. ewrite(fname, fd, hbp->data, wrbytes);
  934. putreadblks(ar, blksread);
  935. bytes -= wrbytes;
  936. assert(bytes >= 0);
  937. }
  938. if (bytes > 0)
  939. fprint(2,
  940. "%s: %lld bytes uncopied at EOF on archive; %s not fully extracted\n",
  941. argv0, bytes, fname);
  942. }
  943. static void
  944. wrmeta(int fd, Hdr *hp, long mtime) /* update metadata */
  945. {
  946. Dir nd;
  947. nulldir(&nd);
  948. nd.mtime = mtime;
  949. dirfwstat(fd, &nd);
  950. if (isustar(hp)) {
  951. nulldir(&nd);
  952. nd.gid = hp->gname;
  953. dirfwstat(fd, &nd);
  954. }
  955. }
  956. /*
  957. * copy a file from the archive into the filesystem.
  958. * fname is result of name(), so has two extra bytes at beginning.
  959. */
  960. static void
  961. extract1(int ar, Hdr *hp, char *fname)
  962. {
  963. int fd = -1, dir = 0;
  964. long mtime = strtol(hp->mtime, nil, 8);
  965. ulong mode = strtoul(hp->mode, nil, 8) & 0777;
  966. Off bytes = hdrsize(hp); /* for printing */
  967. ulong blksleft = BYTES2TBLKS(arsize(hp));
  968. /* fiddle name, figure out mode and blocks */
  969. if (isdir(hp)) {
  970. mode |= DMDIR|0700;
  971. dir = 1;
  972. }
  973. switch (hp->linkflag) {
  974. case LF_LINK:
  975. case LF_SYMLINK1:
  976. case LF_SYMLINK2:
  977. case LF_FIFO:
  978. blksleft = 0;
  979. break;
  980. }
  981. if (relative)
  982. if(fname[0] == '/')
  983. *--fname = '.';
  984. else if(fname[0] == '#'){
  985. *--fname = '/';
  986. *--fname = '.';
  987. }
  988. if (verb == Xtract)
  989. fd = openfname(hp, fname, dir, mode);
  990. else if (verbose) {
  991. char *cp = ctime(mtime);
  992. print("%M %8lld %-12.12s %-4.4s %s\n",
  993. mode, bytes, cp+4, cp+24, fname);
  994. } else
  995. print("%s\n", fname);
  996. copyfromar(ar, fd, fname, blksleft, bytes);
  997. /* touch up meta data and close */
  998. if (fd >= 0) {
  999. /*
  1000. * directories should be wstated *after* we're done
  1001. * creating files in them, but we don't do that.
  1002. */
  1003. if (settime)
  1004. wrmeta(fd, hp, mtime);
  1005. close(fd);
  1006. }
  1007. }
  1008. static void
  1009. skip(int ar, Hdr *hp, char *fname)
  1010. {
  1011. ulong blksleft, blksread;
  1012. Hdr *hbp;
  1013. for (blksleft = BYTES2TBLKS(arsize(hp)); blksleft > 0;
  1014. blksleft -= blksread) {
  1015. hbp = getblkrd(ar, Justnxthdr);
  1016. if (hbp == nil)
  1017. sysfatal("unexpected EOF on archive extracting %s",
  1018. fname);
  1019. blksread = gothowmany(blksleft);
  1020. putreadblks(ar, blksread);
  1021. }
  1022. }
  1023. static char *
  1024. extract(char **argv)
  1025. {
  1026. int ar;
  1027. char *longname;
  1028. Hdr *hp;
  1029. Compress *comp = nil;
  1030. Pushstate ps;
  1031. if (usefile) {
  1032. ar = open(usefile, OREAD);
  1033. comp = compmethod(usefile);
  1034. } else
  1035. ar = Stdin;
  1036. if (comp)
  1037. ar = push(ar, comp->decomp, Input, &ps);
  1038. if (ar < 0)
  1039. sysfatal("can't open archive %s: %r", usefile);
  1040. while ((hp = readhdr(ar)) != nil) {
  1041. longname = name(hp);
  1042. if (match(longname, argv))
  1043. extract1(ar, hp, longname);
  1044. else
  1045. skip(ar, hp, longname);
  1046. }
  1047. if (comp)
  1048. return pushclose(&ps);
  1049. if (ar > Stderr)
  1050. close(ar);
  1051. return nil;
  1052. }
  1053. void
  1054. main(int argc, char *argv[])
  1055. {
  1056. int errflg = 0;
  1057. char *ret = nil;
  1058. fmtinstall('M', dirmodefmt);
  1059. TARGBEGIN {
  1060. case 'c':
  1061. docreate++;
  1062. verb = Replace;
  1063. break;
  1064. case 'f':
  1065. usefile = arname = EARGF(usage());
  1066. break;
  1067. case 'g':
  1068. argid = strtoul(EARGF(usage()), 0, 0);
  1069. break;
  1070. case 'i':
  1071. ignerrs = 1;
  1072. break;
  1073. case 'k':
  1074. keepexisting++;
  1075. break;
  1076. case 'm': /* compatibility */
  1077. settime = 0;
  1078. break;
  1079. case 'p':
  1080. posix++;
  1081. break;
  1082. case 'P':
  1083. posix = 0;
  1084. break;
  1085. case 'r':
  1086. verb = Replace;
  1087. break;
  1088. case 'R':
  1089. relative = 0;
  1090. break;
  1091. case 't':
  1092. verb = Toc;
  1093. break;
  1094. case 'T':
  1095. settime++;
  1096. break;
  1097. case 'u':
  1098. aruid = strtoul(EARGF(usage()), 0, 0);
  1099. break;
  1100. case 'v':
  1101. verbose++;
  1102. break;
  1103. case 'x':
  1104. verb = Xtract;
  1105. break;
  1106. case 'z':
  1107. docompress++;
  1108. break;
  1109. case '-':
  1110. break;
  1111. default:
  1112. fprint(2, "tar: unknown letter %C\n", TARGC());
  1113. errflg++;
  1114. break;
  1115. } TARGEND
  1116. if (argc < 0 || errflg)
  1117. usage();
  1118. initblks();
  1119. switch (verb) {
  1120. case Toc:
  1121. case Xtract:
  1122. ret = extract(argv);
  1123. break;
  1124. case Replace:
  1125. if (getwd(origdir, sizeof origdir) == nil)
  1126. strcpy(origdir, "/tmp");
  1127. ret = replace(argv);
  1128. break;
  1129. default:
  1130. usage();
  1131. break;
  1132. }
  1133. exits(ret);
  1134. }