Home | History | Annotate | Line # | Download | only in mtree
spec.c revision 1.35
      1 /*	$NetBSD: spec.c,v 1.35 2001/10/25 03:00:14 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.35 2001/10/25 03:00:14 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 lineno;				/* Current spec line number. */
    101 
    102 static dev_t	 parsedev(char *);
    103 static void	 set(char *, NODE *);
    104 static void	 unset(char *, NODE *);
    105 
    106 NODE *
    107 spec(FILE *fp)
    108 {
    109 	NODE *centry, *last, *pathparent, *cur;
    110 	char *p, *e, *next;
    111 	NODE ginfo, *root;
    112 	char *buf, *tname;
    113 	size_t tnamelen, plen;
    114 
    115 	root = NULL;
    116 	centry = last = NULL;
    117 	tname = NULL;
    118 	tnamelen = 0;
    119 	memset(&ginfo, 0, sizeof(ginfo));
    120 	for (lineno = 0;
    121 	    (buf = fparseln(fp, NULL, &lineno, NULL,
    122 		FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT | FPARSELN_UNESCESC));
    123 	    free(buf)) {
    124 		/* Skip leading whitespace. */
    125 		for (p = buf; *p && isspace((unsigned char)*p); ++p)
    126 			continue;
    127 
    128 		/* If nothing but whitespace, continue. */
    129 		if (!*p)
    130 			continue;
    131 
    132 #ifdef DEBUG
    133 		(void)fprintf(stderr, "line %d: {%s}\n", lineno, p);
    134 #endif
    135 		/* Grab file name, "$", "set", or "unset". */
    136 		next = buf;
    137 		while ((p = strsep(&next, " \t")) != NULL && *p == '\0')
    138 			continue;
    139 		if (p == NULL)
    140 			mtree_err("missing field");
    141 
    142 		if (p[0] == '/') {
    143 			if (strcmp(p + 1, "set") == 0)
    144 				set(next, &ginfo);
    145 			else if (strcmp(p + 1, "unset") == 0)
    146 				unset(next, &ginfo);
    147 			else
    148 				mtree_err("invalid specification `%s'", p);
    149 			continue;
    150 		}
    151 
    152 		if (strcmp(p, "..") == 0) {
    153 			/* Don't go up, if haven't gone down. */
    154 			if (root == NULL)
    155 				goto noparent;
    156 			if (last->type != F_DIR || last->flags & F_DONE) {
    157 				if (last == root)
    158 					goto noparent;
    159 				last = last->parent;
    160 			}
    161 			last->flags |= F_DONE;
    162 			continue;
    163 
    164 noparent:		mtree_err("no parent node");
    165 		}
    166 
    167 		plen = strlen(p) + 1;
    168 		if (plen > tnamelen) {
    169 			tnamelen = plen;
    170 			if ((tname = realloc(tname, tnamelen)) == NULL)
    171 				mtree_err("realloc: %s", strerror(errno));
    172 		}
    173 		if (strunvis(tname, p) == -1)
    174 			mtree_err("strunvis failed on `%s'", p);
    175 		p = tname;
    176 
    177 		pathparent = NULL;
    178 		if (strchr(p, '/') != NULL) {
    179 			cur = root;
    180 			for (; (e = strchr(p, '/')) != NULL; p = e+1) {
    181 				if (p == e)
    182 					continue;	/* handle // */
    183 				*e = '\0';
    184 				if (strcmp(p, ".") != 0) {
    185 					while (cur && strcmp(cur->name, p)) {
    186 						cur = cur->next;
    187 					}
    188 				}
    189 				if (cur == NULL || cur->type != F_DIR) {
    190 					mtree_err("%s: %s", tname,
    191 					    strerror(ENOENT));
    192 				}
    193 				*e = '/';
    194 				pathparent = cur;
    195 				cur = cur->child;
    196 			}
    197 			if (*p == '\0')
    198 				mtree_err("%s: empty leaf element", tname);
    199 			for (; cur != NULL; cur = cur->next) {
    200 				if (strcmp(cur->name, p) == 0)
    201 					mtree_err("%s: %s", p,
    202 					    strerror(EEXIST));
    203 			}
    204 		}
    205 
    206 		if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
    207 			mtree_err("%s", strerror(errno));
    208 		*centry = ginfo;
    209 		centry->lineno = lineno;
    210 		strcpy(centry->name, p);
    211 #define	MAGIC	"?*["
    212 		if (strpbrk(p, MAGIC))
    213 			centry->flags |= F_MAGIC;
    214 		set(next, centry);
    215 
    216 		if (root == NULL) {
    217 			if (strcmp(centry->name, ".") || centry->type != F_DIR)
    218 				mtree_err(
    219 				    "root node must be the directory `.'");
    220 			last = root = centry;
    221 			root->parent = root;
    222 		} else if (pathparent != NULL) {
    223 			centry->parent = pathparent;
    224 			cur = pathparent->child;
    225 			if (cur == NULL)
    226 				pathparent->child = centry;
    227 			else {
    228 				while (cur->next != NULL)
    229 					cur = cur->next;
    230 				cur->next = centry;
    231 				centry->prev = cur;
    232 			}
    233 			last = centry;
    234 		} else if (last->type == F_DIR && !(last->flags & F_DONE)) {
    235 			centry->parent = last;
    236 			last = last->child = centry;
    237 		} else {
    238 			centry->parent = last->parent;
    239 			centry->prev = last;
    240 			last = last->next = centry;
    241 		}
    242 	}
    243 	return (root);
    244 }
    245 
    246 /*
    247  * dump_nodes --
    248  *	dump the NODEs from `cur', based in the directory `dir'
    249  */
    250 void
    251 dump_nodes(const char *dir, NODE *root)
    252 {
    253 	NODE	*cur;
    254 	char	path[MAXPATHLEN + 1];
    255 	const char *name;
    256 
    257 	for (cur = root; cur != NULL; cur = cur->next) {
    258 		if (cur->type != F_DIR && !matchtags(cur))
    259 			continue;
    260 
    261 		if (snprintf(path, sizeof(path), "%s%s%s",
    262 		    dir, *dir ? "/" : "", cur->name)
    263 		    >= sizeof(path))
    264 			mtree_err("Pathname too long.");
    265 
    266 #define MATCHFLAG(f)	((keys & (f)) && (cur->flags & (f)))
    267 		if (MATCHFLAG(F_TYPE))
    268 			printf("type=%s ", nodetype(cur->type));
    269 		if (MATCHFLAG(F_UID | F_UNAME)) {
    270 			if (keys & F_UNAME &&
    271 			    (name = user_from_uid(cur->st_uid, 1)) != NULL)
    272 				printf("uname=%s ", name);
    273 			else
    274 				printf("uid=%u ", cur->st_uid);
    275 		}
    276 		if (MATCHFLAG(F_GID | F_GNAME)) {
    277 			if (keys & F_GNAME &&
    278 			    (name = group_from_gid(cur->st_gid, 1)) != NULL)
    279 				printf("gname=%s ", name);
    280 			else
    281 				printf("gid=%u ", cur->st_gid);
    282 		}
    283 		if (MATCHFLAG(F_MODE))
    284 			printf("mode=%#o ", cur->st_mode);
    285 		if (MATCHFLAG(F_DEV) &&
    286 		    (cur->type == F_BLOCK || cur->type == F_CHAR))
    287 			printf("device=%#x ", cur->st_rdev);
    288 		if (MATCHFLAG(F_NLINK))
    289 			printf("nlink=%d ", cur->st_nlink);
    290 		if (MATCHFLAG(F_SLINK))
    291 			printf("link=%s ", cur->slink);
    292 		if (MATCHFLAG(F_SIZE))
    293 			printf("size=%lld ", (long long)cur->st_size);
    294 		if (MATCHFLAG(F_TIME))
    295 			printf("time=%ld.%ld ", (long)cur->st_mtimespec.tv_sec,
    296 			    cur->st_mtimespec.tv_nsec);
    297 		if (MATCHFLAG(F_CKSUM))
    298 			printf("cksum=%lu ", cur->cksum);
    299 		if (MATCHFLAG(F_MD5))
    300 			printf("md5=%s ", cur->md5sum);
    301 		if (MATCHFLAG(F_FLAGS))
    302 			printf("flags=%s ",
    303 			    flags_to_string(cur->st_flags, "none"));
    304 		if (MATCHFLAG(F_IGN))
    305 			printf("ignore ");
    306 		if (MATCHFLAG(F_OPT))
    307 			printf("optional ");
    308 		if (MATCHFLAG(F_TAGS))
    309 			printf("tags=%s ", cur->tags);
    310 		puts(path);
    311 
    312 		if (cur->child)
    313 			dump_nodes(path, cur->child);
    314 	}
    315 }
    316 
    317 static dev_t
    318 parsedev(char *arg)
    319 {
    320 #define MAX_PACK_ARGS	3
    321 	u_long	numbers[MAX_PACK_ARGS];
    322 	char	*p, *ep, *dev;
    323 	int	argc;
    324 	pack_t	*pack;
    325 	dev_t	result;
    326 
    327 	if ((dev = strchr(arg, ',')) != NULL) {
    328 		*dev++='\0';
    329 		if ((pack = pack_find(arg)) == NULL)
    330 			mtree_err("unknown format `%s'", arg);
    331 		argc = 0;
    332 		while ((p = strsep(&dev, ",")) != NULL) {
    333 			if (*p == '\0')
    334 				mtree_err("missing number");
    335 			numbers[argc++] = strtoul(p, &ep, 0);
    336 			if (*ep != '\0')
    337 				mtree_err("invalid number `%s'",
    338 				    p);
    339 			if (argc > MAX_PACK_ARGS)
    340 				mtree_err("too many arguments");
    341 		}
    342 		if (argc < 2)
    343 			mtree_err("not enough arguments");
    344 		result = (*pack)(argc, numbers);
    345 	} else {
    346 		result = (dev_t)strtoul(arg, &ep, 0);
    347 		if (*ep != '\0')
    348 			mtree_err("invalid device `%s'", arg);
    349 	}
    350 	return (result);
    351 }
    352 
    353 static void
    354 set(char *t, NODE *ip)
    355 {
    356 	int	type, value, len;
    357 	gid_t	gid;
    358 	uid_t	uid;
    359 	char	*kw, *val, *md, *ep;
    360 	void	*m;
    361 
    362 	val = NULL;
    363 	while ((kw = strsep(&t, "= \t")) != NULL) {
    364 		if (*kw == '\0')
    365 			continue;
    366 		if (strcmp(kw, "all") == 0)
    367 			mtree_err("invalid keyword `all'");
    368 		ip->flags |= type = parsekey(kw, &value);
    369 		if (value) {
    370 			while ((val = strsep(&t, " \t")) != NULL &&
    371 			    *val == '\0')
    372 				continue;
    373 			if (val == NULL)
    374 				mtree_err("missing value");
    375 		}
    376 		switch(type) {
    377 		case F_CKSUM:
    378 			ip->cksum = strtoul(val, &ep, 10);
    379 			if (*ep)
    380 				mtree_err("invalid checksum `%s'", val);
    381 			break;
    382 		case F_DEV:
    383 			ip->st_rdev = parsedev(val);
    384 			break;
    385 		case F_FLAGS:
    386 			if (strcmp("none", val) == 0)
    387 				ip->st_flags = 0;
    388 			else if (string_to_flags(&val, &ip->st_flags, NULL)
    389 			    != 0)
    390 				mtree_err("invalid flag `%s'", val);
    391 			break;
    392 		case F_GID:
    393 			ip->st_gid = (gid_t)strtoul(val, &ep, 10);
    394 			if (*ep)
    395 				mtree_err("invalid gid `%s'", val);
    396 			break;
    397 		case F_GNAME:
    398 			if (gid_from_group(val, &gid) == -1)
    399 			    mtree_err("unknown group `%s'", val);
    400 			ip->st_gid = gid;
    401 			break;
    402 		case F_IGN:
    403 			/* just set flag bit */
    404 			break;
    405 		case F_MD5:
    406 			if (val[0]=='0' && val[1]=='x')
    407 				md=&val[2];
    408 			else
    409 				md=val;
    410 			if ((ip->md5sum = strdup(md)) == NULL)
    411 				mtree_err("memory allocation error");
    412 			break;
    413 		case F_MODE:
    414 			if ((m = setmode(val)) == NULL)
    415 				mtree_err("invalid file mode `%s'", val);
    416 			ip->st_mode = getmode(m, 0);
    417 			free(m);
    418 			break;
    419 		case F_NLINK:
    420 			ip->st_nlink = (nlink_t)strtoul(val, &ep, 10);
    421 			if (*ep)
    422 				mtree_err("invalid link count `%s'", val);
    423 			break;
    424 		case F_OPT:
    425 			/* just set flag bit */
    426 			break;
    427 		case F_SIZE:
    428 			ip->st_size = (off_t)strtoq(val, &ep, 10);
    429 			if (*ep)
    430 				mtree_err("invalid size `%s'", val);
    431 			break;
    432 		case F_SLINK:
    433 			if ((ip->slink = strdup(val)) == NULL)
    434 				mtree_err("memory allocation error");
    435 			break;
    436 		case F_TAGS:
    437 			len = strlen(val) + 3;	/* "," + str + ",\0" */
    438 			if ((ip->tags = malloc(len)) == NULL)
    439 				mtree_err("memory allocation error");
    440 			snprintf(ip->tags, len, ",%s,", val);
    441 			break;
    442 		case F_TIME:
    443 			ip->st_mtimespec.tv_sec =
    444 			    (time_t)strtoul(val, &ep, 10);
    445 			if (*ep != '.')
    446 				mtree_err("invalid time `%s'", val);
    447 			val = ep + 1;
    448 			ip->st_mtimespec.tv_nsec = strtol(val, &ep, 10);
    449 			if (*ep)
    450 				mtree_err("invalid time `%s'", val);
    451 			break;
    452 		case F_TYPE:
    453 			ip->type = parsetype(val);
    454 			break;
    455 		case F_UID:
    456 			ip->st_uid = (uid_t)strtoul(val, &ep, 10);
    457 			if (*ep)
    458 				mtree_err("invalid uid `%s'", val);
    459 			break;
    460 		case F_UNAME:
    461 			if (uid_from_user(val, &uid) == -1)
    462 			    mtree_err("unknown user `%s'", val);
    463 			ip->st_uid = uid;
    464 			break;
    465 		default:
    466 			mtree_err(
    467 			    "set(): unsupported key type 0x%x (INTERNAL ERROR)",
    468 			    type);
    469 			/* NOTREACHED */
    470 		}
    471 	}
    472 }
    473 
    474 static void
    475 unset(char *t, NODE *ip)
    476 {
    477 	char *p;
    478 
    479 	while ((p = strsep(&t, " \t")) != NULL) {
    480 		if (*p == '\0')
    481 			continue;
    482 		if (strcmp(p, "all") == 0)
    483 			mtree_err("invalid keyword `all'");
    484 		ip->flags &= ~parsekey(p, NULL);
    485 	}
    486 }
    487