mingw: don't call `GetFileAttributes()` twice in `mingw_lstat()`

The Win32 API function `GetFileAttributes()` cannot handle paths with trailing dir separators. The current `mingw_stat()`/`mingw_lstat()` implementation calls `GetFileAttributes()` twice if the path has trailing slashes (first with the original path that was passed as function parameter, and and a second time with a path copy with trailing '/' removed). With the conversion to wide Unicode, we get the length of the path for free, and also have a (wide char) buffer that can be modified. This makes it easy to avoid that extraneous Win32 API call. Signed-off-by: Karsten Blees <karsten.blees@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karsten Blees committed Jan 9, 2026 at 20:04 UTC 3b96b99683679ee0d0c4e05cb5d1da37e13e02e5
1 file changed +15 -38
compat/mingw.c
+15 -38
@@ -918,8 +918,9 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
918 }
919
920 /* We keep the do_lstat code in a separate function to avoid recursion.
921 - * When a path ends with a slash, the stat will fail with ENOENT. In
922 - * this case, we strip the trailing slashes and stat again.
921 + * When a path ends with a slash, the call to `GetFileAttributedExW()`
922 + * would fail. To prevent this, we strip any trailing slashes before that
923 + * call.
924 *
925 * If follow is true then act like stat() and report on the link
926 * target. Otherwise report on the link itself.
@@ -928,9 +929,18 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
929 {
930 WIN32_FILE_ATTRIBUTE_DATA fdata;
931 wchar_t wfilename[MAX_PATH];
931 - if (xutftowcs_path(wfilename, file_name) < 0)
932 + int wlen = xutftowcs_path(wfilename, file_name);
933 + if (wlen < 0)
934 return -1;
935
936 + /* strip trailing '/', or GetFileAttributes will fail */
937 + while (wlen && is_dir_sep(wfilename[wlen - 1]))
938 + wfilename[--wlen] = 0;
939 + if (!wlen) {
940 + errno = ENOENT;
941 + return -1;
942 + }
943 +
944 if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
945 buf->st_ino = 0;
946 buf->st_gid = 0;
@@ -990,39 +1000,6 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
1000 return -1;
1001 }
1002
993 -/* We provide our own lstat/fstat functions, since the provided
994 - * lstat/fstat functions are so slow. These stat functions are
995 - * tailored for Git's usage (read: fast), and are not meant to be
996 - * complete. Note that Git stat()s are redirected to mingw_lstat()
997 - * too, since Windows doesn't really handle symlinks that well.
998 - */
999 -static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
1000 -{
1001 - size_t namelen;
1002 - char alt_name[PATH_MAX];
1003 -
1004 - if (!do_lstat(follow, file_name, buf))
1005 - return 0;
1006 -
1007 - /* if file_name ended in a '/', Windows returned ENOENT;
1008 - * try again without trailing slashes
1009 - */
1010 - if (errno != ENOENT)
1011 - return -1;
1012 -
1013 - namelen = strlen(file_name);
1014 - if (namelen && file_name[namelen-1] != '/')
1015 - return -1;
1016 - while (namelen && file_name[namelen-1] == '/')
1017 - --namelen;
1018 - if (!namelen || namelen >= PATH_MAX)
1019 - return -1;
1020 -
1021 - memcpy(alt_name, file_name, namelen);
1022 - alt_name[namelen] = 0;
1023 - return do_lstat(follow, alt_name, buf);
1024 -}
1025 -
1003 static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
1004 {
1005 BY_HANDLE_FILE_INFORMATION fdata;
@@ -1048,11 +1025,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
1025
1026 int mingw_lstat(const char *file_name, struct stat *buf)
1027 {
1051 - return do_stat_internal(0, file_name, buf);
1028 + return do_lstat(0, file_name, buf);
1029 }
1030 int mingw_stat(const char *file_name, struct stat *buf)
1031 {
1055 - return do_stat_internal(1, file_name, buf);
1032 + return do_lstat(1, file_name, buf);
1033 }
1034
1035 int mingw_fstat(int fd, struct stat *buf)