merge the formfield patch from ooo-build
[ooovba.git] / dmake / unix / dcache.c
blob9b893577d789c620cb0f328f2217c6f576d015d6
1 /* $RCSfile: dcache.c,v $
2 -- $Revision: 1.9 $
3 -- last change: $Author: ihi $ $Date: 2007-10-15 15:52:47 $
4 --
5 -- SYNOPSIS
6 -- Directory cache management routines.
7 --
8 -- DESCRIPTION
9 -- This is the code that maintains a directory cache for each directory
10 -- that dmake visits. The entire directory is thus only read once and
11 -- the need for performing costly 'stat' calls when performing target
12 -- inference is much reduced. The improvement in performance should be
13 -- significant for NFS or remote mounted file systems.
15 -- AUTHOR
16 -- Dennis Vadura, dvadura@dmake.wticorp.com
18 -- WWW
19 -- http://dmake.wticorp.com/
21 -- COPYRIGHT
22 -- Copyright (c) 1996,1997 by WTI Corp. All rights reserved.
23 --
24 -- This program is NOT free software; you can redistribute it and/or
25 -- modify it under the terms of the Software License Agreement Provided
26 -- in the file <distribution-root>/readme/license.txt.
28 -- LOG
29 -- Use cvs log to obtain detailed change logs.
32 /* For Borland 5.00 compile, for some reason they seem to insist on pulling
33 * in the winnt.h if __WIN32__ is defined and you include <dirent.h>. This
34 * is, in my opinion, a BUG! on Borland's part.
36 #if defined(__BORLANDC__) && defined(__WIN32__)
37 #undef __WIN32__
38 #endif
40 #ifdef __APPLE__
41 #include <sys/types.h>
42 #endif
43 #include <dirent.h>
44 #include "extern.h"
45 #include "sysintf.h"
48 typedef struct ent {
49 char *name;
50 uint32 hkey;
51 time_t mtime;
52 int isdir;
53 struct ent *next;
54 } Entry, *EntryPtr;
57 typedef struct mydir {
58 char *path;
59 uint32 hkey;
60 EntryPtr entries;
61 struct mydir *next;
62 } DirEntry, *DirEntryPtr;
64 static DirEntryPtr dtab[HASH_TABLE_SIZE];
67 /* Stat a path using the directory cache.
69 * We build a cannonical representation of the path using either an absolute
70 * path name if that is what 'path' is or the relative path name constructed
71 * from 'path' and the present value of Pwd.
73 * The present value of Pwd then gives a directory path that we search for
74 * in our cache using a hash lookup. If the directory component is located
75 * then we search the basename component of the path and return the result of
76 * the search: 0L if the component is not in the cache and it's time stamp
77 * otherwise.
79 * If the directory is not in our cache we insert it into the cache by
80 * openning the directory and reading all of the files within. Once read
81 * then we return the result of the above search.
83 * Optionally, if force is TRUE, and we did NOT read the directory to provide
84 * the result then stat the file anyway and update the internal cache.
87 PUBLIC time_t
88 CacheStat(path, force)
89 char *path;
90 int force;
92 struct stat stbuf;
93 DirEntryPtr dp;
94 EntryPtr ep;
95 uint32 hkey;
96 uint16 hv;
97 char *fpath;
98 char *spath;
99 char *comp;
100 char *dir;
101 char *udir; /* Hold the unchanged (DcacheRespCase) directory. */
102 int loaded=FALSE;
104 if (If_root_path(path))
105 spath = path;
106 else
107 spath = Build_path(Pwd,path);
109 fpath = DmStrDup(spath);
111 comp = Basename(fpath); /* Use before the Filedir() call. */
112 dir = Filedir(fpath);
114 /* do caching and comparing lower case if told so. */
115 if( !STOBOOL(DcacheRespCase) ) {
116 udir = DmStrDup(dir);
117 strlwr(comp);
118 strlwr(dir);
119 } else
120 udir = dir;
122 hv = Hash(dir,&hkey);
124 for(dp=dtab[hv]; dp; dp=dp->next)
125 if (hkey == dp->hkey && strcmp(dp->path,dir) == 0)
126 break;
128 if (!dp) {
129 /* Not cached yet, doing it now. */
130 DIR *dirp;
131 struct dirent *direntp;
133 if( Verbose & V_DIR_CACHE )
134 printf( "%s: Caching directory [%s]\n", Pname, dir );
136 /* Load the directory, we have the right hash position already */
137 loaded = TRUE;
139 TALLOC(dp,1,DirEntry);
140 dp->next = dtab[hv];
141 dtab[hv] = dp;
142 dp->path = DmStrDup(dir);
143 dp->hkey = hkey;
145 /* We use the unchanged (not potentially lowercased because of
146 * DcacheRespCase) directory as this would fail on a case sensitive
147 * file system.
148 * Note: Using case insensitive directory caching on case sensitive
149 * file systems is a *BAD* idea. If in doubt use case sensitive
150 * directory caching even on case insensitive file systems as the
151 * worst case in this szenario is that /foo/bar/ and /foo/BAR/ are
152 * cached separately (with the same content) even though they are
153 * the same directory. This would only happen if different targets
154 * using different upper/lower case spellings for the same directory
155 * and is *never* a good idea. */
156 if (Set_dir(udir) == 0) {
157 if((dirp=opendir(".")) != NIL(DIR)) {
158 while((direntp=readdir(dirp)) != NULL) {
159 TALLOC(ep,1,Entry);
160 ep->name = DmStrDup(direntp->d_name); /* basename only */
161 if( !STOBOOL(DcacheRespCase) )
162 strlwr(ep->name);
164 Hash(ep->name, &ep->hkey); /* This sets ep->hkey. */
166 ep->next = dp->entries;
167 dp->entries = ep;
168 DMSTAT(direntp->d_name,&stbuf);
169 ep->isdir = (stbuf.st_mode & S_IFDIR);
170 ep->mtime = stbuf.st_mtime;
172 closedir(dirp);
174 Set_dir(Pwd);
178 Hash(comp, &hkey); /* Calculate hkey. */
180 /* search in dp->entries for comp. */
181 if (dp) {
182 for(ep=dp->entries; ep; ep=ep->next)
183 if(hkey == ep->hkey && strcmp(ep->name,comp) == 0)
184 break;
186 else
187 ep = NULL;
189 if( force && !loaded) {
190 if (strlen(comp) > NameMax || DMSTAT(spath,&stbuf) != 0) {
191 /* Either file to long or the stat failed. */
192 if (strlen(comp) > NameMax)
193 Warning( "File [%s] longer than value of NAMEMAX [%d].\n\
194 Assume unix time 0.\n", comp, NameMax );
195 if(ep)
196 ep->mtime = 0L;
198 else {
199 if (!ep) {
200 TALLOC(ep,1,Entry);
201 ep->name = DmStrDup(comp);
202 if( !STOBOOL(DcacheRespCase) )
203 strlwr(ep->name);
204 Hash(ep->name, &ep->hkey);
205 ep->next = dp->entries;
206 ep->isdir = (stbuf.st_mode & S_IFDIR);
207 dp->entries = ep;
210 ep->mtime = stbuf.st_mtime;
213 if( Verbose & V_DIR_CACHE )
214 printf("%s: Updating dir cache entry for [%s], new time is %ld\n",
215 Pname, spath, ep ? ep->mtime : 0L);
218 if( udir != dir )
219 FREE(udir); /* Keep this before the free of fpath. */
221 FREE(fpath);
222 return(!ep ? (time_t)0L : ((STOBOOL(Augmake) && ep->isdir)?0L:ep->mtime));