1 From 5dcf147039be35b2e2443e3917c5ab0c3c2b3eb0 Mon Sep 17 00:00:00 2001
2 From: Sven Strickroth <email@cs-ware.de>
3 Date: Sat, 14 Jan 2017 18:20:59 +0100
4 Subject: [PATCH 2/4] Use CreateFile instead of _wopen
6 Signed-off-by: Sven Strickroth <email@cs-ware.de>
8 src/win32/posix_w32.c | 89 ++++++++++++++++++++++++++++++++++++++++++++-------
9 1 file changed, 78 insertions(+), 11 deletions(-)
11 diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
12 index 8c1aa7840..a20ae9b2f 100644
13 --- a/src/win32/posix_w32.c
14 +++ b/src/win32/posix_w32.c
16 #define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
19 -/* Options which we always provide to _wopen.
21 - * _O_BINARY - Raw access; no translation of CR or LF characters
22 - * _O_NOINHERIT - Do not mark the created handle as inheritable by child processes.
23 - * The Windows default is 'not inheritable', but the CRT's default (following
24 - * POSIX convention) is 'inheritable'. We have no desire for our handles to be
25 - * inheritable on Windows, so specify the flag to get default behavior back. */
26 -#define STANDARD_OPEN_FLAGS (_O_BINARY | _O_NOINHERIT)
28 /* Allowable mode bits on Win32. Using mode bits that are not supported on
29 * Win32 (eg S_IRWXU) is generally ignored, but Wine warns loudly about it
30 * so we simply remove them.
31 @@ -283,20 +274,96 @@ int p_symlink(const char *old, const char *new)
32 int p_open(const char *path, int flags, ...)
37 + DWORD desired_access;
38 + DWORD creation_disposition;
39 + DWORD flags_and_attributes = FILE_ATTRIBUTE_NORMAL;
40 + int osfhandle_flags = 0;
42 if (git_win32_path_from_utf8(buf, path) < 0)
45 if (flags & O_CREAT) {
49 va_start(arg_list, flags);
50 mode = (mode_t)va_arg(arg_list, int);
53 + if (!(mode & _S_IWRITE))
54 + flags_and_attributes = FILE_ATTRIBUTE_READONLY;
57 + /* we only support _O_BINARY and set this by default */
58 + if (flags & (_O_TEXT | _O_WTEXT | _O_U16TEXT | _O_U8TEXT)) {
63 + /* currently unsupported */
64 + if (flags & (_O_SEQUENTIAL | _O_RANDOM | _O_TEMPORARY | _O_SHORT_LIVED)) {
69 + switch (flags & (_O_RDONLY | _O_WRONLY | _O_RDWR | _O_APPEND)) {
71 + desired_access = GENERIC_READ;
72 + osfhandle_flags = _O_RDONLY;
74 + case _O_WRONLY | _O_APPEND:
75 + osfhandle_flags = _O_APPEND;
77 + desired_access = GENERIC_WRITE;
79 + case _O_RDWR | _O_APPEND:
80 + osfhandle_flags = _O_APPEND;
82 + desired_access = GENERIC_READ | GENERIC_WRITE;
85 + /* invalid or unsupported flag (combination) */
90 + switch (flags & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
92 + creation_disposition = OPEN_EXISTING;
96 + creation_disposition = OPEN_ALWAYS;
99 + case _O_CREAT | _O_TRUNC:
100 + creation_disposition = CREATE_ALWAYS;
103 + case _O_CREAT | _O_EXCL:
104 + case _O_CREAT | _O_TRUNC | _O_EXCL:
105 + creation_disposition = CREATE_NEW;
109 + creation_disposition = TRUNCATE_EXISTING;
113 + /* _O_EXCL required O_CREAT to be set, IIRC this is no error in MSVC implementaton */
118 + handle = CreateFileW(buf, desired_access, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, creation_disposition, flags_and_attributes, 0);
119 + if (handle == INVALID_HANDLE_VALUE)
121 + _dosmaperr(GetLastError());
125 - return _wopen(buf, flags | STANDARD_OPEN_FLAGS, mode & WIN32_MODE_MASK);
126 + return _open_osfhandle((intptr_t)handle, osfhandle_flags);
129 int p_creat(const char *path, mode_t mode)