Home | History | Annotate | Line # | Download | only in mtree
spec.c revision 1.30
      1  1.30     lukem /*	$NetBSD: spec.c,v 1.30 2001/10/05 13:14:56 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.11     lukem #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38   1.6       cgd #if 0
     39  1.10       mrg static char sccsid[] = "@(#)spec.c	8.2 (Berkeley) 4/28/95";
     40   1.6       cgd #else
     41  1.30     lukem __RCSID("$NetBSD: spec.c,v 1.30 2001/10/05 13:14:56 lukem Exp $");
     42   1.6       cgd #endif
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45  1.25     lukem #include <sys/param.h>
     46   1.3       cgd #include <sys/stat.h>
     47  1.21    simonb 
     48  1.11     lukem #include <ctype.h>
     49  1.11     lukem #include <errno.h>
     50   1.3       cgd #include <fts.h>
     51  1.11     lukem #include <grp.h>
     52   1.1       cgd #include <pwd.h>
     53  1.11     lukem #include <stdio.h>
     54  1.11     lukem #include <string.h>
     55   1.3       cgd #include <unistd.h>
     56  1.24     lukem #include <util.h>
     57  1.18  wennmach #include <vis.h>
     58  1.11     lukem 
     59   1.1       cgd #include "mtree.h"
     60   1.3       cgd #include "extern.h"
     61   1.1       cgd 
     62  1.24     lukem size_t lineno;				/* Current spec line number. */
     63   1.1       cgd 
     64  1.21    simonb static void	 set(char *, NODE *);
     65  1.21    simonb static void	 unset(char *, NODE *);
     66   1.1       cgd 
     67   1.3       cgd NODE *
     68  1.21    simonb spec(void)
     69   1.1       cgd {
     70  1.11     lukem 	NODE *centry, *last;
     71  1.11     lukem 	char *p;
     72   1.3       cgd 	NODE ginfo, *root;
     73  1.24     lukem 	char *buf;
     74   1.1       cgd 
     75   1.3       cgd 	root = NULL;
     76  1.11     lukem 	centry = last = NULL;
     77  1.11     lukem 	memset(&ginfo, 0, sizeof(ginfo));
     78  1.24     lukem 	for (lineno = 0;
     79  1.24     lukem 	    (buf = fparseln(stdin, NULL, &lineno, NULL,
     80  1.24     lukem 		FPARSELN_UNESCCOMM | FPARSELN_UNESCCONT | FPARSELN_UNESCESC));
     81  1.24     lukem 	    free(buf)) {
     82  1.24     lukem 		/* Skip leading whitespace. */
     83  1.24     lukem 		for (p = buf; *p && isspace((unsigned char)*p); ++p)
     84   1.3       cgd 			continue;
     85   1.3       cgd 
     86  1.24     lukem 		/* If nothing but whitespace, continue. */
     87  1.24     lukem 		if (!*p)
     88   1.1       cgd 			continue;
     89   1.1       cgd 
     90   1.3       cgd #ifdef DEBUG
     91   1.3       cgd 		(void)fprintf(stderr, "line %d: {%s}\n", lineno, p);
     92   1.3       cgd #endif
     93  1.24     lukem 
     94   1.3       cgd 		/* Grab file name, "$", "set", or "unset". */
     95   1.3       cgd 		if ((p = strtok(p, "\n\t ")) == NULL)
     96  1.13  wsanchez 			mtree_err("missing field");
     97   1.1       cgd 
     98  1.23     lukem 		if (p[0] == '/') {
     99  1.23     lukem 			if (strcmp(p + 1, "set") == 0)
    100   1.3       cgd 				set(NULL, &ginfo);
    101  1.23     lukem 			else if (strcmp(p + 1, "unset") == 0)
    102   1.3       cgd 				unset(NULL, &ginfo);
    103  1.23     lukem 			else
    104  1.23     lukem 				mtree_err("invalid specification `%s'", p);
    105  1.23     lukem 			continue;
    106  1.23     lukem 		}
    107   1.1       cgd 
    108  1.11     lukem 		if (strchr(p, '/'))
    109  1.13  wsanchez 			mtree_err("slash character in file name");
    110   1.1       cgd 
    111   1.1       cgd 		if (!strcmp(p, "..")) {
    112   1.3       cgd 			/* Don't go up, if haven't gone down. */
    113   1.1       cgd 			if (!root)
    114   1.3       cgd 				goto noparent;
    115   1.1       cgd 			if (last->type != F_DIR || last->flags & F_DONE) {
    116   1.1       cgd 				if (last == root)
    117   1.3       cgd 					goto noparent;
    118   1.1       cgd 				last = last->parent;
    119   1.1       cgd 			}
    120   1.1       cgd 			last->flags |= F_DONE;
    121   1.1       cgd 			continue;
    122   1.3       cgd 
    123  1.13  wsanchez noparent:		mtree_err("no parent node");
    124   1.1       cgd 		}
    125   1.1       cgd 
    126   1.3       cgd 		if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
    127  1.13  wsanchez 			mtree_err("%s", strerror(errno));
    128   1.1       cgd 		*centry = ginfo;
    129  1.18  wennmach                 if (strunvis(centry->name, p) == -1)
    130  1.22  christos 			mtree_err("strunvis failed on `%s'", p);
    131   1.1       cgd #define	MAGIC	"?*["
    132   1.1       cgd 		if (strpbrk(p, MAGIC))
    133   1.1       cgd 			centry->flags |= F_MAGIC;
    134   1.3       cgd 		set(NULL, centry);
    135   1.1       cgd 
    136   1.1       cgd 		if (!root) {
    137   1.1       cgd 			last = root = centry;
    138   1.1       cgd 			root->parent = root;
    139   1.1       cgd 		} else if (last->type == F_DIR && !(last->flags & F_DONE)) {
    140   1.1       cgd 			centry->parent = last;
    141   1.1       cgd 			last = last->child = centry;
    142   1.1       cgd 		} else {
    143   1.1       cgd 			centry->parent = last->parent;
    144   1.1       cgd 			centry->prev = last;
    145   1.1       cgd 			last = last->next = centry;
    146   1.1       cgd 		}
    147   1.1       cgd 	}
    148   1.3       cgd 	return (root);
    149   1.1       cgd }
    150   1.1       cgd 
    151  1.25     lukem /*
    152  1.25     lukem  * dump_nodes --
    153  1.25     lukem  *	dump the NODEs from `cur', based in the directory `dir'
    154  1.25     lukem  */
    155  1.25     lukem void
    156  1.25     lukem dump_nodes(const char *dir, NODE *root)
    157  1.25     lukem {
    158  1.25     lukem 	NODE	*cur;
    159  1.25     lukem 	char	path[MAXPATHLEN + 1];
    160  1.30     lukem 	const char *name;
    161  1.25     lukem 
    162  1.25     lukem 	for (cur = root; cur != NULL; cur = cur->next) {
    163  1.28     lukem 		if (cur->type != F_DIR && !matchtags(cur))
    164  1.28     lukem 			continue;
    165  1.26     lukem 
    166  1.25     lukem 		if (snprintf(path, sizeof(path), "%s%s%s",
    167  1.25     lukem 		    dir, *dir ? "/" : "", cur->name)
    168  1.25     lukem 		    >= sizeof(path))
    169  1.25     lukem 			mtree_err("Pathname too long.");
    170  1.30     lukem 
    171  1.30     lukem #define MATCHFLAG(f)	((keys & (f)) && (cur->flags & (f)))
    172  1.30     lukem 		if (MATCHFLAG(F_TYPE))
    173  1.25     lukem 			printf("type=%s ", nodetype(cur->type));
    174  1.30     lukem 		if (MATCHFLAG(F_UID | F_UNAME)) {
    175  1.30     lukem 			if (keys & F_UNAME &&
    176  1.30     lukem 			    (name = user_from_uid(cur->st_uid, 1)) != NULL)
    177  1.30     lukem 				printf("uname=%s ", name);
    178  1.30     lukem 			else
    179  1.30     lukem 				printf("uid=%u ", cur->st_uid);
    180  1.30     lukem 		}
    181  1.30     lukem 		if (MATCHFLAG(F_GID | F_GNAME)) {
    182  1.30     lukem 			if (keys & F_GNAME &&
    183  1.30     lukem 			    (name = group_from_gid(cur->st_gid, 1)) != NULL)
    184  1.30     lukem 				printf("gname=%s ", name);
    185  1.30     lukem 			else
    186  1.30     lukem 				printf("gid=%u ", cur->st_gid);
    187  1.30     lukem 		}
    188  1.30     lukem 		if (MATCHFLAG(F_MODE))
    189  1.25     lukem 			printf("mode=%#o ", cur->st_mode);
    190  1.30     lukem 		if (MATCHFLAG(F_NLINK) /* && cur->st_nlink > 1 */)
    191  1.25     lukem 			printf("nlink=%d ", cur->st_nlink);
    192  1.30     lukem 		if (MATCHFLAG(F_SLINK))
    193  1.25     lukem 			printf("link=%s ", cur->slink);
    194  1.30     lukem 		if (MATCHFLAG(F_SIZE))
    195  1.25     lukem 			printf("size=%lld ", (long long)cur->st_size);
    196  1.30     lukem 		if (MATCHFLAG(F_TIME))
    197  1.25     lukem 			printf("time=%ld.%ld ", (long)cur->st_mtimespec.tv_sec,
    198  1.25     lukem 			    cur->st_mtimespec.tv_nsec);
    199  1.30     lukem 		if (MATCHFLAG(F_CKSUM))
    200  1.25     lukem 			printf("cksum=%lu ", cur->cksum);
    201  1.30     lukem 		if (MATCHFLAG(F_MD5))
    202  1.25     lukem 			printf("md5=%s ", cur->md5sum);
    203  1.30     lukem 		if (MATCHFLAG(F_FLAGS))
    204  1.25     lukem 			printf("flags=%s ",
    205  1.25     lukem 			    flags_to_string(cur->st_flags, "none"));
    206  1.30     lukem 		if (MATCHFLAG(F_IGN))
    207  1.30     lukem 			printf("ignore ");
    208  1.30     lukem 		if (MATCHFLAG(F_OPT))
    209  1.30     lukem 			printf("optional ");
    210  1.30     lukem 		if (MATCHFLAG(F_TAGS))
    211  1.26     lukem 			printf("tags=%s ", cur->tags);
    212  1.25     lukem 		puts(path);
    213  1.25     lukem 
    214  1.25     lukem 		if (cur->child)
    215  1.25     lukem 			dump_nodes(path, cur->child);
    216  1.25     lukem 	}
    217  1.25     lukem }
    218  1.25     lukem 
    219   1.3       cgd static void
    220  1.21    simonb set(char *t, NODE *ip)
    221   1.1       cgd {
    222  1.11     lukem 	int type;
    223  1.25     lukem 	gid_t gid;
    224  1.25     lukem 	uid_t uid;
    225  1.15     jwise 	char *kw, *val, *md;
    226  1.19     enami 	void *m;
    227  1.26     lukem 	int value, len;
    228   1.3       cgd 	char *ep;
    229   1.3       cgd 
    230  1.11     lukem 	val = NULL;
    231   1.9     mikel 	for (; (kw = strtok(t, "= \t\n")) != NULL; t = NULL) {
    232  1.30     lukem 		if (strcmp(kw, "all") == 0)
    233  1.30     lukem 			mtree_err("invalid keyword `all'");
    234   1.3       cgd 		ip->flags |= type = parsekey(kw, &value);
    235   1.3       cgd 		if (value && (val = strtok(NULL, " \t\n")) == NULL)
    236  1.13  wsanchez 			mtree_err("missing value");
    237   1.1       cgd 		switch(type) {
    238   1.1       cgd 		case F_CKSUM:
    239   1.3       cgd 			ip->cksum = strtoul(val, &ep, 10);
    240   1.3       cgd 			if (*ep)
    241  1.22  christos 				mtree_err("invalid checksum `%s'", val);
    242  1.14       mrg 			break;
    243  1.14       mrg 		case F_FLAGS:
    244  1.14       mrg 			if (strcmp("none", val) == 0)
    245  1.14       mrg 				ip->st_flags = 0;
    246  1.14       mrg 			else if (string_to_flags(&val, &ip->st_flags, NULL) != 0)
    247  1.22  christos 				mtree_err("invalid flag `%s'", val);
    248   1.3       cgd 			break;
    249   1.3       cgd 		case F_GID:
    250   1.8     mikel 			ip->st_gid = (gid_t)strtoul(val, &ep, 10);
    251   1.3       cgd 			if (*ep)
    252  1.22  christos 				mtree_err("invalid gid `%s'", val);
    253   1.3       cgd 			break;
    254   1.3       cgd 		case F_GNAME:
    255  1.25     lukem 			if (gid_from_group(val, &gid) == -1)
    256  1.22  christos 			    mtree_err("unknown group `%s'", val);
    257  1.25     lukem 			ip->st_gid = gid;
    258   1.1       cgd 			break;
    259   1.1       cgd 		case F_IGN:
    260  1.27     lukem 		case F_OPT:
    261   1.1       cgd 			/* just set flag bit */
    262   1.1       cgd 			break;
    263  1.15     jwise 		case F_MD5:
    264  1.15     jwise 			if (val[0]=='0' && val[1]=='x')
    265  1.15     jwise 				md=&val[2];
    266  1.15     jwise 			else
    267  1.15     jwise 				md=val;
    268  1.15     jwise 			if ((ip->md5sum = strdup(md)) == NULL)
    269  1.15     jwise 				mtree_err("memory allocation error");
    270  1.15     jwise 			break;
    271   1.3       cgd 		case F_MODE:
    272   1.3       cgd 			if ((m = setmode(val)) == NULL)
    273  1.22  christos 				mtree_err("invalid file mode `%s'", val);
    274   1.1       cgd 			ip->st_mode = getmode(m, 0);
    275  1.12     itohy 			free(m);
    276   1.1       cgd 			break;
    277   1.1       cgd 		case F_NLINK:
    278   1.8     mikel 			ip->st_nlink = (nlink_t)strtoul(val, &ep, 10);
    279   1.3       cgd 			if (*ep)
    280  1.22  christos 				mtree_err("invalid link count `%s'", val);
    281   1.1       cgd 			break;
    282   1.1       cgd 		case F_SIZE:
    283   1.8     mikel 			ip->st_size = (off_t)strtoq(val, &ep, 10);
    284   1.3       cgd 			if (*ep)
    285  1.22  christos 				mtree_err("invalid size `%s'", val);
    286   1.1       cgd 			break;
    287   1.1       cgd 		case F_SLINK:
    288   1.3       cgd 			if ((ip->slink = strdup(val)) == NULL)
    289  1.15     jwise 				mtree_err("memory allocation error");
    290  1.27     lukem 			break;
    291  1.27     lukem 		case F_TAGS:
    292  1.27     lukem 			len = strlen(val) + 3;	/* "," + str + ",\0" */
    293  1.27     lukem 			if ((ip->tags = malloc(len)) == NULL)
    294  1.27     lukem 				mtree_err("memory allocation error");
    295  1.27     lukem 			snprintf(ip->tags, len, ",%s,", val);
    296   1.1       cgd 			break;
    297   1.1       cgd 		case F_TIME:
    298   1.8     mikel 			ip->st_mtimespec.tv_sec =
    299   1.8     mikel 			    (time_t)strtoul(val, &ep, 10);
    300   1.5       cgd 			if (*ep != '.')
    301  1.22  christos 				mtree_err("invalid time `%s'", val);
    302   1.5       cgd 			val = ep + 1;
    303   1.8     mikel 			ip->st_mtimespec.tv_nsec = strtol(val, &ep, 10);
    304   1.3       cgd 			if (*ep)
    305  1.22  christos 				mtree_err("invalid time `%s'", val);
    306   1.1       cgd 			break;
    307   1.1       cgd 		case F_TYPE:
    308  1.23     lukem 			ip->type = parsetype(val);
    309   1.1       cgd 			break;
    310   1.3       cgd 		case F_UID:
    311   1.8     mikel 			ip->st_uid = (uid_t)strtoul(val, &ep, 10);
    312   1.3       cgd 			if (*ep)
    313  1.22  christos 				mtree_err("invalid uid `%s'", val);
    314   1.3       cgd 			break;
    315   1.3       cgd 		case F_UNAME:
    316  1.25     lukem 			if (uid_from_user(val, &uid) == -1)
    317  1.22  christos 			    mtree_err("unknown user `%s'", val);
    318  1.25     lukem 			ip->st_uid = uid;
    319   1.3       cgd 			break;
    320  1.26     lukem 		default:
    321  1.26     lukem 			mtree_err(
    322  1.26     lukem 			    "set(): unsupported key type 0x%x (INTERNAL ERROR)",
    323  1.26     lukem 			    type);
    324  1.26     lukem 			/* NOTREACHED */
    325   1.1       cgd 		}
    326   1.1       cgd 	}
    327   1.1       cgd }
    328   1.1       cgd 
    329   1.3       cgd static void
    330  1.21    simonb unset(char *t, NODE *ip)
    331   1.1       cgd {
    332  1.11     lukem 	char *p;
    333   1.1       cgd 
    334  1.30     lukem 	while ((p = strtok(t, "\n\t ")) != NULL) {
    335  1.30     lukem 		if (strcmp(p, "all") == 0)
    336  1.30     lukem 			mtree_err("invalid keyword `all'");
    337   1.3       cgd 		ip->flags &= ~parsekey(p, NULL);
    338  1.30     lukem 	}
    339   1.1       cgd }
    340