Skip to content

Commit 6a58f12

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 12f5b49 commit 6a58f12

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
@@ -954,8 +954,17 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
954954
{
955955
WIN32_FILE_ATTRIBUTE_DATA fdata;
956956
wchar_t wfilename[MAX_LONG_PATH];
957-
if (xutftowcs_long_path(wfilename, file_name) < 0)
957+
int wlen = xutftowcs_long_path(wfilename, file_name);
958+
if (wlen < 0)
959+
return -1;
960+
961+
/* strip trailing '/', or GetFileAttributes will fail */
962+
while (wlen && is_dir_sep(wfilename[wlen - 1]))
963+
wfilename[--wlen] = 0;
964+
if (!wlen) {
965+
errno = ENOENT;
958966
return -1;
967+
}
959968

960969
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
961970
buf->st_ino = 0;
@@ -1016,39 +1025,6 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
10161025
return -1;
10171026
}
10181027

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

10541030
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -1076,11 +1052,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
10761052

10771053
int mingw_lstat(const char *file_name, struct stat *buf)
10781054
{
1079-
return do_stat_internal(0, file_name, buf);
1055+
return do_lstat(0, file_name, buf);
10801056
}
10811057
int mingw_stat(const char *file_name, struct stat *buf)
10821058
{
1083-
return do_stat_internal(1, file_name, buf);
1059+
return do_lstat(1, file_name, buf);
10841060
}
10851061

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

0 commit comments

Comments
 (0)