mingw: special-case `open(symlink, O_CREAT | O_EXCL)`

The `_wopen()` function would gladly follow a symbolic link to a non-existent file and create it when given above-mentioned flags. Git expects the `open()` call to fail, though. So let's add yet another work-around to pretend that Windows behaves according to POSIX, see: https://pubs.opengroup.org/onlinepubs/007904875/functions/open.html#:~:text=If%20O_CREAT%20and%20O_EXCL%20are,set%2C%20the%20result%20is%20undefined. This is required to let t4115.8(--reject removes .rej symlink if it exists) pass on Windows when enabling the MSYS2 runtime's symbolic link support. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Dec 17, 2025 at 14:18 UTC 6fa50cc4a1979fb8a2f77a026e307d6336a09172
1 file changed +14
compat/mingw.c
+14
@@ -629,6 +629,7 @@ int mingw_open (const char *filename, int oflags, ...)
629 int fd, create = (oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL);
630 wchar_t wfilename[MAX_PATH];
631 open_fn_t open_fn;
632 + WIN32_FILE_ATTRIBUTE_DATA fdata;
633
634 DECLARE_PROC_ADDR(ntdll.dll, NTSTATUS, NTAPI, RtlGetLastNtStatus, void);
635
@@ -653,6 +654,19 @@ int mingw_open (const char *filename, int oflags, ...)
654 else if (xutftowcs_path(wfilename, filename) < 0)
655 return -1;
656
657 + /*
658 + * When `symlink` exists and is a symbolic link pointing to a
659 + * non-existing file, `_wopen(symlink, O_CREAT | O_EXCL)` would
660 + * create that file. Not what we want: Linux would say `EEXIST`
661 + * in that instance, which is therefore what Git expects.
662 + */
663 + if (create &&
664 + GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata) &&
665 + (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
666 + errno = EEXIST;
667 + return -1;
668 + }
669 +
670 fd = open_fn(wfilename, oflags, mode);
671
672 /*