Skip to content

Commit 96e279f

Browse files
committed
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 like Linux. 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>
1 parent b97afa9 commit 96e279f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

compat/mingw.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ int mingw_open (const char *filename, int oflags, ...)
627627
int fd, create = (oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL);
628628
wchar_t wfilename[MAX_PATH];
629629
open_fn_t open_fn;
630+
WIN32_FILE_ATTRIBUTE_DATA fdata;
630631

631632
DECLARE_PROC_ADDR(ntdll.dll, NTSTATUS, NTAPI, RtlGetLastNtStatus, void);
632633

@@ -651,6 +652,19 @@ int mingw_open (const char *filename, int oflags, ...)
651652
else if (xutftowcs_path(wfilename, filename) < 0)
652653
return -1;
653654

655+
/*
656+
* When `symlink` exists and is a symbolic link pointing to a
657+
* non-existing file, `_wopen(symlink, O_CREAT | O_EXCL)` would
658+
* create that file. Not what we want: Linux would say `EEXIST`
659+
* in that instance, which is therefore what Git expects.
660+
*/
661+
if (create &&
662+
GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata) &&
663+
(fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
664+
errno = EEXIST;
665+
return -1;
666+
}
667+
654668
fd = open_fn(wfilename, oflags, mode);
655669

656670
/*

0 commit comments

Comments
 (0)