Skip to content

Commit 2df27f8

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 9aadb8b commit 2df27f8

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
@@ -771,8 +771,17 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
771771
{
772772
WIN32_FILE_ATTRIBUTE_DATA fdata;
773773
wchar_t wfilename[MAX_LONG_PATH];
774-
if (xutftowcs_long_path(wfilename, file_name) < 0)
774+
int wlen = xutftowcs_long_path(wfilename, file_name);
775+
if (wlen < 0)
776+
return -1;
777+
778+
/* strip trailing '/', or GetFileAttributes will fail */
779+
while (wlen && is_dir_sep(wfilename[wlen - 1]))
780+
wfilename[--wlen] = 0;
781+
if (!wlen) {
782+
errno = ENOENT;
775783
return -1;
784+
}
776785

777786
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
778787
buf->st_ino = 0;
@@ -833,39 +842,6 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
833842
return -1;
834843
}
835844

836-
/* We provide our own lstat/fstat functions, since the provided
837-
* lstat/fstat functions are so slow. These stat functions are
838-
* tailored for Git's usage (read: fast), and are not meant to be
839-
* complete. Note that Git stat()s are redirected to mingw_lstat()
840-
* too, since Windows doesn't really handle symlinks that well.
841-
*/
842-
static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
843-
{
844-
int namelen;
845-
char alt_name[MAX_LONG_PATH];
846-
847-
if (!do_lstat(follow, file_name, buf))
848-
return 0;
849-
850-
/* if file_name ended in a '/', Windows returned ENOENT;
851-
* try again without trailing slashes
852-
*/
853-
if (errno != ENOENT)
854-
return -1;
855-
856-
namelen = strlen(file_name);
857-
if (namelen && file_name[namelen-1] != '/')
858-
return -1;
859-
while (namelen && file_name[namelen-1] == '/')
860-
--namelen;
861-
if (!namelen || namelen >= MAX_LONG_PATH)
862-
return -1;
863-
864-
memcpy(alt_name, file_name, namelen);
865-
alt_name[namelen] = 0;
866-
return do_lstat(follow, alt_name, buf);
867-
}
868-
869845
int (*lstat)(const char *file_name, struct stat *buf) = mingw_lstat;
870846

871847
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -893,11 +869,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
893869

894870
int mingw_lstat(const char *file_name, struct stat *buf)
895871
{
896-
return do_stat_internal(0, file_name, buf);
872+
return do_lstat(0, file_name, buf);
897873
}
898874
int mingw_stat(const char *file_name, struct stat *buf)
899875
{
900-
return do_stat_internal(1, file_name, buf);
876+
return do_lstat(1, file_name, buf);
901877
}
902878

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

0 commit comments

Comments
 (0)