summaryrefslogtreecommitdiff
path: root/usr/src/cmd/filesync/rules.c
blob: 7789ae5c4782f951eb812719b3d6e3d5f1ad46a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1995 Sun Microsystems, Inc.  All Rights Reserved
 *
 * module:
 *	rules.c
 *
 * purpose:
 *	to read and write the rules file and manage rules lists
 *
 * contents:
 *	reading rules file
 *		read_rules
 *		(static) read_command
 *	writing rules file
 *		write_rules
 *		(static) rw_header, rw_base
 *	adding rules
 *		add_ignore, add_include
 *		(static) add_rule
 *	adding/checking restrictions
 *		add_restr, check_restr
 */
#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>

#include "filesync.h"
#include "database.h"
#include "messages.h"
#include "debug.h"

/*
 * routines:
 */
static errmask_t rw_base(FILE *file, struct base *bp);
static errmask_t rw_header(FILE *file);
static errmask_t add_rule(struct base *, int, const char *);
static char *read_cmd(char *);

/*
 * globals
 */
static int rules_added;
static int restr_added;

/*
 * locals
 */
#define	RULE_MAJOR	1		/* rules file format major rev	*/
#define	RULE_MINOR	1		/* rules file format minor rev	*/
#define	RULE_TAG	"PACKINGRULES"	/* magic string for rules files	*/

/*
 * routine:
 *	read_rules
 *
 * purpose:
 *	to read in the rules file
 *
 * parameters:
 *	name of rules file
 *
 * returns:
 *	error mask
 *
 * notes:
 *	later when I implement a proper (comment preserving) update
 *	function I'm going to wish I had figured out how to build the
 *	input functions for this function in a way that would make
 *	the more usable for that too.
 */
errmask_t
read_rules(char *name)
{	FILE *file;
	errmask_t errs = 0;
	int flags;
	int major, minor;
	char *s, *s1, *s2;
	struct base *bp;
	char *errstr = "???";

	file = fopen(name, "r");
	if (file == NULL) {
		fprintf(stderr, gettext(ERR_open), gettext(TXT_rules),
			name);
		return (ERR_FILES);
	}

	lex_linenum = 0;

	if (opt_debug & DBG_FILES)
		fprintf(stderr, "FILE: READ RULES %s\n", name);

	bp = &omnibase;		/* default base before any others	*/

	while (!feof(file)) {
		/* find the first token on the line	*/
		s = lex(file);

		/* skip blank lines and comments	*/
		if (s == 0 || *s == 0 || *s == '#' || *s == '*')
			continue;

		/* see if the first token is a known keyword	*/
		if (strcmp(s, "BASE") == 0) {

			/* get the source & destination tokens	*/
			errstr = gettext(TXT_srcdst);
			s1 = lex(0);
			if (s1 == 0)
				goto bad;
			s1 = strdup(s1);

			s2 = lex(0);
			if (s2 == 0)
				goto bad;
			s2 = strdup(s2);

			/* creat the new base pair		*/
			bp = add_base(s1, s2);
			bp->b_flags |= F_LISTED;

			free(s1);
			free(s2);
			continue;
		}

		if (strcmp(s, "LIST") == 0) {

			/* make sure we are associated with a real base */
			if (bp == &omnibase) {
				errstr = gettext(TXT_nobase);
				goto bad;
			}

			/* skip to the next token */
			s = lex(0);
			errstr = gettext(TXT_noargs);
			if (s == 0)
				goto bad;

			/* see if it is a program or a name */
			if (*s == '!') {
				errs |= add_rule(bp, R_PROGRAM,
						read_cmd(&s[1]));
			} else {
				do {
					flags = wildcards(s) ? R_WILD : 0;
					errs |= add_rule(bp, flags, s);
					s = lex(0);
				} while (s != 0);
			}
			continue;
		}

		if (strcmp(s, "IGNORE") == 0) {

			/* skip to the next token */
			s = lex(0);
			errstr = gettext(TXT_noargs);
			if (s == 0)
				goto bad;

			flags = R_IGNORE;

			/* see if it is a program or a name */
			if (*s == '!') {
				errs |= add_rule(bp, R_PROGRAM|flags,
						read_cmd(&s[1]));
			} else {
				do {
					if (wildcards(s))
						flags |= R_WILD;
					errs |= add_rule(bp, flags, s);
					s = lex(0);
				} while (s != 0);
			}
			continue;
		}

		if (strcmp(s, "VERSION") == 0 || strcmp(s, RULE_TAG) == 0) {
			s = lex(0);
			errstr = gettext(TXT_noargs);
			if (s == 0)
				goto bad;

			major = strtol(s, &s1, 10);
			errstr = gettext(TXT_badver);
			if (*s1 != '.')
				goto bad;
			minor = strtol(&s1[1], 0, 10);

			if (major != RULE_MAJOR || minor > RULE_MINOR) {
				fprintf(stderr, gettext(ERR_badver),
					major, minor, gettext(TXT_rules), name);
				errs |= ERR_FILES;
			}
			continue;
		}

	bad:	/* log the error and continue processing to find others	*/
		fprintf(stderr, gettext(ERR_badinput),
			lex_linenum, errstr, name);
		errs |= ERR_FILES;
	}


	(void) fclose(file);
	return (errs);
}

