xref: /onnv-gate/usr/src/cmd/pack/pack.c (revision 1620:85628df26b6e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1620Smarks  * Common Development and Distribution License (the "License").
6*1620Smarks  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1620Smarks  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
29212Scf46844 #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  *	Huffman encoding program
330Sstevel@tonic-gate  *	Usage:	pack [[ -f ] [ - ] filename ... ] filename ...
340Sstevel@tonic-gate  *		- option: enable/disable listing of statistics
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/stat.h>
400Sstevel@tonic-gate #include <unistd.h>
410Sstevel@tonic-gate #include <locale.h>
420Sstevel@tonic-gate #include <stdarg.h>
430Sstevel@tonic-gate #include <errno.h>
440Sstevel@tonic-gate #include <sys/isa_defs.h>
450Sstevel@tonic-gate #include <stdlib.h>
460Sstevel@tonic-gate #include <limits.h>
470Sstevel@tonic-gate #include <sys/param.h>
480Sstevel@tonic-gate #include <fcntl.h>
490Sstevel@tonic-gate #include <utime.h>
500Sstevel@tonic-gate #include <string.h>
510Sstevel@tonic-gate #include <dirent.h>
520Sstevel@tonic-gate #include <unistd.h>
53789Sahrens #include <sys/acl.h>
54789Sahrens #include <aclutils.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #undef lint
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #define	END	256
590Sstevel@tonic-gate #define	PACKED 017436 /* <US><RS> - Unlikely value */
600Sstevel@tonic-gate #define	SUF0	'.'
610Sstevel@tonic-gate #define	SUF1	'z'
620Sstevel@tonic-gate 
630Sstevel@tonic-gate struct stat status, ostatus;
640Sstevel@tonic-gate static struct utimbuf u_times;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /* union for overlaying a long int with a set of four characters */
670Sstevel@tonic-gate union FOUR {
680Sstevel@tonic-gate 	struct { long int lng; } lint;
690Sstevel@tonic-gate 	struct { char c0, c1, c2, c3; } chars;
700Sstevel@tonic-gate };
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /* character counters */
730Sstevel@tonic-gate long	count [END+1];
740Sstevel@tonic-gate union	FOUR insize;
750Sstevel@tonic-gate long	outsize;
760Sstevel@tonic-gate long	dictsize;
770Sstevel@tonic-gate int	diffbytes;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /* i/o stuff */
800Sstevel@tonic-gate char	vflag = 0;
810Sstevel@tonic-gate int	force = 0;	/* allow forced packing for consistency in directory */
820Sstevel@tonic-gate 
830Sstevel@tonic-gate static	char filename [MAXPATHLEN];
840Sstevel@tonic-gate static int max_name;
850Sstevel@tonic-gate static int max_path = MAXPATHLEN;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate int	infile;		/* unpacked file */
880Sstevel@tonic-gate int	outfile;	/* packed file */
890Sstevel@tonic-gate char	inbuff [BUFSIZ];
900Sstevel@tonic-gate char	outbuff [BUFSIZ+4];
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /* variables associated with the tree */
930Sstevel@tonic-gate int	maxlev;
940Sstevel@tonic-gate int	levcount [25];
950Sstevel@tonic-gate int	lastnode;
960Sstevel@tonic-gate int	parent [2*END+1];
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /* variables associated with the encoding process */
990Sstevel@tonic-gate char	length [END+1];
1000Sstevel@tonic-gate long	bits [END+1];
1010Sstevel@tonic-gate union	FOUR mask;
1020Sstevel@tonic-gate long	inc;
1030Sstevel@tonic-gate #if defined(_LITTLE_ENDIAN)
1040Sstevel@tonic-gate char	*maskshuff[4]  = {&(mask.chars.c3),
1050Sstevel@tonic-gate 			    &(mask.chars.c2),
1060Sstevel@tonic-gate 			    &(mask.chars.c1),
1070Sstevel@tonic-gate 			    &(mask.chars.c0)};
1080Sstevel@tonic-gate #elif defined(_BIG_ENDIAN)
1090Sstevel@tonic-gate char	*maskshuff[4]  = {&(mask.chars.c0),
1100Sstevel@tonic-gate 			    &(mask.chars.c1),
1110Sstevel@tonic-gate 			    &(mask.chars.c2),
1120Sstevel@tonic-gate 			    &(mask.chars.c3)};
1130Sstevel@tonic-gate #else
1140Sstevel@tonic-gate #error Unknown byte ordering!
1150Sstevel@tonic-gate #endif
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate /* the heap */
1180Sstevel@tonic-gate int	n;
1190Sstevel@tonic-gate struct	heap {
1200Sstevel@tonic-gate 	long int count;
1210Sstevel@tonic-gate 	int node;
1220Sstevel@tonic-gate } heap [END+2];
1230Sstevel@tonic-gate #define	hmove(a, b) {(b).count = (a).count; (b).node = (a).node; }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate static void heapify(int i);
1260Sstevel@tonic-gate static int mv_xattrs(int, int, char *, int);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /* gather character frequency statistics */
1290Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
130212Scf46844 int
1310Sstevel@tonic-gate input(char *source)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	register int i;
1340Sstevel@tonic-gate 	for (i = 0; i < END; i++)
1350Sstevel@tonic-gate 		count[i] = 0;
1360Sstevel@tonic-gate 	while ((i = read(infile, inbuff, BUFSIZ)) > 0)
1370Sstevel@tonic-gate 		while (i > 0)
1380Sstevel@tonic-gate 			count[inbuff[--i]&0377] += 2;
1390Sstevel@tonic-gate 	if (i == 0)
1400Sstevel@tonic-gate 		return (1);
1410Sstevel@tonic-gate 	fprintf(stderr, gettext(
1420Sstevel@tonic-gate 		"pack: %s: read error - file unchanged: "), source);
1430Sstevel@tonic-gate 	perror("");
1440Sstevel@tonic-gate 	return (0);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /* encode the current file */
1480Sstevel@tonic-gate /* return 1 if successful, 0 otherwise */
149212Scf46844 int
1500Sstevel@tonic-gate output(char *source)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate 	int c, i, inleft;
1530Sstevel@tonic-gate 	char *inp;
1540Sstevel@tonic-gate 	register char **q, *outp;
1550Sstevel@tonic-gate 	register int bitsleft;
1560Sstevel@tonic-gate 	long temp;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	/* output ``PACKED'' header */
1590Sstevel@tonic-gate 	outbuff[0] = 037; 	/* ascii US */
1600Sstevel@tonic-gate 	outbuff[1] = 036; 	/* ascii RS */
1610Sstevel@tonic-gate 	/* output the length and the dictionary */
1620Sstevel@tonic-gate 	temp = insize.lint.lng;
1630Sstevel@tonic-gate 	for (i = 5; i >= 2; i--) {
1640Sstevel@tonic-gate 		outbuff[i] =  (char)(temp & 0377);
1650Sstevel@tonic-gate 		temp >>= 8;
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 	outp = &outbuff[6];
1680Sstevel@tonic-gate 	*outp++ = maxlev;
1690Sstevel@tonic-gate 	for (i = 1; i < maxlev; i++)
1700Sstevel@tonic-gate 		*outp++ = levcount[i];
1710Sstevel@tonic-gate 	*outp++ = levcount[maxlev]-2;
1720Sstevel@tonic-gate 	for (i = 1; i <= maxlev; i++)
1730Sstevel@tonic-gate 		for (c = 0; c < END; c++)
1740Sstevel@tonic-gate 			if (length[c] == i)
1750Sstevel@tonic-gate 				*outp++ = c;
1760Sstevel@tonic-gate 	dictsize = outp-&outbuff[0];
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	/* output the text */
1790Sstevel@tonic-gate 	lseek(infile, 0L, 0);
1800Sstevel@tonic-gate 	outsize = 0;
1810Sstevel@tonic-gate 	bitsleft = 8;
1820Sstevel@tonic-gate 	inleft = 0;
1830Sstevel@tonic-gate 	do {
1840Sstevel@tonic-gate 		if (inleft <= 0) {
1850Sstevel@tonic-gate 			inleft = read(infile, inp = &inbuff[0], BUFSIZ);
1860Sstevel@tonic-gate 			if (inleft < 0) {
1870Sstevel@tonic-gate 				fprintf(stderr, gettext(
1880Sstevel@tonic-gate 				    "pack: %s: read error - file unchanged: "),
1890Sstevel@tonic-gate 					    source);
1900Sstevel@tonic-gate 				perror("");
1910Sstevel@tonic-gate 				return (0);
1920Sstevel@tonic-gate 			}
1930Sstevel@tonic-gate 		}
1940Sstevel@tonic-gate 		c = (--inleft < 0) ? END : (*inp++ & 0377);
1950Sstevel@tonic-gate 		mask.lint.lng = bits[c]<<bitsleft;
1960Sstevel@tonic-gate 		q = &maskshuff[0];
1970Sstevel@tonic-gate 		if (bitsleft == 8)
1980Sstevel@tonic-gate 			*outp = **q++;
1990Sstevel@tonic-gate 		else
2000Sstevel@tonic-gate 			*outp |= **q++;
2010Sstevel@tonic-gate 		bitsleft -= length[c];
2020Sstevel@tonic-gate 		while (bitsleft < 0) {
2030Sstevel@tonic-gate 			*++outp = **q++;
2040Sstevel@tonic-gate 			bitsleft += 8;
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 		if (outp >= &outbuff[BUFSIZ]) {
2070Sstevel@tonic-gate 			if (write(outfile, outbuff, BUFSIZ) != BUFSIZ) {
2080Sstevel@tonic-gate wrerr:				fprintf(stderr, gettext(
2090Sstevel@tonic-gate 				"pack: %s.z: write error - file unchanged: "),
2100Sstevel@tonic-gate 					source);
2110Sstevel@tonic-gate 				perror("");
2120Sstevel@tonic-gate 				return (0);
2130Sstevel@tonic-gate 			}
2140Sstevel@tonic-gate 			((union FOUR *)outbuff)->lint.lng =
2150Sstevel@tonic-gate 				    ((union FOUR *)&outbuff[BUFSIZ])->lint.lng;
2160Sstevel@tonic-gate 			outp -= BUFSIZ;
2170Sstevel@tonic-gate 			outsize += BUFSIZ;
2180Sstevel@tonic-gate 		}
2190Sstevel@tonic-gate 	} while (c != END);
2200Sstevel@tonic-gate 	if (bitsleft < 8)
2210Sstevel@tonic-gate 		outp++;
2220Sstevel@tonic-gate 	c = outp-outbuff;
2230Sstevel@tonic-gate 	if (write(outfile, outbuff, c) != c)
2240Sstevel@tonic-gate 		goto wrerr;
2250Sstevel@tonic-gate 	outsize += c;
2260Sstevel@tonic-gate 	return (1);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate /* makes a heap out of heap[i],...,heap[n] */
2300Sstevel@tonic-gate void
2310Sstevel@tonic-gate heapify(int i)
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate 	register int k;
2340Sstevel@tonic-gate 	int lastparent;
2350Sstevel@tonic-gate 	struct heap heapsubi;
2360Sstevel@tonic-gate 	hmove(heap[i], heapsubi);
2370Sstevel@tonic-gate 	lastparent = n/2;
2380Sstevel@tonic-gate 	while (i <= lastparent) {
2390Sstevel@tonic-gate 		k = 2*i;
2400Sstevel@tonic-gate 		if (heap[k].count > heap[k+1].count && k < n)
2410Sstevel@tonic-gate 			k++;
2420Sstevel@tonic-gate 		if (heapsubi.count < heap[k].count)
2430Sstevel@tonic-gate 			break;
2440Sstevel@tonic-gate 		hmove(heap[k], heap[i]);
2450Sstevel@tonic-gate 		i = k;
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 	hmove(heapsubi, heap[i]);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate /* return 1 after successful packing, 0 otherwise */
2510Sstevel@tonic-gate int
2520Sstevel@tonic-gate packfile(char *source)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate 	register int c, i, p;
2550Sstevel@tonic-gate 	long bitsout;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	/* gather frequency statistics */
2580Sstevel@tonic-gate 	if (input(source) == 0)
2590Sstevel@tonic-gate 		return (0);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	/* put occurring chars in heap with their counts */
2620Sstevel@tonic-gate 	diffbytes = -1;
2630Sstevel@tonic-gate 	count[END] = 1;
2640Sstevel@tonic-gate 	insize.lint.lng = n = 0;
2650Sstevel@tonic-gate 	for (i = END; i >= 0; i--) {
2660Sstevel@tonic-gate 		parent[i] = 0;
2670Sstevel@tonic-gate 		if (count[i] > 0) {
2680Sstevel@tonic-gate 			diffbytes++;
2690Sstevel@tonic-gate 			insize.lint.lng += count[i];
2700Sstevel@tonic-gate 			heap[++n].count = count[i];
2710Sstevel@tonic-gate 			heap[n].node = i;
2720Sstevel@tonic-gate 		}
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 	if (diffbytes == 1) {
2750Sstevel@tonic-gate 		fprintf(stderr, gettext(
2760Sstevel@tonic-gate 			"pack: %s: trivial file - file unchanged\n"), source);
2770Sstevel@tonic-gate 		return (0);
2780Sstevel@tonic-gate 	}
2790Sstevel@tonic-gate 	insize.lint.lng >>= 1;
2800Sstevel@tonic-gate 	for (i = n/2; i >= 1; i--)
2810Sstevel@tonic-gate 		heapify(i);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	/* build Huffman tree */
2840Sstevel@tonic-gate 	lastnode = END;
2850Sstevel@tonic-gate 	while (n > 1) {
2860Sstevel@tonic-gate 		parent[heap[1].node] = ++lastnode;
2870Sstevel@tonic-gate 		inc = heap[1].count;
2880Sstevel@tonic-gate 		hmove(heap[n], heap[1]);
2890Sstevel@tonic-gate 		n--;
2900Sstevel@tonic-gate 		heapify(1);
2910Sstevel@tonic-gate 		parent[heap[1].node] = lastnode;
2920Sstevel@tonic-gate 		heap[1].node = lastnode;
2930Sstevel@tonic-gate 		heap[1].count += inc;
2940Sstevel@tonic-gate 		heapify(1);
2950Sstevel@tonic-gate 	}
2960Sstevel@tonic-gate 	parent[lastnode] = 0;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	/* assign lengths to encoding for each character */
2990Sstevel@tonic-gate 	bitsout = maxlev = 0;
3000Sstevel@tonic-gate 	for (i = 1; i <= 24; i++)
3010Sstevel@tonic-gate 		levcount[i] = 0;
3020Sstevel@tonic-gate 	for (i = 0; i <= END; i++) {
3030Sstevel@tonic-gate 		c = 0;
3040Sstevel@tonic-gate 		for (p = parent[i]; p != 0; p = parent[p])
3050Sstevel@tonic-gate 			c++;
3060Sstevel@tonic-gate 		levcount[c]++;
3070Sstevel@tonic-gate 		length[i] = c;
3080Sstevel@tonic-gate 		if (c > maxlev)
3090Sstevel@tonic-gate 			maxlev = c;
3100Sstevel@tonic-gate 		bitsout += c*(count[i]>>1);
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate 	if (maxlev > 24) {
3130Sstevel@tonic-gate 		/* can't occur unless insize.lint.lng >= 2**24 */
3140Sstevel@tonic-gate 		fprintf(stderr, gettext(
3150Sstevel@tonic-gate 	"pack: %s: Huffman tree has too many levels - file unchanged\n"),
3160Sstevel@tonic-gate 			source);
3170Sstevel@tonic-gate 		return (0);
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	/* don't bother if no compression results */
3210Sstevel@tonic-gate 	outsize = ((bitsout+7)>>3)+6+maxlev+diffbytes;
3220Sstevel@tonic-gate 	if ((insize.lint.lng+BUFSIZ-1)/BUFSIZ <=
3230Sstevel@tonic-gate 				    (outsize+BUFSIZ-1)/BUFSIZ && !force) {
3240Sstevel@tonic-gate 		printf(gettext(
3250Sstevel@tonic-gate 			"pack: %s: no saving - file unchanged\n"), source);
3260Sstevel@tonic-gate 		return (0);
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	/* compute bit patterns for each character */
3300Sstevel@tonic-gate 	inc = 1L << 24;
3310Sstevel@tonic-gate 	inc >>= maxlev;
3320Sstevel@tonic-gate 	mask.lint.lng = 0;
3330Sstevel@tonic-gate 	for (i = maxlev; i > 0; i--) {
3340Sstevel@tonic-gate 		for (c = 0; c <= END; c++)
3350Sstevel@tonic-gate 			if (length[c] == i) {
3360Sstevel@tonic-gate 				bits[c] = mask.lint.lng;
3370Sstevel@tonic-gate 				mask.lint.lng += inc;
3380Sstevel@tonic-gate 			}
3390Sstevel@tonic-gate 		mask.lint.lng &= ~inc;
3400Sstevel@tonic-gate 		inc <<= 1;
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	return (output(source));
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate 
346212Scf46844 int
3470Sstevel@tonic-gate main(int argc, char *argv[])
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate 	extern  int optind;
3500Sstevel@tonic-gate 	register int i;
3510Sstevel@tonic-gate 	register char *cp;
3520Sstevel@tonic-gate 	int k, sep, errflg = 0;
3530Sstevel@tonic-gate 	int c;
354789Sahrens 	int error;
3550Sstevel@tonic-gate 	int fcount = 0; /* count failures */
356789Sahrens 	acl_t *aclp = NULL;
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3590Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
3600Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
3610Sstevel@tonic-gate #endif
3620Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "f-")) != EOF) {
3650Sstevel@tonic-gate 		if (c == 'f')
3660Sstevel@tonic-gate 			force++;
3670Sstevel@tonic-gate 		else
3680Sstevel@tonic-gate 			++errflg;
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 	/*
3710Sstevel@tonic-gate 	 * Check for invalid option.  Also check for missing
3720Sstevel@tonic-gate 	 * file operand, ie: "pack" or "pack -".
3730Sstevel@tonic-gate 	 */
3740Sstevel@tonic-gate 	argc -= optind;
3750Sstevel@tonic-gate 	argv = &argv[optind];
3760Sstevel@tonic-gate 	if (errflg || argc < 1 ||
3770Sstevel@tonic-gate 		(argc == 1 && argv[0][0] == '-' && argv[0][1] == '\0')) {
3780Sstevel@tonic-gate 		fprintf(stderr, gettext(
3790Sstevel@tonic-gate 			"usage: pack [-f] [-] file...\n"));
3800Sstevel@tonic-gate 		if (argc < 1 ||
3810Sstevel@tonic-gate 			(argc == 1 && argv[0][0] == '-' &&
3820Sstevel@tonic-gate 				argv[0][1] == '\0')) {
3830Sstevel@tonic-gate 			/*
3840Sstevel@tonic-gate 			 * return 1 for usage error when no file was specified
3850Sstevel@tonic-gate 			 */
3860Sstevel@tonic-gate 			return (1);
3870Sstevel@tonic-gate 		}
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 	/* loop through the file names */
3900Sstevel@tonic-gate 	for (k = 0; k < argc; k++) {
3910Sstevel@tonic-gate 		if (argv[k][0] == '-' && argv[k][1] == '\0') {
3920Sstevel@tonic-gate 			vflag = 1 - vflag;
3930Sstevel@tonic-gate 			continue;
3940Sstevel@tonic-gate 		}
3950Sstevel@tonic-gate 		fcount++; /* increase failure count - expect the worst */
3960Sstevel@tonic-gate 		if (errflg) {
3970Sstevel@tonic-gate 			/*
3980Sstevel@tonic-gate 			 * invalid option; just count the number of files not
3990Sstevel@tonic-gate 			 * packed
4000Sstevel@tonic-gate 			 */
4010Sstevel@tonic-gate 			continue;
4020Sstevel@tonic-gate 		}
4030Sstevel@tonic-gate 		/* remove any .z suffix the user may have added */
4040Sstevel@tonic-gate 		for (cp = argv[k]; *cp != '\0'; ++cp)
4050Sstevel@tonic-gate 			;
4060Sstevel@tonic-gate 		if (cp[-1] == SUF1 && cp[-2] == SUF0) {
4070Sstevel@tonic-gate 			*cp-- = '\0'; *cp-- = '\0'; *cp = '\0';
4080Sstevel@tonic-gate 		}
4090Sstevel@tonic-gate 		sep = -1;  cp = filename;
4100Sstevel@tonic-gate 		max_name = pathconf(argv[k], _PC_NAME_MAX);
4110Sstevel@tonic-gate 		if (max_name == -1) {
4120Sstevel@tonic-gate 			/* pathname invalid or no limit on length of filename */
4130Sstevel@tonic-gate 			max_name = _POSIX_NAME_MAX;
4140Sstevel@tonic-gate 		}
4150Sstevel@tonic-gate 		/* copy argv[k] to filename and count chars in base name */
4160Sstevel@tonic-gate 		for (i = 0; i < (MAXPATHLEN-3) && (*cp = argv[k][i]); i++)
4170Sstevel@tonic-gate 			if (*cp++ == '/') sep = i;
4180Sstevel@tonic-gate 		if ((infile = open(filename, 0)) < 0) {
4190Sstevel@tonic-gate 			fprintf(stderr, gettext(
4200Sstevel@tonic-gate 				"pack: %s: cannot open: "), filename);
4210Sstevel@tonic-gate 			perror("");
4220Sstevel@tonic-gate 			continue;
4230Sstevel@tonic-gate 		}
4240Sstevel@tonic-gate 		if (i >= (MAXPATHLEN-3) || (i-sep) > (max_name - 1)) {
4250Sstevel@tonic-gate 			fprintf(stderr, gettext(
4260Sstevel@tonic-gate 				"pack: %s: file name too long\n"), argv[k]);
4270Sstevel@tonic-gate 			continue;
4280Sstevel@tonic-gate 		}
4290Sstevel@tonic-gate 		fstat(infile, &status);
430871Scasper 		if (S_ISDIR(status.st_mode)) {
4310Sstevel@tonic-gate 			fprintf(stderr, gettext(
4320Sstevel@tonic-gate 				"pack: %s: cannot pack a directory\n"),
4330Sstevel@tonic-gate 				    argv[k]);
4340Sstevel@tonic-gate 			goto closein;
4350Sstevel@tonic-gate 		}
4360Sstevel@tonic-gate 		if (status.st_size == 0) {
4370Sstevel@tonic-gate 			fprintf(stderr, gettext(
4380Sstevel@tonic-gate 				"pack: %s: cannot pack a zero length file\n"),
4390Sstevel@tonic-gate 				argv[k]);
4400Sstevel@tonic-gate 			goto closein;
4410Sstevel@tonic-gate 		}
4420Sstevel@tonic-gate 		if (status.st_nlink != 1) {
4430Sstevel@tonic-gate 			fprintf(stderr, gettext(
4440Sstevel@tonic-gate 				"pack: %s: has links\n"),
4450Sstevel@tonic-gate 				argv[k]);
4460Sstevel@tonic-gate 			goto closein;
4470Sstevel@tonic-gate 		}
4480Sstevel@tonic-gate 		*cp++ = SUF0;  *cp++ = SUF1;  *cp = '\0';
4490Sstevel@tonic-gate 		if (stat(filename, &ostatus) != -1) {
4500Sstevel@tonic-gate 			fprintf(stderr, gettext(
4510Sstevel@tonic-gate 				"pack: %s: already exists\n"), filename);
4520Sstevel@tonic-gate 			goto closein;
4530Sstevel@tonic-gate 		}
454789Sahrens 
4550Sstevel@tonic-gate 		if ((outfile = creat(filename, status.st_mode)) < 0) {
4560Sstevel@tonic-gate 			fprintf(stderr, gettext(
4570Sstevel@tonic-gate 				"pack: %s: cannot create: "), filename);
4580Sstevel@tonic-gate 			perror("");
4590Sstevel@tonic-gate 			goto closein;
4600Sstevel@tonic-gate 		}
4610Sstevel@tonic-gate 
462789Sahrens 		error = facl_get(infile, ACL_NO_TRIVIAL, &aclp);
463789Sahrens 
464789Sahrens 		if (error != 0) {
465789Sahrens 			fprintf(stderr, gettext(
466789Sahrens 			    "pack: %s: cannot retrieve ACL: %s\n"), argv[k],
467789Sahrens 			    acl_strerror(error));
468789Sahrens 		}
4690Sstevel@tonic-gate 		if (packfile(argv[k]) &&
4700Sstevel@tonic-gate 		    ((pathconf(argv[k], _PC_XATTR_EXISTS) != 1) ||
4710Sstevel@tonic-gate 				(mv_xattrs(infile, outfile,
4720Sstevel@tonic-gate 					argv[k], 0) == 0))) {
4730Sstevel@tonic-gate 			if (unlink(argv[k]) != 0) {
4740Sstevel@tonic-gate 				fprintf(stderr, gettext(
4750Sstevel@tonic-gate 					"pack: %s: cannot unlink: "),
4760Sstevel@tonic-gate 					argv[k]);
4770Sstevel@tonic-gate 				perror("");
4780Sstevel@tonic-gate 			}
4790Sstevel@tonic-gate 			printf(gettext(
4800Sstevel@tonic-gate 				"pack: %s: %.1f%% Compression\n"),
4810Sstevel@tonic-gate 				argv[k],
4820Sstevel@tonic-gate 	    ((double)(-outsize+(insize.lint.lng))/(double)insize.lint.lng)*100);
4830Sstevel@tonic-gate 			/* output statistics */
4840Sstevel@tonic-gate 			if (vflag) {
4850Sstevel@tonic-gate 				printf(gettext("\tfrom %ld to %ld bytes\n"),
4860Sstevel@tonic-gate 					insize.lint.lng, outsize);
4870Sstevel@tonic-gate 				printf(gettext(
4880Sstevel@tonic-gate 				"\tHuffman tree has %d levels below root\n"),
4890Sstevel@tonic-gate 				    maxlev);
4900Sstevel@tonic-gate 				printf(gettext(
4910Sstevel@tonic-gate 					"\t%d distinct bytes in input\n"),
4920Sstevel@tonic-gate 					diffbytes);
4930Sstevel@tonic-gate 				printf(gettext(
4940Sstevel@tonic-gate 					"\tdictionary overhead = %ld bytes\n"),
4950Sstevel@tonic-gate 					dictsize);
4960Sstevel@tonic-gate 				printf(gettext(
4970Sstevel@tonic-gate 				    "\teffective  entropy  = %.2f bits/byte\n"),
4980Sstevel@tonic-gate 			    ((double)outsize / (double)insize.lint.lng) * 8);
4990Sstevel@tonic-gate 				printf(gettext(
5000Sstevel@tonic-gate 				    "\tasymptotic entropy  = %.2f bits/byte\n"),
5010Sstevel@tonic-gate 					((double)(outsize-dictsize) /
5020Sstevel@tonic-gate 					(double)insize.lint.lng) * 8);
5030Sstevel@tonic-gate 			}
5040Sstevel@tonic-gate 			u_times.actime = status.st_atime;
5050Sstevel@tonic-gate 			u_times.modtime = status.st_mtime;
5060Sstevel@tonic-gate 			if (utime(filename, &u_times) != 0) {
5070Sstevel@tonic-gate 				errflg++;
5080Sstevel@tonic-gate 				fprintf(stderr,
5090Sstevel@tonic-gate 					gettext(
5100Sstevel@tonic-gate 					"pack: cannot change times on %s: "),
5110Sstevel@tonic-gate 					filename);
5120Sstevel@tonic-gate 				perror("");
5130Sstevel@tonic-gate 			}
5140Sstevel@tonic-gate 			if (chmod(filename, status.st_mode) != 0) {
5150Sstevel@tonic-gate 				errflg++;
5160Sstevel@tonic-gate 				fprintf(stderr,
5170Sstevel@tonic-gate 					gettext(
5180Sstevel@tonic-gate 				"pack: can't change mode to %o on %s: "),
5190Sstevel@tonic-gate 					status.st_mode, filename);
5200Sstevel@tonic-gate 				perror("");
5210Sstevel@tonic-gate 			}
5220Sstevel@tonic-gate 			chown(filename, status.st_uid, status.st_gid);
523789Sahrens 			if (aclp && (facl_set(outfile, aclp) < 0)) {
524789Sahrens 				fprintf(stderr, gettext(
525789Sahrens 				    "pack: %s: failed to set acl entries\n"),
526789Sahrens 				    filename);
527789Sahrens 				perror("");
528789Sahrens 			}
5290Sstevel@tonic-gate 			if (!errflg)
5300Sstevel@tonic-gate 				fcount--;  /* success after all */
5310Sstevel@tonic-gate 		} else {
5320Sstevel@tonic-gate 			if (pathconf(filename, _PC_XATTR_EXISTS) == 1) {
5330Sstevel@tonic-gate 				(void) mv_xattrs(outfile, infile, filename, 1);
5340Sstevel@tonic-gate 			}
5350Sstevel@tonic-gate 			unlink(filename);
5360Sstevel@tonic-gate 		}
537789Sahrens 
538*1620Smarks 		if (aclp) {
539789Sahrens 			acl_free(aclp);
540*1620Smarks 			aclp = NULL;
541*1620Smarks 		}
542789Sahrens 
5430Sstevel@tonic-gate closein:	close(outfile);
5440Sstevel@tonic-gate 		close(infile);
5450Sstevel@tonic-gate 	}
5460Sstevel@tonic-gate 	return (fcount);
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate /*
5500Sstevel@tonic-gate  * mv_xattrs - move (via renameat) all of the extended attributes
5510Sstevel@tonic-gate  *	associated with the file referenced by infd to the file
5520Sstevel@tonic-gate  *	referenced by outfd.  The infile and silent arguments are
5530Sstevel@tonic-gate  *	provided for error message processing.  This function
5540Sstevel@tonic-gate  *	returns 0 on success and -1 on error.
5550Sstevel@tonic-gate  */
5560Sstevel@tonic-gate static int
5570Sstevel@tonic-gate mv_xattrs(int infd, int outfd, char *infile, int silent)
5580Sstevel@tonic-gate {
5590Sstevel@tonic-gate 	int indfd, outdfd, tmpfd;
5600Sstevel@tonic-gate 	DIR *dirp = NULL;
5610Sstevel@tonic-gate 	struct dirent *dp = NULL;
5620Sstevel@tonic-gate 	int error = 0;
5630Sstevel@tonic-gate 	char *etext;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	indfd = outdfd = tmpfd = -1;
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	if ((indfd = openat(infd, ".", O_RDONLY|O_XATTR)) == -1) {
5680Sstevel@tonic-gate 		etext = gettext("cannot open source");
5690Sstevel@tonic-gate 		error = -1;
5700Sstevel@tonic-gate 		goto out;
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	if ((outdfd = openat(outfd, ".", O_RDONLY|O_XATTR)) == -1) {
5740Sstevel@tonic-gate 		etext = gettext("cannot open target");
5750Sstevel@tonic-gate 		error = -1;
5760Sstevel@tonic-gate 		goto out;
5770Sstevel@tonic-gate 	}
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	if ((tmpfd = dup(indfd)) == -1) {
5800Sstevel@tonic-gate 		etext = gettext("cannot dup descriptor");
5810Sstevel@tonic-gate 		error = -1;
5820Sstevel@tonic-gate 		goto out;
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	}
5850Sstevel@tonic-gate 	if ((dirp = fdopendir(tmpfd)) == NULL) {
5860Sstevel@tonic-gate 		etext = gettext("cannot access source");
5870Sstevel@tonic-gate 		error = -1;
5880Sstevel@tonic-gate 		goto out;
5890Sstevel@tonic-gate 	}
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	while (dp = readdir(dirp)) {
5920Sstevel@tonic-gate 		if ((dp->d_name[0] == '.' && dp->d_name[1] == '\0') ||
5930Sstevel@tonic-gate 		    (dp->d_name[0] == '.' && dp->d_name[1] == '.' &&
5940Sstevel@tonic-gate 		    dp->d_name[2] == '\0'))
5950Sstevel@tonic-gate 			continue;
5960Sstevel@tonic-gate 		if ((renameat(indfd, dp->d_name, outdfd, dp->d_name)) == -1) {
5970Sstevel@tonic-gate 			etext = dp->d_name;
5980Sstevel@tonic-gate 			error = -1;
5990Sstevel@tonic-gate 			goto out;
6000Sstevel@tonic-gate 		}
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate out:
6030Sstevel@tonic-gate 	if (error == -1 && silent == 0) {
6040Sstevel@tonic-gate 		fprintf(stderr, gettext(
6050Sstevel@tonic-gate 			"pack: %s: cannot move extended attributes, "),
6060Sstevel@tonic-gate 			infile);
6070Sstevel@tonic-gate 		perror(etext);
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 	if (dirp)
6100Sstevel@tonic-gate 		closedir(dirp);
6110Sstevel@tonic-gate 	if (indfd != -1)
6120Sstevel@tonic-gate 		close(indfd);
6130Sstevel@tonic-gate 	if (outdfd != -1)
6140Sstevel@tonic-gate 		close(outdfd);
6150Sstevel@tonic-gate 	return (error);
6160Sstevel@tonic-gate }
617