Skip to content

Commit b0588bf

Browse files
kbleesdscho
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 e842480 commit b0588bf

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
@@ -967,8 +967,17 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
967967
{
968968
WIN32_FILE_ATTRIBUTE_DATA fdata;
969969
wchar_t wfilename[MAX_LONG_PATH];
970-
if (xutftowcs_long_path(wfilename, file_name) < 0)
970+
int wlen = xutftowcs_long_path(wfilename, file_name);
971+
if (wlen < 0)
972+
return -1;
973+
974+
/* strip trailing '/', or GetFileAttributes will fail */
975+
while (wlen && is_dir_sep(wfilename[wlen - 1]))
976+
wfilename[--wlen] = 0;
977+
if (!wlen) {
978+
errno = ENOENT;
971979
return -1;
980+
}
972981

973982
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
974983
buf->st_ino = 0;
@@ -1029,39 +1038,6 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
10291038
return -1;
10301039
}
10311040

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

10671043
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -1089,11 +1065,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
10891065

10901066
int mingw_lstat(const char *file_name, struct stat *buf)
10911067
{
1092-
return do_stat_internal(0, file_name, buf);
1068+
return do_lstat(0, file_name, buf);
10931069
}
10941070
int mingw_stat(const char *file_name, struct stat *buf)
10951071
{
1096-
return do_stat_internal(1, file_name, buf);
1072+
return do_lstat(1, file_name, buf);
10971073
}
10981074

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

0 commit comments

Comments
 (0)