1 /* $NetBSD: walk.c,v 1.24 2008/12/28 21:51:46 christos Exp $ */
2
3 /*
4 * SPDX-License-Identifier: BSD-4-Clause
5 *
6 * Copyright (c) 2001 Wasabi Systems, Inc.
7 * All rights reserved.
8 *
9 * Written by Luke Mewburn for Wasabi Systems, Inc.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed for the NetBSD Project by
22 * Wasabi Systems, Inc.
23 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
24 * or promote products derived from this software without specific prior
25 * written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 *
39 * $FreeBSD: head/usr.sbin/makefs/walk.c 326276 2017-11-27 15:37:16Z pfg $
40 */
41
42
43 #include <sys/param.h>
44 #include <sys/stat.h>
45 #include <sys/time.h>
46
47 #include <assert.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <stdio.h>
51 #include <dirent.h>
52 #include <libutil.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56
57 #include "makefs.h"
58 #include "mtree.h"
59 #include "extern.h"
60
61 static void apply_specdir(const char *, NODE *, fsnode *, int);
62 static void apply_specentry(const char *, NODE *, fsnode *);
63 static fsnode *create_fsnode(const char *, const char *, const char *,
64 struct stat *);
65
66
67 /*
68 * walk_dir --
69 * build a tree of fsnodes from `root' and `dir', with a parent
70 * fsnode of `parent' (which may be NULL for the root of the tree).
71 * append the tree to a fsnode of `join' if it is not NULL.
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 *
walk_dir(const char * root,const char * dir,fsnode * parent,fsnode * join)76 walk_dir(const char *root, const char *dir, fsnode *parent, fsnode *join)
77 {
78 fsnode *first, *cur, *prev, *last;
79 DIR *dirp;
80 struct dirent *dent;
81 char path[MAXPATHLEN + 1];
82 struct stat stbuf;
83 char *name, *rp;
84 size_t len;
85 int dot;
86
87 assert(root != NULL);
88 assert(dir != NULL);
89
90 len = snprintf(path, sizeof(path), "%s/%s", root, dir);
91 if (len >= sizeof(path))
92 errx(1, "Pathname too long.");
93 if (debug & DEBUG_WALK_DIR)
94 printf("walk_dir: %s %p\n", path, parent);
95 if ((dirp = opendir(path)) == NULL)
96 err(1, "Can't opendir `%s'", path);
97 rp = path + strlen(root) + 1;
98 if (join != NULL) {
99 first = cur = join;
100 while (cur->next != NULL)
101 cur = cur->next;
102 prev = cur;
103 } else
104 first = prev = NULL;
105 last = prev;
106 while ((dent = readdir(dirp)) != NULL) {
107 name = dent->d_name;
108 dot = 0;
109 if (name[0] == '.')
110 switch (name[1]) {
111 case '\0': /* "." */
112 if (join != NULL)
113 continue;
114 dot = 1;
115 break;
116 case '.': /* ".." */
117 if (name[2] == '\0')
118 continue;
119 /* FALLTHROUGH */
120 default:
121 dot = 0;
122 }
123 if (debug & DEBUG_WALK_DIR_NODE)
124 printf("scanning %s/%s/%s\n", root, dir, name);
125 if ((size_t)snprintf(path + len, sizeof(path) - len, "/%s",
126 name) >= sizeof(path) - len)
127 errx(1, "Pathname too long.");
128 if (lstat(path, &stbuf) == -1)
129 err(1, "Can't lstat `%s'", path);
130 #ifdef S_ISSOCK
131 if (S_ISSOCK(stbuf.st_mode & S_IFMT)) {
132 if (debug & DEBUG_WALK_DIR_NODE)
133 printf(" skipping socket %s\n", path);
134 continue;
135 }
136 #endif
137
138 if (join != NULL) {
139 cur = join->next;
140 for (;;) {
141 if (cur == NULL || strcmp(cur->name, name) == 0)
142 break;
143 if (cur == last) {
144 cur = NULL;
145 break;
146 }
147 cur = cur->next;
148 }
149 if (cur != NULL) {
150 if (S_ISDIR(cur->type) &&
151 S_ISDIR(stbuf.st_mode)) {
152 if (debug & DEBUG_WALK_DIR_NODE)
153 printf("merging %s with %p\n",
154 path, cur->child);
155 cur->child = walk_dir(root, rp, cur,
156 cur->child);
157 continue;
158 }
159 errx(1, "Can't merge %s `%s' with existing %s",
160 inode_type(stbuf.st_mode), path,
161 inode_type(cur->type));
162 }
163 }
164
165 cur = create_fsnode(root, dir, name, &stbuf);
166 cur->parent = parent;
167 if (dot) {
168 /* ensure "." is at the start of the list */
169 cur->next = first;
170 first = cur;
171 if (! prev)
172 prev = cur;
173 cur->first = first;
174 } else { /* not "." */
175 if (prev)
176 prev->next = cur;
177 prev = cur;
178 if (!first)
179 first = cur;
180 cur->first = first;
181 if (S_ISDIR(cur->type)) {
182 cur->child = walk_dir(root, rp, cur, NULL);
183 continue;
184 }
185 }
186 if (stbuf.st_nlink > 1) {
187 fsinode *curino;
188
189 curino = link_check(cur->inode);
190 if (curino != NULL) {
191 free(cur->inode);
192 cur->inode = curino;
193 cur->inode->nlink++;
194 if (debug & DEBUG_WALK_DIR_LINKCHECK)
195 printf("link_check: found [%llu, %llu]\n",
196 (unsigned long long)curino->st.st_dev,
197 (unsigned long long)curino->st.st_ino);
198 }
199 }
200 if (S_ISLNK(cur->type)) {
201 char slink[PATH_MAX+1];
202 int llen;
203
204 llen = readlink(path, slink, sizeof(slink) - 1);
205 if (llen == -1)
206 err(1, "Readlink `%s'", path);
207 slink[llen] = '\0';
208 cur->symlink = estrdup(slink);
209 }
210 }
211 assert(first != NULL);
212 if (join == NULL)
213 for (cur = first->next; cur != NULL; cur = cur->next)
214 cur->first = first;
215 if (closedir(dirp) == -1)
216 err(1, "Can't closedir `%s/%s'", root, dir);
217 return (first);
218 }
219
220 static fsnode *
create_fsnode(const char * root,const char * path,const char * name,struct stat * stbuf)221 create_fsnode(const char *root, const char *path, const char *name,
222 struct stat *stbuf)
223 {
224 fsnode *cur;
225
226 cur = ecalloc(1, sizeof(*cur));
227 cur->path = estrdup(path);
228 cur->name = estrdup(name);
229 cur->inode = ecalloc(1, sizeof(*cur->inode));
230 cur->root = root;
231 cur->type = stbuf->st_mode & S_IFMT;
232 cur->inode->nlink = 1;
233 cur->inode->st = *stbuf;
234 if (stampst.st_ino) {
235 cur->inode->st.st_atime = stampst.st_atime;
236 cur->inode->st.st_mtime = stampst.st_mtime;
237 cur->inode->st.st_ctime = stampst.st_ctime;
238 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
239 cur->inode->st.st_atimensec = stampst.st_atimensec;
240 cur->inode->st.st_mtimensec = stampst.st_mtimensec;
241 cur->inode->st.st_ctimensec = stampst.st_ctimensec;
242 #endif
243 #if HAVE_STRUCT_STAT_BIRTHTIME
244 cur->inode->st.st_birthtime = stampst.st_birthtime;
245 cur->inode->st.st_birthtimensec = stampst.st_birthtimensec;
246 #endif
247 }
248 return (cur);
249 }
250
251 /*
252 * free_fsnodes --
253 * Removes node from tree and frees it and all of
254 * its descendants.
255 */
256 void
free_fsnodes(fsnode * node)257 free_fsnodes(fsnode *node)
258 {
259 fsnode *cur, *next;
260
261 /* DragonFly: HAMMER2 ioctl commands could pass NULL node */
262 if (node == NULL)
263 return;
264
265 /* for ".", start with actual parent node */
266 if (node->first == node) {
267 assert(node->name[0] == '.' && node->name[1] == '\0');
268 if (node->parent) {
269 assert(node->parent->child == node);
270 node = node->parent;
271 }
272 }
273
274 /* Find ourselves in our sibling list and unlink */
275 if (node->first != node) {
276 for (cur = node->first; cur->next; cur = cur->next) {
277 if (cur->next == node) {
278 cur->next = node->next;
279 node->next = NULL;
280 break;
281 }
282 }
283 }
284
285 for (cur = node; cur != NULL; cur = next) {
286 next = cur->next;
287 if (cur->child) {
288 cur->child->parent = NULL;
289 free_fsnodes(cur->child);
290 }
291 if (cur->inode->nlink-- == 1)
292 free(cur->inode);
293 if (cur->symlink)
294 free(cur->symlink);
295 free(cur->path);
296 free(cur->name);
297 free(cur);
298 }
299 }
300
301 /*
302 * apply_specfile --
303 * read in the mtree(8) specfile, and apply it to the tree
304 * at dir,parent. parameters in parent on equivalent types
305 * will be changed to those found in specfile, and missing
306 * entries will be added.
307 */
308 void
apply_specfile(const char * specfile,const char * dir,fsnode * parent,int speconly)309 apply_specfile(const char *specfile, const char *dir, fsnode *parent, int speconly)
310 {
311 struct timeval start;
312 FILE *fp;
313 NODE *root;
314
315 assert(specfile != NULL);
316 assert(parent != NULL);
317
318 if (debug & DEBUG_APPLY_SPECFILE)
319 printf("apply_specfile: %s, %s %p\n", specfile, dir, parent);
320
321 /* read in the specfile */
322 if ((fp = fopen(specfile, "r")) == NULL)
323 err(1, "Can't open `%s'", specfile);
324 TIMER_START(start);
325 root = spec(fp);
326 TIMER_RESULTS(start, "spec");
327 if (fclose(fp) == EOF)
328 err(1, "Can't close `%s'", specfile);
329
330 /* perform some sanity checks */
331 if (root == NULL)
332 errx(1, "Specfile `%s' did not contain a tree", specfile);
333 assert(strcmp(root->name, ".") == 0);
334 assert(root->type == F_DIR);
335
336 /* merge in the changes */
337 apply_specdir(dir, root, parent, speconly);
338
339 free_nodes(root);
340 }
341
342 static void
apply_specdir(const char * dir,NODE * specnode,fsnode * dirnode,int speconly)343 apply_specdir(const char *dir, NODE *specnode, fsnode *dirnode, int speconly)
344 {
345 char path[MAXPATHLEN + 1];
346 NODE *curnode;
347 fsnode *curfsnode;
348
349 assert(specnode != NULL);
350 assert(dirnode != NULL);
351
352 if (debug & DEBUG_APPLY_SPECFILE)
353 printf("apply_specdir: %s %p %p\n", dir, specnode, dirnode);
354
355 if (specnode->type != F_DIR)
356 errx(1, "Specfile node `%s/%s' is not a directory",
357 dir, specnode->name);
358 if (dirnode->type != S_IFDIR)
359 errx(1, "Directory node `%s/%s' is not a directory",
360 dir, dirnode->name);
361
362 apply_specentry(dir, specnode, dirnode);
363
364 /* Remove any filesystem nodes not found in specfile */
365 /* XXX inefficient. This is O^2 in each dir and it would
366 * have been better never to have walked this part of the tree
367 * to begin with
368 */
369 if (speconly) {
370 fsnode *next;
371 assert(dirnode->name[0] == '.' && dirnode->name[1] == '\0');
372 for (curfsnode = dirnode->next; curfsnode != NULL; curfsnode = next) {
373 next = curfsnode->next;
374 for (curnode = specnode->child; curnode != NULL;
375 curnode = curnode->next) {
376 if (strcmp(curnode->name, curfsnode->name) == 0)
377 break;
378 }
379 if (curnode == NULL) {
380 if (debug & DEBUG_APPLY_SPECONLY) {
381 printf("apply_specdir: trimming %s/%s %p\n", dir, curfsnode->name, curfsnode);
382 }
383 free_fsnodes(curfsnode);
384 }
385 }
386 }
387
388 /* now walk specnode->child matching up with dirnode */
389 for (curnode = specnode->child; curnode != NULL;
390 curnode = curnode->next) {
391 if (debug & DEBUG_APPLY_SPECENTRY)
392 printf("apply_specdir: spec %s\n",
393 curnode->name);
394 for (curfsnode = dirnode->next; curfsnode != NULL;
395 curfsnode = curfsnode->next) {
396 #if 0 /* too verbose for now */
397 if (debug & DEBUG_APPLY_SPECENTRY)
398 printf("apply_specdir: dirent %s\n",
399 curfsnode->name);
400 #endif
401 if (strcmp(curnode->name, curfsnode->name) == 0)
402 break;
403 }
404 if ((size_t)snprintf(path, sizeof(path), "%s/%s", dir,
405 curnode->name) >= sizeof(path))
406 errx(1, "Pathname too long.");
407 if (curfsnode == NULL) { /* need new entry */
408 struct stat stbuf;
409
410 /*
411 * don't add optional spec entries
412 * that lack an existing fs entry
413 */
414 if ((curnode->flags & F_OPT) &&
415 lstat(path, &stbuf) == -1)
416 continue;
417
418 /* check that enough info is provided */
419 #define NODETEST(t, m) \
420 if (!(t)) \
421 errx(1, "`%s': %s not provided", path, m)
422 NODETEST(curnode->flags & F_TYPE, "type");
423 NODETEST(curnode->flags & F_MODE, "mode");
424 /* XXX: require F_TIME ? */
425 NODETEST(curnode->flags & F_GID ||
426 curnode->flags & F_GNAME, "group");
427 NODETEST(curnode->flags & F_UID ||
428 curnode->flags & F_UNAME, "user");
429 /* if (curnode->type == F_BLOCK || curnode->type == F_CHAR)
430 NODETEST(curnode->flags & F_DEV,
431 "device number");*/
432 #undef NODETEST
433
434 if (debug & DEBUG_APPLY_SPECFILE)
435 printf("apply_specdir: adding %s\n",
436 curnode->name);
437 /* build minimal fsnode */
438 memset(&stbuf, 0, sizeof(stbuf));
439 stbuf.st_mode = nodetoino(curnode->type);
440 stbuf.st_nlink = 1;
441 stbuf.st_mtime = stbuf.st_atime =
442 stbuf.st_ctime = start_time.tv_sec;
443 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
444 stbuf.st_mtimensec = stbuf.st_atimensec =
445 stbuf.st_ctimensec = start_time.tv_nsec;
446 #endif
447 curfsnode = create_fsnode(".", ".", curnode->name,
448 &stbuf);
449 curfsnode->parent = dirnode->parent;
450 curfsnode->first = dirnode;
451 curfsnode->next = dirnode->next;
452 dirnode->next = curfsnode;
453 if (curfsnode->type == S_IFDIR) {
454 /* for dirs, make "." entry as well */
455 curfsnode->child = create_fsnode(".", ".", ".",
456 &stbuf);
457 curfsnode->child->parent = curfsnode;
458 curfsnode->child->first = curfsnode->child;
459 }
460 if (curfsnode->type == S_IFLNK) {
461 assert(curnode->slink != NULL);
462 /* for symlinks, copy the target */
463 curfsnode->symlink = estrdup(curnode->slink);
464 }
465 }
466 apply_specentry(dir, curnode, curfsnode);
467 if (curnode->type == F_DIR) {
468 if (curfsnode->type != S_IFDIR)
469 errx(1, "`%s' is not a directory", path);
470 assert (curfsnode->child != NULL);
471 apply_specdir(path, curnode, curfsnode->child, speconly);
472 }
473 }
474 }
475
476 static void
apply_specentry(const char * dir,NODE * specnode,fsnode * dirnode)477 apply_specentry(const char *dir, NODE *specnode, fsnode *dirnode)
478 {
479
480 assert(specnode != NULL);
481 assert(dirnode != NULL);
482
483 if (nodetoino(specnode->type) != dirnode->type)
484 errx(1, "`%s/%s' type mismatch: specfile %s, tree %s",
485 dir, specnode->name, inode_type(nodetoino(specnode->type)),
486 inode_type(dirnode->type));
487
488 if (debug & DEBUG_APPLY_SPECENTRY)
489 printf("apply_specentry: %s/%s\n", dir, dirnode->name);
490
491 #define ASEPRINT(t, b, o, n) \
492 if (debug & DEBUG_APPLY_SPECENTRY) \
493 printf("\t\t\tchanging %s from " b " to " b "\n", \
494 t, o, n)
495
496 if (specnode->flags & (F_GID | F_GNAME)) {
497 ASEPRINT("gid", "%d",
498 dirnode->inode->st.st_gid, specnode->st_gid);
499 dirnode->inode->st.st_gid = specnode->st_gid;
500 }
501 if (specnode->flags & F_MODE) {
502 ASEPRINT("mode", "%#o",
503 dirnode->inode->st.st_mode & ALLPERMS, specnode->st_mode);
504 dirnode->inode->st.st_mode &= ~ALLPERMS;
505 dirnode->inode->st.st_mode |= (specnode->st_mode & ALLPERMS);
506 }
507 /* XXX: ignoring F_NLINK for now */
508 if (specnode->flags & F_SIZE) {
509 ASEPRINT("size", "%lld",
510 (long long)dirnode->inode->st.st_size,
511 (long long)specnode->st_size);
512 dirnode->inode->st.st_size = specnode->st_size;
513 }
514 if (specnode->flags & F_SLINK) {
515 assert(dirnode->symlink != NULL);
516 assert(specnode->slink != NULL);
517 ASEPRINT("symlink", "%s", dirnode->symlink, specnode->slink);
518 free(dirnode->symlink);
519 dirnode->symlink = estrdup(specnode->slink);
520 }
521 if (specnode->flags & F_TIME) {
522 ASEPRINT("time", "%ld",
523 (long)dirnode->inode->st.st_mtime,
524 (long)specnode->st_mtimespec.tv_sec);
525 dirnode->inode->st.st_mtime = specnode->st_mtimespec.tv_sec;
526 dirnode->inode->st.st_atime = specnode->st_mtimespec.tv_sec;
527 dirnode->inode->st.st_ctime = start_time.tv_sec;
528 #if HAVE_STRUCT_STAT_ST_MTIMENSEC
529 dirnode->inode->st.st_mtimensec = specnode->st_mtimespec.tv_nsec;
530 dirnode->inode->st.st_atimensec = specnode->st_mtimespec.tv_nsec;
531 dirnode->inode->st.st_ctimensec = start_time.tv_nsec;
532 #endif
533 }
534 if (specnode->flags & (F_UID | F_UNAME)) {
535 ASEPRINT("uid", "%d",
536 dirnode->inode->st.st_uid, specnode->st_uid);
537 dirnode->inode->st.st_uid = specnode->st_uid;
538 }
539 #if HAVE_STRUCT_STAT_ST_FLAGS
540 if (specnode->flags & F_FLAGS) {
541 ASEPRINT("flags", "%#lX",
542 (unsigned long)dirnode->inode->st.st_flags,
543 (unsigned long)specnode->st_flags);
544 dirnode->inode->st.st_flags = specnode->st_flags;
545 }
546 #endif
547 /* if (specnode->flags & F_DEV) {
548 ASEPRINT("rdev", "%#llx",
549 (unsigned long long)dirnode->inode->st.st_rdev,
550 (unsigned long long)specnode->st_rdev);
551 dirnode->inode->st.st_rdev = specnode->st_rdev;
552 }*/
553 #undef ASEPRINT
554
555 dirnode->flags |= FSNODE_F_HASSPEC;
556 }
557
558
559 /*
560 * dump_fsnodes --
561 * dump the fsnodes from `cur'
562 */
563 void
dump_fsnodes(fsnode * root)564 dump_fsnodes(fsnode *root)
565 {
566 fsnode *cur;
567 char path[MAXPATHLEN + 1];
568
569 printf("dump_fsnodes: %s %p\n", root->path, root);
570 for (cur = root; cur != NULL; cur = cur->next) {
571 if (snprintf(path, sizeof(path), "%s/%s", cur->path,
572 cur->name) >= (int)sizeof(path))
573 errx(1, "Pathname too long.");
574
575 if (debug & DEBUG_DUMP_FSNODES_VERBOSE)
576 printf("cur=%8p parent=%8p first=%8p ",
577 cur, cur->parent, cur->first);
578 printf("%7s: %s", inode_type(cur->type), path);
579 if (S_ISLNK(cur->type)) {
580 assert(cur->symlink != NULL);
581 printf(" -> %s", cur->symlink);
582 } else {
583 assert (cur->symlink == NULL);
584 }
585 if (cur->inode->nlink > 1)
586 printf(", nlinks=%d", cur->inode->nlink);
587 putchar('\n');
588
589 if (cur->child) {
590 assert (cur->type == S_IFDIR);
591 dump_fsnodes(cur->child);
592 }
593 }
594 printf("dump_fsnodes: finished %s/%s\n", root->path, root->name);
595 }
596
597
598 /*
599 * inode_type --
600 * for a given inode type `mode', return a descriptive string.
601 * for most cases, uses inotype() from mtree/misc.c
602 */
603 const char *
inode_type(mode_t mode)604 inode_type(mode_t mode)
605 {
606 if (S_ISREG(mode))
607 return ("file");
608 if (S_ISLNK(mode))
609 return ("symlink");
610 if (S_ISDIR(mode))
611 return ("dir");
612 if (S_ISLNK(mode))
613 return ("link");
614 if (S_ISFIFO(mode))
615 return ("fifo");
616 if (S_ISSOCK(mode))
617 return ("socket");
618 /* XXX should not happen but handle them */
619 if (S_ISCHR(mode))
620 return ("char");
621 if (S_ISBLK(mode))
622 return ("block");
623 return ("unknown");
624 }
625
626
627 /*
628 * link_check --
629 * return pointer to fsinode matching `entry's st_ino & st_dev if it exists,
630 * otherwise add `entry' to table and return NULL
631 */
632 /* This was borrowed from du.c and tweaked to keep an fsnode
633 * pointer instead. -- dbj@netbsd.org
634 */
635 fsinode *
link_check(fsinode * entry)636 link_check(fsinode *entry)
637 {
638 static struct entry {
639 fsinode *data;
640 } *htable;
641 static int htshift; /* log(allocated size) */
642 static int htmask; /* allocated size - 1 */
643 static int htused; /* 2*number of insertions */
644 int h, h2;
645 uint64_t tmp;
646 /* this constant is (1<<64)/((1+sqrt(5))/2)
647 * aka (word size)/(golden ratio)
648 */
649 const uint64_t HTCONST = 11400714819323198485ULL;
650 const int HTBITS = 64;
651
652 /* Never store zero in hashtable */
653 assert(entry);
654
655 /* Extend hash table if necessary, keep load under 0.5 */
656 if (htused<<1 >= htmask) {
657 struct entry *ohtable;
658
659 if (!htable)
660 htshift = 10; /* starting hashtable size */
661 else
662 htshift++; /* exponential hashtable growth */
663
664 htmask = (1 << htshift) - 1;
665 htused = 0;
666
667 ohtable = htable;
668 htable = ecalloc(htmask+1, sizeof(*htable));
669 /* populate newly allocated hashtable */
670 if (ohtable) {
671 int i;
672 for (i = 0; i <= htmask>>1; i++)
673 if (ohtable[i].data)
674 link_check(ohtable[i].data);
675 free(ohtable);
676 }
677 }
678
679 /* multiplicative hashing */
680 tmp = entry->st.st_dev;
681 tmp <<= HTBITS>>1;
682 tmp |= entry->st.st_ino;
683 tmp *= HTCONST;
684 h = tmp >> (HTBITS - htshift);
685 h2 = 1 | ( tmp >> (HTBITS - (htshift<<1) - 1)); /* must be odd */
686
687 /* open address hashtable search with double hash probing */
688 while (htable[h].data) {
689 if ((htable[h].data->st.st_ino == entry->st.st_ino) &&
690 (htable[h].data->st.st_dev == entry->st.st_dev)) {
691 return htable[h].data;
692 }
693 h = (h + h2) & htmask;
694 }
695
696 /* Insert the current entry into hashtable */
697 htable[h].data = entry;
698 htused++;
699 return NULL;
700 }
701