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