Home | History | Annotate | Line # | Download | only in mtree
spec.c revision 1.11
      1 /*	$NetBSD: spec.c,v 1.11 1997/10/17 11:46:55 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 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)spec.c	8.2 (Berkeley) 4/28/95";
     40 #else
     41 __RCSID("$NetBSD: spec.c,v 1.11 1997/10/17 11:46:55 lukem Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/types.h>
     46 #include <sys/stat.h>
     47 #include <ctype.h>
     48 #include <errno.h>
     49 #include <fts.h>
     50 #include <grp.h>
     51 #include <pwd.h>
     52 #include <stdio.h>
     53 #include <string.h>
     54 #include <unistd.h>
     55 
     56 #include "mtree.h"
     57 #include "extern.h"
     58 
     59 int lineno;				/* Current spec line number. */
     60 
     61 static void	 set __P((char *, NODE *));
     62 static void	 unset __P((char *, NODE *));
     63 
     64 NODE *
     65 spec()
     66 {
     67 	NODE *centry, *last;
     68 	char *p;
     69 	NODE ginfo, *root;
     70 	int c_cur, c_next;
     71 	char buf[2048];
     72 
     73 	root = NULL;
     74 	centry = last = NULL;
     75 	memset(&ginfo, 0, sizeof(ginfo));
     76 	c_cur = c_next = 0;
     77 	for (lineno = 1; fgets(buf, sizeof(buf), stdin);
     78 	    ++lineno, c_cur = c_next, c_next = 0) {
     79 		/* Skip empty lines. */
     80 		if (buf[0] == '\n')
     81 			continue;
     82 
     83 		/* Find end of line. */
     84 		if ((p = strchr(buf, '\n')) == NULL)
     85 			err("line %d too long", lineno);
     86 
     87 		/* See if next line is continuation line. */
     88 		if (p[-1] == '\\') {
     89 			--p;
     90 			c_next = 1;
     91 		}
     92 
     93 		/* Null-terminate the line. */
     94 		*p = '\0';
     95 
     96 		/* Skip leading whitespace. */
     97 		for (p = buf; *p && isspace(*p); ++p);
     98 
     99 		/* If nothing but whitespace or comment char, continue. */
    100 		if (!*p || *p == '#')
    101 			continue;
    102 
    103 #ifdef DEBUG
    104 		(void)fprintf(stderr, "line %d: {%s}\n", lineno, p);
    105 #endif
    106 		if (c_cur) {
    107 			set(p, centry);
    108 			continue;
    109 		}
    110 
    111 		/* Grab file name, "$", "set", or "unset". */
    112 		if ((p = strtok(p, "\n\t ")) == NULL)
    113 			err("missing field");
    114 
    115 		if (p[0] == '/')
    116 			switch(p[1]) {
    117 			case 's':
    118 				if (strcmp(p + 1, "set"))
    119 					break;
    120 				set(NULL, &ginfo);
    121 				continue;
    122 			case 'u':
    123 				if (strcmp(p + 1, "unset"))
    124 					break;
    125 				unset(NULL, &ginfo);
    126 				continue;
    127 			}
    128 
    129 		if (strchr(p, '/'))
    130 			err("slash character in file name");
    131 
    132 		if (!strcmp(p, "..")) {
    133 			/* Don't go up, if haven't gone down. */
    134 			if (!root)
    135 				goto noparent;
    136 			if (last->type != F_DIR || last->flags & F_DONE) {
    137 				if (last == root)
    138 					goto noparent;
    139 				last = last->parent;
    140 			}
    141 			last->flags |= F_DONE;
    142 			continue;
    143 
    144 noparent:		err("no parent node");
    145 		}
    146 
    147 		if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
    148 			err("%s", strerror(errno));
    149 		*centry = ginfo;
    150 		(void)strcpy(centry->name, p);
    151 #define	MAGIC	"?*["
    152 		if (strpbrk(p, MAGIC))
    153 			centry->flags |= F_MAGIC;
    154 		set(NULL, centry);
    155 
    156 		if (!root) {
    157 			last = root = centry;
    158 			root->parent = root;
    159 		} else if (last->type == F_DIR && !(last->flags & F_DONE)) {
    160 			centry->parent = last;
    161 			last = last->child = centry;
    162 		} else {
    163 			centry->parent = last->parent;
    164 			centry->prev = last;
    165 			last = last->next = centry;
    166 		}
    167 	}
    168 	return (root);
    169 }
    170 
    171 static void
    172 set(t, ip)
    173 	char *t;
    174 	NODE *ip;
    175 {
    176 	int type;
    177 	char *kw, *val;
    178 	struct group *gr;
    179 	struct passwd *pw;
    180 	mode_t *m;
    181 	int value;
    182 	char *ep;
    183 
    184 	val = NULL;
    185 	for (; (kw = strtok(t, "= \t\n")) != NULL; t = NULL) {
    186 		ip->flags |= type = parsekey(kw, &value);
    187 		if (value && (val = strtok(NULL, " \t\n")) == NULL)
    188 			err("missing value");
    189 		switch(type) {
    190 		case F_CKSUM:
    191 			ip->cksum = strtoul(val, &ep, 10);
    192 			if (*ep)
    193 				err("invalid checksum %s", val);
    194 			break;
    195 		case F_GID:
    196 			ip->st_gid = (gid_t)strtoul(val, &ep, 10);
    197 			if (*ep)
    198 				err("invalid gid %s", val);
    199 			break;
    200 		case F_GNAME:
    201 			if ((gr = getgrnam(val)) == NULL)
    202 			    err("unknown group %s", val);
    203 			ip->st_gid = gr->gr_gid;
    204 			break;
    205 		case F_IGN:
    206 			/* just set flag bit */
    207 			break;
    208 		case F_MODE:
    209 			if ((m = setmode(val)) == NULL)
    210 				err("invalid file mode %s", val);
    211 			ip->st_mode = getmode(m, 0);
    212 			break;
    213 		case F_NLINK:
    214 			ip->st_nlink = (nlink_t)strtoul(val, &ep, 10);
    215 			if (*ep)
    216 				err("invalid link count %s", val);
    217 			break;
    218 		case F_SIZE:
    219 			ip->st_size = (off_t)strtoq(val, &ep, 10);
    220 			if (*ep)
    221 				err("invalid size %s", val);
    222 			break;
    223 		case F_SLINK:
    224 			if ((ip->slink = strdup(val)) == NULL)
    225 				err("%s", strerror(errno));
    226 			break;
    227 		case F_TIME:
    228 			ip->st_mtimespec.tv_sec =
    229 			    (time_t)strtoul(val, &ep, 10);
    230 			if (*ep != '.')
    231 				err("invalid time %s", val);
    232 			val = ep + 1;
    233 			ip->st_mtimespec.tv_nsec = strtol(val, &ep, 10);
    234 			if (*ep)
    235 				err("invalid time %s", val);
    236 			break;
    237 		case F_TYPE:
    238 			switch(*val) {
    239 			case 'b':
    240 				if (!strcmp(val, "block"))
    241 					ip->type = F_BLOCK;
    242 				break;
    243 			case 'c':
    244 				if (!strcmp(val, "char"))
    245 					ip->type = F_CHAR;
    246 				break;
    247 			case 'd':
    248 				if (!strcmp(val, "dir"))
    249 					ip->type = F_DIR;
    250 				break;
    251 			case 'f':
    252 				if (!strcmp(val, "file"))
    253 					ip->type = F_FILE;
    254 				if (!strcmp(val, "fifo"))
    255 					ip->type = F_FIFO;
    256 				break;
    257 			case 'l':
    258 				if (!strcmp(val, "link"))
    259 					ip->type = F_LINK;
    260 				break;
    261 			case 's':
    262 				if (!strcmp(val, "socket"))
    263 					ip->type = F_SOCK;
    264 				break;
    265 			default:
    266 				err("unknown file type %s", val);
    267 			}
    268 			break;
    269 		case F_UID:
    270 			ip->st_uid = (uid_t)strtoul(val, &ep, 10);
    271 			if (*ep)
    272 				err("invalid uid %s", val);
    273 			break;
    274 		case F_UNAME:
    275 			if ((pw = getpwnam(val)) == NULL)
    276 			    err("unknown user %s", val);
    277 			ip->st_uid = pw->pw_uid;
    278 			break;
    279 		}
    280 	}
    281 }
    282 
    283 static void
    284 unset(t, ip)
    285 	char *t;
    286 	NODE *ip;
    287 {
    288 	char *p;
    289 
    290 	while ((p = strtok(t, "\n\t ")) != NULL)
    291 		ip->flags &= ~parsekey(p, NULL);
    292 }
    293