Skip to content

Commit 42bdd9f

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 c63236b commit 42bdd9f

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
@@ -709,6 +709,7 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
709709
int mingw_lstat(const char *file_name, struct stat *buf)
710710
{
711711
WIN32_FILE_ATTRIBUTE_DATA fdata;
712+
WIN32_FIND_DATAW findbuf = { 0 };
712713
wchar_t wfilename[MAX_LONG_PATH];
713714
int wlen = xutftowcs_long_path(wfilename, file_name);
714715
if (wlen < 0)
@@ -723,6 +724,13 @@ int mingw_lstat(const char *file_name, struct stat *buf)
723724
}
724725

725726
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
727+
/* for reparse points, use FindFirstFile to get the reparse tag */
728+
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
729+
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
730+
if (handle == INVALID_HANDLE_VALUE)
731+
goto error;
732+
FindClose(handle);
733+
}
726734
buf->st_ino = 0;
727735
buf->st_gid = 0;
728736
buf->st_uid = 0;
@@ -735,20 +743,16 @@ int mingw_lstat(const char *file_name, struct stat *buf)
735743
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
736744
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
737745
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
738-
WIN32_FIND_DATAW findbuf;
739-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
740-
if (handle != INVALID_HANDLE_VALUE) {
741746
if ((findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
742747
(findbuf.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) {
743748
buf->st_mode = S_IFLNK | S_IREAD;
744749
if (!(findbuf.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
745750
buf->st_mode |= S_IWRITE;
746751
}
747-
FindClose(handle);
748-
}
749752
}
750753
return 0;
751754
}
755+
error:
752756
switch (GetLastError()) {
753757
case ERROR_ACCESS_DENIED:
754758
case ERROR_SHARING_VIOLATION:

0 commit comments

Comments
 (0)