Skip to content

Commit 014c9ba

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 3bbb298 commit 014c9ba

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

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

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

10431019
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -1065,11 +1041,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
10651041

10661042
int mingw_lstat(const char *file_name, struct stat *buf)
10671043
{
1068-
return do_stat_internal(0, file_name, buf);
1044+
return do_lstat(0, file_name, buf);
10691045
}
10701046
int mingw_stat(const char *file_name, struct stat *buf)
10711047
{
1072-
return do_stat_internal(1, file_name, buf);
1048+
return do_lstat(1, file_name, buf);
10731049
}
10741050

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

0 commit comments

Comments
 (0)