Skip to content

Commit 1aeb97b

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 8f5738b commit 1aeb97b

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
@@ -530,9 +530,18 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
530530
{
531531
WIN32_FILE_ATTRIBUTE_DATA fdata;
532532
wchar_t wfilename[MAX_LONG_PATH];
533-
if (xutftowcs_long_path(wfilename, file_name) < 0)
533+
int wlen = xutftowcs_long_path(wfilename, file_name);
534+
if (wlen < 0)
534535
return -1;
535536

537+
/* strip trailing '/', or GetFileAttributes will fail */
538+
while (wlen && is_dir_sep(wfilename[wlen - 1]))
539+
wfilename[--wlen] = 0;
540+
if (!wlen) {
541+
errno = ENOENT;
542+
return -1;
543+
}
544+
536545
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
537546
buf->st_ino = 0;
538547
buf->st_gid = 0;
@@ -592,48 +601,15 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
592601
return -1;
593602
}
594603

595-
/* We provide our own lstat/fstat functions, since the provided
596-
* lstat/fstat functions are so slow. These stat functions are
597-
* tailored for Git's usage (read: fast), and are not meant to be
598-
* complete. Note that Git stat()s are redirected to mingw_lstat()
599-
* too, since Windows doesn't really handle symlinks that well.
600-
*/
601-
static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
602-
{
603-
int namelen;
604-
char alt_name[MAX_LONG_PATH];
605-
606-
if (!do_lstat(follow, file_name, buf))
607-
return 0;
608-
609-
/* if file_name ended in a '/', Windows returned ENOENT;
610-
* try again without trailing slashes
611-
*/
612-
if (errno != ENOENT)
613-
return -1;
614-
615-
namelen = strlen(file_name);
616-
if (namelen && file_name[namelen-1] != '/')
617-
return -1;
618-
while (namelen && file_name[namelen-1] == '/')
619-
--namelen;
620-
if (!namelen || namelen >= MAX_LONG_PATH)
621-
return -1;
622-
623-
memcpy(alt_name, file_name, namelen);
624-
alt_name[namelen] = 0;
625-
return do_lstat(follow, alt_name, buf);
626-
}
627-
628604
int (*lstat)(const char *file_name, struct stat *buf) = mingw_lstat;
629605

630606
int mingw_lstat(const char *file_name, struct stat *buf)
631607
{
632-
return do_stat_internal(0, file_name, buf);
608+
return do_lstat(0, file_name, buf);
633609
}
634610
int mingw_stat(const char *file_name, struct stat *buf)
635611
{
636-
return do_stat_internal(1, file_name, buf);
612+
return do_lstat(1, file_name, buf);
637613
}
638614

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

0 commit comments

Comments
 (0)