Skip to content

Commit c19467a

Browse files
committed
mingw_open_existing: handle directories better (#5342)
[`CreateFileW()` requires `FILE_FLAG_BACKUP_SEMANTICS` to create a directory handle](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew#directories) and errors out with `ERROR_ACCESS_DENIED` without this flag. Fall back to accessing Directory handles this way. This fixes #5068
2 parents ed88613 + e5585c4 commit c19467a

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

compat/mingw.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -590,13 +590,24 @@ static int mingw_open_existing(const wchar_t *filename, int oflags, ...)
590590
&security_attributes, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
591591
if (handle == INVALID_HANDLE_VALUE) {
592592
DWORD err = GetLastError();
593+
if (err == ERROR_ACCESS_DENIED) {
594+
DWORD attrs = GetFileAttributesW(filename);
595+
if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY))
596+
handle = CreateFileW(filename, access,
597+
FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
598+
&security_attributes, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL| FILE_FLAG_BACKUP_SEMANTICS, NULL);
599+
}
593600

594-
/* See `mingw_open_append()` for why we have this conversion. */
595-
if (err == ERROR_INVALID_PARAMETER)
596-
err = ERROR_PATH_NOT_FOUND;
601+
if (handle == INVALID_HANDLE_VALUE) {
602+
err = GetLastError();
597603

598-
errno = err_win_to_posix(err);
599-
return -1;
604+
/* See `mingw_open_append()` for why we have this conversion. */
605+
if (err == ERROR_INVALID_PARAMETER)
606+
err = ERROR_PATH_NOT_FOUND;
607+
608+
errno = err_win_to_posix(err);
609+
return -1;
610+
}
600611
}
601612

602613
fd = _open_osfhandle((intptr_t)handle, oflags | O_BINARY);

0 commit comments

Comments
 (0)