Skip to content

Commit e301774

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 1bd5d76 commit e301774

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
@@ -547,9 +547,18 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
547547
{
548548
WIN32_FILE_ATTRIBUTE_DATA fdata;
549549
wchar_t wfilename[MAX_LONG_PATH];
550-
if (xutftowcs_long_path(wfilename, file_name) < 0)
550+
int wlen = xutftowcs_long_path(wfilename, file_name);
551+
if (wlen < 0)
551552
return -1;
552553

554+
/* strip trailing '/', or GetFileAttributes will fail */
555+
while (wlen && is_dir_sep(wfilename[wlen - 1]))
556+
wfilename[--wlen] = 0;
557+
if (!wlen) {
558+
errno = ENOENT;
559+
return -1;
560+
}
561+
553562
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
554563
buf->st_ino = 0;
555564
buf->st_gid = 0;
@@ -609,48 +618,15 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
609618
return -1;
610619
}
611620

612-
/* We provide our own lstat/fstat functions, since the provided
613-
* lstat/fstat functions are so slow. These stat functions are
614-
* tailored for Git's usage (read: fast), and are not meant to be
615-
* complete. Note that Git stat()s are redirected to mingw_lstat()
616-
* too, since Windows doesn't really handle symlinks that well.
617-
*/
618-
static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
619-
{
620-
int namelen;
621-
char alt_name[MAX_LONG_PATH];
622-
623-
if (!do_lstat(follow, file_name, buf))
624-
return 0;
625-
626-
/* if file_name ended in a '/', Windows returned ENOENT;
627-
* try again without trailing slashes
628-
*/
629-
if (errno != ENOENT)
630-
return -1;
631-
632-
namelen = strlen(file_name);
633-
if (namelen && file_name[namelen-1] != '/')
634-
return -1;
635-
while (namelen && file_name[namelen-1] == '/')
636-
--namelen;
637-
if (!namelen || namelen >= MAX_LONG_PATH)
638-
return -1;
639-
640-
memcpy(alt_name, file_name, namelen);
641-
alt_name[namelen] = 0;
642-
return do_lstat(follow, alt_name, buf);
643-
}
644-
645621
int (*lstat)(const char *file_name, struct stat *buf) = mingw_lstat;
646622

647623
int mingw_lstat(const char *file_name, struct stat *buf)
648624
{
649-
return do_stat_internal(0, file_name, buf);
625+
return do_lstat(0, file_name, buf);
650626
}
651627
int mingw_stat(const char *file_name, struct stat *buf)
652628
{
653-
return do_stat_internal(1, file_name, buf);
629+
return do_lstat(1, file_name, buf);
654630
}
655631

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

0 commit comments

Comments
 (0)