1 /* $NetBSD: spec.c,v 1.88 2013/10/17 17:22:59 christos Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 2001-2004 The NetBSD Foundation, Inc.
34 * All rights reserved.
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Luke Mewburn of Wasabi Systems.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
48 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
49 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
50 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
51 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
52 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58 * POSSIBILITY OF SUCH DAMAGE.
61 #if HAVE_NBTOOL_CONFIG_H
62 #include "nbtool_config.h"
65 #include <sys/cdefs.h>
66 #if defined(__RCSID) && !defined(lint)
68 static char sccsid
[] = "@(#)spec.c 8.2 (Berkeley) 4/28/95";
70 __RCSID("$NetBSD: spec.c,v 1.88 2013/10/17 17:22:59 christos Exp $");
74 #include <sys/param.h>
94 size_t mtree_lineno
; /* Current spec line number */
95 int mtree_Mflag
; /* Merge duplicate entries */
96 int mtree_Wflag
; /* Don't "whack" permissions */
97 int mtree_Sflag
; /* Sort entries */
99 static dev_t
parsedev(char *);
100 static void replacenode(NODE
*, NODE
*);
101 static void set(char *, NODE
*);
102 static void unset(char *, NODE
*);
103 static void addchild(NODE
*, NODE
*);
104 static int nodecmp(const NODE
*, const NODE
*);
105 static int appendfield(int, const char *, ...) __printflike(2, 3);
107 #define REPLACEPTR(x,v) do { if ((x)) free((x)); (x) = (v); } while (0)
112 NODE
*centry
, *last
, *pathparent
, *cur
;
115 char *buf
, *tname
, *ntname
;
116 size_t tnamelen
, plen
;
119 centry
= last
= NULL
;
122 memset(&ginfo
, 0, sizeof(ginfo
));
123 for (mtree_lineno
= 0;
124 (buf
= fparseln(fp
, NULL
, &mtree_lineno
, NULL
,
125 FPARSELN_UNESCCOMM
));
127 /* Skip leading whitespace. */
128 for (p
= buf
; *p
&& isspace((unsigned char)*p
); ++p
)
131 /* If nothing but whitespace, continue. */
136 fprintf(stderr
, "line %lu: {%s}\n",
137 (u_long
)mtree_lineno
, p
);
139 /* Grab file name, "$", "set", or "unset". */
141 while ((p
= strsep(&next
, " \t")) != NULL
&& *p
== '\0')
144 mtree_err("missing field");
147 if (strcmp(p
+ 1, "set") == 0)
149 else if (strcmp(p
+ 1, "unset") == 0)
152 mtree_err("invalid specification `%s'", p
);
156 if (strcmp(p
, "..") == 0) {
157 /* Don't go up, if haven't gone down. */
160 if (last
->type
!= F_DIR
|| last
->flags
& F_DONE
) {
165 last
->flags
|= F_DONE
;
168 noparent
: mtree_err("no parent node");
171 plen
= strlen(p
) + 1;
172 if (plen
> tnamelen
) {
173 if ((ntname
= realloc(tname
, plen
)) == NULL
)
174 mtree_err("realloc: %s", strerror(errno
));
178 if (strunvis(tname
, p
) == -1)
179 mtree_err("strunvis failed on `%s'", p
);
183 if (strchr(p
, '/') != NULL
) {
185 for (; (e
= strchr(p
, '/')) != NULL
; p
= e
+1) {
187 continue; /* handle // */
189 if (strcmp(p
, ".") != 0) {
191 strcmp(cur
->name
, p
) != 0) {
195 if (cur
== NULL
|| cur
->type
!= F_DIR
) {
196 mtree_err("%s: %s", tname
,
197 "missing directory in specification");
204 mtree_err("%s: empty leaf element", tname
);
207 if ((centry
= calloc(1, sizeof(NODE
) + strlen(p
))) == NULL
)
208 mtree_err("%s", strerror(errno
));
210 centry
->lineno
= mtree_lineno
;
211 strcpy(centry
->name
, p
);
213 if (strpbrk(p
, MAGIC
))
214 centry
->flags
|= F_MAGIC
;
222 * Allow a bare "." root node by forcing it to
223 * type=dir for compatibility with FreeBSD.
225 if (strcmp(centry
->name
, ".") == 0 && centry
->type
== 0)
226 centry
->type
= F_DIR
;
227 if (strcmp(centry
->name
, ".") != 0 ||
228 centry
->type
!= F_DIR
)
230 "root node must be the directory `.'");
231 last
= root
= centry
;
233 } else if (pathparent
!= NULL
) {
235 * full path entry; add or replace
237 centry
->parent
= pathparent
;
238 addchild(pathparent
, centry
);
240 } else if (strcmp(centry
->name
, ".") == 0) {
242 * duplicate "." entry; always replace
244 replacenode(root
, centry
);
245 } else if (last
->type
== F_DIR
&& !(last
->flags
& F_DONE
)) {
247 * new relative child in current dir;
250 centry
->parent
= last
;
251 addchild(last
, centry
);
255 * new relative child in parent dir
256 * (after encountering ".." entry);
259 centry
->parent
= last
->parent
;
260 addchild(last
->parent
, centry
);
268 free_nodes(NODE
*root
)
276 for (cur
= root
; cur
!= NULL
; cur
= next
) {
278 free_nodes(cur
->child
);
279 REPLACEPTR(cur
->slink
, NULL
);
280 REPLACEPTR(cur
->md5digest
, NULL
);
281 REPLACEPTR(cur
->rmd160digest
, NULL
);
282 REPLACEPTR(cur
->sha1digest
, NULL
);
283 REPLACEPTR(cur
->sha256digest
, NULL
);
284 REPLACEPTR(cur
->sha384digest
, NULL
);
285 REPLACEPTR(cur
->sha512digest
, NULL
);
286 REPLACEPTR(cur
->tags
, NULL
);
287 REPLACEPTR(cur
, NULL
);
293 * Like printf(), but output a space either before or after
294 * the regular output, according to the pathlast flag.
297 appendfield(int pathlast
, const char *fmt
, ...)
305 result
= vprintf(fmt
, ap
);
314 * dump the NODEs from `cur', based in the directory `dir'.
315 * if pathlast is none zero, print the path last, otherwise print
319 dump_nodes(const char *dir
, NODE
*root
, int pathlast
)
322 char path
[MAXPATHLEN
];
327 for (cur
= root
; cur
!= NULL
; cur
= cur
->next
) {
328 if (cur
->type
!= F_DIR
&& !matchtags(cur
))
331 if (snprintf(path
, sizeof(path
), "%s%s%s",
332 dir
, *dir
? "/" : "", cur
->name
)
333 >= (int)sizeof(path
))
334 mtree_err("Pathname too long.");
337 printf("%s", vispath(path
));
339 #define MATCHFLAG(f) ((keys & (f)) && (cur->flags & (f)))
340 if (MATCHFLAG(F_TYPE
))
341 appendfield(pathlast
, "type=%s", nodetype(cur
->type
));
342 if (MATCHFLAG(F_UID
| F_UNAME
)) {
343 if (keys
& F_UNAME
&&
344 (name
= user_from_uid(cur
->st_uid
, 1)) != NULL
)
345 appendfield(pathlast
, "uname=%s", name
);
347 appendfield(pathlast
, "uid=%u", cur
->st_uid
);
349 if (MATCHFLAG(F_GID
| F_GNAME
)) {
350 if (keys
& F_GNAME
&&
351 (name
= group_from_gid(cur
->st_gid
, 1)) != NULL
)
352 appendfield(pathlast
, "gname=%s", name
);
354 appendfield(pathlast
, "gid=%u", cur
->st_gid
);
356 if (MATCHFLAG(F_MODE
))
357 appendfield(pathlast
, "mode=%#o", cur
->st_mode
);
358 if (MATCHFLAG(F_DEV
) &&
359 (cur
->type
== F_BLOCK
|| cur
->type
== F_CHAR
))
360 appendfield(pathlast
, "device=%#jx",
361 (uintmax_t)cur
->st_rdev
);
362 if (MATCHFLAG(F_NLINK
))
363 appendfield(pathlast
, "nlink=%d", cur
->st_nlink
);
364 if (MATCHFLAG(F_SLINK
))
365 appendfield(pathlast
, "link=%s", vispath(cur
->slink
));
366 if (MATCHFLAG(F_SIZE
))
367 appendfield(pathlast
, "size=%ju",
368 (uintmax_t)cur
->st_size
);
369 if (MATCHFLAG(F_TIME
))
370 appendfield(pathlast
, "time=%jd.%09ld",
371 (intmax_t)cur
->st_mtimespec
.tv_sec
,
372 cur
->st_mtimespec
.tv_nsec
);
373 if (MATCHFLAG(F_CKSUM
))
374 appendfield(pathlast
, "cksum=%lu", cur
->cksum
);
375 if (MATCHFLAG(F_MD5
))
376 appendfield(pathlast
, "%s=%s", MD5KEY
, cur
->md5digest
);
377 if (MATCHFLAG(F_RMD160
))
378 appendfield(pathlast
, "%s=%s", RMD160KEY
,
380 if (MATCHFLAG(F_SHA1
))
381 appendfield(pathlast
, "%s=%s", SHA1KEY
,
383 if (MATCHFLAG(F_SHA256
))
384 appendfield(pathlast
, "%s=%s", SHA256KEY
,
386 if (MATCHFLAG(F_SHA384
))
387 appendfield(pathlast
, "%s=%s", SHA384KEY
,
389 if (MATCHFLAG(F_SHA512
))
390 appendfield(pathlast
, "%s=%s", SHA512KEY
,
392 if (MATCHFLAG(F_FLAGS
)) {
393 str
= flags_to_string(cur
->st_flags
, "none");
394 appendfield(pathlast
, "flags=%s", str
);
397 if (MATCHFLAG(F_IGN
))
398 appendfield(pathlast
, "ignore");
399 if (MATCHFLAG(F_OPT
))
400 appendfield(pathlast
, "optional");
401 if (MATCHFLAG(F_TAGS
)) {
402 /* don't output leading or trailing commas */
407 while(q
> p
&& q
[-1] == ',')
409 appendfield(pathlast
, "tags=%.*s", (int)(q
- p
), p
);
411 puts(pathlast
? vispath(path
) : "");
414 dump_nodes(path
, cur
->child
, pathlast
);
420 * strsvis(3) encodes path, which must not be longer than MAXPATHLEN
421 * characters long, and returns a pointer to a static buffer containing
425 vispath(const char *path
)
427 static const char extra
[] = { ' ', '\t', '\n', '\\', '#', '\0' };
428 static const char extra_glob
[] = { ' ', '\t', '\n', '\\', '#', '*',
430 static char pathbuf
[4*MAXPATHLEN
+ 1];
432 if (flavor
== F_NETBSD6
)
433 strsvis(pathbuf
, path
, VIS_CSTYLE
, extra
);
435 strsvis(pathbuf
, path
, VIS_OCTAL
, extra_glob
);
443 #define MAX_PACK_ARGS 3
444 u_long numbers
[MAX_PACK_ARGS
];
449 const char *error
= NULL
;
451 if ((dev
= strchr(arg
, ',')) != NULL
) {
453 if ((pack
= pack_find(arg
)) == NULL
)
454 mtree_err("unknown format `%s'", arg
);
456 while ((p
= strsep(&dev
, ",")) != NULL
) {
458 mtree_err("missing number");
459 numbers
[argc
++] = strtoul(p
, &ep
, 0);
461 mtree_err("invalid number `%s'",
463 /* MINIX: 03 warns for above array bounds*/
464 if (argc
>= MAX_PACK_ARGS
)
465 mtree_err("too many arguments");
468 mtree_err("not enough arguments");
469 result
= (*pack
)(argc
, numbers
, &error
);
471 mtree_err("%s", error
);
473 result
= (dev_t
)strtoul(arg
, &ep
, 0);
475 mtree_err("invalid device `%s'", arg
);
481 replacenode(NODE
*cur
, NODE
*new)
484 #define REPLACE(x) cur->x = new->x
485 #define REPLACESTR(x) REPLACEPTR(cur->x,new->x)
487 if (cur
->type
!= new->type
) {
490 * merge entries with different types; we
491 * don't want children retained in this case.
494 free_nodes(cur
->child
);
498 "existing entry for `%s', type `%s'"
499 " does not match type `%s'",
500 cur
->name
, nodetype(cur
->type
),
501 nodetype(new->type
));
506 REPLACE(st_mtimespec
);
508 if (cur
->slink
!= NULL
) {
509 if ((cur
->slink
= strdup(new->slink
)) == NULL
)
510 mtree_err("memory allocation error");
511 if (strunvis(cur
->slink
, new->slink
) == -1)
512 mtree_err("strunvis failed on `%s'", new->slink
);
522 REPLACESTR(md5digest
);
523 REPLACESTR(rmd160digest
);
524 REPLACESTR(sha1digest
);
525 REPLACESTR(sha256digest
);
526 REPLACESTR(sha384digest
);
527 REPLACESTR(sha512digest
);
535 set(char *t
, NODE
*ip
)
537 int type
, value
, len
;
540 char *kw
, *val
, *md
, *ep
;
543 while ((kw
= strsep(&t
, "= \t")) != NULL
) {
546 if (strcmp(kw
, "all") == 0)
547 mtree_err("invalid keyword `all'");
548 ip
->flags
|= type
= parsekey(kw
, &value
);
550 /* Just set flag bit (F_IGN and F_OPT) */
552 while ((val
= strsep(&t
, " \t")) != NULL
&& *val
== '\0')
555 mtree_err("missing value");
558 ip
->cksum
= strtoul(val
, &ep
, 10);
560 mtree_err("invalid checksum `%s'", val
);
563 ip
->st_rdev
= parsedev(val
);
566 if (strcmp("none", val
) == 0)
568 else if (string_to_flags(&val
, &ip
->st_flags
, NULL
)
570 mtree_err("invalid flag `%s'", val
);
573 ip
->st_gid
= (gid_t
)strtoul(val
, &ep
, 10);
575 mtree_err("invalid gid `%s'", val
);
578 if (mtree_Wflag
) /* don't parse if whacking */
580 if (gid_from_group(val
, &gid
) == -1)
581 mtree_err("unknown group `%s'", val
);
585 if (val
[0]=='0' && val
[1]=='x')
589 if ((ip
->md5digest
= strdup(md
)) == NULL
)
590 mtree_err("memory allocation error");
593 if ((m
= setmode(val
)) == NULL
)
594 mtree_err("cannot set file mode `%s' (%s)",
595 val
, strerror(errno
));
596 ip
->st_mode
= getmode(m
, 0);
600 ip
->st_nlink
= (nlink_t
)strtoul(val
, &ep
, 10);
602 mtree_err("invalid link count `%s'", val
);
605 if (val
[0]=='0' && val
[1]=='x')
609 if ((ip
->rmd160digest
= strdup(md
)) == NULL
)
610 mtree_err("memory allocation error");
613 if (val
[0]=='0' && val
[1]=='x')
617 if ((ip
->sha1digest
= strdup(md
)) == NULL
)
618 mtree_err("memory allocation error");
621 ip
->st_size
= (off_t
)strtoll(val
, &ep
, 10);
623 mtree_err("invalid size `%s'", val
);
626 if ((ip
->slink
= strdup(val
)) == NULL
)
627 mtree_err("memory allocation error");
628 if (strunvis(ip
->slink
, val
) == -1)
629 mtree_err("strunvis failed on `%s'", val
);
632 len
= strlen(val
) + 3; /* "," + str + ",\0" */
633 if ((ip
->tags
= malloc(len
)) == NULL
)
634 mtree_err("memory allocation error");
635 snprintf(ip
->tags
, len
, ",%s,", val
);
638 ip
->st_mtimespec
.tv_sec
=
639 (time_t)strtoll(val
, &ep
, 10);
641 mtree_err("invalid time `%s'", val
);
643 ip
->st_mtimespec
.tv_nsec
= strtol(val
, &ep
, 10);
645 mtree_err("invalid time `%s'", val
);
648 ip
->type
= parsetype(val
);
651 ip
->st_uid
= (uid_t
)strtoul(val
, &ep
, 10);
653 mtree_err("invalid uid `%s'", val
);
656 if (mtree_Wflag
) /* don't parse if whacking */
658 if (uid_from_user(val
, &uid
) == -1)
659 mtree_err("unknown user `%s'", val
);
663 if (val
[0]=='0' && val
[1]=='x')
667 if ((ip
->sha256digest
= strdup(md
)) == NULL
)
668 mtree_err("memory allocation error");
671 if (val
[0]=='0' && val
[1]=='x')
675 if ((ip
->sha384digest
= strdup(md
)) == NULL
)
676 mtree_err("memory allocation error");
679 if (val
[0]=='0' && val
[1]=='x')
683 if ((ip
->sha512digest
= strdup(md
)) == NULL
)
684 mtree_err("memory allocation error");
688 "set(): unsupported key type 0x%x (INTERNAL ERROR)",
696 unset(char *t
, NODE
*ip
)
700 while ((p
= strsep(&t
, " \t")) != NULL
) {
703 ip
->flags
&= ~parsekey(p
, NULL
);
709 * Add the centry node as a child of the pathparent node. If
710 * centry is a duplicate, call replacenode(). If centry is not
711 * a duplicate, insert it into the linked list referenced by
712 * pathparent->child. Keep the list sorted if Sflag is set.
715 addchild(NODE
*pathparent
, NODE
*centry
)
717 NODE
*samename
; /* node with the same name as centry */
718 NODE
*replacepos
; /* if non-NULL, centry should replace this node */
719 NODE
*insertpos
; /* if non-NULL, centry should be inserted
721 NODE
*cur
; /* for stepping through the list */
722 NODE
*last
; /* the last node in the list */
729 cur
= pathparent
->child
;
731 /* centry is pathparent's first and only child node so far */
732 pathparent
->child
= centry
;
737 * pathparent already has at least one other child, so add the
738 * centry node to the list.
740 * We first scan through the list looking for an existing node
741 * with the same name (setting samename), and also looking
742 * for the correct position to replace or insert the new node
743 * (setting replacepos and/or insertpos).
745 for (; cur
!= NULL
; last
= cur
, cur
= cur
->next
) {
746 if (strcmp(centry
->name
, cur
->name
) == 0) {
750 cmp
= nodecmp(centry
, cur
);
753 } else if (cmp
> 0) {
759 if (samename
!= NULL
) {
760 /* replace node with same name */
761 replacepos
= samename
;
763 /* add new node at end of list */
768 if (samename
!= NULL
) {
770 * We found a node with the same name above. Call
771 * replacenode(), which will either exit with an error,
772 * or replace the information in the samename node and
773 * free the information in the centry node.
775 replacenode(samename
, centry
);
776 if (samename
== replacepos
) {
777 /* The just-replaced node was in the correct position */
780 if (samename
== insertpos
|| samename
->prev
== insertpos
) {
782 * We thought the new node should be just before
783 * or just after the replaced node, but that would
784 * be equivalent to just retaining the replaced node.
790 * The just-replaced node is in the wrong position in
791 * the list. This can happen if sort order depends on
792 * criteria other than the node name.
794 * Make centry point to the just-replaced node. Unlink
795 * the just-replaced node from the list, and allow it to
796 * be insterted in the correct position later.
800 centry
->prev
->next
= centry
->next
;
802 /* centry->next is the new head of the list */
803 pathparent
->child
= centry
->next
;
804 assert(centry
->next
!= NULL
);
807 centry
->next
->prev
= centry
->prev
;
812 if (insertpos
== NULL
) {
813 /* insert centry at the beginning of the list */
814 pathparent
->child
->prev
= centry
;
815 centry
->next
= pathparent
->child
;
817 pathparent
->child
= centry
;
819 /* insert centry into the list just after insertpos */
820 centry
->next
= insertpos
->next
;
821 insertpos
->next
= centry
;
822 centry
->prev
= insertpos
;
824 centry
->next
->prev
= centry
;
831 * used as a comparison function by addchild() to control the order
832 * in which entries appear within a list of sibling nodes. We make
833 * directories sort after non-directories, but otherwise sort in
836 * Keep this in sync with dcmp() in create.c.
839 nodecmp(const NODE
*a
, const NODE
*b
)
842 if ((a
->type
& F_DIR
) != 0) {
843 if ((b
->type
& F_DIR
) == 0)
845 } else if ((b
->type
& F_DIR
) != 0)
847 return strcmp(a
->name
, b
->name
);