1 /* @(#)spec.c 8.2 (Berkeley) 4/28/95 */
2 /* $NetBSD: spec.c,v 1.90 2017/12/14 18:34:41 christos Exp $ */
5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 2001-2004 The NetBSD Foundation, Inc.
35 * All rights reserved.
37 * This code is derived from software contributed to The NetBSD Foundation
38 * by Luke Mewburn of Wasabi Systems.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
49 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
50 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
53 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 * POSSIBILITY OF SUCH DAMAGE.
62 #if HAVE_NBTOOL_CONFIG_H
63 #include "nbtool_config.h"
66 #include <sys/param.h>
86 size_t mtree_lineno; /* Current spec line number */
87 int mtree_Mflag; /* Merge duplicate entries */
88 int mtree_Wflag; /* Don't "whack" permissions */
89 int mtree_Sflag; /* Sort entries */
91 static dev_t parsedev(char *);
92 static void replacenode(NODE *, NODE *);
93 static void set(char *, NODE *);
94 static void unset(char *, NODE *);
95 static void addchild(NODE *, NODE *);
96 static int nodecmp(const NODE *, const NODE *);
97 static int appendfield(FILE *, int, const char *, ...) __printflike(3, 4);
99 #define REPLACEPTR(x,v) do { if ((x)) free((x)); (x) = (v); } while (0)
104 NODE *centry, *last, *pathparent, *cur;
107 char *buf, *tname, *ntname;
108 size_t tnamelen, plen;
111 centry = last = NULL;
114 memset(&ginfo, 0, sizeof(ginfo));
115 for (mtree_lineno = 0;
116 (buf = fparseln(fp, NULL, &mtree_lineno, NULL,
117 FPARSELN_UNESCCOMM));
119 /* Skip leading whitespace. */
120 for (p = buf; *p && isspace((unsigned char)*p); ++p)
123 /* If nothing but whitespace, continue. */
128 fprintf(stderr, "line %lu: {%s}\n",
129 (u_long)mtree_lineno, p);
131 /* Grab file name, "$", "set", or "unset". */
133 while ((p = strsep(&next, " \t")) != NULL && *p == '\0')
136 mtree_err("missing field");
139 if (strcmp(p + 1, "set") == 0)
141 else if (strcmp(p + 1, "unset") == 0)
144 mtree_err("invalid specification `%s'", p);
148 if (strcmp(p, "..") == 0) {
149 /* Don't go up, if haven't gone down. */
152 if (last->type != F_DIR || last->flags & F_DONE) {
157 last->flags |= F_DONE;
160 noparent: mtree_err("no parent node");
163 plen = strlen(p) + 1;
164 if (plen > tnamelen) {
165 if ((ntname = realloc(tname, plen)) == NULL)
166 mtree_err("realloc: %s", strerror(errno));
170 if (strunvis(tname, p) == -1)
171 mtree_err("strunvis failed on `%s'", p);
175 if (strchr(p, '/') != NULL) {
177 for (; (e = strchr(p, '/')) != NULL; p = e+1) {
179 continue; /* handle // */
181 if (strcmp(p, ".") != 0) {
183 strcmp(cur->name, p) != 0) {
187 if (cur == NULL || cur->type != F_DIR) {
188 mtree_err("%s: %s", tname,
189 "missing directory in specification");
196 mtree_err("%s: empty leaf element", tname);
199 if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
200 mtree_err("%s", strerror(errno));
202 centry->lineno = mtree_lineno;
203 strcpy(centry->name, p);
205 if (strpbrk(p, MAGIC))
206 centry->flags |= F_MAGIC;
214 * Allow a bare "." root node by forcing it to
215 * type=dir for compatibility with FreeBSD.
217 if (strcmp(centry->name, ".") == 0 && centry->type == 0)
218 centry->type = F_DIR;
219 if (strcmp(centry->name, ".") != 0 ||
220 centry->type != F_DIR)
222 "root node must be the directory `.'");
223 last = root = centry;
225 } else if (pathparent != NULL) {
227 * full path entry; add or replace
229 centry->parent = pathparent;
230 addchild(pathparent, centry);
232 } else if (strcmp(centry->name, ".") == 0) {
234 * duplicate "." entry; always replace
236 replacenode(root, centry);
237 } else if (last->type == F_DIR && !(last->flags & F_DONE)) {
239 * new relative child in current dir;
242 centry->parent = last;
243 addchild(last, centry);
247 * new relative child in parent dir
248 * (after encountering ".." entry);
251 centry->parent = last->parent;
252 addchild(last->parent, centry);
260 free_nodes(NODE *root)
268 for (cur = root; cur != NULL; cur = next) {
270 free_nodes(cur->child);
271 REPLACEPTR(cur->slink, NULL);
272 REPLACEPTR(cur->md5digest, NULL);
273 REPLACEPTR(cur->rmd160digest, NULL);
274 REPLACEPTR(cur->sha1digest, NULL);
275 REPLACEPTR(cur->sha256digest, NULL);
276 REPLACEPTR(cur->sha384digest, NULL);
277 REPLACEPTR(cur->sha512digest, NULL);
278 REPLACEPTR(cur->tags, NULL);
279 REPLACEPTR(cur, NULL);
285 * Like fprintf(), but output a space either before or after
286 * the regular output, according to the pathlast flag.
289 appendfield(FILE *fp, int pathlast, const char *fmt, ...)
297 result = vprintf(fmt, ap);
306 * dump the NODEs from `cur', based in the directory `dir'.
307 * if pathlast is none zero, print the path last, otherwise print
311 dump_nodes(FILE *fp, const char *dir, NODE *root, int pathlast)
314 char path[MAXPATHLEN];
319 for (cur = root; cur != NULL; cur = cur->next) {
320 if (cur->type != F_DIR && !matchtags(cur))
323 if (snprintf(path, sizeof(path), "%s%s%s",
324 dir, *dir ? "/" : "", cur->name)
325 >= (int)sizeof(path))
326 mtree_err("Pathname too long.");
329 fprintf(fp, "%s", vispath(path));
331 #define MATCHFLAG(f) ((keys & (f)) && (cur->flags & (f)))
332 if (MATCHFLAG(F_TYPE))
333 appendfield(fp, pathlast, "type=%s",
334 nodetype(cur->type));
335 if (MATCHFLAG(F_UID | F_UNAME)) {
336 if (keys & F_UNAME &&
337 (name = user_from_uid(cur->st_uid, 1)) != NULL)
338 appendfield(fp, pathlast, "uname=%s", name);
340 appendfield(fp, pathlast, "uid=%u",
343 if (MATCHFLAG(F_GID | F_GNAME)) {
344 if (keys & F_GNAME &&
345 (name = group_from_gid(cur->st_gid, 1)) != NULL)
346 appendfield(fp, pathlast, "gname=%s", name);
348 appendfield(fp, pathlast, "gid=%u",
351 if (MATCHFLAG(F_MODE))
352 appendfield(fp, pathlast, "mode=%#o", cur->st_mode);
353 if (MATCHFLAG(F_DEV) &&
354 (cur->type == F_BLOCK || cur->type == F_CHAR))
355 appendfield(fp, pathlast, "device=%#jx",
356 (uintmax_t)cur->st_rdev);
357 if (MATCHFLAG(F_NLINK))
358 appendfield(fp, pathlast, "nlink=%ju",
359 (uintmax_t)cur->st_nlink);
360 if (MATCHFLAG(F_SLINK))
361 appendfield(fp, pathlast, "link=%s",
362 vispath(cur->slink));
363 if (MATCHFLAG(F_SIZE))
364 appendfield(fp, pathlast, "size=%ju",
365 (uintmax_t)cur->st_size);
366 if (MATCHFLAG(F_TIME))
367 appendfield(fp, pathlast, "time=%jd.%09ld",
368 (intmax_t)cur->st_mtimespec.tv_sec,
369 cur->st_mtimespec.tv_nsec);
370 if (MATCHFLAG(F_CKSUM))
371 appendfield(fp, pathlast, "cksum=%lu", cur->cksum);
372 if (MATCHFLAG(F_MD5))
373 appendfield(fp, pathlast, "%s=%s", MD5KEY,
375 if (MATCHFLAG(F_RMD160))
376 appendfield(fp, pathlast, "%s=%s", RMD160KEY,
378 if (MATCHFLAG(F_SHA1))
379 appendfield(fp, pathlast, "%s=%s", SHA1KEY,
381 if (MATCHFLAG(F_SHA256))
382 appendfield(fp, pathlast, "%s=%s", SHA256KEY,
384 if (MATCHFLAG(F_SHA384))
385 appendfield(fp, pathlast, "%s=%s", SHA384KEY,
387 if (MATCHFLAG(F_SHA512))
388 appendfield(fp, pathlast, "%s=%s", SHA512KEY,
390 if (MATCHFLAG(F_FLAGS)) {
391 str = flags_to_string(cur->st_flags, "none");
392 appendfield(fp, pathlast, "flags=%s", str);
395 if (MATCHFLAG(F_IGN))
396 appendfield(fp, pathlast, "ignore");
397 if (MATCHFLAG(F_OPT))
398 appendfield(fp, pathlast, "optional");
399 if (MATCHFLAG(F_TAGS)) {
400 /* don't output leading or trailing commas */
405 while(q > p && q[-1] == ',')
407 appendfield(fp, pathlast, "tags=%.*s", (int)(q - p), p);
409 puts(pathlast ? vispath(path) : "");
412 dump_nodes(fp, path, cur->child, pathlast);
418 * strsvis(3) encodes path, which must not be longer than MAXPATHLEN
419 * characters long, and returns a pointer to a static buffer containing
423 vispath(const char *path)
425 static const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
426 static const char extra_glob[] = { ' ', '\t', '\n', '\\', '#', '*',
428 static char pathbuf[4*MAXPATHLEN + 1];
430 if (flavor == F_NETBSD6)
431 strsvis(pathbuf, path, VIS_CSTYLE, extra);
433 strsvis(pathbuf, path, VIS_OCTAL, extra_glob);
441 #define MAX_PACK_ARGS 3
442 u_long numbers[MAX_PACK_ARGS];
447 const char *error = NULL;
449 if ((dev = strchr(arg, ',')) != NULL) {
451 if ((pack = pack_find(arg)) == NULL)
452 mtree_err("unknown format `%s'", arg);
454 while ((p = strsep(&dev, ",")) != NULL) {
456 mtree_err("missing number");
457 numbers[argc++] = strtoul(p, &ep, 0);
459 mtree_err("invalid number `%s'",
461 if (argc > MAX_PACK_ARGS)
462 mtree_err("too many arguments");
465 mtree_err("not enough arguments");
466 result = (*pack)(argc, numbers, &error);
468 mtree_err("%s", error);
470 result = (dev_t)strtoul(arg, &ep, 0);
472 mtree_err("invalid device `%s'", arg);
478 replacenode(NODE *cur, NODE *new)
481 #define REPLACE(x) cur->x = new->x
482 #define REPLACESTR(x) REPLACEPTR(cur->x,new->x)
484 if (cur->type != new->type) {
487 * merge entries with different types; we
488 * don't want children retained in this case.
491 free_nodes(cur->child);
495 "existing entry for `%s', type `%s'"
496 " does not match type `%s'",
497 cur->name, nodetype(cur->type),
498 nodetype(new->type));
503 REPLACE(st_mtimespec);
505 if (cur->slink != NULL) {
506 if ((cur->slink = strdup(new->slink)) == NULL)
507 mtree_err("memory allocation error");
508 if (strunvis(cur->slink, new->slink) == -1)
509 mtree_err("strunvis failed on `%s'", new->slink);
519 REPLACESTR(md5digest);
520 REPLACESTR(rmd160digest);
521 REPLACESTR(sha1digest);
522 REPLACESTR(sha256digest);
523 REPLACESTR(sha384digest);
524 REPLACESTR(sha512digest);
532 set(char *t, NODE *ip)
534 int type, value, len;
537 char *kw, *val, *md, *ep;
540 while ((kw = strsep(&t, "= \t")) != NULL) {
543 if (strcmp(kw, "all") == 0)
544 mtree_err("invalid keyword `all'");
545 ip->flags |= type = parsekey(kw, &value);
547 /* Just set flag bit (F_IGN and F_OPT) */
549 while ((val = strsep(&t, " \t")) != NULL && *val == '\0')
552 mtree_err("missing value");
555 ip->cksum = strtoul(val, &ep, 10);
557 mtree_err("invalid checksum `%s'", val);
560 ip->st_rdev = parsedev(val);
563 if (strcmp("none", val) == 0)
565 else if (string_to_flags(&val, &ip->st_flags, NULL)
567 mtree_err("invalid flag `%s'", val);
570 ip->st_gid = (gid_t)strtoul(val, &ep, 10);
572 mtree_err("invalid gid `%s'", val);
575 if (mtree_Wflag) /* don't parse if whacking */
577 if (gid_from_group(val, &gid) == -1)
578 mtree_err("unknown group `%s'", val);
582 if (val[0]=='0' && val[1]=='x')
586 if ((ip->md5digest = strdup(md)) == NULL)
587 mtree_err("memory allocation error");
590 if ((m = setmode(val)) == NULL)
591 mtree_err("cannot set file mode `%s' (%s)",
592 val, strerror(errno));
593 ip->st_mode = getmode(m, 0);
597 ip->st_nlink = (nlink_t)strtoul(val, &ep, 10);
599 mtree_err("invalid link count `%s'", val);
602 if (val[0]=='0' && val[1]=='x')
606 if ((ip->rmd160digest = strdup(md)) == NULL)
607 mtree_err("memory allocation error");
610 if (val[0]=='0' && val[1]=='x')
614 if ((ip->sha1digest = strdup(md)) == NULL)
615 mtree_err("memory allocation error");
618 ip->st_size = (off_t)strtoll(val, &ep, 10);
620 mtree_err("invalid size `%s'", val);
623 if ((ip->slink = strdup(val)) == NULL)
624 mtree_err("memory allocation error");
625 if (strunvis(ip->slink, val) == -1)
626 mtree_err("strunvis failed on `%s'", val);
629 len = strlen(val) + 3; /* "," + str + ",\0" */
630 if ((ip->tags = malloc(len)) == NULL)
631 mtree_err("memory allocation error");
632 snprintf(ip->tags, len, ",%s,", val);
635 ip->st_mtimespec.tv_sec =
636 (time_t)strtoll(val, &ep, 10);
638 mtree_err("invalid time `%s'", val);
640 ip->st_mtimespec.tv_nsec = strtol(val, &ep, 10);
642 mtree_err("invalid time `%s'", val);
645 ip->type = parsetype(val);
648 ip->st_uid = (uid_t)strtoul(val, &ep, 10);
650 mtree_err("invalid uid `%s'", val);
653 if (mtree_Wflag) /* don't parse if whacking */
655 if (uid_from_user(val, &uid) == -1)
656 mtree_err("unknown user `%s'", val);
660 if (val[0]=='0' && val[1]=='x')
664 if ((ip->sha256digest = strdup(md)) == NULL)
665 mtree_err("memory allocation error");
668 if (val[0]=='0' && val[1]=='x')
672 if ((ip->sha384digest = strdup(md)) == NULL)
673 mtree_err("memory allocation error");
676 if (val[0]=='0' && val[1]=='x')
680 if ((ip->sha512digest = strdup(md)) == NULL)
681 mtree_err("memory allocation error");
685 "set(): unsupported key type 0x%x (INTERNAL ERROR)",
693 unset(char *t, NODE *ip)
697 while ((p = strsep(&t, " \t")) != NULL) {
700 ip->flags &= ~parsekey(p, NULL);
706 * Add the centry node as a child of the pathparent node. If
707 * centry is a duplicate, call replacenode(). If centry is not
708 * a duplicate, insert it into the linked list referenced by
709 * pathparent->child. Keep the list sorted if Sflag is set.
712 addchild(NODE *pathparent, NODE *centry)
714 NODE *samename; /* node with the same name as centry */
715 NODE *replacepos; /* if non-NULL, centry should replace this node */
716 NODE *insertpos; /* if non-NULL, centry should be inserted
718 NODE *cur; /* for stepping through the list */
719 NODE *last; /* the last node in the list */
726 cur = pathparent->child;
728 /* centry is pathparent's first and only child node so far */
729 pathparent->child = centry;
734 * pathparent already has at least one other child, so add the
735 * centry node to the list.
737 * We first scan through the list looking for an existing node
738 * with the same name (setting samename), and also looking
739 * for the correct position to replace or insert the new node
740 * (setting replacepos and/or insertpos).
742 for (; cur != NULL; last = cur, cur = cur->next) {
743 if (strcmp(centry->name, cur->name) == 0) {
747 cmp = nodecmp(centry, cur);
750 } else if (cmp > 0) {
756 if (samename != NULL) {
757 /* replace node with same name */
758 replacepos = samename;
760 /* add new node at end of list */
765 if (samename != NULL) {
767 * We found a node with the same name above. Call
768 * replacenode(), which will either exit with an error,
769 * or replace the information in the samename node and
770 * free the information in the centry node.
772 replacenode(samename, centry);
773 if (samename == replacepos) {
774 /* The just-replaced node was in the correct position */
777 if (samename == insertpos || samename->prev == insertpos) {
779 * We thought the new node should be just before
780 * or just after the replaced node, but that would
781 * be equivalent to just retaining the replaced node.
787 * The just-replaced node is in the wrong position in
788 * the list. This can happen if sort order depends on
789 * criteria other than the node name.
791 * Make centry point to the just-replaced node. Unlink
792 * the just-replaced node from the list, and allow it to
793 * be insterted in the correct position later.
797 centry->prev->next = centry->next;
799 /* centry->next is the new head of the list */
800 pathparent->child = centry->next;
801 assert(centry->next != NULL);
804 centry->next->prev = centry->prev;
809 if (insertpos == NULL) {
810 /* insert centry at the beginning of the list */
811 pathparent->child->prev = centry;
812 centry->next = pathparent->child;
814 pathparent->child = centry;
816 /* insert centry into the list just after insertpos */
817 centry->next = insertpos->next;
818 insertpos->next = centry;
819 centry->prev = insertpos;
821 centry->next->prev = centry;
828 * used as a comparison function by addchild() to control the order
829 * in which entries appear within a list of sibling nodes. We make
830 * directories sort after non-directories, but otherwise sort in
833 * Keep this in sync with dcmp() in create.c.
836 nodecmp(const NODE *a, const NODE *b)
839 if ((a->type & F_DIR) != 0) {
840 if ((b->type & F_DIR) == 0)
842 } else if ((b->type & F_DIR) != 0)
844 return strcmp(a->name, b->name);