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