Skip to content

Commit 75de2bd

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 2232ff4 commit 75de2bd

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

806815
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
807816
buf->st_ino = 0;
@@ -862,39 +871,6 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
862871
return -1;
863872
}
864873

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

900876
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -922,11 +898,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
922898

923899
int mingw_lstat(const char *file_name, struct stat *buf)
924900
{
925-
return do_stat_internal(0, file_name, buf);
901+
return do_lstat(0, file_name, buf);
926902
}
927903
int mingw_stat(const char *file_name, struct stat *buf)
928904
{
929-
return do_stat_internal(1, file_name, buf);
905+
return do_lstat(1, file_name, buf);
930906
}
931907

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

0 commit comments

Comments
 (0)