Deleted Added
sdiff udiff text old ( 138441 ) new ( 138455 )
full compact
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 $");
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);
196static int DirPrintWord(void *, void *);
197static int DirPrintDir(void *, void *);
198
199/*-
200 *-----------------------------------------------------------------------
201 * Dir_Init --
202 * initialize things for this module
203 *
204 * Results:
205 * none
206 *
207 * Side Effects:
208 * none
209 *-----------------------------------------------------------------------
210 */
211void
212Dir_Init(void)
213{
214
215 dirSearchPath = Lst_Init(FALSE);
216 openDirectories = Lst_Init(FALSE);
217 Hash_InitTable(&mtimes, 0);
218}
219
220/*-
221 *-----------------------------------------------------------------------
222 * Dir_InitDot --
223 * initialize the "." directory
224 *
225 * Results:
226 * none
227 *
228 * Side Effects:
229 * some directories may be opened.
230 *-----------------------------------------------------------------------
231 */
232void
233Dir_InitDot(void)
234{
235 LstNode ln;
236
237 Dir_AddDir(openDirectories, ".");
238 if ((ln = Lst_Last(openDirectories)) == NULL)
239 err(1, "cannot open current directory");
240 dot = Lst_Datum(ln);
241
242 /*
243 * We always need to have dot around, so we increment its
244 * reference count to make sure it's not destroyed.
245 */
246 dot->refCount += 1;
247}
248
249/*-
250 *-----------------------------------------------------------------------
251 * Dir_End --
252 * cleanup things for this module
253 *
254 * Results:
255 * none
256 *
257 * Side Effects:
258 * none
259 *-----------------------------------------------------------------------
260 */
261void
262Dir_End(void)
263{
264
265 dot->refCount -= 1;
266 Dir_Destroy(dot);
267 Dir_ClearPath(dirSearchPath);
268 Lst_Destroy(dirSearchPath, NOFREE);
269 Dir_ClearPath(openDirectories);
270 Lst_Destroy(openDirectories, NOFREE);
271 Hash_DeleteTable(&mtimes);
272}
273
274/*-
275 *-----------------------------------------------------------------------
276 * DirFindName --
277 * See if the Path structure describes the same directory as the
278 * given one by comparing their names. Called from Dir_AddDir via
279 * Lst_Find when searching the list of open directories.
280 *
281 * Results:
282 * 0 if it is the same. Non-zero otherwise
283 *
284 * Side Effects:
285 * None
286 *-----------------------------------------------------------------------
287 */
288static int
289DirFindName(void *p, void *dname)
290{
291
292 return (strcmp(((Path *)p)->name, dname));
293}
294
295/*-
296 *-----------------------------------------------------------------------
297 * Dir_HasWildcards --
298 * See if the given name has any wildcard characters in it.
299 *
300 * Results:
301 * returns TRUE if the word should be expanded, FALSE otherwise
302 *
303 * Side Effects:
304 * none
305 *-----------------------------------------------------------------------
306 */
307Boolean
308Dir_HasWildcards(char *name)
309{
310 char *cp;
311 int wild = 0, brace = 0, bracket = 0;
312
313 for (cp = name; *cp; cp++) {
314 switch (*cp) {
315 case '{':
316 brace++;
317 wild = 1;
318 break;
319 case '}':
320 brace--;
321 break;
322 case '[':
323 bracket++;
324 wild = 1;
325 break;
326 case ']':
327 bracket--;
328 break;
329 case '?':
330 case '*':
331 wild = 1;
332 break;
333 default:
334 break;
335 }
336 }
337 return (wild && bracket == 0 && brace == 0);
338}
339
340/*-
341 *-----------------------------------------------------------------------
342 * DirMatchFiles --
343 * Given a pattern and a Path structure, see if any files
344 * match the pattern and add their names to the 'expansions' list if
345 * any do. This is incomplete -- it doesn't take care of patterns like
346 * src / *src / *.c properly (just *.c on any of the directories), but it
347 * will do for now.
348 *
349 * Results:
350 * Always returns 0
351 *
352 * Side Effects:
353 * File names are added to the expansions lst. The directory will be
354 * fully hashed when this is done.
355 *-----------------------------------------------------------------------
356 */
357static int
358DirMatchFiles(char *pattern, Path *p, Lst expansions)
359{
360 Hash_Search search; /* Index into the directory's table */
361 Hash_Entry *entry; /* Current entry in the table */
362 Boolean isDot; /* TRUE if the directory being searched is . */
363
364 isDot = (*p->name == '.' && p->name[1] == '\0');
365
366 for (entry = Hash_EnumFirst(&p->files, &search);
367 entry != NULL;
368 entry = Hash_EnumNext(&search)) {
369 /*
370 * See if the file matches the given pattern. Note we follow
371 * the UNIX convention that dot files will only be found if
372 * the pattern begins with a dot (note also that as a side
373 * effect of the hashing scheme, .* won't match . or ..
374 * since they aren't hashed).
375 */
376 if (Str_Match(entry->name, pattern) &&
377 ((entry->name[0] != '.') ||
378 (pattern[0] == '.'))) {
379 Lst_AtEnd(expansions, (isDot ? estrdup(entry->name) :
380 str_concat(p->name, entry->name, STR_ADDSLASH)));
381 }
382 }
383 return (0);
384}
385
386/*-
387 *-----------------------------------------------------------------------
388 * DirExpandCurly --
389 * Expand curly braces like the C shell. Does this recursively.
390 * Note the special case: if after the piece of the curly brace is
391 * done there are no wildcard characters in the result, the result is
392 * placed on the list WITHOUT CHECKING FOR ITS EXISTENCE. The
393 * given arguments are the entire word to expand, the first curly
394 * brace in the word, the search path, and the list to store the
395 * expansions in.
396 *
397 * Results:
398 * None.
399 *
400 * Side Effects:
401 * The given list is filled with the expansions...
402 *
403 *-----------------------------------------------------------------------
404 */
405static void
406DirExpandCurly(char *word, char *brace, Lst path, Lst expansions)
407{
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 */
411 int bracelevel; /* Number of braces we've seen. If we see a right brace
412 * when this is 0, we've hit the end of the clause. */
413 char *file; /* Current expansion */
414 int otherLen; /* The length of the other pieces of the expansion
415 * (chars before and after the clause in 'word') */
416 char *cp2; /* Pointer for checking for wildcards in
417 * expansion before calling Dir_Expand */
418
419 start = brace + 1;
420
421 /*
422 * Find the end of the brace clause first, being wary of nested brace
423 * clauses.
424 */
425 for (end = start, bracelevel = 0; *end != '\0'; end++) {
426 if (*end == '{')
427 bracelevel++;
428 else if ((*end == '}') && (bracelevel-- == 0))
429 break;
430 }
431 if (*end == '\0') {
432 Error("Unterminated {} clause \"%s\"", start);
433 return;
434 } else
435 end++;
436
437 otherLen = brace - word + strlen(end);
438
439 for (cp = start; cp < end; cp++) {
440 /*
441 * Find the end of this piece of the clause.
442 */
443 bracelevel = 0;
444 while (*cp != ',') {
445 if (*cp == '{')
446 bracelevel++;
447 else if ((*cp == '}') && (bracelevel-- <= 0))
448 break;
449 cp++;
450 }
451 /*
452 * Allocate room for the combination and install the
453 * three pieces.
454 */
455 file = emalloc(otherLen + cp - start + 1);
456 if (brace != word)
457 strncpy(file, word, brace - word);
458 if (cp != start)
459 strncpy(&file[brace - word], start, cp - start);
460 strcpy(&file[(brace - word) + (cp - start)], end);
461
462 /*
463 * See if the result has any wildcards in it. If we find one,
464 * call Dir_Expand right away, telling it to place the result
465 * on our list of expansions.
466 */
467 for (cp2 = file; *cp2 != '\0'; cp2++) {
468 switch (*cp2) {
469 case '*':
470 case '?':
471 case '{':
472 case '[':
473 Dir_Expand(file, path, expansions);
474 goto next;
475 default:
476 break;
477 }
478 }
479 if (*cp2 == '\0') {
480 /*
481 * Hit the end w/o finding any wildcards, so stick
482 * the expansion on the end of the list.
483 */
484 Lst_AtEnd(expansions, file);
485 } else {
486 next:
487 free(file);
488 }
489 start = cp + 1;
490 }
491}
492
493
494/*-
495 *-----------------------------------------------------------------------
496 * DirExpandInt --
497 * Internal expand routine. Passes through the directories in the
498 * path one by one, calling DirMatchFiles for each. NOTE: This still
499 * doesn't handle patterns in directories... Works given a word to
500 * expand, a path to look in, and a list to store expansions in.
501 *
502 * Results:
503 * None.
504 *
505 * Side Effects:
506 * Things are added to the expansions list.
507 *
508 *-----------------------------------------------------------------------
509 */
510static void
511DirExpandInt(char *word, Lst path, Lst expansions)
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}