[BACK]Return to dir.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / make

Annotation of src/usr.bin/make/dir.c, Revision 1.46

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