1991-10-19 03:26:24 +08:00
|
|
|
|
/* Directory hashing for GNU Make.
|
2013-05-17 13:46:11 +08:00
|
|
|
|
Copyright (C) 1988-2013 Free Software Foundation, Inc.
|
1991-10-19 03:26:24 +08:00
|
|
|
|
This file is part of GNU Make.
|
|
|
|
|
|
2006-02-12 03:02:21 +08:00
|
|
|
|
GNU Make is free software; you can redistribute it and/or modify it under the
|
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
2007-07-05 03:35:15 +08:00
|
|
|
|
Foundation; either version 3 of the License, or (at your option) any later
|
|
|
|
|
version.
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
2006-02-12 03:02:21 +08:00
|
|
|
|
GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
2006-02-12 03:02:21 +08:00
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
2007-07-05 03:35:15 +08:00
|
|
|
|
this program. If not, see <http://www.gnu.org/licenses/>. */
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
2013-01-21 00:01:01 +08:00
|
|
|
|
#include "makeint.h"
|
2002-07-11 14:38:57 +08:00
|
|
|
|
#include "hash.h"
|
2013-09-22 12:48:05 +08:00
|
|
|
|
#include "filedef.h"
|
2013-06-22 12:22:08 +08:00
|
|
|
|
#include "dep.h"
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
#ifdef HAVE_DIRENT_H
|
1996-03-02 01:45:37 +08:00
|
|
|
|
# include <dirent.h>
|
|
|
|
|
# define NAMLEN(dirent) strlen((dirent)->d_name)
|
2000-01-22 13:43:03 +08:00
|
|
|
|
# ifdef VMS
|
2009-11-16 10:31:23 +08:00
|
|
|
|
/* its prototype is in vmsdir.h, which is not needed for HAVE_DIRENT_H */
|
|
|
|
|
const char *vmsify (const char *name, int type);
|
2000-01-22 13:43:03 +08:00
|
|
|
|
# endif
|
1996-03-02 01:45:37 +08:00
|
|
|
|
#else
|
|
|
|
|
# define dirent direct
|
|
|
|
|
# define NAMLEN(dirent) (dirent)->d_namlen
|
|
|
|
|
# ifdef HAVE_SYS_NDIR_H
|
|
|
|
|
# include <sys/ndir.h>
|
|
|
|
|
# endif
|
|
|
|
|
# ifdef HAVE_SYS_DIR_H
|
|
|
|
|
# include <sys/dir.h>
|
|
|
|
|
# endif
|
|
|
|
|
# ifdef HAVE_NDIR_H
|
|
|
|
|
# include <ndir.h>
|
|
|
|
|
# endif
|
1996-03-20 22:57:41 +08:00
|
|
|
|
# ifdef HAVE_VMSDIR_H
|
|
|
|
|
# include "vmsdir.h"
|
|
|
|
|
# endif /* HAVE_VMSDIR_H */
|
1996-03-02 01:45:37 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* In GNU systems, <dirent.h> defines this macro for us. */
|
|
|
|
|
#ifdef _D_NAMLEN
|
2000-01-22 13:43:03 +08:00
|
|
|
|
# undef NAMLEN
|
|
|
|
|
# define NAMLEN(d) _D_NAMLEN(d)
|
1996-03-02 01:45:37 +08:00
|
|
|
|
#endif
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
2000-01-22 13:43:03 +08:00
|
|
|
|
#if (defined (POSIX) || defined (VMS) || defined (WINDOWS32)) && !defined (__GNU_LIBRARY__)
|
1991-10-19 03:26:24 +08:00
|
|
|
|
/* Posix does not require that the d_ino field be present, and some
|
|
|
|
|
systems do not provide it. */
|
2000-01-22 13:43:03 +08:00
|
|
|
|
# define REAL_DIR_ENTRY(dp) 1
|
|
|
|
|
# define FAKE_DIR_ENTRY(dp)
|
1991-10-19 03:26:24 +08:00
|
|
|
|
#else
|
2000-01-22 13:43:03 +08:00
|
|
|
|
# define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
|
|
|
|
|
# define FAKE_DIR_ENTRY(dp) (dp->d_ino = 1)
|
1991-10-19 03:26:24 +08:00
|
|
|
|
#endif /* POSIX */
|
1992-10-10 01:35:07 +08:00
|
|
|
|
|
1994-07-26 07:01:19 +08:00
|
|
|
|
#ifdef __MSDOS__
|
1994-09-27 06:20:56 +08:00
|
|
|
|
#include <ctype.h>
|
1997-04-07 15:21:16 +08:00
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
|
|
/* If it's MSDOS that doesn't have _USE_LFN, disable LFN support. */
|
|
|
|
|
#ifndef _USE_LFN
|
|
|
|
|
#define _USE_LFN 0
|
1996-07-26 08:05:41 +08:00
|
|
|
|
#endif
|
1994-07-26 07:01:19 +08:00
|
|
|
|
|
2006-11-19 04:53:44 +08:00
|
|
|
|
static const char *
|
|
|
|
|
dosify (const char *filename)
|
1994-07-26 07:01:19 +08:00
|
|
|
|
{
|
|
|
|
|
static char dos_filename[14];
|
|
|
|
|
char *df;
|
|
|
|
|
int i;
|
|
|
|
|
|
1997-04-07 15:21:16 +08:00
|
|
|
|
if (filename == 0 || _USE_LFN)
|
1996-07-26 08:05:41 +08:00
|
|
|
|
return filename;
|
1994-07-26 07:01:19 +08:00
|
|
|
|
|
1997-04-07 15:21:16 +08:00
|
|
|
|
/* FIXME: what about filenames which violate
|
|
|
|
|
8+3 constraints, like "config.h.in", or ".emacs"? */
|
1994-07-26 07:01:19 +08:00
|
|
|
|
if (strpbrk (filename, "\"*+,;<=>?[\\]|") != 0)
|
|
|
|
|
return filename;
|
|
|
|
|
|
|
|
|
|
df = dos_filename;
|
|
|
|
|
|
|
|
|
|
/* First, transform the name part. */
|
2013-06-22 12:22:08 +08:00
|
|
|
|
for (i = 0; i < 8 && ! STOP_SET (*filename, MAP_DOT|MAP_NUL); ++i)
|
1999-10-15 15:00:58 +08:00
|
|
|
|
*df++ = tolower ((unsigned char)*filename++);
|
1994-07-26 07:01:19 +08:00
|
|
|
|
|
|
|
|
|
/* Now skip to the next dot. */
|
2013-06-22 12:22:08 +08:00
|
|
|
|
while (! STOP_SET (*filename, MAP_DOT|MAP_NUL))
|
1994-07-26 07:01:19 +08:00
|
|
|
|
++filename;
|
|
|
|
|
if (*filename != '\0')
|
|
|
|
|
{
|
|
|
|
|
*df++ = *filename++;
|
2013-06-22 12:22:08 +08:00
|
|
|
|
for (i = 0; i < 3 && ! STOP_SET (*filename, MAP_DOT|MAP_NUL); ++i)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
*df++ = tolower ((unsigned char)*filename++);
|
1994-07-26 07:01:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Look for more dots. */
|
2013-06-22 12:22:08 +08:00
|
|
|
|
while (! STOP_SET (*filename, MAP_DOT|MAP_NUL))
|
1994-07-26 07:01:19 +08:00
|
|
|
|
++filename;
|
|
|
|
|
if (*filename == '.')
|
|
|
|
|
return filename;
|
|
|
|
|
*df = 0;
|
|
|
|
|
return dos_filename;
|
|
|
|
|
}
|
1996-03-20 22:57:41 +08:00
|
|
|
|
#endif /* __MSDOS__ */
|
|
|
|
|
|
1997-04-07 15:21:16 +08:00
|
|
|
|
#ifdef WINDOWS32
|
1996-05-23 05:51:45 +08:00
|
|
|
|
#include "pathstuff.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
1996-05-14 02:38:20 +08:00
|
|
|
|
#ifdef _AMIGA
|
|
|
|
|
#include <ctype.h>
|
1998-07-31 04:54:47 +08:00
|
|
|
|
#endif
|
1996-05-14 02:38:20 +08:00
|
|
|
|
|
1998-07-31 04:54:47 +08:00
|
|
|
|
#ifdef HAVE_CASE_INSENSITIVE_FS
|
2006-11-19 04:53:44 +08:00
|
|
|
|
static const char *
|
|
|
|
|
downcase (const char *filename)
|
1996-05-14 02:38:20 +08:00
|
|
|
|
{
|
2005-02-28 15:48:22 +08:00
|
|
|
|
static PATH_VAR (new_filename);
|
1996-05-14 02:38:20 +08:00
|
|
|
|
char *df;
|
|
|
|
|
|
|
|
|
|
if (filename == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
1998-07-31 04:54:47 +08:00
|
|
|
|
df = new_filename;
|
2006-11-19 04:53:44 +08:00
|
|
|
|
while (*filename != '\0')
|
|
|
|
|
{
|
|
|
|
|
*df++ = tolower ((unsigned char)*filename);
|
|
|
|
|
++filename;
|
|
|
|
|
}
|
1996-05-14 02:38:20 +08:00
|
|
|
|
|
|
|
|
|
*df = 0;
|
|
|
|
|
|
1998-07-31 04:54:47 +08:00
|
|
|
|
return new_filename;
|
1996-05-14 02:38:20 +08:00
|
|
|
|
}
|
1998-07-31 04:54:47 +08:00
|
|
|
|
#endif /* HAVE_CASE_INSENSITIVE_FS */
|
1996-05-14 02:38:20 +08:00
|
|
|
|
|
1996-03-20 22:57:41 +08:00
|
|
|
|
#ifdef VMS
|
|
|
|
|
|
|
|
|
|
static int
|
2009-11-16 10:31:23 +08:00
|
|
|
|
vms_hash (const char *name)
|
1996-03-20 22:57:41 +08:00
|
|
|
|
{
|
|
|
|
|
int h = 0;
|
|
|
|
|
|
|
|
|
|
while (*name)
|
|
|
|
|
{
|
2000-07-31 02:26:42 +08:00
|
|
|
|
unsigned char uc = *name;
|
2013-07-22 05:52:13 +08:00
|
|
|
|
int g;
|
2006-01-04 22:45:16 +08:00
|
|
|
|
#ifdef HAVE_CASE_INSENSITIVE_FS
|
2000-07-31 02:26:42 +08:00
|
|
|
|
h = (h << 4) + (isupper (uc) ? tolower (uc) : uc);
|
2006-01-04 22:45:16 +08:00
|
|
|
|
#else
|
|
|
|
|
h = (h << 4) + uc;
|
|
|
|
|
#endif
|
2000-01-22 13:43:03 +08:00
|
|
|
|
name++;
|
1996-03-20 22:57:41 +08:00
|
|
|
|
g = h & 0xf0000000;
|
|
|
|
|
if (g)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
h = h ^ (g >> 24);
|
|
|
|
|
h = h ^ g;
|
|
|
|
|
}
|
1996-03-20 22:57:41 +08:00
|
|
|
|
}
|
|
|
|
|
return h;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* fake stat entry for a directory */
|
|
|
|
|
static int
|
2009-11-16 10:31:23 +08:00
|
|
|
|
vmsstat_dir (const char *name, struct stat *st)
|
1996-03-20 22:57:41 +08:00
|
|
|
|
{
|
|
|
|
|
char *s;
|
|
|
|
|
int h;
|
|
|
|
|
DIR *dir;
|
|
|
|
|
|
|
|
|
|
dir = opendir (name);
|
|
|
|
|
if (dir == 0)
|
|
|
|
|
return -1;
|
|
|
|
|
closedir (dir);
|
2013-05-17 14:29:46 +08:00
|
|
|
|
s = strchr (name, ':'); /* find device */
|
1996-03-20 22:57:41 +08:00
|
|
|
|
if (s)
|
|
|
|
|
{
|
2009-11-16 10:31:23 +08:00
|
|
|
|
/* to keep the compiler happy we said "const char *name", now we cheat */
|
1996-03-20 22:57:41 +08:00
|
|
|
|
*s++ = 0;
|
|
|
|
|
st->st_dev = (char *)vms_hash (name);
|
1997-08-28 04:30:54 +08:00
|
|
|
|
h = vms_hash (s);
|
|
|
|
|
*(s-1) = ':';
|
1996-03-20 22:57:41 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
st->st_dev = 0;
|
2009-11-16 10:31:23 +08:00
|
|
|
|
h = vms_hash (name);
|
1996-03-20 22:57:41 +08:00
|
|
|
|
}
|
1997-08-28 04:30:54 +08:00
|
|
|
|
|
1996-03-20 22:57:41 +08:00
|
|
|
|
st->st_ino[0] = h & 0xff;
|
|
|
|
|
st->st_ino[1] = h & 0xff00;
|
|
|
|
|
st->st_ino[2] = h >> 16;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif /* VMS */
|
1994-07-26 07:01:19 +08:00
|
|
|
|
|
1991-10-19 03:26:24 +08:00
|
|
|
|
/* Hash table of directories. */
|
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
#ifndef DIRECTORY_BUCKETS
|
1992-10-10 01:35:07 +08:00
|
|
|
|
#define DIRECTORY_BUCKETS 199
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
struct directory_contents
|
1991-10-19 03:26:24 +08:00
|
|
|
|
{
|
2013-05-17 14:29:46 +08:00
|
|
|
|
dev_t dev; /* Device and inode numbers of this dir. */
|
1997-04-07 15:21:16 +08:00
|
|
|
|
#ifdef WINDOWS32
|
2006-11-19 04:53:44 +08:00
|
|
|
|
/* Inode means nothing on WINDOWS32. Even file key information is
|
|
|
|
|
* unreliable because it is random per file open and undefined for remote
|
|
|
|
|
* filesystems. The most unique attribute I can come up with is the fully
|
|
|
|
|
* qualified name of the directory. Beware though, this is also
|
|
|
|
|
* unreliable. I'm open to suggestion on a better way to emulate inode. */
|
1996-05-23 05:51:45 +08:00
|
|
|
|
char *path_key;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
int ctime;
|
1996-05-23 05:51:45 +08:00
|
|
|
|
int mtime; /* controls check for stale directory cache */
|
1997-04-07 15:21:16 +08:00
|
|
|
|
int fs_flags; /* FS_FAT, FS_NTFS, ... */
|
2006-11-19 04:53:44 +08:00
|
|
|
|
# define FS_FAT 0x1
|
|
|
|
|
# define FS_NTFS 0x2
|
|
|
|
|
# define FS_UNKNOWN 0x4
|
1996-05-23 05:51:45 +08:00
|
|
|
|
#else
|
2006-11-19 04:53:44 +08:00
|
|
|
|
# ifdef VMS
|
1996-03-20 22:57:41 +08:00
|
|
|
|
ino_t ino[3];
|
2006-11-19 04:53:44 +08:00
|
|
|
|
# else
|
1996-03-20 22:57:41 +08:00
|
|
|
|
ino_t ino;
|
2006-11-19 04:53:44 +08:00
|
|
|
|
# endif
|
1997-04-07 15:21:16 +08:00
|
|
|
|
#endif /* WINDOWS32 */
|
2013-05-17 14:29:46 +08:00
|
|
|
|
struct hash_table dirfiles; /* Files in this directory. */
|
|
|
|
|
DIR *dirstream; /* Stream reading this directory. */
|
1991-10-19 03:26:24 +08:00
|
|
|
|
};
|
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
static unsigned long
|
2002-10-15 05:54:04 +08:00
|
|
|
|
directory_contents_hash_1 (const void *key_0)
|
2002-07-11 14:38:57 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const struct directory_contents *key = key_0;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
unsigned long hash;
|
|
|
|
|
|
|
|
|
|
#ifdef WINDOWS32
|
2003-07-19 10:46:25 +08:00
|
|
|
|
hash = 0;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
ISTRING_HASH_1 (key->path_key, hash);
|
|
|
|
|
hash ^= ((unsigned int) key->dev << 4) ^ (unsigned int) key->ctime;
|
|
|
|
|
#else
|
|
|
|
|
# ifdef VMS
|
|
|
|
|
hash = (((unsigned int) key->dev << 4)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
^ ((unsigned int) key->ino[0]
|
|
|
|
|
+ (unsigned int) key->ino[1]
|
|
|
|
|
+ (unsigned int) key->ino[2]));
|
2002-07-11 14:38:57 +08:00
|
|
|
|
# else
|
|
|
|
|
hash = ((unsigned int) key->dev << 4) ^ (unsigned int) key->ino;
|
|
|
|
|
# endif
|
|
|
|
|
#endif /* WINDOWS32 */
|
|
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static unsigned long
|
2002-10-15 05:54:04 +08:00
|
|
|
|
directory_contents_hash_2 (const void *key_0)
|
2002-07-11 14:38:57 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const struct directory_contents *key = key_0;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
unsigned long hash;
|
|
|
|
|
|
|
|
|
|
#ifdef WINDOWS32
|
2003-07-19 10:46:25 +08:00
|
|
|
|
hash = 0;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
ISTRING_HASH_2 (key->path_key, hash);
|
|
|
|
|
hash ^= ((unsigned int) key->dev << 4) ^ (unsigned int) ~key->ctime;
|
|
|
|
|
#else
|
|
|
|
|
# ifdef VMS
|
|
|
|
|
hash = (((unsigned int) key->dev << 4)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
^ ~((unsigned int) key->ino[0]
|
|
|
|
|
+ (unsigned int) key->ino[1]
|
|
|
|
|
+ (unsigned int) key->ino[2]));
|
2002-07-11 14:38:57 +08:00
|
|
|
|
# else
|
|
|
|
|
hash = ((unsigned int) key->dev << 4) ^ (unsigned int) ~key->ino;
|
|
|
|
|
# endif
|
|
|
|
|
#endif /* WINDOWS32 */
|
|
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-07 00:21:59 +08:00
|
|
|
|
/* Sometimes it's OK to use subtraction to get this value:
|
|
|
|
|
result = X - Y;
|
|
|
|
|
But, if we're not sure of the type of X and Y they may be too large for an
|
|
|
|
|
int (on a 64-bit system for example). So, use ?: instead.
|
|
|
|
|
See Savannah bug #15534.
|
|
|
|
|
|
|
|
|
|
NOTE! This macro has side-effects!
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define MAKECMP(_x,_y) ((_x)<(_y)?-1:((_x)==(_y)?0:1))
|
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
static int
|
2002-10-15 05:54:04 +08:00
|
|
|
|
directory_contents_hash_cmp (const void *xv, const void *yv)
|
2002-07-11 14:38:57 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const struct directory_contents *x = xv;
|
|
|
|
|
const struct directory_contents *y = yv;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
#ifdef WINDOWS32
|
|
|
|
|
ISTRING_COMPARE (x->path_key, y->path_key, result);
|
|
|
|
|
if (result)
|
|
|
|
|
return result;
|
2006-02-07 00:21:59 +08:00
|
|
|
|
result = MAKECMP(x->ctime, y->ctime);
|
2002-07-11 14:38:57 +08:00
|
|
|
|
if (result)
|
|
|
|
|
return result;
|
|
|
|
|
#else
|
|
|
|
|
# ifdef VMS
|
2006-02-07 00:21:59 +08:00
|
|
|
|
result = MAKECMP(x->ino[0], y->ino[0]);
|
2002-07-11 14:38:57 +08:00
|
|
|
|
if (result)
|
|
|
|
|
return result;
|
2006-02-07 00:21:59 +08:00
|
|
|
|
result = MAKECMP(x->ino[1], y->ino[1]);
|
2002-07-11 14:38:57 +08:00
|
|
|
|
if (result)
|
|
|
|
|
return result;
|
2006-02-07 00:21:59 +08:00
|
|
|
|
result = MAKECMP(x->ino[2], y->ino[2]);
|
2002-07-11 14:38:57 +08:00
|
|
|
|
if (result)
|
|
|
|
|
return result;
|
|
|
|
|
# else
|
2006-02-07 00:21:59 +08:00
|
|
|
|
result = MAKECMP(x->ino, y->ino);
|
2002-07-11 14:38:57 +08:00
|
|
|
|
if (result)
|
|
|
|
|
return result;
|
|
|
|
|
# endif
|
|
|
|
|
#endif /* WINDOWS32 */
|
|
|
|
|
|
2006-02-07 00:21:59 +08:00
|
|
|
|
return MAKECMP(x->dev, y->dev);
|
2002-07-11 14:38:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
1992-10-10 01:35:07 +08:00
|
|
|
|
/* Table of directory contents hashed by device and inode number. */
|
2002-07-11 14:38:57 +08:00
|
|
|
|
static struct hash_table directory_contents;
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
1992-10-10 01:35:07 +08:00
|
|
|
|
struct directory
|
|
|
|
|
{
|
2013-05-17 14:29:46 +08:00
|
|
|
|
const char *name; /* Name of the directory. */
|
1992-10-10 01:35:07 +08:00
|
|
|
|
|
|
|
|
|
/* The directory's contents. This data may be shared by several
|
|
|
|
|
entries in the hash table, which refer to the same directory
|
2012-03-04 08:24:20 +08:00
|
|
|
|
(identified uniquely by 'dev' and 'ino') under different names. */
|
1992-10-10 01:35:07 +08:00
|
|
|
|
struct directory_contents *contents;
|
|
|
|
|
};
|
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
static unsigned long
|
2002-10-15 05:54:04 +08:00
|
|
|
|
directory_hash_1 (const void *key)
|
2002-07-11 14:38:57 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
return_ISTRING_HASH_1 (((const struct directory *) key)->name);
|
2002-07-11 14:38:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static unsigned long
|
2002-10-15 05:54:04 +08:00
|
|
|
|
directory_hash_2 (const void *key)
|
2002-07-11 14:38:57 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
return_ISTRING_HASH_2 (((const struct directory *) key)->name);
|
2002-07-11 14:38:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2002-10-15 05:54:04 +08:00
|
|
|
|
directory_hash_cmp (const void *x, const void *y)
|
2002-07-11 14:38:57 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
return_ISTRING_COMPARE (((const struct directory *) x)->name,
|
2013-05-17 14:29:46 +08:00
|
|
|
|
((const struct directory *) y)->name);
|
2002-07-11 14:38:57 +08:00
|
|
|
|
}
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
/* Table of directories hashed by name. */
|
|
|
|
|
static struct hash_table directories;
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
|
|
|
|
/* Never have more than this many directories open at once. */
|
|
|
|
|
|
|
|
|
|
#define MAX_OPEN_DIRECTORIES 10
|
|
|
|
|
|
|
|
|
|
static unsigned int open_directories = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Hash table of files in each directory. */
|
|
|
|
|
|
|
|
|
|
struct dirfile
|
|
|
|
|
{
|
2013-05-17 14:29:46 +08:00
|
|
|
|
const char *name; /* Name of the file. */
|
2002-07-11 14:38:57 +08:00
|
|
|
|
short length;
|
2013-05-17 14:29:46 +08:00
|
|
|
|
short impossible; /* This file is impossible. */
|
1991-10-19 03:26:24 +08:00
|
|
|
|
};
|
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
static unsigned long
|
2002-10-15 05:54:04 +08:00
|
|
|
|
dirfile_hash_1 (const void *key)
|
2002-07-11 14:38:57 +08:00
|
|
|
|
{
|
|
|
|
|
return_ISTRING_HASH_1 (((struct dirfile const *) key)->name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static unsigned long
|
2002-10-15 05:54:04 +08:00
|
|
|
|
dirfile_hash_2 (const void *key)
|
2002-07-11 14:38:57 +08:00
|
|
|
|
{
|
|
|
|
|
return_ISTRING_HASH_2 (((struct dirfile const *) key)->name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2002-10-15 05:54:04 +08:00
|
|
|
|
dirfile_hash_cmp (const void *xv, const void *yv)
|
2002-07-11 14:38:57 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const struct dirfile *x = xv;
|
|
|
|
|
const struct dirfile *y = yv;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
int result = x->length - y->length;
|
|
|
|
|
if (result)
|
|
|
|
|
return result;
|
|
|
|
|
return_ISTRING_COMPARE (x->name, y->name);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
#ifndef DIRFILE_BUCKETS
|
1992-10-10 01:35:07 +08:00
|
|
|
|
#define DIRFILE_BUCKETS 107
|
1991-10-19 03:26:24 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
2006-11-19 04:53:44 +08:00
|
|
|
|
static int dir_contents_file_exists_p (struct directory_contents *dir,
|
|
|
|
|
const char *filename);
|
|
|
|
|
static struct directory *find_directory (const char *name);
|
1992-10-10 01:35:07 +08:00
|
|
|
|
|
2012-03-04 08:24:20 +08:00
|
|
|
|
/* Find the directory named NAME and return its 'struct directory'. */
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
|
|
|
|
static struct directory *
|
2006-11-19 04:53:44 +08:00
|
|
|
|
find_directory (const char *name)
|
1991-10-19 03:26:24 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
struct directory *dir;
|
|
|
|
|
struct directory **dir_slot;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
struct directory dir_key;
|
2013-07-22 05:52:13 +08:00
|
|
|
|
|
1996-03-20 22:57:41 +08:00
|
|
|
|
#ifdef VMS
|
|
|
|
|
if ((*name == '.') && (*(name+1) == 0))
|
|
|
|
|
name = "[]";
|
|
|
|
|
else
|
|
|
|
|
name = vmsify (name,1);
|
|
|
|
|
#endif
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
dir_key.name = name;
|
|
|
|
|
dir_slot = (struct directory **) hash_find_slot (&directories, &dir_key);
|
|
|
|
|
dir = *dir_slot;
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
if (HASH_VACANT (dir))
|
1991-10-19 03:26:24 +08:00
|
|
|
|
{
|
1992-10-10 01:35:07 +08:00
|
|
|
|
/* The directory was not found. Create a new entry for it. */
|
2013-07-22 05:52:13 +08:00
|
|
|
|
const char *p = name + strlen (name);
|
|
|
|
|
struct stat st;
|
|
|
|
|
int r;
|
1992-10-10 01:35:07 +08:00
|
|
|
|
|
2006-04-10 06:09:24 +08:00
|
|
|
|
dir = xmalloc (sizeof (struct directory));
|
2009-11-16 10:31:23 +08:00
|
|
|
|
#if defined(HAVE_CASE_INSENSITIVE_FS) && defined(VMS)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
dir->name = strcache_add_len (downcase (name), p - name);
|
2009-11-16 10:31:23 +08:00
|
|
|
|
#else
|
2006-11-19 04:53:44 +08:00
|
|
|
|
dir->name = strcache_add_len (name, p - name);
|
2009-11-16 10:31:23 +08:00
|
|
|
|
#endif
|
2002-07-11 14:38:57 +08:00
|
|
|
|
hash_insert_at (&directories, dir, dir_slot);
|
1992-10-10 01:35:07 +08:00
|
|
|
|
/* The directory is not in the name hash table.
|
2013-05-17 14:29:46 +08:00
|
|
|
|
Find its device and inode numbers, and look it up by them. */
|
1992-10-10 01:35:07 +08:00
|
|
|
|
|
2000-06-07 13:43:37 +08:00
|
|
|
|
#ifdef VMS
|
|
|
|
|
r = vmsstat_dir (name, &st);
|
2007-10-10 12:32:15 +08:00
|
|
|
|
#elif defined(WINDOWS32)
|
|
|
|
|
{
|
|
|
|
|
char tem[MAXPATHLEN], *tstart, *tend;
|
|
|
|
|
|
|
|
|
|
/* Remove any trailing slashes. Windows32 stat fails even on
|
|
|
|
|
valid directories if they end in a slash. */
|
|
|
|
|
memcpy (tem, name, p - name + 1);
|
|
|
|
|
tstart = tem;
|
|
|
|
|
if (tstart[1] == ':')
|
|
|
|
|
tstart += 2;
|
|
|
|
|
for (tend = tem + (p - name - 1);
|
|
|
|
|
tend > tstart && (*tend == '/' || *tend == '\\');
|
|
|
|
|
tend--)
|
|
|
|
|
*tend = '\0';
|
|
|
|
|
|
|
|
|
|
r = stat (tem, &st);
|
|
|
|
|
}
|
2000-06-07 13:43:37 +08:00
|
|
|
|
#else
|
2003-01-30 13:22:52 +08:00
|
|
|
|
EINTRLOOP (r, stat (name, &st));
|
2000-06-07 13:43:37 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (r < 0)
|
|
|
|
|
{
|
2013-05-17 14:29:46 +08:00
|
|
|
|
/* Couldn't stat the directory. Mark this by
|
|
|
|
|
setting the 'contents' member to a nil pointer. */
|
|
|
|
|
dir->contents = 0;
|
|
|
|
|
}
|
1991-10-19 03:26:24 +08:00
|
|
|
|
else
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
/* Search the contents hash table; device and inode are the key. */
|
1992-10-10 01:35:07 +08:00
|
|
|
|
|
2013-07-22 05:52:13 +08:00
|
|
|
|
#ifdef WINDOWS32
|
|
|
|
|
char *w32_path;
|
|
|
|
|
#endif
|
2013-05-17 14:29:46 +08:00
|
|
|
|
struct directory_contents *dc;
|
|
|
|
|
struct directory_contents **dc_slot;
|
|
|
|
|
struct directory_contents dc_key;
|
1992-10-10 01:35:07 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
dc_key.dev = st.st_dev;
|
1997-04-07 15:21:16 +08:00
|
|
|
|
#ifdef WINDOWS32
|
2013-05-17 14:29:46 +08:00
|
|
|
|
dc_key.path_key = w32_path = w32ify (name, 1);
|
|
|
|
|
dc_key.ctime = st.st_ctime;
|
1996-05-23 05:51:45 +08:00
|
|
|
|
#else
|
2000-01-22 13:43:03 +08:00
|
|
|
|
# ifdef VMS
|
2013-05-17 14:29:46 +08:00
|
|
|
|
dc_key.ino[0] = st.st_ino[0];
|
|
|
|
|
dc_key.ino[1] = st.st_ino[1];
|
|
|
|
|
dc_key.ino[2] = st.st_ino[2];
|
2000-01-22 13:43:03 +08:00
|
|
|
|
# else
|
2013-05-17 14:29:46 +08:00
|
|
|
|
dc_key.ino = st.st_ino;
|
2000-01-22 13:43:03 +08:00
|
|
|
|
# endif
|
1996-03-20 22:57:41 +08:00
|
|
|
|
#endif
|
2013-05-17 14:29:46 +08:00
|
|
|
|
dc_slot = (struct directory_contents **) hash_find_slot (&directory_contents, &dc_key);
|
|
|
|
|
dc = *dc_slot;
|
1992-10-10 01:35:07 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
if (HASH_VACANT (dc))
|
|
|
|
|
{
|
|
|
|
|
/* Nope; this really is a directory we haven't seen before. */
|
2013-07-22 05:52:13 +08:00
|
|
|
|
#ifdef WINDOWS32
|
|
|
|
|
char fs_label[BUFSIZ];
|
|
|
|
|
char fs_type[BUFSIZ];
|
|
|
|
|
unsigned long fs_serno;
|
|
|
|
|
unsigned long fs_flags;
|
|
|
|
|
unsigned long fs_len;
|
|
|
|
|
#endif
|
2013-05-17 14:29:46 +08:00
|
|
|
|
dc = (struct directory_contents *)
|
|
|
|
|
xmalloc (sizeof (struct directory_contents));
|
1992-10-10 01:35:07 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
/* Enter it in the contents hash table. */
|
|
|
|
|
dc->dev = st.st_dev;
|
1997-04-07 15:21:16 +08:00
|
|
|
|
#ifdef WINDOWS32
|
2002-07-11 14:38:57 +08:00
|
|
|
|
dc->path_key = xstrdup (w32_path);
|
2013-05-17 14:29:46 +08:00
|
|
|
|
dc->ctime = st.st_ctime;
|
1996-05-23 05:51:45 +08:00
|
|
|
|
dc->mtime = st.st_mtime;
|
1997-04-07 15:21:16 +08:00
|
|
|
|
|
2013-07-22 05:52:13 +08:00
|
|
|
|
/* NTFS is the only WINDOWS32 filesystem that bumps mtime on a
|
|
|
|
|
directory when files are added/deleted from a directory. */
|
1997-04-07 15:21:16 +08:00
|
|
|
|
w32_path[3] = '\0';
|
2013-05-17 14:29:46 +08:00
|
|
|
|
if (GetVolumeInformation (w32_path, fs_label, sizeof (fs_label),
|
|
|
|
|
&fs_serno, &fs_len, &fs_flags, fs_type,
|
|
|
|
|
sizeof (fs_type)) == FALSE)
|
1997-04-07 15:21:16 +08:00
|
|
|
|
dc->fs_flags = FS_UNKNOWN;
|
2013-05-17 14:29:46 +08:00
|
|
|
|
else if (!strcmp (fs_type, "FAT"))
|
1997-04-07 15:21:16 +08:00
|
|
|
|
dc->fs_flags = FS_FAT;
|
2013-05-17 14:29:46 +08:00
|
|
|
|
else if (!strcmp (fs_type, "NTFS"))
|
1997-04-07 15:21:16 +08:00
|
|
|
|
dc->fs_flags = FS_NTFS;
|
|
|
|
|
else
|
|
|
|
|
dc->fs_flags = FS_UNKNOWN;
|
1996-05-23 05:51:45 +08:00
|
|
|
|
#else
|
2000-01-22 13:43:03 +08:00
|
|
|
|
# ifdef VMS
|
2013-05-17 14:29:46 +08:00
|
|
|
|
dc->ino[0] = st.st_ino[0];
|
|
|
|
|
dc->ino[1] = st.st_ino[1];
|
|
|
|
|
dc->ino[2] = st.st_ino[2];
|
2000-01-22 13:43:03 +08:00
|
|
|
|
# else
|
2013-05-17 14:29:46 +08:00
|
|
|
|
dc->ino = st.st_ino;
|
2000-01-22 13:43:03 +08:00
|
|
|
|
# endif
|
1997-04-07 15:21:16 +08:00
|
|
|
|
#endif /* WINDOWS32 */
|
2013-05-17 14:29:46 +08:00
|
|
|
|
hash_insert_at (&directory_contents, dc, dc_slot);
|
|
|
|
|
ENULLLOOP (dc->dirstream, opendir (name));
|
|
|
|
|
if (dc->dirstream == 0)
|
2006-11-19 04:53:44 +08:00
|
|
|
|
/* Couldn't open the directory. Mark this by setting the
|
2012-03-04 08:24:20 +08:00
|
|
|
|
'files' member to a nil pointer. */
|
2002-08-08 08:11:19 +08:00
|
|
|
|
dc->dirfiles.ht_vec = 0;
|
2013-05-17 14:29:46 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
hash_init (&dc->dirfiles, DIRFILE_BUCKETS,
|
|
|
|
|
dirfile_hash_1, dirfile_hash_2, dirfile_hash_cmp);
|
|
|
|
|
/* Keep track of how many directories are open. */
|
|
|
|
|
++open_directories;
|
|
|
|
|
if (open_directories == MAX_OPEN_DIRECTORIES)
|
|
|
|
|
/* We have too many directories open already.
|
|
|
|
|
Read the entire directory and then close it. */
|
|
|
|
|
dir_contents_file_exists_p (dc, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Point the name-hashed entry for DIR at its contents data. */
|
|
|
|
|
dir->contents = dc;
|
|
|
|
|
}
|
1991-10-19 03:26:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
|
}
|
|
|
|
|
|
1992-10-10 01:35:07 +08:00
|
|
|
|
/* Return 1 if the name FILENAME is entered in DIR's hash table.
|
1991-10-19 03:26:24 +08:00
|
|
|
|
FILENAME must contain no slashes. */
|
|
|
|
|
|
1992-10-10 01:35:07 +08:00
|
|
|
|
static int
|
2006-11-19 04:53:44 +08:00
|
|
|
|
dir_contents_file_exists_p (struct directory_contents *dir,
|
|
|
|
|
const char *filename)
|
1991-10-19 03:26:24 +08:00
|
|
|
|
{
|
2002-07-11 14:38:57 +08:00
|
|
|
|
struct dirfile *df;
|
|
|
|
|
struct dirent *d;
|
1997-04-07 15:21:16 +08:00
|
|
|
|
#ifdef WINDOWS32
|
1996-05-23 05:51:45 +08:00
|
|
|
|
struct stat st;
|
|
|
|
|
int rehash = 0;
|
|
|
|
|
#endif
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
if (dir == 0 || dir->dirfiles.ht_vec == 0)
|
1992-10-10 01:35:07 +08:00
|
|
|
|
/* The directory could not be stat'd or opened. */
|
2006-11-19 04:53:44 +08:00
|
|
|
|
return 0;
|
|
|
|
|
|
1994-07-26 07:01:19 +08:00
|
|
|
|
#ifdef __MSDOS__
|
|
|
|
|
filename = dosify (filename);
|
|
|
|
|
#endif
|
|
|
|
|
|
1998-07-31 04:54:47 +08:00
|
|
|
|
#ifdef HAVE_CASE_INSENSITIVE_FS
|
|
|
|
|
filename = downcase (filename);
|
1996-05-14 02:38:20 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
2003-03-25 07:14:15 +08:00
|
|
|
|
#ifdef __EMX__
|
2003-05-02 09:44:59 +08:00
|
|
|
|
if (filename != 0)
|
|
|
|
|
_fnlwr (filename); /* lower case for FAT drives */
|
2003-03-25 07:14:15 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
1996-03-20 22:57:41 +08:00
|
|
|
|
#ifdef VMS
|
|
|
|
|
filename = vmsify (filename,0);
|
|
|
|
|
#endif
|
|
|
|
|
|
1991-10-19 03:26:24 +08:00
|
|
|
|
if (filename != 0)
|
|
|
|
|
{
|
2002-07-11 14:38:57 +08:00
|
|
|
|
struct dirfile dirfile_key;
|
|
|
|
|
|
1991-10-19 03:26:24 +08:00
|
|
|
|
if (*filename == '\0')
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
/* Checking if the directory exists. */
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2002-07-11 14:38:57 +08:00
|
|
|
|
dirfile_key.name = filename;
|
|
|
|
|
dirfile_key.length = strlen (filename);
|
2006-11-19 04:53:44 +08:00
|
|
|
|
df = hash_find_item (&dir->dirfiles, &dirfile_key);
|
2002-07-11 14:38:57 +08:00
|
|
|
|
if (df)
|
2006-11-19 04:53:44 +08:00
|
|
|
|
return !df->impossible;
|
1991-10-19 03:26:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* The file was not found in the hashed list.
|
|
|
|
|
Try to read the directory further. */
|
|
|
|
|
|
|
|
|
|
if (dir->dirstream == 0)
|
1996-03-20 22:57:41 +08:00
|
|
|
|
{
|
1997-04-07 15:21:16 +08:00
|
|
|
|
#ifdef WINDOWS32
|
|
|
|
|
/*
|
|
|
|
|
* Check to see if directory has changed since last read. FAT
|
|
|
|
|
* filesystems force a rehash always as mtime does not change
|
|
|
|
|
* on directories (ugh!).
|
|
|
|
|
*/
|
2006-02-01 21:31:25 +08:00
|
|
|
|
if (dir->path_key)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
2006-02-12 03:02:21 +08:00
|
|
|
|
if ((dir->fs_flags & FS_FAT) != 0)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
dir->mtime = time ((time_t *) 0);
|
|
|
|
|
rehash = 1;
|
|
|
|
|
}
|
|
|
|
|
else if (stat (dir->path_key, &st) == 0 && st.st_mtime > dir->mtime)
|
|
|
|
|
{
|
|
|
|
|
/* reset date stamp to show most recent re-process. */
|
|
|
|
|
dir->mtime = st.st_mtime;
|
|
|
|
|
rehash = 1;
|
|
|
|
|
}
|
2006-02-12 03:02:21 +08:00
|
|
|
|
|
|
|
|
|
/* If it has been already read in, all done. */
|
2013-05-17 14:29:46 +08:00
|
|
|
|
if (!rehash)
|
|
|
|
|
return 0;
|
2006-02-12 03:02:21 +08:00
|
|
|
|
|
|
|
|
|
/* make sure directory can still be opened; if not return. */
|
2006-11-19 04:53:44 +08:00
|
|
|
|
dir->dirstream = opendir (dir->path_key);
|
2006-02-12 03:02:21 +08:00
|
|
|
|
if (!dir->dirstream)
|
|
|
|
|
return 0;
|
2013-05-17 14:29:46 +08:00
|
|
|
|
}
|
2002-07-11 14:38:57 +08:00
|
|
|
|
else
|
1996-05-23 05:51:45 +08:00
|
|
|
|
#endif
|
2013-05-17 14:29:46 +08:00
|
|
|
|
/* The directory has been all read in. */
|
|
|
|
|
return 0;
|
1996-03-20 22:57:41 +08:00
|
|
|
|
}
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
2003-01-30 13:22:52 +08:00
|
|
|
|
while (1)
|
1991-10-19 03:26:24 +08:00
|
|
|
|
{
|
|
|
|
|
/* Enter the file in the hash table. */
|
1993-06-03 04:56:37 +08:00
|
|
|
|
unsigned int len;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
struct dirfile dirfile_key;
|
|
|
|
|
struct dirfile **dirfile_slot;
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
2003-01-30 13:22:52 +08:00
|
|
|
|
ENULLLOOP (d, readdir (dir->dirstream));
|
|
|
|
|
if (d == 0)
|
2006-11-19 04:53:44 +08:00
|
|
|
|
{
|
|
|
|
|
if (errno)
|
|
|
|
|
fatal (NILF, "INTERNAL: readdir: %s\n", strerror (errno));
|
|
|
|
|
break;
|
|
|
|
|
}
|
2003-01-30 13:22:52 +08:00
|
|
|
|
|
2000-01-22 13:43:03 +08:00
|
|
|
|
#if defined(VMS) && defined(HAVE_DIRENT_H)
|
|
|
|
|
/* In VMS we get file versions too, which have to be stripped off */
|
|
|
|
|
{
|
|
|
|
|
char *p = strrchr (d->d_name, ';');
|
|
|
|
|
if (p)
|
|
|
|
|
*p = '\0';
|
|
|
|
|
}
|
|
|
|
|
#endif
|
1991-10-19 03:26:24 +08:00
|
|
|
|
if (!REAL_DIR_ENTRY (d))
|
2013-05-17 14:29:46 +08:00
|
|
|
|
continue;
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
1996-03-02 01:45:37 +08:00
|
|
|
|
len = NAMLEN (d);
|
2002-07-11 14:38:57 +08:00
|
|
|
|
dirfile_key.name = d->d_name;
|
|
|
|
|
dirfile_key.length = len;
|
|
|
|
|
dirfile_slot = (struct dirfile **) hash_find_slot (&dir->dirfiles, &dirfile_key);
|
1997-04-07 15:21:16 +08:00
|
|
|
|
#ifdef WINDOWS32
|
1996-05-23 05:51:45 +08:00
|
|
|
|
/*
|
|
|
|
|
* If re-reading a directory, don't cache files that have
|
|
|
|
|
* already been discovered.
|
|
|
|
|
*/
|
2002-07-11 14:38:57 +08:00
|
|
|
|
if (! rehash || HASH_VACANT (*dirfile_slot))
|
1996-05-23 05:51:45 +08:00
|
|
|
|
#endif
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
df = xmalloc (sizeof (struct dirfile));
|
2009-11-16 10:31:23 +08:00
|
|
|
|
#if defined(HAVE_CASE_INSENSITIVE_FS) && defined(VMS)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
df->name = strcache_add_len (downcase (d->d_name), len);
|
2009-11-16 10:31:23 +08:00
|
|
|
|
#else
|
2013-05-17 14:29:46 +08:00
|
|
|
|
df->name = strcache_add_len (d->d_name, len);
|
2009-11-16 10:31:23 +08:00
|
|
|
|
#endif
|
2013-05-17 14:29:46 +08:00
|
|
|
|
df->length = len;
|
|
|
|
|
df->impossible = 0;
|
|
|
|
|
hash_insert_at (&dir->dirfiles, df, dirfile_slot);
|
|
|
|
|
}
|
1991-10-19 03:26:24 +08:00
|
|
|
|
/* Check if the name matches the one we're searching for. */
|
2010-07-01 13:59:08 +08:00
|
|
|
|
if (filename != 0 && patheq (d->d_name, filename))
|
2006-11-19 04:53:44 +08:00
|
|
|
|
return 1;
|
1991-10-19 03:26:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If the directory has been completely read in,
|
|
|
|
|
close the stream and reset the pointer to nil. */
|
|
|
|
|
if (d == 0)
|
|
|
|
|
{
|
|
|
|
|
--open_directories;
|
|
|
|
|
closedir (dir->dirstream);
|
|
|
|
|
dir->dirstream = 0;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
1992-10-10 01:35:07 +08:00
|
|
|
|
|
|
|
|
|
/* Return 1 if the name FILENAME in directory DIRNAME
|
|
|
|
|
is entered in the dir hash table.
|
|
|
|
|
FILENAME must contain no slashes. */
|
|
|
|
|
|
|
|
|
|
int
|
2006-11-19 04:53:44 +08:00
|
|
|
|
dir_file_exists_p (const char *dirname, const char *filename)
|
1992-10-10 01:35:07 +08:00
|
|
|
|
{
|
|
|
|
|
return dir_contents_file_exists_p (find_directory (dirname)->contents,
|
2013-05-17 14:29:46 +08:00
|
|
|
|
filename);
|
1992-10-10 01:35:07 +08:00
|
|
|
|
}
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
|
|
|
|
/* Return 1 if the file named NAME exists. */
|
|
|
|
|
|
|
|
|
|
int
|
2006-11-19 04:53:44 +08:00
|
|
|
|
file_exists_p (const char *name)
|
1991-10-19 03:26:24 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const char *dirend;
|
|
|
|
|
const char *dirname;
|
|
|
|
|
const char *slash;
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
2013-05-17 14:29:46 +08:00
|
|
|
|
#ifndef NO_ARCHIVES
|
1991-10-19 03:26:24 +08:00
|
|
|
|
if (ar_name (name))
|
|
|
|
|
return ar_member_date (name) != (time_t) -1;
|
|
|
|
|
#endif
|
|
|
|
|
|
1996-03-20 22:57:41 +08:00
|
|
|
|
#ifdef VMS
|
1999-10-15 15:00:58 +08:00
|
|
|
|
dirend = strrchr (name, ']');
|
2000-01-22 13:43:03 +08:00
|
|
|
|
if (dirend == 0)
|
|
|
|
|
dirend = strrchr (name, ':');
|
2006-04-10 06:09:24 +08:00
|
|
|
|
if (dirend == 0)
|
1996-03-20 22:57:41 +08:00
|
|
|
|
return dir_file_exists_p ("[]", name);
|
1996-05-14 02:38:20 +08:00
|
|
|
|
#else /* !VMS */
|
1999-10-15 15:00:58 +08:00
|
|
|
|
dirend = strrchr (name, '/');
|
2002-08-10 09:27:16 +08:00
|
|
|
|
#ifdef HAVE_DOS_PATHS
|
1997-04-07 15:21:16 +08:00
|
|
|
|
/* Forward and backslashes might be mixed. We need the rightmost one. */
|
|
|
|
|
{
|
2013-05-17 14:29:46 +08:00
|
|
|
|
const char *bslash = strrchr (name, '\\');
|
1997-04-07 15:21:16 +08:00
|
|
|
|
if (!dirend || bslash > dirend)
|
|
|
|
|
dirend = bslash;
|
1998-07-31 04:54:47 +08:00
|
|
|
|
/* The case of "d:file". */
|
|
|
|
|
if (!dirend && name[0] && name[1] == ':')
|
|
|
|
|
dirend = name + 1;
|
1997-04-07 15:21:16 +08:00
|
|
|
|
}
|
2002-08-10 09:27:16 +08:00
|
|
|
|
#endif /* HAVE_DOS_PATHS */
|
1996-05-14 02:38:20 +08:00
|
|
|
|
if (dirend == 0)
|
|
|
|
|
#ifndef _AMIGA
|
|
|
|
|
return dir_file_exists_p (".", name);
|
|
|
|
|
#else /* !VMS && !AMIGA */
|
|
|
|
|
return dir_file_exists_p ("", name);
|
|
|
|
|
#endif /* AMIGA */
|
|
|
|
|
#endif /* VMS */
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
1998-07-31 04:54:47 +08:00
|
|
|
|
slash = dirend;
|
1997-04-07 15:21:16 +08:00
|
|
|
|
if (dirend == name)
|
|
|
|
|
dirname = "/";
|
|
|
|
|
else
|
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
char *p;
|
2002-08-10 09:27:16 +08:00
|
|
|
|
#ifdef HAVE_DOS_PATHS
|
1998-07-31 04:54:47 +08:00
|
|
|
|
/* d:/ and d: are *very* different... */
|
|
|
|
|
if (dirend < name + 3 && name[1] == ':' &&
|
2013-05-17 14:29:46 +08:00
|
|
|
|
(*dirend == '/' || *dirend == '\\' || *dirend == ':'))
|
|
|
|
|
dirend++;
|
1998-07-31 04:54:47 +08:00
|
|
|
|
#endif
|
2006-11-19 04:53:44 +08:00
|
|
|
|
p = alloca (dirend - name + 1);
|
|
|
|
|
memcpy (p, name, dirend - name);
|
|
|
|
|
p[dirend - name] = '\0';
|
|
|
|
|
dirname = p;
|
1997-04-07 15:21:16 +08:00
|
|
|
|
}
|
1998-07-31 04:54:47 +08:00
|
|
|
|
return dir_file_exists_p (dirname, slash + 1);
|
1991-10-19 03:26:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-03-04 08:24:20 +08:00
|
|
|
|
/* Mark FILENAME as 'impossible' for 'file_impossible_p'.
|
1991-10-19 03:26:24 +08:00
|
|
|
|
This means an attempt has been made to search for FILENAME
|
|
|
|
|
as an intermediate file, and it has failed. */
|
|
|
|
|
|
|
|
|
|
void
|
2006-11-19 04:53:44 +08:00
|
|
|
|
file_impossible (const char *filename)
|
1991-10-19 03:26:24 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const char *dirend;
|
|
|
|
|
const char *p = filename;
|
|
|
|
|
struct directory *dir;
|
|
|
|
|
struct dirfile *new;
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
1996-03-20 22:57:41 +08:00
|
|
|
|
#ifdef VMS
|
1999-10-15 15:00:58 +08:00
|
|
|
|
dirend = strrchr (p, ']');
|
2000-01-22 13:43:03 +08:00
|
|
|
|
if (dirend == 0)
|
2000-02-05 15:50:47 +08:00
|
|
|
|
dirend = strrchr (p, ':');
|
1996-03-20 22:57:41 +08:00
|
|
|
|
dirend++;
|
|
|
|
|
if (dirend == (char *)1)
|
|
|
|
|
dir = find_directory ("[]");
|
|
|
|
|
#else
|
1999-10-15 15:00:58 +08:00
|
|
|
|
dirend = strrchr (p, '/');
|
2002-08-10 09:27:16 +08:00
|
|
|
|
# ifdef HAVE_DOS_PATHS
|
1997-04-07 15:21:16 +08:00
|
|
|
|
/* Forward and backslashes might be mixed. We need the rightmost one. */
|
|
|
|
|
{
|
2013-05-17 14:29:46 +08:00
|
|
|
|
const char *bslash = strrchr (p, '\\');
|
1997-04-07 15:21:16 +08:00
|
|
|
|
if (!dirend || bslash > dirend)
|
|
|
|
|
dirend = bslash;
|
1998-07-31 04:54:47 +08:00
|
|
|
|
/* The case of "d:file". */
|
|
|
|
|
if (!dirend && p[0] && p[1] == ':')
|
|
|
|
|
dirend = p + 1;
|
1997-04-07 15:21:16 +08:00
|
|
|
|
}
|
2002-08-10 09:27:16 +08:00
|
|
|
|
# endif /* HAVE_DOS_PATHS */
|
1991-10-19 03:26:24 +08:00
|
|
|
|
if (dirend == 0)
|
2001-06-01 11:56:50 +08:00
|
|
|
|
# ifdef _AMIGA
|
1996-05-14 02:38:20 +08:00
|
|
|
|
dir = find_directory ("");
|
2001-06-01 11:56:50 +08:00
|
|
|
|
# else /* !VMS && !AMIGA */
|
1991-10-19 03:26:24 +08:00
|
|
|
|
dir = find_directory (".");
|
2001-06-01 11:56:50 +08:00
|
|
|
|
# endif /* AMIGA */
|
1996-05-14 02:38:20 +08:00
|
|
|
|
#endif /* VMS */
|
1991-10-19 03:26:24 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const char *dirname;
|
|
|
|
|
const char *slash = dirend;
|
1997-04-07 15:21:16 +08:00
|
|
|
|
if (dirend == p)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
dirname = "/";
|
1997-04-07 15:21:16 +08:00
|
|
|
|
else
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
char *cp;
|
2002-08-10 09:27:16 +08:00
|
|
|
|
#ifdef HAVE_DOS_PATHS
|
2013-05-17 14:29:46 +08:00
|
|
|
|
/* d:/ and d: are *very* different... */
|
|
|
|
|
if (dirend < p + 3 && p[1] == ':' &&
|
|
|
|
|
(*dirend == '/' || *dirend == '\\' || *dirend == ':'))
|
|
|
|
|
dirend++;
|
1998-07-31 04:54:47 +08:00
|
|
|
|
#endif
|
2013-05-17 14:29:46 +08:00
|
|
|
|
cp = alloca (dirend - p + 1);
|
|
|
|
|
memcpy (cp, p, dirend - p);
|
|
|
|
|
cp[dirend - p] = '\0';
|
2006-11-19 04:53:44 +08:00
|
|
|
|
dirname = cp;
|
2013-05-17 14:29:46 +08:00
|
|
|
|
}
|
1991-10-19 03:26:24 +08:00
|
|
|
|
dir = find_directory (dirname);
|
1998-07-31 04:54:47 +08:00
|
|
|
|
filename = p = slash + 1;
|
1991-10-19 03:26:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
1992-10-10 01:35:07 +08:00
|
|
|
|
if (dir->contents == 0)
|
2010-07-01 13:59:08 +08:00
|
|
|
|
/* 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 = xcalloc (sizeof (struct directory_contents));
|
1992-10-10 01:35:07 +08:00
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
if (dir->contents->dirfiles.ht_vec == 0)
|
1991-10-19 03:26:24 +08:00
|
|
|
|
{
|
2002-07-11 14:38:57 +08:00
|
|
|
|
hash_init (&dir->contents->dirfiles, DIRFILE_BUCKETS,
|
2013-05-17 14:29:46 +08:00
|
|
|
|
dirfile_hash_1, dirfile_hash_2, dirfile_hash_cmp);
|
1991-10-19 03:26:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Make a new entry and put it in the table. */
|
|
|
|
|
|
2006-04-10 06:09:24 +08:00
|
|
|
|
new = xmalloc (sizeof (struct dirfile));
|
2002-07-11 14:38:57 +08:00
|
|
|
|
new->length = strlen (filename);
|
2009-11-16 10:31:23 +08:00
|
|
|
|
#if defined(HAVE_CASE_INSENSITIVE_FS) && defined(VMS)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
new->name = strcache_add_len (downcase (filename), new->length);
|
2009-11-16 10:31:23 +08:00
|
|
|
|
#else
|
2006-11-19 04:53:44 +08:00
|
|
|
|
new->name = strcache_add_len (filename, new->length);
|
2009-11-16 10:31:23 +08:00
|
|
|
|
#endif
|
1991-10-19 03:26:24 +08:00
|
|
|
|
new->impossible = 1;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
hash_insert (&dir->contents->dirfiles, new);
|
1991-10-19 03:26:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Return nonzero if FILENAME has been marked impossible. */
|
|
|
|
|
|
|
|
|
|
int
|
2006-11-19 04:53:44 +08:00
|
|
|
|
file_impossible_p (const char *filename)
|
1991-10-19 03:26:24 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const char *dirend;
|
|
|
|
|
struct directory_contents *dir;
|
|
|
|
|
struct dirfile *dirfile;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
struct dirfile dirfile_key;
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
1996-03-20 22:57:41 +08:00
|
|
|
|
#ifdef VMS
|
1999-10-15 15:00:58 +08:00
|
|
|
|
dirend = strrchr (filename, ']');
|
1996-03-20 22:57:41 +08:00
|
|
|
|
if (dirend == 0)
|
|
|
|
|
dir = find_directory ("[]")->contents;
|
|
|
|
|
#else
|
1999-10-15 15:00:58 +08:00
|
|
|
|
dirend = strrchr (filename, '/');
|
2002-08-10 09:27:16 +08:00
|
|
|
|
#ifdef HAVE_DOS_PATHS
|
1997-04-07 15:21:16 +08:00
|
|
|
|
/* Forward and backslashes might be mixed. We need the rightmost one. */
|
|
|
|
|
{
|
2013-05-17 14:29:46 +08:00
|
|
|
|
const char *bslash = strrchr (filename, '\\');
|
1997-04-07 15:21:16 +08:00
|
|
|
|
if (!dirend || bslash > dirend)
|
|
|
|
|
dirend = bslash;
|
1998-07-31 04:54:47 +08:00
|
|
|
|
/* The case of "d:file". */
|
|
|
|
|
if (!dirend && filename[0] && filename[1] == ':')
|
|
|
|
|
dirend = filename + 1;
|
1997-04-07 15:21:16 +08:00
|
|
|
|
}
|
2002-08-10 09:27:16 +08:00
|
|
|
|
#endif /* HAVE_DOS_PATHS */
|
1991-10-19 03:26:24 +08:00
|
|
|
|
if (dirend == 0)
|
1996-05-14 02:38:20 +08:00
|
|
|
|
#ifdef _AMIGA
|
|
|
|
|
dir = find_directory ("")->contents;
|
|
|
|
|
#else /* !VMS && !AMIGA */
|
1992-10-10 01:35:07 +08:00
|
|
|
|
dir = find_directory (".")->contents;
|
1996-05-14 02:38:20 +08:00
|
|
|
|
#endif /* AMIGA */
|
|
|
|
|
#endif /* VMS */
|
1991-10-19 03:26:24 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const char *dirname;
|
|
|
|
|
const char *slash = dirend;
|
1997-04-07 15:21:16 +08:00
|
|
|
|
if (dirend == filename)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
dirname = "/";
|
1997-04-07 15:21:16 +08:00
|
|
|
|
else
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
char *cp;
|
2002-08-10 09:27:16 +08:00
|
|
|
|
#ifdef HAVE_DOS_PATHS
|
2013-05-17 14:29:46 +08:00
|
|
|
|
/* d:/ and d: are *very* different... */
|
|
|
|
|
if (dirend < filename + 3 && filename[1] == ':' &&
|
|
|
|
|
(*dirend == '/' || *dirend == '\\' || *dirend == ':'))
|
|
|
|
|
dirend++;
|
1998-07-31 04:54:47 +08:00
|
|
|
|
#endif
|
2013-05-17 14:29:46 +08:00
|
|
|
|
cp = alloca (dirend - filename + 1);
|
2013-07-22 05:52:13 +08:00
|
|
|
|
memcpy (cp, filename, dirend - filename);
|
|
|
|
|
cp[dirend - filename] = '\0';
|
2006-11-19 04:53:44 +08:00
|
|
|
|
dirname = cp;
|
2013-05-17 14:29:46 +08:00
|
|
|
|
}
|
1992-10-10 01:35:07 +08:00
|
|
|
|
dir = find_directory (dirname)->contents;
|
2013-07-22 05:52:13 +08:00
|
|
|
|
filename = slash + 1;
|
1991-10-19 03:26:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
if (dir == 0 || dir->dirfiles.ht_vec == 0)
|
1991-10-19 03:26:24 +08:00
|
|
|
|
/* There are no files entered for this directory. */
|
|
|
|
|
return 0;
|
|
|
|
|
|
1994-07-26 07:01:19 +08:00
|
|
|
|
#ifdef __MSDOS__
|
2013-07-22 05:52:13 +08:00
|
|
|
|
filename = dosify (filename);
|
1994-07-26 07:01:19 +08:00
|
|
|
|
#endif
|
1998-07-31 04:54:47 +08:00
|
|
|
|
#ifdef HAVE_CASE_INSENSITIVE_FS
|
2013-07-22 05:52:13 +08:00
|
|
|
|
filename = downcase (filename);
|
1996-05-14 02:38:20 +08:00
|
|
|
|
#endif
|
1996-03-20 22:57:41 +08:00
|
|
|
|
#ifdef VMS
|
2013-07-22 05:52:13 +08:00
|
|
|
|
filename = vmsify (filename, 1);
|
1996-03-20 22:57:41 +08:00
|
|
|
|
#endif
|
1994-07-26 07:01:19 +08:00
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
dirfile_key.name = filename;
|
|
|
|
|
dirfile_key.length = strlen (filename);
|
2006-11-19 04:53:44 +08:00
|
|
|
|
dirfile = hash_find_item (&dir->dirfiles, &dirfile_key);
|
2002-07-11 14:38:57 +08:00
|
|
|
|
if (dirfile)
|
|
|
|
|
return dirfile->impossible;
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Return the already allocated name in the
|
|
|
|
|
directory hash table that matches DIR. */
|
|
|
|
|
|
2006-11-19 04:53:44 +08:00
|
|
|
|
const char *
|
|
|
|
|
dir_name (const char *dir)
|
1991-10-19 03:26:24 +08:00
|
|
|
|
{
|
|
|
|
|
return find_directory (dir)->name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Print the data base of directories. */
|
|
|
|
|
|
|
|
|
|
void
|
2002-10-15 05:54:04 +08:00
|
|
|
|
print_dir_data_base (void)
|
1991-10-19 03:26:24 +08:00
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
unsigned int files;
|
|
|
|
|
unsigned int impossible;
|
|
|
|
|
struct directory **dir_slot;
|
|
|
|
|
struct directory **dir_end;
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
1999-07-28 14:23:37 +08:00
|
|
|
|
puts (_("\n# Directories\n"));
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
files = impossible = 0;
|
|
|
|
|
|
|
|
|
|
dir_slot = (struct directory **) directories.ht_vec;
|
|
|
|
|
dir_end = dir_slot + directories.ht_size;
|
|
|
|
|
for ( ; dir_slot < dir_end; dir_slot++)
|
|
|
|
|
{
|
2006-11-19 04:53:44 +08:00
|
|
|
|
struct directory *dir = *dir_slot;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
if (! HASH_VACANT (dir))
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
if (dir->contents == 0)
|
|
|
|
|
printf (_("# %s: could not be stat'd.\n"), dir->name);
|
|
|
|
|
else if (dir->contents->dirfiles.ht_vec == 0)
|
|
|
|
|
{
|
1997-04-07 15:21:16 +08:00
|
|
|
|
#ifdef WINDOWS32
|
2013-05-17 14:29:46 +08:00
|
|
|
|
printf (_("# %s (key %s, mtime %d): could not be opened.\n"),
|
|
|
|
|
dir->name, dir->contents->path_key,dir->contents->mtime);
|
1997-04-07 15:21:16 +08:00
|
|
|
|
#else /* WINDOWS32 */
|
1996-03-20 22:57:41 +08:00
|
|
|
|
#ifdef VMS
|
2013-05-17 14:29:46 +08:00
|
|
|
|
printf (_("# %s (device %d, inode [%d,%d,%d]): could not be opened.\n"),
|
|
|
|
|
dir->name, dir->contents->dev,
|
|
|
|
|
dir->contents->ino[0], dir->contents->ino[1],
|
|
|
|
|
dir->contents->ino[2]);
|
1996-03-20 22:57:41 +08:00
|
|
|
|
#else
|
2013-05-17 14:29:46 +08:00
|
|
|
|
printf (_("# %s (device %ld, inode %ld): could not be opened.\n"),
|
|
|
|
|
dir->name, (long int) dir->contents->dev,
|
|
|
|
|
(long int) dir->contents->ino);
|
1996-03-20 22:57:41 +08:00
|
|
|
|
#endif
|
1997-04-07 15:21:16 +08:00
|
|
|
|
#endif /* WINDOWS32 */
|
2013-05-17 14:29:46 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
unsigned int f = 0;
|
|
|
|
|
unsigned int im = 0;
|
|
|
|
|
struct dirfile **files_slot;
|
|
|
|
|
struct dirfile **files_end;
|
|
|
|
|
|
|
|
|
|
files_slot = (struct dirfile **) dir->contents->dirfiles.ht_vec;
|
|
|
|
|
files_end = files_slot + dir->contents->dirfiles.ht_size;
|
|
|
|
|
for ( ; files_slot < files_end; files_slot++)
|
|
|
|
|
{
|
|
|
|
|
struct dirfile *df = *files_slot;
|
|
|
|
|
if (! HASH_VACANT (df))
|
|
|
|
|
{
|
|
|
|
|
if (df->impossible)
|
|
|
|
|
++im;
|
|
|
|
|
else
|
|
|
|
|
++f;
|
|
|
|
|
}
|
|
|
|
|
}
|
1997-04-07 15:21:16 +08:00
|
|
|
|
#ifdef WINDOWS32
|
2013-05-17 14:29:46 +08:00
|
|
|
|
printf (_("# %s (key %s, mtime %d): "),
|
|
|
|
|
dir->name, dir->contents->path_key, dir->contents->mtime);
|
1997-04-07 15:21:16 +08:00
|
|
|
|
#else /* WINDOWS32 */
|
1996-03-20 22:57:41 +08:00
|
|
|
|
#ifdef VMS
|
2013-05-17 14:29:46 +08:00
|
|
|
|
printf (_("# %s (device %d, inode [%d,%d,%d]): "),
|
|
|
|
|
dir->name, dir->contents->dev,
|
|
|
|
|
dir->contents->ino[0], dir->contents->ino[1],
|
|
|
|
|
dir->contents->ino[2]);
|
1996-03-20 22:57:41 +08:00
|
|
|
|
#else
|
2013-05-17 14:29:46 +08:00
|
|
|
|
printf (_("# %s (device %ld, inode %ld): "),
|
|
|
|
|
dir->name,
|
|
|
|
|
(long)dir->contents->dev, (long)dir->contents->ino);
|
1996-03-20 22:57:41 +08:00
|
|
|
|
#endif
|
1997-04-07 15:21:16 +08:00
|
|
|
|
#endif /* WINDOWS32 */
|
2013-05-17 14:29:46 +08:00
|
|
|
|
if (f == 0)
|
|
|
|
|
fputs (_("No"), stdout);
|
|
|
|
|
else
|
|
|
|
|
printf ("%u", f);
|
|
|
|
|
fputs (_(" files, "), stdout);
|
|
|
|
|
if (im == 0)
|
|
|
|
|
fputs (_("no"), stdout);
|
|
|
|
|
else
|
|
|
|
|
printf ("%u", im);
|
|
|
|
|
fputs (_(" impossibilities"), stdout);
|
|
|
|
|
if (dir->contents->dirstream == 0)
|
|
|
|
|
puts (".");
|
|
|
|
|
else
|
|
|
|
|
puts (_(" so far."));
|
|
|
|
|
files += f;
|
|
|
|
|
impossible += im;
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-07-11 14:38:57 +08:00
|
|
|
|
}
|
1991-10-19 03:26:24 +08:00
|
|
|
|
|
|
|
|
|
fputs ("\n# ", stdout);
|
|
|
|
|
if (files == 0)
|
1999-07-28 14:23:37 +08:00
|
|
|
|
fputs (_("No"), stdout);
|
1991-10-19 03:26:24 +08:00
|
|
|
|
else
|
|
|
|
|
printf ("%u", files);
|
1999-07-28 14:23:37 +08:00
|
|
|
|
fputs (_(" files, "), stdout);
|
1991-10-19 03:26:24 +08:00
|
|
|
|
if (impossible == 0)
|
1999-07-28 14:23:37 +08:00
|
|
|
|
fputs (_("no"), stdout);
|
1991-10-19 03:26:24 +08:00
|
|
|
|
else
|
|
|
|
|
printf ("%u", impossible);
|
2002-07-11 14:38:57 +08:00
|
|
|
|
printf (_(" impossibilities in %lu directories.\n"), directories.ht_fill);
|
1991-10-19 03:26:24 +08:00
|
|
|
|
}
|
1992-10-12 05:25:04 +08:00
|
|
|
|
|
|
|
|
|
/* Hooks for globbing. */
|
|
|
|
|
|
|
|
|
|
#include <glob.h>
|
|
|
|
|
|
|
|
|
|
/* Structure describing state of iterating through a directory hash table. */
|
|
|
|
|
|
|
|
|
|
struct dirstream
|
|
|
|
|
{
|
|
|
|
|
struct directory_contents *contents; /* The directory being read. */
|
2002-07-11 14:38:57 +08:00
|
|
|
|
struct dirfile **dirfile_slot; /* Current slot in table. */
|
1992-10-12 05:25:04 +08:00
|
|
|
|
};
|
|
|
|
|
|
1993-01-23 03:46:10 +08:00
|
|
|
|
/* Forward declarations. */
|
2006-04-07 09:43:44 +08:00
|
|
|
|
static __ptr_t open_dirstream (const char *);
|
|
|
|
|
static struct dirent *read_dirstream (__ptr_t);
|
1993-01-23 03:46:10 +08:00
|
|
|
|
|
1992-10-12 05:25:04 +08:00
|
|
|
|
static __ptr_t
|
2002-10-15 05:54:04 +08:00
|
|
|
|
open_dirstream (const char *directory)
|
1992-10-12 05:25:04 +08:00
|
|
|
|
{
|
|
|
|
|
struct dirstream *new;
|
2006-04-10 06:09:24 +08:00
|
|
|
|
struct directory *dir = find_directory (directory);
|
1992-10-12 05:25:04 +08:00
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
if (dir->contents == 0 || dir->contents->dirfiles.ht_vec == 0)
|
1992-11-11 04:43:35 +08:00
|
|
|
|
/* DIR->contents is nil if the directory could not be stat'd.
|
2002-07-11 14:38:57 +08:00
|
|
|
|
DIR->contents->dirfiles is nil if it could not be opened. */
|
1992-10-12 05:25:04 +08:00
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
/* Read all the contents of the directory now. There is no benefit
|
|
|
|
|
in being lazy, since glob will want to see every file anyway. */
|
|
|
|
|
|
2006-04-10 06:09:24 +08:00
|
|
|
|
dir_contents_file_exists_p (dir->contents, 0);
|
1992-10-12 05:25:04 +08:00
|
|
|
|
|
2006-04-10 06:09:24 +08:00
|
|
|
|
new = xmalloc (sizeof (struct dirstream));
|
1992-10-12 05:25:04 +08:00
|
|
|
|
new->contents = dir->contents;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
new->dirfile_slot = (struct dirfile **) new->contents->dirfiles.ht_vec;
|
1992-10-12 05:25:04 +08:00
|
|
|
|
|
1992-10-16 07:11:04 +08:00
|
|
|
|
return (__ptr_t) new;
|
1992-10-12 05:25:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
1995-12-12 11:37:15 +08:00
|
|
|
|
static struct dirent *
|
2002-10-15 05:54:04 +08:00
|
|
|
|
read_dirstream (__ptr_t stream)
|
1992-10-12 05:25:04 +08:00
|
|
|
|
{
|
2007-03-20 11:02:26 +08:00
|
|
|
|
static char *buf;
|
|
|
|
|
static unsigned int bufsz;
|
|
|
|
|
|
1992-10-12 05:25:04 +08:00
|
|
|
|
struct dirstream *const ds = (struct dirstream *) stream;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
struct directory_contents *dc = ds->contents;
|
|
|
|
|
struct dirfile **dirfile_end = (struct dirfile **) dc->dirfiles.ht_vec + dc->dirfiles.ht_size;
|
1992-10-12 05:25:04 +08:00
|
|
|
|
|
2002-07-11 14:38:57 +08:00
|
|
|
|
while (ds->dirfile_slot < dirfile_end)
|
1992-10-12 05:25:04 +08:00
|
|
|
|
{
|
2007-03-20 11:02:26 +08:00
|
|
|
|
struct dirfile *df = *ds->dirfile_slot++;
|
2002-07-11 14:38:57 +08:00
|
|
|
|
if (! HASH_VACANT (df) && !df->impossible)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
{
|
|
|
|
|
/* The glob interface wants a 'struct dirent', so mock one up. */
|
|
|
|
|
struct dirent *d;
|
|
|
|
|
unsigned int len = df->length + 1;
|
2007-03-20 11:02:26 +08:00
|
|
|
|
unsigned int sz = sizeof (*d) - sizeof (d->d_name) + len;
|
2013-05-17 14:29:46 +08:00
|
|
|
|
if (sz > bufsz)
|
|
|
|
|
{
|
|
|
|
|
bufsz *= 2;
|
|
|
|
|
if (sz > bufsz)
|
|
|
|
|
bufsz = sz;
|
|
|
|
|
buf = xrealloc (buf, bufsz);
|
|
|
|
|
}
|
|
|
|
|
d = (struct dirent *) buf;
|
2003-11-04 06:04:09 +08:00
|
|
|
|
#ifdef __MINGW32__
|
2005-02-10 08:10:57 +08:00
|
|
|
|
# if __MINGW32_MAJOR_VERSION < 3 || (__MINGW32_MAJOR_VERSION == 3 && \
|
2013-05-17 14:29:46 +08:00
|
|
|
|
__MINGW32_MINOR_VERSION == 0)
|
|
|
|
|
d->d_name = xmalloc (len);
|
2003-11-04 06:04:09 +08:00
|
|
|
|
# endif
|
|
|
|
|
#endif
|
2013-05-17 14:29:46 +08:00
|
|
|
|
FAKE_DIR_ENTRY (d);
|
1996-03-02 01:45:37 +08:00
|
|
|
|
#ifdef _DIRENT_HAVE_D_NAMLEN
|
2013-05-17 14:29:46 +08:00
|
|
|
|
d->d_namlen = len - 1;
|
2000-02-08 03:54:04 +08:00
|
|
|
|
#endif
|
|
|
|
|
#ifdef _DIRENT_HAVE_D_TYPE
|
2013-05-17 14:29:46 +08:00
|
|
|
|
d->d_type = DT_UNKNOWN;
|
1995-12-12 11:37:15 +08:00
|
|
|
|
#endif
|
2013-05-17 14:29:46 +08:00
|
|
|
|
memcpy (d->d_name, df->name, len);
|
|
|
|
|
return d;
|
|
|
|
|
}
|
1992-10-12 05:25:04 +08:00
|
|
|
|
}
|
1992-10-13 02:05:41 +08:00
|
|
|
|
|
|
|
|
|
return 0;
|
1992-10-12 05:25:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
1998-07-31 04:54:47 +08:00
|
|
|
|
static void
|
2005-02-28 15:48:22 +08:00
|
|
|
|
ansi_free (void *p)
|
1998-07-31 04:54:47 +08:00
|
|
|
|
{
|
2005-02-28 15:48:22 +08:00
|
|
|
|
if (p)
|
2013-05-17 14:29:46 +08:00
|
|
|
|
free (p);
|
1998-07-31 04:54:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2000-02-08 03:54:04 +08:00
|
|
|
|
/* On 64 bit ReliantUNIX (5.44 and above) in LFS mode, stat() is actually a
|
|
|
|
|
* macro for stat64(). If stat is a macro, make a local wrapper function to
|
|
|
|
|
* invoke it.
|
2013-04-29 01:53:36 +08:00
|
|
|
|
*
|
|
|
|
|
* On MS-Windows, stat() "succeeds" for foo/bar/. where foo/bar is a
|
|
|
|
|
* regular file; fix that here.
|
2000-02-08 03:54:04 +08:00
|
|
|
|
*/
|
2013-04-29 01:53:36 +08:00
|
|
|
|
#if !defined(stat) && !defined(WINDOWS32)
|
2000-02-08 03:54:04 +08:00
|
|
|
|
# ifndef VMS
|
2006-04-07 09:43:44 +08:00
|
|
|
|
int stat (const char *path, struct stat *sbuf);
|
2000-02-08 03:54:04 +08:00
|
|
|
|
# endif
|
|
|
|
|
# define local_stat stat
|
|
|
|
|
#else
|
2002-10-15 05:54:04 +08:00
|
|
|
|
static int
|
|
|
|
|
local_stat (const char *path, struct stat *buf)
|
2000-02-08 03:54:04 +08:00
|
|
|
|
{
|
2003-01-30 13:22:52 +08:00
|
|
|
|
int e;
|
2013-04-29 01:53:36 +08:00
|
|
|
|
#ifdef WINDOWS32
|
|
|
|
|
size_t plen = strlen (path);
|
|
|
|
|
|
|
|
|
|
/* Make sure the parent of "." exists and is a directory, not a
|
|
|
|
|
file. This is because 'stat' on Windows normalizes the argument
|
|
|
|
|
foo/. => foo without checking first that foo is a directory. */
|
|
|
|
|
if (plen > 1 && path[plen - 1] == '.'
|
|
|
|
|
&& (path[plen - 2] == '/' || path[plen - 2] == '\\'))
|
|
|
|
|
{
|
|
|
|
|
char parent[MAXPATHLEN];
|
|
|
|
|
|
|
|
|
|
strncpy (parent, path, plen - 2);
|
|
|
|
|
parent[plen - 2] = '\0';
|
|
|
|
|
if (stat (parent, buf) < 0 || !_S_ISDIR (buf->st_mode))
|
2013-05-17 14:29:46 +08:00
|
|
|
|
return -1;
|
2013-04-29 01:53:36 +08:00
|
|
|
|
}
|
|
|
|
|
#endif
|
2003-01-30 13:22:52 +08:00
|
|
|
|
|
|
|
|
|
EINTRLOOP (e, stat (path, buf));
|
|
|
|
|
return e;
|
2000-02-08 03:54:04 +08:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
1992-10-12 05:25:04 +08:00
|
|
|
|
void
|
2002-10-15 05:54:04 +08:00
|
|
|
|
dir_setup_glob (glob_t *gl)
|
1992-10-12 05:25:04 +08:00
|
|
|
|
{
|
1995-12-14 15:22:03 +08:00
|
|
|
|
gl->gl_opendir = open_dirstream;
|
|
|
|
|
gl->gl_readdir = read_dirstream;
|
1998-07-31 04:54:47 +08:00
|
|
|
|
gl->gl_closedir = ansi_free;
|
2000-02-08 03:54:04 +08:00
|
|
|
|
gl->gl_stat = local_stat;
|
1995-12-14 15:22:03 +08:00
|
|
|
|
/* We don't bother setting gl_lstat, since glob never calls it.
|
|
|
|
|
The slot is only there for compatibility with 4.4 BSD. */
|
1992-10-12 05:25:04 +08:00
|
|
|
|
}
|
2002-07-11 14:38:57 +08:00
|
|
|
|
|
|
|
|
|
void
|
2002-10-15 05:54:04 +08:00
|
|
|
|
hash_init_directories (void)
|
2002-07-11 14:38:57 +08:00
|
|
|
|
{
|
|
|
|
|
hash_init (&directories, DIRECTORY_BUCKETS,
|
2013-05-17 14:29:46 +08:00
|
|
|
|
directory_hash_1, directory_hash_2, directory_hash_cmp);
|
2002-07-11 14:38:57 +08:00
|
|
|
|
hash_init (&directory_contents, DIRECTORY_BUCKETS,
|
2013-05-17 14:29:46 +08:00
|
|
|
|
directory_contents_hash_1, directory_contents_hash_2,
|
2006-11-19 04:53:44 +08:00
|
|
|
|
directory_contents_hash_cmp);
|
2002-07-11 14:38:57 +08:00
|
|
|
|
}
|