tar.c 27 KB

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