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