Skip to content

Commit 1c11c30

Browse files
rimrulGit for Windows Build Agent
authored and
Git for Windows Build Agent
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 3626735 commit 1c11c30

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
@@ -588,13 +588,24 @@ static int mingw_open_existing(const wchar_t *filename, int oflags, ...)
588588
&security_attributes, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
589589
if (handle == INVALID_HANDLE_VALUE) {
590590
DWORD err = GetLastError();
591+
if (err == ERROR_ACCESS_DENIED) {
592+
DWORD attrs = GetFileAttributesW(filename);
593+
if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY))
594+
handle = CreateFileW(filename, access,
595+
FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
596+
&security_attributes, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL| FILE_FLAG_BACKUP_SEMANTICS, NULL);
597+
}
591598

592-
/* See `mingw_open_append()` for why we have this conversion. */
593-
if (err == ERROR_INVALID_PARAMETER)
594-
err = ERROR_PATH_NOT_FOUND;
599+
if (handle == INVALID_HANDLE_VALUE) {
600+
err = GetLastError();
595601

596-
errno = err_win_to_posix(err);
597-
return -1;
602+
/* See `mingw_open_append()` for why we have this conversion. */
603+
if (err == ERROR_INVALID_PARAMETER)
604+
err = ERROR_PATH_NOT_FOUND;
605+
606+
errno = err_win_to_posix(err);
607+
return -1;
608+
}
598609
}
599610

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

0 commit comments

Comments
 (0)