Skip to content

Commit 49ddf83

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 4cb6041 commit 49ddf83

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

802811
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
803812
buf->st_ino = 0;
@@ -858,39 +867,6 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
858867
return -1;
859868
}
860869

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

896872
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -918,11 +894,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
918894

919895
int mingw_lstat(const char *file_name, struct stat *buf)
920896
{
921-
return do_stat_internal(0, file_name, buf);
897+
return do_lstat(0, file_name, buf);
922898
}
923899
int mingw_stat(const char *file_name, struct stat *buf)
924900
{
925-
return do_stat_internal(1, file_name, buf);
901+
return do_lstat(1, file_name, buf);
926902
}
927903

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

0 commit comments

Comments
 (0)