Skip to content

Commit 808ffc6

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 04db60e commit 808ffc6

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

586-
/* See `mingw_open_append()` for why we have this conversion. */
587-
if (err == ERROR_INVALID_PARAMETER)
588-
err = ERROR_PATH_NOT_FOUND;
593+
if (handle == INVALID_HANDLE_VALUE) {
594+
err = GetLastError();
589595

590-
errno = err_win_to_posix(err);
591-
return -1;
596+
/* See `mingw_open_append()` for why we have this conversion. */
597+
if (err == ERROR_INVALID_PARAMETER)
598+
err = ERROR_PATH_NOT_FOUND;
599+
600+
errno = err_win_to_posix(err);
601+
return -1;
602+
}
592603
}
593604

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

0 commit comments

Comments
 (0)