Skip to content

Commit 7a511c4

Browse files
committed
Merge branch 'fscache'
2 parents d02e784 + 548bd40 commit 7a511c4

File tree

12 files changed

+648
-70
lines changed

12 files changed

+648
-70
lines changed

Documentation/config/core.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,12 @@ relatively high IO latencies. When enabled, Git will do the
685685
index comparison to the filesystem data in parallel, allowing
686686
overlapping IO's. Defaults to true.
687687

688+
core.fscache::
689+
Enable additional caching of file system data for some operations.
690+
+
691+
Git for Windows uses this to bulk-read and cache lstat data of entire
692+
directories (instead of doing lstat file by file).
693+
688694
core.unsetenvvars::
689695
Windows-only: comma-separated list of environment variables'
690696
names that need to be unset before spawning any other process.

builtin/commit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,7 @@ struct repository *repo UNUSED)
15831583
PATHSPEC_PREFER_FULL,
15841584
prefix, argv);
15851585

1586+
enable_fscache(1);
15861587
if (status_format != STATUS_FORMAT_PORCELAIN &&
15871588
status_format != STATUS_FORMAT_PORCELAIN_V2)
15881589
progress_flag = REFRESH_PROGRESS;

compat/mingw.c

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ enum hide_dotfiles_type {
246246
static int core_restrict_inherited_handles = -1;
247247
static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
248248
static char *unset_environment_variables;
249+
int core_fscache;
249250

250251
int mingw_core_config(const char *var, const char *value,
251252
const struct config_context *ctx UNUSED,
@@ -259,6 +260,11 @@ int mingw_core_config(const char *var, const char *value,
259260
return 0;
260261
}
261262

263+
if (!strcmp(var, "core.fscache")) {
264+
core_fscache = git_config_bool(var, value);
265+
return 0;
266+
}
267+
262268
if (!strcmp(var, "core.unsetenvvars")) {
263269
if (!value)
264270
return config_error_nonbool(var);
@@ -857,24 +863,6 @@ int mingw_chmod(const char *filename, int mode)
857863
return _wchmod(wfilename, mode);
858864
}
859865

860-
/*
861-
* The unit of FILETIME is 100-nanoseconds since January 1, 1601, UTC.
862-
* Returns the 100-nanoseconds ("hekto nanoseconds") since the epoch.
863-
*/
864-
static inline long long filetime_to_hnsec(const FILETIME *ft)
865-
{
866-
long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime;
867-
/* Windows to Unix Epoch conversion */
868-
return winTime - 116444736000000000LL;
869-
}
870-
871-
static inline void filetime_to_timespec(const FILETIME *ft, struct timespec *ts)
872-
{
873-
long long hnsec = filetime_to_hnsec(ft);
874-
ts->tv_sec = (time_t)(hnsec / 10000000);
875-
ts->tv_nsec = (hnsec % 10000000) * 100;
876-
}
877-
878866
/**
879867
* Verifies that safe_create_leading_directories() would succeed.
880868
*/
@@ -1014,6 +1002,8 @@ static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
10141002
return do_lstat(follow, alt_name, buf);
10151003
}
10161004

1005+
int (*lstat)(const char *file_name, struct stat *buf) = mingw_lstat;
1006+
10171007
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
10181008
{
10191009
BY_HANDLE_FILE_INFORMATION fdata;

compat/mingw.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ typedef _sigset_t sigset_t;
1111
#undef _POSIX_THREAD_SAFE_FUNCTIONS
1212
#endif
1313

14+
extern int core_fscache;
15+
1416
struct config_context;
1517
int mingw_core_config(const char *var, const char *value,
1618
const struct config_context *ctx, void *cb);
@@ -356,6 +358,17 @@ static inline int getrlimit(int resource, struct rlimit *rlp)
356358
return 0;
357359
}
358360

361+
/*
362+
* The unit of FILETIME is 100-nanoseconds since January 1, 1601, UTC.
363+
* Returns the 100-nanoseconds ("hekto nanoseconds") since the epoch.
364+
*/
365+
static inline long long filetime_to_hnsec(const FILETIME *ft)
366+
{
367+
long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime;
368+
/* Windows to Unix Epoch conversion */
369+
return winTime - 116444736000000000LL;
370+
}
371+
359372
/*
360373
* Use mingw specific stat()/lstat()/fstat() implementations on Windows,
361374
* including our own struct stat with 64 bit st_size and nanosecond-precision
@@ -372,6 +385,13 @@ struct timespec {
372385
#endif
373386
#endif
374387

388+
static inline void filetime_to_timespec(const FILETIME *ft, struct timespec *ts)
389+
{
390+
long long hnsec = filetime_to_hnsec(ft);
391+
ts->tv_sec = (time_t)(hnsec / 10000000);
392+
ts->tv_nsec = (hnsec % 10000000) * 100;
393+
}
394+
375395
struct mingw_stat {
376396
_dev_t st_dev;
377397
_ino_t st_ino;
@@ -404,7 +424,7 @@ int mingw_fstat(int fd, struct stat *buf);
404424
#ifdef lstat
405425
#undef lstat
406426
#endif
407-
#define lstat mingw_lstat
427+
extern int (*lstat)(const char *file_name, struct stat *buf);
408428

409429

410430
int mingw_utime(const char *file_name, const struct utimbuf *times);

compat/win32/dirent.c

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
#include "../../git-compat-util.h"
22

3-
struct DIR {
4-
struct dirent dd_dir; /* includes d_type */
3+
#pragma GCC diagnostic push
4+
#pragma GCC diagnostic ignored "-Wpedantic"
5+
typedef struct dirent_DIR {
6+
struct DIR base_dir; /* extend base struct DIR */
57
HANDLE dd_handle; /* FindFirstFile handle */
68
int dd_stat; /* 0-based index */
7-
};
9+
struct dirent dd_dir; /* includes d_type */
10+
} dirent_DIR;
11+
#pragma GCC diagnostic pop
12+
13+
DIR *(*opendir)(const char *dirname) = dirent_opendir;
814

