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 *, const char *, const char *,
66 * build a tree of fsnodes from `root' and `dir', with a parent
67 * fsnode of `parent' (which may be NULL for the root of the tree).
68 * append the tree to a fsnode of `join' if it is not NULL.
69 * each "level" is a directory, with the "." entry guaranteed to be
70 * at the start of the list, and without ".." entries.
73 walk_dir(const char *root, const char *dir, fsnode *parent, fsnode *join)
75 fsnode *first, *cur, *prev, *last;
78 char path[MAXPATHLEN + 1];
86 len = snprintf(path, sizeof(path), "%s/%s", root, dir);
87 if (len >= (int)sizeof(path))
88 errx(1, "Pathname too long.");
89 if (debug & DEBUG_WALK_DIR)
90 printf("walk_dir: %s %p\n", path, parent);
91 if ((dirp = opendir(path)) == NULL)
92 err(1, "Can't opendir `%s'", path);
93 rp = path + strlen(root) + 1;
96 while (cur->next != NULL)
102 while ((dent = readdir(dirp)) != NULL) {
119 if (debug & DEBUG_WALK_DIR_NODE)
120 printf("scanning %s/%s/%s\n", root, dir, name);
121 if (snprintf(path + len, sizeof(path) - len, "/%s", name) >=
122 (int)sizeof(path) - len)
123 errx(1, "Pathname too long.");
124 if (lstat(path, &stbuf) == -1)
125 err(1, "Can't lstat `%s'", path);
127 if (S_ISSOCK(stbuf.st_mode & S_IFMT)) {
128 if (debug & DEBUG_WALK_DIR_NODE)
129 printf(" skipping socket %s\n", path);
137 if (cur == NULL || strcmp(cur->name, name) == 0)
146 if (S_ISDIR(cur->type) &&
147 S_ISDIR(stbuf.st_mode)) {
148 if (debug & DEBUG_WALK_DIR_NODE)
149 printf("merging %s with %p\n",
151 cur->child = walk_dir(root, rp, cur,
155 errx(1, "Can't merge %s `%s' with existing %s",
156 inode_type(stbuf.st_mode), path,
157 inode_type(cur->type));
161 cur = create_fsnode(root, dir, name, &stbuf);
162 cur->parent = parent;
164 /* ensure "." is at the start of the list */
170 } else { /* not "." */
177 if (S_ISDIR(cur->type)) {
178 cur->child = walk_dir(root, rp, cur, NULL);
182 if (stbuf.st_nlink > 1) {
185 curino = link_check(cur->inode);
186 if (curino != NULL) {
190 if (debug & DEBUG_WALK_DIR_LINKCHECK)
191 printf("link_check: found [%llu, %llu]\n",
192 (unsigned long long)curino->st.st_dev,
193 (unsigned long long)curino->st.st_ino);
196 if (S_ISLNK(cur->type)) {
197 char slink[PATH_MAX+1];
200 llen = readlink(path, slink, sizeof(slink) - 1);
202 err(1, "Readlink `%s'", path);
204 if ((cur->symlink = strdup(slink)) == NULL)
205 err(1, "Memory allocation error");
208 assert(first != NULL);
210 for (cur = first->next; cur != NULL; cur = cur->next)
212 if (closedir(dirp) == -1)
213 err(1, "Can't closedir `%s/%s'", root, dir);
218 create_fsnode(const char *root, const char *path, const char *name,
223 if ((cur = calloc(1, sizeof(fsnode))) == NULL ||
224 (cur->path = strdup(path)) == NULL ||
225 (cur->name = strdup(name)) == NULL ||
226 (cur->inode = calloc(1, sizeof(fsinode))) == NULL)
227 err(1, "Memory allocation error");
229 cur->type = stbuf->st_mode & S_IFMT;
230 cur->inode->nlink = 1;
231 cur->inode->st = *stbuf;
237 * Removes node from tree and frees it and all of
241 free_fsnodes(fsnode *node)
245 assert(node != NULL);
247 /* for ".", start with actual parent node */
248 if (node->first == node) {
249 assert(node->name[0] == '.' && node->name[1] == '\0');
251 assert(node->parent->child == node);
256 /* Find ourselves in our sibling list and unlink */
257 if (node->first != node) {
258 for (cur = node->first; cur->next; cur = cur->next) {
259 if (cur->next == node) {
260 cur->next = node->next;
267 for (cur = node; cur != NULL; cur = next) {
270 cur->child->parent = NULL;
271 free_fsnodes(cur->child);
273 if (cur->inode->nlink-- == 1)
285 * read in the mtree(8) specfile, and apply it to the tree
286 * at dir,parent. parameters in parent on equivalent types
287 * will be changed to those found in specfile, and missing
288 * entries will be added.
291 apply_specfile(const char *specfile, const char *dir, fsnode *parent, int speconly)
293 struct timeval start;
297 assert(specfile != NULL);
298 assert(parent != NULL);
300 if (debug & DEBUG_APPLY_SPECFILE)
301 printf("apply_specfile: %s, %s %p\n", specfile, dir, parent);
303 /* read in the specfile */
304 if ((fp = fopen(specfile, "r")) == NULL)
305 err(1, "Can't open `%s'", specfile);
307 root = mtree_readspec(fp);
308 TIMER_RESULTS(start, "spec");
309 if (fclose(fp) == EOF)
310 err(1, "Can't close `%s'", specfile);
312 /* perform some sanity checks */
314 errx(1, "Specfile `%s' did not contain a tree", specfile);
315 assert(strcmp(root->name, ".") == 0);
316 assert(root->type == F_DIR);
318 /* merge in the changes */
319 apply_specdir(dir, root, parent, speconly);
324 nodetoino(u_int type)
343 printf("unknown type %d", type);
351 apply_specdir(const char *dir, NODE *specnode, fsnode *dirnode, int speconly)
353 char path[MAXPATHLEN + 1];
357 assert(specnode != NULL);
358 assert(dirnode != NULL);
360 if (debug & DEBUG_APPLY_SPECFILE)
361 printf("apply_specdir: %s %p %p\n", dir, specnode, dirnode);
363 if (specnode->type != F_DIR)
364 errx(1, "Specfile node `%s/%s' is not a directory",
365 dir, specnode->name);
366 if (dirnode->type != S_IFDIR)
367 errx(1, "Directory node `%s/%s' is not a directory",
370 apply_specentry(dir, specnode, dirnode);
372 /* Remove any filesystem nodes not found in specfile */
373 /* XXX inefficient. This is O^2 in each dir and it would
374 * have been better never to have walked this part of the tree
379 assert(dirnode->name[0] == '.' && dirnode->name[1] == '\0');
380 for (curfsnode = dirnode->next; curfsnode != NULL; curfsnode = next) {
381 next = curfsnode->next;
382 for (curnode = specnode->child; curnode != NULL;
383 curnode = curnode->next) {
384 if (strcmp(curnode->name, curfsnode->name) == 0)
387 if (curnode == NULL) {
388 if (debug & DEBUG_APPLY_SPECONLY) {
389 printf("apply_specdir: trimming %s/%s %p\n", dir, curfsnode->name, curfsnode);
391 free_fsnodes(curfsnode);
396 /* now walk specnode->child matching up with dirnode */
397 for (curnode = specnode->child; curnode != NULL;
398 curnode = curnode->next) {
399 if (debug & DEBUG_APPLY_SPECENTRY)
400 printf("apply_specdir: spec %s\n",
402 for (curfsnode = dirnode->next; curfsnode != NULL;
403 curfsnode = curfsnode->next) {
404 #if 0 /* too verbose for now */
405 if (debug & DEBUG_APPLY_SPECENTRY)
406 printf("apply_specdir: dirent %s\n",
409 if (strcmp(curnode->name, curfsnode->name) == 0)
412 if (snprintf(path, sizeof(path), "%s/%s",
413 dir, curnode->name) >= sizeof(path))
414 errx(1, "Pathname too long.");
415 if (curfsnode == NULL) { /* need new entry */
419 * don't add optional spec entries
420 * that lack an existing fs entry
422 if ((curnode->flags & F_OPT) &&
423 lstat(path, &stbuf) == -1)
426 /* check that enough info is provided */
427 #define NODETEST(t, m) \
429 errx(1, "`%s': %s not provided", path, m)
430 NODETEST(curnode->flags & F_TYPE, "type");
431 NODETEST(curnode->flags & F_MODE, "mode");
432 /* XXX: require F_TIME ? */
433 NODETEST(curnode->flags & F_GID ||
434 curnode->flags & F_GNAME, "group");
435 NODETEST(curnode->flags & F_UID ||
436 curnode->flags & F_UNAME, "user");
437 /* if (curnode->type == F_BLOCK || curnode->type == F_CHAR)
438 NODETEST(curnode->flags & F_DEV,
442 if (debug & DEBUG_APPLY_SPECFILE)
443 printf("apply_specdir: adding %s\n",
445 /* build minimal fsnode */
446 memset(&stbuf, 0, sizeof(stbuf));
447 stbuf.st_mode = nodetoino(curnode->type);
449 stbuf.st_mtime = stbuf.st_atime =
450 stbuf.st_ctime = start_time.tv_sec;
451 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
452 stbuf.st_mtimensec = stbuf.st_atimensec =
453 stbuf.st_ctimensec = start_time.tv_nsec;
455 curfsnode = create_fsnode(".", ".", curnode->name,
457 curfsnode->parent = dirnode->parent;
458 curfsnode->first = dirnode;
459 curfsnode->next = dirnode->next;
460 dirnode->next = curfsnode;
461 if (curfsnode->type == S_IFDIR) {
462 /* for dirs, make "." entry as well */
463 curfsnode->child = create_fsnode(".", ".", ".",
465 curfsnode->child->parent = curfsnode;
466 curfsnode->child->first = curfsnode->child;
468 if (curfsnode->type == S_IFLNK) {
469 assert(curnode->slink != NULL);
470 /* for symlinks, copy the target */
471 if ((curfsnode->symlink =
472 strdup(curnode->slink)) == NULL)
473 err(1, "Memory allocation error");
476 apply_specentry(dir, curnode, curfsnode);
477 if (curnode->type == F_DIR) {
478 if (curfsnode->type != S_IFDIR)
479 errx(1, "`%s' is not a directory", path);
480 assert (curfsnode->child != NULL);
481 apply_specdir(path, curnode, curfsnode->child, speconly);
487 apply_specentry(const char *dir, NODE *specnode, fsnode *dirnode)
490 assert(specnode != NULL);
491 assert(dirnode != NULL);
493 if (nodetoino(specnode->type) != dirnode->type)
494 errx(1, "`%s/%s' type mismatch: specfile %s, tree %s",
495 dir, specnode->name, inode_type(nodetoino(specnode->type)),
496 inode_type(dirnode->type));
498 if (debug & DEBUG_APPLY_SPECENTRY)
499 printf("apply_specentry: %s/%s\n", dir, dirnode->name);
501 #define ASEPRINT(t, b, o, n) \
502 if (debug & DEBUG_APPLY_SPECENTRY) \
503 printf("\t\t\tchanging %s from " b " to " b "\n", \
506 if (specnode->flags & (F_GID | F_GNAME)) {
507 ASEPRINT("gid", "%d",
508 dirnode->inode->st.st_gid, specnode->st_gid);
509 dirnode->inode->st.st_gid = specnode->st_gid;
511 if (specnode->flags & F_MODE) {
512 ASEPRINT("mode", "%#o",
513 dirnode->inode->st.st_mode & ALLPERMS, specnode->st_mode);
514 dirnode->inode->st.st_mode &= ~ALLPERMS;
515 dirnode->inode->st.st_mode |= (specnode->st_mode & ALLPERMS);
517 /* XXX: ignoring F_NLINK for now */
518 if (specnode->flags & F_SIZE) {
519 ASEPRINT("size", "%lld",
520 (long long)dirnode->inode->st.st_size,
521 (long long)specnode->st_size);
522 dirnode->inode->st.st_size = specnode->st_size;
524 if (specnode->flags & F_SLINK) {
525 assert(dirnode->symlink != NULL);
526 assert(specnode->slink != NULL);
527 ASEPRINT("symlink", "%s", dirnode->symlink, specnode->slink);
528 free(dirnode->symlink);
529 if ((dirnode->symlink = strdup(specnode->slink)) == NULL)
530 err(1, "Memory allocation error");
532 if (specnode->flags & F_TIME) {
533 ASEPRINT("time", "%ld",
534 (long)dirnode->inode->st.st_mtime,
535 (long)specnode->st_mtimespec.tv_sec);
536 dirnode->inode->st.st_mtime = specnode->st_mtimespec.tv_sec;
537 dirnode->inode->st.st_atime = specnode->st_mtimespec.tv_sec;
538 dirnode->inode->st.st_ctime = start_time.tv_sec;
539 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
540 dirnode->inode->st.st_mtimensec = specnode->st_mtimespec.tv_nsec;
541 dirnode->inode->st.st_atimensec = specnode->st_mtimespec.tv_nsec;
542 dirnode->inode->st.st_ctimensec = start_time.tv_nsec;
545 if (specnode->flags & (F_UID | F_UNAME)) {
546 ASEPRINT("uid", "%d",
547 dirnode->inode->st.st_uid, specnode->st_uid);
548 dirnode->inode->st.st_uid = specnode->st_uid;
550 #if HAVE_STRUCT_STAT_ST_FLAGS
551 if (specnode->flags & F_FLAGS) {
552 ASEPRINT("flags", "%#lX",
553 (unsigned long)dirnode->inode->st.st_flags,
554 (unsigned long)specnode->st_flags);
555 dirnode->inode->st.st_flags = specnode->st_flags;
558 /* if (specnode->flags & F_DEV) {
559 ASEPRINT("rdev", "%#llx",
560 (unsigned long long)dirnode->inode->st.st_rdev,
561 (unsigned long long)specnode->st_rdev);
562 dirnode->inode->st.st_rdev = specnode->st_rdev;
566 dirnode->flags |= FSNODE_F_HASSPEC;
572 * dump the fsnodes from `cur'
575 dump_fsnodes(fsnode *root)
578 char path[MAXPATHLEN + 1];
580 printf("dump_fsnodes: %s %p\n", root->path, root);
581 for (cur = root; cur != NULL; cur = cur->next) {
582 if (snprintf(path, sizeof(path), "%s/%s", cur->path,
583 cur->name) >= (int)sizeof(path))
584 errx(1, "Pathname too long.");
586 if (debug & DEBUG_DUMP_FSNODES_VERBOSE)
587 printf("cur=%8p parent=%8p first=%8p ",
588 cur, cur->parent, cur->first);
589 printf("%7s: %s", inode_type(cur->type), path);
590 if (S_ISLNK(cur->type)) {
591 assert(cur->symlink != NULL);
592 printf(" -> %s", cur->symlink);
594 assert (cur->symlink == NULL);
596 if (cur->inode->nlink > 1)
597 printf(", nlinks=%d", cur->inode->nlink);
601 assert (cur->type == S_IFDIR);
602 dump_fsnodes(cur->child);
605 printf("dump_fsnodes: finished %s/%s\n", root->path, root->name);
611 * for a given inode type `mode', return a descriptive string.
612 * for most cases, uses inotype() from mtree/misc.c
615 inode_type(mode_t mode)
629 /* XXX should not happen but handle them */
640 * return pointer to fsinode matching `entry's st_ino & st_dev if it exists,
641 * otherwise add `entry' to table and return NULL
643 /* This was borrowed from du.c and tweaked to keep an fsnode
644 * pointer instead. -- dbj@netbsd.org
647 link_check(fsinode *entry)
649 static struct entry {
652 static int htshift; /* log(allocated size) */
653 static int htmask; /* allocated size - 1 */
654 static int htused; /* 2*number of insertions */
657 /* this constant is (1<<64)/((1+sqrt(5))/2)
658 * aka (word size)/(golden ratio)
660 const uint64_t HTCONST = 11400714819323198485ULL;
661 const int HTBITS = 64;
663 /* Never store zero in hashtable */
666 /* Extend hash table if necessary, keep load under 0.5 */
667 if (htused<<1 >= htmask) {
668 struct entry *ohtable;
671 htshift = 10; /* starting hashtable size */
673 htshift++; /* exponential hashtable growth */
675 htmask = (1 << htshift) - 1;
679 htable = calloc(htmask+1, sizeof(*htable));
681 err(1, "Memory allocation error");
683 /* populate newly allocated hashtable */
686 for (i = 0; i <= htmask>>1; i++)
688 link_check(ohtable[i].data);
693 /* multiplicative hashing */
694 tmp = entry->st.st_dev;
696 tmp |= entry->st.st_ino;
698 h = tmp >> (HTBITS - htshift);
699 h2 = 1 | ( tmp >> (HTBITS - (htshift<<1) - 1)); /* must be odd */
701 /* open address hashtable search with double hash probing */
702 while (htable[h].data) {
703 if ((htable[h].data->st.st_ino == entry->st.st_ino) &&
704 (htable[h].data->st.st_dev == entry->st.st_dev)) {
705 return htable[h].data;
707 h = (h + h2) & htmask;
710 /* Insert the current entry into hashtable */
711 htable[h].data = entry;