1 /* $NetBSD: walk.c,v 1.8 2002/01/31 22:44:03 tv 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. All advertising materials mentioning features or use of this software 58 * must display the following acknowledgement: 59 * This product includes software developed by the University of 60 * California, Berkeley and its contributors. 61 * 4. Neither the name of the University nor the names of its contributors 62 * may be used to endorse or promote products derived from this software 63 * without specific prior written permission. 64 * 65 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 66 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 68 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 69 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 73 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 74 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 75 * SUCH DAMAGE. 76 */ 77 78 #include <sys/cdefs.h> 79 #if defined(__RCSID) && !defined(__lint) 80 __RCSID("$NetBSD: walk.c,v 1.8 2002/01/31 22:44:03 tv 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 if (S_ISSOCK(stbuf.st_mode & S_IFMT)) { 137 if (debug & DEBUG_WALK_DIR_NODE) 138 printf(" skipping socket %s\n", path); 139 continue; 140 } 141 142 cur = create_fsnode(dent->d_name, &stbuf); 143 cur->parent = parent; 144 if (strcmp(dent->d_name, ".") == 0) { 145 /* ensure "." is at the start of the list */ 146 cur->next = first; 147 first = cur; 148 if (! prev) 149 prev = cur; 150 } else { /* not "." */ 151 if (prev) 152 prev->next = cur; 153 prev = cur; 154 if (!first) 155 first = cur; 156 if (S_ISDIR(cur->type)) { 157 cur->child = walk_dir(path, cur); 158 continue; 159 } 160 } 161 if (stbuf.st_nlink > 1) { 162 fsinode *curino; 163 164 curino = link_check(cur->inode); 165 if (curino != NULL) { 166 free(cur->inode); 167 cur->inode = curino; 168 cur->inode->nlink++; 169 } 170 } 171 if (S_ISLNK(cur->type)) { 172 char slink[PATH_MAX+1]; 173 int llen; 174 175 llen = readlink(path, slink, PATH_MAX); 176 if (llen == -1) 177 err(1, "Readlink `%s'", path); 178 slink[llen] = '\0'; 179 if ((cur->symlink = strdup(slink)) == NULL) 180 err(1, "Memory allocation error"); 181 } 182 } 183 for (cur = first; cur != NULL; cur = cur->next) 184 cur->first = first; 185 if (closedir(dirp) == -1) 186 err(1, "Can't closedir `%s'", dir); 187 return (first); 188 } 189 190 static fsnode * 191 create_fsnode(const char *name, struct stat *stbuf) 192 { 193 fsnode *cur; 194 195 if ((cur = calloc(1, sizeof(fsnode))) == NULL || 196 (cur->name = strdup(name)) == NULL || 197 (cur->inode = calloc(1, sizeof(fsinode))) == NULL) 198 err(1, "Memory allocation error"); 199 cur->type = stbuf->st_mode & S_IFMT; 200 cur->inode->nlink = 1; 201 cur->inode->st = *stbuf; 202 return (cur); 203 } 204 205 /* 206 * apply_specfile -- 207 * read in the mtree(8) specfile, and apply it to the tree 208 * at dir,parent. parameters in parent on equivalent types 209 * will be changed to those found in specfile, and missing 210 * entries will be added. 211 */ 212 void 213 apply_specfile(const char *specfile, const char *dir, fsnode *parent) 214 { 215 struct timeval start; 216 FILE *fp; 217 NODE *root; 218 219 assert(specfile != NULL); 220 assert(parent != NULL); 221 222 if (debug & DEBUG_APPLY_SPECFILE) 223 printf("apply_specfile: %s, %s %p\n", specfile, dir, parent); 224 225 /* read in the specfile */ 226 if ((fp = fopen(specfile, "r")) == NULL) 227 err(1, "Can't open `%s'", specfile); 228 TIMER_START(start); 229 root = spec(fp); 230 TIMER_RESULTS(start, "spec"); 231 if (fclose(fp) == EOF) 232 err(1, "Can't close `%s'", specfile); 233 234 /* perform some sanity checks */ 235 if (root == NULL) 236 errx(1, "Specfile `%s' did not contain a tree", specfile); 237 assert(strcmp(root->name, ".") == 0); 238 assert(root->type == F_DIR); 239 240 /* merge in the changes */ 241 apply_specdir(dir, root, parent); 242 } 243 244 static void 245 apply_specdir(const char *dir, NODE *specnode, fsnode *dirnode) 246 { 247 char path[MAXPATHLEN + 1]; 248 NODE *curnode; 249 fsnode *curfsnode; 250 251 assert(specnode != NULL); 252 assert(dirnode != NULL); 253 254 if (debug & DEBUG_APPLY_SPECFILE) 255 printf("apply_specdir: %s %p %p\n", dir, specnode, dirnode); 256 257 if (specnode->type != F_DIR) 258 errx(1, "Specfile node `%s/%s' is not a directory", 259 dir, specnode->name); 260 if (dirnode->type != S_IFDIR) 261 errx(1, "Directory node `%s/%s' is not a directory", 262 dir, dirnode->name); 263 264 apply_specentry(dir, specnode, dirnode); 265 266 /* now walk specnode->child matching up with dirnode */ 267 for (curnode = specnode->child; curnode != NULL; 268 curnode = curnode->next) { 269 if (debug & DEBUG_APPLY_SPECENTRY) 270 printf("apply_specdir: spec %s\n", 271 curnode->name); 272 for (curfsnode = dirnode->next; curfsnode != NULL; 273 curfsnode = curfsnode->next) { 274 #if 0 /* too verbose for now */ 275 if (debug & DEBUG_APPLY_SPECENTRY) 276 printf("apply_specdir: dirent %s\n", 277 curfsnode->name); 278 #endif 279 if (strcmp(curnode->name, curfsnode->name) == 0) 280 break; 281 } 282 if (curfsnode == NULL) { /* need new entry */ 283 struct stat stbuf; 284 285 /* check that enough info is provided */ 286 #define NODETEST(t, m) \ 287 if (!(t)) \ 288 errx(1, "`%s/%s': %s not provided", \ 289 dir, curnode->name, m) 290 NODETEST(curnode->flags & F_TYPE, "type"); 291 NODETEST(curnode->flags & F_MODE, "mode"); 292 /* XXX: require F_TIME ? */ 293 NODETEST(curnode->flags & F_GID || 294 curnode->flags & F_GNAME, "group"); 295 NODETEST(curnode->flags & F_UID || 296 curnode->flags & F_UNAME, "user"); 297 if (curnode->type == F_BLOCK || curnode->type == F_CHAR) 298 NODETEST(curnode->flags & F_DEV, 299 "device number"); 300 #undef NODETEST 301 302 if (debug & DEBUG_APPLY_SPECFILE) 303 printf("apply_specdir: adding %s\n", 304 curnode->name); 305 /* build minimal fsnode */ 306 memset(&stbuf, 0, sizeof(stbuf)); 307 stbuf.st_mode = nodetoino(curnode->type); 308 stbuf.st_nlink = 1; 309 stbuf.st_mtime = stbuf.st_atime = 310 stbuf.st_ctime = start_time.tv_sec; 311 #if HAVE_STRUCT_STAT_ST_MTIMENSEC 312 stbuf.st_mtimensec = stbuf.st_atimensec = 313 stbuf.st_ctimensec = start_time.tv_nsec; 314 #endif 315 curfsnode = create_fsnode(curnode->name, &stbuf); 316 curfsnode->parent = dirnode->parent; 317 curfsnode->first = dirnode; 318 curfsnode->next = dirnode->next; 319 dirnode->next = curfsnode; 320 if (curfsnode->type == S_IFDIR) { 321 /* for dirs, make "." entry as well */ 322 curfsnode->child = create_fsnode(".", &stbuf); 323 curfsnode->child->parent = curfsnode; 324 curfsnode->child->first = curfsnode->child; 325 } 326 if (curfsnode->type == S_IFLNK) { 327 assert(curnode->slink != NULL); 328 /* for symlinks, copy the target */ 329 if ((curfsnode->symlink = 330 strdup(curnode->slink)) == NULL) 331 err(1, "Memory allocation error"); 332 } 333 } 334 apply_specentry(dir, curnode, curfsnode); 335 if (curnode->type == F_DIR) { 336 if (curfsnode->type != S_IFDIR) 337 errx(1, "`%s/%s' is not a directory", 338 dir, curfsnode->name); 339 assert (curfsnode->child != NULL); 340 if (snprintf(path, sizeof(path), "%s/%s", 341 dir, curnode->name) >= sizeof(path)) 342 errx(1, "Pathname too long."); 343 apply_specdir(path, curnode, curfsnode->child); 344 } 345 } 346 } 347 348 static void 349 apply_specentry(const char *dir, NODE *specnode, fsnode *dirnode) 350 { 351 352 assert(specnode != NULL); 353 assert(dirnode != NULL); 354 355 if (nodetoino(specnode->type) != dirnode->type) 356 errx(1, "`%s/%s' type mismatch: specfile %s, tree %s", 357 dir, specnode->name, inode_type(nodetoino(specnode->type)), 358 inode_type(dirnode->type)); 359 360 if (debug & DEBUG_APPLY_SPECENTRY) 361 printf("apply_specentry: %s/%s\n", dir, dirnode->name); 362 363 #define ASEPRINT(t, b, o, n) \ 364 if (debug & DEBUG_APPLY_SPECENTRY) \ 365 printf("\t\t\tchanging %s from " b " to " b "\n", \ 366 t, o, n) 367 368 if (specnode->flags & (F_GID | F_GNAME)) { 369 ASEPRINT("gid", "%d", 370 dirnode->inode->st.st_gid, specnode->st_gid); 371 dirnode->inode->st.st_gid = specnode->st_gid; 372 } 373 if (specnode->flags & F_MODE) { 374 ASEPRINT("mode", "%#o", 375 dirnode->inode->st.st_mode & ALLPERMS, specnode->st_mode); 376 dirnode->inode->st.st_mode &= ~ALLPERMS; 377 dirnode->inode->st.st_mode |= (specnode->st_mode & ALLPERMS); 378 } 379 /* XXX: ignoring F_NLINK for now */ 380 if (specnode->flags & F_SIZE) { 381 ASEPRINT("size", "%lld", 382 (long long)dirnode->inode->st.st_size, 383 (long long)specnode->st_size); 384 dirnode->inode->st.st_size = specnode->st_size; 385 } 386 if (specnode->flags & F_SLINK) { 387 assert(dirnode->symlink != NULL); 388 assert(specnode->slink != NULL); 389 ASEPRINT("symlink", "%s", dirnode->symlink, specnode->slink); 390 free(dirnode->symlink); 391 if ((dirnode->symlink = strdup(specnode->slink)) == NULL) 392 err(1, "Memory allocation error"); 393 } 394 if (specnode->flags & F_TIME) { 395 ASEPRINT("time", "%ld", 396 (long)dirnode->inode->st.st_mtime, 397 (long)specnode->st_mtimespec.tv_sec); 398 dirnode->inode->st.st_mtime = specnode->st_mtimespec.tv_sec; 399 dirnode->inode->st.st_atime = specnode->st_mtimespec.tv_sec; 400 dirnode->inode->st.st_ctime = start_time.tv_sec; 401 #if HAVE_STRUCT_STAT_ST_MTIMENSEC 402 dirnode->inode->st.st_mtimensec = specnode->st_mtimensec; 403 dirnode->inode->st.st_atimensec = specnode->st_mtimensec; 404 dirnode->inode->st.st_ctimensec = start_time.tv_nsec; 405 #endif 406 } 407 if (specnode->flags & (F_UID | F_UNAME)) { 408 ASEPRINT("uid", "%d", 409 dirnode->inode->st.st_uid, specnode->st_uid); 410 dirnode->inode->st.st_uid = specnode->st_uid; 411 } 412 #if HAVE_STRUCT_STAT_ST_FLAGS 413 if (specnode->flags & F_FLAGS) { 414 ASEPRINT("flags", "%#lX", 415 (ulong)dirnode->inode->st.st_flags, 416 (ulong)specnode->st_flags); 417 dirnode->inode->st.st_flags = specnode->st_flags; 418 } 419 #endif 420 if (specnode->flags & F_DEV) { 421 ASEPRINT("rdev", "%#x", 422 dirnode->inode->st.st_rdev, specnode->st_rdev); 423 dirnode->inode->st.st_rdev = specnode->st_rdev; 424 } 425 #undef ASEPRINT 426 } 427 428 429 /* 430 * dump_fsnodes -- 431 * dump the fsnodes from `cur', based in the directory `dir' 432 */ 433 void 434 dump_fsnodes(const char *dir, fsnode *root) 435 { 436 fsnode *cur; 437 char path[MAXPATHLEN + 1]; 438 439 assert (dir != NULL); 440 printf("dump_fsnodes: %s %p\n", dir, root); 441 for (cur = root; cur != NULL; cur = cur->next) { 442 if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name) 443 >= sizeof(path)) 444 errx(1, "Pathname too long."); 445 446 if (debug & DEBUG_DUMP_FSNODES_VERBOSE) 447 printf("cur=%8p parent=%8p first=%8p ", 448 cur, cur->parent, cur->first); 449 printf("%7s: %s", inode_type(cur->type), path); 450 if (S_ISLNK(cur->type)) { 451 assert(cur->symlink != NULL); 452 printf(" -> %s", cur->symlink); 453 } else { 454 assert (cur->symlink == NULL); 455 } 456 if (cur->inode->nlink > 1) 457 printf(", nlinks=%d", cur->inode->nlink); 458 putchar('\n'); 459 460 if (cur->child) { 461 assert (cur->type == S_IFDIR); 462 dump_fsnodes(path, cur->child); 463 } 464 } 465 printf("dump_fsnodes: finished %s\n", dir); 466 } 467 468 469 /* 470 * inode_type -- 471 * for a given inode type `mode', return a descriptive string. 472 * for most cases, uses inotype() from mtree/misc.c 473 */ 474 const char * 475 inode_type(mode_t mode) 476 { 477 478 if (S_ISLNK(mode)) 479 return ("symlink"); /* inotype() returns "link"... */ 480 return (inotype(mode)); 481 } 482 483 484 /* 485 * link_check -- 486 * return pointer to fsnode matching `entry's st_ino & st_dev if it exists, 487 * otherwise add `entry' to table and return NULL 488 */ 489 static fsinode * 490 link_check(fsinode *entry) 491 { 492 static struct dupnode { 493 uint32_t dev; 494 uint32_t ino; 495 fsinode *dup; 496 } *dups; 497 static int ndups, maxdups; 498 499 int i; 500 501 assert (entry != NULL); 502 503 /* XXX; maybe traverse in reverse for speed? */ 504 for (i = 0; i < ndups; i++) { 505 if (dups[i].dev == entry->st.st_dev && 506 dups[i].ino == entry->st.st_ino) { 507 if (debug & DEBUG_WALK_DIR_LINKCHECK) 508 printf("link_check: found [%d,%d]\n", 509 entry->st.st_dev, entry->st.st_ino); 510 return (dups[i].dup); 511 } 512 } 513 514 if (debug & DEBUG_WALK_DIR_LINKCHECK) 515 printf("link_check: no match for [%d, %d]\n", 516 entry->st.st_dev, entry->st.st_ino); 517 if (ndups == maxdups) { 518 maxdups += 128; 519 if ((dups = realloc(dups, sizeof(struct dupnode) * maxdups)) 520 == NULL) 521 err(1, "Memory allocation error"); 522 } 523 dups[ndups].dev = entry->st.st_dev; 524 dups[ndups].ino = entry->st.st_ino; 525 dups[ndups].dup = entry; 526 ndups++; 527 528 return (NULL); 529 } 530