Skip to content

Commit 78d52c8

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 33ccd0f commit 78d52c8

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
@@ -629,6 +629,7 @@ int mingw_open (const char *filename, int oflags, ...)
629629
int fd, create = (oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL);
630630
wchar_t wfilename[MAX_PATH];
631631
open_fn_t open_fn;
632+
WIN32_FILE_ATTRIBUTE_DATA fdata;
632633

633634
DECLARE_PROC_ADDR(ntdll.dll, NTSTATUS, NTAPI, RtlGetLastNtStatus, void);
634635

@@ -653,6 +654,19 @@ int mingw_open (const char *filename, int oflags, ...)
653654
else if (xutftowcs_path(wfilename, filename) < 0)
654655
return -1;
655656

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+
656670
fd = open_fn(wfilename, oflags, mode);
657671

658672
/*

0 commit comments

Comments
 (0)