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