915
static inline void finddata2dirent(struct dirent *ent, WIN32_FIND_DATAW *fdata)
1016
{
11-
/* convert UTF-16 name to UTF-8 */
12-
xwcstoutf(ent->d_name, fdata->cFileName, sizeof(ent->d_name));
17+
/* convert UTF-16 name to UTF-8 (d_name points to dirent_DIR.dd_name) */
18+
xwcstoutf(ent->d_name, fdata->cFileName, MAX_PATH * 3);
1319

1420
/* Set file type, based on WIN32_FIND_DATA */
1521
if (fdata->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
@@ -18,41 +24,7 @@ static inline void finddata2dirent(struct dirent *ent, WIN32_FIND_DATAW *fdata)
1824
ent->d_type = DT_REG;
1925
}
2026

21-
DIR *opendir(const char *name)
22-
{
23-
wchar_t pattern[MAX_PATH + 2]; /* + 2 for '/' '*' */
24-
WIN32_FIND_DATAW fdata;
25-
HANDLE h;
26-
int len;
27-
DIR *dir;
28-
29-
/* convert name to UTF-16 and check length < MAX_PATH */
30-
if ((len = xutftowcs_path(pattern, name)) < 0)
31-
return NULL;
32-
33-
/* append optional '/' and wildcard '*' */
34-
if (len && !is_dir_sep(pattern[len - 1]))
35-
pattern[len++] = '/';
36-
pattern[len++] = '*';
37-
pattern[len] = 0;
38-
39-
/* open find handle */
40-
h = FindFirstFileW(pattern, &fdata);
41-
if (h == INVALID_HANDLE_VALUE) {
42-
DWORD err = GetLastError();
43-
errno = (err == ERROR_DIRECTORY) ? ENOTDIR : err_win_to_posix(err);
44-
return NULL;
45-
}
46-
47-
/* initialize DIR structure and copy first dir entry */
48-
dir = xmalloc(sizeof(DIR));
49-
dir->dd_handle = h;
50-
dir->dd_stat = 0;
51-
finddata2dirent(&dir->dd_dir, &fdata);
52-
return dir;
53-
}
54-
55-
struct dirent *readdir(DIR *dir)
27+
static struct dirent *dirent_readdir(dirent_DIR *dir)
5628
{
5729
if (!dir) {
5830
errno = EBADF; /* No set_errno for mingw */
@@ -79,7 +51,7 @@ struct dirent *readdir(DIR *dir)
7951
return &dir->dd_dir;
8052
}
8153

82-
int closedir(DIR *dir)
54+
static int dirent_closedir(dirent_DIR *dir)
8355
{
8456
if (!dir) {
8557
errno = EBADF;
@@ -90,3 +62,39 @@ int closedir(DIR *dir)
9062
free(dir);
9163
return 0;
9264
}
65+
66+
DIR *dirent_opendir(const char *name)
67+
{
68+
wchar_t pattern[MAX_PATH + 2]; /* + 2 for '/' '*' */
69+
WIN32_FIND_DATAW fdata;
70+
HANDLE h;
71+
int len;
72+
dirent_DIR *dir;
73+
74+
/* convert name to UTF-16 and check length < MAX_PATH */
75+
if ((len = xutftowcs_path(pattern, name)) < 0)
76+
return NULL;
77+
78+
/* append optional '/' and wildcard '*' */
79+
if (len && !is_dir_sep(pattern[len - 1]))
80+
pattern[len++] = '/';
81+
pattern[len++] = '*';
82+
pattern[len] = 0;
83+
84+
/* open find handle */
85+
h = FindFirstFileW(pattern, &fdata);
86+
if (h == INVALID_HANDLE_VALUE) {
87+
DWORD err = GetLastError();
88+
errno = (err == ERROR_DIRECTORY) ? ENOTDIR : err_win_to_posix(err);
89+
return NULL;
90+
}
91+
92+
/* initialize DIR structure and copy first dir entry */
93+
dir = xmalloc(sizeof(dirent_DIR) + MAX_PATH);
94+
dir->base_dir.preaddir = (struct dirent *(*)(DIR *dir)) dirent_readdir;
95+
dir->base_dir.pclosedir = (int (*)(DIR *dir)) dirent_closedir;
96+
dir->dd_handle = h;
97+
dir->dd_stat = 0;
98+
finddata2dirent(&dir->dd_dir, &fdata);
99+
return (DIR*) dir;
100+
}

compat/win32/dirent.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
#ifndef DIRENT_H
22
#define DIRENT_H
33

4-
typedef struct DIR DIR;
5-
64
#define DT_UNKNOWN 0
75
#define DT_DIR 1
86
#define DT_REG 2
97
#define DT_LNK 3
108

119
struct dirent {
12-
unsigned char d_type; /* file type to prevent lstat after readdir */
13-
char d_name[MAX_PATH * 3]; /* file name (* 3 for UTF-8 conversion) */
10+
unsigned char d_type; /* file type to prevent lstat after readdir */
11+
char d_name[FLEX_ARRAY]; /* file name */
1412
};
1513

16-
DIR *opendir(const char *dirname);
17-
struct dirent *readdir(DIR *dir);
18-
int closedir(DIR *dir);
14+
/*
15+
* Base DIR structure, contains pointers to readdir/closedir implementations so
16+
* that opendir may choose a concrete implementation on a call-by-call basis.
17+
*/
18+
typedef struct DIR {
19+
struct dirent *(*preaddir)(struct DIR *dir);
20+
int (*pclosedir)(struct DIR *dir);
21+
} DIR;
22+
23+
/* default dirent implementation */
24+
extern DIR *dirent_opendir(const char *dirname);
25+
26+
/* current dirent implementation */
27+
extern DIR *(*opendir)(const char *dirname);
28+
29+
#define readdir(dir) (dir->preaddir(dir))
30+
#define closedir(dir) (dir->pclosedir(dir))
1931

2032
#endif /* DIRENT_H */

0 commit comments

Comments
 (0)