walk.c revision 186256
1/* $NetBSD: walk.c,v 1.17 2004/06/20 22:20:18 jmc Exp $ */ 2 3/* 4 * Copyright (c) 2001 Wasabi Systems, Inc. 5 * All rights reserved. 6 * 7 * Written by Luke Mewburn for Wasabi Systems, Inc. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed for the NetBSD Project by 20 * Wasabi Systems, Inc. 21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22 * or promote products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38/* 39 * The function link_check() was inspired from NetBSD's usr.bin/du/du.c, 40 * which has the following copyright notice: 41 * 42 * 43 * Copyright (c) 1989, 1993, 1994 44 * The Regents of the University of California. All rights reserved. 45 * 46 * This code is derived from software contributed to Berkeley by 47 * Chris Newcomb. 48 * 49 * Redistribution and use in source and binary forms, with or without 50 * modification, are permitted provided that the following conditions 51 * are met: 52 * 1. Redistributions of source code must retain the above copyright 53 * notice, this list of conditions and the following disclaimer. 54 * 2. Redistributions in binary form must reproduce the above copyright 55 * notice, this list of conditions and the following disclaimer in the 56 * documentation and/or other materials provided with the distribution. 57 * 3. Neither the name of the University nor the names of its contributors 58 * may be used to endorse or promote products derived from this software 59 * without specific prior written permission. 60 * 61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 71 * SUCH DAMAGE. 72 */ 73 74#if HAVE_NBTOOL_CONFIG_H 75#include "nbtool_config.h" 76#endif 77 78#include <sys/cdefs.h> 79#if defined(__RCSID) && !defined(__lint) 80__RCSID("$NetBSD: walk.c,v 1.17 2004/06/20 22:20:18 jmc Exp $"); 81#endif /* !__lint */ 82 83#include <sys/param.h> 84 85#include <assert.h> 86#include <errno.h> 87#include <fcntl.h> 88#include <stdio.h> 89#include <dirent.h> 90#include <stdlib.h> 91#include <string.h> 92#include <unistd.h> 93 94#include "makefs.h" 95 96#include "mtree.h" 97#include "extern.h" /* NB: mtree */ 98 99static void apply_specdir(const char *, NODE *, fsnode *); 100static void apply_specentry(const char *, NODE *, fsnode *); 101static fsnode *create_fsnode(const char *, struct stat *); 102static fsinode *link_check(fsinode *); 103 104 105/* 106 * walk_dir -- 107 * build a tree of fsnodes from `dir', with a parent fsnode of `parent' 108 * (which may be NULL for the root of the tree). 109 * each "level" is a directory, with the "." entry guaranteed to be 110 * at the start of the list, and without ".." entries. 111 */ 112fsnode * 113walk_dir(const char *dir, fsnode *parent) 114{ 115 fsnode *first, *cur, *prev; 116 DIR *dirp; 117 struct dirent *dent; 118 char path[MAXPATHLEN + 1]; 119 struct stat stbuf; 120 121 assert(dir != NULL); 122 123 if (debug & DEBUG_WALK_DIR) 124 printf("walk_dir: %s %p\n", dir, parent); 125 if ((dirp = opendir(dir)) == NULL) 126 err(1, "Can't opendir `%s'", dir); 127 first = prev = NULL; 128 while ((dent = readdir(dirp)) != NULL) { 129 if (strcmp(dent->d_name, "..") == 0) 130 continue; 131 if (debug & DEBUG_WALK_DIR_NODE) 132 printf("scanning %s/%s\n", dir, dent->d_name); 133 if (snprintf(path, sizeof(path), "%s/%s", dir, dent->d_name) 134 >= sizeof(path)) 135 errx(1, "Pathname too long."); 136 if (lstat(path, &stbuf) == -1) 137 err(1, "Can't lstat `%s'", path); 138#ifdef S_ISSOCK 139 if (S_ISSOCK(stbuf.st_mode & S_IFMT)) { 140 if (debug & DEBUG_WALK_DIR_NODE) 141 printf(" skipping socket %s\n", path); 142 continue; 143 } 144#endif 145 146 cur = create_fsnode(dent->d_name, &stbuf); 147 cur->parent = parent; 148 if (strcmp(dent->d_name, ".") == 0) { 149 /* ensure "." is at the start of the list */ 150 cur->next = first; 151 first = cur; 152 if (! prev) 153 prev = cur; 154 } else { /* not "." */ 155 if (prev) 156 prev->next = cur; 157 prev = cur; 158 if (!first) 159 first = cur; 160 if (S_ISDIR(cur->type)) { 161 cur->child = walk_dir(path, cur); 162 continue; 163 } 164 } 165 if (stbuf.st_nlink > 1) { 166 fsinode *curino; 167 168 curino = link_check(cur->inode); 169 if (curino != NULL) { 170 free(cur->inode); 171 cur->inode = curino; 172 cur->inode->nlink++; 173 } 174 } 175 if (S_ISLNK(cur->type)) { 176 char slink[PATH_MAX+1]; 177 int llen; 178 179 llen = readlink(path, slink, sizeof(slink) - 1); 180 if (llen == -1) 181 err(1, "Readlink `%s'", path); 182 slink[llen] = '\0'; 183 if ((cur->symlink = strdup(slink)) == NULL) 184 err(1, "Memory allocation error"); 185 } 186 } 187 for (cur = first; cur != NULL; cur = cur->next) 188 cur->first = first; 189 if (closedir(dirp) == -1) 190 err(1, "Can't closedir `%s'", dir); 191 return (first); 192} 193 194static fsnode * 195create_fsnode(const char *name, struct stat *stbuf) 196{ 197 fsnode *cur; 198 199 if ((cur = calloc(1, sizeof(fsnode))) == NULL || 200 (cur->name = strdup(name)) == NULL || 201 (cur->inode = calloc(1, sizeof(fsinode))) == NULL) 202 err(1, "Memory allocation error"); 203 cur->type = stbuf->st_mode & S_IFMT; 204 cur->inode->nlink = 1; 205 cur->inode->st = *stbuf; 206 return (cur); 207} 208 209/* 210 * apply_specfile -- 211 * read in the mtree(8) specfile, and apply it to the tree 212 * at dir,parent. parameters in parent on equivalent types 213 * will be changed to those found in specfile, and missing 214 * entries will be added. 215 */ 216void 217apply_specfile(const char *specfile, const char *dir, fsnode *parent) 218{ 219 struct timeval start; 220 FILE *fp; 221 NODE *root; 222 223 assert(specfile != NULL); 224 assert(parent != NULL); 225 226 if (debug & DEBUG_APPLY_SPECFILE) 227 printf("apply_specfile: %s, %s %p\n", specfile, dir, parent); 228 229 /* read in the specfile */ 230 if ((fp = fopen(specfile, "r")) == NULL) 231 err(1, "Can't open `%s'", specfile); 232 TIMER_START(start); 233 root = mtree_readspec(fp); 234 TIMER_RESULTS(start, "spec"); 235 if (fclose(fp) == EOF) 236 err(1, "Can't close `%s'", specfile); 237 238 /* perform some sanity checks */ 239 if (root == NULL) 240 errx(1, "Specfile `%s' did not contain a tree", specfile); 241 assert(strcmp(root->name, ".") == 0); 242 assert(root->type == F_DIR); 243 244 /* merge in the changes */ 245 apply_specdir(dir, root, parent); 246} 247 248static u_int 249nodetoino(u_int type) 250{ 251 252 switch (type) { 253 case F_BLOCK: 254 return S_IFBLK; 255 case F_CHAR: 256 return S_IFCHR; 257 case F_DIR: 258 return S_IFDIR; 259 case F_FIFO: 260 return S_IFIFO; 261 case F_FILE: 262 return S_IFREG; 263 case F_LINK: 264 return S_IFLNK; 265 case F_SOCK: 266 return S_IFSOCK; 267 default: 268 printf("unknown type %d", type); 269 abort(); 270 } 271 /* NOTREACHED */ 272} 273 274static void 275apply_specdir(const char *dir, NODE *specnode, fsnode *dirnode) 276{ 277 char path[MAXPATHLEN + 1]; 278 NODE *curnode; 279 fsnode *curfsnode; 280 281 assert(specnode != NULL); 282 assert(dirnode != NULL); 283 284 if (debug & DEBUG_APPLY_SPECFILE) 285 printf("apply_specdir: %s %p %p\n", dir, specnode, dirnode); 286 287 if (specnode->type != F_DIR) 288 errx(1, "Specfile node `%s/%s' is not a directory", 289 dir, specnode->name); 290 if (dirnode->type != S_IFDIR) 291 errx(1, "Directory node `%s/%s' is not a directory", 292 dir, dirnode->name); 293 294 apply_specentry(dir, specnode, dirnode); 295 296 /* now walk specnode->child matching up with dirnode */ 297 for (curnode = specnode->child; curnode != NULL; 298 curnode = curnode->next) { 299 if (debug & DEBUG_APPLY_SPECENTRY) 300 printf("apply_specdir: spec %s\n", 301 curnode->name); 302 for (curfsnode = dirnode->next; curfsnode != NULL; 303 curfsnode = curfsnode->next) { 304#if 0 /* too verbose for now */ 305 if (debug & DEBUG_APPLY_SPECENTRY) 306 printf("apply_specdir: dirent %s\n", 307 curfsnode->name); 308#endif 309 if (strcmp(curnode->name, curfsnode->name) == 0) 310 break; 311 } 312 if (snprintf(path, sizeof(path), "%s/%s", 313 dir, curnode->name) >= sizeof(path)) 314 errx(1, "Pathname too long."); 315 if (curfsnode == NULL) { /* need new entry */ 316 struct stat stbuf; 317 318 /* 319 * don't add optional spec entries 320 * that lack an existing fs entry 321 */ 322 if ((curnode->flags & F_OPT) && 323 lstat(path, &stbuf) == -1) 324 continue; 325 326 /* check that enough info is provided */ 327#define NODETEST(t, m) \ 328 if (!(t)) \ 329 errx(1, "`%s': %s not provided", path, m) 330 NODETEST(curnode->flags & F_TYPE, "type"); 331 NODETEST(curnode->flags & F_MODE, "mode"); 332 /* XXX: require F_TIME ? */ 333 NODETEST(curnode->flags & F_GID || 334 curnode->flags & F_GNAME, "group"); 335 NODETEST(curnode->flags & F_UID || 336 curnode->flags & F_UNAME, "user"); 337#undef NODETEST 338 339 if (debug & DEBUG_APPLY_SPECFILE) 340 printf("apply_specdir: adding %s\n", 341 curnode->name); 342 /* build minimal fsnode */ 343 memset(&stbuf, 0, sizeof(stbuf)); 344 stbuf.st_mode = nodetoino(curnode->type); 345 stbuf.st_nlink = 1; 346 stbuf.st_mtime = stbuf.st_atime = 347 stbuf.st_ctime = start_time.tv_sec; 348#if HAVE_STRUCT_STAT_ST_MTIMENSEC 349 stbuf.st_mtimensec = stbuf.st_atimensec = 350 stbuf.st_ctimensec = start_time.tv_nsec; 351#endif 352 curfsnode = create_fsnode(curnode->name, &stbuf); 353 curfsnode->parent = dirnode->parent; 354 curfsnode->first = dirnode; 355 curfsnode->next = dirnode->next; 356 dirnode->next = curfsnode; 357 if (curfsnode->type == S_IFDIR) { 358 /* for dirs, make "." entry as well */ 359 curfsnode->child = create_fsnode(".", &stbuf); 360 curfsnode->child->parent = curfsnode; 361 curfsnode->child->first = curfsnode->child; 362 } 363 if (curfsnode->type == S_IFLNK) { 364 assert(curnode->slink != NULL); 365 /* for symlinks, copy the target */ 366 if ((curfsnode->symlink = 367 strdup(curnode->slink)) == NULL) 368 err(1, "Memory allocation error"); 369 } 370 } 371 apply_specentry(dir, curnode, curfsnode); 372 if (curnode->type == F_DIR) { 373 if (curfsnode->type != S_IFDIR) 374 errx(1, "`%s' is not a directory", path); 375 assert (curfsnode->child != NULL); 376 apply_specdir(path, curnode, curfsnode->child); 377 } 378 } 379} 380 381static void 382apply_specentry(const char *dir, NODE *specnode, fsnode *dirnode) 383{ 384 385 assert(specnode != NULL); 386 assert(dirnode != NULL); 387 388 if (nodetoino(specnode->type) != dirnode->type) 389 errx(1, "`%s/%s' type mismatch: specfile %s, tree %s", 390 dir, specnode->name, inode_type(nodetoino(specnode->type)), 391 inode_type(dirnode->type)); 392 393 if (debug & DEBUG_APPLY_SPECENTRY) 394 printf("apply_specentry: %s/%s\n", dir, dirnode->name); 395 396#define ASEPRINT(t, b, o, n) \ 397 if (debug & DEBUG_APPLY_SPECENTRY) \ 398 printf("\t\t\tchanging %s from " b " to " b "\n", \ 399 t, o, n) 400 401 if (specnode->flags & (F_GID | F_GNAME)) { 402 ASEPRINT("gid", "%d", 403 dirnode->inode->st.st_gid, specnode->st_gid); 404 dirnode->inode->st.st_gid = specnode->st_gid; 405 } 406 if (specnode->flags & F_MODE) { 407 ASEPRINT("mode", "%#o", 408 dirnode->inode->st.st_mode & ALLPERMS, specnode->st_mode); 409 dirnode->inode->st.st_mode &= ~ALLPERMS; 410 dirnode->inode->st.st_mode |= (specnode->st_mode & ALLPERMS); 411 } 412 /* XXX: ignoring F_NLINK for now */ 413 if (specnode->flags & F_SIZE) { 414 ASEPRINT("size", "%lld", 415 (long long)dirnode->inode->st.st_size, 416 (long long)specnode->st_size); 417 dirnode->inode->st.st_size = specnode->st_size; 418 } 419 if (specnode->flags & F_SLINK) { 420 assert(dirnode->symlink != NULL); 421 assert(specnode->slink != NULL); 422 ASEPRINT("symlink", "%s", dirnode->symlink, specnode->slink); 423 free(dirnode->symlink); 424 if ((dirnode->symlink = strdup(specnode->slink)) == NULL) 425 err(1, "Memory allocation error"); 426 } 427 if (specnode->flags & F_TIME) { 428 ASEPRINT("time", "%ld", 429 (long)dirnode->inode->st.st_mtime, 430 (long)specnode->st_mtimespec.tv_sec); 431 dirnode->inode->st.st_mtime = specnode->st_mtimespec.tv_sec; 432 dirnode->inode->st.st_atime = specnode->st_mtimespec.tv_sec; 433 dirnode->inode->st.st_ctime = start_time.tv_sec; 434#if HAVE_STRUCT_STAT_ST_MTIMENSEC 435 dirnode->inode->st.st_mtimensec = specnode->st_mtimespec.tv_nsec; 436 dirnode->inode->st.st_atimensec = specnode->st_mtimespec.tv_nsec; 437 dirnode->inode->st.st_ctimensec = start_time.tv_nsec; 438#endif 439 } 440 if (specnode->flags & (F_UID | F_UNAME)) { 441 ASEPRINT("uid", "%d", 442 dirnode->inode->st.st_uid, specnode->st_uid); 443 dirnode->inode->st.st_uid = specnode->st_uid; 444 } 445#if HAVE_STRUCT_STAT_ST_FLAGS 446 if (specnode->flags & F_FLAGS) { 447 ASEPRINT("flags", "%#lX", 448 (unsigned long)dirnode->inode->st.st_flags, 449 (unsigned long)specnode->st_flags); 450 dirnode->inode->st.st_flags = specnode->st_flags; 451 } 452#endif 453#undef ASEPRINT 454 455 dirnode->flags |= FSNODE_F_HASSPEC; 456} 457 458 459/* 460 * dump_fsnodes -- 461 * dump the fsnodes from `cur', based in the directory `dir' 462 */ 463void 464dump_fsnodes(const char *dir, fsnode *root) 465{ 466 fsnode *cur; 467 char path[MAXPATHLEN + 1]; 468 469 assert (dir != NULL); 470 printf("dump_fsnodes: %s %p\n", dir, root); 471 for (cur = root; cur != NULL; cur = cur->next) { 472 if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name) 473 >= sizeof(path)) 474 errx(1, "Pathname too long."); 475 476 if (debug & DEBUG_DUMP_FSNODES_VERBOSE) 477 printf("cur=%8p parent=%8p first=%8p ", 478 cur, cur->parent, cur->first); 479 printf("%7s: %s", inode_type(cur->type), path); 480 if (S_ISLNK(cur->type)) { 481 assert(cur->symlink != NULL); 482 printf(" -> %s", cur->symlink); 483 } else { 484 assert (cur->symlink == NULL); 485 } 486 if (cur->inode->nlink > 1) 487 printf(", nlinks=%d", cur->inode->nlink); 488 putchar('\n'); 489 490 if (cur->child) { 491 assert (cur->type == S_IFDIR); 492 dump_fsnodes(path, cur->child); 493 } 494 } 495 printf("dump_fsnodes: finished %s\n", dir); 496} 497 498 499/* 500 * inode_type -- 501 * for a given inode type `mode', return a descriptive string. 502 */ 503const char * 504inode_type(mode_t mode) 505{ 506 507 if (S_ISREG(mode)) 508 return ("file"); 509 if (S_ISLNK(mode)) 510 return ("symlink"); 511 if (S_ISDIR(mode)) 512 return ("dir"); 513 if (S_ISLNK(mode)) 514 return ("link"); 515 if (S_ISFIFO(mode)) 516 return ("fifo"); 517 if (S_ISSOCK(mode)) 518 return ("socket"); 519 /* XXX should not happen but handle them */ 520 if (S_ISCHR(mode)) 521 return ("char"); 522 if (S_ISBLK(mode)) 523 return ("block"); 524 return ("unknown"); 525} 526 527 528/* 529 * link_check -- 530 * return pointer to fsnode matching `entry's st_ino & st_dev if it exists, 531 * otherwise add `entry' to table and return NULL 532 */ 533static fsinode * 534link_check(fsinode *entry) 535{ 536 static struct dupnode { 537 uint32_t dev; 538 uint64_t ino; 539 fsinode *dup; 540 } *dups, *newdups; 541 static int ndups, maxdups; 542 543 int i; 544 545 assert (entry != NULL); 546 547 /* XXX; maybe traverse in reverse for speed? */ 548 for (i = 0; i < ndups; i++) { 549 if (dups[i].dev == entry->st.st_dev && 550 dups[i].ino == entry->st.st_ino) { 551 if (debug & DEBUG_WALK_DIR_LINKCHECK) 552 printf("link_check: found [%d,%d]\n", 553 entry->st.st_dev, entry->st.st_ino); 554 return (dups[i].dup); 555 } 556 } 557 558 if (debug & DEBUG_WALK_DIR_LINKCHECK) 559 printf("link_check: no match for [%d, %d]\n", 560 entry->st.st_dev, entry->st.st_ino); 561 if (ndups == maxdups) { 562 if ((newdups = realloc(dups, sizeof(struct dupnode) * (maxdups + 128))) 563 == NULL) 564 err(1, "Memory allocation error"); 565 dups = newdups; 566 maxdups += 128; 567 } 568 dups[ndups].dev = entry->st.st_dev; 569 dups[ndups].ino = entry->st.st_ino; 570 dups[ndups].dup = entry; 571 ndups++; 572 573 return (NULL); 574} 575