summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMilan Jurik <milan.jurik@xylab.cz>2012-06-05 10:09:55 -0700
committerMilan Jurik <milan.jurik@xylab.cz>2012-06-05 10:09:55 -0700
commitaa7166bdd5adefed25e8be2f08d7228e5add7f24 (patch)
tree112855848f5c30088fcbc15380bea3b64b86575e
parentec8accef1ba1bafa7937389d8446d882c70a8865 (diff)
downloadillumos-joyent-aa7166bdd5adefed25e8be2f08d7228e5add7f24.tar.gz
2730 tar misbehaves if compress utility missing
2731 add -a --auto feature to tar for gtar comptability Reviewed by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net> Reviewed by: Richard Lowe <richlowe@richlowe.net> Approved by: Garrett D'Amore <garrett@damore.org>
-rw-r--r--usr/src/cmd/tar/tar.c45
-rw-r--r--usr/src/man/man1/tar.113
2 files changed, 54 insertions, 4 deletions
diff --git a/usr/src/cmd/tar/tar.c b/usr/src/cmd/tar/tar.c
index 690cdf71c6..6cb084e9d0 100644
--- a/usr/src/cmd/tar/tar.c
+++ b/usr/src/cmd/tar/tar.c
@@ -555,6 +555,8 @@ static char *gz_suffix(void);
static char *xz_suffix(void);
static char *add_suffix();
static void wait_pid(pid_t);
+static void verify_compress_opt(const char *t);
+static void detect_compress(void);
static struct stat stbuf;
@@ -575,6 +577,7 @@ 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' */
+static int aflag; /* flag to use autocompression */
/* Trusted Extensions */
static int Tflag; /* Trusted Extensions attr flags */
@@ -915,6 +918,9 @@ main(int argc, char *argv[])
case 'J': /* compression "xz" */
Jflag = 1;
break;
+ case 'a':
+ aflag = 1; /* autocompression */
+ break;
default:
(void) fprintf(stderr, gettext(
"tar: %c: unknown function modifier\n"), *cp);
@@ -929,9 +935,9 @@ main(int argc, char *argv[])
usage();
}
if (cflag) {
- if ((jflag + zflag + Zflag + Jflag) > 1) {
+ if ((jflag + zflag + Zflag + Jflag + aflag) > 1) {
(void) fprintf(stderr, gettext(
- "tar: specify only one of [jJzZ] to "
+ "tar: specify only one of [ajJzZ] to "
"create a compressed file.\n"));
usage();
}
@@ -1001,6 +1007,9 @@ main(int argc, char *argv[])
if (rflag) {
if (cflag && usefile != NULL) {
/* Set the compression type */
+ if (aflag)
+ detect_compress();
+
if (jflag) {
compress_opt = compress_malloc(strlen(BZIP)
+ 1);
@@ -9170,6 +9179,7 @@ compress_back()
usefile, compress_opt);
}
if ((pid = fork()) == 0) {
+ verify_compress_opt(compress_opt);
(void) execlp(compress_opt, compress_opt,
usefile, NULL);
} else if (pid == -1) {
@@ -9283,6 +9293,7 @@ decompress_file(void)
gettext("Decompressing '%s' with "
"'%s'...\n"), usefile, compress_opt);
}
+ verify_compress_opt(compress_opt);
(void) execlp(compress_opt, compress_opt, "-df",
tfname, NULL);
vperror(1, gettext("Could not exec %s"), compress_opt);
@@ -9320,6 +9331,7 @@ compress_file(void)
(void) dup2(fd[0], STDIN_FILENO);
(void) close(fd[1]);
(void) dup2(mt, STDOUT_FILENO);
+ verify_compress_opt(compress_opt);
(void) execlp(compress_opt, compress_opt, NULL);
vperror(1, gettext("Could not exec %s"), compress_opt);
return (0); /*NOTREACHED*/
@@ -9348,6 +9360,7 @@ uncompress_file(void)
(void) dup2(fd[1], STDOUT_FILENO);
(void) close(fd[0]);
(void) dup2(mt, STDIN_FILENO);
+ verify_compress_opt(compress_opt);
(void) execlp(compress_opt, compress_opt, NULL);
vperror(1, gettext("Could not exec %s"), compress_opt);
return (0); /*NOTREACHED*/
@@ -9369,7 +9382,6 @@ check_suffix(char **suf, int size)
return (suf[i]);
}
return (NULL);
-
}
/* Checking valid 'bzip2' suffix */
@@ -9413,3 +9425,30 @@ wait_pid(pid_t pid)
while (waitpid(pid, &status, 0) == -1 && errno == EINTR)
;
}
+
+static void
+verify_compress_opt(const char *t)
+{
+ struct stat statbuf;
+
+ if (stat(t, &statbuf) == -1)
+ vperror(1, "%s %s: %s\n", gettext("Could not stat"),
+ t, strerror(errno));
+}
+
+static void
+detect_compress(void)
+{
+ char *zsuf[] = {".Z"};
+ if (check_suffix(zsuf, 1) != NULL) {
+ Zflag = 1;
+ } else if (check_suffix(bsuffix, BSUF) != NULL) {
+ jflag = 1;
+ } else if (check_suffix(gsuffix, GSUF) != NULL) {
+ zflag = 1;
+ } else if (check_suffix(xsuffix, XSUF) != NULL) {
+ Jflag = 1;
+ } else {
+ vperror(1, "%s\n", gettext("No compression method detected"));
+ }
+}
diff --git a/usr/src/man/man1/tar.1 b/usr/src/man/man1/tar.1
index d7d0b773c3..fd21f6c53c 100644
--- a/usr/src/man/man1/tar.1
+++ b/usr/src/man/man1/tar.1
@@ -16,7 +16,7 @@ tar \- create tape archives and add or extract files
.SH SYNOPSIS
.LP
.nf
-\fBtar\fR c[BDeEFhilnopPqTvw@/[0-7]][bfk][X...][j|J|z|Z] [\fIblocksize\fR]
+\fBtar\fR c[BDeEFhilnopPqTvw@/[0-7]][bfk][X...][a|j|J|z|Z] [\fIblocksize\fR]
[\fItarfile\fR] [\fIsize\fR] [\fIexclude-file\fR]...
{\fIfile\fR | \(miI \fIinclude-file\fR | \(miC \fIdirectory\fR \fIfile\fR}...
.fi
@@ -242,6 +242,17 @@ the desired function.
.sp
.ne 2
.na
+\fB\fBa\fR\fR
+.ad
+.sp .6
+.RS 4n
+During a \fBcreate\fR operation autodetect compression based on the archive
+suffix.
+.RE
+
+.sp
+.ne 2
+.na
\fB\fBb\fR \fIblocksize\fR\fR
.ad
.sp .6