/*
* tar - `tape archiver', actually usable on any medium.
* POSIX "ustar" compliant when extracting, and by default when creating.
* this tar attempts to read and write multiple Tblock-byte blocks
* at once to and from the filesystem, and does not copy blocks
* around internally.
*/
typedef struct {
int kid;
int fd; /* original fd */
int rfd; /* replacement fd */
int input;
int open;
} Pushstate;
#define OTHER(rdwr) ((rdwr) == Rd? Wr: Rd)
static int fixednblock;
static int verb;
static int posix = 1;
static int docreate;
static int aruid;
static int argid;
static int relative = 1;
static int settime;
static int verbose;
static int docompress;
static int keepexisting;
static int ignerrs; /* flag: ignore i/o errors if possible */
static Off blkoff; /* offset of the current archive block (not Tblock) */
static Off nexthdr;
static Compress *
compmethod(char *name)
{
if (name) {
int i, nmlen, sfxlen;
Compress *cp;
nmlen = strlen(name);
for (cp = comps; cp < comps + nelem(comps); cp++) {
for (i = 0; i < nelem(cp->sfx) && cp->sfx[i]; i++) {
sfxlen = strlen(cp->sfx[i]);
if (nmlen > sfxlen &&
strcmp(cp->sfx[i], name + nmlen - sfxlen) == 0)
return cp;
}
}
}
return docompress? comps: nil;
}
/*
* push a filter, cmd, onto fd. if input, it's an input descriptor.
* returns a descriptor to replace fd, or -1 on error.
*/
static int
push(int fd, char *cmd, int input, Pushstate *ps)
{
int nfd, pifds[2];
String *s;
/*
* (re)fill block buffers from archive. `justhdr' means we don't care
* about the data before the next header block.
*/
static char *
refill(int ar, char *bufs, int justhdr)
{
int i, n;
unsigned bytes = Tblock * nblock;
static int done, first = 1, seekable;
if (done)
return nil;
blkoff = seek(ar, 0, 1); /* note position for `tar r' */
if (first)
seekable = blkoff >= 0;
/* try to size non-pipe input at first read */
if (first && usefile && !fixednblock) {
n = eread(arname, ar, bufs, bytes);
if (n == 0)
sysfatal("EOF reading archive %s: %r", arname);
i = n;
if (i % Tblock != 0)
sysfatal("%s: archive block size (%d) error", arname, i);
i /= Tblock;
if (i != nblock) {
nblock = i;
fprint(2, "%s: blocking = %d\n", argv0, nblock);
endblk = (Blk *)bufs + nblock;
bytes = n;
}
} else if (justhdr && seekable && nexthdr - blkoff >= bytes) {
/* optimisation for huge archive members on seekable media */
if (seek(ar, bytes, 1) < 0)
sysfatal("can't seek on archive %s: %r", arname);
n = bytes;
} else
n = ereadn(arname, ar, bufs, bytes);
first = 0;
if (n == 0)
sysfatal("unexpected EOF reading archive %s", arname);
if (n % Tblock != 0)
sysfatal("partial block read from archive %s", arname);
if (n != bytes) {
done = 1;
memset(bufs + n, 0, bytes - n);
}
return bufs;
}
if (bp != nil)
memset(bp->data, 0, Tblock);
return bp;
}
/*
* how many block buffers are available, starting at the address
* just returned by getblk*?
*/
static int
gothowmany(int max)
{
int n = endblk - (curblk - 1);
return n > max? max: n;
}
/*
* indicate that one is done with the last block obtained from getblke
* and it is now available to be written into the archive.
*/
static void
putlastblk(int ar)
{
unsigned bytes = Tblock * nblock;
/* if writing end-of-archive, aid compression (good hygiene too) */
if (curblk < endblk)
memset(curblk, 0, (char *)endblk - (char *)curblk);
ewrite(arname, ar, tpblk, bytes);
}
/*
* modifies hp->chksum but restores it; important for the last block of the
* old archive when updating with `tar rf archive'
*/
static long
chksum(Blk *bp)
{
int n = Tblock;
long i = 0;
uchar *cp = bp->data;
char oldsum[sizeof bp->chksum];
/*
* s is at most n bytes long, but need not be NUL-terminated.
* if shorter than n bytes, all bytes after the first NUL must also
* be NUL.
*/
static int
strnlen(char *s, int n)
{
return s[n - 1] != '\0'? n: strlen(s);
}
/* set fullname from header */
static char *
parsename(Blk *bp, char *buf, int nbuf)
{
int pfxlen, namlen;
/*
* cautious parsing of octal numbers as ascii strings in
* a tar header block. this is particularly important for
* trusting the checksum when trying to resync.
*/
static uvlong
hdrotoull(char *st, char *end, uvlong errval, char *name, char *field)
{
char *numb;
/*
* return the nominal size from the header block, which is not always the
* size in the archive (the archive size may be zero for some file types
* regardless of the nominal size).
*
* gnu and freebsd tars are now recording vlongs as big-endian binary
* with a flag in byte 0 to indicate this, which permits file sizes up to
* 2^64-1 (actually 2^80-1 but our file sizes are vlongs) rather than 2^33-1.
*/
static Off
hdrsize(Blk *bp)
{
uchar *p;
char buf[Maxname + 1];
if((uchar)bp->size[0] == Binnegsz) {
fprint(2, "%s: %s: negative length, which is insane\n",
argv0, parsename(bp, buf, sizeof(buf)));
return 0;
} else if((uchar)bp->size[0] == Binsize) {
p = (uchar *)bp->size + sizeof bp->size - 1 -
sizeof(vlong); /* -1 for terminating space */
return G8BEBYTE(p);
}
/*
* return the number of bytes recorded in the archive.
*/
static Off
arsize(Blk *bp, char *fname)
{
if(isdir(bp, fname) || islink(bp->linkflag))
return 0;
return hdrsize(bp);
}
static long
parsecksum(char *cksum, char *name)
{
Blk *bp;
bp = getblkrd(ar, Alldata);
if (bp == nil)
sysfatal("unexpected EOF instead of archive header in %s",
arname);
if (eotar(bp)) /* end-of-archive block? */
return nil;
hdrcksum = parsecksum(bp->chksum, parsename(bp, buf, sizeof(buf)));
if (hdrcksum == -1 || chksum(bp) != hdrcksum) {
if (!resync)
sysfatal("bad archive header checksum in %s: "
"name %.100s...; expected %#luo got %#luo",
arname, bp->name, hdrcksum, chksum(bp));
fprint(2, "%s: skipping past archive header with bad checksum in %s...",
argv0, arname);
do {
bp = getblkrd(ar, Alldata);
if (bp == nil)
sysfatal("unexpected EOF looking for archive header in %s",
arname);
hdrcksum = parsecksum(bp->chksum, parsename(bp, buf, sizeof(buf)));
} while (hdrcksum == -1 || chksum(bp) != hdrcksum);
fprint(2, "found %s\n", parsename(bp, buf, sizeof(buf)));
}
nexthdr += Tblock*(1 + BYTES2TBLKS(hdrsize(bp)));
return bp;
}
static int
getname(int ar, Blk *bp, Hdr *hdr)
{
char buf[Maxlongname+1];
ulong blksleft, blksread;
char *p;
int n;
static int
parsepax(int ar, Blk *bp, Hdr *hdr, int paxtype)
{
char *p, *lp, *le, *e, *kvp, *val, lenbuf[16];
ulong blksleft;
int n, off, len;
Blk *b;
if (!isustar(bp) || bp->linkflag != paxtype)
return 0;
off = 0;
len = -1;
kvp = nil;
lp = lenbuf;
le = lenbuf + sizeof(lenbuf);
for (blksleft = BYTES2TBLKS(hdrsize(bp)); blksleft > 0; ) {
b = getblkrd(ar, Alldata);
if (b == nil)
sysfatal("unexpected EOF on archive reading attr for %s from %s", bp->name, arname);
p = (char *)b->data;
e = (char *)b->data + sizeof(b->data);
while(p != e) {
if(*p == '\0')
break;
/*
* Copy out the length prefix. This may span a block boundary, so be
* careful about when we get the next block. The length prefix includes
* both its own length and the trailing newline. We want to trim the
* length prefix, since we already consumed it.
*/
if(len == -1){
while(p != e && *p >= '0' && *p <= '9'){
if(lp + 1 == le)
sysfatal("oversize length prefix in pax header");
*lp++ = *p++;
}
if (p == e && blksleft > 0)
goto nextblk;
if (*p++ != ' ')
sysfatal("invalid delimiter in pax header: %c (%s)\n", *p, p);
*lp++ = '\0';
len = atoi(lenbuf) - strlen(lenbuf) - 1;
}
if (kvp == nil && (kvp = malloc(len)) == nil)
sysfatal("out of memory: %r");
n = (len - off > e - p) ? e - p : len - off;
memcpy(kvp + off, p, n);
kvp[len - 1] = '\0';
off += n;
p += n;
assert(e >= p);
if (len == off) {
if ((val = matchattr(kvp, "path=")) != nil) {
free(hdr->name);
hdr->name = strdup(val);
} else if ((val = matchattr(kvp, "linkpath=")) != nil) {
/* Mostly for better error messages. */
free(hdr->name);
hdr->name = strdup(val);
} else if ((val = matchattr(kvp, "uname=")) != nil) {
free(hdr->uid);
hdr->uid = strdup(val);
} else if ((val = matchattr(kvp, "gname=")) != nil) {
free(hdr->gid);
hdr->gid = strdup(val);
} else if ((val = matchattr(kvp, "atime=")) != nil)
hdr->atime = strtoll(val, nil, 0);
else if ((val = matchattr(kvp, "mtime=")) != nil)
hdr->mtime = strtoll(val, nil, 0);
else if ((val = matchattr(kvp, "size=")) != nil)
hdr->size = strtoll(val, nil, 0);
else
if (matchattr(kvp, "comment=") == nil && matchattr(kvp, "ctime=") == nil)
fprint(2, "warning: pax attribute not supported: %s\n", kvp);
free(kvp);
kvp = nil;
lp = lenbuf;
off = 0;
len = -1;
}
}
nextblk:
blksleft--;
}
return 1;
}
static int
parsehdr(Hdr *hdr, Blk *bp)
{
int ustar;
/*
* if name is longer than Namsiz bytes, try to split it at a slash and fit the
* pieces into bp->prefix and bp->name.
*/
static int
putfullname(Blk *bp, char *name)
{
int namlen, pfxlen;
char *sl, *osl;
String *slname = nil;
if (isdir(bp, name)) {
slname = s_new();
s_append(slname, name);
s_append(slname, "/"); /* posix requires this */
name = s_to_c(slname);
}
if (!posix || namlen > Maxname) {
fprint(2, "%s: name too long for tar header: %s\n",
argv0, name);
return -1;
}
/*
* try various splits until one results in pieces that fit into the
* appropriate fields of the header. look for slashes from right
* to left, in the hopes of putting the largest part of the name into
* bp->prefix, which is larger than bp->name.
*/
sl = strrchr(name, '/');
while (sl != nil) {
pfxlen = sl - name;
if (pfxlen <= sizeof bp->prefix && namlen-1 - pfxlen <= Namsiz)
break;
osl = sl;
*osl = '\0';
sl = strrchr(name, '/');
*osl = '/';
}
if (sl == nil) {
fprint(2, "%s: name can't be split to fit tar header: %s\n",
argv0, name);
return -1;
}
*sl = '\0';
strncpy(bp->prefix, name, sizeof bp->prefix);
*sl++ = '/';
strncpy(bp->name, sl, sizeof bp->name);
if (slname)
s_free(slname);
return 0;
}
static int
mkhdr(Blk *bp, Dir *dir, char *file)
{
int r;
/*
* some of these fields run together, so we format them left-to-right
* and don't use snprint.
*/
sprint(bp->mode, "%6lo ", dir->mode & 0777);
sprint(bp->uid, "%6o ", aruid);
sprint(bp->gid, "%6o ", argid);
if (dir->length >= (Off)1<<32) {
static int printed;
/*
* this assumes that shortf is just one component, which is true
* during directory descent, but not necessarily true of command-line
* arguments. Our caller (or addtoar's) must reset the working
* directory if necessary.
*/
if (chdir("..") < 0)
sysfatal("chdir %s/..: %r", file);
if (Debug)
fprint(2, "chdir ..\n");
}
static void
addtoar(int ar, char *file, char *shortf)
{
int n, fd, isdir;
long bytes, blksread;
ulong blksleft;
Blk *hbp;
Dir *dir;
String *name = nil;
if (shortf[0] == '#') {
name = s_new();
s_append(name, "./");
s_append(name, shortf);
shortf = s_to_c(name);
}
if (Debug)
fprint(2, "opening %s # %s\n", shortf, file);
fd = open(shortf, OREAD);
if (fd < 0) {
fprint(2, "%s: can't open %s: %r\n", argv0, file);
if (name)
s_free(name);
return;
}
dir = dirfstat(fd);
if (dir == nil)
sysfatal("can't fstat %s: %r", file);
if (isdir)
addtreetoar(ar, file, shortf, fd);
else {
for (; blksleft > 0; blksleft -= blksread) {
hbp = getblke(ar);
blksread = gothowmany(blksleft);
assert(blksread >= 0);
bytes = blksread * Tblock;
n = ereadn(file, fd, hbp->data, bytes);
assert(n >= 0);
/*
* ignore EOF. zero any partial block to aid
* compression and emergency recovery of data.
*/
if (n < Tblock)
memset(hbp->data + n, 0, bytes - n);
putblkmany(ar, blksread);
}
close(fd);
if (verbose)
fprint(2, "%s\n", file);
}
if (name)
s_free(name);
}
static char *
replace(char **argv)
{
int i, ar;
ulong blksleft, blksread;
Off bytes;
Blk *bp;
Compress *comp = nil;
Pushstate ps;
if (usefile && docreate)
ar = create(usefile, OWRITE, 0666);
else if (usefile)
ar = open(usefile, ORDWR);
else
ar = Stdout;
if (docreate && docompress) {
comp = compmethod(usefile);
if (comp)
ar = push(ar, comp->comp, Output, &ps);
}
if (ar < 0)
sysfatal("can't open archive %s: %r", usefile);
if (usefile && !docreate) {
/* skip quickly to the end */
while ((bp = readhdrblk(ar)) != nil) {
bytes = hdrsize(bp);
for (blksleft = BYTES2TBLKS(bytes);
blksleft > 0 && getblkrd(ar, Justnxthdr) != nil;
blksleft -= blksread) {
blksread = gothowmany(blksleft);
putreadblks(ar, blksread);
}
}
/*
* we have just read the end-of-archive Tblock.
* now seek back over the (big) archive block containing it,
* and back up curblk ptr over end-of-archive Tblock in memory.
*/
if (seek(ar, blkoff, 0) < 0)
sysfatal("can't seek back over end-of-archive in %s: %r",
arname);
curblk--;
}
for (i = 0; argv[i] != nil; i++) {
addtoar(ar, argv[i], argv[i]);
chdir(origdir); /* for correctness & profiling */
}
/* copy from archive to file system (or nowhere for table-of-contents) */
static void
copyfromar(int ar, int fd, char *fname, ulong blksleft, Off bytes)
{
int wrbytes;
ulong blksread;
Blk *hbp;
/*
* copy a file from the archive into the filesystem.
* fname is result of getname(), so has two extra bytes at beginning.
*/
static void
extract1(int ar, Blk *bp, Hdr *hdr)
{
int fd = -1;
Off bytes = hdr->size; /* for printing */
uvlong blksleft = BYTES2TBLKS(bytes);
char *path;
/* fiddle name, figure out mode and blocks */
switch (bp->linkflag) {
case LF_LINK:
case LF_SYMLINK1:
case LF_SYMLINK2:
case LF_FIFO:
blksleft = 0;
break;
}
if((path = malloc(strlen(hdr->name) + 3)) == nil)
sysfatal("malloc: %r");
if (relative && hdr->name[0] == '/')
strcpy(path, ".");
else if(relative && hdr->name[0] == '#')
strcpy(path, "./");
else
path[0] = '\0';
strcat(path, hdr->name);
if (verb == Xtract)
fd = openfname(bp, path, hdr->mode);
else if (verbose) {
char *cp = ctime(hdr->mtime);
/* touch up meta data and close */
if (fd >= 0) {
/*
* directories should be wstated *after* we're done
* creating files in them, but we don't do that.
*/
if (settime)
wrmeta(fd, hdr);
close(fd);
}
free(path);
}
if (usefile)
ar = open(usefile, OREAD);
else
ar = Stdin;
comp = compmethod(usefile);
if (comp)
ar = push(ar, comp->decomp, Input, &ps);
if (ar < 0)
sysfatal("can't open archive %s: %r", usefile);
while ((bp = readhdr(ar, &hdr)) != nil) {
if (match(hdr.name, argv))
extract1(ar, bp, &hdr);
else
skip(ar, bp, &hdr);
freehdr(&hdr);
}
if (comp)
return pushclose(&ps);
if (ar > Stderr)
close(ar);
return nil;
}