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