Skip to content

Commit 5ac069e

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 fc9059b commit 5ac069e

File tree

1 file changed

+12
-36
lines changed

1 file changed

+12
-36
lines changed

compat/mingw.c

+12-36
Original file line numberDiff line numberDiff line change
@@ -846,8 +846,17 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
846846
{
847847
WIN32_FILE_ATTRIBUTE_DATA fdata;
848848
wchar_t wfilename[MAX_LONG_PATH];
849-
if (xutftowcs_long_path(wfilename, file_name) < 0)
849+
int wlen = xutftowcs_long_path(wfilename, file_name);
850+
if (wlen < 0)
851+
return -1;
852+
853+
/* strip trailing '/', or GetFileAttributes will fail */
854+
while (wlen && is_dir_sep(wfilename[wlen - 1]))
855+
wfilename[--wlen] = 0;
856+
if (!wlen) {
857+
errno = ENOENT;
850858
return -1;
859+
}
851860

852861
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
853862
buf->st_ino = 0;
@@ -908,39 +917,6 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
908917
return -1;
909918
}
910919

911-
/* We provide our own lstat/fstat functions, since the provided
912-
* lstat/fstat functions are so slow. These stat functions are
913-
* tailored for Git's usage (read: fast), and are not meant to be
914-
* complete. Note that Git stat()s are redirected to mingw_lstat()
915-
* too, since Windows doesn't really handle symlinks that well.
916-
*/
917-
static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
918-
{
919-
int namelen;
920-
char alt_name[MAX_LONG_PATH];
921-
922-
if (!do_lstat(follow, file_name, buf))
923-
return 0;
924-
925-
/* if file_name ended in a '/', Windows returned ENOENT;
926-
* try again without trailing slashes
927-
*/
928-
if (errno != ENOENT)
929-
return -1;
930-
931-
namelen = strlen(file_name);
932-
if (namelen && file_name[namelen-1] != '/')
933-
return -1;
934-
while (namelen && file_name[namelen-1] == '/')
935-
--namelen;
936-
if (!namelen || namelen >= MAX_LONG_PATH)
937-
return -1;
938-
939-
memcpy(alt_name, file_name, namelen);
940-
alt_name[namelen] = 0;
941-
return do_lstat(follow, alt_name, buf);
942-
}
943-
944920
int (*lstat)(const char *file_name, struct stat *buf) = mingw_lstat;
945921

946922
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -968,11 +944,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
968944

969945
int mingw_lstat(const char *file_name, struct stat *buf)
970946
{
971-
return do_stat_internal(0, file_name, buf);
947+
return do_lstat(0, file_name, buf);
972948
}
973949
int mingw_stat(const char *file_name, struct stat *buf)
974950
{
975-
return do_stat_internal(1, file_name, buf);
951+
return do_lstat(1, file_name, buf);
976952
}
977953

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

0 commit comments

Comments
 (0)