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