Skip to content

Commit 818823b

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 8eda664 commit 818823b

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

950959
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
951960
buf->st_ino = 0;
@@ -1006,39 +1015,6 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
10061015
return -1;
10071016
}
10081017

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

10441020
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -1066,11 +1042,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
10661042

10671043
int mingw_lstat(const char *file_name, struct stat *buf)
10681044
{
1069-
return do_stat_internal(0, file_name, buf);
1045+
return do_lstat(0, file_name, buf);
10701046
}
10711047
int mingw_stat(const char *file_name, struct stat *buf)
10721048
{
1073-
return do_stat_internal(1, file_name, buf);
1049+
return do_lstat(1, file_name, buf);
10741050
}
10751051

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

0 commit comments

Comments
 (0)