Skip to content

Commit 512d05d

Browse files
kbleesvdye
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 f3de4bb commit 512d05d

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
@@ -798,8 +798,17 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
798798
{
799799
WIN32_FILE_ATTRIBUTE_DATA fdata;
800800
wchar_t wfilename[MAX_LONG_PATH];
801-
if (xutftowcs_long_path(wfilename, file_name) < 0)
801+
int wlen = xutftowcs_long_path(wfilename, file_name);
802+
if (wlen < 0)
803+
return -1;
804+
805+
/* strip trailing '/', or GetFileAttributes will fail */
806+
while (wlen && is_dir_sep(wfilename[wlen - 1]))
807+
wfilename[--wlen] = 0;
808+
if (!wlen) {
809+
errno = ENOENT;
802810
return -1;
811+
}
803812

804813
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
805814
buf->st_ino = 0;
@@ -860,39 +869,6 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
860869
return -1;
861870
}
862871

863-
/* We provide our own lstat/fstat functions, since the provided
864-
* lstat/fstat functions are so slow. These stat functions are
865-
* tailored for Git's usage (read: fast), and are not meant to be
866-
* complete. Note that Git stat()s are redirected to mingw_lstat()
867-
* too, since Windows doesn't really handle symlinks that well.
868-
*/
869-
static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
870-
{
871-
int namelen;
872-
char alt_name[MAX_LONG_PATH];
873-
874-
if (!do_lstat(follow, file_name, buf))
875-
return 0;
876-
877-
/* if file_name ended in a '/', Windows returned ENOENT;
878-
* try again without trailing slashes
879-
*/
880-
if (errno != ENOENT)
881-
return -1;
882-
883-
namelen = strlen(file_name);
884-
if (namelen && file_name[namelen-1] != '/')
885-
return -1;
886-
while (namelen && file_name[namelen-1] == '/')
887-
--namelen;
888-
if (!namelen || namelen >= MAX_LONG_PATH)
889-
return -1;
890-
891-
memcpy(alt_name, file_name, namelen);
892-
alt_name[namelen] = 0;
893-
return do_lstat(follow, alt_name, buf);
894-
}
895-
896872
int (*lstat)(const char *file_name, struct stat *buf) = mingw_lstat;
897873

898874
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -920,11 +896,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
920896

921897
int mingw_lstat(const char *file_name, struct stat *buf)
922898
{
923-
return do_stat_internal(0, file_name, buf);
899+
return do_lstat(0, file_name, buf);
924900
}
925901
int mingw_stat(const char *file_name, struct stat *buf)
926902
{
927-
return do_stat_internal(1, file_name, buf);
903+
return do_lstat(1, file_name, buf);
928904
}
929905

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

0 commit comments

Comments
 (0)