[53] | 1 | /* Directory hashing for GNU Make.
|
---|
[3138] | 2 | Copyright (C) 1988-2016 Free Software Foundation, Inc.
|
---|
[53] | 3 | This file is part of GNU Make.
|
---|
| 4 |
|
---|
[501] | 5 | GNU Make is free software; you can redistribute it and/or modify it under the
|
---|
| 6 | terms of the GNU General Public License as published by the Free Software
|
---|
[1989] | 7 | Foundation; either version 3 of the License, or (at your option) any later
|
---|
| 8 | version.
|
---|
[53] | 9 |
|
---|
[501] | 10 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
| 11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
---|
| 12 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
[53] | 13 |
|
---|
[501] | 14 | You should have received a copy of the GNU General Public License along with
|
---|
[1989] | 15 | this program. If not, see <http://www.gnu.org/licenses/>. */
|
---|
[53] | 16 |
|
---|
[3138] | 17 | #include "makeint.h"
|
---|
[53] | 18 | #include "hash.h"
|
---|
[3138] | 19 | #include "filedef.h"
|
---|
| 20 | #include "dep.h"
|
---|
[53] | 21 |
|
---|
[3138] | 22 | #ifdef HAVE_DIRENT_H
|
---|
[53] | 23 | # include <dirent.h>
|
---|
| 24 | # define NAMLEN(dirent) strlen((dirent)->d_name)
|
---|
| 25 | # ifdef VMS
|
---|
[2596] | 26 | /* its prototype is in vmsdir.h, which is not needed for HAVE_DIRENT_H */
|
---|
| 27 | const char *vmsify (const char *name, int type);
|
---|
[53] | 28 | # endif
|
---|
| 29 | #else
|
---|
| 30 | # define dirent direct
|
---|
| 31 | # define NAMLEN(dirent) (dirent)->d_namlen
|
---|
| 32 | # ifdef HAVE_SYS_NDIR_H
|
---|
| 33 | # include <sys/ndir.h>
|
---|
| 34 | # endif
|
---|
| 35 | # ifdef HAVE_SYS_DIR_H
|
---|
| 36 | # include <sys/dir.h>
|
---|
| 37 | # endif
|
---|
| 38 | # ifdef HAVE_NDIR_H
|
---|
| 39 | # include <ndir.h>
|
---|
| 40 | # endif
|
---|
| 41 | # ifdef HAVE_VMSDIR_H
|
---|
| 42 | # include "vmsdir.h"
|
---|
| 43 | # endif /* HAVE_VMSDIR_H */
|
---|
| 44 | #endif
|
---|
| 45 |
|
---|
| 46 | /* In GNU systems, <dirent.h> defines this macro for us. */
|
---|
| 47 | #ifdef _D_NAMLEN
|
---|
| 48 | # undef NAMLEN
|
---|
| 49 | # define NAMLEN(d) _D_NAMLEN(d)
|
---|
| 50 | #endif
|
---|
| 51 |
|
---|
| 52 | #if (defined (POSIX) || defined (VMS) || defined (WINDOWS32)) && !defined (__GNU_LIBRARY__)
|
---|
| 53 | /* Posix does not require that the d_ino field be present, and some
|
---|
| 54 | systems do not provide it. */
|
---|
| 55 | # define REAL_DIR_ENTRY(dp) 1
|
---|
| 56 | # define FAKE_DIR_ENTRY(dp)
|
---|
| 57 | #else
|
---|
| 58 | # define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
|
---|
| 59 | # define FAKE_DIR_ENTRY(dp) (dp->d_ino = 1)
|
---|
| 60 | #endif /* POSIX */
|
---|
| 61 | |
---|
| 62 |
|
---|
| 63 | #ifdef __MSDOS__
|
---|
| 64 | #include <ctype.h>
|
---|
| 65 | #include <fcntl.h>
|
---|
| 66 |
|
---|
| 67 | /* If it's MSDOS that doesn't have _USE_LFN, disable LFN support. */
|
---|
| 68 | #ifndef _USE_LFN
|
---|
| 69 | #define _USE_LFN 0
|
---|
| 70 | #endif
|
---|
[900] | 71 |
|
---|
| 72 | static const char *
|
---|
[53] | 73 | dosify (const char *filename)
|
---|
| 74 | {
|
---|
| 75 | static char dos_filename[14];
|
---|
| 76 | char *df;
|
---|
| 77 | int i;
|
---|
| 78 |
|
---|
| 79 | if (filename == 0 || _USE_LFN)
|
---|
| 80 | return filename;
|
---|
| 81 |
|
---|
| 82 | /* FIXME: what about filenames which violate
|
---|
| 83 | 8+3 constraints, like "config.h.in", or ".emacs"? */
|
---|
| 84 | if (strpbrk (filename, "\"*+,;<=>?[\\]|") != 0)
|
---|
| 85 | return filename;
|
---|
| 86 |
|
---|
| 87 | df = dos_filename;
|
---|
| 88 |
|
---|
[3138] | 89 | /* First, transform the name part. */
|
---|
[53] | 90 | for (i = 0; i < 8 && ! STOP_SET (*filename, MAP_DOT|MAP_NUL); ++i)
|
---|
| 91 | *df++ = tolower ((unsigned char)*filename++);
|
---|
| 92 |
|
---|
[3138] | 93 | /* Now skip to the next dot. */
|
---|
[53] | 94 | while (! STOP_SET (*filename, MAP_DOT|MAP_NUL))
|
---|
| 95 | ++filename;
|
---|
| 96 | if (*filename != '\0')
|
---|
| 97 | {
|
---|
[3138] | 98 | *df++ = *filename++;
|
---|
| 99 | for (i = 0; i < 3 && ! STOP_SET (*filename, MAP_DOT|MAP_NUL); ++i)
|
---|
[53] | 100 | *df++ = tolower ((unsigned char)*filename++);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[3138] | 103 | /* Look for more dots. */
|
---|
[53] | 104 | while (! STOP_SET (*filename, MAP_DOT|MAP_NUL))
|
---|
| 105 | ++filename;
|
---|
| 106 | if (*filename == '.')
|
---|
| 107 | return filename;
|
---|
| 108 | *df = 0;
|
---|
| 109 | return dos_filename;
|
---|
| 110 | }
|
---|
| 111 | #endif /* __MSDOS__ */
|
---|
| 112 |
|
---|
| 113 | #ifdef WINDOWS32
|
---|
| 114 | #include "pathstuff.h"
|
---|
| 115 | #endif
|
---|
| 116 |
|
---|
| 117 | #ifdef _AMIGA
|
---|
| 118 | #include <ctype.h>
|
---|
| 119 | #endif
|
---|
| 120 |
|
---|
[900] | 121 | #ifdef HAVE_CASE_INSENSITIVE_FS
|
---|
| 122 | static const char *
|
---|
[53] | 123 | downcase (const char *filename)
|
---|
[280] | 124 | {
|
---|
[53] | 125 | static PATH_VAR (new_filename);
|
---|
| 126 | char *df;
|
---|
| 127 |
|
---|
| 128 | if (filename == 0)
|
---|
| 129 | return 0;
|
---|
| 130 |
|
---|
[900] | 131 | df = new_filename;
|
---|
| 132 | while (*filename != '\0')
|
---|
| 133 | {
|
---|
| 134 | *df++ = tolower ((unsigned char)*filename);
|
---|
| 135 | ++filename;
|
---|
[53] | 136 | }
|
---|
| 137 |
|
---|
| 138 | *df = 0;
|
---|
| 139 |
|
---|
| 140 | return new_filename;
|
---|
| 141 | }
|
---|
| 142 | #endif /* HAVE_CASE_INSENSITIVE_FS */
|
---|
| 143 |
|
---|
| 144 | #ifdef VMS
|
---|
[3138] | 145 |
|
---|
| 146 | static char *
|
---|
| 147 | downcase_inplace(char *filename)
|
---|
| 148 | {
|
---|
| 149 | char *name;
|
---|
| 150 | name = filename;
|
---|
| 151 | while (*name != '\0')
|
---|
| 152 | {
|
---|
| 153 | *name = tolower ((unsigned char)*name);
|
---|
| 154 | ++name;
|
---|
| 155 | }
|
---|
| 156 | return filename;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | #ifndef _USE_STD_STAT
|
---|
| 160 | /* VMS 8.2 fixed the VMS stat output to have unique st_dev and st_ino
|
---|
| 161 | when _USE_STD_STAT is used on the compile line.
|
---|
| 162 |
|
---|
| 163 | Prior to _USE_STD_STAT support, the st_dev is a pointer to thread
|
---|
| 164 | static memory containing the device of the last filename looked up.
|
---|
| 165 |
|
---|
| 166 | Todo: find out if the ino_t still needs to be faked on a directory.
|
---|
| 167 | */
|
---|
| 168 |
|
---|
| 169 | /* Define this if the older VMS_INO_T is needed */
|
---|
| 170 | #define VMS_INO_T 1
|
---|
[53] | 171 |
|
---|
[2596] | 172 | static int
|
---|
[53] | 173 | vms_hash (const char *name)
|
---|
| 174 | {
|
---|
| 175 | int h = 0;
|
---|
| 176 |
|
---|
| 177 | while (*name)
|
---|
| 178 | {
|
---|
[3138] | 179 | unsigned char uc = *name;
|
---|
[501] | 180 | int g;
|
---|
[53] | 181 | #ifdef HAVE_CASE_INSENSITIVE_FS
|
---|
[501] | 182 | h = (h << 4) + (isupper (uc) ? tolower (uc) : uc);
|
---|
| 183 | #else
|
---|
| 184 | h = (h << 4) + uc;
|
---|
[53] | 185 | #endif
|
---|
| 186 | name++;
|
---|
| 187 | g = h & 0xf0000000;
|
---|
[3138] | 188 | if (g)
|
---|
| 189 | {
|
---|
| 190 | h = h ^ (g >> 24);
|
---|
| 191 | h = h ^ g;
|
---|
[53] | 192 | }
|
---|
| 193 | }
|
---|
| 194 | return h;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | /* fake stat entry for a directory */
|
---|
[2596] | 198 | static int
|
---|
[53] | 199 | vmsstat_dir (const char *name, struct stat *st)
|
---|
| 200 | {
|
---|
| 201 | char *s;
|
---|
| 202 | int h;
|
---|
| 203 | DIR *dir;
|
---|
| 204 |
|
---|
| 205 | dir = opendir (name);
|
---|
| 206 | if (dir == 0)
|
---|
| 207 | return -1;
|
---|
[3138] | 208 | closedir (dir);
|
---|
[53] | 209 | s = strchr (name, ':'); /* find device */
|
---|
| 210 | if (s)
|
---|
[2596] | 211 | {
|
---|
[53] | 212 | /* to keep the compiler happy we said "const char *name", now we cheat */
|
---|
| 213 | *s++ = 0;
|
---|
| 214 | st->st_dev = (char *)vms_hash (name);
|
---|
| 215 | h = vms_hash (s);
|
---|
| 216 | *(s-1) = ':';
|
---|
| 217 | }
|
---|
| 218 | else
|
---|
| 219 | {
|
---|
[2596] | 220 | st->st_dev = 0;
|
---|
[53] | 221 | h = vms_hash (name);
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | st->st_ino[0] = h & 0xff;
|
---|
| 225 | st->st_ino[1] = h & 0xff00;
|
---|
| 226 | st->st_ino[2] = h >> 16;
|
---|
| 227 |
|
---|
| 228 | return 0;
|
---|
[3138] | 229 | }
|
---|
| 230 |
|
---|
| 231 | # define stat(__path, __sbuf) vmsstat_dir (__path, __sbuf)
|
---|
| 232 |
|
---|
[53] | 233 | #endif /* _USE_STD_STAT */
|
---|
| 234 | #endif /* VMS */
|
---|
| 235 | |
---|
| 236 |
|
---|
[3138] | 237 | /* Hash table of directories. */
|
---|
[53] | 238 |
|
---|
| 239 | #ifndef DIRECTORY_BUCKETS
|
---|
| 240 | #define DIRECTORY_BUCKETS 199
|
---|
| 241 | #endif
|
---|
| 242 |
|
---|
[3138] | 243 | struct directory_contents
|
---|
[53] | 244 | {
|
---|
[900] | 245 | dev_t dev; /* Device and inode numbers of this dir. */
|
---|
| 246 | #ifdef WINDOWS32
|
---|
| 247 | /* Inode means nothing on WINDOWS32. Even file key information is
|
---|
| 248 | * unreliable because it is random per file open and undefined for remote
|
---|
| 249 | * filesystems. The most unique attribute I can come up with is the fully
|
---|
[53] | 250 | * qualified name of the directory. Beware though, this is also
|
---|
[3138] | 251 | * unreliable. I'm open to suggestion on a better way to emulate inode. */
|
---|
| 252 | char *path_key;
|
---|
| 253 | time_t ctime;
|
---|
[900] | 254 | time_t mtime; /* controls check for stale directory cache */
|
---|
| 255 | int fs_flags; /* FS_FAT, FS_NTFS, ... */
|
---|
| 256 | # define FS_FAT 0x1
|
---|
[53] | 257 | # define FS_NTFS 0x2
|
---|
[3138] | 258 | # define FS_UNKNOWN 0x4
|
---|
[53] | 259 | #else
|
---|
[900] | 260 | # ifdef VMS_INO_T
|
---|
[53] | 261 | ino_t ino[3];
|
---|
[900] | 262 | # else
|
---|
[53] | 263 | ino_t ino;
|
---|
[3138] | 264 | # endif
|
---|
| 265 | #endif /* WINDOWS32 */
|
---|
[53] | 266 | struct hash_table dirfiles; /* Files in this directory. */
|
---|
| 267 | DIR *dirstream; /* Stream reading this directory. */
|
---|
| 268 | };
|
---|
| 269 |
|
---|
| 270 | static unsigned long
|
---|
[900] | 271 | directory_contents_hash_1 (const void *key_0)
|
---|
[53] | 272 | {
|
---|
| 273 | const struct directory_contents *key = key_0;
|
---|
| 274 | unsigned long hash;
|
---|
| 275 |
|
---|
| 276 | #ifdef WINDOWS32
|
---|
| 277 | hash = 0;
|
---|
| 278 | ISTRING_HASH_1 (key->path_key, hash);
|
---|
[3138] | 279 | hash ^= ((unsigned int) key->dev << 4) ^ (unsigned int) key->ctime;
|
---|
[53] | 280 | #else
|
---|
[3138] | 281 | # ifdef VMS_INO_T
|
---|
| 282 | hash = (((unsigned int) key->dev << 4)
|
---|
| 283 | ^ ((unsigned int) key->ino[0]
|
---|
[53] | 284 | + (unsigned int) key->ino[1]
|
---|
| 285 | + (unsigned int) key->ino[2]));
|
---|
| 286 | # else
|
---|
| 287 | hash = ((unsigned int) key->dev << 4) ^ (unsigned int) key->ino;
|
---|
| 288 | # endif
|
---|
| 289 | #endif /* WINDOWS32 */
|
---|
| 290 | return hash;
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | static unsigned long
|
---|
[900] | 294 | directory_contents_hash_2 (const void *key_0)
|
---|
[53] | 295 | {
|
---|
| 296 | const struct directory_contents *key = key_0;
|
---|
| 297 | unsigned long hash;
|
---|
| 298 |
|
---|
| 299 | #ifdef WINDOWS32
|
---|
| 300 | hash = 0;
|
---|
| 301 | ISTRING_HASH_2 (key->path_key, hash);
|
---|
[3138] | 302 | hash ^= ((unsigned int) key->dev << 4) ^ (unsigned int) ~key->ctime;
|
---|
[53] | 303 | #else
|
---|
[3138] | 304 | # ifdef VMS_INO_T
|
---|
| 305 | hash = (((unsigned int) key->dev << 4)
|
---|
| 306 | ^ ~((unsigned int) key->ino[0]
|
---|
[53] | 307 | + (unsigned int) key->ino[1]
|
---|
| 308 | + (unsigned int) key->ino[2]));
|
---|
| 309 | # else
|
---|
| 310 | hash = ((unsigned int) key->dev << 4) ^ (unsigned int) ~key->ino;
|
---|
| 311 | # endif
|
---|
| 312 | #endif /* WINDOWS32 */
|
---|
| 313 |
|
---|
| 314 | return hash;
|
---|
[501] | 315 | }
|
---|
| 316 |
|
---|
| 317 | /* Sometimes it's OK to use subtraction to get this value:
|
---|
| 318 | result = X - Y;
|
---|
| 319 | But, if we're not sure of the type of X and Y they may be too large for an
|
---|
| 320 | int (on a 64-bit system for example). So, use ?: instead.
|
---|
| 321 | See Savannah bug #15534.
|
---|
| 322 |
|
---|
| 323 | NOTE! This macro has side-effects!
|
---|
| 324 | */
|
---|
| 325 |
|
---|
[53] | 326 | #define MAKECMP(_x,_y) ((_x)<(_y)?-1:((_x)==(_y)?0:1))
|
---|
| 327 |
|
---|
| 328 | static int
|
---|
[900] | 329 | directory_contents_hash_cmp (const void *xv, const void *yv)
|
---|
| 330 | {
|
---|
[53] | 331 | const struct directory_contents *x = xv;
|
---|
| 332 | const struct directory_contents *y = yv;
|
---|
| 333 | int result;
|
---|
| 334 |
|
---|
| 335 | #ifdef WINDOWS32
|
---|
| 336 | ISTRING_COMPARE (x->path_key, y->path_key, result);
|
---|
[501] | 337 | if (result)
|
---|
[53] | 338 | return result;
|
---|
| 339 | result = MAKECMP(x->ctime, y->ctime);
|
---|
| 340 | if (result)
|
---|
[3138] | 341 | return result;
|
---|
[501] | 342 | #else
|
---|
[53] | 343 | # ifdef VMS_INO_T
|
---|
| 344 | result = MAKECMP(x->ino[0], y->ino[0]);
|
---|
[501] | 345 | if (result)
|
---|
[53] | 346 | return result;
|
---|
| 347 | result = MAKECMP(x->ino[1], y->ino[1]);
|
---|
[501] | 348 | if (result)
|
---|
[53] | 349 | return result;
|
---|
| 350 | result = MAKECMP(x->ino[2], y->ino[2]);
|
---|
| 351 | if (result)
|
---|
[501] | 352 | return result;
|
---|
[53] | 353 | # else
|
---|
| 354 | result = MAKECMP(x->ino, y->ino);
|
---|
| 355 | if (result)
|
---|
| 356 | return result;
|
---|
| 357 | # endif
|
---|
[501] | 358 | #endif /* WINDOWS32 */
|
---|
[53] | 359 |
|
---|
| 360 | return MAKECMP(x->dev, y->dev);
|
---|
| 361 | }
|
---|
| 362 |
|
---|
| 363 | /* Table of directory contents hashed by device and inode number. */
|
---|
| 364 | static struct hash_table directory_contents;
|
---|
| 365 |
|
---|
[3138] | 366 | struct directory
|
---|
[53] | 367 | {
|
---|
| 368 | const char *name; /* Name of the directory. */
|
---|
| 369 |
|
---|
[3138] | 370 | /* The directory's contents. This data may be shared by several
|
---|
[53] | 371 | entries in the hash table, which refer to the same directory
|
---|
| 372 | (identified uniquely by 'dev' and 'ino') under different names. */
|
---|
| 373 | struct directory_contents *contents;
|
---|
| 374 | };
|
---|
| 375 |
|
---|
| 376 | static unsigned long
|
---|
[900] | 377 | directory_hash_1 (const void *key)
|
---|
[53] | 378 | {
|
---|
| 379 | return_ISTRING_HASH_1 (((const struct directory *) key)->name);
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | static unsigned long
|
---|
[900] | 383 | directory_hash_2 (const void *key)
|
---|
[53] | 384 | {
|
---|
| 385 | return_ISTRING_HASH_2 (((const struct directory *) key)->name);
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | static int
|
---|
[900] | 389 | directory_hash_cmp (const void *x, const void *y)
|
---|
[3138] | 390 | {
|
---|
[53] | 391 | return_ISTRING_COMPARE (((const struct directory *) x)->name,
|
---|
| 392 | ((const struct directory *) y)->name);
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | /* Table of directories hashed by name. */
|
---|
| 396 | static struct hash_table directories;
|
---|
| 397 |
|
---|
| 398 | /* Never have more than this many directories open at once. */
|
---|
| 399 |
|
---|
| 400 | #define MAX_OPEN_DIRECTORIES 10
|
---|
| 401 |
|
---|
| 402 | static unsigned int open_directories = 0;
|
---|
| 403 |
|
---|
| 404 |
|
---|
| 405 | /* Hash table of files in each directory. */
|
---|
| 406 |
|
---|
[3138] | 407 | struct dirfile
|
---|
| 408 | {
|
---|
| 409 | const char *name; /* Name of the file. */
|
---|
[53] | 410 | size_t length;
|
---|
| 411 | short impossible; /* This file is impossible. */
|
---|
| 412 | };
|
---|
| 413 |
|
---|
| 414 | static unsigned long
|
---|
| 415 | dirfile_hash_1 (const void *key)
|
---|
| 416 | {
|
---|
| 417 | return_ISTRING_HASH_1 (((struct dirfile const *) key)->name);
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | static unsigned long
|
---|
| 421 | dirfile_hash_2 (const void *key)
|
---|
| 422 | {
|
---|
| 423 | return_ISTRING_HASH_2 (((struct dirfile const *) key)->name);
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | static int
|
---|
[900] | 427 | dirfile_hash_cmp (const void *xv, const void *yv)
|
---|
| 428 | {
|
---|
[53] | 429 | const struct dirfile *x = xv;
|
---|
| 430 | const struct dirfile *y = yv;
|
---|
| 431 | int result = x->length - y->length;
|
---|
| 432 | if (result)
|
---|
| 433 | return result;
|
---|
| 434 | return_ISTRING_COMPARE (x->name, y->name);
|
---|
[3138] | 435 | }
|
---|
[53] | 436 |
|
---|
| 437 | #ifndef DIRFILE_BUCKETS
|
---|
| 438 | #define DIRFILE_BUCKETS 107
|
---|
[900] | 439 | #endif
|
---|
| 440 | |
---|
| 441 |
|
---|
[53] | 442 | static int dir_contents_file_exists_p (struct directory_contents *dir,
|
---|
[3138] | 443 | const char *filename);
|
---|
[53] | 444 | static struct directory *find_directory (const char *name);
|
---|
| 445 |
|
---|
[900] | 446 | /* Find the directory named NAME and return its 'struct directory'. */
|
---|
[53] | 447 |
|
---|
[900] | 448 | static struct directory *
|
---|
| 449 | find_directory (const char *name)
|
---|
[53] | 450 | {
|
---|
| 451 | struct directory *dir;
|
---|
| 452 | struct directory **dir_slot;
|
---|
| 453 | struct directory dir_key;
|
---|
| 454 |
|
---|
| 455 | dir_key.name = name;
|
---|
| 456 | dir_slot = (struct directory **) hash_find_slot (&directories, &dir_key);
|
---|
| 457 | dir = *dir_slot;
|
---|
[3138] | 458 |
|
---|
| 459 | if (HASH_VACANT (dir))
|
---|
[53] | 460 | {
|
---|
[3138] | 461 | /* The directory was not found. Create a new entry for it. */
|
---|
[53] | 462 | const char *p = name + strlen (name);
|
---|
[900] | 463 | struct stat st;
|
---|
[2596] | 464 | int r;
|
---|
[3138] | 465 |
|
---|
| 466 | dir = xmalloc (sizeof (struct directory));
|
---|
| 467 | #if defined(HAVE_CASE_INSENSITIVE_FS) && defined(VMS)
|
---|
| 468 | /* Todo: Why is this only needed on VMS? */
|
---|
| 469 | {
|
---|
| 470 | char *lname = downcase_inplace (xstrdup (name));
|
---|
[2596] | 471 | dir->name = strcache_add_len (lname, p - name);
|
---|
[900] | 472 | free (lname);
|
---|
[2596] | 473 | }
|
---|
[53] | 474 | #else
|
---|
| 475 | dir->name = strcache_add_len (name, p - name);
|
---|
[3138] | 476 | #endif
|
---|
[53] | 477 | hash_insert_at (&directories, dir, dir_slot);
|
---|
[3138] | 478 | /* The directory is not in the name hash table.
|
---|
[1989] | 479 | Find its device and inode numbers, and look it up by them. */
|
---|
| 480 |
|
---|
| 481 | #if defined(WINDOWS32)
|
---|
| 482 | {
|
---|
| 483 | char tem[MAXPATHLEN], *tstart, *tend;
|
---|
| 484 |
|
---|
| 485 | /* Remove any trailing slashes. Windows32 stat fails even on
|
---|
| 486 | valid directories if they end in a slash. */
|
---|
| 487 | memcpy (tem, name, p - name + 1);
|
---|
| 488 | tstart = tem;
|
---|
| 489 | if (tstart[1] == ':')
|
---|
| 490 | tstart += 2;
|
---|
| 491 | for (tend = tem + (p - name - 1);
|
---|
| 492 | tend > tstart && (*tend == '/' || *tend == '\\');
|
---|
| 493 | tend--)
|
---|
| 494 | *tend = '\0';
|
---|
[53] | 495 |
|
---|
| 496 | r = stat (tem, &st);
|
---|
| 497 | }
|
---|
| 498 | #else
|
---|
| 499 | EINTRLOOP (r, stat (name, &st));
|
---|
| 500 | #endif
|
---|
[3138] | 501 |
|
---|
| 502 | if (r < 0)
|
---|
| 503 | {
|
---|
| 504 | /* Couldn't stat the directory. Mark this by
|
---|
[53] | 505 | setting the 'contents' member to a nil pointer. */
|
---|
[3138] | 506 | dir->contents = 0;
|
---|
| 507 | }
|
---|
[53] | 508 | else
|
---|
[3138] | 509 | {
|
---|
| 510 | /* Search the contents hash table; device and inode are the key. */
|
---|
| 511 |
|
---|
| 512 | #ifdef WINDOWS32
|
---|
| 513 | char *w32_path;
|
---|
| 514 | #endif
|
---|
[53] | 515 | struct directory_contents *dc;
|
---|
[3138] | 516 | struct directory_contents **dc_slot;
|
---|
[53] | 517 | struct directory_contents dc_key;
|
---|
[3138] | 518 |
|
---|
| 519 | dc_key.dev = st.st_dev;
|
---|
[53] | 520 | #ifdef WINDOWS32
|
---|
[3138] | 521 | dc_key.path_key = w32_path = w32ify (name, 1);
|
---|
| 522 | dc_key.ctime = st.st_ctime;
|
---|
| 523 | #else
|
---|
| 524 | # ifdef VMS_INO_T
|
---|
[53] | 525 | dc_key.ino[0] = st.st_ino[0];
|
---|
[3138] | 526 | dc_key.ino[1] = st.st_ino[1];
|
---|
[53] | 527 | dc_key.ino[2] = st.st_ino[2];
|
---|
| 528 | # else
|
---|
[3138] | 529 | dc_key.ino = st.st_ino;
|
---|
| 530 | # endif
|
---|
[53] | 531 | #endif
|
---|
[3138] | 532 | dc_slot = (struct directory_contents **) hash_find_slot (&directory_contents, &dc_key);
|
---|
| 533 | dc = *dc_slot;
|
---|
| 534 |
|
---|
| 535 | if (HASH_VACANT (dc))
|
---|
| 536 | {
|
---|
| 537 | /* Nope; this really is a directory we haven't seen before. */
|
---|
| 538 | #ifdef WINDOWS32
|
---|
| 539 | char fs_label[BUFSIZ];
|
---|
| 540 | char fs_type[BUFSIZ];
|
---|
| 541 | unsigned long fs_serno;
|
---|
| 542 | unsigned long fs_flags;
|
---|
| 543 | unsigned long fs_len;
|
---|
[53] | 544 | #endif
|
---|
[3138] | 545 | dc = (struct directory_contents *)
|
---|
| 546 | xmalloc (sizeof (struct directory_contents));
|
---|
[53] | 547 |
|
---|
| 548 | /* Enter it in the contents hash table. */
|
---|
[3138] | 549 | dc->dev = st.st_dev;
|
---|
[53] | 550 | #ifdef WINDOWS32
|
---|
| 551 | dc->path_key = xstrdup (w32_path);
|
---|
[3138] | 552 | dc->ctime = st.st_ctime;
|
---|
| 553 | dc->mtime = st.st_mtime;
|
---|
[53] | 554 |
|
---|
[3138] | 555 | /* NTFS is the only WINDOWS32 filesystem that bumps mtime on a
|
---|
| 556 | directory when files are added/deleted from a directory. */
|
---|
| 557 | w32_path[3] = '\0';
|
---|
[53] | 558 | if (GetVolumeInformation (w32_path, fs_label, sizeof (fs_label),
|
---|
[3138] | 559 | &fs_serno, &fs_len, &fs_flags, fs_type,
|
---|
[53] | 560 | sizeof (fs_type)) == FALSE)
|
---|
[3138] | 561 | dc->fs_flags = FS_UNKNOWN;
|
---|
[53] | 562 | else if (!strcmp (fs_type, "FAT"))
|
---|
| 563 | dc->fs_flags = FS_FAT;
|
---|
| 564 | else if (!strcmp (fs_type, "NTFS"))
|
---|
| 565 | dc->fs_flags = FS_NTFS;
|
---|
[3138] | 566 | else
|
---|
| 567 | dc->fs_flags = FS_UNKNOWN;
|
---|
| 568 | #else
|
---|
| 569 | # ifdef VMS_INO_T
|
---|
[53] | 570 | dc->ino[0] = st.st_ino[0];
|
---|
[3138] | 571 | dc->ino[1] = st.st_ino[1];
|
---|
[53] | 572 | dc->ino[2] = st.st_ino[2];
|
---|
| 573 | # else
|
---|
[3138] | 574 | dc->ino = st.st_ino;
|
---|
| 575 | # endif
|
---|
| 576 | #endif /* WINDOWS32 */
|
---|
[900] | 577 | hash_insert_at (&directory_contents, dc, dc_slot);
|
---|
[3138] | 578 | ENULLLOOP (dc->dirstream, opendir (name));
|
---|
[53] | 579 | if (dc->dirstream == 0)
|
---|
[3138] | 580 | /* Couldn't open the directory. Mark this by setting the
|
---|
| 581 | 'files' member to a nil pointer. */
|
---|
| 582 | dc->dirfiles.ht_vec = 0;
|
---|
| 583 | else
|
---|
| 584 | {
|
---|
| 585 | hash_init (&dc->dirfiles, DIRFILE_BUCKETS,
|
---|
| 586 | dirfile_hash_1, dirfile_hash_2, dirfile_hash_cmp);
|
---|
| 587 | /* Keep track of how many directories are open. */
|
---|
| 588 | ++open_directories;
|
---|
| 589 | if (open_directories == MAX_OPEN_DIRECTORIES)
|
---|
| 590 | /* We have too many directories open already.
|
---|
| 591 | Read the entire directory and then close it. */
|
---|
[53] | 592 | dir_contents_file_exists_p (dc, 0);
|
---|
[3138] | 593 | }
|
---|
| 594 | }
|
---|
| 595 |
|
---|
[53] | 596 | /* Point the name-hashed entry for DIR at its contents data. */
|
---|
| 597 | dir->contents = dc;
|
---|
| 598 | }
|
---|
| 599 | }
|
---|
| 600 |
|
---|
| 601 | return dir;
|
---|
| 602 | }
|
---|
| 603 | |
---|
| 604 |
|
---|
[900] | 605 | /* Return 1 if the name FILENAME is entered in DIR's hash table.
|
---|
| 606 | FILENAME must contain no slashes. */
|
---|
[53] | 607 |
|
---|
| 608 | static int
|
---|
| 609 | dir_contents_file_exists_p (struct directory_contents *dir,
|
---|
| 610 | const char *filename)
|
---|
| 611 | {
|
---|
| 612 | struct dirfile *df;
|
---|
| 613 | struct dirent *d;
|
---|
| 614 | #ifdef WINDOWS32
|
---|
| 615 | struct stat st;
|
---|
| 616 | int rehash = 0;
|
---|
[900] | 617 | #endif
|
---|
| 618 |
|
---|
[53] | 619 | if (dir == 0 || dir->dirfiles.ht_vec == 0)
|
---|
| 620 | /* The directory could not be stat'd or opened. */
|
---|
| 621 | return 0;
|
---|
| 622 |
|
---|
| 623 | #ifdef __MSDOS__
|
---|
| 624 | filename = dosify (filename);
|
---|
| 625 | #endif
|
---|
| 626 |
|
---|
| 627 | #ifdef HAVE_CASE_INSENSITIVE_FS
|
---|
| 628 | filename = downcase (filename);
|
---|
| 629 | #endif
|
---|
| 630 |
|
---|
| 631 | #ifdef __EMX__
|
---|
| 632 | if (filename != 0)
|
---|
| 633 | _fnlwr (filename); /* lower case for FAT drives */
|
---|
| 634 | #endif
|
---|
| 635 | if (filename != 0)
|
---|
[3138] | 636 | {
|
---|
| 637 | struct dirfile dirfile_key;
|
---|
| 638 |
|
---|
| 639 | if (*filename == '\0')
|
---|
[53] | 640 | {
|
---|
| 641 | /* Checking if the directory exists. */
|
---|
[900] | 642 | return 1;
|
---|
[53] | 643 | }
|
---|
[900] | 644 | dirfile_key.name = filename;
|
---|
[53] | 645 | dirfile_key.length = strlen (filename);
|
---|
| 646 | df = hash_find_item (&dir->dirfiles, &dirfile_key);
|
---|
| 647 | if (df)
|
---|
| 648 | return !df->impossible;
|
---|
| 649 | }
|
---|
| 650 |
|
---|
| 651 | /* The file was not found in the hashed list.
|
---|
| 652 | Try to read the directory further. */
|
---|
| 653 |
|
---|
| 654 | if (dir->dirstream == 0)
|
---|
| 655 | {
|
---|
| 656 | #ifdef WINDOWS32
|
---|
| 657 | /*
|
---|
[501] | 658 | * Check to see if directory has changed since last read. FAT
|
---|
[3138] | 659 | * filesystems force a rehash always as mtime does not change
|
---|
[501] | 660 | * on directories (ugh!).
|
---|
[3138] | 661 | */
|
---|
| 662 | if (dir->path_key)
|
---|
| 663 | {
|
---|
| 664 | if ((dir->fs_flags & FS_FAT) != 0)
|
---|
| 665 | {
|
---|
| 666 | dir->mtime = time ((time_t *) 0);
|
---|
| 667 | rehash = 1;
|
---|
| 668 | }
|
---|
| 669 | else if (stat (dir->path_key, &st) == 0 && st.st_mtime > dir->mtime)
|
---|
| 670 | {
|
---|
[53] | 671 | /* reset date stamp to show most recent re-process. */
|
---|
[501] | 672 | dir->mtime = st.st_mtime;
|
---|
[3138] | 673 | rehash = 1;
|
---|
| 674 | }
|
---|
[53] | 675 |
|
---|
[501] | 676 | /* If it has been already read in, all done. */
|
---|
[900] | 677 | if (!rehash)
|
---|
[501] | 678 | return 0;
|
---|
| 679 |
|
---|
[3138] | 680 | /* make sure directory can still be opened; if not return. */
|
---|
[53] | 681 | dir->dirstream = opendir (dir->path_key);
|
---|
| 682 | if (!dir->dirstream)
|
---|
[3138] | 683 | return 0;
|
---|
| 684 | }
|
---|
[53] | 685 | else
|
---|
| 686 | #endif
|
---|
| 687 | /* The directory has been all read in. */
|
---|
| 688 | return 0;
|
---|
| 689 | }
|
---|
| 690 |
|
---|
| 691 | while (1)
|
---|
| 692 | {
|
---|
| 693 | /* Enter the file in the hash table. */
|
---|
| 694 | unsigned int len;
|
---|
| 695 | struct dirfile dirfile_key;
|
---|
[900] | 696 | struct dirfile **dirfile_slot;
|
---|
| 697 |
|
---|
[3138] | 698 | ENULLLOOP (d, readdir (dir->dirstream));
|
---|
[900] | 699 | if (d == 0)
|
---|
| 700 | {
|
---|
[53] | 701 | if (errno)
|
---|
| 702 | pfatal_with_name ("INTERNAL: readdir");
|
---|
[3138] | 703 | break;
|
---|
| 704 | }
|
---|
| 705 |
|
---|
[53] | 706 | #if defined(VMS) && defined(HAVE_DIRENT_H)
|
---|
| 707 | /* In VMS we get file versions too, which have to be stripped off.
|
---|
| 708 | Some versions of VMS return versions on Unix files even when
|
---|
| 709 | the feature option to strip them is set. */
|
---|
| 710 | {
|
---|
| 711 | char *p = strrchr (d->d_name, ';');
|
---|
| 712 | if (p)
|
---|
[3138] | 713 | *p = '\0';
|
---|
[53] | 714 | }
|
---|
| 715 | #endif
|
---|
| 716 | if (!REAL_DIR_ENTRY (d))
|
---|
| 717 | continue;
|
---|
| 718 |
|
---|
| 719 | len = NAMLEN (d);
|
---|
| 720 | dirfile_key.name = d->d_name;
|
---|
| 721 | dirfile_key.length = len;
|
---|
| 722 | dirfile_slot = (struct dirfile **) hash_find_slot (&dir->dirfiles, &dirfile_key);
|
---|
| 723 | #ifdef WINDOWS32
|
---|
| 724 | /*
|
---|
| 725 | * If re-reading a directory, don't cache files that have
|
---|
[3138] | 726 | * already been discovered.
|
---|
| 727 | */
|
---|
[2596] | 728 | if (! rehash || HASH_VACANT (*dirfile_slot))
|
---|
[3138] | 729 | #endif
|
---|
| 730 | {
|
---|
[2596] | 731 | df = xmalloc (sizeof (struct dirfile));
|
---|
[3138] | 732 | #if defined(HAVE_CASE_INSENSITIVE_FS) && defined(VMS)
|
---|
[2596] | 733 | /* TODO: Why is this only needed on VMS? */
|
---|
[3138] | 734 | df->name = strcache_add_len (downcase_inplace (d->d_name), len);
|
---|
| 735 | #else
|
---|
| 736 | df->name = strcache_add_len (d->d_name, len);
|
---|
| 737 | #endif
|
---|
[53] | 738 | df->length = len;
|
---|
[2596] | 739 | df->impossible = 0;
|
---|
[900] | 740 | hash_insert_at (&dir->dirfiles, df, dirfile_slot);
|
---|
[53] | 741 | }
|
---|
| 742 | /* Check if the name matches the one we're searching for. */
|
---|
| 743 | if (filename != 0 && patheq (d->d_name, filename))
|
---|
| 744 | return 1;
|
---|
| 745 | }
|
---|
| 746 |
|
---|
| 747 | /* If the directory has been completely read in,
|
---|
| 748 | close the stream and reset the pointer to nil. */
|
---|
| 749 | if (d == 0)
|
---|
| 750 | {
|
---|
| 751 | --open_directories;
|
---|
| 752 | closedir (dir->dirstream);
|
---|
| 753 | dir->dirstream = 0;
|
---|
| 754 | }
|
---|
| 755 | return 0;
|
---|
| 756 | }
|
---|
| 757 |
|
---|
| 758 | /* Return 1 if the name FILENAME in directory DIRNAME
|
---|
[900] | 759 | is entered in the dir hash table.
|
---|
[53] | 760 | FILENAME must contain no slashes. */
|
---|
[3138] | 761 |
|
---|
| 762 | int
|
---|
| 763 | dir_file_exists_p (const char *dirname, const char *filename)
|
---|
| 764 | {
|
---|
| 765 | #ifdef VMS
|
---|
| 766 | if ((filename != NULL) && (dirname != NULL))
|
---|
| 767 | {
|
---|
| 768 | int want_vmsify;
|
---|
| 769 | want_vmsify = (strpbrk (dirname, ":<[") != NULL);
|
---|
[53] | 770 | if (want_vmsify)
|
---|
[3138] | 771 | filename = vmsify (filename, 0);
|
---|
[53] | 772 | }
|
---|
| 773 | #endif
|
---|
| 774 | return dir_contents_file_exists_p (find_directory (dirname)->contents,
|
---|
| 775 | filename);
|
---|
| 776 | }
|
---|
[900] | 777 | |
---|
[53] | 778 |
|
---|
[900] | 779 | /* Return 1 if the file named NAME exists. */
|
---|
| 780 |
|
---|
| 781 | int
|
---|
[53] | 782 | file_exists_p (const char *name)
|
---|
[3138] | 783 | {
|
---|
[53] | 784 | const char *dirend;
|
---|
| 785 | const char *dirname;
|
---|
| 786 | const char *slash;
|
---|
| 787 |
|
---|
[3138] | 788 | #ifndef NO_ARCHIVES
|
---|
[53] | 789 | if (ar_name (name))
|
---|
| 790 | return ar_member_date (name) != (time_t) -1;
|
---|
[3138] | 791 | #endif
|
---|
| 792 |
|
---|
| 793 | dirend = strrchr (name, '/');
|
---|
| 794 | #ifdef VMS
|
---|
[900] | 795 | if (dirend == 0)
|
---|
[3138] | 796 | {
|
---|
| 797 | dirend = strrchr (name, ']');
|
---|
| 798 | dirend == NULL ? dirend : dirend++;
|
---|
| 799 | }
|
---|
| 800 | if (dirend == 0)
|
---|
| 801 | {
|
---|
| 802 | dirend = strrchr (name, '>');
|
---|
| 803 | dirend == NULL ? dirend : dirend++;
|
---|
| 804 | }
|
---|
| 805 | if (dirend == 0)
|
---|
[53] | 806 | {
|
---|
| 807 | dirend = strrchr (name, ':');
|
---|
| 808 | dirend == NULL ? dirend : dirend++;
|
---|
[3138] | 809 | }
|
---|
[53] | 810 | #endif /* VMS */
|
---|
| 811 | #ifdef HAVE_DOS_PATHS
|
---|
| 812 | /* Forward and backslashes might be mixed. We need the rightmost one. */
|
---|
| 813 | {
|
---|
| 814 | const char *bslash = strrchr (name, '\\');
|
---|
| 815 | if (!dirend || bslash > dirend)
|
---|
| 816 | dirend = bslash;
|
---|
| 817 | /* The case of "d:file". */
|
---|
| 818 | if (!dirend && name[0] && name[1] == ':')
|
---|
| 819 | dirend = name + 1;
|
---|
[3138] | 820 | }
|
---|
[53] | 821 | #endif /* HAVE_DOS_PATHS */
|
---|
| 822 | if (dirend == 0)
|
---|
| 823 | #ifndef _AMIGA
|
---|
| 824 | return dir_file_exists_p (".", name);
|
---|
| 825 | #else /* !AMIGA */
|
---|
| 826 | return dir_file_exists_p ("", name);
|
---|
| 827 | #endif /* AMIGA */
|
---|
| 828 |
|
---|
[900] | 829 | slash = dirend;
|
---|
[53] | 830 | if (dirend == name)
|
---|
| 831 | dirname = "/";
|
---|
| 832 | else
|
---|
[3138] | 833 | {
|
---|
| 834 | char *p;
|
---|
[53] | 835 | #ifdef HAVE_DOS_PATHS
|
---|
[900] | 836 | /* d:/ and d: are *very* different... */
|
---|
| 837 | if (dirend < name + 3 && name[1] == ':' &&
|
---|
| 838 | (*dirend == '/' || *dirend == '\\' || *dirend == ':'))
|
---|
| 839 | dirend++;
|
---|
[53] | 840 | #endif
|
---|
[3138] | 841 | p = alloca (dirend - name + 1);
|
---|
| 842 | memcpy (p, name, dirend - name);
|
---|
| 843 | p[dirend - name] = '\0';
|
---|
| 844 | dirname = p;
|
---|
| 845 | }
|
---|
| 846 | #ifdef VMS
|
---|
| 847 | if (*slash == '/')
|
---|
[53] | 848 | slash++;
|
---|
| 849 | #else
|
---|
[3138] | 850 | slash++;
|
---|
[53] | 851 | #endif
|
---|
| 852 | return dir_file_exists_p (dirname, slash);
|
---|
| 853 | }
|
---|
| 854 | |
---|
[900] | 855 |
|
---|
[53] | 856 | /* Mark FILENAME as 'impossible' for 'file_impossible_p'.
|
---|
[900] | 857 | This means an attempt has been made to search for FILENAME
|
---|
| 858 | as an intermediate file, and it has failed. */
|
---|
| 859 |
|
---|
| 860 | void
|
---|
[53] | 861 | file_impossible (const char *filename)
|
---|
[3138] | 862 | {
|
---|
[53] | 863 | const char *dirend;
|
---|
[3138] | 864 | const char *p = filename;
|
---|
| 865 | struct directory *dir;
|
---|
| 866 | struct dirfile *new;
|
---|
| 867 |
|
---|
| 868 | dirend = strrchr (p, '/');
|
---|
| 869 | #ifdef VMS
|
---|
| 870 | if (dirend == NULL)
|
---|
| 871 | {
|
---|
| 872 | dirend = strrchr (p, ']');
|
---|
| 873 | dirend == NULL ? dirend : dirend++;
|
---|
| 874 | }
|
---|
| 875 | if (dirend == NULL)
|
---|
| 876 | {
|
---|
| 877 | dirend = strrchr (p, '>');
|
---|
| 878 | dirend == NULL ? dirend : dirend++;
|
---|
| 879 | }
|
---|
| 880 | if (dirend == NULL)
|
---|
[53] | 881 | {
|
---|
| 882 | dirend = strrchr (p, ':');
|
---|
[3138] | 883 | dirend == NULL ? dirend : dirend++;
|
---|
[53] | 884 | }
|
---|
| 885 | #endif
|
---|
| 886 | #ifdef HAVE_DOS_PATHS
|
---|
| 887 | /* Forward and backslashes might be mixed. We need the rightmost one. */
|
---|
| 888 | {
|
---|
| 889 | const char *bslash = strrchr (p, '\\');
|
---|
[3138] | 890 | if (!dirend || bslash > dirend)
|
---|
[53] | 891 | dirend = bslash;
|
---|
[3138] | 892 | /* The case of "d:file". */
|
---|
[53] | 893 | if (!dirend && p[0] && p[1] == ':')
|
---|
[3138] | 894 | dirend = p + 1;
|
---|
[53] | 895 | }
|
---|
[3138] | 896 | #endif /* HAVE_DOS_PATHS */
|
---|
[53] | 897 | if (dirend == 0)
|
---|
| 898 | #ifdef _AMIGA
|
---|
[900] | 899 | dir = find_directory ("");
|
---|
| 900 | #else /* !AMIGA */
|
---|
[53] | 901 | dir = find_directory (".");
|
---|
[3138] | 902 | #endif /* AMIGA */
|
---|
[53] | 903 | else
|
---|
[3138] | 904 | {
|
---|
[900] | 905 | const char *dirname;
|
---|
[53] | 906 | const char *slash = dirend;
|
---|
[3138] | 907 | if (dirend == p)
|
---|
| 908 | dirname = "/";
|
---|
| 909 | else
|
---|
| 910 | {
|
---|
[53] | 911 | char *cp;
|
---|
[3138] | 912 | #ifdef HAVE_DOS_PATHS
|
---|
| 913 | /* d:/ and d: are *very* different... */
|
---|
| 914 | if (dirend < p + 3 && p[1] == ':' &&
|
---|
[900] | 915 | (*dirend == '/' || *dirend == '\\' || *dirend == ':'))
|
---|
[3138] | 916 | dirend++;
|
---|
[53] | 917 | #endif
|
---|
[3138] | 918 | cp = alloca (dirend - p + 1);
|
---|
| 919 | memcpy (cp, p, dirend - p);
|
---|
| 920 | cp[dirend - p] = '\0';
|
---|
| 921 | dirname = cp;
|
---|
| 922 | }
|
---|
| 923 | dir = find_directory (dirname);
|
---|
[53] | 924 | #ifdef VMS
|
---|
[3138] | 925 | if (*slash == '/')
|
---|
[53] | 926 | filename = p = slash + 1;
|
---|
| 927 | else
|
---|
| 928 | filename = p = slash;
|
---|
[2596] | 929 | #else
|
---|
| 930 | filename = p = slash + 1;
|
---|
| 931 | #endif
|
---|
[53] | 932 | }
|
---|
| 933 |
|
---|
| 934 | if (dir->contents == 0)
|
---|
| 935 | /* The directory could not be stat'd. We allocate a contents
|
---|
[3138] | 936 | structure for it, but leave it out of the contents hash table. */
|
---|
[53] | 937 | dir->contents = xcalloc (sizeof (struct directory_contents));
|
---|
| 938 |
|
---|
| 939 | if (dir->contents->dirfiles.ht_vec == 0)
|
---|
| 940 | {
|
---|
[900] | 941 | hash_init (&dir->contents->dirfiles, DIRFILE_BUCKETS,
|
---|
[53] | 942 | dirfile_hash_1, dirfile_hash_2, dirfile_hash_cmp);
|
---|
[2596] | 943 | }
|
---|
[3138] | 944 |
|
---|
| 945 | /* Make a new entry and put it in the table. */
|
---|
[2596] | 946 |
|
---|
[900] | 947 | new = xmalloc (sizeof (struct dirfile));
|
---|
[2596] | 948 | new->length = strlen (filename);
|
---|
[53] | 949 | #if defined(HAVE_CASE_INSENSITIVE_FS) && defined(VMS)
|
---|
| 950 | /* todo: Why is this only needed on VMS? */
|
---|
| 951 | new->name = strcache_add_len (downcase (filename), new->length);
|
---|
| 952 | #else
|
---|
| 953 | new->name = strcache_add_len (filename, new->length);
|
---|
| 954 | #endif
|
---|
| 955 | new->impossible = 1;
|
---|
[900] | 956 | hash_insert (&dir->contents->dirfiles, new);
|
---|
[53] | 957 | }
|
---|
[900] | 958 | |
---|
| 959 |
|
---|
| 960 | /* Return nonzero if FILENAME has been marked impossible. */
|
---|
[53] | 961 |
|
---|
[3138] | 962 | int
|
---|
| 963 | file_impossible_p (const char *filename)
|
---|
| 964 | {
|
---|
[53] | 965 | const char *dirend;
|
---|
[3138] | 966 | struct directory_contents *dir;
|
---|
[53] | 967 | struct dirfile *dirfile;
|
---|
[3138] | 968 | struct dirfile dirfile_key;
|
---|
| 969 | #ifdef VMS
|
---|
| 970 | int want_vmsify = 0;
|
---|
| 971 | #endif
|
---|
| 972 |
|
---|
| 973 | dirend = strrchr (filename, '/');
|
---|
| 974 | #ifdef VMS
|
---|
| 975 | if (dirend == NULL)
|
---|
| 976 | {
|
---|
| 977 | want_vmsify = (strpbrk (filename, "]>:^") != NULL);
|
---|
[53] | 978 | dirend = strrchr (filename, ']');
|
---|
| 979 | }
|
---|
| 980 | if (dirend == NULL && want_vmsify)
|
---|
[3138] | 981 | dirend = strrchr (filename, '>');
|
---|
[53] | 982 | if (dirend == NULL && want_vmsify)
|
---|
| 983 | dirend = strrchr (filename, ':');
|
---|
| 984 | #endif
|
---|
| 985 | #ifdef HAVE_DOS_PATHS
|
---|
| 986 | /* Forward and backslashes might be mixed. We need the rightmost one. */
|
---|
| 987 | {
|
---|
| 988 | const char *bslash = strrchr (filename, '\\');
|
---|
| 989 | if (!dirend || bslash > dirend)
|
---|
| 990 | dirend = bslash;
|
---|
| 991 | /* The case of "d:file". */
|
---|
[3138] | 992 | if (!dirend && filename[0] && filename[1] == ':')
|
---|
[53] | 993 | dirend = filename + 1;
|
---|
| 994 | }
|
---|
| 995 | #endif /* HAVE_DOS_PATHS */
|
---|
| 996 | if (dirend == 0)
|
---|
[900] | 997 | #ifdef _AMIGA
|
---|
| 998 | dir = find_directory ("")->contents;
|
---|
[53] | 999 | #else /* !AMIGA */
|
---|
[3138] | 1000 | dir = find_directory (".")->contents;
|
---|
[53] | 1001 | #endif /* AMIGA */
|
---|
[3138] | 1002 | else
|
---|
[900] | 1003 | {
|
---|
[53] | 1004 | const char *dirname;
|
---|
[3138] | 1005 | const char *slash = dirend;
|
---|
| 1006 | if (dirend == filename)
|
---|
| 1007 | dirname = "/";
|
---|
| 1008 | else
|
---|
[53] | 1009 | {
|
---|
[3138] | 1010 | char *cp;
|
---|
| 1011 | #ifdef HAVE_DOS_PATHS
|
---|
| 1012 | /* d:/ and d: are *very* different... */
|
---|
[900] | 1013 | if (dirend < filename + 3 && filename[1] == ':' &&
|
---|
[3138] | 1014 | (*dirend == '/' || *dirend == '\\' || *dirend == ':'))
|
---|
[53] | 1015 | dirend++;
|
---|
[3138] | 1016 | #endif
|
---|
| 1017 | cp = alloca (dirend - filename + 1);
|
---|
| 1018 | memcpy (cp, filename, dirend - filename);
|
---|
| 1019 | cp[dirend - filename] = '\0';
|
---|
| 1020 | dirname = cp;
|
---|
| 1021 | }
|
---|
| 1022 | dir = find_directory (dirname)->contents;
|
---|
| 1023 | #ifdef VMS
|
---|
[53] | 1024 | if (*slash == '/')
|
---|
| 1025 | filename = slash + 1;
|
---|
| 1026 | else
|
---|
| 1027 | filename = slash;
|
---|
| 1028 | #else
|
---|
| 1029 | filename = slash + 1;
|
---|
| 1030 | #endif
|
---|
[3138] | 1031 | }
|
---|
[53] | 1032 |
|
---|
| 1033 | if (dir == 0 || dir->dirfiles.ht_vec == 0)
|
---|
[3138] | 1034 | /* There are no files entered for this directory. */
|
---|
[53] | 1035 | return 0;
|
---|
| 1036 |
|
---|
[3138] | 1037 | #ifdef __MSDOS__
|
---|
| 1038 | filename = dosify (filename);
|
---|
[53] | 1039 | #endif
|
---|
| 1040 | #ifdef HAVE_CASE_INSENSITIVE_FS
|
---|
| 1041 | filename = downcase (filename);
|
---|
| 1042 | #endif
|
---|
[900] | 1043 | #ifdef VMS
|
---|
[53] | 1044 | if (want_vmsify)
|
---|
| 1045 | filename = vmsify (filename, 1);
|
---|
| 1046 | #endif
|
---|
| 1047 |
|
---|
| 1048 | dirfile_key.name = filename;
|
---|
| 1049 | dirfile_key.length = strlen (filename);
|
---|
| 1050 | dirfile = hash_find_item (&dir->dirfiles, &dirfile_key);
|
---|
| 1051 | if (dirfile)
|
---|
| 1052 | return dirfile->impossible;
|
---|
[900] | 1053 |
|
---|
| 1054 | return 0;
|
---|
[53] | 1055 | }
|
---|
| 1056 | |
---|
| 1057 |
|
---|
| 1058 | /* Return the already allocated name in the
|
---|
| 1059 | directory hash table that matches DIR. */
|
---|
| 1060 |
|
---|
| 1061 | const char *
|
---|
| 1062 | dir_name (const char *dir)
|
---|
| 1063 | {
|
---|
[900] | 1064 | return find_directory (dir)->name;
|
---|
| 1065 | }
|
---|
| 1066 | |
---|
| 1067 |
|
---|
[53] | 1068 | /* Print the data base of directories. */
|
---|
| 1069 |
|
---|
| 1070 | void
|
---|
| 1071 | print_dir_data_base (void)
|
---|
| 1072 | {
|
---|
| 1073 | unsigned int files;
|
---|
| 1074 | unsigned int impossible;
|
---|
| 1075 | struct directory **dir_slot;
|
---|
| 1076 | struct directory **dir_end;
|
---|
[900] | 1077 |
|
---|
[53] | 1078 | puts (_("\n# Directories\n"));
|
---|
[3138] | 1079 |
|
---|
| 1080 | files = impossible = 0;
|
---|
| 1081 |
|
---|
| 1082 | dir_slot = (struct directory **) directories.ht_vec;
|
---|
| 1083 | dir_end = dir_slot + directories.ht_size;
|
---|
[53] | 1084 | for ( ; dir_slot < dir_end; dir_slot++)
|
---|
[3138] | 1085 | {
|
---|
| 1086 | struct directory *dir = *dir_slot;
|
---|
| 1087 | if (! HASH_VACANT (dir))
|
---|
[53] | 1088 | {
|
---|
[3138] | 1089 | if (dir->contents == 0)
|
---|
| 1090 | printf (_("# %s: could not be stat'd.\n"), dir->name);
|
---|
| 1091 | else if (dir->contents->dirfiles.ht_vec == 0)
|
---|
| 1092 | {
|
---|
| 1093 | #ifdef WINDOWS32
|
---|
[53] | 1094 | printf (_("# %s (key %s, mtime %I64u): could not be opened.\n"),
|
---|
[3138] | 1095 | dir->name, dir->contents->path_key,
|
---|
| 1096 | (unsigned long long)dir->contents->mtime);
|
---|
| 1097 | #else /* WINDOWS32 */
|
---|
[53] | 1098 | #ifdef VMS_INO_T
|
---|
| 1099 | printf (_("# %s (device %d, inode [%d,%d,%d]): could not be opened.\n"),
|
---|
[3138] | 1100 | dir->name, dir->contents->dev,
|
---|
| 1101 | dir->contents->ino[0], dir->contents->ino[1],
|
---|
| 1102 | dir->contents->ino[2]);
|
---|
| 1103 | #else
|
---|
| 1104 | printf (_("# %s (device %ld, inode %ld): could not be opened.\n"),
|
---|
| 1105 | dir->name, (long int) dir->contents->dev,
|
---|
| 1106 | (long int) dir->contents->ino);
|
---|
[53] | 1107 | #endif
|
---|
[3138] | 1108 | #endif /* WINDOWS32 */
|
---|
| 1109 | }
|
---|
| 1110 | else
|
---|
| 1111 | {
|
---|
| 1112 | unsigned int f = 0;
|
---|
| 1113 | unsigned int im = 0;
|
---|
| 1114 | struct dirfile **files_slot;
|
---|
| 1115 | struct dirfile **files_end;
|
---|
| 1116 |
|
---|
| 1117 | files_slot = (struct dirfile **) dir->contents->dirfiles.ht_vec;
|
---|
| 1118 | files_end = files_slot + dir->contents->dirfiles.ht_size;
|
---|
| 1119 | for ( ; files_slot < files_end; files_slot++)
|
---|
| 1120 | {
|
---|
[53] | 1121 | struct dirfile *df = *files_slot;
|
---|
[3138] | 1122 | if (! HASH_VACANT (df))
|
---|
| 1123 | {
|
---|
| 1124 | if (df->impossible)
|
---|
[53] | 1125 | ++im;
|
---|
[3138] | 1126 | else
|
---|
| 1127 | ++f;
|
---|
| 1128 | }
|
---|
| 1129 | }
|
---|
| 1130 | #ifdef WINDOWS32
|
---|
[53] | 1131 | printf (_("# %s (key %s, mtime %I64u): "),
|
---|
[3138] | 1132 | dir->name, dir->contents->path_key,
|
---|
| 1133 | (unsigned long long)dir->contents->mtime);
|
---|
| 1134 | #else /* WINDOWS32 */
|
---|
[53] | 1135 | #ifdef VMS_INO_T
|
---|
| 1136 | printf (_("# %s (device %d, inode [%d,%d,%d]): "),
|
---|
[3138] | 1137 | dir->name, dir->contents->dev,
|
---|
| 1138 | dir->contents->ino[0], dir->contents->ino[1],
|
---|
| 1139 | dir->contents->ino[2]);
|
---|
| 1140 | #else
|
---|
| 1141 | printf (_("# %s (device %ld, inode %ld): "),
|
---|
| 1142 | dir->name,
|
---|
| 1143 | (long)dir->contents->dev, (long)dir->contents->ino);
|
---|
| 1144 | #endif
|
---|
| 1145 | #endif /* WINDOWS32 */
|
---|
| 1146 | if (f == 0)
|
---|
| 1147 | fputs (_("No"), stdout);
|
---|
| 1148 | else
|
---|
| 1149 | printf ("%u", f);
|
---|
| 1150 | fputs (_(" files, "), stdout);
|
---|
| 1151 | if (im == 0)
|
---|
| 1152 | fputs (_("no"), stdout);
|
---|
| 1153 | else
|
---|
| 1154 | printf ("%u", im);
|
---|
[53] | 1155 | fputs (_(" impossibilities"), stdout);
|
---|
| 1156 | if (dir->contents->dirstream == 0)
|
---|
| 1157 | puts (".");
|
---|
| 1158 | else
|
---|
| 1159 | puts (_(" so far."));
|
---|
| 1160 | files += f;
|
---|
| 1161 | impossible += im;
|
---|
| 1162 | }
|
---|
| 1163 | }
|
---|
| 1164 | }
|
---|
| 1165 |
|
---|
| 1166 | fputs ("\n# ", stdout);
|
---|
| 1167 | if (files == 0)
|
---|
| 1168 | fputs (_("No"), stdout);
|
---|
| 1169 | else
|
---|
| 1170 | printf ("%u", files);
|
---|
| 1171 | fputs (_(" files, "), stdout);
|
---|
| 1172 | if (impossible == 0)
|
---|
| 1173 | fputs (_("no"), stdout);
|
---|
| 1174 | else
|
---|
| 1175 | printf ("%u", impossible);
|
---|
| 1176 | printf (_(" impossibilities in %lu directories.\n"), directories.ht_fill);
|
---|
| 1177 | }
|
---|
| 1178 | |
---|
| 1179 |
|
---|
| 1180 | /* Hooks for globbing. */
|
---|
[900] | 1181 |
|
---|
| 1182 | /* Structure describing state of iterating through a directory hash table. */
|
---|
[53] | 1183 |
|
---|
| 1184 | struct dirstream
|
---|
| 1185 | {
|
---|
| 1186 | struct directory_contents *contents; /* The directory being read. */
|
---|
| 1187 | struct dirfile **dirfile_slot; /* Current slot in table. */
|
---|
[900] | 1188 | };
|
---|
[53] | 1189 |
|
---|
| 1190 | /* Forward declarations. */
|
---|
| 1191 | static __ptr_t open_dirstream (const char *);
|
---|
| 1192 | static struct dirent *read_dirstream (__ptr_t);
|
---|
| 1193 |
|
---|
| 1194 | static __ptr_t
|
---|
| 1195 | open_dirstream (const char *directory)
|
---|
| 1196 | {
|
---|
| 1197 | struct dirstream *new;
|
---|
[900] | 1198 | struct directory *dir = find_directory (directory);
|
---|
[53] | 1199 |
|
---|
[900] | 1200 | if (dir->contents == 0 || dir->contents->dirfiles.ht_vec == 0)
|
---|
[53] | 1201 | /* DIR->contents is nil if the directory could not be stat'd.
|
---|
| 1202 | DIR->contents->dirfiles is nil if it could not be opened. */
|
---|
| 1203 | return 0;
|
---|
| 1204 |
|
---|
| 1205 | /* Read all the contents of the directory now. There is no benefit
|
---|
| 1206 | in being lazy, since glob will want to see every file anyway. */
|
---|
| 1207 |
|
---|
| 1208 | dir_contents_file_exists_p (dir->contents, 0);
|
---|
| 1209 |
|
---|
[900] | 1210 | new = xmalloc (sizeof (struct dirstream));
|
---|
| 1211 | new->contents = dir->contents;
|
---|
| 1212 | new->dirfile_slot = (struct dirfile **) new->contents->dirfiles.ht_vec;
|
---|
[53] | 1213 |
|
---|
| 1214 | return (__ptr_t) new;
|
---|
| 1215 | }
|
---|
| 1216 |
|
---|
| 1217 | static struct dirent *
|
---|
| 1218 | read_dirstream (__ptr_t stream)
|
---|
[900] | 1219 | {
|
---|
[53] | 1220 | static char *buf;
|
---|
[3138] | 1221 | static unsigned int bufsz;
|
---|
| 1222 |
|
---|
| 1223 | struct dirstream *const ds = (struct dirstream *) stream;
|
---|
| 1224 | struct directory_contents *dc = ds->contents;
|
---|
[900] | 1225 | struct dirfile **dirfile_end = (struct dirfile **) dc->dirfiles.ht_vec + dc->dirfiles.ht_size;
|
---|
[3138] | 1226 |
|
---|
| 1227 | while (ds->dirfile_slot < dirfile_end)
|
---|
| 1228 | {
|
---|
| 1229 | struct dirfile *df = *ds->dirfile_slot++;
|
---|
| 1230 | if (! HASH_VACANT (df) && !df->impossible)
|
---|
| 1231 | {
|
---|
| 1232 | /* The glob interface wants a 'struct dirent', so mock one up. */
|
---|
| 1233 | struct dirent *d;
|
---|
[53] | 1234 | unsigned int len = df->length + 1;
|
---|
[280] | 1235 | unsigned int sz = sizeof (*d) - sizeof (d->d_name) + len;
|
---|
[3138] | 1236 | if (sz > bufsz)
|
---|
| 1237 | {
|
---|
[53] | 1238 | bufsz *= 2;
|
---|
| 1239 | if (sz > bufsz)
|
---|
[3138] | 1240 | bufsz = sz;
|
---|
[53] | 1241 | buf = xrealloc (buf, bufsz);
|
---|
[3138] | 1242 | }
|
---|
[53] | 1243 | d = (struct dirent *) buf;
|
---|
| 1244 | #ifdef __MINGW32__
|
---|
[3138] | 1245 | # if __MINGW32_MAJOR_VERSION < 3 || (__MINGW32_MAJOR_VERSION == 3 && \
|
---|
[53] | 1246 | __MINGW32_MINOR_VERSION == 0)
|
---|
[3138] | 1247 | d->d_name = xmalloc (len);
|
---|
| 1248 | # endif
|
---|
| 1249 | #endif
|
---|
[53] | 1250 | FAKE_DIR_ENTRY (d);
|
---|
| 1251 | #ifdef _DIRENT_HAVE_D_NAMLEN
|
---|
| 1252 | d->d_namlen = len - 1;
|
---|
| 1253 | #endif
|
---|
| 1254 | #ifdef _DIRENT_HAVE_D_TYPE
|
---|
| 1255 | d->d_type = DT_UNKNOWN;
|
---|
| 1256 | #endif
|
---|
| 1257 | memcpy (d->d_name, df->name, len);
|
---|
[3138] | 1258 | return d;
|
---|
| 1259 | }
|
---|
| 1260 | }
|
---|
[53] | 1261 |
|
---|
[3138] | 1262 | return 0;
|
---|
[53] | 1263 | }
|
---|
[3138] | 1264 |
|
---|
[900] | 1265 | /* On 64 bit ReliantUNIX (5.44 and above) in LFS mode, stat() is actually a
|
---|
[3138] | 1266 | * macro for stat64(). If stat is a macro, make a local wrapper function to
|
---|
| 1267 | * invoke it.
|
---|
| 1268 | *
|
---|
| 1269 | * On MS-Windows, stat() "succeeds" for foo/bar/. where foo/bar is a
|
---|
| 1270 | * regular file; fix that here.
|
---|
| 1271 | */
|
---|
[53] | 1272 | #if !defined(stat) && !defined(WINDOWS32) || defined(VMS)
|
---|
| 1273 | # ifndef VMS
|
---|
| 1274 | # ifndef HAVE_SYS_STAT_H
|
---|
| 1275 | int stat (const char *path, struct stat *sbuf);
|
---|
| 1276 | # endif
|
---|
| 1277 | # else
|
---|
| 1278 | /* We are done with the fake stat. Go back to the real stat */
|
---|
[3138] | 1279 | # ifdef stat
|
---|
| 1280 | # undef stat
|
---|
[53] | 1281 | # endif
|
---|
[3138] | 1282 | # endif
|
---|
| 1283 | # define local_stat stat
|
---|
| 1284 | #else
|
---|
| 1285 | static int
|
---|
| 1286 | local_stat (const char *path, struct stat *buf)
|
---|
| 1287 | {
|
---|
| 1288 | int e;
|
---|
| 1289 | #ifdef WINDOWS32
|
---|
| 1290 | size_t plen = strlen (path);
|
---|
| 1291 |
|
---|
| 1292 | /* Make sure the parent of "." exists and is a directory, not a
|
---|
| 1293 | file. This is because 'stat' on Windows normalizes the argument
|
---|
| 1294 | foo/. => foo without checking first that foo is a directory. */
|
---|
| 1295 | if (plen > 1 && path[plen - 1] == '.'
|
---|
| 1296 | && (path[plen - 2] == '/' || path[plen - 2] == '\\'))
|
---|
[53] | 1297 | {
|
---|
| 1298 | char parent[MAXPATHLEN];
|
---|
| 1299 |
|
---|
| 1300 | strncpy (parent, path, plen - 2);
|
---|
| 1301 | parent[plen - 2] = '\0';
|
---|
| 1302 | if (stat (parent, buf) < 0 || !_S_ISDIR (buf->st_mode))
|
---|
| 1303 | return -1;
|
---|
| 1304 | }
|
---|
| 1305 | #endif
|
---|
| 1306 |
|
---|
[3138] | 1307 | EINTRLOOP (e, stat (path, buf));
|
---|
[53] | 1308 | return e;
|
---|
| 1309 | }
|
---|
| 1310 | #endif
|
---|
| 1311 |
|
---|
| 1312 | void
|
---|
| 1313 | dir_setup_glob (glob_t *gl)
|
---|
| 1314 | {
|
---|
| 1315 | gl->gl_opendir = open_dirstream;
|
---|
| 1316 | gl->gl_readdir = read_dirstream;
|
---|
[3138] | 1317 | gl->gl_closedir = free;
|
---|
[53] | 1318 | gl->gl_stat = local_stat;
|
---|
[3138] | 1319 | /* We don't bother setting gl_lstat, since glob never calls it.
|
---|
[900] | 1320 | The slot is only there for compatibility with 4.4 BSD. */
|
---|
[53] | 1321 | }
|
---|
| 1322 |
|
---|
| 1323 | void
|
---|
| 1324 | hash_init_directories (void)
|
---|
| 1325 | {
|
---|
| 1326 | hash_init (&directories, DIRECTORY_BUCKETS,
|
---|
| 1327 | directory_hash_1, directory_hash_2, directory_hash_cmp);
|
---|
| 1328 | hash_init (&directory_contents, DIRECTORY_BUCKETS,
|
---|
| 1329 | directory_contents_hash_1, directory_contents_hash_2,
|
---|
| 1330 | directory_contents_hash_cmp);
|
---|
| 1331 | }
|
---|