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