tar: make z flag work, even when no file name was provided (thanks aiju)

tar used to infer compression type from the filenames extension, but when
no file name is given (stdin/stdout), the -z flag was ignored and no
compression filter applied. this changes tar to assume the default
gzip compression method when z is given and no file name is specified.
This commit is contained in:
cinap_lenrek 2015-09-08 18:27:48 +02:00
parent 4d4b825dea
commit c7eae3fb72

View file

@ -222,16 +222,20 @@ ewrite(char *name, int fd, void *buf, long len)
static Compress * static Compress *
compmethod(char *name) compmethod(char *name)
{ {
int i, nmlen = strlen(name), sfxlen; if (name) {
int i, nmlen, sfxlen;
Compress *cp; Compress *cp;
for (cp = comps; cp < comps + nelem(comps); cp++) nmlen = strlen(name);
for (cp = comps; cp < comps + nelem(comps); cp++) {
for (i = 0; i < nelem(cp->sfx) && cp->sfx[i]; i++) { for (i = 0; i < nelem(cp->sfx) && cp->sfx[i]; i++) {
sfxlen = strlen(cp->sfx[i]); sfxlen = strlen(cp->sfx[i]);
if (nmlen > sfxlen && if (nmlen > sfxlen &&
strcmp(cp->sfx[i], name + nmlen - sfxlen) == 0) strcmp(cp->sfx[i], name + nmlen - sfxlen) == 0)
return cp; return cp;
} }
}
}
return docompress? comps: nil; return docompress? comps: nil;
} }
@ -880,14 +884,15 @@ replace(char **argv)
if (usefile && docreate) { if (usefile && docreate) {
ar = create(usefile, OWRITE, 0666); ar = create(usefile, OWRITE, 0666);
if (docompress)
comp = compmethod(usefile);
} else if (usefile) } else if (usefile)
ar = open(usefile, ORDWR); ar = open(usefile, ORDWR);
else else
ar = Stdout; ar = Stdout;
if (docreate && docompress) {
comp = compmethod(usefile);
if (comp) if (comp)
ar = push(ar, comp->comp, Output, &ps); ar = push(ar, comp->comp, Output, &ps);
}
if (ar < 0) if (ar < 0)
sysfatal("can't open archive %s: %r", usefile); sysfatal("can't open archive %s: %r", usefile);
@ -1253,14 +1258,14 @@ extract(char **argv)
int ar; int ar;
char *longname; char *longname;
Hdr *hp; Hdr *hp;
Compress *comp = nil; Compress *comp;
Pushstate ps; Pushstate ps;
if (usefile) { if (usefile)
ar = open(usefile, OREAD); ar = open(usefile, OREAD);
comp = compmethod(usefile); else
} else
ar = Stdin; ar = Stdin;
comp = compmethod(usefile);
if (comp) if (comp)
ar = push(ar, comp->decomp, Input, &ps); ar = push(ar, comp->decomp, Input, &ps);
if (ar < 0) if (ar < 0)