tar.c 22 KB

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