2003-09-09 18:44:35 +04:00
|
|
|
/* $NetBSD: dir.c,v 1.38 2003/09/09 14:44:35 drochner Exp $ */
|
1995-06-14 19:18:37 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
|
2003-08-07 15:13:06 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Adam de Boor.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Copyright (c) 1988, 1989 by Adam de Boor
|
|
|
|
* Copyright (c) 1989 by Berkeley Softworks
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Adam de Boor.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-09-28 07:30:58 +04:00
|
|
|
#ifdef MAKE_BOOTSTRAP
|
2003-09-09 18:44:35 +04:00
|
|
|
static char rcsid[] = "$NetBSD: dir.c,v 1.38 2003/09/09 14:44:35 drochner Exp $";
|
1997-09-28 07:30:58 +04:00
|
|
|
#else
|
1997-07-02 01:17:00 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1995-06-14 19:18:37 +04:00
|
|
|
#if 0
|
1996-11-06 20:58:58 +03:00
|
|
|
static char sccsid[] = "@(#)dir.c 8.2 (Berkeley) 1/2/94";
|
1995-06-14 19:18:37 +04:00
|
|
|
#else
|
2003-09-09 18:44:35 +04:00
|
|
|
__RCSID("$NetBSD: dir.c,v 1.38 2003/09/09 14:44:35 drochner Exp $");
|
1995-06-14 19:18:37 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
1997-09-28 07:30:58 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* dir.c --
|
|
|
|
* Directory searching using wildcards and/or normal names...
|
|
|
|
* Used both for source wildcarding in the Makefile and for finding
|
|
|
|
* implicit sources.
|
|
|
|
*
|
|
|
|
* The interface for this module is:
|
|
|
|
* Dir_Init Initialize the module.
|
|
|
|
*
|
2002-11-26 09:12:59 +03:00
|
|
|
* Dir_InitCur Set the cur Path.
|
|
|
|
*
|
|
|
|
* Dir_InitDot Set the dot Path.
|
|
|
|
*
|
1994-06-07 02:45:17 +04:00
|
|
|
* Dir_End Cleanup the module.
|
|
|
|
*
|
2002-11-26 09:12:59 +03:00
|
|
|
* Dir_SetPATH Set ${.PATH} to reflect state of dirSearchPath.
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Dir_HasWildcards Returns TRUE if the name given it needs to
|
|
|
|
* be wildcard-expanded.
|
|
|
|
*
|
|
|
|
* Dir_Expand Given a pattern and a path, return a Lst of names
|
|
|
|
* which match the pattern on the search path.
|
|
|
|
*
|
|
|
|
* Dir_FindFile Searches for a file on a given search path.
|
|
|
|
* If it exists, the entire path is returned.
|
|
|
|
* Otherwise NULL is returned.
|
|
|
|
*
|
|
|
|
* Dir_MTime Return the modification time of a node. The file
|
|
|
|
* is searched for along the default search path.
|
|
|
|
* The path and mtime fields of the node are filled
|
|
|
|
* in.
|
|
|
|
*
|
|
|
|
* Dir_AddDir Add a directory to a search path.
|
|
|
|
*
|
|
|
|
* Dir_MakeFlags Given a search path and a command flag, create
|
|
|
|
* a string with each of the directories in the path
|
|
|
|
* preceded by the command flag and all of them
|
|
|
|
* separated by a space.
|
|
|
|
*
|
|
|
|
* Dir_Destroy Destroy an element of a search path. Frees up all
|
|
|
|
* things that can be freed for the element as long
|
|
|
|
* as the element is no longer referenced by any other
|
|
|
|
* search path.
|
|
|
|
* Dir_ClearPath Resets a search path to the empty list.
|
|
|
|
*
|
|
|
|
* For debugging:
|
|
|
|
* Dir_PrintDirectories Print stats about the directory cache.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
1994-03-05 03:34:29 +03:00
|
|
|
#include <sys/stat.h>
|
2002-06-15 22:24:55 +04:00
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "make.h"
|
|
|
|
#include "hash.h"
|
1994-03-05 03:34:29 +03:00
|
|
|
#include "dir.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A search path consists of a Lst of Path structures. A Path structure
|
|
|
|
* has in it the name of the directory and a hash table of all the files
|
|
|
|
* in the directory. This is used to cut down on the number of system
|
|
|
|
* calls necessary to find implicit dependents and their like. Since
|
|
|
|
* these searches are made before any actions are taken, we need not
|
|
|
|
* worry about the directory changing due to creation commands. If this
|
|
|
|
* hampers the style of some makefiles, they must be changed.
|
|
|
|
*
|
|
|
|
* A list of all previously-read directories is kept in the
|
|
|
|
* openDirectories Lst. This list is checked first before a directory
|
|
|
|
* is opened.
|
|
|
|
*
|
|
|
|
* The need for the caching of whole directories is brought about by
|
|
|
|
* the multi-level transformation code in suff.c, which tends to search
|
|
|
|
* for far more files than regular make does. In the initial
|
|
|
|
* implementation, the amount of time spent performing "stat" calls was
|
|
|
|
* truly astronomical. The problem with hashing at the start is,
|
|
|
|
* of course, that pmake doesn't then detect changes to these directories
|
|
|
|
* during the course of the make. Three possibilities suggest themselves:
|
|
|
|
*
|
|
|
|
* 1) just use stat to test for a file's existence. As mentioned
|
|
|
|
* above, this is very inefficient due to the number of checks
|
|
|
|
* engendered by the multi-level transformation code.
|
|
|
|
* 2) use readdir() and company to search the directories, keeping
|
|
|
|
* them open between checks. I have tried this and while it
|
|
|
|
* didn't slow down the process too much, it could severely
|
|
|
|
* affect the amount of parallelism available as each directory
|
|
|
|
* open would take another file descriptor out of play for
|
|
|
|
* handling I/O for another job. Given that it is only recently
|
|
|
|
* that UNIX OS's have taken to allowing more than 20 or 32
|
|
|
|
* file descriptors for a process, this doesn't seem acceptable
|
|
|
|
* to me.
|
|
|
|
* 3) record the mtime of the directory in the Path structure and
|
|
|
|
* verify the directory hasn't changed since the contents were
|
|
|
|
* hashed. This will catch the creation or deletion of files,
|
|
|
|
* but not the updating of files. However, since it is the
|
|
|
|
* creation and deletion that is the problem, this could be
|
|
|
|
* a good thing to do. Unfortunately, if the directory (say ".")
|
|
|
|
* were fairly large and changed fairly frequently, the constant
|
|
|
|
* rehashing could seriously degrade performance. It might be
|
|
|
|
* good in such cases to keep track of the number of rehashes
|
|
|
|
* and if the number goes over a (small) limit, resort to using
|
|
|
|
* stat in its place.
|
|
|
|
*
|
|
|
|
* An additional thing to consider is that pmake is used primarily
|
|
|
|
* to create C programs and until recently pcc-based compilers refused
|
|
|
|
* to allow you to specify where the resulting object file should be
|
|
|
|
* placed. This forced all objects to be created in the current
|
|
|
|
* directory. This isn't meant as a full excuse, just an explanation of
|
|
|
|
* some of the reasons for the caching used here.
|
|
|
|
*
|
|
|
|
* One more note: the location of a target's file is only performed
|
|
|
|
* on the downward traversal of the graph and then only for terminal
|
|
|
|
* nodes in the graph. This could be construed as wrong in some cases,
|
|
|
|
* but prevents inadvertent modification of files when the "installed"
|
|
|
|
* directory for a file is provided in the search path.
|
|
|
|
*
|
|
|
|
* Another data structure maintained by this module is an mtime
|
|
|
|
* cache used when the searching of cached directories fails to find
|
|
|
|
* a file. In the past, Dir_FindFile would simply perform an access()
|
|
|
|
* call in such a case to determine if the file could be found using
|
|
|
|
* just the name given. When this hit, however, all that was gained
|
|
|
|
* was the knowledge that the file existed. Given that an access() is
|
|
|
|
* essentially a stat() without the copyout() call, and that the same
|
|
|
|
* filesystem overhead would have to be incurred in Dir_MTime, it made
|
|
|
|
* sense to replace the access() with a stat() and record the mtime
|
|
|
|
* in a cache for when Dir_MTime was actually called.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Lst dirSearchPath; /* main search path */
|
|
|
|
|
|
|
|
static Lst openDirectories; /* the list of all open directories */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Variables for gathering statistics on the efficiency of the hashing
|
|
|
|
* mechanism.
|
|
|
|
*/
|
|
|
|
static int hits, /* Found in directory cache */
|
|
|
|
misses, /* Sad, but not evil misses */
|
|
|
|
nearmisses, /* Found under search path */
|
|
|
|
bigmisses; /* Sought by itself */
|
|
|
|
|
|
|
|
static Path *dot; /* contents of current directory */
|
1997-05-09 01:24:41 +04:00
|
|
|
static Path *cur; /* contents of current directory, if not dot */
|
1999-07-11 06:06:57 +04:00
|
|
|
static Path *dotLast; /* a fake path entry indicating we need to
|
|
|
|
* look for . last */
|
1993-03-21 12:45:37 +03:00
|
|
|
static Hash_Table mtimes; /* Results of doing a last-resort stat in
|
|
|
|
* Dir_FindFile -- if we have to go to the
|
|
|
|
* system to find the file, we might as well
|
|
|
|
* have its mtime on record. XXX: If this is done
|
|
|
|
* way early, there's a chance other rules will
|
|
|
|
* have already updated the file, in which case
|
|
|
|
* we'll update it again. Generally, there won't
|
|
|
|
* be two rules to update a single file, so this
|
|
|
|
* should be ok, but... */
|
|
|
|
|
|
|
|
|
2002-06-15 22:24:55 +04:00
|
|
|
static int DirFindName(ClientData, ClientData);
|
2003-07-14 22:19:11 +04:00
|
|
|
static int DirMatchFiles(const char *, Path *, Lst);
|
|
|
|
static void DirExpandCurly(const char *, const char *, Lst, Lst);
|
|
|
|
static void DirExpandInt(const char *, Lst, Lst);
|
2002-06-15 22:24:55 +04:00
|
|
|
static int DirPrintWord(ClientData, ClientData);
|
|
|
|
static int DirPrintDir(ClientData, ClientData);
|
2003-07-14 22:19:11 +04:00
|
|
|
static char *DirLookup(Path *, const char *, const char *, Boolean);
|
|
|
|
static char *DirLookupSubdir(Path *, const char *);
|
|
|
|
static char *DirFindDot(Boolean, const char *, const char *);
|
|
|
|
static char *DirLookupAbs(Path *, const char *, const char *);
|
1994-03-05 03:34:29 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Dir_Init --
|
|
|
|
* initialize things for this module
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* none
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* some directories may be opened.
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2002-06-15 22:24:55 +04:00
|
|
|
Dir_Init (const char *cdname)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
dirSearchPath = Lst_Init (FALSE);
|
|
|
|
openDirectories = Lst_Init (FALSE);
|
|
|
|
Hash_InitTable(&mtimes, 0);
|
1996-11-06 20:58:58 +03:00
|
|
|
|
2002-11-26 09:12:59 +03:00
|
|
|
Dir_InitCur(cdname);
|
1999-07-11 06:06:57 +04:00
|
|
|
|
|
|
|
dotLast = (Path *) emalloc (sizeof (Path));
|
|
|
|
dotLast->refCount = 1;
|
|
|
|
dotLast->hits = 0;
|
|
|
|
dotLast->name = estrdup(".DOTLAST");
|
|
|
|
Hash_InitTable (&dotLast->files, -1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2002-11-26 09:12:59 +03:00
|
|
|
/*
|
|
|
|
* Called by Dir_Init() and whenever .CURDIR is assigned to.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Dir_InitCur (const char *cdname)
|
|
|
|
{
|
|
|
|
Path *p;
|
|
|
|
|
|
|
|
if (cdname != NULL) {
|
|
|
|
/*
|
|
|
|
* Our build directory is not the same as our source directory.
|
|
|
|
* Keep this one around too.
|
|
|
|
*/
|
|
|
|
if ((p = Dir_AddDir(NULL, cdname))) {
|
|
|
|
p->refCount += 1;
|
|
|
|
if (cur && cur != p) {
|
|
|
|
/*
|
|
|
|
* We've been here before, cleanup.
|
|
|
|
*/
|
|
|
|
cur->refCount -= 1;
|
|
|
|
Dir_Destroy((ClientData) cur);
|
|
|
|
}
|
|
|
|
cur = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-13 00:58:17 +03:00
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Dir_InitDot --
|
|
|
|
* (re)initialize "dot" (current/object directory) path hash
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* none
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* some directories may be opened.
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2002-06-15 22:24:55 +04:00
|
|
|
Dir_InitDot(void)
|
2001-11-13 00:58:17 +03:00
|
|
|
{
|
|
|
|
if (dot != NULL) {
|
|
|
|
LstNode ln;
|
|
|
|
|
|
|
|
/* Remove old entry from openDirectories, but do not destroy. */
|
|
|
|
ln = Lst_Member (openDirectories, (ClientData)dot);
|
|
|
|
(void) Lst_Remove (openDirectories, ln);
|
|
|
|
}
|
|
|
|
|
|
|
|
dot = Dir_AddDir (NULL, ".");
|
|
|
|
|
|
|
|
if (dot == NULL) {
|
|
|
|
Error("Cannot open `.' (%s)", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We always need to have dot around, so we increment its reference count
|
|
|
|
* to make sure it's not destroyed.
|
|
|
|
*/
|
|
|
|
dot->refCount += 1;
|
2002-11-26 09:12:59 +03:00
|
|
|
Dir_SetPATH(); /* initialize */
|
2001-11-13 00:58:17 +03:00
|
|
|
}
|
|
|
|
|
1994-06-07 02:45:17 +04:00
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Dir_End --
|
|
|
|
* cleanup things for this module
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* none
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* none
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2002-06-15 22:24:55 +04:00
|
|
|
Dir_End(void)
|
1994-06-07 02:45:17 +04:00
|
|
|
{
|
1999-09-15 12:43:21 +04:00
|
|
|
#ifdef CLEANUP
|
1997-05-09 01:24:41 +04:00
|
|
|
if (cur) {
|
|
|
|
cur->refCount -= 1;
|
|
|
|
Dir_Destroy((ClientData) cur);
|
|
|
|
}
|
1994-06-07 02:45:17 +04:00
|
|
|
dot->refCount -= 1;
|
1999-07-11 06:06:57 +04:00
|
|
|
dotLast->refCount -= 1;
|
|
|
|
Dir_Destroy((ClientData) dotLast);
|
1994-06-07 02:45:17 +04:00
|
|
|
Dir_Destroy((ClientData) dot);
|
|
|
|
Dir_ClearPath(dirSearchPath);
|
|
|
|
Lst_Destroy(dirSearchPath, NOFREE);
|
|
|
|
Dir_ClearPath(openDirectories);
|
|
|
|
Lst_Destroy(openDirectories, NOFREE);
|
|
|
|
Hash_DeleteTable(&mtimes);
|
1999-09-15 12:43:21 +04:00
|
|
|
#endif
|
1994-06-07 02:45:17 +04:00
|
|
|
}
|
|
|
|
|
2002-11-26 09:12:59 +03:00
|
|
|
/*
|
|
|
|
* We want ${.PATH} to indicate the order in which we will actually
|
|
|
|
* search, so we rebuild it after any .PATH: target.
|
|
|
|
* This is the simplest way to deal with the effect of .DOTLAST.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Dir_SetPATH (void)
|
|
|
|
{
|
|
|
|
LstNode ln; /* a list element */
|
|
|
|
Path *p;
|
|
|
|
Boolean hasLastDot = FALSE; /* true we should search dot last */
|
|
|
|
|
|
|
|
Var_Delete(".PATH", VAR_GLOBAL);
|
|
|
|
|
|
|
|
if (Lst_Open (dirSearchPath) == SUCCESS) {
|
|
|
|
if ((ln = Lst_First (dirSearchPath)) != NILLNODE) {
|
|
|
|
p = (Path *) Lst_Datum (ln);
|
|
|
|
if (p == dotLast) {
|
|
|
|
hasLastDot = TRUE;
|
|
|
|
Var_Append(".PATH", dotLast->name, VAR_GLOBAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasLastDot) {
|
|
|
|
if (dot)
|
|
|
|
Var_Append(".PATH", dot->name, VAR_GLOBAL);
|
|
|
|
if (cur)
|
|
|
|
Var_Append(".PATH", cur->name, VAR_GLOBAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((ln = Lst_Next (dirSearchPath)) != NILLNODE) {
|
|
|
|
p = (Path *) Lst_Datum (ln);
|
|
|
|
if (p == dotLast)
|
|
|
|
continue;
|
|
|
|
if (p == dot && hasLastDot)
|
|
|
|
continue;
|
|
|
|
Var_Append(".PATH", p->name, VAR_GLOBAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasLastDot) {
|
|
|
|
if (dot)
|
|
|
|
Var_Append(".PATH", dot->name, VAR_GLOBAL);
|
|
|
|
if (cur)
|
|
|
|
Var_Append(".PATH", cur->name, VAR_GLOBAL);
|
|
|
|
}
|
|
|
|
Lst_Close(dirSearchPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* DirFindName --
|
|
|
|
* See if the Path structure describes the same directory as the
|
|
|
|
* given one by comparing their names. Called from Dir_AddDir via
|
|
|
|
* Lst_Find when searching the list of open directories.
|
|
|
|
*
|
2002-06-15 22:24:55 +04:00
|
|
|
* Input:
|
|
|
|
* p Current name
|
|
|
|
* dname Desired name
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Results:
|
|
|
|
* 0 if it is the same. Non-zero otherwise
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* None
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static int
|
2002-06-15 22:24:55 +04:00
|
|
|
DirFindName(ClientData p, ClientData dname)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-06-07 02:45:17 +04:00
|
|
|
return (strcmp (((Path *)p)->name, (char *) dname));
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Dir_HasWildcards --
|
|
|
|
* see if the given name has any wildcard characters in it
|
1997-03-27 20:20:18 +03:00
|
|
|
* be careful not to expand unmatching brackets or braces.
|
|
|
|
* XXX: This code is not 100% correct. ([^]] fails etc.)
|
|
|
|
* I really don't think that make(1) should be expanding
|
|
|
|
* patterns, because then you have to set a mechanism for
|
1997-03-29 19:51:26 +03:00
|
|
|
* escaping the expansion!
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
2002-06-15 22:24:55 +04:00
|
|
|
* Input:
|
|
|
|
* name name to check
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Results:
|
|
|
|
* returns TRUE if the word should be expanded, FALSE otherwise
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* none
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
Boolean
|
2002-06-15 22:24:55 +04:00
|
|
|
Dir_HasWildcards(char *name)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2002-06-15 22:24:55 +04:00
|
|
|
char *cp;
|
1997-03-27 20:20:18 +03:00
|
|
|
int wild = 0, brace = 0, bracket = 0;
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
for (cp = name; *cp; cp++) {
|
|
|
|
switch(*cp) {
|
|
|
|
case '{':
|
1997-03-27 20:20:18 +03:00
|
|
|
brace++;
|
|
|
|
wild = 1;
|
|
|
|
break;
|
|
|
|
case '}':
|
|
|
|
brace--;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case '[':
|
1997-03-27 20:20:18 +03:00
|
|
|
bracket++;
|
|
|
|
wild = 1;
|
|
|
|
break;
|
|
|
|
case ']':
|
|
|
|
bracket--;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case '?':
|
|
|
|
case '*':
|
1997-03-27 20:20:18 +03:00
|
|
|
wild = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
1997-03-27 20:20:18 +03:00
|
|
|
return wild && bracket == 0 && brace == 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* DirMatchFiles --
|
|
|
|
* Given a pattern and a Path structure, see if any files
|
|
|
|
* match the pattern and add their names to the 'expansions' list if
|
|
|
|
* any do. This is incomplete -- it doesn't take care of patterns like
|
1994-03-05 03:34:29 +03:00
|
|
|
* src / *src / *.c properly (just *.c on any of the directories), but it
|
1993-03-21 12:45:37 +03:00
|
|
|
* will do for now.
|
|
|
|
*
|
2002-06-15 22:24:55 +04:00
|
|
|
* Input:
|
|
|
|
* pattern Pattern to look for
|
|
|
|
* p Directory to search
|
|
|
|
* expansion Place to store the results
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Results:
|
|
|
|
* Always returns 0
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* File names are added to the expansions lst. The directory will be
|
|
|
|
* fully hashed when this is done.
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static int
|
2003-07-14 22:19:11 +04:00
|
|
|
DirMatchFiles(const char *pattern, Path *p, Lst expansions)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1996-11-06 20:58:58 +03:00
|
|
|
Hash_Search search; /* Index into the directory's table */
|
1993-03-21 12:45:37 +03:00
|
|
|
Hash_Entry *entry; /* Current entry in the table */
|
|
|
|
Boolean isDot; /* TRUE if the directory being searched is . */
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
isDot = (*p->name == '.' && p->name[1] == '\0');
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
for (entry = Hash_EnumFirst(&p->files, &search);
|
|
|
|
entry != (Hash_Entry *)NULL;
|
|
|
|
entry = Hash_EnumNext(&search))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* See if the file matches the given pattern. Note we follow the UNIX
|
|
|
|
* convention that dot files will only be found if the pattern
|
|
|
|
* begins with a dot (note also that as a side effect of the hashing
|
|
|
|
* scheme, .* won't match . or .. since they aren't hashed).
|
|
|
|
*/
|
|
|
|
if (Str_Match(entry->name, pattern) &&
|
|
|
|
((entry->name[0] != '.') ||
|
|
|
|
(pattern[0] == '.')))
|
|
|
|
{
|
|
|
|
(void)Lst_AtEnd(expansions,
|
1996-08-13 20:42:00 +04:00
|
|
|
(isDot ? estrdup(entry->name) :
|
1993-03-21 12:45:37 +03:00
|
|
|
str_concat(p->name, entry->name,
|
|
|
|
STR_ADDSLASH)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* DirExpandCurly --
|
|
|
|
* Expand curly braces like the C shell. Does this recursively.
|
|
|
|
* Note the special case: if after the piece of the curly brace is
|
|
|
|
* done there are no wildcard characters in the result, the result is
|
|
|
|
* placed on the list WITHOUT CHECKING FOR ITS EXISTENCE.
|
|
|
|
*
|
2002-06-15 22:24:55 +04:00
|
|
|
* Input:
|
|
|
|
* word Entire word to expand
|
|
|
|
* brace First curly brace in it
|
|
|
|
* path Search path to use
|
|
|
|
* expansions Place to store the expansions
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Results:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* The given list is filled with the expansions...
|
|
|
|
*
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static void
|
2003-07-14 22:19:11 +04:00
|
|
|
DirExpandCurly(const char *word, const char *brace, Lst path, Lst expansions)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2003-07-14 22:19:11 +04:00
|
|
|
const char *end; /* Character after the closing brace */
|
|
|
|
const char *cp; /* Current position in brace clause */
|
|
|
|
const char *start; /* Start of current piece of brace clause */
|
1993-03-21 12:45:37 +03:00
|
|
|
int bracelevel; /* Number of braces we've seen. If we see a
|
|
|
|
* right brace when this is 0, we've hit the
|
|
|
|
* end of the clause. */
|
2003-07-14 22:19:11 +04:00
|
|
|
char *file; /* Current expansion */
|
1993-03-21 12:45:37 +03:00
|
|
|
int otherLen; /* The length of the other pieces of the
|
|
|
|
* expansion (chars before and after the
|
|
|
|
* clause in 'word') */
|
2003-07-14 22:19:11 +04:00
|
|
|
char *cp2; /* Pointer for checking for wildcards in
|
1993-03-21 12:45:37 +03:00
|
|
|
* expansion before calling Dir_Expand */
|
|
|
|
|
|
|
|
start = brace+1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the end of the brace clause first, being wary of nested brace
|
|
|
|
* clauses.
|
|
|
|
*/
|
|
|
|
for (end = start, bracelevel = 0; *end != '\0'; end++) {
|
|
|
|
if (*end == '{') {
|
|
|
|
bracelevel++;
|
|
|
|
} else if ((*end == '}') && (bracelevel-- == 0)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*end == '\0') {
|
|
|
|
Error("Unterminated {} clause \"%s\"", start);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
otherLen = brace - word + strlen(end);
|
|
|
|
|
|
|
|
for (cp = start; cp < end; cp++) {
|
|
|
|
/*
|
|
|
|
* Find the end of this piece of the clause.
|
|
|
|
*/
|
|
|
|
bracelevel = 0;
|
|
|
|
while (*cp != ',') {
|
|
|
|
if (*cp == '{') {
|
|
|
|
bracelevel++;
|
|
|
|
} else if ((*cp == '}') && (bracelevel-- <= 0)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Allocate room for the combination and install the three pieces.
|
|
|
|
*/
|
|
|
|
file = emalloc(otherLen + cp - start + 1);
|
|
|
|
if (brace != word) {
|
|
|
|
strncpy(file, word, brace-word);
|
|
|
|
}
|
|
|
|
if (cp != start) {
|
|
|
|
strncpy(&file[brace-word], start, cp-start);
|
|
|
|
}
|
|
|
|
strcpy(&file[(brace-word)+(cp-start)], end);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if the result has any wildcards in it. If we find one, call
|
|
|
|
* Dir_Expand right away, telling it to place the result on our list
|
|
|
|
* of expansions.
|
|
|
|
*/
|
|
|
|
for (cp2 = file; *cp2 != '\0'; cp2++) {
|
|
|
|
switch(*cp2) {
|
|
|
|
case '*':
|
|
|
|
case '?':
|
|
|
|
case '{':
|
|
|
|
case '[':
|
|
|
|
Dir_Expand(file, path, expansions);
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*cp2 == '\0') {
|
|
|
|
/*
|
|
|
|
* Hit the end w/o finding any wildcards, so stick the expansion
|
|
|
|
* on the end of the list.
|
|
|
|
*/
|
|
|
|
(void)Lst_AtEnd(expansions, file);
|
|
|
|
} else {
|
|
|
|
next:
|
|
|
|
free(file);
|
|
|
|
}
|
|
|
|
start = cp+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* DirExpandInt --
|
|
|
|
* Internal expand routine. Passes through the directories in the
|
|
|
|
* path one by one, calling DirMatchFiles for each. NOTE: This still
|
|
|
|
* doesn't handle patterns in directories...
|
|
|
|
*
|
2002-06-15 22:24:55 +04:00
|
|
|
* Input:
|
|
|
|
* word Word to expand
|
|
|
|
* path Path on which to look
|
|
|
|
* expansions Place to store the result
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Results:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* Things are added to the expansions list.
|
|
|
|
*
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static void
|
2003-07-14 22:19:11 +04:00
|
|
|
DirExpandInt(const char *word, Lst path, Lst expansions)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
LstNode ln; /* Current node */
|
|
|
|
Path *p; /* Directory in the node */
|
|
|
|
|
|
|
|
if (Lst_Open(path) == SUCCESS) {
|
|
|
|
while ((ln = Lst_Next(path)) != NILLNODE) {
|
|
|
|
p = (Path *)Lst_Datum(ln);
|
|
|
|
DirMatchFiles(word, p, expansions);
|
|
|
|
}
|
|
|
|
Lst_Close(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* DirPrintWord --
|
|
|
|
* Print a word in the list of expansions. Callback for Dir_Expand
|
|
|
|
* when DEBUG(DIR), via Lst_ForEach.
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* === 0
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* The passed word is printed, followed by a space.
|
|
|
|
*
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static int
|
2002-06-15 22:24:55 +04:00
|
|
|
DirPrintWord(ClientData word, ClientData dummy)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-06-07 02:45:17 +04:00
|
|
|
printf("%s ", (char *) word);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-06-07 02:45:17 +04:00
|
|
|
return(dummy ? 0 : 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Dir_Expand --
|
|
|
|
* Expand the given word into a list of words by globbing it looking
|
|
|
|
* in the directories on the given search path.
|
|
|
|
*
|
2002-06-15 22:24:55 +04:00
|
|
|
* Input:
|
|
|
|
* word the word to expand
|
|
|
|
* path the list of directories in which to find the
|
|
|
|
* resulting files
|
|
|
|
* expansions the list on which to place the results
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Results:
|
|
|
|
* A list of words consisting of the files which exist along the search
|
|
|
|
* path matching the given pattern.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* Directories may be opened. Who knows?
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2003-07-14 22:19:11 +04:00
|
|
|
Dir_Expand(const char *word, Lst path, Lst expansions)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2003-07-14 22:19:11 +04:00
|
|
|
const char *cp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("expanding \"%s\"...", word);
|
|
|
|
}
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1994-03-05 03:34:29 +03:00
|
|
|
cp = strchr(word, '{');
|
1993-03-21 12:45:37 +03:00
|
|
|
if (cp) {
|
|
|
|
DirExpandCurly(word, cp, path, expansions);
|
|
|
|
} else {
|
1994-03-05 03:34:29 +03:00
|
|
|
cp = strchr(word, '/');
|
1993-03-21 12:45:37 +03:00
|
|
|
if (cp) {
|
|
|
|
/*
|
|
|
|
* The thing has a directory component -- find the first wildcard
|
|
|
|
* in the string.
|
|
|
|
*/
|
|
|
|
for (cp = word; *cp; cp++) {
|
|
|
|
if (*cp == '?' || *cp == '[' || *cp == '*' || *cp == '{') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*cp == '{') {
|
|
|
|
/*
|
|
|
|
* This one will be fun.
|
|
|
|
*/
|
|
|
|
DirExpandCurly(word, cp, path, expansions);
|
|
|
|
return;
|
|
|
|
} else if (*cp != '\0') {
|
|
|
|
/*
|
|
|
|
* Back up to the start of the component
|
|
|
|
*/
|
|
|
|
char *dirpath;
|
|
|
|
|
|
|
|
while (cp > word && *cp != '/') {
|
|
|
|
cp--;
|
|
|
|
}
|
|
|
|
if (cp != word) {
|
1994-03-05 03:34:29 +03:00
|
|
|
char sc;
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* If the glob isn't in the first component, try and find
|
|
|
|
* all the components up to the one with a wildcard.
|
|
|
|
*/
|
1994-03-05 03:34:29 +03:00
|
|
|
sc = cp[1];
|
2003-07-14 22:19:11 +04:00
|
|
|
((char *)UNCONST(cp))[1] = '\0';
|
1993-03-21 12:45:37 +03:00
|
|
|
dirpath = Dir_FindFile(word, path);
|
2003-07-14 22:19:11 +04:00
|
|
|
((char *)UNCONST(cp))[1] = sc;
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* dirpath is null if can't find the leading component
|
|
|
|
* XXX: Dir_FindFile won't find internal components.
|
|
|
|
* i.e. if the path contains ../Etc/Object and we're
|
|
|
|
* looking for Etc, it won't be found. Ah well.
|
|
|
|
* Probably not important.
|
|
|
|
*/
|
|
|
|
if (dirpath != (char *)NULL) {
|
1994-03-05 03:34:29 +03:00
|
|
|
char *dp = &dirpath[strlen(dirpath) - 1];
|
|
|
|
if (*dp == '/')
|
|
|
|
*dp = '\0';
|
1993-03-21 12:45:37 +03:00
|
|
|
path = Lst_Init(FALSE);
|
1997-05-09 01:24:41 +04:00
|
|
|
(void) Dir_AddDir(path, dirpath);
|
1993-03-21 12:45:37 +03:00
|
|
|
DirExpandInt(cp+1, path, expansions);
|
|
|
|
Lst_Destroy(path, NOFREE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Start the search from the local directory
|
|
|
|
*/
|
|
|
|
DirExpandInt(word, path, expansions);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Return the file -- this should never happen.
|
|
|
|
*/
|
|
|
|
DirExpandInt(word, path, expansions);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* First the files in dot
|
|
|
|
*/
|
|
|
|
DirMatchFiles(word, dot, expansions);
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Then the files in every other directory on the path.
|
|
|
|
*/
|
|
|
|
DirExpandInt(word, path, expansions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (DEBUG(DIR)) {
|
1994-06-07 02:45:17 +04:00
|
|
|
Lst_ForEach(expansions, DirPrintWord, (ClientData) 0);
|
1994-03-05 03:34:29 +03:00
|
|
|
fputc('\n', stdout);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-05-09 21:05:59 +04:00
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* DirLookup --
|
|
|
|
* Find if the file with the given name exists in the given path.
|
|
|
|
*
|
|
|
|
* Results:
|
Fix the bug addressed in revision 1.27 properly. Analysis of the problem
(see also PR#15179):
When looking up names which directory components (i.e. having slashes,
except when of the form `./name'), FindFile()/DirLookup() first looks
the final filename component in the cache for each directory on the search
path and then proceeds to match the prefixed directory components by
comparing them to the trailing directory components of the the search
path being probed.
This is not correct. When looking for `bar/target' in a path `.../src/foo',
you want it to come up with `.../src/foo/bar/target' (if it exists). There's
no point in comparing the the `bar' prefix on the target to the `foo' suffix
on the search path. Indeed, this will cause a false match if those prefix
and suffix components are actually equal and search path itself also has a
file called `target'. For example, looking for `foo/target' in `.../src/foo'
will spuriously match `.../src/foo/target', not `.../src/foo/foo/target'.
This last bug prompted the change in dir.c, rev 1.27, which happens
to partially workaround it by avoiding the above matching code in the
case of the `curdir' search path entry (at the cost of incurring an
exorbitant amount of cache misses). The situation is unchanged however,
when processing other entries on the search path (e.g. those other than
`dot' and `cur').
Drop the prefix matching code in DirLookup() entirely and use DirFindDot()
and DirLookup() only for names without proper directory components (i.e.
`target' and `./target). Otherwise, non-absolute names are dealt with by
DirLookupSubdir(), while absolute names can be checked for an exact match
of the directory components prefix against the directories on the current
search path. This allows for the use of the file cache to check the
existence of the file and additionally, provides a shortcut out of
Dir_FindFile() if we have the prefix match but not a cache entry (this
is especially beneficial for searches in .CURDIR when it's not equal
to `dot').
2002-01-31 15:38:34 +03:00
|
|
|
* The path to the file or NULL. This path is guaranteed to be in a
|
1997-05-09 21:05:59 +04:00
|
|
|
* different part of memory than name and so may be safely free'd.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* None.
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static char *
|
2003-07-14 22:19:11 +04:00
|
|
|
DirLookup(Path *p, const char *name, const char *cp, Boolean hasSlash)
|
1997-05-09 21:05:59 +04:00
|
|
|
{
|
|
|
|
char *file; /* the current filename to check */
|
|
|
|
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("%s...", p->name);
|
|
|
|
}
|
Fix the bug addressed in revision 1.27 properly. Analysis of the problem
(see also PR#15179):
When looking up names which directory components (i.e. having slashes,
except when of the form `./name'), FindFile()/DirLookup() first looks
the final filename component in the cache for each directory on the search
path and then proceeds to match the prefixed directory components by
comparing them to the trailing directory components of the the search
path being probed.
This is not correct. When looking for `bar/target' in a path `.../src/foo',
you want it to come up with `.../src/foo/bar/target' (if it exists). There's
no point in comparing the the `bar' prefix on the target to the `foo' suffix
on the search path. Indeed, this will cause a false match if those prefix
and suffix components are actually equal and search path itself also has a
file called `target'. For example, looking for `foo/target' in `.../src/foo'
will spuriously match `.../src/foo/target', not `.../src/foo/foo/target'.
This last bug prompted the change in dir.c, rev 1.27, which happens
to partially workaround it by avoiding the above matching code in the
case of the `curdir' search path entry (at the cost of incurring an
exorbitant amount of cache misses). The situation is unchanged however,
when processing other entries on the search path (e.g. those other than
`dot' and `cur').
Drop the prefix matching code in DirLookup() entirely and use DirFindDot()
and DirLookup() only for names without proper directory components (i.e.
`target' and `./target). Otherwise, non-absolute names are dealt with by
DirLookupSubdir(), while absolute names can be checked for an exact match
of the directory components prefix against the directories on the current
search path. This allows for the use of the file cache to check the
existence of the file and additionally, provides a shortcut out of
Dir_FindFile() if we have the prefix match but not a cache entry (this
is especially beneficial for searches in .CURDIR when it's not equal
to `dot').
2002-01-31 15:38:34 +03:00
|
|
|
|
|
|
|
if (Hash_FindEntry (&p->files, cp) == (Hash_Entry *)NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("here...");
|
1997-05-09 21:05:59 +04:00
|
|
|
}
|
Fix the bug addressed in revision 1.27 properly. Analysis of the problem
(see also PR#15179):
When looking up names which directory components (i.e. having slashes,
except when of the form `./name'), FindFile()/DirLookup() first looks
the final filename component in the cache for each directory on the search
path and then proceeds to match the prefixed directory components by
comparing them to the trailing directory components of the the search
path being probed.
This is not correct. When looking for `bar/target' in a path `.../src/foo',
you want it to come up with `.../src/foo/bar/target' (if it exists). There's
no point in comparing the the `bar' prefix on the target to the `foo' suffix
on the search path. Indeed, this will cause a false match if those prefix
and suffix components are actually equal and search path itself also has a
file called `target'. For example, looking for `foo/target' in `.../src/foo'
will spuriously match `.../src/foo/target', not `.../src/foo/foo/target'.
This last bug prompted the change in dir.c, rev 1.27, which happens
to partially workaround it by avoiding the above matching code in the
case of the `curdir' search path entry (at the cost of incurring an
exorbitant amount of cache misses). The situation is unchanged however,
when processing other entries on the search path (e.g. those other than
`dot' and `cur').
Drop the prefix matching code in DirLookup() entirely and use DirFindDot()
and DirLookup() only for names without proper directory components (i.e.
`target' and `./target). Otherwise, non-absolute names are dealt with by
DirLookupSubdir(), while absolute names can be checked for an exact match
of the directory components prefix against the directories on the current
search path. This allows for the use of the file cache to check the
existence of the file and additionally, provides a shortcut out of
Dir_FindFile() if we have the prefix match but not a cache entry (this
is especially beneficial for searches in .CURDIR when it's not equal
to `dot').
2002-01-31 15:38:34 +03:00
|
|
|
file = str_concat (p->name, cp, STR_ADDSLASH);
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("returning %s\n", file);
|
|
|
|
}
|
|
|
|
p->hits += 1;
|
|
|
|
hits += 1;
|
|
|
|
return file;
|
1997-05-09 21:05:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* DirLookupSubdir --
|
|
|
|
* Find if the file with the given name exists in the given path.
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* The path to the file or NULL. This path is guaranteed to be in a
|
|
|
|
* different part of memory than name and so may be safely free'd.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* If the file is found, it is added in the modification times hash
|
|
|
|
* table.
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static char *
|
2003-07-14 22:19:11 +04:00
|
|
|
DirLookupSubdir(Path *p, const char *name)
|
1997-05-09 21:05:59 +04:00
|
|
|
{
|
|
|
|
struct stat stb; /* Buffer for stat, if necessary */
|
|
|
|
Hash_Entry *entry; /* Entry for mtimes table */
|
|
|
|
char *file; /* the current filename to check */
|
|
|
|
|
|
|
|
if (p != dot) {
|
|
|
|
file = str_concat (p->name, name, STR_ADDSLASH);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Checking in dot -- DON'T put a leading ./ on the thing.
|
|
|
|
*/
|
|
|
|
file = estrdup(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("checking %s...", file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat (file, &stb) == 0) {
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("got it.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save the modification time so if it's needed, we don't have
|
|
|
|
* to fetch it again.
|
|
|
|
*/
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime),
|
|
|
|
file);
|
|
|
|
}
|
|
|
|
entry = Hash_CreateEntry(&mtimes, (char *) file,
|
|
|
|
(Boolean *)NULL);
|
|
|
|
Hash_SetValue(entry, (long)stb.st_mtime);
|
|
|
|
nearmisses += 1;
|
|
|
|
return (file);
|
|
|
|
}
|
|
|
|
free (file);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
Fix the bug addressed in revision 1.27 properly. Analysis of the problem
(see also PR#15179):
When looking up names which directory components (i.e. having slashes,
except when of the form `./name'), FindFile()/DirLookup() first looks
the final filename component in the cache for each directory on the search
path and then proceeds to match the prefixed directory components by
comparing them to the trailing directory components of the the search
path being probed.
This is not correct. When looking for `bar/target' in a path `.../src/foo',
you want it to come up with `.../src/foo/bar/target' (if it exists). There's
no point in comparing the the `bar' prefix on the target to the `foo' suffix
on the search path. Indeed, this will cause a false match if those prefix
and suffix components are actually equal and search path itself also has a
file called `target'. For example, looking for `foo/target' in `.../src/foo'
will spuriously match `.../src/foo/target', not `.../src/foo/foo/target'.
This last bug prompted the change in dir.c, rev 1.27, which happens
to partially workaround it by avoiding the above matching code in the
case of the `curdir' search path entry (at the cost of incurring an
exorbitant amount of cache misses). The situation is unchanged however,
when processing other entries on the search path (e.g. those other than
`dot' and `cur').
Drop the prefix matching code in DirLookup() entirely and use DirFindDot()
and DirLookup() only for names without proper directory components (i.e.
`target' and `./target). Otherwise, non-absolute names are dealt with by
DirLookupSubdir(), while absolute names can be checked for an exact match
of the directory components prefix against the directories on the current
search path. This allows for the use of the file cache to check the
existence of the file and additionally, provides a shortcut out of
Dir_FindFile() if we have the prefix match but not a cache entry (this
is especially beneficial for searches in .CURDIR when it's not equal
to `dot').
2002-01-31 15:38:34 +03:00
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* DirLookupAbs --
|
|
|
|
* Find if the file with the given name exists in the given path.
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* The path to the file, the empty string or NULL. If the file is
|
|
|
|
* the empty string, the search should be terminated.
|
|
|
|
* This path is guaranteed to be in a different part of memory
|
|
|
|
* than name and so may be safely free'd.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* None.
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static char *
|
2003-07-14 22:19:11 +04:00
|
|
|
DirLookupAbs(Path *p, const char *name, const char *cp)
|
Fix the bug addressed in revision 1.27 properly. Analysis of the problem
(see also PR#15179):
When looking up names which directory components (i.e. having slashes,
except when of the form `./name'), FindFile()/DirLookup() first looks
the final filename component in the cache for each directory on the search
path and then proceeds to match the prefixed directory components by
comparing them to the trailing directory components of the the search
path being probed.
This is not correct. When looking for `bar/target' in a path `.../src/foo',
you want it to come up with `.../src/foo/bar/target' (if it exists). There's
no point in comparing the the `bar' prefix on the target to the `foo' suffix
on the search path. Indeed, this will cause a false match if those prefix
and suffix components are actually equal and search path itself also has a
file called `target'. For example, looking for `foo/target' in `.../src/foo'
will spuriously match `.../src/foo/target', not `.../src/foo/foo/target'.
This last bug prompted the change in dir.c, rev 1.27, which happens
to partially workaround it by avoiding the above matching code in the
case of the `curdir' search path entry (at the cost of incurring an
exorbitant amount of cache misses). The situation is unchanged however,
when processing other entries on the search path (e.g. those other than
`dot' and `cur').
Drop the prefix matching code in DirLookup() entirely and use DirFindDot()
and DirLookup() only for names without proper directory components (i.e.
`target' and `./target). Otherwise, non-absolute names are dealt with by
DirLookupSubdir(), while absolute names can be checked for an exact match
of the directory components prefix against the directories on the current
search path. This allows for the use of the file cache to check the
existence of the file and additionally, provides a shortcut out of
Dir_FindFile() if we have the prefix match but not a cache entry (this
is especially beneficial for searches in .CURDIR when it's not equal
to `dot').
2002-01-31 15:38:34 +03:00
|
|
|
{
|
|
|
|
char *p1; /* pointer into p->name */
|
2003-07-14 22:19:11 +04:00
|
|
|
const char *p2; /* pointer into name */
|
Fix the bug addressed in revision 1.27 properly. Analysis of the problem
(see also PR#15179):
When looking up names which directory components (i.e. having slashes,
except when of the form `./name'), FindFile()/DirLookup() first looks
the final filename component in the cache for each directory on the search
path and then proceeds to match the prefixed directory components by
comparing them to the trailing directory components of the the search
path being probed.
This is not correct. When looking for `bar/target' in a path `.../src/foo',
you want it to come up with `.../src/foo/bar/target' (if it exists). There's
no point in comparing the the `bar' prefix on the target to the `foo' suffix
on the search path. Indeed, this will cause a false match if those prefix
and suffix components are actually equal and search path itself also has a
file called `target'. For example, looking for `foo/target' in `.../src/foo'
will spuriously match `.../src/foo/target', not `.../src/foo/foo/target'.
This last bug prompted the change in dir.c, rev 1.27, which happens
to partially workaround it by avoiding the above matching code in the
case of the `curdir' search path entry (at the cost of incurring an
exorbitant amount of cache misses). The situation is unchanged however,
when processing other entries on the search path (e.g. those other than
`dot' and `cur').
Drop the prefix matching code in DirLookup() entirely and use DirFindDot()
and DirLookup() only for names without proper directory components (i.e.
`target' and `./target). Otherwise, non-absolute names are dealt with by
DirLookupSubdir(), while absolute names can be checked for an exact match
of the directory components prefix against the directories on the current
search path. This allows for the use of the file cache to check the
existence of the file and additionally, provides a shortcut out of
Dir_FindFile() if we have the prefix match but not a cache entry (this
is especially beneficial for searches in .CURDIR when it's not equal
to `dot').
2002-01-31 15:38:34 +03:00
|
|
|
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("%s...", p->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the file has a leading path component and that component
|
|
|
|
* exactly matches the entire name of the current search
|
|
|
|
* directory, we can attempt another cache lookup. And if we don't
|
|
|
|
* have a hit, we can safely assume the file does not exist at all.
|
|
|
|
*/
|
|
|
|
for (p1 = p->name, p2 = name; *p1 && *p1 == *p2; p1++, p2++) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*p1 != '\0' || p2 != cp - 1) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Hash_FindEntry (&p->files, cp) == (Hash_Entry *)NULL) {
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("must be here but isn't -- returning\n");
|
|
|
|
}
|
|
|
|
/* Return empty string: terminates search */
|
2003-07-14 22:19:11 +04:00
|
|
|
return estrdup("");
|
Fix the bug addressed in revision 1.27 properly. Analysis of the problem
(see also PR#15179):
When looking up names which directory components (i.e. having slashes,
except when of the form `./name'), FindFile()/DirLookup() first looks
the final filename component in the cache for each directory on the search
path and then proceeds to match the prefixed directory components by
comparing them to the trailing directory components of the the search
path being probed.
This is not correct. When looking for `bar/target' in a path `.../src/foo',
you want it to come up with `.../src/foo/bar/target' (if it exists). There's
no point in comparing the the `bar' prefix on the target to the `foo' suffix
on the search path. Indeed, this will cause a false match if those prefix
and suffix components are actually equal and search path itself also has a
file called `target'. For example, looking for `foo/target' in `.../src/foo'
will spuriously match `.../src/foo/target', not `.../src/foo/foo/target'.
This last bug prompted the change in dir.c, rev 1.27, which happens
to partially workaround it by avoiding the above matching code in the
case of the `curdir' search path entry (at the cost of incurring an
exorbitant amount of cache misses). The situation is unchanged however,
when processing other entries on the search path (e.g. those other than
`dot' and `cur').
Drop the prefix matching code in DirLookup() entirely and use DirFindDot()
and DirLookup() only for names without proper directory components (i.e.
`target' and `./target). Otherwise, non-absolute names are dealt with by
DirLookupSubdir(), while absolute names can be checked for an exact match
of the directory components prefix against the directories on the current
search path. This allows for the use of the file cache to check the
existence of the file and additionally, provides a shortcut out of
Dir_FindFile() if we have the prefix match but not a cache entry (this
is especially beneficial for searches in .CURDIR when it's not equal
to `dot').
2002-01-31 15:38:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("here...");
|
|
|
|
}
|
|
|
|
p->hits += 1;
|
|
|
|
hits += 1;
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("returning %s\n", name);
|
|
|
|
}
|
|
|
|
return (estrdup (name));
|
|
|
|
}
|
|
|
|
|
1999-07-11 06:06:57 +04:00
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* DirFindDot --
|
|
|
|
* Find the file given on "." or curdir
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* The path to the file or NULL. This path is guaranteed to be in a
|
|
|
|
* different part of memory than name and so may be safely free'd.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* Hit counts change
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static char *
|
2003-07-14 22:19:11 +04:00
|
|
|
DirFindDot(Boolean hasSlash, const char *name, const char *cp)
|
1999-07-11 06:06:57 +04:00
|
|
|
{
|
|
|
|
|
|
|
|
if (Hash_FindEntry (&dot->files, cp) != (Hash_Entry *)NULL) {
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("in '.'\n");
|
|
|
|
}
|
|
|
|
hits += 1;
|
|
|
|
dot->hits += 1;
|
|
|
|
return (estrdup (name));
|
|
|
|
}
|
|
|
|
if (cur &&
|
|
|
|
Hash_FindEntry (&cur->files, cp) != (Hash_Entry *)NULL) {
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("in ${.CURDIR} = %s\n", cur->name);
|
|
|
|
}
|
|
|
|
hits += 1;
|
|
|
|
cur->hits += 1;
|
|
|
|
return str_concat (cur->name, cp, STR_ADDSLASH);
|
|
|
|
}
|
|
|
|
|
Fix the bug addressed in revision 1.27 properly. Analysis of the problem
(see also PR#15179):
When looking up names which directory components (i.e. having slashes,
except when of the form `./name'), FindFile()/DirLookup() first looks
the final filename component in the cache for each directory on the search
path and then proceeds to match the prefixed directory components by
comparing them to the trailing directory components of the the search
path being probed.
This is not correct. When looking for `bar/target' in a path `.../src/foo',
you want it to come up with `.../src/foo/bar/target' (if it exists). There's
no point in comparing the the `bar' prefix on the target to the `foo' suffix
on the search path. Indeed, this will cause a false match if those prefix
and suffix components are actually equal and search path itself also has a
file called `target'. For example, looking for `foo/target' in `.../src/foo'
will spuriously match `.../src/foo/target', not `.../src/foo/foo/target'.
This last bug prompted the change in dir.c, rev 1.27, which happens
to partially workaround it by avoiding the above matching code in the
case of the `curdir' search path entry (at the cost of incurring an
exorbitant amount of cache misses). The situation is unchanged however,
when processing other entries on the search path (e.g. those other than
`dot' and `cur').
Drop the prefix matching code in DirLookup() entirely and use DirFindDot()
and DirLookup() only for names without proper directory components (i.e.
`target' and `./target). Otherwise, non-absolute names are dealt with by
DirLookupSubdir(), while absolute names can be checked for an exact match
of the directory components prefix against the directories on the current
search path. This allows for the use of the file cache to check the
existence of the file and additionally, provides a shortcut out of
Dir_FindFile() if we have the prefix match but not a cache entry (this
is especially beneficial for searches in .CURDIR when it's not equal
to `dot').
2002-01-31 15:38:34 +03:00
|
|
|
return NULL;
|
1999-07-11 06:06:57 +04:00
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Dir_FindFile --
|
|
|
|
* Find the file with the given name along the given search path.
|
|
|
|
*
|
2002-06-15 22:24:55 +04:00
|
|
|
* Input:
|
|
|
|
* name the file to find
|
|
|
|
* path the Lst of directories to search
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Results:
|
|
|
|
* The path to the file or NULL. This path is guaranteed to be in a
|
|
|
|
* different part of memory than name and so may be safely free'd.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* If the file is found in a directory which is not on the path
|
|
|
|
* already (either 'name' is absolute or it is a relative path
|
|
|
|
* [ dir1/.../dirn/file ] which exists below one of the directories
|
|
|
|
* already on the search path), its directory is added to the end
|
|
|
|
* of the path on the assumption that there will be more files in
|
|
|
|
* that directory later on. Sometimes this is true. Sometimes not.
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
char *
|
2003-07-14 22:19:11 +04:00
|
|
|
Dir_FindFile(const char *name, Lst path)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1999-07-11 06:06:57 +04:00
|
|
|
LstNode ln; /* a list element */
|
2002-06-15 22:24:55 +04:00
|
|
|
char *file; /* the current filename to check */
|
|
|
|
Path *p; /* current path member */
|
2003-07-14 22:19:11 +04:00
|
|
|
const char *cp; /* index of first slash, if any */
|
2002-01-27 04:50:54 +03:00
|
|
|
Boolean hasLastDot = FALSE; /* true we should search dot last */
|
1999-07-11 06:06:57 +04:00
|
|
|
Boolean hasSlash; /* true if 'name' contains a / */
|
|
|
|
struct stat stb; /* Buffer for stat, if necessary */
|
|
|
|
Hash_Entry *entry; /* Entry for mtimes table */
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Find the final component of the name and note whether it has a
|
|
|
|
* slash in it (the name, I mean)
|
|
|
|
*/
|
1994-03-05 03:34:29 +03:00
|
|
|
cp = strrchr (name, '/');
|
1993-03-21 12:45:37 +03:00
|
|
|
if (cp) {
|
|
|
|
hasSlash = TRUE;
|
|
|
|
cp += 1;
|
|
|
|
} else {
|
|
|
|
hasSlash = FALSE;
|
|
|
|
cp = name;
|
|
|
|
}
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("Searching for %s...", name);
|
|
|
|
}
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (Lst_Open (path) == FAILURE) {
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("couldn't open path, file not found\n");
|
|
|
|
}
|
|
|
|
misses += 1;
|
|
|
|
return ((char *) NULL);
|
|
|
|
}
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1999-07-11 06:06:57 +04:00
|
|
|
if ((ln = Lst_First (path)) != NILLNODE) {
|
|
|
|
p = (Path *) Lst_Datum (ln);
|
2002-01-27 04:50:54 +03:00
|
|
|
if (p == dotLast) {
|
|
|
|
hasLastDot = TRUE;
|
Fix the bug addressed in revision 1.27 properly. Analysis of the problem
(see also PR#15179):
When looking up names which directory components (i.e. having slashes,
except when of the form `./name'), FindFile()/DirLookup() first looks
the final filename component in the cache for each directory on the search
path and then proceeds to match the prefixed directory components by
comparing them to the trailing directory components of the the search
path being probed.
This is not correct. When looking for `bar/target' in a path `.../src/foo',
you want it to come up with `.../src/foo/bar/target' (if it exists). There's
no point in comparing the the `bar' prefix on the target to the `foo' suffix
on the search path. Indeed, this will cause a false match if those prefix
and suffix components are actually equal and search path itself also has a
file called `target'. For example, looking for `foo/target' in `.../src/foo'
will spuriously match `.../src/foo/target', not `.../src/foo/foo/target'.
This last bug prompted the change in dir.c, rev 1.27, which happens
to partially workaround it by avoiding the above matching code in the
case of the `curdir' search path entry (at the cost of incurring an
exorbitant amount of cache misses). The situation is unchanged however,
when processing other entries on the search path (e.g. those other than
`dot' and `cur').
Drop the prefix matching code in DirLookup() entirely and use DirFindDot()
and DirLookup() only for names without proper directory components (i.e.
`target' and `./target). Otherwise, non-absolute names are dealt with by
DirLookupSubdir(), while absolute names can be checked for an exact match
of the directory components prefix against the directories on the current
search path. This allows for the use of the file cache to check the
existence of the file and additionally, provides a shortcut out of
Dir_FindFile() if we have the prefix match but not a cache entry (this
is especially beneficial for searches in .CURDIR when it's not equal
to `dot').
2002-01-31 15:38:34 +03:00
|
|
|
if (DEBUG(DIR))
|
|
|
|
printf("[dot last]...");
|
1999-07-11 06:06:57 +04:00
|
|
|
}
|
1997-05-09 21:05:59 +04:00
|
|
|
}
|
|
|
|
|
1999-07-11 06:06:57 +04:00
|
|
|
/*
|
Fix the bug addressed in revision 1.27 properly. Analysis of the problem
(see also PR#15179):
When looking up names which directory components (i.e. having slashes,
except when of the form `./name'), FindFile()/DirLookup() first looks
the final filename component in the cache for each directory on the search
path and then proceeds to match the prefixed directory components by
comparing them to the trailing directory components of the the search
path being probed.
This is not correct. When looking for `bar/target' in a path `.../src/foo',
you want it to come up with `.../src/foo/bar/target' (if it exists). There's
no point in comparing the the `bar' prefix on the target to the `foo' suffix
on the search path. Indeed, this will cause a false match if those prefix
and suffix components are actually equal and search path itself also has a
file called `target'. For example, looking for `foo/target' in `.../src/foo'
will spuriously match `.../src/foo/target', not `.../src/foo/foo/target'.
This last bug prompted the change in dir.c, rev 1.27, which happens
to partially workaround it by avoiding the above matching code in the
case of the `curdir' search path entry (at the cost of incurring an
exorbitant amount of cache misses). The situation is unchanged however,
when processing other entries on the search path (e.g. those other than
`dot' and `cur').
Drop the prefix matching code in DirLookup() entirely and use DirFindDot()
and DirLookup() only for names without proper directory components (i.e.
`target' and `./target). Otherwise, non-absolute names are dealt with by
DirLookupSubdir(), while absolute names can be checked for an exact match
of the directory components prefix against the directories on the current
search path. This allows for the use of the file cache to check the
existence of the file and additionally, provides a shortcut out of
Dir_FindFile() if we have the prefix match but not a cache entry (this
is especially beneficial for searches in .CURDIR when it's not equal
to `dot').
2002-01-31 15:38:34 +03:00
|
|
|
* If there's no leading directory components or if the leading
|
|
|
|
* directory component is exactly `./', consult the cached contents
|
|
|
|
* of each of the directories on the search path.
|
1999-07-11 06:06:57 +04:00
|
|
|
*/
|
Fix the bug addressed in revision 1.27 properly. Analysis of the problem
(see also PR#15179):
When looking up names which directory components (i.e. having slashes,
except when of the form `./name'), FindFile()/DirLookup() first looks
the final filename component in the cache for each directory on the search
path and then proceeds to match the prefixed directory components by
comparing them to the trailing directory components of the the search
path being probed.
This is not correct. When looking for `bar/target' in a path `.../src/foo',
you want it to come up with `.../src/foo/bar/target' (if it exists). There's
no point in comparing the the `bar' prefix on the target to the `foo' suffix
on the search path. Indeed, this will cause a false match if those prefix
and suffix components are actually equal and search path itself also has a
file called `target'. For example, looking for `foo/target' in `.../src/foo'
will spuriously match `.../src/foo/target', not `.../src/foo/foo/target'.
This last bug prompted the change in dir.c, rev 1.27, which happens
to partially workaround it by avoiding the above matching code in the
case of the `curdir' search path entry (at the cost of incurring an
exorbitant amount of cache misses). The situation is unchanged however,
when processing other entries on the search path (e.g. those other than
`dot' and `cur').
Drop the prefix matching code in DirLookup() entirely and use DirFindDot()
and DirLookup() only for names without proper directory components (i.e.
`target' and `./target). Otherwise, non-absolute names are dealt with by
DirLookupSubdir(), while absolute names can be checked for an exact match
of the directory components prefix against the directories on the current
search path. This allows for the use of the file cache to check the
existence of the file and additionally, provides a shortcut out of
Dir_FindFile() if we have the prefix match but not a cache entry (this
is especially beneficial for searches in .CURDIR when it's not equal
to `dot').
2002-01-31 15:38:34 +03:00
|
|
|
if ((!hasSlash || (cp - name == 2 && *name == '.'))) {
|
|
|
|
/*
|
|
|
|
* We look through all the directories on the path seeking one which
|
|
|
|
* contains the final component of the given name. If such a beast
|
|
|
|
* is found, we concatenate the directory name and the final
|
|
|
|
* component and return the resulting string. If we don't find any
|
|
|
|
* such thing, we go on to phase two...
|
|
|
|
*
|
|
|
|
* No matter what, we always look for the file in the current
|
|
|
|
* directory before anywhere else (unless we found the magic
|
|
|
|
* DOTLAST path, in which case we search it last) and we *do not*
|
|
|
|
* add the ./ to it if it exists.
|
|
|
|
* This is so there are no conflicts between what the user
|
|
|
|
* specifies (fish.c) and what pmake finds (./fish.c).
|
|
|
|
*/
|
|
|
|
if (!hasLastDot &&
|
|
|
|
(file = DirFindDot(hasSlash, name, cp)) != NULL) {
|
|
|
|
Lst_Close (path);
|
|
|
|
return file;
|
|
|
|
}
|
1999-07-11 06:06:57 +04:00
|
|
|
|
Fix the bug addressed in revision 1.27 properly. Analysis of the problem
(see also PR#15179):
When looking up names which directory components (i.e. having slashes,
except when of the form `./name'), FindFile()/DirLookup() first looks
the final filename component in the cache for each directory on the search
path and then proceeds to match the prefixed directory components by
comparing them to the trailing directory components of the the search
path being probed.
This is not correct. When looking for `bar/target' in a path `.../src/foo',
you want it to come up with `.../src/foo/bar/target' (if it exists). There's
no point in comparing the the `bar' prefix on the target to the `foo' suffix
on the search path. Indeed, this will cause a false match if those prefix
and suffix components are actually equal and search path itself also has a
file called `target'. For example, looking for `foo/target' in `.../src/foo'
will spuriously match `.../src/foo/target', not `.../src/foo/foo/target'.
This last bug prompted the change in dir.c, rev 1.27, which happens
to partially workaround it by avoiding the above matching code in the
case of the `curdir' search path entry (at the cost of incurring an
exorbitant amount of cache misses). The situation is unchanged however,
when processing other entries on the search path (e.g. those other than
`dot' and `cur').
Drop the prefix matching code in DirLookup() entirely and use DirFindDot()
and DirLookup() only for names without proper directory components (i.e.
`target' and `./target). Otherwise, non-absolute names are dealt with by
DirLookupSubdir(), while absolute names can be checked for an exact match
of the directory components prefix against the directories on the current
search path. This allows for the use of the file cache to check the
existence of the file and additionally, provides a shortcut out of
Dir_FindFile() if we have the prefix match but not a cache entry (this
is especially beneficial for searches in .CURDIR when it's not equal
to `dot').
2002-01-31 15:38:34 +03:00
|
|
|
while ((ln = Lst_Next (path)) != NILLNODE) {
|
|
|
|
p = (Path *) Lst_Datum (ln);
|
|
|
|
if (p == dotLast)
|
|
|
|
continue;
|
|
|
|
if ((file = DirLookup(p, name, cp, hasSlash)) != NULL) {
|
|
|
|
Lst_Close (path);
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasLastDot &&
|
|
|
|
(file = DirFindDot(hasSlash, name, cp)) != NULL) {
|
|
|
|
Lst_Close (path);
|
|
|
|
return file;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2002-01-18 22:18:23 +03:00
|
|
|
Lst_Close (path);
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
Fix the bug addressed in revision 1.27 properly. Analysis of the problem
(see also PR#15179):
When looking up names which directory components (i.e. having slashes,
except when of the form `./name'), FindFile()/DirLookup() first looks
the final filename component in the cache for each directory on the search
path and then proceeds to match the prefixed directory components by
comparing them to the trailing directory components of the the search
path being probed.
This is not correct. When looking for `bar/target' in a path `.../src/foo',
you want it to come up with `.../src/foo/bar/target' (if it exists). There's
no point in comparing the the `bar' prefix on the target to the `foo' suffix
on the search path. Indeed, this will cause a false match if those prefix
and suffix components are actually equal and search path itself also has a
file called `target'. For example, looking for `foo/target' in `.../src/foo'
will spuriously match `.../src/foo/target', not `.../src/foo/foo/target'.
This last bug prompted the change in dir.c, rev 1.27, which happens
to partially workaround it by avoiding the above matching code in the
case of the `curdir' search path entry (at the cost of incurring an
exorbitant amount of cache misses). The situation is unchanged however,
when processing other entries on the search path (e.g. those other than
`dot' and `cur').
Drop the prefix matching code in DirLookup() entirely and use DirFindDot()
and DirLookup() only for names without proper directory components (i.e.
`target' and `./target). Otherwise, non-absolute names are dealt with by
DirLookupSubdir(), while absolute names can be checked for an exact match
of the directory components prefix against the directories on the current
search path. This allows for the use of the file cache to check the
existence of the file and additionally, provides a shortcut out of
Dir_FindFile() if we have the prefix match but not a cache entry (this
is especially beneficial for searches in .CURDIR when it's not equal
to `dot').
2002-01-31 15:38:34 +03:00
|
|
|
* We didn't find the file on any directory in the search path.
|
1993-03-21 12:45:37 +03:00
|
|
|
* If the name doesn't contain a slash, that means it doesn't exist.
|
|
|
|
* If it *does* contain a slash, however, there is still hope: it
|
|
|
|
* could be in a subdirectory of one of the members of the search
|
|
|
|
* path. (eg. /usr/include and sys/types.h. The above search would
|
|
|
|
* fail to turn up types.h in /usr/include, but it *is* in
|
Fix the bug addressed in revision 1.27 properly. Analysis of the problem
(see also PR#15179):
When looking up names which directory components (i.e. having slashes,
except when of the form `./name'), FindFile()/DirLookup() first looks
the final filename component in the cache for each directory on the search
path and then proceeds to match the prefixed directory components by
comparing them to the trailing directory components of the the search
path being probed.
This is not correct. When looking for `bar/target' in a path `.../src/foo',
you want it to come up with `.../src/foo/bar/target' (if it exists). There's
no point in comparing the the `bar' prefix on the target to the `foo' suffix
on the search path. Indeed, this will cause a false match if those prefix
and suffix components are actually equal and search path itself also has a
file called `target'. For example, looking for `foo/target' in `.../src/foo'
will spuriously match `.../src/foo/target', not `.../src/foo/foo/target'.
This last bug prompted the change in dir.c, rev 1.27, which happens
to partially workaround it by avoiding the above matching code in the
case of the `curdir' search path entry (at the cost of incurring an
exorbitant amount of cache misses). The situation is unchanged however,
when processing other entries on the search path (e.g. those other than
`dot' and `cur').
Drop the prefix matching code in DirLookup() entirely and use DirFindDot()
and DirLookup() only for names without proper directory components (i.e.
`target' and `./target). Otherwise, non-absolute names are dealt with by
DirLookupSubdir(), while absolute names can be checked for an exact match
of the directory components prefix against the directories on the current
search path. This allows for the use of the file cache to check the
existence of the file and additionally, provides a shortcut out of
Dir_FindFile() if we have the prefix match but not a cache entry (this
is especially beneficial for searches in .CURDIR when it's not equal
to `dot').
2002-01-31 15:38:34 +03:00
|
|
|
* /usr/include/sys/types.h).
|
|
|
|
* [ This no longer applies: If we find such a beast, we assume there
|
1993-03-21 12:45:37 +03:00
|
|
|
* will be more (what else can we assume?) and add all but the last
|
|
|
|
* component of the resulting name onto the search path (at the
|
Fix the bug addressed in revision 1.27 properly. Analysis of the problem
(see also PR#15179):
When looking up names which directory components (i.e. having slashes,
except when of the form `./name'), FindFile()/DirLookup() first looks
the final filename component in the cache for each directory on the search
path and then proceeds to match the prefixed directory components by
comparing them to the trailing directory components of the the search
path being probed.
This is not correct. When looking for `bar/target' in a path `.../src/foo',
you want it to come up with `.../src/foo/bar/target' (if it exists). There's
no point in comparing the the `bar' prefix on the target to the `foo' suffix
on the search path. Indeed, this will cause a false match if those prefix
and suffix components are actually equal and search path itself also has a
file called `target'. For example, looking for `foo/target' in `.../src/foo'
will spuriously match `.../src/foo/target', not `.../src/foo/foo/target'.
This last bug prompted the change in dir.c, rev 1.27, which happens
to partially workaround it by avoiding the above matching code in the
case of the `curdir' search path entry (at the cost of incurring an
exorbitant amount of cache misses). The situation is unchanged however,
when processing other entries on the search path (e.g. those other than
`dot' and `cur').
Drop the prefix matching code in DirLookup() entirely and use DirFindDot()
and DirLookup() only for names without proper directory components (i.e.
`target' and `./target). Otherwise, non-absolute names are dealt with by
DirLookupSubdir(), while absolute names can be checked for an exact match
of the directory components prefix against the directories on the current
search path. This allows for the use of the file cache to check the
existence of the file and additionally, provides a shortcut out of
Dir_FindFile() if we have the prefix match but not a cache entry (this
is especially beneficial for searches in .CURDIR when it's not equal
to `dot').
2002-01-31 15:38:34 +03:00
|
|
|
* end).]
|
|
|
|
* This phase is only performed if the file is *not* absolute.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
if (!hasSlash) {
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("failed.\n");
|
|
|
|
}
|
|
|
|
misses += 1;
|
|
|
|
return ((char *) NULL);
|
|
|
|
}
|
1996-11-06 20:58:58 +03:00
|
|
|
|
Fix the bug addressed in revision 1.27 properly. Analysis of the problem
(see also PR#15179):
When looking up names which directory components (i.e. having slashes,
except when of the form `./name'), FindFile()/DirLookup() first looks
the final filename component in the cache for each directory on the search
path and then proceeds to match the prefixed directory components by
comparing them to the trailing directory components of the the search
path being probed.
This is not correct. When looking for `bar/target' in a path `.../src/foo',
you want it to come up with `.../src/foo/bar/target' (if it exists). There's
no point in comparing the the `bar' prefix on the target to the `foo' suffix
on the search path. Indeed, this will cause a false match if those prefix
and suffix components are actually equal and search path itself also has a
file called `target'. For example, looking for `foo/target' in `.../src/foo'
will spuriously match `.../src/foo/target', not `.../src/foo/foo/target'.
This last bug prompted the change in dir.c, rev 1.27, which happens
to partially workaround it by avoiding the above matching code in the
case of the `curdir' search path entry (at the cost of incurring an
exorbitant amount of cache misses). The situation is unchanged however,
when processing other entries on the search path (e.g. those other than
`dot' and `cur').
Drop the prefix matching code in DirLookup() entirely and use DirFindDot()
and DirLookup() only for names without proper directory components (i.e.
`target' and `./target). Otherwise, non-absolute names are dealt with by
DirLookupSubdir(), while absolute names can be checked for an exact match
of the directory components prefix against the directories on the current
search path. This allows for the use of the file cache to check the
existence of the file and additionally, provides a shortcut out of
Dir_FindFile() if we have the prefix match but not a cache entry (this
is especially beneficial for searches in .CURDIR when it's not equal
to `dot').
2002-01-31 15:38:34 +03:00
|
|
|
if (name[0] != '/') {
|
1993-03-21 12:45:37 +03:00
|
|
|
Boolean checkedDot = FALSE;
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("failed. Trying subdirectories...");
|
|
|
|
}
|
1997-05-09 21:05:59 +04:00
|
|
|
|
2002-11-26 09:12:59 +03:00
|
|
|
if (!hasLastDot) {
|
|
|
|
if (dot) {
|
|
|
|
checkedDot = TRUE;
|
|
|
|
if ((file = DirLookupSubdir(dot, name)) != NULL)
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
if (cur && (file = DirLookupSubdir(cur, name)) != NULL)
|
|
|
|
return file;
|
|
|
|
}
|
1997-05-09 21:05:59 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
(void) Lst_Open (path);
|
|
|
|
while ((ln = Lst_Next (path)) != NILLNODE) {
|
|
|
|
p = (Path *) Lst_Datum (ln);
|
1999-07-11 06:06:57 +04:00
|
|
|
if (p == dotLast)
|
|
|
|
continue;
|
2002-11-26 09:12:59 +03:00
|
|
|
if (p == dot) {
|
|
|
|
if (checkedDot)
|
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
checkedDot = TRUE;
|
2002-11-26 09:12:59 +03:00
|
|
|
}
|
1997-05-09 21:05:59 +04:00
|
|
|
if ((file = DirLookupSubdir(p, name)) != NULL) {
|
1993-03-21 12:45:37 +03:00
|
|
|
Lst_Close (path);
|
1997-05-09 21:05:59 +04:00
|
|
|
return file;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
2002-01-18 22:18:23 +03:00
|
|
|
Lst_Close (path);
|
1996-11-06 20:58:58 +03:00
|
|
|
|
2002-11-26 09:12:59 +03:00
|
|
|
if (hasLastDot) {
|
|
|
|
if (dot && !checkedDot) {
|
|
|
|
checkedDot = TRUE;
|
|
|
|
if ((file = DirLookupSubdir(dot, name)) != NULL)
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
if (cur && (file = DirLookupSubdir(cur, name)) != NULL)
|
|
|
|
return file;
|
|
|
|
}
|
1999-07-11 06:06:57 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("failed. ");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (checkedDot) {
|
|
|
|
/*
|
|
|
|
* Already checked by the given name, since . was in the path,
|
|
|
|
* so no point in proceeding...
|
|
|
|
*/
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("Checked . already, returning NULL\n");
|
|
|
|
}
|
|
|
|
return(NULL);
|
|
|
|
}
|
Fix the bug addressed in revision 1.27 properly. Analysis of the problem
(see also PR#15179):
When looking up names which directory components (i.e. having slashes,
except when of the form `./name'), FindFile()/DirLookup() first looks
the final filename component in the cache for each directory on the search
path and then proceeds to match the prefixed directory components by
comparing them to the trailing directory components of the the search
path being probed.
This is not correct. When looking for `bar/target' in a path `.../src/foo',
you want it to come up with `.../src/foo/bar/target' (if it exists). There's
no point in comparing the the `bar' prefix on the target to the `foo' suffix
on the search path. Indeed, this will cause a false match if those prefix
and suffix components are actually equal and search path itself also has a
file called `target'. For example, looking for `foo/target' in `.../src/foo'
will spuriously match `.../src/foo/target', not `.../src/foo/foo/target'.
This last bug prompted the change in dir.c, rev 1.27, which happens
to partially workaround it by avoiding the above matching code in the
case of the `curdir' search path entry (at the cost of incurring an
exorbitant amount of cache misses). The situation is unchanged however,
when processing other entries on the search path (e.g. those other than
`dot' and `cur').
Drop the prefix matching code in DirLookup() entirely and use DirFindDot()
and DirLookup() only for names without proper directory components (i.e.
`target' and `./target). Otherwise, non-absolute names are dealt with by
DirLookupSubdir(), while absolute names can be checked for an exact match
of the directory components prefix against the directories on the current
search path. This allows for the use of the file cache to check the
existence of the file and additionally, provides a shortcut out of
Dir_FindFile() if we have the prefix match but not a cache entry (this
is especially beneficial for searches in .CURDIR when it's not equal
to `dot').
2002-01-31 15:38:34 +03:00
|
|
|
|
|
|
|
} else { /* name[0] == '/' */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For absolute names, compare directory path prefix against the
|
|
|
|
* the directory path of each member on the search path for an exact
|
|
|
|
* match. If we have an exact match on any member of the search path,
|
|
|
|
* use the cached contents of that member to lookup the final file
|
|
|
|
* component. If that lookup fails we can safely assume that the
|
|
|
|
* file does not exist at all. This is signified by DirLookupAbs()
|
|
|
|
* returning an empty string.
|
|
|
|
*/
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("failed. Trying exact path matches...");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasLastDot && cur && (file = DirLookupAbs(cur, name, cp)) != NULL)
|
|
|
|
return *file?file:NULL;
|
|
|
|
|
|
|
|
(void) Lst_Open (path);
|
|
|
|
while ((ln = Lst_Next (path)) != NILLNODE) {
|
|
|
|
p = (Path *) Lst_Datum (ln);
|
|
|
|
if (p == dotLast)
|
|
|
|
continue;
|
|
|
|
if ((file = DirLookupAbs(p, name, cp)) != NULL) {
|
|
|
|
Lst_Close (path);
|
|
|
|
return *file?file:NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Lst_Close (path);
|
|
|
|
|
|
|
|
if (hasLastDot && cur && (file = DirLookupAbs(cur, name, cp)) != NULL)
|
|
|
|
return *file?file:NULL;
|
|
|
|
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("failed. ");
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Didn't find it that way, either. Sigh. Phase 3. Add its directory
|
|
|
|
* onto the search path in any case, just in case, then look for the
|
|
|
|
* thing in the hash table. If we find it, grand. We return a new
|
|
|
|
* copy of the name. Otherwise we sadly return a NULL pointer. Sigh.
|
|
|
|
* Note that if the directory holding the file doesn't exist, this will
|
|
|
|
* do an extra search of the final directory on the path. Unless something
|
|
|
|
* weird happens, this search won't succeed and life will be groovy.
|
|
|
|
*
|
|
|
|
* Sigh. We cannot add the directory onto the search path because
|
|
|
|
* of this amusing case:
|
|
|
|
* $(INSTALLDIR)/$(FILE): $(FILE)
|
|
|
|
*
|
|
|
|
* $(FILE) exists in $(INSTALLDIR) but not in the current one.
|
|
|
|
* When searching for $(FILE), we will find it in $(INSTALLDIR)
|
|
|
|
* b/c we added it here. This is not good...
|
|
|
|
*/
|
|
|
|
#ifdef notdef
|
|
|
|
cp[-1] = '\0';
|
1997-05-09 01:24:41 +04:00
|
|
|
(void) Dir_AddDir (path, name);
|
1993-03-21 12:45:37 +03:00
|
|
|
cp[-1] = '/';
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
bigmisses += 1;
|
|
|
|
ln = Lst_Last (path);
|
|
|
|
if (ln == NILLNODE) {
|
|
|
|
return ((char *) NULL);
|
|
|
|
} else {
|
|
|
|
p = (Path *) Lst_Datum (ln);
|
|
|
|
}
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (Hash_FindEntry (&p->files, cp) != (Hash_Entry *)NULL) {
|
1996-08-13 20:42:00 +04:00
|
|
|
return (estrdup (name));
|
1993-03-21 12:45:37 +03:00
|
|
|
} else {
|
|
|
|
return ((char *) NULL);
|
|
|
|
}
|
|
|
|
#else /* !notdef */
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("Looking for \"%s\"...", name);
|
|
|
|
}
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
bigmisses += 1;
|
|
|
|
entry = Hash_FindEntry(&mtimes, name);
|
|
|
|
if (entry != (Hash_Entry *)NULL) {
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("got it (in mtime cache)\n");
|
|
|
|
}
|
1996-08-13 20:42:00 +04:00
|
|
|
return(estrdup(name));
|
1993-03-21 12:45:37 +03:00
|
|
|
} else if (stat (name, &stb) == 0) {
|
|
|
|
entry = Hash_CreateEntry(&mtimes, name, (Boolean *)NULL);
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("Caching %s for %s\n", Targ_FmtTime(stb.st_mtime),
|
|
|
|
name);
|
|
|
|
}
|
1994-12-24 19:54:24 +03:00
|
|
|
Hash_SetValue(entry, (long)stb.st_mtime);
|
1996-08-13 20:42:00 +04:00
|
|
|
return (estrdup (name));
|
1993-03-21 12:45:37 +03:00
|
|
|
} else {
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("failed. Returning NULL\n");
|
|
|
|
}
|
|
|
|
return ((char *)NULL);
|
|
|
|
}
|
|
|
|
#endif /* notdef */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Dir_MTime --
|
|
|
|
* Find the modification time of the file described by gn along the
|
|
|
|
* search path dirSearchPath.
|
1996-11-06 20:58:58 +03:00
|
|
|
*
|
2002-06-15 22:24:55 +04:00
|
|
|
* Input:
|
|
|
|
* gn the file whose modification time is desired
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Results:
|
|
|
|
* The modification time or 0 if it doesn't exist
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* The modification time is placed in the node's mtime slot.
|
|
|
|
* If the node didn't have a path entry before, and Dir_FindFile
|
|
|
|
* found one for it, the full name is placed in the path slot.
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
int
|
2002-06-15 22:24:55 +04:00
|
|
|
Dir_MTime(GNode *gn)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
char *fullName; /* the full pathname of name */
|
|
|
|
struct stat stb; /* buffer for finding the mod time */
|
|
|
|
Hash_Entry *entry;
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (gn->type & OP_ARCHV) {
|
|
|
|
return Arch_MTime (gn);
|
2002-02-03 23:08:30 +03:00
|
|
|
} else if (gn->type & OP_PHONY) {
|
|
|
|
gn->mtime = 0;
|
|
|
|
return 0;
|
1999-11-26 01:34:16 +03:00
|
|
|
} else if (gn->path == (char *)NULL) {
|
2002-02-03 23:08:30 +03:00
|
|
|
if (gn->type & OP_NOPATH)
|
1997-05-07 00:59:42 +04:00
|
|
|
fullName = NULL;
|
|
|
|
else
|
|
|
|
fullName = Dir_FindFile (gn->name, dirSearchPath);
|
1993-03-21 12:45:37 +03:00
|
|
|
} else {
|
|
|
|
fullName = gn->path;
|
|
|
|
}
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (fullName == (char *)NULL) {
|
1996-08-13 20:42:00 +04:00
|
|
|
fullName = estrdup(gn->name);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
entry = Hash_FindEntry(&mtimes, fullName);
|
|
|
|
if (entry != (Hash_Entry *)NULL) {
|
|
|
|
/*
|
|
|
|
* Only do this once -- the second time folks are checking to
|
|
|
|
* see if the file was actually updated, so we need to actually go
|
|
|
|
* to the file system.
|
|
|
|
*/
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("Using cached time %s for %s\n",
|
1994-12-24 19:54:24 +03:00
|
|
|
Targ_FmtTime((time_t)(long)Hash_GetValue(entry)), fullName);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-12-24 19:54:24 +03:00
|
|
|
stb.st_mtime = (time_t)(long)Hash_GetValue(entry);
|
1993-03-21 12:45:37 +03:00
|
|
|
Hash_DeleteEntry(&mtimes, entry);
|
|
|
|
} else if (stat (fullName, &stb) < 0) {
|
|
|
|
if (gn->type & OP_MEMBER) {
|
1994-06-07 02:45:17 +04:00
|
|
|
if (fullName != gn->path)
|
|
|
|
free(fullName);
|
1993-03-21 12:45:37 +03:00
|
|
|
return Arch_MemMTime (gn);
|
|
|
|
} else {
|
|
|
|
stb.st_mtime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fullName && gn->path == (char *)NULL) {
|
|
|
|
gn->path = fullName;
|
|
|
|
}
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
gn->mtime = stb.st_mtime;
|
|
|
|
return (gn->mtime);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Dir_AddDir --
|
|
|
|
* Add the given name to the end of the given path. The order of
|
|
|
|
* the arguments is backwards so ParseDoDependency can do a
|
|
|
|
* Lst_ForEach of its list of paths...
|
|
|
|
*
|
2002-06-15 22:24:55 +04:00
|
|
|
* Input:
|
|
|
|
* path the path to which the directory should be
|
|
|
|
* added
|
|
|
|
* name the name of the directory to add
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Results:
|
|
|
|
* none
|
|
|
|
*
|
|
|
|
* Side Effects:
|
1996-11-06 20:58:58 +03:00
|
|
|
* A structure is added to the list and the directory is
|
1993-03-21 12:45:37 +03:00
|
|
|
* read and hashed.
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
1997-05-09 01:24:41 +04:00
|
|
|
Path *
|
2002-06-15 22:24:55 +04:00
|
|
|
Dir_AddDir(Lst path, const char *name)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2002-11-26 09:12:59 +03:00
|
|
|
LstNode ln = NILLNODE; /* node in case Path structure is found */
|
2002-06-15 22:24:55 +04:00
|
|
|
Path *p = NULL; /* pointer to new Path structure */
|
1993-03-21 12:45:37 +03:00
|
|
|
DIR *d; /* for reading directory */
|
2002-06-15 22:24:55 +04:00
|
|
|
struct dirent *dp; /* entry in directory */
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1999-07-11 06:06:57 +04:00
|
|
|
if (strcmp(name, ".DOTLAST") == 0) {
|
2003-07-14 22:19:11 +04:00
|
|
|
ln = Lst_Find (path, (ClientData)UNCONST(name), DirFindName);
|
1999-07-11 06:06:57 +04:00
|
|
|
if (ln != NILLNODE)
|
|
|
|
return (Path *) Lst_Datum(ln);
|
|
|
|
else {
|
|
|
|
dotLast->refCount += 1;
|
|
|
|
(void)Lst_AtFront(path, (ClientData)dotLast);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-26 09:12:59 +03:00
|
|
|
if (path)
|
2003-07-14 22:19:11 +04:00
|
|
|
ln = Lst_Find (openDirectories, (ClientData)UNCONST(name), DirFindName);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (ln != NILLNODE) {
|
|
|
|
p = (Path *)Lst_Datum (ln);
|
|
|
|
if (Lst_Member(path, (ClientData)p) == NILLNODE) {
|
|
|
|
p->refCount += 1;
|
|
|
|
(void)Lst_AtEnd (path, (ClientData)p);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("Caching %s...", name);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if ((d = opendir (name)) != (DIR *) NULL) {
|
|
|
|
p = (Path *) emalloc (sizeof (Path));
|
1996-08-13 20:42:00 +04:00
|
|
|
p->name = estrdup (name);
|
1993-03-21 12:45:37 +03:00
|
|
|
p->hits = 0;
|
|
|
|
p->refCount = 1;
|
|
|
|
Hash_InitTable (&p->files, -1);
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-12-08 03:37:37 +03:00
|
|
|
while ((dp = readdir (d)) != (struct dirent *) NULL) {
|
1996-02-05 01:20:27 +03:00
|
|
|
#if defined(sun) && defined(d_ino) /* d_ino is a sunos4 #define for d_fileno */
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* The sun directory library doesn't check for a 0 inode
|
|
|
|
* (0-inode slots just take up space), so we have to do
|
|
|
|
* it ourselves.
|
|
|
|
*/
|
|
|
|
if (dp->d_fileno == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
1996-02-05 01:20:27 +03:00
|
|
|
#endif /* sun && d_ino */
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)Hash_CreateEntry(&p->files, dp->d_name, (Boolean *)NULL);
|
|
|
|
}
|
|
|
|
(void) closedir (d);
|
|
|
|
(void)Lst_AtEnd (openDirectories, (ClientData)p);
|
1997-05-09 01:24:41 +04:00
|
|
|
if (path != NULL)
|
|
|
|
(void)Lst_AtEnd (path, (ClientData)p);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if (DEBUG(DIR)) {
|
|
|
|
printf("done\n");
|
|
|
|
}
|
|
|
|
}
|
1997-05-09 01:24:41 +04:00
|
|
|
return p;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Dir_CopyDir --
|
|
|
|
* Callback function for duplicating a search path via Lst_Duplicate.
|
|
|
|
* Ups the reference count for the directory.
|
|
|
|
*
|
|
|
|
* Results:
|
|
|
|
* Returns the Path it was given.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* The refCount of the path is incremented.
|
|
|
|
*
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
ClientData
|
2002-06-15 22:24:55 +04:00
|
|
|
Dir_CopyDir(ClientData p)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-06-07 02:45:17 +04:00
|
|
|
((Path *) p)->refCount += 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
return ((ClientData)p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Dir_MakeFlags --
|
|
|
|
* Make a string by taking all the directories in the given search
|
|
|
|
* path and preceding them by the given flag. Used by the suffix
|
|
|
|
* module to create variables for compilers based on suffix search
|
|
|
|
* paths.
|
|
|
|
*
|
2002-06-15 22:24:55 +04:00
|
|
|
* Input:
|
|
|
|
* flag flag which should precede each directory
|
|
|
|
* path list of directories
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Results:
|
|
|
|
* The string mentioned above. Note that there is no space between
|
|
|
|
* the given flag and each directory. The empty string is returned if
|
|
|
|
* Things don't go well.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* None
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
char *
|
2003-07-14 22:19:11 +04:00
|
|
|
Dir_MakeFlags(const char *flag, Lst path)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
char *str; /* the string which will be returned */
|
2003-07-14 22:19:11 +04:00
|
|
|
char *s1, *s2;/* the current directory preceded by 'flag' */
|
1993-03-21 12:45:37 +03:00
|
|
|
LstNode ln; /* the node of the current directory */
|
|
|
|
Path *p; /* the structure describing the current directory */
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1996-08-13 20:42:00 +04:00
|
|
|
str = estrdup ("");
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (Lst_Open (path) == SUCCESS) {
|
|
|
|
while ((ln = Lst_Next (path)) != NILLNODE) {
|
|
|
|
p = (Path *) Lst_Datum (ln);
|
2003-07-14 22:19:11 +04:00
|
|
|
s2 = str_concat (flag, p->name, 0);
|
|
|
|
str = str_concat (s1 = str, s2, STR_ADDSPACE);
|
|
|
|
free(s1);
|
|
|
|
free(s2);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
Lst_Close (path);
|
|
|
|
}
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
return (str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Dir_Destroy --
|
|
|
|
* Nuke a directory descriptor, if possible. Callback procedure
|
|
|
|
* for the suffixes module when destroying a search path.
|
|
|
|
*
|
2002-06-15 22:24:55 +04:00
|
|
|
* Input:
|
|
|
|
* pp The directory descriptor to nuke
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Results:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* If no other path references this directory (refCount == 0),
|
|
|
|
* the Path and all its data are freed.
|
|
|
|
*
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2002-06-15 22:24:55 +04:00
|
|
|
Dir_Destroy(ClientData pp)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-06-07 02:45:17 +04:00
|
|
|
Path *p = (Path *) pp;
|
1993-03-21 12:45:37 +03:00
|
|
|
p->refCount -= 1;
|
|
|
|
|
|
|
|
if (p->refCount == 0) {
|
|
|
|
LstNode ln;
|
|
|
|
|
|
|
|
ln = Lst_Member (openDirectories, (ClientData)p);
|
|
|
|
(void) Lst_Remove (openDirectories, ln);
|
|
|
|
|
|
|
|
Hash_DeleteTable (&p->files);
|
|
|
|
free((Address)p->name);
|
|
|
|
free((Address)p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Dir_ClearPath --
|
|
|
|
* Clear out all elements of the given search path. This is different
|
|
|
|
* from destroying the list, notice.
|
|
|
|
*
|
2002-06-15 22:24:55 +04:00
|
|
|
* Input:
|
|
|
|
* path Path to clear
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Results:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* The path is set to the empty list.
|
|
|
|
*
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2002-06-15 22:24:55 +04:00
|
|
|
Dir_ClearPath(Lst path)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
Path *p;
|
|
|
|
while (!Lst_IsEmpty(path)) {
|
|
|
|
p = (Path *)Lst_DeQueue(path);
|
1994-06-07 02:45:17 +04:00
|
|
|
Dir_Destroy((ClientData) p);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
* Dir_Concat --
|
|
|
|
* Concatenate two paths, adding the second to the end of the first.
|
|
|
|
* Makes sure to avoid duplicates.
|
|
|
|
*
|
2002-06-15 22:24:55 +04:00
|
|
|
* Input:
|
|
|
|
* path1 Dest
|
|
|
|
* path2 Source
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Results:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Side Effects:
|
|
|
|
* Reference counts for added dirs are upped.
|
|
|
|
*
|
|
|
|
*-----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void
|
2002-06-15 22:24:55 +04:00
|
|
|
Dir_Concat(Lst path1, Lst path2)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
LstNode ln;
|
|
|
|
Path *p;
|
|
|
|
|
|
|
|
for (ln = Lst_First(path2); ln != NILLNODE; ln = Lst_Succ(ln)) {
|
|
|
|
p = (Path *)Lst_Datum(ln);
|
|
|
|
if (Lst_Member(path1, (ClientData)p) == NILLNODE) {
|
|
|
|
p->refCount += 1;
|
|
|
|
(void)Lst_AtEnd(path1, (ClientData)p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/********** DEBUG INFO **********/
|
1994-03-05 03:34:29 +03:00
|
|
|
void
|
2002-06-15 22:24:55 +04:00
|
|
|
Dir_PrintDirectories(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
LstNode ln;
|
|
|
|
Path *p;
|
1996-11-06 20:58:58 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
printf ("#*** Directory Cache:\n");
|
|
|
|
printf ("# Stats: %d hits %d misses %d near misses %d losers (%d%%)\n",
|
|
|
|
hits, misses, nearmisses, bigmisses,
|
|
|
|
(hits+bigmisses+nearmisses ?
|
|
|
|
hits * 100 / (hits + bigmisses + nearmisses) : 0));
|
|
|
|
printf ("# %-20s referenced\thits\n", "directory");
|
|
|
|
if (Lst_Open (openDirectories) == SUCCESS) {
|
|
|
|
while ((ln = Lst_Next (openDirectories)) != NILLNODE) {
|
|
|
|
p = (Path *) Lst_Datum (ln);
|
|
|
|
printf ("# %-20s %10d\t%4d\n", p->name, p->refCount, p->hits);
|
|
|
|
}
|
|
|
|
Lst_Close (openDirectories);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-15 22:24:55 +04:00
|
|
|
static int
|
|
|
|
DirPrintDir(ClientData p, ClientData dummy)
|
1996-11-06 20:58:58 +03:00
|
|
|
{
|
1994-06-07 02:45:17 +04:00
|
|
|
printf ("%s ", ((Path *) p)->name);
|
|
|
|
return (dummy ? 0 : 0);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-03-05 03:34:29 +03:00
|
|
|
void
|
2002-06-15 22:24:55 +04:00
|
|
|
Dir_PrintPath(Lst path)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
Lst_ForEach (path, DirPrintDir, (ClientData)0);
|
|
|
|
}
|