Skip to content

Commit 211bead

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 9819207 commit 211bead

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
@@ -874,8 +874,17 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
874874
{
875875
WIN32_FILE_ATTRIBUTE_DATA fdata;
876876
wchar_t wfilename[MAX_LONG_PATH];
877-
if (xutftowcs_long_path(wfilename, file_name) < 0)
877+
int wlen = xutftowcs_long_path(wfilename, file_name);
878+
if (wlen < 0)
879+
return -1;
880+
881+
/* strip trailing '/', or GetFileAttributes will fail */
882+
while (wlen && is_dir_sep(wfilename[wlen - 1]))
883+
wfilename[--wlen] = 0;
884+
if (!wlen) {
885+
errno = ENOENT;
878886
return -1;
887+
}
879888

880889
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
881890
buf->st_ino = 0;
@@ -936,39 +945,6 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
936945
return -1;
937946
}
938947

939-
/* We provide our own lstat/fstat functions, since the provided
940-
* lstat/fstat functions are so slow. These stat functions are
941-
* tailored for Git's usage (read: fast), and are not meant to be
942-
* complete. Note that Git stat()s are redirected to mingw_lstat()
943-
* too, since Windows doesn't really handle symlinks that well.
944-
*/
945-
static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
946-
{
947-
int namelen;
948-
char alt_name[MAX_LONG_PATH];
949-
950-
if (!do_lstat(follow, file_name, buf))
951-
return 0;
952-
953-
/* if file_name ended in a '/', Windows returned ENOENT;
954-
* try again without trailing slashes
955-
*/
956-
if (errno != ENOENT)
957-
return -1;
958-
959-
namelen = strlen(file_name);
960-
if (namelen && file_name[namelen-1] != '/')
961-
return -1;
962-
while (namelen && file_name[namelen-1] == '/')
963-
--namelen;
964-
if (!namelen || namelen >= MAX_LONG_PATH)
965-
return -1;
966-
967-
memcpy(alt_name, file_name, namelen);
968-
alt_name[namelen] = 0;
969-
return do_lstat(follow, alt_name, buf);
970-
}
971-
972948
int (*lstat)(const char *file_name, struct stat *buf) = mingw_lstat;
973949

974950
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -996,11 +972,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
996972

997973
int mingw_lstat(const char *file_name, struct stat *buf)
998974
{
999-
return do_stat_internal(0, file_name, buf);
975+
return do_lstat(0, file_name, buf);
1000976
}
1001977
int mingw_stat(const char *file_name, struct stat *buf)
1002978
{
1003-
return do_stat_internal(1, file_name, buf);
979+
return do_lstat(1, file_name, buf);
1004980
}
1005981

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

0 commit comments

Comments
 (0)