Skip to content

Commit 1f68a74

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 20fd2bd commit 1f68a74

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
@@ -820,6 +820,7 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
820820
int mingw_lstat(const char *file_name, struct stat *buf)
821821
{
822822
WIN32_FILE_ATTRIBUTE_DATA fdata;
823+
WIN32_FIND_DATAW findbuf = { 0 };
823824
wchar_t wfilename[MAX_LONG_PATH];
824825
int wlen = xutftowcs_long_path(wfilename, file_name);
825826
if (wlen < 0)
@@ -834,6 +835,13 @@ int mingw_lstat(const char *file_name, struct stat *buf)
834835
}
835836

836837
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
838+
/* for reparse points, use FindFirstFile to get the reparse tag */
839+
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
840+
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
841+
if (handle == INVALID_HANDLE_VALUE)
842+
goto error;
843+
FindClose(handle);
844+
}
837845
buf->st_ino = 0;
838846
buf->st_gid = 0;
839847
buf->st_uid = 0;
@@ -846,20 +854,16 @@ int mingw_lstat(const char *file_name, struct stat *buf)
846854
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
847855
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
848856
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
849-
WIN32_FIND_DATAW findbuf;
850-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
851-
if (handle != INVALID_HANDLE_VALUE) {
852857
if ((findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
853858
(findbuf.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) {
854859
buf->st_mode = S_IFLNK | S_IREAD;
855860
if (!(findbuf.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
856861
buf->st_mode |= S_IWRITE;
857862
}
858-
FindClose(handle);
859-
}
860863
}
861864
return 0;
862865
}
866+
error:
863867
switch (GetLastError()) {
864868
case ERROR_ACCESS_DENIED:
865869
case ERROR_SHARING_VIOLATION:

0 commit comments

Comments
 (0)