1 | /* $Id: kFsCache.h 3381 2020-06-12 11:36:10Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kFsCache.c - NT directory content cache.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2016 knut st. osmundsen <[email protected]>
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person obtaining a
|
---|
10 | * copy of this software and associated documentation files (the "Software"),
|
---|
11 | * to deal in the Software without restriction, including without limitation
|
---|
12 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
---|
13 | * and/or sell copies of the Software, and to permit persons to whom the
|
---|
14 | * Software is furnished to do so, subject to the following conditions:
|
---|
15 | *
|
---|
16 | * The above copyright notice and this permission notice shall be included
|
---|
17 | * in all copies or substantial portions of the Software.
|
---|
18 | *
|
---|
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
---|
22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
---|
25 | * IN THE SOFTWARE.
|
---|
26 | *
|
---|
27 | * Alternatively, the content of this file may be used under the terms of the
|
---|
28 | * GPL version 2 or later, or LGPL version 2.1 or later.
|
---|
29 | */
|
---|
30 |
|
---|
31 | #ifndef ___lib_nt_kFsCache_h___
|
---|
32 | #define ___lib_nt_kFsCache_h___
|
---|
33 |
|
---|
34 |
|
---|
35 | #include <k/kHlp.h>
|
---|
36 | #include "ntstat.h"
|
---|
37 | #ifndef NDEBUG
|
---|
38 | # include <stdarg.h>
|
---|
39 | #endif
|
---|
40 |
|
---|
41 |
|
---|
42 | /** @def KFSCACHE_CFG_UTF16
|
---|
43 | * Whether to compile in the UTF-16 names support. */
|
---|
44 | #define KFSCACHE_CFG_UTF16 1
|
---|
45 | /** @def KFSCACHE_CFG_SHORT_NAMES
|
---|
46 | * Whether to compile in the short name support. */
|
---|
47 | #define KFSCACHE_CFG_SHORT_NAMES 1
|
---|
48 | /** @def KFSCACHE_CFG_PATH_HASH_TAB_SIZE
|
---|
49 | * Size of the path hash table. */
|
---|
50 | #define KFSCACHE_CFG_PATH_HASH_TAB_SIZE 99991
|
---|
51 | /** The max length paths we consider. */
|
---|
52 | #define KFSCACHE_CFG_MAX_PATH 1024
|
---|
53 | /** The max ANSI name length. */
|
---|
54 | #define KFSCACHE_CFG_MAX_ANSI_NAME (256*3 + 16)
|
---|
55 | /** The max UTF-16 name length. */
|
---|
56 | #define KFSCACHE_CFG_MAX_UTF16_NAME (256*2 + 16)
|
---|
57 | /** Enables locking of the cache and thereby making it thread safe. */
|
---|
58 | #define KFSCACHE_CFG_LOCKING 1
|
---|
59 |
|
---|
60 |
|
---|
61 |
|
---|
62 | /** Special KFSOBJ::uCacheGen number indicating that it does not apply. */
|
---|
63 | #define KFSOBJ_CACHE_GEN_IGNORE KU32_MAX
|
---|
64 |
|
---|
65 |
|
---|
66 | /** @name KFSOBJ_TYPE_XXX - KFSOBJ::bObjType
|
---|
67 | * @{ */
|
---|
68 | /** Directory, type KFSDIR. */
|
---|
69 | #define KFSOBJ_TYPE_DIR KU8_C(0x01)
|
---|
70 | /** Regular file - type KFSOBJ. */
|
---|
71 | #define KFSOBJ_TYPE_FILE KU8_C(0x02)
|
---|
72 | /** Other file - type KFSOBJ. */
|
---|
73 | #define KFSOBJ_TYPE_OTHER KU8_C(0x03)
|
---|
74 | /** Caching of a negative result - type KFSOBJ.
|
---|
75 | * @remarks We will allocate enough space for the largest cache node, so this
|
---|
76 | * can metamorph into any other object should it actually turn up. */
|
---|
77 | #define KFSOBJ_TYPE_MISSING KU8_C(0x04)
|
---|
78 | ///** Invalidated entry flag. */
|
---|
79 | //#define KFSOBJ_TYPE_F_INVALID KU8_C(0x20)
|
---|
80 | /** @} */
|
---|
81 |
|
---|
82 | /** @name KFSOBJ_F_XXX - KFSOBJ::fFlags
|
---|
83 | * @{ */
|
---|
84 | /** Use custom generation.
|
---|
85 | * @remarks This is given the value 1, as we use it as an index into
|
---|
86 | * KFSCACHE::auGenerations, 0 being the default. */
|
---|
87 | #define KFSOBJ_F_USE_CUSTOM_GEN KU32_C(0x00000001)
|
---|
88 |
|
---|
89 | /** Whether the file system update the modified timestamp of directories
|
---|
90 | * when something is removed from it or added to it.
|
---|
91 | * @remarks They say NTFS is the only windows filesystem doing this. */
|
---|
92 | #define KFSOBJ_F_WORKING_DIR_MTIME KU32_C(0x00000002)
|
---|
93 | /** NTFS file system volume. */
|
---|
94 | #define KFSOBJ_F_NTFS KU32_C(0x80000000)
|
---|
95 | /** Flags that are automatically inherited. */
|
---|
96 | #define KFSOBJ_F_INHERITED_MASK KU32_C(0xffffffff)
|
---|
97 | /** @} */
|
---|
98 |
|
---|
99 |
|
---|
100 | #define IS_ALPHA(ch) ( ((ch) >= 'A' && (ch) <= 'Z') || ((ch) >= 'a' && (ch) <= 'z') )
|
---|
101 | #define IS_SLASH(ch) ((ch) == '\\' || (ch) == '/')
|
---|
102 |
|
---|
103 |
|
---|
104 |
|
---|
105 |
|
---|
106 | /** Pointer to a cache. */
|
---|
107 | typedef struct KFSCACHE *PKFSCACHE;
|
---|
108 | /** Pointer to a core object. */
|
---|
109 | typedef struct KFSOBJ *PKFSOBJ;
|
---|
110 | /** Pointer to a directory object. */
|
---|
111 | typedef struct KFSDIR *PKFSDIR;
|
---|
112 | /** Pointer to a directory hash table entry. */
|
---|
113 | typedef struct KFSOBJHASH *PKFSOBJHASH;
|
---|
114 |
|
---|
115 |
|
---|
116 |
|
---|
117 | /** Pointer to a user data item. */
|
---|
118 | typedef struct KFSUSERDATA *PKFSUSERDATA;
|
---|
119 | /**
|
---|
120 | * User data item associated with a cache node.
|
---|
121 | */
|
---|
122 | typedef struct KFSUSERDATA
|
---|
123 | {
|
---|
124 | /** Pointer to the next piece of user data. */
|
---|
125 | PKFSUSERDATA pNext;
|
---|
126 | /** The key identifying this user. */
|
---|
127 | KUPTR uKey;
|
---|
128 | /** The destructor. */
|
---|
129 | void (*pfnDestructor)(PKFSCACHE pCache, PKFSOBJ pObj, PKFSUSERDATA pData);
|
---|
130 | } KFSUSERDATA;
|
---|
131 |
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Storage for name strings for the unlikely event that they should grow in
|
---|
135 | * length after the KFSOBJ was created.
|
---|
136 | */
|
---|
137 | typedef struct KFSOBJNAMEALLOC
|
---|
138 | {
|
---|
139 | /** Size of the allocation. */
|
---|
140 | KU32 cb;
|
---|
141 | /** The space for names. */
|
---|
142 | char abSpace[1];
|
---|
143 | } KFSOBJNAMEALLOC;
|
---|
144 | /** Name growth allocation. */
|
---|
145 | typedef KFSOBJNAMEALLOC *PKFSOBJNAMEALLOC;
|
---|
146 |
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Base cache node.
|
---|
150 | */
|
---|
151 | typedef struct KFSOBJ
|
---|
152 | {
|
---|
153 | /** Magic value (KFSOBJ_MAGIC). */
|
---|
154 | KU32 u32Magic;
|
---|
155 | /** Number of references. */
|
---|
156 | KU32 volatile cRefs;
|
---|
157 | /** The cache generation, see KFSOBJ_CACHE_GEN_IGNORE. */
|
---|
158 | KU32 uCacheGen;
|
---|
159 | /** The object type, KFSOBJ_TYPE_XXX. */
|
---|
160 | KU8 bObjType;
|
---|
161 | /** Set if the Stats member is valid, clear if not. */
|
---|
162 | KBOOL fHaveStats;
|
---|
163 | /** Internal debug field for counting path hash references.
|
---|
164 | * @internal */
|
---|
165 | KU8 cPathHashRefs;
|
---|
166 | /** Index into KFSCACHE::auUserData. */
|
---|
167 | KU8 idxUserDataLock;
|
---|
168 | /** Flags, KFSOBJ_F_XXX. */
|
---|
169 | KU32 fFlags;
|
---|
170 |
|
---|
171 | /** Hash value of the name inserted into the parent hash table.
|
---|
172 | * This is 0 if not inserted. Names are only hashed and inserted as they are
|
---|
173 | * first found thru linear searching of its siblings, and which name it is
|
---|
174 | * dependens on the lookup function (W or A) and whether the normal name or
|
---|
175 | * short name seems to have matched.
|
---|
176 | *
|
---|
177 | * @note It was ruled out as too much work to hash and track all four names,
|
---|
178 | * so instead this minimalist approach was choosen in stead. */
|
---|
179 | KU32 uNameHash;
|
---|
180 | /** Pointer to the next child with the same name hash value. */
|
---|
181 | PKFSOBJ pNextNameHash;
|
---|
182 | /** Pointer to the parent (directory).
|
---|
183 | * This is only NULL for a root. */
|
---|
184 | PKFSDIR pParent;
|
---|
185 |
|
---|
186 | /** The directory name. (Allocated after the structure.) */
|
---|
187 | const char *pszName;
|
---|
188 | /** The length of pszName. */
|
---|
189 | KU16 cchName;
|
---|
190 | /** The length of the parent path (up to where pszName starts).
|
---|
191 | * @note This is valuable when constructing an absolute path to this node by
|
---|
192 | * means of the parent pointer (no need for recursion). */
|
---|
193 | KU16 cchParent;
|
---|
194 | #ifdef KFSCACHE_CFG_UTF16
|
---|
195 | /** The length of pwszName (in wchar_t's). */
|
---|
196 | KU16 cwcName;
|
---|
197 | /** The length of the parent UTF-16 path (in wchar_t's).
|
---|
198 | * @note This is valuable when constructing an absolute path to this node by
|
---|
199 | * means of the parent pointer (no need for recursion). */
|
---|
200 | KU16 cwcParent;
|
---|
201 | /** The UTF-16 object name. (Allocated after the structure.) */
|
---|
202 | const wchar_t *pwszName;
|
---|
203 | #endif
|
---|
204 |
|
---|
205 | #ifdef KFSCACHE_CFG_SHORT_NAMES
|
---|
206 | /** The short object name. (Allocated after the structure, could be same
|
---|
207 | * as pszName.) */
|
---|
208 | const char *pszShortName;
|
---|
209 | /** The length of pszShortName. */
|
---|
210 | KU16 cchShortName;
|
---|
211 | /** The length of the short parent path (up to where pszShortName starts). */
|
---|
212 | KU16 cchShortParent;
|
---|
213 | # ifdef KFSCACHE_CFG_UTF16
|
---|
214 | /** The length of pwszShortName (in wchar_t's). */
|
---|
215 | KU16 cwcShortName;
|
---|
216 | /** The length of the short parent UTF-16 path (in wchar_t's). */
|
---|
217 | KU16 cwcShortParent;
|
---|
218 | /** The UTF-16 short object name. (Allocated after the structure, possibly
|
---|
219 | * same as pwszName.) */
|
---|
220 | const wchar_t *pwszShortName;
|
---|
221 | # endif
|
---|
222 | #endif
|
---|
223 |
|
---|
224 | /** Allocation for handling name length increases. */
|
---|
225 | PKFSOBJNAMEALLOC pNameAlloc;
|
---|
226 |
|
---|
227 | /** Pointer to the first user data item */
|
---|
228 | PKFSUSERDATA pUserDataHead;
|
---|
229 |
|
---|
230 | /** Stats - only valid when fHaveStats is set. */
|
---|
231 | BirdStat_T Stats;
|
---|
232 | } KFSOBJ;
|
---|
233 |
|
---|
234 | /** The magic for a KFSOBJ structure (Thelonious Sphere Monk). */
|
---|
235 | #define KFSOBJ_MAGIC KU32_C(0x19171010)
|
---|
236 |
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Directory node in the cache.
|
---|
240 | */
|
---|
241 | typedef struct KFSDIR
|
---|
242 | {
|
---|
243 | /** The core object information. */
|
---|
244 | KFSOBJ Obj;
|
---|
245 |
|
---|
246 | /** Child objects. */
|
---|
247 | PKFSOBJ *papChildren;
|
---|
248 | /** The number of child objects. */
|
---|
249 | KU32 cChildren;
|
---|
250 | /** The allocated size of papChildren. */
|
---|
251 | KU32 cChildrenAllocated;
|
---|
252 |
|
---|
253 | /** Pointer to the child hash table. */
|
---|
254 | PKFSOBJ *papHashTab;
|
---|
255 | /** The mask shift of the hash table.
|
---|
256 | * Hash table size is a power of two, this is the size minus one.
|
---|
257 | *
|
---|
258 | * @remarks The hash table is optional and populated by lookup hits. The
|
---|
259 | * assumption being that a lookup is repeated and will choose a good
|
---|
260 | * name to hash on. We've got up to 4 different hashes, so this
|
---|
261 | * was the easy way out. */
|
---|
262 | KU32 fHashTabMask;
|
---|
263 |
|
---|
264 | /** Handle to the directory (we generally keep it open). */
|
---|
265 | #ifndef DECLARE_HANDLE
|
---|
266 | KUPTR hDir;
|
---|
267 | #else
|
---|
268 | HANDLE hDir;
|
---|
269 | #endif
|
---|
270 | /** The device number we queried/inherited when opening it. */
|
---|
271 | KU64 uDevNo;
|
---|
272 |
|
---|
273 | /** The last write time sampled the last time the directory was refreshed.
|
---|
274 | * @remarks May differ from st_mtim because it will be updated when the
|
---|
275 | * parent directory is refreshed. */
|
---|
276 | KI64 iLastWrite;
|
---|
277 | /** The time that iLastWrite was read. */
|
---|
278 | KI64 iLastPopulated;
|
---|
279 |
|
---|
280 | /** Set if populated. */
|
---|
281 | KBOOL fPopulated;
|
---|
282 | /** Set if it needs re-populated. */
|
---|
283 | KBOOL fNeedRePopulating;
|
---|
284 | } KFSDIR;
|
---|
285 |
|
---|
286 |
|
---|
287 | /**
|
---|
288 | * Lookup errors.
|
---|
289 | */
|
---|
290 | typedef enum KFSLOOKUPERROR
|
---|
291 | {
|
---|
292 | /** Lookup was a success. */
|
---|
293 | KFSLOOKUPERROR_SUCCESS = 0,
|
---|
294 | /** A path component was not found. */
|
---|
295 | KFSLOOKUPERROR_PATH_COMP_NOT_FOUND,
|
---|
296 | /** A path component is not a directory. */
|
---|
297 | KFSLOOKUPERROR_PATH_COMP_NOT_DIR,
|
---|
298 | /** The final path entry is not a directory (trailing slash). */
|
---|
299 | KFSLOOKUPERROR_NOT_DIR,
|
---|
300 | /** Not found. */
|
---|
301 | KFSLOOKUPERROR_NOT_FOUND,
|
---|
302 | /** The path is too long. */
|
---|
303 | KFSLOOKUPERROR_PATH_TOO_LONG,
|
---|
304 | /** The path is too short. */
|
---|
305 | KFSLOOKUPERROR_PATH_TOO_SHORT,
|
---|
306 | /** Unsupported path type. */
|
---|
307 | KFSLOOKUPERROR_UNSUPPORTED,
|
---|
308 | /** We're out of memory. */
|
---|
309 | KFSLOOKUPERROR_OUT_OF_MEMORY,
|
---|
310 |
|
---|
311 | /** Error opening directory. */
|
---|
312 | KFSLOOKUPERROR_DIR_OPEN_ERROR,
|
---|
313 | /** Error reading directory. */
|
---|
314 | KFSLOOKUPERROR_DIR_READ_ERROR,
|
---|
315 | /** UTF-16 to ANSI conversion error. */
|
---|
316 | KFSLOOKUPERROR_ANSI_CONVERSION_ERROR,
|
---|
317 | /** ANSI to UTF-16 conversion error. */
|
---|
318 | KFSLOOKUPERROR_UTF16_CONVERSION_ERROR,
|
---|
319 | /** Internal error. */
|
---|
320 | KFSLOOKUPERROR_INTERNAL_ERROR
|
---|
321 | } KFSLOOKUPERROR;
|
---|
322 |
|
---|
323 |
|
---|
324 | /** Pointer to an ANSI path hash table entry. */
|
---|
325 | typedef struct KFSHASHA *PKFSHASHA;
|
---|
326 | /**
|
---|
327 | * ANSI file system path hash table entry.
|
---|
328 | * The path hash table allows us to skip parsing and walking a path.
|
---|
329 | */
|
---|
330 | typedef struct KFSHASHA
|
---|
331 | {
|
---|
332 | /** Next entry with the same hash table slot. */
|
---|
333 | PKFSHASHA pNext;
|
---|
334 | /** Path hash value. */
|
---|
335 | KU32 uHashPath;
|
---|
336 | /** The path length. */
|
---|
337 | KU16 cchPath;
|
---|
338 | /** Set if aboslute path. */
|
---|
339 | KBOOL fAbsolute;
|
---|
340 | /** Index into KFSCACHE:auGenerationsMissing when pFsObj is NULL. */
|
---|
341 | KU8 idxMissingGen;
|
---|
342 | /** The cache generation ID. */
|
---|
343 | KU32 uCacheGen;
|
---|
344 | /** The lookup error (when pFsObj is NULL). */
|
---|
345 | KFSLOOKUPERROR enmError;
|
---|
346 | /** The path. (Allocated after the structure.) */
|
---|
347 | const char *pszPath;
|
---|
348 | /** Pointer to the matching FS object.
|
---|
349 | * This is NULL for negative path entries? */
|
---|
350 | PKFSOBJ pFsObj;
|
---|
351 | } KFSHASHA;
|
---|
352 |
|
---|
353 |
|
---|
354 | #ifdef KFSCACHE_CFG_UTF16
|
---|
355 | /** Pointer to an UTF-16 path hash table entry. */
|
---|
356 | typedef struct KFSHASHW *PKFSHASHW;
|
---|
357 | /**
|
---|
358 | * UTF-16 file system path hash table entry. The path hash table allows us
|
---|
359 | * to skip parsing and walking a path.
|
---|
360 | */
|
---|
361 | typedef struct KFSHASHW
|
---|
362 | {
|
---|
363 | /** Next entry with the same hash table slot. */
|
---|
364 | PKFSHASHW pNext;
|
---|
365 | /** Path hash value. */
|
---|
366 | KU32 uHashPath;
|
---|
367 | /** The path length (in wchar_t units). */
|
---|
368 | KU16 cwcPath;
|
---|
369 | /** Set if aboslute path. */
|
---|
370 | KBOOL fAbsolute;
|
---|
371 | /** Index into KFSCACHE:auGenerationsMissing when pFsObj is NULL. */
|
---|
372 | KU8 idxMissingGen;
|
---|
373 | /** The cache generation ID. */
|
---|
374 | KU32 uCacheGen;
|
---|
375 | /** The lookup error (when pFsObj is NULL). */
|
---|
376 | KFSLOOKUPERROR enmError;
|
---|
377 | /** The path. (Allocated after the structure.) */
|
---|
378 | const wchar_t *pwszPath;
|
---|
379 | /** Pointer to the matching FS object.
|
---|
380 | * This is NULL for negative path entries? */
|
---|
381 | PKFSOBJ pFsObj;
|
---|
382 | } KFSHASHW;
|
---|
383 | #endif
|
---|
384 |
|
---|
385 |
|
---|
386 | /** @def KFSCACHE_LOCK
|
---|
387 | * Locks the cache exclusively. */
|
---|
388 | /** @def KFSCACHE_UNLOCK
|
---|
389 | * Counterpart to KFSCACHE_LOCK. */
|
---|
390 | /** @def KFSCACHE_OBJUSERDATA_LOCK
|
---|
391 | * Locks the user data list of an object exclusively. */
|
---|
392 | /** @def KFSCACHE_OBJUSERDATA_UNLOCK
|
---|
393 | * Counterpart to KFSCACHE_OBJUSERDATA_LOCK. */
|
---|
394 | #ifdef KFSCACHE_CFG_LOCKING
|
---|
395 | # define KFSCACHE_LOCK(a_pCache) EnterCriticalSection(&(a_pCache)->u.CritSect)
|
---|
396 | # define KFSCACHE_UNLOCK(a_pCache) LeaveCriticalSection(&(a_pCache)->u.CritSect)
|
---|
397 | # define KFSCACHE_OBJUSERDATA_LOCK(a_pCache, a_pObj) do { \
|
---|
398 | KU8 idxUserDataLock = (a_pObj)->idxUserDataLock; \
|
---|
399 | if (idxUserDataLock != KU8_MAX) \
|
---|
400 | { /* likely */ } \
|
---|
401 | else \
|
---|
402 | idxUserDataLock = kFsCacheObjGetUserDataLockIndex(a_pCache, a_pObj); \
|
---|
403 | idxUserDataLock &= (KU8)(K_ELEMENTS((a_pCache)->auUserDataLocks) - 1); \
|
---|
404 | EnterCriticalSection(&(a_pCache)->auUserDataLocks[idxUserDataLock].CritSect); \
|
---|
405 | } while (0)
|
---|
406 | # define KFSCACHE_OBJUSERDATA_UNLOCK(a_pCache, a_pObj) \
|
---|
407 | LeaveCriticalSection(&(a_pCache)->auUserDataLocks[(a_pObj)->idxUserDataLock & (K_ELEMENTS((a_pCache)->auUserDataLocks) - 1)].CritSect)
|
---|
408 | #else
|
---|
409 | # define KFSCACHE_LOCK(a_pCache) do { } while (0)
|
---|
410 | # define KFSCACHE_UNLOCK(a_pCache) do { } while (0)
|
---|
411 | # define KFSCACHE_OBJUSERDATA_LOCK(a_pCache, a_pObj) do { } while (0)
|
---|
412 | # define KFSCACHE_OBJUSERDATA_UNLOCK(a_pCache, a_pObj) do { } while (0)
|
---|
413 | #endif
|
---|
414 |
|
---|
415 |
|
---|
416 | /** @name KFSCACHE_F_XXX
|
---|
417 | * @{ */
|
---|
418 | /** Whether to cache missing directory entries (KFSOBJ_TYPE_MISSING). */
|
---|
419 | #define KFSCACHE_F_MISSING_OBJECTS KU32_C(0x00000001)
|
---|
420 | /** Whether to cache missing paths. */
|
---|
421 | #define KFSCACHE_F_MISSING_PATHS KU32_C(0x00000002)
|
---|
422 | /** @} */
|
---|
423 |
|
---|
424 |
|
---|
425 | /**
|
---|
426 | * Directory cache instance.
|
---|
427 | */
|
---|
428 | typedef struct KFSCACHE
|
---|
429 | {
|
---|
430 | /** Magic value (KFSCACHE_MAGIC). */
|
---|
431 | KU32 u32Magic;
|
---|
432 | /** Cache flags. */
|
---|
433 | KU32 fFlags;
|
---|
434 |
|
---|
435 | /** The default and custom cache generations for stuff that exists, indexed by
|
---|
436 | * KFSOBJ_F_USE_CUSTOM_GEN.
|
---|
437 | *
|
---|
438 | * The custom generation can be used to invalidate parts of the file system that
|
---|
439 | * are known to be volatile without triggering refreshing of the more static
|
---|
440 | * parts. Like the 'out' directory in a kBuild setup or a 'TEMP' directory are
|
---|
441 | * expected to change and you need to invalidate the caching of these frequently
|
---|
442 | * to stay on top of things. Whereas the sources, headers, compilers, sdk,
|
---|
443 | * ddks, windows directory and such generally doesn't change all that often.
|
---|
444 | */
|
---|
445 | KU32 auGenerations[2];
|
---|
446 | /** The current cache generation for missing objects, negative results, ++.
|
---|
447 | * This comes with a custom variant too. Indexed by KFSOBJ_F_USE_CUSTOM_GEN. */
|
---|
448 | KU32 auGenerationsMissing[2];
|
---|
449 |
|
---|
450 | /** Number of cache objects. */
|
---|
451 | KSIZE cObjects;
|
---|
452 | /** Memory occupied by the cache object structures. */
|
---|
453 | KSIZE cbObjects;
|
---|
454 | /** Number of lookups. */
|
---|
455 | KSIZE cLookups;
|
---|
456 | /** Number of hits in the path hash tables. */
|
---|
457 | KSIZE cPathHashHits;
|
---|
458 | /** Number of hits walking the file system hierarchy. */
|
---|
459 | KSIZE cWalkHits;
|
---|
460 | /** Number of child searches. */
|
---|
461 | KSIZE cChildSearches;
|
---|
462 | /** Number of cChildLookups resolved thru hash table hits. */
|
---|
463 | KSIZE cChildHashHits;
|
---|
464 | /** The number of child hash tables. */
|
---|
465 | KSIZE cChildHashTabs;
|
---|
466 | /** The sum of all child hash table sizes. */
|
---|
467 | KSIZE cChildHashEntriesTotal;
|
---|
468 | /** Number of children inserted into the hash tables. */
|
---|
469 | KSIZE cChildHashed;
|
---|
470 | /** Number of collisions in the child hash tables. */
|
---|
471 | KSIZE cChildHashCollisions;
|
---|
472 | /** Number times a object name changed. */
|
---|
473 | KSIZE cNameChanges;
|
---|
474 | /** Number times a object name grew and needed KFSOBJNAMEALLOC.
|
---|
475 | * (Subset of cNameChanges) */
|
---|
476 | KSIZE cNameGrowths;
|
---|
477 |
|
---|
478 | /** The root directory. */
|
---|
479 | KFSDIR RootDir;
|
---|
480 |
|
---|
481 | #ifdef KFSCACHE_CFG_LOCKING
|
---|
482 | union
|
---|
483 | {
|
---|
484 | # ifdef _WINBASE_
|
---|
485 | CRITICAL_SECTION CritSect;
|
---|
486 | # endif
|
---|
487 | KU64 abPadding[2 * 4 + 4 * sizeof(void *)];
|
---|
488 | }
|
---|
489 | /** Critical section protecting the cache. */
|
---|
490 | u,
|
---|
491 | /** Critical section protecting the pUserDataHead of objects.
|
---|
492 | * @note Array size must be a power of two. */
|
---|
493 | auUserDataLocks[8];
|
---|
494 | /** The next auUserData index. */
|
---|
495 | KU8 idxUserDataNext;
|
---|
496 | #endif
|
---|
497 |
|
---|
498 | /** File system hash table for ANSI filename strings. */
|
---|
499 | PKFSHASHA apAnsiPaths[KFSCACHE_CFG_PATH_HASH_TAB_SIZE];
|
---|
500 | /** Number of paths in the apAnsiPaths hash table. */
|
---|
501 | KSIZE cAnsiPaths;
|
---|
502 | /** Number of collisions in the apAnsiPaths hash table. */
|
---|
503 | KSIZE cAnsiPathCollisions;
|
---|
504 | /** Amount of memory used by the path entries. */
|
---|
505 | KSIZE cbAnsiPaths;
|
---|
506 |
|
---|
507 | #ifdef KFSCACHE_CFG_UTF16
|
---|
508 | /** Number of paths in the apUtf16Paths hash table. */
|
---|
509 | KSIZE cUtf16Paths;
|
---|
510 | /** Number of collisions in the apUtf16Paths hash table. */
|
---|
511 | KSIZE cUtf16PathCollisions;
|
---|
512 | /** Amount of memory used by the UTF-16 path entries. */
|
---|
513 | KSIZE cbUtf16Paths;
|
---|
514 | /** File system hash table for UTF-16 filename strings. */
|
---|
515 | PKFSHASHW apUtf16Paths[KFSCACHE_CFG_PATH_HASH_TAB_SIZE];
|
---|
516 | #endif
|
---|
517 | } KFSCACHE;
|
---|
518 |
|
---|
519 | /** Magic value for KFSCACHE::u32Magic (Jon Batiste). */
|
---|
520 | #define KFSCACHE_MAGIC KU32_C(0x19861111)
|
---|
521 |
|
---|
522 |
|
---|
523 | /** @def KW_LOG
|
---|
524 | * Generic logging.
|
---|
525 | * @param a Argument list for kFsCacheDbgPrintf */
|
---|
526 | #if 1 /*def NDEBUG - enable when needed! */
|
---|
527 | # define KFSCACHE_LOG(a) do { } while (0)
|
---|
528 | #else
|
---|
529 | # define KFSCACHE_LOG(a) kFsCacheDbgPrintf a
|
---|
530 | void kFsCacheDbgPrintfV(const char *pszFormat, va_list va);
|
---|
531 | void kFsCacheDbgPrintf(const char *pszFormat, ...);
|
---|
532 | #endif
|
---|
533 |
|
---|
534 |
|
---|
535 | KBOOL kFsCacheDirEnsurePopuplated(PKFSCACHE pCache, PKFSDIR pDir, KFSLOOKUPERROR *penmError);
|
---|
536 | KBOOL kFsCacheDirAddChild(PKFSCACHE pCache, PKFSDIR pParent, PKFSOBJ pChild, KFSLOOKUPERROR *penmError);
|
---|
537 | PKFSOBJ kFsCacheCreateObject(PKFSCACHE pCache, PKFSDIR pParent,
|
---|
538 | char const *pszName, KU16 cchName, wchar_t const *pwszName, KU16 cwcName,
|
---|
539 | #ifdef KFSCACHE_CFG_SHORT_NAMES
|
---|
540 | char const *pszShortName, KU16 cchShortName, wchar_t const *pwszShortName, KU16 cwcShortName,
|
---|
541 | #endif
|
---|
542 | KU8 bObjType, KFSLOOKUPERROR *penmError);
|
---|
543 | PKFSOBJ kFsCacheCreateObjectW(PKFSCACHE pCache, PKFSDIR pParent, wchar_t const *pwszName, KU32 cwcName,
|
---|
544 | #ifdef KFSCACHE_CFG_SHORT_NAMES
|
---|
545 | wchar_t const *pwszShortName, KU32 cwcShortName,
|
---|
546 | #endif
|
---|
547 | KU8 bObjType, KFSLOOKUPERROR *penmError);
|
---|
548 | PKFSOBJ kFsCacheLookupA(PKFSCACHE pCache, const char *pszPath, KFSLOOKUPERROR *penmError);
|
---|
549 | PKFSOBJ kFsCacheLookupW(PKFSCACHE pCache, const wchar_t *pwszPath, KFSLOOKUPERROR *penmError);
|
---|
550 | PKFSOBJ kFsCacheLookupRelativeToDirA(PKFSCACHE pCache, PKFSDIR pParent, const char *pszPath, KU32 cchPath, KU32 fFlags,
|
---|
551 | KFSLOOKUPERROR *penmError, PKFSOBJ *ppLastAncestor);
|
---|
552 | PKFSOBJ kFsCacheLookupRelativeToDirW(PKFSCACHE pCache, PKFSDIR pParent, const wchar_t *pwszPath, KU32 cwcPath, KU32 fFlags,
|
---|
553 | KFSLOOKUPERROR *penmError, PKFSOBJ *ppLastAncestor);
|
---|
554 | PKFSOBJ kFsCacheLookupWithLengthA(PKFSCACHE pCache, const char *pchPath, KSIZE cchPath, KFSLOOKUPERROR *penmError);
|
---|
555 | PKFSOBJ kFsCacheLookupWithLengthW(PKFSCACHE pCache, const wchar_t *pwcPath, KSIZE cwcPath, KFSLOOKUPERROR *penmError);
|
---|
556 | PKFSOBJ kFsCacheLookupNoMissingA(PKFSCACHE pCache, const char *pszPath, KFSLOOKUPERROR *penmError);
|
---|
557 | PKFSOBJ kFsCacheLookupNoMissingW(PKFSCACHE pCache, const wchar_t *pwszPath, KFSLOOKUPERROR *penmError);
|
---|
558 |
|
---|
559 | /** @name KFSCACHE_LOOKUP_F_XXX - lookup flags
|
---|
560 | * @{ */
|
---|
561 | /** No inserting new cache entries.
|
---|
562 | * This effectively prevent directories from being repopulated too. */
|
---|
563 | #define KFSCACHE_LOOKUP_F_NO_INSERT KU32_C(1)
|
---|
564 | /** No refreshing cache entries. */
|
---|
565 | #define KFSCACHE_LOOKUP_F_NO_REFRESH KU32_C(2)
|
---|
566 | /** @} */
|
---|
567 |
|
---|
568 | KU32 kFsCacheObjRelease(PKFSCACHE pCache, PKFSOBJ pObj);
|
---|
569 | KU32 kFsCacheObjReleaseTagged(PKFSCACHE pCache, PKFSOBJ pObj, const char *pszWhere);
|
---|
570 | #ifndef NDEBUG /* enable to debug object release. */
|
---|
571 | # define kFsCacheObjRelease(a_pCache, a_pObj) kFsCacheObjReleaseTagged(a_pCache, a_pObj, __FUNCTION__)
|
---|
572 | #endif
|
---|
573 | KU32 kFsCacheObjRetain(PKFSOBJ pObj);
|
---|
574 | PKFSUSERDATA kFsCacheObjAddUserData(PKFSCACHE pCache, PKFSOBJ pObj, KUPTR uKey, KSIZE cbUserData);
|
---|
575 | PKFSUSERDATA kFsCacheObjGetUserData(PKFSCACHE pCache, PKFSOBJ pObj, KUPTR uKey);
|
---|
576 | KU8 kFsCacheObjGetUserDataLockIndex(PKFSCACHE pCache, PKFSOBJ pObj);
|
---|
577 | KBOOL kFsCacheObjGetFullPathA(PKFSOBJ pObj, char *pszPath, KSIZE cbPath, char chSlash);
|
---|
578 | KBOOL kFsCacheObjGetFullPathW(PKFSOBJ pObj, wchar_t *pwszPath, KSIZE cwcPath, wchar_t wcSlash);
|
---|
579 | KBOOL kFsCacheObjGetFullShortPathA(PKFSOBJ pObj, char *pszPath, KSIZE cbPath, char chSlash);
|
---|
580 | KBOOL kFsCacheObjGetFullShortPathW(PKFSOBJ pObj, wchar_t *pwszPath, KSIZE cwcPath, wchar_t wcSlash);
|
---|
581 |
|
---|
582 | KBOOL kFsCacheFileSimpleOpenReadClose(PKFSCACHE pCache, PKFSOBJ pFileObj, KU64 offStart, void *pvBuf, KSIZE cbToRead);
|
---|
583 |
|
---|
584 | PKFSCACHE kFsCacheCreate(KU32 fFlags);
|
---|
585 | void kFsCacheDestroy(PKFSCACHE);
|
---|
586 | void kFsCacheInvalidateMissing(PKFSCACHE pCache);
|
---|
587 | void kFsCacheInvalidateAll(PKFSCACHE pCache);
|
---|
588 | void kFsCacheInvalidateAllAndCloseDirs(PKFSCACHE pCache, KBOOL fIncludingRoot);
|
---|
589 | void kFsCacheInvalidateCustomMissing(PKFSCACHE pCache);
|
---|
590 | void kFsCacheInvalidateCustomBoth(PKFSCACHE pCache);
|
---|
591 | KBOOL kFsCacheSetupCustomRevisionForTree(PKFSCACHE pCache, PKFSOBJ pRoot);
|
---|
592 | KBOOL kFsCacheInvalidateDeletedDirectoryA(PKFSCACHE pCache, const char *pszDir);
|
---|
593 |
|
---|
594 | #endif
|
---|