Skip to content

Commit bc2f9ed

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 914992e commit bc2f9ed

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
@@ -935,6 +935,7 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
935935
int mingw_lstat(const char *file_name, struct stat *buf)
936936
{
937937
WIN32_FILE_ATTRIBUTE_DATA fdata;
938+
WIN32_FIND_DATAW findbuf = { 0 };
938939
wchar_t wfilename[MAX_LONG_PATH];
939940
int wlen = xutftowcs_long_path(wfilename, file_name);
940941
if (wlen < 0)
@@ -949,6 +950,13 @@ int mingw_lstat(const char *file_name, struct stat *buf)
949950
}
950951

951952
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
953+
/* for reparse points, use FindFirstFile to get the reparse tag */
954+
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
955+
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
956+
if (handle == INVALID_HANDLE_VALUE)
957+
goto error;
958+
FindClose(handle);
959+
}
952960
buf->st_ino = 0;
953961
buf->st_gid = 0;
954962
buf->st_uid = 0;
@@ -961,20 +969,16 @@ int mingw_lstat(const char *file_name, struct stat *buf)
961969
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
962970
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
963971
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
964-
WIN32_FIND_DATAW findbuf;
965-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
966-
if (handle != INVALID_HANDLE_VALUE) {
967972
if ((findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
968973
(findbuf.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) {
969974
buf->st_mode = S_IFLNK | S_IREAD;
970975
if (!(findbuf.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
971976
buf->st_mode |= S_IWRITE;
972977
}
973-
FindClose(handle);
974-
}
975978
}
976979
return 0;
977980
}
981+
error:
978982
switch (GetLastError()) {
979983
case ERROR_ACCESS_DENIED:
980984
case ERROR_SHARING_VIOLATION:

0 commit comments

Comments
 (0)