SyncDlg: Enable more options for conflict view
[TortoiseGit.git] / ext / libgit2-0007-Use-CreateFile-instead-of-_wopen.patch
blob84d646394c48ee381549518e719acf23de783dd6
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>
7 ---
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
15 @@ -26,15 +26,6 @@
16 #define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
17 #endif
19 -/* Options which we always provide to _wopen.
20 - *
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, ...)
34 git_win32_path buf;
35 - mode_t mode = 0;
36 + HANDLE handle;
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)
43 return -1;
45 if (flags & O_CREAT) {
46 va_list arg_list;
47 + mode_t mode = 0;
49 va_start(arg_list, flags);
50 mode = (mode_t)va_arg(arg_list, int);
51 va_end(arg_list);
53 + if (!(mode & _S_IWRITE))
54 + flags_and_attributes = FILE_ATTRIBUTE_READONLY;
55 + }
57 + /* we only support _O_BINARY and set this by default */
58 + if (flags & (_O_TEXT | _O_WTEXT | _O_U16TEXT | _O_U8TEXT)) {
59 + errno = EINVAL;
60 + return -1;
61 + }
63 + /* currently unsupported */
64 + if (flags & (_O_SEQUENTIAL | _O_RANDOM | _O_TEMPORARY | _O_SHORT_LIVED)) {
65 + errno = EINVAL;
66 + return -1;
67 + }
69 + switch (flags & (_O_RDONLY | _O_WRONLY | _O_RDWR | _O_APPEND)) {
70 + case _O_RDONLY:
71 + desired_access = GENERIC_READ;
72 + osfhandle_flags = _O_RDONLY;
73 + break;
74 + case _O_WRONLY | _O_APPEND:
75 + osfhandle_flags = _O_APPEND;
76 + case _O_WRONLY:
77 + desired_access = GENERIC_WRITE;
78 + break;
79 + case _O_RDWR | _O_APPEND:
80 + osfhandle_flags = _O_APPEND;
81 + case _O_RDWR:
82 + desired_access = GENERIC_READ | GENERIC_WRITE;
83 + break;
84 + default:
85 + /* invalid or unsupported flag (combination) */
86 + errno = EINVAL;
87 + return -1;
88 + }
90 + switch (flags & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
91 + case 0:
92 + creation_disposition = OPEN_EXISTING;
93 + break;
95 + case _O_CREAT:
96 + creation_disposition = OPEN_ALWAYS;
97 + break;
99 + case _O_CREAT | _O_TRUNC:
100 + creation_disposition = CREATE_ALWAYS;
101 + break;
103 + case _O_CREAT | _O_EXCL:
104 + case _O_CREAT | _O_TRUNC | _O_EXCL:
105 + creation_disposition = CREATE_NEW;
106 + break;
108 + case _O_TRUNC:
109 + creation_disposition = TRUNCATE_EXISTING;
110 + break;
112 + default:
113 + /* _O_EXCL required O_CREAT to be set, IIRC this is no error in MSVC implementaton */
114 + errno = EINVAL;
115 + return -1;
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());
122 + return -1;
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)
131 2.11.0.windows.1