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>
14 #define creat tar_creat
17 * modified versions of those in libc.h; scans only the first arg for
18 * keyletters and options.
21 __fixargv0(); if(!argv0) argv0=*argv; \
28 while(*_args && (_args += chartorune(&_argc, _args)))\
30 #define TARGEND SET(_argt); USED(_argt); USED(_argc); USED(_args); argc--, argv++; } \
31 USED(argv);USED(argc); }
33 #define ROUNDUP(a, b) (((a) + (b) - 1)/(b))
34 #define BYTES2TBLKS(bytes) ROUNDUP(bytes, Tblock)
37 typedef char *(*Refill)(int ar, char *bufs);
39 enum { Stdin, Stdout, Stderr };
40 enum { None, Toc, Xtract, Replace };
43 Nblock = 40, /* maximum blocksize */
44 Dblock = 20, /* default blocksize */
46 Maxpfx = 155, /* from POSIX */
47 Maxname = Namsiz + 1 + Maxpfx,
51 /* POSIX link flags */
63 /* 'A' - 'Z' are reserved for custom implementations */
66 #define islink(lf) (isreallink(lf) || issymlink(lf))
67 #define isreallink(lf) ((lf) == LF_LINK)
68 #define issymlink(lf) ((lf) == LF_SYMLINK1 || (lf) == LF_SYMLINK2)
79 char linkname[Namsiz];
81 /* rest are defined by POSIX's ustar format; see p1003.2b */
82 char magic[6]; /* "ustar" */
88 char prefix[Maxpfx]; /* if non-null, path= prefix "/" name */
101 static int nblock = Dblock;
102 static char *usefile;
103 static char origdir[Maxname*2];
104 static Hdr *tpblk, *endblk;
110 fprint(2, "usage: %s {txrc}[pPvRTugf] [archive] file1 file2...\n",
116 * block-buffer management
123 tpblk = malloc(Tblock * nblock);
124 assert(tpblk != nil);
125 endblk = tpblk + nblock;
129 refill(int ar, char *bufs)
132 unsigned bytes = Tblock * nblock;
133 static int done, first = 1;
138 /* try to size non-pipe input at first read */
139 if (first && usefile) {
141 n = read(ar, bufs, bytes);
143 sysfatal("error reading archive: %r");
145 if (i % Tblock != 0) {
146 fprint(2, "%s: archive block size (%d) error\n",
153 fprint(2, "%s: blocking = %d\n", argv0, nblock);
154 endblk = (Hdr *)bufs + nblock;
158 n = readn(ar, bufs, bytes);
160 sysfatal("unexpected EOF reading archive");
162 sysfatal("error reading archive: %r");
163 else if (n%Tblock != 0)
164 sysfatal("partial block read from archive");
167 memset(bufs + n, 0, bytes - n);
173 getblk(int ar, Refill rfp)
175 if (curblk == nil || curblk >= endblk) { /* input block exhausted? */
176 if (rfp != nil && (*rfp)(ar, (char *)tpblk) == nil)
186 return getblk(ar, refill);
192 return getblk(ar, nil);
198 Hdr *hp = getblke(ar);
201 memset(hp, 0, Tblock);
206 * how many block buffers are available, starting at the address
207 * just returned by getblk*?
212 int n = endblk - (curblk - 1);
214 return n > max? max: n;
218 * indicate that one is done with the last block obtained from getblke
219 * and it is now available to be written into the archive.
224 unsigned bytes = Tblock * nblock;
226 /* if writing end-of-archive, aid compression (good hygiene too) */
228 memset(curblk, 0, (char *)endblk - (char *)curblk);
229 if (write(ar, tpblk, bytes) != bytes)
230 sysfatal("error writing archive: %r");
236 if (curblk >= endblk)
248 putreadblks(int ar, int blks)
255 putblkmany(int ar, int blks)
265 /* modifies hp->chksum */
271 uchar *cp = (uchar*)hp;
273 memset(hp->chksum, ' ', sizeof hp->chksum);
282 return strcmp(hp->magic, "ustar") == 0;
286 * s is at most n bytes long, but need not be NUL-terminated.
287 * if shorter than n bytes, all bytes after the first NUL must also
291 strnlen(char *s, int n)
293 return s[n - 1] != '\0'? n: strlen(s);
296 /* set fullname from header */
301 static char fullname[Maxname + 1];
303 namlen = strnlen(hp->name, sizeof hp->name);
304 if (hp->prefix[0] == '\0' || !isustar(hp)) { /* old-style name? */
305 memmove(fullname, hp->name, namlen);
306 fullname[namlen] = '\0';
310 /* name is in two pieces */
311 pfxlen = strnlen(hp->prefix, sizeof hp->prefix);
312 memmove(fullname, hp->prefix, pfxlen);
313 fullname[pfxlen] = '/';
314 memmove(fullname + pfxlen + 1, hp->name, namlen);
315 fullname[pfxlen + 1 + namlen] = '\0';
322 /* the mode test is ugly but sometimes necessary */
323 return hp->linkflag == LF_DIR ||
324 strrchr(name(hp), '\0')[-1] == '/' ||
325 (strtoul(hp->mode, nil, 8)&0170000) == 040000;
331 return name(hp)[0] == '\0';
342 sysfatal("unexpected EOF instead of archive header");
343 if (eotar(hp)) /* end-of-archive block? */
345 hdrcksum = strtoul(hp->chksum, nil, 8);
346 if (chksum(hp) != hdrcksum)
347 sysfatal("bad archive header checksum: name %.64s...",
357 * if name is longer than Namsiz bytes, try to split it at a slash and fit the
358 * pieces into hp->prefix and hp->name.
361 putfullname(Hdr *hp, char *name)
365 String *slname = nil;
369 s_append(slname, name);
370 s_append(slname, "/"); /* posix requires this */
371 name = s_to_c(slname);
374 namlen = strlen(name);
375 if (namlen <= Namsiz) {
376 strncpy(hp->name, name, Namsiz);
377 hp->prefix[0] = '\0'; /* ustar paranoia */
381 if (!posix || namlen > Maxname) {
382 fprint(2, "%s: name too long for tar header: %s\n",
387 * try various splits until one results in pieces that fit into the
388 * appropriate fields of the header. look for slashes from right
389 * to left, in the hopes of putting the largest part of the name into
390 * hp->prefix, which is larger than hp->name.
392 sl = strrchr(name, '/');
395 if (pfxlen <= sizeof hp->prefix && namlen-1 - pfxlen <= Namsiz)
399 sl = strrchr(name, '/');
403 fprint(2, "%s: name can't be split to fit tar header: %s\n",
408 strncpy(hp->prefix, name, sizeof hp->prefix);
410 strncpy(hp->name, sl, sizeof hp->name);
417 mkhdr(Hdr *hp, Dir *dir, char *file)
420 * these fields run together, so we format them in order and don't use
423 sprint(hp->mode, "%6lo ", dir->mode & 0777);
424 sprint(hp->uid, "%6o ", aruid);
425 sprint(hp->gid, "%6o ", argid);
427 * files > 2 bytes can't be described
428 * (unless we resort to xustar or exustar formats).
430 if (dir->length >= (Off)1<<33) {
431 fprint(2, "%s: %s: too large for tar header format\n",
435 sprint(hp->size, "%11lluo ", dir->length);
436 sprint(hp->mtime, "%11luo ", dir->mtime);
437 hp->linkflag = (dir->mode&DMDIR? LF_DIR: LF_PLAIN1);
438 putfullname(hp, file);
440 strncpy(hp->magic, "ustar", sizeof hp->magic);
441 strncpy(hp->version, "00", sizeof hp->version);
442 strncpy(hp->uname, dir->uid, sizeof hp->uname);
443 strncpy(hp->gname, dir->gid, sizeof hp->gname);
445 sprint(hp->chksum, "%6luo", chksum(hp));
449 static void addtoar(int ar, char *file, char *shortf);
452 addtreetoar(int ar, char *file, char *shortf, int fd)
456 String *name = s_new();
458 n = dirreadall(fd, &dirents);
463 if (chdir(shortf) < 0)
464 sysfatal("chdir %s: %r", file);
466 fprint(2, "chdir %s\t# %s\n", shortf, file);
468 for (dent = dirents; dent < dirents + n; dent++) {
470 s_append(name, file);
472 s_append(name, dent->name);
473 addtoar(ar, s_to_c(name), dent->name);
479 sysfatal("chdir %s: %r", file);
481 fprint(2, "chdir ..\n");
485 addtoar(int ar, char *file, char *shortf)
489 ulong blksleft, blksread;
493 fd = open(shortf, OREAD);
495 fprint(2, "%s: can't open %s: %r\n", argv0, file);
500 sysfatal("can't fstat %s: %r", file);
503 isdir = !!(dir->qid.type&QTDIR);
504 r = mkhdr(hbp, dir, file);
513 blksleft = BYTES2TBLKS(dir->length);
517 addtreetoar(ar, file, shortf, fd);
519 for (; blksleft > 0; blksleft -= blksread) {
521 blksread = gothowmany(blksleft);
522 bytes = blksread * Tblock;
523 n = read(fd, hbp, bytes);
525 sysfatal("error reading %s: %r", file);
527 * ignore EOF. zero any partial block to aid
528 * compression and emergency recovery of data.
531 memset((char*)hbp + n, 0, bytes - n);
532 putblkmany(ar, blksread);
536 fprint(2, "%s: a %s\n", argv0, file);
544 ulong blksleft, blksread;
548 if (usefile && creat)
549 ar = create(usefile, ORDWR, 0666);
551 ar = open(usefile, ORDWR);
555 sysfatal("can't open archive %s: %r", usefile);
557 if (usefile && !creat) {
558 /* skip quickly to the end */
559 while ((hp = readhdr(ar)) != nil) {
560 bytes = (isdir(hp)? 0: strtoull(hp->size, nil, 8));
561 for (blksleft = BYTES2TBLKS(bytes);
562 blksleft > 0 && getblkrd(ar) != nil;
563 blksleft -= blksread) {
564 blksread = gothowmany(blksleft);
565 putreadblks(ar, blksread);
569 * we have just read the end-of-archive Tblock.
570 * now seek back over the (big) archive block containing it,
571 * and back up curblk ptr over end-of-archive Tblock in memory.
573 if (seek(ar, -Tblock*nblock, 1) < 0)
574 sysfatal("can't seek back over end-of-archive: %r");
578 for (i = 0; argv[i] != nil; i++)
579 addtoar(ar, argv[i], argv[i]);
581 /* write end-of-archive marker */
595 /* is pfx a file-name prefix of name? */
597 prefix(char *name, char *pfx)
599 int pfxlen = strlen(pfx);
600 char clpfx[Maxname+1];
602 if (pfxlen > Maxname)
606 return strncmp(pfx, name, pfxlen) == 0 &&
607 (name[pfxlen] == '\0' || name[pfxlen] == '/');
611 match(char *name, char **argv)
614 char clname[Maxname+1];
618 strcpy(clname, name);
620 for (i = 0; argv[i] != nil; i++)
621 if (prefix(clname, argv[i]))
631 if (access(s, AEXIST) == 0)
633 f = create(s, OREAD, DMDIR | 0777);
645 while (!done && (p = strchr(p + 1, '/')) != nil) {
647 done = (access(s, AEXIST) < 0 && makedir(s) < 0);
652 /* copy a file from the archive into the filesystem */
654 extract1(int ar, Hdr *hp, char *fname)
656 int wrbytes, fd = -1, dir = 0, okcreate = 1;
657 long mtime = strtol(hp->mtime, nil, 8);
658 ulong mode = strtoul(hp->mode, nil, 8) & 0777;
659 Off bytes = strtoll(hp->size, nil, 8);
660 ulong blksread, blksleft = BYTES2TBLKS(bytes);
668 switch (hp->linkflag) {
673 blksleft = okcreate = 0;
676 if (relative && fname[0] == '/')
678 if (verb == Xtract) {
680 switch (hp->linkflag) {
684 fprint(2, "%s: can't make (sym)link %s\n",
688 fprint(2, "%s: can't make fifo %s\n", argv0, fname);
692 fd = create(fname, (dir? OREAD: OWRITE), mode);
695 fd = create(fname, (dir? OREAD: OWRITE), mode);
696 if (fd < 0 && (!dir || access(fname, AEXIST) < 0))
697 fprint(2, "%s: can't create %s: %r\n",
700 if (fd >= 0 && verbose)
701 fprint(2, "%s: x %s\n", argv0, fname);
702 } else if (verbose) {
703 char *cp = ctime(mtime);
705 print("%M %8lld %-12.12s %-4.4s %s\n",
706 mode, bytes, cp+4, cp+24, fname);
708 print("%s\n", fname);
710 for (; blksleft > 0; blksleft -= blksread) {
713 sysfatal("unexpected EOF on archive extracting %s",
715 blksread = gothowmany(blksleft);
716 wrbytes = (bytes >= Tblock*blksread? Tblock*blksread: bytes);
717 if (fd >= 0 && write(fd, (char*)hbp, wrbytes) != wrbytes)
718 sysfatal("write error on %s: %r", fname);
719 putreadblks(ar, blksread);
724 * directories should be wstated after we're done
725 * creating files in them.
741 skip(int ar, Hdr *hp, char *fname)
744 ulong blksleft, blksread;
749 bytes = strtoull(hp->size, nil, 8);
750 blksleft = BYTES2TBLKS(bytes);
751 for (; blksleft > 0; blksleft -= blksread) {
754 sysfatal("unexpected EOF on archive extracting %s",
756 blksread = gothowmany(blksleft);
757 putreadblks(ar, blksread);
769 ar = open(usefile, OREAD);
773 sysfatal("can't open archive %s: %r", usefile);
774 while ((hp = readhdr(ar)) != nil) {
776 if (match(longname, argv))
777 extract1(ar, hp, longname);
779 skip(ar, hp, longname);
786 main(int argc, char *argv[])
792 fmtinstall('M', dirmodefmt);
800 usefile = EARGF(usage());
803 argid = strtoul(EARGF(usage()), 0, 0);
824 aruid = strtoul(EARGF(usage()), 0, 0);
839 if (argc < 0 || errflg)
849 if (getwd(origdir, sizeof origdir) == nil)
850 strcpy(origdir, "/tmp");
852 chdir(origdir); /* for profiling */