Skip to content

Commit 41964b9

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 6c29e40 commit 41964b9

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
@@ -855,8 +855,17 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
855855
{
856856
WIN32_FILE_ATTRIBUTE_DATA fdata;
857857
wchar_t wfilename[MAX_LONG_PATH];
858-
if (xutftowcs_long_path(wfilename, file_name) < 0)
858+
int wlen = xutftowcs_long_path(wfilename, file_name);
859+
if (wlen < 0)
860+
return -1;
861+
862+
/* strip trailing '/', or GetFileAttributes will fail */
863+
while (wlen && is_dir_sep(wfilename[wlen - 1]))
864+
wfilename[--wlen] = 0;
865+
if (!wlen) {
866+
errno = ENOENT;
859867
return -1;
868+
}
860869

861870
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
862871
buf->st_ino = 0;
@@ -917,39 +926,6 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
917926
return -1;
918927
}
919928

920-
/* We provide our own lstat/fstat functions, since the provided
921-
* lstat/fstat functions are so slow. These stat functions are
922-
* tailored for Git's usage (read: fast), and are not meant to be
923-
* complete. Note that Git stat()s are redirected to mingw_lstat()
924-
* too, since Windows doesn't really handle symlinks that well.
925-
*/
926-
static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
927-
{
928-
int namelen;
929-
char alt_name[MAX_LONG_PATH];
930-
931-
if (!do_lstat(follow, file_name, buf))
932-
return 0;
933-
934-
/* if file_name ended in a '/', Windows returned ENOENT;
935-
* try again without trailing slashes
936-
*/
937-
if (errno != ENOENT)
938-
return -1;
939-
940-
namelen = strlen(file_name);
941-
if (namelen && file_name[namelen-1] != '/')
942-
return -1;
943-
while (namelen && file_name[namelen-1] == '/')
944-
--namelen;
945-
if (!namelen || namelen >= MAX_LONG_PATH)
946-
return -1;
947-
948-
memcpy(alt_name, file_name, namelen);
949-
alt_name[namelen] = 0;
950-
return do_lstat(follow, alt_name, buf);
951-
}
952-
953929
int (*lstat)(const char *file_name, struct stat *buf) = mingw_lstat;
954930

955931
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -977,11 +953,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
977953

978954
int mingw_lstat(const char *file_name, struct stat *buf)
979955
{
980-
return do_stat_internal(0, file_name, buf);
956+
return do_lstat(0, file_name, buf);
981957
}
982958
int mingw_stat(const char *file_name, struct stat *buf)
983959
{
984-
return do_stat_internal(1, file_name, buf);
960+
return do_lstat(1, file_name, buf);
985961
}
986962

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

0 commit comments

Comments
 (0)