home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 4
/
FreshFish_May-June1994.bin
/
bsd
/
src
/
make
/
make-amiga
/
dir.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-09-23
|
38KB
|
1,238 lines
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
* 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.
*/
#ifndef lint
static char sccsid[] = "@(#)dir.c 5.6 (Berkeley) 12/28/90";
#endif /* not lint */
/*-
* 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.
*
* 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 <stdio.h>
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include "make.h"
#include "hash.h"
/*
* 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 */
bigmisse