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