spec.c revision 1.15
1/* $NetBSD: spec.c,v 1.15 1998/12/06 19:07:53 jwise 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 39static char sccsid[] = "@(#)spec.c 8.2 (Berkeley) 4/28/95"; 40#else 41__RCSID("$NetBSD: spec.c,v 1.15 1998/12/06 19:07:53 jwise 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 59int lineno; /* Current spec line number. */ 60 61static void set __P((char *, NODE *)); 62static void unset __P((char *, NODE *)); 63 64NODE * 65spec() 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 mtree_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 mtree_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 mtree_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 144noparent: mtree_err("no parent node"); 145 } 146 147 if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL) 148 mtree_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 171static void 172set(t, ip) 173 char *t; 174 NODE *ip; 175{ 176 int type; 177 char *kw, *val, *md; 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 mtree_err("missing value"); 189 switch(type) { 190 case F_CKSUM: 191 ip->cksum = strtoul(val, &ep, 10); 192 if (*ep) 193 mtree_err("invalid checksum %s", val); 194 break; 195 case F_FLAGS: 196 if (strcmp("none", val) == 0) 197 ip->st_flags = 0; 198 else if (string_to_flags(&val, &ip->st_flags, NULL) != 0) 199 mtree_err("invalid flag %s", val); 200 break; 201 case F_GID: 202 ip->st_gid = (gid_t)strtoul(val, &ep, 10); 203 if (*ep) 204 mtree_err("invalid gid %s", val); 205 break; 206 case F_GNAME: 207 if ((gr = getgrnam(val)) == NULL) 208 mtree_err("unknown group %s", val); 209 ip->st_gid = gr->gr_gid; 210 break; 211 case F_IGN: 212 /* just set flag bit */ 213 break; 214 case F_MD5: 215 if (val[0]=='0' && val[1]=='x') 216 md=&val[2]; 217 else 218 md=val; 219 if ((ip->md5sum = strdup(md)) == NULL) 220 mtree_err("memory allocation error"); 221 break; 222 case F_MODE: 223 if ((m = setmode(val)) == NULL) 224 mtree_err("invalid file mode %s", val); 225 ip->st_mode = getmode(m, 0); 226 free(m); 227 break; 228 case F_NLINK: 229 ip->st_nlink = (nlink_t)strtoul(val, &ep, 10); 230 if (*ep) 231 mtree_err("invalid link count %s", val); 232 break; 233 case F_SIZE: 234 ip->st_size = (off_t)strtoq(val, &ep, 10); 235 if (*ep) 236 mtree_err("invalid size %s", val); 237 break; 238 case F_SLINK: 239 if ((ip->slink = strdup(val)) == NULL) 240 mtree_err("memory allocation error"); 241 break; 242 case F_TIME: 243#ifndef __APPLE__ 244 ip->st_mtimespec.tv_sec = 245 (time_t)strtoul(val, &ep, 10); 246#else 247 ip->st_mtimespec.ts_sec = 248 (time_t)strtoul(val, &ep, 10); 249#endif 250 if (*ep != '.') 251 mtree_err("invalid time %s", val); 252 val = ep + 1; 253#ifndef __APPLE__ 254 ip->st_mtimespec.tv_nsec = strtol(val, &ep, 10); 255#else 256 ip->st_mtimespec.ts_nsec = strtol(val, &ep, 10); 257#endif 258 if (*ep) 259 mtree_err("invalid time %s", val); 260 break; 261 case F_TYPE: 262 switch(*val) { 263 case 'b': 264 if (!strcmp(val, "block")) 265 ip->type = F_BLOCK; 266 break; 267 case 'c': 268 if (!strcmp(val, "char")) 269 ip->type = F_CHAR; 270 break; 271 case 'd': 272 if (!strcmp(val, "dir")) 273 ip->type = F_DIR; 274 break; 275 case 'f': 276 if (!strcmp(val, "file")) 277 ip->type = F_FILE; 278 if (!strcmp(val, "fifo")) 279 ip->type = F_FIFO; 280 break; 281 case 'l': 282 if (!strcmp(val, "link")) 283 ip->type = F_LINK; 284 break; 285 case 's': 286 if (!strcmp(val, "socket")) 287 ip->type = F_SOCK; 288 break; 289 default: 290 mtree_err("unknown file type %s", val); 291 } 292 break; 293 case F_UID: 294 ip->st_uid = (uid_t)strtoul(val, &ep, 10); 295 if (*ep) 296 mtree_err("invalid uid %s", val); 297 break; 298 case F_UNAME: 299 if ((pw = getpwnam(val)) == NULL) 300 mtree_err("unknown user %s", val); 301 ip->st_uid = pw->pw_uid; 302 break; 303 } 304 } 305} 306 307static void 308unset(t, ip) 309 char *t; 310 NODE *ip; 311{ 312 char *p; 313 314 while ((p = strtok(t, "\n\t ")) != NULL) 315 ip->flags &= ~parsekey(p, NULL); 316} 317