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
11 #include <fcall.h> /* for %M */
12 #include <libString.h>
15 * modified versions of those in libc.h; scans only the first arg for
16 * keyletters and options.
27 while(*_args && (_args += chartorune(&_argc, _args)))\
29 #define TARGEND SET(_argt); USED(_argt);USED(_argc);USED(_args); \
31 USED(argv); USED(argc); }
32 #define TARGC() (_argc)
34 #define ROUNDUP(a, b) (((a) + (b) - 1)/(b))
35 #define BYTES2TBLKS(bytes) ROUNDUP(bytes, Tblock)
38 typedef char *(*Refill)(int ar, char *bufs, int justhdr);
40 enum { Stdin, Stdout, Stderr };
41 enum { Rd, Wr }; /* pipe fd-array indices */
42 enum { Output, Input };
43 enum { None, Toc, Xtract, Replace };
44 enum { Alldata, Justnxthdr };
47 Nblock = 40, /* maximum blocksize */
48 Dblock = 20, /* default blocksize */
50 Maxpfx = 155, /* from POSIX */
51 Maxname = Namsiz + 1 + Maxpfx,
55 /* POSIX link flags */
67 /* 'A' - 'Z' are reserved for custom implementations */
70 #define islink(lf) (isreallink(lf) || issymlink(lf))
71 #define isreallink(lf) ((lf) == LF_LINK)
72 #define issymlink(lf) ((lf) == LF_SYMLINK1 || (lf) == LF_SYMLINK2)
83 char linkname[Namsiz];
85 /* rest are defined by POSIX's ustar format; see p1003.2b */
86 char magic[6]; /* "ustar" */
92 char prefix[Maxpfx]; /* if non-null, path= prefix "/" name */
101 static Compress comps[] = {
102 "gzip", "gunzip", { ".tar.gz", ".tgz" }, /* default */
103 "compress", "uncompress", { ".tar.Z", ".tz" },
104 "bzip2", "bunzip2", { ".tar.bz", ".tbz",
105 ".tar.bz2",".tbz2" },
110 int fd; /* original fd */
111 int rfd; /* replacement fd */
116 #define OTHER(rdwr) (rdwr == Rd? Wr: Rd)
118 /* static int debug; */
120 static int posix = 1;
124 static int relative = 1;
127 static int docompress;
128 static int keepexisting;
129 static Off blkoff; /* offset of the current archive block (not Tblock) */
132 static int nblock = Dblock;
133 static char *usefile;
134 static char origdir[Maxname*2];
135 static Hdr *tpblk, *endblk;
141 fprint(2, "usage: %s {crtx}[PRTfgkmpuvz] [archive] file1 file2...\n",
149 compmethod(char *name)
151 int i, nmlen = strlen(name), sfxlen;
154 for (cp = comps; cp < comps + nelem(comps); cp++)
155 for (i = 0; i < nelem(cp->sfx) && cp->sfx[i]; i++) {
156 sfxlen = strlen(cp->sfx[i]);
157 if (nmlen > sfxlen &&
158 strcmp(cp->sfx[i], name + nmlen - sfxlen) == 0)
161 return docompress? comps: nil;
165 * push a filter, cmd, onto fd. if input, it's an input descriptor.
166 * returns a descriptor to replace fd, or -1 on error.
169 push(int fd, char *cmd, int input, Pushstate *ps)
177 if (fd < 0 || pipe(pifds) < 0)
185 dup(pifds[Wr], Stdout);
187 dup(pifds[Rd], Stdin);
188 close(pifds[input? Rd: Wr]);
189 dup(fd, (input? Stdin: Stdout));
192 s_append(s, "/bin/");
194 execl(s_to_c(s), cmd, nil);
195 sysfatal("can't exec %s: %r", cmd);
197 nfd = pifds[input? Rd: Wr];
198 close(pifds[input? Wr: Rd]);
207 pushclose(Pushstate *ps)
211 if (ps->fd < 0 || ps->rfd < 0 || !ps->open)
216 while ((wm = wait()) != nil && wm->pid != ps->kid)
218 return wm? wm->msg: nil;
222 * block-buffer management
229 tpblk = malloc(Tblock * nblock);
230 assert(tpblk != nil);
231 endblk = tpblk + nblock;
235 * (re)fill block buffers from archive. `justhdr' means we don't care
236 * about the data before the next header block.
239 refill(int ar, char *bufs, int justhdr)
242 unsigned bytes = Tblock * nblock;
243 static int done, first = 1, seekable;
249 seekable = seek(ar, 0, 1) >= 0;
250 /* try to size non-pipe input at first read */
251 if (first && usefile) {
252 blkoff = seek(ar, 0, 1); /* note position */
253 n = read(ar, bufs, bytes);
255 sysfatal("error reading archive: %r");
257 if (i % Tblock != 0) {
258 fprint(2, "%s: archive block size (%d) error\n",
265 fprint(2, "%s: blocking = %d\n", argv0, nblock);
266 endblk = (Hdr *)bufs + nblock;
269 } else if (justhdr && seekable && nexthdr - seek(ar, 0, 1) >= bytes) {
270 /* optimisation for huge archive members on seekable media */
271 if (seek(ar, bytes, 1) < 0)
272 sysfatal("can't seek on archive: %r");
275 n = readn(ar, bufs, bytes);
279 sysfatal("unexpected EOF reading archive");
281 sysfatal("error reading archive: %r");
282 else if (n%Tblock != 0)
283 sysfatal("partial block read from archive");
286 memset(bufs + n, 0, bytes - n);
292 getblk(int ar, Refill rfp, int justhdr)
294 if (curblk == nil || curblk >= endblk) { /* input block exhausted? */
295 if (rfp != nil && (*rfp)(ar, (char *)tpblk, justhdr) == nil)
303 getblkrd(int ar, int justhdr)
305 return getblk(ar, refill, justhdr);
311 return getblk(ar, nil, Alldata);
317 Hdr *hp = getblke(ar);
320 memset(hp, 0, Tblock);
325 * how many block buffers are available, starting at the address
326 * just returned by getblk*?
331 int n = endblk - (curblk - 1);
333 return n > max? max: n;
337 * indicate that one is done with the last block obtained from getblke
338 * and it is now available to be written into the archive.
343 unsigned bytes = Tblock * nblock;
345 /* if writing end-of-archive, aid compression (good hygiene too) */
347 memset(curblk, 0, (char *)endblk - (char *)curblk);
348 if (write(ar, tpblk, bytes) != bytes)
349 sysfatal("error writing archive: %r");
355 if (curblk >= endblk)
367 putreadblks(int ar, int blks)
374 putblkmany(int ar, int blks)
385 * modifies hp->chksum but restores it; important for the last block of the
386 * old archive when updating with `tar rf archive'
393 uchar *cp = (uchar*)hp;
394 char oldsum[sizeof hp->chksum];
396 memmove(oldsum, hp->chksum, sizeof oldsum);
397 memset(hp->chksum, ' ', sizeof hp->chksum);
400 memmove(hp->chksum, oldsum, sizeof oldsum);
407 return strcmp(hp->magic, "ustar") == 0;
411 * s is at most n bytes long, but need not be NUL-terminated.
412 * if shorter than n bytes, all bytes after the first NUL must also
416 strnlen(char *s, int n)
418 return s[n - 1] != '\0'? n: strlen(s);
421 /* set fullname from header */
426 static char fullnamebuf[2 + Maxname + 1]; /* 2 at beginning for ./ on relative names */
429 fullname = fullnamebuf+2;
430 namlen = strnlen(hp->name, sizeof hp->name);
431 if (hp->prefix[0] == '\0' || !isustar(hp)) { /* old-style name? */
432 memmove(fullname, hp->name, namlen);
433 fullname[namlen] = '\0';
437 /* name is in two pieces */
438 pfxlen = strnlen(hp->prefix, sizeof hp->prefix);
439 memmove(fullname, hp->prefix, pfxlen);
440 fullname[pfxlen] = '/';
441 memmove(fullname + pfxlen + 1, hp->name, namlen);
442 fullname[pfxlen + 1 + namlen] = '\0';
449 /* the mode test is ugly but sometimes necessary */
450 return hp->linkflag == LF_DIR ||
451 strrchr(name(hp), '\0')[-1] == '/' ||
452 (strtoul(hp->mode, nil, 8)&0170000) == 040000;
458 return name(hp)[0] == '\0';
464 Off bytes = strtoull(hp->size, nil, 8);
477 hp = getblkrd(ar, Alldata);
479 sysfatal("unexpected EOF instead of archive header");
480 if (eotar(hp)) /* end-of-archive block? */
482 hdrcksum = strtoul(hp->chksum, nil, 8);
483 if (chksum(hp) != hdrcksum)
484 sysfatal("bad archive header checksum: name %.64s...",
486 nexthdr += Tblock*(1 + BYTES2TBLKS(hdrsize(hp)));
495 * if name is longer than Namsiz bytes, try to split it at a slash and fit the
496 * pieces into hp->prefix and hp->name.
499 putfullname(Hdr *hp, char *name)
503 String *slname = nil;
507 s_append(slname, name);
508 s_append(slname, "/"); /* posix requires this */
509 name = s_to_c(slname);
512 namlen = strlen(name);
513 if (namlen <= Namsiz) {
514 strncpy(hp->name, name, Namsiz);
515 hp->prefix[0] = '\0'; /* ustar paranoia */
519 if (!posix || namlen > Maxname) {
520 fprint(2, "%s: name too long for tar header: %s\n",
525 * try various splits until one results in pieces that fit into the
526 * appropriate fields of the header. look for slashes from right
527 * to left, in the hopes of putting the largest part of the name into
528 * hp->prefix, which is larger than hp->name.
530 sl = strrchr(name, '/');
533 if (pfxlen <= sizeof hp->prefix && namlen-1 - pfxlen <= Namsiz)
537 sl = strrchr(name, '/');
541 fprint(2, "%s: name can't be split to fit tar header: %s\n",
546 strncpy(hp->prefix, name, sizeof hp->prefix);
548 strncpy(hp->name, sl, sizeof hp->name);
555 mkhdr(Hdr *hp, Dir *dir, char *file)
558 * these fields run together, so we format them in order and don't use
561 sprint(hp->mode, "%6lo ", dir->mode & 0777);
562 sprint(hp->uid, "%6o ", aruid);
563 sprint(hp->gid, "%6o ", argid);
565 * files > 2 bytes can't be described
566 * (unless we resort to xustar or exustar formats).
568 if (dir->length >= (Off)1<<33) {
569 fprint(2, "%s: %s: too large for tar header format\n",
573 sprint(hp->size, "%11lluo ", dir->length);
574 sprint(hp->mtime, "%11luo ", dir->mtime);
575 hp->linkflag = (dir->mode&DMDIR? LF_DIR: LF_PLAIN1);
576 putfullname(hp, file);
578 strncpy(hp->magic, "ustar", sizeof hp->magic);
579 strncpy(hp->version, "00", sizeof hp->version);
580 strncpy(hp->uname, dir->uid, sizeof hp->uname);
581 strncpy(hp->gname, dir->gid, sizeof hp->gname);
583 sprint(hp->chksum, "%6luo", chksum(hp));
587 static void addtoar(int ar, char *file, char *shortf);
590 addtreetoar(int ar, char *file, char *shortf, int fd)
594 String *name = s_new();
596 n = dirreadall(fd, &dirents);
601 if (chdir(shortf) < 0)
602 sysfatal("chdir %s: %r", file);
604 fprint(2, "chdir %s\t# %s\n", shortf, file);
606 for (dent = dirents; dent < dirents + n; dent++) {
608 s_append(name, file);
610 s_append(name, dent->name);
611 addtoar(ar, s_to_c(name), dent->name);
617 * this assumes that shortf is just one component, which is true
618 * during directory descent, but not necessarily true of command-line
619 * arguments. Our caller (or addtoar's) must reset the working
620 * directory if necessary.
623 sysfatal("chdir %s/..: %r", file);
625 fprint(2, "chdir ..\n");
629 addtoar(int ar, char *file, char *shortf)
633 ulong blksleft, blksread;
637 fd = open(shortf, OREAD);
639 fprint(2, "%s: can't open %s: %r\n", argv0, file);
644 sysfatal("can't fstat %s: %r", file);
647 isdir = !!(dir->qid.type&QTDIR);
648 if (mkhdr(hbp, dir, file) < 0) {
656 blksleft = BYTES2TBLKS(dir->length);
660 addtreetoar(ar, file, shortf, fd);
662 for (; blksleft > 0; blksleft -= blksread) {
664 blksread = gothowmany(blksleft);
665 bytes = blksread * Tblock;
666 n = readn(fd, hbp, bytes);
668 sysfatal("error reading %s: %r", file);
670 * ignore EOF. zero any partial block to aid
671 * compression and emergency recovery of data.
674 memset((uchar*)hbp + n, 0, bytes - n);
675 putblkmany(ar, blksread);
679 fprint(2, "%s\n", file);
687 ulong blksleft, blksread;
690 Compress *comp = nil;
693 if (usefile && docreate) {
694 ar = create(usefile, OWRITE, 0666);
696 comp = compmethod(usefile);
698 ar = open(usefile, ORDWR);
702 ar = push(ar, comp->comp, Output, &ps);
704 sysfatal("can't open archive %s: %r", usefile);
706 if (usefile && !docreate) {
707 /* skip quickly to the end */
708 while ((hp = readhdr(ar)) != nil) {
710 for (blksleft = BYTES2TBLKS(bytes);
711 blksleft > 0 && getblkrd(ar, Justnxthdr) != nil;
712 blksleft -= blksread) {
713 blksread = gothowmany(blksleft);
714 putreadblks(ar, blksread);
718 * we have just read the end-of-archive Tblock.
719 * now seek back over the (big) archive block containing it,
720 * and back up curblk ptr over end-of-archive Tblock in memory.
722 if (seek(ar, blkoff, 0) < 0)
723 sysfatal("can't seek back over end-of-archive: %r");
727 for (i = 0; argv[i] != nil; i++) {
728 addtoar(ar, argv[i], argv[i]);
729 chdir(origdir); /* for correctness & profiling */
732 /* write end-of-archive marker */
739 return pushclose(&ps);
749 /* is pfx a file-name prefix of name? */
751 prefix(char *name, char *pfx)
753 int pfxlen = strlen(pfx);
754 char clpfx[Maxname+1];
756 if (pfxlen > Maxname)
760 return strncmp(pfx, name, pfxlen) == 0 &&
761 (name[pfxlen] == '\0' || name[pfxlen] == '/');
765 match(char *name, char **argv)
768 char clname[Maxname+1];
772 strcpy(clname, name);
774 for (i = 0; argv[i] != nil; i++)
775 if (prefix(clname, argv[i]))
785 if (access(s, AEXIST) == 0)
787 f = create(s, OREAD, DMDIR | 0777);
799 while (!done && (p = strchr(p + 1, '/')) != nil) {
801 done = (access(s, AEXIST) < 0 && makedir(s) < 0);
806 /* copy a file from the archive into the filesystem */
807 /* fname is result of name(), so has two extra bytes at beginning */
809 extract1(int ar, Hdr *hp, char *fname)
811 int wrbytes, fd = -1, dir = 0;
812 long mtime = strtol(hp->mtime, nil, 8);
813 ulong mode = strtoul(hp->mode, nil, 8) & 0777;
814 Off bytes = strtoll(hp->size, nil, 8); /* for printing */
815 ulong blksread, blksleft = BYTES2TBLKS(hdrsize(hp));
822 switch (hp->linkflag) {
833 else if(fname[0] == '#'){
838 if (verb == Xtract) {
840 switch (hp->linkflag) {
844 fprint(2, "%s: can't make (sym)link %s\n",
848 fprint(2, "%s: can't make fifo %s\n", argv0, fname);
851 if (!keepexisting || access(fname, AEXIST) < 0) {
852 int rw = (dir? OREAD: OWRITE);
854 fd = create(fname, rw, mode);
857 fd = create(fname, rw, mode);
860 (!dir || access(fname, AEXIST) < 0))
861 fprint(2, "%s: can't create %s: %r\n",
864 if (fd >= 0 && verbose)
865 fprint(2, "%s\n", fname);
868 } else if (verbose) {
869 char *cp = ctime(mtime);
871 print("%M %8lld %-12.12s %-4.4s %s\n",
872 mode, bytes, cp+4, cp+24, fname);
874 print("%s\n", fname);
876 for (; blksleft > 0; blksleft -= blksread) {
877 hbp = getblkrd(ar, (fd >= 0? Alldata: Justnxthdr));
879 sysfatal("unexpected EOF on archive extracting %s",
881 blksread = gothowmany(blksleft);
882 wrbytes = Tblock*blksread;
885 if (fd >= 0 && write(fd, hbp, wrbytes) != wrbytes)
886 sysfatal("write error on %s: %r", fname);
887 putreadblks(ar, blksread);
892 * directories should be wstated after we're done
893 * creating files in them.
909 skip(int ar, Hdr *hp, char *fname)
911 ulong blksleft, blksread;
914 for (blksleft = BYTES2TBLKS(hdrsize(hp)); blksleft > 0;
915 blksleft -= blksread) {
916 hbp = getblkrd(ar, Justnxthdr);
918 sysfatal("unexpected EOF on archive extracting %s",
920 blksread = gothowmany(blksleft);
921 putreadblks(ar, blksread);
931 Compress *comp = nil;
935 ar = open(usefile, OREAD);
936 comp = compmethod(usefile);
940 ar = push(ar, comp->decomp, Input, &ps);
942 sysfatal("can't open archive %s: %r", usefile);
944 while ((hp = readhdr(ar)) != nil) {
946 if (match(longname, argv))
947 extract1(ar, hp, longname);
949 skip(ar, hp, longname);
953 return pushclose(&ps);
960 main(int argc, char *argv[])
966 fmtinstall('M', dirmodefmt);
974 usefile = EARGF(usage());
977 argid = strtoul(EARGF(usage()), 0, 0);
982 case 'm': /* compatibility */
1004 aruid = strtoul(EARGF(usage()), 0, 0);
1018 fprint(2, "tar: unknown letter %C\n", TARGC());
1023 if (argc < 0 || errflg)
1030 ret = extract(argv);
1033 if (getwd(origdir, sizeof origdir) == nil)
1034 strcpy(origdir, "/tmp");
1035 ret = replace(argv);