1 /* $NetBSD: walk.c,v 1.24 2008/12/28 21:51:46 christos Exp $ */
4 * Copyright (c) 2001 Wasabi Systems, Inc.
7 * Written by Luke Mewburn for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
58 static void apply_specdir(const char *, NODE *, fsnode *, int);
59 static void apply_specentry(const char *, NODE *, fsnode *);
60 static fsnode *create_fsnode(const char *, struct stat *);
61 static fsinode *link_check(fsinode *);
66 * build a tree of fsnodes from `dir', with a parent fsnode of `parent'
67 * (which may be NULL for the root of the tree).
68 * each "level" is a directory, with the "." entry guaranteed to be
69 * at the start of the list, and without ".." entries.
72 walk_dir(const char *dir, fsnode *parent)
74 fsnode *first, *cur, *prev;
77 char path[MAXPATHLEN + 1];
82 if (debug & DEBUG_WALK_DIR)
83 printf("walk_dir: %s %p\n", dir, parent);
84 if ((dirp = opendir(dir)) == NULL)
85 err(1, "Can't opendir `%s'", dir);
87 while ((dent = readdir(dirp)) != NULL) {
88 if (strcmp(dent->d_name, "..") == 0)
90 if (debug & DEBUG_WALK_DIR_NODE)
91 printf("scanning %s/%s\n", dir, dent->d_name);
92 if (snprintf(path, sizeof(path), "%s/%s", dir, dent->d_name)
94 errx(1, "Pathname too long.");
95 if (lstat(path, &stbuf) == -1)
96 err(1, "Can't lstat `%s'", path);
98 if (S_ISSOCK(stbuf.st_mode & S_IFMT)) {
99 if (debug & DEBUG_WALK_DIR_NODE)
100 printf(" skipping socket %s\n", path);
105 cur = create_fsnode(dent->d_name, &stbuf);
106 cur->parent = parent;
107 if (strcmp(dent->d_name, ".") == 0) {
108 /* ensure "." is at the start of the list */
113 } else { /* not "." */
119 if (S_ISDIR(cur->type)) {
120 cur->child = walk_dir(path, cur);
124 if (stbuf.st_nlink > 1) {
127 curino = link_check(cur->inode);
128 if (curino != NULL) {
132 if (debug & DEBUG_WALK_DIR_LINKCHECK)
133 printf("link_check: found [%llu, %llu]\n",
134 (unsigned long long)curino->st.st_dev,
135 (unsigned long long)curino->st.st_ino);
138 if (S_ISLNK(cur->type)) {
139 char slink[PATH_MAX+1];
142 llen = readlink(path, slink, sizeof(slink) - 1);
144 err(1, "Readlink `%s'", path);
146 if ((cur->symlink = strdup(slink)) == NULL)
147 err(1, "Memory allocation error");
150 for (cur = first; cur != NULL; cur = cur->next)
152 if (closedir(dirp) == -1)
153 err(1, "Can't closedir `%s'", dir);
158 create_fsnode(const char *name, struct stat *stbuf)
162 if ((cur = calloc(1, sizeof(fsnode))) == NULL ||
163 (cur->name = strdup(name)) == NULL ||
164 (cur->inode = calloc(1, sizeof(fsinode))) == NULL)
165 err(1, "Memory allocation error");
166 cur->type = stbuf->st_mode & S_IFMT;
167 cur->inode->nlink = 1;
168 cur->inode->st = *stbuf;
174 * Removes node from tree and frees it and all of
178 free_fsnodes(fsnode *node)
182 assert(node != NULL);
184 /* for ".", start with actual parent node */
185 if (node->first == node) {
186 assert(node->name[0] == '.' && node->name[1] == '\0');
188 assert(node->parent->child == node);
193 /* Find ourselves in our sibling list and unlink */
194 if (node->first != node) {
195 for (cur = node->first; cur->next; cur = cur->next) {
196 if (cur->next == node) {
197 cur->next = node->next;
204 for (cur = node; cur != NULL; cur = next) {
207 cur->child->parent = NULL;
208 free_fsnodes(cur->child);
210 if (cur->inode->nlink-- == 1)
221 * read in the mtree(8) specfile, and apply it to the tree
222 * at dir,parent. parameters in parent on equivalent types
223 * will be changed to those found in specfile, and missing
224 * entries will be added.
227 apply_specfile(const char *specfile, const char *dir, fsnode *parent, int speconly)
229 struct timeval start;
233 assert(specfile != NULL);
234 assert(parent != NULL);
236 if (debug & DEBUG_APPLY_SPECFILE)
237 printf("apply_specfile: %s, %s %p\n", specfile, dir, parent);
239 /* read in the specfile */
240 if ((fp = fopen(specfile, "r")) == NULL)
241 err(1, "Can't open `%s'", specfile);
243 root = mtree_readspec(fp);
244 TIMER_RESULTS(start, "spec");
245 if (fclose(fp) == EOF)
246 err(1, "Can't close `%s'", specfile);
248 /* perform some sanity checks */
250 errx(1, "Specfile `%s' did not contain a tree", specfile);
251 assert(strcmp(root->name, ".") == 0);
252 assert(root->type == F_DIR);
254 /* merge in the changes */
255 apply_specdir(dir, root, parent, speconly);
260 nodetoino(u_int type)
279 printf("unknown type %d", type);
287 apply_specdir(const char *dir, NODE *specnode, fsnode *dirnode, int speconly)
289 char path[MAXPATHLEN + 1];
293 assert(specnode != NULL);
294 assert(dirnode != NULL);
296 if (debug & DEBUG_APPLY_SPECFILE)
297 printf("apply_specdir: %s %p %p\n", dir, specnode, dirnode);
299 if (specnode->type != F_DIR)
300 errx(1, "Specfile node `%s/%s' is not a directory",
301 dir, specnode->name);
302 if (dirnode->type != S_IFDIR)
303 errx(1, "Directory node `%s/%s' is not a directory",
306 apply_specentry(dir, specnode, dirnode);
308 /* Remove any filesystem nodes not found in specfile */
309 /* XXX inefficient. This is O^2 in each dir and it would
310 * have been better never to have walked this part of the tree
315 assert(dirnode->name[0] == '.' && dirnode->name[1] == '\0');
316 for (curfsnode = dirnode->next; curfsnode != NULL; curfsnode = next) {
317 next = curfsnode->next;
318 for (curnode = specnode->child; curnode != NULL;
319 curnode = curnode->next) {
320 if (strcmp(curnode->name, curfsnode->name) == 0)
323 if (curnode == NULL) {
324 if (debug & DEBUG_APPLY_SPECONLY) {
325 printf("apply_specdir: trimming %s/%s %p\n", dir, curfsnode->name, curfsnode);
327 free_fsnodes(curfsnode);
332 /* now walk specnode->child matching up with dirnode */
333 for (curnode = specnode->child; curnode != NULL;
334 curnode = curnode->next) {
335 if (debug & DEBUG_APPLY_SPECENTRY)
336 printf("apply_specdir: spec %s\n",
338 for (curfsnode = dirnode->next; curfsnode != NULL;
339 curfsnode = curfsnode->next) {
340 #if 0 /* too verbose for now */
341 if (debug & DEBUG_APPLY_SPECENTRY)
342 printf("apply_specdir: dirent %s\n",
345 if (strcmp(curnode->name, curfsnode->name) == 0)
348 if (snprintf(path, sizeof(path), "%s/%s",
349 dir, curnode->name) >= sizeof(path))
350 errx(1, "Pathname too long.");
351 if (curfsnode == NULL) { /* need new entry */
355 * don't add optional spec entries
356 * that lack an existing fs entry
358 if ((curnode->flags & F_OPT) &&
359 lstat(path, &stbuf) == -1)
362 /* check that enough info is provided */
363 #define NODETEST(t, m) \
365 errx(1, "`%s': %s not provided", path, m)
366 NODETEST(curnode->flags & F_TYPE, "type");
367 NODETEST(curnode->flags & F_MODE, "mode");
368 /* XXX: require F_TIME ? */
369 NODETEST(curnode->flags & F_GID ||
370 curnode->flags & F_GNAME, "group");
371 NODETEST(curnode->flags & F_UID ||
372 curnode->flags & F_UNAME, "user");
373 /* if (curnode->type == F_BLOCK || curnode->type == F_CHAR)
374 NODETEST(curnode->flags & F_DEV,
378 if (debug & DEBUG_APPLY_SPECFILE)
379 printf("apply_specdir: adding %s\n",
381 /* build minimal fsnode */
382 memset(&stbuf, 0, sizeof(stbuf));
383 stbuf.st_mode = nodetoino(curnode->type);
385 stbuf.st_mtime = stbuf.st_atime =
386 stbuf.st_ctime = start_time.tv_sec;
387 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
388 stbuf.st_mtimensec = stbuf.st_atimensec =
389 stbuf.st_ctimensec = start_time.tv_nsec;
391 curfsnode = create_fsnode(curnode->name, &stbuf);
392 curfsnode->parent = dirnode->parent;
393 curfsnode->first = dirnode;
394 curfsnode->next = dirnode->next;
395 dirnode->next = curfsnode;
396 if (curfsnode->type == S_IFDIR) {
397 /* for dirs, make "." entry as well */
398 curfsnode->child = create_fsnode(".", &stbuf);
399 curfsnode->child->parent = curfsnode;
400 curfsnode->child->first = curfsnode->child;
402 if (curfsnode->type == S_IFLNK) {
403 assert(curnode->slink != NULL);
404 /* for symlinks, copy the target */
405 if ((curfsnode->symlink =
406 strdup(curnode->slink)) == NULL)
407 err(1, "Memory allocation error");
410 apply_specentry(dir, curnode, curfsnode);
411 if (curnode->type == F_DIR) {
412 if (curfsnode->type != S_IFDIR)
413 errx(1, "`%s' is not a directory", path);
414 assert (curfsnode->child != NULL);
415 apply_specdir(path, curnode, curfsnode->child, speconly);
421 apply_specentry(const char *dir, NODE *specnode, fsnode *dirnode)
424 assert(specnode != NULL);
425 assert(dirnode != NULL);
427 if (nodetoino(specnode->type) != dirnode->type)
428 errx(1, "`%s/%s' type mismatch: specfile %s, tree %s",
429 dir, specnode->name, inode_type(nodetoino(specnode->type)),
430 inode_type(dirnode->type));
432 if (debug & DEBUG_APPLY_SPECENTRY)
433 printf("apply_specentry: %s/%s\n", dir, dirnode->name);
435 #define ASEPRINT(t, b, o, n) \
436 if (debug & DEBUG_APPLY_SPECENTRY) \
437 printf("\t\t\tchanging %s from " b " to " b "\n", \
440 if (specnode->flags & (F_GID | F_GNAME)) {
441 ASEPRINT("gid", "%d",
442 dirnode->inode->st.st_gid, specnode->st_gid);
443 dirnode->inode->st.st_gid = specnode->st_gid;
445 if (specnode->flags & F_MODE) {
446 ASEPRINT("mode", "%#o",
447 dirnode->inode->st.st_mode & ALLPERMS, specnode->st_mode);
448 dirnode->inode->st.st_mode &= ~ALLPERMS;
449 dirnode->inode->st.st_mode |= (specnode->st_mode & ALLPERMS);
451 /* XXX: ignoring F_NLINK for now */
452 if (specnode->flags & F_SIZE) {
453 ASEPRINT("size", "%lld",
454 (long long)dirnode->inode->st.st_size,
455 (long long)specnode->st_size);
456 dirnode->inode->st.st_size = specnode->st_size;
458 if (specnode->flags & F_SLINK) {
459 assert(dirnode->symlink != NULL);
460 assert(specnode->slink != NULL);
461 ASEPRINT("symlink", "%s", dirnode->symlink, specnode->slink);
462 free(dirnode->symlink);
463 if ((dirnode->symlink = strdup(specnode->slink)) == NULL)
464 err(1, "Memory allocation error");
466 if (specnode->flags & F_TIME) {
467 ASEPRINT("time", "%ld",
468 (long)dirnode->inode->st.st_mtime,
469 (long)specnode->st_mtimespec.tv_sec);
470 dirnode->inode->st.st_mtime = specnode->st_mtimespec.tv_sec;
471 dirnode->inode->st.st_atime = specnode->st_mtimespec.tv_sec;
472 dirnode->inode->st.st_ctime = start_time.tv_sec;
473 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
474 dirnode->inode->st.st_mtimensec = specnode->st_mtimespec.tv_nsec;
475 dirnode->inode->st.st_atimensec = specnode->st_mtimespec.tv_nsec;
476 dirnode->inode->st.st_ctimensec = start_time.tv_nsec;
479 if (specnode->flags & (F_UID | F_UNAME)) {
480 ASEPRINT("uid", "%d",
481 dirnode->inode->st.st_uid, specnode->st_uid);
482 dirnode->inode->st.st_uid = specnode->st_uid;
484 #if HAVE_STRUCT_STAT_ST_FLAGS
485 if (specnode->flags & F_FLAGS) {
486 ASEPRINT("flags", "%#lX",
487 (unsigned long)dirnode->inode->st.st_flags,
488 (unsigned long)specnode->st_flags);
489 dirnode->inode->st.st_flags = specnode->st_flags;
492 /* if (specnode->flags & F_DEV) {
493 ASEPRINT("rdev", "%#llx",
494 (unsigned long long)dirnode->inode->st.st_rdev,
495 (unsigned long long)specnode->st_rdev);
496 dirnode->inode->st.st_rdev = specnode->st_rdev;
500 dirnode->flags |= FSNODE_F_HASSPEC;
506 * dump the fsnodes from `cur', based in the directory `dir'
509 dump_fsnodes(const char *dir, fsnode *root)
512 char path[MAXPATHLEN + 1];
514 assert (dir != NULL);
515 printf("dump_fsnodes: %s %p\n", dir, root);
516 for (cur = root; cur != NULL; cur = cur->next) {
517 if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name)
519 errx(1, "Pathname too long.");
521 if (debug & DEBUG_DUMP_FSNODES_VERBOSE)
522 printf("cur=%8p parent=%8p first=%8p ",
523 cur, cur->parent, cur->first);
524 printf("%7s: %s", inode_type(cur->type), path);
525 if (S_ISLNK(cur->type)) {
526 assert(cur->symlink != NULL);
527 printf(" -> %s", cur->symlink);
529 assert (cur->symlink == NULL);
531 if (cur->inode->nlink > 1)
532 printf(", nlinks=%d", cur->inode->nlink);
536 assert (cur->type == S_IFDIR);
537 dump_fsnodes(path, cur->child);
540 printf("dump_fsnodes: finished %s\n", dir);
546 * for a given inode type `mode', return a descriptive string.
547 * for most cases, uses inotype() from mtree/misc.c
550 inode_type(mode_t mode)
564 /* XXX should not happen but handle them */
575 * return pointer to fsinode matching `entry's st_ino & st_dev if it exists,
576 * otherwise add `entry' to table and return NULL
578 /* This was borrowed from du.c and tweaked to keep an fsnode
579 * pointer instead. -- dbj@netbsd.org
582 link_check(fsinode *entry)
584 static struct entry {
587 static int htshift; /* log(allocated size) */
588 static int htmask; /* allocated size - 1 */
589 static int htused; /* 2*number of insertions */
592 /* this constant is (1<<64)/((1+sqrt(5))/2)
593 * aka (word size)/(golden ratio)
595 const uint64_t HTCONST = 11400714819323198485ULL;
596 const int HTBITS = 64;
598 /* Never store zero in hashtable */
601 /* Extend hash table if necessary, keep load under 0.5 */
602 if (htused<<1 >= htmask) {
603 struct entry *ohtable;
606 htshift = 10; /* starting hashtable size */
608 htshift++; /* exponential hashtable growth */
610 htmask = (1 << htshift) - 1;
614 htable = calloc(htmask+1, sizeof(*htable));
616 err(1, "Memory allocation error");
618 /* populate newly allocated hashtable */
621 for (i = 0; i <= htmask>>1; i++)
623 link_check(ohtable[i].data);
628 /* multiplicative hashing */
629 tmp = entry->st.st_dev;
631 tmp |= entry->st.st_ino;
633 h = tmp >> (HTBITS - htshift);
634 h2 = 1 | ( tmp >> (HTBITS - (htshift<<1) - 1)); /* must be odd */
636 /* open address hashtable search with double hash probing */
637 while (htable[h].data) {
638 if ((htable[h].data->st.st_ino == entry->st.st_ino) &&
639 (htable[h].data->st.st_dev == entry->st.st_dev)) {
640 return htable[h].data;
642 h = (h + h2) & htmask;
645 /* Insert the current entry into hashtable */
646 htable[h].data = entry;