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