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