Skip to content

Commit 60eb6fc

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 29a7f8a commit 60eb6fc

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

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

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

816792
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -838,11 +814,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
838814

839815
int mingw_lstat(const char *file_name, struct stat *buf)
840816
{
841-
return do_stat_internal(0, file_name, buf);
817+
return do_lstat(0, file_name, buf);
842818
}
843819
int mingw_stat(const char *file_name, struct stat *buf)
844820
{
845-
return do_stat_internal(1, file_name, buf);
821+
return do_lstat(1, file_name, buf);
846822
}
847823

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

0 commit comments

Comments
 (0)