/*
 * routine:
 *	read_cmd
 *
 * purpose:
 *	to lex a runnable command (! lines) into a buffer
 *
 * parameters:
 *	first token
 *
 * returns:
 *	pointer to a command line in a static buffer
 *	(it is assumed the caller will copy it promptly)
 *
 * notes:
 *	this is necessary because lex has already choped off
 *	the first token for us
 */
static char *read_cmd(char * s)
{
	static char cmdbuf[ MAX_LINE ];

	cmdbuf[0] = 0;

	do {
		if (*s) {
			strcat(cmdbuf, s);
			strcat(cmdbuf, " ");
		}
	} while ((s = lex(0)) != 0);

	return (cmdbuf);
}

/*
 * routine:
 *	write_rules
 *
 * purpose:
 *	to rewrite the rules file, appending the new rules
 *
 * parameters:
 *	name of output file
 *
 * returns:
 *	error mask
 *
 */
errmask_t
write_rules(char *name)
{	FILE *newfile;
	errmask_t errs = 0;
	struct base *bp;
	char tmpname[ MAX_PATH ];

	/* if no-touch is specified, we don't update files	*/
	if (opt_notouch || rules_added == 0)
		return (0);

	/* create a temporary output file			*/
	sprintf(tmpname, "%s-TMP", name);

	/* create our output file	*/
	newfile = fopen(tmpname, "w+");
	if (newfile == NULL) {
		fprintf(stderr, gettext(ERR_creat), gettext(TXT_rules),
			name);
		return (ERR_FILES);
	}

	if (opt_debug & DBG_FILES)
		fprintf(stderr, "FILE: UPDATE RULES %s\n", name);

	errs |= rw_header(newfile);
	errs |= rw_base(newfile, &omnibase);
	for (bp = bases; bp; bp = bp->b_next)
		errs |= rw_base(newfile, bp);

	if (ferror(newfile)) {
		fprintf(stderr, gettext(ERR_write), gettext(TXT_rules),
			tmpname);
		errs |= ERR_FILES;
	}

	if (fclose(newfile)) {
		fprintf(stderr, gettext(ERR_fclose), gettext(TXT_rules),
			tmpname);
		errs |= ERR_FILES;
	}

	/* now switch the new file for the old one	*/
	if (errs == 0)
		if (rename(tmpname, name) != 0) {
			fprintf(stderr, gettext(ERR_rename),
				gettext(TXT_rules), tmpname, name);
			errs |= ERR_FILES;
		}

	return (errs);
}

/*
 * routine:
 *	rw_header
 *
 * purpose:
 *	to write out a rules header
 *
 * parameters:
 *	FILE* for the output file
 *
 * returns:
 *	error mask
 *
 * notes:
 */
static errmask_t rw_header(FILE *file)
{
	time_t now;
	struct tm *local;

	/* figure out what time it is	*/
	(void) time(&now);
	local = localtime(&now);

	fprintf(file, "%s %d.%d\n", RULE_TAG, RULE_MAJOR, RULE_MINOR);
	fprintf(file, "#\n");
	fprintf(file, "# filesync rules, last written by %s, %s",
		cuserid((char *) 0), asctime(local));
	fprintf(file, "#\n");

	return (0);
}

/*
 * routine:
 *	rw_base
 *
 * purpose:
 *	to write out the summary for one base-pair
 *
 * parameters:
 *	FILE * for the output file
 *
 * returns:
 *	error mask
 *
 * notes:
 */
static errmask_t rw_base(FILE *file, struct base *bp)
{	struct rule *rp;

	fprintf(file, "\n");

	/* global rules don't appear within a base */
	if (bp->b_ident)
		fprintf(file, "BASE %s %s\n", noblanks(bp->b_src_spec),
				noblanks(bp->b_dst_spec));

	for (rp = bp->b_includes; rp; rp = rp->r_next)
		if (rp->r_flags & R_PROGRAM)
			fprintf(file, "LIST !%s\n", rp->r_file);
		else
			fprintf(file, "LIST %s\n", noblanks(rp->r_file));

	for (rp = bp->b_excludes; rp; rp = rp->r_next)
		if (rp->r_flags & R_PROGRAM)
			fprintf(file, "IGNORE !%s\n", rp->r_file);
		else
			fprintf(file, "IGNORE %s\n", noblanks(rp->r_file));

	return (0);
}

/*
 * routine:
 *	add_rule
 *
 * purpose:
 *	to add a new rule
 *
 * parameters:
 *	pointer to list base
 *	rule flags
 *	associated name/arguments
 *
 * returns:
 *	error flags
 *
 * notes:
 *	we always copy the argument string because most of them
 *	were read from a file and are just in a transient buffer
 */
