Skip to content

Commit 20e81f0

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 05512a4 commit 20e81f0

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
@@ -861,6 +861,7 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
861861
int mingw_lstat(const char *file_name, struct stat *buf)
862862
{
863863
WIN32_FILE_ATTRIBUTE_DATA fdata;
864+
WIN32_FIND_DATAW findbuf = { 0 };
864865
wchar_t wfilename[MAX_LONG_PATH];
865866
int wlen = xutftowcs_long_path(wfilename, file_name);
866867
if (wlen < 0)
@@ -875,6 +876,13 @@ int mingw_lstat(const char *file_name, struct stat *buf)
875876
}
876877

877878
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
879+
/* for reparse points, use FindFirstFile to get the reparse tag */
880+
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
881+
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
882+
if (handle == INVALID_HANDLE_VALUE)
883+
goto error;
884+
FindClose(handle);
885+
}
878886
buf->st_ino = 0;
879887
buf->st_gid = 0;
880888
buf->st_uid = 0;
@@ -887,20 +895,16 @@ int mingw_lstat(const char *file_name, struct stat *buf)
887895
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
888896
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
889897
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
890-
WIN32_FIND_DATAW findbuf;
891-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
892-
if (handle != INVALID_HANDLE_VALUE) {
893898
if ((findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
894899
(findbuf.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) {
895900
buf->st_mode = S_IFLNK | S_IREAD;
896901
if (!(findbuf.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
897902
buf->st_mode |= S_IWRITE;
898903
}
899-
FindClose(handle);
900-
}
901904
}
902905
return 0;
903906
}
907+
error:
904908
switch (GetLastError()) {
905909
case ERROR_ACCESS_DENIED:
906910
case ERROR_SHARING_VIOLATION:

0 commit comments

Comments
 (0)