1 /* $NetBSD: walk.c,v 1.19 2006/02/01 22:19:34 dyoung 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.19 2006/02/01 22:19:34 dyoung 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 #include <sys/stat.h> 94 95 #include "makefs.h" 96 #include "mtree.h" 97 98 static void apply_specdir(const char *, NODE *, fsnode *); 99 static void apply_specentry(const char *, NODE *, fsnode *); 100 static fsnode *create_fsnode(const char *, struct stat *); 101 static fsinode *link_check(fsinode *); 102 103 104 /* 105 * walk_dir -- 106 * build a tree of fsnodes from `dir', with a parent fsnode of `parent' 107 * (which may be NULL for the root of the tree). 108 * each "level" is a directory, with the "." entry guaranteed to be 109 * at the start of the list, and without ".." entries. 110 */ 111 fsnode * 112 walk_dir(const char *dir, fsnode *parent) 113 { 114 fsnode *first, *cur, *prev; 115 DIR *dirp; 116 struct dirent *dent; 117 char path[MAXPATHLEN + 1]; 118 struct stat stbuf; 119 120 assert(dir != NULL); 121 122 if (debug & DEBUG_WALK_DIR) 123 printf("walk_dir: %s %p\n", dir, parent); 124 if ((dirp = opendir(dir)) == NULL) 125 err(1, "Can't opendir `%s'", dir); 126 first = prev = NULL; 127 while ((dent = readdir(dirp)) != NULL) { 128 if (strcmp(dent->d_name, "..") == 0) 129 continue; 130 if (debug & DEBUG_WALK_DIR_NODE) 131 printf("scanning %s/%s\n", dir, dent->d_name); 132 if (snprintf(path, sizeof(path), "%s/%s", dir, dent->d_name) 133 >= sizeof(path)) 134 errx(1, "Pathname too long."); 135 if (lstat(path, &stbuf) == -1) 136 err(1, "Can't lstat `%s'", path); 137 #ifdef S_ISSOCK 138 if (S_ISSOCK(stbuf.st_mode & S_IFMT)) { 139 if (debug & DEBUG_WALK_DIR_NODE) 140 printf(" skipping socket %s\n", path); 141 continue; 142 } 143 #endif 144 145 cur = create_fsnode(dent->d_name, &stbuf); 146 cur->parent = parent; 147 if (strcmp(dent->d_name, ".") == 0) { 148 /* ensure "." is at the start of the list */ 149 cur->next = first; 150 first = cur; 151 if (! prev) 152 prev = cur; 153 } else { /* not "." */ 154 if (prev) 155 prev->next = cur; 156 prev = cur; 157 if (!first) 158 first = cur; 159 if (S_ISDIR(cur->type)) { 160 cur->child = walk_dir(path, cur); 161 continue; 162 } 163 } 164 if (stbuf.st_nlink > 1) { 165 fsinode *curino; 166 167 curino = link_check(cur->inode); 168 if (curino != NULL) { 169 free(cur->inode); 170 cur->inode = curino; 171 cur->inode->nlink++; 172 } 173 } 174 if (S_ISLNK(cur->type)) { 175 char slink[PATH_MAX+1]; 176 int llen; 177 178 llen = readlink(path, slink, sizeof(slink) - 1); 179 if (llen == -1) 180 err(1, "Readlink `%s'", path); 181 slink[llen] = '\0'; 182 if ((cur->symlink = strdup(slink)) == NULL) 183 err(1, "Memory allocation error"); 184 } 185 } 186 for (cur = first; cur != NULL; cur = cur->next) 187 cur->first = first; 188 if (closedir(dirp) == -1) 189 err(1, "Can't closedir `%s'", dir); 190 return (first); 191 } 192 193 static fsnode * 194 create_fsnode(const char *name, struct stat *stbuf) 195 { 196 fsnode *cur; 197 198 if ((cur = calloc(1, sizeof(fsnode))) == NULL || 199 (cur->name = strdup(name)) == NULL || 200 (cur->inode = calloc(1, sizeof(fsinode))) == NULL) 201 err(1, "Memory allocation error"); 202 cur->type = stbuf->st_mode & S_IFMT; 203 cur->inode->nlink = 1; 204 cur->inode->st = *stbuf; 205 return (cur); 206 } 207 208 /* 209 * apply_specfile -- 210 * read in the mtree(8) specfile, and apply it to the tree 211 * at dir,parent. parameters in parent on equivalent types 212 * will be changed to those found in specfile, and missing 213 * entries will be added. 214 */ 215 void 216 apply_specfile(const char *specfile, const char *dir, fsnode *parent) 217 { 218 struct timeval start; 219 FILE *fp; 220 NODE *root; 221 222 assert(specfile != NULL); 223 assert(parent != NULL); 224 225 if (debug & DEBUG_APPLY_SPECFILE) 226 printf("apply_specfile: %s, %s %p\n", specfile, dir, parent); 227 228 /* read in the specfile */ 229 if ((fp = fopen(specfile, "r")) == NULL) 230 err(1, "Can't open `%s'", specfile); 231 TIMER_START(start); 232 root = spec(fp); 233 TIMER_RESULTS(start, "spec"); 234 if (fclose(fp) == EOF) 235 err(1, "Can't close `%s'", specfile); 236 237 /* perform some sanity checks */ 238 if (root == NULL) 239 errx(1, "Specfile `%s' did not contain a tree", specfile); 240 assert(strcmp(root->name, ".") == 0); 241 assert(root->type == F_DIR); 242 243 /* merge in the changes */ 244 apply_specdir(dir, root, parent); 245 } 246 247 static void 248 apply_specdir(const char *dir, NODE *specnode, fsnode *dirnode) 249 { 250 char path[MAXPATHLEN + 1]; 251 NODE *curnode; 252 fsnode *curfsnode; 253 254 assert(specnode != NULL); 255 assert(dirnode != NULL); 256 257 if (debug & DEBUG_APPLY_SPECFILE) 258 printf("apply_specdir: %s %p %p\n", dir, specnode, dirnode); 259 260 if (specnode->type != F_DIR) 261 errx(1, "Specfile node `%s/%s' is not a directory", 262 dir, specnode->name); 263 if (dirnode->type != S_IFDIR) 264 errx(1, "Directory node `%s/%s' is not a directory", 265 dir, dirnode->name); 266 267 apply_specentry(dir, specnode, dirnode); 268 269 /* now walk specnode->child matching up with dirnode */ 270 for (curnode = specnode->child; curnode != NULL; 271 curnode = curnode->next) { 272 if (debug & DEBUG_APPLY_SPECENTRY) 273 printf("apply_specdir: spec %s\n", 274 curnode->name); 275 for (curfsnode = dirnode->next; curfsnode != NULL; 276 curfsnode = curfsnode->next) { 277 #if 0 /* too verbose for now */ 278 if (debug & DEBUG_APPLY_SPECENTRY) 279 printf("apply_specdir: dirent %s\n", 280 curfsnode->name); 281 #endif 282 if (strcmp(curnode->name, curfsnode->name) == 0) 283 break; 284 } 285 if (snprintf(path, sizeof(path), "%s/%s", 286 dir, curnode->name) >= sizeof(path)) 287 errx(1, "Pathname too long."); 288 if (curfsnode == NULL) { /* need new entry */ 289 struct stat stbuf; 290 291 /* 292 * don't add optional spec entries 293 * that lack an existing fs entry 294 */ 295 if ((curnode->flags & F_OPT) && 296 lstat(path, &stbuf) == -1) 297 continue; 298 299 /* check that enough info is provided */ 300 #define NODETEST(t, m) \ 301 if (!(t)) \ 302 errx(1, "`%s': %s not provided", path, m) 303 NODETEST(curnode->flags & F_TYPE, "type"); 304 NODETEST(curnode->flags & F_MODE, "mode"); 305 /* XXX: require F_TIME ? */ 306 NODETEST(curnode->flags & F_GID || 307 curnode->flags & F_GNAME, "group"); 308 NODETEST(curnode->flags & F_UID || 309 curnode->flags & F_UNAME, "user"); 310 if (curnode->type == F_BLOCK || curnode->type == F_CHAR) 311 NODETEST(curnode->flags & F_DEV, 312 "device number"); 313 #undef NODETEST 314 315 if (debug & DEBUG_APPLY_SPECFILE) 316 printf("apply_specdir: adding %s\n", 317 curnode->name); 318 /* build minimal fsnode */ 319 memset(&stbuf, 0, sizeof(stbuf)); 320 stbuf.st_mode = nodetoino(curnode->type); 321 stbuf.st_nlink = 1; 322 stbuf.st_mtime = stbuf.st_atime = 323 stbuf.st_ctime = start_time.tv_sec; 324 #if HAVE_STRUCT_STAT_ST_MTIMENSEC 325 stbuf.st_mtimensec = stbuf.st_atimensec = 326 stbuf.st_ctimensec = start_time.tv_nsec; 327 #endif 328 curfsnode = create_fsnode(curnode->name, &stbuf); 329 curfsnode->parent = dirnode->parent; 330 curfsnode->first = dirnode; 331 curfsnode->next = dirnode->next; 332 dirnode->next = curfsnode; 333 if (curfsnode->type == S_IFDIR) { 334 /* for dirs, make "." entry as well */ 335 curfsnode->child = create_fsnode(".", &stbuf); 336 curfsnode->child->parent = curfsnode; 337 curfsnode->child->first = curfsnode->child; 338 } 339 if (curfsnode->type == S_IFLNK) { 340 assert(curnode->slink != NULL); 341 /* for symlinks, copy the target */ 342 if ((curfsnode->symlink = 343 strdup(curnode->slink)) == NULL) 344 err(1, "Memory allocation error"); 345 } 346 } 347 apply_specentry(dir, curnode, curfsnode); 348 if (curnode->type == F_DIR) { 349 if (curfsnode->type != S_IFDIR) 350 errx(1, "`%s' is not a directory", path); 351 assert (curfsnode->child != NULL); 352 apply_specdir(path, curnode, curfsnode->child); 353 } 354 } 355 } 356 357 static void 358 apply_specentry(const char *dir, NODE *specnode, fsnode *dirnode) 359 { 360 361 assert(specnode != NULL); 362 assert(dirnode != NULL); 363 364 if (nodetoino(specnode->type) != dirnode->type) 365 errx(1, "`%s/%s' type mismatch: specfile %s, tree %s", 366 dir, specnode->name, inode_type(nodetoino(specnode->type)), 367 inode_type(dirnode->type)); 368 369 if (debug & DEBUG_APPLY_SPECENTRY) 370 printf("apply_specentry: %s/%s\n", dir, dirnode->name); 371 372 #define ASEPRINT(t, b, o, n) \ 373 if (debug & DEBUG_APPLY_SPECENTRY) \ 374 printf("\t\t\tchanging %s from " b " to " b "\n", \ 375 t, o, n) 376 377 if (specnode->flags & (F_GID | F_GNAME)) { 378 ASEPRINT("gid", "%d", 379 dirnode->inode->st.st_gid, specnode->st_gid); 380 dirnode->inode->st.st_gid = specnode->st_gid; 381 } 382 if (specnode->flags & F_MODE) { 383 ASEPRINT("mode", "%#o", 384 dirnode->inode->st.st_mode & ALLPERMS, specnode->st_mode); 385 dirnode->inode->st.st_mode &= ~ALLPERMS; 386 dirnode->inode->st.st_mode |= (specnode->st_mode & ALLPERMS); 387 } 388 /* XXX: ignoring F_NLINK for now */ 389 if (specnode->flags & F_SIZE) { 390 ASEPRINT("size", "%lld", 391 (long long)dirnode->inode->st.st_size, 392 (long long)specnode->st_size); 393 dirnode->inode->st.st_size = specnode->st_size; 394 } 395 if (specnode->flags & F_SLINK) { 396 assert(dirnode->symlink != NULL); 397 assert(specnode->slink != NULL); 398 ASEPRINT("symlink", "%s", dirnode->symlink, specnode->slink); 399 free(dirnode->symlink); 400 if ((dirnode->symlink = strdup(specnode->slink)) == NULL) 401 err(1, "Memory allocation error"); 402 } 403 if (specnode->flags & F_TIME) { 404 ASEPRINT("time", "%ld", 405 (long)dirnode->inode->st.st_mtime, 406 (long)specnode->st_mtimespec.tv_sec); 407 dirnode->inode->st.st_mtime = specnode->st_mtimespec.tv_sec; 408 dirnode->inode->st.st_atime = specnode->st_mtimespec.tv_sec; 409 dirnode->inode->st.st_ctime = start_time.tv_sec; 410 #if HAVE_STRUCT_STAT_ST_MTIMENSEC 411 dirnode->inode->st.st_mtimensec = specnode->st_mtimespec.tv_nsec; 412 dirnode->inode->st.st_atimensec = specnode->st_mtimespec.tv_nsec; 413 dirnode->inode->st.st_ctimensec = start_time.tv_nsec; 414 #endif 415 } 416 if (specnode->flags & (F_UID | F_UNAME)) { 417 ASEPRINT("uid", "%d", 418 dirnode->inode->st.st_uid, specnode->st_uid); 419 dirnode->inode->st.st_uid = specnode->st_uid; 420 } 421 #if HAVE_STRUCT_STAT_ST_FLAGS 422 if (specnode->flags & F_FLAGS) { 423 ASEPRINT("flags", "%#lX", 424 (unsigned long)dirnode->inode->st.st_flags, 425 (unsigned long)specnode->st_flags); 426 dirnode->inode->st.st_flags = specnode->st_flags; 427 } 428 #endif 429 if (specnode->flags & F_DEV) { 430 ASEPRINT("rdev", "%#x", 431 dirnode->inode->st.st_rdev, specnode->st_rdev); 432 dirnode->inode->st.st_rdev = specnode->st_rdev; 433 } 434 #undef ASEPRINT 435 436 dirnode->flags |= FSNODE_F_HASSPEC; 437 } 438 439 440 /* 441 * dump_fsnodes -- 442 * dump the fsnodes from `cur', based in the directory `dir' 443 */ 444 void 445 dump_fsnodes(const char *dir, fsnode *root) 446 { 447 fsnode *cur; 448 char path[MAXPATHLEN + 1]; 449 450 assert (dir != NULL); 451 printf("dump_fsnodes: %s %p\n", dir, root); 452 for (cur = root; cur != NULL; cur = cur->next) { 453 if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name) 454 >= sizeof(path)) 455 errx(1, "Pathname too long."); 456 457 if (debug & DEBUG_DUMP_FSNODES_VERBOSE) 458 printf("cur=%8p parent=%8p first=%8p ", 459 cur, cur->parent, cur->first); 460 printf("%7s: %s", inode_type(cur->type), path); 461 if (S_ISLNK(cur->type)) { 462 assert(cur->symlink != NULL); 463 printf(" -> %s", cur->symlink); 464 } else { 465 assert (cur->symlink == NULL); 466 } 467 if (cur->inode->nlink > 1) 468 printf(", nlinks=%d", cur->inode->nlink); 469 putchar('\n'); 470 471 if (cur->child) { 472 assert (cur->type == S_IFDIR); 473 dump_fsnodes(path, cur->child); 474 } 475 } 476 printf("dump_fsnodes: finished %s\n", dir); 477 } 478 479 480 /* 481 * inode_type -- 482 * for a given inode type `mode', return a descriptive string. 483 * for most cases, uses inotype() from mtree/misc.c 484 */ 485 const char * 486 inode_type(mode_t mode) 487 { 488 489 if (S_ISLNK(mode)) 490 return ("symlink"); /* inotype() returns "link"... */ 491 return (inotype(mode)); 492 } 493 494 495 /* 496 * link_check -- 497 * return pointer to fsnode matching `entry's st_ino & st_dev if it exists, 498 * otherwise add `entry' to table and return NULL 499 */ 500 static fsinode * 501 link_check(fsinode *entry) 502 { 503 static struct dupnode { 504 uint32_t dev; 505 uint64_t ino; 506 fsinode *dup; 507 } *dups, *newdups; 508 static int ndups, maxdups; 509 510 int i; 511 512 assert (entry != NULL); 513 514 /* XXX; maybe traverse in reverse for speed? */ 515 for (i = 0; i < ndups; i++) { 516 if (dups[i].dev == entry->st.st_dev && 517 dups[i].ino == entry->st.st_ino) { 518 if (debug & DEBUG_WALK_DIR_LINKCHECK) 519 printf("link_check: found [%u, %llu]\n", 520 entry->st.st_dev, 521 (unsigned long long)entry->st.st_ino); 522 return (dups[i].dup); 523 } 524 } 525 526 if (debug & DEBUG_WALK_DIR_LINKCHECK) 527 printf("link_check: no match for [%u, %llu]\n", 528 entry->st.st_dev, (unsigned long long)entry->st.st_ino); 529 if (ndups == maxdups) { 530 if ((newdups = realloc(dups, sizeof(struct dupnode) * (maxdups + 128))) 531 == NULL) 532 err(1, "Memory allocation error"); 533 dups = newdups; 534 maxdups += 128; 535 } 536 dups[ndups].dev = entry->st.st_dev; 537 dups[ndups].ino = entry->st.st_ino; 538 dups[ndups].dup = entry; 539 ndups++; 540 541 return (NULL); 542 } 543