dir.c revision 1.160 1 /* $NetBSD: dir.c,v 1.160 2020/10/05 20:21:30 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 /*
36 * Copyright (c) 1988, 1989 by Adam de Boor
37 * Copyright (c) 1989 by Berkeley Softworks
38 * All rights reserved.
39 *
40 * This code is derived from software contributed to Berkeley by
41 * Adam de Boor.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 */
71
72 /*-
73 * dir.c --
74 * Directory searching using wildcards and/or normal names...
75 * Used both for source wildcarding in the Makefile and for finding
76 * implicit sources.
77 *
78 * The interface for this module is:
79 * Dir_Init Initialize the module.
80 *
81 * Dir_InitCur Set the cur CachedDir.
82 *
83 * Dir_InitDot Set the dot CachedDir.
84 *
85 * Dir_End Cleanup the module.
86 *
87 * Dir_SetPATH Set ${.PATH} to reflect state of dirSearchPath.
88 *
89 * Dir_HasWildcards
90 * Returns TRUE if the name given it needs to
91 * be wildcard-expanded.
92 *
93 * Dir_Expand Given a pattern and a path, return a Lst of names
94 * which match the pattern on the search path.
95 *
96 * Dir_FindFile Searches for a file on a given search path.
97 * If it exists, the entire path is returned.
98 * Otherwise NULL is returned.
99 *
100 * Dir_FindHereOrAbove
101 * Search for a path in the current directory and
102 * then all the directories above it in turn until
103 * the path is found or we reach the root ("/").
104 *
105 * Dir_MTime Return the modification time of a node. The file
106 * is searched for along the default search path.
107 * The path and mtime fields of the node are filled in.
108 *
109 * Dir_AddDir Add a directory to a search path.
110 *
111 * Dir_MakeFlags Given a search path and a command flag, create
112 * a string with each of the directories in the path
113 * preceded by the command flag and all of them
114 * separated by a space.
115 *
116 * Dir_Destroy Destroy an element of a search path. Frees up all
117 * things that can be freed for the element as long
118 * as the element is no longer referenced by any other
119 * search path.
120 *
121 * Dir_ClearPath Resets a search path to the empty list.
122 *
123 * For debugging:
124 * Dir_PrintDirectories Print stats about the directory cache.
125 */
126
127 #include <sys/types.h>
128 #include <sys/stat.h>
129
130 #include <dirent.h>
131 #include <errno.h>
132
133 #include "make.h"
134 #include "dir.h"
135 #include "job.h"
136
137 /* "@(#)dir.c 8.2 (Berkeley) 1/2/94" */
138 MAKE_RCSID("$NetBSD: dir.c,v 1.160 2020/10/05 20:21:30 rillig Exp $");
139
140 #define DIR_DEBUG0(text) DEBUG0(DIR, text)
141 #define DIR_DEBUG1(fmt, arg1) DEBUG1(DIR, fmt, arg1)
142 #define DIR_DEBUG2(fmt, arg1, arg2) DEBUG2(DIR, fmt, arg1, arg2)
143
144 /*
145 * A search path consists of a list of CachedDir structures. A CachedDir
146 * has in it the name of the directory and a hash table of all the files
147 * in the directory. This is used to cut down on the number of system
148 * calls necessary to find implicit dependents and their like. Since
149 * these searches are made before any actions are taken, we need not
150 * worry about the directory changing due to creation commands. If this
151 * hampers the style of some makefiles, they must be changed.
152 *
153 * A list of all previously-read directories is kept in the
154 * openDirectories Lst. This list is checked first before a directory
155 * is opened.
156 *
157 * The need for the caching of whole directories is brought about by
158 * the multi-level transformation code in suff.c, which tends to search
159 * for far more files than regular make does. In the initial
160 * implementation, the amount of time spent performing "stat" calls was
161 * truly astronomical. The problem with hashing at the start is,
162 * of course, that pmake doesn't then detect changes to these directories
163 * during the course of the make. Three possibilities suggest themselves:
164 *
165 * 1) just use stat to test for a file's existence. As mentioned
166 * above, this is very inefficient due to the number of checks
167 * engendered by the multi-level transformation code.
168 * 2) use readdir() and company to search the directories, keeping
169 * them open between checks. I have tried this and while it
170 * didn't slow down the process too much, it could severely
171 * affect the amount of parallelism available as each directory
172 * open would take another file descriptor out of play for
173 * handling I/O for another job. Given that it is only recently
174 * that UNIX OS's have taken to allowing more than 20 or 32
175 * file descriptors for a process, this doesn't seem acceptable
176 * to me.
177 * 3) record the mtime of the directory in the CachedDir structure and
178 * verify the directory hasn't changed since the contents were
179 * hashed. This will catch the creation or deletion of files,
180 * but not the updating of files. However, since it is the
181 * creation and deletion that is the problem, this could be
182 * a good thing to do. Unfortunately, if the directory (say ".")
183 * were fairly large and changed fairly frequently, the constant
184 * rehashing could seriously degrade performance. It might be
185 * good in such cases to keep track of the number of rehashes
186 * and if the number goes over a (small) limit, resort to using
187 * stat in its place.
188 *
189 * An additional thing to consider is that pmake is used primarily
190 * to create C programs and until recently pcc-based compilers refused
191 * to allow you to specify where the resulting object file should be
192 * placed. This forced all objects to be created in the current
193 * directory. This isn't meant as a full excuse, just an explanation of
194 * some of the reasons for the caching used here.
195 *
196 * One more note: the location of a target's file is only performed
197 * on the downward traversal of the graph and then only for terminal
198 * nodes in the graph. This could be construed as wrong in some cases,
199 * but prevents inadvertent modification of files when the "installed"
200 * directory for a file is provided in the search path.
201 *
202 * Another data structure maintained by this module is an mtime
203 * cache used when the searching of cached directories fails to find
204 * a file. In the past, Dir_FindFile would simply perform an access()
205 * call in such a case to determine if the file could be found using
206 * just the name given. When this hit, however, all that was gained
207 * was the knowledge that the file existed. Given that an access() is
208 * essentially a stat() without the copyout() call, and that the same
209 * filesystem overhead would have to be incurred in Dir_MTime, it made
210 * sense to replace the access() with a stat() and record the mtime
211 * in a cache for when Dir_MTime was actually called.
212 */
213
214 typedef List CachedDirList;
215 typedef ListNode CachedDirListNode;
216
217 typedef ListNode SearchPathNode;
218
219 SearchPath *dirSearchPath; /* main search path */
220
221 /* A list of cached directories, with fast lookup by directory name. */
222 typedef struct OpenDirs {
223 CachedDirList *list;
224 Hash_Table /* of CachedDirListNode */ table;
225 } OpenDirs;
226
227 static void
228 OpenDirs_Init(OpenDirs *odirs)
229 {
230 odirs->list = Lst_Init();
231 Hash_InitTable(&odirs->table);
232 }
233
234 static void MAKE_ATTR_UNUSED
235 OpenDirs_Done(OpenDirs *odirs)
236 {
237 CachedDirListNode *ln = odirs->list->first;
238 while (ln != NULL) {
239 CachedDirListNode *next = ln->next;
240 CachedDir *dir = ln->datum;
241 Dir_Destroy(dir); /* removes the dir from odirs->list */
242 ln = next;
243 }
244 Lst_Free(odirs->list);
245 Hash_DeleteTable(&odirs->table);
246 }
247
248 static CachedDir *
249 OpenDirs_Find(OpenDirs *odirs, const char *name)
250 {
251 CachedDirListNode *ln = Hash_FindValue(&odirs->table, name);
252 return ln != NULL ? ln->datum : NULL;
253 }
254
255 static void
256 OpenDirs_Add(OpenDirs *odirs, CachedDir *cdir)
257 {
258 Hash_Entry *he = Hash_FindEntry(&odirs->table, cdir->name);
259 if (he != NULL)
260 return;
261 he = Hash_CreateEntry(&odirs->table, cdir->name, NULL);
262 Lst_Append(odirs->list, cdir);
263 Hash_SetValue(he, odirs->list->last);
264 }
265
266 static void
267 OpenDirs_Remove(OpenDirs *odirs, const char *name)
268 {
269 Hash_Entry *he = Hash_FindEntry(&odirs->table, name);
270 CachedDirListNode *ln;
271 if (he == NULL)
272 return;
273 ln = Hash_GetValue(he);
274 Hash_DeleteEntry(&odirs->table, he);
275 Lst_Remove(odirs->list, ln);
276 }
277
278 static OpenDirs openDirs; /* the list of all open directories */
279
280 /*
281 * Variables for gathering statistics on the efficiency of the hashing
282 * mechanism.
283 */
284 static int hits; /* Found in directory cache */
285 static int misses; /* Sad, but not evil misses */
286 static int nearmisses; /* Found under search path */
287 static int bigmisses; /* Sought by itself */
288
289 static CachedDir *dot; /* contents of current directory */
290 static CachedDir *cur; /* contents of current directory, if not dot */
291 static CachedDir *dotLast; /* a fake path entry indicating we need to
292 * look for . last */
293
294 /* Results of doing a last-resort stat in Dir_FindFile -- if we have to go to
295 * the system to find the file, we might as well have its mtime on record.
296 *
297 * XXX: If this is done way early, there's a chance other rules will have
298 * already updated the file, in which case we'll update it again. Generally,
299 * there won't be two rules to update a single file, so this should be ok,
300 * but... */
301 static Hash_Table mtimes;
302
303 static Hash_Table lmtimes; /* same as mtimes but for lstat */
304
305 /*
306 * We use stat(2) a lot, cache the results.
307 * mtime and mode are all we care about.
308 */
309 struct cache_st {
310 time_t lmtime; /* lstat */
311 time_t mtime; /* stat */
312 mode_t mode;
313 };
314
315 /* minimize changes below */
316 typedef enum {
317 CST_LSTAT = 0x01, /* call lstat(2) instead of stat(2) */
318 CST_UPDATE = 0x02 /* ignore existing cached entry */
319 } CachedStatsFlags;
320
321 /* Returns 0 and the result of stat(2) or lstat(2) in *mst, or -1 on error. */
322 static int
323 cached_stats(Hash_Table *htp, const char *pathname, struct make_stat *mst,
324 CachedStatsFlags flags)
325 {
326 Hash_Entry *entry;
327 struct stat sys_st;
328 struct cache_st *cst;
329 int rc;
330
331 if (!pathname || !pathname[0])
332 return -1;
333
334 entry = Hash_FindEntry(htp, pathname);
335
336 if (entry && !(flags & CST_UPDATE)) {
337 cst = Hash_GetValue(entry);
338
339 mst->mst_mode = cst->mode;
340 mst->mst_mtime = (flags & CST_LSTAT) ? cst->lmtime : cst->mtime;
341 if (mst->mst_mtime) {
342 DIR_DEBUG2("Using cached time %s for %s\n",
343 Targ_FmtTime(mst->mst_mtime), pathname);
344 return 0;
345 }
346 }
347
348 rc = (flags & CST_LSTAT)
349 ? lstat(pathname, &sys_st)
350 : stat(pathname, &sys_st);
351 if (rc == -1)
352 return -1;
353
354 if (sys_st.st_mtime == 0)
355 sys_st.st_mtime = 1; /* avoid confusion with missing file */
356
357 mst->mst_mode = sys_st.st_mode;
358 mst->mst_mtime = sys_st.st_mtime;
359
360 if (entry == NULL)
361 entry = Hash_CreateEntry(htp, pathname, NULL);
362 if (Hash_GetValue(entry) == NULL) {
363 Hash_SetValue(entry, bmake_malloc(sizeof(*cst)));
364 memset(Hash_GetValue(entry), 0, sizeof(*cst));
365 }
366 cst = Hash_GetValue(entry);
367 if (flags & CST_LSTAT) {
368 cst->lmtime = sys_st.st_mtime;
369 } else {
370 cst->mtime = sys_st.st_mtime;
371 }
372 cst->mode = sys_st.st_mode;
373 DIR_DEBUG2(" Caching %s for %s\n",
374 Targ_FmtTime(sys_st.st_mtime), pathname);
375
376 return 0;
377 }
378
379 int
380 cached_stat(const char *pathname, struct make_stat *st)
381 {
382 return cached_stats(&mtimes, pathname, st, 0);
383 }
384
385 int
386 cached_lstat(const char *pathname, struct make_stat *st)
387 {
388 return cached_stats(&lmtimes, pathname, st, CST_LSTAT);
389 }
390
391 /* Initialize things for this module. */
392 void
393 Dir_Init(void)
394 {
395 dirSearchPath = Lst_Init();
396 OpenDirs_Init(&openDirs);
397 Hash_InitTable(&mtimes);
398 Hash_InitTable(&lmtimes);
399 }
400
401 void
402 Dir_InitDir(const char *cdname)
403 {
404 Dir_InitCur(cdname);
405
406 dotLast = bmake_malloc(sizeof(CachedDir));
407 dotLast->refCount = 1;
408 dotLast->hits = 0;
409 dotLast->name = bmake_strdup(".DOTLAST");
410 Hash_InitTable(&dotLast->files);
411 }
412
413 /*
414 * Called by Dir_InitDir and whenever .CURDIR is assigned to.
415 */
416 void
417 Dir_InitCur(const char *cdname)
418 {
419 CachedDir *dir;
420
421 if (cdname != NULL) {
422 /*
423 * Our build directory is not the same as our source directory.
424 * Keep this one around too.
425 */
426 if ((dir = Dir_AddDir(NULL, cdname))) {
427 dir->refCount++;
428 if (cur && cur != dir) {
429 /*
430 * We've been here before, cleanup.
431 */
432 cur->refCount--;
433 Dir_Destroy(cur);
434 }
435 cur = dir;
436 }
437 }
438 }
439
440 /* (Re)initialize "dot" (current/object directory) path hash.
441 * Some directories may be opened. */
442 void
443 Dir_InitDot(void)
444 {
445 if (dot != NULL) {
446 /* Remove old entry from openDirs, but do not destroy. */
447 OpenDirs_Remove(&openDirs, dot->name);
448 }
449
450 dot = Dir_AddDir(NULL, ".");
451
452 if (dot == NULL) {
453 Error("Cannot open `.' (%s)", strerror(errno));
454 exit(1);
455 }
456
457 /*
458 * We always need to have dot around, so we increment its reference count
459 * to make sure it's not destroyed.
460 */
461 dot->refCount++;
462 Dir_SetPATH(); /* initialize */
463 }
464
465 /* Clean up things for this module. */
466 void
467 Dir_End(void)
468 {
469 #ifdef CLEANUP
470 if (cur) {
471 cur->refCount--;
472 Dir_Destroy(cur);
473 }
474 dot->refCount--;
475 dotLast->refCount--;
476 Dir_Destroy(dotLast);
477 Dir_Destroy(dot);
478 Dir_ClearPath(dirSearchPath);
479 Lst_Free(dirSearchPath);
480 OpenDirs_Done(&openDirs);
481 Hash_DeleteTable(&mtimes);
482 #endif
483 }
484
485 /*
486 * We want ${.PATH} to indicate the order in which we will actually
487 * search, so we rebuild it after any .PATH: target.
488 * This is the simplest way to deal with the effect of .DOTLAST.
489 */
490 void
491 Dir_SetPATH(void)
492 {
493 CachedDirListNode *ln;
494 Boolean hasLastDot = FALSE; /* true if we should search dot last */
495
496 Var_Delete(".PATH", VAR_GLOBAL);
497
498 Lst_Open(dirSearchPath);
499 if ((ln = Lst_First(dirSearchPath)) != NULL) {
500 CachedDir *dir = LstNode_Datum(ln);
501 if (dir == dotLast) {
502 hasLastDot = TRUE;
503 Var_Append(".PATH", dotLast->name, VAR_GLOBAL);
504 }
505 }
506
507 if (!hasLastDot) {
508 if (dot)
509 Var_Append(".PATH", dot->name, VAR_GLOBAL);
510 if (cur)
511 Var_Append(".PATH", cur->name, VAR_GLOBAL);
512 }
513
514 while ((ln = Lst_Next(dirSearchPath)) != NULL) {
515 CachedDir *dir = LstNode_Datum(ln);
516 if (dir == dotLast)
517 continue;
518 if (dir == dot && hasLastDot)
519 continue;
520 Var_Append(".PATH", dir->name, VAR_GLOBAL);
521 }
522
523 if (hasLastDot) {
524 if (dot)
525 Var_Append(".PATH", dot->name, VAR_GLOBAL);
526 if (cur)
527 Var_Append(".PATH", cur->name, VAR_GLOBAL);
528 }
529 Lst_Close(dirSearchPath);
530 }
531
532 /* See if the CachedDir structure describes the same directory as the
533 * given one by comparing their names. Called from Dir_AddDir via
534 * Lst_Find when searching the list of open directories. */
535 static Boolean
536 DirFindName(const void *p, const void *desiredName)
537 {
538 const CachedDir *dir = p;
539 return strcmp(dir->name, desiredName) == 0;
540 }
541
542 /* See if the given name has any wildcard characters in it. Be careful not to
543 * expand unmatching brackets or braces.
544 *
545 * XXX: This code is not 100% correct ([^]] fails etc.). I really don't think
546 * that make(1) should be expanding patterns, because then you have to set a
547 * mechanism for escaping the expansion!
548 *
549 * Input:
550 * name name to check
551 *
552 * Results:
553 * returns TRUE if the word should be expanded, FALSE otherwise
554 */
555 Boolean
556 Dir_HasWildcards(const char *name)
557 {
558 const char *cp;
559 Boolean wild = FALSE;
560 int braces = 0, brackets = 0;
561
562 for (cp = name; *cp; cp++) {
563 switch (*cp) {
564 case '{':
565 braces++;
566 wild = TRUE;
567 break;
568 case '}':
569 braces--;
570 break;
571 case '[':
572 brackets++;
573 wild = TRUE;
574 break;
575 case ']':
576 brackets--;
577 break;
578 case '?':
579 case '*':
580 wild = TRUE;
581 break;
582 default:
583 break;
584 }
585 }
586 return wild && brackets == 0 && braces == 0;
587 }
588
589 /*-
590 *-----------------------------------------------------------------------
591 * DirMatchFiles --
592 * Given a pattern and a CachedDir structure, see if any files
593 * match the pattern and add their names to the 'expansions' list if
594 * any do. This is incomplete -- it doesn't take care of patterns like
595 * src / *src / *.c properly (just *.c on any of the directories), but it
596 * will do for now.
597 *
598 * Input:
599 * pattern Pattern to look for
600 * dir Directory to search
601 * expansion Place to store the results
602 *
603 * Side Effects:
604 * File names are added to the expansions lst. The directory will be
605 * fully hashed when this is done.
606 *-----------------------------------------------------------------------
607 */
608 static void
609 DirMatchFiles(const char *pattern, CachedDir *dir, StringList *expansions)
610 {
611 Hash_Search search; /* Index into the directory's table */
612 Hash_Entry *entry; /* Current entry in the table */
613 Boolean isDot; /* TRUE if the directory being searched is . */
614
615 isDot = (dir->name[0] == '.' && dir->name[1] == '\0');
616
617 for (entry = Hash_EnumFirst(&dir->files, &search);
618 entry != NULL;
619 entry = Hash_EnumNext(&search))
620 {
621 /*
622 * See if the file matches the given pattern. Note we follow the UNIX
623 * convention that dot files will only be found if the pattern
624 * begins with a dot (note also that as a side effect of the hashing
625 * scheme, .* won't match . or .. since they aren't hashed).
626 */
627 if (Str_Match(entry->name, pattern) &&
628 ((entry->name[0] != '.') ||
629 (pattern[0] == '.')))
630 {
631 Lst_Append(expansions,
632 (isDot ? bmake_strdup(entry->name) :
633 str_concat3(dir->name, "/", entry->name)));
634 }
635 }
636 }
637
638 /* Find the next closing brace in the string, taking nested braces into
639 * account. */
640 static const char *
641 closing_brace(const char *p)
642 {
643 int nest = 0;
644 while (*p != '\0') {
645 if (*p == '}' && nest == 0)
646 break;
647 if (*p == '{')
648 nest++;
649 if (*p == '}')
650 nest--;
651 p++;
652 }
653 return p;
654 }
655
656 /* Find the next closing brace or comma in the string, taking nested braces
657 * into account. */
658 static const char *
659 separator_comma(const char *p)
660 {
661 int nest = 0;
662 while (*p != '\0') {
663 if ((*p == '}' || *p == ',') && nest == 0)
664 break;
665 if (*p == '{')
666 nest++;
667 if (*p == '}')
668 nest--;
669 p++;
670 }
671 return p;
672 }
673
674 static Boolean
675 contains_wildcard(const char *p)
676 {
677 for (; *p != '\0'; p++) {
678 switch (*p) {
679 case '*':
680 case '?':
681 case '{':
682 case '[':
683 return TRUE;
684 }
685 }
686 return FALSE;
687 }
688
689 static char *
690 concat3(const char *a, size_t a_len, const char *b, size_t b_len,
691 const char *c, size_t c_len)
692 {
693 size_t s_len = a_len + b_len + c_len;
694 char *s = bmake_malloc(s_len + 1);
695 memcpy(s, a, a_len);
696 memcpy(s + a_len, b, b_len);
697 memcpy(s + a_len + b_len, c, c_len);
698 s[s_len] = '\0';
699 return s;
700 }
701
702 /*-
703 *-----------------------------------------------------------------------
704 * DirExpandCurly --
705 * Expand curly braces like the C shell. Does this recursively.
706 * Note the special case: if after the piece of the curly brace is
707 * done there are no wildcard characters in the result, the result is
708 * placed on the list WITHOUT CHECKING FOR ITS EXISTENCE.
709 *
710 * Input:
711 * word Entire word to expand
712 * brace First curly brace in it
713 * path Search path to use
714 * expansions Place to store the expansions
715 *
716 * Results:
717 * None.
718 *
719 * Side Effects:
720 * The given list is filled with the expansions...
721 *
722 *-----------------------------------------------------------------------
723 */
724 static void
725 DirExpandCurly(const char *word, const char *brace, SearchPath *path,
726 StringList *expansions)
727 {
728 const char *prefix, *middle, *piece, *middle_end, *suffix;
729 size_t prefix_len, suffix_len;
730
731 /* Split the word into prefix '{' middle '}' suffix. */
732
733 middle = brace + 1;
734 middle_end = closing_brace(middle);
735 if (*middle_end == '\0') {
736 Error("Unterminated {} clause \"%s\"", middle);
737 return;
738 }
739
740 prefix = word;
741 prefix_len = (size_t)(brace - prefix);
742 suffix = middle_end + 1;
743 suffix_len = strlen(suffix);
744
745 /* Split the middle into pieces, separated by commas. */
746
747 piece = middle;
748 while (piece < middle_end + 1) {
749 const char *piece_end = separator_comma(piece);
750 size_t piece_len = (size_t)(piece_end - piece);
751
752 char *file = concat3(prefix, prefix_len, piece, piece_len,
753 suffix, suffix_len);
754
755 if (contains_wildcard(file)) {
756 Dir_Expand(file, path, expansions);
757 free(file);
758 } else {
759 Lst_Append(expansions, file);
760 }
761
762 piece = piece_end + 1; /* skip over the comma or closing brace */
763 }
764 }
765
766
767 /*-
768 *-----------------------------------------------------------------------
769 * DirExpandInt --
770 * Internal expand routine. Passes through the directories in the
771 * path one by one, calling DirMatchFiles for each. NOTE: This still
772 * doesn't handle patterns in directories...
773 *
774 * Input:
775 * word Word to expand
776 * path Directory in which to look
777 * expansions Place to store the result
778 *
779 * Results:
780 * None.
781 *
782 * Side Effects:
783 * Things are added to the expansions list.
784 *
785 *-----------------------------------------------------------------------
786 */
787 static void
788 DirExpandInt(const char *word, SearchPath *path, StringList *expansions)
789 {
790 SearchPathNode *ln;
791 for (ln = path->first; ln != NULL; ln = ln->next) {
792 CachedDir *dir = ln->datum;
793 DirMatchFiles(word, dir, expansions);
794 }
795 }
796
797 static void
798 DirPrintExpansions(StringList *words)
799 {
800 StringListNode *ln;
801 for (ln = words->first; ln != NULL; ln = ln->next) {
802 const char *word = ln->datum;
803 debug_printf("%s ", word);
804 }
805 debug_printf("\n");
806 }
807
808 /*-
809 *-----------------------------------------------------------------------
810 * Dir_Expand --
811 * Expand the given word into a list of words by globbing it looking
812 * in the directories on the given search path.
813 *
814 * Input:
815 * word the word to expand
816 * path the list of directories in which to find the
817 * resulting files
818 * expansions the list on which to place the results
819 *
820 * Results:
821 * A list of words consisting of the files which exist along the search
822 * path matching the given pattern.
823 *
824 * Side Effects:
825 * Directories may be opened. Who knows?
826 * Undefined behavior if the word is really in read-only memory.
827 *-----------------------------------------------------------------------
828 */
829 void
830 Dir_Expand(const char *word, SearchPath *path, StringList *expansions)
831 {
832 const char *cp;
833
834 assert(path != NULL);
835 assert(expansions != NULL);
836
837 DIR_DEBUG1("Expanding \"%s\"... ", word);
838
839 cp = strchr(word, '{');
840 if (cp) {
841 DirExpandCurly(word, cp, path, expansions);
842 } else {
843 cp = strchr(word, '/');
844 if (cp) {
845 /*
846 * The thing has a directory component -- find the first wildcard
847 * in the string.
848 */
849 for (cp = word; *cp; cp++) {
850 if (*cp == '?' || *cp == '[' || *cp == '*' || *cp == '{') {
851 break;
852 }
853 }
854 if (*cp == '{') {
855 /*
856 * This one will be fun.
857 */
858 DirExpandCurly(word, cp, path, expansions);
859 return;
860 } else if (*cp != '\0') {
861 /*
862 * Back up to the start of the component
863 */
864 while (cp > word && *cp != '/') {
865 cp--;
866 }
867 if (cp != word) {
868 char sc;
869 char *dirpath;
870 /*
871 * If the glob isn't in the first component, try and find
872 * all the components up to the one with a wildcard.
873 */
874 sc = cp[1];
875 ((char *)UNCONST(cp))[1] = '\0';
876 dirpath = Dir_FindFile(word, path);
877 ((char *)UNCONST(cp))[1] = sc;
878 /*
879 * dirpath is null if can't find the leading component
880 * XXX: Dir_FindFile won't find internal components.
881 * i.e. if the path contains ../Etc/Object and we're
882 * looking for Etc, it won't be found. Ah well.
883 * Probably not important.
884 */
885 if (dirpath != NULL) {
886 char *dp = &dirpath[strlen(dirpath) - 1];
887 if (*dp == '/')
888 *dp = '\0';
889 path = Lst_Init();
890 (void)Dir_AddDir(path, dirpath);
891 DirExpandInt(cp + 1, path, expansions);
892 Lst_Free(path);
893 }
894 } else {
895 /*
896 * Start the search from the local directory
897 */
898 DirExpandInt(word, path, expansions);
899 }
900 } else {
901 /*
902 * Return the file -- this should never happen.
903 */
904 DirExpandInt(word, path, expansions);
905 }
906 } else {
907 /*
908 * First the files in dot
909 */
910 DirMatchFiles(word, dot, expansions);
911
912 /*
913 * Then the files in every other directory on the path.
914 */
915 DirExpandInt(word, path, expansions);
916 }
917 }
918 if (DEBUG(DIR))
919 DirPrintExpansions(expansions);
920 }
921
922 /*-
923 *-----------------------------------------------------------------------
924 * DirLookup --
925 * Find if the file with the given name exists in the given path.
926 *
927 * Results:
928 * The path to the file or NULL. This path is guaranteed to be in a
929 * different part of memory than name and so may be safely free'd.
930 *
931 * Side Effects:
932 * None.
933 *-----------------------------------------------------------------------
934 */
935 static char *
936 DirLookup(CachedDir *dir, const char *name MAKE_ATTR_UNUSED, const char *cp,
937 Boolean hasSlash MAKE_ATTR_UNUSED)
938 {
939 char *file; /* the current filename to check */
940
941 DIR_DEBUG1(" %s ...\n", dir->name);
942
943 if (Hash_FindEntry(&dir->files, cp) == NULL)
944 return NULL;
945
946 file = str_concat3(dir->name, "/", cp);
947 DIR_DEBUG1(" returning %s\n", file);
948 dir->hits++;
949 hits++;
950 return file;
951 }
952
953
954 /*-
955 *-----------------------------------------------------------------------
956 * DirLookupSubdir --
957 * Find if the file with the given name exists in the given path.
958 *
959 * Results:
960 * The path to the file or NULL. This path is guaranteed to be in a
961 * different part of memory than name and so may be safely free'd.
962 *
963 * Side Effects:
964 * If the file is found, it is added in the modification times hash
965 * table.
966 *-----------------------------------------------------------------------
967 */
968 static char *
969 DirLookupSubdir(CachedDir *dir, const char *name)
970 {
971 struct make_stat mst;
972 char *file; /* the current filename to check */
973
974 if (dir != dot) {
975 file = str_concat3(dir->name, "/", name);
976 } else {
977 /*
978 * Checking in dot -- DON'T put a leading ./ on the thing.
979 */
980 file = bmake_strdup(name);
981 }
982
983 DIR_DEBUG1("checking %s ...\n", file);
984
985 if (cached_stat(file, &mst) == 0) {
986 nearmisses++;
987 return file;
988 }
989 free(file);
990 return NULL;
991 }
992
993 /*-
994 *-----------------------------------------------------------------------
995 * DirLookupAbs --
996 * Find if the file with the given name exists in the given path.
997 *
998 * Results:
999 * The path to the file, the empty string or NULL. If the file is
1000 * the empty string, the search should be terminated.
1001 * This path is guaranteed to be in a different part of memory
1002 * than name and so may be safely free'd.
1003 *
1004 * Side Effects:
1005 * None.
1006 *-----------------------------------------------------------------------
1007 */
1008 static char *
1009 DirLookupAbs(CachedDir *dir, const char *name, const char *cp)
1010 {
1011 char *p1; /* pointer into dir->name */
1012 const char *p2; /* pointer into name */
1013
1014 DIR_DEBUG1(" %s ...\n", dir->name);
1015
1016 /*
1017 * If the file has a leading path component and that component
1018 * exactly matches the entire name of the current search
1019 * directory, we can attempt another cache lookup. And if we don't
1020 * have a hit, we can safely assume the file does not exist at all.
1021 */
1022 for (p1 = dir->name, p2 = name; *p1 && *p1 == *p2; p1++, p2++) {
1023 continue;
1024 }
1025 if (*p1 != '\0' || p2 != cp - 1) {
1026 return NULL;
1027 }
1028
1029 if (Hash_FindEntry(&dir->files, cp) == NULL) {
1030 DIR_DEBUG0(" must be here but isn't -- returning\n");
1031 /* Return empty string: terminates search */
1032 return bmake_strdup("");
1033 }
1034
1035 dir->hits++;
1036 hits++;
1037 DIR_DEBUG1(" returning %s\n", name);
1038 return bmake_strdup(name);
1039 }
1040
1041 /*-
1042 *-----------------------------------------------------------------------
1043 * DirFindDot --
1044 * Find the file given on "." or curdir
1045 *
1046 * Results:
1047 * The path to the file or NULL. This path is guaranteed to be in a
1048 * different part of memory than name and so may be safely free'd.
1049 *
1050 * Side Effects:
1051 * Hit counts change
1052 *-----------------------------------------------------------------------
1053 */
1054 static char *
1055 DirFindDot(Boolean hasSlash MAKE_ATTR_UNUSED, const char *name, const char *cp)
1056 {
1057
1058 if (Hash_FindEntry(&dot->files, cp) != NULL) {
1059 DIR_DEBUG0(" in '.'\n");
1060 hits++;
1061 dot->hits++;
1062 return bmake_strdup(name);
1063 }
1064 if (cur && Hash_FindEntry(&cur->files, cp) != NULL) {
1065 DIR_DEBUG1(" in ${.CURDIR} = %s\n", cur->name);
1066 hits++;
1067 cur->hits++;
1068 return str_concat3(cur->name, "/", cp);
1069 }
1070
1071 return NULL;
1072 }
1073
1074 /*-
1075 *-----------------------------------------------------------------------
1076 * Dir_FindFile --
1077 * Find the file with the given name along the given search path.
1078 *
1079 * Input:
1080 * name the file to find
1081 * path the Lst of directories to search
1082 *
1083 * Results:
1084 * The path to the file or NULL. This path is guaranteed to be in a
1085 * different part of memory than name and so may be safely free'd.
1086 *
1087 * Side Effects:
1088 * If the file is found in a directory which is not on the path
1089 * already (either 'name' is absolute or it is a relative path
1090 * [ dir1/.../dirn/file ] which exists below one of the directories
1091 * already on the search path), its directory is added to the end
1092 * of the path on the assumption that there will be more files in
1093 * that directory later on. Sometimes this is true. Sometimes not.
1094 *-----------------------------------------------------------------------
1095 */
1096 char *
1097 Dir_FindFile(const char *name, SearchPath *path)
1098 {
1099 SearchPathNode *ln;
1100 char *file; /* the current filename to check */
1101 CachedDir *dir;
1102 const char *base; /* Terminal name of file */
1103 Boolean hasLastDot = FALSE; /* true if we should search dot last */
1104 Boolean hasSlash; /* true if 'name' contains a / */
1105 struct make_stat mst; /* Buffer for stat, if necessary */
1106 const char *trailing_dot = ".";
1107
1108 /*
1109 * Find the final component of the name and note whether it has a
1110 * slash in it (the name, I mean)
1111 */
1112 base = strrchr(name, '/');
1113 if (base) {
1114 hasSlash = TRUE;
1115 base++;
1116 } else {
1117 hasSlash = FALSE;
1118 base = name;
1119 }
1120
1121 DIR_DEBUG1("Searching for %s ...", name);
1122
1123 if (path == NULL) {
1124 DIR_DEBUG0("couldn't open path, file not found\n");
1125 misses++;
1126 return NULL;
1127 }
1128
1129 Lst_Open(path);
1130 if ((ln = Lst_First(path)) != NULL) {
1131 dir = LstNode_Datum(ln);
1132 if (dir == dotLast) {
1133 hasLastDot = TRUE;
1134 DIR_DEBUG0("[dot last]...");
1135 }
1136 }
1137 DIR_DEBUG0("\n");
1138
1139 /*
1140 * If there's no leading directory components or if the leading
1141 * directory component is exactly `./', consult the cached contents
1142 * of each of the directories on the search path.
1143 */
1144 if (!hasSlash || (base - name == 2 && *name == '.')) {
1145 /*
1146 * We look through all the directories on the path seeking one which
1147 * contains the final component of the given name. If such a beast
1148 * is found, we concatenate the directory name and the final
1149 * component and return the resulting string. If we don't find any
1150 * such thing, we go on to phase two...
1151 *
1152 * No matter what, we always look for the file in the current
1153 * directory before anywhere else (unless we found the magic
1154 * DOTLAST path, in which case we search it last) and we *do not*
1155 * add the ./ to it if it exists.
1156 * This is so there are no conflicts between what the user
1157 * specifies (fish.c) and what pmake finds (./fish.c).
1158 */
1159 if (!hasLastDot && (file = DirFindDot(hasSlash, name, base)) != NULL) {
1160 Lst_Close(path);
1161 return file;
1162 }
1163
1164 while ((ln = Lst_Next(path)) != NULL) {
1165 dir = LstNode_Datum(ln);
1166 if (dir == dotLast)
1167 continue;
1168 if ((file = DirLookup(dir, name, base, hasSlash)) != NULL) {
1169 Lst_Close(path);
1170 return file;
1171 }
1172 }
1173
1174 if (hasLastDot && (file = DirFindDot(hasSlash, name, base)) != NULL) {
1175 Lst_Close(path);
1176 return file;
1177 }
1178 }
1179 Lst_Close(path);
1180
1181 /*
1182 * We didn't find the file on any directory in the search path.
1183 * If the name doesn't contain a slash, that means it doesn't exist.
1184 * If it *does* contain a slash, however, there is still hope: it
1185 * could be in a subdirectory of one of the members of the search
1186 * path. (eg. /usr/include and sys/types.h. The above search would
1187 * fail to turn up types.h in /usr/include, but it *is* in
1188 * /usr/include/sys/types.h).
1189 * [ This no longer applies: If we find such a beast, we assume there
1190 * will be more (what else can we assume?) and add all but the last
1191 * component of the resulting name onto the search path (at the
1192 * end).]
1193 * This phase is only performed if the file is *not* absolute.
1194 */
1195 if (!hasSlash) {
1196 DIR_DEBUG0(" failed.\n");
1197 misses++;
1198 return NULL;
1199 }
1200
1201 if (*base == '\0') {
1202 /* we were given a trailing "/" */
1203 base = trailing_dot;
1204 }
1205
1206 if (name[0] != '/') {
1207 Boolean checkedDot = FALSE;
1208
1209 DIR_DEBUG0(" Trying subdirectories...\n");
1210
1211 if (!hasLastDot) {
1212 if (dot) {
1213 checkedDot = TRUE;
1214 if ((file = DirLookupSubdir(dot, name)) != NULL)
1215 return file;
1216 }
1217 if (cur && (file = DirLookupSubdir(cur, name)) != NULL)
1218 return file;
1219 }
1220
1221 Lst_Open(path);
1222 while ((ln = Lst_Next(path)) != NULL) {
1223 dir = LstNode_Datum(ln);
1224 if (dir == dotLast)
1225 continue;
1226 if (dir == dot) {
1227 if (checkedDot)
1228 continue;
1229 checkedDot = TRUE;
1230 }
1231 if ((file = DirLookupSubdir(dir, name)) != NULL) {
1232 Lst_Close(path);
1233 return file;
1234 }
1235 }
1236 Lst_Close(path);
1237
1238 if (hasLastDot) {
1239 if (dot && !checkedDot) {
1240 checkedDot = TRUE;
1241 if ((file = DirLookupSubdir(dot, name)) != NULL)
1242 return file;
1243 }
1244 if (cur && (file = DirLookupSubdir(cur, name)) != NULL)
1245 return file;
1246 }
1247
1248 if (checkedDot) {
1249 /*
1250 * Already checked by the given name, since . was in the path,
1251 * so no point in proceeding...
1252 */
1253 DIR_DEBUG0(" Checked . already, returning NULL\n");
1254 return NULL;
1255 }
1256
1257 } else { /* name[0] == '/' */
1258
1259 /*
1260 * For absolute names, compare directory path prefix against the
1261 * the directory path of each member on the search path for an exact
1262 * match. If we have an exact match on any member of the search path,
1263 * use the cached contents of that member to lookup the final file
1264 * component. If that lookup fails we can safely assume that the
1265 * file does not exist at all. This is signified by DirLookupAbs()
1266 * returning an empty string.
1267 */
1268 DIR_DEBUG0(" Trying exact path matches...\n");
1269
1270 if (!hasLastDot && cur &&
1271 ((file = DirLookupAbs(cur, name, base)) != NULL)) {
1272 if (file[0] == '\0') {
1273 free(file);
1274 return NULL;
1275 }
1276 return file;
1277 }
1278
1279 Lst_Open(path);
1280 while ((ln = Lst_Next(path)) != NULL) {
1281 dir = LstNode_Datum(ln);
1282 if (dir == dotLast)
1283 continue;
1284 if ((file = DirLookupAbs(dir, name, base)) != NULL) {
1285 Lst_Close(path);
1286 if (file[0] == '\0') {
1287 free(file);
1288 return NULL;
1289 }
1290 return file;
1291 }
1292 }
1293 Lst_Close(path);
1294
1295 if (hasLastDot && cur &&
1296 ((file = DirLookupAbs(cur, name, base)) != NULL)) {
1297 if (file[0] == '\0') {
1298 free(file);
1299 return NULL;
1300 }
1301 return file;
1302 }
1303 }
1304
1305 /*
1306 * Didn't find it that way, either. Sigh. Phase 3. Add its directory
1307 * onto the search path in any case, just in case, then look for the
1308 * thing in the hash table. If we find it, grand. We return a new
1309 * copy of the name. Otherwise we sadly return a NULL pointer. Sigh.
1310 * Note that if the directory holding the file doesn't exist, this will
1311 * do an extra search of the final directory on the path. Unless something
1312 * weird happens, this search won't succeed and life will be groovy.
1313 *
1314 * Sigh. We cannot add the directory onto the search path because
1315 * of this amusing case:
1316 * $(INSTALLDIR)/$(FILE): $(FILE)
1317 *
1318 * $(FILE) exists in $(INSTALLDIR) but not in the current one.
1319 * When searching for $(FILE), we will find it in $(INSTALLDIR)
1320 * b/c we added it here. This is not good...
1321 */
1322 #ifdef notdef
1323 if (base == trailing_dot) {
1324 base = strrchr(name, '/');
1325 base++;
1326 }
1327 base[-1] = '\0';
1328 (void)Dir_AddDir(path, name);
1329 base[-1] = '/';
1330
1331 bigmisses++;
1332 ln = Lst_Last(path);
1333 if (ln == NULL) {
1334 return NULL;
1335 } else {
1336 dir = LstNode_Datum(ln);
1337 }
1338
1339 if (Hash_FindEntry(&dir->files, base) != NULL) {
1340 return bmake_strdup(name);
1341 } else {
1342 return NULL;
1343 }
1344 #else /* !notdef */
1345 DIR_DEBUG1(" Looking for \"%s\" ...\n", name);
1346
1347 bigmisses++;
1348 if (cached_stat(name, &mst) == 0) {
1349 return bmake_strdup(name);
1350 }
1351
1352 DIR_DEBUG0(" failed. Returning NULL\n");
1353 return NULL;
1354 #endif /* notdef */
1355 }
1356
1357
1358 /*-
1359 *-----------------------------------------------------------------------
1360 * Dir_FindHereOrAbove --
1361 * search for a path starting at a given directory and then working
1362 * our way up towards the root.
1363 *
1364 * Input:
1365 * here starting directory
1366 * search_path the path we are looking for
1367 * result the result of a successful search is placed here
1368 * result_len the length of the result buffer
1369 * (typically MAXPATHLEN + 1)
1370 *
1371 * Results:
1372 * 0 on failure, 1 on success [in which case the found path is put
1373 * in the result buffer].
1374 *
1375 * Side Effects:
1376 *-----------------------------------------------------------------------
1377 */
1378 Boolean
1379 Dir_FindHereOrAbove(const char *here, const char *search_path,
1380 char *result, size_t result_len)
1381 {
1382 struct make_stat mst;
1383 char dirbase[MAXPATHLEN + 1], *dirbase_end;
1384 char try[MAXPATHLEN + 1], *try_end;
1385
1386 /* copy out our starting point */
1387 snprintf(dirbase, sizeof(dirbase), "%s", here);
1388 dirbase_end = dirbase + strlen(dirbase);
1389
1390 /* loop until we determine a result */
1391 while (TRUE) {
1392
1393 /* try and stat(2) it ... */
1394 snprintf(try, sizeof(try), "%s/%s", dirbase, search_path);
1395 if (cached_stat(try, &mst) != -1) {
1396 /*
1397 * success! if we found a file, chop off
1398 * the filename so we return a directory.
1399 */
1400 if ((mst.mst_mode & S_IFMT) != S_IFDIR) {
1401 try_end = try + strlen(try);
1402 while (try_end > try && *try_end != '/')
1403 try_end--;
1404 if (try_end > try)
1405 *try_end = '\0'; /* chop! */
1406 }
1407
1408 snprintf(result, result_len, "%s", try);
1409 return TRUE;
1410 }
1411
1412 /*
1413 * nope, we didn't find it. if we used up dirbase we've
1414 * reached the root and failed.
1415 */
1416 if (dirbase_end == dirbase)
1417 break; /* failed! */
1418
1419 /*
1420 * truncate dirbase from the end to move up a dir
1421 */
1422 while (dirbase_end > dirbase && *dirbase_end != '/')
1423 dirbase_end--;
1424 *dirbase_end = '\0'; /* chop! */
1425
1426 } /* while (TRUE) */
1427
1428 return FALSE;
1429 }
1430
1431 /*-
1432 *-----------------------------------------------------------------------
1433 * Dir_MTime --
1434 * Find the modification time of the file described by gn along the
1435 * search path dirSearchPath.
1436 *
1437 * Input:
1438 * gn the file whose modification time is desired
1439 *
1440 * Results:
1441 * The modification time or 0 if it doesn't exist
1442 *
1443 * Side Effects:
1444 * The modification time is placed in the node's mtime slot.
1445 * If the node didn't have a path entry before, and Dir_FindFile
1446 * found one for it, the full name is placed in the path slot.
1447 *-----------------------------------------------------------------------
1448 */
1449 time_t
1450 Dir_MTime(GNode *gn, Boolean recheck)
1451 {
1452 char *fullName; /* the full pathname of name */
1453 struct make_stat mst; /* buffer for finding the mod time */
1454
1455 if (gn->type & OP_ARCHV) {
1456 return Arch_MTime(gn);
1457 } else if (gn->type & OP_PHONY) {
1458 gn->mtime = 0;
1459 return 0;
1460 } else if (gn->path == NULL) {
1461 if (gn->type & OP_NOPATH)
1462 fullName = NULL;
1463 else {
1464 fullName = Dir_FindFile(gn->name, Suff_FindPath(gn));
1465 if (fullName == NULL && gn->flags & FROM_DEPEND &&
1466 !Lst_IsEmpty(gn->implicitParents)) {
1467 char *cp;
1468
1469 cp = strrchr(gn->name, '/');
1470 if (cp) {
1471 /*
1472 * This is an implied source, and it may have moved,
1473 * see if we can find it via the current .PATH
1474 */
1475 cp++;
1476
1477 fullName = Dir_FindFile(cp, Suff_FindPath(gn));
1478 if (fullName) {
1479 /*
1480 * Put the found file in gn->path
1481 * so that we give that to the compiler.
1482 */
1483 gn->path = bmake_strdup(fullName);
1484 if (!Job_RunTarget(".STALE", gn->fname))
1485 fprintf(stdout,
1486 "%s: %s, %d: ignoring stale %s for %s, "
1487 "found %s\n", progname, gn->fname,
1488 gn->lineno,
1489 makeDependfile, gn->name, fullName);
1490 }
1491 }
1492 }
1493 DIR_DEBUG2("Found '%s' as '%s'\n",
1494 gn->name, fullName ? fullName : "(not found)");
1495 }
1496 } else {
1497 fullName = gn->path;
1498 }
1499
1500 if (fullName == NULL) {
1501 fullName = bmake_strdup(gn->name);
1502 }
1503
1504 if (cached_stats(&mtimes, fullName, &mst, recheck ? CST_UPDATE : 0) < 0) {
1505 if (gn->type & OP_MEMBER) {
1506 if (fullName != gn->path)
1507 free(fullName);
1508 return Arch_MemMTime(gn);
1509 } else {
1510 mst.mst_mtime = 0;
1511 }
1512 }
1513
1514 if (fullName && gn->path == NULL) {
1515 gn->path = fullName;
1516 }
1517
1518 gn->mtime = mst.mst_mtime;
1519 return gn->mtime;
1520 }
1521
1522 /* Read the list of filenames in the directory and store the result
1523 * in openDirectories.
1524 *
1525 * If a path is given, append the directory to that path.
1526 *
1527 * Input:
1528 * path The path to which the directory should be
1529 * added, or NULL to only add the directory to
1530 * openDirectories
1531 * name The name of the directory to add.
1532 * The name is not normalized in any way.
1533 */
1534 CachedDir *
1535 Dir_AddDir(SearchPath *path, const char *name)
1536 {
1537 CachedDir *dir = NULL; /* the added directory */
1538 DIR *d;
1539 struct dirent *dp;
1540
1541 if (path != NULL && strcmp(name, ".DOTLAST") == 0) {
1542 SearchPathNode *ln = Lst_Find(path, DirFindName, name);
1543 if (ln != NULL)
1544 return LstNode_Datum(ln);
1545
1546 dotLast->refCount++;
1547 Lst_Prepend(path, dotLast);
1548 }
1549
1550 if (path != NULL)
1551 dir = OpenDirs_Find(&openDirs, name);
1552 if (dir != NULL) {
1553 if (Lst_FindDatum(path, dir) == NULL) {
1554 dir->refCount++;
1555 Lst_Append(path, dir);
1556 }
1557 return dir;
1558 }
1559
1560 DIR_DEBUG1("Caching %s ...", name);
1561
1562 if ((d = opendir(name)) != NULL) {
1563 dir = bmake_malloc(sizeof(CachedDir));
1564 dir->name = bmake_strdup(name);
1565 dir->hits = 0;
1566 dir->refCount = 1;
1567 Hash_InitTable(&dir->files);
1568
1569 while ((dp = readdir(d)) != NULL) {
1570 #if defined(sun) && defined(d_ino) /* d_ino is a sunos4 #define for d_fileno */
1571 /*
1572 * The sun directory library doesn't check for a 0 inode
1573 * (0-inode slots just take up space), so we have to do
1574 * it ourselves.
1575 */
1576 if (dp->d_fileno == 0) {
1577 continue;
1578 }
1579 #endif /* sun && d_ino */
1580 (void)Hash_CreateEntry(&dir->files, dp->d_name, NULL);
1581 }
1582 (void)closedir(d);
1583 OpenDirs_Add(&openDirs, dir);
1584 if (path != NULL)
1585 Lst_Append(path, dir);
1586 }
1587 DIR_DEBUG0("done\n");
1588 return dir;
1589 }
1590
1591 /*-
1592 *-----------------------------------------------------------------------
1593 * Dir_CopyDir --
1594 * Callback function for duplicating a search path via Lst_Copy.
1595 * Ups the reference count for the directory.
1596 *
1597 * Results:
1598 * Returns the Path it was given.
1599 *-----------------------------------------------------------------------
1600 */
1601 void *
1602 Dir_CopyDir(void *p)
1603 {
1604 CachedDir *dir = (CachedDir *)p;
1605 dir->refCount++;
1606
1607 return p;
1608 }
1609
1610 /*-
1611 *-----------------------------------------------------------------------
1612 * Dir_MakeFlags --
1613 * Make a string by taking all the directories in the given search
1614 * path and preceding them by the given flag. Used by the suffix
1615 * module to create variables for compilers based on suffix search
1616 * paths.
1617 *
1618 * Input:
1619 * flag flag which should precede each directory
1620 * path list of directories
1621 *
1622 * Results:
1623 * The string mentioned above. Note that there is no space between
1624 * the given flag and each directory. The empty string is returned if
1625 * Things don't go well.
1626 *
1627 * Side Effects:
1628 * None
1629 *-----------------------------------------------------------------------
1630 */
1631 char *
1632 Dir_MakeFlags(const char *flag, SearchPath *path)
1633 {
1634 Buffer buf;
1635 SearchPathNode *ln;
1636
1637 Buf_Init(&buf, 0);
1638
1639 if (path != NULL) {
1640 for (ln = path->first; ln != NULL; ln = ln->next) {
1641 CachedDir *dir = ln->datum;
1642 Buf_AddStr(&buf, " ");
1643 Buf_AddStr(&buf, flag);
1644 Buf_AddStr(&buf, dir->name);
1645 }
1646 }
1647
1648 return Buf_Destroy(&buf, FALSE);
1649 }
1650
1651 /*-
1652 *-----------------------------------------------------------------------
1653 * Dir_Destroy --
1654 * Nuke a directory descriptor, if possible. Callback procedure
1655 * for the suffixes module when destroying a search path.
1656 *
1657 * Input:
1658 * dirp The directory descriptor to nuke
1659 *
1660 * Results:
1661 * None.
1662 *
1663 * Side Effects:
1664 * If no other path references this directory (refCount == 0),
1665 * the CachedDir and all its data are freed.
1666 *
1667 *-----------------------------------------------------------------------
1668 */
1669 void
1670 Dir_Destroy(void *dirp)
1671 {
1672 CachedDir *dir = dirp;
1673 dir->refCount--;
1674
1675 if (dir->refCount == 0) {
1676 OpenDirs_Remove(&openDirs, dir->name);
1677
1678 Hash_DeleteTable(&dir->files);
1679 free(dir->name);
1680 free(dir);
1681 }
1682 }
1683
1684 /*-
1685 *-----------------------------------------------------------------------
1686 * Dir_ClearPath --
1687 * Clear out all elements of the given search path. This is different
1688 * from destroying the list, notice.
1689 *
1690 * Input:
1691 * path Path to clear
1692 *
1693 * Results:
1694 * None.
1695 *
1696 * Side Effects:
1697 * The path is set to the empty list.
1698 *
1699 *-----------------------------------------------------------------------
1700 */
1701 void
1702 Dir_ClearPath(SearchPath *path)
1703 {
1704 while (!Lst_IsEmpty(path)) {
1705 CachedDir *dir = Lst_Dequeue(path);
1706 Dir_Destroy(dir);
1707 }
1708 }
1709
1710
1711 /*-
1712 *-----------------------------------------------------------------------
1713 * Dir_Concat --
1714 * Concatenate two paths, adding the second to the end of the first.
1715 * Makes sure to avoid duplicates.
1716 *
1717 * Input:
1718 * path1 Dest
1719 * path2 Source
1720 *
1721 * Results:
1722 * None
1723 *
1724 * Side Effects:
1725 * Reference counts for added dirs are upped.
1726 *
1727 *-----------------------------------------------------------------------
1728 */
1729 void
1730 Dir_Concat(SearchPath *path1, SearchPath *path2)
1731 {
1732 SearchPathNode *ln;
1733
1734 for (ln = path2->first; ln != NULL; ln = ln->next) {
1735 CachedDir *dir = ln->datum;
1736 if (Lst_FindDatum(path1, dir) == NULL) {
1737 dir->refCount++;
1738 Lst_Append(path1, dir);
1739 }
1740 }
1741 }
1742
1743 static int
1744 percentage(int num, int den)
1745 {
1746 return den != 0 ? num * 100 / den : 0;
1747 }
1748
1749 /********** DEBUG INFO **********/
1750 void
1751 Dir_PrintDirectories(void)
1752 {
1753 CachedDirListNode *ln;
1754
1755 debug_printf("#*** Directory Cache:\n");
1756 debug_printf("# Stats: %d hits %d misses %d near misses %d losers (%d%%)\n",
1757 hits, misses, nearmisses, bigmisses,
1758 percentage(hits, hits + bigmisses + nearmisses));
1759 debug_printf("# %-20s referenced\thits\n", "directory");
1760
1761 for (ln = openDirs.list->first; ln != NULL; ln = ln->next) {
1762 CachedDir *dir = ln->datum;
1763 debug_printf("# %-20s %10d\t%4d\n", dir->name, dir->refCount,
1764 dir->hits);
1765 }
1766 }
1767
1768 void
1769 Dir_PrintPath(SearchPath *path)
1770 {
1771 SearchPathNode *node;
1772 for (node = path->first; node != NULL; node = node->next) {
1773 const CachedDir *dir = node->datum;
1774 debug_printf("%s ", dir->name);
1775 }
1776 }
1777