tar.c 24 KB

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