spec.c revision 1.73 1 1.73 apb /* $NetBSD: spec.c,v 1.73 2009/04/07 18:06:41 apb 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.50 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.31 lukem /*-
33 1.57 lukem * Copyright (c) 2001-2004 The NetBSD Foundation, Inc.
34 1.31 lukem * All rights reserved.
35 1.31 lukem *
36 1.31 lukem * This code is derived from software contributed to The NetBSD Foundation
37 1.31 lukem * by Luke Mewburn of Wasabi Systems.
38 1.31 lukem *
39 1.31 lukem * Redistribution and use in source and binary forms, with or without
40 1.31 lukem * modification, are permitted provided that the following conditions
41 1.31 lukem * are met:
42 1.31 lukem * 1. Redistributions of source code must retain the above copyright
43 1.31 lukem * notice, this list of conditions and the following disclaimer.
44 1.31 lukem * 2. Redistributions in binary form must reproduce the above copyright
45 1.31 lukem * notice, this list of conditions and the following disclaimer in the
46 1.31 lukem * documentation and/or other materials provided with the distribution.
47 1.31 lukem *
48 1.31 lukem * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
49 1.31 lukem * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
50 1.31 lukem * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
51 1.31 lukem * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
52 1.31 lukem * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53 1.31 lukem * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54 1.31 lukem * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55 1.31 lukem * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56 1.31 lukem * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57 1.31 lukem * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58 1.31 lukem * POSSIBILITY OF SUCH DAMAGE.
59 1.31 lukem */
60 1.31 lukem
61 1.56 jmc #if HAVE_NBTOOL_CONFIG_H
62 1.56 jmc #include "nbtool_config.h"
63 1.56 jmc #endif
64 1.56 jmc
65 1.11 lukem #include <sys/cdefs.h>
66 1.45 tv #if defined(__RCSID) && !defined(lint)
67 1.6 cgd #if 0
68 1.10 mrg static char sccsid[] = "@(#)spec.c 8.2 (Berkeley) 4/28/95";
69 1.6 cgd #else
70 1.73 apb __RCSID("$NetBSD: spec.c,v 1.73 2009/04/07 18:06:41 apb Exp $");
71 1.6 cgd #endif
72 1.1 cgd #endif /* not lint */
73 1.1 cgd
74 1.25 lukem #include <sys/param.h>
75 1.3 cgd #include <sys/stat.h>
76 1.21 simonb
77 1.11 lukem #include <ctype.h>
78 1.11 lukem #include <errno.h>
79 1.11 lukem #include <grp.h>
80 1.1 cgd #include <pwd.h>
81 1.11 lukem #include <stdio.h>
82 1.34 lukem #include <stdlib.h>
83 1.11 lukem #include <string.h>
84 1.3 cgd #include <unistd.h>
85 1.49 lukem #include <vis.h>
86 1.53 dbj #include <util.h>
87 1.11 lukem
88 1.3 cgd #include "extern.h"
89 1.32 lukem #include "pack_dev.h"
90 1.1 cgd
91 1.39 lukem size_t mtree_lineno; /* Current spec line number */
92 1.57 lukem int mtree_Mflag; /* Merge duplicate entries */
93 1.57 lukem int mtree_Wflag; /* Don't "whack" permissions */
94 1.1 cgd
95 1.47 lukem static dev_t parsedev(char *);
96 1.47 lukem static void replacenode(NODE *, NODE *);
97 1.47 lukem static void set(char *, NODE *);
98 1.47 lukem static void unset(char *, NODE *);
99 1.69 apb static void addchild(NODE *, NODE *);
100 1.69 apb static int nodecmp(const NODE *, const NODE *);
101 1.1 cgd
102 1.57 lukem #define REPLACEPTR(x,v) do { if ((x)) free((x)); (x) = (v); } while (0)
103 1.57 lukem
104 1.3 cgd NODE *
105 1.34 lukem spec(FILE *fp)
106 1.1 cgd {
107 1.31 lukem NODE *centry, *last, *pathparent, *cur;
108 1.33 lukem char *p, *e, *next;
109 1.3 cgd NODE ginfo, *root;
110 1.51 itojun char *buf, *tname, *ntname;
111 1.31 lukem size_t tnamelen, plen;
112 1.1 cgd
113 1.3 cgd root = NULL;
114 1.11 lukem centry = last = NULL;
115 1.31 lukem tname = NULL;
116 1.31 lukem tnamelen = 0;
117 1.11 lukem memset(&ginfo, 0, sizeof(ginfo));
118 1.39 lukem for (mtree_lineno = 0;
119 1.39 lukem (buf = fparseln(fp, NULL, &mtree_lineno, NULL,
120 1.63 martin FPARSELN_UNESCCOMM));
121 1.24 lukem free(buf)) {
122 1.24 lukem /* Skip leading whitespace. */
123 1.24 lukem for (p = buf; *p && isspace((unsigned char)*p); ++p)
124 1.3 cgd continue;
125 1.3 cgd
126 1.24 lukem /* If nothing but whitespace, continue. */
127 1.24 lukem if (!*p)
128 1.1 cgd continue;
129 1.1 cgd
130 1.3 cgd #ifdef DEBUG
131 1.40 lukem fprintf(stderr, "line %lu: {%s}\n",
132 1.39 lukem (u_long)mtree_lineno, p);
133 1.3 cgd #endif
134 1.3 cgd /* Grab file name, "$", "set", or "unset". */
135 1.33 lukem next = buf;
136 1.33 lukem while ((p = strsep(&next, " \t")) != NULL && *p == '\0')
137 1.33 lukem continue;
138 1.33 lukem if (p == NULL)
139 1.13 wsanchez mtree_err("missing field");
140 1.1 cgd
141 1.23 lukem if (p[0] == '/') {
142 1.23 lukem if (strcmp(p + 1, "set") == 0)
143 1.33 lukem set(next, &ginfo);
144 1.23 lukem else if (strcmp(p + 1, "unset") == 0)
145 1.33 lukem unset(next, &ginfo);
146 1.23 lukem else
147 1.23 lukem mtree_err("invalid specification `%s'", p);
148 1.23 lukem continue;
149 1.23 lukem }
150 1.1 cgd
151 1.31 lukem if (strcmp(p, "..") == 0) {
152 1.3 cgd /* Don't go up, if haven't gone down. */
153 1.31 lukem if (root == NULL)
154 1.3 cgd goto noparent;
155 1.1 cgd if (last->type != F_DIR || last->flags & F_DONE) {
156 1.1 cgd if (last == root)
157 1.3 cgd goto noparent;
158 1.1 cgd last = last->parent;
159 1.1 cgd }
160 1.1 cgd last->flags |= F_DONE;
161 1.1 cgd continue;
162 1.3 cgd
163 1.13 wsanchez noparent: mtree_err("no parent node");
164 1.1 cgd }
165 1.1 cgd
166 1.31 lukem plen = strlen(p) + 1;
167 1.31 lukem if (plen > tnamelen) {
168 1.51 itojun if ((ntname = realloc(tname, plen)) == NULL)
169 1.51 itojun mtree_err("realloc: %s", strerror(errno));
170 1.51 itojun tname = ntname;
171 1.31 lukem tnamelen = plen;
172 1.31 lukem }
173 1.31 lukem if (strunvis(tname, p) == -1)
174 1.31 lukem mtree_err("strunvis failed on `%s'", p);
175 1.31 lukem p = tname;
176 1.31 lukem
177 1.31 lukem pathparent = NULL;
178 1.31 lukem if (strchr(p, '/') != NULL) {
179 1.31 lukem cur = root;
180 1.31 lukem for (; (e = strchr(p, '/')) != NULL; p = e+1) {
181 1.31 lukem if (p == e)
182 1.31 lukem continue; /* handle // */
183 1.31 lukem *e = '\0';
184 1.31 lukem if (strcmp(p, ".") != 0) {
185 1.47 lukem while (cur &&
186 1.47 lukem strcmp(cur->name, p) != 0) {
187 1.31 lukem cur = cur->next;
188 1.31 lukem }
189 1.31 lukem }
190 1.31 lukem if (cur == NULL || cur->type != F_DIR) {
191 1.31 lukem mtree_err("%s: %s", tname,
192 1.31 lukem strerror(ENOENT));
193 1.31 lukem }
194 1.31 lukem *e = '/';
195 1.31 lukem pathparent = cur;
196 1.31 lukem cur = cur->child;
197 1.31 lukem }
198 1.31 lukem if (*p == '\0')
199 1.31 lukem mtree_err("%s: empty leaf element", tname);
200 1.31 lukem }
201 1.31 lukem
202 1.3 cgd if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL)
203 1.13 wsanchez mtree_err("%s", strerror(errno));
204 1.1 cgd *centry = ginfo;
205 1.39 lukem centry->lineno = mtree_lineno;
206 1.31 lukem strcpy(centry->name, p);
207 1.1 cgd #define MAGIC "?*["
208 1.1 cgd if (strpbrk(p, MAGIC))
209 1.1 cgd centry->flags |= F_MAGIC;
210 1.33 lukem set(next, centry);
211 1.1 cgd
212 1.31 lukem if (root == NULL) {
213 1.47 lukem /*
214 1.47 lukem * empty tree
215 1.47 lukem */
216 1.47 lukem if (strcmp(centry->name, ".") != 0 ||
217 1.47 lukem centry->type != F_DIR)
218 1.34 lukem mtree_err(
219 1.34 lukem "root node must be the directory `.'");
220 1.1 cgd last = root = centry;
221 1.1 cgd root->parent = root;
222 1.31 lukem } else if (pathparent != NULL) {
223 1.47 lukem /*
224 1.69 apb * full path entry; add or replace
225 1.47 lukem */
226 1.31 lukem centry->parent = pathparent;
227 1.69 apb addchild(pathparent, centry);
228 1.69 apb last = centry;
229 1.47 lukem } else if (strcmp(centry->name, ".") == 0) {
230 1.47 lukem /*
231 1.47 lukem * duplicate "." entry; always replace
232 1.47 lukem */
233 1.47 lukem replacenode(root, centry);
234 1.1 cgd } else if (last->type == F_DIR && !(last->flags & F_DONE)) {
235 1.47 lukem /*
236 1.69 apb * new relative child in current dir;
237 1.69 apb * add or replace
238 1.47 lukem */
239 1.1 cgd centry->parent = last;
240 1.69 apb addchild(last, centry);
241 1.69 apb last = centry;
242 1.1 cgd } else {
243 1.47 lukem /*
244 1.69 apb * new relative child in parent dir
245 1.69 apb * (after encountering ".." entry);
246 1.69 apb * add or replace
247 1.47 lukem */
248 1.1 cgd centry->parent = last->parent;
249 1.69 apb addchild(last->parent, centry);
250 1.69 apb last = centry;
251 1.1 cgd }
252 1.1 cgd }
253 1.3 cgd return (root);
254 1.1 cgd }
255 1.1 cgd
256 1.57 lukem void
257 1.57 lukem free_nodes(NODE *root)
258 1.57 lukem {
259 1.57 lukem NODE *cur, *next;
260 1.57 lukem
261 1.57 lukem if (root == NULL)
262 1.57 lukem return;
263 1.57 lukem
264 1.57 lukem next = NULL;
265 1.57 lukem for (cur = root; cur != NULL; cur = next) {
266 1.57 lukem next = cur->next;
267 1.57 lukem free_nodes(cur->child);
268 1.57 lukem REPLACEPTR(cur->slink, NULL);
269 1.57 lukem REPLACEPTR(cur->md5digest, NULL);
270 1.57 lukem REPLACEPTR(cur->rmd160digest, NULL);
271 1.57 lukem REPLACEPTR(cur->sha1digest, NULL);
272 1.60 elad REPLACEPTR(cur->sha256digest, NULL);
273 1.60 elad REPLACEPTR(cur->sha384digest, NULL);
274 1.60 elad REPLACEPTR(cur->sha512digest, NULL);
275 1.57 lukem REPLACEPTR(cur->tags, NULL);
276 1.57 lukem REPLACEPTR(cur, NULL);
277 1.57 lukem }
278 1.57 lukem }
279 1.57 lukem
280 1.25 lukem /*
281 1.25 lukem * dump_nodes --
282 1.49 lukem * dump the NODEs from `cur', based in the directory `dir'.
283 1.49 lukem * if pathlast is none zero, print the path last, otherwise print
284 1.49 lukem * it first.
285 1.25 lukem */
286 1.25 lukem void
287 1.49 lukem dump_nodes(const char *dir, NODE *root, int pathlast)
288 1.25 lukem {
289 1.25 lukem NODE *cur;
290 1.41 lukem char path[MAXPATHLEN];
291 1.30 lukem const char *name;
292 1.64 he char *str;
293 1.70 apb char *p, *q;
294 1.25 lukem
295 1.25 lukem for (cur = root; cur != NULL; cur = cur->next) {
296 1.28 lukem if (cur->type != F_DIR && !matchtags(cur))
297 1.28 lukem continue;
298 1.26 lukem
299 1.25 lukem if (snprintf(path, sizeof(path), "%s%s%s",
300 1.25 lukem dir, *dir ? "/" : "", cur->name)
301 1.68 lukem >= (int)sizeof(path))
302 1.25 lukem mtree_err("Pathname too long.");
303 1.30 lukem
304 1.49 lukem if (!pathlast)
305 1.49 lukem printf("%s ", vispath(path));
306 1.49 lukem
307 1.30 lukem #define MATCHFLAG(f) ((keys & (f)) && (cur->flags & (f)))
308 1.30 lukem if (MATCHFLAG(F_TYPE))
309 1.25 lukem printf("type=%s ", nodetype(cur->type));
310 1.30 lukem if (MATCHFLAG(F_UID | F_UNAME)) {
311 1.30 lukem if (keys & F_UNAME &&
312 1.30 lukem (name = user_from_uid(cur->st_uid, 1)) != NULL)
313 1.30 lukem printf("uname=%s ", name);
314 1.30 lukem else
315 1.30 lukem printf("uid=%u ", cur->st_uid);
316 1.30 lukem }
317 1.30 lukem if (MATCHFLAG(F_GID | F_GNAME)) {
318 1.30 lukem if (keys & F_GNAME &&
319 1.30 lukem (name = group_from_gid(cur->st_gid, 1)) != NULL)
320 1.30 lukem printf("gname=%s ", name);
321 1.30 lukem else
322 1.30 lukem printf("gid=%u ", cur->st_gid);
323 1.30 lukem }
324 1.30 lukem if (MATCHFLAG(F_MODE))
325 1.25 lukem printf("mode=%#o ", cur->st_mode);
326 1.32 lukem if (MATCHFLAG(F_DEV) &&
327 1.32 lukem (cur->type == F_BLOCK || cur->type == F_CHAR))
328 1.67 christos printf("device=%#llx ", (long long)cur->st_rdev);
329 1.31 lukem if (MATCHFLAG(F_NLINK))
330 1.25 lukem printf("nlink=%d ", cur->st_nlink);
331 1.30 lukem if (MATCHFLAG(F_SLINK))
332 1.58 lukem printf("link=%s ", vispath(cur->slink));
333 1.30 lukem if (MATCHFLAG(F_SIZE))
334 1.25 lukem printf("size=%lld ", (long long)cur->st_size);
335 1.30 lukem if (MATCHFLAG(F_TIME))
336 1.67 christos printf("time=%lld.%ld ",
337 1.67 christos (long long)cur->st_mtimespec.tv_sec,
338 1.25 lukem cur->st_mtimespec.tv_nsec);
339 1.30 lukem if (MATCHFLAG(F_CKSUM))
340 1.25 lukem printf("cksum=%lu ", cur->cksum);
341 1.30 lukem if (MATCHFLAG(F_MD5))
342 1.40 lukem printf("md5=%s ", cur->md5digest);
343 1.40 lukem if (MATCHFLAG(F_RMD160))
344 1.40 lukem printf("rmd160=%s ", cur->rmd160digest);
345 1.40 lukem if (MATCHFLAG(F_SHA1))
346 1.40 lukem printf("sha1=%s ", cur->sha1digest);
347 1.60 elad if (MATCHFLAG(F_SHA256))
348 1.60 elad printf("sha256=%s ", cur->sha256digest);
349 1.60 elad if (MATCHFLAG(F_SHA384))
350 1.60 elad printf("sha384=%s ", cur->sha384digest);
351 1.60 elad if (MATCHFLAG(F_SHA512))
352 1.60 elad printf("sha512=%s ", cur->sha512digest);
353 1.64 he if (MATCHFLAG(F_FLAGS)) {
354 1.64 he str = flags_to_string(cur->st_flags, "none");
355 1.64 he printf("flags=%s ", str);
356 1.64 he free(str);
357 1.64 he }
358 1.30 lukem if (MATCHFLAG(F_IGN))
359 1.30 lukem printf("ignore ");
360 1.30 lukem if (MATCHFLAG(F_OPT))
361 1.30 lukem printf("optional ");
362 1.70 apb if (MATCHFLAG(F_TAGS)) {
363 1.70 apb /* don't output leading or trailing commas */
364 1.70 apb p = cur->tags;
365 1.70 apb while (*p == ',')
366 1.70 apb p++;
367 1.70 apb q = p + strlen(p);
368 1.70 apb while(q > p && q[-1] == ',')
369 1.70 apb q--;
370 1.71 dogcow printf("tags=%.*s ", (int)(q - p), p);
371 1.70 apb }
372 1.49 lukem puts(pathlast ? vispath(path) : "");
373 1.25 lukem
374 1.25 lukem if (cur->child)
375 1.49 lukem dump_nodes(path, cur->child, pathlast);
376 1.25 lukem }
377 1.25 lukem }
378 1.49 lukem
379 1.49 lukem /*
380 1.49 lukem * vispath --
381 1.49 lukem * strsvis(3) encodes path, which must not be longer than MAXPATHLEN
382 1.49 lukem * characters long, and returns a pointer to a static buffer containing
383 1.49 lukem * the result.
384 1.49 lukem */
385 1.49 lukem char *
386 1.49 lukem vispath(const char *path)
387 1.49 lukem {
388 1.49 lukem const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
389 1.49 lukem static char pathbuf[4*MAXPATHLEN + 1];
390 1.49 lukem
391 1.49 lukem strsvis(pathbuf, path, VIS_CSTYLE, extra);
392 1.49 lukem return(pathbuf);
393 1.49 lukem }
394 1.49 lukem
395 1.25 lukem
396 1.32 lukem static dev_t
397 1.32 lukem parsedev(char *arg)
398 1.32 lukem {
399 1.32 lukem #define MAX_PACK_ARGS 3
400 1.32 lukem u_long numbers[MAX_PACK_ARGS];
401 1.32 lukem char *p, *ep, *dev;
402 1.32 lukem int argc;
403 1.32 lukem pack_t *pack;
404 1.32 lukem dev_t result;
405 1.55 christos const char *error = NULL;
406 1.32 lukem
407 1.32 lukem if ((dev = strchr(arg, ',')) != NULL) {
408 1.32 lukem *dev++='\0';
409 1.32 lukem if ((pack = pack_find(arg)) == NULL)
410 1.32 lukem mtree_err("unknown format `%s'", arg);
411 1.32 lukem argc = 0;
412 1.32 lukem while ((p = strsep(&dev, ",")) != NULL) {
413 1.32 lukem if (*p == '\0')
414 1.32 lukem mtree_err("missing number");
415 1.32 lukem numbers[argc++] = strtoul(p, &ep, 0);
416 1.32 lukem if (*ep != '\0')
417 1.32 lukem mtree_err("invalid number `%s'",
418 1.32 lukem p);
419 1.32 lukem if (argc > MAX_PACK_ARGS)
420 1.32 lukem mtree_err("too many arguments");
421 1.32 lukem }
422 1.32 lukem if (argc < 2)
423 1.32 lukem mtree_err("not enough arguments");
424 1.54 ross result = (*pack)(argc, numbers, &error);
425 1.54 ross if (error != NULL)
426 1.66 dbj mtree_err("%s", error);
427 1.32 lukem } else {
428 1.32 lukem result = (dev_t)strtoul(arg, &ep, 0);
429 1.32 lukem if (*ep != '\0')
430 1.32 lukem mtree_err("invalid device `%s'", arg);
431 1.32 lukem }
432 1.32 lukem return (result);
433 1.47 lukem }
434 1.47 lukem
435 1.47 lukem static void
436 1.47 lukem replacenode(NODE *cur, NODE *new)
437 1.47 lukem {
438 1.47 lukem
439 1.47 lukem #define REPLACE(x) cur->x = new->x
440 1.57 lukem #define REPLACESTR(x) REPLACEPTR(cur->x,new->x)
441 1.57 lukem
442 1.57 lukem if (cur->type != new->type) {
443 1.57 lukem if (mtree_Mflag) {
444 1.57 lukem /*
445 1.57 lukem * merge entries with different types; we
446 1.57 lukem * don't want children retained in this case.
447 1.57 lukem */
448 1.57 lukem REPLACE(type);
449 1.57 lukem free_nodes(cur->child);
450 1.57 lukem cur->child = NULL;
451 1.57 lukem } else {
452 1.57 lukem mtree_err(
453 1.57 lukem "existing entry for `%s', type `%s'"
454 1.57 lukem " does not match type `%s'",
455 1.57 lukem cur->name, nodetype(cur->type),
456 1.57 lukem nodetype(new->type));
457 1.57 lukem }
458 1.57 lukem }
459 1.47 lukem
460 1.47 lukem REPLACE(st_size);
461 1.47 lukem REPLACE(st_mtimespec);
462 1.47 lukem REPLACESTR(slink);
463 1.59 lukem if (cur->slink != NULL) {
464 1.59 lukem if ((cur->slink = strdup(new->slink)) == NULL)
465 1.59 lukem mtree_err("memory allocation error");
466 1.59 lukem if (strunvis(cur->slink, new->slink) == -1)
467 1.59 lukem mtree_err("strunvis failed on `%s'", new->slink);
468 1.59 lukem free(new->slink);
469 1.59 lukem }
470 1.47 lukem REPLACE(st_uid);
471 1.47 lukem REPLACE(st_gid);
472 1.47 lukem REPLACE(st_mode);
473 1.47 lukem REPLACE(st_rdev);
474 1.47 lukem REPLACE(st_flags);
475 1.47 lukem REPLACE(st_nlink);
476 1.47 lukem REPLACE(cksum);
477 1.47 lukem REPLACESTR(md5digest);
478 1.47 lukem REPLACESTR(rmd160digest);
479 1.47 lukem REPLACESTR(sha1digest);
480 1.60 elad REPLACESTR(sha256digest);
481 1.60 elad REPLACESTR(sha384digest);
482 1.60 elad REPLACESTR(sha512digest);
483 1.47 lukem REPLACESTR(tags);
484 1.47 lukem REPLACE(lineno);
485 1.47 lukem REPLACE(flags);
486 1.47 lukem free(new);
487 1.32 lukem }
488 1.32 lukem
489 1.3 cgd static void
490 1.21 simonb set(char *t, NODE *ip)
491 1.1 cgd {
492 1.32 lukem int type, value, len;
493 1.32 lukem gid_t gid;
494 1.32 lukem uid_t uid;
495 1.32 lukem char *kw, *val, *md, *ep;
496 1.32 lukem void *m;
497 1.3 cgd
498 1.33 lukem while ((kw = strsep(&t, "= \t")) != NULL) {
499 1.33 lukem if (*kw == '\0')
500 1.33 lukem continue;
501 1.30 lukem if (strcmp(kw, "all") == 0)
502 1.30 lukem mtree_err("invalid keyword `all'");
503 1.3 cgd ip->flags |= type = parsekey(kw, &value);
504 1.62 dsl if (!value)
505 1.62 dsl /* Just set flag bit (F_IGN and F_OPT) */
506 1.62 dsl continue;
507 1.62 dsl while ((val = strsep(&t, " \t")) != NULL && *val == '\0')
508 1.62 dsl continue;
509 1.62 dsl if (val == NULL)
510 1.62 dsl mtree_err("missing value");
511 1.62 dsl switch (type) {
512 1.1 cgd case F_CKSUM:
513 1.3 cgd ip->cksum = strtoul(val, &ep, 10);
514 1.3 cgd if (*ep)
515 1.22 christos mtree_err("invalid checksum `%s'", val);
516 1.14 mrg break;
517 1.32 lukem case F_DEV:
518 1.32 lukem ip->st_rdev = parsedev(val);
519 1.32 lukem break;
520 1.14 mrg case F_FLAGS:
521 1.14 mrg if (strcmp("none", val) == 0)
522 1.14 mrg ip->st_flags = 0;
523 1.32 lukem else if (string_to_flags(&val, &ip->st_flags, NULL)
524 1.32 lukem != 0)
525 1.22 christos mtree_err("invalid flag `%s'", val);
526 1.3 cgd break;
527 1.3 cgd case F_GID:
528 1.8 mikel ip->st_gid = (gid_t)strtoul(val, &ep, 10);
529 1.3 cgd if (*ep)
530 1.22 christos mtree_err("invalid gid `%s'", val);
531 1.3 cgd break;
532 1.3 cgd case F_GNAME:
533 1.57 lukem if (mtree_Wflag) /* don't parse if whacking */
534 1.39 lukem break;
535 1.38 tv if (gid_from_group(val, &gid) == -1)
536 1.39 lukem mtree_err("unknown group `%s'", val);
537 1.38 tv ip->st_gid = gid;
538 1.1 cgd break;
539 1.15 jwise case F_MD5:
540 1.15 jwise if (val[0]=='0' && val[1]=='x')
541 1.15 jwise md=&val[2];
542 1.15 jwise else
543 1.15 jwise md=val;
544 1.40 lukem if ((ip->md5digest = strdup(md)) == NULL)
545 1.15 jwise mtree_err("memory allocation error");
546 1.15 jwise break;
547 1.3 cgd case F_MODE:
548 1.3 cgd if ((m = setmode(val)) == NULL)
549 1.61 christos mtree_err("cannot set file mode `%s' (%s)",
550 1.61 christos val, strerror(errno));
551 1.1 cgd ip->st_mode = getmode(m, 0);
552 1.12 itohy free(m);
553 1.1 cgd break;
554 1.1 cgd case F_NLINK:
555 1.8 mikel ip->st_nlink = (nlink_t)strtoul(val, &ep, 10);
556 1.3 cgd if (*ep)
557 1.22 christos mtree_err("invalid link count `%s'", val);
558 1.32 lukem break;
559 1.40 lukem case F_RMD160:
560 1.40 lukem if (val[0]=='0' && val[1]=='x')
561 1.40 lukem md=&val[2];
562 1.40 lukem else
563 1.40 lukem md=val;
564 1.40 lukem if ((ip->rmd160digest = strdup(md)) == NULL)
565 1.40 lukem mtree_err("memory allocation error");
566 1.40 lukem break;
567 1.40 lukem case F_SHA1:
568 1.40 lukem if (val[0]=='0' && val[1]=='x')
569 1.40 lukem md=&val[2];
570 1.40 lukem else
571 1.40 lukem md=val;
572 1.40 lukem if ((ip->sha1digest = strdup(md)) == NULL)
573 1.40 lukem mtree_err("memory allocation error");
574 1.40 lukem break;
575 1.1 cgd case F_SIZE:
576 1.40 lukem ip->st_size = (off_t)strtoll(val, &ep, 10);
577 1.3 cgd if (*ep)
578 1.22 christos mtree_err("invalid size `%s'", val);
579 1.1 cgd break;
580 1.1 cgd case F_SLINK:
581 1.3 cgd if ((ip->slink = strdup(val)) == NULL)
582 1.15 jwise mtree_err("memory allocation error");
583 1.58 lukem if (strunvis(ip->slink, val) == -1)
584 1.58 lukem mtree_err("strunvis failed on `%s'", val);
585 1.27 lukem break;
586 1.27 lukem case F_TAGS:
587 1.27 lukem len = strlen(val) + 3; /* "," + str + ",\0" */
588 1.27 lukem if ((ip->tags = malloc(len)) == NULL)
589 1.27 lukem mtree_err("memory allocation error");
590 1.27 lukem snprintf(ip->tags, len, ",%s,", val);
591 1.1 cgd break;
592 1.1 cgd case F_TIME:
593 1.8 mikel ip->st_mtimespec.tv_sec =
594 1.67 christos (time_t)strtoll(val, &ep, 10);
595 1.5 cgd if (*ep != '.')
596 1.22 christos mtree_err("invalid time `%s'", val);
597 1.5 cgd val = ep + 1;
598 1.67 christos ip->st_mtimespec.tv_nsec = strtol(val, &ep, 10);
599 1.3 cgd if (*ep)
600 1.22 christos mtree_err("invalid time `%s'", val);
601 1.1 cgd break;
602 1.1 cgd case F_TYPE:
603 1.23 lukem ip->type = parsetype(val);
604 1.1 cgd break;
605 1.3 cgd case F_UID:
606 1.8 mikel ip->st_uid = (uid_t)strtoul(val, &ep, 10);
607 1.3 cgd if (*ep)
608 1.22 christos mtree_err("invalid uid `%s'", val);
609 1.3 cgd break;
610 1.3 cgd case F_UNAME:
611 1.57 lukem if (mtree_Wflag) /* don't parse if whacking */
612 1.39 lukem break;
613 1.38 tv if (uid_from_user(val, &uid) == -1)
614 1.39 lukem mtree_err("unknown user `%s'", val);
615 1.38 tv ip->st_uid = uid;
616 1.3 cgd break;
617 1.60 elad case F_SHA256:
618 1.60 elad if (val[0]=='0' && val[1]=='x')
619 1.60 elad md=&val[2];
620 1.60 elad else
621 1.60 elad md=val;
622 1.60 elad if ((ip->sha256digest = strdup(md)) == NULL)
623 1.60 elad mtree_err("memory allocation error");
624 1.60 elad break;
625 1.60 elad case F_SHA384:
626 1.60 elad if (val[0]=='0' && val[1]=='x')
627 1.60 elad md=&val[2];
628 1.60 elad else
629 1.60 elad md=val;
630 1.60 elad if ((ip->sha384digest = strdup(md)) == NULL)
631 1.60 elad mtree_err("memory allocation error");
632 1.60 elad break;
633 1.60 elad case F_SHA512:
634 1.60 elad if (val[0]=='0' && val[1]=='x')
635 1.60 elad md=&val[2];
636 1.60 elad else
637 1.60 elad md=val;
638 1.60 elad if ((ip->sha512digest = strdup(md)) == NULL)
639 1.60 elad mtree_err("memory allocation error");
640 1.60 elad break;
641 1.26 lukem default:
642 1.26 lukem mtree_err(
643 1.26 lukem "set(): unsupported key type 0x%x (INTERNAL ERROR)",
644 1.26 lukem type);
645 1.26 lukem /* NOTREACHED */
646 1.1 cgd }
647 1.1 cgd }
648 1.1 cgd }
649 1.1 cgd
650 1.3 cgd static void
651 1.21 simonb unset(char *t, NODE *ip)
652 1.1 cgd {
653 1.11 lukem char *p;
654 1.1 cgd
655 1.33 lukem while ((p = strsep(&t, " \t")) != NULL) {
656 1.33 lukem if (*p == '\0')
657 1.33 lukem continue;
658 1.3 cgd ip->flags &= ~parsekey(p, NULL);
659 1.30 lukem }
660 1.1 cgd }
661 1.69 apb
662 1.69 apb /*
663 1.69 apb * addchild --
664 1.69 apb * Add the centry node as a child of the pathparent node. If
665 1.69 apb * centry is a duplicate, call replacenode(). If centry is not
666 1.69 apb * a duplicate, insert it into the linked list referenced by
667 1.69 apb * pathparent->child. Keep the list sorted.
668 1.69 apb */
669 1.69 apb static void
670 1.69 apb addchild(NODE *pathparent, NODE *centry)
671 1.69 apb {
672 1.69 apb NODE *cur, *insertpos;
673 1.69 apb int cmp;
674 1.69 apb
675 1.69 apb cur = pathparent->child;
676 1.69 apb if (cur == NULL) {
677 1.69 apb /* centry is pathparent's first and only child node so far */
678 1.69 apb pathparent->child = centry;
679 1.69 apb return;
680 1.69 apb }
681 1.69 apb
682 1.69 apb /*
683 1.69 apb * pathparent already has at least one other child, so add the
684 1.69 apb * centry node to the list.
685 1.69 apb *
686 1.69 apb * To keep the list sorted, the new centry node will be
687 1.69 apb * inserted just after the existing insertpos node, if any;
688 1.69 apb * otherwise it will be inserted at the start of the list.
689 1.69 apb */
690 1.69 apb insertpos = NULL;
691 1.69 apb for (; cur != NULL; cur = cur->next) {
692 1.69 apb cmp = nodecmp(centry, cur);
693 1.69 apb if (cmp == 0) {
694 1.69 apb /* existing entry; replace */
695 1.69 apb replacenode(cur, centry);
696 1.69 apb break;
697 1.69 apb } else if (cmp > 0) {
698 1.69 apb /* centry appears after cur in sort order */
699 1.69 apb insertpos = cur;
700 1.69 apb }
701 1.69 apb if (cmp < 0 || cur->next == NULL) {
702 1.69 apb /*
703 1.69 apb * centry appears before cur in sort order,
704 1.69 apb * or we reached the end of the list; insert
705 1.69 apb * centry either just after insertpos, or at the
706 1.69 apb * beginning of the list.
707 1.69 apb */
708 1.69 apb if (insertpos) {
709 1.69 apb centry->next = insertpos->next;
710 1.69 apb insertpos->next = centry;
711 1.73 apb centry->prev = insertpos;
712 1.69 apb if (centry->next)
713 1.69 apb centry->next->prev = centry;
714 1.69 apb } else {
715 1.72 apb pathparent->child->prev = centry;
716 1.69 apb centry->next = pathparent->child;
717 1.69 apb pathparent->child = centry;
718 1.69 apb }
719 1.69 apb break;
720 1.69 apb }
721 1.69 apb }
722 1.69 apb return;
723 1.69 apb }
724 1.69 apb
725 1.69 apb /*
726 1.69 apb * nodecmp --
727 1.69 apb * used as a comparison function by addchild() to control the order
728 1.69 apb * in which entries appear within a list of sibling nodes. We make
729 1.69 apb * directories sort after non-directories, but otherwise sort in
730 1.69 apb * strcmp() order.
731 1.69 apb *
732 1.69 apb * Keep this in sync with dcmp() in create.c.
733 1.69 apb */
734 1.69 apb static int
735 1.69 apb nodecmp(const NODE *a, const NODE *b)
736 1.69 apb {
737 1.69 apb
738 1.69 apb if ((a->type & F_DIR) != 0) {
739 1.69 apb if ((b->type & F_DIR) == 0)
740 1.69 apb return 1;
741 1.69 apb } else if ((b->type & F_DIR) != 0)
742 1.69 apb return -1;
743 1.69 apb return strcmp(a->name, b->name);
744 1.69 apb }
745