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