1 /* Directory hashing for GNU Make.
2 Copyright (C) 1988,89,91,92,93,94,95,96,97 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 # define NAMLEN(dirent) strlen((dirent)->d_name)
25 # define dirent direct
26 # define NAMLEN(dirent) (dirent)->d_namlen
27 # ifdef HAVE_SYS_NDIR_H
28 # include <sys/ndir.h>
30 # ifdef HAVE_SYS_DIR_H
38 # endif /* HAVE_VMSDIR_H */
41 /* In GNU systems, <dirent.h> defines this macro for us. */
44 #define NAMLEN(d) _D_NAMLEN(d)
47 #if (defined (POSIX) || defined (WINDOWS32)) && !defined (__GNU_LIBRARY__)
48 /* Posix does not require that the d_ino field be present, and some
49 systems do not provide it. */
50 #define REAL_DIR_ENTRY(dp) 1
51 #define FAKE_DIR_ENTRY(dp)
53 #define REAL_DIR_ENTRY(dp) (dp->d_ino != 0)
54 #define FAKE_DIR_ENTRY(dp) (dp->d_ino = 1)
61 /* If it's MSDOS that doesn't have _USE_LFN, disable LFN support. */
70 static char dos_filename
[14];
74 if (filename
== 0 || _USE_LFN
)
77 /* FIXME: what about filenames which violate
78 8+3 constraints, like "config.h.in", or ".emacs"? */
79 if (strpbrk (filename
, "\"*+,;<=>?[\\]|") != 0)
84 /* First, transform the name part. */
85 for (i
= 0; *filename
!= '\0' && i
< 8 && *filename
!= '.'; ++i
)
86 *df
++ = tolower (*filename
++);
88 /* Now skip to the next dot. */
89 while (*filename
!= '\0' && *filename
!= '.')
91 if (*filename
!= '\0')
94 for (i
= 0; *filename
!= '\0' && i
< 3 && *filename
!= '.'; ++i
)
95 *df
++ = tolower (*filename
++);
98 /* Look for more dots. */
99 while (*filename
!= '\0' && *filename
!= '.')
101 if (*filename
== '.')
106 #endif /* __MSDOS__ */
109 #include "pathstuff.h"
119 static char amiga_filename
[136];
128 /* First, transform the name part. */
129 for (i
= 0; *filename
!= '\0'; ++i
)
131 *df
++ = tolower (*filename
);
137 return amiga_filename
;
152 h
= (h
<< 4) + *name
++;
163 /* fake stat entry for a directory */
165 vmsstat_dir (name
, st
)
173 dir
= opendir (name
);
177 s
= strchr (name
, ':'); /* find device */
181 st
->st_dev
= (char *)vms_hash (name
);
192 st
->st_ino
[0] = h
& 0xff;
193 st
->st_ino
[1] = h
& 0xff00;
194 st
->st_ino
[2] = h
>> 16;
200 /* Hash table of directories. */
202 #ifndef DIRECTORY_BUCKETS
203 #define DIRECTORY_BUCKETS 199
206 struct directory_contents
208 struct directory_contents
*next
;
210 dev_t dev
; /* Device and inode numbers of this dir. */
213 * Inode means nothing on WINDOWS32. Even file key information is
214 * unreliable because it is random per file open and undefined
215 * for remote filesystems. The most unique attribute I can
216 * come up with is the fully qualified name of the directory. Beware
217 * though, this is also unreliable. I'm open to suggestion on a better
218 * way to emulate inode.
221 int mtime
; /* controls check for stale directory cache */
222 int fs_flags
; /* FS_FAT, FS_NTFS, ... */
225 #define FS_UNKNOWN 0x4
232 #endif /* WINDOWS32 */
233 struct dirfile
**files
; /* Files in this directory. */
234 DIR *dirstream
; /* Stream reading this directory. */
237 /* Table of directory contents hashed by device and inode number. */
238 static struct directory_contents
*directories_contents
[DIRECTORY_BUCKETS
];
242 struct directory
*next
;
244 char *name
; /* Name of the directory. */
246 /* The directory's contents. This data may be shared by several
247 entries in the hash table, which refer to the same directory
248 (identified uniquely by `dev' and `ino') under different names. */
249 struct directory_contents
*contents
;
252 /* Table of directories hashed by name. */
253 static struct directory
*directories
[DIRECTORY_BUCKETS
];
256 /* Never have more than this many directories open at once. */
258 #define MAX_OPEN_DIRECTORIES 10
260 static unsigned int open_directories
= 0;
263 /* Hash table of files in each directory. */
267 struct dirfile
*next
;
268 char *name
; /* Name of the file. */
269 char impossible
; /* This file is impossible. */
272 #ifndef DIRFILE_BUCKETS
273 #define DIRFILE_BUCKETS 107
276 static int dir_contents_file_exists_p
PARAMS ((struct directory_contents
*dir
, char *filename
));
277 static struct directory
*find_directory
PARAMS ((char *name
));
279 /* Find the directory named NAME and return its `struct directory'. */
281 static struct directory
*
282 find_directory (name
)
285 register unsigned int hash
= 0;
287 register struct directory
*dir
;
290 char fs_label
[BUFSIZ
];
291 char fs_type
[BUFSIZ
];
297 if ((*name
== '.') && (*(name
+1) == 0))
300 name
= vmsify (name
,1);
303 for (p
= name
; *p
!= '\0'; ++p
)
305 hash
%= DIRECTORY_BUCKETS
;
307 for (dir
= directories
[hash
]; dir
!= 0; dir
= dir
->next
)
308 if (strieq (dir
->name
, name
))
315 /* The directory was not found. Create a new entry for it. */
317 dir
= (struct directory
*) xmalloc (sizeof (struct directory
));
318 dir
->next
= directories
[hash
];
319 directories
[hash
] = dir
;
320 dir
->name
= savestring (name
, p
- name
);
322 /* The directory is not in the name hash table.
323 Find its device and inode numbers, and look it up by them. */
326 if (vmsstat_dir (name
, &st
) < 0)
328 if (stat (name
, &st
) < 0)
331 /* Couldn't stat the directory. Mark this by
332 setting the `contents' member to a nil pointer. */
337 /* Search the contents hash table; device and inode are the key. */
339 struct directory_contents
*dc
;
342 w32_path
= w32ify(name
, 1);
343 hash
= ((unsigned int) st
.st_dev
<< 16) | (unsigned int) st
.st_ctime
;
346 hash
= ((unsigned int) st
.st_dev
<< 16)
347 | ((unsigned int) st
.st_ino
[0]
348 + (unsigned int) st
.st_ino
[1]
349 + (unsigned int) st
.st_ino
[2]);
351 hash
= ((unsigned int) st
.st_dev
<< 16) | (unsigned int) st
.st_ino
;
354 hash
%= DIRECTORY_BUCKETS
;
356 for (dc
= directories_contents
[hash
]; dc
!= 0; dc
= dc
->next
)
358 if (!strcmp(dc
->path_key
, w32_path
))
360 if (dc
->dev
== st
.st_dev
362 && dc
->ino
[0] == st
.st_ino
[0]
363 && dc
->ino
[1] == st
.st_ino
[1]
364 && dc
->ino
[2] == st
.st_ino
[2])
366 && dc
->ino
== st
.st_ino
)
368 #endif /* WINDOWS32 */
373 /* Nope; this really is a directory we haven't seen before. */
375 dc
= (struct directory_contents
*)
376 xmalloc (sizeof (struct directory_contents
));
378 /* Enter it in the contents hash table. */
381 dc
->path_key
= strdup(w32_path
);
382 dc
->mtime
= st
.st_mtime
;
385 * NTFS is the only WINDOWS32 filesystem that bumps mtime
386 * on a directory when files are added/deleted from
390 if (GetVolumeInformation(w32_path
,
391 fs_label
, sizeof (fs_label
),
393 &fs_flags
, fs_type
, sizeof (fs_type
)) == FALSE
)
394 dc
->fs_flags
= FS_UNKNOWN
;
395 else if (!strcmp(fs_type
, "FAT"))
396 dc
->fs_flags
= FS_FAT
;
397 else if (!strcmp(fs_type
, "NTFS"))
398 dc
->fs_flags
= FS_NTFS
;
400 dc
->fs_flags
= FS_UNKNOWN
;
403 dc
->ino
[0] = st
.st_ino
[0];
404 dc
->ino
[1] = st
.st_ino
[1];
405 dc
->ino
[2] = st
.st_ino
[2];
409 #endif /* WINDOWS32 */
410 dc
->next
= directories_contents
[hash
];
411 directories_contents
[hash
] = dc
;
413 dc
->dirstream
= opendir (name
);
414 if (dc
->dirstream
== 0)
416 /* Couldn't open the directory. Mark this by
417 setting the `files' member to a nil pointer. */
422 /* Allocate an array of buckets for files and zero it. */
423 dc
->files
= (struct dirfile
**)
424 xmalloc (sizeof (struct dirfile
*) * DIRFILE_BUCKETS
);
425 bzero ((char *) dc
->files
,
426 sizeof (struct dirfile
*) * DIRFILE_BUCKETS
);
428 /* Keep track of how many directories are open. */
430 if (open_directories
== MAX_OPEN_DIRECTORIES
)
431 /* We have too many directories open already.
432 Read the entire directory and then close it. */
433 (void) dir_contents_file_exists_p (dc
, (char *) 0);
437 /* Point the name-hashed entry for DIR at its contents data. */
445 /* Return 1 if the name FILENAME is entered in DIR's hash table.
446 FILENAME must contain no slashes. */
449 dir_contents_file_exists_p (dir
, filename
)
450 register struct directory_contents
*dir
;
451 register char *filename
;
453 register unsigned int hash
;
455 register struct dirfile
*df
;
456 register struct dirent
*d
;
462 if (dir
== 0 || dir
->files
== 0)
464 /* The directory could not be stat'd or opened. */
468 filename
= dosify (filename
);
472 filename
= amigafy (filename
);
476 filename
= vmsify (filename
,0);
482 if (*filename
== '\0')
484 /* Checking if the directory exists. */
488 for (p
= filename
; *p
!= '\0'; ++p
)
490 hash
%= DIRFILE_BUCKETS
;
492 /* Search the list of hashed files. */
494 for (df
= dir
->files
[hash
]; df
!= 0; df
= df
->next
)
496 if (strieq (df
->name
, filename
))
498 return !df
->impossible
;
503 /* The file was not found in the hashed list.
504 Try to read the directory further. */
506 if (dir
->dirstream
== 0)
510 * Check to see if directory has changed since last read. FAT
511 * filesystems force a rehash always as mtime does not change
512 * on directories (ugh!).
515 (dir
->fs_flags
& FS_FAT
||
516 (stat(dir
->path_key
, &st
) == 0 &&
517 st
.st_mtime
> dir
->mtime
))) {
519 /* reset date stamp to show most recent re-process */
520 dir
->mtime
= st
.st_mtime
;
522 /* make sure directory can still be opened */
523 dir
->dirstream
= opendir(dir
->path_key
);
528 return 0; /* couldn't re-read - fail */
531 /* The directory has been all read in. */
535 while ((d
= readdir (dir
->dirstream
)) != 0)
537 /* Enter the file in the hash table. */
538 register unsigned int newhash
= 0;
540 register unsigned int i
;
542 if (!REAL_DIR_ENTRY (d
))
546 for (i
= 0; i
< len
; ++i
)
547 HASHI (newhash
, d
->d_name
[i
]);
548 newhash
%= DIRFILE_BUCKETS
;
551 * If re-reading a directory, check that this file isn't already
555 for (df
= dir
->files
[newhash
]; df
!= 0; df
= df
->next
)
556 if (streq(df
->name
, d
->d_name
))
562 * If re-reading a directory, don't cache files that have
563 * already been discovered.
568 df
= (struct dirfile
*) xmalloc (sizeof (struct dirfile
));
569 df
->next
= dir
->files
[newhash
];
570 dir
->files
[newhash
] = df
;
571 df
->name
= savestring (d
->d_name
, len
);
576 /* Check if the name matches the one we're searching for. */
578 && newhash
== hash
&& strieq (d
->d_name
, filename
))
584 /* If the directory has been completely read in,
585 close the stream and reset the pointer to nil. */
589 closedir (dir
->dirstream
);
595 /* Return 1 if the name FILENAME in directory DIRNAME
596 is entered in the dir hash table.
597 FILENAME must contain no slashes. */
600 dir_file_exists_p (dirname
, filename
)
601 register char *dirname
;
602 register char *filename
;
604 return dir_contents_file_exists_p (find_directory (dirname
)->contents
,
608 /* Return 1 if the file named NAME exists. */
619 return ar_member_date (name
) != (time_t) -1;
623 dirend
= rindex (name
, ']');
625 if (dirend
== (char *)1)
626 return dir_file_exists_p ("[]", name
);
628 dirend
= rindex (name
, '/');
629 #if defined (WINDOWS32) || defined (__MSDOS__)
630 /* Forward and backslashes might be mixed. We need the rightmost one. */
632 char *bslash
= rindex(name
, '\\');
633 if (!dirend
|| bslash
> dirend
)
635 /* The case of "d:file" is unhandled. But I don't think
636 such names can happen here. */
638 #endif /* WINDOWS32 || __MSDOS__ */
641 return dir_file_exists_p (".", name
);
642 #else /* !VMS && !AMIGA */
643 return dir_file_exists_p ("", name
);
651 dirname
= (char *) alloca (dirend
- name
+ 1);
652 bcopy (name
, dirname
, dirend
- name
);
653 dirname
[dirend
- name
] = '\0';
655 return dir_file_exists_p (dirname
, dirend
+ 1);
658 /* Mark FILENAME as `impossible' for `file_impossible_p'.
659 This means an attempt has been made to search for FILENAME
660 as an intermediate file, and it has failed. */
663 file_impossible (filename
)
664 register char *filename
;
667 register char *p
= filename
;
668 register unsigned int hash
;
669 register struct directory
*dir
;
670 register struct dirfile
*new;
673 dirend
= rindex (p
, ']');
675 if (dirend
== (char *)1)
676 dir
= find_directory ("[]");
678 dirend
= rindex (p
, '/');
679 #if defined (WINDOWS32) || defined (__MSDOS__)
680 /* Forward and backslashes might be mixed. We need the rightmost one. */
682 char *bslash
= rindex(p
, '\\');
683 if (!dirend
|| bslash
> dirend
)
685 /* The case of "d:file" is unhandled. But I don't think
686 such names can happen here. */
688 #endif /* WINDOWS32 or __MSDOS__ */
691 dir
= find_directory ("");
692 #else /* !VMS && !AMIGA */
693 dir
= find_directory (".");
703 dirname
= (char *) alloca (dirend
- p
+ 1);
704 bcopy (p
, dirname
, dirend
- p
);
705 dirname
[dirend
- p
] = '\0';
707 dir
= find_directory (dirname
);
708 filename
= p
= dirend
+ 1;
711 for (hash
= 0; *p
!= '\0'; ++p
)
713 hash
%= DIRFILE_BUCKETS
;
715 if (dir
->contents
== 0)
717 /* The directory could not be stat'd. We allocate a contents
718 structure for it, but leave it out of the contents hash table. */
719 dir
->contents
= (struct directory_contents
*)
720 xmalloc (sizeof (struct directory_contents
));
722 dir
->contents
->path_key
= NULL
;
723 dir
->contents
->mtime
= 0;
724 #else /* WINDOWS32 */
726 dir
->contents
->dev
= 0;
727 dir
->contents
->ino
[0] = dir
->contents
->ino
[1] =
728 dir
->contents
->ino
[2] = 0;
730 dir
->contents
->dev
= dir
->contents
->ino
= 0;
732 #endif /* WINDOWS32 */
733 dir
->contents
->files
= 0;
734 dir
->contents
->dirstream
= 0;
737 if (dir
->contents
->files
== 0)
739 /* The directory was not opened; we must allocate the hash buckets. */
740 dir
->contents
->files
= (struct dirfile
**)
741 xmalloc (sizeof (struct dirfile
) * DIRFILE_BUCKETS
);
742 bzero ((char *) dir
->contents
->files
,
743 sizeof (struct dirfile
) * DIRFILE_BUCKETS
);
746 /* Make a new entry and put it in the table. */
748 new = (struct dirfile
*) xmalloc (sizeof (struct dirfile
));
749 new->next
= dir
->contents
->files
[hash
];
750 dir
->contents
->files
[hash
] = new;
751 new->name
= savestring (filename
, strlen (filename
));
755 /* Return nonzero if FILENAME has been marked impossible. */
758 file_impossible_p (filename
)
762 register char *p
= filename
;
763 register unsigned int hash
;
764 register struct directory_contents
*dir
;
765 register struct dirfile
*next
;
768 dirend
= rindex (filename
, ']');
770 dir
= find_directory ("[]")->contents
;
772 dirend
= rindex (filename
, '/');
773 #if defined (WINDOWS32) || defined (__MSDOS__)
774 /* Forward and backslashes might be mixed. We need the rightmost one. */
776 char *bslash
= rindex(filename
, '\\');
777 if (!dirend
|| bslash
> dirend
)
779 /* The case of "d:file" is unhandled. But I don't think
780 such names can happen here. */
782 #endif /* WINDOWS32 || __MSDOS__ */
785 dir
= find_directory ("")->contents
;
786 #else /* !VMS && !AMIGA */
787 dir
= find_directory (".")->contents
;
793 if (dirend
== filename
)
797 dirname
= (char *) alloca (dirend
- filename
+ 1);
798 bcopy (p
, dirname
, dirend
- p
);
799 dirname
[dirend
- p
] = '\0';
801 dir
= find_directory (dirname
)->contents
;
802 p
= filename
= dirend
+ 1;
805 if (dir
== 0 || dir
->files
== 0)
806 /* There are no files entered for this directory. */
810 p
= filename
= dosify (p
);
813 p
= filename
= amigafy (p
);
816 p
= filename
= vmsify (p
, 1);
819 for (hash
= 0; *p
!= '\0'; ++p
)
821 hash
%= DIRFILE_BUCKETS
;
823 for (next
= dir
->files
[hash
]; next
!= 0; next
= next
->next
)
824 if (strieq (filename
, next
->name
))
825 return next
->impossible
;
830 /* Return the already allocated name in the
831 directory hash table that matches DIR. */
837 return find_directory (dir
)->name
;
840 /* Print the data base of directories. */
843 print_dir_data_base ()
845 register unsigned int i
, dirs
, files
, impossible
;
846 register struct directory
*dir
;
848 puts ("\n# Directories\n");
850 dirs
= files
= impossible
= 0;
851 for (i
= 0; i
< DIRECTORY_BUCKETS
; ++i
)
852 for (dir
= directories
[i
]; dir
!= 0; dir
= dir
->next
)
855 if (dir
->contents
== 0)
856 printf ("# %s: could not be stat'd.\n", dir
->name
);
857 else if (dir
->contents
->files
== 0)
859 printf ("# %s (key %s, mtime %d): could not be opened.\n",
860 dir
->name
, dir
->contents
->path_key
,dir
->contents
->mtime
);
861 #else /* WINDOWS32 */
863 printf ("# %s (device %d, inode [%d,%d,%d]): could not be opened.\n",
864 dir
->name
, dir
->contents
->dev
,
865 dir
->contents
->ino
[0], dir
->contents
->ino
[1],
866 dir
->contents
->ino
[2]);
868 printf ("# %s (device %ld, inode %ld): could not be opened.\n",
869 dir
->name
, (long int) dir
->contents
->dev
,
870 (long int) dir
->contents
->ino
);
872 #endif /* WINDOWS32 */
875 register unsigned int f
= 0, im
= 0;
876 register unsigned int j
;
877 register struct dirfile
*df
;
878 for (j
= 0; j
< DIRFILE_BUCKETS
; ++j
)
879 for (df
= dir
->contents
->files
[j
]; df
!= 0; df
= df
->next
)
885 printf ("# %s (key %s, mtime %d): ",
886 dir
->name
, dir
->contents
->path_key
, dir
->contents
->mtime
);
887 #else /* WINDOWS32 */
889 printf ("# %s (device %d, inode [%d,%d,%d]): ",
890 dir
->name
, dir
->contents
->dev
,
891 dir
->contents
->ino
[0], dir
->contents
->ino
[1],
892 dir
->contents
->ino
[2]);
894 printf ("# %s (device %d, inode %d): ",
895 dir
->name
, dir
->contents
->dev
, dir
->contents
->ino
);
897 #endif /* WINDOWS32 */
899 fputs ("No", stdout
);
902 fputs (" files, ", stdout
);
904 fputs ("no", stdout
);
907 fputs (" impossibilities", stdout
);
908 if (dir
->contents
->dirstream
== 0)
917 fputs ("\n# ", stdout
);
919 fputs ("No", stdout
);
921 printf ("%u", files
);
922 fputs (" files, ", stdout
);
924 fputs ("no", stdout
);
926 printf ("%u", impossible
);
927 printf (" impossibilities in %u directories.\n", dirs
);
930 /* Hooks for globbing. */
934 /* Structure describing state of iterating through a directory hash table. */
938 struct directory_contents
*contents
; /* The directory being read. */
940 unsigned int bucket
; /* Current hash bucket. */
941 struct dirfile
*elt
; /* Current elt in bucket. */
944 /* Forward declarations. */
945 static __ptr_t open_dirstream
PARAMS ((const char *));
946 static struct dirent
*read_dirstream
PARAMS ((__ptr_t
));
949 open_dirstream (directory
)
950 const char *directory
;
952 struct dirstream
*new;
953 struct directory
*dir
= find_directory ((char *)directory
);
955 if (dir
->contents
== 0 || dir
->contents
->files
== 0)
956 /* DIR->contents is nil if the directory could not be stat'd.
957 DIR->contents->files is nil if it could not be opened. */
960 /* Read all the contents of the directory now. There is no benefit
961 in being lazy, since glob will want to see every file anyway. */
963 (void) dir_contents_file_exists_p (dir
->contents
, (char *) 0);
965 new = (struct dirstream
*) xmalloc (sizeof (struct dirstream
));
966 new->contents
= dir
->contents
;
968 new->elt
= new->contents
->files
[0];
970 return (__ptr_t
) new;
973 static struct dirent
*
974 read_dirstream (stream
)
977 struct dirstream
*const ds
= (struct dirstream
*) stream
;
978 register struct dirfile
*df
;
980 static unsigned int bufsz
;
982 while (ds
->bucket
< DIRFILE_BUCKETS
)
984 while ((df
= ds
->elt
) != 0)
989 /* The glob interface wants a `struct dirent',
992 unsigned int len
= strlen (df
->name
) + 1;
993 if (sizeof *d
- sizeof d
->d_name
+ len
> bufsz
)
998 if (sizeof *d
- sizeof d
->d_name
+ len
> bufsz
)
999 bufsz
= sizeof *d
- sizeof d
->d_name
+ len
;
1000 buf
= xmalloc (bufsz
);
1002 d
= (struct dirent
*) buf
;
1004 #ifdef _DIRENT_HAVE_D_NAMLEN
1005 d
->d_namlen
= len
- 1;
1007 memcpy (d
->d_name
, df
->name
, len
);
1011 if (++ds
->bucket
== DIRFILE_BUCKETS
)
1013 ds
->elt
= ds
->contents
->files
[ds
->bucket
];
1025 /* Bogus sunos4 compiler complains (!) about & before functions. */
1026 gl
->gl_opendir
= open_dirstream
;
1027 gl
->gl_readdir
= read_dirstream
;
1028 gl
->gl_closedir
= free
;
1030 /* We don't bother setting gl_lstat, since glob never calls it.
1031 The slot is only there for compatibility with 4.4 BSD. */