Skip to content

Commit 35f24cc

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 da68ec6 commit 35f24cc

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

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

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

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

0 commit comments

Comments
 (0)