Skip to content

Commit ebb9cf3

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 60923a4 commit ebb9cf3

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
@@ -539,6 +539,7 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
539539
int mingw_lstat(const char *file_name, struct stat *buf)
540540
{
541541
WIN32_FILE_ATTRIBUTE_DATA fdata;
542+
WIN32_FIND_DATAW findbuf;
542543
wchar_t wfilename[MAX_LONG_PATH];
543544
int wlen = xutftowcs_long_path(wfilename, file_name);
544545
if (wlen < 0)
@@ -553,6 +554,13 @@ int mingw_lstat(const char *file_name, struct stat *buf)
553554
}
554555

555556
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
557+
/* for reparse points, use FindFirstFile to get the reparse tag */
558+
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
559+
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
560+
if (handle == INVALID_HANDLE_VALUE)
561+
goto error;
562+
FindClose(handle);
563+
}
556564
buf->st_ino = 0;
557565
buf->st_gid = 0;
558566
buf->st_uid = 0;
@@ -565,20 +573,16 @@ int mingw_lstat(const char *file_name, struct stat *buf)
565573
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
566574
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
567575
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
568-
WIN32_FIND_DATAW findbuf;
569-
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
570-
if (handle != INVALID_HANDLE_VALUE) {
571576
if ((findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
572577
(findbuf.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) {
573578
buf->st_mode = S_IFLNK | S_IREAD;
574579
if (!(findbuf.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
575580
buf->st_mode |= S_IWRITE;
576581
}
577-
FindClose(handle);
578-
}
579582
}
580583
return 0;
581584
}
585+
error:
582586
switch (GetLastError()) {
583587
case ERROR_ACCESS_DENIED:
584588
case ERROR_SHARING_VIOLATION:

0 commit comments

Comments
 (0)