1 /* $NetBSD: walk.c,v 1.24 2008/12/28 21:51:46 christos Exp $ */
4 * SPDX-License-Identifier: BSD-4-Clause
6 * Copyright (c) 2001 Wasabi Systems, Inc.
9 * Written by Luke Mewburn for Wasabi Systems, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed for the NetBSD Project by
22 * Wasabi Systems, Inc.
23 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
24 * or promote products derived from this software without specific prior
27 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
39 * $FreeBSD: head/usr.sbin/makefs/walk.c 326276 2017-11-27 15:37:16Z pfg $
43 #include <sys/param.h>
60 static void apply_specdir(const char *, NODE *, fsnode *, int);
61 static void apply_specentry(const char *, NODE *, fsnode *);
62 static fsnode *create_fsnode(const char *, const char *, const char *,
68 * build a tree of fsnodes from `root' and `dir', with a parent
69 * fsnode of `parent' (which may be NULL for the root of the tree).
70 * append the tree to a fsnode of `join' if it is not NULL.
71 * each "level" is a directory, with the "." entry guaranteed to be
72 * at the start of the list, and without ".." entries.
75 walk_dir(const char *root, const char *dir, fsnode *parent, fsnode *join)
77 fsnode *first, *cur, *prev, *last;
80 char path[MAXPATHLEN + 1];
89 len = snprintf(path, sizeof(path), "%s/%s", root, dir);
90 if (len >= sizeof(path))
91 errx(1, "Pathname too long.");
92 if (debug & DEBUG_WALK_DIR)
93 printf("walk_dir: %s %p\n", path, parent);
94 if ((dirp = opendir(path)) == NULL)
95 err(1, "Can't opendir `%s'", path);
96 rp = path + strlen(root) + 1;
99 while (cur->next != NULL)
105 while ((dent = readdir(dirp)) != NULL) {
122 if (debug & DEBUG_WALK_DIR_NODE)
123 printf("scanning %s/%s/%s\n", root, dir, name);
124 if ((size_t)snprintf(path + len, sizeof(path) - len, "/%s",
125 name) >= sizeof(path) - len)
126 errx(1, "Pathname too long.");
127 if (lstat(path, &stbuf) == -1)
128 err(1, "Can't lstat `%s'", path);
130 if (S_ISSOCK(stbuf.st_mode & S_IFMT)) {
131 if (debug & DEBUG_WALK_DIR_NODE)
132 printf(" skipping socket %s\n", path);
140 if (cur == NULL || strcmp(cur->name, name) == 0)
149 if (S_ISDIR(cur->type) &&
150 S_ISDIR(stbuf.st_mode)) {
151 if (debug & DEBUG_WALK_DIR_NODE)
152 printf("merging %s with %p\n",
154 cur->child = walk_dir(root, rp, cur,
158 errx(1, "Can't merge %s `%s' with existing %s",
159 inode_type(stbuf.st_mode), path,
160 inode_type(cur->type));
164 cur = create_fsnode(root, dir, name, &stbuf);
165 cur->parent = parent;
167 /* ensure "." is at the start of the list */
173 } else { /* not "." */
180 if (S_ISDIR(cur->type)) {
181 cur->child = walk_dir(root, rp, cur, NULL);
185 if (stbuf.st_nlink > 1) {
188 curino = link_check(cur->inode);
189 if (curino != NULL) {
193 if (debug & DEBUG_WALK_DIR_LINKCHECK)
194 printf("link_check: found [%llu, %llu]\n",
195 (unsigned long long)curino->st.st_dev,
196 (unsigned long long)curino->st.st_ino);
199 if (S_ISLNK(cur->type)) {
200 char slink[PATH_MAX+1];
203 llen = readlink(path, slink, sizeof(slink) - 1);
205 err(1, "Readlink `%s'", path);
207 cur->symlink = estrdup(slink);
210 assert(first != NULL);
212 for (cur = first->next; cur != NULL; cur = cur->next)
214 if (closedir(dirp) == -1)
215 err(1, "Can't closedir `%s/%s'", root, dir);
220 create_fsnode(const char *root, const char *path, const char *name,
225 cur = ecalloc(1, sizeof(*cur));
226 cur->path = estrdup(path);
227 cur->name = estrdup(name);
228 cur->inode = ecalloc(1, sizeof(*cur->inode));
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);
340 apply_specdir(const char *dir, NODE *specnode, fsnode *dirnode, int speconly)
342 char path[MAXPATHLEN + 1];
346 assert(specnode != NULL);
347 assert(dirnode != NULL);
349 if (debug & DEBUG_APPLY_SPECFILE)
350 printf("apply_specdir: %s %p %p\n", dir, specnode, dirnode);
352 if (specnode->type != F_DIR)
353 errx(1, "Specfile node `%s/%s' is not a directory",
354 dir, specnode->name);
355 if (dirnode->type != S_IFDIR)
356 errx(1, "Directory node `%s/%s' is not a directory",
359 apply_specentry(dir, specnode, dirnode);
361 /* Remove any filesystem nodes not found in specfile */
362 /* XXX inefficient. This is O^2 in each dir and it would
363 * have been better never to have walked this part of the tree
368 assert(dirnode->name[0] == '.' && dirnode->name[1] == '\0');
369 for (curfsnode = dirnode->next; curfsnode != NULL; curfsnode = next) {
370 next = curfsnode->next;
371 for (curnode = specnode->child; curnode != NULL;
372 curnode = curnode->next) {
373 if (strcmp(curnode->name, curfsnode->name) == 0)
376 if (curnode == NULL) {
377 if (debug & DEBUG_APPLY_SPECONLY) {
378 printf("apply_specdir: trimming %s/%s %p\n", dir, curfsnode->name, curfsnode);
380 free_fsnodes(curfsnode);
385 /* now walk specnode->child matching up with dirnode */
386 for (curnode = specnode->child; curnode != NULL;
387 curnode = curnode->next) {
388 if (debug & DEBUG_APPLY_SPECENTRY)
389 printf("apply_specdir: spec %s\n",
391 for (curfsnode = dirnode->next; curfsnode != NULL;
392 curfsnode = curfsnode->next) {
393 #if 0 /* too verbose for now */
394 if (debug & DEBUG_APPLY_SPECENTRY)
395 printf("apply_specdir: dirent %s\n",
398 if (strcmp(curnode->name, curfsnode->name) == 0)
401 if ((size_t)snprintf(path, sizeof(path), "%s/%s", dir,
402 curnode->name) >= sizeof(path))
403 errx(1, "Pathname too long.");
404 if (curfsnode == NULL) { /* need new entry */
408 * don't add optional spec entries
409 * that lack an existing fs entry
411 if ((curnode->flags & F_OPT) &&
412 lstat(path, &stbuf) == -1)
415 /* check that enough info is provided */
416 #define NODETEST(t, m) \
418 errx(1, "`%s': %s not provided", path, m)
419 NODETEST(curnode->flags & F_TYPE, "type");
420 NODETEST(curnode->flags & F_MODE, "mode");
421 /* XXX: require F_TIME ? */
422 NODETEST(curnode->flags & F_GID ||
423 curnode->flags & F_GNAME, "group");
424 NODETEST(curnode->flags & F_UID ||
425 curnode->flags & F_UNAME, "user");
426 /* if (curnode->type == F_BLOCK || curnode->type == F_CHAR)
427 NODETEST(curnode->flags & F_DEV,
431 if (debug & DEBUG_APPLY_SPECFILE)
432 printf("apply_specdir: adding %s\n",
434 /* build minimal fsnode */
435 memset(&stbuf, 0, sizeof(stbuf));
436 stbuf.st_mode = nodetoino(curnode->type);
438 stbuf.st_mtime = stbuf.st_atime =
439 stbuf.st_ctime = start_time.tv_sec;
440 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
441 stbuf.st_mtimensec = stbuf.st_atimensec =
442 stbuf.st_ctimensec = start_time.tv_nsec;
444 curfsnode = create_fsnode(".", ".", curnode->name,
446 curfsnode->parent = dirnode->parent;
447 curfsnode->first = dirnode;
448 curfsnode->next = dirnode->next;
449 dirnode->next = curfsnode;
450 if (curfsnode->type == S_IFDIR) {
451 /* for dirs, make "." entry as well */
452 curfsnode->child = create_fsnode(".", ".", ".",
454 curfsnode->child->parent = curfsnode;
455 curfsnode->child->first = curfsnode->child;
457 if (curfsnode->type == S_IFLNK) {
458 assert(curnode->slink != NULL);
459 /* for symlinks, copy the target */
460 curfsnode->symlink = estrdup(curnode->slink);
463 apply_specentry(dir, curnode, curfsnode);
464 if (curnode->type == F_DIR) {
465 if (curfsnode->type != S_IFDIR)
466 errx(1, "`%s' is not a directory", path);
467 assert (curfsnode->child != NULL);
468 apply_specdir(path, curnode, curfsnode->child, speconly);
474 apply_specentry(const char *dir, NODE *specnode, fsnode *dirnode)
477 assert(specnode != NULL);
478 assert(dirnode != NULL);
480 if (nodetoino(specnode->type) != dirnode->type)
481 errx(1, "`%s/%s' type mismatch: specfile %s, tree %s",
482 dir, specnode->name, inode_type(nodetoino(specnode->type)),
483 inode_type(dirnode->type));
485 if (debug & DEBUG_APPLY_SPECENTRY)
486 printf("apply_specentry: %s/%s\n", dir, dirnode->name);
488 #define ASEPRINT(t, b, o, n) \
489 if (debug & DEBUG_APPLY_SPECENTRY) \
490 printf("\t\t\tchanging %s from " b " to " b "\n", \
493 if (specnode->flags & (F_GID | F_GNAME)) {
494 ASEPRINT("gid", "%d",
495 dirnode->inode->st.st_gid, specnode->st_gid);
496 dirnode->inode->st.st_gid = specnode->st_gid;
498 if (specnode->flags & F_MODE) {
499 ASEPRINT("mode", "%#o",
500 dirnode->inode->st.st_mode & ALLPERMS, specnode->st_mode);
501 dirnode->inode->st.st_mode &= ~ALLPERMS;
502 dirnode->inode->st.st_mode |= (specnode->st_mode & ALLPERMS);
504 /* XXX: ignoring F_NLINK for now */
505 if (specnode->flags & F_SIZE) {
506 ASEPRINT("size", "%lld",
507 (long long)dirnode->inode->st.st_size,
508 (long long)specnode->st_size);
509 dirnode->inode->st.st_size = specnode->st_size;
511 if (specnode->flags & F_SLINK) {
512 assert(dirnode->symlink != NULL);
513 assert(specnode->slink != NULL);
514 ASEPRINT("symlink", "%s", dirnode->symlink, specnode->slink);
515 free(dirnode->symlink);
516 dirnode->symlink = estrdup(specnode->slink);
518 if (specnode->flags & F_TIME) {
519 ASEPRINT("time", "%ld",
520 (long)dirnode->inode->st.st_mtime,
521 (long)specnode->st_mtimespec.tv_sec);
522 dirnode->inode->st.st_mtime = specnode->st_mtimespec.tv_sec;
523 dirnode->inode->st.st_atime = specnode->st_mtimespec.tv_sec;
524 dirnode->inode->st.st_ctime = start_time.tv_sec;
525 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
526 dirnode->inode->st.st_mtimensec = specnode->st_mtimespec.tv_nsec;
527 dirnode->inode->st.st_atimensec = specnode->st_mtimespec.tv_nsec;
528 dirnode->inode->st.st_ctimensec = start_time.tv_nsec;
531 if (specnode->flags & (F_UID | F_UNAME)) {
532 ASEPRINT("uid", "%d",
533 dirnode->inode->st.st_uid, specnode->st_uid);
534 dirnode->inode->st.st_uid = specnode->st_uid;
536 #if HAVE_STRUCT_STAT_ST_FLAGS
537 if (specnode->flags & F_FLAGS) {
538 ASEPRINT("flags", "%#lX",
539 (unsigned long)dirnode->inode->st.st_flags,
540 (unsigned long)specnode->st_flags);
541 dirnode->inode->st.st_flags = specnode->st_flags;
544 /* if (specnode->flags & F_DEV) {
545 ASEPRINT("rdev", "%#llx",
546 (unsigned long long)dirnode->inode->st.st_rdev,
547 (unsigned long long)specnode->st_rdev);
548 dirnode->inode->st.st_rdev = specnode->st_rdev;
552 dirnode->flags |= FSNODE_F_HASSPEC;
558 * dump the fsnodes from `cur'
561 dump_fsnodes(fsnode *root)
564 char path[MAXPATHLEN + 1];
566 printf("dump_fsnodes: %s %p\n", root->path, root);
567 for (cur = root; cur != NULL; cur = cur->next) {
568 if (snprintf(path, sizeof(path), "%s/%s", cur->path,
569 cur->name) >= (int)sizeof(path))
570 errx(1, "Pathname too long.");
572 if (debug & DEBUG_DUMP_FSNODES_VERBOSE)
573 printf("cur=%8p parent=%8p first=%8p ",
574 cur, cur->parent, cur->first);
575 printf("%7s: %s", inode_type(cur->type), path);
576 if (S_ISLNK(cur->type)) {
577 assert(cur->symlink != NULL);
578 printf(" -> %s", cur->symlink);
580 assert (cur->symlink == NULL);
582 if (cur->inode->nlink > 1)
583 printf(", nlinks=%d", cur->inode->nlink);
587 assert (cur->type == S_IFDIR);
588 dump_fsnodes(cur->child);
591 printf("dump_fsnodes: finished %s/%s\n", root->path, root->name);
597 * for a given inode type `mode', return a descriptive string.
598 * for most cases, uses inotype() from mtree/misc.c
601 inode_type(mode_t mode)
615 /* XXX should not happen but handle them */
626 * return pointer to fsinode matching `entry's st_ino & st_dev if it exists,
627 * otherwise add `entry' to table and return NULL
629 /* This was borrowed from du.c and tweaked to keep an fsnode
630 * pointer instead. -- dbj@netbsd.org
633 link_check(fsinode *entry)
635 static struct entry {
638 static int htshift; /* log(allocated size) */
639 static int htmask; /* allocated size - 1 */
640 static int htused; /* 2*number of insertions */
643 /* this constant is (1<<64)/((1+sqrt(5))/2)
644 * aka (word size)/(golden ratio)
646 const uint64_t HTCONST = 11400714819323198485ULL;
647 const int HTBITS = 64;
649 /* Never store zero in hashtable */
652 /* Extend hash table if necessary, keep load under 0.5 */
653 if (htused<<1 >= htmask) {
654 struct entry *ohtable;
657 htshift = 10; /* starting hashtable size */
659 htshift++; /* exponential hashtable growth */
661 htmask = (1 << htshift) - 1;
665 htable = ecalloc(htmask+1, sizeof(*htable));
666 /* populate newly allocated hashtable */
669 for (i = 0; i <= htmask>>1; i++)
671 link_check(ohtable[i].data);
676 /* multiplicative hashing */
677 tmp = entry->st.st_dev;
679 tmp |= entry->st.st_ino;
681 h = tmp >> (HTBITS - htshift);
682 h2 = 1 | ( tmp >> (HTBITS - (htshift<<1) - 1)); /* must be odd */
684 /* open address hashtable search with double hash probing */
685 while (htable[h].data) {
686 if ((htable[h].data->st.st_ino == entry->st.st_ino) &&
687 (htable[h].data->st.st_dev == entry->st.st_dev)) {
688 return htable[h].data;
690 h = (h + h2) & htmask;
693 /* Insert the current entry into hashtable */
694 htable[h].data = entry;