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