Skip to content

Commit 42b384f

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 8edc0ee + eeb13a8 commit 42b384f

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

compat/mingw.c

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

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

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

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

0 commit comments

Comments
 (0)