1 /* $NetBSD: walk.c,v 1.17 2004/06/20 22:20:18 jmc Exp $ */
4 * Copyright (c) 2001 Wasabi Systems, Inc.
7 * Written by Luke Mewburn for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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.
39 * The function link_check() was inspired from NetBSD's usr.bin/du/du.c,
40 * which has the following copyright notice:
43 * Copyright (c) 1989, 1993, 1994
44 * The Regents of the University of California. All rights reserved.
46 * This code is derived from software contributed to Berkeley by
49 * Redistribution and use in source and binary forms, with or without
50 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 #include <sys/cdefs.h>
75 __FBSDID("$FreeBSD$");
77 #include <sys/param.h>
91 #include "extern.h" /* NB: mtree */
93 static void apply_specdir(const char *, NODE *, fsnode *);
94 static void apply_specentry(const char *, NODE *, fsnode *);
95 static fsnode *create_fsnode(const char *, struct stat *);
96 static fsinode *link_check(fsinode *);
101 * build a tree of fsnodes from `dir', with a parent fsnode of `parent'
102 * (which may be NULL for the root of the tree).
103 * each "level" is a directory, with the "." entry guaranteed to be
104 * at the start of the list, and without ".." entries.
107 walk_dir(const char *dir, fsnode *parent)
109 fsnode *first, *cur, *prev;
112 char path[MAXPATHLEN + 1];
117 if (debug & DEBUG_WALK_DIR)
118 printf("walk_dir: %s %p\n", dir, parent);
119 if ((dirp = opendir(dir)) == NULL)
120 err(1, "Can't opendir `%s'", dir);
122 while ((dent = readdir(dirp)) != NULL) {
123 if (strcmp(dent->d_name, "..") == 0)
125 if (debug & DEBUG_WALK_DIR_NODE)
126 printf("scanning %s/%s\n", dir, dent->d_name);
127 if (snprintf(path, sizeof(path), "%s/%s", dir, dent->d_name)
129 errx(1, "Pathname too long.");
130 if (lstat(path, &stbuf) == -1)
131 err(1, "Can't lstat `%s'", path);
133 if (S_ISSOCK(stbuf.st_mode & S_IFMT)) {
134 if (debug & DEBUG_WALK_DIR_NODE)
135 printf(" skipping socket %s\n", path);
140 cur = create_fsnode(dent->d_name, &stbuf);
141 cur->parent = parent;
142 if (strcmp(dent->d_name, ".") == 0) {
143 /* ensure "." is at the start of the list */
148 } else { /* not "." */
154 if (S_ISDIR(cur->type)) {
155 cur->child = walk_dir(path, cur);
159 if (stbuf.st_nlink > 1) {
162 curino = link_check(cur->inode);
163 if (curino != NULL) {
169 if (S_ISLNK(cur->type)) {
170 char slink[PATH_MAX+1];
173 llen = readlink(path, slink, sizeof(slink) - 1);
175 err(1, "Readlink `%s'", path);
177 if ((cur->symlink = strdup(slink)) == NULL)
178 err(1, "Memory allocation error");
181 for (cur = first; cur != NULL; cur = cur->next)
183 if (closedir(dirp) == -1)
184 err(1, "Can't closedir `%s'", dir);
189 create_fsnode(const char *name, struct stat *stbuf)
193 if ((cur = calloc(1, sizeof(fsnode))) == NULL ||
194 (cur->name = strdup(name)) == NULL ||
195 (cur->inode = calloc(1, sizeof(fsinode))) == NULL)
196 err(1, "Memory allocation error");
197 cur->type = stbuf->st_mode & S_IFMT;
198 cur->inode->nlink = 1;
199 cur->inode->st = *stbuf;
205 * read in the mtree(8) specfile, and apply it to the tree
206 * at dir,parent. parameters in parent on equivalent types
207 * will be changed to those found in specfile, and missing
208 * entries will be added.
211 apply_specfile(const char *specfile, const char *dir, fsnode *parent)
213 struct timeval start;
217 assert(specfile != NULL);
218 assert(parent != NULL);
220 if (debug & DEBUG_APPLY_SPECFILE)
221 printf("apply_specfile: %s, %s %p\n", specfile, dir, parent);
223 /* read in the specfile */
224 if ((fp = fopen(specfile, "r")) == NULL)
225 err(1, "Can't open `%s'", specfile);
227 root = mtree_readspec(fp);
228 TIMER_RESULTS(start, "spec");
229 if (fclose(fp) == EOF)
230 err(1, "Can't close `%s'", specfile);
232 /* perform some sanity checks */
234 errx(1, "Specfile `%s' did not contain a tree", specfile);
235 assert(strcmp(root->name, ".") == 0);
236 assert(root->type == F_DIR);
238 /* merge in the changes */
239 apply_specdir(dir, root, parent);
243 nodetoino(u_int type)
262 printf("unknown type %d", type);
269 apply_specdir(const char *dir, NODE *specnode, fsnode *dirnode)
271 char path[MAXPATHLEN + 1];
275 assert(specnode != NULL);
276 assert(dirnode != NULL);
278 if (debug & DEBUG_APPLY_SPECFILE)
279 printf("apply_specdir: %s %p %p\n", dir, specnode, dirnode);
281 if (specnode->type != F_DIR)
282 errx(1, "Specfile node `%s/%s' is not a directory",
283 dir, specnode->name);
284 if (dirnode->type != S_IFDIR)
285 errx(1, "Directory node `%s/%s' is not a directory",
288 apply_specentry(dir, specnode, dirnode);
290 /* now walk specnode->child matching up with dirnode */
291 for (curnode = specnode->child; curnode != NULL;
292 curnode = curnode->next) {
293 if (debug & DEBUG_APPLY_SPECENTRY)
294 printf("apply_specdir: spec %s\n",
296 for (curfsnode = dirnode->next; curfsnode != NULL;
297 curfsnode = curfsnode->next) {
298 #if 0 /* too verbose for now */
299 if (debug & DEBUG_APPLY_SPECENTRY)
300 printf("apply_specdir: dirent %s\n",
303 if (strcmp(curnode->name, curfsnode->name) == 0)
306 if (snprintf(path, sizeof(path), "%s/%s",
307 dir, curnode->name) >= sizeof(path))
308 errx(1, "Pathname too long.");
309 if (curfsnode == NULL) { /* need new entry */
313 * don't add optional spec entries
314 * that lack an existing fs entry
316 if ((curnode->flags & F_OPT) &&
317 lstat(path, &stbuf) == -1)
320 /* check that enough info is provided */
321 #define NODETEST(t, m) \
323 errx(1, "`%s': %s not provided", path, m)
324 NODETEST(curnode->flags & F_TYPE, "type");
325 NODETEST(curnode->flags & F_MODE, "mode");
326 /* XXX: require F_TIME ? */
327 NODETEST(curnode->flags & F_GID ||
328 curnode->flags & F_GNAME, "group");
329 NODETEST(curnode->flags & F_UID ||
330 curnode->flags & F_UNAME, "user");
333 if (debug & DEBUG_APPLY_SPECFILE)
334 printf("apply_specdir: adding %s\n",
336 /* build minimal fsnode */
337 memset(&stbuf, 0, sizeof(stbuf));
338 stbuf.st_mode = nodetoino(curnode->type);
340 stbuf.st_mtime = stbuf.st_atime =
341 stbuf.st_ctime = start_time.tv_sec;
342 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
343 stbuf.st_mtimensec = stbuf.st_atimensec =
344 stbuf.st_ctimensec = start_time.tv_nsec;
346 curfsnode = create_fsnode(curnode->name, &stbuf);
347 curfsnode->parent = dirnode->parent;
348 curfsnode->first = dirnode;
349 curfsnode->next = dirnode->next;
350 dirnode->next = curfsnode;
351 if (curfsnode->type == S_IFDIR) {
352 /* for dirs, make "." entry as well */
353 curfsnode->child = create_fsnode(".", &stbuf);
354 curfsnode->child->parent = curfsnode;
355 curfsnode->child->first = curfsnode->child;
357 if (curfsnode->type == S_IFLNK) {
358 assert(curnode->slink != NULL);
359 /* for symlinks, copy the target */
360 if ((curfsnode->symlink =
361 strdup(curnode->slink)) == NULL)
362 err(1, "Memory allocation error");
365 apply_specentry(dir, curnode, curfsnode);
366 if (curnode->type == F_DIR) {
367 if (curfsnode->type != S_IFDIR)
368 errx(1, "`%s' is not a directory", path);
369 assert (curfsnode->child != NULL);
370 apply_specdir(path, curnode, curfsnode->child);
376 apply_specentry(const char *dir, NODE *specnode, fsnode *dirnode)
379 assert(specnode != NULL);
380 assert(dirnode != NULL);
382 if (nodetoino(specnode->type) != dirnode->type)
383 errx(1, "`%s/%s' type mismatch: specfile %s, tree %s",
384 dir, specnode->name, inode_type(nodetoino(specnode->type)),
385 inode_type(dirnode->type));
387 if (debug & DEBUG_APPLY_SPECENTRY)
388 printf("apply_specentry: %s/%s\n", dir, dirnode->name);
390 #define ASEPRINT(t, b, o, n) \
391 if (debug & DEBUG_APPLY_SPECENTRY) \
392 printf("\t\t\tchanging %s from " b " to " b "\n", \
395 if (specnode->flags & (F_GID | F_GNAME)) {
396 ASEPRINT("gid", "%d",
397 dirnode->inode->st.st_gid, specnode->st_gid);
398 dirnode->inode->st.st_gid = specnode->st_gid;
400 if (specnode->flags & F_MODE) {
401 ASEPRINT("mode", "%#o",
402 dirnode->inode->st.st_mode & ALLPERMS, specnode->st_mode);
403 dirnode->inode->st.st_mode &= ~ALLPERMS;
404 dirnode->inode->st.st_mode |= (specnode->st_mode & ALLPERMS);
406 /* XXX: ignoring F_NLINK for now */
407 if (specnode->flags & F_SIZE) {
408 ASEPRINT("size", "%lld",
409 (long long)dirnode->inode->st.st_size,
410 (long long)specnode->st_size);
411 dirnode->inode->st.st_size = specnode->st_size;
413 if (specnode->flags & F_SLINK) {
414 assert(dirnode->symlink != NULL);
415 assert(specnode->slink != NULL);
416 ASEPRINT("symlink", "%s", dirnode->symlink, specnode->slink);
417 free(dirnode->symlink);
418 if ((dirnode->symlink = strdup(specnode->slink)) == NULL)
419 err(1, "Memory allocation error");
421 if (specnode->flags & F_TIME) {
422 ASEPRINT("time", "%ld",
423 (long)dirnode->inode->st.st_mtime,
424 (long)specnode->st_mtimespec.tv_sec);
425 dirnode->inode->st.st_mtime = specnode->st_mtimespec.tv_sec;
426 dirnode->inode->st.st_atime = specnode->st_mtimespec.tv_sec;
427 dirnode->inode->st.st_ctime = start_time.tv_sec;
428 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
429 dirnode->inode->st.st_mtimensec = specnode->st_mtimespec.tv_nsec;
430 dirnode->inode->st.st_atimensec = specnode->st_mtimespec.tv_nsec;
431 dirnode->inode->st.st_ctimensec = start_time.tv_nsec;
434 if (specnode->flags & (F_UID | F_UNAME)) {
435 ASEPRINT("uid", "%d",
436 dirnode->inode->st.st_uid, specnode->st_uid);
437 dirnode->inode->st.st_uid = specnode->st_uid;
439 #if HAVE_STRUCT_STAT_ST_FLAGS
440 if (specnode->flags & F_FLAGS) {
441 ASEPRINT("flags", "%#lX",
442 (unsigned long)dirnode->inode->st.st_flags,
443 (unsigned long)specnode->st_flags);
444 dirnode->inode->st.st_flags = specnode->st_flags;
449 dirnode->flags |= FSNODE_F_HASSPEC;
455 * dump the fsnodes from `cur', based in the directory `dir'
458 dump_fsnodes(const char *dir, fsnode *root)
461 char path[MAXPATHLEN + 1];
463 assert (dir != NULL);
464 printf("dump_fsnodes: %s %p\n", dir, root);
465 for (cur = root; cur != NULL; cur = cur->next) {
466 if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name)
468 errx(1, "Pathname too long.");
470 if (debug & DEBUG_DUMP_FSNODES_VERBOSE)
471 printf("cur=%8p parent=%8p first=%8p ",
472 cur, cur->parent, cur->first);
473 printf("%7s: %s", inode_type(cur->type), path);
474 if (S_ISLNK(cur->type)) {
475 assert(cur->symlink != NULL);
476 printf(" -> %s", cur->symlink);
478 assert (cur->symlink == NULL);
480 if (cur->inode->nlink > 1)
481 printf(", nlinks=%d", cur->inode->nlink);
485 assert (cur->type == S_IFDIR);
486 dump_fsnodes(path, cur->child);
489 printf("dump_fsnodes: finished %s\n", dir);
495 * for a given inode type `mode', return a descriptive string.
498 inode_type(mode_t mode)
513 /* XXX should not happen but handle them */
524 * return pointer to fsnode matching `entry's st_ino & st_dev if it exists,
525 * otherwise add `entry' to table and return NULL
528 link_check(fsinode *entry)
530 static struct dupnode {
535 static int ndups, maxdups;
539 assert (entry != NULL);
541 /* XXX; maybe traverse in reverse for speed? */
542 for (i = 0; i < ndups; i++) {
543 if (dups[i].dev == entry->st.st_dev &&
544 dups[i].ino == entry->st.st_ino) {
545 if (debug & DEBUG_WALK_DIR_LINKCHECK)
546 printf("link_check: found [%d,%d]\n",
547 entry->st.st_dev, entry->st.st_ino);
548 return (dups[i].dup);
552 if (debug & DEBUG_WALK_DIR_LINKCHECK)
553 printf("link_check: no match for [%d, %d]\n",
554 entry->st.st_dev, entry->st.st_ino);
555 if (ndups == maxdups) {
556 if ((newdups = realloc(dups, sizeof(struct dupnode) * (maxdups + 128)))
558 err(1, "Memory allocation error");
562 dups[ndups].dev = entry->st.st_dev;
563 dups[ndups].ino = entry->st.st_ino;
564 dups[ndups].dup = entry;