diff options
Diffstat (limited to 'usr/src/cmd/tar/tar.c')
-rw-r--r-- | usr/src/cmd/tar/tar.c | 99 |
1 files changed, 63 insertions, 36 deletions
diff --git a/usr/src/cmd/tar/tar.c b/usr/src/cmd/tar/tar.c index d6e39db1dd..690cdf71c6 100644 --- a/usr/src/cmd/tar/tar.c +++ b/usr/src/cmd/tar/tar.c @@ -20,6 +20,7 @@ */ /* * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright 2012 Milan Jurik. All rights reserved. */ /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ @@ -548,9 +549,10 @@ static void compress_back(void); static void decompress_file(void); static pid_t uncompress_file(void); static void *compress_malloc(size_t); -static void check_compression(); -static char *bz_suffix(); -static char *gz_suffix(); +static void check_compression(void); +static char *bz_suffix(void); +static char *gz_suffix(void); +static char *xz_suffix(void); static char *add_suffix(); static void wait_pid(pid_t); @@ -572,6 +574,7 @@ static int Dflag; /* Data change flag */ static int jflag; /* flag to use 'bzip2' */ static int zflag; /* flag to use 'gzip' */ static int Zflag; /* flag to use 'compress' */ +static int Jflag; /* flag to use 'xz' */ /* Trusted Extensions */ static int Tflag; /* Trusted Extensions attr flags */ @@ -693,17 +696,21 @@ int waitaround = 0; /* wait for rendezvous with the debugger */ #define BZIP "/usr/bin/bzip2" #define GZIP "/usr/bin/gzip" #define COMPRESS "/usr/bin/compress" +#define XZ "/usr/bin/xz" #define BZCAT "/usr/bin/bzcat" #define GZCAT "/usr/bin/gzcat" #define ZCAT "/usr/bin/zcat" -#define GS 8 /* number of valid 'gzip' sufixes */ -#define BS 4 /* number of valid 'bzip2' sufixes */ +#define XZCAT "/usr/bin/xzcat" +#define GSUF 8 /* number of valid 'gzip' sufixes */ +#define BSUF 4 /* number of valid 'bzip2' sufixes */ +#define XSUF 1 /* number of valid 'xz' suffixes */ static char *compress_opt; /* compression type */ static char *gsuffix[] = {".gz", "-gz", ".z", "-z", "_z", ".Z", ".tgz", ".taz"}; static char *bsuffix[] = {".bz2", ".bz", ".tbz2", ".tbz"}; +static char *xsuffix[] = {".xz"}; static char *suffix; @@ -897,13 +904,16 @@ main(int argc, char *argv[]) pflag++; /* also set flag for ACL */ break; case 'j': /* compession "bzip2" */ - jflag++; + jflag = 1; break; case 'z': /* compression "gzip" */ - zflag++; + zflag = 1; break; case 'Z': /* compression "compress" */ - Zflag++; + Zflag = 1; + break; + case 'J': /* compression "xz" */ + Jflag = 1; break; default: (void) fprintf(stderr, gettext( @@ -919,10 +929,9 @@ main(int argc, char *argv[]) usage(); } if (cflag) { - if ((zflag && jflag) || (zflag && Zflag) || - (jflag && Zflag)) { + if ((jflag + zflag + Zflag + Jflag) > 1) { (void) fprintf(stderr, gettext( - "tar: specify only one of [jzZ] to " + "tar: specify only one of [jJzZ] to " "create a compressed file.\n")); usage(); } @@ -1004,6 +1013,9 @@ main(int argc, char *argv[]) compress_opt = compress_malloc(strlen(COMPRESS) + 1); (void) strcpy(compress_opt, COMPRESS); + } else if (Jflag) { + compress_opt = compress_malloc(strlen(XZ) + 1); + (void) strcpy(compress_opt, XZ); } } else { /* @@ -1167,7 +1179,7 @@ usage(void) #else "Usage: tar {c|r|t|u|x}[BDeEFhilmnopPTvw[0-7]][bf][X...] " #endif /* O_XATTR */ - "[j|z|Z] " + "[j|J|z|Z] " "[blocksize] [tarfile] [size] [exclude-file...] " "{file | -I include-file | -C directory file}...\n")); done(1); @@ -9174,18 +9186,16 @@ compress_back() #define GZIP_MAGIC "\037\213" #define BZIP_MAGIC "BZh" #define COMP_MAGIC "\037\235" +#define XZ_MAGIC "\375\067\172\130\132\000" void -check_compression() +check_compression(void) { - char magic[2]; - char buf[16]; + char magic[16]; FILE *fp; if ((fp = fopen(usefile, "r")) != NULL) { - (void) fread(buf, sizeof (char), 6, fp); - magic[0] = buf[0]; - magic[1] = buf[1]; + (void) fread(magic, sizeof (char), 6, fp); (void) fclose(fp); } @@ -9213,6 +9223,14 @@ check_compression() compress_opt = compress_malloc(strlen(COMPRESS) + 1); (void) strcpy(compress_opt, COMPRESS); } + } else if (memcmp(magic, XZ_MAGIC, 6) == 0) { + if (xflag || tflag) { + compress_opt = compress_malloc(strlen(XZCAT) + 1); + (void) strcpy(compress_opt, XZCAT); + } else if (uflag || rflag) { + compress_opt = compress_malloc(strlen(XZ) + 1); + (void) strcpy(compress_opt, XZ); + } } } @@ -9235,6 +9253,11 @@ add_suffix() strlcat(tfname, bsuffix[0], sizeof (tfname)); return (bsuffix[0]); } + } else if (strcmp(compress_opt, XZ) == 0) { + if ((suffix = xz_suffix()) == NULL) { + strlcat(tfname, xsuffix[0], sizeof (tfname)); + return (xsuffix[0]); + } } return (NULL); } @@ -9330,40 +9353,44 @@ uncompress_file(void) return (0); /*NOTREACHED*/ } -/* Checking valid 'bzip2' suffix */ +/* Checking suffix validity */ char * -bz_suffix() +check_suffix(char **suf, int size) { int i; int slen; int nlen = strlen(usefile); - for (i = 0; i < BS; i++) { - slen = strlen(bsuffix[i]); + for (i = 0; i < size; i++) { + slen = strlen(suf[i]); if (nlen < slen) return (NULL); - if (strcmp(usefile + nlen - slen, bsuffix[i]) == 0) - return (bsuffix[i]); + if (strcmp(usefile + nlen - slen, suf[i]) == 0) + return (suf[i]); } return (NULL); + +} + +/* Checking valid 'bzip2' suffix */ +char * +bz_suffix(void) +{ + return (check_suffix(bsuffix, BSUF)); } /* Checking valid 'gzip' suffix */ char * -gz_suffix() +gz_suffix(void) { - int i; - int slen; - int nlen = strlen(usefile); + return (check_suffix(gsuffix, GSUF)); +} - for (i = 0; i < GS; i++) { - slen = strlen(gsuffix[i]); - if (nlen < slen) - return (NULL); - if (strcmp(usefile + nlen - slen, gsuffix[i]) == 0) - return (gsuffix[i]); - } - return (NULL); +/* Checking valid 'xz' suffix */ +char * +xz_suffix(void) +{ + return (check_suffix(xsuffix, XSUF)); } void * |