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