Skip to content

Commit f6c9719

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 e78772f commit f6c9719

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
@@ -565,9 +565,18 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
565565
{
566566
WIN32_FILE_ATTRIBUTE_DATA fdata;
567567
wchar_t wfilename[MAX_LONG_PATH];
568-
if (xutftowcs_long_path(wfilename, file_name) < 0)
568+
int wlen = xutftowcs_long_path(wfilename, file_name);
569+
if (wlen < 0)
569570
return -1;
570571

572+
/* strip trailing '/', or GetFileAttributes will fail */
573+
while (wlen && is_dir_sep(wfilename[wlen - 1]))
574+
wfilename[--wlen] = 0;
575+
if (!wlen) {
576+
errno = ENOENT;
577+
return -1;
578+
}
579+
571580
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
572581
buf->st_ino = 0;
573582
buf->st_gid = 0;
@@ -627,48 +636,15 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
627636
return -1;
628637
}
629638

630-
/* We provide our own lstat/fstat functions, since the provided
631-
* lstat/fstat functions are so slow. These stat functions are
632-
* tailored for Git's usage (read: fast), and are not meant to be
633-
* complete. Note that Git stat()s are redirected to mingw_lstat()
634-
* too, since Windows doesn't really handle symlinks that well.
635-
*/
636-
static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
637-
{
638-
int namelen;
639-
char alt_name[MAX_LONG_PATH];
640-
641-
if (!do_lstat(follow, file_name, buf))
642-
return 0;
643-
644-
/* if file_name ended in a '/', Windows returned ENOENT;
645-
* try again without trailing slashes
646-
*/
647-
if (errno != ENOENT)
648-
return -1;
649-
650-
namelen = strlen(file_name);
651-
if (namelen && file_name[namelen-1] != '/')
652-
return -1;
653-
while (namelen && file_name[namelen-1] == '/')
654-
--namelen;
655-
if (!namelen || namelen >= MAX_LONG_PATH)
656-
return -1;
657-
658-
memcpy(alt_name, file_name, namelen);
659-
alt_name[namelen] = 0;
660-
return do_lstat(follow, alt_name, buf);
661-
}
662-
663639
int (*lstat)(const char *file_name, struct stat *buf) = mingw_lstat;
664640

665641
int mingw_lstat(const char *file_name, struct stat *buf)
666642
{
667-
return do_stat_internal(0, file_name, buf);
643+
return do_lstat(0, file_name, buf);
668644
}
669645
int mingw_stat(const char *file_name, struct stat *buf)
670646
{
671-
return do_stat_internal(1, file_name, buf);
647+
return do_lstat(1, file_name, buf);
672648
}
673649

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

0 commit comments

Comments
 (0)