[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.50

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