Skip to content

Commit ac991bc

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 861ad61 commit ac991bc

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

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

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

899875
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -921,11 +897,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
921897

922898
int mingw_lstat(const char *file_name, struct stat *buf)
923899
{
924-
return do_stat_internal(0, file_name, buf);
900+
return do_lstat(0, file_name, buf);
925901
}
926902
int mingw_stat(const char *file_name, struct stat *buf)
927903
{
928-
return do_stat_internal(1, file_name, buf);
904+
return do_lstat(1, file_name, buf);
929905
}
930906

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

0 commit comments

Comments
 (0)