Skip to content

Commit 3aae221

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 756ab38 commit 3aae221

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
@@ -869,8 +869,17 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
869869
{
870870
WIN32_FILE_ATTRIBUTE_DATA fdata;
871871
wchar_t wfilename[MAX_LONG_PATH];
872-
if (xutftowcs_long_path(wfilename, file_name) < 0)
872+
int wlen = xutftowcs_long_path(wfilename, file_name);
873+
if (wlen < 0)
874+
return -1;
875+
876+
/* strip trailing '/', or GetFileAttributes will fail */
877+
while (wlen && is_dir_sep(wfilename[wlen - 1]))
878+
wfilename[--wlen] = 0;
879+
if (!wlen) {
880+
errno = ENOENT;
873881
return -1;
882+
}
874883

875884
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
876885
buf->st_ino = 0;
@@ -931,39 +940,6 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
931940
return -1;
932941
}
933942

934-
/* We provide our own lstat/fstat functions, since the provided
935-
* lstat/fstat functions are so slow. These stat functions are
936-
* tailored for Git's usage (read: fast), and are not meant to be
937-
* complete. Note that Git stat()s are redirected to mingw_lstat()
938-
* too, since Windows doesn't really handle symlinks that well.
939-
*/
940-
static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
941-
{
942-
int namelen;
943-
char alt_name[MAX_LONG_PATH];
944-
945-
if (!do_lstat(follow, file_name, buf))
946-
return 0;
947-
948-
/* if file_name ended in a '/', Windows returned ENOENT;
949-
* try again without trailing slashes
950-
*/
951-
if (errno != ENOENT)
952-
return -1;
953-
954-
namelen = strlen(file_name);
955-
if (namelen && file_name[namelen-1] != '/')
956-
return -1;
957-
while (namelen && file_name[namelen-1] == '/')
958-
--namelen;
959-
if (!namelen || namelen >= MAX_LONG_PATH)
960-
return -1;
961-
962-
memcpy(alt_name, file_name, namelen);
963-
alt_name[namelen] = 0;
964-
return do_lstat(follow, alt_name, buf);
965-
}
966-
967943
int (*lstat)(const char *file_name, struct stat *buf) = mingw_lstat;
968944

969945
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -991,11 +967,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
991967

992968
int mingw_lstat(const char *file_name, struct stat *buf)
993969
{
994-
return do_stat_internal(0, file_name, buf);
970+
return do_lstat(0, file_name, buf);
995971
}
996972
int mingw_stat(const char *file_name, struct stat *buf)
997973
{
998-
return do_stat_internal(1, file_name, buf);
974+
return do_lstat(1, file_name, buf);
999975
}
1000976

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

0 commit comments

Comments
 (0)