spec.c revision 1.34 1 /* $NetBSD: spec.c,v 1.34 2001/10/22 07:07:46 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 /*-
37 * Copyright (c) 2001 The NetBSD Foundation, Inc.
38 * All rights reserved.
39 *
40 * This code is derived from software contributed to The NetBSD Foundation
41 * by Luke Mewburn of Wasabi Systems.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the NetBSD
54 * Foundation, Inc. and its contributors.
55 * 4. Neither the name of The NetBSD Foundation nor the names of its
56 * contributors may be used to endorse or promote products derived
57 * from this software without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
60 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
61 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
62 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
63 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
69 * POSSIBILITY OF SUCH DAMAGE.
70 */
71
72 #include <sys/cdefs.h>
73 #ifndef lint
74 #if 0
75 static char sccsid[] = "@(#)spec.c 8.2 (Berkeley) 4/28/95";
76 #else
77 __RCSID("$NetBSD: spec.c,v 1.34 2001/10/22 07:07:46 lukem Exp $");
78 #endif
79 #endif /* not lint */
80
81 #include <sys/param.h>
82 #include <sys/stat.h>
83
84 #include <ctype.h>
85 #include <errno.h>
86 #include <fts.h>
87 #include <grp.h>
88 #include <pwd.h>
89 #include <stdio.h>
90 #include <stdlib.h>
91 #include <string.h>
92 #include <unistd.h>
93 #include <util.h>
94 #include <vis.h>
95
96 #include "mtree.h"
97 #include "extern.h"
98 #include "pack_dev.h"
99
100 size_t lineno; /* Current spec line number. */
101
102 static dev_t parsedev(char *);
103 static void set(char *, NODE *);
104 static void unset(char *, NODE *);
105
106 NODE *
107 spec(FILE *fp)
108 {
109 NODE *centry, *last, *pathparent, *cur;
110 char *p, *e, *next;
111 NODE ginfo, *root;
112 char *buf, *tname;
113 size_t tnamelen, plen;
114
115 root = NULL;
116 centry = last = NULL;
117 tname = NULL;
118 tnamelen = 0;
119 memset(&ginfo, 0, sizeof(ginfo));
120 for (lineno = 0;
121 (buf = fparseln(fp, NULL, &lineno, NULL,
122 FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT | FPARSELN_UNESCESC));
123 free(buf)) {
124 /* Skip leading whitespace. */
125 for (p = buf; *p && isspace((unsigned char)*p); ++p)
126 continue;
127
128 /* If nothing but whitespace, continue. */
129 if (!*p)
130 continue;
131
132 #ifdef DEBUG
133 (void)fprintf(stderr, "line %d: {%s}\n", lineno, p);
134 #endif
135 /* Grab file name, "$", "set", or "unset". */
136 next = buf;
137 while ((p = strsep(&next, " \t")) != NULL && *p == '\0')
138 continue;
139 if (p == NULL)
140 mtree_err("missing field");
141
142 if (p[0] == '/') {
143 if (strcmp(p + 1, "set") == 0)
144 set(next, &ginfo);
145 else if (strcmp(p + 1, "unset") == 0)
146 unset(next, &ginfo);
147 else
148 mtree_err("invalid specification `%s'", p);
149 continue;
150 }
151
152 if (strcmp(p, "..") == 0) {
153 /* Don't go up, if haven't gone down. */
154 if (root == NULL)
155 goto noparent;
156 if (last->type != F_DIR || last->flags & F_DONE) {
157 if (last == root)
158 goto noparent;
159 last = last->parent;
160 }
161 last->flags |= F_DONE;
162 continue;
163
164 noparent: mtree_err("no parent node");
165 }
166
167 plen = strlen(p) + 1;
168 if (plen > tnamelen) {
169 tnamelen = plen;
170 if ((tname = realloc(tname, tnamelen)) == NULL)
171 mtree_err("realloc: %s", strerror(errno));
172 }
173 if (strunvis(tname, p) == -1)
174 mtree_err("strunvis failed on `%s'", p);
175 p = tname;
176
177 pathparent = NULL;
178 if (strchr(p, '/') != NULL) {
179 cur = root;
180 for (; (e = strchr(p, '/')) != NULL; p = e+1) {
181 if (p == e)
182 continue; /* handle // */
183 *e = '\0';
184 if (strcmp(p, ".") != 0) {
185 while (cur && strcmp(cur->name, p)) {
186 cur = cur->next;
187 }
188 }
189 if (cur == NULL || cur->type != F_DIR) {
190 mtree_err("%s: %s", tname,
191 strerror(ENOENT));
192 }
193 *e = '/';
194 pathparent = cur;
195 cur = cur->child;
196 }
197 if (*p == '\0')
198 mtree_err("%s: empty leaf element", tname);
199 for (; cur != NULL; cur = cur->next) {
200 if (strcmp(cur->name, p) == 0)
201 mtree_err("%s: %s", p,
202 strerror(EEXIST));
203 }
204 }
205
206 if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
207 mtree_err("%s", strerror(errno));
208 *centry = ginfo;
209 strcpy(centry->name, p);
210 #define MAGIC "?*["
211 if (strpbrk(p, MAGIC))
212 centry->flags |= F_MAGIC;
213 set(next, centry);
214
215 if (root == NULL) {
216 if (strcmp(centry->name, ".") || centry->type != F_DIR)
217 mtree_err(
218 "root node must be the directory `.'");
219 last = root = centry;
220 root->parent = root;
221 } else if (pathparent != NULL) {
222 centry->parent = pathparent;
223 cur = pathparent->child;
224 if (cur == NULL)
225 pathparent->child = centry;
226 else {
227 while (cur->next != NULL)
228 cur = cur->next;
229 cur->next = centry;
230 centry->prev = cur;
231 }
232 last = centry;
233 } else if (last->type == F_DIR && !(last->flags & F_DONE)) {
234 centry->parent = last;
235 last = last->child = centry;
236 } else {
237 centry->parent = last->parent;
238 centry->prev = last;
239 last = last->next = centry;
240 }
241 }
242 return (root);
243 }
244
245 /*
246 * dump_nodes --
247 * dump the NODEs from `cur', based in the directory `dir'
248 */
249 void
250 dump_nodes(const char *dir, NODE *root)
251 {
252 NODE *cur;
253 char path[MAXPATHLEN + 1];
254 const char *name;
255
256 for (cur = root; cur != NULL; cur = cur->next) {
257 if (cur->type != F_DIR && !matchtags(cur))
258 continue;
259
260 if (snprintf(path, sizeof(path), "%s%s%s",
261 dir, *dir ? "/" : "", cur->name)
262 >= sizeof(path))
263 mtree_err("Pathname too long.");
264
265 #define MATCHFLAG(f) ((keys & (f)) && (cur->flags & (f)))
266 if (MATCHFLAG(F_TYPE))
267 printf("type=%s ", nodetype(cur->type));
268 if (MATCHFLAG(F_UID | F_UNAME)) {
269 if (keys & F_UNAME &&
270 (name = user_from_uid(cur->st_uid, 1)) != NULL)
271 printf("uname=%s ", name);
272 else
273 printf("uid=%u ", cur->st_uid);
274 }
275 if (MATCHFLAG(F_GID | F_GNAME)) {
276 if (keys & F_GNAME &&
277 (name = group_from_gid(cur->st_gid, 1)) != NULL)
278 printf("gname=%s ", name);
279 else
280 printf("gid=%u ", cur->st_gid);
281 }
282 if (MATCHFLAG(F_MODE))
283 printf("mode=%#o ", cur->st_mode);
284 if (MATCHFLAG(F_DEV) &&
285 (cur->type == F_BLOCK || cur->type == F_CHAR))
286 printf("device=%#x ", cur->st_rdev);
287 if (MATCHFLAG(F_NLINK))
288 printf("nlink=%d ", cur->st_nlink);
289 if (MATCHFLAG(F_SLINK))
290 printf("link=%s ", cur->slink);
291 if (MATCHFLAG(F_SIZE))
292 printf("size=%lld ", (long long)cur->st_size);
293 if (MATCHFLAG(F_TIME))
294 printf("time=%ld.%ld ", (long)cur->st_mtimespec.tv_sec,
295 cur->st_mtimespec.tv_nsec);
296 if (MATCHFLAG(F_CKSUM))
297 printf("cksum=%lu ", cur->cksum);
298 if (MATCHFLAG(F_MD5))
299 printf("md5=%s ", cur->md5sum);
300 if (MATCHFLAG(F_FLAGS))
301 printf("flags=%s ",
302 flags_to_string(cur->st_flags, "none"));
303 if (MATCHFLAG(F_IGN))
304 printf("ignore ");
305 if (MATCHFLAG(F_OPT))
306 printf("optional ");
307 if (MATCHFLAG(F_TAGS))
308 printf("tags=%s ", cur->tags);
309 puts(path);
310
311 if (cur->child)
312 dump_nodes(path, cur->child);
313 }
314 }
315
316 static dev_t
317 parsedev(char *arg)
318 {
319 #define MAX_PACK_ARGS 3
320 u_long numbers[MAX_PACK_ARGS];
321 char *p, *ep, *dev;
322 int argc;
323 pack_t *pack;
324 dev_t result;
325
326 if ((dev = strchr(arg, ',')) != NULL) {
327 *dev++='\0';
328 if ((pack = pack_find(arg)) == NULL)
329 mtree_err("unknown format `%s'", arg);
330 argc = 0;
331 while ((p = strsep(&dev, ",")) != NULL) {
332 if (*p == '\0')
333 mtree_err("missing number");
334 numbers[argc++] = strtoul(p, &ep, 0);
335 if (*ep != '\0')
336 mtree_err("invalid number `%s'",
337 p);
338 if (argc > MAX_PACK_ARGS)
339 mtree_err("too many arguments");
340 }
341 if (argc < 2)
342 mtree_err("not enough arguments");
343 result = (*pack)(argc, numbers);
344 } else {
345 result = (dev_t)strtoul(arg, &ep, 0);
346 if (*ep != '\0')
347 mtree_err("invalid device `%s'", arg);
348 }
349 return (result);
350 }
351
352 static void
353 set(char *t, NODE *ip)
354 {
355 int type, value, len;
356 gid_t gid;
357 uid_t uid;
358 char *kw, *val, *md, *ep;
359 void *m;
360
361 val = NULL;
362 while ((kw = strsep(&t, "= \t")) != NULL) {
363 if (*kw == '\0')
364 continue;
365 if (strcmp(kw, "all") == 0)
366 mtree_err("invalid keyword `all'");
367 ip->flags |= type = parsekey(kw, &value);
368 if (value) {
369 while ((val = strsep(&t, " \t")) != NULL &&
370 *val == '\0')
371 continue;
372 if (val == NULL)
373 mtree_err("missing value");
374 }
375 switch(type) {
376 case F_CKSUM:
377 ip->cksum = strtoul(val, &ep, 10);
378 if (*ep)
379 mtree_err("invalid checksum `%s'", val);
380 break;
381 case F_DEV:
382 ip->st_rdev = parsedev(val);
383 break;
384 case F_FLAGS:
385 if (strcmp("none", val) == 0)
386 ip->st_flags = 0;
387 else if (string_to_flags(&val, &ip->st_flags, NULL)
388 != 0)
389 mtree_err("invalid flag `%s'", val);
390 break;
391 case F_GID:
392 ip->st_gid = (gid_t)strtoul(val, &ep, 10);
393 if (*ep)
394 mtree_err("invalid gid `%s'", val);
395 break;
396 case F_GNAME:
397 if (gid_from_group(val, &gid) == -1)
398 mtree_err("unknown group `%s'", val);
399 ip->st_gid = gid;
400 break;
401 case F_IGN:
402 /* just set flag bit */
403 break;
404 case F_MD5:
405 if (val[0]=='0' && val[1]=='x')
406 md=&val[2];
407 else
408 md=val;
409 if ((ip->md5sum = strdup(md)) == NULL)
410 mtree_err("memory allocation error");
411 break;
412 case F_MODE:
413 if ((m = setmode(val)) == NULL)
414 mtree_err("invalid file mode `%s'", val);
415 ip->st_mode = getmode(m, 0);
416 free(m);
417 break;
418 case F_NLINK:
419 ip->st_nlink = (nlink_t)strtoul(val, &ep, 10);
420 if (*ep)
421 mtree_err("invalid link count `%s'", val);
422 break;
423 case F_OPT:
424 /* just set flag bit */
425 break;
426 case F_SIZE:
427 ip->st_size = (off_t)strtoq(val, &ep, 10);
428 if (*ep)
429 mtree_err("invalid size `%s'", val);
430 break;
431 case F_SLINK:
432 if ((ip->slink = strdup(val)) == NULL)
433 mtree_err("memory allocation error");
434 break;
435 case F_TAGS:
436 len = strlen(val) + 3; /* "," + str + ",\0" */
437 if ((ip->tags = malloc(len)) == NULL)
438 mtree_err("memory allocation error");
439 snprintf(ip->tags, len, ",%s,", val);
440 break;
441 case F_TIME:
442 ip->st_mtimespec.tv_sec =
443 (time_t)strtoul(val, &ep, 10);
444 if (*ep != '.')
445 mtree_err("invalid time `%s'", val);
446 val = ep + 1;
447 ip->st_mtimespec.tv_nsec = strtol(val, &ep, 10);
448 if (*ep)
449 mtree_err("invalid time `%s'", val);
450 break;
451 case F_TYPE:
452 ip->type = parsetype(val);
453 break;
454 case F_UID:
455 ip->st_uid = (uid_t)strtoul(val, &ep, 10);
456 if (*ep)
457 mtree_err("invalid uid `%s'", val);
458 break;
459 case F_UNAME:
460 if (uid_from_user(val, &uid) == -1)
461 mtree_err("unknown user `%s'", val);
462 ip->st_uid = uid;
463 break;
464 default:
465 mtree_err(
466 "set(): unsupported key type 0x%x (INTERNAL ERROR)",
467 type);
468 /* NOTREACHED */
469 }
470 }
471 }
472
473 static void
474 unset(char *t, NODE *ip)
475 {
476 char *p;
477
478 while ((p = strsep(&t, " \t")) != NULL) {
479 if (*p == '\0')
480 continue;
481 if (strcmp(p, "all") == 0)
482 mtree_err("invalid keyword `all'");
483 ip->flags &= ~parsekey(p, NULL);
484 }
485 }
486