Home | History | Annotate | Line # | Download | only in makefs
walk.c revision 1.20
      1  1.20      dbj /*	$NetBSD: walk.c,v 1.20 2006/10/10 01:39:10 dbj Exp $	*/
      2   1.1    lukem 
      3   1.1    lukem /*
      4   1.5    lukem  * Copyright (c) 2001 Wasabi Systems, Inc.
      5   1.1    lukem  * All rights reserved.
      6   1.1    lukem  *
      7   1.1    lukem  * Written by Luke Mewburn for Wasabi Systems, Inc.
      8   1.1    lukem  *
      9   1.1    lukem  * Redistribution and use in source and binary forms, with or without
     10   1.1    lukem  * modification, are permitted provided that the following conditions
     11   1.1    lukem  * are met:
     12   1.1    lukem  * 1. Redistributions of source code must retain the above copyright
     13   1.1    lukem  *    notice, this list of conditions and the following disclaimer.
     14   1.1    lukem  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1    lukem  *    notice, this list of conditions and the following disclaimer in the
     16   1.1    lukem  *    documentation and/or other materials provided with the distribution.
     17   1.1    lukem  * 3. All advertising materials mentioning features or use of this software
     18   1.1    lukem  *    must display the following acknowledgement:
     19   1.1    lukem  *      This product includes software developed for the NetBSD Project by
     20   1.1    lukem  *      Wasabi Systems, Inc.
     21   1.1    lukem  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22   1.1    lukem  *    or promote products derived from this software without specific prior
     23   1.1    lukem  *    written permission.
     24   1.1    lukem  *
     25   1.1    lukem  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26   1.1    lukem  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27   1.1    lukem  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28   1.1    lukem  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29   1.1    lukem  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30   1.1    lukem  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31   1.1    lukem  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32   1.1    lukem  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33   1.1    lukem  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34   1.1    lukem  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35   1.1    lukem  * POSSIBILITY OF SUCH DAMAGE.
     36   1.1    lukem  */
     37   1.1    lukem 
     38  1.17      jmc #if HAVE_NBTOOL_CONFIG_H
     39  1.17      jmc #include "nbtool_config.h"
     40  1.17      jmc #endif
     41  1.17      jmc 
     42   1.2    lukem #include <sys/cdefs.h>
     43   1.8       tv #if defined(__RCSID) && !defined(__lint)
     44  1.20      dbj __RCSID("$NetBSD: walk.c,v 1.20 2006/10/10 01:39:10 dbj Exp $");
     45   1.2    lukem #endif	/* !__lint */
     46   1.1    lukem 
     47   1.1    lukem #include <sys/param.h>
     48   1.1    lukem 
     49   1.1    lukem #include <assert.h>
     50   1.1    lukem #include <errno.h>
     51   1.1    lukem #include <fcntl.h>
     52   1.1    lukem #include <stdio.h>
     53   1.1    lukem #include <dirent.h>
     54   1.1    lukem #include <stdlib.h>
     55   1.1    lukem #include <string.h>
     56   1.1    lukem #include <unistd.h>
     57  1.19   dyoung #include <sys/stat.h>
     58   1.1    lukem 
     59   1.1    lukem #include "makefs.h"
     60   1.1    lukem #include "mtree.h"
     61   1.1    lukem 
     62   1.1    lukem static	void	 apply_specdir(const char *, NODE *, fsnode *);
     63   1.1    lukem static	void	 apply_specentry(const char *, NODE *, fsnode *);
     64   1.1    lukem static	fsnode	*create_fsnode(const char *, struct stat *);
     65   1.6    lukem static	fsinode	*link_check(fsinode *);
     66   1.1    lukem 
     67   1.1    lukem 
     68   1.1    lukem /*
     69   1.1    lukem  * walk_dir --
     70   1.1    lukem  *	build a tree of fsnodes from `dir', with a parent fsnode of `parent'
     71   1.1    lukem  *	(which may be NULL for the root of the tree).
     72   1.1    lukem  *	each "level" is a directory, with the "." entry guaranteed to be
     73   1.1    lukem  *	at the start of the list, and without ".." entries.
     74   1.1    lukem  */
     75   1.1    lukem fsnode *
     76   1.1    lukem walk_dir(const char *dir, fsnode *parent)
     77   1.1    lukem {
     78   1.1    lukem 	fsnode		*first, *cur, *prev;
     79   1.1    lukem 	DIR		*dirp;
     80   1.1    lukem 	struct dirent	*dent;
     81   1.1    lukem 	char		path[MAXPATHLEN + 1];
     82   1.1    lukem 	struct stat	stbuf;
     83   1.1    lukem 
     84   1.1    lukem 	assert(dir != NULL);
     85   1.1    lukem 
     86   1.1    lukem 	if (debug & DEBUG_WALK_DIR)
     87   1.1    lukem 		printf("walk_dir: %s %p\n", dir, parent);
     88   1.1    lukem 	if ((dirp = opendir(dir)) == NULL)
     89   1.1    lukem 		err(1, "Can't opendir `%s'", dir);
     90   1.1    lukem 	first = prev = NULL;
     91   1.1    lukem 	while ((dent = readdir(dirp)) != NULL) {
     92   1.1    lukem 		if (strcmp(dent->d_name, "..") == 0)
     93   1.1    lukem 			continue;
     94   1.1    lukem 		if (debug & DEBUG_WALK_DIR_NODE)
     95   1.1    lukem 			printf("scanning %s/%s\n", dir, dent->d_name);
     96   1.1    lukem 		if (snprintf(path, sizeof(path), "%s/%s", dir, dent->d_name)
     97   1.1    lukem 		    >= sizeof(path))
     98   1.1    lukem 			errx(1, "Pathname too long.");
     99   1.1    lukem 		if (lstat(path, &stbuf) == -1)
    100   1.1    lukem 			err(1, "Can't lstat `%s'", path);
    101  1.17      jmc #ifdef S_ISSOCK
    102   1.1    lukem 		if (S_ISSOCK(stbuf.st_mode & S_IFMT)) {
    103   1.1    lukem 			if (debug & DEBUG_WALK_DIR_NODE)
    104   1.1    lukem 				printf("  skipping socket %s\n", path);
    105   1.1    lukem 			continue;
    106   1.1    lukem 		}
    107  1.17      jmc #endif
    108   1.1    lukem 
    109   1.1    lukem 		cur = create_fsnode(dent->d_name, &stbuf);
    110   1.1    lukem 		cur->parent = parent;
    111   1.1    lukem 		if (strcmp(dent->d_name, ".") == 0) {
    112   1.1    lukem 				/* ensure "." is at the start of the list */
    113   1.1    lukem 			cur->next = first;
    114   1.1    lukem 			first = cur;
    115   1.1    lukem 			if (! prev)
    116   1.1    lukem 				prev = cur;
    117   1.1    lukem 		} else {			/* not "." */
    118   1.1    lukem 			if (prev)
    119   1.1    lukem 				prev->next = cur;
    120   1.1    lukem 			prev = cur;
    121   1.1    lukem 			if (!first)
    122   1.1    lukem 				first = cur;
    123   1.1    lukem 			if (S_ISDIR(cur->type)) {
    124   1.1    lukem 				cur->child = walk_dir(path, cur);
    125   1.1    lukem 				continue;
    126   1.1    lukem 			}
    127   1.1    lukem 		}
    128   1.6    lukem 		if (stbuf.st_nlink > 1) {
    129   1.6    lukem 			fsinode	*curino;
    130   1.6    lukem 
    131   1.6    lukem 			curino = link_check(cur->inode);
    132   1.6    lukem 			if (curino != NULL) {
    133   1.6    lukem 				free(cur->inode);
    134   1.6    lukem 				cur->inode = curino;
    135   1.6    lukem 				cur->inode->nlink++;
    136  1.20      dbj 				if (debug & DEBUG_WALK_DIR_LINKCHECK)
    137  1.20      dbj 					printf("link_check: found [%u, %llu]\n",
    138  1.20      dbj 					    curino->st.st_dev,
    139  1.20      dbj 					    (unsigned long long)curino->st.st_ino);
    140   1.6    lukem 			}
    141   1.1    lukem 		}
    142   1.1    lukem 		if (S_ISLNK(cur->type)) {
    143   1.1    lukem 			char	slink[PATH_MAX+1];
    144   1.1    lukem 			int	llen;
    145   1.1    lukem 
    146  1.13   itojun 			llen = readlink(path, slink, sizeof(slink) - 1);
    147   1.1    lukem 			if (llen == -1)
    148   1.1    lukem 				err(1, "Readlink `%s'", path);
    149   1.1    lukem 			slink[llen] = '\0';
    150   1.1    lukem 			if ((cur->symlink = strdup(slink)) == NULL)
    151   1.1    lukem 				err(1, "Memory allocation error");
    152   1.1    lukem 		}
    153   1.1    lukem 	}
    154   1.1    lukem 	for (cur = first; cur != NULL; cur = cur->next)
    155   1.1    lukem 		cur->first = first;
    156   1.1    lukem 	if (closedir(dirp) == -1)
    157   1.1    lukem 		err(1, "Can't closedir `%s'", dir);
    158   1.1    lukem 	return (first);
    159   1.1    lukem }
    160   1.1    lukem 
    161   1.1    lukem static fsnode *
    162   1.6    lukem create_fsnode(const char *name, struct stat *stbuf)
    163   1.1    lukem {
    164   1.1    lukem 	fsnode *cur;
    165   1.1    lukem 
    166   1.1    lukem 	if ((cur = calloc(1, sizeof(fsnode))) == NULL ||
    167   1.7    lukem 	    (cur->name = strdup(name)) == NULL ||
    168   1.7    lukem 	    (cur->inode = calloc(1, sizeof(fsinode))) == NULL)
    169   1.1    lukem 		err(1, "Memory allocation error");
    170   1.6    lukem 	cur->type = stbuf->st_mode & S_IFMT;
    171   1.7    lukem 	cur->inode->nlink = 1;
    172   1.7    lukem 	cur->inode->st = *stbuf;
    173   1.1    lukem 	return (cur);
    174   1.1    lukem }
    175   1.1    lukem 
    176   1.1    lukem /*
    177   1.1    lukem  * apply_specfile --
    178   1.1    lukem  *	read in the mtree(8) specfile, and apply it to the tree
    179   1.1    lukem  *	at dir,parent. parameters in parent on equivalent types
    180   1.1    lukem  *	will be changed to those found in specfile, and missing
    181   1.1    lukem  *	entries will be added.
    182   1.1    lukem  */
    183   1.1    lukem void
    184   1.1    lukem apply_specfile(const char *specfile, const char *dir, fsnode *parent)
    185   1.1    lukem {
    186   1.1    lukem 	struct timeval	 start;
    187   1.1    lukem 	FILE	*fp;
    188   1.1    lukem 	NODE	*root;
    189   1.1    lukem 
    190   1.1    lukem 	assert(specfile != NULL);
    191   1.1    lukem 	assert(parent != NULL);
    192   1.1    lukem 
    193   1.1    lukem 	if (debug & DEBUG_APPLY_SPECFILE)
    194   1.1    lukem 		printf("apply_specfile: %s, %s %p\n", specfile, dir, parent);
    195   1.1    lukem 
    196   1.1    lukem 				/* read in the specfile */
    197   1.1    lukem 	if ((fp = fopen(specfile, "r")) == NULL)
    198   1.1    lukem 		err(1, "Can't open `%s'", specfile);
    199   1.1    lukem 	TIMER_START(start);
    200   1.1    lukem 	root = spec(fp);
    201   1.1    lukem 	TIMER_RESULTS(start, "spec");
    202   1.1    lukem 	if (fclose(fp) == EOF)
    203   1.1    lukem 		err(1, "Can't close `%s'", specfile);
    204   1.1    lukem 
    205   1.1    lukem 				/* perform some sanity checks */
    206   1.1    lukem 	if (root == NULL)
    207   1.1    lukem 		errx(1, "Specfile `%s' did not contain a tree", specfile);
    208   1.1    lukem 	assert(strcmp(root->name, ".") == 0);
    209   1.1    lukem 	assert(root->type == F_DIR);
    210   1.1    lukem 
    211   1.1    lukem 				/* merge in the changes */
    212   1.1    lukem 	apply_specdir(dir, root, parent);
    213   1.1    lukem }
    214   1.1    lukem 
    215   1.1    lukem static void
    216   1.1    lukem apply_specdir(const char *dir, NODE *specnode, fsnode *dirnode)
    217   1.1    lukem {
    218   1.1    lukem 	char	 path[MAXPATHLEN + 1];
    219   1.1    lukem 	NODE	*curnode;
    220   1.1    lukem 	fsnode	*curfsnode;
    221   1.1    lukem 
    222   1.1    lukem 	assert(specnode != NULL);
    223   1.1    lukem 	assert(dirnode != NULL);
    224   1.1    lukem 
    225   1.1    lukem 	if (debug & DEBUG_APPLY_SPECFILE)
    226   1.1    lukem 		printf("apply_specdir: %s %p %p\n", dir, specnode, dirnode);
    227   1.1    lukem 
    228   1.1    lukem 	if (specnode->type != F_DIR)
    229   1.1    lukem 		errx(1, "Specfile node `%s/%s' is not a directory",
    230   1.1    lukem 		    dir, specnode->name);
    231   1.1    lukem 	if (dirnode->type != S_IFDIR)
    232   1.1    lukem 		errx(1, "Directory node `%s/%s' is not a directory",
    233   1.1    lukem 		    dir, dirnode->name);
    234   1.1    lukem 
    235   1.1    lukem 	apply_specentry(dir, specnode, dirnode);
    236   1.1    lukem 
    237   1.1    lukem 			/* now walk specnode->child matching up with dirnode */
    238   1.1    lukem 	for (curnode = specnode->child; curnode != NULL;
    239   1.1    lukem 	    curnode = curnode->next) {
    240   1.1    lukem 		if (debug & DEBUG_APPLY_SPECENTRY)
    241   1.1    lukem 			printf("apply_specdir:  spec %s\n",
    242   1.1    lukem 			    curnode->name);
    243   1.1    lukem 		for (curfsnode = dirnode->next; curfsnode != NULL;
    244   1.1    lukem 		    curfsnode = curfsnode->next) {
    245   1.3    lukem #if 0	/* too verbose for now */
    246   1.1    lukem 			if (debug & DEBUG_APPLY_SPECENTRY)
    247   1.1    lukem 				printf("apply_specdir:  dirent %s\n",
    248   1.1    lukem 				    curfsnode->name);
    249   1.3    lukem #endif
    250   1.1    lukem 			if (strcmp(curnode->name, curfsnode->name) == 0)
    251   1.1    lukem 				break;
    252   1.1    lukem 		}
    253   1.9    lukem 		if (snprintf(path, sizeof(path), "%s/%s",
    254   1.9    lukem 		    dir, curnode->name) >= sizeof(path))
    255   1.9    lukem 			errx(1, "Pathname too long.");
    256   1.1    lukem 		if (curfsnode == NULL) {	/* need new entry */
    257   1.1    lukem 			struct stat	stbuf;
    258   1.1    lukem 
    259   1.9    lukem 					    /*
    260   1.9    lukem 					     * don't add optional spec entries
    261   1.9    lukem 					     * that lack an existing fs entry
    262   1.9    lukem 					     */
    263   1.9    lukem 			if ((curnode->flags & F_OPT) &&
    264   1.9    lukem 			    lstat(path, &stbuf) == -1)
    265   1.9    lukem 					continue;
    266   1.9    lukem 
    267   1.1    lukem 					/* check that enough info is provided */
    268   1.1    lukem #define NODETEST(t, m)							\
    269   1.1    lukem 			if (!(t))					\
    270   1.9    lukem 				errx(1, "`%s': %s not provided", path, m)
    271   1.1    lukem 			NODETEST(curnode->flags & F_TYPE, "type");
    272   1.1    lukem 			NODETEST(curnode->flags & F_MODE, "mode");
    273   1.1    lukem 				/* XXX: require F_TIME ? */
    274   1.1    lukem 			NODETEST(curnode->flags & F_GID ||
    275   1.1    lukem 			    curnode->flags & F_GNAME, "group");
    276   1.1    lukem 			NODETEST(curnode->flags & F_UID ||
    277   1.1    lukem 			    curnode->flags & F_UNAME, "user");
    278   1.1    lukem 			if (curnode->type == F_BLOCK || curnode->type == F_CHAR)
    279   1.1    lukem 				NODETEST(curnode->flags & F_DEV,
    280   1.1    lukem 				    "device number");
    281   1.1    lukem #undef NODETEST
    282   1.1    lukem 
    283   1.1    lukem 			if (debug & DEBUG_APPLY_SPECFILE)
    284   1.1    lukem 				printf("apply_specdir: adding %s\n",
    285   1.1    lukem 				    curnode->name);
    286   1.1    lukem 					/* build minimal fsnode */
    287   1.1    lukem 			memset(&stbuf, 0, sizeof(stbuf));
    288   1.1    lukem 			stbuf.st_mode = nodetoino(curnode->type);
    289   1.6    lukem 			stbuf.st_nlink = 1;
    290   1.1    lukem 			stbuf.st_mtime = stbuf.st_atime =
    291   1.1    lukem 			    stbuf.st_ctime = start_time.tv_sec;
    292   1.8       tv #if HAVE_STRUCT_STAT_ST_MTIMENSEC
    293   1.1    lukem 			stbuf.st_mtimensec = stbuf.st_atimensec =
    294   1.1    lukem 			    stbuf.st_ctimensec = start_time.tv_nsec;
    295   1.8       tv #endif
    296   1.1    lukem 			curfsnode = create_fsnode(curnode->name, &stbuf);
    297   1.1    lukem 			curfsnode->parent = dirnode->parent;
    298   1.1    lukem 			curfsnode->first = dirnode;
    299   1.1    lukem 			curfsnode->next = dirnode->next;
    300   1.1    lukem 			dirnode->next = curfsnode;
    301   1.1    lukem 			if (curfsnode->type == S_IFDIR) {
    302   1.1    lukem 					/* for dirs, make "." entry as well */
    303   1.1    lukem 				curfsnode->child = create_fsnode(".", &stbuf);
    304   1.1    lukem 				curfsnode->child->parent = curfsnode;
    305   1.1    lukem 				curfsnode->child->first = curfsnode->child;
    306   1.1    lukem 			}
    307   1.1    lukem 			if (curfsnode->type == S_IFLNK) {
    308   1.3    lukem 				assert(curnode->slink != NULL);
    309   1.1    lukem 					/* for symlinks, copy the target */
    310   1.1    lukem 				if ((curfsnode->symlink =
    311   1.1    lukem 				    strdup(curnode->slink)) == NULL)
    312   1.1    lukem 					err(1, "Memory allocation error");
    313   1.1    lukem 			}
    314   1.1    lukem 		}
    315   1.1    lukem 		apply_specentry(dir, curnode, curfsnode);
    316   1.1    lukem 		if (curnode->type == F_DIR) {
    317   1.1    lukem 			if (curfsnode->type != S_IFDIR)
    318   1.9    lukem 				errx(1, "`%s' is not a directory", path);
    319   1.1    lukem 			assert (curfsnode->child != NULL);
    320   1.1    lukem 			apply_specdir(path, curnode, curfsnode->child);
    321   1.1    lukem 		}
    322   1.1    lukem 	}
    323   1.1    lukem }
    324   1.1    lukem 
    325   1.1    lukem static void
    326   1.1    lukem apply_specentry(const char *dir, NODE *specnode, fsnode *dirnode)
    327   1.1    lukem {
    328   1.1    lukem 
    329   1.1    lukem 	assert(specnode != NULL);
    330   1.1    lukem 	assert(dirnode != NULL);
    331   1.1    lukem 
    332   1.1    lukem 	if (nodetoino(specnode->type) != dirnode->type)
    333   1.1    lukem 		errx(1, "`%s/%s' type mismatch: specfile %s, tree %s",
    334   1.1    lukem 		    dir, specnode->name, inode_type(nodetoino(specnode->type)),
    335   1.1    lukem 		    inode_type(dirnode->type));
    336   1.1    lukem 
    337   1.1    lukem 	if (debug & DEBUG_APPLY_SPECENTRY)
    338   1.1    lukem 		printf("apply_specentry: %s/%s\n", dir, dirnode->name);
    339   1.1    lukem 
    340   1.1    lukem #define ASEPRINT(t, b, o, n) \
    341   1.1    lukem 		if (debug & DEBUG_APPLY_SPECENTRY) \
    342   1.1    lukem 			printf("\t\t\tchanging %s from " b " to " b "\n", \
    343   1.1    lukem 			    t, o, n)
    344   1.1    lukem 
    345   1.1    lukem 	if (specnode->flags & (F_GID | F_GNAME)) {
    346   1.1    lukem 		ASEPRINT("gid", "%d",
    347   1.6    lukem 		    dirnode->inode->st.st_gid, specnode->st_gid);
    348   1.6    lukem 		dirnode->inode->st.st_gid = specnode->st_gid;
    349   1.1    lukem 	}
    350   1.1    lukem 	if (specnode->flags & F_MODE) {
    351   1.1    lukem 		ASEPRINT("mode", "%#o",
    352   1.6    lukem 		    dirnode->inode->st.st_mode & ALLPERMS, specnode->st_mode);
    353   1.6    lukem 		dirnode->inode->st.st_mode &= ~ALLPERMS;
    354   1.6    lukem 		dirnode->inode->st.st_mode |= (specnode->st_mode & ALLPERMS);
    355   1.1    lukem 	}
    356   1.1    lukem 		/* XXX: ignoring F_NLINK for now */
    357   1.1    lukem 	if (specnode->flags & F_SIZE) {
    358   1.1    lukem 		ASEPRINT("size", "%lld",
    359   1.6    lukem 		    (long long)dirnode->inode->st.st_size,
    360   1.1    lukem 		    (long long)specnode->st_size);
    361   1.6    lukem 		dirnode->inode->st.st_size = specnode->st_size;
    362   1.1    lukem 	}
    363   1.1    lukem 	if (specnode->flags & F_SLINK) {
    364   1.1    lukem 		assert(dirnode->symlink != NULL);
    365   1.1    lukem 		assert(specnode->slink != NULL);
    366   1.1    lukem 		ASEPRINT("symlink", "%s", dirnode->symlink, specnode->slink);
    367   1.1    lukem 		free(dirnode->symlink);
    368   1.1    lukem 		if ((dirnode->symlink = strdup(specnode->slink)) == NULL)
    369   1.1    lukem 			err(1, "Memory allocation error");
    370   1.1    lukem 	}
    371   1.1    lukem 	if (specnode->flags & F_TIME) {
    372   1.1    lukem 		ASEPRINT("time", "%ld",
    373   1.6    lukem 		    (long)dirnode->inode->st.st_mtime,
    374   1.8       tv 		    (long)specnode->st_mtimespec.tv_sec);
    375   1.8       tv 		dirnode->inode->st.st_mtime =		specnode->st_mtimespec.tv_sec;
    376   1.8       tv 		dirnode->inode->st.st_atime =		specnode->st_mtimespec.tv_sec;
    377   1.8       tv 		dirnode->inode->st.st_ctime =		start_time.tv_sec;
    378   1.8       tv #if HAVE_STRUCT_STAT_ST_MTIMENSEC
    379  1.17      jmc 		dirnode->inode->st.st_mtimensec =	specnode->st_mtimespec.tv_nsec;
    380  1.17      jmc 		dirnode->inode->st.st_atimensec =	specnode->st_mtimespec.tv_nsec;
    381   1.6    lukem 		dirnode->inode->st.st_ctimensec =	start_time.tv_nsec;
    382   1.8       tv #endif
    383   1.1    lukem 	}
    384   1.1    lukem 	if (specnode->flags & (F_UID | F_UNAME)) {
    385   1.1    lukem 		ASEPRINT("uid", "%d",
    386   1.6    lukem 		    dirnode->inode->st.st_uid, specnode->st_uid);
    387   1.6    lukem 		dirnode->inode->st.st_uid = specnode->st_uid;
    388   1.1    lukem 	}
    389   1.8       tv #if HAVE_STRUCT_STAT_ST_FLAGS
    390   1.1    lukem 	if (specnode->flags & F_FLAGS) {
    391   1.1    lukem 		ASEPRINT("flags", "%#lX",
    392  1.11      uwe 		    (unsigned long)dirnode->inode->st.st_flags,
    393  1.11      uwe 		    (unsigned long)specnode->st_flags);
    394   1.6    lukem 		dirnode->inode->st.st_flags = specnode->st_flags;
    395   1.1    lukem 	}
    396   1.8       tv #endif
    397   1.1    lukem 	if (specnode->flags & F_DEV) {
    398   1.1    lukem 		ASEPRINT("rdev", "%#x",
    399   1.6    lukem 		    dirnode->inode->st.st_rdev, specnode->st_rdev);
    400   1.6    lukem 		dirnode->inode->st.st_rdev = specnode->st_rdev;
    401   1.1    lukem 	}
    402   1.1    lukem #undef ASEPRINT
    403  1.12  thorpej 
    404  1.12  thorpej 	dirnode->flags |= FSNODE_F_HASSPEC;
    405   1.1    lukem }
    406   1.1    lukem 
    407   1.1    lukem 
    408   1.1    lukem /*
    409   1.1    lukem  * dump_fsnodes --
    410   1.1    lukem  *	dump the fsnodes from `cur', based in the directory `dir'
    411   1.1    lukem  */
    412   1.1    lukem void
    413   1.1    lukem dump_fsnodes(const char *dir, fsnode *root)
    414   1.1    lukem {
    415   1.1    lukem 	fsnode	*cur;
    416   1.1    lukem 	char	path[MAXPATHLEN + 1];
    417   1.1    lukem 
    418   1.1    lukem 	assert (dir != NULL);
    419   1.1    lukem 	printf("dump_fsnodes: %s %p\n", dir, root);
    420   1.1    lukem 	for (cur = root; cur != NULL; cur = cur->next) {
    421   1.1    lukem 		if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name)
    422   1.1    lukem 		    >= sizeof(path))
    423   1.1    lukem 			errx(1, "Pathname too long.");
    424   1.1    lukem 
    425   1.1    lukem 		if (debug & DEBUG_DUMP_FSNODES_VERBOSE)
    426   1.1    lukem 			printf("cur=%8p parent=%8p first=%8p ",
    427   1.1    lukem 			    cur, cur->parent, cur->first);
    428   1.1    lukem 		printf("%7s: %s", inode_type(cur->type), path);
    429   1.1    lukem 		if (S_ISLNK(cur->type)) {
    430   1.1    lukem 			assert(cur->symlink != NULL);
    431   1.1    lukem 			printf(" -> %s", cur->symlink);
    432   1.1    lukem 		} else {
    433   1.1    lukem 			assert (cur->symlink == NULL);
    434   1.1    lukem 		}
    435   1.6    lukem 		if (cur->inode->nlink > 1)
    436   1.6    lukem 			printf(", nlinks=%d", cur->inode->nlink);
    437   1.1    lukem 		putchar('\n');
    438   1.1    lukem 
    439   1.1    lukem 		if (cur->child) {
    440   1.1    lukem 			assert (cur->type == S_IFDIR);
    441   1.1    lukem 			dump_fsnodes(path, cur->child);
    442   1.1    lukem 		}
    443   1.1    lukem 	}
    444   1.1    lukem 	printf("dump_fsnodes: finished %s\n", dir);
    445   1.1    lukem }
    446   1.1    lukem 
    447   1.1    lukem 
    448   1.1    lukem /*
    449   1.1    lukem  * inode_type --
    450   1.1    lukem  *	for a given inode type `mode', return a descriptive string.
    451   1.1    lukem  *	for most cases, uses inotype() from mtree/misc.c
    452   1.1    lukem  */
    453   1.1    lukem const char *
    454   1.1    lukem inode_type(mode_t mode)
    455   1.1    lukem {
    456   1.1    lukem 
    457   1.3    lukem 	if (S_ISLNK(mode))
    458   1.1    lukem 		return ("symlink");	/* inotype() returns "link"...  */
    459   1.1    lukem 	return (inotype(mode));
    460   1.1    lukem }
    461   1.1    lukem 
    462   1.1    lukem 
    463   1.1    lukem /*
    464   1.1    lukem  * link_check --
    465  1.20      dbj  *	return pointer to fsinode matching `entry's st_ino & st_dev if it exists,
    466   1.1    lukem  *	otherwise add `entry' to table and return NULL
    467   1.1    lukem  */
    468  1.20      dbj /* This was borrowed from du.c and tweaked to keep an fsnode
    469  1.20      dbj  * pointer instead. -- dbj (at) netbsd.org
    470  1.20      dbj  */
    471   1.6    lukem static fsinode *
    472   1.6    lukem link_check(fsinode *entry)
    473   1.1    lukem {
    474  1.20      dbj 	static struct entry {
    475  1.20      dbj 		fsinode *data;
    476  1.20      dbj 	} *htable;
    477  1.20      dbj 	static int htshift;  /* log(allocated size) */
    478  1.20      dbj 	static int htmask;   /* allocated size - 1 */
    479  1.20      dbj 	static int htused;   /* 2*number of insertions */
    480  1.20      dbj 	int h, h2;
    481  1.20      dbj 	uint64_t tmp;
    482  1.20      dbj 	/* this constant is (1<<64)/((1+sqrt(5))/2)
    483  1.20      dbj 	 * aka (word size)/(golden ratio)
    484  1.20      dbj 	 */
    485  1.20      dbj 	const uint64_t HTCONST = 11400714819323198485ULL;
    486  1.20      dbj 	const int HTBITS = 64;
    487  1.20      dbj 
    488  1.20      dbj 	/* Never store zero in hashtable */
    489  1.20      dbj 	assert(entry);
    490  1.20      dbj 
    491  1.20      dbj 	/* Extend hash table if necessary, keep load under 0.5 */
    492  1.20      dbj 	if (htused<<1 >= htmask) {
    493  1.20      dbj 		struct entry *ohtable;
    494  1.20      dbj 
    495  1.20      dbj 		if (!htable)
    496  1.20      dbj 			htshift = 10;   /* starting hashtable size */
    497  1.20      dbj 		else
    498  1.20      dbj 			htshift++;   /* exponential hashtable growth */
    499  1.20      dbj 
    500  1.20      dbj 		htmask  = (1 << htshift) - 1;
    501  1.20      dbj 		htused = 0;
    502  1.20      dbj 
    503  1.20      dbj 		ohtable = htable;
    504  1.20      dbj 		htable = calloc(htmask+1, sizeof(*htable));
    505  1.20      dbj 		if (!htable)
    506  1.20      dbj 			err(1, "Memory allocation error");
    507  1.20      dbj 
    508  1.20      dbj 		/* populate newly allocated hashtable */
    509  1.20      dbj 		if (ohtable) {
    510  1.20      dbj 			int i;
    511  1.20      dbj 			for (i = 0; i <= htmask>>1; i++)
    512  1.20      dbj 				if (ohtable[i].data)
    513  1.20      dbj 					link_check(ohtable[i].data);
    514  1.20      dbj 			free(ohtable);
    515   1.1    lukem 		}
    516   1.1    lukem 	}
    517   1.1    lukem 
    518  1.20      dbj 	/* multiplicative hashing */
    519  1.20      dbj 	tmp = entry->st.st_dev;
    520  1.20      dbj 	tmp <<= HTBITS>>1;
    521  1.20      dbj 	tmp |=  entry->st.st_ino;
    522  1.20      dbj 	tmp *= HTCONST;
    523  1.20      dbj 	h  = tmp >> (HTBITS - htshift);
    524  1.20      dbj 	h2 = 1 | ( tmp >> (HTBITS - (htshift<<1) - 1)); /* must be odd */
    525  1.20      dbj 
    526  1.20      dbj 	/* open address hashtable search with double hash probing */
    527  1.20      dbj 	while (htable[h].data) {
    528  1.20      dbj 		if ((htable[h].data->st.st_ino == entry->st.st_ino) &&
    529  1.20      dbj 		    (htable[h].data->st.st_dev == entry->st.st_dev)) {
    530  1.20      dbj 			return htable[h].data;
    531  1.20      dbj 		}
    532  1.20      dbj 		h = (h + h2) & htmask;
    533   1.1    lukem 	}
    534   1.1    lukem 
    535  1.20      dbj 	/* Insert the current entry into hashtable */
    536  1.20      dbj 	htable[h].data = entry;
    537  1.20      dbj 	htused++;
    538  1.20      dbj 	return NULL;
    539   1.1    lukem }
    540