xref: /onnv-gate/usr/src/cmd/fs.d/cachefs/cachefspack/main.c (revision 633:04519cb7de3b)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*633Sgt29601  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*633Sgt29601  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <locale.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <unistd.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #define	MAIN	1
360Sstevel@tonic-gate #include "rules.h"
370Sstevel@tonic-gate #include "elfrd.h"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate int verbose = 0;
400Sstevel@tonic-gate struct libpath *libp, libp_hd;
410Sstevel@tonic-gate 
420Sstevel@tonic-gate int
main(int argc,char ** argv)430Sstevel@tonic-gate main(int argc, char **argv)
440Sstevel@tonic-gate {
450Sstevel@tonic-gate 	int prtfn();
460Sstevel@tonic-gate 	int packfn();
470Sstevel@tonic-gate 	int unpackfn();
480Sstevel@tonic-gate 	int inquirefn();
490Sstevel@tonic-gate 	FILE *open_rulesfile();
500Sstevel@tonic-gate 	FILE *rfd;
510Sstevel@tonic-gate 	int c;
520Sstevel@tonic-gate 	int fflag = 0;
530Sstevel@tonic-gate 	int Bflag = 0;
540Sstevel@tonic-gate 	int index;
550Sstevel@tonic-gate 	char *rulesfile;
560Sstevel@tonic-gate 	int typearg = 0;
570Sstevel@tonic-gate 	int (*wrkfunc)();
580Sstevel@tonic-gate 	extern char *optarg;
590Sstevel@tonic-gate 	extern int optind, opterr;
600Sstevel@tonic-gate 	extern void bld_pack_list();
610Sstevel@tonic-gate 	extern void usage();
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
640Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
650Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
660Sstevel@tonic-gate #endif
670Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	global_flags = LF_NULL;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	rfd = open_rulesfile();
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	libp = &libp_hd;
740Sstevel@tonic-gate 	get_libsrch_path(libp);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	/* create hash table for tracking libraries */
770Sstevel@tonic-gate 	if (hcreate(10000) == 0) {
780Sstevel@tonic-gate 		/* unlikely this ever happens or I would work around it */
790Sstevel@tonic-gate 		fprintf(stderr,
800Sstevel@tonic-gate 		    gettext("cachefspack: can't create hash table\n"));
810Sstevel@tonic-gate 		exit(1);
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "df:hiprsuvB:I:L:U:")) != -1) {
850Sstevel@tonic-gate 		switch (c) {
860Sstevel@tonic-gate 		case 'd':
870Sstevel@tonic-gate 			wrkfunc = prtfn;
880Sstevel@tonic-gate 			typearg++;
890Sstevel@tonic-gate 			break;
900Sstevel@tonic-gate 		case 'f':
910Sstevel@tonic-gate 			fflag++;
920Sstevel@tonic-gate 			rulesfile = strdup(optarg);
930Sstevel@tonic-gate 			break;
940Sstevel@tonic-gate 		case 'h':
950Sstevel@tonic-gate 			usage();
960Sstevel@tonic-gate 			exit(0);
970Sstevel@tonic-gate 			break;
980Sstevel@tonic-gate 		case 'i':
990Sstevel@tonic-gate 			wrkfunc = inquirefn;
1000Sstevel@tonic-gate 			typearg++;
1010Sstevel@tonic-gate 			break;
1020Sstevel@tonic-gate 		case 'p':
1030Sstevel@tonic-gate 			wrkfunc = packfn;
1040Sstevel@tonic-gate 			typearg++;
1050Sstevel@tonic-gate 			break;
1060Sstevel@tonic-gate 		case 'r':
1070Sstevel@tonic-gate 			global_flags |= LF_REGEX;
1080Sstevel@tonic-gate 			break;
1090Sstevel@tonic-gate 		case 's':
1100Sstevel@tonic-gate 			global_flags |= LF_STRIP_DOTSLASH;
1110Sstevel@tonic-gate 			break;
1120Sstevel@tonic-gate 		case 'u':
1130Sstevel@tonic-gate 			wrkfunc = unpackfn;
1140Sstevel@tonic-gate 			typearg++;
1150Sstevel@tonic-gate 			break;
1160Sstevel@tonic-gate 		case 'v':
1170Sstevel@tonic-gate 			verbose = 1;
1180Sstevel@tonic-gate 			break;
1190Sstevel@tonic-gate 		case 'B':
1200Sstevel@tonic-gate 			Bflag++;
1210Sstevel@tonic-gate 			fprintf(rfd, "BASE %s\n", optarg);
1220Sstevel@tonic-gate 			break;
1230Sstevel@tonic-gate 		case 'I':
1240Sstevel@tonic-gate 			fprintf(rfd, "IGNORE %s\n", optarg);
1250Sstevel@tonic-gate 			break;
1260Sstevel@tonic-gate 		case 'L':
1270Sstevel@tonic-gate 			fprintf(rfd, "LIST %s\n", optarg);
1280Sstevel@tonic-gate 			break;
1290Sstevel@tonic-gate 		case 'U':
1300Sstevel@tonic-gate 			typearg++;
1310Sstevel@tonic-gate 			wrkfunc = unpackfn;
1320Sstevel@tonic-gate 			bld_pack_list(rfd, optarg);
1330Sstevel@tonic-gate 			break;
1340Sstevel@tonic-gate 		default:
1350Sstevel@tonic-gate 			usage();
1360Sstevel@tonic-gate 			exit(1);
1370Sstevel@tonic-gate 		}
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	def_lign_flags = LF_NULL;
1410Sstevel@tonic-gate 	def_gign_flags = LF_NULL;
1420Sstevel@tonic-gate 	def_list_flags = LF_REGEX;
1430Sstevel@tonic-gate 	bang_list_flags = LF_STRIP_DOTSLASH;
1440Sstevel@tonic-gate 	if (global_flags != 0) {
1450Sstevel@tonic-gate 		def_list_flags = global_flags;
1460Sstevel@tonic-gate 		bang_list_flags = global_flags;
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	if (fflag & Bflag) {
1500Sstevel@tonic-gate 		fprintf(stderr, gettext(
1510Sstevel@tonic-gate 		    "cachefspack: B and f options are mutually exclusive\n"));
1520Sstevel@tonic-gate 		exit(1);
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	if (fflag) {
1560Sstevel@tonic-gate 		fclose(rfd);
1570Sstevel@tonic-gate 		rfd = fopen(rulesfile, "r");
1580Sstevel@tonic-gate 		if (rfd == NULL) {
1590Sstevel@tonic-gate 			fprintf(stderr, gettext(
1600Sstevel@tonic-gate 			    "cachefspack: can't open file associated"
1610Sstevel@tonic-gate 			    " with -f\n"));
1620Sstevel@tonic-gate 			exit(1);
1630Sstevel@tonic-gate 		}
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	if (typearg != 1) {
1670Sstevel@tonic-gate 		if (typearg == 0) {
1680Sstevel@tonic-gate 			wrkfunc = packfn;
1690Sstevel@tonic-gate 		} else {
1700Sstevel@tonic-gate 			fprintf(stderr,
1710Sstevel@tonic-gate 			    gettext(
1720Sstevel@tonic-gate 			    "cachefspack: only one 'd', 'i', 'p' or 'u' "));
1730Sstevel@tonic-gate 			fprintf(stderr,
1740Sstevel@tonic-gate 			    gettext(" option allowed\n"));
1750Sstevel@tonic-gate 			exit(1);
1760Sstevel@tonic-gate 		}
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 	if (optind < argc) {
1790Sstevel@tonic-gate 		if (fflag || Bflag) {
1800Sstevel@tonic-gate 			fprintf(stderr,
1810Sstevel@tonic-gate 			    gettext(
1820Sstevel@tonic-gate 			    "cachefspack: 'B' or 'f' specified "));
1830Sstevel@tonic-gate 			fprintf(stderr,
1840Sstevel@tonic-gate 			    gettext("with filenames\n"));
1850Sstevel@tonic-gate 			exit(1);
1860Sstevel@tonic-gate 		}
1870Sstevel@tonic-gate 		for (index = optind; index < argc; index++) {
1880Sstevel@tonic-gate #ifdef  DEBUG
1890Sstevel@tonic-gate 			printf("argv[%d] = %s\n", index, argv[index]);
1900Sstevel@tonic-gate #endif /* DEBUG */
1910Sstevel@tonic-gate 			bld_pack_list(rfd, argv[index]);
1920Sstevel@tonic-gate 		}
1930Sstevel@tonic-gate 	}
1940Sstevel@tonic-gate 	rewind(rfd);
1950Sstevel@tonic-gate 	read_rules(rfd, wrkfunc);
1960Sstevel@tonic-gate 	fclose(rfd);
197*633Sgt29601 	return (0);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate /*
2010Sstevel@tonic-gate  * The bld_pack_list() function is used to write the temporary packing
2020Sstevel@tonic-gate  * list function. When the BASE directory changes, a new BASE command is
2030Sstevel@tonic-gate  * generated. If the  filename argument(fnam) starts with a '/', then the
2040Sstevel@tonic-gate  * filename is assumed to be an absolute pathname. Otherwise, the filename
2050Sstevel@tonic-gate  * is assumed to be realtive to the current directory.
2060Sstevel@tonic-gate  */
2070Sstevel@tonic-gate void
bld_pack_list(FILE * fd,char * filename)2080Sstevel@tonic-gate bld_pack_list(FILE *fd, char *filename)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	static char last_base[MAXPATHLEN+1] = {" "};
2110Sstevel@tonic-gate 	static char fnam[MAXPATHLEN+1];
2120Sstevel@tonic-gate 	static int last_base_sz = 1;
2130Sstevel@tonic-gate 	char *lastsl_pos;
2140Sstevel@tonic-gate 	int sz;
2150Sstevel@tonic-gate 	int endpos;
2160Sstevel@tonic-gate 	char *cwd;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	/* strip off any trailing /'s */
2190Sstevel@tonic-gate 	strcpy(fnam, filename);
2200Sstevel@tonic-gate 	for (endpos = strlen(fnam) - 1; endpos > 0; endpos--) {
2210Sstevel@tonic-gate 		if (fnam[endpos] == '/')
2220Sstevel@tonic-gate 			fnam[endpos] = '\0';
2230Sstevel@tonic-gate 		else
2240Sstevel@tonic-gate 			break;
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	if (*fnam == '/') {	/* absolute pathname */
2280Sstevel@tonic-gate 		lastsl_pos = strrchr(fnam, '/');
2290Sstevel@tonic-gate 		sz = (int)lastsl_pos - (int)fnam + 1;
2300Sstevel@tonic-gate 		if ((last_base_sz != sz) ||
2310Sstevel@tonic-gate 		    (strncmp(last_base, fnam, sz) != 0)) {
2320Sstevel@tonic-gate 			fprintf(fd, "BASE %.*s\n", (sz <= 1 ? sz : sz-1), fnam);
2330Sstevel@tonic-gate 			last_base_sz = sz;
2340Sstevel@tonic-gate 			strncpy(last_base, fnam, sz);
2350Sstevel@tonic-gate 		}
2360Sstevel@tonic-gate 		fprintf(fd, "LIST %s\n", &fnam[sz]);
2370Sstevel@tonic-gate 	} else {		/* relative pathname */
2380Sstevel@tonic-gate 		/* Really only need to call this once, ... */
2390Sstevel@tonic-gate 		cwd = getcwd(NULL, MAXPATHLEN+1);
2400Sstevel@tonic-gate 		sz = strlen(cwd);
2410Sstevel@tonic-gate 		if ((last_base_sz != sz) ||
2420Sstevel@tonic-gate 		    (strncmp(last_base, cwd, sz) != 0)) {
2430Sstevel@tonic-gate 			fprintf(fd, "BASE %s\n", cwd);
2440Sstevel@tonic-gate 			last_base_sz = sz;
2450Sstevel@tonic-gate 			strncpy(last_base, cwd, sz);
2460Sstevel@tonic-gate 		}
2470Sstevel@tonic-gate 		free(cwd);
2480Sstevel@tonic-gate 		fprintf(fd, "LIST %s\n", fnam);
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate void
usage()2530Sstevel@tonic-gate usage()
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate #ifdef  DEBUG
2560Sstevel@tonic-gate 	printf(
2570Sstevel@tonic-gate 	    gettext("cachefspack -[dipu] -[fBIL] [-h] [-r] [-s] [-U dir]"));
2580Sstevel@tonic-gate #else  /* DEBUG */
2590Sstevel@tonic-gate 	printf(
2600Sstevel@tonic-gate 	    gettext("cachefspack -[dipu] -[f] [-h] [-r] [-s] [-U dir]"));
2610Sstevel@tonic-gate #endif /* DEBUG */
2620Sstevel@tonic-gate 	printf(gettext(" [files]\n"));
2630Sstevel@tonic-gate 	printf("\n");
2640Sstevel@tonic-gate 	printf(
2650Sstevel@tonic-gate 	    gettext("Must select 1 and only 1 of the following 5 options\n"));
2660Sstevel@tonic-gate 	printf(gettext("-d Display selected filenames\n"));
2670Sstevel@tonic-gate 	printf(gettext("-i Display selected filenames packing status\n"));
2680Sstevel@tonic-gate 	printf(gettext("-p Pack selected filenames\n"));
2690Sstevel@tonic-gate 	printf(gettext("-u Unpack selected filenames\n"));
2700Sstevel@tonic-gate 	printf(gettext("-U Unpack all files in directory 'dir'\n"));
2710Sstevel@tonic-gate 	printf(gettext("\n"));
2720Sstevel@tonic-gate 	printf(gettext("-f Specify input file containing rules\n"));
2730Sstevel@tonic-gate #ifdef  DEBUG
2740Sstevel@tonic-gate 	printf(gettext("-B Specify BASE rule on command line\n"));
2750Sstevel@tonic-gate 	printf(gettext("-I Specify IGNORE rule on command line\n"));
2760Sstevel@tonic-gate 	printf(gettext("-L Specify LIST rule on command line\n"));
2770Sstevel@tonic-gate 	printf(gettext("\n"));
2780Sstevel@tonic-gate #endif /* DEBUG */
2790Sstevel@tonic-gate 	printf(gettext("-h Print usage information\n"));
2800Sstevel@tonic-gate 	printf(gettext(
2810Sstevel@tonic-gate 	    "-r Interpret strings in LIST rules as regular expressions\n"));
2820Sstevel@tonic-gate 	printf(gettext("-s Strip './' from the beginning of a pattern name\n"));
2830Sstevel@tonic-gate 	printf(gettext("-v Verbose option\n"));
2840Sstevel@tonic-gate 	printf(gettext("files - a list of filenames to be packed/unpacked\n"));
2850Sstevel@tonic-gate }
286