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.
19 if (!argv0) argv0 = *argv; \
26 while(*_args && (_args += chartorune(&_argc, _args)))\
28 #define TARGEND SET(_argt); USED(_argt);USED(_argc);USED(_args); \
30 USED(argv); USED(argc); }
31 #define TARGC() (_argc)
33 #define ROUNDUP(a, b) (((a) + (b) - 1)/(b))
34 #define BYTES2TBLKS(bytes) ROUNDUP(bytes, Tblock)
36 /* read big-endian binary integers; args must be (uchar *) */
37 #define G2BEBYTE(x) (((x)[0]<<8) | (x)[1])
38 #define G3BEBYTE(x) (((x)[0]<<16) | ((x)[1]<<8) | (x)[2])
39 #define G4BEBYTE(x) (((x)[0]<<24) | ((x)[1]<<16) | ((x)[2]<<8) | (x)[3])
40 #define G8BEBYTE(x) (((vlong)G4BEBYTE(x)<<32) | (u32int)G4BEBYTE((x)+4))
43 typedef char *(*Refill)(int ar, char *bufs, int justhdr);
45 enum { Stdin, Stdout, Stderr };
46 enum { Rd, Wr }; /* pipe fd-array indices */
47 enum { Output, Input };
48 enum { None, Toc, Xtract, Replace };
49 enum { Alldata, Justnxthdr };
53 Maxpfx = 155, /* from POSIX */
54 Maxname = Namsiz + 1 + Maxpfx,
55 Binsize = 0x80, /* flag in size[0], from gnu: positive binary size */
56 Binnegsz = 0xff, /* flag in size[0]: negative binary size */
58 Nblock = 40, /* maximum blocksize */
59 Dblock = 20, /* default blocksize */
63 /* POSIX link flags */
69 LF_SYMLINK2 = 's', /* 4BSD used this */
75 /* 'A' - 'Z' are reserved for custom implementations */
78 #define islink(lf) (isreallink(lf) || issymlink(lf))
79 #define isreallink(lf) ((lf) == LF_LINK)
80 #define issymlink(lf) ((lf) == LF_SYMLINK1 || (lf) == LF_SYMLINK2)
91 char linkname[Namsiz];
93 /* rest are defined by POSIX's ustar format; see p1003.2b */
94 char magic[6]; /* "ustar" */
100 char prefix[Maxpfx]; /* if non-null, path= prefix "/" name */
105 typedef struct Compress {
111 static Compress comps[] = {
112 "gzip", "gunzip", { ".tar.gz", ".tgz" }, /* default */
113 "compress", "uncompress", { ".tar.Z", ".tz" },
114 "bzip2", "bunzip2", { ".tar.bz", ".tbz",
115 ".tar.bz2",".tbz2" },
118 typedef struct Pushstate {
120 int fd; /* original fd */
121 int rfd; /* replacement fd */
126 #define OTHER(rdwr) (rdwr == Rd? Wr: Rd)
128 /* static int debug; */
130 static int posix = 1;
134 static int relative = 1;
137 static int docompress;
138 static int keepexisting;
139 static Off blkoff; /* offset of the current archive block (not Tblock) */
142 static int nblock = Dblock;
143 static char *usefile;
144 static char origdir[Maxname*2];
145 static Hdr *tpblk, *endblk;
151 fprint(2, "usage: %s {crtx}[PRTfgkmpuvz] [archive] file1 file2...\n",
159 compmethod(char *name)
161 int i, nmlen = strlen(name), sfxlen;
164 for (cp = comps; cp < comps + nelem(comps); cp++)
165 for (i = 0; i < nelem(cp->sfx) && cp->sfx[i]; i++) {
166 sfxlen = strlen(cp->sfx[i]);
167 if (nmlen > sfxlen &&
168 strcmp(cp->sfx[i], name + nmlen - sfxlen) == 0)
171 return docompress? comps: nil;
175 * push a filter, cmd, onto fd. if input, it's an input descriptor.
176 * returns a descriptor to replace fd, or -1 on error.
179 push(int fd, char *cmd, int input, Pushstate *ps)
187 if (fd < 0 || pipe(pifds) < 0)
195 dup(pifds[Wr], Stdout);
197 dup(pifds[Rd], Stdin);
198 close(pifds[input? Rd: Wr]);
199 dup(fd, (input? Stdin: Stdout));
202 s_append(s, "/bin/");
204 execl(s_to_c(s), cmd, nil);
205 sysfatal("can't exec %s: %r", cmd);
207 nfd = pifds[input? Rd: Wr];
208 close(pifds[input? Wr: Rd]);
217 pushclose(Pushstate *ps)
221 if (ps->fd < 0 || ps->rfd < 0 || !ps->open)
226 while ((wm = wait()) != nil && wm->pid != ps->kid)
228 return wm? wm->msg: nil;
232 * block-buffer management
239 tpblk = malloc(Tblock * nblock);
240 assert(tpblk != nil);
241 endblk = tpblk + nblock;
245 * (re)fill block buffers from archive. `justhdr' means we don't care
246 * about the data before the next header block.
249 refill(int ar, char *bufs, int justhdr)
252 unsigned bytes = Tblock * nblock;
253 static int done, first = 1, seekable;
259 seekable = seek(ar, 0, 1) >= 0;
260 blkoff = seek(ar, 0, 1); /* note position for `tar r' */
261 /* try to size non-pipe input at first read */
262 if (first && usefile) {
263 n = read(ar, bufs, bytes);
265 sysfatal("error reading archive: %r");
267 if (i % Tblock != 0) {
268 fprint(2, "%s: archive block size (%d) error\n",
275 fprint(2, "%s: blocking = %d\n", argv0, nblock);
276 endblk = (Hdr *)bufs + nblock;
279 } else if (justhdr && seekable && nexthdr - seek(ar, 0, 1) >= bytes) {
280 /* optimisation for huge archive members on seekable media */
281 if (seek(ar, bytes, 1) < 0)
282 sysfatal("can't seek on archive: %r");
285 n = readn(ar, bufs, bytes);
289 sysfatal("unexpected EOF reading archive");
291 sysfatal("error reading archive: %r");
292 else if (n%Tblock != 0)
293 sysfatal("partial block read from archive");
296 memset(bufs + n, 0, bytes - n);
302 getblk(int ar, Refill rfp, int justhdr)
304 if (curblk == nil || curblk >= endblk) { /* input block exhausted? */
305 if (rfp != nil && (*rfp)(ar, (char *)tpblk, justhdr) == nil)
313 getblkrd(int ar, int justhdr)
315 return getblk(ar, refill, justhdr);
321 return getblk(ar, nil, Alldata);
327 Hdr *hp = getblke(ar);
330 memset(hp, 0, Tblock);
335 * how many block buffers are available, starting at the address
336 * just returned by getblk*?
341 int n = endblk - (curblk - 1);
343 return n > max? max: n;
347 * indicate that one is done with the last block obtained from getblke
348 * and it is now available to be written into the archive.
353 unsigned bytes = Tblock * nblock;
355 /* if writing end-of-archive, aid compression (good hygiene too) */
357 memset(curblk, 0, (char *)endblk - (char *)curblk);
358 if (write(ar, tpblk, bytes) != bytes)
359 sysfatal("error writing archive: %r");
365 if (curblk >= endblk)
377 putreadblks(int ar, int blks)
384 putblkmany(int ar, int blks)
395 * modifies hp->chksum but restores it; important for the last block of the
396 * old archive when updating with `tar rf archive'
403 uchar *cp = (uchar*)hp;
404 char oldsum[sizeof hp->chksum];
406 memmove(oldsum, hp->chksum, sizeof oldsum);
407 memset(hp->chksum, ' ', sizeof hp->chksum);
410 memmove(hp->chksum, oldsum, sizeof oldsum);
417 return strcmp(hp->magic, "ustar") == 0;
421 * s is at most n bytes long, but need not be NUL-terminated.
422 * if shorter than n bytes, all bytes after the first NUL must also
426 strnlen(char *s, int n)
428 return s[n - 1] != '\0'? n: strlen(s);
431 /* set fullname from header */
436 static char fullnamebuf[2 + Maxname + 1]; /* 2 at beginning for ./ on relative names */
439 fullname = fullnamebuf+2;
440 namlen = strnlen(hp->name, sizeof hp->name);
441 if (hp->prefix[0] == '\0' || !isustar(hp)) { /* old-style name? */
442 memmove(fullname, hp->name, namlen);
443 fullname[namlen] = '\0';
447 /* name is in two pieces */
448 pfxlen = strnlen(hp->prefix, sizeof hp->prefix);
449 memmove(fullname, hp->prefix, pfxlen);
450 fullname[pfxlen] = '/';
451 memmove(fullname + pfxlen + 1, hp->name, namlen);
452 fullname[pfxlen + 1 + namlen] = '\0';
459 /* the mode test is ugly but sometimes necessary */
460 return hp->linkflag == LF_DIR ||
461 strrchr(name(hp), '\0')[-1] == '/' ||
462 (strtoul(hp->mode, nil, 8)&0170000) == 040000;
468 return name(hp)[0] == '\0';
473 getbe(uchar *src, int size)
486 putbe(uchar *dest, uvlong vl, int size)
488 for (dest += size; size-- > 0; vl >>= 8)
493 * return the nominal size from the header block, which is not always the
494 * size in the archive (the archive size may be zero for some file types
495 * regardless of the nominal size).
497 * gnu and freebsd tars are now recording vlongs as big-endian binary
498 * with a flag in byte 0 to indicate this, which permits file sizes up to
499 * 2^64-1 (actually 2^80-1 but our file sizes are vlongs) rather than 2^33-1.
506 if((uchar)hp->size[0] == Binnegsz) {
507 fprint(2, "%s: %s: negative length, which is insane\n",
510 } else if((uchar)hp->size[0] == Binsize) {
511 p = (uchar *)hp->size + sizeof hp->size - 1 -
512 sizeof(vlong); /* -1 for terminating space */
515 return strtoull(hp->size, nil, 8);
519 * return the number of bytes recorded in the archive.
524 if(isdir(hp) || islink(hp->linkflag))
535 hp = getblkrd(ar, Alldata);
537 sysfatal("unexpected EOF instead of archive header");
538 if (eotar(hp)) /* end-of-archive block? */
540 hdrcksum = strtoul(hp->chksum, nil, 8);
541 if (chksum(hp) != hdrcksum)
542 sysfatal("bad archive header checksum: name %.64s...",
544 nexthdr += Tblock*(1 + BYTES2TBLKS(arsize(hp)));
553 * if name is longer than Namsiz bytes, try to split it at a slash and fit the
554 * pieces into hp->prefix and hp->name.
557 putfullname(Hdr *hp, char *name)
561 String *slname = nil;
565 s_append(slname, name);
566 s_append(slname, "/"); /* posix requires this */
567 name = s_to_c(slname);
570 namlen = strlen(name);
571 if (namlen <= Namsiz) {
572 strncpy(hp->name, name, Namsiz);
573 hp->prefix[0] = '\0'; /* ustar paranoia */
577 if (!posix || namlen > Maxname) {
578 fprint(2, "%s: name too long for tar header: %s\n",
583 * try various splits until one results in pieces that fit into the
584 * appropriate fields of the header. look for slashes from right
585 * to left, in the hopes of putting the largest part of the name into
586 * hp->prefix, which is larger than hp->name.
588 sl = strrchr(name, '/');
591 if (pfxlen <= sizeof hp->prefix && namlen-1 - pfxlen <= Namsiz)
595 sl = strrchr(name, '/');
599 fprint(2, "%s: name can't be split to fit tar header: %s\n",
604 strncpy(hp->prefix, name, sizeof hp->prefix);
606 strncpy(hp->name, sl, sizeof hp->name);
613 mkhdr(Hdr *hp, Dir *dir, char *file)
616 * these fields run together, so we format them in order and don't use
619 sprint(hp->mode, "%6lo ", dir->mode & 0777);
620 sprint(hp->uid, "%6o ", aruid);
621 sprint(hp->gid, "%6o ", argid);
622 if (dir->length >= (Off)1<<32) {
627 fprint(2, "%s: storing large sizes in \"base 256\"\n", argv0);
629 hp->size[0] = (char)Binsize;
630 /* emit so-called `base 256' representation of size */
631 putbe((uchar *)hp->size+1, dir->length, sizeof hp->size - 2);
632 hp->size[sizeof hp->size - 1] = ' ';
634 sprint(hp->size, "%11lluo ", dir->length);
635 sprint(hp->mtime, "%11luo ", dir->mtime);
636 hp->linkflag = (dir->mode&DMDIR? LF_DIR: LF_PLAIN1);
637 putfullname(hp, file);
639 strncpy(hp->magic, "ustar", sizeof hp->magic);
640 strncpy(hp->version, "00", sizeof hp->version);
641 strncpy(hp->uname, dir->uid, sizeof hp->uname);
642 strncpy(hp->gname, dir->gid, sizeof hp->gname);
644 sprint(hp->chksum, "%6luo", chksum(hp));
648 static void addtoar(int ar, char *file, char *shortf);
651 addtreetoar(int ar, char *file, char *shortf, int fd)
655 String *name = s_new();
657 n = dirreadall(fd, &dirents);
662 if (chdir(shortf) < 0)
663 sysfatal("chdir %s: %r", file);
665 fprint(2, "chdir %s\t# %s\n", shortf, file);
667 for (dent = dirents; dent < dirents + n; dent++) {
669 s_append(name, file);
671 s_append(name, dent->name);
672 addtoar(ar, s_to_c(name), dent->name);
678 * this assumes that shortf is just one component, which is true
679 * during directory descent, but not necessarily true of command-line
680 * arguments. Our caller (or addtoar's) must reset the working
681 * directory if necessary.
684 sysfatal("chdir %s/..: %r", file);
686 fprint(2, "chdir ..\n");
690 addtoar(int ar, char *file, char *shortf)
694 ulong blksleft, blksread;
699 if (shortf[0] == '#') {
701 s_append(name, "./");
702 s_append(name, shortf);
703 shortf = s_to_c(name);
706 fd = open(shortf, OREAD);
708 fprint(2, "%s: can't open %s: %r\n", argv0, file);
715 sysfatal("can't fstat %s: %r", file);
718 isdir = !!(dir->qid.type&QTDIR);
719 if (mkhdr(hbp, dir, file) < 0) {
729 blksleft = BYTES2TBLKS(dir->length);
733 addtreetoar(ar, file, shortf, fd);
735 for (; blksleft > 0; blksleft -= blksread) {
737 blksread = gothowmany(blksleft);
738 bytes = blksread * Tblock;
739 n = readn(fd, hbp, bytes);
741 sysfatal("error reading %s: %r", file);
743 * ignore EOF. zero any partial block to aid
744 * compression and emergency recovery of data.
747 memset((uchar*)hbp + n, 0, bytes - n);
748 putblkmany(ar, blksread);
752 fprint(2, "%s\n", file);
762 ulong blksleft, blksread;
765 Compress *comp = nil;
768 if (usefile && docreate) {
769 ar = create(usefile, OWRITE, 0666);
771 comp = compmethod(usefile);
773 ar = open(usefile, ORDWR);
777 ar = push(ar, comp->comp, Output, &ps);
779 sysfatal("can't open archive %s: %r", usefile);
781 if (usefile && !docreate) {
782 /* skip quickly to the end */
783 while ((hp = readhdr(ar)) != nil) {
785 for (blksleft = BYTES2TBLKS(bytes);
786 blksleft > 0 && getblkrd(ar, Justnxthdr) != nil;
787 blksleft -= blksread) {
788 blksread = gothowmany(blksleft);
789 putreadblks(ar, blksread);
793 * we have just read the end-of-archive Tblock.
794 * now seek back over the (big) archive block containing it,
795 * and back up curblk ptr over end-of-archive Tblock in memory.
797 if (seek(ar, blkoff, 0) < 0)
798 sysfatal("can't seek back over end-of-archive: %r");
802 for (i = 0; argv[i] != nil; i++) {
803 addtoar(ar, argv[i], argv[i]);
804 chdir(origdir); /* for correctness & profiling */
807 /* write end-of-archive marker */
814 return pushclose(&ps);
824 /* is pfx a file-name prefix of name? */
826 prefix(char *name, char *pfx)
828 int pfxlen = strlen(pfx);
829 char clpfx[Maxname+1];
831 if (pfxlen > Maxname)
835 return strncmp(pfx, name, pfxlen) == 0 &&
836 (name[pfxlen] == '\0' || name[pfxlen] == '/');
840 match(char *name, char **argv)
843 char clname[Maxname+1];
847 strcpy(clname, name);
849 for (i = 0; argv[i] != nil; i++)
850 if (prefix(clname, argv[i]))
856 cantcreate(char *s, int mode)
862 * Always print about files. Only print about directories
863 * we haven't printed about. (Assumes archive is ordered
868 /* already printed this directory */
869 if(strcmp(s, last) == 0)
871 /* printed a higher directory, so printed this one */
873 if(memcmp(s, last, len) == 0 && last[len] == '/')
880 fprint(2, "%s: can't create %s: %r\n", argv0, s);
888 if (access(s, AEXIST) == 0)
890 f = create(s, OREAD, DMDIR | 0777);
894 cantcreate(s, DMDIR);
906 while (!err && (p = strchr(p+1, '/')) != nil) {
908 err = (access(s, AEXIST) < 0 && makedir(s) < 0);
914 /* Call access but preserve the error string. */
916 xaccess(char *name, int mode)
922 errstr(err, sizeof err);
923 rv = access(name, mode);
924 errstr(err, sizeof err);
928 /* copy a file from the archive into the filesystem */
929 /* fname is result of name(), so has two extra bytes at beginning */
931 extract1(int ar, Hdr *hp, char *fname)
933 int wrbytes, fd = -1, dir = 0;
934 long mtime = strtol(hp->mtime, nil, 8);
935 ulong mode = strtoul(hp->mode, nil, 8) & 0777;
936 Off bytes = hdrsize(hp); /* for printing */
937 ulong blksread, blksleft = BYTES2TBLKS(arsize(hp));
944 switch (hp->linkflag) {
955 else if(fname[0] == '#'){
960 if (verb == Xtract) {
962 switch (hp->linkflag) {
966 fprint(2, "%s: can't make (sym)link %s\n",
970 fprint(2, "%s: can't make fifo %s\n", argv0, fname);
973 if (!keepexisting || access(fname, AEXIST) < 0) {
974 int rw = (dir? OREAD: OWRITE);
976 fd = create(fname, rw, mode);
979 fd = create(fname, rw, mode);
982 (!dir || xaccess(fname, AEXIST) < 0))
983 cantcreate(fname, mode);
985 if (fd >= 0 && verbose)
986 fprint(2, "%s\n", fname);
989 } else if (verbose) {
990 char *cp = ctime(mtime);
992 print("%M %8lld %-12.12s %-4.4s %s\n",
993 mode, bytes, cp+4, cp+24, fname);
995 print("%s\n", fname);
999 for (; blksleft > 0; blksleft -= blksread) {
1000 hbp = getblkrd(ar, (fd >= 0? Alldata: Justnxthdr));
1002 sysfatal("unexpected EOF on archive extracting %s",
1004 blksread = gothowmany(blksleft);
1006 fprint(2, "%s: got %ld blocks reading %s!\n",
1007 argv0, blksread, fname);
1008 wrbytes = Tblock*blksread;
1011 if (fd >= 0 && write(fd, hbp, wrbytes) != wrbytes)
1012 sysfatal("write error on %s: %r", fname);
1013 putreadblks(ar, blksread);
1018 "%s: %lld bytes uncopied at eof; %s not fully extracted\n",
1019 argv0, bytes, fname);
1022 * directories should be wstated after we're done
1023 * creating files in them.
1042 skip(int ar, Hdr *hp, char *fname)
1044 ulong blksleft, blksread;
1047 for (blksleft = BYTES2TBLKS(arsize(hp)); blksleft > 0;
1048 blksleft -= blksread) {
1049 hbp = getblkrd(ar, Justnxthdr);
1051 sysfatal("unexpected EOF on archive extracting %s",
1053 blksread = gothowmany(blksleft);
1054 putreadblks(ar, blksread);
1059 extract(char **argv)
1064 Compress *comp = nil;
1068 ar = open(usefile, OREAD);
1069 comp = compmethod(usefile);
1073 ar = push(ar, comp->decomp, Input, &ps);
1075 sysfatal("can't open archive %s: %r", usefile);
1077 while ((hp = readhdr(ar)) != nil) {
1078 longname = name(hp);
1079 if (match(longname, argv))
1080 extract1(ar, hp, longname);
1082 skip(ar, hp, longname);
1086 return pushclose(&ps);
1093 main(int argc, char *argv[])
1098 fmtinstall('M', dirmodefmt);
1100 if(sizeof(Hdr) != Tblock)
1101 sysfatal("padding in hdr should be %d", Tblock-sizeof(Hdr)+sizeof(curblk->pad));
1108 usefile = EARGF(usage());
1111 argid = strtoul(EARGF(usage()), 0, 0);
1116 case 'm': /* compatibility */
1138 aruid = strtoul(EARGF(usage()), 0, 0);
1152 fprint(2, "tar: unknown letter %C\n", TARGC());
1157 if (argc < 0 || errflg)
1164 ret = extract(argv);
1167 if (getwd(origdir, sizeof origdir) == nil)
1168 strcpy(origdir, "/tmp");
1169 ret = replace(argv);