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