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