tar.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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. if (n < 0)
  587. fprint(2, "%s: dirreadall %s: %r\n", argv0, file);
  588. close(fd);
  589. if (n <= 0)
  590. return;
  591. if (chdir(shortf) < 0)
  592. sysfatal("chdir %s: %r", file);
  593. if (DEBUG)
  594. fprint(2, "chdir %s\t# %s\n", shortf, file);
  595. for (dent = dirents; dent < dirents + n; dent++) {
  596. s_reset(name);
  597. s_append(name, file);
  598. s_append(name, "/");
  599. s_append(name, dent->name);
  600. addtoar(ar, s_to_c(name), dent->name);
  601. }
  602. s_free(name);
  603. free(dirents);
  604. /*
  605. * this assumes that shortf is just one component, which is true
  606. * during directory descent, but not necessarily true of command-line
  607. * arguments. Our caller (or addtoar's) must reset the working
  608. * directory if necessary.
  609. */
  610. if (chdir("..") < 0)
  611. sysfatal("chdir %s/..: %r", file);
  612. if (DEBUG)
  613. fprint(2, "chdir ..\n");
  614. }
  615. static void
  616. addtoar(int ar, char *file, char *shortf)
  617. {
  618. int n, fd, isdir;
  619. long bytes;
  620. ulong blksleft, blksread;
  621. Hdr *hbp;
  622. Dir *dir;
  623. String *name = nil;
  624. if (shortf[0] == '#') {
  625. name = s_new();
  626. s_append(name, "./");
  627. s_append(name, shortf);
  628. shortf = s_to_c(name);
  629. }
  630. fd = open(shortf, OREAD);
  631. if (fd < 0) {
  632. fprint(2, "%s: can't open %s: %r\n", argv0, file);
  633. if (name)
  634. s_free(name);
  635. return;
  636. }
  637. dir = dirfstat(fd);
  638. if (dir == nil)
  639. sysfatal("can't fstat %s: %r", file);
  640. hbp = getblkz(ar);
  641. isdir = !!(dir->qid.type&QTDIR);
  642. if (mkhdr(hbp, dir, file) < 0) {
  643. putbackblk(ar);
  644. free(dir);
  645. close(fd);
  646. if (name)
  647. s_free(name);
  648. return;
  649. }
  650. putblk(ar);
  651. blksleft = BYTES2TBLKS(dir->length);
  652. free(dir);
  653. if (isdir)
  654. addtreetoar(ar, file, shortf, fd);
  655. else {
  656. for (; blksleft > 0; blksleft -= blksread) {
  657. hbp = getblke(ar);
  658. blksread = gothowmany(blksleft);
  659. bytes = blksread * Tblock;
  660. n = readn(fd, hbp->data, bytes);
  661. if (n < 0)
  662. sysfatal("error reading %s: %r", file);
  663. /*
  664. * ignore EOF. zero any partial block to aid
  665. * compression and emergency recovery of data.
  666. */
  667. if (n < Tblock)
  668. memset(hbp->data + n, 0, bytes - n);
  669. putblkmany(ar, blksread);
  670. }
  671. close(fd);
  672. if (verbose)
  673. fprint(2, "%s\n", file);
  674. }
  675. if (name)
  676. s_free(name);
  677. }
  678. static char *
  679. replace(char **argv)
  680. {
  681. int i, ar;
  682. ulong blksleft, blksread;
  683. Off bytes;
  684. Hdr *hp;
  685. Compress *comp = nil;
  686. Pushstate ps;
  687. if (usefile && docreate) {
  688. ar = create(usefile, OWRITE, 0666);
  689. if (docompress)
  690. comp = compmethod(usefile);
  691. } else if (usefile)
  692. ar = open(usefile, ORDWR);
  693. else
  694. ar = Stdout;
  695. if (comp)
  696. ar = push(ar, comp->comp, Output, &ps);
  697. if (ar < 0)
  698. sysfatal("can't open archive %s: %r", usefile);
  699. if (usefile && !docreate) {
  700. /* skip quickly to the end */
  701. while ((hp = readhdr(ar)) != nil) {
  702. bytes = arsize(hp);
  703. for (blksleft = BYTES2TBLKS(bytes);
  704. blksleft > 0 && getblkrd(ar, Justnxthdr) != nil;
  705. blksleft -= blksread) {
  706. blksread = gothowmany(blksleft);
  707. putreadblks(ar, blksread);
  708. }
  709. }
  710. /*
  711. * we have just read the end-of-archive Tblock.
  712. * now seek back over the (big) archive block containing it,
  713. * and back up curblk ptr over end-of-archive Tblock in memory.
  714. */
  715. if (seek(ar, blkoff, 0) < 0)
  716. sysfatal("can't seek back over end-of-archive: %r");
  717. curblk--;
  718. }
  719. for (i = 0; argv[i] != nil; i++) {
  720. addtoar(ar, argv[i], argv[i]);
  721. chdir(origdir); /* for correctness & profiling */
  722. }
  723. /* write end-of-archive marker */
  724. getblkz(ar);
  725. putblk(ar);
  726. getblkz(ar);
  727. putlastblk(ar);
  728. if (comp)
  729. return pushclose(&ps);
  730. if (ar > Stderr)
  731. close(ar);
  732. return nil;
  733. }
  734. /*
  735. * tar [xt]
  736. */
  737. /* is pfx a file-name prefix of name? */
  738. static int
  739. prefix(char *name, char *pfx)
  740. {
  741. int pfxlen = strlen(pfx);
  742. char clpfx[Maxname+1];
  743. if (pfxlen > Maxname)
  744. return 0;
  745. strcpy(clpfx, pfx);
  746. cleanname(clpfx);
  747. return strncmp(pfx, name, pfxlen) == 0 &&
  748. (name[pfxlen] == '\0' || name[pfxlen] == '/');
  749. }
  750. static int
  751. match(char *name, char **argv)
  752. {
  753. int i;
  754. char clname[Maxname+1];
  755. if (argv[0] == nil)
  756. return 1;
  757. strcpy(clname, name);
  758. cleanname(clname);
  759. for (i = 0; argv[i] != nil; i++)
  760. if (prefix(clname, argv[i]))
  761. return 1;
  762. return 0;
  763. }
  764. static void
  765. cantcreate(char *s, int mode)
  766. {
  767. int len;
  768. static char *last;
  769. /*
  770. * Always print about files. Only print about directories
  771. * we haven't printed about. (Assumes archive is ordered
  772. * nicely.)
  773. */
  774. if(mode&DMDIR){
  775. if(last){
  776. /* already printed this directory */
  777. if(strcmp(s, last) == 0)
  778. return;
  779. /* printed a higher directory, so printed this one */
  780. len = strlen(s);
  781. if(memcmp(s, last, len) == 0 && last[len] == '/')
  782. return;
  783. }
  784. /* save */
  785. free(last);
  786. last = strdup(s);
  787. }
  788. fprint(2, "%s: can't create %s: %r\n", argv0, s);
  789. }
  790. static int
  791. makedir(char *s)
  792. {
  793. int f;
  794. if (access(s, AEXIST) == 0)
  795. return -1;
  796. f = create(s, OREAD, DMDIR | 0777);
  797. if (f >= 0)
  798. close(f);
  799. else
  800. cantcreate(s, DMDIR);
  801. return f;
  802. }
  803. static int
  804. mkpdirs(char *s)
  805. {
  806. int err;
  807. char *p;
  808. p = s;
  809. err = 0;
  810. while (!err && (p = strchr(p+1, '/')) != nil) {
  811. *p = '\0';
  812. err = (access(s, AEXIST) < 0 && makedir(s) < 0);
  813. *p = '/';
  814. }
  815. return -err;
  816. }
  817. /* Call access but preserve the error string. */
  818. static int
  819. xaccess(char *name, int mode)
  820. {
  821. char err[ERRMAX];
  822. int rv;
  823. err[0] = 0;
  824. errstr(err, sizeof err);
  825. rv = access(name, mode);
  826. errstr(err, sizeof err);
  827. return rv;
  828. }
  829. /* copy a file from the archive into the filesystem */
  830. /* fname is result of name(), so has two extra bytes at beginning */
  831. static void
  832. extract1(int ar, Hdr *hp, char *fname)
  833. {
  834. int wrbytes, fd = -1, dir = 0;
  835. long mtime = strtol(hp->mtime, nil, 8);
  836. ulong mode = strtoul(hp->mode, nil, 8) & 0777;
  837. Off bytes = hdrsize(hp); /* for printing */
  838. ulong blksread, blksleft = BYTES2TBLKS(arsize(hp));
  839. Hdr *hbp;
  840. if (isdir(hp)) {
  841. mode |= DMDIR|0700;
  842. dir = 1;
  843. }
  844. switch (hp->linkflag) {
  845. case LF_LINK:
  846. case LF_SYMLINK1:
  847. case LF_SYMLINK2:
  848. case LF_FIFO:
  849. blksleft = 0;
  850. break;
  851. }
  852. if (relative) {
  853. if(fname[0] == '/')
  854. *--fname = '.';
  855. else if(fname[0] == '#'){
  856. *--fname = '/';
  857. *--fname = '.';
  858. }
  859. }
  860. if (verb == Xtract) {
  861. cleanname(fname);
  862. switch (hp->linkflag) {
  863. case LF_LINK:
  864. case LF_SYMLINK1:
  865. case LF_SYMLINK2:
  866. fprint(2, "%s: can't make (sym)link %s\n",
  867. argv0, fname);
  868. break;
  869. case LF_FIFO:
  870. fprint(2, "%s: can't make fifo %s\n", argv0, fname);
  871. break;
  872. default:
  873. if (!keepexisting || access(fname, AEXIST) < 0) {
  874. int rw = (dir? OREAD: OWRITE);
  875. fd = create(fname, rw, mode);
  876. if (fd < 0) {
  877. mkpdirs(fname);
  878. fd = create(fname, rw, mode);
  879. }
  880. if (fd < 0 &&
  881. (!dir || xaccess(fname, AEXIST) < 0))
  882. cantcreate(fname, mode);
  883. }
  884. if (fd >= 0 && verbose)
  885. fprint(2, "%s\n", fname);
  886. break;
  887. }
  888. } else if (verbose) {
  889. char *cp = ctime(mtime);
  890. print("%M %8lld %-12.12s %-4.4s %s\n",
  891. mode, bytes, cp+4, cp+24, fname);
  892. } else
  893. print("%s\n", fname);
  894. if (blksleft == 0)
  895. bytes = 0;
  896. for (; blksleft > 0; blksleft -= blksread) {
  897. hbp = getblkrd(ar, (fd >= 0? Alldata: Justnxthdr));
  898. if (hbp == nil)
  899. sysfatal("unexpected EOF on archive extracting %s",
  900. fname);
  901. blksread = gothowmany(blksleft);
  902. if (blksread <= 0)
  903. fprint(2, "%s: got %ld blocks reading %s!\n",
  904. argv0, blksread, fname);
  905. wrbytes = Tblock*blksread;
  906. if(wrbytes > bytes)
  907. wrbytes = bytes;
  908. if (fd >= 0 && write(fd, hbp->data, wrbytes) != wrbytes)
  909. sysfatal("write error on %s: %r", fname);
  910. putreadblks(ar, blksread);
  911. bytes -= wrbytes;
  912. }
  913. if (bytes > 0)
  914. fprint(2,
  915. "%s: %lld bytes uncopied at eof; %s not fully extracted\n",
  916. argv0, bytes, fname);
  917. if (fd >= 0) {
  918. /*
  919. * directories should be wstated after we're done
  920. * creating files in them.
  921. */
  922. if (settime) {
  923. Dir nd;
  924. nulldir(&nd);
  925. nd.mtime = mtime;
  926. dirfwstat(fd, &nd);
  927. if (isustar(hp)) {
  928. nulldir(&nd);
  929. nd.gid = hp->gname;
  930. dirfwstat(fd, &nd);
  931. }
  932. }
  933. close(fd);
  934. }
  935. }
  936. static void
  937. skip(int ar, Hdr *hp, char *fname)
  938. {
  939. ulong blksleft, blksread;
  940. Hdr *hbp;
  941. for (blksleft = BYTES2TBLKS(arsize(hp)); blksleft > 0;
  942. blksleft -= blksread) {
  943. hbp = getblkrd(ar, Justnxthdr);
  944. if (hbp == nil)
  945. sysfatal("unexpected EOF on archive extracting %s",
  946. fname);
  947. blksread = gothowmany(blksleft);
  948. putreadblks(ar, blksread);
  949. }
  950. }
  951. static char *
  952. extract(char **argv)
  953. {
  954. int ar;
  955. char *longname;
  956. Hdr *hp;
  957. Compress *comp = nil;
  958. Pushstate ps;
  959. if (usefile) {
  960. ar = open(usefile, OREAD);
  961. comp = compmethod(usefile);
  962. } else
  963. ar = Stdin;
  964. if (comp)
  965. ar = push(ar, comp->decomp, Input, &ps);
  966. if (ar < 0)
  967. sysfatal("can't open archive %s: %r", usefile);
  968. while ((hp = readhdr(ar)) != nil) {
  969. longname = name(hp);
  970. if (match(longname, argv))
  971. extract1(ar, hp, longname);
  972. else
  973. skip(ar, hp, longname);
  974. }
  975. if (comp)
  976. return pushclose(&ps);
  977. if (ar > Stderr)
  978. close(ar);
  979. return nil;
  980. }
  981. void
  982. main(int argc, char *argv[])
  983. {
  984. int errflg = 0;
  985. char *ret = nil;
  986. fmtinstall('M', dirmodefmt);
  987. TARGBEGIN {
  988. case 'c':
  989. docreate++;
  990. verb = Replace;
  991. break;
  992. case 'f':
  993. usefile = EARGF(usage());
  994. break;
  995. case 'g':
  996. argid = strtoul(EARGF(usage()), 0, 0);
  997. break;
  998. case 'k':
  999. keepexisting++;
  1000. break;
  1001. case 'm': /* compatibility */
  1002. settime = 0;
  1003. break;
  1004. case 'p':
  1005. posix++;
  1006. break;
  1007. case 'P':
  1008. posix = 0;
  1009. break;
  1010. case 'r':
  1011. verb = Replace;
  1012. break;
  1013. case 'R':
  1014. relative = 0;
  1015. break;
  1016. case 't':
  1017. verb = Toc;
  1018. break;
  1019. case 'T':
  1020. settime++;
  1021. break;
  1022. case 'u':
  1023. aruid = strtoul(EARGF(usage()), 0, 0);
  1024. break;
  1025. case 'v':
  1026. verbose++;
  1027. break;
  1028. case 'x':
  1029. verb = Xtract;
  1030. break;
  1031. case 'z':
  1032. docompress++;
  1033. break;
  1034. case '-':
  1035. break;
  1036. default:
  1037. fprint(2, "tar: unknown letter %C\n", TARGC());
  1038. errflg++;
  1039. break;
  1040. } TARGEND
  1041. if (argc < 0 || errflg)
  1042. usage();
  1043. initblks();
  1044. switch (verb) {
  1045. case Toc:
  1046. case Xtract:
  1047. ret = extract(argv);
  1048. break;
  1049. case Replace:
  1050. if (getwd(origdir, sizeof origdir) == nil)
  1051. strcpy(origdir, "/tmp");
  1052. ret = replace(argv);
  1053. break;
  1054. default:
  1055. usage();
  1056. break;
  1057. }
  1058. exits(ret);
  1059. }