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