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