1 /* 2 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 3 * Copyright (c) 1988, 1989 by Adam de Boor 4 * Copyright (c) 1989 by Berkeley Softworks 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Adam de Boor. 9 * 10 * %sccs.include.redist.c% 11 */ 12 13 #ifndef lint 14 static char sccsid[] = "@(#)dir.c 5.6 (Berkeley) 12/28/90"; 15 #endif /* not lint */ 16 17 /*- 18 * dir.c -- 19 * Directory searching using wildcards and/or normal names... 20 * Used both for source wildcarding in the Makefile and for finding 21 * implicit sources. 22 * 23 * The interface for this module is: 24 * Dir_Init Initialize the module. 25 * 26 * Dir_HasWildcards Returns TRUE if the name given it needs to 27 * be wildcard-expanded. 28 * 29 * Dir_Expand Given a pattern and a path, return a Lst of names 30 * which match the pattern on the search path. 31 * 32 * Dir_FindFile Searches for a file on a given search path. 33 * If it exists, the entire path is returned. 34 * Otherwise NULL is returned. 35 * 36 * Dir_MTime Return the modification time of a node. The file 37 * is searched for along the default search path. 38 * The path and mtime fields of the node are filled 39 * in. 40 * 41 * Dir_AddDir Add a directory to a search path. 42 * 43 * Dir_MakeFlags Given a search path and a command flag, create 44 * a string with each of the directories in the path 45 * preceded by the command flag and all of them 46 * separated by a space. 47 * 48 * Dir_Destroy Destroy an element of a search path. Frees up all 49 * things that can be freed for the element as long 50 * as the element is no longer referenced by any other 51 * search path. 52 * Dir_ClearPath Resets a search path to the empty list. 53 * 54 * For debugging: 55 * Dir_PrintDirectories Print stats about the directory cache. 56 */ 57 58 #include <stdio.h> 59 #include <sys/types.h> 60 #include <sys/dir.h> 61 #include <sys/stat.h> 62 #include "make.h" 63 #include "hash.h" 64 65 /* 66 * A search path consists of a Lst of Path structures. A Path structure 67 * has in it the name of the directory and a hash table of all the files 68 * in the directory. This is used to cut down on the number of system 69 * calls necessary to find implicit dependents and their like. Since 70 * these searches are made before any actions are taken, we need not 71 * worry about the directory changing due to creation commands. If this 72 * hampers the style of some makefiles, they must be changed. 73 * 74 * A list of all previously-read directories is kept in the 75 * openDirectories Lst. This list is checked first before a directory 76 * is opened. 77 * 78 * The need for the caching of whole directories is brought about by 79 * the multi-level transformation code in suff.c, which tends to search 80 * for far more files than regular make does. In the initial 81 * implementation, the amount of time spent performing "stat" calls was 82 * truly astronomical. The problem with hashing at the start is, 83 * of course, that pmake doesn't then detect changes to these directories 84 * during the course of the make. Three possibilities suggest themselves: 85 * 86 * 1) just use stat to test for a file's existence. As mentioned 87 * above, this is very inefficient due to the number of checks 88 * engendered by the multi-level transformation code. 89 * 2) use readdir() and company to search the directories, keeping 90 * them open between checks. I have tried this and while it 91 * didn't slow down the process too much, it could severely 92 * affect the amount of parallelism available as each directory 93 * open would take another file descriptor out of play for 94 * handling I/O for another job. Given that it is only recently 95 * that UNIX OS's have taken to allowing more than 20 or 32 96 * file descriptors for a process, this doesn't seem acceptable 97 * to me. 98 * 3) record the mtime of the directory in the Path structure and 99 * verify the directory hasn't changed since the contents were 100 * hashed. This will catch the creation or deletion of files, 101 * but not the updating of files. However, since it is the 102 * creation and deletion that is the problem, this could be 103 * a good thing to do. Unfortunately, if the directory (say ".") 104 * were fairly large and changed fairly frequently, the constant 105 * rehashing could seriously degrade performance. It might be 106 * good in such cases to keep track of the number of rehashes 107 * and if the number goes over a (small) limit, resort to using 108 * stat in its place. 109 * 110 * An additional thing to consider is that pmake is used primarily 111 * to create C programs and until recently pcc-based compilers refused 112 * to allow you to specify where the resulting object file should be 113 * placed. This forced all objects to be created in the current 114 * directory. This isn't meant as a full excuse, just an explanation of 115 * some of the reasons for the caching used here. 116 * 117 * One more note: the location of a target's file is only performed 118 * on the downward traversal of the graph and then only for terminal 119 * nodes in the graph. This could be construed as wrong in some cases, 120 * but prevents inadvertent modification of files when the "installed" 121 * directory for a file is provided in the search path. 122 * 123 * Another data structure maintained by this module is an mtime 124 * cache used when the searching of cached directories fails to find 125 * a file. In the past, Dir_FindFile would simply perform an access() 126 * call in such a case to determine if the file could be found using 127 * just the name given. When this hit, however, all that was gained 128 * was the knowledge that the file existed. Given that an access() is 129 * essentially a stat() without the copyout() call, and that the same 130 * filesystem overhead would have to be incurred in Dir_MTime, it made 131 * sense to replace the access() with a stat() and record the mtime 132 * in a cache for when Dir_MTime was actually called. 133 */ 134 135 Lst dirSearchPath; /* main search path */ 136 137 static Lst openDirectories; /* the list of all open directories */ 138 139 /* 140 * Variables for gathering statistics on the efficiency of the hashing 141 * mechanism. 142 */ 143 static int hits, /* Found in directory cache */ 144 misses, /* Sad, but not evil misses */ 145 nearmisses, /* Found under search path */ 146 bigmisses; /* Sought by itself */ 147 148 typedef struct Path { 149 char *name; /* Name of directory */ 150 int refCount; /* Number of paths with this directory */ 151 int hits; /* the number of times a file in this 152 * directory has been found */ 153 Hash_Table files; /* Hash table of files in directory */ 154 } Path; 155 156 static Path *dot; /* contents of current directory */ 157 static Hash_Table mtimes; /* Results of doing a last-resort stat in 158 * Dir_FindFile -- if we have to go to the 159 * system to find the file, we might as well 160 * have its mtime on record. XXX: If this is done 161 * way early, there's a chance other rules will 162 * have already updated the file, in which case 163 * we'll update it again. Generally, there won't 164 * be two rules to update a single file, so this 165 * should be ok, but... */ 166 167 168 /*- 169 *----------------------------------------------------------------------- 170 * Dir_Init -- 171 * initialize things for this module 172 * 173 * Results: 174 * none 175 * 176 * Side Effects: 177 * some directories may be opened. 178 *----------------------------------------------------------------------- 179 */ 180 void 181 Dir_Init () 182 { 183 dirSearchPath = Lst_Init (FALSE); 184 openDirectories = Lst_Init (FALSE); 185 Hash_InitTable(&mtimes, 0); 186 187 /* 188 * Since the Path structure is placed on both openDirectories and 189 * the path we give Dir_AddDir (which in this case is openDirectories), 190 * we need to remove "." from openDirectories and what better time to 191 * do it than when we have to fetch the thing anyway? 192 */ 193 Dir_AddDir (openDirectories, "."); 194 dot = (Path *) Lst_DeQueue (openDirectories); 195 196 /* 197 * We always need to have dot around, so we increment its reference count 198 * to make sure it's not destroyed. 199 */ 200 dot->refCount += 1; 201 } 202 203 /*- 204 *----------------------------------------------------------------------- 205 * DirFindName -- 206 * See if the Path structure describes the same directory as the 207 * given one by comparing their names. Called from Dir_AddDir via 208 * Lst_Find when searching the list of open directories. 209 * 210 * Results: 211 * 0 if it is the same. Non-zero otherwise 212 * 213 * Side Effects: 214 * None 215 *----------------------------------------------------------------------- 216 */ 217 static int 218 DirFindName (p, dname) 219 Path *p; /* Current name */ 220 char *dname; /* Desired name */ 221 { 222 return (strcmp (p->name, dname)); 223 } 224 225 /*- 226 *----------------------------------------------------------------------- 227 * Dir_HasWildcards -- 228 * see if the given name has any wildcard characters in it 229 * 230 * Results: 231 * returns TRUE if the word should be expanded, FALSE otherwise 232 * 233 * Side Effects: 234 * none 235 *----------------------------------------------------------------------- 236 */ 237 Boolean 238 Dir_HasWildcards (name) 239 char *name; /* name to check */ 240 { 241 register char *cp; 242 243 for (cp = name; *cp; cp++) { 244 switch(*cp) { 245 case '{': 246 case '[': 247 case '?': 248 case '*': 249 return (TRUE); 250 } 251 } 252 return (FALSE); 253 } 254 255 /*- 256 *----------------------------------------------------------------------- 257 * DirMatchFiles -- 258 * Given a pattern and a Path structure, see if any files 259 * match the pattern and add their names to the 'expansions' list if 260 * any do. This is incomplete -- it doesn't take care of patterns like 261 * src/*src/*.c properly (just *.c on any of the directories), but it 262 * will do for now. 263 * 264 * Results: 265 * Always returns 0 266 * 267 * Side Effects: 268 * File names are added to the expansions lst. The directory will be 269 * fully hashed when this is done. 270 *----------------------------------------------------------------------- 271 */ 272 static int 273 DirMatchFiles (pattern, p, expansions) 274 char *pattern; /* Pattern to look for */ 275 Path *p; /* Directory to search */ 276 Lst expansions; /* Place to store the results */ 277 { 278 Hash_Search search; /* Index into the directory's table */ 279 Hash_Entry *entry; /* Current entry in the table */ 280 char *f; /* Current entry in the directory */ 281 Boolean isDot; /* TRUE if the directory being searched is . */ 282 283 isDot = (*p->name == '.' && p->name[1] == '\0'); 284 285 for (entry = Hash_EnumFirst(&p->files, &search); 286 entry != (Hash_Entry *)NULL; 287 entry = Hash_EnumNext(&search)) 288 { 289 /* 290 * See if the file matches the given pattern. Note we follow the UNIX 291 * convention that dot files will only be found if the pattern 292 * begins with a dot (note also that as a side effect of the hashing 293 * scheme, .* won't match . or .. since they aren't hashed). 294 */ 295 if (Str_Match(entry->name, pattern) && 296 ((entry->name[0] != '.') || 297 (pattern[0] == '.'))) 298 { 299 (void)Lst_AtEnd(expansions, 300 (isDot ? strdup(entry->name) : 301 str_concat(p->name, entry->name, 302 STR_ADDSLASH))); 303 } 304 } 305 return (0); 306 } 307 308 /*- 309 *----------------------------------------------------------------------- 310 * DirExpandCurly -- 311 * Expand curly braces like the C shell. Does this recursively. 312 * Note the special case: if after the piece of the curly brace is 313 * done there are no wildcard characters in the result, the result is 314 * placed on the list WITHOUT CHECKING FOR ITS EXISTENCE. 315 * 316 * Results: 317 * None. 318 * 319 * Side Effects: 320 * The given list is filled with the expansions... 321 * 322 *----------------------------------------------------------------------- 323 */ 324 static void 325 DirExpandCurly(word, brace, path, expansions) 326 char *word; /* Entire word to expand */ 327 char *brace; /* First curly brace in it */ 328 Lst path; /* Search path to use */ 329 Lst expansions; /* Place to store the expansions */ 330 { 331 char *end; /* Character after the closing brace */ 332 char *cp; /* Current position in brace clause */ 333 char *start; /* Start of current piece of brace clause */ 334 int bracelevel; /* Number of braces we've seen. If we see a 335 * right brace when this is 0, we've hit the 336 * end of the clause. */ 337 char *file; /* Current expansion */ 338 int otherLen; /* The length of the other pieces of the 339 * expansion (chars before and after the 340 * clause in 'word') */ 341 char *cp2; /* Pointer for checking for wildcards in 342 * expansion before calling Dir_Expand */ 343 344 start = brace+1; 345 346 /* 347 * Find the end of the brace clause first, being wary of nested brace 348 * clauses. 349 */ 350 for (end = start, bracelevel = 0; *end != '\0'; end++) { 351 if (*end == '{') { 352 bracelevel++; 353 } else if ((*end == '}') && (bracelevel-- == 0)) { 354 break; 355 } 356 } 357 if (*end == '\0') { 358 Error("Unterminated {} clause \"%s\"", start); 359 return; 360 } else { 361 end++; 362 } 363 otherLen = brace - word + strlen(end); 364 365 for (cp = start; cp < end; cp++) { 366 /* 367 * Find the end of this piece of the clause. 368 */ 369 bracelevel = 0; 370 while (*cp != ',') { 371 if (*cp == '{') { 372 bracelevel++; 373 } else if ((*cp == '}') && (bracelevel-- <= 0)) { 374 break; 375 } 376 cp++; 377 } 378 /* 379 * Allocate room for the combination and install the three pieces. 380 */ 381 file = emalloc(otherLen + cp - start + 1); 382 if (brace != word) { 383 strncpy(file, word, brace-word); 384 } 385 if (cp != start) { 386 strncpy(&file[brace-word], start, cp-start); 387 } 388 strcpy(&file[(brace-word)+(cp-start)], end); 389 390 /* 391 * See if the result has any wildcards in it. If we find one, call 392 * Dir_Expand right away, telling it to place the result on our list 393 * of expansions. 394 */ 395 for (cp2 = file; *cp2 != '\0'; cp2++) { 396 switch(*cp2) { 397 case '*': 398 case '?': 399 case '{': 400 case '[': 401 Dir_Expand(file, path, expansions); 402 goto next; 403 } 404 } 405 if (*cp2 == '\0') { 406 /* 407 * Hit the end w/o finding any wildcards, so stick the expansion 408 * on the end of the list. 409 */ 410 (void)Lst_AtEnd(expansions, file); 411 } else { 412 next: 413 free(file); 414 } 415 start = cp+1; 416 } 417 } 418 419 420 /*- 421 *----------------------------------------------------------------------- 422 * DirExpandInt -- 423 * Internal expand routine. Passes through the directories in the 424 * path one by one, calling DirMatchFiles for each. NOTE: This still 425 * doesn't handle patterns in directories... 426 * 427 * Results: 428 * None. 429 * 430 * Side Effects: 431 * Things are added to the expansions list. 432 * 433 *----------------------------------------------------------------------- 434 */ 435 static void 436 DirExpandInt(word, path, expansions) 437 char *word; /* Word to expand */ 438 Lst path; /* Path on which to look */ 439 Lst expansions; /* Place to store the result */ 440 { 441 LstNode ln; /* Current node */ 442 Path *p; /* Directory in the node */ 443 444 if (Lst_Open(path) == SUCCESS) { 445 while ((ln = Lst_Next(path)) != NILLNODE) { 446 p = (Path *)Lst_Datum(ln); 447 DirMatchFiles(word, p, expansions); 448 } 449 Lst_Close(path); 450 } 451 } 452 453 /*- 454 *----------------------------------------------------------------------- 455 * DirPrintWord -- 456 * Print a word in the list of expansions. Callback for Dir_Expand 457 * when DEBUG(DIR), via Lst_ForEach. 458 * 459 * Results: 460 * === 0 461 * 462 * Side Effects: 463 * The passed word is printed, followed by a space. 464 * 465 *----------------------------------------------------------------------- 466 */ 467 static int 468 DirPrintWord(word) 469 char *word; 470 { 471 printf("%s ", word); 472 473 return(0); 474 } 475 476 /*- 477 *----------------------------------------------------------------------- 478 * Dir_Expand -- 479 * Expand the given word into a list of words by globbing it looking 480 * in the directories on the given search path. 481 * 482 * Results: 483 * A list of words consisting of the files which exist along the search 484 * path matching the given pattern. 485 * 486 * Side Effects: 487 * Directories may be opened. Who knows? 488 *----------------------------------------------------------------------- 489 */ 490 void 491 Dir_Expand (word, path, expansions) 492 char *word; /* the word to expand */ 493 Lst path; /* the list of directories in which to find 494 * the resulting files */ 495 Lst expansions; /* the list on which to place the results */ 496 { 497 char *cp; 498 499 if (DEBUG(DIR)) { 500 printf("expanding \"%s\"...", word); 501 } 502 503 cp = index(word, '{'); 504 if (cp) { 505 DirExpandCurly(word, cp, path, expansions); 506 } else { 507 cp = index(word, '/'); 508 if (cp) { 509 /* 510 * The thing has a directory component -- find the first wildcard 511 * in the string. 512 */ 513 for (cp = word; *cp; cp++) { 514 if (*cp == '?' || *cp == '[' || *cp == '*' || *cp == '{') { 515 break; 516 } 517 } 518 if (*cp == '{') { 519 /* 520 * This one will be fun. 521 */ 522 DirExpandCurly(word, cp, path, expansions); 523 return; 524 } else if (*cp != '\0') { 525 /* 526 * Back up to the start of the component 527 */ 528 char *dirpath; 529 530 while (cp > word && *cp != '/') { 531 cp--; 532 } 533 if (cp != word) { 534 /* 535 * If the glob isn't in the first component, try and find 536 * all the components up to the one with a wildcard. 537 */ 538 *cp = '\0'; 539 dirpath = Dir_FindFile(word, path); 540 *cp = '/'; 541 /* 542 * dirpath is null if can't find the leading component 543 * XXX: Dir_FindFile won't find internal components. 544 * i.e. if the path contains ../Etc/Object and we're 545 * looking for Etc, it won't be found. Ah well. 546 * Probably not important. 547 */ 548 if (dirpath != (char *)NULL) { 549 path = Lst_Init(FALSE); 550 Dir_AddDir(path, dirpath); 551 DirExpandInt(cp+1, path, expansions); 552 Lst_Destroy(path, NOFREE); 553 } 554 } else { 555 /* 556 * Start the search from the local directory 557 */ 558 DirExpandInt(word, path, expansions); 559 } 560 } else { 561 /* 562 * Return the file -- this should never happen. 563 */ 564 DirExpandInt(word, path, expansions); 565 } 566 } else { 567 /* 568 * First the files in dot 569 */ 570 DirMatchFiles(word, dot, expansions); 571 572 /* 573 * Then the files in every other directory on the path. 574 */ 575 DirExpandInt(word, path, expansions); 576 } 577 } 578 if (DEBUG(DIR)) { 579 Lst_ForEach(expansions, DirPrintWord, NULL); 580 putchar('\n'); 581 } 582 } 583 584 /*- 585 *----------------------------------------------------------------------- 586 * Dir_FindFile -- 587 * Find the file with the given name along the given search path. 588 * 589 * Results: 590 * The path to the file or NULL. This path is guaranteed to be in a 591 * different part of memory than name and so may be safely free'd. 592 * 593 * Side Effects: 594 * If the file is found in a directory which is not on the path 595 * already (either 'name' is absolute or it is a relative path 596 * [ dir1/.../dirn/file ] which exists below one of the directories 597 * already on the search path), its directory is added to the end 598 * of the path on the assumption that there will be more files in 599 * that directory later on. Sometimes this is true. Sometimes not. 600 *----------------------------------------------------------------------- 601 */ 602 char * 603 Dir_FindFile (name, path) 604 char *name; /* the file to find */ 605 Lst path; /* the Lst of directories to search */ 606 { 607 register char *p1; /* pointer into p->name */ 608 register char *p2; /* pointer into name */ 609 LstNode ln; /* a list element */ 610 register char *file; /* the current filename to check */ 611 register Path *p; /* current path member */ 612 register char *cp; /* index of first slash, if any */ 613 Boolean hasSlash; /* true if 'name' contains a / */ 614 struct stat stb; /* Buffer for stat, if necessary */ 615 Hash_Entry *entry; /* Entry for mtimes table */ 616 617 /* 618 * Find the final component of the name and note whether it has a 619 * slash in it (the name, I mean) 620 */ 621 cp = rindex (name, '/'); 622 if (cp) { 623 hasSlash = TRUE; 624 cp += 1; 625 } else { 626 hasSlash = FALSE; 627 cp = name; 628 } 629 630 if (DEBUG(DIR)) { 631 printf("Searching for %s...", name); 632 } 633 /* 634 * No matter what, we always look for the file in the current directory 635 * before anywhere else and we *do not* add the ./ to it if it exists. 636 * This is so there are no conflicts between what the user specifies 637 * (fish.c) and what pmake finds (./fish.c). 638 */ 639 if ((!hasSlash || (cp - name == 2 && *name == '.')) && 640 (Hash_FindEntry (&dot->files, cp) != (Hash_Entry *)NULL)) { 641 if (DEBUG(DIR)) { 642 printf("in '.'\n"); 643 } 644 hits += 1; 645 dot->hits += 1; 646 return (strdup (name)); 647 } 648 649 if (Lst_Open (path) == FAILURE) { 650 if (DEBUG(DIR)) { 651 printf("couldn't open path, file not found\n"); 652 } 653 misses += 1; 654 return ((char *) NULL); 655 } 656 657 /* 658 * We look through all the directories on the path seeking one which 659 * contains the final component of the given name and whose final 660 * component(s) match the name's initial component(s). If such a beast 661 * is found, we concatenate the directory name and the final component 662 * and return the resulting string. If we don't find any such thing, 663 * we go on to phase two... 664 */ 665 while ((ln = Lst_Next (path)) != NILLNODE) { 666 p = (Path *) Lst_Datum (ln); 667 if (DEBUG(DIR)) { 668 printf("%s...", p->name); 669 } 670 if (Hash_FindEntry (&p->files, cp) != (Hash_Entry *)NULL) { 671 if (DEBUG(DIR)) { 672 printf("here..."); 673 } 674 if (hasSlash) { 675 /* 676 * If the name had a slash, its initial components and p's 677 * final components must match. This is false if a mismatch 678 * is encountered before all of the initial components 679 * have been checked (p2 > name at the end of the loop), or 680 * we matched only part of one of the components of p 681 * along with all the rest of them (*p1 != '/'). 682 */ 683 p1 = p->name + strlen (p->name) - 1; 684 p2 = cp - 2; 685 while (p2 >= name && *p1 == *p2) { 686 p1 -= 1; p2 -= 1; 687 } 688 if (p2 >= name || (p1 >= p->name && *p1 != '/')) { 689 if (DEBUG(DIR)) { 690 printf("component mismatch -- continuing..."); 691 } 692 continue; 693 } 694 } 695 file = str_concat (p->name, cp, STR_ADDSLASH); 696 if (DEBUG(DIR)) { 697 printf("returning %s\n", file); 698 } 699 Lst_Close (path); 700 p->hits += 1; 701 hits += 1; 702 return (file); 703 } else if (hasSlash) { 704 /* 705 * If the file has a leading path component and that component 706 * exactly matches the entire name of the current search 707 * directory, we assume the file doesn't exist and return NULL. 708 */ 709 for (p1 = p->name, p2 = name; *p1 && *p1 == *p2; p1++, p2++) { 710 continue; 711 } 712 if (*p1 == '\0' && p2 == cp - 1) { 713 if (DEBUG(DIR)) { 714 printf("must be here but isn't -- returing NULL\n"); 715 } 716 Lst_Close (path); 717 return ((char *) NULL); 718 } 719 } 720 } 721 722 /* 723 * We didn't find the file on any existing members of the directory. 724 * If the name doesn't contain a slash, that means it doesn't exist. 725 * If it *does* contain a slash, however, there is still hope: it 726 * could be in a subdirectory of one of the members of the search 727 * path. (eg. /usr/include and sys/types.h. The above search would 728 * fail to turn up types.h in /usr/include, but it *is* in 729 * /usr/include/sys/types.h) If we find such a beast, we assume there 730 * will be more (what else can we assume?) and add all but the last 731 * component of the resulting name onto the search path (at the 732 * end). This phase is only performed if the file is *not* absolute. 733 */ 734 if (!hasSlash) { 735 if (DEBUG(DIR)) { 736 printf("failed.\n"); 737 } 738 misses += 1; 739 return ((char *) NULL); 740 } 741 742 if (*name != '/') { 743 Boolean checkedDot = FALSE; 744 745 if (DEBUG(DIR)) { 746 printf("failed. Trying subdirectories..."); 747 } 748 (void) Lst_Open (path); 749 while ((ln = Lst_Next (path)) != NILLNODE) { 750 p = (Path *) Lst_Datum (ln); 751 if (p != dot) { 752 file = str_concat (p->name, name, STR_ADDSLASH); 753 } else { 754 /* 755 * Checking in dot -- DON'T put a leading ./ on the thing. 756 */ 757 file = strdup(name); 758 checkedDot = TRUE; 759 } 760 if (DEBUG(DIR)) { 761 printf("checking %s...", file); 762 } 763 764 765 if (stat (file, &stb) == 0) { 766 if (DEBUG(DIR)) { 767 printf("got it.\n"); 768 } 769 770 Lst_Close (path); 771 772 /* 773 * We've found another directory to search. We know there's 774 * a slash in 'file' because we put one there. We nuke it after 775 * finding it and call Dir_AddDir to add this new directory 776 * onto the existing search path. Once that's done, we restore 777 * the slash and triumphantly return the file name, knowing 778 * that should a file in this directory every be referenced 779 * again in such a manner, we will find it without having to do 780 * numerous numbers of access calls. Hurrah! 781 */ 782 cp = rindex (file, '/'); 783 *cp = '\0'; 784 Dir_AddDir (path, file); 785 *cp = '/'; 786 787 /* 788 * Save the modification time so if it's needed, we don't have 789 * to fetch it again. 790 */ 791 if (DEBUG(DIR)) { 792 printf("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime), 793 file); 794 } 795 entry = Hash_CreateEntry(&mtimes, (ClientData)file, 796 (Boolean *)NULL); 797 Hash_SetValue(entry, stb.st_mtime); 798 nearmisses += 1; 799 return (file); 800 } else { 801 free (file); 802 } 803 } 804 805 if (DEBUG(DIR)) { 806 printf("failed. "); 807 } 808 Lst_Close (path); 809 810 if (checkedDot) { 811 /* 812 * Already checked by the given name, since . was in the path, 813 * so no point in proceeding... 814 */ 815 if (DEBUG(DIR)) { 816 printf("Checked . already, returning NULL\n"); 817 } 818 return(NULL); 819 } 820 } 821 822 /* 823 * Didn't find it that way, either. Sigh. Phase 3. Add its directory 824 * onto the search path in any case, just in case, then look for the 825 * thing in the hash table. If we find it, grand. We return a new 826 * copy of the name. Otherwise we sadly return a NULL pointer. Sigh. 827 * Note that if the directory holding the file doesn't exist, this will 828 * do an extra search of the final directory on the path. Unless something 829 * weird happens, this search won't succeed and life will be groovy. 830 * 831 * Sigh. We cannot add the directory onto the search path because 832 * of this amusing case: 833 * $(INSTALLDIR)/$(FILE): $(FILE) 834 * 835 * $(FILE) exists in $(INSTALLDIR) but not in the current one. 836 * When searching for $(FILE), we will find it in $(INSTALLDIR) 837 * b/c we added it here. This is not good... 838 */ 839 #ifdef notdef 840 cp[-1] = '\0'; 841 Dir_AddDir (path, name); 842 cp[-1] = '/'; 843 844 bigmisses += 1; 845 ln = Lst_Last (path); 846 if (ln == NILLNODE) { 847 return ((char *) NULL); 848 } else { 849 p = (Path *) Lst_Datum (ln); 850 } 851 852 if (Hash_FindEntry (&p->files, cp) != (Hash_Entry *)NULL) { 853 return (strdup (name)); 854 } else { 855 return ((char *) NULL); 856 } 857 #else /* !notdef */ 858 if (DEBUG(DIR)) { 859 printf("Looking for \"%s\"...", name); 860 } 861 862 bigmisses += 1; 863 entry = Hash_FindEntry(&mtimes, name); 864 if (entry != (Hash_Entry *)NULL) { 865 if (DEBUG(DIR)) { 866 printf("got it (in mtime cache)\n"); 867 } 868 return(strdup(name)); 869 } else if (stat (name, &stb) == 0) { 870 entry = Hash_CreateEntry(&mtimes, name, (Boolean *)NULL); 871 if (DEBUG(DIR)) { 872 printf("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime), 873 name); 874 } 875 Hash_SetValue(entry, stb.st_mtime); 876 return (strdup (name)); 877 } else { 878 if (DEBUG(DIR)) { 879 printf("failed. Returning NULL\n"); 880 } 881 return ((char *)NULL); 882 } 883 #endif /* notdef */ 884 } 885 886 /*- 887 *----------------------------------------------------------------------- 888 * Dir_MTime -- 889 * Find the modification time of the file described by gn along the 890 * search path dirSearchPath. 891 * 892 * Results: 893 * The modification time or 0 if it doesn't exist 894 * 895 * Side Effects: 896 * The modification time is placed in the node's mtime slot. 897 * If the node didn't have a path entry before, and Dir_FindFile 898 * found one for it, the full name is placed in the path slot. 899 *----------------------------------------------------------------------- 900 */ 901 int 902 Dir_MTime (gn) 903 GNode *gn; /* the file whose modification time is 904 * desired */ 905 { 906 char *fullName; /* the full pathname of name */ 907 struct stat stb; /* buffer for finding the mod time */ 908 Hash_Entry *entry; 909 910 if (gn->type & OP_ARCHV) { 911 return Arch_MTime (gn); 912 } else if (gn->path == (char *)NULL) { 913 fullName = Dir_FindFile (gn->name, dirSearchPath); 914 } else { 915 fullName = gn->path; 916 } 917 918 if (fullName == (char *)NULL) { 919 fullName = gn->name; 920 } 921 922 entry = Hash_FindEntry(&mtimes, fullName); 923 if (entry != (Hash_Entry *)NULL) { 924 /* 925 * Only do this once -- the second time folks are checking to 926 * see if the file was actually updated, so we need to actually go 927 * to the file system. 928 */ 929 if (DEBUG(DIR)) { 930 printf("Using cached time %s for %s\n", 931 Targ_FmtTime(Hash_GetValue(entry)), fullName); 932 } 933 stb.st_mtime = (time_t)Hash_GetValue(entry); 934 Hash_DeleteEntry(&mtimes, entry); 935 } else if (stat (fullName, &stb) < 0) { 936 if (gn->type & OP_MEMBER) { 937 return Arch_MemMTime (gn); 938 } else { 939 stb.st_mtime = 0; 940 } 941 } 942 if (fullName && gn->path == (char *)NULL) { 943 gn->path = fullName; 944 } 945 946 gn->mtime = stb.st_mtime; 947 return (gn->mtime); 948 } 949 950 /*- 951 *----------------------------------------------------------------------- 952 * Dir_AddDir -- 953 * Add the given name to the end of the given path. The order of 954 * the arguments is backwards so ParseDoDependency can do a 955 * Lst_ForEach of its list of paths... 956 * 957 * Results: 958 * none 959 * 960 * Side Effects: 961 * A structure is added to the list and the directory is 962 * read and hashed. 963 *----------------------------------------------------------------------- 964 */ 965 void 966 Dir_AddDir (path, name) 967 Lst path; /* the path to which the directory should be 968 * added */ 969 char *name; /* the name of the directory to add */ 970 { 971 LstNode ln; /* node in case Path structure is found */ 972 register Path *p; /* pointer to new Path structure */ 973 DIR *d; /* for reading directory */ 974 register struct direct *dp; /* entry in directory */ 975 Hash_Entry *he; 976 char *fName; 977 978 ln = Lst_Find (openDirectories, (ClientData)name, DirFindName); 979 if (ln != NILLNODE) { 980 p = (Path *)Lst_Datum (ln); 981 if (Lst_Member(path, (ClientData)p) == NILLNODE) { 982 p->refCount += 1; 983 (void)Lst_AtEnd (path, (ClientData)p); 984 } 985 } else { 986 if (DEBUG(DIR)) { 987 printf("Caching %s...", name); 988 fflush(stdout); 989 } 990 991 if ((d = opendir (name)) != (DIR *) NULL) { 992 p = (Path *) emalloc (sizeof (Path)); 993 p->name = strdup (name); 994 p->hits = 0; 995 p->refCount = 1; 996 Hash_InitTable (&p->files, -1); 997 998 /* 999 * Skip the first two entries -- these will *always* be . and .. 1000 */ 1001 (void)readdir(d); 1002 (void)readdir(d); 1003 1004 while ((dp = readdir (d)) != (struct direct *) NULL) { 1005 #ifdef sun 1006 /* 1007 * The sun directory library doesn't check for a 0 inode 1008 * (0-inode slots just take up space), so we have to do 1009 * it ourselves. 1010 */ 1011 if (dp->d_fileno == 0) { 1012 continue; 1013 } 1014 #endif sun 1015 (void)Hash_CreateEntry(&p->files, dp->d_name, (Boolean *)NULL); 1016 } 1017 (void) closedir (d); 1018 (void)Lst_AtEnd (openDirectories, (ClientData)p); 1019 (void)Lst_AtEnd (path, (ClientData)p); 1020 } 1021 if (DEBUG(DIR)) { 1022 printf("done\n"); 1023 } 1024 } 1025 } 1026 1027 /*- 1028 *----------------------------------------------------------------------- 1029 * Dir_CopyDir -- 1030 * Callback function for duplicating a search path via Lst_Duplicate. 1031 * Ups the reference count for the directory. 1032 * 1033 * Results: 1034 * Returns the Path it was given. 1035 * 1036 * Side Effects: 1037 * The refCount of the path is incremented. 1038 * 1039 *----------------------------------------------------------------------- 1040 */ 1041 ClientData 1042 Dir_CopyDir(p) 1043 Path *p; /* Directory descriptor to copy */ 1044 { 1045 p->refCount += 1; 1046 1047 return ((ClientData)p); 1048 } 1049 1050 /*- 1051 *----------------------------------------------------------------------- 1052 * Dir_MakeFlags -- 1053 * Make a string by taking all the directories in the given search 1054 * path and preceding them by the given flag. Used by the suffix 1055 * module to create variables for compilers based on suffix search 1056 * paths. 1057 * 1058 * Results: 1059 * The string mentioned above. Note that there is no space between 1060 * the given flag and each directory. The empty string is returned if 1061 * Things don't go well. 1062 * 1063 * Side Effects: 1064 * None 1065 *----------------------------------------------------------------------- 1066 */ 1067 char * 1068 Dir_MakeFlags (flag, path) 1069 char *flag; /* flag which should precede each directory */ 1070 Lst path; /* list of directories */ 1071 { 1072 char *str; /* the string which will be returned */ 1073 char *tstr; /* the current directory preceded by 'flag' */ 1074 LstNode ln; /* the node of the current directory */ 1075 Path *p; /* the structure describing the current directory */ 1076 1077 str = strdup (""); 1078 1079 if (Lst_Open (path) == SUCCESS) { 1080 while ((ln = Lst_Next (path)) != NILLNODE) { 1081 p = (Path *) Lst_Datum (ln); 1082 tstr = str_concat (flag, p->name, 0); 1083 str = str_concat (str, tstr, STR_ADDSPACE | STR_DOFREE); 1084 } 1085 Lst_Close (path); 1086 } 1087 1088 return (str); 1089 } 1090 1091 /*- 1092 *----------------------------------------------------------------------- 1093 * Dir_Destroy -- 1094 * Nuke a directory descriptor, if possible. Callback procedure 1095 * for the suffixes module when destroying a search path. 1096 * 1097 * Results: 1098 * None. 1099 * 1100 * Side Effects: 1101 * If no other path references this directory (refCount == 0), 1102 * the Path and all its data are freed. 1103 * 1104 *----------------------------------------------------------------------- 1105 */ 1106 void 1107 Dir_Destroy (p) 1108 Path *p; /* The directory descriptor to nuke */ 1109 { 1110 Hash_Search thing1; 1111 Hash_Entry *thing2; 1112 1113 p->refCount -= 1; 1114 1115 if (p->refCount == 0) { 1116 LstNode ln; 1117 1118 ln = Lst_Member (openDirectories, (ClientData)p); 1119 (void) Lst_Remove (openDirectories, ln); 1120 1121 Hash_DeleteTable (&p->files); 1122 free((Address)p->name); 1123 free((Address)p); 1124 } 1125 } 1126 1127 /*- 1128 *----------------------------------------------------------------------- 1129 * Dir_ClearPath -- 1130 * Clear out all elements of the given search path. This is different 1131 * from destroying the list, notice. 1132 * 1133 * Results: 1134 * None. 1135 * 1136 * Side Effects: 1137 * The path is set to the empty list. 1138 * 1139 *----------------------------------------------------------------------- 1140 */ 1141 void 1142 Dir_ClearPath(path) 1143 Lst path; /* Path to clear */ 1144 { 1145 Path *p; 1146 while (!Lst_IsEmpty(path)) { 1147 p = (Path *)Lst_DeQueue(path); 1148 Dir_Destroy(p); 1149 } 1150 } 1151 1152 1153 /*- 1154 *----------------------------------------------------------------------- 1155 * Dir_Concat -- 1156 * Concatenate two paths, adding the second to the end of the first. 1157 * Makes sure to avoid duplicates. 1158 * 1159 * Results: 1160 * None 1161 * 1162 * Side Effects: 1163 * Reference counts for added dirs are upped. 1164 * 1165 *----------------------------------------------------------------------- 1166 */ 1167 void 1168 Dir_Concat(path1, path2) 1169 Lst path1; /* Dest */ 1170 Lst path2; /* Source */ 1171 { 1172 LstNode ln; 1173 Path *p; 1174 1175 for (ln = Lst_First(path2); ln != NILLNODE; ln = Lst_Succ(ln)) { 1176 p = (Path *)Lst_Datum(ln); 1177 if (Lst_Member(path1, (ClientData)p) == NILLNODE) { 1178 p->refCount += 1; 1179 (void)Lst_AtEnd(path1, (ClientData)p); 1180 } 1181 } 1182 } 1183 1184 /********** DEBUG INFO **********/ 1185 Dir_PrintDirectories() 1186 { 1187 LstNode ln; 1188 Path *p; 1189 1190 printf ("#*** Directory Cache:\n"); 1191 printf ("# Stats: %d hits %d misses %d near misses %d losers (%d%%)\n", 1192 hits, misses, nearmisses, bigmisses, 1193 (hits+bigmisses+nearmisses ? 1194 hits * 100 / (hits + bigmisses + nearmisses) : 0)); 1195 printf ("# %-20s referenced\thits\n", "directory"); 1196 if (Lst_Open (openDirectories) == SUCCESS) { 1197 while ((ln = Lst_Next (openDirectories)) != NILLNODE) { 1198 p = (Path *) Lst_Datum (ln); 1199 printf ("# %-20s %10d\t%4d\n", p->name, p->refCount, p->hits); 1200 } 1201 Lst_Close (openDirectories); 1202 } 1203 } 1204 1205 static int DirPrintDir (p) Path *p; { printf ("%s ", p->name); return (0); } 1206 1207 Dir_PrintPath (path) 1208 Lst path; 1209 { 1210 Lst_ForEach (path, DirPrintDir, (ClientData)0); 1211 } 1212