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