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