Skip to content

Commit 7bd0779

Browse files
committed
Merge pull request #994 from jeffhostetler/jeffhostetler/fscache_nfd
fscache: add not-found directory cache to fscache
2 parents 95843ae + 7198fe2 commit 7bd0779

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

compat/win32/fscache.c

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ static int initialized;
77
static volatile long enabled;
88
static struct hashmap map;
99
static CRITICAL_SECTION mutex;
10+
static struct trace_key trace_fscache = TRACE_KEY_INIT(FSCACHE);
1011

1112
/*
1213
* An entry in the file system cache. Used for both entire directory listings
@@ -164,7 +165,8 @@ static struct fsentry *fseentry_create_entry(struct fsentry *list,
164165
* Dir should not contain trailing '/'. Use an empty string for the current
165166
* directory (not "."!).
166167
*/
167-
static struct fsentry *fsentry_create_list(const struct fsentry *dir)
168+
static struct fsentry *fsentry_create_list(const struct fsentry *dir,
169+
int *dir_not_found)
168170
{
169171
wchar_t pattern[MAX_PATH + 2]; /* + 2 for '/' '*' */
170172
WIN32_FIND_DATAW fdata;
@@ -173,6 +175,8 @@ static struct fsentry *fsentry_create_list(const struct fsentry *dir)
173175
struct fsentry *list, **phead;
174176
DWORD err;
175177

178+
*dir_not_found = 0;
179+
176180
/* convert name to UTF-16 and check length < MAX_PATH */
177181
if ((wlen = xutftowcsn(pattern, dir->name, MAX_PATH, dir->len)) < 0) {
178182
if (errno == ERANGE)
@@ -190,12 +194,16 @@ static struct fsentry *fsentry_create_list(const struct fsentry *dir)
190194
h = FindFirstFileW(pattern, &fdata);
191195
if (h == INVALID_HANDLE_VALUE) {
192196
err = GetLastError();
197+
*dir_not_found = 1; /* or empty directory */
193198
errno = (err == ERROR_DIRECTORY) ? ENOTDIR : err_win_to_posix(err);
199+
trace_printf_key(&trace_fscache, "fscache: error(%d) '%.*s'\n",
200+
errno, dir->len, dir->name);
194201
return NULL;
195202
}
196203

197204
/* allocate object to hold directory listing */
198205
list = fsentry_alloc(NULL, dir->name, dir->len);
206+
list->st_mode = S_IFDIR;
199207

200208
/* walk directory and build linked list of fsentry structures */
201209
phead = &list->next;
@@ -280,12 +288,16 @@ static struct fsentry *fscache_get_wait(struct fsentry *key)
280288
static struct fsentry *fscache_get(struct fsentry *key)
281289
{
282290
struct fsentry *fse, *future, *waiter;
291+
int dir_not_found;
283292

284293
EnterCriticalSection(&mutex);
285294
/* check if entry is in cache */
286295
fse = fscache_get_wait(key);
287296
if (fse) {
288-
fsentry_addref(fse);
297+
if (fse->st_mode)
298+
fsentry_addref(fse);
299+
else
300+
fse = NULL; /* non-existing directory */
289301
LeaveCriticalSection(&mutex);
290302
return fse;
291303
}
@@ -294,7 +306,10 @@ static struct fsentry *fscache_get(struct fsentry *key)
294306
fse = fscache_get_wait(key->list);
295307
if (fse) {
296308
LeaveCriticalSection(&mutex);
297-
/* dir entry without file entry -> file doesn't exist */
309+
/*
310+
* dir entry without file entry, or dir does not
311+
* exist -> file doesn't exist
312+
*/
298313
errno = ENOENT;
299314
return NULL;
300315
}
@@ -308,7 +323,7 @@ static struct fsentry *fscache_get(struct fsentry *key)
308323

309324
/* create the directory listing (outside mutex!) */
310325
LeaveCriticalSection(&mutex);
311-
fse = fsentry_create_list(future);
326+
fse = fsentry_create_list(future, &dir_not_found);
312327
EnterCriticalSection(&mutex);
313328

314329
/* remove future entry and signal waiting threads */
@@ -322,6 +337,17 @@ static struct fsentry *fscache_get(struct fsentry *key)
322337

323338
/* leave on error (errno set by fsentry_create_list) */
324339
if (!fse) {
340+
if (dir_not_found && key->list) {
341+
/*
342+
* Record that the directory does not exist (or is
343+
* empty, which for all practical matters is the same
344+
* thing as far as fscache is concerned).
345+
*/
346+
fse = fsentry_alloc(key->list->list,
347+
key->list->name, key->list->len);
348+
fse->st_mode = 0;
349+
hashmap_add(&map, fse);
350+
}
325351
LeaveCriticalSection(&mutex);
326352
return NULL;
327353
}
@@ -333,6 +359,9 @@ static struct fsentry *fscache_get(struct fsentry *key)
333359
if (key->list)
334360
fse = hashmap_get(&map, key, NULL);
335361

362+
if (fse && !fse->st_mode)
363+
fse = NULL; /* non-existing directory */
364+
336365
/* return entry or ENOENT */
337366
if (fse)
338367
fsentry_addref(fse);
@@ -376,6 +405,7 @@ int fscache_enable(int enable)
376405
fscache_clear();
377406
LeaveCriticalSection(&mutex);
378407
}
408+
trace_printf_key(&trace_fscache, "fscache: enable(%d)\n", enable);
379409
return result;
380410
}
381411

t/t1090-sparse-checkout-scope.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,24 @@ test_expect_success 'in partial clone, sparse checkout only fetches needed blobs
9696
test_cmp expect actual
9797
'
9898

99+
test_expect_success MINGW 'no unnecessary opendir() with fscache' '
100+
git clone . fscache-test &&
101+
(
102+
cd fscache-test &&
103+
git config core.fscache 1 &&
104+
echo "/excluded/*" >.git/info/sparse-checkout &&
105+
for f in $(test_seq 10)
106+
do
107+
sha1=$(echo $f | git hash-object -w --stdin) &&
108+
git update-index --add \
109+
--cacheinfo 100644,$sha1,excluded/$f || break
110+
done &&
111+
test_tick &&
112+
git commit -m excluded &&
113+
GIT_TRACE_FSCACHE=1 git status >out 2>err &&
114+
grep excluded err >grep.out &&
115+
test_line_count = 1 grep.out
116+
)
117+
'
118+
99119
test_done

0 commit comments

Comments
 (0)