Skip to content

Commit 544f6e8

Browse files
kbleesvdye
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 dc7ba21 commit 544f6e8

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
@@ -790,6 +790,7 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
790790
int mingw_lstat(const char *file_name, struct stat *buf)
791791
{
792792
WIN32_FILE_ATTRIBUTE_DATA fdata;
793+
WIN32_FIND_DATAW findbuf = { 0 };
793794
wchar_t wfilename[MAX_LONG_PATH];
794795
int wlen = xutftowcs_long_path(wfilename, file_name);
795796
if (wlen < 0)
@@ -804,6 +805,13 @@ int mingw_lstat(const char *file_name, struct stat *buf)
804805
}
805806

806807
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
808+
/* for reparse points, use FindFirstFile to get the reparse tag */
809+
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
810+
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
811+
if (handle == INVALID_HANDLE_VALUE)
812+
goto error;
813+
FindClose(handle);
814+
}
807815
buf->st_ino = 0;
808816
buf->st_gid = 0;
809817
buf->st_uid = 0;
@@ -816,20 +824,16 @@ int mingw_lstat(const char *file_name, struct stat *buf)
816824
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
817825
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
818826
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
819-
WIN32_FIND_DATAW findbuf;
820-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
821-
if (handle != INVALID_HANDLE_VALUE) {
822827
if ((findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
823828
(findbuf.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) {
824829
buf->st_mode = S_IFLNK | S_IREAD;
825830
if (!(findbuf.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
826831
buf->st_mode |= S_IWRITE;
827832
}
828-
FindClose(handle);
829-
}
830833
}
831834
return 0;
832835
}
836+
error:
833837
switch (GetLastError()) {
834838
case ERROR_ACCESS_DENIED:
835839
case ERROR_SHARING_VIOLATION:

0 commit comments

Comments
 (0)