Home | History | Annotate | Line # | Download | only in mtree
spec.c revision 1.39
      1 /*	$NetBSD: spec.c,v 1.39 2001/11/03 12:51:41 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 /*-
     37  * Copyright (c) 2001 The NetBSD Foundation, Inc.
     38  * All rights reserved.
     39  *
     40  * This code is derived from software contributed to The NetBSD Foundation
     41  * by Luke Mewburn of Wasabi Systems.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *        This product includes software developed by the NetBSD
     54  *        Foundation, Inc. and its contributors.
     55  * 4. Neither the name of The NetBSD Foundation nor the names of its
     56  *    contributors may be used to endorse or promote products derived
     57  *    from this software without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     60  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     61  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     62  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     63  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     64  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     65  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     66  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     67  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     68  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     69  * POSSIBILITY OF SUCH DAMAGE.
     70  */
     71 
     72 #include <sys/cdefs.h>
     73 #ifndef lint
     74 #if 0
     75 static char sccsid[] = "@(#)spec.c	8.2 (Berkeley) 4/28/95";
     76 #else
     77 __RCSID("$NetBSD: spec.c,v 1.39 2001/11/03 12:51:41 lukem Exp $");
     78 #endif
     79 #endif /* not lint */
     80 
     81 #include <sys/param.h>
     82 #include <sys/stat.h>
     83 
     84 #include <ctype.h>
     85 #include <errno.h>
     86 #include <fts.h>
     87 #include <grp.h>
     88 #include <pwd.h>
     89 #include <stdio.h>
     90 #include <stdlib.h>
     91 #include <string.h>
     92 #include <unistd.h>
     93 #include <util.h>
     94 #include <vis.h>
     95 
     96 #include "mtree.h"
     97 #include "extern.h"
     98 #include "pack_dev.h"
     99 
    100 size_t	mtree_lineno;			/* Current spec line number */
    101 int	Wflag;				/* Don't "whack" permissions */
    102 
    103 static dev_t	 parsedev(char *);
    104 static void	 set(char *, NODE *);
    105 static void	 unset(char *, NODE *);
    106 
    107 NODE *
    108 spec(FILE *fp)
    109 {
    110 	NODE *centry, *last, *pathparent, *cur;
    111 	char *p, *e, *next;
    112 	NODE ginfo, *root;
    113 	char *buf, *tname;
    114 	size_t tnamelen, plen;
    115 
    116 	root = NULL;
    117 	centry = last = NULL;
    118 	tname = NULL;
    119 	tnamelen = 0;
    120 	memset(&ginfo, 0, sizeof(ginfo));
    121 	for (mtree_lineno = 0;
    122 	    (buf = fparseln(fp, NULL, &mtree_lineno, NULL,
    123 		FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT | FPARSELN_UNESCESC));
    124 	    free(buf)) {
    125 		/* Skip leading whitespace. */
    126 		for (p = buf; *p && isspace((unsigned char)*p); ++p)
    127 			continue;
    128 
    129 		/* If nothing but whitespace, continue. */
    130 		if (!*p)
    131 			continue;
    132 
    133 #ifdef DEBUG
    134 		(void)fprintf(stderr, "line %lu: {%s}\n",
    135 		    (u_long)mtree_lineno, p);
    136 #endif
    137 		/* Grab file name, "$", "set", or "unset". */
    138 		next = buf;
    139 		while ((p = strsep(&next, " \t")) != NULL && *p == '\0')
    140 			continue;
    141 		if (p == NULL)
    142 			mtree_err("missing field");
    143 
    144 		if (p[0] == '/') {
    145 			if (strcmp(p + 1, "set") == 0)
    146 				set(next, &ginfo);
    147 			else if (strcmp(p + 1, "unset") == 0)
    148 				unset(next, &ginfo);
    149 			else
    150 				mtree_err("invalid specification `%s'", p);
    151 			continue;
    152 		}
    153 
    154 		if (strcmp(p, "..") == 0) {
    155 			/* Don't go up, if haven't gone down. */
    156 			if (root == NULL)
    157 				goto noparent;
    158 			if (last->type != F_DIR || last->flags & F_DONE) {
    159 				if (last == root)
    160 					goto noparent;
    161 				last = last->parent;
    162 			}
    163 			last->flags |= F_DONE;
    164 			continue;
    165 
    166 noparent:		mtree_err("no parent node");
    167 		}
    168 
    169 		plen = strlen(p) + 1;
    170 		if (plen > tnamelen) {
    171 			tnamelen = plen;
    172 			if ((tname = realloc(tname, tnamelen)) == NULL)
    173 				mtree_err("realloc: %s", strerror(errno));
    174 		}
    175 		if (strunvis(tname, p) == -1)
    176 			mtree_err("strunvis failed on `%s'", p);
    177 		p = tname;
    178 
    179 		pathparent = NULL;
    180 		if (strchr(p, '/') != NULL) {
    181 			cur = root;
    182 			for (; (e = strchr(p, '/')) != NULL; p = e+1) {
    183 				if (p == e)
    184 					continue;	/* handle // */
    185 				*e = '\0';
    186 				if (strcmp(p, ".") != 0) {
    187 					while (cur && strcmp(cur->name, p)) {
    188 						cur = cur->next;
    189 					}
    190 				}
    191 				if (cur == NULL || cur->type != F_DIR) {
    192 					mtree_err("%s: %s", tname,
    193 					    strerror(ENOENT));
    194 				}
    195 				*e = '/';
    196 				pathparent = cur;
    197 				cur = cur->child;
    198 			}
    199 			if (*p == '\0')
    200 				mtree_err("%s: empty leaf element", tname);
    201 			for (; cur != NULL; cur = cur->next) {
    202 				if (strcmp(cur->name, p) == 0)
    203 					mtree_err("%s: %s", p,
    204 					    strerror(EEXIST));
    205 			}
    206 		}
    207 
    208 		if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
    209 			mtree_err("%s", strerror(errno));
    210 		*centry = ginfo;
    211 		centry->lineno = mtree_lineno;
    212 		strcpy(centry->name, p);
    213 #define	MAGIC	"?*["
    214 		if (strpbrk(p, MAGIC))
    215 			centry->flags |= F_MAGIC;
    216 		set(next, centry);
    217 
    218 		if (root == NULL) {
    219 			if (strcmp(centry->name, ".") || centry->type != F_DIR)
    220 				mtree_err(
    221 				    "root node must be the directory `.'");
    222 			last = root = centry;
    223 			root->parent = root;
    224 		} else if (pathparent != NULL) {
    225 			centry->parent = pathparent;
    226 			cur = pathparent->child;
    227 			if (cur == NULL)
    228 				pathparent->child = centry;
    229 			else {
    230 				while (cur->next != NULL)
    231 					cur = cur->next;
    232 				cur->next = centry;
    233 				centry->prev = cur;
    234 			}
    235 			last = centry;
    236 		} else if (last->type == F_DIR && !(last->flags & F_DONE)) {
    237 			centry->parent = last;
    238 			last = last->child = centry;
    239 		} else {
    240 			centry->parent = last->parent;
    241 			centry->prev = last;
    242 			last = last->next = centry;
    243 		}
    244 	}
    245 	return (root);
    246 }
    247 
    248 /*
    249  * dump_nodes --
    250  *	dump the NODEs from `cur', based in the directory `dir'
    251  */
    252 void
    253 dump_nodes(const char *dir, NODE *root)
    254 {
    255 	NODE	*cur;
    256 	char	path[MAXPATHLEN + 1];
    257 	const char *name;
    258 
    259 	for (cur = root; cur != NULL; cur = cur->next) {
    260 		if (cur->type != F_DIR && !matchtags(cur))
    261 			continue;
    262 
    263 		if (snprintf(path, sizeof(path), "%s%s%s",
    264 		    dir, *dir ? "/" : "", cur->name)
    265 		    >= sizeof(path))
    266 			mtree_err("Pathname too long.");
    267 
    268 #define MATCHFLAG(f)	((keys & (f)) && (cur->flags & (f)))
    269 		if (MATCHFLAG(F_TYPE))
    270 			printf("type=%s ", nodetype(cur->type));
    271 		if (MATCHFLAG(F_UID | F_UNAME)) {
    272 			if (keys & F_UNAME &&
    273 			    (name = user_from_uid(cur->st_uid, 1)) != NULL)
    274 				printf("uname=%s ", name);
    275 			else
    276 				printf("uid=%u ", cur->st_uid);
    277 		}
    278 		if (MATCHFLAG(F_GID | F_GNAME)) {
    279 			if (keys & F_GNAME &&
    280 			    (name = group_from_gid(cur->st_gid, 1)) != NULL)
    281 				printf("gname=%s ", name);
    282 			else
    283 				printf("gid=%u ", cur->st_gid);
    284 		}
    285 		if (MATCHFLAG(F_MODE))
    286 			printf("mode=%#o ", cur->st_mode);
    287 		if (MATCHFLAG(F_DEV) &&
    288 		    (cur->type == F_BLOCK || cur->type == F_CHAR))
    289 			printf("device=%#x ", cur->st_rdev);
    290 		if (MATCHFLAG(F_NLINK))
    291 			printf("nlink=%d ", cur->st_nlink);
    292 		if (MATCHFLAG(F_SLINK))
    293 			printf("link=%s ", cur->slink);
    294 		if (MATCHFLAG(F_SIZE))
    295 			printf("size=%lld ", (long long)cur->st_size);
    296 		if (MATCHFLAG(F_TIME))
    297 			printf("time=%ld.%ld ", (long)cur->st_mtimespec.tv_sec,
    298 			    cur->st_mtimespec.tv_nsec);
    299 		if (MATCHFLAG(F_CKSUM))
    300 			printf("cksum=%lu ", cur->cksum);
    301 		if (MATCHFLAG(F_MD5))
    302 			printf("md5=%s ", cur->md5sum);
    303 		if (MATCHFLAG(F_FLAGS))
    304 			printf("flags=%s ",
    305 			    flags_to_string(cur->st_flags, "none"));
    306 		if (MATCHFLAG(F_IGN))
    307 			printf("ignore ");
    308 		if (MATCHFLAG(F_OPT))
    309 			printf("optional ");
    310 		if (MATCHFLAG(F_TAGS))
    311 			printf("tags=%s ", cur->tags);
    312 		puts(path);
    313 
    314 		if (cur->child)
    315 			dump_nodes(path, cur->child);
    316 	}
    317 }
    318 
    319 static dev_t
    320 parsedev(char *arg)
    321 {
    322 #define MAX_PACK_ARGS	3
    323 	u_long	numbers[MAX_PACK_ARGS];
    324 	char	*p, *ep, *dev;
    325 	int	argc;
    326 	pack_t	*pack;
    327 	dev_t	result;
    328 
    329 	if ((dev = strchr(arg, ',')) != NULL) {
    330 		*dev++='\0';
    331 		if ((pack = pack_find(arg)) == NULL)
    332 			mtree_err("unknown format `%s'", arg);
    333 		argc = 0;
    334 		while ((p = strsep(&dev, ",")) != NULL) {
    335 			if (*p == '\0')
    336 				mtree_err("missing number");
    337 			numbers[argc++] = strtoul(p, &ep, 0);
    338 			if (*ep != '\0')
    339 				mtree_err("invalid number `%s'",
    340 				    p);
    341 			if (argc > MAX_PACK_ARGS)
    342 				mtree_err("too many arguments");
    343 		}
    344 		if (argc < 2)
    345 			mtree_err("not enough arguments");
    346 		result = (*pack)(argc, numbers);
    347 	} else {
    348 		result = (dev_t)strtoul(arg, &ep, 0);
    349 		if (*ep != '\0')
    350 			mtree_err("invalid device `%s'", arg);
    351 	}
    352 	return (result);
    353 }
    354 
    355 static void
    356 set(char *t, NODE *ip)
    357 {
    358 	int	type, value, len;
    359 	gid_t	gid;
    360 	uid_t	uid;
    361 	char	*kw, *val, *md, *ep;
    362 	void	*m;
    363 
    364 	val = NULL;
    365 	while ((kw = strsep(&t, "= \t")) != NULL) {
    366 		if (*kw == '\0')
    367 			continue;
    368 		if (strcmp(kw, "all") == 0)
    369 			mtree_err("invalid keyword `all'");
    370 		ip->flags |= type = parsekey(kw, &value);
    371 		if (value) {
    372 			while ((val = strsep(&t, " \t")) != NULL &&
    373 			    *val == '\0')
    374 				continue;
    375 			if (val == NULL)
    376 				mtree_err("missing value");
    377 		}
    378 		switch(type) {
    379 		case F_CKSUM:
    380 			ip->cksum = strtoul(val, &ep, 10);
    381 			if (*ep)
    382 				mtree_err("invalid checksum `%s'", val);
    383 			break;
    384 		case F_DEV:
    385 			ip->st_rdev = parsedev(val);
    386 			break;
    387 		case F_FLAGS:
    388 			if (strcmp("none", val) == 0)
    389 				ip->st_flags = 0;
    390 			else if (string_to_flags(&val, &ip->st_flags, NULL)
    391 			    != 0)
    392 				mtree_err("invalid flag `%s'", val);
    393 			break;
    394 		case F_GID:
    395 			ip->st_gid = (gid_t)strtoul(val, &ep, 10);
    396 			if (*ep)
    397 				mtree_err("invalid gid `%s'", val);
    398 			break;
    399 		case F_GNAME:
    400 			if (Wflag)	/* don't parse if whacking */
    401 				break;
    402 			if (gid_from_group(val, &gid) == -1)
    403 				mtree_err("unknown group `%s'", val);
    404 			ip->st_gid = gid;
    405 			break;
    406 		case F_IGN:
    407 			/* just set flag bit */
    408 			break;
    409 		case F_MD5:
    410 			if (val[0]=='0' && val[1]=='x')
    411 				md=&val[2];
    412 			else
    413 				md=val;
    414 			if ((ip->md5sum = strdup(md)) == NULL)
    415 				mtree_err("memory allocation error");
    416 			break;
    417 		case F_MODE:
    418 			if ((m = setmode(val)) == NULL)
    419 				mtree_err("invalid file mode `%s'", val);
    420 			ip->st_mode = getmode(m, 0);
    421 			free(m);
    422 			break;
    423 		case F_NLINK:
    424 			ip->st_nlink = (nlink_t)strtoul(val, &ep, 10);
    425 			if (*ep)
    426 				mtree_err("invalid link count `%s'", val);
    427 			break;
    428 		case F_OPT:
    429 			/* just set flag bit */
    430 			break;
    431 		case F_SIZE:
    432 			ip->st_size = (off_t)strtoq(val, &ep, 10);
    433 			if (*ep)
    434 				mtree_err("invalid size `%s'", val);
    435 			break;
    436 		case F_SLINK:
    437 			if ((ip->slink = strdup(val)) == NULL)
    438 				mtree_err("memory allocation error");
    439 			break;
    440 		case F_TAGS:
    441 			len = strlen(val) + 3;	/* "," + str + ",\0" */
    442 			if ((ip->tags = malloc(len)) == NULL)
    443 				mtree_err("memory allocation error");
    444 			snprintf(ip->tags, len, ",%s,", val);
    445 			break;
    446 		case F_TIME:
    447 			ip->st_mtimespec.tv_sec =
    448 			    (time_t)strtoul(val, &ep, 10);
    449 			if (*ep != '.')
    450 				mtree_err("invalid time `%s'", val);
    451 			val = ep + 1;
    452 			ip->st_mtimespec.tv_nsec = strtol(val, &ep, 10);
    453 			if (*ep)
    454 				mtree_err("invalid time `%s'", val);
    455 			break;
    456 		case F_TYPE:
    457 			ip->type = parsetype(val);
    458 			break;
    459 		case F_UID:
    460 			ip->st_uid = (uid_t)strtoul(val, &ep, 10);
    461 			if (*ep)
    462 				mtree_err("invalid uid `%s'", val);
    463 			break;
    464 		case F_UNAME:
    465 			if (Wflag)	/* don't parse if whacking */
    466 				break;
    467 			if (uid_from_user(val, &uid) == -1)
    468 				mtree_err("unknown user `%s'", val);
    469 			ip->st_uid = uid;
    470 			break;
    471 		default:
    472 			mtree_err(
    473 			    "set(): unsupported key type 0x%x (INTERNAL ERROR)",
    474 			    type);
    475 			/* NOTREACHED */
    476 		}
    477 	}
    478 }
    479 
    480 static void
    481 unset(char *t, NODE *ip)
    482 {
    483 	char *p;
    484 
    485 	while ((p = strsep(&t, " \t")) != NULL) {
    486 		if (*p == '\0')
    487 			continue;
    488 		if (strcmp(p, "all") == 0)
    489 			mtree_err("invalid keyword `all'");
    490 		ip->flags &= ~parsekey(p, NULL);
    491 	}
    492 }
    493