dirload: loop conditional; less path mangling
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
diff --git a/src/win32/path_w32.c b/src/win32/path_w32.c
index f793cbd..eda6e26 100644
--- a/src/win32/path_w32.c
+++ b/src/win32/path_w32.c
@@ -32,6 +32,11 @@
#define PATH__MAX_UNC_LEN (32767)
+/* Using _FIND_FIRST_EX_LARGE_FETCH may increase performance in Windows 7
+ * and better. Prior versions will ignore this.
+ */
+#define _FIND_FIRST_EX_LARGE_FETCH 2
+
GIT_INLINE(int) path__cwd(wchar_t *path, int size)
{
int len;
@@ -309,6 +314,45 @@ char *git_win32_path_8dot3_name(const char *path)
return shortname;
}
+GIT_INLINE(int) path_with_stat_alloc(
+ git_path_with_stat **out,
+ const char *parent_path,
+ size_t parent_path_len,
+ const char *child_path,
+ size_t child_path_len,
+ bool trailing_slash)
+{
+ git_path_with_stat *ps;
+ int inner_slash =
+ (parent_path_len > 0 && parent_path[parent_path_len-1] != '/');
+ size_t path_len, ps_size;
+
+ GITERR_CHECK_ALLOC_ADD(&path_len, parent_path_len, inner_slash);
+ GITERR_CHECK_ALLOC_ADD(&path_len, path_len, child_path_len);
+ GITERR_CHECK_ALLOC_ADD(&path_len, path_len, trailing_slash ? 1 : 0);
+
+ GITERR_CHECK_ALLOC_ADD(&ps_size, sizeof(git_path_with_stat), path_len);
+
+ ps = git__calloc(1, ps_size);
+ GITERR_CHECK_ALLOC(ps);
+
+ if (parent_path_len)
+ memcpy(ps->path, parent_path, parent_path_len);
+
+ if (inner_slash)
+ ps->path[parent_path_len] = '/';
+
+ memcpy(&ps->path[parent_path_len + inner_slash], child_path, child_path_len);
+
+ if (trailing_slash)
+ ps->path[path_len-1] = '/';
+
+ ps->path_len = path_len;
+
+ *out = ps;
+ return 0;
+}
+
#if !defined(__MINGW32__)
int git_win32_path_dirload_with_stat(
const char *path,
@@ -326,11 +370,9 @@ int git_win32_path_dirload_with_stat(
size_t cmp_len;
size_t start_len = start_stat ? strlen(start_stat) : 0;
size_t end_len = end_stat ? strlen(end_stat) : 0;
- size_t path_size = strlen(path);
- const char *repo_path = path + prefix_len;
- size_t repo_path_len = strlen(repo_path);
char work_path[PATH__MAX_UNC_LEN];
- size_t path_len;
+ const char *suffix;
+ size_t path_len, work_path_len, suffix_len;
if (!git_win32__findfirstfile_filter(pathw, path)) {
giterr_set(GITERR_OS, "Could not parse the path '%s'", path);
@@ -338,14 +380,23 @@ int git_win32_path_dirload_with_stat(
}
strncomp = (flags & GIT_PATH_DIR_IGNORE_CASE) != 0 ?
- git__strncasecmp : git__strncmp;
+ git__strncasecmp : git__strncmp;
+
+ path_len = strlen(path);
+ suffix = path + prefix_len;
+ suffix_len = path_len - prefix_len;
/* use of FIND_FIRST_EX_LARGE_FETCH flag in the FindFirstFileExW call could benefit perormance
* here when querying large repositories on Windows 7 (0x0600) or newer versions of Windows.
* doing so could introduce compatibility issues on older versions of Windows. */
- dir.h = FindFirstFileExW(pathw, FindExInfoBasic, &dir.f, FindExSearchNameMatch, NULL, 0);
- dir.first = 1;
+ dir.h = FindFirstFileExW(
+ pathw,
+ FindExInfoBasic,
+ &dir.f,
+ FindExSearchNameMatch,
+ NULL,
+ _FIND_FIRST_EX_LARGE_FETCH);
if (dir.h == INVALID_HANDLE_VALUE) {
error = -1;
@@ -353,64 +404,56 @@ int git_win32_path_dirload_with_stat(
goto clean_up_and_exit;
}
- if (repo_path_len > PATH__MAX_UNC_LEN) {
- error = -1;
- giterr_set(GITERR_OS, "Could not open directory '%s'", path);
- goto clean_up_and_exit;
- }
-
- memcpy(work_path, repo_path, repo_path_len);
-
- while (1) {
- if (!git_path_is_dot_or_dotdotW(dir.f.cFileName)) {
- path_len = git__utf16_to_8(work_path + repo_path_len, ARRAYSIZE(work_path) - repo_path_len, dir.f.cFileName);
-
- work_path[path_len + repo_path_len] = '\0';
- path_len = path_len + repo_path_len;
-
- cmp_len = min(start_len, path_len);
- if (!(cmp_len && strncomp(work_path, start_stat, cmp_len) < 0)) {
- cmp_len = min(end_len, path_len);
-
- if (!(cmp_len && strncomp(work_path, end_stat, cmp_len) > 0)) {
- ps = git__calloc(1, sizeof(git_path_with_stat) + path_len + 2);
-
- if ((error = git_win32__file_attribute_to_stat(&ps->st,
- (WIN32_FILE_ATTRIBUTE_DATA *)&dir.f,
- NULL)) < 0) {
- git__free(ps);
- goto clean_up_and_exit;
- }
+ do {
+ if (git_path_is_dot_or_dotdotW(dir.f.cFileName))
+ continue;
+
+ if ((work_path_len = git__utf16_to_8(work_path, PATH__MAX_UNC_LEN, dir.f.cFileName)) < 0) {
+ error = -1;
+ giterr_set(GITERR_OS, "Could not convert path to UTF-8 (path too long?)");
+ goto clean_up_and_exit;
+ }
+ work_path[work_path_len] = '\0';
+
+ /* TODO: what about junctions to directories? */
+ if ((error = path_with_stat_alloc(&ps,
+ suffix, suffix_len,
+ work_path, work_path_len,
+ (dir.f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)) < 0)
+ goto clean_up_and_exit;
+
+ /* skip if before start_stat or after end_stat */
+ cmp_len = min(start_len, work_path_len);
+ if (cmp_len && strncomp(ps->path, start_stat, cmp_len) < 0) {
+ git__free(ps);
+ continue;
+ }
- memcpy(ps->path, work_path, path_len + 1);
- ps->path_len = path_len;
+ cmp_len = min(end_len, work_path_len);
+ if (cmp_len && strncomp(ps->path, end_stat, cmp_len) > 0) {
+ git__free(ps);
+ continue;
+ }
- if (S_ISDIR(ps->st.st_mode)) {
- ps->path[ps->path_len++] = '/';
- ps->path[ps->path_len] = '\0';
- } else if (!S_ISREG(ps->st.st_mode) && !S_ISLNK(ps->st.st_mode)) {
- git__free(ps);
- ps = NULL;
- }
+ if ((error = git_win32__file_attribute_to_stat(&ps->st,
+ (WIN32_FILE_ATTRIBUTE_DATA *)&dir.f,
+ NULL)) < 0) {
+ git__free(ps);
+ goto clean_up_and_exit;
+ }
- if (ps)
- git_vector_insert(contents, ps);
- }
- }
+ if (!S_ISDIR(ps->st.st_mode) && !S_ISREG(ps->st.st_mode) && !S_ISLNK(ps->st.st_mode)) {
+ git__free(ps);
+ continue;
}
- memset(&dir.f, 0, sizeof(git_path_with_stat));
- dir.first = 0;
+ git_vector_insert(contents, ps);
+ } while (FindNextFileW(dir.h, &dir.f));
- if (!FindNextFileW(dir.h, &dir.f)) {
- if (GetLastError() == ERROR_NO_MORE_FILES)
- break;
- else {
- error = -1;
- giterr_set(GITERR_OS, "Could not get attributes for file in '%s'", path);
- goto clean_up_and_exit;
- }
- }
+ if (GetLastError() != ERROR_NO_MORE_FILES) {
+ error = -1;
+ giterr_set(GITERR_OS, "Could not get attributes for file in '%s'", path);
+ goto clean_up_and_exit;
}
/* sort now that directory suffix is added */