walk.c revision 1.22 1 /* $NetBSD: walk.c,v 1.22 2006/10/10 01:46:49 dbj 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 #if HAVE_NBTOOL_CONFIG_H
39 #include "nbtool_config.h"
40 #endif
41
42 #include <sys/cdefs.h>
43 #if defined(__RCSID) && !defined(__lint)
44 __RCSID("$NetBSD: walk.c,v 1.22 2006/10/10 01:46:49 dbj Exp $");
45 #endif /* !__lint */
46
47 #include <sys/param.h>
48
49 #include <assert.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <stdio.h>
53 #include <dirent.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <sys/stat.h>
58
59 #include "makefs.h"
60 #include "mtree.h"
61
62 static void apply_specdir(const char *, NODE *, fsnode *);
63 static void apply_specentry(const char *, NODE *, fsnode *);
64 static fsnode *create_fsnode(const char *, struct stat *);
65 static fsinode *link_check(fsinode *);
66
67
68 /*
69 * walk_dir --
70 * build a tree of fsnodes from `dir', with a parent fsnode of `parent'
71 * (which may be NULL for the root of the tree).
72 * each "level" is a directory, with the "." entry guaranteed to be
73 * at the start of the list, and without ".." entries.
74 */
75 fsnode *
76 walk_dir(const char *dir, fsnode *parent)
77 {
78 fsnode *first, *cur, *prev;
79 DIR *dirp;
80 struct dirent *dent;
81 char path[MAXPATHLEN + 1];
82 struct stat stbuf;
83
84 assert(dir != NULL);
85
86 if (debug & DEBUG_WALK_DIR)
87 printf("walk_dir: %s %p\n", dir, parent);
88 if ((dirp = opendir(dir)) == NULL)
89 err(1, "Can't opendir `%s'", dir);
90 first = prev = NULL;
91 while ((dent = readdir(dirp)) != NULL) {
92 if (strcmp(dent->d_name, "..") == 0)
93 continue;
94 if (debug & DEBUG_WALK_DIR_NODE)
95 printf("scanning %s/%s\n", dir, dent->d_name);
96 if (snprintf(path, sizeof(path), "%s/%s", dir, dent->d_name)
97 >= sizeof(path))
98 errx(1, "Pathname too long.");
99 if (lstat(path, &stbuf) == -1)
100 err(1, "Can't lstat `%s'", path);
101 #ifdef S_ISSOCK
102 if (S_ISSOCK(stbuf.st_mode & S_IFMT)) {
103 if (debug & DEBUG_WALK_DIR_NODE)
104 printf(" skipping socket %s\n", path);
105 continue;
106 }
107 #endif
108
109 cur = create_fsnode(dent->d_name, &stbuf);
110 cur->parent = parent;
111 if (strcmp(dent->d_name, ".") == 0) {
112 /* ensure "." is at the start of the list */
113 cur->next = first;
114 first = cur;
115 if (! prev)
116 prev = cur;
117 } else { /* not "." */
118 if (prev)
119 prev->next = cur;
120 prev = cur;
121 if (!first)
122 first = cur;
123 if (S_ISDIR(cur->type)) {
124 cur->child = walk_dir(path, cur);
125 continue;
126 }
127 }
128 if (stbuf.st_nlink > 1) {
129 fsinode *curino;
130
131 curino = link_check(cur->inode);
132 if (curino != NULL) {
133 free(cur->inode);
134 cur->inode = curino;
135 cur->inode->nlink++;
136 if (debug & DEBUG_WALK_DIR_LINKCHECK)
137 printf("link_check: found [%u, %llu]\n",
138 curino->st.st_dev,
139 (unsigned long long)curino->st.st_ino);
140 }
141 }
142 if (S_ISLNK(cur->type)) {
143 char slink[PATH_MAX+1];
144 int llen;
145
146 llen = readlink(path, slink, sizeof(slink) - 1);
147 if (llen == -1)
148 err(1, "Readlink `%s'", path);
149 slink[llen] = '\0';
150 if ((cur->symlink = strdup(slink)) == NULL)
151 err(1, "Memory allocation error");
152 }
153 }
154 for (cur = first; cur != NULL; cur = cur->next)
155 cur->first = first;
156 if (closedir(dirp) == -1)
157 err(1, "Can't closedir `%s'", dir);
158 return (first);
159 }
160
161 static fsnode *
162 create_fsnode(const char *name, struct stat *stbuf)
163 {
164 fsnode *cur;
165
166 if ((cur = calloc(1, sizeof(fsnode))) == NULL ||
167 (cur->name = strdup(name)) == NULL ||
168 (cur->inode = calloc(1, sizeof(fsinode))) == NULL)
169 err(1, "Memory allocation error");
170 cur->type = stbuf->st_mode & S_IFMT;
171 cur->inode->nlink = 1;
172 cur->inode->st = *stbuf;
173 return (cur);
174 }
175
176 /*
177 * free_fsnodes --
178 * Removes node from tree and frees it and all of
179 * its decendents.
180 */
181 void
182 free_fsnodes(fsnode *node)
183 {
184 fsnode *cur, *next;
185
186 assert(node != NULL);
187
188 /* for ".", start with actual parent node */
189 if (node->first == node) {
190 assert(node->name[0] == '.' && node->name[1] == '\0');
191 if (node->parent) {
192 assert(node->parent->child == node);
193 node = node->parent;
194 }
195 }
196
197 /* Find ourselves in our sibling list and unlink */
198 if (node->first != node) {
199 for (cur = node->first; cur->next; cur = cur->next) {
200 if (cur->next == node) {
201 cur->next = node->next;
202 node->next = NULL;
203 break;
204 }
205 }
206 }
207
208 for (cur = node; cur != NULL; cur = next) {
209 next = cur->next;
210 if (cur->child) {
211 cur->child->parent = NULL;
212 free_fsnodes(cur->child);
213 }
214 if (cur->inode->nlink-- == 1)
215 free(cur->inode);
216 if (cur->symlink)
217 free(cur->symlink);
218 free(cur->name);
219 free(cur);
220 }
221 }
222
223 /*
224 * apply_specfile --
225 * read in the mtree(8) specfile, and apply it to the tree
226 * at dir,parent. parameters in parent on equivalent types
227 * will be changed to those found in specfile, and missing
228 * entries will be added.
229 */
230 void
231 apply_specfile(const char *specfile, const char *dir, fsnode *parent)
232 {
233 struct timeval start;
234 FILE *fp;
235 NODE *root;
236
237 assert(specfile != NULL);
238 assert(parent != NULL);
239
240 if (debug & DEBUG_APPLY_SPECFILE)
241 printf("apply_specfile: %s, %s %p\n", specfile, dir, parent);
242
243 /* read in the specfile */
244 if ((fp = fopen(specfile, "r")) == NULL)
245 err(1, "Can't open `%s'", specfile);
246 TIMER_START(start);
247 root = spec(fp);
248 TIMER_RESULTS(start, "spec");
249 if (fclose(fp) == EOF)
250 err(1, "Can't close `%s'", specfile);
251
252 /* perform some sanity checks */
253 if (root == NULL)
254 errx(1, "Specfile `%s' did not contain a tree", specfile);
255 assert(strcmp(root->name, ".") == 0);
256 assert(root->type == F_DIR);
257
258 /* merge in the changes */
259 apply_specdir(dir, root, parent);
260
261 free_nodes(root);
262 }
263
264 static void
265 apply_specdir(const char *dir, NODE *specnode, fsnode *dirnode)
266 {
267 char path[MAXPATHLEN + 1];
268 NODE *curnode;
269 fsnode *curfsnode;
270
271 assert(specnode != NULL);
272 assert(dirnode != NULL);
273
274 if (debug & DEBUG_APPLY_SPECFILE)
275 printf("apply_specdir: %s %p %p\n", dir, specnode, dirnode);
276
277 if (specnode->type != F_DIR)
278 errx(1, "Specfile node `%s/%s' is not a directory",
279 dir, specnode->name);
280 if (dirnode->type != S_IFDIR)
281 errx(1, "Directory node `%s/%s' is not a directory",
282 dir, dirnode->name);
283
284 apply_specentry(dir, specnode, dirnode);
285
286 /* now walk specnode->child matching up with dirnode */
287 for (curnode = specnode->child; curnode != NULL;
288 curnode = curnode->next) {
289 if (debug & DEBUG_APPLY_SPECENTRY)
290 printf("apply_specdir: spec %s\n",
291 curnode->name);
292 for (curfsnode = dirnode->next; curfsnode != NULL;
293 curfsnode = curfsnode->next) {
294 #if 0 /* too verbose for now */
295 if (debug & DEBUG_APPLY_SPECENTRY)
296 printf("apply_specdir: dirent %s\n",
297 curfsnode->name);
298 #endif
299 if (strcmp(curnode->name, curfsnode->name) == 0)
300 break;
301 }
302 if (snprintf(path, sizeof(path), "%s/%s",
303 dir, curnode->name) >= sizeof(path))
304 errx(1, "Pathname too long.");
305 if (curfsnode == NULL) { /* need new entry */
306 struct stat stbuf;
307
308 /*
309 * don't add optional spec entries
310 * that lack an existing fs entry
311 */
312 if ((curnode->flags & F_OPT) &&
313 lstat(path, &stbuf) == -1)
314 continue;
315
316 /* check that enough info is provided */
317 #define NODETEST(t, m) \
318 if (!(t)) \
319 errx(1, "`%s': %s not provided", path, m)
320 NODETEST(curnode->flags & F_TYPE, "type");
321 NODETEST(curnode->flags & F_MODE, "mode");
322 /* XXX: require F_TIME ? */
323 NODETEST(curnode->flags & F_GID ||
324 curnode->flags & F_GNAME, "group");
325 NODETEST(curnode->flags & F_UID ||
326 curnode->flags & F_UNAME, "user");
327 if (curnode->type == F_BLOCK || curnode->type == F_CHAR)
328 NODETEST(curnode->flags & F_DEV,
329 "device number");
330 #undef NODETEST
331
332 if (debug & DEBUG_APPLY_SPECFILE)
333 printf("apply_specdir: adding %s\n",
334 curnode->name);
335 /* build minimal fsnode */
336 memset(&stbuf, 0, sizeof(stbuf));
337 stbuf.st_mode = nodetoino(curnode->type);
338 stbuf.st_nlink = 1;
339 stbuf.st_mtime = stbuf.st_atime =
340 stbuf.st_ctime = start_time.tv_sec;
341 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
342 stbuf.st_mtimensec = stbuf.st_atimensec =
343 stbuf.st_ctimensec = start_time.tv_nsec;
344 #endif
345 curfsnode = create_fsnode(curnode->name, &stbuf);
346 curfsnode->parent = dirnode->parent;
347 curfsnode->first = dirnode;
348 curfsnode->next = dirnode->next;
349 dirnode->next = curfsnode;
350 if (curfsnode->type == S_IFDIR) {
351 /* for dirs, make "." entry as well */
352 curfsnode->child = create_fsnode(".", &stbuf);
353 curfsnode->child->parent = curfsnode;
354 curfsnode->child->first = curfsnode->child;
355 }
356 if (curfsnode->type == S_IFLNK) {
357 assert(curnode->slink != NULL);
358 /* for symlinks, copy the target */
359 if ((curfsnode->symlink =
360 strdup(curnode->slink)) == NULL)
361 err(1, "Memory allocation error");
362 }
363 }
364 apply_specentry(dir, curnode, curfsnode);
365 if (curnode->type == F_DIR) {
366 if (curfsnode->type != S_IFDIR)
367 errx(1, "`%s' is not a directory", path);
368 assert (curfsnode->child != NULL);
369 apply_specdir(path, curnode, curfsnode->child);
370 }
371 }
372 }
373
374 static void
375 apply_specentry(const char *dir, NODE *specnode, fsnode *dirnode)
376 {
377
378 assert(specnode != NULL);
379 assert(dirnode != NULL);
380
381 if (nodetoino(specnode->type) != dirnode->type)
382 errx(1, "`%s/%s' type mismatch: specfile %s, tree %s",
383 dir, specnode->name, inode_type(nodetoino(specnode->type)),
384 inode_type(dirnode->type));
385
386 if (debug & DEBUG_APPLY_SPECENTRY)
387 printf("apply_specentry: %s/%s\n", dir, dirnode->name);
388
389 #define ASEPRINT(t, b, o, n) \
390 if (debug & DEBUG_APPLY_SPECENTRY) \
391 printf("\t\t\tchanging %s from " b " to " b "\n", \
392 t, o, n)
393
394 if (specnode->flags & (F_GID | F_GNAME)) {
395 ASEPRINT("gid", "%d",
396 dirnode->inode->st.st_gid, specnode->st_gid);
397 dirnode->inode->st.st_gid = specnode->st_gid;
398 }
399 if (specnode->flags & F_MODE) {
400 ASEPRINT("mode", "%#o",
401 dirnode->inode->st.st_mode & ALLPERMS, specnode->st_mode);
402 dirnode->inode->st.st_mode &= ~ALLPERMS;
403 dirnode->inode->st.st_mode |= (specnode->st_mode & ALLPERMS);
404 }
405 /* XXX: ignoring F_NLINK for now */
406 if (specnode->flags & F_SIZE) {
407 ASEPRINT("size", "%lld",
408 (long long)dirnode->inode->st.st_size,
409 (long long)specnode->st_size);
410 dirnode->inode->st.st_size = specnode->st_size;
411 }
412 if (specnode->flags & F_SLINK) {
413 assert(dirnode->symlink != NULL);
414 assert(specnode->slink != NULL);
415 ASEPRINT("symlink", "%s", dirnode->symlink, specnode->slink);
416 free(dirnode->symlink);
417 if ((dirnode->symlink = strdup(specnode->slink)) == NULL)
418 err(1, "Memory allocation error");
419 }
420 if (specnode->flags & F_TIME) {
421 ASEPRINT("time", "%ld",
422 (long)dirnode->inode->st.st_mtime,
423 (long)specnode->st_mtimespec.tv_sec);
424 dirnode->inode->st.st_mtime = specnode->st_mtimespec.tv_sec;
425 dirnode->inode->st.st_atime = specnode->st_mtimespec.tv_sec;
426 dirnode->inode->st.st_ctime = start_time.tv_sec;
427 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
428 dirnode->inode->st.st_mtimensec = specnode->st_mtimespec.tv_nsec;
429 dirnode->inode->st.st_atimensec = specnode->st_mtimespec.tv_nsec;
430 dirnode->inode->st.st_ctimensec = start_time.tv_nsec;
431 #endif
432 }
433 if (specnode->flags & (F_UID | F_UNAME)) {
434 ASEPRINT("uid", "%d",
435 dirnode->inode->st.st_uid, specnode->st_uid);
436 dirnode->inode->st.st_uid = specnode->st_uid;
437 }
438 #if HAVE_STRUCT_STAT_ST_FLAGS
439 if (specnode->flags & F_FLAGS) {
440 ASEPRINT("flags", "%#lX",
441 (unsigned long)dirnode->inode->st.st_flags,
442 (unsigned long)specnode->st_flags);
443 dirnode->inode->st.st_flags = specnode->st_flags;
444 }
445 #endif
446 if (specnode->flags & F_DEV) {
447 ASEPRINT("rdev", "%#x",
448 dirnode->inode->st.st_rdev, specnode->st_rdev);
449 dirnode->inode->st.st_rdev = specnode->st_rdev;
450 }
451 #undef ASEPRINT
452
453 dirnode->flags |= FSNODE_F_HASSPEC;
454 }
455
456
457 /*
458 * dump_fsnodes --
459 * dump the fsnodes from `cur', based in the directory `dir'
460 */
461 void
462 dump_fsnodes(const char *dir, fsnode *root)
463 {
464 fsnode *cur;
465 char path[MAXPATHLEN + 1];
466
467 assert (dir != NULL);
468 printf("dump_fsnodes: %s %p\n", dir, root);
469 for (cur = root; cur != NULL; cur = cur->next) {
470 if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name)
471 >= sizeof(path))
472 errx(1, "Pathname too long.");
473
474 if (debug & DEBUG_DUMP_FSNODES_VERBOSE)
475 printf("cur=%8p parent=%8p first=%8p ",
476 cur, cur->parent, cur->first);
477 printf("%7s: %s", inode_type(cur->type), path);
478 if (S_ISLNK(cur->type)) {
479 assert(cur->symlink != NULL);
480 printf(" -> %s", cur->symlink);
481 } else {
482 assert (cur->symlink == NULL);
483 }
484 if (cur->inode->nlink > 1)
485 printf(", nlinks=%d", cur->inode->nlink);
486 putchar('\n');
487
488 if (cur->child) {
489 assert (cur->type == S_IFDIR);
490 dump_fsnodes(path, cur->child);
491 }
492 }
493 printf("dump_fsnodes: finished %s\n", dir);
494 }
495
496
497 /*
498 * inode_type --
499 * for a given inode type `mode', return a descriptive string.
500 * for most cases, uses inotype() from mtree/misc.c
501 */
502 const char *
503 inode_type(mode_t mode)
504 {
505
506 if (S_ISLNK(mode))
507 return ("symlink"); /* inotype() returns "link"... */
508 return (inotype(mode));
509 }
510
511
512 /*
513 * link_check --
514 * return pointer to fsinode matching `entry's st_ino & st_dev if it exists,
515 * otherwise add `entry' to table and return NULL
516 */
517 /* This was borrowed from du.c and tweaked to keep an fsnode
518 * pointer instead. -- dbj (at) netbsd.org
519 */
520 static fsinode *
521 link_check(fsinode *entry)
522 {
523 static struct entry {
524 fsinode *data;
525 } *htable;
526 static int htshift; /* log(allocated size) */
527 static int htmask; /* allocated size - 1 */
528 static int htused; /* 2*number of insertions */
529 int h, h2;
530 uint64_t tmp;
531 /* this constant is (1<<64)/((1+sqrt(5))/2)
532 * aka (word size)/(golden ratio)
533 */
534 const uint64_t HTCONST = 11400714819323198485ULL;
535 const int HTBITS = 64;
536
537 /* Never store zero in hashtable */
538 assert(entry);
539
540 /* Extend hash table if necessary, keep load under 0.5 */
541 if (htused<<1 >= htmask) {
542 struct entry *ohtable;
543
544 if (!htable)
545 htshift = 10; /* starting hashtable size */
546 else
547 htshift++; /* exponential hashtable growth */
548
549 htmask = (1 << htshift) - 1;
550 htused = 0;
551
552 ohtable = htable;
553 htable = calloc(htmask+1, sizeof(*htable));
554 if (!htable)
555 err(1, "Memory allocation error");
556
557 /* populate newly allocated hashtable */
558 if (ohtable) {
559 int i;
560 for (i = 0; i <= htmask>>1; i++)
561 if (ohtable[i].data)
562 link_check(ohtable[i].data);
563 free(ohtable);
564 }
565 }
566
567 /* multiplicative hashing */
568 tmp = entry->st.st_dev;
569 tmp <<= HTBITS>>1;
570 tmp |= entry->st.st_ino;
571 tmp *= HTCONST;
572 h = tmp >> (HTBITS - htshift);
573 h2 = 1 | ( tmp >> (HTBITS - (htshift<<1) - 1)); /* must be odd */
574
575 /* open address hashtable search with double hash probing */
576 while (htable[h].data) {
577 if ((htable[h].data->st.st_ino == entry->st.st_ino) &&
578 (htable[h].data->st.st_dev == entry->st.st_dev)) {
579 return htable[h].data;
580 }
581 h = (h + h2) & htmask;
582 }
583
584 /* Insert the current entry into hashtable */
585 htable[h].data = entry;
586 htused++;
587 return NULL;
588 }
589