Skip to content

Commit a1bf4dc

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 d6dc232 commit a1bf4dc

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
@@ -717,9 +717,18 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
717717
{
718718
WIN32_FILE_ATTRIBUTE_DATA fdata;
719719
wchar_t wfilename[MAX_LONG_PATH];
720-
if (xutftowcs_long_path(wfilename, file_name) < 0)
720+
int wlen = xutftowcs_long_path(wfilename, file_name);
721+
if (wlen < 0)
721722
return -1;
722723

724+
/* strip trailing '/', or GetFileAttributes will fail */
725+
while (wlen && is_dir_sep(wfilename[wlen - 1]))
726+
wfilename[--wlen] = 0;
727+
if (!wlen) {
728+
errno = ENOENT;
729+
return -1;
730+
}
731+
723732
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
724733
buf->st_ino = 0;
725734
buf->st_gid = 0;
@@ -779,39 +788,6 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
779788
return -1;
780789
}
781790

782-
/* We provide our own lstat/fstat functions, since the provided
783-
* lstat/fstat functions are so slow. These stat functions are
784-
* tailored for Git's usage (read: fast), and are not meant to be
785-
* complete. Note that Git stat()s are redirected to mingw_lstat()
786-
* too, since Windows doesn't really handle symlinks that well.
787-
*/
788-
static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
789-
{
790-
int namelen;
791-
char alt_name[MAX_LONG_PATH];
792-
793-
if (!do_lstat(follow, file_name, buf))
794-
return 0;
795-
796-
/* if file_name ended in a '/', Windows returned ENOENT;
797-
* try again without trailing slashes
798-
*/
799-
if (errno != ENOENT)
800-
return -1;
801-
802-
namelen = strlen(file_name);
803-
if (namelen && file_name[namelen-1] != '/')
804-
return -1;
805-
while (namelen && file_name[namelen-1] == '/')
806-
--namelen;
807-
if (!namelen || namelen >= MAX_LONG_PATH)
808-
return -1;
809-
810-
memcpy(alt_name, file_name, namelen);
811-
alt_name[namelen] = 0;
812-
return do_lstat(follow, alt_name, buf);
813-
}
814-
815791
int (*lstat)(const char *file_name, struct stat *buf) = mingw_lstat;
816792

817793
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -839,11 +815,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
839815

840816
int mingw_lstat(const char *file_name, struct stat *buf)
841817
{
842-
return do_stat_internal(0, file_name, buf);
818+
return do_lstat(0, file_name, buf);
843819
}
844820
int mingw_stat(const char *file_name, struct stat *buf)
845821
{
846-
return do_stat_internal(1, file_name, buf);
822+
return do_lstat(1, file_name, buf);
847823
}
848824

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

0 commit comments

Comments
 (0)