Skip to content

Commit a9e9fbd

Browse files
dschoGit for Windows Build Agent
authored and
Git for Windows Build Agent
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 3739978 + a990786 commit a9e9fbd

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
@@ -584,13 +584,24 @@ static int mingw_open_existing(const wchar_t *filename, int oflags, ...)
584584
&security_attributes, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
585585
if (handle == INVALID_HANDLE_VALUE) {
586586
DWORD err = GetLastError();
587+
if (err == ERROR_ACCESS_DENIED) {
588+
DWORD attrs = GetFileAttributesW(filename);
589+
if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY))
590+
handle = CreateFileW(filename, access,
591+
FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
592+
&security_attributes, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL| FILE_FLAG_BACKUP_SEMANTICS, NULL);
593+
}
587594

588-
/* See `mingw_open_append()` for why we have this conversion. */
589-
if (err == ERROR_INVALID_PARAMETER)
590-
err = ERROR_PATH_NOT_FOUND;
595+
if (handle == INVALID_HANDLE_VALUE) {
596+
err = GetLastError();
591597

592-
errno = err_win_to_posix(err);
593-
return -1;
598+
/* See `mingw_open_append()` for why we have this conversion. */
599+
if (err == ERROR_INVALID_PARAMETER)
600+
err = ERROR_PATH_NOT_FOUND;
601+
602+
errno = err_win_to_posix(err);
603+
return -1;
604+
}
594605
}
595606

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

0 commit comments

Comments
 (0)