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