static errmask_t add_rule(struct base *bp, int flags, const char *args)
{	struct rule *rp;
	struct rule **list;

	rp = malloc(sizeof (struct rule));
	if (rp == 0)
		nomem("rule struture");

	/* initialize the new base			*/
	memset((void *) rp, 0, sizeof (struct rule));
	rp->r_flags = flags;
	rp->r_file = strdup(args);

	/* figure out which list to put it on		*/
	if (flags&R_IGNORE)
		list = &bp->b_excludes;
	else if (flags&R_RESTRICT)
		list = &bp->b_restrictions;
	else
		list = &bp->b_includes;

	while (*list)
		list = &((*list)->r_next);
	*list = rp;

	if (flags & R_NEW)
		rules_added++;

	if (opt_debug & DBG_RULE) {
		fprintf(stderr, "RULE: base=%d, ", bp->b_ident);
		fprintf(stderr, "flags=%s, ",
			showflags(rflags, rp->r_flags));
		fprintf(stderr, "arg=%s\n", rp->r_file);
	}

	return (0);
}

/*
 * routine:
 *	add_ignore, add_include
 *
 * purpose:
 *	wrappers for add_rule that permit outsiders (like main.c)
 *	not to know what is inside of a base, file, or list entry
 *
 * parameters:
 *	base under which rules should be added
 *	argument associated with rule
 *
 * returns:
 *	error flags
 *
 * notes:
 *	basically these routines figure out what the right
 *	flags are for a rule, and what list to put it on,
 *	and then call a common handler.
 */
errmask_t
add_ignore(struct base *bp, char *name)
{	int flags = R_IGNORE | R_NEW;

	if (bp == 0)
		bp = &omnibase;

	if (wildcards(name))
		flags |= R_WILD;

	return (add_rule(bp, flags, name));
}

errmask_t
add_include(struct base *bp, char *name)
{	int flags = R_NEW;

	if (bp == 0)
		bp = &omnibase;

	if (wildcards(name))
		flags |= R_WILD;

	bp->b_flags |= F_LISTED;

	return (add_rule(bp, flags, name));
}

/*
 * routine:
 *	add_restr
 *
 * purpose:
 *	to add a restriction to a base
 *
 * parameters:
 *	address of base
 *	restriction string
 *
 * returns:
 * 	error mask
 *
 * notes:
 *	a restriction is specified on the command line and
 *	tells us to limit our analysis/reconcilation to
 *	specified files and/or directories.  We deal with
 *	these by adding a restriction rule to any base that
 *	looks like it might fit the restriction.  We need to
 *	treat this as a rule because the restriction string
 *	may extend beyond the base directory and part-way into
 *	its tree ... meaning that individual file names under
 *	the base will have to be checked against the restriction.
 */
errmask_t
add_restr(char *restr)
{	const char *s;
	errmask_t errs = 0;
	struct base *bp;

	for (bp = bases; bp; bp = bp->b_next) {
		/*
		 * see if this restriction could apply to this base.
		 * It could match either the source or destination
		 * directory name for this base.  If it matches neither
		 * then the restriction does not apply to this base.
		 */
		s = prefix(restr, bp->b_src_name);
		if (s == 0)
			s = prefix(restr, bp->b_dst_name);
		if (s == 0)
			continue;

		/*
		 * if there is more restriction string after the
		 * base, we will need to note the remainder of the
		 * string so that we can match individual files
		 * against it.
		 */
		if (*s == '/')
			s++;

		errs |= add_rule(bp, R_RESTRICT, s);
		restr_added++;
	}

	return (errs);
}

/*
 * routine:
 *	check_restr
 *
 * purpose:
 *	to see if an argument falls within restrictions
 *
 * parameters:
 *	pointer to relevant base
 *	file name
 *
 * returns:
 *	TRUE	name is within restrictions
 *	FALSE	name is outside of restrictions
 *	MAYBE	name is on the path to a restriction
 *
 * notes:
 *	if no restrictions have been specified, we evaluate
 *	everything.  If any restrictions have been specified,
 *	we process only files that match one of the restrictions.
 *
 *	add_restr has ensured that if the restriction includes
 *	a portion that must be matched by individual files under
 *	the base, that the restriction rule will contain that
 *	portion of the restriction which must be matched against
 *	individual file names.
 */
bool_t
check_restr(struct base *bp, const char *name)
{	struct rule *rp;

	/* if there are no restrictions, everything is OK	*/
	if (restr_added == 0)
		return (TRUE);

	/* now we have to run through the list			*/
	for (rp = bp->b_restrictions; rp; rp = rp->r_next) {
		/* see if current path is under the restriction	*/
		if (prefix(name, rp->r_file))
			return (TRUE);

		/* see if current path is on the way to restr	*/
		if (prefix(rp->r_file, name))
			/*
			 * this is kinky, but walker really needs
			 * to know the difference between a directory
			 * that we are unreservedly scanning, and one
			 * that we are scanning only to find something
			 * beneath it.
			 */
			return (MAYBE);
	}

	/*
	 * there are restrictions in effect and this file doesn't seem
	 * to meet any of them
	 */
	if (opt_debug & DBG_RULE)
		fprintf(stderr, "RULE: FAIL RESTRICTION base=%d, file=%s\n",
			bp->b_ident, name);

	return (FALSE);
}