Skip to content

Commit 83e3509

Browse files
kbleesdscho
authored andcommitted
Win32: let mingw_lstat() error early upon problems with reparse points
When obtaining lstat information for reparse points, we need to call FindFirstFile() in addition to GetFileInformationEx() to obtain the type of the reparse point (symlink, mount point etc.). However, currently there is no error handling whatsoever if FindFirstFile() fails. Call FindFirstFile() before modifying the stat *buf output parameter and error out if the call fails. Note: The FindFirstFile() return value includes all the data that we get from GetFileAttributesEx(), so we could replace GetFileAttributesEx() with FindFirstFile(). We don't do that because GetFileAttributesEx() is about twice as fast for single files. I.e. we only pay the extra cost of calling FindFirstFile() in the rare case that we encounter a reparse point. Note: The indentation of the remaining reparse point code will be fixed in the next patch. Signed-off-by: Karsten Blees <[email protected]>
1 parent 02c9d3c commit 83e3509

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

compat/mingw.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
866866
int mingw_lstat(const char *file_name, struct stat *buf)
867867
{
868868
WIN32_FILE_ATTRIBUTE_DATA fdata;
869+
WIN32_FIND_DATAW findbuf = { 0 };
869870
wchar_t wfilename[MAX_LONG_PATH];
870871
int wlen = xutftowcs_long_path(wfilename, file_name);
871872
if (wlen < 0)
@@ -880,6 +881,13 @@ int mingw_lstat(const char *file_name, struct stat *buf)
880881
}
881882

882883
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
884+
/* for reparse points, use FindFirstFile to get the reparse tag */
885+
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
886+
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
887+
if (handle == INVALID_HANDLE_VALUE)
888+
goto error;
889+
FindClose(handle);
890+
}
883891
buf->st_ino = 0;
884892
buf->st_gid = 0;
885893
buf->st_uid = 0;
@@ -892,20 +900,16 @@ int mingw_lstat(const char *file_name, struct stat *buf)
892900
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
893901
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
894902
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
895-
WIN32_FIND_DATAW findbuf;
896-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
897-
if (handle != INVALID_HANDLE_VALUE) {
898903
if ((findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
899904
(findbuf.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) {
900905
buf->st_mode = S_IFLNK | S_IREAD;
901906
if (!(findbuf.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
902907
buf->st_mode |= S_IWRITE;
903908
}
904-
FindClose(handle);
905-
}
906909
}
907910
return 0;
908911
}
912+
error:
909913
switch (GetLastError()) {
910914
case ERROR_ACCESS_DENIED:
911915
case ERROR_SHARING_VIOLATION:

0 commit comments

Comments
 (0)