mirror of
https://git.savannah.gnu.org/git/make.git
synced 2025-02-07 14:56:06 +00:00
Formerly dir.c.~4~
This commit is contained in:
parent
9603279fb8
commit
46ccc784fe
1 changed files with 135 additions and 48 deletions
177
dir.c
177
dir.c
|
@ -53,21 +53,39 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
|
||||||
#else
|
#else
|
||||||
#define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
|
#define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
|
||||||
#endif /* POSIX */
|
#endif /* POSIX */
|
||||||
|
|
||||||
/* Hash table of directories. */
|
/* Hash table of directories. */
|
||||||
|
|
||||||
struct directory
|
#ifndef DIRECTORY_BUCKETS
|
||||||
|
#define DIRECTORY_BUCKETS 199
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct directory_contents
|
||||||
{
|
{
|
||||||
struct directory *next;
|
struct directory_contents *next;
|
||||||
char *name; /* Name of the directory. */
|
|
||||||
|
int dev, ino; /* Device and inode numbers of this dir. */
|
||||||
|
|
||||||
struct dirfile **files; /* Files in this directory. */
|
struct dirfile **files; /* Files in this directory. */
|
||||||
DIR *dirstream; /* Stream reading this directory. */
|
DIR *dirstream; /* Stream reading this directory. */
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifndef DIRECTORY_BUCKETS
|
/* Table of directory contents hashed by device and inode number. */
|
||||||
#define DIRECTORY_BUCKETS 23
|
static struct directory_contents *directories_contents[DIRECTORY_BUCKETS];
|
||||||
#endif
|
|
||||||
|
|
||||||
|
struct directory
|
||||||
|
{
|
||||||
|
struct directory *next;
|
||||||
|
|
||||||
|
char *name; /* Name of the directory. */
|
||||||
|
|
||||||
|
/* The directory's contents. This data may be shared by several
|
||||||
|
entries in the hash table, which refer to the same directory
|
||||||
|
(identified uniquely by `dev' and `ino') under different names. */
|
||||||
|
struct directory_contents *contents;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Table of directories hashed by name. */
|
||||||
static struct directory *directories[DIRECTORY_BUCKETS];
|
static struct directory *directories[DIRECTORY_BUCKETS];
|
||||||
|
|
||||||
|
|
||||||
|
@ -88,9 +106,11 @@ struct dirfile
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifndef DIRFILE_BUCKETS
|
#ifndef DIRFILE_BUCKETS
|
||||||
#define DIRFILE_BUCKETS 1007
|
#define DIRFILE_BUCKETS 107
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static int dir_contents_file_exists_p ();
|
||||||
|
|
||||||
/* Find the directory named NAME and return its `struct directory'. */
|
/* Find the directory named NAME and return its `struct directory'. */
|
||||||
|
|
||||||
static struct directory *
|
static struct directory *
|
||||||
|
@ -111,54 +131,93 @@ find_directory (name)
|
||||||
|
|
||||||
if (dir == 0)
|
if (dir == 0)
|
||||||
{
|
{
|
||||||
/* The directory was not found. Create a new entry
|
struct stat st;
|
||||||
for it and start its directory stream reading. */
|
|
||||||
|
/* The directory was not found. Create a new entry for it. */
|
||||||
|
|
||||||
dir = (struct directory *) xmalloc (sizeof (struct directory));
|
dir = (struct directory *) xmalloc (sizeof (struct directory));
|
||||||
dir->next = directories[hash];
|
dir->next = directories[hash];
|
||||||
directories[hash] = dir;
|
directories[hash] = dir;
|
||||||
dir->name = savestring (name, p - name);
|
dir->name = savestring (name, p - name);
|
||||||
dir->dirstream = opendir (name);
|
|
||||||
if (dir->dirstream == 0)
|
/* The directory is not in the name hash table.
|
||||||
/* Couldn't open the directory. Mark this by
|
Find its device and inode numbers, and look it up by them. */
|
||||||
setting the `files' member to a nil pointer. */
|
|
||||||
dir->files = 0;
|
if (stat (name, &st) < 0)
|
||||||
|
/* Couldn't stat the directory. Mark this by
|
||||||
|
setting the `contents' member to a nil pointer. */
|
||||||
|
dir->contents = 0;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* Allocate an array of hash buckets for files and zero it. */
|
/* Search the contents hash table; device and inode are the key. */
|
||||||
dir->files = (struct dirfile **)
|
|
||||||
xmalloc (sizeof (struct dirfile) * DIRFILE_BUCKETS);
|
struct directory_contents *dc;
|
||||||
bzero ((char *) dir->files,
|
|
||||||
sizeof (struct dirfile) * DIRFILE_BUCKETS);
|
hash = ((unsigned int) st.st_dev << 16) | (unsigned int) st.st_ino;
|
||||||
|
hash %= DIRECTORY_BUCKETS;
|
||||||
|
|
||||||
|
for (dc = directories_contents[hash]; dc != 0; dc = dc->next)
|
||||||
|
if (dc->dev == st.st_dev && dc->ino == st.st_ino)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (dc == 0)
|
||||||
|
{
|
||||||
|
/* Nope; this really is a directory we haven't seen before. */
|
||||||
|
|
||||||
|
dc = (struct directory_contents *)
|
||||||
|
xmalloc (sizeof (struct directory_contents));
|
||||||
|
|
||||||
|
/* Enter it in the contents hash table. */
|
||||||
|
dc->dev = st.st_dev;
|
||||||
|
dc->ino = st.st_ino;
|
||||||
|
dc->next = directories_contents[hash];
|
||||||
|
directories_contents[hash] = dc;
|
||||||
|
|
||||||
|
dc->dirstream = opendir (name);
|
||||||
|
if (dc->dirstream == 0)
|
||||||
|
/* Couldn't open the directory. Mark this by
|
||||||
|
setting the `files' member to a nil pointer. */
|
||||||
|
dc->files = 0;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Allocate an array of buckets for files and zero it. */
|
||||||
|
dc->files = (struct dirfile **)
|
||||||
|
xmalloc (sizeof (struct dirfile *) * DIRFILE_BUCKETS);
|
||||||
|
bzero ((char *) dc->files,
|
||||||
|
sizeof (struct dirfile *) * DIRFILE_BUCKETS);
|
||||||
|
|
||||||
/* Keep track of how many directories are open. */
|
/* Keep track of how many directories are open. */
|
||||||
++open_directories;
|
++open_directories;
|
||||||
if (open_directories == MAX_OPEN_DIRECTORIES)
|
if (open_directories == MAX_OPEN_DIRECTORIES)
|
||||||
/* Read the entire directory and then close it. */
|
/* We have too many directories open already.
|
||||||
(void) dir_file_exists_p (dir->name, (char *) 0);
|
Read the entire directory and then close it. */
|
||||||
|
(void) dir_contents_file_exists_p (dc, (char *) 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Point the name-hashed entry for DIR at its contents data. */
|
||||||
|
dir->contents = dc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return dir;
|
return dir;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return 1 if the name FILENAME in directory DIRNAME
|
/* Return 1 if the name FILENAME is entered in DIR's hash table.
|
||||||
is entered in the dir hash table.
|
|
||||||
FILENAME must contain no slashes. */
|
FILENAME must contain no slashes. */
|
||||||
|
|
||||||
int
|
static int
|
||||||
dir_file_exists_p (dirname, filename)
|
dir_contents_file_exists_p (dir, filename)
|
||||||
register char *dirname;
|
register struct directory_contents *dir;
|
||||||
register char *filename;
|
register char *filename;
|
||||||
{
|
{
|
||||||
register unsigned int hash;
|
register unsigned int hash;
|
||||||
register char *p;
|
register char *p;
|
||||||
register struct directory *dir;
|
|
||||||
register struct dirfile *df;
|
register struct dirfile *df;
|
||||||
register struct dirent *d;
|
register struct dirent *d;
|
||||||
dir = find_directory (dirname);
|
|
||||||
|
|
||||||
if (dir->files == 0)
|
if (dir == 0 || dir->files == 0)
|
||||||
/* The directory could not be opened. */
|
/* The directory could not be stat'd or opened. */
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
hash = 0;
|
hash = 0;
|
||||||
|
@ -222,6 +281,19 @@ dir_file_exists_p (dirname, filename)
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Return 1 if the name FILENAME in directory DIRNAME
|
||||||
|
is entered in the dir hash table.
|
||||||
|
FILENAME must contain no slashes. */
|
||||||
|
|
||||||
|
int
|
||||||
|
dir_file_exists_p (dirname, filename)
|
||||||
|
register char *dirname;
|
||||||
|
register char *filename;
|
||||||
|
{
|
||||||
|
return dir_contents_file_exists_p (find_directory (dirname)->contents,
|
||||||
|
filename);
|
||||||
|
}
|
||||||
|
|
||||||
/* Return 1 if the file named NAME exists. */
|
/* Return 1 if the file named NAME exists. */
|
||||||
|
|
||||||
|
@ -277,19 +349,30 @@ file_impossible (filename)
|
||||||
HASH (hash, *p);
|
HASH (hash, *p);
|
||||||
hash %= DIRFILE_BUCKETS;
|
hash %= DIRFILE_BUCKETS;
|
||||||
|
|
||||||
if (dir->files == 0)
|
if (dir->contents == 0)
|
||||||
|
{
|
||||||
|
/* The directory could not be stat'd. We allocate a contents
|
||||||
|
structure for it, but leave it out of the contents hash table. */
|
||||||
|
dir->contents = (struct directory_contents *)
|
||||||
|
xmalloc (sizeof (struct directory_contents));
|
||||||
|
dir->contents->dev = dir->contents->ino = 0;
|
||||||
|
dir->contents->files = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dir->contents->files == 0)
|
||||||
{
|
{
|
||||||
/* The directory was not opened; we must allocate the hash buckets. */
|
/* The directory was not opened; we must allocate the hash buckets. */
|
||||||
dir->files = (struct dirfile **)
|
dir->contents->files = (struct dirfile **)
|
||||||
xmalloc (sizeof (struct dirfile) * DIRFILE_BUCKETS);
|
xmalloc (sizeof (struct dirfile) * DIRFILE_BUCKETS);
|
||||||
bzero ((char *) dir->files, sizeof (struct dirfile) * DIRFILE_BUCKETS);
|
bzero ((char *) dir->contents->files,
|
||||||
|
sizeof (struct dirfile) * DIRFILE_BUCKETS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Make a new entry and put it in the table. */
|
/* Make a new entry and put it in the table. */
|
||||||
|
|
||||||
new = (struct dirfile *) xmalloc (sizeof (struct dirfile));
|
new = (struct dirfile *) xmalloc (sizeof (struct dirfile));
|
||||||
new->next = dir->files[hash];
|
new->next = dir->contents->files[hash];
|
||||||
dir->files[hash] = new;
|
dir->contents->files[hash] = new;
|
||||||
new->name = savestring (filename, strlen (filename));
|
new->name = savestring (filename, strlen (filename));
|
||||||
new->impossible = 1;
|
new->impossible = 1;
|
||||||
}
|
}
|
||||||
|
@ -303,22 +386,22 @@ file_impossible_p (filename)
|
||||||
char *dirend;
|
char *dirend;
|
||||||
register char *p = filename;
|
register char *p = filename;
|
||||||
register unsigned int hash;
|
register unsigned int hash;
|
||||||
register struct directory *dir;
|
register struct directory_contents *dir;
|
||||||
register struct dirfile *next;
|
register struct dirfile *next;
|
||||||
|
|
||||||
dirend = rindex (filename, '/');
|
dirend = rindex (filename, '/');
|
||||||
if (dirend == 0)
|
if (dirend == 0)
|
||||||
dir = find_directory (".");
|
dir = find_directory (".")->contents;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
char *dirname = (char *) alloca (dirend - filename + 1);
|
char *dirname = (char *) alloca (dirend - filename + 1);
|
||||||
bcopy (p, dirname, dirend - p);
|
bcopy (p, dirname, dirend - p);
|
||||||
dirname[dirend - p] = '\0';
|
dirname[dirend - p] = '\0';
|
||||||
dir = find_directory (dirname);
|
dir = find_directory (dirname)->contents;
|
||||||
p = dirend + 1;
|
p = dirend + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dir->files == 0)
|
if (dir == 0 || dir->files == 0)
|
||||||
/* There are no files entered for this directory. */
|
/* There are no files entered for this directory. */
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
@ -358,20 +441,24 @@ print_dir_data_base ()
|
||||||
for (dir = directories[i]; dir != 0; dir = dir->next)
|
for (dir = directories[i]; dir != 0; dir = dir->next)
|
||||||
{
|
{
|
||||||
++dirs;
|
++dirs;
|
||||||
if (dir->files == 0)
|
if (dir->contents == 0)
|
||||||
printf ("# %s: could not be opened.\n", dir->name);
|
printf ("# %s: could not be stat'd.\n", dir->name);
|
||||||
|
else if (dir->contents->files == 0)
|
||||||
|
printf ("# %s (device %d, inode %d): could not be opened.\n",
|
||||||
|
dir->name, dir->contents->dev, dir->contents->ino);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
register unsigned int f = 0, im = 0;
|
register unsigned int f = 0, im = 0;
|
||||||
register unsigned int j;
|
register unsigned int j;
|
||||||
register struct dirfile *df;
|
register struct dirfile *df;
|
||||||
for (j = 0; j < DIRFILE_BUCKETS; ++j)
|
for (j = 0; j < DIRFILE_BUCKETS; ++j)
|
||||||
for (df = dir->files[j]; df != 0; df = df->next)
|
for (df = dir->contents->files[j]; df != 0; df = df->next)
|
||||||
if (df->impossible)
|
if (df->impossible)
|
||||||
++im;
|
++im;
|
||||||
else
|
else
|
||||||
++f;
|
++f;
|
||||||
printf ("# %s: ", dir->name);
|
printf ("# %s (device %d, inode %d): ",
|
||||||
|
dir->name, dir->contents->dev, dir->contents->ino);
|
||||||
if (f == 0)
|
if (f == 0)
|
||||||
fputs ("No", stdout);
|
fputs ("No", stdout);
|
||||||
else
|
else
|
||||||
|
@ -382,7 +469,7 @@ print_dir_data_base ()
|
||||||
else
|
else
|
||||||
printf ("%u", im);
|
printf ("%u", im);
|
||||||
fputs (" impossibilities", stdout);
|
fputs (" impossibilities", stdout);
|
||||||
if (dir->dirstream == 0)
|
if (dir->contents->dirstream == 0)
|
||||||
puts (".");
|
puts (".");
|
||||||
else
|
else
|
||||||
puts (" so far.");
|
puts (" so far.");
|
||||||
|
|
Loading…
Reference in a new issue