1 /* Directory hashing for GNU Make.
2 Copyright (C) 1988, 89, 91, 92, 93, 94, 95, 96 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 (WIN32)) && !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)
64 static char dos_filename
[14];
71 if (strpbrk (filename
, "\"*+,;<=>?[\\]|") != 0)
76 /* First, transform the name part. */
77 for (i
= 0; *filename
!= '\0' && i
< 8 && *filename
!= '.'; ++i
)
78 *df
++ = tolower (*filename
++);
80 /* Now skip to the next dot. */
81 while (*filename
!= '\0' && *filename
!= '.')
83 if (*filename
!= '\0')
86 for (i
= 0; *filename
!= '\0' && i
< 3 && *filename
!= '.'; ++i
)
87 *df
++ = tolower (*filename
++);
90 /* Look for more dots. */
91 while (*filename
!= '\0' && *filename
!= '.')
98 #endif /* __MSDOS__ */
101 #include "pathstuff.h"
111 static char amiga_filename
[136];
120 /* First, transform the name part. */
121 for (i
= 0; *filename
!= '\0'; ++i
)
123 *df
++ = tolower (*filename
);
129 return amiga_filename
;
144 h
= (h
<< 4) + *name
++;
155 /* fake stat entry for a directory */
157 vmsstat_dir (name
, st
)
165 dir
= opendir (name
);
169 s
= strchr (name
, ':'); /* find device */
173 st
->st_dev
= (char *)vms_hash (name
);
181 st
->st_ino
[0] = h
& 0xff;
182 st
->st_ino
[1] = h
& 0xff00;
183 st
->st_ino
[2] = h
>> 16;
189 /* Hash table of directories. */
191 #ifndef DIRECTORY_BUCKETS
192 #define DIRECTORY_BUCKETS 199
195 struct directory_contents
197 struct directory_contents
*next
;
199 dev_t dev
; /* Device and inode numbers of this dir. */
202 * Inode means nothing on WIN32. Even file key information is
203 * unreliable because it is random per file open and undefined
204 * for remote filesystems. The most unique attribute I can
205 * come up with is the fully qualified name of the directory. Beware
206 * though, this is also unreliable. I'm open to suggestion on a better
207 * way to emulate inode.
210 int mtime
; /* controls check for stale directory cache */
218 struct dirfile
**files
; /* Files in this directory. */
219 DIR *dirstream
; /* Stream reading this directory. */
222 /* Table of directory contents hashed by device and inode number. */
223 static struct directory_contents
*directories_contents
[DIRECTORY_BUCKETS
];
227 struct directory
*next
;
229 char *name
; /* Name of the directory. */
231 /* The directory's contents. This data may be shared by several
232 entries in the hash table, which refer to the same directory
233 (identified uniquely by `dev' and `ino') under different names. */
234 struct directory_contents
*contents
;
237 /* Table of directories hashed by name. */
238 static struct directory
*directories
[DIRECTORY_BUCKETS
];
241 /* Never have more than this many directories open at once. */
243 #define MAX_OPEN_DIRECTORIES 10
245 static unsigned int open_directories
= 0;
248 /* Hash table of files in each directory. */
252 struct dirfile
*next
;
253 char *name
; /* Name of the file. */
254 char impossible
; /* This file is impossible. */
257 #ifndef DIRFILE_BUCKETS
258 #define DIRFILE_BUCKETS 107
261 static int dir_contents_file_exists_p
PARAMS ((struct directory_contents
*dir
, char *filename
));
262 static struct directory
*find_directory
PARAMS ((char *name
));
264 /* Find the directory named NAME and return its `struct directory'. */
266 static struct directory
*
267 find_directory (name
)
270 register unsigned int hash
= 0;
272 register struct directory
*dir
;
277 if ((*name
== '.') && (*(name
+1) == 0))
280 name
= vmsify (name
,1);
283 for (p
= name
; *p
!= '\0'; ++p
)
285 hash
%= DIRECTORY_BUCKETS
;
287 for (dir
= directories
[hash
]; dir
!= 0; dir
= dir
->next
)
288 if (strieq (dir
->name
, name
))
295 /* The directory was not found. Create a new entry for it. */
297 dir
= (struct directory
*) xmalloc (sizeof (struct directory
));
298 dir
->next
= directories
[hash
];
299 directories
[hash
] = dir
;
300 dir
->name
= savestring (name
, p
- name
);
302 /* The directory is not in the name hash table.
303 Find its device and inode numbers, and look it up by them. */
306 if (vmsstat_dir (name
, &st
) < 0)
308 if (stat (name
, &st
) < 0)
311 /* Couldn't stat the directory. Mark this by
312 setting the `contents' member to a nil pointer. */
317 /* Search the contents hash table; device and inode are the key. */
319 struct directory_contents
*dc
;
322 w32_path
= w32ify(name
, 1);
323 hash
= ((unsigned int) st
.st_dev
<< 16) | (unsigned int) st
.st_ctime
;
326 hash
= ((unsigned int) st
.st_dev
<< 16)
327 | ((unsigned int) st
.st_ino
[0]
328 + (unsigned int) st
.st_ino
[1]
329 + (unsigned int) st
.st_ino
[2]);
331 hash
= ((unsigned int) st
.st_dev
<< 16) | (unsigned int) st
.st_ino
;
334 hash
%= DIRECTORY_BUCKETS
;
336 for (dc
= directories_contents
[hash
]; dc
!= 0; dc
= dc
->next
)
338 if (!strcmp(dc
->path_key
, w32_path
))
340 if (dc
->dev
== st
.st_dev
342 && dc
->ino
[0] == st
.st_ino
[0]
343 && dc
->ino
[1] == st
.st_ino
[1]
344 && dc
->ino
[2] == st
.st_ino
[2])
346 && dc
->ino
== st
.st_ino
)
353 /* Nope; this really is a directory we haven't seen before. */
355 dc
= (struct directory_contents
*)
356 xmalloc (sizeof (struct directory_contents
));
358 /* Enter it in the contents hash table. */
361 dc
->path_key
= strdup(w32_path
);
362 dc
->mtime
= st
.st_mtime
;
365 dc
->ino
[0] = st
.st_ino
[0];
366 dc
->ino
[1] = st
.st_ino
[1];
367 dc
->ino
[2] = st
.st_ino
[2];
372 dc
->next
= directories_contents
[hash
];
373 directories_contents
[hash
] = dc
;
375 dc
->dirstream
= opendir (name
);
376 if (dc
->dirstream
== 0)
378 /* Couldn't open the directory. Mark this by
379 setting the `files' member to a nil pointer. */
384 /* Allocate an array of buckets for files and zero it. */
385 dc
->files
= (struct dirfile
**)
386 xmalloc (sizeof (struct dirfile
*) * DIRFILE_BUCKETS
);
387 bzero ((char *) dc
->files
,
388 sizeof (struct dirfile
*) * DIRFILE_BUCKETS
);
390 /* Keep track of how many directories are open. */
392 if (open_directories
== MAX_OPEN_DIRECTORIES
)
393 /* We have too many directories open already.
394 Read the entire directory and then close it. */
395 (void) dir_contents_file_exists_p (dc
, (char *) 0);
399 /* Point the name-hashed entry for DIR at its contents data. */
407 /* Return 1 if the name FILENAME is entered in DIR's hash table.
408 FILENAME must contain no slashes. */
411 dir_contents_file_exists_p (dir
, filename
)
412 register struct directory_contents
*dir
;
413 register char *filename
;
415 register unsigned int hash
;
417 register struct dirfile
*df
;
418 register struct dirent
*d
;
424 if (dir
== 0 || dir
->files
== 0)
426 /* The directory could not be stat'd or opened. */
430 filename
= dosify (filename
);
434 filename
= amigafy (filename
);
438 filename
= vmsify (filename
,0);
444 if (*filename
== '\0')
446 /* Checking if the directory exists. */
450 for (p
= filename
; *p
!= '\0'; ++p
)
452 hash
%= DIRFILE_BUCKETS
;
454 /* Search the list of hashed files. */
456 for (df
= dir
->files
[hash
]; df
!= 0; df
= df
->next
)
458 if (strieq (df
->name
, filename
))
460 return !df
->impossible
;
465 /* The file was not found in the hashed list.
466 Try to read the directory further. */
468 if (dir
->dirstream
== 0)
471 /* Check to see if directory has changed since last read */
473 stat(dir
->path_key
, &st
) == 0 &&
474 st
.st_mtime
> dir
->mtime
) {
476 /* reset date stamp to show most recent re-process */
477 dir
->mtime
= st
.st_mtime
;
479 /* make sure directory can still be opened */
480 dir
->dirstream
= opendir(dir
->path_key
);
485 return 0; /* couldn't re-read - fail */
488 /* The directory has been all read in. */
492 while ((d
= readdir (dir
->dirstream
)) != 0)
494 /* Enter the file in the hash table. */
495 register unsigned int newhash
= 0;
497 register unsigned int i
;
499 if (!REAL_DIR_ENTRY (d
))
503 for (i
= 0; i
< len
; ++i
)
504 HASHI (newhash
, d
->d_name
[i
]);
505 newhash
%= DIRFILE_BUCKETS
;
508 * If re-reading a directory, check that this file isn't already
512 for (df
= dir
->files
[newhash
]; df
!= 0; df
= df
->next
)
513 if (streq(df
->name
, d
->d_name
))
519 * If re-reading a directory, don't cache files that have
520 * already been discovered.
525 df
= (struct dirfile
*) xmalloc (sizeof (struct dirfile
));
526 df
->next
= dir
->files
[newhash
];
527 dir
->files
[newhash
] = df
;
528 df
->name
= savestring (d
->d_name
, len
);
533 /* Check if the name matches the one we're searching for. */
535 && newhash
== hash
&& strieq (d
->d_name
, filename
))
541 /* If the directory has been completely read in,
542 close the stream and reset the pointer to nil. */
546 closedir (dir
->dirstream
);
552 /* Return 1 if the name FILENAME in directory DIRNAME
553 is entered in the dir hash table.
554 FILENAME must contain no slashes. */
557 dir_file_exists_p (dirname
, filename
)
558 register char *dirname
;
559 register char *filename
;
561 return dir_contents_file_exists_p (find_directory (dirname
)->contents
,
565 /* Return 1 if the file named NAME exists. */
576 return ar_member_date (name
) != (time_t) -1;
580 dirend
= rindex (name
, ']');
582 if (dirend
== (char *)1)
583 return dir_file_exists_p ("[]", name
);
585 dirend
= rindex (name
, '/');
588 dirend
= rindex(name
, '\\');
591 return dir_file_exists_p (".", name
);
594 return dir_file_exists_p (".", name
);
595 #else /* !VMS && !AMIGA */
596 return dir_file_exists_p ("", name
);
600 dirname
= (char *) alloca (dirend
- name
+ 1);
601 bcopy (name
, dirname
, dirend
- name
);
602 dirname
[dirend
- name
] = '\0';
603 return dir_file_exists_p (dirname
, dirend
+ 1);
606 /* Mark FILENAME as `impossible' for `file_impossible_p'.
607 This means an attempt has been made to search for FILENAME
608 as an intermediate file, and it has failed. */
611 file_impossible (filename
)
612 register char *filename
;
615 register char *p
= filename
;
616 register unsigned int hash
;
617 register struct directory
*dir
;
618 register struct dirfile
*new;
621 dirend
= rindex (p
, ']');
623 if (dirend
== (char *)1)
624 dir
= find_directory ("[]");
626 dirend
= rindex (p
, '/');
629 dir
= find_directory ("");
630 #else /* !VMS && !AMIGA */
631 dir
= find_directory (".");
636 char *dirname
= (char *) alloca (dirend
- p
+ 1);
637 bcopy (p
, dirname
, dirend
- p
);
638 dirname
[dirend
- p
] = '\0';
639 dir
= find_directory (dirname
);
640 filename
= p
= dirend
+ 1;
643 for (hash
= 0; *p
!= '\0'; ++p
)
645 hash
%= DIRFILE_BUCKETS
;
647 if (dir
->contents
== 0)
649 /* The directory could not be stat'd. We allocate a contents
650 structure for it, but leave it out of the contents hash table. */
651 dir
->contents
= (struct directory_contents
*)
652 xmalloc (sizeof (struct directory_contents
));
654 dir
->contents
->path_key
= NULL
;
655 dir
->contents
->mtime
= 0;
658 dir
->contents
->dev
= 0;
659 dir
->contents
->ino
[0] = dir
->contents
->ino
[1] =
660 dir
->contents
->ino
[2] = 0;
662 dir
->contents
->dev
= dir
->contents
->ino
= 0;
665 dir
->contents
->files
= 0;
666 dir
->contents
->dirstream
= 0;
669 if (dir
->contents
->files
== 0)
671 /* The directory was not opened; we must allocate the hash buckets. */
672 dir
->contents
->files
= (struct dirfile
**)
673 xmalloc (sizeof (struct dirfile
) * DIRFILE_BUCKETS
);
674 bzero ((char *) dir
->contents
->files
,
675 sizeof (struct dirfile
) * DIRFILE_BUCKETS
);
678 /* Make a new entry and put it in the table. */
680 new = (struct dirfile
*) xmalloc (sizeof (struct dirfile
));
681 new->next
= dir
->contents
->files
[hash
];
682 dir
->contents
->files
[hash
] = new;
683 new->name
= savestring (filename
, strlen (filename
));
687 /* Return nonzero if FILENAME has been marked impossible. */
690 file_impossible_p (filename
)
694 register char *p
= filename
;
695 register unsigned int hash
;
696 register struct directory_contents
*dir
;
697 register struct dirfile
*next
;
700 dirend
= rindex (filename
, ']');
702 dir
= find_directory ("[]")->contents
;
704 dirend
= rindex (filename
, '/');
707 dirend
= rindex (filename
, '\\');
711 dir
= find_directory ("")->contents
;
712 #else /* !VMS && !AMIGA */
713 dir
= find_directory (".")->contents
;
718 char *dirname
= (char *) alloca (dirend
- filename
+ 1);
719 bcopy (p
, dirname
, dirend
- p
);
720 dirname
[dirend
- p
] = '\0';
721 dir
= find_directory (dirname
)->contents
;
722 p
= filename
= dirend
+ 1;
725 if (dir
== 0 || dir
->files
== 0)
726 /* There are no files entered for this directory. */
730 p
= filename
= dosify (p
);
733 p
= filename
= amigafy (p
);
736 p
= filename
= vmsify (p
, 1);
739 for (hash
= 0; *p
!= '\0'; ++p
)
741 hash
%= DIRFILE_BUCKETS
;
743 for (next
= dir
->files
[hash
]; next
!= 0; next
= next
->next
)
744 if (strieq (filename
, next
->name
))
745 return next
->impossible
;
750 /* Return the already allocated name in the
751 directory hash table that matches DIR. */
757 return find_directory (dir
)->name
;
760 /* Print the data base of directories. */
763 print_dir_data_base ()
765 register unsigned int i
, dirs
, files
, impossible
;
766 register struct directory
*dir
;
768 puts ("\n# Directories\n");
770 dirs
= files
= impossible
= 0;
771 for (i
= 0; i
< DIRECTORY_BUCKETS
; ++i
)
772 for (dir
= directories
[i
]; dir
!= 0; dir
= dir
->next
)
775 if (dir
->contents
== 0)
776 printf ("# %s: could not be stat'd.\n", dir
->name
);
777 else if (dir
->contents
->files
== 0)
779 printf ("# %s (key %s, mtime %d): could not be opened.\n",
780 dir
->name
, dir
->contents
->path_key
,dir
->contents
->mtime
);
783 printf ("# %s (device %d, inode [%d,%d,%d]): could not be opened.\n",
784 dir
->name
, dir
->contents
->dev
,
785 dir
->contents
->ino
[0], dir
->contents
->ino
[1],
786 dir
->contents
->ino
[2]);
788 printf ("# %s (device %ld, inode %ld): could not be opened.\n",
789 dir
->name
, (long int) dir
->contents
->dev
,
790 (long int) dir
->contents
->ino
);
795 register unsigned int f
= 0, im
= 0;
796 register unsigned int j
;
797 register struct dirfile
*df
;
798 for (j
= 0; j
< DIRFILE_BUCKETS
; ++j
)
799 for (df
= dir
->contents
->files
[j
]; df
!= 0; df
= df
->next
)
805 printf ("# %s (key %s, mtime %d): ",
806 dir
->name
, dir
->contents
->path_key
, dir
->contents
->mtime
);
809 printf ("# %s (device %d, inode [%d,%d,%d]): ",
810 dir
->name
, dir
->contents
->dev
,
811 dir
->contents
->ino
[0], dir
->contents
->ino
[1],
812 dir
->contents
->ino
[2]);
814 printf ("# %s (device %d, inode %d): ",
815 dir
->name
, dir
->contents
->dev
, dir
->contents
->ino
);
819 fputs ("No", stdout
);
822 fputs (" files, ", stdout
);
824 fputs ("no", stdout
);
827 fputs (" impossibilities", stdout
);
828 if (dir
->contents
->dirstream
== 0)
837 fputs ("\n# ", stdout
);
839 fputs ("No", stdout
);
841 printf ("%u", files
);
842 fputs (" files, ", stdout
);
844 fputs ("no", stdout
);
846 printf ("%u", impossible
);
847 printf (" impossibilities in %u directories.\n", dirs
);
850 /* Hooks for globbing. */
854 /* Structure describing state of iterating through a directory hash table. */
858 struct directory_contents
*contents
; /* The directory being read. */
860 unsigned int bucket
; /* Current hash bucket. */
861 struct dirfile
*elt
; /* Current elt in bucket. */
864 /* Forward declarations. */
865 static __ptr_t open_dirstream
PARAMS ((const char *));
866 static struct dirent
*read_dirstream
PARAMS ((__ptr_t
));
869 open_dirstream (directory
)
870 const char *directory
;
872 struct dirstream
*new;
873 struct directory
*dir
= find_directory ((char *)directory
);
875 if (dir
->contents
== 0 || dir
->contents
->files
== 0)
876 /* DIR->contents is nil if the directory could not be stat'd.
877 DIR->contents->files is nil if it could not be opened. */
880 /* Read all the contents of the directory now. There is no benefit
881 in being lazy, since glob will want to see every file anyway. */
883 (void) dir_contents_file_exists_p (dir
->contents
, (char *) 0);
885 new = (struct dirstream
*) xmalloc (sizeof (struct dirstream
));
886 new->contents
= dir
->contents
;
888 new->elt
= new->contents
->files
[0];
890 return (__ptr_t
) new;
893 static struct dirent
*
894 read_dirstream (stream
)
897 struct dirstream
*const ds
= (struct dirstream
*) stream
;
898 register struct dirfile
*df
;
900 static unsigned int bufsz
;
902 while (ds
->bucket
< DIRFILE_BUCKETS
)
904 while ((df
= ds
->elt
) != 0)
909 /* The glob interface wants a `struct dirent',
912 unsigned int len
= strlen (df
->name
) + 1;
913 if (sizeof *d
- sizeof d
->d_name
+ len
> bufsz
)
918 if (sizeof *d
- sizeof d
->d_name
+ len
> bufsz
)
919 bufsz
= sizeof *d
- sizeof d
->d_name
+ len
;
920 buf
= xmalloc (bufsz
);
922 d
= (struct dirent
*) buf
;
924 #ifdef _DIRENT_HAVE_D_NAMLEN
925 d
->d_namlen
= len
- 1;
927 memcpy (d
->d_name
, df
->name
, len
);
931 if (++ds
->bucket
== DIRFILE_BUCKETS
)
933 ds
->elt
= ds
->contents
->files
[ds
->bucket
];
945 /* Bogus sunos4 compiler complains (!) about & before functions. */
946 gl
->gl_opendir
= open_dirstream
;
947 gl
->gl_readdir
= read_dirstream
;
948 gl
->gl_closedir
= free
;
950 /* We don't bother setting gl_lstat, since glob never calls it.
951 The slot is only there for compatibility with 4.4 BSD. */