Skip to content

Commit fae6b7a

Browse files
committed
mingw_open_existing: handle directories better
CreateFileW() requires FILE_FLAG_BACKUP_SEMANTICS to create a directory handle [1] and errors out with ERROR_ACCESS_DENIED without this flag. Fall back to accessing Directory handles this way. [1] https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew#directories This fixes #5068 Signed-off-by: Matthias Aßhauer <[email protected]>
1 parent 58d6792 commit fae6b7a

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
@@ -810,13 +810,24 @@ static int mingw_open_existing(const wchar_t *filename, int oflags, ...)
810810
&security_attributes, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
811811
if (handle == INVALID_HANDLE_VALUE) {
812812
DWORD err = GetLastError();
813+
if (err == ERROR_ACCESS_DENIED) {
814+
DWORD attrs = GetFileAttributesW(filename);
815+
if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY))
816+
handle = CreateFileW(filename, access,
817+
FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
818+
&security_attributes, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL| FILE_FLAG_BACKUP_SEMANTICS, NULL);
819+
}
813820

814-
/* See `mingw_open_append()` for why we have this conversion. */
815-
if (err == ERROR_INVALID_PARAMETER)
816-
err = ERROR_PATH_NOT_FOUND;
821+
if (handle == INVALID_HANDLE_VALUE) {
822+
err = GetLastError();
817823

818-
errno = err_win_to_posix(err);
819-
return -1;
824+
/* See `mingw_open_append()` for why we have this conversion. */
825+
if (err == ERROR_INVALID_PARAMETER)
826+
err = ERROR_PATH_NOT_FOUND;
827+
828+
errno = err_win_to_posix(err);
829+
return -1;
830+
}
820831
}
821832

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

0 commit comments

Comments
 (0)