xref: /netbsd/usr.sbin/makefs/walk.c (revision c4a72b64)
1 /*	$NetBSD: walk.c,v 1.10 2002/10/19 20:33:20 provos 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 #if defined(__RCSID) && !defined(__lint)
80 __RCSID("$NetBSD: walk.c,v 1.10 2002/10/19 20:33:20 provos Exp $");
81 #endif	/* !__lint */
82 
83 #include <sys/param.h>
84 
85 #include <assert.h>
86 #include <errno.h>
87 #include <fcntl.h>
88 #include <stdio.h>
89 #include <dirent.h>
90 #include <stdlib.h>
91 #include <string.h>
92 #include <unistd.h>
93 
94 #include "makefs.h"
95 #include "mtree.h"
96 
97 static	void	 apply_specdir(const char *, NODE *, fsnode *);
98 static	void	 apply_specentry(const char *, NODE *, fsnode *);
99 static	fsnode	*create_fsnode(const char *, struct stat *);
100 static	fsinode	*link_check(fsinode *);
101 
102 
103 /*
104  * walk_dir --
105  *	build a tree of fsnodes from `dir', with a parent fsnode of `parent'
106  *	(which may be NULL for the root of the tree).
107  *	each "level" is a directory, with the "." entry guaranteed to be
108  *	at the start of the list, and without ".." entries.
109  */
110 fsnode *
111 walk_dir(const char *dir, fsnode *parent)
112 {
113 	fsnode		*first, *cur, *prev;
114 	DIR		*dirp;
115 	struct dirent	*dent;
116 	char		path[MAXPATHLEN + 1];
117 	struct stat	stbuf;
118 
119 	assert(dir != NULL);
120 
121 	if (debug & DEBUG_WALK_DIR)
122 		printf("walk_dir: %s %p\n", dir, parent);
123 	if ((dirp = opendir(dir)) == NULL)
124 		err(1, "Can't opendir `%s'", dir);
125 	first = prev = NULL;
126 	while ((dent = readdir(dirp)) != NULL) {
127 		if (strcmp(dent->d_name, "..") == 0)
128 			continue;
129 		if (debug & DEBUG_WALK_DIR_NODE)
130 			printf("scanning %s/%s\n", dir, dent->d_name);
131 		if (snprintf(path, sizeof(path), "%s/%s", dir, dent->d_name)
132 		    >= sizeof(path))
133 			errx(1, "Pathname too long.");
134 		if (lstat(path, &stbuf) == -1)
135 			err(1, "Can't lstat `%s'", path);
136 		if (S_ISSOCK(stbuf.st_mode & S_IFMT)) {
137 			if (debug & DEBUG_WALK_DIR_NODE)
138 				printf("  skipping socket %s\n", path);
139 			continue;
140 		}
141 
142 		cur = create_fsnode(dent->d_name, &stbuf);
143 		cur->parent = parent;
144 		if (strcmp(dent->d_name, ".") == 0) {
145 				/* ensure "." is at the start of the list */
146 			cur->next = first;
147 			first = cur;
148 			if (! prev)
149 				prev = cur;
150 		} else {			/* not "." */
151 			if (prev)
152 				prev->next = cur;
153 			prev = cur;
154 			if (!first)
155 				first = cur;
156 			if (S_ISDIR(cur->type)) {
157 				cur->child = walk_dir(path, cur);
158 				continue;
159 			}
160 		}
161 		if (stbuf.st_nlink > 1) {
162 			fsinode	*curino;
163 
164 			curino = link_check(cur->inode);
165 			if (curino != NULL) {
166 				free(cur->inode);
167 				cur->inode = curino;
168 				cur->inode->nlink++;
169 			}
170 		}
171 		if (S_ISLNK(cur->type)) {
172 			char	slink[PATH_MAX+1];
173 			int	llen;
174 
175 			llen = readlink(path, slink, PATH_MAX - 1);
176 			if (llen == -1)
177 				err(1, "Readlink `%s'", path);
178 			slink[llen] = '\0';
179 			if ((cur->symlink = strdup(slink)) == NULL)
180 				err(1, "Memory allocation error");
181 		}
182 	}
183 	for (cur = first; cur != NULL; cur = cur->next)
184 		cur->first = first;
185 	if (closedir(dirp) == -1)
186 		err(1, "Can't closedir `%s'", dir);
187 	return (first);
188 }
189 
190 static fsnode *
191 create_fsnode(const char *name, struct stat *stbuf)
192 {
193 	fsnode *cur;
194 
195 	if ((cur = calloc(1, sizeof(fsnode))) == NULL ||
196 	    (cur->name = strdup(name)) == NULL ||
197 	    (cur->inode = calloc(1, sizeof(fsinode))) == NULL)
198 		err(1, "Memory allocation error");
199 	cur->type = stbuf->st_mode & S_IFMT;
200 	cur->inode->nlink = 1;
201 	cur->inode->st = *stbuf;
202 	return (cur);
203 }
204 
205 /*
206  * apply_specfile --
207  *	read in the mtree(8) specfile, and apply it to the tree
208  *	at dir,parent. parameters in parent on equivalent types
209  *	will be changed to those found in specfile, and missing
210  *	entries will be added.
211  */
212 void
213 apply_specfile(const char *specfile, const char *dir, fsnode *parent)
214 {
215 	struct timeval	 start;
216 	FILE	*fp;
217 	NODE	*root;
218 
219 	assert(specfile != NULL);
220 	assert(parent != NULL);
221 
222 	if (debug & DEBUG_APPLY_SPECFILE)
223 		printf("apply_specfile: %s, %s %p\n", specfile, dir, parent);
224 
225 				/* read in the specfile */
226 	if ((fp = fopen(specfile, "r")) == NULL)
227 		err(1, "Can't open `%s'", specfile);
228 	TIMER_START(start);
229 	root = spec(fp);
230 	TIMER_RESULTS(start, "spec");
231 	if (fclose(fp) == EOF)
232 		err(1, "Can't close `%s'", specfile);
233 
234 				/* perform some sanity checks */
235 	if (root == NULL)
236 		errx(1, "Specfile `%s' did not contain a tree", specfile);
237 	assert(strcmp(root->name, ".") == 0);
238 	assert(root->type == F_DIR);
239 
240 				/* merge in the changes */
241 	apply_specdir(dir, root, parent);
242 }
243 
244 static void
245 apply_specdir(const char *dir, NODE *specnode, fsnode *dirnode)
246 {
247 	char	 path[MAXPATHLEN + 1];
248 	NODE	*curnode;
249 	fsnode	*curfsnode;
250 
251 	assert(specnode != NULL);
252 	assert(dirnode != NULL);
253 
254 	if (debug & DEBUG_APPLY_SPECFILE)
255 		printf("apply_specdir: %s %p %p\n", dir, specnode, dirnode);
256 
257 	if (specnode->type != F_DIR)
258 		errx(1, "Specfile node `%s/%s' is not a directory",
259 		    dir, specnode->name);
260 	if (dirnode->type != S_IFDIR)
261 		errx(1, "Directory node `%s/%s' is not a directory",
262 		    dir, dirnode->name);
263 
264 	apply_specentry(dir, specnode, dirnode);
265 
266 			/* now walk specnode->child matching up with dirnode */
267 	for (curnode = specnode->child; curnode != NULL;
268 	    curnode = curnode->next) {
269 		if (debug & DEBUG_APPLY_SPECENTRY)
270 			printf("apply_specdir:  spec %s\n",
271 			    curnode->name);
272 		for (curfsnode = dirnode->next; curfsnode != NULL;
273 		    curfsnode = curfsnode->next) {
274 #if 0	/* too verbose for now */
275 			if (debug & DEBUG_APPLY_SPECENTRY)
276 				printf("apply_specdir:  dirent %s\n",
277 				    curfsnode->name);
278 #endif
279 			if (strcmp(curnode->name, curfsnode->name) == 0)
280 				break;
281 		}
282 		if (snprintf(path, sizeof(path), "%s/%s",
283 		    dir, curnode->name) >= sizeof(path))
284 			errx(1, "Pathname too long.");
285 		if (curfsnode == NULL) {	/* need new entry */
286 			struct stat	stbuf;
287 
288 					    /*
289 					     * don't add optional spec entries
290 					     * that lack an existing fs entry
291 					     */
292 			if ((curnode->flags & F_OPT) &&
293 			    lstat(path, &stbuf) == -1)
294 					continue;
295 
296 					/* check that enough info is provided */
297 #define NODETEST(t, m)							\
298 			if (!(t))					\
299 				errx(1, "`%s': %s not provided", path, m)
300 			NODETEST(curnode->flags & F_TYPE, "type");
301 			NODETEST(curnode->flags & F_MODE, "mode");
302 				/* XXX: require F_TIME ? */
303 			NODETEST(curnode->flags & F_GID ||
304 			    curnode->flags & F_GNAME, "group");
305 			NODETEST(curnode->flags & F_UID ||
306 			    curnode->flags & F_UNAME, "user");
307 			if (curnode->type == F_BLOCK || curnode->type == F_CHAR)
308 				NODETEST(curnode->flags & F_DEV,
309 				    "device number");
310 #undef NODETEST
311 
312 			if (debug & DEBUG_APPLY_SPECFILE)
313 				printf("apply_specdir: adding %s\n",
314 				    curnode->name);
315 					/* build minimal fsnode */
316 			memset(&stbuf, 0, sizeof(stbuf));
317 			stbuf.st_mode = nodetoino(curnode->type);
318 			stbuf.st_nlink = 1;
319 			stbuf.st_mtime = stbuf.st_atime =
320 			    stbuf.st_ctime = start_time.tv_sec;
321 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
322 			stbuf.st_mtimensec = stbuf.st_atimensec =
323 			    stbuf.st_ctimensec = start_time.tv_nsec;
324 #endif
325 			curfsnode = create_fsnode(curnode->name, &stbuf);
326 			curfsnode->parent = dirnode->parent;
327 			curfsnode->first = dirnode;
328 			curfsnode->next = dirnode->next;
329 			dirnode->next = curfsnode;
330 			if (curfsnode->type == S_IFDIR) {
331 					/* for dirs, make "." entry as well */
332 				curfsnode->child = create_fsnode(".", &stbuf);
333 				curfsnode->child->parent = curfsnode;
334 				curfsnode->child->first = curfsnode->child;
335 			}
336 			if (curfsnode->type == S_IFLNK) {
337 				assert(curnode->slink != NULL);
338 					/* for symlinks, copy the target */
339 				if ((curfsnode->symlink =
340 				    strdup(curnode->slink)) == NULL)
341 					err(1, "Memory allocation error");
342 			}
343 		}
344 		apply_specentry(dir, curnode, curfsnode);
345 		if (curnode->type == F_DIR) {
346 			if (curfsnode->type != S_IFDIR)
347 				errx(1, "`%s' is not a directory", path);
348 			assert (curfsnode->child != NULL);
349 			apply_specdir(path, curnode, curfsnode->child);
350 		}
351 	}
352 }
353 
354 static void
355 apply_specentry(const char *dir, NODE *specnode, fsnode *dirnode)
356 {
357 
358 	assert(specnode != NULL);
359 	assert(dirnode != NULL);
360 
361 	if (nodetoino(specnode->type) != dirnode->type)
362 		errx(1, "`%s/%s' type mismatch: specfile %s, tree %s",
363 		    dir, specnode->name, inode_type(nodetoino(specnode->type)),
364 		    inode_type(dirnode->type));
365 
366 	if (debug & DEBUG_APPLY_SPECENTRY)
367 		printf("apply_specentry: %s/%s\n", dir, dirnode->name);
368 
369 #define ASEPRINT(t, b, o, n) \
370 		if (debug & DEBUG_APPLY_SPECENTRY) \
371 			printf("\t\t\tchanging %s from " b " to " b "\n", \
372 			    t, o, n)
373 
374 	if (specnode->flags & (F_GID | F_GNAME)) {
375 		ASEPRINT("gid", "%d",
376 		    dirnode->inode->st.st_gid, specnode->st_gid);
377 		dirnode->inode->st.st_gid = specnode->st_gid;
378 	}
379 	if (specnode->flags & F_MODE) {
380 		ASEPRINT("mode", "%#o",
381 		    dirnode->inode->st.st_mode & ALLPERMS, specnode->st_mode);
382 		dirnode->inode->st.st_mode &= ~ALLPERMS;
383 		dirnode->inode->st.st_mode |= (specnode->st_mode & ALLPERMS);
384 	}
385 		/* XXX: ignoring F_NLINK for now */
386 	if (specnode->flags & F_SIZE) {
387 		ASEPRINT("size", "%lld",
388 		    (long long)dirnode->inode->st.st_size,
389 		    (long long)specnode->st_size);
390 		dirnode->inode->st.st_size = specnode->st_size;
391 	}
392 	if (specnode->flags & F_SLINK) {
393 		assert(dirnode->symlink != NULL);
394 		assert(specnode->slink != NULL);
395 		ASEPRINT("symlink", "%s", dirnode->symlink, specnode->slink);
396 		free(dirnode->symlink);
397 		if ((dirnode->symlink = strdup(specnode->slink)) == NULL)
398 			err(1, "Memory allocation error");
399 	}
400 	if (specnode->flags & F_TIME) {
401 		ASEPRINT("time", "%ld",
402 		    (long)dirnode->inode->st.st_mtime,
403 		    (long)specnode->st_mtimespec.tv_sec);
404 		dirnode->inode->st.st_mtime =		specnode->st_mtimespec.tv_sec;
405 		dirnode->inode->st.st_atime =		specnode->st_mtimespec.tv_sec;
406 		dirnode->inode->st.st_ctime =		start_time.tv_sec;
407 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
408 		dirnode->inode->st.st_mtimensec =	specnode->st_mtimensec;
409 		dirnode->inode->st.st_atimensec =	specnode->st_mtimensec;
410 		dirnode->inode->st.st_ctimensec =	start_time.tv_nsec;
411 #endif
412 	}
413 	if (specnode->flags & (F_UID | F_UNAME)) {
414 		ASEPRINT("uid", "%d",
415 		    dirnode->inode->st.st_uid, specnode->st_uid);
416 		dirnode->inode->st.st_uid = specnode->st_uid;
417 	}
418 #if HAVE_STRUCT_STAT_ST_FLAGS
419 	if (specnode->flags & F_FLAGS) {
420 		ASEPRINT("flags", "%#lX",
421 		    (ulong)dirnode->inode->st.st_flags,
422 		    (ulong)specnode->st_flags);
423 		dirnode->inode->st.st_flags = specnode->st_flags;
424 	}
425 #endif
426 	if (specnode->flags & F_DEV) {
427 		ASEPRINT("rdev", "%#x",
428 		    dirnode->inode->st.st_rdev, specnode->st_rdev);
429 		dirnode->inode->st.st_rdev = specnode->st_rdev;
430 	}
431 #undef ASEPRINT
432 }
433 
434 
435 /*
436  * dump_fsnodes --
437  *	dump the fsnodes from `cur', based in the directory `dir'
438  */
439 void
440 dump_fsnodes(const char *dir, fsnode *root)
441 {
442 	fsnode	*cur;
443 	char	path[MAXPATHLEN + 1];
444 
445 	assert (dir != NULL);
446 	printf("dump_fsnodes: %s %p\n", dir, root);
447 	for (cur = root; cur != NULL; cur = cur->next) {
448 		if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name)
449 		    >= sizeof(path))
450 			errx(1, "Pathname too long.");
451 
452 		if (debug & DEBUG_DUMP_FSNODES_VERBOSE)
453 			printf("cur=%8p parent=%8p first=%8p ",
454 			    cur, cur->parent, cur->first);
455 		printf("%7s: %s", inode_type(cur->type), path);
456 		if (S_ISLNK(cur->type)) {
457 			assert(cur->symlink != NULL);
458 			printf(" -> %s", cur->symlink);
459 		} else {
460 			assert (cur->symlink == NULL);
461 		}
462 		if (cur->inode->nlink > 1)
463 			printf(", nlinks=%d", cur->inode->nlink);
464 		putchar('\n');
465 
466 		if (cur->child) {
467 			assert (cur->type == S_IFDIR);
468 			dump_fsnodes(path, cur->child);
469 		}
470 	}
471 	printf("dump_fsnodes: finished %s\n", dir);
472 }
473 
474 
475 /*
476  * inode_type --
477  *	for a given inode type `mode', return a descriptive string.
478  *	for most cases, uses inotype() from mtree/misc.c
479  */
480 const char *
481 inode_type(mode_t mode)
482 {
483 
484 	if (S_ISLNK(mode))
485 		return ("symlink");	/* inotype() returns "link"...  */
486 	return (inotype(mode));
487 }
488 
489 
490 /*
491  * link_check --
492  *	return pointer to fsnode matching `entry's st_ino & st_dev if it exists,
493  *	otherwise add `entry' to table and return NULL
494  */
495 static fsinode *
496 link_check(fsinode *entry)
497 {
498 	static	struct dupnode {
499 		uint32_t	dev;
500 		uint32_t	ino;
501 		fsinode		*dup;
502 	} *dups;
503 	static	int	ndups, maxdups;
504 
505 	int	i;
506 
507 	assert (entry != NULL);
508 
509 		/* XXX; maybe traverse in reverse for speed? */
510 	for (i = 0; i < ndups; i++) {
511 		if (dups[i].dev == entry->st.st_dev &&
512 		    dups[i].ino == entry->st.st_ino) {
513 			if (debug & DEBUG_WALK_DIR_LINKCHECK)
514 				printf("link_check: found [%d,%d]\n",
515 				    entry->st.st_dev, entry->st.st_ino);
516 			return (dups[i].dup);
517 		}
518 	}
519 
520 	if (debug & DEBUG_WALK_DIR_LINKCHECK)
521 		printf("link_check: no match for [%d, %d]\n",
522 		    entry->st.st_dev, entry->st.st_ino);
523 	if (ndups == maxdups) {
524 		maxdups += 128;
525 		if ((dups = realloc(dups, sizeof(struct dupnode) * maxdups))
526 		    == NULL)
527 			err(1, "Memory allocation error");
528 	}
529 	dups[ndups].dev = entry->st.st_dev;
530 	dups[ndups].ino = entry->st.st_ino;
531 	dups[ndups].dup = entry;
532 	ndups++;
533 
534 	return (NULL);
535 }
536