Home | History | Annotate | Line # | Download | only in mtree
spec.c revision 1.62
      1  1.62       dsl /*	$NetBSD: spec.c,v 1.62 2006/04/12 19:49:59 dsl Exp $	*/
      2   1.6       cgd 
      3   1.1       cgd /*-
      4   1.5       cgd  * Copyright (c) 1989, 1993
      5   1.5       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.50       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.31     lukem /*-
     33  1.57     lukem  * Copyright (c) 2001-2004 The NetBSD Foundation, Inc.
     34  1.31     lukem  * All rights reserved.
     35  1.31     lukem  *
     36  1.31     lukem  * This code is derived from software contributed to The NetBSD Foundation
     37  1.31     lukem  * by Luke Mewburn of Wasabi Systems.
     38  1.31     lukem  *
     39  1.31     lukem  * Redistribution and use in source and binary forms, with or without
     40  1.31     lukem  * modification, are permitted provided that the following conditions
     41  1.31     lukem  * are met:
     42  1.31     lukem  * 1. Redistributions of source code must retain the above copyright
     43  1.31     lukem  *    notice, this list of conditions and the following disclaimer.
     44  1.31     lukem  * 2. Redistributions in binary form must reproduce the above copyright
     45  1.31     lukem  *    notice, this list of conditions and the following disclaimer in the
     46  1.31     lukem  *    documentation and/or other materials provided with the distribution.
     47  1.31     lukem  * 3. All advertising materials mentioning features or use of this software
     48  1.31     lukem  *    must display the following acknowledgement:
     49  1.31     lukem  *        This product includes software developed by the NetBSD
     50  1.31     lukem  *        Foundation, Inc. and its contributors.
     51  1.31     lukem  * 4. Neither the name of The NetBSD Foundation nor the names of its
     52  1.31     lukem  *    contributors may be used to endorse or promote products derived
     53  1.31     lukem  *    from this software without specific prior written permission.
     54  1.31     lukem  *
     55  1.31     lukem  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     56  1.31     lukem  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     57  1.31     lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     58  1.31     lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     59  1.31     lukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     60  1.31     lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     61  1.31     lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     62  1.31     lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     63  1.31     lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     64  1.31     lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     65  1.31     lukem  * POSSIBILITY OF SUCH DAMAGE.
     66  1.31     lukem  */
     67  1.31     lukem 
     68  1.56       jmc #if HAVE_NBTOOL_CONFIG_H
     69  1.56       jmc #include "nbtool_config.h"
     70  1.56       jmc #endif
     71  1.56       jmc 
     72  1.11     lukem #include <sys/cdefs.h>
     73  1.45        tv #if defined(__RCSID) && !defined(lint)
     74   1.6       cgd #if 0
     75  1.10       mrg static char sccsid[] = "@(#)spec.c	8.2 (Berkeley) 4/28/95";
     76   1.6       cgd #else
     77  1.62       dsl __RCSID("$NetBSD: spec.c,v 1.62 2006/04/12 19:49:59 dsl Exp $");
     78   1.6       cgd #endif
     79   1.1       cgd #endif /* not lint */
     80   1.1       cgd 
     81  1.25     lukem #include <sys/param.h>
     82   1.3       cgd #include <sys/stat.h>
     83  1.21    simonb 
     84  1.11     lukem #include <ctype.h>
     85  1.11     lukem #include <errno.h>
     86  1.11     lukem #include <grp.h>
     87   1.1       cgd #include <pwd.h>
     88  1.11     lukem #include <stdio.h>
     89  1.34     lukem #include <stdlib.h>
     90  1.11     lukem #include <string.h>
     91   1.3       cgd #include <unistd.h>
     92  1.49     lukem #include <vis.h>
     93  1.53       dbj #include <util.h>
     94  1.11     lukem 
     95   1.3       cgd #include "extern.h"
     96  1.32     lukem #include "pack_dev.h"
     97   1.1       cgd 
     98  1.39     lukem size_t	mtree_lineno;			/* Current spec line number */
     99  1.57     lukem int	mtree_Mflag;			/* Merge duplicate entries */
    100  1.57     lukem int	mtree_Wflag;			/* Don't "whack" permissions */
    101   1.1       cgd 
    102  1.47     lukem static	dev_t	parsedev(char *);
    103  1.47     lukem static	void	replacenode(NODE *, NODE *);
    104  1.47     lukem static	void	set(char *, NODE *);
    105  1.47     lukem static	void	unset(char *, NODE *);
    106   1.1       cgd 
    107  1.57     lukem #define REPLACEPTR(x,v)	do { if ((x)) free((x)); (x) = (v); } while (0)
    108  1.57     lukem 
    109   1.3       cgd NODE *
    110  1.34     lukem spec(FILE *fp)
    111   1.1       cgd {
    112  1.31     lukem 	NODE *centry, *last, *pathparent, *cur;
    113  1.33     lukem 	char *p, *e, *next;
    114   1.3       cgd 	NODE ginfo, *root;
    115  1.51    itojun 	char *buf, *tname, *ntname;
    116  1.31     lukem 	size_t tnamelen, plen;
    117   1.1       cgd 
    118   1.3       cgd 	root = NULL;
    119  1.11     lukem 	centry = last = NULL;
    120  1.31     lukem 	tname = NULL;
    121  1.31     lukem 	tnamelen = 0;
    122  1.11     lukem 	memset(&ginfo, 0, sizeof(ginfo));
    123  1.39     lukem 	for (mtree_lineno = 0;
    124  1.39     lukem 	    (buf = fparseln(fp, NULL, &mtree_lineno, NULL,
    125  1.24     lukem 		FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT | FPARSELN_UNESCESC));
    126  1.24     lukem 	    free(buf)) {
    127  1.24     lukem 		/* Skip leading whitespace. */
    128  1.24     lukem 		for (p = buf; *p && isspace((unsigned char)*p); ++p)
    129   1.3       cgd 			continue;
    130   1.3       cgd 
    131  1.24     lukem 		/* If nothing but whitespace, continue. */
    132  1.24     lukem 		if (!*p)
    133   1.1       cgd 			continue;
    134   1.1       cgd 
    135   1.3       cgd #ifdef DEBUG
    136  1.40     lukem 		fprintf(stderr, "line %lu: {%s}\n",
    137  1.39     lukem 		    (u_long)mtree_lineno, p);
    138   1.3       cgd #endif
    139   1.3       cgd 		/* Grab file name, "$", "set", or "unset". */
    140  1.33     lukem 		next = buf;
    141  1.33     lukem 		while ((p = strsep(&next, " \t")) != NULL && *p == '\0')
    142  1.33     lukem 			continue;
    143  1.33     lukem 		if (p == NULL)
    144  1.13  wsanchez 			mtree_err("missing field");
    145   1.1       cgd 
    146  1.23     lukem 		if (p[0] == '/') {
    147  1.23     lukem 			if (strcmp(p + 1, "set") == 0)
    148  1.33     lukem 				set(next, &ginfo);
    149  1.23     lukem 			else if (strcmp(p + 1, "unset") == 0)
    150  1.33     lukem 				unset(next, &ginfo);
    151  1.23     lukem 			else
    152  1.23     lukem 				mtree_err("invalid specification `%s'", p);
    153  1.23     lukem 			continue;
    154  1.23     lukem 		}
    155   1.1       cgd 
    156  1.31     lukem 		if (strcmp(p, "..") == 0) {
    157   1.3       cgd 			/* Don't go up, if haven't gone down. */
    158  1.31     lukem 			if (root == NULL)
    159   1.3       cgd 				goto noparent;
    160   1.1       cgd 			if (last->type != F_DIR || last->flags & F_DONE) {
    161   1.1       cgd 				if (last == root)
    162   1.3       cgd 					goto noparent;
    163   1.1       cgd 				last = last->parent;
    164   1.1       cgd 			}
    165   1.1       cgd 			last->flags |= F_DONE;
    166   1.1       cgd 			continue;
    167   1.3       cgd 
    168  1.13  wsanchez noparent:		mtree_err("no parent node");
    169   1.1       cgd 		}
    170   1.1       cgd 
    171  1.31     lukem 		plen = strlen(p) + 1;
    172  1.31     lukem 		if (plen > tnamelen) {
    173  1.51    itojun 			if ((ntname = realloc(tname, plen)) == NULL)
    174  1.51    itojun 				mtree_err("realloc: %s", strerror(errno));
    175  1.51    itojun 			tname = ntname;
    176  1.31     lukem 			tnamelen = plen;
    177  1.31     lukem 		}
    178  1.31     lukem 		if (strunvis(tname, p) == -1)
    179  1.31     lukem 			mtree_err("strunvis failed on `%s'", p);
    180  1.31     lukem 		p = tname;
    181  1.31     lukem 
    182  1.31     lukem 		pathparent = NULL;
    183  1.31     lukem 		if (strchr(p, '/') != NULL) {
    184  1.31     lukem 			cur = root;
    185  1.31     lukem 			for (; (e = strchr(p, '/')) != NULL; p = e+1) {
    186  1.31     lukem 				if (p == e)
    187  1.31     lukem 					continue;	/* handle // */
    188  1.31     lukem 				*e = '\0';
    189  1.31     lukem 				if (strcmp(p, ".") != 0) {
    190  1.47     lukem 					while (cur &&
    191  1.47     lukem 					    strcmp(cur->name, p) != 0) {
    192  1.31     lukem 						cur = cur->next;
    193  1.31     lukem 					}
    194  1.31     lukem 				}
    195  1.31     lukem 				if (cur == NULL || cur->type != F_DIR) {
    196  1.31     lukem 					mtree_err("%s: %s", tname,
    197  1.31     lukem 					    strerror(ENOENT));
    198  1.31     lukem 				}
    199  1.31     lukem 				*e = '/';
    200  1.31     lukem 				pathparent = cur;
    201  1.31     lukem 				cur = cur->child;
    202  1.31     lukem 			}
    203  1.31     lukem 			if (*p == '\0')
    204  1.31     lukem 				mtree_err("%s: empty leaf element", tname);
    205  1.31     lukem 		}
    206  1.31     lukem 
    207   1.3       cgd 		if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
    208  1.13  wsanchez 			mtree_err("%s", strerror(errno));
    209   1.1       cgd 		*centry = ginfo;
    210  1.39     lukem 		centry->lineno = mtree_lineno;
    211  1.31     lukem 		strcpy(centry->name, p);
    212   1.1       cgd #define	MAGIC	"?*["
    213   1.1       cgd 		if (strpbrk(p, MAGIC))
    214   1.1       cgd 			centry->flags |= F_MAGIC;
    215  1.33     lukem 		set(next, centry);
    216   1.1       cgd 
    217  1.31     lukem 		if (root == NULL) {
    218  1.47     lukem 				/*
    219  1.47     lukem 				 * empty tree
    220  1.47     lukem 				 */
    221  1.47     lukem 			if (strcmp(centry->name, ".") != 0 ||
    222  1.47     lukem 			    centry->type != F_DIR)
    223  1.34     lukem 				mtree_err(
    224  1.34     lukem 				    "root node must be the directory `.'");
    225   1.1       cgd 			last = root = centry;
    226   1.1       cgd 			root->parent = root;
    227  1.31     lukem 		} else if (pathparent != NULL) {
    228  1.47     lukem 				/*
    229  1.47     lukem 				 * full path entry
    230  1.47     lukem 				 */
    231  1.31     lukem 			centry->parent = pathparent;
    232  1.31     lukem 			cur = pathparent->child;
    233  1.47     lukem 			if (cur == NULL) {
    234  1.31     lukem 				pathparent->child = centry;
    235  1.47     lukem 				last = centry;
    236  1.47     lukem 			} else {
    237  1.47     lukem 				for (; cur != NULL; cur = cur->next) {
    238  1.47     lukem 					if (strcmp(cur->name, centry->name)
    239  1.47     lukem 					    == 0) {
    240  1.47     lukem 						/* existing entry; replace */
    241  1.47     lukem 						replacenode(cur, centry);
    242  1.47     lukem 						break;
    243  1.47     lukem 					}
    244  1.47     lukem 					if (cur->next == NULL) {
    245  1.47     lukem 						/* last entry; add new */
    246  1.47     lukem 						cur->next = centry;
    247  1.47     lukem 						centry->prev = cur;
    248  1.47     lukem 						break;
    249  1.47     lukem 					}
    250  1.47     lukem 				}
    251  1.47     lukem 				last = cur;
    252  1.47     lukem 				while (last->next != NULL)
    253  1.47     lukem 					last = last->next;
    254  1.31     lukem 			}
    255  1.47     lukem 		} else if (strcmp(centry->name, ".") == 0) {
    256  1.47     lukem 				/*
    257  1.47     lukem 				 * duplicate "." entry; always replace
    258  1.47     lukem 				 */
    259  1.47     lukem 			replacenode(root, centry);
    260   1.1       cgd 		} else if (last->type == F_DIR && !(last->flags & F_DONE)) {
    261  1.47     lukem 				/*
    262  1.47     lukem 				 * new relative child
    263  1.47     lukem 				 * (no duplicate check)
    264  1.47     lukem 				 */
    265   1.1       cgd 			centry->parent = last;
    266   1.1       cgd 			last = last->child = centry;
    267   1.1       cgd 		} else {
    268  1.47     lukem 				/*
    269  1.47     lukem 				 * relative entry, up one directory
    270  1.47     lukem 				 * (no duplicate check)
    271  1.47     lukem 				 */
    272   1.1       cgd 			centry->parent = last->parent;
    273   1.1       cgd 			centry->prev = last;
    274   1.1       cgd 			last = last->next = centry;
    275   1.1       cgd 		}
    276   1.1       cgd 	}
    277   1.3       cgd 	return (root);
    278   1.1       cgd }
    279   1.1       cgd 
    280  1.57     lukem void
    281  1.57     lukem free_nodes(NODE *root)
    282  1.57     lukem {
    283  1.57     lukem 	NODE	*cur, *next;
    284  1.57     lukem 
    285  1.57     lukem 	if (root == NULL)
    286  1.57     lukem 		return;
    287  1.57     lukem 
    288  1.57     lukem 	next = NULL;
    289  1.57     lukem 	for (cur = root; cur != NULL; cur = next) {
    290  1.57     lukem 		next = cur->next;
    291  1.57     lukem 		free_nodes(cur->child);
    292  1.57     lukem 		REPLACEPTR(cur->slink, NULL);
    293  1.57     lukem 		REPLACEPTR(cur->md5digest, NULL);
    294  1.57     lukem 		REPLACEPTR(cur->rmd160digest, NULL);
    295  1.57     lukem 		REPLACEPTR(cur->sha1digest, NULL);
    296  1.60      elad 		REPLACEPTR(cur->sha256digest, NULL);
    297  1.60      elad 		REPLACEPTR(cur->sha384digest, NULL);
    298  1.60      elad 		REPLACEPTR(cur->sha512digest, NULL);
    299  1.57     lukem 		REPLACEPTR(cur->tags, NULL);
    300  1.57     lukem 		REPLACEPTR(cur, NULL);
    301  1.57     lukem 	}
    302  1.57     lukem }
    303  1.57     lukem 
    304  1.25     lukem /*
    305  1.25     lukem  * dump_nodes --
    306  1.49     lukem  *	dump the NODEs from `cur', based in the directory `dir'.
    307  1.49     lukem  *	if pathlast is none zero, print the path last, otherwise print
    308  1.49     lukem  *	it first.
    309  1.25     lukem  */
    310  1.25     lukem void
    311  1.49     lukem dump_nodes(const char *dir, NODE *root, int pathlast)
    312  1.25     lukem {
    313  1.25     lukem 	NODE	*cur;
    314  1.41     lukem 	char	path[MAXPATHLEN];
    315  1.30     lukem 	const char *name;
    316  1.25     lukem 
    317  1.25     lukem 	for (cur = root; cur != NULL; cur = cur->next) {
    318  1.28     lukem 		if (cur->type != F_DIR && !matchtags(cur))
    319  1.28     lukem 			continue;
    320  1.26     lukem 
    321  1.25     lukem 		if (snprintf(path, sizeof(path), "%s%s%s",
    322  1.25     lukem 		    dir, *dir ? "/" : "", cur->name)
    323  1.25     lukem 		    >= sizeof(path))
    324  1.25     lukem 			mtree_err("Pathname too long.");
    325  1.30     lukem 
    326  1.49     lukem 		if (!pathlast)
    327  1.49     lukem 			printf("%s ", vispath(path));
    328  1.49     lukem 
    329  1.30     lukem #define MATCHFLAG(f)	((keys & (f)) && (cur->flags & (f)))
    330  1.30     lukem 		if (MATCHFLAG(F_TYPE))
    331  1.25     lukem 			printf("type=%s ", nodetype(cur->type));
    332  1.30     lukem 		if (MATCHFLAG(F_UID | F_UNAME)) {
    333  1.30     lukem 			if (keys & F_UNAME &&
    334  1.30     lukem 			    (name = user_from_uid(cur->st_uid, 1)) != NULL)
    335  1.30     lukem 				printf("uname=%s ", name);
    336  1.30     lukem 			else
    337  1.30     lukem 				printf("uid=%u ", cur->st_uid);
    338  1.30     lukem 		}
    339  1.30     lukem 		if (MATCHFLAG(F_GID | F_GNAME)) {
    340  1.30     lukem 			if (keys & F_GNAME &&
    341  1.30     lukem 			    (name = group_from_gid(cur->st_gid, 1)) != NULL)
    342  1.30     lukem 				printf("gname=%s ", name);
    343  1.30     lukem 			else
    344  1.30     lukem 				printf("gid=%u ", cur->st_gid);
    345  1.30     lukem 		}
    346  1.30     lukem 		if (MATCHFLAG(F_MODE))
    347  1.25     lukem 			printf("mode=%#o ", cur->st_mode);
    348  1.32     lukem 		if (MATCHFLAG(F_DEV) &&
    349  1.32     lukem 		    (cur->type == F_BLOCK || cur->type == F_CHAR))
    350  1.32     lukem 			printf("device=%#x ", cur->st_rdev);
    351  1.31     lukem 		if (MATCHFLAG(F_NLINK))
    352  1.25     lukem 			printf("nlink=%d ", cur->st_nlink);
    353  1.30     lukem 		if (MATCHFLAG(F_SLINK))
    354  1.58     lukem 			printf("link=%s ", vispath(cur->slink));
    355  1.30     lukem 		if (MATCHFLAG(F_SIZE))
    356  1.25     lukem 			printf("size=%lld ", (long long)cur->st_size);
    357  1.30     lukem 		if (MATCHFLAG(F_TIME))
    358  1.25     lukem 			printf("time=%ld.%ld ", (long)cur->st_mtimespec.tv_sec,
    359  1.25     lukem 			    cur->st_mtimespec.tv_nsec);
    360  1.30     lukem 		if (MATCHFLAG(F_CKSUM))
    361  1.25     lukem 			printf("cksum=%lu ", cur->cksum);
    362  1.30     lukem 		if (MATCHFLAG(F_MD5))
    363  1.40     lukem 			printf("md5=%s ", cur->md5digest);
    364  1.40     lukem 		if (MATCHFLAG(F_RMD160))
    365  1.40     lukem 			printf("rmd160=%s ", cur->rmd160digest);
    366  1.40     lukem 		if (MATCHFLAG(F_SHA1))
    367  1.40     lukem 			printf("sha1=%s ", cur->sha1digest);
    368  1.60      elad 		if (MATCHFLAG(F_SHA256))
    369  1.60      elad 			printf("sha256=%s ", cur->sha256digest);
    370  1.60      elad 		if (MATCHFLAG(F_SHA384))
    371  1.60      elad 			printf("sha384=%s ", cur->sha384digest);
    372  1.60      elad 		if (MATCHFLAG(F_SHA512))
    373  1.60      elad 			printf("sha512=%s ", cur->sha512digest);
    374  1.30     lukem 		if (MATCHFLAG(F_FLAGS))
    375  1.25     lukem 			printf("flags=%s ",
    376  1.25     lukem 			    flags_to_string(cur->st_flags, "none"));
    377  1.30     lukem 		if (MATCHFLAG(F_IGN))
    378  1.30     lukem 			printf("ignore ");
    379  1.30     lukem 		if (MATCHFLAG(F_OPT))
    380  1.30     lukem 			printf("optional ");
    381  1.30     lukem 		if (MATCHFLAG(F_TAGS))
    382  1.26     lukem 			printf("tags=%s ", cur->tags);
    383  1.49     lukem 		puts(pathlast ? vispath(path) : "");
    384  1.25     lukem 
    385  1.25     lukem 		if (cur->child)
    386  1.49     lukem 			dump_nodes(path, cur->child, pathlast);
    387  1.25     lukem 	}
    388  1.25     lukem }
    389  1.49     lukem 
    390  1.49     lukem /*
    391  1.49     lukem  * vispath --
    392  1.49     lukem  *	strsvis(3) encodes path, which must not be longer than MAXPATHLEN
    393  1.49     lukem  *	characters long, and returns a pointer to a static buffer containing
    394  1.49     lukem  *	the result.
    395  1.49     lukem  */
    396  1.49     lukem char *
    397  1.49     lukem vispath(const char *path)
    398  1.49     lukem {
    399  1.49     lukem 	const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
    400  1.49     lukem 	static char pathbuf[4*MAXPATHLEN + 1];
    401  1.49     lukem 
    402  1.49     lukem 	strsvis(pathbuf, path, VIS_CSTYLE, extra);
    403  1.49     lukem 	return(pathbuf);
    404  1.49     lukem }
    405  1.49     lukem 
    406  1.25     lukem 
    407  1.32     lukem static dev_t
    408  1.32     lukem parsedev(char *arg)
    409  1.32     lukem {
    410  1.32     lukem #define MAX_PACK_ARGS	3
    411  1.32     lukem 	u_long	numbers[MAX_PACK_ARGS];
    412  1.32     lukem 	char	*p, *ep, *dev;
    413  1.32     lukem 	int	argc;
    414  1.32     lukem 	pack_t	*pack;
    415  1.32     lukem 	dev_t	result;
    416  1.55  christos 	const char *error = NULL;
    417  1.32     lukem 
    418  1.32     lukem 	if ((dev = strchr(arg, ',')) != NULL) {
    419  1.32     lukem 		*dev++='\0';
    420  1.32     lukem 		if ((pack = pack_find(arg)) == NULL)
    421  1.32     lukem 			mtree_err("unknown format `%s'", arg);
    422  1.32     lukem 		argc = 0;
    423  1.32     lukem 		while ((p = strsep(&dev, ",")) != NULL) {
    424  1.32     lukem 			if (*p == '\0')
    425  1.32     lukem 				mtree_err("missing number");
    426  1.32     lukem 			numbers[argc++] = strtoul(p, &ep, 0);
    427  1.32     lukem 			if (*ep != '\0')
    428  1.32     lukem 				mtree_err("invalid number `%s'",
    429  1.32     lukem 				    p);
    430  1.32     lukem 			if (argc > MAX_PACK_ARGS)
    431  1.32     lukem 				mtree_err("too many arguments");
    432  1.32     lukem 		}
    433  1.32     lukem 		if (argc < 2)
    434  1.32     lukem 			mtree_err("not enough arguments");
    435  1.54      ross 		result = (*pack)(argc, numbers, &error);
    436  1.54      ross 		if (error != NULL)
    437  1.54      ross 			mtree_err(error);
    438  1.32     lukem 	} else {
    439  1.32     lukem 		result = (dev_t)strtoul(arg, &ep, 0);
    440  1.32     lukem 		if (*ep != '\0')
    441  1.32     lukem 			mtree_err("invalid device `%s'", arg);
    442  1.32     lukem 	}
    443  1.32     lukem 	return (result);
    444  1.47     lukem }
    445  1.47     lukem 
    446  1.47     lukem static void
    447  1.47     lukem replacenode(NODE *cur, NODE *new)
    448  1.47     lukem {
    449  1.47     lukem 
    450  1.47     lukem #define REPLACE(x)	cur->x = new->x
    451  1.57     lukem #define REPLACESTR(x)	REPLACEPTR(cur->x,new->x)
    452  1.57     lukem 
    453  1.57     lukem 	if (cur->type != new->type) {
    454  1.57     lukem 		if (mtree_Mflag) {
    455  1.57     lukem 				/*
    456  1.57     lukem 				 * merge entries with different types; we
    457  1.57     lukem 				 * don't want children retained in this case.
    458  1.57     lukem 				 */
    459  1.57     lukem 			REPLACE(type);
    460  1.57     lukem 			free_nodes(cur->child);
    461  1.57     lukem 			cur->child = NULL;
    462  1.57     lukem 		} else {
    463  1.57     lukem 			mtree_err(
    464  1.57     lukem 			    "existing entry for `%s', type `%s'"
    465  1.57     lukem 			    " does not match type `%s'",
    466  1.57     lukem 			    cur->name, nodetype(cur->type),
    467  1.57     lukem 			    nodetype(new->type));
    468  1.57     lukem 		}
    469  1.57     lukem 	}
    470  1.47     lukem 
    471  1.47     lukem 	REPLACE(st_size);
    472  1.47     lukem 	REPLACE(st_mtimespec);
    473  1.47     lukem 	REPLACESTR(slink);
    474  1.59     lukem 	if (cur->slink != NULL) {
    475  1.59     lukem 		if ((cur->slink = strdup(new->slink)) == NULL)
    476  1.59     lukem 			mtree_err("memory allocation error");
    477  1.59     lukem 		if (strunvis(cur->slink, new->slink) == -1)
    478  1.59     lukem 			mtree_err("strunvis failed on `%s'", new->slink);
    479  1.59     lukem 		free(new->slink);
    480  1.59     lukem 	}
    481  1.47     lukem 	REPLACE(st_uid);
    482  1.47     lukem 	REPLACE(st_gid);
    483  1.47     lukem 	REPLACE(st_mode);
    484  1.47     lukem 	REPLACE(st_rdev);
    485  1.47     lukem 	REPLACE(st_flags);
    486  1.47     lukem 	REPLACE(st_nlink);
    487  1.47     lukem 	REPLACE(cksum);
    488  1.47     lukem 	REPLACESTR(md5digest);
    489  1.47     lukem 	REPLACESTR(rmd160digest);
    490  1.47     lukem 	REPLACESTR(sha1digest);
    491  1.60      elad 	REPLACESTR(sha256digest);
    492  1.60      elad 	REPLACESTR(sha384digest);
    493  1.60      elad 	REPLACESTR(sha512digest);
    494  1.47     lukem 	REPLACESTR(tags);
    495  1.47     lukem 	REPLACE(lineno);
    496  1.47     lukem 	REPLACE(flags);
    497  1.47     lukem 	free(new);
    498  1.32     lukem }
    499  1.32     lukem 
    500   1.3       cgd static void
    501  1.21    simonb set(char *t, NODE *ip)
    502   1.1       cgd {
    503  1.32     lukem 	int	type, value, len;
    504  1.32     lukem 	gid_t	gid;
    505  1.32     lukem 	uid_t	uid;
    506  1.32     lukem 	char	*kw, *val, *md, *ep;
    507  1.32     lukem 	void	*m;
    508   1.3       cgd 
    509  1.33     lukem 	while ((kw = strsep(&t, "= \t")) != NULL) {
    510  1.33     lukem 		if (*kw == '\0')
    511  1.33     lukem 			continue;
    512  1.30     lukem 		if (strcmp(kw, "all") == 0)
    513  1.30     lukem 			mtree_err("invalid keyword `all'");
    514   1.3       cgd 		ip->flags |= type = parsekey(kw, &value);
    515  1.62       dsl 		if (!value)
    516  1.62       dsl 			/* Just set flag bit (F_IGN and F_OPT) */
    517  1.62       dsl 			continue;
    518  1.62       dsl 		while ((val = strsep(&t, " \t")) != NULL && *val == '\0')
    519  1.62       dsl 			continue;
    520  1.62       dsl 		if (val == NULL)
    521  1.62       dsl 			mtree_err("missing value");
    522  1.62       dsl 		switch (type) {
    523   1.1       cgd 		case F_CKSUM:
    524   1.3       cgd 			ip->cksum = strtoul(val, &ep, 10);
    525   1.3       cgd 			if (*ep)
    526  1.22  christos 				mtree_err("invalid checksum `%s'", val);
    527  1.14       mrg 			break;
    528  1.32     lukem 		case F_DEV:
    529  1.32     lukem 			ip->st_rdev = parsedev(val);
    530  1.32     lukem 			break;
    531  1.14       mrg 		case F_FLAGS:
    532  1.14       mrg 			if (strcmp("none", val) == 0)
    533  1.14       mrg 				ip->st_flags = 0;
    534  1.32     lukem 			else if (string_to_flags(&val, &ip->st_flags, NULL)
    535  1.32     lukem 			    != 0)
    536  1.22  christos 				mtree_err("invalid flag `%s'", val);
    537   1.3       cgd 			break;
    538   1.3       cgd 		case F_GID:
    539   1.8     mikel 			ip->st_gid = (gid_t)strtoul(val, &ep, 10);
    540   1.3       cgd 			if (*ep)
    541  1.22  christos 				mtree_err("invalid gid `%s'", val);
    542   1.3       cgd 			break;
    543   1.3       cgd 		case F_GNAME:
    544  1.57     lukem 			if (mtree_Wflag)	/* don't parse if whacking */
    545  1.39     lukem 				break;
    546  1.38        tv 			if (gid_from_group(val, &gid) == -1)
    547  1.39     lukem 				mtree_err("unknown group `%s'", val);
    548  1.38        tv 			ip->st_gid = gid;
    549   1.1       cgd 			break;
    550  1.15     jwise 		case F_MD5:
    551  1.15     jwise 			if (val[0]=='0' && val[1]=='x')
    552  1.15     jwise 				md=&val[2];
    553  1.15     jwise 			else
    554  1.15     jwise 				md=val;
    555  1.40     lukem 			if ((ip->md5digest = strdup(md)) == NULL)
    556  1.15     jwise 				mtree_err("memory allocation error");
    557  1.15     jwise 			break;
    558   1.3       cgd 		case F_MODE:
    559   1.3       cgd 			if ((m = setmode(val)) == NULL)
    560  1.61  christos 				mtree_err("cannot set file mode `%s' (%s)",
    561  1.61  christos 				    val, strerror(errno));
    562   1.1       cgd 			ip->st_mode = getmode(m, 0);
    563  1.12     itohy 			free(m);
    564   1.1       cgd 			break;
    565   1.1       cgd 		case F_NLINK:
    566   1.8     mikel 			ip->st_nlink = (nlink_t)strtoul(val, &ep, 10);
    567   1.3       cgd 			if (*ep)
    568  1.22  christos 				mtree_err("invalid link count `%s'", val);
    569  1.32     lukem 			break;
    570  1.40     lukem 		case F_RMD160:
    571  1.40     lukem 			if (val[0]=='0' && val[1]=='x')
    572  1.40     lukem 				md=&val[2];
    573  1.40     lukem 			else
    574  1.40     lukem 				md=val;
    575  1.40     lukem 			if ((ip->rmd160digest = strdup(md)) == NULL)
    576  1.40     lukem 				mtree_err("memory allocation error");
    577  1.40     lukem 			break;
    578  1.40     lukem 		case F_SHA1:
    579  1.40     lukem 			if (val[0]=='0' && val[1]=='x')
    580  1.40     lukem 				md=&val[2];
    581  1.40     lukem 			else
    582  1.40     lukem 				md=val;
    583  1.40     lukem 			if ((ip->sha1digest = strdup(md)) == NULL)
    584  1.40     lukem 				mtree_err("memory allocation error");
    585  1.40     lukem 			break;
    586   1.1       cgd 		case F_SIZE:
    587  1.40     lukem 			ip->st_size = (off_t)strtoll(val, &ep, 10);
    588   1.3       cgd 			if (*ep)
    589  1.22  christos 				mtree_err("invalid size `%s'", val);
    590   1.1       cgd 			break;
    591   1.1       cgd 		case F_SLINK:
    592   1.3       cgd 			if ((ip->slink = strdup(val)) == NULL)
    593  1.15     jwise 				mtree_err("memory allocation error");
    594  1.58     lukem 			if (strunvis(ip->slink, val) == -1)
    595  1.58     lukem 				mtree_err("strunvis failed on `%s'", val);
    596  1.27     lukem 			break;
    597  1.27     lukem 		case F_TAGS:
    598  1.27     lukem 			len = strlen(val) + 3;	/* "," + str + ",\0" */
    599  1.27     lukem 			if ((ip->tags = malloc(len)) == NULL)
    600  1.27     lukem 				mtree_err("memory allocation error");
    601  1.27     lukem 			snprintf(ip->tags, len, ",%s,", val);
    602   1.1       cgd 			break;
    603   1.1       cgd 		case F_TIME:
    604   1.8     mikel 			ip->st_mtimespec.tv_sec =
    605   1.8     mikel 			    (time_t)strtoul(val, &ep, 10);
    606   1.5       cgd 			if (*ep != '.')
    607  1.22  christos 				mtree_err("invalid time `%s'", val);
    608   1.5       cgd 			val = ep + 1;
    609  1.40     lukem 			ip->st_mtimespec.tv_nsec = strtoul(val, &ep, 10);
    610   1.3       cgd 			if (*ep)
    611  1.22  christos 				mtree_err("invalid time `%s'", val);
    612   1.1       cgd 			break;
    613   1.1       cgd 		case F_TYPE:
    614  1.23     lukem 			ip->type = parsetype(val);
    615   1.1       cgd 			break;
    616   1.3       cgd 		case F_UID:
    617   1.8     mikel 			ip->st_uid = (uid_t)strtoul(val, &ep, 10);
    618   1.3       cgd 			if (*ep)
    619  1.22  christos 				mtree_err("invalid uid `%s'", val);
    620   1.3       cgd 			break;
    621   1.3       cgd 		case F_UNAME:
    622  1.57     lukem 			if (mtree_Wflag)	/* don't parse if whacking */
    623  1.39     lukem 				break;
    624  1.38        tv 			if (uid_from_user(val, &uid) == -1)
    625  1.39     lukem 				mtree_err("unknown user `%s'", val);
    626  1.38        tv 			ip->st_uid = uid;
    627   1.3       cgd 			break;
    628  1.60      elad 		case F_SHA256:
    629  1.60      elad 			if (val[0]=='0' && val[1]=='x')
    630  1.60      elad 				md=&val[2];
    631  1.60      elad 			else
    632  1.60      elad 				md=val;
    633  1.60      elad 			if ((ip->sha256digest = strdup(md)) == NULL)
    634  1.60      elad 				mtree_err("memory allocation error");
    635  1.60      elad 			break;
    636  1.60      elad 		case F_SHA384:
    637  1.60      elad 			if (val[0]=='0' && val[1]=='x')
    638  1.60      elad 				md=&val[2];
    639  1.60      elad 			else
    640  1.60      elad 				md=val;
    641  1.60      elad 			if ((ip->sha384digest = strdup(md)) == NULL)
    642  1.60      elad 				mtree_err("memory allocation error");
    643  1.60      elad 			break;
    644  1.60      elad 		case F_SHA512:
    645  1.60      elad 			if (val[0]=='0' && val[1]=='x')
    646  1.60      elad 				md=&val[2];
    647  1.60      elad 			else
    648  1.60      elad 				md=val;
    649  1.60      elad 			if ((ip->sha512digest = strdup(md)) == NULL)
    650  1.60      elad 				mtree_err("memory allocation error");
    651  1.60      elad 			break;
    652  1.26     lukem 		default:
    653  1.26     lukem 			mtree_err(
    654  1.26     lukem 			    "set(): unsupported key type 0x%x (INTERNAL ERROR)",
    655  1.26     lukem 			    type);
    656  1.26     lukem 			/* NOTREACHED */
    657   1.1       cgd 		}
    658   1.1       cgd 	}
    659   1.1       cgd }
    660   1.1       cgd 
    661   1.3       cgd static void
    662  1.21    simonb unset(char *t, NODE *ip)
    663   1.1       cgd {
    664  1.11     lukem 	char *p;
    665   1.1       cgd 
    666  1.33     lukem 	while ((p = strsep(&t, " \t")) != NULL) {
    667  1.33     lukem 		if (*p == '\0')
    668  1.33     lukem 			continue;
    669   1.3       cgd 		ip->flags &= ~parsekey(p, NULL);
    670  1.30     lukem 	}
    671   1.1       cgd }
    672