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 *);
| 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 *);
|
512{ 513 LstNode ln; /* Current node */ 514 Path *p; /* Directory in the node */ 515 516 if (Lst_Open(path) == SUCCESS) { 517 while ((ln = Lst_Next(path)) != NULL) { 518 p = Lst_Datum(ln); 519 DirMatchFiles(word, p, expansions); 520 } 521 Lst_Close(path); 522 } 523} 524 525/*- 526 *----------------------------------------------------------------------- 527 * DirPrintWord -- 528 * Print a word in the list of expansions. Callback for Dir_Expand 529 * when DEBUG(DIR), via Lst_ForEach. 530 * 531 * Results: 532 * === 0 533 * 534 * Side Effects: 535 * The passed word is printed, followed by a space. 536 * 537 *----------------------------------------------------------------------- 538 */ 539static int 540DirPrintWord(void *word, void *dummy __unused) 541{ 542 543 DEBUGF(DIR, ("%s ", (char *)word)); 544 545 return (0); 546} 547 548/*- 549 *----------------------------------------------------------------------- 550 * Dir_Expand -- 551 * Expand the given word into a list of words by globbing it looking 552 * in the directories on the given search path. 553 * 554 * Results: 555 * A list of words consisting of the files which exist along the search 556 * path matching the given pattern is placed in expansions. 557 * 558 * Side Effects: 559 * Directories may be opened. Who knows? 560 *----------------------------------------------------------------------- 561 */ 562void 563Dir_Expand(char *word, Lst path, Lst expansions) 564{ 565 char *cp; 566 567 DEBUGF(DIR, ("expanding \"%s\"...", word)); 568 569 cp = strchr(word, '{'); 570 if (cp != NULL) 571 DirExpandCurly(word, cp, path, expansions); 572 else { 573 cp = strchr(word, '/'); 574 if (cp != NULL) { 575 /* 576 * The thing has a directory component -- find the 577 * first wildcard in the string. 578 */ 579 for (cp = word; *cp != '\0'; cp++) { 580 if (*cp == '?' || *cp == '[' || 581 *cp == '*' || *cp == '{') { 582 break; 583 } 584 } 585 if (*cp == '{') { 586 /* 587 * This one will be fun. 588 */ 589 DirExpandCurly(word, cp, path, expansions); 590 return; 591 } else if (*cp != '\0') { 592 /* 593 * Back up to the start of the component 594 */ 595 char *dirpath; 596 597 while (cp > word && *cp != '/') 598 cp--; 599 if (cp != word) { 600 char sc; 601 602 /* 603 * If the glob isn't in the first 604 * component, try and find all the 605 * components up to the one with a 606 * wildcard. 607 */ 608 sc = cp[1]; 609 cp[1] = '\0'; 610 dirpath = Dir_FindFile(word, path); 611 cp[1] = sc; 612 /* 613 * dirpath is null if can't find the 614 * leading component 615 * XXX: Dir_FindFile won't find internal 616 * components. i.e. if the path contains 617 * ../Etc/Object and we're looking for 618 * Etc, * it won't be found. Ah well. 619 * Probably not important. 620 */ 621 if (dirpath != NULL) { 622 char *dp = 623 &dirpath[strlen(dirpath) 624 - 1]; 625 626 if (*dp == '/') 627 *dp = '\0'; 628 path = Lst_Init(FALSE); 629 Dir_AddDir(path, dirpath); 630 DirExpandInt(cp + 1, path, 631 expansions); 632 Lst_Destroy(path, NOFREE); 633 } 634 } else { 635 /* 636 * Start the search from the local 637 * directory 638 */ 639 DirExpandInt(word, path, expansions); 640 } 641 } else { 642 /* 643 * Return the file -- this should never happen. 644 */ 645 DirExpandInt(word, path, expansions); 646 } 647 } else { 648 /* 649 * First the files in dot 650 */ 651 DirMatchFiles(word, dot, expansions); 652 653 /* 654 * Then the files in every other directory on the path. 655 */ 656 DirExpandInt(word, path, expansions); 657 } 658 } 659 if (DEBUG(DIR)) { 660 Lst_ForEach(expansions, DirPrintWord, (void *)NULL); 661 DEBUGF(DIR, ("\n")); 662 } 663} 664 665/*- 666 *----------------------------------------------------------------------- 667 * Dir_FindFile -- 668 * Find the file with the given name along the given search path. 669 * 670 * Results: 671 * The path to the file or NULL. This path is guaranteed to be in a 672 * different part of memory than name and so may be safely free'd. 673 * 674 * Side Effects: 675 * If the file is found in a directory which is not on the path 676 * already (either 'name' is absolute or it is a relative path 677 * [ dir1/.../dirn/file ] which exists below one of the directories 678 * already on the search path), its directory is added to the end 679 * of the path on the assumption that there will be more files in 680 * that directory later on. Sometimes this is true. Sometimes not. 681 *----------------------------------------------------------------------- 682 */ 683char * 684Dir_FindFile(char *name, Lst path) 685{ 686 char *p1; /* pointer into p->name */ 687 char *p2; /* pointer into name */ 688 LstNode ln; /* a list element */ 689 char *file; /* the current filename to check */ 690 Path *p; /* current path member */ 691 char *cp; /* final component of the name */ 692 Boolean hasSlash; /* true if 'name' contains a / */ 693 struct stat stb; /* Buffer for stat, if necessary */ 694 Hash_Entry *entry; /* Entry for mtimes table */ 695 696 /* 697 * Find the final component of the name and note whether it has a 698 * slash in it (the name, I mean) 699 */ 700 cp = strrchr(name, '/'); 701 if (cp != NULL) { 702 hasSlash = TRUE; 703 cp += 1; 704 } else { 705 hasSlash = FALSE; 706 cp = name; 707 } 708 709 DEBUGF(DIR, ("Searching for %s...", name)); 710 /* 711 * No matter what, we always look for the file in the current directory 712 * before anywhere else and we *do not* add the ./ to it if it exists. 713 * This is so there are no conflicts between what the user specifies 714 * (fish.c) and what pmake finds (./fish.c). 715 */ 716 if ((!hasSlash || (cp - name == 2 && *name == '.')) && 717 (Hash_FindEntry(&dot->files, cp) != NULL)) { 718 DEBUGF(DIR, ("in '.'\n")); 719 hits += 1; 720 dot->hits += 1; 721 return (estrdup(name)); 722 } 723 724 if (Lst_Open(path) == FAILURE) { 725 DEBUGF(DIR, ("couldn't open path, file not found\n")); 726 misses += 1; 727 return (NULL); 728 } 729 730 /* 731 * We look through all the directories on the path seeking one which 732 * contains the final component of the given name and whose final 733 * component(s) match the name's initial component(s). If such a beast 734 * is found, we concatenate the directory name and the final component 735 * and return the resulting string. If we don't find any such thing, 736 * we go on to phase two... 737 */ 738 while ((ln = Lst_Next(path)) != NULL) { 739 p = Lst_Datum(ln); 740 DEBUGF(DIR, ("%s...", p->name)); 741 if (Hash_FindEntry(&p->files, cp) != NULL) { 742 DEBUGF(DIR, ("here...")); 743 if (hasSlash) { 744 /* 745 * If the name had a slash, its initial 746 * components and p's final components must 747 * match. This is false if a mismatch is 748 * encountered before all of the initial 749 * components have been checked (p2 > name at 750 * the end of the loop), or we matched only 751 * part of one of the components of p 752 * along with all the rest of them (*p1 != '/'). 753 */ 754 p1 = p->name + strlen(p->name) - 1; 755 p2 = cp - 2; 756 while (p2 >= name && p1 >= p->name && 757 *p1 == *p2) { 758 p1 -= 1; p2 -= 1; 759 } 760 if (p2 >= name || (p1 >= p->name && 761 *p1 != '/')) { 762 DEBUGF(DIR, ("component mismatch -- " 763 "continuing...")); 764 continue; 765 } 766 } 767 file = str_concat(p->name, cp, STR_ADDSLASH); 768 DEBUGF(DIR, ("returning %s\n", file)); 769 Lst_Close(path); 770 p->hits += 1; 771 hits += 1; 772 return (file); 773 } else if (hasSlash) { 774 /* 775 * If the file has a leading path component and that 776 * component exactly matches the entire name of the 777 * current search directory, we assume the file 778 * doesn't exist and return NULL. 779 */ 780 for (p1 = p->name, p2 = name; *p1 && *p1 == *p2; 781 p1++, p2++) 782 continue; 783 if (*p1 == '\0' && p2 == cp - 1) { 784 Lst_Close(path); 785 if (*cp == '\0' || ISDOT(cp) || ISDOTDOT(cp)) { 786 DEBUGF(DIR, ("returning %s\n", name)); 787 return (estrdup(name)); 788 } else { 789 DEBUGF(DIR, ("must be here but isn't --" 790 " returning NULL\n")); 791 return (NULL); 792 } 793 } 794 } 795 } 796 797 /* 798 * We didn't find the file on any existing members of the directory. 799 * If the name doesn't contain a slash, that means it doesn't exist. 800 * If it *does* contain a slash, however, there is still hope: it 801 * could be in a subdirectory of one of the members of the search 802 * path. (eg. /usr/include and sys/types.h. The above search would 803 * fail to turn up types.h in /usr/include, but it *is* in 804 * /usr/include/sys/types.h) If we find such a beast, we assume there 805 * will be more (what else can we assume?) and add all but the last 806 * component of the resulting name onto the search path (at the 807 * end). This phase is only performed if the file is *not* absolute. 808 */ 809 if (!hasSlash) { 810 DEBUGF(DIR, ("failed.\n")); 811 misses += 1; 812 return (NULL); 813 } 814 815 if (*name != '/') { 816 Boolean checkedDot = FALSE; 817 818 DEBUGF(DIR, ("failed. Trying subdirectories...")); 819 Lst_Open(path); 820 while ((ln = Lst_Next(path)) != NULL) { 821 p = Lst_Datum(ln); 822 if (p != dot) { 823 file = str_concat(p->name, name, STR_ADDSLASH); 824 } else { 825 /* 826 * Checking in dot -- DON'T put a leading ./ 827 * on the thing. 828 */ 829 file = estrdup(name); 830 checkedDot = TRUE; 831 } 832 DEBUGF(DIR, ("checking %s...", file)); 833 834 if (stat(file, &stb) == 0) { 835 DEBUGF(DIR, ("got it.\n")); 836 837 Lst_Close(path); 838 839 /* 840 * We've found another directory to search. We 841 * know there's a slash in 'file' because we put 842 * one there. We nuke it after finding it and 843 * call Dir_AddDir to add this new directory 844 * onto the existing search path. Once that's 845 * done, we restore the slash and triumphantly 846 * return the file name, knowing that should a 847 * file in this directory every be referenced 848 * again in such a manner, we will find it 849 * without having to do numerous numbers of 850 * access calls. Hurrah! 851 */ 852 cp = strrchr(file, '/'); 853 *cp = '\0'; 854 Dir_AddDir(path, file); 855 *cp = '/'; 856 857 /* 858 * Save the modification time so if 859 * it's needed, we don't have to fetch it again. 860 */ 861 DEBUGF(DIR, ("Caching %s for %s\n", 862 Targ_FmtTime(stb.st_mtime), file)); 863 entry = Hash_CreateEntry(&mtimes, file, 864 (Boolean *)NULL); 865 Hash_SetValue(entry, 866 (void *)(long)stb.st_mtime); 867 nearmisses += 1; 868 return (file); 869 } else { 870 free(file); 871 } 872 } 873 874 DEBUGF(DIR, ("failed. ")); 875 Lst_Close(path); 876 877 if (checkedDot) { 878 /* 879 * Already checked by the given name, since . was in 880 * the path, so no point in proceeding... 881 */ 882 DEBUGF(DIR, ("Checked . already, returning NULL\n")); 883 return (NULL); 884 } 885 } 886 887 /* 888 * Didn't find it that way, either. Sigh. Phase 3. Add its directory 889 * onto the search path in any case, just in case, then look for the 890 * thing in the hash table. If we find it, grand. We return a new 891 * copy of the name. Otherwise we sadly return a NULL pointer. Sigh. 892 * Note that if the directory holding the file doesn't exist, this will 893 * do an extra search of the final directory on the path. Unless 894 * something weird happens, this search won't succeed and life will 895 * be groovy. 896 * 897 * Sigh. We cannot add the directory onto the search path because 898 * of this amusing case: 899 * $(INSTALLDIR)/$(FILE): $(FILE) 900 * 901 * $(FILE) exists in $(INSTALLDIR) but not in the current one. 902 * When searching for $(FILE), we will find it in $(INSTALLDIR) 903 * b/c we added it here. This is not good... 904 */ 905#ifdef notdef 906 cp[-1] = '\0'; 907 Dir_AddDir(path, name); 908 cp[-1] = '/'; 909 910 bigmisses += 1; 911 ln = Lst_Last(path); 912 if (ln == NULL) 913 return (NULL); 914 915 p = Lst_Datum(ln); 916 917 if (Hash_FindEntry(&p->files, cp) != NULL) { 918 return (estrdup(name)); 919 920 return (NULL); 921#else /* !notdef */ 922 DEBUGF(DIR, ("Looking for \"%s\"...", name)); 923 924 bigmisses += 1; 925 entry = Hash_FindEntry(&mtimes, name); 926 if (entry != NULL) { 927 DEBUGF(DIR, ("got it (in mtime cache)\n")); 928 return (estrdup(name)); 929 } else if (stat (name, &stb) == 0) { 930 entry = Hash_CreateEntry(&mtimes, name, (Boolean *)NULL); 931 DEBUGF(DIR, ("Caching %s for %s\n", 932 Targ_FmtTime(stb.st_mtime), name)); 933 Hash_SetValue(entry, (void *)(long)stb.st_mtime); 934 return (estrdup(name)); 935 } else { 936 DEBUGF(DIR, ("failed. Returning NULL\n")); 937 return (NULL); 938 } 939#endif /* notdef */ 940} 941 942/*- 943 *----------------------------------------------------------------------- 944 * Dir_MTime -- 945 * Find the modification time of the file described by gn along the 946 * search path dirSearchPath. 947 * 948 * Results: 949 * The modification time or 0 if it doesn't exist 950 * 951 * Side Effects: 952 * The modification time is placed in the node's mtime slot. 953 * If the node didn't have a path entry before, and Dir_FindFile 954 * found one for it, the full name is placed in the path slot. 955 *----------------------------------------------------------------------- 956 */ 957int 958Dir_MTime(GNode *gn) 959{ 960 char *fullName; /* the full pathname of name */ 961 struct stat stb; /* buffer for finding the mod time */ 962 Hash_Entry *entry; 963 964 if (gn->type & OP_ARCHV) 965 return (Arch_MTime(gn)); 966 967 else if (gn->path == NULL) 968 fullName = Dir_FindFile(gn->name, dirSearchPath); 969 else 970 fullName = gn->path; 971 972 if (fullName == NULL) 973 fullName = estrdup(gn->name); 974 975 entry = Hash_FindEntry(&mtimes, fullName); 976 if (entry != NULL) { 977 /* 978 * Only do this once -- the second time folks are checking to 979 * see if the file was actually updated, so we need to 980 * actually go to the filesystem. 981 */ 982 DEBUGF(DIR, ("Using cached time %s for %s\n", 983 Targ_FmtTime((time_t)(long)Hash_GetValue(entry)), 984 fullName)); 985 stb.st_mtime = (time_t)(long)Hash_GetValue(entry); 986 Hash_DeleteEntry(&mtimes, entry); 987 } else if (stat(fullName, &stb) < 0) { 988 if (gn->type & OP_MEMBER) { 989 if (fullName != gn->path) 990 free(fullName); 991 return (Arch_MemMTime(gn)); 992 } else { 993 stb.st_mtime = 0; 994 } 995 } 996 if (fullName && gn->path == (char *)NULL) 997 gn->path = fullName; 998 999 gn->mtime = stb.st_mtime; 1000 return (gn->mtime); 1001} 1002 1003/*- 1004 *----------------------------------------------------------------------- 1005 * Dir_AddDir -- 1006 * Add the given name to the end of the given path. The order of 1007 * the arguments is backwards so ParseDoDependency can do a 1008 * Lst_ForEach of its list of paths... 1009 * 1010 * Results: 1011 * none 1012 * 1013 * Side Effects: 1014 * A structure is added to the list and the directory is 1015 * read and hashed. 1016 *----------------------------------------------------------------------- 1017 */ 1018void 1019Dir_AddDir(Lst path, char *name) 1020{ 1021 LstNode ln; /* node in case Path structure is found */ 1022 Path *p; /* pointer to new Path structure */ 1023 DIR *d; /* for reading directory */ 1024 struct dirent *dp; /* entry in directory */ 1025 1026 ln = Lst_Find(openDirectories, name, DirFindName); 1027 if (ln != NULL) { 1028 p = Lst_Datum(ln); 1029 if (Lst_Member(path, p) == NULL) { 1030 p->refCount += 1; 1031 Lst_AtEnd(path, p); 1032 } 1033 } else { 1034 DEBUGF(DIR, ("Caching %s...", name)); 1035 1036 if ((d = opendir(name)) != NULL) { 1037 p = emalloc(sizeof(Path)); 1038 p->name = estrdup(name); 1039 p->hits = 0; 1040 p->refCount = 1; 1041 Hash_InitTable(&p->files, -1); 1042 1043 while ((dp = readdir(d)) != NULL) { 1044#if defined(sun) && defined(d_ino) /* d_ino is a sunos4 #define for d_fileno */ 1045 /* 1046 * The sun directory library doesn't check for 1047 * a 0 inode (0-inode slots just take up space), 1048 * so we have to do it ourselves. 1049 */ 1050 if (dp->d_fileno == 0) 1051 continue; 1052#endif /* sun && d_ino */ 1053 1054 /* Skip the '.' and '..' entries by checking 1055 * for them specifically instead of assuming 1056 * readdir() reuturns them in that order when 1057 * first going through a directory. This is 1058 * needed for XFS over NFS filesystems since 1059 * SGI does not guarantee that these are the 1060 * first two entries returned from readdir(). 1061 */ 1062 if (ISDOT(dp->d_name) || ISDOTDOT(dp->d_name)) 1063 continue; 1064 1065 Hash_CreateEntry(&p->files, dp->d_name, 1066 (Boolean *)NULL); 1067 } 1068 closedir(d); 1069 Lst_AtEnd(openDirectories, p); 1070 if (path != openDirectories) 1071 Lst_AtEnd(path, p); 1072 } 1073 DEBUGF(DIR, ("done\n")); 1074 } 1075} 1076 1077/*- 1078 *----------------------------------------------------------------------- 1079 * Dir_CopyDir -- 1080 * Callback function for duplicating a search path via Lst_Duplicate. 1081 * Ups the reference count for the directory. 1082 * 1083 * Results: 1084 * Returns the Path it was given. 1085 * 1086 * Side Effects: 1087 * The refCount of the path is incremented. 1088 * 1089 *----------------------------------------------------------------------- 1090 */ 1091void * 1092Dir_CopyDir(void *p) 1093{ 1094 1095 ((Path *)p)->refCount += 1; 1096 1097 return (p); 1098} 1099 1100/*- 1101 *----------------------------------------------------------------------- 1102 * Dir_MakeFlags -- 1103 * Make a string by taking all the directories in the given search 1104 * path and preceding them by the given flag. Used by the suffix 1105 * module to create variables for compilers based on suffix search 1106 * paths. 1107 * 1108 * Results: 1109 * The string mentioned above. Note that there is no space between 1110 * the given flag and each directory. The empty string is returned if 1111 * Things don't go well. 1112 * 1113 * Side Effects: 1114 * None 1115 *----------------------------------------------------------------------- 1116 */ 1117char * 1118Dir_MakeFlags(char *flag, Lst path) 1119{ 1120 char *str; /* the string which will be returned */ 1121 char *tstr; /* the current directory preceded by 'flag' */ 1122 LstNode ln; /* the node of the current directory */ 1123 Path *p; /* the structure describing the current directory */ 1124 1125 str = estrdup(""); 1126 1127 if (Lst_Open(path) == SUCCESS) { 1128 while ((ln = Lst_Next(path)) != NULL) { 1129 p = Lst_Datum(ln); 1130 tstr = str_concat(flag, p->name, 0); 1131 str = str_concat(str, tstr, STR_ADDSPACE | STR_DOFREE); 1132 } 1133 Lst_Close(path); 1134 } 1135 1136 return (str); 1137} 1138 1139/*- 1140 *----------------------------------------------------------------------- 1141 * Dir_Destroy -- 1142 * Nuke a directory descriptor, if possible. Callback procedure 1143 * for the suffixes module when destroying a search path. 1144 * 1145 * Results: 1146 * None. 1147 * 1148 * Side Effects: 1149 * If no other path references this directory (refCount == 0), 1150 * the Path and all its data are freed. 1151 * 1152 *----------------------------------------------------------------------- 1153 */ 1154void 1155Dir_Destroy(void *pp) 1156{ 1157 Path *p = pp; 1158 1159 p->refCount -= 1; 1160 1161 if (p->refCount == 0) { 1162 LstNode ln; 1163 1164 ln = Lst_Member(openDirectories, p); 1165 Lst_Remove(openDirectories, ln); 1166 1167 Hash_DeleteTable(&p->files); 1168 free(p->name); 1169 free(p); 1170 } 1171} 1172 1173/*- 1174 *----------------------------------------------------------------------- 1175 * Dir_ClearPath -- 1176 * Clear out all elements of the given search path. This is different 1177 * from destroying the list, notice. 1178 * 1179 * Results: 1180 * None. 1181 * 1182 * Side Effects: 1183 * The path is set to the empty list. 1184 * 1185 *----------------------------------------------------------------------- 1186 */ 1187void 1188Dir_ClearPath(Lst path) 1189{ 1190 Path *p; 1191 1192 while (!Lst_IsEmpty(path)) { 1193 p = Lst_DeQueue(path); 1194 Dir_Destroy(p); 1195 } 1196} 1197 1198 1199/*- 1200 *----------------------------------------------------------------------- 1201 * Dir_Concat -- 1202 * Concatenate two paths, adding the second to the end of the first. 1203 * Makes sure to avoid duplicates. 1204 * 1205 * Results: 1206 * None 1207 * 1208 * Side Effects: 1209 * Reference counts for added dirs are upped. 1210 * 1211 *----------------------------------------------------------------------- 1212 */ 1213void 1214Dir_Concat(Lst path1, Lst path2) 1215{ 1216 LstNode ln; 1217 Path *p; 1218 1219 for (ln = Lst_First(path2); ln != NULL; ln = Lst_Succ(ln)) { 1220 p = Lst_Datum(ln); 1221 if (Lst_Member(path1, p) == NULL) { 1222 p->refCount += 1; 1223 Lst_AtEnd(path1, p); 1224 } 1225 } 1226} 1227 1228/********** DEBUG INFO **********/ 1229void 1230Dir_PrintDirectories(void) 1231{ 1232 LstNode ln; 1233 Path *p; 1234 1235 printf("#*** Directory Cache:\n"); 1236 printf("# Stats: %d hits %d misses %d near misses %d losers (%d%%)\n", 1237 hits, misses, nearmisses, bigmisses, 1238 (hits + bigmisses + nearmisses ? 1239 hits * 100 / (hits + bigmisses + nearmisses) : 0)); 1240 printf("# %-20s referenced\thits\n", "directory"); 1241 if (Lst_Open(openDirectories) == SUCCESS) { 1242 while ((ln = Lst_Next(openDirectories)) != NULL) { 1243 p = Lst_Datum(ln); 1244 printf("# %-20s %10d\t%4d\n", p->name, p->refCount, 1245 p->hits); 1246 } 1247 Lst_Close(openDirectories); 1248 } 1249} 1250 1251static int 1252DirPrintDir(void *p, void *dummy __unused) 1253{ 1254 1255 printf("%s ", ((Path *)p)->name); 1256 1257 return (0); 1258} 1259 1260void 1261Dir_PrintPath(Lst path) 1262{ 1263 1264 Lst_ForEach(path, DirPrintDir, (void *)NULL); 1265}
| 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}
|