changeset 13693:b236aca9d46b

356 tar command needs to add the "J" switch and XZ compression support Reviewed by: David Hoeppner <0xffea@googlemail.com> Reviewed by: Andrew Stormont <Andrew.Stormont@nexenta.com> Approved by: Garrett D'Amore <garrett@damore.org>
author Milan Jurik <milan.jurik@xylab.cz>
date Mon, 14 May 2012 16:03:20 -0700
parents 05fa76d16740
children 3a75fed3cee2
files usr/src/cmd/file/magic usr/src/cmd/tar/tar.c usr/src/man/man1/tar.1
diffstat 3 files changed, 119 insertions(+), 84 deletions(-) [+]
line wrap: on
line diff
--- a/usr/src/cmd/file/magic	Mon May 14 12:16:08 2012 -0700
+++ b/usr/src/cmd/file/magic	Mon May 14 16:03:20 2012 -0700
@@ -617,3 +617,5 @@
 >8	string	jpx		JPEG 2000 w/ extensions (.JPX) [ISO 15444-2]
 >8	string	mp42		v2 [ISO 14496-14]
 4	string	moov		QuickTime MOV file
+
+0	string		\375\067\172\130\132\000	xz compressed data
--- a/usr/src/cmd/tar/tar.c	Mon May 14 12:16:08 2012 -0700
+++ b/usr/src/cmd/tar/tar.c	Mon May 14 16:03:20 2012 -0700
@@ -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 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	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 @@
 #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 @@
 			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 @@
 		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 @@
 				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 @@
 #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 @@
 #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()
