1244541Sbrooks/* $NetBSD: spec.c,v 1.85 2012/12/20 16:43:16 christos Exp $ */ 2244541Sbrooks 3244541Sbrooks/*- 4244541Sbrooks * Copyright (c) 1989, 1993 5244541Sbrooks * The Regents of the University of California. All rights reserved. 6244541Sbrooks * 7244541Sbrooks * Redistribution and use in source and binary forms, with or without 8244541Sbrooks * modification, are permitted provided that the following conditions 9244541Sbrooks * are met: 10244541Sbrooks * 1. Redistributions of source code must retain the above copyright 11244541Sbrooks * notice, this list of conditions and the following disclaimer. 12244541Sbrooks * 2. Redistributions in binary form must reproduce the above copyright 13244541Sbrooks * notice, this list of conditions and the following disclaimer in the 14244541Sbrooks * documentation and/or other materials provided with the distribution. 15244541Sbrooks * 3. Neither the name of the University nor the names of its contributors 16244541Sbrooks * may be used to endorse or promote products derived from this software 17244541Sbrooks * without specific prior written permission. 18244541Sbrooks * 19244541Sbrooks * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20244541Sbrooks * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21244541Sbrooks * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22244541Sbrooks * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23244541Sbrooks * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24244541Sbrooks * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25244541Sbrooks * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26244541Sbrooks * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27244541Sbrooks * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28244541Sbrooks * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29244541Sbrooks * SUCH DAMAGE. 30244541Sbrooks */ 31244541Sbrooks 32244541Sbrooks/*- 33244541Sbrooks * Copyright (c) 2001-2004 The NetBSD Foundation, Inc. 34244541Sbrooks * All rights reserved. 35244541Sbrooks * 36244541Sbrooks * This code is derived from software contributed to The NetBSD Foundation 37244541Sbrooks * by Luke Mewburn of Wasabi Systems. 38244541Sbrooks * 39244541Sbrooks * Redistribution and use in source and binary forms, with or without 40244541Sbrooks * modification, are permitted provided that the following conditions 41244541Sbrooks * are met: 42244541Sbrooks * 1. Redistributions of source code must retain the above copyright 43244541Sbrooks * notice, this list of conditions and the following disclaimer. 44244541Sbrooks * 2. Redistributions in binary form must reproduce the above copyright 45244541Sbrooks * notice, this list of conditions and the following disclaimer in the 46244541Sbrooks * documentation and/or other materials provided with the distribution. 47244541Sbrooks * 48244541Sbrooks * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 49244541Sbrooks * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 50244541Sbrooks * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 51244541Sbrooks * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 52244541Sbrooks * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 53244541Sbrooks * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 54244541Sbrooks * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 55244541Sbrooks * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 56244541Sbrooks * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 57244541Sbrooks * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 58244541Sbrooks * POSSIBILITY OF SUCH DAMAGE. 59244541Sbrooks */ 60244541Sbrooks 61244541Sbrooks#if HAVE_NBTOOL_CONFIG_H 62244541Sbrooks#include "nbtool_config.h" 63244541Sbrooks#endif 64244541Sbrooks 65244541Sbrooks#include <sys/cdefs.h> 66244541Sbrooks#if defined(__RCSID) && !defined(lint) 67244541Sbrooks#if 0 68244541Sbrooksstatic char sccsid[] = "@(#)spec.c 8.2 (Berkeley) 4/28/95"; 69244541Sbrooks#else 70244541Sbrooks__RCSID("$NetBSD: spec.c,v 1.85 2012/12/20 16:43:16 christos Exp $"); 71244541Sbrooks#endif 72244541Sbrooks#endif /* not lint */ 73244541Sbrooks 74244541Sbrooks#include <sys/param.h> 75244541Sbrooks#include <sys/stat.h> 76244541Sbrooks 77244541Sbrooks#include <assert.h> 78244541Sbrooks#include <ctype.h> 79244541Sbrooks#include <errno.h> 80244541Sbrooks#include <grp.h> 81244541Sbrooks#include <pwd.h> 82244541Sbrooks#include <stdarg.h> 83244541Sbrooks#include <stdio.h> 84244541Sbrooks#include <stdlib.h> 85244541Sbrooks#include <string.h> 86244541Sbrooks#include <unistd.h> 87244541Sbrooks#include <vis.h> 88244541Sbrooks#include <util.h> 89244541Sbrooks 90244541Sbrooks#include "extern.h" 91244541Sbrooks#include "pack_dev.h" 92244541Sbrooks 93244541Sbrookssize_t mtree_lineno; /* Current spec line number */ 94244541Sbrooksint mtree_Mflag; /* Merge duplicate entries */ 95244541Sbrooksint mtree_Wflag; /* Don't "whack" permissions */ 96244541Sbrooksint mtree_Sflag; /* Sort entries */ 97244541Sbrooks 98244541Sbrooksstatic dev_t parsedev(char *); 99244541Sbrooksstatic void replacenode(NODE *, NODE *); 100244541Sbrooksstatic void set(char *, NODE *); 101244541Sbrooksstatic void unset(char *, NODE *); 102244541Sbrooksstatic void addchild(NODE *, NODE *); 103244541Sbrooksstatic int nodecmp(const NODE *, const NODE *); 104244541Sbrooksstatic int appendfield(int, const char *, ...) __printflike(2, 3); 105244541Sbrooks 106244541Sbrooks#define REPLACEPTR(x,v) do { if ((x)) free((x)); (x) = (v); } while (0) 107244541Sbrooks 108244541SbrooksNODE * 109244541Sbrooksspec(FILE *fp) 110244541Sbrooks{ 111244541Sbrooks NODE *centry, *last, *pathparent, *cur; 112244541Sbrooks char *p, *e, *next; 113244541Sbrooks NODE ginfo, *root; 114244541Sbrooks char *buf, *tname, *ntname; 115244541Sbrooks size_t tnamelen, plen; 116244541Sbrooks 117244541Sbrooks root = NULL; 118244541Sbrooks centry = last = NULL; 119244541Sbrooks tname = NULL; 120244541Sbrooks tnamelen = 0; 121244541Sbrooks memset(&ginfo, 0, sizeof(ginfo)); 122244541Sbrooks for (mtree_lineno = 0; 123244541Sbrooks (buf = fparseln(fp, NULL, &mtree_lineno, NULL, 124244541Sbrooks FPARSELN_UNESCCOMM)); 125244541Sbrooks free(buf)) { 126244541Sbrooks /* Skip leading whitespace. */ 127244541Sbrooks for (p = buf; *p && isspace((unsigned char)*p); ++p) 128244541Sbrooks continue; 129244541Sbrooks 130244541Sbrooks /* If nothing but whitespace, continue. */ 131244541Sbrooks if (!*p) 132244541Sbrooks continue; 133244541Sbrooks 134244541Sbrooks#ifdef DEBUG 135244541Sbrooks fprintf(stderr, "line %lu: {%s}\n", 136244541Sbrooks (u_long)mtree_lineno, p); 137244541Sbrooks#endif 138244541Sbrooks /* Grab file name, "$", "set", or "unset". */ 139244541Sbrooks next = buf; 140244541Sbrooks while ((p = strsep(&next, " \t")) != NULL && *p == '\0') 141244541Sbrooks continue; 142244541Sbrooks if (p == NULL) 143244541Sbrooks mtree_err("missing field"); 144244541Sbrooks 145244541Sbrooks if (p[0] == '/') { 146244541Sbrooks if (strcmp(p + 1, "set") == 0) 147244541Sbrooks set(next, &ginfo); 148244541Sbrooks else if (strcmp(p + 1, "unset") == 0) 149244541Sbrooks unset(next, &ginfo); 150244541Sbrooks else 151244541Sbrooks mtree_err("invalid specification `%s'", p); 152244541Sbrooks continue; 153244541Sbrooks } 154244541Sbrooks 155244541Sbrooks if (strcmp(p, "..") == 0) { 156244541Sbrooks /* Don't go up, if haven't gone down. */ 157244541Sbrooks if (root == NULL) 158244541Sbrooks goto noparent; 159244541Sbrooks if (last->type != F_DIR || last->flags & F_DONE) { 160244541Sbrooks if (last == root) 161244541Sbrooks goto noparent; 162244541Sbrooks last = last->parent; 163244541Sbrooks } 164244541Sbrooks last->flags |= F_DONE; 165244541Sbrooks continue; 166244541Sbrooks 167244541Sbrooksnoparent: mtree_err("no parent node"); 168244541Sbrooks } 169244541Sbrooks 170244541Sbrooks plen = strlen(p) + 1; 171244541Sbrooks if (plen > tnamelen) { 172244541Sbrooks if ((ntname = realloc(tname, plen)) == NULL) 173244541Sbrooks mtree_err("realloc: %s", strerror(errno)); 174244541Sbrooks tname = ntname; 175244541Sbrooks tnamelen = plen; 176244541Sbrooks } 177244541Sbrooks if (strunvis(tname, p) == -1) 178244541Sbrooks mtree_err("strunvis failed on `%s'", p); 179244541Sbrooks p = tname; 180244541Sbrooks 181244541Sbrooks pathparent = NULL; 182244541Sbrooks if (strchr(p, '/') != NULL) { 183244541Sbrooks cur = root; 184244541Sbrooks for (; (e = strchr(p, '/')) != NULL; p = e+1) { 185244541Sbrooks if (p == e) 186244541Sbrooks continue; /* handle // */ 187244541Sbrooks *e = '\0'; 188244541Sbrooks if (strcmp(p, ".") != 0) { 189244541Sbrooks while (cur && 190244541Sbrooks strcmp(cur->name, p) != 0) { 191244541Sbrooks cur = cur->next; 192244541Sbrooks } 193244541Sbrooks } 194244541Sbrooks if (cur == NULL || cur->type != F_DIR) { 195244541Sbrooks mtree_err("%s: %s", tname, 196244541Sbrooks "missing directory in specification"); 197244541Sbrooks } 198244541Sbrooks *e = '/'; 199244541Sbrooks pathparent = cur; 200244541Sbrooks cur = cur->child; 201244541Sbrooks } 202244541Sbrooks if (*p == '\0') 203244541Sbrooks mtree_err("%s: empty leaf element", tname); 204244541Sbrooks } 205244541Sbrooks 206244541Sbrooks if ((centry = calloc(1, sizeof(NODE) + strlen(p))) == NULL) 207244541Sbrooks mtree_err("%s", strerror(errno)); 208244541Sbrooks *centry = ginfo; 209244541Sbrooks centry->lineno = mtree_lineno; 210244541Sbrooks strcpy(centry->name, p); 211244541Sbrooks#define MAGIC "?*[" 212244541Sbrooks if (strpbrk(p, MAGIC)) 213244541Sbrooks centry->flags |= F_MAGIC; 214244541Sbrooks set(next, centry); 215244541Sbrooks 216244541Sbrooks if (root == NULL) { 217244541Sbrooks /* 218244541Sbrooks * empty tree 219244541Sbrooks */ 220244541Sbrooks if (strcmp(centry->name, ".") != 0 || 221244541Sbrooks centry->type != F_DIR) 222244541Sbrooks mtree_err( 223244541Sbrooks "root node must be the directory `.'"); 224244541Sbrooks last = root = centry; 225244541Sbrooks root->parent = root; 226244541Sbrooks } else if (pathparent != NULL) { 227244541Sbrooks /* 228244541Sbrooks * full path entry; add or replace 229244541Sbrooks */ 230244541Sbrooks centry->parent = pathparent; 231244541Sbrooks addchild(pathparent, centry); 232244541Sbrooks last = centry; 233244541Sbrooks } else if (strcmp(centry->name, ".") == 0) { 234244541Sbrooks /* 235244541Sbrooks * duplicate "." entry; always replace 236244541Sbrooks */ 237244541Sbrooks replacenode(root, centry); 238244541Sbrooks } else if (last->type == F_DIR && !(last->flags & F_DONE)) { 239244541Sbrooks /* 240244541Sbrooks * new relative child in current dir; 241244541Sbrooks * add or replace 242244541Sbrooks */ 243244541Sbrooks centry->parent = last; 244244541Sbrooks addchild(last, centry); 245244541Sbrooks last = centry; 246244541Sbrooks } else { 247244541Sbrooks /* 248244541Sbrooks * new relative child in parent dir 249244541Sbrooks * (after encountering ".." entry); 250244541Sbrooks * add or replace 251244541Sbrooks */ 252244541Sbrooks centry->parent = last->parent; 253244541Sbrooks addchild(last->parent, centry); 254244541Sbrooks last = centry; 255244541Sbrooks } 256244541Sbrooks } 257244541Sbrooks return (root); 258244541Sbrooks} 259244541Sbrooks 260244541Sbrooksvoid 261244541Sbrooksfree_nodes(NODE *root) 262244541Sbrooks{ 263244541Sbrooks NODE *cur, *next; 264244541Sbrooks 265244541Sbrooks if (root == NULL) 266244541Sbrooks return; 267244541Sbrooks 268244541Sbrooks next = NULL; 269244541Sbrooks for (cur = root; cur != NULL; cur = next) { 270244541Sbrooks next = cur->next; 271244541Sbrooks free_nodes(cur->child); 272244541Sbrooks REPLACEPTR(cur->slink, NULL); 273244541Sbrooks REPLACEPTR(cur->md5digest, NULL); 274244541Sbrooks REPLACEPTR(cur->rmd160digest, NULL); 275244541Sbrooks REPLACEPTR(cur->sha1digest, NULL); 276244541Sbrooks REPLACEPTR(cur->sha256digest, NULL); 277244541Sbrooks REPLACEPTR(cur->sha384digest, NULL); 278244541Sbrooks REPLACEPTR(cur->sha512digest, NULL); 279244541Sbrooks REPLACEPTR(cur->tags, NULL); 280244541Sbrooks REPLACEPTR(cur, NULL); 281244541Sbrooks } 282244541Sbrooks} 283244541Sbrooks 284244541Sbrooks/* 285244541Sbrooks * appendfield -- 286244541Sbrooks * Like printf(), but output a space either before or after 287244541Sbrooks * the regular output, according to the pathlast flag. 288244541Sbrooks */ 289244541Sbrooksstatic int 290244541Sbrooksappendfield(int pathlast, const char *fmt, ...) 291244541Sbrooks{ 292244541Sbrooks va_list ap; 293244541Sbrooks int result; 294244541Sbrooks 295244541Sbrooks va_start(ap, fmt); 296244541Sbrooks if (!pathlast) 297244541Sbrooks printf(" "); 298244541Sbrooks result = vprintf(fmt, ap); 299244541Sbrooks if (pathlast) 300244541Sbrooks printf(" "); 301244541Sbrooks va_end(ap); 302244541Sbrooks return result; 303244541Sbrooks} 304244541Sbrooks 305244541Sbrooks/* 306244541Sbrooks * dump_nodes -- 307244541Sbrooks * dump the NODEs from `cur', based in the directory `dir'. 308244541Sbrooks * if pathlast is none zero, print the path last, otherwise print 309244541Sbrooks * it first. 310244541Sbrooks */ 311244541Sbrooksvoid 312244541Sbrooksdump_nodes(const char *dir, NODE *root, int pathlast) 313244541Sbrooks{ 314244541Sbrooks NODE *cur; 315244541Sbrooks char path[MAXPATHLEN]; 316244541Sbrooks const char *name; 317244541Sbrooks char *str; 318244541Sbrooks char *p, *q; 319244541Sbrooks 320244541Sbrooks for (cur = root; cur != NULL; cur = cur->next) { 321244541Sbrooks if (cur->type != F_DIR && !matchtags(cur)) 322244541Sbrooks continue; 323244541Sbrooks 324244541Sbrooks if (snprintf(path, sizeof(path), "%s%s%s", 325244541Sbrooks dir, *dir ? "/" : "", cur->name) 326244541Sbrooks >= (int)sizeof(path)) 327244541Sbrooks mtree_err("Pathname too long."); 328244541Sbrooks 329244541Sbrooks if (!pathlast) 330244541Sbrooks printf("%s", vispath(path)); 331244541Sbrooks 332244541Sbrooks#define MATCHFLAG(f) ((keys & (f)) && (cur->flags & (f))) 333244541Sbrooks if (MATCHFLAG(F_TYPE)) 334244541Sbrooks appendfield(pathlast, "type=%s", nodetype(cur->type)); 335244541Sbrooks if (MATCHFLAG(F_UID | F_UNAME)) { 336244541Sbrooks if (keys & F_UNAME && 337244541Sbrooks (name = user_from_uid(cur->st_uid, 1)) != NULL) 338244541Sbrooks appendfield(pathlast, "uname=%s", name); 339244541Sbrooks else 340244541Sbrooks appendfield(pathlast, "uid=%u", cur->st_uid); 341244541Sbrooks } 342244541Sbrooks if (MATCHFLAG(F_GID | F_GNAME)) { 343244541Sbrooks if (keys & F_GNAME && 344244541Sbrooks (name = group_from_gid(cur->st_gid, 1)) != NULL) 345244541Sbrooks appendfield(pathlast, "gname=%s", name); 346244541Sbrooks else 347244541Sbrooks appendfield(pathlast, "gid=%u", cur->st_gid); 348244541Sbrooks } 349244541Sbrooks if (MATCHFLAG(F_MODE)) 350244541Sbrooks appendfield(pathlast, "mode=%#o", cur->st_mode); 351244541Sbrooks if (MATCHFLAG(F_DEV) && 352244541Sbrooks (cur->type == F_BLOCK || cur->type == F_CHAR)) 353244541Sbrooks appendfield(pathlast, "device=%#llx", (long long)cur->st_rdev); 354244541Sbrooks if (MATCHFLAG(F_NLINK)) 355244541Sbrooks appendfield(pathlast, "nlink=%d", cur->st_nlink); 356244541Sbrooks if (MATCHFLAG(F_SLINK)) 357244541Sbrooks appendfield(pathlast, "link=%s", vispath(cur->slink)); 358244541Sbrooks if (MATCHFLAG(F_SIZE)) 359244541Sbrooks appendfield(pathlast, "size=%lld", (long long)cur->st_size); 360244541Sbrooks if (MATCHFLAG(F_TIME)) 361244541Sbrooks appendfield(pathlast, "time=%lld.%09ld", 362244541Sbrooks (long long)cur->st_mtimespec.tv_sec, 363244541Sbrooks cur->st_mtimespec.tv_nsec); 364244541Sbrooks if (MATCHFLAG(F_CKSUM)) 365244541Sbrooks appendfield(pathlast, "cksum=%lu", cur->cksum); 366244541Sbrooks if (MATCHFLAG(F_MD5)) 367244541Sbrooks appendfield(pathlast, "%s=%s", MD5KEY, cur->md5digest); 368244541Sbrooks if (MATCHFLAG(F_RMD160)) 369244541Sbrooks appendfield(pathlast, "%s=%s", RMD160KEY, 370244541Sbrooks cur->rmd160digest); 371244541Sbrooks if (MATCHFLAG(F_SHA1)) 372244541Sbrooks appendfield(pathlast, "%s=%s", SHA1KEY, 373244541Sbrooks cur->sha1digest); 374244541Sbrooks if (MATCHFLAG(F_SHA256)) 375244541Sbrooks appendfield(pathlast, "%s=%s", SHA256KEY, 376244541Sbrooks cur->sha256digest); 377244541Sbrooks if (MATCHFLAG(F_SHA384)) 378244541Sbrooks appendfield(pathlast, "%s=%s", SHA384KEY, 379244541Sbrooks cur->sha384digest); 380244541Sbrooks if (MATCHFLAG(F_SHA512)) 381244541Sbrooks appendfield(pathlast, "%s=%s", SHA512KEY, 382244541Sbrooks cur->sha512digest); 383244541Sbrooks if (MATCHFLAG(F_FLAGS)) { 384244541Sbrooks str = flags_to_string(cur->st_flags, "none"); 385244541Sbrooks appendfield(pathlast, "flags=%s", str); 386244541Sbrooks free(str); 387244541Sbrooks } 388244541Sbrooks if (MATCHFLAG(F_IGN)) 389244541Sbrooks appendfield(pathlast, "ignore"); 390244541Sbrooks if (MATCHFLAG(F_OPT)) 391244541Sbrooks appendfield(pathlast, "optional"); 392244541Sbrooks if (MATCHFLAG(F_TAGS)) { 393244541Sbrooks /* don't output leading or trailing commas */ 394244541Sbrooks p = cur->tags; 395244541Sbrooks while (*p == ',') 396244541Sbrooks p++; 397244541Sbrooks q = p + strlen(p); 398244541Sbrooks while(q > p && q[-1] == ',') 399244541Sbrooks q--; 400244541Sbrooks appendfield(pathlast, "tags=%.*s", (int)(q - p), p); 401244541Sbrooks } 402244541Sbrooks puts(pathlast ? vispath(path) : ""); 403244541Sbrooks 404244541Sbrooks if (cur->child) 405244541Sbrooks dump_nodes(path, cur->child, pathlast); 406244541Sbrooks } 407244541Sbrooks} 408244541Sbrooks 409244541Sbrooks/* 410244541Sbrooks * vispath -- 411244541Sbrooks * strsvis(3) encodes path, which must not be longer than MAXPATHLEN 412244541Sbrooks * characters long, and returns a pointer to a static buffer containing 413244541Sbrooks * the result. 414244541Sbrooks */ 415244541Sbrookschar * 416244541Sbrooksvispath(const char *path) 417244541Sbrooks{ 418244541Sbrooks static const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' }; 419244541Sbrooks static const char extra_glob[] = { ' ', '\t', '\n', '\\', '#', '*', 420244541Sbrooks '?', '[', '\0' }; 421244541Sbrooks static char pathbuf[4*MAXPATHLEN + 1]; 422244541Sbrooks 423244541Sbrooks if (flavor == F_NETBSD6) 424244541Sbrooks strsvis(pathbuf, path, VIS_CSTYLE, extra); 425244541Sbrooks else 426244541Sbrooks strsvis(pathbuf, path, VIS_OCTAL, extra_glob); 427244541Sbrooks return pathbuf; 428244541Sbrooks} 429244541Sbrooks 430244541Sbrooks 431244541Sbrooksstatic dev_t 432244541Sbrooksparsedev(char *arg) 433244541Sbrooks{ 434244541Sbrooks#define MAX_PACK_ARGS 3 435244541Sbrooks u_long numbers[MAX_PACK_ARGS]; 436244541Sbrooks char *p, *ep, *dev; 437244541Sbrooks int argc; 438244541Sbrooks pack_t *pack; 439244541Sbrooks dev_t result; 440244541Sbrooks const char *error = NULL; 441244541Sbrooks 442244541Sbrooks if ((dev = strchr(arg, ',')) != NULL) { 443244541Sbrooks *dev++='\0'; 444244541Sbrooks if ((pack = pack_find(arg)) == NULL) 445244541Sbrooks mtree_err("unknown format `%s'", arg); 446244541Sbrooks argc = 0; 447244541Sbrooks while ((p = strsep(&dev, ",")) != NULL) { 448244541Sbrooks if (*p == '\0') 449244541Sbrooks mtree_err("missing number"); 450244541Sbrooks numbers[argc++] = strtoul(p, &ep, 0); 451244541Sbrooks if (*ep != '\0') 452244541Sbrooks mtree_err("invalid number `%s'", 453244541Sbrooks p); 454244541Sbrooks if (argc > MAX_PACK_ARGS) 455244541Sbrooks mtree_err("too many arguments"); 456244541Sbrooks } 457244541Sbrooks if (argc < 2) 458244541Sbrooks mtree_err("not enough arguments"); 459244541Sbrooks result = (*pack)(argc, numbers, &error); 460244541Sbrooks if (error != NULL) 461244541Sbrooks mtree_err("%s", error); 462244541Sbrooks } else { 463244541Sbrooks result = (dev_t)strtoul(arg, &ep, 0); 464244541Sbrooks if (*ep != '\0') 465244541Sbrooks mtree_err("invalid device `%s'", arg); 466244541Sbrooks } 467244541Sbrooks return (result); 468244541Sbrooks} 469244541Sbrooks 470244541Sbrooksstatic void 471244541Sbrooksreplacenode(NODE *cur, NODE *new) 472244541Sbrooks{ 473244541Sbrooks 474244541Sbrooks#define REPLACE(x) cur->x = new->x 475244541Sbrooks#define REPLACESTR(x) REPLACEPTR(cur->x,new->x) 476244541Sbrooks 477244541Sbrooks if (cur->type != new->type) { 478244541Sbrooks if (mtree_Mflag) { 479244541Sbrooks /* 480244541Sbrooks * merge entries with different types; we 481244541Sbrooks * don't want children retained in this case. 482244541Sbrooks */ 483244541Sbrooks REPLACE(type); 484244541Sbrooks free_nodes(cur->child); 485244541Sbrooks cur->child = NULL; 486244541Sbrooks } else { 487244541Sbrooks mtree_err( 488244541Sbrooks "existing entry for `%s', type `%s'" 489244541Sbrooks " does not match type `%s'", 490244541Sbrooks cur->name, nodetype(cur->type), 491244541Sbrooks nodetype(new->type)); 492244541Sbrooks } 493244541Sbrooks } 494244541Sbrooks 495244541Sbrooks REPLACE(st_size); 496244541Sbrooks REPLACE(st_mtimespec); 497244541Sbrooks REPLACESTR(slink); 498244541Sbrooks if (cur->slink != NULL) { 499244541Sbrooks if ((cur->slink = strdup(new->slink)) == NULL) 500244541Sbrooks mtree_err("memory allocation error"); 501244541Sbrooks if (strunvis(cur->slink, new->slink) == -1) 502244541Sbrooks mtree_err("strunvis failed on `%s'", new->slink); 503244541Sbrooks free(new->slink); 504244541Sbrooks } 505244541Sbrooks REPLACE(st_uid); 506244541Sbrooks REPLACE(st_gid); 507244541Sbrooks REPLACE(st_mode); 508244541Sbrooks REPLACE(st_rdev); 509244541Sbrooks REPLACE(st_flags); 510244541Sbrooks REPLACE(st_nlink); 511244541Sbrooks REPLACE(cksum); 512244541Sbrooks REPLACESTR(md5digest); 513244541Sbrooks REPLACESTR(rmd160digest); 514244541Sbrooks REPLACESTR(sha1digest); 515244541Sbrooks REPLACESTR(sha256digest); 516244541Sbrooks REPLACESTR(sha384digest); 517244541Sbrooks REPLACESTR(sha512digest); 518244541Sbrooks REPLACESTR(tags); 519244541Sbrooks REPLACE(lineno); 520244541Sbrooks REPLACE(flags); 521244541Sbrooks free(new); 522244541Sbrooks} 523244541Sbrooks 524244541Sbrooksstatic void 525244541Sbrooksset(char *t, NODE *ip) 526244541Sbrooks{ 527244541Sbrooks int type, value, len; 528244541Sbrooks gid_t gid; 529244541Sbrooks uid_t uid; 530244541Sbrooks char *kw, *val, *md, *ep; 531244541Sbrooks void *m; 532244541Sbrooks 533244541Sbrooks while ((kw = strsep(&t, "= \t")) != NULL) { 534244541Sbrooks if (*kw == '\0') 535244541Sbrooks continue; 536244541Sbrooks if (strcmp(kw, "all") == 0) 537244541Sbrooks mtree_err("invalid keyword `all'"); 538244541Sbrooks ip->flags |= type = parsekey(kw, &value); 539244541Sbrooks if (!value) 540244541Sbrooks /* Just set flag bit (F_IGN and F_OPT) */ 541244541Sbrooks continue; 542244541Sbrooks while ((val = strsep(&t, " \t")) != NULL && *val == '\0') 543244541Sbrooks continue; 544244541Sbrooks if (val == NULL) 545244541Sbrooks mtree_err("missing value"); 546244541Sbrooks switch (type) { 547244541Sbrooks case F_CKSUM: 548244541Sbrooks ip->cksum = strtoul(val, &ep, 10); 549244541Sbrooks if (*ep) 550244541Sbrooks mtree_err("invalid checksum `%s'", val); 551244541Sbrooks break; 552244541Sbrooks case F_DEV: 553244541Sbrooks ip->st_rdev = parsedev(val); 554244541Sbrooks break; 555244541Sbrooks case F_FLAGS: 556244541Sbrooks if (strcmp("none", val) == 0) 557244541Sbrooks ip->st_flags = 0; 558244541Sbrooks else if (string_to_flags(&val, &ip->st_flags, NULL) 559244541Sbrooks != 0) 560244541Sbrooks mtree_err("invalid flag `%s'", val); 561244541Sbrooks break; 562244541Sbrooks case F_GID: 563244541Sbrooks ip->st_gid = (gid_t)strtoul(val, &ep, 10); 564244541Sbrooks if (*ep) 565244541Sbrooks mtree_err("invalid gid `%s'", val); 566244541Sbrooks break; 567244541Sbrooks case F_GNAME: 568244541Sbrooks if (mtree_Wflag) /* don't parse if whacking */ 569244541Sbrooks break; 570244541Sbrooks if (gid_from_group(val, &gid) == -1) 571244541Sbrooks mtree_err("unknown group `%s'", val); 572244541Sbrooks ip->st_gid = gid; 573244541Sbrooks break; 574244541Sbrooks case F_MD5: 575244541Sbrooks if (val[0]=='0' && val[1]=='x') 576244541Sbrooks md=&val[2]; 577244541Sbrooks else 578244541Sbrooks md=val; 579244541Sbrooks if ((ip->md5digest = strdup(md)) == NULL) 580244541Sbrooks mtree_err("memory allocation error"); 581244541Sbrooks break; 582244541Sbrooks case F_MODE: 583244541Sbrooks if ((m = setmode(val)) == NULL) 584244541Sbrooks mtree_err("cannot set file mode `%s' (%s)", 585244541Sbrooks val, strerror(errno)); 586244541Sbrooks ip->st_mode = getmode(m, 0); 587244541Sbrooks free(m); 588244541Sbrooks break; 589244541Sbrooks case F_NLINK: 590244541Sbrooks ip->st_nlink = (nlink_t)strtoul(val, &ep, 10); 591244541Sbrooks if (*ep) 592244541Sbrooks mtree_err("invalid link count `%s'", val); 593244541Sbrooks break; 594244541Sbrooks case F_RMD160: 595244541Sbrooks if (val[0]=='0' && val[1]=='x') 596244541Sbrooks md=&val[2]; 597244541Sbrooks else 598244541Sbrooks md=val; 599244541Sbrooks if ((ip->rmd160digest = strdup(md)) == NULL) 600244541Sbrooks mtree_err("memory allocation error"); 601244541Sbrooks break; 602244541Sbrooks case F_SHA1: 603244541Sbrooks if (val[0]=='0' && val[1]=='x') 604244541Sbrooks md=&val[2]; 605244541Sbrooks else 606244541Sbrooks md=val; 607244541Sbrooks if ((ip->sha1digest = strdup(md)) == NULL) 608244541Sbrooks mtree_err("memory allocation error"); 609244541Sbrooks break; 610244541Sbrooks case F_SIZE: 611244541Sbrooks ip->st_size = (off_t)strtoll(val, &ep, 10); 612244541Sbrooks if (*ep) 613244541Sbrooks mtree_err("invalid size `%s'", val); 614244541Sbrooks break; 615244541Sbrooks case F_SLINK: 616244541Sbrooks if ((ip->slink = strdup(val)) == NULL) 617244541Sbrooks mtree_err("memory allocation error"); 618244541Sbrooks if (strunvis(ip->slink, val) == -1) 619244541Sbrooks mtree_err("strunvis failed on `%s'", val); 620244541Sbrooks break; 621244541Sbrooks case F_TAGS: 622244541Sbrooks len = strlen(val) + 3; /* "," + str + ",\0" */ 623244541Sbrooks if ((ip->tags = malloc(len)) == NULL) 624244541Sbrooks mtree_err("memory allocation error"); 625244541Sbrooks snprintf(ip->tags, len, ",%s,", val); 626244541Sbrooks break; 627244541Sbrooks case F_TIME: 628244541Sbrooks ip->st_mtimespec.tv_sec = 629244541Sbrooks (time_t)strtoll(val, &ep, 10); 630244541Sbrooks if (*ep != '.') 631244541Sbrooks mtree_err("invalid time `%s'", val); 632244541Sbrooks val = ep + 1; 633244541Sbrooks ip->st_mtimespec.tv_nsec = strtol(val, &ep, 10); 634244541Sbrooks if (*ep) 635244541Sbrooks mtree_err("invalid time `%s'", val); 636244541Sbrooks break; 637244541Sbrooks case F_TYPE: 638244541Sbrooks ip->type = parsetype(val); 639244541Sbrooks break; 640244541Sbrooks case F_UID: 641244541Sbrooks ip->st_uid = (uid_t)strtoul(val, &ep, 10); 642244541Sbrooks if (*ep) 643244541Sbrooks mtree_err("invalid uid `%s'", val); 644244541Sbrooks break; 645244541Sbrooks case F_UNAME: 646244541Sbrooks if (mtree_Wflag) /* don't parse if whacking */ 647244541Sbrooks break; 648244541Sbrooks if (uid_from_user(val, &uid) == -1) 649244541Sbrooks mtree_err("unknown user `%s'", val); 650244541Sbrooks ip->st_uid = uid; 651244541Sbrooks break; 652244541Sbrooks case F_SHA256: 653244541Sbrooks if (val[0]=='0' && val[1]=='x') 654244541Sbrooks md=&val[2]; 655244541Sbrooks else 656244541Sbrooks md=val; 657244541Sbrooks if ((ip->sha256digest = strdup(md)) == NULL) 658244541Sbrooks mtree_err("memory allocation error"); 659244541Sbrooks break; 660244541Sbrooks case F_SHA384: 661244541Sbrooks if (val[0]=='0' && val[1]=='x') 662244541Sbrooks md=&val[2]; 663244541Sbrooks else 664244541Sbrooks md=val; 665244541Sbrooks if ((ip->sha384digest = strdup(md)) == NULL) 666244541Sbrooks mtree_err("memory allocation error"); 667244541Sbrooks break; 668244541Sbrooks case F_SHA512: 669244541Sbrooks if (val[0]=='0' && val[1]=='x') 670244541Sbrooks md=&val[2]; 671244541Sbrooks else 672244541Sbrooks md=val; 673244541Sbrooks if ((ip->sha512digest = strdup(md)) == NULL) 674244541Sbrooks mtree_err("memory allocation error"); 675244541Sbrooks break; 676244541Sbrooks default: 677244541Sbrooks mtree_err( 678244541Sbrooks "set(): unsupported key type 0x%x (INTERNAL ERROR)", 679244541Sbrooks type); 680244541Sbrooks /* NOTREACHED */ 681244541Sbrooks } 682244541Sbrooks } 683244541Sbrooks} 684244541Sbrooks 685244541Sbrooksstatic void 686244541Sbrooksunset(char *t, NODE *ip) 687244541Sbrooks{ 688244541Sbrooks char *p; 689244541Sbrooks 690244541Sbrooks while ((p = strsep(&t, " \t")) != NULL) { 691244541Sbrooks if (*p == '\0') 692244541Sbrooks continue; 693244541Sbrooks ip->flags &= ~parsekey(p, NULL); 694244541Sbrooks } 695244541Sbrooks} 696244541Sbrooks 697244541Sbrooks/* 698244541Sbrooks * addchild -- 699244541Sbrooks * Add the centry node as a child of the pathparent node. If 700244541Sbrooks * centry is a duplicate, call replacenode(). If centry is not 701244541Sbrooks * a duplicate, insert it into the linked list referenced by 702244541Sbrooks * pathparent->child. Keep the list sorted if Sflag is set. 703244541Sbrooks */ 704244541Sbrooksstatic void 705244541Sbrooksaddchild(NODE *pathparent, NODE *centry) 706244541Sbrooks{ 707244541Sbrooks NODE *samename; /* node with the same name as centry */ 708244541Sbrooks NODE *replacepos; /* if non-NULL, centry should replace this node */ 709244541Sbrooks NODE *insertpos; /* if non-NULL, centry should be inserted 710244541Sbrooks * after this node */ 711244541Sbrooks NODE *cur; /* for stepping through the list */ 712244541Sbrooks NODE *last; /* the last node in the list */ 713244541Sbrooks int cmp; 714244541Sbrooks 715244541Sbrooks samename = NULL; 716244541Sbrooks replacepos = NULL; 717244541Sbrooks insertpos = NULL; 718244541Sbrooks last = NULL; 719244541Sbrooks cur = pathparent->child; 720244541Sbrooks if (cur == NULL) { 721244541Sbrooks /* centry is pathparent's first and only child node so far */ 722244541Sbrooks pathparent->child = centry; 723244541Sbrooks return; 724244541Sbrooks } 725244541Sbrooks 726244541Sbrooks /* 727244541Sbrooks * pathparent already has at least one other child, so add the 728244541Sbrooks * centry node to the list. 729244541Sbrooks * 730244541Sbrooks * We first scan through the list looking for an existing node 731244541Sbrooks * with the same name (setting samename), and also looking 732244541Sbrooks * for the correct position to replace or insert the new node 733244541Sbrooks * (setting replacepos and/or insertpos). 734244541Sbrooks */ 735244541Sbrooks for (; cur != NULL; last = cur, cur = cur->next) { 736244541Sbrooks if (strcmp(centry->name, cur->name) == 0) { 737244541Sbrooks samename = cur; 738244541Sbrooks } 739244541Sbrooks if (mtree_Sflag) { 740244541Sbrooks cmp = nodecmp(centry, cur); 741244541Sbrooks if (cmp == 0) { 742244541Sbrooks replacepos = cur; 743244541Sbrooks } else if (cmp > 0) { 744244541Sbrooks insertpos = cur; 745244541Sbrooks } 746244541Sbrooks } 747244541Sbrooks } 748244541Sbrooks if (! mtree_Sflag) { 749244541Sbrooks if (samename != NULL) { 750244541Sbrooks /* replace node with same name */ 751244541Sbrooks replacepos = samename; 752244541Sbrooks } else { 753244541Sbrooks /* add new node at end of list */ 754244541Sbrooks insertpos = last; 755244541Sbrooks } 756244541Sbrooks } 757244541Sbrooks 758244541Sbrooks if (samename != NULL) { 759244541Sbrooks /* 760244541Sbrooks * We found a node with the same name above. Call 761244541Sbrooks * replacenode(), which will either exit with an error, 762244541Sbrooks * or replace the information in the samename node and 763244541Sbrooks * free the information in the centry node. 764244541Sbrooks */ 765244541Sbrooks replacenode(samename, centry); 766244541Sbrooks if (samename == replacepos) { 767244541Sbrooks /* The just-replaced node was in the correct position */ 768244541Sbrooks return; 769244541Sbrooks } 770244541Sbrooks if (samename == insertpos || samename->prev == insertpos) { 771244541Sbrooks /* 772244541Sbrooks * We thought the new node should be just before 773244541Sbrooks * or just after the replaced node, but that would 774244541Sbrooks * be equivalent to just retaining the replaced node. 775244541Sbrooks */ 776244541Sbrooks return; 777244541Sbrooks } 778244541Sbrooks 779244541Sbrooks /* 780244541Sbrooks * The just-replaced node is in the wrong position in 781244541Sbrooks * the list. This can happen if sort order depends on 782244541Sbrooks * criteria other than the node name. 783244541Sbrooks * 784244541Sbrooks * Make centry point to the just-replaced node. Unlink 785244541Sbrooks * the just-replaced node from the list, and allow it to 786244541Sbrooks * be insterted in the correct position later. 787244541Sbrooks */ 788244541Sbrooks centry = samename; 789244541Sbrooks if (centry->prev) 790244541Sbrooks centry->prev->next = centry->next; 791244541Sbrooks else { 792244541Sbrooks /* centry->next is the new head of the list */ 793244541Sbrooks pathparent->child = centry->next; 794244541Sbrooks assert(centry->next != NULL); 795244541Sbrooks } 796244541Sbrooks if (centry->next) 797244541Sbrooks centry->next->prev = centry->prev; 798244541Sbrooks centry->prev = NULL; 799244541Sbrooks centry->next = NULL; 800244541Sbrooks } 801244541Sbrooks 802244541Sbrooks if (insertpos == NULL) { 803244541Sbrooks /* insert centry at the beginning of the list */ 804244541Sbrooks pathparent->child->prev = centry; 805244541Sbrooks centry->next = pathparent->child; 806244541Sbrooks centry->prev = NULL; 807244541Sbrooks pathparent->child = centry; 808244541Sbrooks } else { 809244541Sbrooks /* insert centry into the list just after insertpos */ 810244541Sbrooks centry->next = insertpos->next; 811244541Sbrooks insertpos->next = centry; 812244541Sbrooks centry->prev = insertpos; 813244541Sbrooks if (centry->next) 814244541Sbrooks centry->next->prev = centry; 815244541Sbrooks } 816244541Sbrooks return; 817244541Sbrooks} 818244541Sbrooks 819244541Sbrooks/* 820244541Sbrooks * nodecmp -- 821244541Sbrooks * used as a comparison function by addchild() to control the order 822244541Sbrooks * in which entries appear within a list of sibling nodes. We make 823244541Sbrooks * directories sort after non-directories, but otherwise sort in 824244541Sbrooks * strcmp() order. 825244541Sbrooks * 826244541Sbrooks * Keep this in sync with dcmp() in create.c. 827244541Sbrooks */ 828244541Sbrooksstatic int 829244541Sbrooksnodecmp(const NODE *a, const NODE *b) 830244541Sbrooks{ 831244541Sbrooks 832244541Sbrooks if ((a->type & F_DIR) != 0) { 833244541Sbrooks if ((b->type & F_DIR) == 0) 834244541Sbrooks return 1; 835244541Sbrooks } else if ((b->type & F_DIR) != 0) 836244541Sbrooks return -1; 837244541Sbrooks return strcmp(a->name, b->name); 838244541Sbrooks} 839