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>
59 static void apply_specdir(const char *, NODE *, fsnode *, int);
60 static void apply_specentry(const char *, NODE *, fsnode *);
61 static fsnode *create_fsnode(const char *, const char *, const char *,
67 * build a tree of fsnodes from `root' and `dir', with a parent
68 * fsnode of `parent' (which may be NULL for the root of the tree).
69 * append the tree to a fsnode of `join' if it is not NULL.
70 * each "level" is a directory, with the "." entry guaranteed to be
71 * at the start of the list, and without ".." entries.
74 walk_dir(const char *root, const char *dir, fsnode *parent, fsnode *join)
76 fsnode *first, *cur, *prev, *last;
79 char path[MAXPATHLEN + 1];
87 len = snprintf(path, sizeof(path), "%s/%s", root, dir);
88 if (len >= (int)sizeof(path))
89 errx(1, "Pathname too long.");
90 if (debug & DEBUG_WALK_DIR)
91 printf("walk_dir: %s %p\n", path, parent);
92 if ((dirp = opendir(path)) == NULL)
93 err(1, "Can't opendir `%s'", path);
94 rp = path + strlen(root) + 1;
97 while (cur->next != NULL)
103 while ((dent = readdir(dirp)) != NULL) {
120 if (debug & DEBUG_WALK_DIR_NODE)
121 printf("scanning %s/%s/%s\n", root, dir, name);
122 if (snprintf(path + len, sizeof(path) - len, "/%s", name) >=
123 (int)sizeof(path) - len)
124 errx(1, "Pathname too long.");
125 if (lstat(path, &stbuf) == -1)
126 err(1, "Can't lstat `%s'", path);
128 if (S_ISSOCK(stbuf.st_mode & S_IFMT)) {
129 if (debug & DEBUG_WALK_DIR_NODE)
130 printf(" skipping socket %s\n", path);
138 if (cur == NULL || strcmp(cur->name, name) == 0)
147 if (S_ISDIR(cur->type) &&
148 S_ISDIR(stbuf.st_mode)) {
149 if (debug & DEBUG_WALK_DIR_NODE)
150 printf("merging %s with %p\n",
152 cur->child = walk_dir(root, rp, cur,
156 errx(1, "Can't merge %s `%s' with existing %s",
157 inode_type(stbuf.st_mode), path,
158 inode_type(cur->type));
162 cur = create_fsnode(root, dir, name, &stbuf);
163 cur->parent = parent;
165 /* ensure "." is at the start of the list */
171 } else { /* not "." */
178 if (S_ISDIR(cur->type)) {
179 cur->child = walk_dir(root, rp, cur, NULL);
183 if (stbuf.st_nlink > 1) {
186 curino = link_check(cur->inode);
187 if (curino != NULL) {
191 if (debug & DEBUG_WALK_DIR_LINKCHECK)
192 printf("link_check: found [%llu, %llu]\n",
193 (unsigned long long)curino->st.st_dev,
194 (unsigned long long)curino->st.st_ino);
197 if (S_ISLNK(cur->type)) {
198 char slink[PATH_MAX+1];
201 llen = readlink(path, slink, sizeof(slink) - 1);
203 err(1, "Readlink `%s'", path);
205 if ((cur->symlink = strdup(slink)) == NULL)
206 err(1, "Memory allocation error");
209 assert(first != NULL);
211 for (cur = first->next; cur != NULL; cur = cur->next)
213 if (closedir(dirp) == -1)
214 err(1, "Can't closedir `%s/%s'", root, dir);
219 create_fsnode(const char *root, const char *path, const char *name,
224 if ((cur = calloc(1, sizeof(fsnode))) == NULL ||
225 (cur->path = strdup(path)) == NULL ||
226 (cur->name = strdup(name)) == NULL ||
227 (cur->inode = calloc(1, sizeof(fsinode))) == NULL)
228 err(1, "Memory allocation error");
230 cur->type = stbuf->st_mode & S_IFMT;
231 cur->inode->nlink = 1;
232 cur->inode->st = *stbuf;
233 if (stampst.st_ino) {
234 cur->inode->st.st_atime = stampst.st_atime;
235 cur->inode->st.st_mtime = stampst.st_mtime;
236 cur->inode->st.st_ctime = stampst.st_ctime;
237 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
238 cur->inode->st.st_atimensec = stampst.st_atimensec;
239 cur->inode->st.st_mtimensec = stampst.st_mtimensec;
240 cur->inode->st.st_ctimensec = stampst.st_ctimensec;
242 #if HAVE_STRUCT_STAT_BIRTHTIME
243 cur->inode->st.st_birthtime = stampst.st_birthtime;
244 cur->inode->st.st_birthtimensec = stampst.st_birthtimensec;
252 * Removes node from tree and frees it and all of
256 free_fsnodes(fsnode *node)
260 assert(node != NULL);
262 /* for ".", start with actual parent node */
263 if (node->first == node) {
264 assert(node->name[0] == '.' && node->name[1] == '\0');
266 assert(node->parent->child == node);
271 /* Find ourselves in our sibling list and unlink */
272 if (node->first != node) {
273 for (cur = node->first; cur->next; cur = cur->next) {
274 if (cur->next == node) {
275 cur->next = node->next;
282 for (cur = node; cur != NULL; cur = next) {
285 cur->child->parent = NULL;
286 free_fsnodes(cur->child);
288 if (cur->inode->nlink-- == 1)
300 * read in the mtree(8) specfile, and apply it to the tree
301 * at dir,parent. parameters in parent on equivalent types
302 * will be changed to those found in specfile, and missing
303 * entries will be added.
306 apply_specfile(const char *specfile, const char *dir, fsnode *parent, int speconly)
308 struct timeval start;
312 assert(specfile != NULL);
313 assert(parent != NULL);
315 if (debug & DEBUG_APPLY_SPECFILE)
316 printf("apply_specfile: %s, %s %p\n", specfile, dir, parent);
318 /* read in the specfile */
319 if ((fp = fopen(specfile, "r")) == NULL)
320 err(1, "Can't open `%s'", specfile);
323 TIMER_RESULTS(start, "spec");
324 if (fclose(fp) == EOF)
325 err(1, "Can't close `%s'", specfile);
327 /* perform some sanity checks */
329 errx(1, "Specfile `%s' did not contain a tree", specfile);
330 assert(strcmp(root->name, ".") == 0);
331 assert(root->type == F_DIR);
333 /* merge in the changes */
334 apply_specdir(dir, root, parent, speconly);
339 apply_specdir(const char *dir, NODE *specnode, fsnode *dirnode, int speconly)
341 char path[MAXPATHLEN + 1];
345 assert(specnode != NULL);
346 assert(dirnode != NULL);
348 if (debug & DEBUG_APPLY_SPECFILE)
349 printf("apply_specdir: %s %p %p\n", dir, specnode, dirnode);
351 if (specnode->type != F_DIR)
352 errx(1, "Specfile node `%s/%s' is not a directory",
353 dir, specnode->name);
354 if (dirnode->type != S_IFDIR)
355 errx(1, "Directory node `%s/%s' is not a directory",
358 apply_specentry(dir, specnode, dirnode);
360 /* Remove any filesystem nodes not found in specfile */
361 /* XXX inefficient. This is O^2 in each dir and it would
362 * have been better never to have walked this part of the tree
367 assert(dirnode->name[0] == '.' && dirnode->name[1] == '\0');
368 for (curfsnode = dirnode->next; curfsnode != NULL; curfsnode = next) {
369 next = curfsnode->next;
370 for (curnode = specnode->child; curnode != NULL;
371 curnode = curnode->next) {
372 if (strcmp(curnode->name, curfsnode->name) == 0)
375 if (curnode == NULL) {
376 if (debug & DEBUG_APPLY_SPECONLY) {
377 printf("apply_specdir: trimming %s/%s %p\n", dir, curfsnode->name, curfsnode);
379 free_fsnodes(curfsnode);
384 /* now walk specnode->child matching up with dirnode */
385 for (curnode = specnode->child; curnode != NULL;
386 curnode = curnode->next) {
387 if (debug & DEBUG_APPLY_SPECENTRY)
388 printf("apply_specdir: spec %s\n",
390 for (curfsnode = dirnode->next; curfsnode != NULL;
391 curfsnode = curfsnode->next) {
392 #if 0 /* too verbose for now */
393 if (debug & DEBUG_APPLY_SPECENTRY)
394 printf("apply_specdir: dirent %s\n",
397 if (strcmp(curnode->name, curfsnode->name) == 0)
400 if (snprintf(path, sizeof(path), "%s/%s",
401 dir, curnode->name) >= sizeof(path))
402 errx(1, "Pathname too long.");
403 if (curfsnode == NULL) { /* need new entry */
407 * don't add optional spec entries
408 * that lack an existing fs entry
410 if ((curnode->flags & F_OPT) &&
411 lstat(path, &stbuf) == -1)
414 /* check that enough info is provided */
415 #define NODETEST(t, m) \
417 errx(1, "`%s': %s not provided", path, m)
418 NODETEST(curnode->flags & F_TYPE, "type");
419 NODETEST(curnode->flags & F_MODE, "mode");
420 /* XXX: require F_TIME ? */
421 NODETEST(curnode->flags & F_GID ||
422 curnode->flags & F_GNAME, "group");
423 NODETEST(curnode->flags & F_UID ||
424 curnode->flags & F_UNAME, "user");
425 /* if (curnode->type == F_BLOCK || curnode->type == F_CHAR)
426 NODETEST(curnode->flags & F_DEV,
430 if (debug & DEBUG_APPLY_SPECFILE)
431 printf("apply_specdir: adding %s\n",
433 /* build minimal fsnode */
434 memset(&stbuf, 0, sizeof(stbuf));
435 stbuf.st_mode = nodetoino(curnode->type);
437 stbuf.st_mtime = stbuf.st_atime =
438 stbuf.st_ctime = start_time.tv_sec;
439 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
440 stbuf.st_mtimensec = stbuf.st_atimensec =
441 stbuf.st_ctimensec = start_time.tv_nsec;
443 curfsnode = create_fsnode(".", ".", curnode->name,
445 curfsnode->parent = dirnode->parent;
446 curfsnode->first = dirnode;
447 curfsnode->next = dirnode->next;
448 dirnode->next = curfsnode;
449 if (curfsnode->type == S_IFDIR) {
450 /* for dirs, make "." entry as well */
451 curfsnode->child = create_fsnode(".", ".", ".",
453 curfsnode->child->parent = curfsnode;
454 curfsnode->child->first = curfsnode->child;
456 if (curfsnode->type == S_IFLNK) {
457 assert(curnode->slink != NULL);
458 /* for symlinks, copy the target */
459 if ((curfsnode->symlink =
460 strdup(curnode->slink)) == NULL)
461 err(1, "Memory allocation error");
464 apply_specentry(dir, curnode, curfsnode);
465 if (curnode->type == F_DIR) {
466 if (curfsnode->type != S_IFDIR)
467 errx(1, "`%s' is not a directory", path);
468 assert (curfsnode->child != NULL);
469 apply_specdir(path, curnode, curfsnode->child, speconly);
475 apply_specentry(const char *dir, NODE *specnode, fsnode *dirnode)
478 assert(specnode != NULL);
479 assert(dirnode != NULL);
481 if (nodetoino(specnode->type) != dirnode->type)
482 errx(1, "`%s/%s' type mismatch: specfile %s, tree %s",
483 dir, specnode->name, inode_type(nodetoino(specnode->type)),
484 inode_type(dirnode->type));
486 if (debug & DEBUG_APPLY_SPECENTRY)
487 printf("apply_specentry: %s/%s\n", dir, dirnode->name);
489 #define ASEPRINT(t, b, o, n) \
490 if (debug & DEBUG_APPLY_SPECENTRY) \
491 printf("\t\t\tchanging %s from " b " to " b "\n", \
494 if (specnode->flags & (F_GID | F_GNAME)) {
495 ASEPRINT("gid", "%d",
496 dirnode->inode->st.st_gid, specnode->st_gid);
497 dirnode->inode->st.st_gid = specnode->st_gid;
499 if (specnode->flags & F_MODE) {
500 ASEPRINT("mode", "%#o",
501 dirnode->inode->st.st_mode & ALLPERMS, specnode->st_mode);
502 dirnode->inode->st.st_mode &= ~ALLPERMS;
503 dirnode->inode->st.st_mode |= (specnode->st_mode & ALLPERMS);
505 /* XXX: ignoring F_NLINK for now */
506 if (specnode->flags & F_SIZE) {
507 ASEPRINT("size", "%lld",
508 (long long)dirnode->inode->st.st_size,
509 (long long)specnode->st_size);
510 dirnode->inode->st.st_size = specnode->st_size;
512 if (specnode->flags & F_SLINK) {
513 assert(dirnode->symlink != NULL);
514 assert(specnode->slink != NULL);
515 ASEPRINT("symlink", "%s", dirnode->symlink, specnode->slink);
516 free(dirnode->symlink);
517 if ((dirnode->symlink = strdup(specnode->slink)) == NULL)
518 err(1, "Memory allocation error");
520 if (specnode->flags & F_TIME) {
521 ASEPRINT("time", "%ld",
522 (long)dirnode->inode->st.st_mtime,
523 (long)specnode->st_mtimespec.tv_sec);
524 dirnode->inode->st.st_mtime = specnode->st_mtimespec.tv_sec;
525 dirnode->inode->st.st_atime = specnode->st_mtimespec.tv_sec;
526 dirnode->inode->st.st_ctime = start_time.tv_sec;
527 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
528 dirnode->inode->st.st_mtimensec = specnode->st_mtimespec.tv_nsec;
529 dirnode->inode->st.st_atimensec = specnode->st_mtimespec.tv_nsec;
530 dirnode->inode->st.st_ctimensec = start_time.tv_nsec;
533 if (specnode->flags & (F_UID | F_UNAME)) {
534 ASEPRINT("uid", "%d",
535 dirnode->inode->st.st_uid, specnode->st_uid);
536 dirnode->inode->st.st_uid = specnode->st_uid;
538 #if HAVE_STRUCT_STAT_ST_FLAGS
539 if (specnode->flags & F_FLAGS) {
540 ASEPRINT("flags", "%#lX",
541 (unsigned long)dirnode->inode->st.st_flags,
542 (unsigned long)specnode->st_flags);
543 dirnode->inode->st.st_flags = specnode->st_flags;
546 /* if (specnode->flags & F_DEV) {
547 ASEPRINT("rdev", "%#llx",
548 (unsigned long long)dirnode->inode->st.st_rdev,
549 (unsigned long long)specnode->st_rdev);
550 dirnode->inode->st.st_rdev = specnode->st_rdev;
554 dirnode->flags |= FSNODE_F_HASSPEC;
560 * dump the fsnodes from `cur'
563 dump_fsnodes(fsnode *root)
566 char path[MAXPATHLEN + 1];
568 printf("dump_fsnodes: %s %p\n", root->path, root);
569 for (cur = root; cur != NULL; cur = cur->next) {
570 if (snprintf(path, sizeof(path), "%s/%s", cur->path,
571 cur->name) >= (int)sizeof(path))
572 errx(1, "Pathname too long.");
574 if (debug & DEBUG_DUMP_FSNODES_VERBOSE)
575 printf("cur=%8p parent=%8p first=%8p ",
576 cur, cur->parent, cur->first);
577 printf("%7s: %s", inode_type(cur->type), path);
578 if (S_ISLNK(cur->type)) {
579 assert(cur->symlink != NULL);
580 printf(" -> %s", cur->symlink);
582 assert (cur->symlink == NULL);
584 if (cur->inode->nlink > 1)
585 printf(", nlinks=%d", cur->inode->nlink);
589 assert (cur->type == S_IFDIR);
590 dump_fsnodes(cur->child);
593 printf("dump_fsnodes: finished %s/%s\n", root->path, root->name);
599 * for a given inode type `mode', return a descriptive string.
600 * for most cases, uses inotype() from mtree/misc.c
603 inode_type(mode_t mode)
617 /* XXX should not happen but handle them */
628 * return pointer to fsinode matching `entry's st_ino & st_dev if it exists,
629 * otherwise add `entry' to table and return NULL
631 /* This was borrowed from du.c and tweaked to keep an fsnode
632 * pointer instead. -- dbj@netbsd.org
635 link_check(fsinode *entry)
637 static struct entry {
640 static int htshift; /* log(allocated size) */
641 static int htmask; /* allocated size - 1 */
642 static int htused; /* 2*number of insertions */
645 /* this constant is (1<<64)/((1+sqrt(5))/2)
646 * aka (word size)/(golden ratio)
648 const uint64_t HTCONST = 11400714819323198485ULL;
649 const int HTBITS = 64;
651 /* Never store zero in hashtable */
654 /* Extend hash table if necessary, keep load under 0.5 */
655 if (htused<<1 >= htmask) {
656 struct entry *ohtable;
659 htshift = 10; /* starting hashtable size */
661 htshift++; /* exponential hashtable growth */
663 htmask = (1 << htshift) - 1;
667 htable = calloc(htmask+1, sizeof(*htable));
669 err(1, "Memory allocation error");
671 /* populate newly allocated hashtable */
674 for (i = 0; i <= htmask>>1; i++)
676 link_check(ohtable[i].data);
681 /* multiplicative hashing */
682 tmp = entry->st.st_dev;
684 tmp |= entry->st.st_ino;
686 h = tmp >> (HTBITS - htshift);
687 h2 = 1 | ( tmp >> (HTBITS - (htshift<<1) - 1)); /* must be odd */
689 /* open address hashtable search with double hash probing */
690 while (htable[h].data) {
691 if ((htable[h].data->st.st_ino == entry->st.st_ino) &&
692 (htable[h].data->st.st_dev == entry->st.st_dev)) {
693 return htable[h].data;
695 h = (h + h2) & htmask;
698 /* Insert the current entry into hashtable */
699 htable[h].data = entry;