Skip to content

Commit 902b18a

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 4b1d390 commit 902b18a

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
@@ -942,8 +942,17 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
942942
{
943943
WIN32_FILE_ATTRIBUTE_DATA fdata;
944944
wchar_t wfilename[MAX_LONG_PATH];
945-
if (xutftowcs_long_path(wfilename, file_name) < 0)
945+
int wlen = xutftowcs_long_path(wfilename, file_name);
946+
if (wlen < 0)
947+
return -1;
948+
949+
/* strip trailing '/', or GetFileAttributes will fail */
950+
while (wlen && is_dir_sep(wfilename[wlen - 1]))
951+
wfilename[--wlen] = 0;
952+
if (!wlen) {
953+
errno = ENOENT;
946954
return -1;
955+
}
947956

948957
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
949958
buf->st_ino = 0;
@@ -1004,39 +1013,6 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
10041013
return -1;
10051014
}
10061015

1007-
/* We provide our own lstat/fstat functions, since the provided
1008-
* lstat/fstat functions are so slow. These stat functions are
1009-
* tailored for Git's usage (read: fast), and are not meant to be
1010-
* complete. Note that Git stat()s are redirected to mingw_lstat()
1011-
* too, since Windows doesn't really handle symlinks that well.
1012-
*/
1013-
static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
1014-
{
1015-
size_t namelen;
1016-
char alt_name[MAX_LONG_PATH];
1017-
1018-
if (!do_lstat(follow, file_name, buf))
1019-
return 0;
1020-
1021-
/* if file_name ended in a '/', Windows returned ENOENT;
1022-
* try again without trailing slashes
1023-
*/
1024-
if (errno != ENOENT)
1025-
return -1;
1026-
1027-
namelen = strlen(file_name);
1028-
if (namelen && file_name[namelen-1] != '/')
1029-
return -1;
1030-
while (namelen && file_name[namelen-1] == '/')
1031-
--namelen;
1032-
if (!namelen || namelen >= MAX_LONG_PATH)
1033-
return -1;
1034-
1035-
memcpy(alt_name, file_name, namelen);
1036-
alt_name[namelen] = 0;
1037-
return do_lstat(follow, alt_name, buf);
1038-
}
1039-
10401016
int (*lstat)(const char *file_name, struct stat *buf) = mingw_lstat;
10411017

10421018
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -1064,11 +1040,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
10641040

10651041
int mingw_lstat(const char *file_name, struct stat *buf)
10661042
{
1067-
return do_stat_internal(0, file_name, buf);
1043+
return do_lstat(0, file_name, buf);
10681044
}
10691045
int mingw_stat(const char *file_name, struct stat *buf)
10701046
{
1071-
return do_stat_internal(1, file_name, buf);
1047+
return do_lstat(1, file_name, buf);
10721048
}
10731049

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

0 commit comments

Comments
 (0)