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