xref: /netbsd-src/usr.sbin/makefs/walk.c (revision 345cf9fb81bd0411c53e25d62cd93bdcaa865312)
1 /*	$NetBSD: walk.c,v 1.33 2023/12/28 12:13:55 tsutsui 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 #if HAVE_NBTOOL_CONFIG_H
39 #include "nbtool_config.h"
40 #endif
41 
42 #include <sys/cdefs.h>
43 #if defined(__RCSID) && !defined(__lint)
44 __RCSID("$NetBSD: walk.c,v 1.33 2023/12/28 12:13:55 tsutsui Exp $");
45 #endif	/* !__lint */
46 
47 #include <sys/param.h>
48 #include <sys/stat.h>
49 
50 #include <assert.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <stdio.h>
54 #include <dirent.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <util.h>
59 
60 #include "makefs.h"
61 #include "mtree.h"
62 
63 static	void	 apply_specdir(const char *, NODE *, fsnode *, int);
64 static	void	 apply_specentry(const char *, NODE *, fsnode *);
65 static	fsnode	*create_fsnode(const char *, const char *, const char *,
66 			       struct stat *);
67 static	fsinode	*link_check(fsinode *);
68 
69 
70 /*
71  * walk_dir --
72  *	build a tree of fsnodes from `root' and `dir', with a parent
73  *	fsnode of `parent' (which may be NULL for the root of the tree).
74  *	append the tree to a fsnode of `join' if it is not NULL.
75  *	each "level" is a directory, with the "." entry guaranteed to be
76  *	at the start of the list, and without ".." entries.
77  */
78 fsnode *
79 walk_dir(const char *root, const char *dir, fsnode *parent, fsnode *join,
80     int replace, int follow)
81 {
82 	fsnode		*first, *cur, *prev, *last;
83 	DIR		*dirp;
84 	struct dirent	*dent;
85 	char		path[MAXPATHLEN + 1];
86 	struct stat	stbuf;
87 	char		*name, *rp;
88 	int		dot, len;
89 
90 	assert(root != NULL);
91 	assert(dir != NULL);
92 
93 	len = snprintf(path, sizeof(path), "%s/%s", root, dir);
94 	if (len >= (int)sizeof(path))
95 		errx(EXIT_FAILURE, "Pathname too long.");
96 	if (debug & DEBUG_WALK_DIR)
97 		printf("walk_dir: %s %p\n", path, parent);
98 	if ((dirp = opendir(path)) == NULL)
99 		err(EXIT_FAILURE, "Can't opendir `%s'", path);
100 	rp = path + strlen(root) + 1;
101 	if (join != NULL) {
102 		first = cur = join;
103 		while (cur->next != NULL)
104 			cur = cur->next;
105 		prev = last = cur;
106 	} else
107 		last = first = prev = NULL;
108 	while ((dent = readdir(dirp)) != NULL) {
109 		name = dent->d_name;
110 		dot = 0;
111 		if (name[0] == '.')
112 			switch (name[1]) {
113 			case '\0':	/* "." */
114 				if (join != NULL)
115 					continue;
116 				dot = 1;
117 				break;
118 			case '.':	/* ".." */
119 				if (name[2] == '\0')
120 					continue;
121 				/* FALLTHROUGH */
122 			default:
123 				dot = 0;
124 			}
125 		if (debug & DEBUG_WALK_DIR_NODE)
126 			printf("scanning %s/%s/%s\n", root, dir, name);
127 		if (snprintf(path + len, sizeof(path) - len, "/%s", name) >=
128 		    (int)sizeof(path) - len)
129 			errx(EXIT_FAILURE, "Pathname too long.");
130 		if (follow) {
131 			if (stat(path, &stbuf) == -1)
132 				err(EXIT_FAILURE, "Can't stat `%s'", path);
133 		} else {
134 			if (lstat(path, &stbuf) == -1)
135 				err(EXIT_FAILURE, "Can't lstat `%s'", path);
136 		}
137 #ifdef S_ISSOCK
138 		if (S_ISSOCK(stbuf.st_mode & S_IFMT)) {
139 			if (debug & DEBUG_WALK_DIR_NODE)
140 				printf("  skipping socket %s\n", path);
141 			continue;
142 		}
143 #endif
144 
145 		if (join != NULL) {
146 			cur = join->next;
147 			for (;;) {
148 				if (cur == NULL || strcmp(cur->name, name) == 0)
149 					break;
150 				if (cur == last) {
151 					cur = NULL;
152 					break;
153 				}
154 				cur = cur->next;
155 			}
156 			if (cur != NULL) {
157 				if (S_ISDIR(cur->type) &&
158 				    S_ISDIR(stbuf.st_mode)) {
159 					if (debug & DEBUG_WALK_DIR_NODE)
160 						printf("merging %s with %p\n",
161 						    path, cur->child);
162 					cur->child = walk_dir(root, rp, cur,
163 					    cur->child, replace, follow);
164 					continue;
165 				}
166 				if (!replace)
167 					errx(EXIT_FAILURE,
168 					    "Can't merge %s `%s' with "
169 					    "existing %s",
170 					    inode_type(stbuf.st_mode), path,
171 					    inode_type(cur->type));
172 				else {
173 					if (debug & DEBUG_WALK_DIR_NODE)
174 						printf("replacing %s %s\n",
175 						    inode_type(stbuf.st_mode),
176 						    path);
177 					if (cur == join->next)
178 						join->next = cur->next;
179 					else {
180 						fsnode *p;
181 						for (p = join->next;
182 						    p->next != cur; p = p->next)
183 							continue;
184 						p->next = cur->next;
185 					}
186 					free(cur);
187 				}
188 			}
189 		}
190 
191 		cur = create_fsnode(root, dir, name, &stbuf);
192 		cur->parent = parent;
193 		if (dot) {
194 				/* ensure "." is at the start of the list */
195 			cur->next = first;
196 			first = cur;
197 			if (! prev)
198 				prev = cur;
199 			cur->first = first;
200 		} else {			/* not "." */
201 			if (prev)
202 				prev->next = cur;
203 			prev = cur;
204 			if (!first)
205 				first = cur;
206 			cur->first = first;
207 			if (S_ISDIR(cur->type)) {
208 				cur->child = walk_dir(root, rp, cur, NULL,
209 				    replace, follow);
210 				continue;
211 			}
212 		}
213 		if (stbuf.st_nlink > 1) {
214 			fsinode	*curino;
215 
216 			curino = link_check(cur->inode);
217 			if (curino != NULL) {
218 				free(cur->inode);
219 				cur->inode = curino;
220 				cur->inode->nlink++;
221 				if (debug & DEBUG_WALK_DIR_LINKCHECK)
222 					printf("link_check: found [%llu, %llu]\n",
223 					    (unsigned long long)curino->st.st_dev,
224 					    (unsigned long long)curino->st.st_ino);
225 			}
226 		}
227 		if (S_ISLNK(cur->type)) {
228 			char	slink[PATH_MAX+1];
229 			int	llen;
230 
231 			llen = readlink(path, slink, sizeof(slink) - 1);
232 			if (llen == -1)
233 				err(EXIT_FAILURE, "Readlink `%s'", path);
234 			slink[llen] = '\0';
235 			cur->symlink = estrdup(slink);
236 		}
237 	}
238 	assert(first != NULL);
239 	if (join == NULL)
240 		for (cur = first->next; cur != NULL; cur = cur->next)
241 			cur->first = first;
242 	if (closedir(dirp) == -1)
243 		err(EXIT_FAILURE, "Can't closedir `%s/%s'", root, dir);
244 	return (first);
245 }
246 
247 static fsnode *
248 create_fsnode(const char *root, const char *path, const char *name,
249     struct stat *stbuf)
250 {
251 	fsnode *cur;
252 
253 	cur = ecalloc(1, sizeof(*cur));
254 	cur->path = estrdup(path);
255 	cur->name = estrdup(name);
256 	cur->inode = ecalloc(1, sizeof(*cur->inode));
257 	cur->root = root;
258 	cur->type = stbuf->st_mode & S_IFMT;
259 	cur->inode->nlink = 1;
260 	cur->inode->st = *stbuf;
261 	if (stampst.st_ino) {
262 		cur->inode->st.st_atime = stampst.st_atime;
263 		cur->inode->st.st_mtime = stampst.st_mtime;
264 		cur->inode->st.st_ctime = stampst.st_ctime;
265 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
266 		cur->inode->st.st_atimensec = stampst.st_atimensec;
267 		cur->inode->st.st_mtimensec = stampst.st_mtimensec;
268 		cur->inode->st.st_ctimensec = stampst.st_ctimensec;
269 #endif
270 #if HAVE_STRUCT_STAT_BIRTHTIME
271 		cur->inode->st.st_birthtime = stampst.st_birthtime;
272 		cur->inode->st.st_birthtimensec = stampst.st_birthtimensec;
273 #endif
274 	}
275 	return (cur);
276 }
277 
278 /*
279  * free_fsnodes --
280  *	Removes node from tree and frees it and all of
281  *   its descendents.
282  */
283 void
284 free_fsnodes(fsnode *node)
285 {
286 	fsnode	*cur, *next;
287 
288 	assert(node != NULL);
289 
290 	/* for ".", start with actual parent node */
291 	if (node->first == node) {
292 		assert(node->name[0] == '.' && node->name[1] == '\0');
293 		if (node->parent) {
294 			assert(node->parent->child == node);
295 			node = node->parent;
296 		}
297 	}
298 
299 	/* Find ourselves in our sibling list and unlink */
300 	if (node->first != node) {
301 		for (cur = node->first; cur->next; cur = cur->next) {
302 			if (cur->next == node) {
303 				cur->next = node->next;
304 				node->next = NULL;
305 				break;
306 			}
307 		}
308 	}
309 
310 	for (cur = node; cur != NULL; cur = next) {
311 		next = cur->next;
312 		if (cur->child) {
313 			cur->child->parent = NULL;
314 			free_fsnodes(cur->child);
315 		}
316 		if (cur->inode->nlink-- == 1)
317 			free(cur->inode);
318 		if (cur->symlink)
319 			free(cur->symlink);
320 		free(cur->path);
321 		free(cur->name);
322 		free(cur);
323 	}
324 }
325 
326 /*
327  * apply_specfile --
328  *	read in the mtree(8) specfile, and apply it to the tree
329  *	at dir,parent. parameters in parent on equivalent types
330  *	will be changed to those found in specfile, and missing
331  *	entries will be added.
332  */
333 void
334 apply_specfile(const char *specfile, const char *dir, fsnode *parent, int speconly)
335 {
336 	struct timeval	 start;
337 	FILE	*fp;
338 	NODE	*root;
339 
340 	assert(specfile != NULL);
341 	assert(parent != NULL);
342 
343 	if (debug & DEBUG_APPLY_SPECFILE)
344 		printf("apply_specfile: %s, %s %p\n", specfile, dir, parent);
345 
346 				/* read in the specfile */
347 	if ((fp = fopen(specfile, "r")) == NULL)
348 		err(EXIT_FAILURE, "Can't open `%s'", specfile);
349 	TIMER_START(start);
350 	root = spec(fp);
351 	TIMER_RESULTS(start, "spec");
352 	if (fclose(fp) == EOF)
353 		err(EXIT_FAILURE, "Can't close `%s'", specfile);
354 
355 				/* perform some sanity checks */
356 	if (root == NULL)
357 		errx(EXIT_FAILURE,
358 		    "Specfile `%s' did not contain a tree", specfile);
359 	assert(strcmp(root->name, ".") == 0);
360 	assert(root->type == F_DIR);
361 
362 				/* merge in the changes */
363 	apply_specdir(dir, root, parent, speconly);
364 
365 	free_nodes(root);
366 }
367 
368 static void
369 apply_specdir(const char *dir, NODE *specnode, fsnode *dirnode, int speconly)
370 {
371 	char	 path[MAXPATHLEN + 1];
372 	NODE	*curnode;
373 	fsnode	*curfsnode;
374 
375 	assert(specnode != NULL);
376 	assert(dirnode != NULL);
377 
378 	if (debug & DEBUG_APPLY_SPECFILE)
379 		printf("apply_specdir: %s %p %p\n", dir, specnode, dirnode);
380 
381 	if (specnode->type != F_DIR)
382 		errx(EXIT_FAILURE, "Specfile node `%s/%s' is not a directory",
383 		    dir, specnode->name);
384 	if (dirnode->type != S_IFDIR)
385 		errx(EXIT_FAILURE, "Directory node `%s/%s' is not a directory",
386 		    dir, dirnode->name);
387 
388 	apply_specentry(dir, specnode, dirnode);
389 
390 	/* Remove any filesystem nodes not found in specfile */
391 	/* XXX inefficient.  This is O^2 in each dir and it would
392 	 * have been better never to have walked this part of the tree
393 	 * to begin with
394 	 */
395 	if (speconly) {
396 		fsnode *next;
397 		assert(dirnode->name[0] == '.' && dirnode->name[1] == '\0');
398 		for (curfsnode = dirnode->next; curfsnode != NULL; curfsnode = next) {
399 			next = curfsnode->next;
400 			for (curnode = specnode->child; curnode != NULL;
401 			     curnode = curnode->next) {
402 				if (strcmp(curnode->name, curfsnode->name) == 0)
403 					break;
404 			}
405 			if (curnode == NULL) {
406 				if (debug & DEBUG_APPLY_SPECONLY) {
407 					printf("apply_specdir: trimming %s/%s %p\n", dir, curfsnode->name, curfsnode);
408 				}
409 				free_fsnodes(curfsnode);
410 			}
411 		}
412 	}
413 
414 			/* now walk specnode->child matching up with dirnode */
415 	for (curnode = specnode->child; curnode != NULL;
416 	    curnode = curnode->next) {
417 		if (debug & DEBUG_APPLY_SPECENTRY)
418 			printf("apply_specdir:  spec %s\n",
419 			    curnode->name);
420 		for (curfsnode = dirnode->next; curfsnode != NULL;
421 		    curfsnode = curfsnode->next) {
422 #if 0	/* too verbose for now */
423 			if (debug & DEBUG_APPLY_SPECENTRY)
424 				printf("apply_specdir:  dirent %s\n",
425 				    curfsnode->name);
426 #endif
427 			if (strcmp(curnode->name, curfsnode->name) == 0)
428 				break;
429 		}
430 		if ((size_t)snprintf(path, sizeof(path), "%s/%s",
431 		    dir, curnode->name) >= sizeof(path))
432 			errx(EXIT_FAILURE, "Pathname too long.");
433 		if (curfsnode == NULL) {	/* need new entry */
434 			struct stat	stbuf;
435 
436 					    /*
437 					     * don't add optional spec entries
438 					     * that lack an existing fs entry
439 					     */
440 			if ((curnode->flags & F_OPT) &&
441 			    lstat(path, &stbuf) == -1)
442 					continue;
443 
444 					/* check that enough info is provided */
445 #define NODETEST(t, m)							\
446 			if (!(t))					\
447 				errx(EXIT_FAILURE,			\
448 				    "`%s': %s not provided", path, m)
449 			NODETEST(curnode->flags & F_TYPE, "type");
450 			NODETEST(curnode->flags & F_MODE, "mode");
451 				/* XXX: require F_TIME ? */
452 			NODETEST(curnode->flags & F_GID ||
453 			    curnode->flags & F_GNAME, "group");
454 			NODETEST(curnode->flags & F_UID ||
455 			    curnode->flags & F_UNAME, "user");
456 			if (curnode->type == F_BLOCK || curnode->type == F_CHAR)
457 				NODETEST(curnode->flags & F_DEV,
458 				    "device number");
459 #undef NODETEST
460 
461 			if (debug & DEBUG_APPLY_SPECFILE)
462 				printf("apply_specdir: adding %s\n",
463 				    curnode->name);
464 					/* build minimal fsnode */
465 			memset(&stbuf, 0, sizeof(stbuf));
466 			stbuf.st_mode = nodetoino(curnode->type);
467 			stbuf.st_nlink = 1;
468 			stbuf.st_mtime = stbuf.st_atime =
469 			    stbuf.st_ctime = start_time.tv_sec;
470 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
471 			stbuf.st_mtimensec = stbuf.st_atimensec =
472 			    stbuf.st_ctimensec = start_time.tv_nsec;
473 #endif
474 			curfsnode = create_fsnode(".", ".", curnode->name,
475 			    &stbuf);
476 			curfsnode->parent = dirnode->parent;
477 			curfsnode->first = dirnode;
478 			curfsnode->next = dirnode->next;
479 			dirnode->next = curfsnode;
480 			if (curfsnode->type == S_IFDIR) {
481 					/* for dirs, make "." entry as well */
482 				curfsnode->child = create_fsnode(".", ".", ".",
483 				    &stbuf);
484 				curfsnode->child->parent = curfsnode;
485 				curfsnode->child->first = curfsnode->child;
486 			}
487 			if (curfsnode->type == S_IFLNK) {
488 				assert(curnode->slink != NULL);
489 					/* for symlinks, copy the target */
490 				curfsnode->symlink = estrdup(curnode->slink);
491 			}
492 		}
493 		apply_specentry(dir, curnode, curfsnode);
494 		if (curnode->type == F_DIR) {
495 			if (curfsnode->type != S_IFDIR)
496 				errx(EXIT_FAILURE,
497 				    "`%s' is not a directory", path);
498 			assert (curfsnode->child != NULL);
499 			apply_specdir(path, curnode, curfsnode->child, speconly);
500 		}
501 	}
502 }
503 
504 static void
505 apply_specentry(const char *dir, NODE *specnode, fsnode *dirnode)
506 {
507 
508 	assert(specnode != NULL);
509 	assert(dirnode != NULL);
510 
511 	if (nodetoino(specnode->type) != dirnode->type)
512 		errx(EXIT_FAILURE,
513 		    "`%s/%s' type mismatch: specfile %s, tree %s",
514 		    dir, specnode->name, inode_type(nodetoino(specnode->type)),
515 		    inode_type(dirnode->type));
516 
517 	if (debug & DEBUG_APPLY_SPECENTRY)
518 		printf("apply_specentry: %s/%s\n", dir, dirnode->name);
519 
520 #define ASEPRINT(t, b, o, n) \
521 		if (debug & DEBUG_APPLY_SPECENTRY) \
522 			printf("\t\t\tchanging %s from " b " to " b "\n", \
523 			    t, o, n)
524 
525 	if (specnode->flags & (F_GID | F_GNAME)) {
526 		ASEPRINT("gid", "%d",
527 		    dirnode->inode->st.st_gid, specnode->st_gid);
528 		dirnode->inode->st.st_gid = specnode->st_gid;
529 	}
530 	if (specnode->flags & F_MODE) {
531 		ASEPRINT("mode", "%#o",
532 		    dirnode->inode->st.st_mode & ALLPERMS, specnode->st_mode);
533 		dirnode->inode->st.st_mode &= ~ALLPERMS;
534 		dirnode->inode->st.st_mode |= (specnode->st_mode & ALLPERMS);
535 	}
536 		/* XXX: ignoring F_NLINK for now */
537 	if (specnode->flags & F_SIZE) {
538 		ASEPRINT("size", "%lld",
539 		    (long long)dirnode->inode->st.st_size,
540 		    (long long)specnode->st_size);
541 		dirnode->inode->st.st_size = specnode->st_size;
542 	}
543 	if (specnode->flags & F_SLINK) {
544 		assert(dirnode->symlink != NULL);
545 		assert(specnode->slink != NULL);
546 		ASEPRINT("symlink", "%s", dirnode->symlink, specnode->slink);
547 		free(dirnode->symlink);
548 		dirnode->symlink = estrdup(specnode->slink);
549 	}
550 	if (specnode->flags & F_TIME) {
551 		ASEPRINT("time", "%ld",
552 		    (long)dirnode->inode->st.st_mtime,
553 		    (long)specnode->st_mtimespec.tv_sec);
554 		dirnode->inode->st.st_mtime =		specnode->st_mtimespec.tv_sec;
555 		dirnode->inode->st.st_atime =		specnode->st_mtimespec.tv_sec;
556 		dirnode->inode->st.st_ctime =		start_time.tv_sec;
557 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
558 		dirnode->inode->st.st_mtimensec =	specnode->st_mtimespec.tv_nsec;
559 		dirnode->inode->st.st_atimensec =	specnode->st_mtimespec.tv_nsec;
560 		dirnode->inode->st.st_ctimensec =	start_time.tv_nsec;
561 #endif
562 	}
563 	if (specnode->flags & (F_UID | F_UNAME)) {
564 		ASEPRINT("uid", "%d",
565 		    dirnode->inode->st.st_uid, specnode->st_uid);
566 		dirnode->inode->st.st_uid = specnode->st_uid;
567 	}
568 #if HAVE_STRUCT_STAT_ST_FLAGS
569 	if (specnode->flags & F_FLAGS) {
570 		ASEPRINT("flags", "%#lX",
571 		    (unsigned long)dirnode->inode->st.st_flags,
572 		    (unsigned long)specnode->st_flags);
573 		dirnode->inode->st.st_flags = specnode->st_flags;
574 	}
575 #endif
576 	if (specnode->flags & F_DEV) {
577 		ASEPRINT("rdev", "%#llx",
578 		    (unsigned long long)dirnode->inode->st.st_rdev,
579 		    (unsigned long long)specnode->st_rdev);
580 		dirnode->inode->st.st_rdev = specnode->st_rdev;
581 	}
582 #undef ASEPRINT
583 
584 	dirnode->flags |= FSNODE_F_HASSPEC;
585 }
586 
587 
588 /*
589  * dump_fsnodes --
590  *	dump the fsnodes from `cur'
591  */
592 void
593 dump_fsnodes(fsnode *root)
594 {
595 	fsnode	*cur;
596 	char	path[MAXPATHLEN + 1];
597 
598 	printf("dump_fsnodes: %s %p\n", root->path, root);
599 	for (cur = root; cur != NULL; cur = cur->next) {
600 		if (snprintf(path, sizeof(path), "%s/%s", cur->path,
601 		    cur->name) >= (int)sizeof(path))
602 			errx(EXIT_FAILURE, "Pathname too long.");
603 
604 		if (debug & DEBUG_DUMP_FSNODES_VERBOSE)
605 			printf("cur=%8p parent=%8p first=%8p ",
606 			    cur, cur->parent, cur->first);
607 		printf("%7s: %s", inode_type(cur->type), path);
608 		if (S_ISLNK(cur->type)) {
609 			assert(cur->symlink != NULL);
610 			printf(" -> %s", cur->symlink);
611 		} else {
612 			assert (cur->symlink == NULL);
613 		}
614 		if (cur->inode->nlink > 1)
615 			printf(", nlinks=%d", cur->inode->nlink);
616 		putchar('\n');
617 
618 		if (cur->child) {
619 			assert (cur->type == S_IFDIR);
620 			dump_fsnodes(cur->child);
621 		}
622 	}
623 	printf("dump_fsnodes: finished %s/%s\n", root->path, root->name);
624 }
625 
626 
627 /*
628  * inode_type --
629  *	for a given inode type `mode', return a descriptive string.
630  *	for most cases, uses inotype() from mtree/misc.c
631  */
632 const char *
633 inode_type(mode_t mode)
634 {
635 
636 	if (S_ISLNK(mode))
637 		return ("symlink");	/* inotype() returns "link"...  */
638 	return (inotype(mode));
639 }
640 
641 
642 /*
643  * link_check --
644  *	return pointer to fsinode matching `entry's st_ino & st_dev if it exists,
645  *	otherwise add `entry' to table and return NULL
646  */
647 /* This was borrowed from du.c and tweaked to keep an fsnode
648  * pointer instead. -- dbj@netbsd.org
649  */
650 static fsinode *
651 link_check(fsinode *entry)
652 {
653 	static struct entry {
654 		fsinode *data;
655 	} *htable;
656 	static int htshift;  /* log(allocated size) */
657 	static int htmask;   /* allocated size - 1 */
658 	static int htused;   /* 2*number of insertions */
659 	int h, h2;
660 	uint64_t tmp;
661 	/* this constant is (1<<64)/((1+sqrt(5))/2)
662 	 * aka (word size)/(golden ratio)
663 	 */
664 	const uint64_t HTCONST = 11400714819323198485ULL;
665 	const int HTBITS = 64;
666 
667 	/* Never store zero in hashtable */
668 	assert(entry);
669 
670 	/* Extend hash table if necessary, keep load under 0.5 */
671 	if (htused<<1 >= htmask) {
672 		struct entry *ohtable;
673 
674 		if (!htable)
675 			htshift = 10;   /* starting hashtable size */
676 		else
677 			htshift++;   /* exponential hashtable growth */
678 
679 		htmask  = (1 << htshift) - 1;
680 		htused = 0;
681 
682 		ohtable = htable;
683 		htable = ecalloc(htmask+1, sizeof(*htable));
684 		/* populate newly allocated hashtable */
685 		if (ohtable) {
686 			int i;
687 			for (i = 0; i <= htmask>>1; i++)
688 				if (ohtable[i].data)
689 					link_check(ohtable[i].data);
690 			free(ohtable);
691 		}
692 	}
693 
694 	/* multiplicative hashing */
695 	tmp = entry->st.st_dev;
696 	tmp <<= HTBITS>>1;
697 	tmp |=  entry->st.st_ino;
698 	tmp *= HTCONST;
699 	h  = tmp >> (HTBITS - htshift);
700 	h2 = 1 | ( tmp >> (HTBITS - (htshift<<1) - 1)); /* must be odd */
701 
702 	/* open address hashtable search with double hash probing */
703 	while (htable[h].data) {
704 		if ((htable[h].data->st.st_ino == entry->st.st_ino) &&
705 		    (htable[h].data->st.st_dev == entry->st.st_dev)) {
706 			return htable[h].data;
707 		}
708 		h = (h + h2) & htmask;
709 	}
710 
711 	/* Insert the current entry into hashtable */
712 	htable[h].data = entry;
713 	htused++;
714 	return NULL;
715 }
716