changeset 13710:3cee4ba952f0

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>
author Milan Jurik <milan.jurik@xylab.cz>
date Tue, 05 Jun 2012 10:09:55 -0700
parents 1f68af63b8d9
children 0765d0ec4e2a
files usr/src/cmd/tar/tar.c usr/src/man/man1/tar.1
diffstat 2 files changed, 54 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/usr/src/cmd/tar/tar.c	Sat Jun 02 18:42:53 2012 +0200
+++ b/usr/src/cmd/tar/tar.c	Tue Jun 05 10:09:55 2012 -0700
@@ -555,6 +555,8 @@
 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	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 @@
 		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 @@
 		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 @@
 	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 @@
 		    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 @@
 			    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 @@
 	(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 @@
 	(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 @@
 			return (suf[i]);
 	}
 	return (NULL);
-
 }
 
 /* Checking valid 'bzip2' suffix */
@@ -9413,3 +9425,30 @@
 	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"));
+	}
+}
--- a/usr/src/man/man1/tar.1	Sat Jun 02 18:42:53 2012 +0200
+++ b/usr/src/man/man1/tar.1	Tue Jun 05 10:09:55 2012 -0700
@@ -16,7 +16,7 @@
 .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 @@
 .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