Skip to content

Commit f31f1e4

Browse files
kbleesGit for Windows Build Agent
authored andcommitted
Win32: don't call GetFileAttributes twice in mingw_lstat()
GetFileAttributes cannot handle paths with trailing dir separator. The current [l]stat implementation calls GetFileAttributes twice if the path has trailing slashes (first with the original path passed to [l]stat, and and a second time with a path copy with trailing '/' removed). With Unicode conversion, we get the length of the path for free and also have a (wide char) buffer that can be modified. Remove trailing directory separators before calling the Win32 API. Signed-off-by: Karsten Blees <[email protected]>
1 parent 71e607b commit f31f1e4

File tree

1 file changed

+12
-36
lines changed

1 file changed

+12
-36
lines changed

compat/mingw.c

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -969,8 +969,17 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
969969
{
970970
WIN32_FILE_ATTRIBUTE_DATA fdata;
971971
wchar_t wfilename[MAX_LONG_PATH];
972-
if (xutftowcs_long_path(wfilename, file_name) < 0)
972+
int wlen = xutftowcs_long_path(wfilename, file_name);
973+
if (wlen < 0)
974+
return -1;
975+
976+
/* strip trailing '/', or GetFileAttributes will fail */
977+
while (wlen && is_dir_sep(wfilename[wlen - 1]))
978+
wfilename[--wlen] = 0;
979+
if (!wlen) {
980+
errno = ENOENT;
973981
return -1;
982+
}
974983

975984
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
976985
buf->st_ino = 0;
@@ -1031,39 +1040,6 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
10311040
return -1;
10321041
}
10331042

1034-
/* We provide our own lstat/fstat functions, since the provided
1035-
* lstat/fstat functions are so slow. These stat functions are
1036-
* tailored for Git's usage (read: fast), and are not meant to be
1037-
* complete. Note that Git stat()s are redirected to mingw_lstat()
1038-
* too, since Windows doesn't really handle symlinks that well.
1039-
*/
1040-
static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
1041-
{
1042-
size_t namelen;
1043-
char alt_name[MAX_LONG_PATH];
1044-
1045-
if (!do_lstat(follow, file_name, buf))
1046-
return 0;
1047-
1048-
/* if file_name ended in a '/', Windows returned ENOENT;
1049-
* try again without trailing slashes
1050-
*/
1051-
if (errno != ENOENT)
1052-
return -1;
1053-
1054-
namelen = strlen(file_name);
1055-
if (namelen && file_name[namelen-1] != '/')
1056-
return -1;
1057-
while (namelen && file_name[namelen-1] == '/')
1058-
--namelen;
1059-
if (!namelen || namelen >= MAX_LONG_PATH)
1060-
return -1;
1061-
1062-
memcpy(alt_name, file_name, namelen);
1063-
alt_name[namelen] = 0;
1064-
return do_lstat(follow, alt_name, buf);
1065-
}
1066-
10671043
int (*lstat)(const char *file_name, struct stat *buf) = mingw_lstat;
10681044

10691045
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -1091,11 +1067,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
10911067

10921068
int mingw_lstat(const char *file_name, struct stat *buf)
10931069
{
1094-
return do_stat_internal(0, file_name, buf);
1070+
return do_lstat(0, file_name, buf);
10951071
}
10961072
int mingw_stat(const char *file_name, struct stat *buf)
10971073
{
1098-
return do_stat_internal(1, file_name, buf);
1074+
return do_lstat(1, file_name, buf);
10991075
}
11001076

11011077
int mingw_fstat(int fd, struct stat *buf)

0 commit comments

Comments
 (0)