Skip to content

Commit 0e4aced

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 8152a32 commit 0e4aced

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
@@ -671,9 +671,18 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
671671
{
672672
WIN32_FILE_ATTRIBUTE_DATA fdata;
673673
wchar_t wfilename[MAX_LONG_PATH];
674-
if (xutftowcs_long_path(wfilename, file_name) < 0)
674+
int wlen = xutftowcs_long_path(wfilename, file_name);
675+
if (wlen < 0)
675676
return -1;
676677

678+
/* strip trailing '/', or GetFileAttributes will fail */
679+
while (wlen && is_dir_sep(wfilename[wlen - 1]))
680+
wfilename[--wlen] = 0;
681+
if (!wlen) {
682+
errno = ENOENT;
683+
return -1;
684+
}
685+
677686
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
678687
buf->st_ino = 0;
679688
buf->st_gid = 0;
@@ -733,39 +742,6 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
733742
return -1;
734743
}
735744

736-
/* We provide our own lstat/fstat functions, since the provided
737-
* lstat/fstat functions are so slow. These stat functions are
738-
* tailored for Git's usage (read: fast), and are not meant to be
739-
* complete. Note that Git stat()s are redirected to mingw_lstat()
740-
* too, since Windows doesn't really handle symlinks that well.
741-
*/
742-
static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
743-
{
744-
int namelen;
745-
char alt_name[MAX_LONG_PATH];
746-
747-
if (!do_lstat(follow, file_name, buf))
748-
return 0;
749-
750-
/* if file_name ended in a '/', Windows returned ENOENT;
751-
* try again without trailing slashes
752-
*/
753-
if (errno != ENOENT)
754-
return -1;
755-
756-
namelen = strlen(file_name);
757-
if (namelen && file_name[namelen-1] != '/')
758-
return -1;
759-
while (namelen && file_name[namelen-1] == '/')
760-
--namelen;
761-
if (!namelen || namelen >= MAX_LONG_PATH)
762-
return -1;
763-
764-
memcpy(alt_name, file_name, namelen);
765-
alt_name[namelen] = 0;
766-
return do_lstat(follow, alt_name, buf);
767-
}
768-
769745
int (*lstat)(const char *file_name, struct stat *buf) = mingw_lstat;
770746

771747
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -793,11 +769,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
793769

794770
int mingw_lstat(const char *file_name, struct stat *buf)
795771
{
796-
return do_stat_internal(0, file_name, buf);
772+
return do_lstat(0, file_name, buf);
797773
}
798774
int mingw_stat(const char *file_name, struct stat *buf)
799775
{
800-
return do_stat_internal(1, file_name, buf);
776+
return do_lstat(1, file_name, buf);
801777
}
802778

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

0 commit comments

Comments
 (0)