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