aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Aßhauer <mha1993@live.de>2025-08-03 21:25:15 +0000
committerJunio C Hamano <gitster@pobox.com>2025-08-03 18:30:38 -0700
commit5f277fc5f25dc0fb54aa42bad62c23325bab9200 (patch)
tree3e834ad6b959900292b290c12fdf326f4ca45911
parentThe sixteenth batch (diff)
downloadgit-5f277fc5f25dc0fb54aa42bad62c23325bab9200.tar.gz
git-5f277fc5f25dc0fb54aa42bad62c23325bab9200.zip
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 https://github.com/git-for-windows/git/issues/5068 Signed-off-by: Matthias Aßhauer <mha1993@live.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--compat/mingw.c21
1 files changed, 16 insertions, 5 deletions
diff --git a/compat/mingw.c b/compat/mingw.c
index 5d69ae32f4..2dd5cbcaee 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -588,13 +588,24 @@ static int mingw_open_existing(const wchar_t *filename, int oflags, ...)
&security_attributes, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (handle == INVALID_HANDLE_VALUE) {
DWORD err = GetLastError();
+ if (err == ERROR_ACCESS_DENIED) {
+ DWORD attrs = GetFileAttributesW(filename);
+ if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY))
+ handle = CreateFileW(filename, access,
+ FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
+ &security_attributes, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL| FILE_FLAG_BACKUP_SEMANTICS, NULL);
+ }
- /* See `mingw_open_append()` for why we have this conversion. */
- if (err == ERROR_INVALID_PARAMETER)
- err = ERROR_PATH_NOT_FOUND;
+ if (handle == INVALID_HANDLE_VALUE) {
+ err = GetLastError();
- errno = err_win_to_posix(err);
- return -1;
+ /* See `mingw_open_append()` for why we have this conversion. */
+ if (err == ERROR_INVALID_PARAMETER)
+ err = ERROR_PATH_NOT_FOUND;
+
+ errno = err_win_to_posix(err);
+ return -1;
+ }
}
fd = _open_osfhandle((intptr_t)handle, oflags | O_BINARY);