Annotation of src/usr.bin/make/dir.c, Revision 1.40
1.31 espie 1: /* $OpenPackages$ */
1.40 ! millert 2: /* $OpenBSD: dir.c,v 1.39 2003/04/21 23:10:52 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/types.h>
67: #include <sys/stat.h>
68: #include <dirent.h>
1.34 espie 69: #include <limits.h>
1.25 espie 70: #include <stddef.h>
1.1 deraadt 71: #include <stdio.h>
1.34 espie 72: #include <stdlib.h>
1.32 espie 73: #include <string.h>
74: #include "config.h"
75: #include "defines.h"
1.25 espie 76: #include "ohash.h"
1.1 deraadt 77: #include "dir.h"
1.32 espie 78: #include "lst.h"
79: #include "memory.h"
80: #include "buf.h"
81: #include "gnode.h"
82: #include "arch.h"
83: #include "targ.h"
84: #include "error.h"
85: #include "str.h"
86: #include "timestamp.h"
87:
88:
89: typedef struct Path_ {
90: int refCount; /* Number of paths with this directory */
91: #ifdef DEBUG_DIRECTORY_CACHE
92: int hits; /* the number of times a file in this
93: * directory has been found */
1.24 espie 94: #endif
1.32 espie 95: struct ohash files; /* Hash table of files in directory */
96: char name[1]; /* Name of directory */
97: } Path;
1.1 deraadt 98:
1.31 espie 99: /* A search path consists of a Lst of Path structures. A Path structure
1.1 deraadt 100: * has in it the name of the directory and a hash table of all the files
101: * in the directory. This is used to cut down on the number of system
102: * calls necessary to find implicit dependents and their like. Since
103: * these searches are made before any actions are taken, we need not
104: * worry about the directory changing due to creation commands. If this
105: * hampers the style of some makefiles, they must be changed.
106: *
107: * A list of all previously-read directories is kept in the
1.25 espie 108: * openDirectories cache.
1.1 deraadt 109: *
110: * The need for the caching of whole directories is brought about by
111: * the multi-level transformation code in suff.c, which tends to search
112: * for far more files than regular make does. In the initial
113: * implementation, the amount of time spent performing "stat" calls was
114: * truly astronomical. The problem with hashing at the start is,
115: * of course, that pmake doesn't then detect changes to these directories
116: * during the course of the make. Three possibilities suggest themselves:
117: *
118: * 1) just use stat to test for a file's existence. As mentioned
119: * above, this is very inefficient due to the number of checks
120: * engendered by the multi-level transformation code.
121: * 2) use readdir() and company to search the directories, keeping
122: * them open between checks. I have tried this and while it
123: * didn't slow down the process too much, it could severely
124: * affect the amount of parallelism available as each directory
125: * open would take another file descriptor out of play for
126: * handling I/O for another job. Given that it is only recently
127: * that UNIX OS's have taken to allowing more than 20 or 32
128: * file descriptors for a process, this doesn't seem acceptable
129: * to me.
130: * 3) record the mtime of the directory in the Path structure and
131: * verify the directory hasn't changed since the contents were
132: * hashed. This will catch the creation or deletion of files,
133: * but not the updating of files. However, since it is the
134: * creation and deletion that is the problem, this could be
135: * a good thing to do. Unfortunately, if the directory (say ".")
136: * were fairly large and changed fairly frequently, the constant
137: * rehashing could seriously degrade performance. It might be
138: * good in such cases to keep track of the number of rehashes
139: * and if the number goes over a (small) limit, resort to using
140: * stat in its place.
141: *
142: * An additional thing to consider is that pmake is used primarily
143: * to create C programs and until recently pcc-based compilers refused
144: * to allow you to specify where the resulting object file should be
145: * placed. This forced all objects to be created in the current
146: * directory. This isn't meant as a full excuse, just an explanation of
147: * some of the reasons for the caching used here.
148: *
149: * One more note: the location of a target's file is only performed
150: * on the downward traversal of the graph and then only for terminal
151: * nodes in the graph. This could be construed as wrong in some cases,
152: * but prevents inadvertent modification of files when the "installed"
153: * directory for a file is provided in the search path.
154: *
155: * Another data structure maintained by this module is an mtime
156: * cache used when the searching of cached directories fails to find
157: * a file. In the past, Dir_FindFile would simply perform an access()
158: * call in such a case to determine if the file could be found using
159: * just the name given. When this hit, however, all that was gained
160: * was the knowledge that the file existed. Given that an access() is
161: * essentially a stat() without the copyout() call, and that the same
162: * filesystem overhead would have to be incurred in Dir_MTime, it made
163: * sense to replace the access() with a stat() and record the mtime
1.31 espie 164: * in a cache for when Dir_MTime was actually called. */
1.1 deraadt 165:
1.32 espie 166: static LIST thedirSearchPath; /* main search path */
167: Lst dirSearchPath= &thedirSearchPath;
1.1 deraadt 168:
1.32 espie 169: #ifdef DEBUG_DIRECTORY_CACHE
1.31 espie 170: /* Variables for gathering statistics on the efficiency of the hashing
171: * mechanism. */
172: static int hits, /* Found in directory cache */
173: misses, /* Sad, but not evil misses */
174: nearmisses, /* Found under search path */
175: bigmisses; /* Sought by itself */
1.32 espie 176: #endif
1.1 deraadt 177:
1.31 espie 178: static Path *dot; /* contents of current directory */
1.27 espie 179:
180: struct file_stamp {
181: TIMESTAMP mtime; /* time stamp... */
182: char name[1]; /* ...for that file. */
183: };
184:
1.31 espie 185: static struct ohash openDirectories; /* cache all open directories */
186:
1.32 espie 187: /* Global structure used to cache mtimes. XXX We don't cache an mtime
188: * before a caller actually looks up for the given time, because of the
189: * possibility a caller might update the file and invalidate the cache
190: * entry, and we don't look up in this cache except as a last resort.
191: */
192: static struct ohash mtimes;
1.1 deraadt 193:
1.31 espie 194:
1.27 espie 195: /* There are three distinct hash structures:
196: * - to collate files's last modification times (global mtimes)
197: * - to collate file names (in each Path structure)
198: * - to collate known directories (global openDirectories). */
1.30 espie 199: static struct ohash_info stamp_info = { offsetof(struct file_stamp, name),
1.27 espie 200: NULL, hash_alloc, hash_free, element_alloc };
201:
1.31 espie 202: static struct ohash_info file_info = { 0,
1.26 espie 203: NULL, hash_alloc, hash_free, element_alloc };
204:
1.31 espie 205: static struct ohash_info dir_info = { offsetof(Path, name),
1.25 espie 206: 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. */
212: static char *find_file_hashi(Path *, const char *, const char *, u_int32_t);
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: /* free_hash(o): free a ohash structure, where each element can be free'd. */
1.31 espie 222: static void free_hash(struct ohash *);
223:
1.32 espie 224: /* p = DirReaddiri(name, end): read an actual directory, caching results
225: * as we go. */
226: static Path *DirReaddiri(const char *, const char *);
227: /* Handles wildcard expansion on a given directory. */
228: static void DirMatchFilesi(const char *, const char *, Path *, Lst);
229: /* Handles simple wildcard expansion on a path. */
230: static void PathMatchFilesi(const char *, const char *, Lst, Lst);
231: /* Handles wildcards expansion except for curly braces. */
232: static void DirExpandWildi(const char *, const char *, Lst, Lst);
233: #define DirExpandWild(s, l1, l2) DirExpandWildi(s, strchr(s, '\0'), l1, l2)
234: /* Handles wildcard expansion including curly braces. */
235: static void DirExpandCurlyi(const char *, const char *, Lst, Lst);
1.31 espie 236:
1.32 espie 237: /* Debugging: show each word in an expansion list. */
1.31 espie 238: static void DirPrintWord(void *);
1.32 espie 239: /* Debugging: show a dir name in a path. */
1.31 espie 240: static void DirPrintDir(void *);
1.1 deraadt 241:
1.26 espie 242: static void
1.27 espie 243: record_stamp(file, t)
1.31 espie 244: const char *file;
245: TIMESTAMP t;
1.27 espie 246: {
1.38 deraadt 247: unsigned int slot;
1.31 espie 248: const char *end = NULL;
249: struct file_stamp *n;
1.27 espie 250:
1.30 espie 251: slot = ohash_qlookupi(&mtimes, file, &end);
252: n = ohash_find(&mtimes, slot);
1.27 espie 253: if (n)
254: n->mtime = t;
255: else {
1.30 espie 256: n = ohash_create_entry(&stamp_info, file, &end);
1.27 espie 257: n->mtime = t;
1.30 espie 258: ohash_insert(&mtimes, slot, n);
1.27 espie 259: }
260: }
1.31 espie 261:
1.27 espie 262: static struct file_stamp *
1.31 espie 263: find_stampi(file, end)
264: const char *file;
265: const char *end;
1.27 espie 266: {
1.31 espie 267: return ohash_find(&mtimes, ohash_qlookupi(&mtimes, file, &end));
1.27 espie 268: }
269:
270: static void
1.26 espie 271: add_file(p, file)
1.31 espie 272: Path *p;
273: const char *file;
1.26 espie 274: {
1.38 deraadt 275: unsigned int slot;
1.31 espie 276: const char *end = NULL;
277: char *n;
278: struct ohash *h = &p->files;
1.26 espie 279:
1.30 espie 280: slot = ohash_qlookupi(h, file, &end);
281: n = ohash_find(h, slot);
1.26 espie 282: if (n == NULL) {
1.30 espie 283: n = ohash_create_entry(&file_info, file, &end);
284: ohash_insert(h, slot, n);
1.26 espie 285: }
286: }
1.31 espie 287:
1.26 espie 288: static char *
1.32 espie 289: find_file_hashi(p, file, e, hv)
1.26 espie 290: Path *p;
1.31 espie 291: const char *file;
292: const char *e;
1.26 espie 293: u_int32_t hv;
294: {
1.30 espie 295: struct ohash *h = &p->files;
1.26 espie 296:
1.30 espie 297: return ohash_find(h, ohash_lookup_interval(h, file, e, hv));
1.26 espie 298: }
299:
300: static void
301: free_hash(h)
1.30 espie 302: struct ohash *h;
1.26 espie 303: {
1.31 espie 304: void *e;
1.38 deraadt 305: unsigned int i;
1.26 espie 306:
1.30 espie 307: for (e = ohash_first(h, &i); e != NULL; e = ohash_next(h, &i))
1.26 espie 308: free(e);
1.30 espie 309: ohash_delete(h);
1.26 espie 310: }
311:
1.32 espie 312:
313: /* Side Effects: cache the current directory */
1.1 deraadt 314: void
1.20 espie 315: Dir_Init()
1.1 deraadt 316: {
1.32 espie 317: char *dotname = ".";
318:
1.37 espie 319: Static_Lst_Init(dirSearchPath);
1.30 espie 320: ohash_init(&openDirectories, 4, &dir_info);
321: ohash_init(&mtimes, 4, &stamp_info);
1.6 millert 322:
1.32 espie 323:
324: dot = DirReaddiri(dotname, dotname+1);
1.1 deraadt 325:
1.31 espie 326: if (!dot)
327: Error("Can't access current directory");
328:
1.25 espie 329: /* We always need to have dot around, so we increment its reference count
1.31 espie 330: * to make sure it won't be destroyed. */
1.25 espie 331: dot->refCount++;
1.1 deraadt 332: }
333:
1.32 espie 334: #ifdef CLEANUP
1.1 deraadt 335: void
336: Dir_End()
337: {
1.25 espie 338: struct Path *p;
339: unsigned int i;
340:
341: dot->refCount--;
1.17 espie 342: Dir_Destroy(dot);
1.32 espie 343: Lst_Destroy(dirSearchPath, Dir_Destroy);
1.30 espie 344: for (p = ohash_first(&openDirectories, &i); p != NULL;
1.31 espie 345: p = ohash_next(&openDirectories, &i))
1.25 espie 346: Dir_Destroy(p);
1.30 espie 347: ohash_delete(&openDirectories);
1.28 espie 348: free_hash(&mtimes);
1.32 espie 349: }
1.9 espie 350: #endif
1.1 deraadt 351:
1.32 espie 352:
353: /* XXX: This code is not 100% correct ([^]] fails) */
354: bool
355: Dir_HasWildcardsi(name, end)
356: const char *name;
357: const char *end;
1.1 deraadt 358: {
1.31 espie 359: const char *cp;
1.32 espie 360: bool wild = false;
1.28 espie 361: unsigned long brace = 0, bracket = 0;
1.6 millert 362:
1.32 espie 363: for (cp = name; cp != end; cp++) {
1.28 espie 364: switch (*cp) {
1.1 deraadt 365: case '{':
1.7 millert 366: brace++;
1.32 espie 367: wild = true;
1.7 millert 368: break;
369: case '}':
1.28 espie 370: if (brace == 0)
1.32 espie 371: return false;
1.7 millert 372: brace--;
373: break;
1.1 deraadt 374: case '[':
1.7 millert 375: bracket++;
1.32 espie 376: wild = true;
1.7 millert 377: break;
378: case ']':
1.28 espie 379: if (bracket == 0)
1.32 espie 380: return false;
1.7 millert 381: bracket--;
382: break;
1.1 deraadt 383: case '?':
384: case '*':
1.32 espie 385: wild = true;
1.7 millert 386: break;
387: default:
388: break;
1.1 deraadt 389: }
390: }
1.28 espie 391: return wild && bracket == 0 && brace == 0;
1.1 deraadt 392: }
393:
394: /*-
395: *-----------------------------------------------------------------------
1.32 espie 396: * DirMatchFilesi --
1.31 espie 397: * Given a pattern and a Path structure, see if any files
1.1 deraadt 398: * match the pattern and add their names to the 'expansions' list if
399: * any do. This is incomplete -- it doesn't take care of patterns like
400: * src / *src / *.c properly (just *.c on any of the directories), but it
401: * will do for now.
402: *-----------------------------------------------------------------------
403: */
1.26 espie 404: static void
1.32 espie 405: DirMatchFilesi(pattern, end, p, expansions)
1.31 espie 406: const char *pattern; /* Pattern to look for */
1.32 espie 407: const char *end; /* End of pattern */
1.31 espie 408: Path *p; /* Directory to search */
409: Lst expansions; /* Place to store the results */
410: {
411: unsigned int search; /* Index into the directory's table */
412: const char *entry; /* Current entry in the table */
1.32 espie 413: bool isDot; /* Is the directory "." ? */
1.6 millert 414:
1.31 espie 415: isDot = p->name[0] == '.' && p->name[1] == '\0';
1.6 millert 416:
1.30 espie 417: for (entry = ohash_first(&p->files, &search); entry != NULL;
418: entry = ohash_next(&p->files, &search)) {
1.31 espie 419: /* See if the file matches the given pattern. We follow the UNIX
1.1 deraadt 420: * convention that dot files will only be found if the pattern
1.31 espie 421: * begins with a dot (the hashing scheme doesn't hash . or ..,
422: * so they won't match `.*'. */
423: if (*pattern != '.' && *entry == '.')
424: continue;
1.32 espie 425: if (Str_Matchi(entry, strchr(entry, '\0'), pattern, end))
1.13 espie 426: Lst_AtEnd(expansions,
1.32 espie 427: isDot ? estrdup(entry) : Str_concat(p->name, entry, '/'));
1.1 deraadt 428: }
429: }
430:
431: /*-
432: *-----------------------------------------------------------------------
1.32 espie 433: * PathMatchFilesi --
1.31 espie 434: * Traverse directories in the path, calling DirMatchFiles for each.
435: * NOTE: This doesn't handle patterns in directories.
1.1 deraadt 436: *-----------------------------------------------------------------------
437: */
438: static void
1.32 espie 439: PathMatchFilesi(word, end, path, expansions)
1.31 espie 440: const char *word; /* Word to expand */
1.32 espie 441: const char *end; /* End of word */
1.31 espie 442: Lst path; /* Path on which to look */
443: Lst expansions; /* Place to store the result */
444: {
445: LstNode ln; /* Current node */
1.1 deraadt 446:
1.31 espie 447: for (ln = Lst_First(path); ln != NULL; ln = Lst_Adv(ln))
1.32 espie 448: DirMatchFilesi(word, end, (Path *)Lst_Datum(ln), expansions);
1.31 espie 449: }
1.1 deraadt 450:
1.31 espie 451: static void
452: DirPrintWord(word)
453: void *word;
454: {
455: printf("%s ", (char *)word);
1.1 deraadt 456: }
457:
458: /*-
459: *-----------------------------------------------------------------------
1.32 espie 460: * DirExpandWildi:
1.31 espie 461: * Expand all wild cards in a fully qualified name, except for
462: * curly braces.
1.32 espie 463: * Side-effect:
464: * Will hash any directory in which a file is found, and add it to
465: * the path, on the assumption that future lookups will find files
466: * there as well.
1.1 deraadt 467: *-----------------------------------------------------------------------
468: */
469: static void
1.32 espie 470: DirExpandWildi(word, end, path, expansions)
1.31 espie 471: const char *word; /* the word to expand */
1.32 espie 472: const char *end; /* end of word */
1.31 espie 473: Lst path; /* the list of directories in which to find
474: * the resulting files */
475: Lst expansions; /* the list on which to place the results */
476: {
477: const char *cp;
478: const char *slash; /* keep track of first slash before wildcard */
479:
1.32 espie 480: slash = memchr(word, '/', end - word);
1.31 espie 481: if (slash == NULL) {
482: /* First the files in dot. */
1.32 espie 483: DirMatchFilesi(word, end, dot, expansions);
1.1 deraadt 484:
1.31 espie 485: /* Then the files in every other directory on the path. */
1.32 espie 486: PathMatchFilesi(word, end, path, expansions);
1.31 espie 487: return;
488: }
489: /* The thing has a directory component -- find the first wildcard
490: * in the string. */
491: slash = word;
1.32 espie 492: for (cp = word; cp != end; cp++) {
1.31 espie 493: if (*cp == '/')
494: slash = cp;
495: if (*cp == '?' || *cp == '[' || *cp == '*') {
496:
497: if (slash != word) {
498: char *dirpath;
499:
500: /* If the glob isn't in the first component, try and find
501: * all the components up to the one with a wildcard. */
502: dirpath = Dir_FindFilei(word, slash+1, path);
503: /* dirpath is null if we can't find the leading component
504: * XXX: Dir_FindFile won't find internal components.
505: * i.e. if the path contains ../Etc/Object and we're
506: * looking for Etc, it won't be found. */
507: if (dirpath != NULL) {
1.32 espie 508: char *dp;
1.31 espie 509: LIST temp;
510:
1.32 espie 511: dp = strchr(dirpath, '\0');
512: while (dp > dirpath && dp[-1] == '/')
513: dp--;
514:
1.31 espie 515: Lst_Init(&temp);
1.32 espie 516: Dir_AddDiri(&temp, dirpath, dp);
517: PathMatchFilesi(slash+1, end, &temp, expansions);
1.31 espie 518: Lst_Destroy(&temp, NOFREE);
519: }
520: } else
521: /* Start the search from the local directory. */
1.32 espie 522: PathMatchFilesi(word, end, path, expansions);
1.31 espie 523: return;
524: }
1.1 deraadt 525: }
1.31 espie 526: /* Return the file -- this should never happen. */
1.32 espie 527: PathMatchFilesi(word, end, path, expansions);
1.1 deraadt 528: }
529:
530: /*-
531: *-----------------------------------------------------------------------
1.31 espie 532: * DirExpandCurly --
533: * Expand curly braces like the C shell, and other wildcards as per
534: * Str_Match.
1.32 espie 535: * XXX: if curly expansion yields a result with
1.31 espie 536: * no wildcards, the result is placed on the list WITHOUT CHECKING
537: * FOR ITS EXISTENCE.
1.1 deraadt 538: *-----------------------------------------------------------------------
539: */
1.18 espie 540: static void
1.32 espie 541: DirExpandCurlyi(word, endw, path, expansions)
1.31 espie 542: const char *word; /* Entire word to expand */
1.32 espie 543: const char *endw; /* End of word */
1.31 espie 544: Lst path; /* Search path to use */
545: Lst expansions; /* Place to store the expansions */
1.1 deraadt 546: {
1.31 espie 547: const char *cp2; /* Pointer for checking for wildcards in
548: * expansion before calling Dir_Expand */
549: LIST curled; /* Queue of words to expand */
550: char *toexpand; /* Current word to expand */
1.32 espie 551: bool dowild; /* Wildcard left after curlies ? */
1.31 espie 552:
553: /* Determine once and for all if there is something else going on */
1.32 espie 554: dowild = false;
555: for (cp2 = word; cp2 != endw; cp2++)
1.31 espie 556: if (*cp2 == '*' || *cp2 == '?' || *cp2 == '[') {
1.32 espie 557: dowild = true;
1.31 espie 558: break;
559: }
560:
561: /* Prime queue with copy of initial word */
562: Lst_Init(&curled);
1.32 espie 563: Lst_EnQueue(&curled, Str_dupi(word, endw));
1.31 espie 564: while ((toexpand = (char *)Lst_DeQueue(&curled)) != NULL) {
565: const char *brace;
566: const char *start; /* Start of current chunk of brace clause */
567: const char *end; /* Character after the closing brace */
568: int bracelevel;
569: /* Keep track of nested braces. If we hit
570: * the right brace with bracelevel == 0,
571: * this is the end of the clause. */
1.39 espie 572: size_t endLen;
573: /* The length of the ending non-curlied
574: * part of the current expansion */
1.31 espie 575:
576: /* End case: no curly left to expand */
577: brace = strchr(toexpand, '{');
578: if (brace == NULL) {
579: if (dowild) {
580: DirExpandWild(toexpand, path, expansions);
581: free(toexpand);
582: } else
583: Lst_AtEnd(expansions, toexpand);
1.33 espie 584: continue;
585: }
1.31 espie 586:
587: start = brace+1;
588:
589: /* Find the end of the brace clause first, being wary of nested brace
590: * clauses. */
591: for (end = start, bracelevel = 0;; end++) {
592: if (*end == '{')
593: bracelevel++;
594: else if (*end == '\0') {
595: Error("Unterminated {} clause \"%s\"", start);
596: return;
597: } else if (*end == '}' && bracelevel-- == 0)
598: break;
599: }
600: end++;
1.39 espie 601: endLen = strlen(end);
1.31 espie 602:
1.33 espie 603: for (;;) {
1.31 espie 604: char *file; /* To hold current expansion */
1.34 espie 605: const char *cp; /* Current position in brace clause */
1.33 espie 606:
607: /* Find the end of the current expansion */
608: for (bracelevel = 0, cp = start;
609: bracelevel != 0 || (*cp != '}' && *cp != ','); cp++) {
1.31 espie 610: if (*cp == '{')
611: bracelevel++;
1.33 espie 612: else if (*cp == '}')
613: bracelevel--;
1.31 espie 614: }
1.33 espie 615:
1.31 espie 616: /* Build the current combination and enqueue it. */
1.39 espie 617: file = emalloc((brace - toexpand) + (cp - start) + endLen + 1);
1.31 espie 618: if (brace != toexpand)
1.33 espie 619: memcpy(file, toexpand, brace-toexpand);
1.31 espie 620: if (cp != start)
1.33 espie 621: memcpy(file+(brace-toexpand), start, cp-start);
1.39 espie 622: memcpy(file+(brace-toexpand)+(cp-start), end, endLen + 1);
1.31 espie 623: Lst_EnQueue(&curled, file);
1.33 espie 624: if (*cp == '}')
625: break;
626: start = cp+1;
1.31 espie 627: }
1.33 espie 628: free(toexpand);
1.31 espie 629: }
1.1 deraadt 630: }
631:
1.32 espie 632: /* Side effects:
633: * Dir_Expandi will hash directories that were not yet visited */
1.1 deraadt 634: void
1.32 espie 635: Dir_Expandi(word, end, path, expansions)
1.31 espie 636: const char *word; /* the word to expand */
1.32 espie 637: const char *end; /* end of word */
1.31 espie 638: Lst path; /* the list of directories in which to find
639: * the resulting files */
640: Lst expansions; /* the list on which to place the results */
1.1 deraadt 641: {
1.31 espie 642: const char *cp;
1.1 deraadt 643:
1.32 espie 644: if (DEBUG(DIR)) {
645: char *s = Str_dupi(word, end);
646: printf("expanding \"%s\"...", s);
647: free(s);
648: }
1.6 millert 649:
1.32 espie 650: cp = memchr(word, '{', end - word);
1.31 espie 651: if (cp)
1.32 espie 652: DirExpandCurlyi(word, end, path, expansions);
1.31 espie 653: else
1.32 espie 654: DirExpandWildi(word, end, path, expansions);
1.6 millert 655:
1.1 deraadt 656: if (DEBUG(DIR)) {
1.18 espie 657: Lst_Every(expansions, DirPrintWord);
1.1 deraadt 658: fputc('\n', stdout);
659: }
660: }
661:
1.32 espie 662:
1.1 deraadt 663: /*-
664: * Side Effects:
665: * If the file is found in a directory which is not on the path
666: * already (either 'name' is absolute or it is a relative path
667: * [ dir1/.../dirn/file ] which exists below one of the directories
668: * already on the search path), its directory is added to the end
669: * of the path on the assumption that there will be more files in
1.32 espie 670: * that directory later on.
1.1 deraadt 671: */
672: char *
1.31 espie 673: Dir_FindFilei(name, end, path)
1.32 espie 674: const char *name;
675: const char *end;
676: Lst path;
1.31 espie 677: {
1.32 espie 678: Path *p; /* current path member */
1.31 espie 679: char *p1; /* pointer into p->name */
680: const char *p2; /* pointer into name */
681: LstNode ln; /* a list element */
682: char *file; /* the current filename to check */
683: char *temp; /* index into file */
684: const char *cp; /* index of first slash, if any */
1.32 espie 685: bool hasSlash;
1.31 espie 686: struct stat stb; /* Buffer for stat, if necessary */
687: struct file_stamp *entry; /* Entry for mtimes table */
1.32 espie 688: u_int32_t hv; /* hash value for last component in file name */
689: char *q; /* Str_dupi(name, end) */
1.31 espie 690:
1.32 espie 691: /* Find the final component of the name and note whether name has a
692: * slash in it */
693: cp = Str_rchri(name, end, '/');
1.1 deraadt 694: if (cp) {
1.32 espie 695: hasSlash = true;
1.31 espie 696: cp++;
1.1 deraadt 697: } else {
1.32 espie 698: hasSlash = false;
1.1 deraadt 699: cp = name;
700: }
1.6 millert 701:
1.31 espie 702: hv = ohash_interval(cp, &end);
1.26 espie 703:
1.31 espie 704: if (DEBUG(DIR))
1.1 deraadt 705: printf("Searching for %s...", name);
1.31 espie 706: /* No matter what, we always look for the file in the current directory
1.32 espie 707: * before anywhere else and we always return exactly what the caller
708: * specified. */
1.1 deraadt 709: if ((!hasSlash || (cp - name == 2 && *name == '.')) &&
1.32 espie 710: find_file_hashi(dot, cp, end, hv) != NULL) {
1.26 espie 711: if (DEBUG(DIR))
1.1 deraadt 712: printf("in '.'\n");
1.32 espie 713: #ifdef DEBUG_DIRECTORY_CACHE
1.31 espie 714: hits++;
715: dot->hits++;
1.32 espie 716: #endif
717: return Str_dupi(name, end);
1.1 deraadt 718: }
1.6 millert 719:
1.32 espie 720: /* Then, we look through all the directories on path, seeking one
721: * containing the final component of name and whose final
722: * component(s) match name's initial component(s).
723: * If found, we concatenate the directory name and the
724: * final component and return the resulting string. */
1.26 espie 725: for (ln = Lst_First(path); ln != NULL; ln = Lst_Adv(ln)) {
1.22 espie 726: p = (Path *)Lst_Datum(ln);
1.26 espie 727: if (DEBUG(DIR))
1.1 deraadt 728: printf("%s...", p->name);
1.32 espie 729: if (find_file_hashi(p, cp, end, hv) != NULL) {
1.26 espie 730: if (DEBUG(DIR))
1.1 deraadt 731: printf("here...");
732: if (hasSlash) {
1.31 espie 733: /* If the name had a slash, its initial components and p's
1.1 deraadt 734: * final components must match. This is false if a mismatch
735: * is encountered before all of the initial components
736: * have been checked (p2 > name at the end of the loop), or
737: * we matched only part of one of the components of p
1.31 espie 738: * along with all the rest of them (*p1 != '/'). */
739: p1 = p->name + strlen(p->name) - 1;
1.1 deraadt 740: p2 = cp - 2;
741: while (p2 >= name && p1 >= p->name && *p1 == *p2) {
1.31 espie 742: p1--;
743: p2--;
1.1 deraadt 744: }
745: if (p2 >= name || (p1 >= p->name && *p1 != '/')) {
1.26 espie 746: if (DEBUG(DIR))
1.1 deraadt 747: printf("component mismatch -- continuing...");
748: continue;
749: }
750: }
1.32 espie 751: file = Str_concati(p->name, strchr(p->name, '\0'), cp, end, '/');
1.26 espie 752: if (DEBUG(DIR))
1.1 deraadt 753: printf("returning %s\n", file);
1.32 espie 754: #ifdef DEBUG_DIRECTORY_CACHE
1.31 espie 755: p->hits++;
756: hits++;
1.32 espie 757: #endif
1.26 espie 758: return file;
1.1 deraadt 759: } else if (hasSlash) {
1.26 espie 760: /* If the file has a leading path component and that component
1.1 deraadt 761: * exactly matches the entire name of the current search
1.26 espie 762: * directory, we assume the file doesn't exist and return NULL. */
1.31 espie 763: for (p1 = p->name, p2 = name; *p1 && *p1 == *p2; p1++, p2++)
1.1 deraadt 764: continue;
765: if (*p1 == '\0' && p2 == cp - 1) {
1.26 espie 766: if (DEBUG(DIR))
1.31 espie 767: printf("has to be here but isn't -- returning NULL\n");
1.26 espie 768: return NULL;
1.1 deraadt 769: }
770: }
771: }
1.6 millert 772:
1.32 espie 773: /* We didn't find the file on any existing member of the path.
774: * If the name doesn't contain a slash, end of story.
775: * If it does contain a slash, however, it could be in a subdirectory
776: * of one of the members of the search path. (eg., for path=/usr/include
777: * and name=sys/types.h, the above search fails to turn up types.h
778: * in /usr/include, even though /usr/include/sys/types.h exists).
779: *
780: * We only perform this look-up for non-absolute file names.
781: *
782: * Whenever we score a hit, we assume there will be more matches from
783: * that directory, and append all but the last component of the
784: * resulting name onto the search path. */
1.1 deraadt 785: if (!hasSlash) {
1.26 espie 786: if (DEBUG(DIR))
1.1 deraadt 787: printf("failed.\n");
1.32 espie 788: #ifdef DEBUG_DIRECTORY_CACHE
1.31 espie 789: misses++;
1.32 espie 790: #endif
1.26 espie 791: return NULL;
1.1 deraadt 792: }
1.6 millert 793:
1.1 deraadt 794: if (*name != '/') {
1.32 espie 795: bool checkedDot = false;
1.6 millert 796:
1.26 espie 797: if (DEBUG(DIR))
1.1 deraadt 798: printf("failed. Trying subdirectories...");
1.26 espie 799: for (ln = Lst_First(path); ln != NULL; ln = Lst_Adv(ln)) {
1.22 espie 800: p = (Path *)Lst_Datum(ln);
1.26 espie 801: if (p != dot)
1.32 espie 802: file = Str_concati(p->name, strchr(p->name, '\0'), name, end, '/');
1.26 espie 803: else {
804: /* Checking in dot -- DON'T put a leading ./ on the thing. */
1.32 espie 805: file = Str_dupi(name, end);
806: checkedDot = true;
1.1 deraadt 807: }
1.26 espie 808: if (DEBUG(DIR))
1.1 deraadt 809: printf("checking %s...", file);
1.6 millert 810:
1.26 espie 811: if (stat(file, &stb) == 0) {
1.31 espie 812: TIMESTAMP mtime;
1.27 espie 813:
1.32 espie 814: ts_set_from_stat(stb, mtime);
1.26 espie 815: if (DEBUG(DIR))
1.1 deraadt 816: printf("got it.\n");
1.6 millert 817:
1.32 espie 818: /* We've found another directory to search. We know there
819: * is a slash in 'file'. We call Dir_AddDiri to add the
820: * new directory onto the existing search path. Once
821: * that's done, we return the file name, knowing that
822: * should a file in this directory ever be referenced again
823: * in such a manner, we will find it without having to do
824: * numerous access calls. */
1.31 espie 825: temp = strrchr(file, '/');
1.32 espie 826: Dir_AddDiri(path, file, temp);
1.6 millert 827:
1.26 espie 828: /* Save the modification time so if it's needed, we don't have
829: * to fetch it again. */
830: if (DEBUG(DIR))
1.29 espie 831: printf("Caching %s for %s\n", Targ_FmtTime(mtime),
1.1 deraadt 832: file);
1.27 espie 833: record_stamp(file, mtime);
1.32 espie 834: #ifdef DEBUG_DIRECTORY_CACHE
1.31 espie 835: nearmisses++;
1.32 espie 836: #endif
1.26 espie 837: return file;
838: } else
839: free(file);
1.1 deraadt 840: }
1.6 millert 841:
1.26 espie 842: if (DEBUG(DIR))
1.1 deraadt 843: printf("failed. ");
1.31 espie 844:
1.1 deraadt 845: if (checkedDot) {
1.26 espie 846: /* Already checked by the given name, since . was in the path,
847: * so no point in proceeding... */
848: if (DEBUG(DIR))
1.1 deraadt 849: printf("Checked . already, returning NULL\n");
1.26 espie 850: return NULL;
1.1 deraadt 851: }
852: }
1.6 millert 853:
1.32 espie 854: /* Didn't find it that way, either. Last resort: look for the file
855: * in the global mtime cache, then on the disk.
856: * If this doesn't succeed, we finally return a NULL pointer.
1.1 deraadt 857: *
1.32 espie 858: * We cannot add this directory onto the search path because
1.1 deraadt 859: * of this amusing case:
860: * $(INSTALLDIR)/$(FILE): $(FILE)
861: *
862: * $(FILE) exists in $(INSTALLDIR) but not in the current one.
863: * When searching for $(FILE), we will find it in $(INSTALLDIR)
1.26 espie 864: * b/c we added it here. This is not good... */
1.32 espie 865: q = Str_dupi(name, end);
1.26 espie 866: if (DEBUG(DIR))
1.31 espie 867: printf("Looking for \"%s\"...", q);
1.6 millert 868:
1.32 espie 869: #ifdef DEBUG_DIRECTORY_CACHE
1.31 espie 870: bigmisses++;
1.32 espie 871: #endif
1.31 espie 872: entry = find_stampi(name, end);
1.27 espie 873: if (entry != NULL) {
1.26 espie 874: if (DEBUG(DIR))
1.1 deraadt 875: printf("got it (in mtime cache)\n");
1.31 espie 876: return q;
877: } else if (stat(q, &stb) == 0) {
878: TIMESTAMP mtime;
1.27 espie 879:
1.32 espie 880: ts_set_from_stat(stb, mtime);
1.26 espie 881: if (DEBUG(DIR))
1.29 espie 882: printf("Caching %s for %s\n", Targ_FmtTime(mtime),
1.31 espie 883: q);
884: record_stamp(q, mtime);
885: return q;
1.1 deraadt 886: } else {
1.26 espie 887: if (DEBUG(DIR))
1.1 deraadt 888: printf("failed. Returning NULL\n");
1.31 espie 889: free(q);
1.26 espie 890: return NULL;
1.1 deraadt 891: }
892: }
893:
1.25 espie 894: /* Read a directory, either from the disk, or from the cache. */
895: static Path *
1.32 espie 896: DirReaddiri(name, end)
1.31 espie 897: const char *name;
898: const char *end;
1.25 espie 899: {
1.31 espie 900: Path *p; /* pointer to new Path structure */
901: DIR *d; /* for reading directory */
902: struct dirent *dp; /* entry in directory */
903: unsigned int slot;
1.25 espie 904:
1.30 espie 905: slot = ohash_qlookupi(&openDirectories, name, &end);
906: p = ohash_find(&openDirectories, slot);
1.25 espie 907:
908: if (p != NULL)
909: return p;
910:
1.30 espie 911: p = ohash_create_entry(&dir_info, name, &end);
1.32 espie 912: #ifdef DEBUG_DIRECTORY_CACHE
1.25 espie 913: p->hits = 0;
1.32 espie 914: #endif
1.25 espie 915: p->refCount = 0;
1.30 espie 916: ohash_init(&p->files, 4, &file_info);
1.25 espie 917:
918: if (DEBUG(DIR)) {
919: printf("Caching %s...", p->name);
920: fflush(stdout);
921: }
922:
1.31 espie 923: if ((d = opendir(p->name)) == NULL)
924: return NULL;
1.25 espie 925: /* Skip the first two entries -- these will *always* be . and .. */
926: (void)readdir(d);
927: (void)readdir(d);
928:
929: while ((dp = readdir(d)) != NULL) {
930: #if defined(sun) && defined(d_ino) /* d_ino is a sunos4 #define for d_fileno */
931: /* The sun directory library doesn't check for a 0 inode
932: * (0-inode slots just take up space), so we have to do
933: * it ourselves. */
934: if (dp->d_fileno == 0)
935: continue;
936: #endif /* sun && d_ino */
1.26 espie 937: add_file(p, dp->d_name);
1.25 espie 938: }
939: (void)closedir(d);
940: if (DEBUG(DIR))
941: printf("done\n");
942:
1.30 espie 943: ohash_insert(&openDirectories, slot, p);
1.25 espie 944: return p;
945: }
946:
1.1 deraadt 947: /*-
948: *-----------------------------------------------------------------------
1.32 espie 949: * Dir_AddDiri --
1.1 deraadt 950: * Add the given name to the end of the given path. The order of
951: * the arguments is backwards so ParseDoDependency can do a
952: * Lst_ForEach of its list of paths...
953: *
954: * Side Effects:
1.6 millert 955: * A structure is added to the list and the directory is
1.1 deraadt 956: * read and hashed.
957: *-----------------------------------------------------------------------
958: */
1.31 espie 959:
1.1 deraadt 960: void
1.32 espie 961: Dir_AddDiri(path, name, end)
1.31 espie 962: Lst path; /* the path to which the directory should be added */
1.25 espie 963: const char *name; /* the name of the directory to add */
964: const char *end;
1.1 deraadt 965: {
1.25 espie 966: Path *p; /* pointer to new Path structure */
1.6 millert 967:
1.32 espie 968: p = DirReaddiri(name, end);
1.25 espie 969: if (p == NULL)
1.31 espie 970: return;
1.25 espie 971: if (p->refCount == 0)
1.31 espie 972: Lst_AtEnd(path, p);
1.32 espie 973: else if (!Lst_AddNew(path, p))
1.31 espie 974: return;
1.25 espie 975: p->refCount++;
1.1 deraadt 976: }
977:
978: /*-
979: *-----------------------------------------------------------------------
980: * Dir_CopyDir --
981: * Callback function for duplicating a search path via Lst_Duplicate.
982: * Ups the reference count for the directory.
983: *
984: * Results:
985: * Returns the Path it was given.
986: *
987: * Side Effects:
988: * The refCount of the path is incremented.
989: *-----------------------------------------------------------------------
990: */
1.19 espie 991: void *
1.1 deraadt 992: Dir_CopyDir(p)
1.19 espie 993: void *p;
1.1 deraadt 994: {
1.31 espie 995: ((Path *)p)->refCount++;
1.17 espie 996: return p;
1.1 deraadt 997: }
998:
999: /*-
1000: *-----------------------------------------------------------------------
1001: * Dir_MakeFlags --
1002: * Make a string by taking all the directories in the given search
1003: * path and preceding them by the given flag. Used by the suffix
1004: * module to create variables for compilers based on suffix search
1005: * paths.
1006: *
1007: * Results:
1008: * The string mentioned above. Note that there is no space between
1009: * the given flag and each directory. The empty string is returned if
1010: * Things don't go well.
1011: *-----------------------------------------------------------------------
1012: */
1013: char *
1.22 espie 1014: Dir_MakeFlags(flag, path)
1.31 espie 1015: const char *flag; /* flag which should precede each directory */
1016: Lst path; /* list of directories */
1.1 deraadt 1017: {
1018: LstNode ln; /* the node of the current directory */
1.23 espie 1019: BUFFER buf;
1.6 millert 1020:
1.23 espie 1021: Buf_Init(&buf, 0);
1.6 millert 1022:
1.23 espie 1023: for (ln = Lst_First(path); ln != NULL; ln = Lst_Adv(ln)) {
1024: Buf_AddString(&buf, flag);
1025: Buf_AddString(&buf, ((Path *)Lst_Datum(ln))->name);
1026: Buf_AddSpace(&buf);
1.1 deraadt 1027: }
1.6 millert 1028:
1.23 espie 1029: return Buf_Retrieve(&buf);
1.1 deraadt 1030: }
1031:
1032: /*-
1033: *-----------------------------------------------------------------------
1034: * Dir_Destroy --
1035: * Nuke a directory descriptor, if possible. Callback procedure
1036: * for the suffixes module when destroying a search path.
1037: *
1038: * Side Effects:
1039: * If no other path references this directory (refCount == 0),
1040: * the Path and all its data are freed.
1041: *-----------------------------------------------------------------------
1042: */
1043: void
1.31 espie 1044: Dir_Destroy(pp)
1045: void *pp; /* The directory descriptor to nuke */
1.1 deraadt 1046: {
1.31 espie 1047: Path *p = (Path *)pp;
1.1 deraadt 1048:
1.25 espie 1049: if (--p->refCount == 0) {
1.31 espie 1050: ohash_remove(&openDirectories, ohash_qlookup(&openDirectories, p->name));
1.26 espie 1051: free_hash(&p->files);
1.19 espie 1052: free(p);
1.1 deraadt 1053: }
1054: }
1055:
1056: /*-
1057: *-----------------------------------------------------------------------
1058: * Dir_Concat --
1059: * Concatenate two paths, adding the second to the end of the first.
1060: * Makes sure to avoid duplicates.
1061: *
1062: * Side Effects:
1063: * Reference counts for added dirs are upped.
1064: *-----------------------------------------------------------------------
1065: */
1066: void
1067: Dir_Concat(path1, path2)
1.31 espie 1068: Lst path1; /* Dest */
1069: Lst path2; /* Source */
1.1 deraadt 1070: {
1.31 espie 1071: LstNode ln;
1072: Path *p;
1.1 deraadt 1073:
1.22 espie 1074: for (ln = Lst_First(path2); ln != NULL; ln = Lst_Adv(ln)) {
1.1 deraadt 1075: p = (Path *)Lst_Datum(ln);
1.32 espie 1076: if (Lst_AddNew(path1, p))
1.31 espie 1077: p->refCount++;
1.1 deraadt 1078: }
1079: }
1080:
1.32 espie 1081: #ifdef DEBUG_DIRECTORY_CACHE
1.1 deraadt 1082: void
1083: Dir_PrintDirectories()
1084: {
1.31 espie 1085: Path *p;
1086: unsigned int i;
1.6 millert 1087:
1.31 espie 1088: printf("#*** Directory Cache:\n");
1089: printf("# Stats: %d hits %d misses %d near misses %d losers (%d%%)\n",
1.1 deraadt 1090: hits, misses, nearmisses, bigmisses,
1091: (hits+bigmisses+nearmisses ?
1092: hits * 100 / (hits + bigmisses + nearmisses) : 0));
1.31 espie 1093: printf("# %-20s referenced\thits\n", "directory");
1.30 espie 1094: for (p = ohash_first(&openDirectories, &i); p != NULL;
1.31 espie 1095: p = ohash_next(&openDirectories, &i))
1.25 espie 1096: printf("# %-20s %10d\t%4d\n", p->name, p->refCount, p->hits);
1.1 deraadt 1097: }
1.32 espie 1098: #endif
1.1 deraadt 1099:
1.31 espie 1100: static void
1.19 espie 1101: DirPrintDir(p)
1.31 espie 1102: void *p;
1.6 millert 1103: {
1.18 espie 1104: printf("%s ", ((Path *)p)->name);
1.1 deraadt 1105: }
1106:
1107: void
1.18 espie 1108: Dir_PrintPath(path)
1.31 espie 1109: Lst path;
1.1 deraadt 1110: {
1.18 espie 1111: Lst_Every(path, DirPrintDir);
1.29 espie 1112: }
1113:
1.32 espie 1114: TIMESTAMP
1115: Dir_MTime(gn)
1116: GNode *gn; /* the file whose modification time is
1117: * desired */
1118: {
1119: char *fullName; /* the full pathname of name */
1120: struct stat stb; /* buffer for finding the mod time */
1121: struct file_stamp
1122: *entry;
1123: unsigned int slot;
1124: TIMESTAMP mtime;
1125:
1126: if (gn->type & OP_ARCHV)
1127: return Arch_MTime(gn);
1128:
1129: if (gn->path == NULL) {
1130: fullName = Dir_FindFile(gn->name, dirSearchPath);
1131: if (fullName == NULL)
1132: fullName = estrdup(gn->name);
1133: } else
1134: fullName = gn->path;
1135:
1136: slot = ohash_qlookup(&mtimes, fullName);
1137: entry = ohash_find(&mtimes, slot);
1138: if (entry != NULL) {
1139: /* Only do this once -- the second time folks are checking to
1140: * see if the file was actually updated, so we need to actually go
1141: * to the file system. */
1142: if (DEBUG(DIR))
1143: printf("Using cached time %s for %s\n",
1144: Targ_FmtTime(entry->mtime), fullName);
1145: mtime = entry->mtime;
1146: free(entry);
1147: ohash_remove(&mtimes, slot);
1148: } else if (stat(fullName, &stb) == 0)
1149: ts_set_from_stat(stb, mtime);
1150: else {
1151: if (gn->type & OP_MEMBER) {
1152: if (fullName != gn->path)
1153: free(fullName);
1154: return Arch_MemMTime(gn);
1155: } else
1156: ts_set_out_of_date(mtime);
1157: }
1158: if (fullName && gn->path == NULL)
1159: gn->path = fullName;
1.29 espie 1160:
1.32 espie 1161: gn->mtime = mtime;
1162: return gn->mtime;
1.1 deraadt 1163: }
1.32 espie 1164: