Skip to content

Commit bd1d088

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 512490f commit bd1d088

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

882891
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
883892
buf->st_ino = 0;
@@ -938,39 +947,6 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
938947
return -1;
939948
}
940949

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

976952
static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
@@ -998,11 +974,11 @@ static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
998974

999975
int mingw_lstat(const char *file_name, struct stat *buf)
1000976
{
1001-
return do_stat_internal(0, file_name, buf);
977+
return do_lstat(0, file_name, buf);
1002978
}
1003979
int mingw_stat(const char *file_name, struct stat *buf)
1004980
{
1005-
return do_stat_internal(1, file_name, buf);
981+
return do_lstat(1, file_name, buf);
1006982
}
1007983

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

0 commit comments

Comments
 (0)