git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Matthias Aßhauer via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: "Johannes Schindelin" <johannes.schindelin@gmx.de>,
	"Matthias Aßhauer" <mha1993@live.de>
Subject: [PATCH 1/4] mingw_open_existing: handle directories better
Date: Sun, 03 Aug 2025 21:25:15 +0000	[thread overview]
Message-ID: <0995ecadaff56d2ff44c965763800ec892490bad.1754256318.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1948.git.1754256318.gitgitgadget@gmail.com>

From: =?UTF-8?q?Matthias=20A=C3=9Fhauer?= <mha1993@live.de>

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>
---
 compat/mingw.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/compat/mingw.c b/compat/mingw.c
index 5d69ae32f4b9..2dd5cbcaee0d 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);
-- 
gitgitgadget


  reply	other threads:[~2025-08-03 21:25 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-03 21:25 [PATCH 0/4] mingw: rename and open fixes Johannes Schindelin via GitGitGadget
2025-08-03 21:25 ` Matthias Aßhauer via GitGitGadget [this message]
2025-08-03 21:25 ` [PATCH 2/4] mingw: drop Windows 7-specific work-around Johannes Schindelin via GitGitGadget
2025-08-04  8:54   ` Oswald Buddenhagen
2025-08-03 21:25 ` [PATCH 3/4] mingw_rename: support ReFS on Windows 2022 Johannes Schindelin via GitGitGadget
2025-08-03 21:25 ` [PATCH 4/4] mingw: support Windows Server 2016 again Johannes Schindelin via GitGitGadget
2025-08-04  1:29 ` [PATCH 0/4] mingw: rename and open fixes Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0995ecadaff56d2ff44c965763800ec892490bad.1754256318.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=johannes.schindelin@gmx.de \
    --cc=mha1993@live.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).