Annotation of src/usr.bin/make/dir.c, Revision 1.48
1.31 espie 1: /* $OpenPackages$ */
1.48 ! espie 2: /* $OpenBSD: dir.c,v 1.47 2007/09/16 10:14:26 espie Exp $ */
1.7 millert 3: /* $NetBSD: dir.c,v 1.14 1997/03/29 16:51:26 christos Exp $ */
1.1 deraadt 4:
5: /*
1.31 espie 6: * Copyright (c) 1999 Marc Espie.
7: *
8: * Extensive code changes for the OpenBSD project.
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: *
19: * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
20: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
23: * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30: */
31: /*
1.1 deraadt 32: * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
33: * Copyright (c) 1988, 1989 by Adam de Boor
34: * Copyright (c) 1989 by Berkeley Softworks
35: * All rights reserved.
36: *
37: * This code is derived from software contributed to Berkeley by
38: * Adam de Boor.
39: *
40: * Redistribution and use in source and binary forms, with or without
41: * modification, are permitted provided that the following conditions
42: * are met:
43: * 1. Redistributions of source code must retain the above copyright
44: * notice, this list of conditions and the following disclaimer.
45: * 2. Redistributions in binary form must reproduce the above copyright
46: * notice, this list of conditions and the following disclaimer in the
47: * documentation and/or other materials provided with the distribution.
1.40 millert 48: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 49: * may be used to endorse or promote products derived from this software
50: * without specific prior written permission.
51: *
52: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62: * SUCH DAMAGE.
63: */
64:
1.34 espie 65: #include <sys/param.h>
1.32 espie 66: #include <sys/stat.h>
67: #include <dirent.h>
1.34 espie 68: #include <limits.h>
1.25 espie 69: #include <stddef.h>
1.1 deraadt 70: #include <stdio.h>
1.44 espie 71: #include <stdint.h>
1.34 espie 72: #include <stdlib.h>
1.32 espie 73: #include <string.h>
74: #include "config.h"
75: #include "defines.h"
1.25 espie 76: #include "ohash.h"
1.1 deraadt 77: #include "dir.h"
1.32 espie 78: #include "lst.h"
79: #include "memory.h"
80: #include "buf.h"
81: #include "gnode.h"
82: #include "arch.h"
83: #include "targ.h"
84: #include "error.h"
85: #include "str.h"
86: #include "timestamp.h"
87:
88:
89: typedef struct Path_ {
1.46 espie 90: int refCount; /* Number of paths with this directory */
1.32 espie 91: #ifdef DEBUG_DIRECTORY_CACHE
1.46 espie 92: int hits; /* the number of times a file in this
1.32 espie 93: * directory has been found */
1.24 espie 94: #endif
1.46 espie 95: struct ohash files; /* Hash table of files in directory */
96: char name[1]; /* Name of directory */
1.32 espie 97: } Path;
1.1 deraadt 98:
1.31 espie 99: /* A search path consists of a Lst of Path structures. A Path structure
1.1 deraadt 100: * has in it the name of the directory and a hash table of all the files
101: * in the directory. This is used to cut down on the number of system
102: * calls necessary to find implicit dependents and their like. Since
103: * these searches are made before any actions are taken, we need not
104: * worry about the directory changing due to creation commands. If this
105: * hampers the style of some makefiles, they must be changed.
106: *
107: * A list of all previously-read directories is kept in the
1.25 espie 108: * openDirectories cache.
1.1 deraadt 109: *
110: * The need for the caching of whole directories is brought about by
111: * the multi-level transformation code in suff.c, which tends to search
112: * for far more files than regular make does. In the initial
113: * implementation, the amount of time spent performing "stat" calls was
114: * truly astronomical. The problem with hashing at the start is,
115: * of course, that pmake doesn't then detect changes to these directories
116: * during the course of the make. Three possibilities suggest themselves:
117: *
118: * 1) just use stat to test for a file's existence. As mentioned
119: * above, this is very inefficient due to the number of checks
120: * engendered by the multi-level transformation code.
121: * 2) use readdir() and company to search the directories, keeping
122: * them open between checks. I have tried this and while it
123: * didn't slow down the process too much, it could severely
124: * affect the amount of parallelism available as each directory
125: * open would take another file descriptor out of play for
126: * handling I/O for another job. Given that it is only recently
127: * that UNIX OS's have taken to allowing more than 20 or 32
128: * file descriptors for a process, this doesn't seem acceptable
129: * to me.
130: * 3) record the mtime of the directory in the Path structure and
131: * verify the directory hasn't changed since the contents were
132: * hashed. This will catch the creation or deletion of files,
133: * but not the updating of files. However, since it is the
134: * creation and deletion that is the problem, this could be
135: * a good thing to do. Unfortunately, if the directory (say ".")
136: * were fairly large and changed fairly frequently, the constant
137: * rehashing could seriously degrade performance. It might be
138: * good in such cases to keep track of the number of rehashes
139: * and if the number goes over a (small) limit, resort to using
140: * stat in its place.
141: *
142: * An additional thing to consider is that pmake is used primarily
143: * to create C programs and until recently pcc-based compilers refused
144: * to allow you to specify where the resulting object file should be
145: * placed. This forced all objects to be created in the current
146: * directory. This isn't meant as a full excuse, just an explanation of
147: * some of the reasons for the caching used here.
148: *
149: * One more note: the location of a target's file is only performed
150: * on the downward traversal of the graph and then only for terminal
151: * nodes in the graph. This could be construed as wrong in some cases,
152: * but prevents inadvertent modification of files when the "installed"
153: * directory for a file is provided in the search path.
154: *
155: * Another data structure maintained by this module is an mtime
156: * cache used when the searching of cached directories fails to find
157: * a file. In the past, Dir_FindFile would simply perform an access()
158: * call in such a case to determine if the file could be found using
159: * just the name given. When this hit, however, all that was gained
160: * was the knowledge that the file existed. Given that an access() is
161: * essentially a stat() without the copyout() call, and that the same
162: * filesystem overhead would have to be incurred in Dir_MTime, it made
163: * sense to replace the access() with a stat() and record the mtime
1.31 espie 164: * in a cache for when Dir_MTime was actually called. */
1.1 deraadt 165:
1.32 espie 166: static LIST thedirSearchPath; /* main search path */
167: Lst dirSearchPath= &thedirSearchPath;
1.1 deraadt 168:
1.32 espie 169: #ifdef DEBUG_DIRECTORY_CACHE
1.31 espie 170: /* Variables for gathering statistics on the efficiency of the hashing
171: * mechanism. */
172: static int hits, /* Found in directory cache */
173: misses, /* Sad, but not evil misses */
174: nearmisses, /* Found under search path */
175: bigmisses; /* Sought by itself */
1.32 espie 176: #endif
1.1 deraadt 177:
1.47 espie 178: Path *dot; /* contents of current directory */
1.27 espie 179:
180: struct file_stamp {
181: TIMESTAMP mtime; /* time stamp... */
182: char name[1]; /* ...for that file. */
183: };
184:
1.31 espie 185: static struct ohash openDirectories; /* cache all open directories */
186:
1.32 espie 187: /* Global structure used to cache mtimes. XXX We don't cache an mtime
188: * before a caller actually looks up for the given time, because of the
189: * possibility a caller might update the file and invalidate the cache
190: * entry, and we don't look up in this cache except as a last resort.
191: */
192: static struct ohash mtimes;
1.1 deraadt 193:
1.31 espie 194:
1.27 espie 195: /* There are three distinct hash structures:
196: * - to collate files's last modification times (global mtimes)
197: * - to collate file names (in each Path structure)
198: * - to collate known directories (global openDirectories). */
1.46 espie 199: static struct ohash_info stamp_info = {
200: offsetof(struct file_stamp, name), NULL, hash_alloc, hash_free,
201: element_alloc };
1.27 espie 202:
1.46 espie 203: static struct ohash_info file_info = {
204: 0, NULL, hash_alloc, hash_free, element_alloc };
1.26 espie 205:
1.46 espie 206: static struct ohash_info dir_info = {
207: offsetof(Path, name), NULL, hash_alloc, hash_free, element_alloc };
1.1 deraadt 208:
1.32 espie 209: /* add_file(path, name): add a file name to a path hash structure. */
1.31 espie 210: static void add_file(Path *, const char *);
1.32 espie 211: /* n = find_file_hashi(p, name, end, hv): retrieve name in a path hash
212: * structure. */
1.44 espie 213: static char *find_file_hashi(Path *, const char *, const char *, uint32_t);
1.32 espie 214:
215: /* stamp = find_stampi(name, end): look for (name, end) in the global
216: * cache. */
1.31 espie 217: static struct file_stamp *find_stampi(const char *, const char *);
1.32 espie 218: /* record_stamp(name, timestamp): record timestamp for name in the global
219: * cache. */
220: static void record_stamp(const char *, TIMESTAMP);
221:
222: /* p = DirReaddiri(name, end): read an actual directory, caching results
223: * as we go. */
224: static Path *DirReaddiri(const char *, const char *);
225: /* Debugging: show a dir name in a path. */
1.31 espie 226: static void DirPrintDir(void *);
1.1 deraadt 227:
1.26 espie 228: static void
1.41 espie 229: record_stamp(const char *file, TIMESTAMP t)
1.27 espie 230: {
1.46 espie 231: unsigned int slot;
232: const char *end = NULL;
233: struct file_stamp *n;
234:
235: slot = ohash_qlookupi(&mtimes, file, &end);
236: n = ohash_find(&mtimes, slot);
237: if (n)
238: n->mtime = t;
239: else {
240: n = ohash_create_entry(&stamp_info, file, &end);
241: n->mtime = t;
242: ohash_insert(&mtimes, slot, n);
243: }
1.27 espie 244: }
1.31 espie 245:
1.27 espie 246: static struct file_stamp *
1.41 espie 247: find_stampi(const char *file, const char *efile)
1.27 espie 248: {
1.46 espie 249: return ohash_find(&mtimes, ohash_qlookupi(&mtimes, file, &efile));
1.27 espie 250: }
251:
252: static void
1.41 espie 253: add_file(Path *p, const char *file)
1.26 espie 254: {
1.46 espie 255: unsigned int slot;
256: const char *end = NULL;
257: char *n;
258: struct ohash *h = &p->files;
259:
260: slot = ohash_qlookupi(h, file, &end);
261: n = ohash_find(h, slot);
262: if (n == NULL) {
263: n = ohash_create_entry(&file_info, file, &end);
264: ohash_insert(h, slot, n);
265: }
1.26 espie 266: }
1.31 espie 267:
1.26 espie 268: static char *
1.44 espie 269: find_file_hashi(Path *p, const char *file, const char *efile, uint32_t hv)
1.26 espie 270: {
1.46 espie 271: struct ohash *h = &p->files;
1.26 espie 272:
1.46 espie 273: return ohash_find(h, ohash_lookup_interval(h, file, efile, hv));
1.26 espie 274: }
275:
1.32 espie 276:
277: /* Side Effects: cache the current directory */
1.1 deraadt 278: void
1.41 espie 279: Dir_Init(void)
1.1 deraadt 280: {
1.46 espie 281: char *dotname = ".";
1.32 espie 282:
1.46 espie 283: Static_Lst_Init(dirSearchPath);
284: ohash_init(&openDirectories, 4, &dir_info);
285: ohash_init(&mtimes, 4, &stamp_info);
1.6 millert 286:
1.32 espie 287:
1.46 espie 288: dot = DirReaddiri(dotname, dotname+1);
1.1 deraadt 289:
1.46 espie 290: if (!dot)
291: Fatal("Can't access current directory");
1.31 espie 292:
1.46 espie 293: /* We always need to have dot around, so we increment its reference
294: * count to make sure it won't be destroyed. */
295: dot->refCount++;
1.1 deraadt 296: }
297:
1.32 espie 298: #ifdef CLEANUP
1.1 deraadt 299: void
1.41 espie 300: Dir_End(void)
1.1 deraadt 301: {
1.46 espie 302: struct Path *p;
303: unsigned int i;
1.25 espie 304:
1.46 espie 305: dot->refCount--;
306: Dir_Destroy(dot);
307: Lst_Destroy(dirSearchPath, Dir_Destroy);
308: for (p = ohash_first(&openDirectories, &i); p != NULL;
309: p = ohash_next(&openDirectories, &i))
310: Dir_Destroy(p);
311: ohash_delete(&openDirectories);
312: free_hash(&mtimes);
1.32 espie 313: }
1.9 espie 314: #endif
1.1 deraadt 315:
316: /*-
317: *-----------------------------------------------------------------------
1.47 espie 318: * Dir_MatchFilesi --
1.31 espie 319: * Given a pattern and a Path structure, see if any files
1.1 deraadt 320: * match the pattern and add their names to the 'expansions' list if
321: * any do. This is incomplete -- it doesn't take care of patterns like
322: * src / *src / *.c properly (just *.c on any of the directories), but it
323: * will do for now.
324: *-----------------------------------------------------------------------
325: */
1.47 espie 326: void
327: Dir_MatchFilesi(const char *word, const char *eword, Path *p, Lst expansions)
1.31 espie 328: {
1.46 espie 329: unsigned int search; /* Index into the directory's table */
330: const char *entry; /* Current entry in the table */
331: bool isDot; /* Is the directory "." ? */
332:
333: isDot = p->name[0] == '.' && p->name[1] == '\0';
334:
335: for (entry = ohash_first(&p->files, &search); entry != NULL;
336: entry = ohash_next(&p->files, &search)) {
337: /* See if the file matches the given pattern. We follow the UNIX
338: * convention that dot files will only be found if the pattern
339: * begins with a dot (the hashing scheme doesn't hash . or ..,
340: * so they won't match `.*'. */
341: if (*word != '.' && *entry == '.')
342: continue;
343: if (Str_Matchi(entry, strchr(entry, '\0'), word, eword))
344: Lst_AtEnd(expansions,
345: isDot ? estrdup(entry) :
346: Str_concat(p->name, entry, '/'));
347: }
1.1 deraadt 348: }
1.32 espie 349:
1.1 deraadt 350: /*-
351: * Side Effects:
352: * If the file is found in a directory which is not on the path
353: * already (either 'name' is absolute or it is a relative path
354: * [ dir1/.../dirn/file ] which exists below one of the directories
355: * already on the search path), its directory is added to the end
356: * of the path on the assumption that there will be more files in
1.32 espie 357: * that directory later on.
1.1 deraadt 358: */
359: char *
1.45 espie 360: Dir_FindFileComplexi(const char *name, const char *ename, Lst path,
361: bool checkCurdirFirst)
1.31 espie 362: {
1.46 espie 363: Path *p; /* current path member */
364: char *p1; /* pointer into p->name */
365: const char *p2; /* pointer into name */
366: LstNode ln; /* a list element */
367: char *file; /* the current filename to check */
368: char *temp; /* index into file */
369: const char *cp; /* index of first slash, if any */
370: bool hasSlash;
371: struct stat stb;/* Buffer for stat, if necessary */
372: struct file_stamp *entry;
373: /* Entry for mtimes table */
374: uint32_t hv; /* hash value for last component in file name */
375: char *q; /* Str_dupi(name, ename) */
376:
377: /* Find the final component of the name and note whether name has a
378: * slash in it */
379: cp = Str_rchri(name, ename, '/');
380: if (cp) {
381: hasSlash = true;
382: cp++;
383: } else {
384: hasSlash = false;
385: cp = name;
386: }
387:
388: hv = ohash_interval(cp, &ename);
1.6 millert 389:
1.26 espie 390: if (DEBUG(DIR))
1.46 espie 391: printf("Searching for %s...", name);
392: /* Unless checkCurDirFirst is false, we always look for
393: * the file in the current directory before anywhere else
394: * and we always return exactly what the caller specified. */
395: if (checkCurdirFirst &&
396: (!hasSlash || (cp - name == 2 && *name == '.')) &&
397: find_file_hashi(dot, cp, ename, hv) != NULL) {
398: if (DEBUG(DIR))
399: printf("in '.'\n");
1.32 espie 400: #ifdef DEBUG_DIRECTORY_CACHE
1.46 espie 401: hits++;
402: dot->hits++;
1.32 espie 403: #endif
1.46 espie 404: return Str_dupi(name, ename);
1.1 deraadt 405: }
1.6 millert 406:
1.46 espie 407: /* Then, we look through all the directories on path, seeking one
408: * containing the final component of name and whose final
409: * component(s) match name's initial component(s).
410: * If found, we concatenate the directory name and the
411: * final component and return the resulting string. */
412: for (ln = Lst_First(path); ln != NULL; ln = Lst_Adv(ln)) {
413: p = (Path *)Lst_Datum(ln);
414: if (DEBUG(DIR))
415: printf("%s...", p->name);
416: if (find_file_hashi(p, cp, ename, hv) != NULL) {
417: if (DEBUG(DIR))
418: printf("here...");
419: if (hasSlash) {
420: /* If the name had a slash, its initial
421: * components and p's final components must
422: * match. This is false if a mismatch is
423: * encountered before all of the initial
424: * components have been checked (p2 > name at
425: * the end of the loop), or we matched only
426: * part of one of the components of p along
427: * with all the rest of them (*p1 != '/'). */
428: p1 = p->name + strlen(p->name) - 1;
429: p2 = cp - 2;
430: while (p2 >= name && p1 >= p->name &&
431: *p1 == *p2) {
432: p1--;
433: p2--;
434: }
435: if (p2 >= name ||
436: (p1 >= p->name && *p1 != '/')) {
437: if (DEBUG(DIR))
438: printf("component mismatch -- continuing...");
439: continue;
440: }
441: }
442: file = Str_concati(p->name, strchr(p->name, '\0'), cp,
443: ename, '/');
444: if (DEBUG(DIR))
445: printf("returning %s\n", file);
1.32 espie 446: #ifdef DEBUG_DIRECTORY_CACHE
1.46 espie 447: p->hits++;
448: hits++;
1.32 espie 449: #endif
1.46 espie 450: return file;
451: } else if (hasSlash) {
452: /* If the file has a leading path component and that
453: * component exactly matches the entire name of the
454: * current search directory, we assume the file
455: * doesn't exist and return NULL. */
456: for (p1 = p->name, p2 = name; *p1 && *p1 == *p2;
457: p1++, p2++)
458: continue;
459: if (*p1 == '\0' && p2 == cp - 1) {
460: if (DEBUG(DIR))
461: printf("has to be here but isn't -- returning NULL\n");
462: return NULL;
463: }
464: }
465: }
1.27 espie 466:
1.46 espie 467: /* We didn't find the file on any existing member of the path.
468: * If the name doesn't contain a slash, end of story.
469: * If it does contain a slash, however, it could be in a subdirectory
470: * of one of the members of the search path. (eg., for path=/usr/include
471: * and name=sys/types.h, the above search fails to turn up types.h
472: * in /usr/include, even though /usr/include/sys/types.h exists).
473: *
474: * We only perform this look-up for non-absolute file names.
475: *
476: * Whenever we score a hit, we assume there will be more matches from
477: * that directory, and append all but the last component of the
478: * resulting name onto the search path. */
479: if (!hasSlash) {
1.26 espie 480: if (DEBUG(DIR))
1.46 espie 481: printf("failed.\n");
482: #ifdef DEBUG_DIRECTORY_CACHE
483: misses++;
484: #endif
485: return NULL;
486: }
1.6 millert 487:
1.46 espie 488: if (*name != '/') {
489: bool checkedDot = false;
1.6 millert 490:
1.26 espie 491: if (DEBUG(DIR))
1.46 espie 492: printf("failed. Trying subdirectories...");
493: for (ln = Lst_First(path); ln != NULL; ln = Lst_Adv(ln)) {
494: p = (Path *)Lst_Datum(ln);
495: if (p != dot)
496: file = Str_concati(p->name,
497: strchr(p->name, '\0'), name, ename, '/');
498: else {
499: /* Checking in dot -- DON'T put a leading
500: * ./ on the thing. */
501: file = Str_dupi(name, ename);
502: checkedDot = true;
503: }
504: if (DEBUG(DIR))
505: printf("checking %s...", file);
506:
507: if (stat(file, &stb) == 0) {
508: TIMESTAMP mtime;
509:
510: ts_set_from_stat(stb, mtime);
511: if (DEBUG(DIR))
512: printf("got it.\n");
513:
514: /* We've found another directory to search.
515: * We know there is a slash in 'file'. We
516: * call Dir_AddDiri to add the new directory
517: * onto the existing search path. Once that's
518: * done, we return the file name, knowing that
519: * should a file in this directory ever be
520: * referenced again in such a manner, we will
521: * find it without having to do numerous
522: * access calls. */
523: temp = strrchr(file, '/');
524: Dir_AddDiri(path, file, temp);
525:
526: /* Save the modification time so if it's
527: * needed, we don't have to fetch it again. */
528: if (DEBUG(DIR))
529: printf("Caching %s for %s\n",
530: Targ_FmtTime(mtime), file);
531: record_stamp(file, mtime);
1.32 espie 532: #ifdef DEBUG_DIRECTORY_CACHE
1.46 espie 533: nearmisses++;
1.32 espie 534: #endif
1.46 espie 535: return file;
536: } else
537: free(file);
538: }
1.6 millert 539:
1.46 espie 540: if (DEBUG(DIR))
541: printf("failed. ");
1.31 espie 542:
1.46 espie 543: if (checkedDot) {
544: /* Already checked by the given name, since . was in
545: * the path, so no point in proceeding... */
546: if (DEBUG(DIR))
547: printf("Checked . already, returning NULL\n");
548: return NULL;
549: }
1.1 deraadt 550: }
1.6 millert 551:
1.46 espie 552: /* Didn't find it that way, either. Last resort: look for the file
553: * in the global mtime cache, then on the disk.
554: * If this doesn't succeed, we finally return a NULL pointer.
555: *
556: * We cannot add this directory onto the search path because
557: * of this amusing case:
558: * $(INSTALLDIR)/$(FILE): $(FILE)
559: *
560: * $(FILE) exists in $(INSTALLDIR) but not in the current one.
561: * When searching for $(FILE), we will find it in $(INSTALLDIR)
562: * b/c we added it here. This is not good... */
563: q = Str_dupi(name, ename);
564: if (DEBUG(DIR))
565: printf("Looking for \"%s\"...", q);
1.6 millert 566:
1.32 espie 567: #ifdef DEBUG_DIRECTORY_CACHE
1.46 espie 568: bigmisses++;
1.32 espie 569: #endif
1.46 espie 570: entry = find_stampi(name, ename);
571: if (entry != NULL) {
572: if (DEBUG(DIR))
573: printf("got it (in mtime cache)\n");
574: return q;
575: } else if (stat(q, &stb) == 0) {
576: TIMESTAMP mtime;
1.27 espie 577:
1.46 espie 578: ts_set_from_stat(stb, mtime);
579: if (DEBUG(DIR))
580: printf("Caching %s for %s\n", Targ_FmtTime(mtime), q);
581: record_stamp(q, mtime);
582: return q;
583: } else {
584: if (DEBUG(DIR))
585: printf("failed. Returning NULL\n");
586: free(q);
587: return NULL;
588: }
1.1 deraadt 589: }
590:
1.25 espie 591: /* Read a directory, either from the disk, or from the cache. */
592: static Path *
1.41 espie 593: DirReaddiri(const char *name, const char *ename)
1.25 espie 594: {
1.46 espie 595: Path *p;
596: DIR *d;
597: struct dirent *dp;
598: unsigned int slot;
1.25 espie 599:
1.46 espie 600: slot = ohash_qlookupi(&openDirectories, name, &ename);
601: p = ohash_find(&openDirectories, slot);
1.25 espie 602:
1.46 espie 603: if (p != NULL)
604: return p;
1.25 espie 605:
1.46 espie 606: p = ohash_create_entry(&dir_info, name, &ename);
1.32 espie 607: #ifdef DEBUG_DIRECTORY_CACHE
1.46 espie 608: p->hits = 0;
1.32 espie 609: #endif
1.46 espie 610: p->refCount = 0;
611: ohash_init(&p->files, 4, &file_info);
1.25 espie 612:
1.46 espie 613: if (DEBUG(DIR)) {
614: printf("Caching %s...", p->name);
615: fflush(stdout);
616: }
617:
618: if ((d = opendir(p->name)) == NULL)
619: return NULL;
1.25 espie 620:
1.46 espie 621: while ((dp = readdir(d)) != NULL) {
622: if (dp->d_name[0] == '.' &&
623: (dp->d_name[1] == '\0' ||
624: (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
625: continue;
626: add_file(p, dp->d_name);
627: }
628: (void)closedir(d);
629: if (DEBUG(DIR))
630: printf("done\n");
631:
632: ohash_insert(&openDirectories, slot, p);
633: return p;
1.25 espie 634: }
635:
1.1 deraadt 636: /*-
637: *-----------------------------------------------------------------------
1.32 espie 638: * Dir_AddDiri --
1.1 deraadt 639: * Add the given name to the end of the given path. The order of
640: * the arguments is backwards so ParseDoDependency can do a
641: * Lst_ForEach of its list of paths...
642: *
643: * Side Effects:
1.6 millert 644: * A structure is added to the list and the directory is
1.1 deraadt 645: * read and hashed.
646: *-----------------------------------------------------------------------
647: */
1.31 espie 648:
1.1 deraadt 649: void
1.41 espie 650: Dir_AddDiri(Lst path, const char *name, const char *ename)
1.1 deraadt 651: {
1.46 espie 652: Path *p;
1.6 millert 653:
1.46 espie 654: p = DirReaddiri(name, ename);
655: if (p == NULL)
656: return;
657: if (p->refCount == 0)
658: Lst_AtEnd(path, p);
659: else if (!Lst_AddNew(path, p))
660: return;
661: p->refCount++;
1.1 deraadt 662: }
663:
664: /*-
665: *-----------------------------------------------------------------------
666: * Dir_CopyDir --
667: * Callback function for duplicating a search path via Lst_Duplicate.
668: * Ups the reference count for the directory.
669: *
670: * Results:
671: * Returns the Path it was given.
672: *
673: * Side Effects:
674: * The refCount of the path is incremented.
675: *-----------------------------------------------------------------------
676: */
1.19 espie 677: void *
1.41 espie 678: Dir_CopyDir(void *p)
1.1 deraadt 679: {
1.46 espie 680: ((Path *)p)->refCount++;
681: return p;
1.1 deraadt 682: }
683:
684: /*-
685: *-----------------------------------------------------------------------
686: * Dir_MakeFlags --
687: * Make a string by taking all the directories in the given search
688: * path and preceding them by the given flag. Used by the suffix
689: * module to create variables for compilers based on suffix search
690: * paths.
691: *
692: * Results:
693: * The string mentioned above. Note that there is no space between
694: * the given flag and each directory. The empty string is returned if
695: * Things don't go well.
696: *-----------------------------------------------------------------------
697: */
698: char *
1.41 espie 699: Dir_MakeFlags(const char *flag, Lst path)
1.1 deraadt 700: {
1.46 espie 701: LstNode ln;
702: BUFFER buf;
1.6 millert 703:
1.46 espie 704: Buf_Init(&buf, 0);
1.6 millert 705:
1.46 espie 706: for (ln = Lst_First(path); ln != NULL; ln = Lst_Adv(ln)) {
707: Buf_AddString(&buf, flag);
708: Buf_AddString(&buf, ((Path *)Lst_Datum(ln))->name);
709: Buf_AddSpace(&buf);
710: }
1.6 millert 711:
1.46 espie 712: return Buf_Retrieve(&buf);
1.1 deraadt 713: }
714:
715: /*-
716: *-----------------------------------------------------------------------
717: * Dir_Destroy --
718: * Nuke a directory descriptor, if possible. Callback procedure
719: * for the suffixes module when destroying a search path.
720: *
721: * Side Effects:
722: * If no other path references this directory (refCount == 0),
723: * the Path and all its data are freed.
724: *-----------------------------------------------------------------------
725: */
726: void
1.41 espie 727: Dir_Destroy(void *pp)
1.1 deraadt 728: {
1.46 espie 729: Path *p = (Path *)pp;
1.1 deraadt 730:
1.46 espie 731: if (--p->refCount == 0) {
732: ohash_remove(&openDirectories,
733: ohash_qlookup(&openDirectories, p->name));
734: free_hash(&p->files);
735: free(p);
736: }
1.1 deraadt 737: }
738:
739: /*-
740: *-----------------------------------------------------------------------
741: * Dir_Concat --
742: * Concatenate two paths, adding the second to the end of the first.
743: * Makes sure to avoid duplicates.
744: *
745: * Side Effects:
746: * Reference counts for added dirs are upped.
747: *-----------------------------------------------------------------------
748: */
749: void
1.41 espie 750: Dir_Concat(Lst path1, Lst path2)
1.1 deraadt 751: {
1.46 espie 752: LstNode ln;
753: Path *p;
1.1 deraadt 754:
1.46 espie 755: for (ln = Lst_First(path2); ln != NULL; ln = Lst_Adv(ln)) {
756: p = (Path *)Lst_Datum(ln);
757: if (Lst_AddNew(path1, p))
758: p->refCount++;
759: }
1.1 deraadt 760: }
761:
1.32 espie 762: #ifdef DEBUG_DIRECTORY_CACHE
1.1 deraadt 763: void
1.41 espie 764: Dir_PrintDirectories(void)
1.1 deraadt 765: {
1.46 espie 766: Path *p;
767: unsigned int i;
1.6 millert 768:
1.46 espie 769: printf("#*** Directory Cache:\n");
770: printf("# Stats: %d hits %d misses %d near misses %d losers (%d%%)\n",
1.1 deraadt 771: hits, misses, nearmisses, bigmisses,
772: (hits+bigmisses+nearmisses ?
773: hits * 100 / (hits + bigmisses + nearmisses) : 0));
1.46 espie 774: printf("# %-20s referenced\thits\n", "directory");
775: for (p = ohash_first(&openDirectories, &i); p != NULL;
776: p = ohash_next(&openDirectories, &i))
777: printf("# %-20s %10d\t%4d\n", p->name, p->refCount, p->hits);
1.1 deraadt 778: }
1.32 espie 779: #endif
1.1 deraadt 780:
1.31 espie 781: static void
1.41 espie 782: DirPrintDir(void *p)
1.6 millert 783: {
1.46 espie 784: printf("%s ", ((Path *)p)->name);
1.1 deraadt 785: }
786:
787: void
1.41 espie 788: Dir_PrintPath(Lst path)
1.1 deraadt 789: {
1.46 espie 790: Lst_Every(path, DirPrintDir);
1.29 espie 791: }
792:
1.32 espie 793: TIMESTAMP
1.41 espie 794: Dir_MTime(GNode *gn)
1.32 espie 795: {
1.46 espie 796: char *fullName;
797: struct stat stb;
798: struct file_stamp *entry;
799: unsigned int slot;
800: TIMESTAMP mtime;
801:
802: if (gn->type & OP_ARCHV)
803: return Arch_MTime(gn);
804:
805: if (gn->path == NULL) {
806: fullName = Dir_FindFile(gn->name, dirSearchPath);
807: if (fullName == NULL)
808: fullName = estrdup(gn->name);
1.32 espie 809: } else
1.46 espie 810: fullName = gn->path;
811:
812: slot = ohash_qlookup(&mtimes, fullName);
813: entry = ohash_find(&mtimes, slot);
814: if (entry != NULL) {
815: /* Only do this once -- the second time folks are checking to
816: * see if the file was actually updated, so we need to
817: * actually go to the file system. */
818: if (DEBUG(DIR))
819: printf("Using cached time %s for %s\n",
820: Targ_FmtTime(entry->mtime), fullName);
821: mtime = entry->mtime;
822: free(entry);
823: ohash_remove(&mtimes, slot);
824: } else if (stat(fullName, &stb) == 0)
825: ts_set_from_stat(stb, mtime);
826: else {
827: if (gn->type & OP_MEMBER) {
828: if (fullName != gn->path)
829: free(fullName);
830: return Arch_MemMTime(gn);
831: } else
832: ts_set_out_of_date(mtime);
833: }
834: if (fullName && gn->path == NULL)
835: gn->path = fullName;
1.29 espie 836:
1.46 espie 837: gn->mtime = mtime;
838: return gn->mtime;
1.1 deraadt 839: }
1.32 espie 840: