Skip to content

Commit 96c28a7

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 0659296 commit 96c28a7

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
@@ -828,8 +828,17 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
828828
{
829829
WIN32_FILE_ATTRIBUTE_DATA fdata;
830830
wchar_t wfilename[MAX_LONG_PATH];
831-
if (xutftowcs_long_path(wfilename, file_name) < 0)
831+
int wlen = xutftowcs_long_path(wfilename, file_name);
832+
if (wlen < 0)
833+
return -1;
834+
835+
/* strip trailing '/', or GetFileAttributes will fail */
836+
while (wlen && is_dir_sep(wfilename[wlen - 1]))
837+
wfilename[--wlen] = 0;
838+
if (!wlen) {
839+
errno = ENOENT;
832840
return -1;
841+
}
833842

834843
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
835844
buf->st_ino = 0;
@@ -890,39 +899,6 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
890899
return -1;
891900
}
892901

893-
/* We provide our own lstat/fstat functions, since the provided
894-
* lstat/fstat functions are so slow. These stat functions are
895-
* tailored for Git's usage (read: fast), and are not meant to be
896-
* complete. Note that Git stat()s are redirected to mingw_lstat()
897-
* too, since Windows doesn't really handle symlinks that well.
898-
*/
899-
static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
900-
{
901-
int namelen;
902-
char alt_name[MAX_LONG_PATH];
903-
904-
if (!do_lstat(follow, file_name, buf))
905-
return 0;
906-
907-
/* if file_name ended in a '/', Windows returned ENOENT;
908-
* try again without trailing slashes
909-
*/
910-
if (errno != ENOENT)
911-
return -1;
912-
913-
namelen = strlen(file_name);
914-
if (namelen && file_name[namelen-1] != '/')
915-
return -1;
916-
while (namelen && file_name[namelen-1] == '/')
917-
--namelen;
918-
if (!namelen || namelen >= MAX_LONG_PATH)
919-
return -1;
920-
921-
memcpy(alt_name, file_name, namelen);
922-
alt_name[namelen] = 0;
923-
return do_lstat(follow, alt_name, buf);
924-
}
925-
926902
int (*lstat)(const char *file_name, struct stat *buf) = mingw_lstat;
927903

928904
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -950,11 +926,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
950926

951927
int mingw_lstat(const char *file_name, struct stat *buf)
952928
{
953-
return do_stat_internal(0, file_name, buf);
929+
return do_lstat(0, file_name, buf);
954930
}
955931
int mingw_stat(const char *file_name, struct stat *buf)
956932
{
957-
return do_stat_internal(1, file_name, buf);
933+
return do_lstat(1, file_name, buf);
958934
}
959935

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

0 commit comments

Comments
 (0)