Skip to content

Commit 89302a1

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 fd37175 commit 89302a1

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
@@ -663,6 +663,7 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
663663
int mingw_lstat(const char *file_name, struct stat *buf)
664664
{
665665
WIN32_FILE_ATTRIBUTE_DATA fdata;
666+
WIN32_FIND_DATAW findbuf = { 0 };
666667
wchar_t wfilename[MAX_LONG_PATH];
667668
int wlen = xutftowcs_long_path(wfilename, file_name);
668669
if (wlen < 0)
@@ -677,6 +678,13 @@ int mingw_lstat(const char *file_name, struct stat *buf)
677678
}
678679

679680
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
681+
/* for reparse points, use FindFirstFile to get the reparse tag */
682+
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
683+
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
684+
if (handle == INVALID_HANDLE_VALUE)
685+
goto error;
686+
FindClose(handle);
687+
}
680688
buf->st_ino = 0;
681689
buf->st_gid = 0;
682690
buf->st_uid = 0;
@@ -689,20 +697,16 @@ int mingw_lstat(const char *file_name, struct stat *buf)
689697
filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
690698
filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
691699
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
692-
WIN32_FIND_DATAW findbuf;
693-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
694-
if (handle != INVALID_HANDLE_VALUE) {
695700
if ((findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
696701
(findbuf.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) {
697702
buf->st_mode = S_IFLNK | S_IREAD;
698703
if (!(findbuf.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
699704
buf->st_mode |= S_IWRITE;
700705
}
701-
FindClose(handle);
702-
}
703706
}
704707
return 0;
705708
}
709+
error:
706710
switch (GetLastError()) {
707711
case ERROR_ACCESS_DENIED:
708712
case ERROR_SHARING_VIOLATION:

0 commit comments

Comments
 (0)