1 /* $NetBSD: spec.c,v 1.84 2012/10/07 18:40:49 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.84 2012/10/07 18:40:49 christos Exp $");
74 #include <sys/param.h>
93 size_t mtree_lineno
; /* Current spec line number */
94 int mtree_Mflag
; /* Merge duplicate entries */
95 int mtree_Wflag
; /* Don't "whack" permissions */
96 int mtree_Sflag
; /* Sort entries */
98 static dev_t
parsedev(char *);
99 static void replacenode(NODE
*, NODE
*);
100 static void set(char *, NODE
*);
101 static void unset(char *, NODE
*);
102 static void addchild(NODE
*, NODE
*);
103 static int nodecmp(const NODE
*, const NODE
*);
104 static int appendfield(int, const char *, ...) __printflike(2, 3);
106 #define REPLACEPTR(x,v) do { if ((x)) free((x)); (x) = (v); } while (0)
111 NODE
*centry
, *last
, *pathparent
, *cur
;
114 char *buf
, *tname
, *ntname
;
115 size_t tnamelen
, plen
;
118 centry
= last
= NULL
;
121 memset(&ginfo
, 0, sizeof(ginfo
));
122 for (mtree_lineno
= 0;
123 (buf
= fparseln(fp
, NULL
, &mtree_lineno
, NULL
,
124 FPARSELN_UNESCCOMM
));
126 /* Skip leading whitespace. */
127 for (p
= buf
; *p
&& isspace((unsigned char)*p
); ++p
)
130 /* If nothing but whitespace, continue. */
135 fprintf(stderr
, "line %lu: {%s}\n",
136 (u_long
)mtree_lineno
, p
);
138 /* Grab file name, "$", "set", or "unset". */
140 while ((p
= strsep(&next
, " \t")) != NULL
&& *p
== '\0')
143 mtree_err("missing field");
146 if (strcmp(p
+ 1, "set") == 0)
148 else if (strcmp(p
+ 1, "unset") == 0)
151 mtree_err("invalid specification `%s'", p
);
155 if (strcmp(p
, "..") == 0) {
156 /* Don't go up, if haven't gone down. */
159 if (last
->type
!= F_DIR
|| last
->flags
& F_DONE
) {
164 last
->flags
|= F_DONE
;
167 noparent
: mtree_err("no parent node");
170 plen
= strlen(p
) + 1;
171 if (plen
> tnamelen
) {
172 if ((ntname
= realloc(tname
, plen
)) == NULL
)
173 mtree_err("realloc: %s", strerror(errno
));
177 if (strunvis(tname
, p
) == -1)
178 mtree_err("strunvis failed on `%s'", p
);
182 if (strchr(p
, '/') != NULL
) {
184 for (; (e
= strchr(p
, '/')) != NULL
; p
= e
+1) {
186 continue; /* handle // */
188 if (strcmp(p
, ".") != 0) {
190 strcmp(cur
->name
, p
) != 0) {
194 if (cur
== NULL
|| cur
->type
!= F_DIR
) {
195 mtree_err("%s: %s", tname
,
196 "missing directory in specification");
203 mtree_err("%s: empty leaf element", tname
);
206 if ((centry
= calloc(1, sizeof(NODE
) + strlen(p
))) == NULL
)
207 mtree_err("%s", strerror(errno
));
209 centry
->lineno
= mtree_lineno
;
210 strcpy(centry
->name
, p
);
212 if (strpbrk(p
, MAGIC
))
213 centry
->flags
|= F_MAGIC
;
220 if (strcmp(centry
->name
, ".") != 0 ||
221 centry
->type
!= F_DIR
)
223 "root node must be the directory `.'");
224 last
= root
= centry
;
226 } else if (pathparent
!= NULL
) {
228 * full path entry; add or replace
230 centry
->parent
= pathparent
;
231 addchild(pathparent
, centry
);
233 } else if (strcmp(centry
->name
, ".") == 0) {
235 * duplicate "." entry; always replace
237 replacenode(root
, centry
);
238 } else if (last
->type
== F_DIR
&& !(last
->flags
& F_DONE
)) {
240 * new relative child in current dir;
243 centry
->parent
= last
;
244 addchild(last
, centry
);
248 * new relative child in parent dir
249 * (after encountering ".." entry);
252 centry
->parent
= last
->parent
;
253 addchild(last
->parent
, centry
);
261 free_nodes(NODE
*root
)
269 for (cur
= root
; cur
!= NULL
; cur
= next
) {
271 free_nodes(cur
->child
);
272 REPLACEPTR(cur
->slink
, NULL
);
273 REPLACEPTR(cur
->md5digest
, NULL
);
274 REPLACEPTR(cur
->rmd160digest
, NULL
);
275 REPLACEPTR(cur
->sha1digest
, NULL
);
276 REPLACEPTR(cur
->sha256digest
, NULL
);
277 REPLACEPTR(cur
->sha384digest
, NULL
);
278 REPLACEPTR(cur
->sha512digest
, NULL
);
279 REPLACEPTR(cur
->tags
, NULL
);
280 REPLACEPTR(cur
, NULL
);
286 * Like printf(), but output a space either before or after
287 * the regular output, according to the pathlast flag.
290 appendfield(int pathlast
, const char *fmt
, ...)
298 result
= vprintf(fmt
, ap
);
307 * dump the NODEs from `cur', based in the directory `dir'.
308 * if pathlast is none zero, print the path last, otherwise print
312 dump_nodes(const char *dir
, NODE
*root
, int pathlast
)
315 char path
[MAXPATHLEN
];
320 for (cur
= root
; cur
!= NULL
; cur
= cur
->next
) {
321 if (cur
->type
!= F_DIR
&& !matchtags(cur
))
324 if (snprintf(path
, sizeof(path
), "%s%s%s",
325 dir
, *dir
? "/" : "", cur
->name
)
326 >= (int)sizeof(path
))
327 mtree_err("Pathname too long.");
330 printf("%s", vispath(path
));
332 #define MATCHFLAG(f) ((keys & (f)) && (cur->flags & (f)))
333 if (MATCHFLAG(F_TYPE
))
334 appendfield(pathlast
, "type=%s", 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(pathlast
, "uname=%s", name
);
340 appendfield(pathlast
, "uid=%u", cur
->st_uid
);
342 if (MATCHFLAG(F_GID
| F_GNAME
)) {
343 if (keys
& F_GNAME
&&
344 (name
= group_from_gid(cur
->st_gid
, 1)) != NULL
)
345 appendfield(pathlast
, "gname=%s", name
);
347 appendfield(pathlast
, "gid=%u", cur
->st_gid
);
349 if (MATCHFLAG(F_MODE
))
350 appendfield(pathlast
, "mode=%#o", cur
->st_mode
);
351 if (MATCHFLAG(F_DEV
) &&
352 (cur
->type
== F_BLOCK
|| cur
->type
== F_CHAR
))
353 appendfield(pathlast
, "device=%#llx", (long long)cur
->st_rdev
);
354 if (MATCHFLAG(F_NLINK
))
355 appendfield(pathlast
, "nlink=%d", cur
->st_nlink
);
356 if (MATCHFLAG(F_SLINK
))
357 appendfield(pathlast
, "link=%s", vispath(cur
->slink
));
358 if (MATCHFLAG(F_SIZE
))
359 appendfield(pathlast
, "size=%lld", (long long)cur
->st_size
);
360 if (MATCHFLAG(F_TIME
))
361 appendfield(pathlast
, "time=%lld.%09ld",
362 (long long)cur
->st_mtimespec
.tv_sec
,
363 cur
->st_mtimespec
.tv_nsec
);
364 if (MATCHFLAG(F_CKSUM
))
365 appendfield(pathlast
, "cksum=%lu", cur
->cksum
);
366 if (MATCHFLAG(F_MD5
))
367 appendfield(pathlast
, "%s=%s", MD5KEY
, cur
->md5digest
);
368 if (MATCHFLAG(F_RMD160
))
369 appendfield(pathlast
, "%s=%s", RMD160KEY
,
371 if (MATCHFLAG(F_SHA1
))
372 appendfield(pathlast
, "%s=%s", SHA1KEY
,
374 if (MATCHFLAG(F_SHA256
))
375 appendfield(pathlast
, "%s=%s", SHA256KEY
,
377 if (MATCHFLAG(F_SHA384
))
378 appendfield(pathlast
, "%s=%s", SHA384KEY
,
380 if (MATCHFLAG(F_SHA512
))
381 appendfield(pathlast
, "%s=%s", SHA512KEY
,
383 if (MATCHFLAG(F_FLAGS
)) {
384 str
= flags_to_string(cur
->st_flags
, "none");
385 appendfield(pathlast
, "flags=%s", str
);
388 if (MATCHFLAG(F_IGN
))
389 appendfield(pathlast
, "ignore");
390 if (MATCHFLAG(F_OPT
))
391 appendfield(pathlast
, "optional");
392 if (MATCHFLAG(F_TAGS
)) {
393 /* don't output leading or trailing commas */
398 while(q
> p
&& q
[-1] == ',')
400 appendfield(pathlast
, "tags=%.*s", (int)(q
- p
), p
);
402 puts(pathlast
? vispath(path
) : "");
405 dump_nodes(path
, cur
->child
, pathlast
);
411 * strsvis(3) encodes path, which must not be longer than MAXPATHLEN
412 * characters long, and returns a pointer to a static buffer containing
416 vispath(const char *path
)
418 const char extra
[] = { ' ', '\t', '\n', '\\', '#',
421 * We don't encode the globbing characters yet, because they
422 * get encoded as \c and strunvis fails to decode them
427 static char pathbuf
[4*MAXPATHLEN
+ 1];
429 strsvis(pathbuf
, path
, VIS_CSTYLE
, extra
);
437 #define MAX_PACK_ARGS 3
438 u_long numbers
[MAX_PACK_ARGS
];
443 const char *error
= NULL
;
445 if ((dev
= strchr(arg
, ',')) != NULL
) {
447 if ((pack
= pack_find(arg
)) == NULL
)
448 mtree_err("unknown format `%s'", arg
);
450 while ((p
= strsep(&dev
, ",")) != NULL
) {
452 mtree_err("missing number");
453 numbers
[argc
++] = strtoul(p
, &ep
, 0);
455 mtree_err("invalid number `%s'",
457 if (argc
> MAX_PACK_ARGS
)
458 mtree_err("too many arguments");
461 mtree_err("not enough arguments");
462 result
= (*pack
)(argc
, numbers
, &error
);
464 mtree_err("%s", error
);
466 result
= (dev_t
)strtoul(arg
, &ep
, 0);
468 mtree_err("invalid device `%s'", arg
);
474 replacenode(NODE
*cur
, NODE
*new)
477 #define REPLACE(x) cur->x = new->x
478 #define REPLACESTR(x) REPLACEPTR(cur->x,new->x)
480 if (cur
->type
!= new->type
) {
483 * merge entries with different types; we
484 * don't want children retained in this case.
487 free_nodes(cur
->child
);
491 "existing entry for `%s', type `%s'"
492 " does not match type `%s'",
493 cur
->name
, nodetype(cur
->type
),
494 nodetype(new->type
));
499 REPLACE(st_mtimespec
);
501 if (cur
->slink
!= NULL
) {
502 if ((cur
->slink
= strdup(new->slink
)) == NULL
)
503 mtree_err("memory allocation error");
504 if (strunvis(cur
->slink
, new->slink
) == -1)
505 mtree_err("strunvis failed on `%s'", new->slink
);
515 REPLACESTR(md5digest
);
516 REPLACESTR(rmd160digest
);
517 REPLACESTR(sha1digest
);
518 REPLACESTR(sha256digest
);
519 REPLACESTR(sha384digest
);
520 REPLACESTR(sha512digest
);
528 set(char *t
, NODE
*ip
)
530 int type
, value
, len
;
533 char *kw
, *val
, *md
, *ep
;
536 while ((kw
= strsep(&t
, "= \t")) != NULL
) {
539 if (strcmp(kw
, "all") == 0)
540 mtree_err("invalid keyword `all'");
541 ip
->flags
|= type
= parsekey(kw
, &value
);
543 /* Just set flag bit (F_IGN and F_OPT) */
545 while ((val
= strsep(&t
, " \t")) != NULL
&& *val
== '\0')
548 mtree_err("missing value");
551 ip
->cksum
= strtoul(val
, &ep
, 10);
553 mtree_err("invalid checksum `%s'", val
);
556 ip
->st_rdev
= parsedev(val
);
559 if (strcmp("none", val
) == 0)
561 else if (string_to_flags(&val
, &ip
->st_flags
, NULL
)
563 mtree_err("invalid flag `%s'", val
);
566 ip
->st_gid
= (gid_t
)strtoul(val
, &ep
, 10);
568 mtree_err("invalid gid `%s'", val
);
571 if (mtree_Wflag
) /* don't parse if whacking */
573 if (gid_from_group(val
, &gid
) == -1)
574 mtree_err("unknown group `%s'", val
);
578 if (val
[0]=='0' && val
[1]=='x')
582 if ((ip
->md5digest
= strdup(md
)) == NULL
)
583 mtree_err("memory allocation error");
586 if ((m
= setmode(val
)) == NULL
)
587 mtree_err("cannot set file mode `%s' (%s)",
588 val
, strerror(errno
));
589 ip
->st_mode
= getmode(m
, 0);
593 ip
->st_nlink
= (nlink_t
)strtoul(val
, &ep
, 10);
595 mtree_err("invalid link count `%s'", val
);
598 if (val
[0]=='0' && val
[1]=='x')
602 if ((ip
->rmd160digest
= strdup(md
)) == NULL
)
603 mtree_err("memory allocation error");
606 if (val
[0]=='0' && val
[1]=='x')
610 if ((ip
->sha1digest
= strdup(md
)) == NULL
)
611 mtree_err("memory allocation error");
614 ip
->st_size
= (off_t
)strtoll(val
, &ep
, 10);
616 mtree_err("invalid size `%s'", val
);
619 if ((ip
->slink
= strdup(val
)) == NULL
)
620 mtree_err("memory allocation error");
621 if (strunvis(ip
->slink
, val
) == -1)
622 mtree_err("strunvis failed on `%s'", val
);
625 len
= strlen(val
) + 3; /* "," + str + ",\0" */
626 if ((ip
->tags
= malloc(len
)) == NULL
)
627 mtree_err("memory allocation error");
628 snprintf(ip
->tags
, len
, ",%s,", val
);
631 ip
->st_mtimespec
.tv_sec
=
632 (time_t)strtoll(val
, &ep
, 10);
634 mtree_err("invalid time `%s'", val
);
636 ip
->st_mtimespec
.tv_nsec
= strtol(val
, &ep
, 10);
638 mtree_err("invalid time `%s'", val
);
641 ip
->type
= parsetype(val
);
644 ip
->st_uid
= (uid_t
)strtoul(val
, &ep
, 10);
646 mtree_err("invalid uid `%s'", val
);
649 if (mtree_Wflag
) /* don't parse if whacking */
651 if (uid_from_user(val
, &uid
) == -1)
652 mtree_err("unknown user `%s'", val
);
656 if (val
[0]=='0' && val
[1]=='x')
660 if ((ip
->sha256digest
= strdup(md
)) == NULL
)
661 mtree_err("memory allocation error");
664 if (val
[0]=='0' && val
[1]=='x')
668 if ((ip
->sha384digest
= strdup(md
)) == NULL
)
669 mtree_err("memory allocation error");
672 if (val
[0]=='0' && val
[1]=='x')
676 if ((ip
->sha512digest
= strdup(md
)) == NULL
)
677 mtree_err("memory allocation error");
681 "set(): unsupported key type 0x%x (INTERNAL ERROR)",
689 unset(char *t
, NODE
*ip
)
693 while ((p
= strsep(&t
, " \t")) != NULL
) {
696 ip
->flags
&= ~parsekey(p
, NULL
);
702 * Add the centry node as a child of the pathparent node. If
703 * centry is a duplicate, call replacenode(). If centry is not
704 * a duplicate, insert it into the linked list referenced by
705 * pathparent->child. Keep the list sorted if Sflag is set.
708 addchild(NODE
*pathparent
, NODE
*centry
)
710 NODE
*samename
; /* node with the same name as centry */
711 NODE
*replacepos
; /* if non-NULL, centry should replace this node */
712 NODE
*insertpos
; /* if non-NULL, centry should be inserted
714 NODE
*cur
; /* for stepping through the list */
715 NODE
*last
; /* the last node in the list */
722 cur
= pathparent
->child
;
724 /* centry is pathparent's first and only child node so far */
725 pathparent
->child
= centry
;
730 * pathparent already has at least one other child, so add the
731 * centry node to the list.
733 * We first scan through the list looking for an existing node
734 * with the same name (setting samename), and also looking
735 * for the correct position to replace or insert the new node
736 * (setting replacepos and/or insertpos).
738 for (; cur
!= NULL
; last
= cur
, cur
= cur
->next
) {
739 if (strcmp(centry
->name
, cur
->name
) == 0) {
743 cmp
= nodecmp(centry
, cur
);
746 } else if (cmp
> 0) {
752 if (samename
!= NULL
) {
753 /* replace node with same name */
754 replacepos
= samename
;
756 /* add new node at end of list */
761 if (samename
!= NULL
) {
763 * We found a node with the same name above. Call
764 * replacenode(), which will either exit with an error,
765 * or replace the information in the samename node and
766 * free the information in the centry node.
768 replacenode(samename
, centry
);
769 if (samename
== replacepos
) {
770 /* The just-replaced node was in the correct position */
773 if (samename
== insertpos
|| samename
->prev
== insertpos
) {
775 * We thought the new node should be just before
776 * or just after the replaced node, but that would
777 * be equivalent to just retaining the replaced node.
783 * The just-replaced node is in the wrong position in
784 * the list. This can happen if sort order depends on
785 * criteria other than the node name.
787 * Make centry point to the just-replaced node. Unlink
788 * the just-replaced node from the list, and allow it to
789 * be insterted in the correct position later.
793 centry
->prev
->next
= centry
->next
;
795 /* centry->next is the new head of the list */
796 pathparent
->child
= centry
->next
;
797 assert(centry
->next
!= NULL
);
800 centry
->next
->prev
= centry
->prev
;
805 if (insertpos
== NULL
) {
806 /* insert centry at the beginning of the list */
807 pathparent
->child
->prev
= centry
;
808 centry
->next
= pathparent
->child
;
810 pathparent
->child
= centry
;
812 /* insert centry into the list just after insertpos */
813 centry
->next
= insertpos
->next
;
814 insertpos
->next
= centry
;
815 centry
->prev
= insertpos
;
817 centry
->next
->prev
= centry
;
824 * used as a comparison function by addchild() to control the order
825 * in which entries appear within a list of sibling nodes. We make
826 * directories sort after non-directories, but otherwise sort in
829 * Keep this in sync with dcmp() in create.c.
832 nodecmp(const NODE
*a
, const NODE
*b
)
835 if ((a
->type
& F_DIR
) != 0) {
836 if ((b
->type
& F_DIR
) == 0)
838 } else if ((b
->type
& F_DIR
) != 0)
840 return strcmp(a
->name
, b
->name
);