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