spec.c revision 44303
1/*-
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)spec.c	8.1 (Berkeley) 6/6/93";
37#endif
38static const char rcsid[] =
39	"$Id: spec.c,v 1.9 1999/01/12 02:58:23 jkoshy Exp $";
40#endif /* not lint */
41
42#include <sys/types.h>
43#include <sys/stat.h>
44#include <ctype.h>
45#include <err.h>
46#include <errno.h>
47#include <fts.h>
48#include <grp.h>
49#include <pwd.h>
50#include <stdio.h>
51#include <unistd.h>
52#include <vis.h>
53#include "mtree.h"
54#include "extern.h"
55
56int lineno;				/* Current spec line number. */
57
58static void	 set __P((char *, NODE *));
59static void	 unset __P((char *, NODE *));
60
61NODE *
62spec()
63{
64	register NODE *centry, *last;
65	register char *p;
66	NODE ginfo, *root;
67	int c_cur, c_next;
68	char buf[2048];
69
70	centry = last = root = NULL;
71	bzero(&ginfo, sizeof(ginfo));
72	c_cur = c_next = 0;
73	for (lineno = 1; fgets(buf, sizeof(buf), stdin);
74	    ++lineno, c_cur = c_next, c_next = 0) {
75		/* Skip empty lines. */
76		if (buf[0] == '\n')
77			continue;
78
79		/* Find end of line. */
80		if ((p = index(buf, '\n')) == NULL)
81			errx(1, "line %d too long", lineno);
82
83		/* See if next line is continuation line. */
84		if (p[-1] == '\\') {
85			--p;
86			c_next = 1;
87		}
88
89		/* Null-terminate the line. */
90		*p = '\0';
91
92		/* Skip leading whitespace. */
93		for (p = buf; *p && isspace(*p); ++p);
94
95		/* If nothing but whitespace or comment char, continue. */
96		if (!*p || *p == '#')
97			continue;
98
99#ifdef DEBUG
100		(void)fprintf(stderr, "line %d: {%s}\n", lineno, p);
101#endif
102		if (c_cur) {
103			set(p, centry);
104			continue;
105		}
106
107		/* Grab file name, "$", "set", or "unset". */
108		if ((p = strtok(p, "\n\t ")) == NULL)
109			errx(1, "line %d: missing field", lineno);
110
111		if (p[0] == '/')
112			switch(p[1]) {
113			case 's':
114				if (strcmp(p + 1, "set"))
115					break;
116				set(NULL, &ginfo);
117				continue;
118			case 'u':
119				if (strcmp(p + 1, "unset"))
120					break;
121				unset(NULL, &ginfo);
122				continue;
123			}
124
125		if (index(p, '/'))
126			errx(1, "line %d: slash character in file name",
127			lineno);
128
129		if (!strcmp(p, "..")) {
130			/* Don't go up, if haven't gone down. */
131			if (!root)
132				goto noparent;
133			if (last->type != F_DIR || last->flags & F_DONE) {
134				if (last == root)
135					goto noparent;
136				last = last->parent;
137			}
138			last->flags |= F_DONE;
139			continue;
140
141noparent:		errx(1, "line %d: no parent node", lineno);
142		}
143
144		if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
145			errx(1, "calloc");
146		*centry = ginfo;
147#define	MAGIC	"?*["
148		if (strpbrk(p, MAGIC))
149			centry->flags |= F_MAGIC;
150		if (strunvis(centry->name, p) == -1) {
151			warnx("filename %s is ill-encoded and literally used",
152			    p);
153			strcpy(centry->name, p);
154		}
155		set(NULL, centry);
156
157		if (!root) {
158			last = root = centry;
159			root->parent = root;
160		} else if (last->type == F_DIR && !(last->flags & F_DONE)) {
161			centry->parent = last;
162			last = last->child = centry;
163		} else {
164			centry->parent = last->parent;
165			centry->prev = last;
166			last = last->next = centry;
167		}
168	}
169	return (root);
170}
171
172static void
173set(t, ip)
174	char *t;
175	register NODE *ip;
176{
177	register int type;
178	register char *kw, *val = NULL;
179	struct group *gr;
180	struct passwd *pw;
181	mode_t *m;
182	int value;
183	char *ep;
184
185	for (; (kw = strtok(t, "= \t\n")); t = NULL) {
186		ip->flags |= type = parsekey(kw, &value);
187		if (value && (val = strtok(NULL, " \t\n")) == NULL)
188			errx(1, "line %d: missing value", lineno);
189		switch(type) {
190		case F_CKSUM:
191			ip->cksum = strtoul(val, &ep, 10);
192			if (*ep)
193				errx(1, "line %d: invalid checksum %s",
194				lineno, val);
195			break;
196		case F_MD5:
197			ip->md5digest = strdup(val);
198			if(!ip->md5digest) {
199				errx(1, "strdup");
200			}
201			break;
202		case F_SHA1:
203			ip->sha1digest = strdup(val);
204			if(!ip->sha1digest) {
205				errx(1, "strdup");
206			}
207			break;
208		case F_RMD160:
209			ip->rmd160digest = strdup(val);
210			if(!ip->rmd160digest) {
211				errx(1, "strdup");
212			}
213			break;
214		case F_GID:
215			ip->st_gid = strtoul(val, &ep, 10);
216			if (*ep)
217				errx(1, "line %d: invalid gid %s", lineno, val);
218			break;
219		case F_GNAME:
220			if ((gr = getgrnam(val)) == NULL)
221			    errx(1, "line %d: unknown group %s", lineno, val);
222			ip->st_gid = gr->gr_gid;
223			break;
224		case F_IGN:
225			/* just set flag bit */
226			break;
227		case F_MODE:
228			if ((m = setmode(val)) == NULL)
229				errx(1, "line %d: invalid file mode %s",
230				lineno, val);
231			ip->st_mode = getmode(m, 0);
232			free(m);
233			break;
234		case F_NLINK:
235			ip->st_nlink = strtoul(val, &ep, 10);
236			if (*ep)
237				errx(1, "line %d: invalid link count %s",
238				lineno,  val);
239			break;
240		case F_SIZE:
241			ip->st_size = strtoq(val, &ep, 10);
242			if (*ep)
243				errx(1, "line %d: invalid size %s",
244				lineno, val);
245			break;
246		case F_SLINK:
247			if ((ip->slink = strdup(val)) == NULL)
248				errx(1, "strdup");
249			break;
250		case F_TIME:
251			ip->st_mtimespec.tv_sec = strtoul(val, &ep, 10);
252			if (*ep != '.')
253				errx(1, "line %d: invalid time %s",
254				lineno, val);
255			val = ep + 1;
256			ip->st_mtimespec.tv_nsec = strtoul(val, &ep, 10);
257			if (*ep)
258				errx(1, "line %d: invalid time %s",
259				lineno, val);
260			break;
261		case F_TYPE:
262			switch(*val) {
263			case 'b':
264				if (!strcmp(val, "block"))
265					ip->type = F_BLOCK;
266				break;
267			case 'c':
268				if (!strcmp(val, "char"))
269					ip->type = F_CHAR;
270				break;
271			case 'd':
272				if (!strcmp(val, "dir"))
273					ip->type = F_DIR;
274				break;
275			case 'f':
276				if (!strcmp(val, "file"))
277					ip->type = F_FILE;
278				if (!strcmp(val, "fifo"))
279					ip->type = F_FIFO;
280				break;
281			case 'l':
282				if (!strcmp(val, "link"))
283					ip->type = F_LINK;
284				break;
285			case 's':
286				if (!strcmp(val, "socket"))
287					ip->type = F_SOCK;
288				break;
289			default:
290				errx(1, "line %d: unknown file type %s",
291				lineno, val);
292			}
293			break;
294		case F_UID:
295			ip->st_uid = strtoul(val, &ep, 10);
296			if (*ep)
297				errx(1, "line %d: invalid uid %s", lineno, val);
298			break;
299		case F_UNAME:
300			if ((pw = getpwnam(val)) == NULL)
301			    errx(1, "line %d: unknown user %s", lineno, val);
302			ip->st_uid = pw->pw_uid;
303			break;
304		}
305	}
306}
307
308static void
309unset(t, ip)
310	char *t;
311	register NODE *ip;
312{
313	register char *p;
314
315	while ((p = strtok(t, "\n\t ")))
316		ip->flags &= ~parsekey(p, NULL);
317}
318