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