-{
-	char 	magic[2];
-	char	buf[16];
+check_compression(void)
+{
+	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 @@
 			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 @@
 			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 @@
 	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()
-{
-	int 	i;
-	int	slen;
-	int	nlen = strlen(usefile);
-
-	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);
+gz_suffix(void)
+{
+	return (check_suffix(gsuffix, GSUF));
+}
+
+/* Checking valid 'xz' suffix */
+char *
+xz_suffix(void)
+{
+	return (check_suffix(xsuffix, XSUF));
 }
 
 void *
--- a/usr/src/man/man1/tar.1	Mon May 14 12:16:08 2012 -0700
+++ b/usr/src/man/man1/tar.1	Mon May 14 16:03:20 2012 -0700
@@ -1,6 +1,7 @@
 '\" te
 .\" Copyright 1989 AT&T
 .\" Copyright (c) 2006, Sun Microsystems, Inc. All Rights Reserved
+.\" Copyright 2012 Milan Jurik. All rights reserved.
 .\" Portions Copyright (c) 1992, X/Open Company Limited All Rights Reserved
 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
 .\" http://www.opengroup.org/bookstore/.
@@ -9,39 +10,39 @@
 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
-.TH TAR 1 "Oct 26, 2007"
+.TH TAR 1 "May 9, 2012"
 .SH NAME
 tar \- create tape archives and add or extract files
 .SH SYNOPSIS
 .LP
 .nf
-\fBtar\fR c[BDeEFhilnopPqTvw@/[0-7]][bfk][X...] [\fIblocksize\fR]
+\fBtar\fR c[BDeEFhilnopPqTvw@/[0-7]][bfk][X...][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
 
 .LP
 .nf
-\fBtar\fR r[BDeEFhilnqTvw@/[0-7]][bfk] [\fIblocksize\fR] [\fItarfile\fR]
+\fBtar\fR r[BDeEFhilnqTvw@/[0-7]][bfk][j|J|z|Z] [\fIblocksize\fR] [\fItarfile\fR]
      [\fIsize\fR]
      {\fIfile\fR | \(miI \fIinclude-file\fR | \(miC \fIdirectory\fR \fIfile\fR}...
 .fi
 
 .LP
 .nf
-\fBtar\fR t[BeFhilnqTv[0-7]][fk][X...] [\fItarfile\fR] [\fIsize\fR]
+\fBtar\fR t[BeFhilnqTv[0-7]][fk][X...][j|J|z|Z] [\fItarfile\fR] [\fIsize\fR]
      [\fIexclude-file\fR]... {\fIfile\fR | \(miI \fIinclude-file\fR}...
 .fi
 
 .LP
 .nf
-\fBtar\fR u[BDeEFhilnqTvw@/[0-7]][bfk] [\fIblocksize\fR] [\fItarfile\fR]
+\fBtar\fR u[BDeEFhilnqTvw@/[0-7]][bfk][j|J|z|Z] [\fIblocksize\fR] [\fItarfile\fR]
      [\fIsize\fR] \fIfile\fR...
 .fi
 
 .LP
 .nf
-\fBtar\fR x[BeFhilmnopqTvw@/[0-7]][fk][X...] [\fItarfile\fR] [\fIsize\fR]
+\fBtar\fR x[BeFhilmnopqTvw@/[0-7]][fk][X...][j|J|z|Z] [\fItarfile\fR] [\fIsize\fR]
      [\fIexclude-file\fR]... [\fIfile\fR]...
 .fi
 
@@ -397,6 +398,26 @@
 .sp
 .ne 2
 .na
+\fB\fBj\fR\fR
+.ad
+.sp .6
+.RS 4n
+Use \fBbzip2\fR for compressing or decompressing the archives.
+.RE
+
+.sp
+.ne 2
+.na
+\fB\fBJ\fR\fR
+.ad
+.sp .6
+.RS 4n
+Use \fBxz\fR for compressing or decompressing the archives.
+.RE
+
+.sp
+.ne 2
+.na
 \fB\fBk\fR \fIsize\fR\fR
 .ad
 .sp .6
@@ -589,6 +610,26 @@
 .sp
 .ne 2
 .na
+\fB\fBz\fR\fR
+.ad
+.sp .6
+.RS 4n
+Use \fBgzip\fR for compressing or decompressing the archives.
+.RE
+
+.sp
+.ne 2
+.na
+\fB\fBZ\fR\fR
+.ad
+.sp .6
+.RS 4n
+Use \fBcompress\fR for compressing or decompressing the archives.
+.RE
+
+.sp
+.ne 2
+.na
 \fB\fB@\fR\fR
 .ad
 .sp .6
@@ -952,42 +993,6 @@
 
 .SH ENVIRONMENT VARIABLES
 .sp
-.ne 2
-.na
-\fB\fBSYSV3\fR\fR
-.ad
-.sp .6
-.RS 4n
-This variable is used to override the default behavior of \fBtar\fR, provide
-compatibility with INTERACTIVE UNIX Systems and SCO UNIX installation scripts,
-and should not be used in new scripts. (It is intended for compatibility
-purposes only.) When set, the following function modifiers behave differently:
-.sp
-.ne 2
-.na
-\fB\fBF\fR \fIfilename\fR\fR
-.ad
-.sp .6
-.RS 4n
-Uses \fIfilename\fR to obtain a list of command line switches and files on
-which to operate.
-.RE
-
-.sp
-.ne 2
-.na
-\fB\fBe\fR\fR
-.ad
-.sp .6
-.RS 4n
-Prevents files from being split across volumes. If there is insufficient room
-on one volume, \fBtar\fR prompts for a new volume. If the file does not fit on
-the new volume, \fBtar\fR exits with an error.
-.RE
-
-.RE
-
-.sp
 .LP
 See \fBenviron\fR(5) for descriptions of the following environment variables
 that affect the execution of \fBtar\fR: \fBLC_COLLATE\fR, \fBLC_CTYPE\fR,
@@ -1160,9 +1165,10 @@
 .SH SEE ALSO
 .sp
 .LP
-\fBar\fR(1), \fBbasename\fR(1), \fBcd\fR(1), \fBchown\fR(1), \fBcpio\fR(1),
-\fBcsh\fR(1), \fBdirname\fR(1), \fBfind\fR(1), \fBls\fR(1), \fBmt\fR(1),
-\fBpax\fR(1), \fBsetfacl\fR(1), \fBumask\fR(1), \fBmknod\fR(1M),
+\fBar\fR(1), \fBbasename\fR(1), \fBbzip2\fR(1), \fBcd\fR(1), \fBchown\fR(1),
+\fBcompress\fR)(1), \fBcpio\fR(1), \fBcsh\fR(1), \fBdirname\fR(1), 
+\fBfind\fR(1), \fBgzip\fR(1), \fBls\fR(1), \fBmt\fR(1), \fBpax\fR(1),
+\fBsetfacl\fR(1), \fBumask\fR(1), \fBxz\fR(1), \fBmknod\fR(1M),
 \fBarchives.h\fR(3HEAD), \fBattributes\fR(5), \fBenviron\fR(5),
 \fBfsattr\fR(5), \fBlargefile\fR(5), \fBmtio\fR(7I)
 .SH DIAGNOSTICS