xref: /onnv-gate/usr/src/cmd/filesync/ignore.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1995 Sun Microsystems, Inc.  All Rights Reserved
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * module:
26*0Sstevel@tonic-gate  *	ignore.c
27*0Sstevel@tonic-gate  *
28*0Sstevel@tonic-gate  * purpose:
29*0Sstevel@tonic-gate  *	routines to manage the ignore lists and test names against them,
30*0Sstevel@tonic-gate  *
31*0Sstevel@tonic-gate  * contents:
32*0Sstevel@tonic-gate  *	ignore_check ... is a particular file covered by an ignore rule
33*0Sstevel@tonic-gate  *	ignore_file .... add a specific file name to be ignored
34*0Sstevel@tonic-gate  *	ignore_expr .... add a regular expression for files to be ignored
35*0Sstevel@tonic-gate  *	ignore_pgm ..... add a rule to run a program to generate a list
36*0Sstevel@tonic-gate  *	ignore_reset ... flush the internal optimization data structures
37*0Sstevel@tonic-gate  *
38*0Sstevel@tonic-gate  *	static
39*0Sstevel@tonic-gate  *	    ign_hash ... maintain a hash table of ignored names
40*0Sstevel@tonic-gate  *	    cheap_check. build up a table of safe suffixes
41*0Sstevel@tonic-gate  *
42*0Sstevel@tonic-gate  * notes:
43*0Sstevel@tonic-gate  *	a much simpler implementation could have been provided, but
44*0Sstevel@tonic-gate  *	this test (every file tested against every rule) has the
45*0Sstevel@tonic-gate  *	potential to be EXTREMELY expensive.  This module implements
46*0Sstevel@tonic-gate  *	an engine that attempts to optimize the process of determining
47*0Sstevel@tonic-gate  *	that a file has not been ignored.
48*0Sstevel@tonic-gate  *
49*0Sstevel@tonic-gate  *	the usage scenario is
50*0Sstevel@tonic-gate  *	    per base
51*0Sstevel@tonic-gate  *		call ignore_{file,expr,pgm} for each ignore rule
52*0Sstevel@tonic-gate  *		call ignore_check for every file under the base
53*0Sstevel@tonic-gate  *		call ignore_reset when you are done
54*0Sstevel@tonic-gate  */
55*0Sstevel@tonic-gate #ident	"%W%	%E% SMI"
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate #include <stdio.h>
58*0Sstevel@tonic-gate #include <stdlib.h>
59*0Sstevel@tonic-gate #include <string.h>
60*0Sstevel@tonic-gate #include <libgen.h>
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate #include "filesync.h"
63*0Sstevel@tonic-gate #include "messages.h"
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate /*
66*0Sstevel@tonic-gate  * routines:
67*0Sstevel@tonic-gate  */
68*0Sstevel@tonic-gate static struct list *ign_hash(const char *, int);
69*0Sstevel@tonic-gate static void cheap_check(const char *);
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate /*
72*0Sstevel@tonic-gate  * globals
73*0Sstevel@tonic-gate  */
74*0Sstevel@tonic-gate struct list {
75*0Sstevel@tonic-gate 	char *l_value;			/* the actual string		*/
76*0Sstevel@tonic-gate 	struct list *l_next;		/* pointer to next element	*/
77*0Sstevel@tonic-gate };
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate static struct list *expr_list;		/* list of regular expressions	*/
80*0Sstevel@tonic-gate static struct list *file_list[ HASH_SIZE ]; /* hash table of literal names */
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate static char cheap_last[256];		/* cheap test: last char	*/
83*0Sstevel@tonic-gate static char cheap_penu[256];		/* cheap test: penultimate char	*/
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate /*
86*0Sstevel@tonic-gate  * routine:
87*0Sstevel@tonic-gate  *	ignore_check
88*0Sstevel@tonic-gate  *
89*0Sstevel@tonic-gate  * purpose:
90*0Sstevel@tonic-gate  *	determine whether or not a particular name matches an ignore pattern.
91*0Sstevel@tonic-gate  *
92*0Sstevel@tonic-gate  * parameters:
93*0Sstevel@tonic-gate  *	file name
94*0Sstevel@tonic-gate  *
95*0Sstevel@tonic-gate  * returns:
96*0Sstevel@tonic-gate  *	true/false
97*0Sstevel@tonic-gate  *
98*0Sstevel@tonic-gate  * note:
99*0Sstevel@tonic-gate  *	becuse this routine is called on every single file in
100*0Sstevel@tonic-gate  *	every single sub-directory, it is critical that we make
101*0Sstevel@tonic-gate  *	it fail quickly for most files.  The purpose of the cheap_last
102*0Sstevel@tonic-gate  *	and cheap_penu arrays is to quickly determine there is no chance
103*0Sstevel@tonic-gate  *	that a name will match any expression.  Most expressions have
104*0Sstevel@tonic-gate  *	wildcards near the front and constant suffixes, so our cheap
105*0Sstevel@tonic-gate  *	test is to look at the last two bytes.
106*0Sstevel@tonic-gate  */
107*0Sstevel@tonic-gate bool_t
ignore_check(const char * name)108*0Sstevel@tonic-gate ignore_check(const char *name)
109*0Sstevel@tonic-gate {	struct list *lp;
110*0Sstevel@tonic-gate 	const char *s;
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	/*
113*0Sstevel@tonic-gate 	 * start with the cheap test
114*0Sstevel@tonic-gate 	 */
115*0Sstevel@tonic-gate 	for (s = name; *s; s++);
116*0Sstevel@tonic-gate 	if (cheap_last[ (unsigned char) s[-1] ] == 0 ||
117*0Sstevel@tonic-gate 	    cheap_penu[ (unsigned char) s[-2] ] == 0)
118*0Sstevel@tonic-gate 		return (FALSE);
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	/* check the literal names in the hash table		*/
121*0Sstevel@tonic-gate 	if (ign_hash(name, 0)) {
122*0Sstevel@tonic-gate 		if (opt_debug & DBG_IGNORE)
123*0Sstevel@tonic-gate 			fprintf(stderr, "IGNO: match %s\n", name);
124*0Sstevel@tonic-gate 		return (TRUE);
125*0Sstevel@tonic-gate 	}
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	/* check all the regular expressions			*/
128*0Sstevel@tonic-gate 	for (lp = expr_list; lp; lp = lp->l_next) {
129*0Sstevel@tonic-gate 		if (gmatch(name, lp->l_value) == 0)
130*0Sstevel@tonic-gate 			continue;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 		if (opt_debug & DBG_IGNORE)
133*0Sstevel@tonic-gate 			fprintf(stderr, "IGNO: regex %s : %s\n",
134*0Sstevel@tonic-gate 				lp->l_value, name);
135*0Sstevel@tonic-gate 		return (TRUE);
136*0Sstevel@tonic-gate 	}
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	return (FALSE);
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate /*
142*0Sstevel@tonic-gate  * routine:
143*0Sstevel@tonic-gate  *	ignore_file
144*0Sstevel@tonic-gate  *
145*0Sstevel@tonic-gate  * purpose:
146*0Sstevel@tonic-gate  *	to add a specific file to an ignore list
147*0Sstevel@tonic-gate  *
148*0Sstevel@tonic-gate  * parameters:
149*0Sstevel@tonic-gate  *	command to run
150*0Sstevel@tonic-gate  */
151*0Sstevel@tonic-gate void
ignore_file(const char * name)152*0Sstevel@tonic-gate ignore_file(const char *name)
153*0Sstevel@tonic-gate {
154*0Sstevel@tonic-gate 	cheap_check(name);
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	(void) ign_hash(name, 1);
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	if (opt_debug & DBG_IGNORE)
159*0Sstevel@tonic-gate 		fprintf(stderr, "IGNO: add file %s\n", name);
160*0Sstevel@tonic-gate }
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate /*
163*0Sstevel@tonic-gate  * routine:
164*0Sstevel@tonic-gate  *	ignore_expr
165*0Sstevel@tonic-gate  *
166*0Sstevel@tonic-gate  * purpose:
167*0Sstevel@tonic-gate  *	to add a regular expression to an ignore list
168*0Sstevel@tonic-gate  *
169*0Sstevel@tonic-gate  * parameters:
170*0Sstevel@tonic-gate  *	command to run
171*0Sstevel@tonic-gate  */
172*0Sstevel@tonic-gate void
ignore_expr(const char * expr)173*0Sstevel@tonic-gate ignore_expr(const char *expr)
174*0Sstevel@tonic-gate {	struct list *lp;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	cheap_check(expr);
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	/* allocate a new node and stick it on the front of the list	*/
179*0Sstevel@tonic-gate 	lp = malloc(sizeof (*lp));
180*0Sstevel@tonic-gate 	if (lp == 0)
181*0Sstevel@tonic-gate 		nomem("ignore list");
182*0Sstevel@tonic-gate 	lp->l_value = strdup(expr);
183*0Sstevel@tonic-gate 	lp->l_next = expr_list;
184*0Sstevel@tonic-gate 	expr_list = lp;
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	if (opt_debug & DBG_IGNORE)
187*0Sstevel@tonic-gate 		fprintf(stderr, "IGNO: add expr %s\n", expr);
188*0Sstevel@tonic-gate }
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate /*
191*0Sstevel@tonic-gate  * routine:
192*0Sstevel@tonic-gate  *	ignore_pgm
193*0Sstevel@tonic-gate  *
194*0Sstevel@tonic-gate  * purpose:
195*0Sstevel@tonic-gate  *	to run a program and gather up the ignore list it produces
196*0Sstevel@tonic-gate  *
197*0Sstevel@tonic-gate  * parameters:
198*0Sstevel@tonic-gate  *	command to run
199*0Sstevel@tonic-gate  */
200*0Sstevel@tonic-gate void
ignore_pgm(const char * cmd)201*0Sstevel@tonic-gate ignore_pgm(const char *cmd)
202*0Sstevel@tonic-gate {	char *s;
203*0Sstevel@tonic-gate 	FILE *fp;
204*0Sstevel@tonic-gate 	char inbuf[ MAX_LINE ];
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	if (opt_debug & DBG_IGNORE)
207*0Sstevel@tonic-gate 		fprintf(stderr, "IGNO: add pgm %s\n", cmd);
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	/* run the command and collect its ouput	*/
210*0Sstevel@tonic-gate 	fp = popen(cmd, "r");
211*0Sstevel@tonic-gate 	if (fp == NULL) {
212*0Sstevel@tonic-gate 		fprintf(stderr, gettext(ERR_badrun), cmd);
213*0Sstevel@tonic-gate 		return;
214*0Sstevel@tonic-gate 	}
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	/*
217*0Sstevel@tonic-gate 	 * read each line, strip off the newline and add it to the list
218*0Sstevel@tonic-gate 	 */
219*0Sstevel@tonic-gate 	while (fgets(inbuf, sizeof (inbuf), fp) != 0) {
220*0Sstevel@tonic-gate 		/* strip off any trailing newline	*/
221*0Sstevel@tonic-gate 		for (s = inbuf; *s && *s != '\n'; s++);
222*0Sstevel@tonic-gate 		*s = 0;
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 		/* skip any leading white space		*/
225*0Sstevel@tonic-gate 		for (s = inbuf; *s == ' ' || *s == '\t'; s++);
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 		/* add this file to the list		*/
228*0Sstevel@tonic-gate 		if (*s) {
229*0Sstevel@tonic-gate 			cheap_check(s);
230*0Sstevel@tonic-gate 			(void) ign_hash(s, 1);
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 			if (opt_debug & DBG_IGNORE)
233*0Sstevel@tonic-gate 				fprintf(stderr, "IGNO: ... %s\n", s);
234*0Sstevel@tonic-gate 		}
235*0Sstevel@tonic-gate 	}
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 	pclose(fp);
238*0Sstevel@tonic-gate }
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate /*
241*0Sstevel@tonic-gate  * routine:
242*0Sstevel@tonic-gate  *	ign_hash
243*0Sstevel@tonic-gate  *
244*0Sstevel@tonic-gate  * purpose:
245*0Sstevel@tonic-gate  *	to find an entry in the hash list
246*0Sstevel@tonic-gate  *
247*0Sstevel@tonic-gate  * parameters:
248*0Sstevel@tonic-gate  *	name
249*0Sstevel@tonic-gate  *	allocate flag
250*0Sstevel@tonic-gate  *
251*0Sstevel@tonic-gate  * returns:
252*0Sstevel@tonic-gate  *	pointer to new list entry or 0
253*0Sstevel@tonic-gate  */
254*0Sstevel@tonic-gate static struct list *
ign_hash(const char * name,int alloc)255*0Sstevel@tonic-gate ign_hash(const char *name, int alloc)
256*0Sstevel@tonic-gate {	const unsigned char *s;
257*0Sstevel@tonic-gate 	int i;
258*0Sstevel@tonic-gate 	struct list *lp;
259*0Sstevel@tonic-gate 	struct list **pp;
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	/* perform the hash and find the chain	*/
262*0Sstevel@tonic-gate 	for (s = (const unsigned char *) name, i = 0; *s; s++)
263*0Sstevel@tonic-gate 		i += *s;
264*0Sstevel@tonic-gate 	pp = &file_list[i % HASH_SIZE ];
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	/* search for the specified entry	*/
267*0Sstevel@tonic-gate 	for (lp = *pp; lp; lp = *pp) {
268*0Sstevel@tonic-gate 		if (strcmp(name, lp->l_value) == 0)
269*0Sstevel@tonic-gate 			return (lp);
270*0Sstevel@tonic-gate 		pp = &(lp->l_next);
271*0Sstevel@tonic-gate 	}
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	/* if caller said alloc, buy a new node and chain it in	*/
274*0Sstevel@tonic-gate 	if (alloc) {
275*0Sstevel@tonic-gate 		lp = malloc(sizeof (*lp));
276*0Sstevel@tonic-gate 		if (lp == 0)
277*0Sstevel@tonic-gate 			nomem("ignore list");
278*0Sstevel@tonic-gate 		lp->l_value = strdup(name);
279*0Sstevel@tonic-gate 		lp->l_next = 0;
280*0Sstevel@tonic-gate 		*pp = lp;
281*0Sstevel@tonic-gate 	}
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 	return (lp);
284*0Sstevel@tonic-gate }
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate /*
287*0Sstevel@tonic-gate  * routine:
288*0Sstevel@tonic-gate  *	cheap_check
289*0Sstevel@tonic-gate  *
290*0Sstevel@tonic-gate  * purpose:
291*0Sstevel@tonic-gate  *	to update the cheap-check arrays for an ignore expression
292*0Sstevel@tonic-gate  *
293*0Sstevel@tonic-gate  * parameters:
294*0Sstevel@tonic-gate  *	name/expression
295*0Sstevel@tonic-gate  */
296*0Sstevel@tonic-gate static void
cheap_check(const char * name)297*0Sstevel@tonic-gate cheap_check(const char *name)
298*0Sstevel@tonic-gate {	const char *s;
299*0Sstevel@tonic-gate 	unsigned char c;
300*0Sstevel@tonic-gate 	int i;
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	for (s = name; *s; s++);
303*0Sstevel@tonic-gate 	s--;
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 	/* if expr ends in a wild card, we are undone		*/
306*0Sstevel@tonic-gate 	c = *s;
307*0Sstevel@tonic-gate 	if (c == '*' || c == '?' || c == ']' || c == '}') {
308*0Sstevel@tonic-gate 		for (i = 0; i < 256; i++) {
309*0Sstevel@tonic-gate 			cheap_last[i] = 1;
310*0Sstevel@tonic-gate 			cheap_penu[i] = 1;
311*0Sstevel@tonic-gate 		}
312*0Sstevel@tonic-gate 		return;
313*0Sstevel@tonic-gate 	} else
314*0Sstevel@tonic-gate 		cheap_last[c] = 1;
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	if (s <= name)
317*0Sstevel@tonic-gate 		return;
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 	/* check the next to last character too		*/
320*0Sstevel@tonic-gate 	c = s[-1];
321*0Sstevel@tonic-gate 	if (c == '*' || c == '?' || c == ']' || c == '}') {
322*0Sstevel@tonic-gate 		for (i = 0; i < 256; i++)
323*0Sstevel@tonic-gate 			cheap_penu[i] = 1;
324*0Sstevel@tonic-gate 	} else
325*0Sstevel@tonic-gate 		cheap_penu[c] = 1;
326*0Sstevel@tonic-gate }
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate /*
329*0Sstevel@tonic-gate  * routine:
330*0Sstevel@tonic-gate  *	ignore_reset
331*0Sstevel@tonic-gate  *
332*0Sstevel@tonic-gate  * purpose:
333*0Sstevel@tonic-gate  *	to free up all the ignore entries so we can start anew
334*0Sstevel@tonic-gate  */
335*0Sstevel@tonic-gate void
ignore_reset(void)336*0Sstevel@tonic-gate ignore_reset(void)
337*0Sstevel@tonic-gate {	int i;
338*0Sstevel@tonic-gate 	struct list *np = 0;	/* for LINT */
339*0Sstevel@tonic-gate 	struct list *lp;
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 	/* clear the cheap check arrays */
342*0Sstevel@tonic-gate 	for (i = 0; i < 255; i++) {
343*0Sstevel@tonic-gate 		cheap_last[i] = 0;
344*0Sstevel@tonic-gate 		cheap_penu[i] = 0;
345*0Sstevel@tonic-gate 	}
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	/* free all of the literal hash chains	*/
348*0Sstevel@tonic-gate 	for (i = 0; i < HASH_SIZE; i++) {
349*0Sstevel@tonic-gate 		for (lp = file_list[i]; lp; lp = np) {
350*0Sstevel@tonic-gate 			np = lp->l_next;
351*0Sstevel@tonic-gate 			free(lp->l_value);
352*0Sstevel@tonic-gate 			free(lp);
353*0Sstevel@tonic-gate 		}
354*0Sstevel@tonic-gate 		file_list[i] = 0;
355*0Sstevel@tonic-gate 	}
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	/* free all of the expressions on the chain	*/
358*0Sstevel@tonic-gate 	for (lp = expr_list; lp; lp = np) {
359*0Sstevel@tonic-gate 		np = lp->l_next;
360*0Sstevel@tonic-gate 		free(lp->l_value);
361*0Sstevel@tonic-gate 		free(lp);
362*0Sstevel@tonic-gate 	}
363*0Sstevel@tonic-gate 	expr_list = 0;
364*0Sstevel@tonic-gate }
365