Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 1 | #include "../git-compat-util.h" |
Dmitry Potapov | 444dc90 | 2008-09-27 12:43:01 +0400 | [diff] [blame] | 2 | #include "win32.h" |
Johannes Schindelin | bdc77d1 | 2022-03-02 11:06:24 +0100 | [diff] [blame] | 3 | #include <aclapi.h> |
Johannes Schindelin | e883e04 | 2022-08-08 13:27:48 +0000 | [diff] [blame] | 4 | #include <sddl.h> |
Johannes Schindelin | 28a559c | 2009-06-01 08:41:45 +0200 | [diff] [blame] | 5 | #include <conio.h> |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 6 | #include <wchar.h> |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 7 | #include "../strbuf.h" |
Heiko Voigt | c9b7840 | 2011-02-07 21:51:21 +0100 | [diff] [blame] | 8 | #include "../run-command.h" |
Elijah Newren | 0b027f6 | 2023-03-21 06:25:58 +0000 | [diff] [blame] | 9 | #include "../abspath.h" |
Elijah Newren | 36bf195 | 2023-02-24 00:09:24 +0000 | [diff] [blame] | 10 | #include "../alloc.h" |
Johannes Schindelin | 564be79 | 2018-10-15 02:47:06 -0700 | [diff] [blame] | 11 | #include "win32/lazyload.h" |
Johannes Schindelin | bdfbb0e | 2018-10-30 11:40:06 -0700 | [diff] [blame] | 12 | #include "../config.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 13 | #include "../environment.h" |
Elijah Newren | 74ea5c9 | 2023-04-11 03:00:38 +0000 | [diff] [blame] | 14 | #include "../trace2.h" |
Elijah Newren | cb2a513 | 2023-04-22 20:17:09 +0000 | [diff] [blame] | 15 | #include "../symlinks.h" |
Elijah Newren | d5ebb50 | 2023-03-21 06:26:01 +0000 | [diff] [blame] | 16 | #include "../wrapper.h" |
Johannes Schindelin | 9e9da23 | 2019-01-17 12:14:48 -0800 | [diff] [blame] | 17 | #include "dir.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 18 | #include "gettext.h" |
Matthias Aßhauer | 4a9b204 | 2022-01-08 16:02:30 +0000 | [diff] [blame] | 19 | #define SECURITY_WIN32 |
| 20 | #include <sspi.h> |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 21 | |
Johannes Schindelin | 7c00bc3 | 2016-01-15 14:24:34 +0100 | [diff] [blame] | 22 | #define HCAST(type, handle) ((type)(intptr_t)handle) |
| 23 | |
Heiko Voigt | 19e1254 | 2011-02-07 21:50:26 +0100 | [diff] [blame] | 24 | static const int delay[] = { 0, 1, 10, 20, 40 }; |
| 25 | |
Johannes Schindelin | 08809c0 | 2020-02-13 21:49:53 +0000 | [diff] [blame] | 26 | void open_in_gdb(void) |
| 27 | { |
| 28 | static struct child_process cp = CHILD_PROCESS_INIT; |
| 29 | extern char *_pgmptr; |
| 30 | |
Jeff King | ef8d7ac | 2020-07-28 16:24:53 -0400 | [diff] [blame] | 31 | strvec_pushl(&cp.args, "mintty", "gdb", NULL); |
| 32 | strvec_pushf(&cp.args, "--pid=%d", getpid()); |
Johannes Schindelin | 08809c0 | 2020-02-13 21:49:53 +0000 | [diff] [blame] | 33 | cp.clean_on_exit = 1; |
| 34 | if (start_command(&cp) < 0) |
| 35 | die_errno("Could not start gdb"); |
| 36 | sleep(1); |
| 37 | } |
| 38 | |
Andrzej K. Haczewski | 44626dc | 2010-01-15 21:12:20 +0100 | [diff] [blame] | 39 | int err_win_to_posix(DWORD winerr) |
Petr Kodl | b3debd2 | 2009-01-24 15:04:39 +0100 | [diff] [blame] | 40 | { |
| 41 | int error = ENOSYS; |
| 42 | switch(winerr) { |
| 43 | case ERROR_ACCESS_DENIED: error = EACCES; break; |
| 44 | case ERROR_ACCOUNT_DISABLED: error = EACCES; break; |
| 45 | case ERROR_ACCOUNT_RESTRICTION: error = EACCES; break; |
| 46 | case ERROR_ALREADY_ASSIGNED: error = EBUSY; break; |
| 47 | case ERROR_ALREADY_EXISTS: error = EEXIST; break; |
| 48 | case ERROR_ARITHMETIC_OVERFLOW: error = ERANGE; break; |
| 49 | case ERROR_BAD_COMMAND: error = EIO; break; |
| 50 | case ERROR_BAD_DEVICE: error = ENODEV; break; |
| 51 | case ERROR_BAD_DRIVER_LEVEL: error = ENXIO; break; |
| 52 | case ERROR_BAD_EXE_FORMAT: error = ENOEXEC; break; |
| 53 | case ERROR_BAD_FORMAT: error = ENOEXEC; break; |
| 54 | case ERROR_BAD_LENGTH: error = EINVAL; break; |
| 55 | case ERROR_BAD_PATHNAME: error = ENOENT; break; |
| 56 | case ERROR_BAD_PIPE: error = EPIPE; break; |
| 57 | case ERROR_BAD_UNIT: error = ENODEV; break; |
| 58 | case ERROR_BAD_USERNAME: error = EINVAL; break; |
| 59 | case ERROR_BROKEN_PIPE: error = EPIPE; break; |
| 60 | case ERROR_BUFFER_OVERFLOW: error = ENAMETOOLONG; break; |
| 61 | case ERROR_BUSY: error = EBUSY; break; |
| 62 | case ERROR_BUSY_DRIVE: error = EBUSY; break; |
| 63 | case ERROR_CALL_NOT_IMPLEMENTED: error = ENOSYS; break; |
| 64 | case ERROR_CANNOT_MAKE: error = EACCES; break; |
| 65 | case ERROR_CANTOPEN: error = EIO; break; |
| 66 | case ERROR_CANTREAD: error = EIO; break; |
| 67 | case ERROR_CANTWRITE: error = EIO; break; |
| 68 | case ERROR_CRC: error = EIO; break; |
| 69 | case ERROR_CURRENT_DIRECTORY: error = EACCES; break; |
| 70 | case ERROR_DEVICE_IN_USE: error = EBUSY; break; |
| 71 | case ERROR_DEV_NOT_EXIST: error = ENODEV; break; |
| 72 | case ERROR_DIRECTORY: error = EINVAL; break; |
| 73 | case ERROR_DIR_NOT_EMPTY: error = ENOTEMPTY; break; |
| 74 | case ERROR_DISK_CHANGE: error = EIO; break; |
| 75 | case ERROR_DISK_FULL: error = ENOSPC; break; |
| 76 | case ERROR_DRIVE_LOCKED: error = EBUSY; break; |
| 77 | case ERROR_ENVVAR_NOT_FOUND: error = EINVAL; break; |
| 78 | case ERROR_EXE_MARKED_INVALID: error = ENOEXEC; break; |
| 79 | case ERROR_FILENAME_EXCED_RANGE: error = ENAMETOOLONG; break; |
| 80 | case ERROR_FILE_EXISTS: error = EEXIST; break; |
| 81 | case ERROR_FILE_INVALID: error = ENODEV; break; |
| 82 | case ERROR_FILE_NOT_FOUND: error = ENOENT; break; |
| 83 | case ERROR_GEN_FAILURE: error = EIO; break; |
| 84 | case ERROR_HANDLE_DISK_FULL: error = ENOSPC; break; |
| 85 | case ERROR_INSUFFICIENT_BUFFER: error = ENOMEM; break; |
| 86 | case ERROR_INVALID_ACCESS: error = EACCES; break; |
| 87 | case ERROR_INVALID_ADDRESS: error = EFAULT; break; |
| 88 | case ERROR_INVALID_BLOCK: error = EFAULT; break; |
| 89 | case ERROR_INVALID_DATA: error = EINVAL; break; |
| 90 | case ERROR_INVALID_DRIVE: error = ENODEV; break; |
| 91 | case ERROR_INVALID_EXE_SIGNATURE: error = ENOEXEC; break; |
| 92 | case ERROR_INVALID_FLAGS: error = EINVAL; break; |
| 93 | case ERROR_INVALID_FUNCTION: error = ENOSYS; break; |
| 94 | case ERROR_INVALID_HANDLE: error = EBADF; break; |
| 95 | case ERROR_INVALID_LOGON_HOURS: error = EACCES; break; |
| 96 | case ERROR_INVALID_NAME: error = EINVAL; break; |
| 97 | case ERROR_INVALID_OWNER: error = EINVAL; break; |
| 98 | case ERROR_INVALID_PARAMETER: error = EINVAL; break; |
| 99 | case ERROR_INVALID_PASSWORD: error = EPERM; break; |
| 100 | case ERROR_INVALID_PRIMARY_GROUP: error = EINVAL; break; |
| 101 | case ERROR_INVALID_SIGNAL_NUMBER: error = EINVAL; break; |
| 102 | case ERROR_INVALID_TARGET_HANDLE: error = EIO; break; |
| 103 | case ERROR_INVALID_WORKSTATION: error = EACCES; break; |
| 104 | case ERROR_IO_DEVICE: error = EIO; break; |
| 105 | case ERROR_IO_INCOMPLETE: error = EINTR; break; |
| 106 | case ERROR_LOCKED: error = EBUSY; break; |
| 107 | case ERROR_LOCK_VIOLATION: error = EACCES; break; |
| 108 | case ERROR_LOGON_FAILURE: error = EACCES; break; |
| 109 | case ERROR_MAPPED_ALIGNMENT: error = EINVAL; break; |
| 110 | case ERROR_META_EXPANSION_TOO_LONG: error = E2BIG; break; |
| 111 | case ERROR_MORE_DATA: error = EPIPE; break; |
| 112 | case ERROR_NEGATIVE_SEEK: error = ESPIPE; break; |
| 113 | case ERROR_NOACCESS: error = EFAULT; break; |
| 114 | case ERROR_NONE_MAPPED: error = EINVAL; break; |
| 115 | case ERROR_NOT_ENOUGH_MEMORY: error = ENOMEM; break; |
| 116 | case ERROR_NOT_READY: error = EAGAIN; break; |
| 117 | case ERROR_NOT_SAME_DEVICE: error = EXDEV; break; |
| 118 | case ERROR_NO_DATA: error = EPIPE; break; |
| 119 | case ERROR_NO_MORE_SEARCH_HANDLES: error = EIO; break; |
| 120 | case ERROR_NO_PROC_SLOTS: error = EAGAIN; break; |
| 121 | case ERROR_NO_SUCH_PRIVILEGE: error = EACCES; break; |
| 122 | case ERROR_OPEN_FAILED: error = EIO; break; |
| 123 | case ERROR_OPEN_FILES: error = EBUSY; break; |
| 124 | case ERROR_OPERATION_ABORTED: error = EINTR; break; |
| 125 | case ERROR_OUTOFMEMORY: error = ENOMEM; break; |
| 126 | case ERROR_PASSWORD_EXPIRED: error = EACCES; break; |
| 127 | case ERROR_PATH_BUSY: error = EBUSY; break; |
| 128 | case ERROR_PATH_NOT_FOUND: error = ENOENT; break; |
| 129 | case ERROR_PIPE_BUSY: error = EBUSY; break; |
| 130 | case ERROR_PIPE_CONNECTED: error = EPIPE; break; |
| 131 | case ERROR_PIPE_LISTENING: error = EPIPE; break; |
| 132 | case ERROR_PIPE_NOT_CONNECTED: error = EPIPE; break; |
| 133 | case ERROR_PRIVILEGE_NOT_HELD: error = EACCES; break; |
| 134 | case ERROR_READ_FAULT: error = EIO; break; |
| 135 | case ERROR_SEEK: error = EIO; break; |
| 136 | case ERROR_SEEK_ON_DEVICE: error = ESPIPE; break; |
| 137 | case ERROR_SHARING_BUFFER_EXCEEDED: error = ENFILE; break; |
| 138 | case ERROR_SHARING_VIOLATION: error = EACCES; break; |
| 139 | case ERROR_STACK_OVERFLOW: error = ENOMEM; break; |
Johannes Schindelin | 3ba3720 | 2019-12-02 11:33:29 +0000 | [diff] [blame] | 140 | case ERROR_SUCCESS: BUG("err_win_to_posix() called without an error!"); |
Petr Kodl | b3debd2 | 2009-01-24 15:04:39 +0100 | [diff] [blame] | 141 | case ERROR_SWAPERROR: error = ENOENT; break; |
| 142 | case ERROR_TOO_MANY_MODULES: error = EMFILE; break; |
| 143 | case ERROR_TOO_MANY_OPEN_FILES: error = EMFILE; break; |
| 144 | case ERROR_UNRECOGNIZED_MEDIA: error = ENXIO; break; |
| 145 | case ERROR_UNRECOGNIZED_VOLUME: error = ENODEV; break; |
| 146 | case ERROR_WAIT_NO_CHILDREN: error = ECHILD; break; |
| 147 | case ERROR_WRITE_FAULT: error = EIO; break; |
| 148 | case ERROR_WRITE_PROTECT: error = EROFS; break; |
| 149 | } |
| 150 | return error; |
| 151 | } |
| 152 | |
Heiko Voigt | 19e1254 | 2011-02-07 21:50:26 +0100 | [diff] [blame] | 153 | static inline int is_file_in_use_error(DWORD errcode) |
| 154 | { |
| 155 | switch (errcode) { |
| 156 | case ERROR_SHARING_VIOLATION: |
| 157 | case ERROR_ACCESS_DENIED: |
| 158 | return 1; |
| 159 | } |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
Heiko Voigt | c9b7840 | 2011-02-07 21:51:21 +0100 | [diff] [blame] | 164 | static int read_yes_no_answer(void) |
| 165 | { |
| 166 | char answer[1024]; |
| 167 | |
| 168 | if (fgets(answer, sizeof(answer), stdin)) { |
| 169 | size_t answer_len = strlen(answer); |
| 170 | int got_full_line = 0, c; |
| 171 | |
| 172 | /* remove the newline */ |
| 173 | if (answer_len >= 2 && answer[answer_len-2] == '\r') { |
| 174 | answer[answer_len-2] = '\0'; |
| 175 | got_full_line = 1; |
| 176 | } else if (answer_len >= 1 && answer[answer_len-1] == '\n') { |
| 177 | answer[answer_len-1] = '\0'; |
| 178 | got_full_line = 1; |
| 179 | } |
| 180 | /* flush the buffer in case we did not get the full line */ |
| 181 | if (!got_full_line) |
| 182 | while ((c = getchar()) != EOF && c != '\n') |
| 183 | ; |
| 184 | } else |
| 185 | /* we could not read, return the |
| 186 | * default answer which is no */ |
| 187 | return 0; |
| 188 | |
| 189 | if (tolower(answer[0]) == 'y' && !answer[1]) |
| 190 | return 1; |
| 191 | if (!strncasecmp(answer, "yes", sizeof(answer))) |
| 192 | return 1; |
| 193 | if (tolower(answer[0]) == 'n' && !answer[1]) |
| 194 | return 0; |
| 195 | if (!strncasecmp(answer, "no", sizeof(answer))) |
| 196 | return 0; |
| 197 | |
| 198 | /* did not find an answer we understand */ |
| 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | static int ask_yes_no_if_possible(const char *format, ...) |
| 203 | { |
| 204 | char question[4096]; |
René Scharfe | 4120294 | 2022-10-30 12:50:27 +0100 | [diff] [blame] | 205 | const char *retry_hook; |
Heiko Voigt | c9b7840 | 2011-02-07 21:51:21 +0100 | [diff] [blame] | 206 | va_list args; |
| 207 | |
| 208 | va_start(args, format); |
| 209 | vsnprintf(question, sizeof(question), format, args); |
| 210 | va_end(args); |
| 211 | |
René Scharfe | 4120294 | 2022-10-30 12:50:27 +0100 | [diff] [blame] | 212 | retry_hook = mingw_getenv("GIT_ASK_YESNO"); |
| 213 | if (retry_hook) { |
| 214 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 215 | |
| 216 | strvec_pushl(&cmd.args, retry_hook, question, NULL); |
| 217 | return !run_command(&cmd); |
Heiko Voigt | c9b7840 | 2011-02-07 21:51:21 +0100 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | if (!isatty(_fileno(stdin)) || !isatty(_fileno(stderr))) |
| 221 | return 0; |
| 222 | |
| 223 | while (1) { |
| 224 | int answer; |
| 225 | fprintf(stderr, "%s (y/n) ", question); |
| 226 | |
| 227 | if ((answer = read_yes_no_answer()) >= 0) |
| 228 | return answer; |
| 229 | |
| 230 | fprintf(stderr, "Sorry, I did not understand your answer. " |
| 231 | "Please type 'y' or 'n'\n"); |
| 232 | } |
| 233 | } |
| 234 | |
Johannes Schindelin | bdfbb0e | 2018-10-30 11:40:06 -0700 | [diff] [blame] | 235 | /* Windows only */ |
| 236 | enum hide_dotfiles_type { |
| 237 | HIDE_DOTFILES_FALSE = 0, |
| 238 | HIDE_DOTFILES_TRUE, |
| 239 | HIDE_DOTFILES_DOTGITONLY |
| 240 | }; |
| 241 | |
Johannes Schindelin | ac33519 | 2019-11-22 14:41:05 +0000 | [diff] [blame] | 242 | static int core_restrict_inherited_handles = -1; |
Johannes Schindelin | bdfbb0e | 2018-10-30 11:40:06 -0700 | [diff] [blame] | 243 | static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY; |
Johannes Schindelin | 0e218f9 | 2018-10-30 11:40:07 -0700 | [diff] [blame] | 244 | static char *unset_environment_variables; |
Johannes Schindelin | bdfbb0e | 2018-10-30 11:40:06 -0700 | [diff] [blame] | 245 | |
Glen Choo | a4e7e31 | 2023-06-28 19:26:22 +0000 | [diff] [blame] | 246 | int mingw_core_config(const char *var, const char *value, |
| 247 | const struct config_context *ctx, void *cb) |
Johannes Schindelin | 70fc579 | 2018-10-30 11:40:04 -0700 | [diff] [blame] | 248 | { |
Johannes Schindelin | bdfbb0e | 2018-10-30 11:40:06 -0700 | [diff] [blame] | 249 | if (!strcmp(var, "core.hidedotfiles")) { |
| 250 | if (value && !strcasecmp(value, "dotgitonly")) |
| 251 | hide_dotfiles = HIDE_DOTFILES_DOTGITONLY; |
| 252 | else |
| 253 | hide_dotfiles = git_config_bool(var, value); |
| 254 | return 0; |
| 255 | } |
| 256 | |
Johannes Schindelin | 0e218f9 | 2018-10-30 11:40:07 -0700 | [diff] [blame] | 257 | if (!strcmp(var, "core.unsetenvvars")) { |
Jeff King | ba176db | 2023-12-07 02:11:14 -0500 | [diff] [blame] | 258 | if (!value) |
| 259 | return config_error_nonbool(var); |
Johannes Schindelin | 0e218f9 | 2018-10-30 11:40:07 -0700 | [diff] [blame] | 260 | free(unset_environment_variables); |
| 261 | unset_environment_variables = xstrdup(value); |
| 262 | return 0; |
| 263 | } |
| 264 | |
Johannes Schindelin | ac33519 | 2019-11-22 14:41:05 +0000 | [diff] [blame] | 265 | if (!strcmp(var, "core.restrictinheritedhandles")) { |
| 266 | if (value && !strcasecmp(value, "auto")) |
| 267 | core_restrict_inherited_handles = -1; |
| 268 | else |
| 269 | core_restrict_inherited_handles = |
| 270 | git_config_bool(var, value); |
| 271 | return 0; |
| 272 | } |
| 273 | |
Johannes Schindelin | 70fc579 | 2018-10-30 11:40:04 -0700 | [diff] [blame] | 274 | return 0; |
| 275 | } |
| 276 | |
Anton Serbulov | 4745fee | 2018-10-23 03:52:49 -0700 | [diff] [blame] | 277 | /* Normalizes NT paths as returned by some low-level APIs. */ |
| 278 | static wchar_t *normalize_ntpath(wchar_t *wbuf) |
| 279 | { |
| 280 | int i; |
| 281 | /* fix absolute path prefixes */ |
| 282 | if (wbuf[0] == '\\') { |
| 283 | /* strip NT namespace prefixes */ |
| 284 | if (!wcsncmp(wbuf, L"\\??\\", 4) || |
| 285 | !wcsncmp(wbuf, L"\\\\?\\", 4)) |
| 286 | wbuf += 4; |
| 287 | else if (!wcsnicmp(wbuf, L"\\DosDevices\\", 12)) |
| 288 | wbuf += 12; |
| 289 | /* replace remaining '...UNC\' with '\\' */ |
| 290 | if (!wcsnicmp(wbuf, L"UNC\\", 4)) { |
| 291 | wbuf += 2; |
| 292 | *wbuf = '\\'; |
| 293 | } |
| 294 | } |
| 295 | /* convert backslashes to slashes */ |
| 296 | for (i = 0; wbuf[i]; i++) |
| 297 | if (wbuf[i] == '\\') |
| 298 | wbuf[i] = '/'; |
| 299 | return wbuf; |
| 300 | } |
| 301 | |
Heiko Voigt | 337967f | 2011-02-07 21:49:33 +0100 | [diff] [blame] | 302 | int mingw_unlink(const char *pathname) |
| 303 | { |
Heiko Voigt | 19e1254 | 2011-02-07 21:50:26 +0100 | [diff] [blame] | 304 | int ret, tries = 0; |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 305 | wchar_t wpathname[MAX_PATH]; |
| 306 | if (xutftowcs_path(wpathname, pathname) < 0) |
| 307 | return -1; |
Heiko Voigt | 19e1254 | 2011-02-07 21:50:26 +0100 | [diff] [blame] | 308 | |
Jeff Hostetler | 680e0b4 | 2020-08-17 10:37:02 +0000 | [diff] [blame] | 309 | if (DeleteFileW(wpathname)) |
| 310 | return 0; |
| 311 | |
Heiko Voigt | 337967f | 2011-02-07 21:49:33 +0100 | [diff] [blame] | 312 | /* read-only files cannot be removed */ |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 313 | _wchmod(wpathname, 0666); |
| 314 | while ((ret = _wunlink(wpathname)) == -1 && tries < ARRAY_SIZE(delay)) { |
Heiko Voigt | 19e1254 | 2011-02-07 21:50:26 +0100 | [diff] [blame] | 315 | if (!is_file_in_use_error(GetLastError())) |
| 316 | break; |
| 317 | /* |
| 318 | * We assume that some other process had the source or |
| 319 | * destination file open at the wrong moment and retry. |
| 320 | * In order to give the other process a higher chance to |
| 321 | * complete its operation, we give up our time slice now. |
| 322 | * If we have to retry again, we do sleep a bit. |
| 323 | */ |
| 324 | Sleep(delay[tries]); |
| 325 | tries++; |
| 326 | } |
Heiko Voigt | c9b7840 | 2011-02-07 21:51:21 +0100 | [diff] [blame] | 327 | while (ret == -1 && is_file_in_use_error(GetLastError()) && |
| 328 | ask_yes_no_if_possible("Unlink of file '%s' failed. " |
| 329 | "Should I try again?", pathname)) |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 330 | ret = _wunlink(wpathname); |
Heiko Voigt | 19e1254 | 2011-02-07 21:50:26 +0100 | [diff] [blame] | 331 | return ret; |
Heiko Voigt | 337967f | 2011-02-07 21:49:33 +0100 | [diff] [blame] | 332 | } |
| 333 | |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 334 | static int is_dir_empty(const wchar_t *wpath) |
Johannes Schindelin | ab1a11b | 2011-02-07 21:54:01 +0100 | [diff] [blame] | 335 | { |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 336 | WIN32_FIND_DATAW findbuf; |
Johannes Schindelin | ab1a11b | 2011-02-07 21:54:01 +0100 | [diff] [blame] | 337 | HANDLE handle; |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 338 | wchar_t wbuf[MAX_PATH + 2]; |
| 339 | wcscpy(wbuf, wpath); |
| 340 | wcscat(wbuf, L"\\*"); |
| 341 | handle = FindFirstFileW(wbuf, &findbuf); |
| 342 | if (handle == INVALID_HANDLE_VALUE) |
Johannes Schindelin | ab1a11b | 2011-02-07 21:54:01 +0100 | [diff] [blame] | 343 | return GetLastError() == ERROR_NO_MORE_FILES; |
Johannes Schindelin | ab1a11b | 2011-02-07 21:54:01 +0100 | [diff] [blame] | 344 | |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 345 | while (!wcscmp(findbuf.cFileName, L".") || |
| 346 | !wcscmp(findbuf.cFileName, L"..")) |
| 347 | if (!FindNextFileW(handle, &findbuf)) { |
| 348 | DWORD err = GetLastError(); |
| 349 | FindClose(handle); |
| 350 | return err == ERROR_NO_MORE_FILES; |
Johannes Schindelin | ab1a11b | 2011-02-07 21:54:01 +0100 | [diff] [blame] | 351 | } |
| 352 | FindClose(handle); |
Johannes Schindelin | ab1a11b | 2011-02-07 21:54:01 +0100 | [diff] [blame] | 353 | return 0; |
| 354 | } |
| 355 | |
Heiko Voigt | 4f28810 | 2011-02-07 21:52:34 +0100 | [diff] [blame] | 356 | int mingw_rmdir(const char *pathname) |
| 357 | { |
| 358 | int ret, tries = 0; |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 359 | wchar_t wpathname[MAX_PATH]; |
Thomas Bétous | 3e7d488 | 2021-08-02 21:07:30 +0000 | [diff] [blame] | 360 | struct stat st; |
| 361 | |
| 362 | /* |
| 363 | * Contrary to Linux' `rmdir()`, Windows' _wrmdir() and _rmdir() |
| 364 | * (and `RemoveDirectoryW()`) will attempt to remove the target of a |
| 365 | * symbolic link (if it points to a directory). |
| 366 | * |
| 367 | * This behavior breaks the assumption of e.g. `remove_path()` which |
| 368 | * upon successful deletion of a file will attempt to remove its parent |
| 369 | * directories recursively until failure (which usually happens when |
| 370 | * the directory is not empty). |
| 371 | * |
| 372 | * Therefore, before calling `_wrmdir()`, we first check if the path is |
| 373 | * a symbolic link. If it is, we exit and return the same error as |
| 374 | * Linux' `rmdir()` would, i.e. `ENOTDIR`. |
| 375 | */ |
| 376 | if (!mingw_lstat(pathname, &st) && S_ISLNK(st.st_mode)) { |
| 377 | errno = ENOTDIR; |
| 378 | return -1; |
| 379 | } |
| 380 | |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 381 | if (xutftowcs_path(wpathname, pathname) < 0) |
| 382 | return -1; |
Heiko Voigt | 4f28810 | 2011-02-07 21:52:34 +0100 | [diff] [blame] | 383 | |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 384 | while ((ret = _wrmdir(wpathname)) == -1 && tries < ARRAY_SIZE(delay)) { |
Heiko Voigt | 4f28810 | 2011-02-07 21:52:34 +0100 | [diff] [blame] | 385 | if (!is_file_in_use_error(GetLastError())) |
Erik Faye-Lund | a83b2b5 | 2012-12-10 15:42:27 +0100 | [diff] [blame] | 386 | errno = err_win_to_posix(GetLastError()); |
| 387 | if (errno != EACCES) |
Heiko Voigt | 4f28810 | 2011-02-07 21:52:34 +0100 | [diff] [blame] | 388 | break; |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 389 | if (!is_dir_empty(wpathname)) { |
Johannes Schindelin | ab1a11b | 2011-02-07 21:54:01 +0100 | [diff] [blame] | 390 | errno = ENOTEMPTY; |
| 391 | break; |
| 392 | } |
Heiko Voigt | 4f28810 | 2011-02-07 21:52:34 +0100 | [diff] [blame] | 393 | /* |
| 394 | * We assume that some other process had the source or |
| 395 | * destination file open at the wrong moment and retry. |
| 396 | * In order to give the other process a higher chance to |
| 397 | * complete its operation, we give up our time slice now. |
| 398 | * If we have to retry again, we do sleep a bit. |
| 399 | */ |
| 400 | Sleep(delay[tries]); |
| 401 | tries++; |
| 402 | } |
Erik Faye-Lund | a83b2b5 | 2012-12-10 15:42:27 +0100 | [diff] [blame] | 403 | while (ret == -1 && errno == EACCES && is_file_in_use_error(GetLastError()) && |
Heiko Voigt | 4f28810 | 2011-02-07 21:52:34 +0100 | [diff] [blame] | 404 | ask_yes_no_if_possible("Deletion of directory '%s' failed. " |
| 405 | "Should I try again?", pathname)) |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 406 | ret = _wrmdir(wpathname); |
Matheus Tavares | 684dd4c | 2020-12-10 10:27:55 -0300 | [diff] [blame] | 407 | if (!ret) |
| 408 | invalidate_lstat_cache(); |
Heiko Voigt | 4f28810 | 2011-02-07 21:52:34 +0100 | [diff] [blame] | 409 | return ret; |
| 410 | } |
| 411 | |
Johannes Schindelin | f30afda | 2016-05-11 10:43:37 +0200 | [diff] [blame] | 412 | static inline int needs_hiding(const char *path) |
| 413 | { |
| 414 | const char *basename; |
| 415 | |
| 416 | if (hide_dotfiles == HIDE_DOTFILES_FALSE) |
| 417 | return 0; |
| 418 | |
| 419 | /* We cannot use basename(), as it would remove trailing slashes */ |
Torsten Bögershausen | 1cadad6 | 2018-12-15 05:33:30 +0100 | [diff] [blame] | 420 | win32_skip_dos_drive_prefix((char **)&path); |
Johannes Schindelin | f30afda | 2016-05-11 10:43:37 +0200 | [diff] [blame] | 421 | if (!*path) |
| 422 | return 0; |
| 423 | |
| 424 | for (basename = path; *path; path++) |
| 425 | if (is_dir_sep(*path)) { |
| 426 | do { |
| 427 | path++; |
| 428 | } while (is_dir_sep(*path)); |
| 429 | /* ignore trailing slashes */ |
| 430 | if (*path) |
| 431 | basename = path; |
Johannes Schindelin | 60e6569 | 2019-10-25 14:13:36 +0000 | [diff] [blame] | 432 | else |
| 433 | break; |
Johannes Schindelin | f30afda | 2016-05-11 10:43:37 +0200 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | if (hide_dotfiles == HIDE_DOTFILES_TRUE) |
| 437 | return *basename == '.'; |
| 438 | |
| 439 | assert(hide_dotfiles == HIDE_DOTFILES_DOTGITONLY); |
| 440 | return !strncasecmp(".git", basename, 4) && |
| 441 | (!basename[4] || is_dir_sep(basename[4])); |
| 442 | } |
| 443 | |
| 444 | static int set_hidden_flag(const wchar_t *path, int set) |
| 445 | { |
| 446 | DWORD original = GetFileAttributesW(path), modified; |
| 447 | if (set) |
| 448 | modified = original | FILE_ATTRIBUTE_HIDDEN; |
| 449 | else |
| 450 | modified = original & ~FILE_ATTRIBUTE_HIDDEN; |
| 451 | if (original == modified || SetFileAttributesW(path, modified)) |
| 452 | return 0; |
| 453 | errno = err_win_to_posix(GetLastError()); |
| 454 | return -1; |
| 455 | } |
| 456 | |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 457 | int mingw_mkdir(const char *path, int mode) |
| 458 | { |
| 459 | int ret; |
| 460 | wchar_t wpath[MAX_PATH]; |
Johannes Schindelin | d2c84da | 2019-09-05 13:27:53 +0200 | [diff] [blame] | 461 | |
Johannes Schindelin | 4dc42c6 | 2019-12-21 22:05:01 +0000 | [diff] [blame] | 462 | if (!is_valid_win32_path(path, 0)) { |
Johannes Schindelin | d2c84da | 2019-09-05 13:27:53 +0200 | [diff] [blame] | 463 | errno = EINVAL; |
| 464 | return -1; |
| 465 | } |
| 466 | |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 467 | if (xutftowcs_path(wpath, path) < 0) |
| 468 | return -1; |
| 469 | ret = _wmkdir(wpath); |
Johannes Schindelin | f30afda | 2016-05-11 10:43:37 +0200 | [diff] [blame] | 470 | if (!ret && needs_hiding(path)) |
| 471 | return set_hidden_flag(wpath, 1); |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 472 | return ret; |
| 473 | } |
| 474 | |
Jeff Hostetler | eeaf7dd | 2018-09-11 13:06:02 -0700 | [diff] [blame] | 475 | /* |
| 476 | * Calling CreateFile() using FILE_APPEND_DATA and without FILE_WRITE_DATA |
| 477 | * is documented in [1] as opening a writable file handle in append mode. |
| 478 | * (It is believed that) this is atomic since it is maintained by the |
| 479 | * kernel unlike the O_APPEND flag which is racily maintained by the CRT. |
| 480 | * |
| 481 | * [1] https://docs.microsoft.com/en-us/windows/desktop/fileio/file-access-rights-constants |
| 482 | * |
| 483 | * This trick does not appear to work for named pipes. Instead it creates |
| 484 | * a named pipe client handle that cannot be written to. Callers should |
| 485 | * just use the regular _wopen() for them. (And since client handle gets |
| 486 | * bound to a unique server handle, it isn't really an issue.) |
| 487 | */ |
Johannes Sixt | d641097 | 2018-08-13 21:02:50 +0200 | [diff] [blame] | 488 | static int mingw_open_append(wchar_t const *wfilename, int oflags, ...) |
| 489 | { |
| 490 | HANDLE handle; |
| 491 | int fd; |
| 492 | DWORD create = (oflags & O_CREAT) ? OPEN_ALWAYS : OPEN_EXISTING; |
| 493 | |
| 494 | /* only these flags are supported */ |
| 495 | if ((oflags & ~O_CREAT) != (O_WRONLY | O_APPEND)) |
| 496 | return errno = ENOSYS, -1; |
| 497 | |
| 498 | /* |
| 499 | * FILE_SHARE_WRITE is required to permit child processes |
| 500 | * to append to the file. |
| 501 | */ |
| 502 | handle = CreateFileW(wfilename, FILE_APPEND_DATA, |
| 503 | FILE_SHARE_WRITE | FILE_SHARE_READ, |
| 504 | NULL, create, FILE_ATTRIBUTE_NORMAL, NULL); |
Nathan Sanders | 23eafd9 | 2020-04-10 11:28:56 +0000 | [diff] [blame] | 505 | if (handle == INVALID_HANDLE_VALUE) { |
| 506 | DWORD err = GetLastError(); |
| 507 | |
| 508 | /* |
| 509 | * Some network storage solutions (e.g. Isilon) might return |
| 510 | * ERROR_INVALID_PARAMETER instead of expected error |
| 511 | * ERROR_PATH_NOT_FOUND, which results in an unknown error. If |
| 512 | * so, let's turn the error to ERROR_PATH_NOT_FOUND instead. |
| 513 | */ |
| 514 | if (err == ERROR_INVALID_PARAMETER) |
| 515 | err = ERROR_PATH_NOT_FOUND; |
| 516 | |
| 517 | errno = err_win_to_posix(err); |
| 518 | return -1; |
| 519 | } |
Jeff Hostetler | eeaf7dd | 2018-09-11 13:06:02 -0700 | [diff] [blame] | 520 | |
Johannes Sixt | d641097 | 2018-08-13 21:02:50 +0200 | [diff] [blame] | 521 | /* |
| 522 | * No O_APPEND here, because the CRT uses it only to reset the |
Jeff Hostetler | eeaf7dd | 2018-09-11 13:06:02 -0700 | [diff] [blame] | 523 | * file pointer to EOF before each write(); but that is not |
| 524 | * necessary (and may lead to races) for a file created with |
| 525 | * FILE_APPEND_DATA. |
Johannes Sixt | d641097 | 2018-08-13 21:02:50 +0200 | [diff] [blame] | 526 | */ |
| 527 | fd = _open_osfhandle((intptr_t)handle, O_BINARY); |
| 528 | if (fd < 0) |
| 529 | CloseHandle(handle); |
| 530 | return fd; |
| 531 | } |
| 532 | |
Jeff Hostetler | eeaf7dd | 2018-09-11 13:06:02 -0700 | [diff] [blame] | 533 | /* |
| 534 | * Does the pathname map to the local named pipe filesystem? |
| 535 | * That is, does it have a "//./pipe/" prefix? |
| 536 | */ |
| 537 | static int is_local_named_pipe_path(const char *filename) |
| 538 | { |
| 539 | return (is_dir_sep(filename[0]) && |
| 540 | is_dir_sep(filename[1]) && |
| 541 | filename[2] == '.' && |
| 542 | is_dir_sep(filename[3]) && |
| 543 | !strncasecmp(filename+4, "pipe", 4) && |
| 544 | is_dir_sep(filename[8]) && |
| 545 | filename[9]); |
| 546 | } |
| 547 | |
Johannes Sixt | 3e4a1ba | 2007-11-15 22:22:47 +0100 | [diff] [blame] | 548 | int mingw_open (const char *filename, int oflags, ...) |
| 549 | { |
Johannes Sixt | d641097 | 2018-08-13 21:02:50 +0200 | [diff] [blame] | 550 | typedef int (*open_fn_t)(wchar_t const *wfilename, int oflags, ...); |
Johannes Sixt | 3e4a1ba | 2007-11-15 22:22:47 +0100 | [diff] [blame] | 551 | va_list args; |
| 552 | unsigned mode; |
Johannes Schindelin | d2c84da | 2019-09-05 13:27:53 +0200 | [diff] [blame] | 553 | int fd, create = (oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL); |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 554 | wchar_t wfilename[MAX_PATH]; |
Johannes Sixt | d641097 | 2018-08-13 21:02:50 +0200 | [diff] [blame] | 555 | open_fn_t open_fn; |
Frank Li | 0d30ad7 | 2009-09-16 10:20:17 +0200 | [diff] [blame] | 556 | |
Johannes Sixt | 3e4a1ba | 2007-11-15 22:22:47 +0100 | [diff] [blame] | 557 | va_start(args, oflags); |
| 558 | mode = va_arg(args, int); |
| 559 | va_end(args); |
| 560 | |
Johannes Schindelin | 4dc42c6 | 2019-12-21 22:05:01 +0000 | [diff] [blame] | 561 | if (!is_valid_win32_path(filename, !create)) { |
Johannes Schindelin | d2c84da | 2019-09-05 13:27:53 +0200 | [diff] [blame] | 562 | errno = create ? EINVAL : ENOENT; |
| 563 | return -1; |
| 564 | } |
| 565 | |
Jeff Hostetler | eeaf7dd | 2018-09-11 13:06:02 -0700 | [diff] [blame] | 566 | if ((oflags & O_APPEND) && !is_local_named_pipe_path(filename)) |
Johannes Sixt | d641097 | 2018-08-13 21:02:50 +0200 | [diff] [blame] | 567 | open_fn = mingw_open_append; |
| 568 | else |
| 569 | open_fn = _wopen; |
| 570 | |
Johannes Schindelin | 98d9b23 | 2019-12-21 22:05:00 +0000 | [diff] [blame] | 571 | if (filename && !strcmp(filename, "/dev/null")) |
| 572 | wcscpy(wfilename, L"nul"); |
| 573 | else if (xutftowcs_path(wfilename, filename) < 0) |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 574 | return -1; |
Johannes Schindelin | 98d9b23 | 2019-12-21 22:05:00 +0000 | [diff] [blame] | 575 | |
Johannes Sixt | d641097 | 2018-08-13 21:02:50 +0200 | [diff] [blame] | 576 | fd = open_fn(wfilename, oflags, mode); |
Frank Li | 0d30ad7 | 2009-09-16 10:20:17 +0200 | [diff] [blame] | 577 | |
Johannes Sixt | ba6fad0 | 2014-11-16 22:06:26 +0100 | [diff] [blame] | 578 | if (fd < 0 && (oflags & O_ACCMODE) != O_RDONLY && errno == EACCES) { |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 579 | DWORD attrs = GetFileAttributesW(wfilename); |
Johannes Sixt | 3e4a1ba | 2007-11-15 22:22:47 +0100 | [diff] [blame] | 580 | if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY)) |
| 581 | errno = EISDIR; |
| 582 | } |
Johannes Schindelin | f30afda | 2016-05-11 10:43:37 +0200 | [diff] [blame] | 583 | if ((oflags & O_CREAT) && needs_hiding(filename)) { |
| 584 | /* |
| 585 | * Internally, _wopen() uses the CreateFile() API which errors |
| 586 | * out with an ERROR_ACCESS_DENIED if CREATE_ALWAYS was |
| 587 | * specified and an already existing file's attributes do not |
| 588 | * match *exactly*. As there is no mode or flag we can set that |
| 589 | * would correspond to FILE_ATTRIBUTE_HIDDEN, let's just try |
| 590 | * again *without* the O_CREAT flag (that corresponds to the |
| 591 | * CREATE_ALWAYS flag of CreateFile()). |
| 592 | */ |
| 593 | if (fd < 0 && errno == EACCES) |
Johannes Sixt | d641097 | 2018-08-13 21:02:50 +0200 | [diff] [blame] | 594 | fd = open_fn(wfilename, oflags & ~O_CREAT, mode); |
Johannes Schindelin | f30afda | 2016-05-11 10:43:37 +0200 | [diff] [blame] | 595 | if (fd >= 0 && set_hidden_flag(wfilename, 1)) |
| 596 | warning("could not mark '%s' as hidden.", filename); |
| 597 | } |
Johannes Sixt | 3e4a1ba | 2007-11-15 22:22:47 +0100 | [diff] [blame] | 598 | return fd; |
| 599 | } |
| 600 | |
Erik Faye-Lund | 176478a | 2012-12-04 09:10:38 +0100 | [diff] [blame] | 601 | static BOOL WINAPI ctrl_ignore(DWORD type) |
| 602 | { |
| 603 | return TRUE; |
| 604 | } |
| 605 | |
| 606 | #undef fgetc |
| 607 | int mingw_fgetc(FILE *stream) |
| 608 | { |
| 609 | int ch; |
| 610 | if (!isatty(_fileno(stream))) |
| 611 | return fgetc(stream); |
| 612 | |
| 613 | SetConsoleCtrlHandler(ctrl_ignore, TRUE); |
| 614 | while (1) { |
| 615 | ch = fgetc(stream); |
| 616 | if (ch != EOF || GetLastError() != ERROR_OPERATION_ABORTED) |
| 617 | break; |
| 618 | |
| 619 | /* Ctrl+C was pressed, simulate SIGINT and retry */ |
| 620 | mingw_raise(SIGINT); |
| 621 | } |
| 622 | SetConsoleCtrlHandler(ctrl_ignore, FALSE); |
| 623 | return ch; |
| 624 | } |
| 625 | |
Johannes Sixt | 3fdcdbd | 2010-02-25 21:03:44 +0100 | [diff] [blame] | 626 | #undef fopen |
| 627 | FILE *mingw_fopen (const char *filename, const char *otype) |
| 628 | { |
Johannes Schindelin | f30afda | 2016-05-11 10:43:37 +0200 | [diff] [blame] | 629 | int hide = needs_hiding(filename); |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 630 | FILE *file; |
| 631 | wchar_t wfilename[MAX_PATH], wotype[4]; |
Johannes Schindelin | 4dc42c6 | 2019-12-21 22:05:01 +0000 | [diff] [blame] | 632 | if (filename && !strcmp(filename, "/dev/null")) |
| 633 | wcscpy(wfilename, L"nul"); |
| 634 | else if (!is_valid_win32_path(filename, 1)) { |
Johannes Schindelin | d2c84da | 2019-09-05 13:27:53 +0200 | [diff] [blame] | 635 | int create = otype && strchr(otype, 'w'); |
| 636 | errno = create ? EINVAL : ENOENT; |
| 637 | return NULL; |
Johannes Schindelin | 4dc42c6 | 2019-12-21 22:05:01 +0000 | [diff] [blame] | 638 | } else if (xutftowcs_path(wfilename, filename) < 0) |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 639 | return NULL; |
Johannes Schindelin | 98d9b23 | 2019-12-21 22:05:00 +0000 | [diff] [blame] | 640 | |
| 641 | if (xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0) |
| 642 | return NULL; |
| 643 | |
Johannes Schindelin | f30afda | 2016-05-11 10:43:37 +0200 | [diff] [blame] | 644 | if (hide && !access(filename, F_OK) && set_hidden_flag(wfilename, 0)) { |
| 645 | error("could not unhide %s", filename); |
| 646 | return NULL; |
| 647 | } |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 648 | file = _wfopen(wfilename, wotype); |
Johannes Sixt | e5b3134 | 2017-05-29 22:27:35 +0200 | [diff] [blame] | 649 | if (!file && GetLastError() == ERROR_INVALID_NAME) |
| 650 | errno = ENOENT; |
Johannes Schindelin | f30afda | 2016-05-11 10:43:37 +0200 | [diff] [blame] | 651 | if (file && hide && set_hidden_flag(wfilename, 1)) |
| 652 | warning("could not mark '%s' as hidden.", filename); |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 653 | return file; |
Johannes Sixt | 3fdcdbd | 2010-02-25 21:03:44 +0100 | [diff] [blame] | 654 | } |
| 655 | |
Johannes Sixt | 3fdcdbd | 2010-02-25 21:03:44 +0100 | [diff] [blame] | 656 | FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream) |
| 657 | { |
Johannes Schindelin | f30afda | 2016-05-11 10:43:37 +0200 | [diff] [blame] | 658 | int hide = needs_hiding(filename); |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 659 | FILE *file; |
| 660 | wchar_t wfilename[MAX_PATH], wotype[4]; |
Johannes Schindelin | 4dc42c6 | 2019-12-21 22:05:01 +0000 | [diff] [blame] | 661 | if (filename && !strcmp(filename, "/dev/null")) |
| 662 | wcscpy(wfilename, L"nul"); |
| 663 | else if (!is_valid_win32_path(filename, 1)) { |
Johannes Schindelin | d2c84da | 2019-09-05 13:27:53 +0200 | [diff] [blame] | 664 | int create = otype && strchr(otype, 'w'); |
| 665 | errno = create ? EINVAL : ENOENT; |
| 666 | return NULL; |
Johannes Schindelin | 4dc42c6 | 2019-12-21 22:05:01 +0000 | [diff] [blame] | 667 | } else if (xutftowcs_path(wfilename, filename) < 0) |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 668 | return NULL; |
Johannes Schindelin | 98d9b23 | 2019-12-21 22:05:00 +0000 | [diff] [blame] | 669 | |
| 670 | if (xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0) |
| 671 | return NULL; |
| 672 | |
Johannes Schindelin | f30afda | 2016-05-11 10:43:37 +0200 | [diff] [blame] | 673 | if (hide && !access(filename, F_OK) && set_hidden_flag(wfilename, 0)) { |
| 674 | error("could not unhide %s", filename); |
| 675 | return NULL; |
| 676 | } |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 677 | file = _wfreopen(wfilename, wotype, stream); |
Johannes Schindelin | f30afda | 2016-05-11 10:43:37 +0200 | [diff] [blame] | 678 | if (file && hide && set_hidden_flag(wfilename, 1)) |
| 679 | warning("could not mark '%s' as hidden.", filename); |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 680 | return file; |
Johannes Sixt | 3fdcdbd | 2010-02-25 21:03:44 +0100 | [diff] [blame] | 681 | } |
| 682 | |
Johannes Sixt | 84adb64 | 2012-10-17 09:05:51 +0200 | [diff] [blame] | 683 | #undef fflush |
| 684 | int mingw_fflush(FILE *stream) |
| 685 | { |
| 686 | int ret = fflush(stream); |
| 687 | |
| 688 | /* |
| 689 | * write() is used behind the scenes of stdio output functions. |
| 690 | * Since git code does not check for errors after each stdio write |
| 691 | * operation, it can happen that write() is called by a later |
| 692 | * stdio function even if an earlier write() call failed. In the |
| 693 | * case of a pipe whose readable end was closed, only the first |
| 694 | * call to write() reports EPIPE on Windows. Subsequent write() |
| 695 | * calls report EINVAL. It is impossible to notice whether this |
| 696 | * fflush invocation triggered such a case, therefore, we have to |
| 697 | * catch all EINVAL errors whole-sale. |
| 698 | */ |
| 699 | if (ret && errno == EINVAL) |
| 700 | errno = EPIPE; |
| 701 | |
| 702 | return ret; |
| 703 | } |
| 704 | |
Johannes Schindelin | 2b86292 | 2015-12-17 18:08:15 +0100 | [diff] [blame] | 705 | #undef write |
| 706 | ssize_t mingw_write(int fd, const void *buf, size_t len) |
| 707 | { |
| 708 | ssize_t result = write(fd, buf, len); |
| 709 | |
Johannes Schindelin | 19ed0df | 2024-01-30 21:36:59 +0000 | [diff] [blame] | 710 | if (result < 0 && (errno == EINVAL || errno == ENOSPC) && buf) { |
| 711 | int orig = errno; |
| 712 | |
Johannes Schindelin | 2b86292 | 2015-12-17 18:08:15 +0100 | [diff] [blame] | 713 | /* check if fd is a pipe */ |
| 714 | HANDLE h = (HANDLE) _get_osfhandle(fd); |
Johannes Schindelin | 19ed0df | 2024-01-30 21:36:59 +0000 | [diff] [blame] | 715 | if (GetFileType(h) != FILE_TYPE_PIPE) |
| 716 | errno = orig; |
| 717 | else if (orig == EINVAL) |
Johannes Schindelin | 2b86292 | 2015-12-17 18:08:15 +0100 | [diff] [blame] | 718 | errno = EPIPE; |
Johannes Schindelin | 19ed0df | 2024-01-30 21:36:59 +0000 | [diff] [blame] | 719 | else { |
| 720 | DWORD buf_size; |
| 721 | |
| 722 | if (!GetNamedPipeInfo(h, NULL, NULL, &buf_size, NULL)) |
| 723 | buf_size = 4096; |
| 724 | if (len > buf_size) |
| 725 | return write(fd, buf, buf_size); |
| 726 | errno = orig; |
| 727 | } |
Johannes Schindelin | 2b86292 | 2015-12-17 18:08:15 +0100 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | return result; |
| 731 | } |
| 732 | |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 733 | int mingw_access(const char *filename, int mode) |
| 734 | { |
| 735 | wchar_t wfilename[MAX_PATH]; |
Johannes Schindelin | 9160068 | 2021-04-16 13:21:01 +0200 | [diff] [blame] | 736 | if (!strcmp("nul", filename) || !strcmp("/dev/null", filename)) |
| 737 | return 0; |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 738 | if (xutftowcs_path(wfilename, filename) < 0) |
| 739 | return -1; |
| 740 | /* X_OK is not supported by the MSVCRT version */ |
| 741 | return _waccess(wfilename, mode & ~X_OK); |
| 742 | } |
| 743 | |
| 744 | int mingw_chdir(const char *dirname) |
| 745 | { |
| 746 | wchar_t wdirname[MAX_PATH]; |
| 747 | if (xutftowcs_path(wdirname, dirname) < 0) |
| 748 | return -1; |
| 749 | return _wchdir(wdirname); |
| 750 | } |
| 751 | |
| 752 | int mingw_chmod(const char *filename, int mode) |
| 753 | { |
| 754 | wchar_t wfilename[MAX_PATH]; |
| 755 | if (xutftowcs_path(wfilename, filename) < 0) |
| 756 | return -1; |
| 757 | return _wchmod(wfilename, mode); |
| 758 | } |
| 759 | |
Johannes Sixt | a6d15bc | 2010-01-15 21:12:21 +0100 | [diff] [blame] | 760 | /* |
| 761 | * The unit of FILETIME is 100-nanoseconds since January 1, 1601, UTC. |
| 762 | * Returns the 100-nanoseconds ("hekto nanoseconds") since the epoch. |
| 763 | */ |
| 764 | static inline long long filetime_to_hnsec(const FILETIME *ft) |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 765 | { |
| 766 | long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime; |
Johannes Sixt | a6d15bc | 2010-01-15 21:12:21 +0100 | [diff] [blame] | 767 | /* Windows to Unix Epoch conversion */ |
| 768 | return winTime - 116444736000000000LL; |
| 769 | } |
| 770 | |
Karsten Blees | d7e8c87 | 2018-10-23 03:23:22 -0700 | [diff] [blame] | 771 | static inline void filetime_to_timespec(const FILETIME *ft, struct timespec *ts) |
Johannes Sixt | a6d15bc | 2010-01-15 21:12:21 +0100 | [diff] [blame] | 772 | { |
Karsten Blees | d7e8c87 | 2018-10-23 03:23:22 -0700 | [diff] [blame] | 773 | long long hnsec = filetime_to_hnsec(ft); |
| 774 | ts->tv_sec = (time_t)(hnsec / 10000000); |
| 775 | ts->tv_nsec = (hnsec % 10000000) * 100; |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 776 | } |
| 777 | |
Johannes Schindelin | 4b0abd5 | 2016-01-26 15:34:52 +0100 | [diff] [blame] | 778 | /** |
| 779 | * Verifies that safe_create_leading_directories() would succeed. |
| 780 | */ |
| 781 | static int has_valid_directory_prefix(wchar_t *wfilename) |
| 782 | { |
| 783 | int n = wcslen(wfilename); |
| 784 | |
| 785 | while (n > 0) { |
| 786 | wchar_t c = wfilename[--n]; |
| 787 | DWORD attributes; |
| 788 | |
| 789 | if (!is_dir_sep(c)) |
| 790 | continue; |
| 791 | |
| 792 | wfilename[n] = L'\0'; |
| 793 | attributes = GetFileAttributesW(wfilename); |
| 794 | wfilename[n] = c; |
Johannes Schindelin | 82ba119 | 2022-07-29 10:05:52 +0000 | [diff] [blame] | 795 | if (attributes & |
| 796 | (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE)) |
Johannes Schindelin | 4b0abd5 | 2016-01-26 15:34:52 +0100 | [diff] [blame] | 797 | return 1; |
| 798 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 799 | switch (GetLastError()) { |
| 800 | case ERROR_PATH_NOT_FOUND: |
| 801 | continue; |
| 802 | case ERROR_FILE_NOT_FOUND: |
| 803 | /* This implies parent directory exists. */ |
| 804 | return 1; |
| 805 | } |
| 806 | return 0; |
| 807 | } |
| 808 | return 1; |
| 809 | } |
| 810 | |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 811 | /* We keep the do_lstat code in a separate function to avoid recursion. |
| 812 | * When a path ends with a slash, the stat will fail with ENOENT. In |
| 813 | * this case, we strip the trailing slashes and stat again. |
Pat Thoyts | 9b9784c | 2010-03-17 15:17:34 +0000 | [diff] [blame] | 814 | * |
| 815 | * If follow is true then act like stat() and report on the link |
| 816 | * target. Otherwise report on the link itself. |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 817 | */ |
Pat Thoyts | 9b9784c | 2010-03-17 15:17:34 +0000 | [diff] [blame] | 818 | static int do_lstat(int follow, const char *file_name, struct stat *buf) |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 819 | { |
| 820 | WIN32_FILE_ATTRIBUTE_DATA fdata; |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 821 | wchar_t wfilename[MAX_PATH]; |
| 822 | if (xutftowcs_path(wfilename, file_name) < 0) |
| 823 | return -1; |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 824 | |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 825 | if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) { |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 826 | buf->st_ino = 0; |
| 827 | buf->st_gid = 0; |
| 828 | buf->st_uid = 0; |
Johannes Sixt | 180964f | 2008-08-18 22:01:06 +0200 | [diff] [blame] | 829 | buf->st_nlink = 1; |
Dmitry Potapov | 444dc90 | 2008-09-27 12:43:01 +0400 | [diff] [blame] | 830 | buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes); |
Johannes Schindelin | 1d4e4cd | 2009-03-05 17:05:12 +0100 | [diff] [blame] | 831 | buf->st_size = fdata.nFileSizeLow | |
| 832 | (((off_t)fdata.nFileSizeHigh)<<32); |
Dmitry Potapov | 8252df6 | 2008-09-27 12:39:45 +0400 | [diff] [blame] | 833 | buf->st_dev = buf->st_rdev = 0; /* not used by Git */ |
Karsten Blees | d7e8c87 | 2018-10-23 03:23:22 -0700 | [diff] [blame] | 834 | filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim)); |
| 835 | filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim)); |
| 836 | filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim)); |
Pat Thoyts | 9b9784c | 2010-03-17 15:17:34 +0000 | [diff] [blame] | 837 | if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 838 | WIN32_FIND_DATAW findbuf; |
| 839 | HANDLE handle = FindFirstFileW(wfilename, &findbuf); |
Pat Thoyts | 9b9784c | 2010-03-17 15:17:34 +0000 | [diff] [blame] | 840 | if (handle != INVALID_HANDLE_VALUE) { |
| 841 | if ((findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) && |
| 842 | (findbuf.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) { |
| 843 | if (follow) { |
| 844 | char buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 845 | buf->st_size = readlink(file_name, buffer, MAXIMUM_REPARSE_DATA_BUFFER_SIZE); |
| 846 | } else { |
| 847 | buf->st_mode = S_IFLNK; |
| 848 | } |
| 849 | buf->st_mode |= S_IREAD; |
| 850 | if (!(findbuf.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) |
| 851 | buf->st_mode |= S_IWRITE; |
| 852 | } |
| 853 | FindClose(handle); |
| 854 | } |
| 855 | } |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 856 | return 0; |
| 857 | } |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 858 | switch (GetLastError()) { |
| 859 | case ERROR_ACCESS_DENIED: |
| 860 | case ERROR_SHARING_VIOLATION: |
| 861 | case ERROR_LOCK_VIOLATION: |
| 862 | case ERROR_SHARING_BUFFER_EXCEEDED: |
| 863 | errno = EACCES; |
| 864 | break; |
| 865 | case ERROR_BUFFER_OVERFLOW: |
| 866 | errno = ENAMETOOLONG; |
| 867 | break; |
| 868 | case ERROR_NOT_ENOUGH_MEMORY: |
| 869 | errno = ENOMEM; |
| 870 | break; |
Johannes Schindelin | 4b0abd5 | 2016-01-26 15:34:52 +0100 | [diff] [blame] | 871 | case ERROR_PATH_NOT_FOUND: |
| 872 | if (!has_valid_directory_prefix(wfilename)) { |
| 873 | errno = ENOTDIR; |
| 874 | break; |
| 875 | } |
| 876 | /* fallthru */ |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 877 | default: |
| 878 | errno = ENOENT; |
| 879 | break; |
| 880 | } |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 881 | return -1; |
| 882 | } |
| 883 | |
| 884 | /* We provide our own lstat/fstat functions, since the provided |
| 885 | * lstat/fstat functions are so slow. These stat functions are |
| 886 | * tailored for Git's usage (read: fast), and are not meant to be |
| 887 | * complete. Note that Git stat()s are redirected to mingw_lstat() |
| 888 | * too, since Windows doesn't really handle symlinks that well. |
| 889 | */ |
Pat Thoyts | 9b9784c | 2010-03-17 15:17:34 +0000 | [diff] [blame] | 890 | static int do_stat_internal(int follow, const char *file_name, struct stat *buf) |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 891 | { |
| 892 | int namelen; |
Karsten Blees | 58aa3d2 | 2011-01-07 18:04:16 +0100 | [diff] [blame] | 893 | char alt_name[PATH_MAX]; |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 894 | |
Pat Thoyts | 9b9784c | 2010-03-17 15:17:34 +0000 | [diff] [blame] | 895 | if (!do_lstat(follow, file_name, buf)) |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 896 | return 0; |
| 897 | |
| 898 | /* if file_name ended in a '/', Windows returned ENOENT; |
| 899 | * try again without trailing slashes |
| 900 | */ |
| 901 | if (errno != ENOENT) |
| 902 | return -1; |
| 903 | |
| 904 | namelen = strlen(file_name); |
| 905 | if (namelen && file_name[namelen-1] != '/') |
| 906 | return -1; |
| 907 | while (namelen && file_name[namelen-1] == '/') |
| 908 | --namelen; |
| 909 | if (!namelen || namelen >= PATH_MAX) |
| 910 | return -1; |
| 911 | |
| 912 | memcpy(alt_name, file_name, namelen); |
| 913 | alt_name[namelen] = 0; |
Pat Thoyts | 9b9784c | 2010-03-17 15:17:34 +0000 | [diff] [blame] | 914 | return do_lstat(follow, alt_name, buf); |
| 915 | } |
| 916 | |
Johannes Schindelin | 7bf1983 | 2018-10-23 03:23:19 -0700 | [diff] [blame] | 917 | static int get_file_info_by_handle(HANDLE hnd, struct stat *buf) |
| 918 | { |
| 919 | BY_HANDLE_FILE_INFORMATION fdata; |
| 920 | |
| 921 | if (!GetFileInformationByHandle(hnd, &fdata)) { |
| 922 | errno = err_win_to_posix(GetLastError()); |
| 923 | return -1; |
| 924 | } |
| 925 | |
| 926 | buf->st_ino = 0; |
| 927 | buf->st_gid = 0; |
| 928 | buf->st_uid = 0; |
| 929 | buf->st_nlink = 1; |
| 930 | buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes); |
| 931 | buf->st_size = fdata.nFileSizeLow | |
| 932 | (((off_t)fdata.nFileSizeHigh)<<32); |
| 933 | buf->st_dev = buf->st_rdev = 0; /* not used by Git */ |
Karsten Blees | d7e8c87 | 2018-10-23 03:23:22 -0700 | [diff] [blame] | 934 | filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim)); |
| 935 | filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim)); |
| 936 | filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim)); |
Johannes Schindelin | 7bf1983 | 2018-10-23 03:23:19 -0700 | [diff] [blame] | 937 | return 0; |
| 938 | } |
| 939 | |
Pat Thoyts | 9b9784c | 2010-03-17 15:17:34 +0000 | [diff] [blame] | 940 | int mingw_lstat(const char *file_name, struct stat *buf) |
| 941 | { |
| 942 | return do_stat_internal(0, file_name, buf); |
| 943 | } |
| 944 | int mingw_stat(const char *file_name, struct stat *buf) |
| 945 | { |
| 946 | return do_stat_internal(1, file_name, buf); |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 947 | } |
| 948 | |
Johannes Sixt | 180964f | 2008-08-18 22:01:06 +0200 | [diff] [blame] | 949 | int mingw_fstat(int fd, struct stat *buf) |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 950 | { |
| 951 | HANDLE fh = (HANDLE)_get_osfhandle(fd); |
Karsten Blees | d75e697 | 2018-10-23 03:23:21 -0700 | [diff] [blame] | 952 | DWORD avail, type = GetFileType(fh) & ~FILE_TYPE_REMOTE; |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 953 | |
Karsten Blees | d75e697 | 2018-10-23 03:23:21 -0700 | [diff] [blame] | 954 | switch (type) { |
| 955 | case FILE_TYPE_DISK: |
| 956 | return get_file_info_by_handle(fh, buf); |
| 957 | |
| 958 | case FILE_TYPE_CHAR: |
| 959 | case FILE_TYPE_PIPE: |
| 960 | /* initialize stat fields */ |
| 961 | memset(buf, 0, sizeof(*buf)); |
| 962 | buf->st_nlink = 1; |
| 963 | |
| 964 | if (type == FILE_TYPE_CHAR) { |
| 965 | buf->st_mode = _S_IFCHR; |
| 966 | } else { |
| 967 | buf->st_mode = _S_IFIFO; |
| 968 | if (PeekNamedPipe(fh, NULL, 0, NULL, &avail, NULL)) |
| 969 | buf->st_size = avail; |
| 970 | } |
| 971 | return 0; |
| 972 | |
| 973 | default: |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 974 | errno = EBADF; |
| 975 | return -1; |
| 976 | } |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 977 | } |
| 978 | |
Johannes Sixt | 7c0ffa1 | 2007-09-07 13:05:00 +0200 | [diff] [blame] | 979 | static inline void time_t_to_filetime(time_t t, FILETIME *ft) |
| 980 | { |
| 981 | long long winTime = t * 10000000LL + 116444736000000000LL; |
| 982 | ft->dwLowDateTime = winTime; |
| 983 | ft->dwHighDateTime = winTime >> 32; |
| 984 | } |
| 985 | |
| 986 | int mingw_utime (const char *file_name, const struct utimbuf *times) |
| 987 | { |
| 988 | FILETIME mft, aft; |
Tao Klerks | 090a308 | 2022-03-02 06:05:23 +0000 | [diff] [blame] | 989 | int rc; |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 990 | DWORD attrs; |
| 991 | wchar_t wfilename[MAX_PATH]; |
Tao Klerks | 090a308 | 2022-03-02 06:05:23 +0000 | [diff] [blame] | 992 | HANDLE osfilehandle; |
| 993 | |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 994 | if (xutftowcs_path(wfilename, file_name) < 0) |
| 995 | return -1; |
Johannes Sixt | 7c0ffa1 | 2007-09-07 13:05:00 +0200 | [diff] [blame] | 996 | |
| 997 | /* must have write permission */ |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 998 | attrs = GetFileAttributesW(wfilename); |
Johannes Sixt | 852f098 | 2010-03-30 09:46:23 +0200 | [diff] [blame] | 999 | if (attrs != INVALID_FILE_ATTRIBUTES && |
| 1000 | (attrs & FILE_ATTRIBUTE_READONLY)) { |
| 1001 | /* ignore errors here; open() will report them */ |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 1002 | SetFileAttributesW(wfilename, attrs & ~FILE_ATTRIBUTE_READONLY); |
Johannes Sixt | 852f098 | 2010-03-30 09:46:23 +0200 | [diff] [blame] | 1003 | } |
| 1004 | |
Tao Klerks | 090a308 | 2022-03-02 06:05:23 +0000 | [diff] [blame] | 1005 | osfilehandle = CreateFileW(wfilename, |
| 1006 | FILE_WRITE_ATTRIBUTES, |
| 1007 | 0 /*FileShare.None*/, |
| 1008 | NULL, |
| 1009 | OPEN_EXISTING, |
| 1010 | (attrs != INVALID_FILE_ATTRIBUTES && |
| 1011 | (attrs & FILE_ATTRIBUTE_DIRECTORY)) ? |
| 1012 | FILE_FLAG_BACKUP_SEMANTICS : 0, |
| 1013 | NULL); |
| 1014 | if (osfilehandle == INVALID_HANDLE_VALUE) { |
| 1015 | errno = err_win_to_posix(GetLastError()); |
Johannes Sixt | 852f098 | 2010-03-30 09:46:23 +0200 | [diff] [blame] | 1016 | rc = -1; |
| 1017 | goto revert_attrs; |
| 1018 | } |
Johannes Sixt | 7c0ffa1 | 2007-09-07 13:05:00 +0200 | [diff] [blame] | 1019 | |
SZEDER Gábor | ded2d47 | 2010-07-13 01:42:03 +0200 | [diff] [blame] | 1020 | if (times) { |
| 1021 | time_t_to_filetime(times->modtime, &mft); |
| 1022 | time_t_to_filetime(times->actime, &aft); |
| 1023 | } else { |
| 1024 | GetSystemTimeAsFileTime(&mft); |
| 1025 | aft = mft; |
| 1026 | } |
Tao Klerks | 090a308 | 2022-03-02 06:05:23 +0000 | [diff] [blame] | 1027 | |
| 1028 | if (!SetFileTime(osfilehandle, NULL, &aft, &mft)) { |
Johannes Sixt | 7c0ffa1 | 2007-09-07 13:05:00 +0200 | [diff] [blame] | 1029 | errno = EINVAL; |
| 1030 | rc = -1; |
| 1031 | } else |
| 1032 | rc = 0; |
Tao Klerks | 090a308 | 2022-03-02 06:05:23 +0000 | [diff] [blame] | 1033 | |
| 1034 | if (osfilehandle != INVALID_HANDLE_VALUE) |
| 1035 | CloseHandle(osfilehandle); |
Johannes Sixt | 852f098 | 2010-03-30 09:46:23 +0200 | [diff] [blame] | 1036 | |
| 1037 | revert_attrs: |
| 1038 | if (attrs != INVALID_FILE_ATTRIBUTES && |
| 1039 | (attrs & FILE_ATTRIBUTE_READONLY)) { |
| 1040 | /* ignore errors again */ |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 1041 | SetFileAttributesW(wfilename, attrs); |
Johannes Sixt | 852f098 | 2010-03-30 09:46:23 +0200 | [diff] [blame] | 1042 | } |
Johannes Sixt | 7c0ffa1 | 2007-09-07 13:05:00 +0200 | [diff] [blame] | 1043 | return rc; |
| 1044 | } |
| 1045 | |
Johannes Schindelin | 9ee0540 | 2018-03-19 17:49:22 +0100 | [diff] [blame] | 1046 | #undef strftime |
| 1047 | size_t mingw_strftime(char *s, size_t max, |
| 1048 | const char *format, const struct tm *tm) |
| 1049 | { |
Matthias Aßhauer | a748f3f | 2020-04-08 17:58:49 +0000 | [diff] [blame] | 1050 | /* a pointer to the original strftime in case we can't find the UCRT version */ |
| 1051 | static size_t (*fallback)(char *, size_t, const char *, const struct tm *) = strftime; |
| 1052 | size_t ret; |
Matthias Aßhauer | 4a9b204 | 2022-01-08 16:02:30 +0000 | [diff] [blame] | 1053 | DECLARE_PROC_ADDR(ucrtbase.dll, size_t, __cdecl, strftime, char *, size_t, |
Matthias Aßhauer | a748f3f | 2020-04-08 17:58:49 +0000 | [diff] [blame] | 1054 | const char *, const struct tm *); |
| 1055 | |
| 1056 | if (INIT_PROC_ADDR(strftime)) |
| 1057 | ret = strftime(s, max, format, tm); |
| 1058 | else |
| 1059 | ret = fallback(s, max, format, tm); |
Johannes Schindelin | 9ee0540 | 2018-03-19 17:49:22 +0100 | [diff] [blame] | 1060 | |
| 1061 | if (!ret && errno == EINVAL) |
| 1062 | die("invalid strftime format: '%s'", format); |
| 1063 | return ret; |
| 1064 | } |
| 1065 | |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 1066 | unsigned int sleep (unsigned int seconds) |
| 1067 | { |
| 1068 | Sleep(seconds*1000); |
| 1069 | return 0; |
| 1070 | } |
| 1071 | |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 1072 | char *mingw_mktemp(char *template) |
| 1073 | { |
| 1074 | wchar_t wtemplate[MAX_PATH]; |
| 1075 | if (xutftowcs_path(wtemplate, template) < 0) |
| 1076 | return NULL; |
| 1077 | if (!_wmktemp(wtemplate)) |
| 1078 | return NULL; |
| 1079 | if (xwcstoutf(template, wtemplate, strlen(template) + 1) < 0) |
| 1080 | return NULL; |
| 1081 | return template; |
| 1082 | } |
| 1083 | |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 1084 | int mkstemp(char *template) |
| 1085 | { |
René Scharfe | ae25974 | 2022-07-15 05:58:50 +0200 | [diff] [blame] | 1086 | return git_mkstemp_mode(template, 0600); |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | int gettimeofday(struct timeval *tv, void *tz) |
| 1090 | { |
Johannes Sixt | a6d15bc | 2010-01-15 21:12:21 +0100 | [diff] [blame] | 1091 | FILETIME ft; |
| 1092 | long long hnsec; |
| 1093 | |
| 1094 | GetSystemTimeAsFileTime(&ft); |
| 1095 | hnsec = filetime_to_hnsec(&ft); |
| 1096 | tv->tv_sec = hnsec / 10000000; |
| 1097 | tv->tv_usec = (hnsec % 10000000) / 10; |
Johannes Sixt | a42a0c2 | 2007-12-01 21:51:20 +0100 | [diff] [blame] | 1098 | return 0; |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 1099 | } |
| 1100 | |
Johannes Sixt | 897bb8c | 2007-12-07 22:05:36 +0100 | [diff] [blame] | 1101 | int pipe(int filedes[2]) |
| 1102 | { |
Johannes Sixt | 3e34d66 | 2010-01-15 21:12:17 +0100 | [diff] [blame] | 1103 | HANDLE h[2]; |
Johannes Sixt | 897bb8c | 2007-12-07 22:05:36 +0100 | [diff] [blame] | 1104 | |
Johannes Sixt | 3e34d66 | 2010-01-15 21:12:17 +0100 | [diff] [blame] | 1105 | /* this creates non-inheritable handles */ |
| 1106 | if (!CreatePipe(&h[0], &h[1], NULL, 8192)) { |
| 1107 | errno = err_win_to_posix(GetLastError()); |
Johannes Sixt | 897bb8c | 2007-12-07 22:05:36 +0100 | [diff] [blame] | 1108 | return -1; |
| 1109 | } |
Johannes Schindelin | 7c00bc3 | 2016-01-15 14:24:34 +0100 | [diff] [blame] | 1110 | filedes[0] = _open_osfhandle(HCAST(int, h[0]), O_NOINHERIT); |
Johannes Sixt | 3e34d66 | 2010-01-15 21:12:17 +0100 | [diff] [blame] | 1111 | if (filedes[0] < 0) { |
Johannes Sixt | 897bb8c | 2007-12-07 22:05:36 +0100 | [diff] [blame] | 1112 | CloseHandle(h[0]); |
| 1113 | CloseHandle(h[1]); |
| 1114 | return -1; |
| 1115 | } |
Johannes Schindelin | 7c00bc3 | 2016-01-15 14:24:34 +0100 | [diff] [blame] | 1116 | filedes[1] = _open_osfhandle(HCAST(int, h[1]), O_NOINHERIT); |
Jose F. Morales | c3cb7b6 | 2015-08-28 09:43:37 +0000 | [diff] [blame] | 1117 | if (filedes[1] < 0) { |
Johannes Sixt | 897bb8c | 2007-12-07 22:05:36 +0100 | [diff] [blame] | 1118 | close(filedes[0]); |
Johannes Sixt | 897bb8c | 2007-12-07 22:05:36 +0100 | [diff] [blame] | 1119 | CloseHandle(h[1]); |
| 1120 | return -1; |
| 1121 | } |
Johannes Sixt | 897bb8c | 2007-12-07 22:05:36 +0100 | [diff] [blame] | 1122 | return 0; |
| 1123 | } |
| 1124 | |
Carlo Marcelo Arenas Belón | 9e12400 | 2021-11-27 10:15:32 +0000 | [diff] [blame] | 1125 | #ifndef __MINGW64__ |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 1126 | struct tm *gmtime_r(const time_t *timep, struct tm *result) |
| 1127 | { |
Doan Tran Cong Danh | 0109d67 | 2019-11-28 19:25:05 +0700 | [diff] [blame] | 1128 | if (gmtime_s(result, timep) == 0) |
| 1129 | return result; |
| 1130 | return NULL; |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | struct tm *localtime_r(const time_t *timep, struct tm *result) |
| 1134 | { |
Doan Tran Cong Danh | 0109d67 | 2019-11-28 19:25:05 +0700 | [diff] [blame] | 1135 | if (localtime_s(result, timep) == 0) |
| 1136 | return result; |
| 1137 | return NULL; |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 1138 | } |
Carlo Marcelo Arenas Belón | 9e12400 | 2021-11-27 10:15:32 +0000 | [diff] [blame] | 1139 | #endif |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 1140 | |
Johannes Sixt | 25fe217 | 2008-03-05 21:51:27 +0100 | [diff] [blame] | 1141 | char *mingw_getcwd(char *pointer, int len) |
| 1142 | { |
Johannes Schindelin | 937974f | 2018-10-23 03:52:48 -0700 | [diff] [blame] | 1143 | wchar_t cwd[MAX_PATH], wpointer[MAX_PATH]; |
| 1144 | DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd); |
| 1145 | |
| 1146 | if (!ret || ret >= ARRAY_SIZE(cwd)) { |
| 1147 | errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError()); |
| 1148 | return NULL; |
| 1149 | } |
| 1150 | ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer)); |
Anton Serbulov | 4745fee | 2018-10-23 03:52:49 -0700 | [diff] [blame] | 1151 | if (!ret && GetLastError() == ERROR_ACCESS_DENIED) { |
| 1152 | HANDLE hnd = CreateFileW(cwd, 0, |
| 1153 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 1154 | OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 1155 | if (hnd == INVALID_HANDLE_VALUE) |
| 1156 | return NULL; |
| 1157 | ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0); |
| 1158 | CloseHandle(hnd); |
| 1159 | if (!ret || ret >= ARRAY_SIZE(wpointer)) |
| 1160 | return NULL; |
| 1161 | if (xwcstoutf(pointer, normalize_ntpath(wpointer), len) < 0) |
| 1162 | return NULL; |
| 1163 | return pointer; |
| 1164 | } |
Johannes Schindelin | 937974f | 2018-10-23 03:52:48 -0700 | [diff] [blame] | 1165 | if (!ret || ret >= ARRAY_SIZE(wpointer)) |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 1166 | return NULL; |
Johannes Schindelin | e2724c1 | 2022-01-19 18:56:01 +0000 | [diff] [blame] | 1167 | if (GetFileAttributesW(wpointer) == INVALID_FILE_ATTRIBUTES) { |
| 1168 | errno = ENOENT; |
| 1169 | return NULL; |
| 1170 | } |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 1171 | if (xwcstoutf(pointer, wpointer, len) < 0) |
| 1172 | return NULL; |
Johannes Sixt | 8e9b208 | 2016-04-02 21:03:14 +0200 | [diff] [blame] | 1173 | convert_slashes(pointer); |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 1174 | return pointer; |
Johannes Sixt | 25fe217 | 2008-03-05 21:51:27 +0100 | [diff] [blame] | 1175 | } |
| 1176 | |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1177 | /* |
Johannes Schindelin | 2ef2ae2 | 2018-11-15 03:22:40 -0800 | [diff] [blame] | 1178 | * See "Parsing C++ Command-Line Arguments" at Microsoft's Docs: |
| 1179 | * https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1180 | */ |
Johannes Schindelin | 9e9da23 | 2019-01-17 12:14:48 -0800 | [diff] [blame] | 1181 | static const char *quote_arg_msvc(const char *arg) |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1182 | { |
| 1183 | /* count chars to quote */ |
| 1184 | int len = 0, n = 0; |
| 1185 | int force_quotes = 0; |
| 1186 | char *q, *d; |
| 1187 | const char *p = arg; |
| 1188 | if (!*p) force_quotes = 1; |
| 1189 | while (*p) { |
Johannes Sixt | 3aea1a5 | 2009-03-24 21:43:02 +0100 | [diff] [blame] | 1190 | if (isspace(*p) || *p == '*' || *p == '?' || *p == '{' || *p == '\'') |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1191 | force_quotes = 1; |
| 1192 | else if (*p == '"') |
| 1193 | n++; |
| 1194 | else if (*p == '\\') { |
| 1195 | int count = 0; |
| 1196 | while (*p == '\\') { |
| 1197 | count++; |
| 1198 | p++; |
| 1199 | len++; |
| 1200 | } |
Johannes Schindelin | 6d86841 | 2019-09-13 16:32:43 +0200 | [diff] [blame] | 1201 | if (*p == '"' || !*p) |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1202 | n += count*2 + 1; |
| 1203 | continue; |
| 1204 | } |
| 1205 | len++; |
| 1206 | p++; |
| 1207 | } |
| 1208 | if (!force_quotes && n == 0) |
| 1209 | return arg; |
| 1210 | |
| 1211 | /* insert \ where necessary */ |
Jeff King | 50a6c8e | 2016-02-22 17:44:35 -0500 | [diff] [blame] | 1212 | d = q = xmalloc(st_add3(len, n, 3)); |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1213 | *d++ = '"'; |
| 1214 | while (*arg) { |
| 1215 | if (*arg == '"') |
| 1216 | *d++ = '\\'; |
| 1217 | else if (*arg == '\\') { |
| 1218 | int count = 0; |
| 1219 | while (*arg == '\\') { |
| 1220 | count++; |
| 1221 | *d++ = *arg++; |
| 1222 | } |
Johannes Schindelin | 6d86841 | 2019-09-13 16:32:43 +0200 | [diff] [blame] | 1223 | if (*arg == '"' || !*arg) { |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1224 | while (count-- > 0) |
| 1225 | *d++ = '\\'; |
Johannes Schindelin | 6d86841 | 2019-09-13 16:32:43 +0200 | [diff] [blame] | 1226 | /* don't escape the surrounding end quote */ |
| 1227 | if (!*arg) |
| 1228 | break; |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1229 | *d++ = '\\'; |
| 1230 | } |
| 1231 | } |
| 1232 | *d++ = *arg++; |
| 1233 | } |
| 1234 | *d++ = '"'; |
Johannes Schindelin | 6d86841 | 2019-09-13 16:32:43 +0200 | [diff] [blame] | 1235 | *d++ = '\0'; |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1236 | return q; |
| 1237 | } |
| 1238 | |
Johannes Schindelin | 9e9da23 | 2019-01-17 12:14:48 -0800 | [diff] [blame] | 1239 | #include "quote.h" |
| 1240 | |
| 1241 | static const char *quote_arg_msys2(const char *arg) |
| 1242 | { |
| 1243 | struct strbuf buf = STRBUF_INIT; |
| 1244 | const char *p2 = arg, *p; |
| 1245 | |
| 1246 | for (p = arg; *p; p++) { |
| 1247 | int ws = isspace(*p); |
Johannes Schindelin | 7d8b676 | 2019-09-19 23:43:03 +0200 | [diff] [blame] | 1248 | if (!ws && *p != '\\' && *p != '"' && *p != '{' && *p != '\'' && |
| 1249 | *p != '?' && *p != '*' && *p != '~') |
Johannes Schindelin | 9e9da23 | 2019-01-17 12:14:48 -0800 | [diff] [blame] | 1250 | continue; |
| 1251 | if (!buf.len) |
| 1252 | strbuf_addch(&buf, '"'); |
| 1253 | if (p != p2) |
| 1254 | strbuf_add(&buf, p2, p - p2); |
Johannes Schindelin | 7d8b676 | 2019-09-19 23:43:03 +0200 | [diff] [blame] | 1255 | if (*p == '\\' || *p == '"') |
Johannes Schindelin | 9e9da23 | 2019-01-17 12:14:48 -0800 | [diff] [blame] | 1256 | strbuf_addch(&buf, '\\'); |
| 1257 | p2 = p; |
| 1258 | } |
| 1259 | |
| 1260 | if (p == arg) |
| 1261 | strbuf_addch(&buf, '"'); |
| 1262 | else if (!buf.len) |
| 1263 | return arg; |
| 1264 | else |
Johannes Schindelin | 04522ed | 2019-09-19 23:38:33 +0200 | [diff] [blame] | 1265 | strbuf_add(&buf, p2, p - p2); |
Johannes Schindelin | 9e9da23 | 2019-01-17 12:14:48 -0800 | [diff] [blame] | 1266 | |
| 1267 | strbuf_addch(&buf, '"'); |
| 1268 | return strbuf_detach(&buf, 0); |
| 1269 | } |
| 1270 | |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1271 | static const char *parse_interpreter(const char *cmd) |
| 1272 | { |
| 1273 | static char buf[100]; |
| 1274 | char *p, *opt; |
| 1275 | int n, fd; |
| 1276 | |
| 1277 | /* don't even try a .exe */ |
| 1278 | n = strlen(cmd); |
| 1279 | if (n >= 4 && !strcasecmp(cmd+n-4, ".exe")) |
| 1280 | return NULL; |
| 1281 | |
| 1282 | fd = open(cmd, O_RDONLY); |
| 1283 | if (fd < 0) |
| 1284 | return NULL; |
| 1285 | n = read(fd, buf, sizeof(buf)-1); |
| 1286 | close(fd); |
| 1287 | if (n < 4) /* at least '#!/x' and not error */ |
| 1288 | return NULL; |
| 1289 | |
| 1290 | if (buf[0] != '#' || buf[1] != '!') |
| 1291 | return NULL; |
| 1292 | buf[n] = '\0'; |
Peter Harris | bedc427 | 2009-05-23 10:04:47 +0200 | [diff] [blame] | 1293 | p = buf + strcspn(buf, "\r\n"); |
| 1294 | if (!*p) |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1295 | return NULL; |
| 1296 | |
| 1297 | *p = '\0'; |
| 1298 | if (!(p = strrchr(buf+2, '/')) && !(p = strrchr(buf+2, '\\'))) |
| 1299 | return NULL; |
| 1300 | /* strip options */ |
| 1301 | if ((opt = strchr(p+1, ' '))) |
| 1302 | *opt = '\0'; |
| 1303 | return p+1; |
| 1304 | } |
| 1305 | |
| 1306 | /* |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1307 | * exe_only means that we only want to detect .exe files, but not scripts |
| 1308 | * (which do not have an extension) |
| 1309 | */ |
René Scharfe | e0ca1ca | 2017-05-20 21:35:37 +0200 | [diff] [blame] | 1310 | static char *lookup_prog(const char *dir, int dirlen, const char *cmd, |
| 1311 | int isexe, int exe_only) |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1312 | { |
| 1313 | char path[MAX_PATH]; |
Adam Roben | 4e1a641 | 2019-08-24 15:38:56 -0700 | [diff] [blame] | 1314 | wchar_t wpath[MAX_PATH]; |
René Scharfe | e0ca1ca | 2017-05-20 21:35:37 +0200 | [diff] [blame] | 1315 | snprintf(path, sizeof(path), "%.*s\\%s.exe", dirlen, dir, cmd); |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1316 | |
Adam Roben | 4e1a641 | 2019-08-24 15:38:56 -0700 | [diff] [blame] | 1317 | if (xutftowcs_path(wpath, path) < 0) |
| 1318 | return NULL; |
| 1319 | |
| 1320 | if (!isexe && _waccess(wpath, F_OK) == 0) |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1321 | return xstrdup(path); |
Adam Roben | 4e1a641 | 2019-08-24 15:38:56 -0700 | [diff] [blame] | 1322 | wpath[wcslen(wpath)-4] = '\0'; |
| 1323 | if ((!exe_only || isexe) && _waccess(wpath, F_OK) == 0) { |
| 1324 | if (!(GetFileAttributesW(wpath) & FILE_ATTRIBUTE_DIRECTORY)) { |
| 1325 | path[strlen(path)-4] = '\0'; |
Eric Raible | fe77b69 | 2008-07-18 09:34:42 +0200 | [diff] [blame] | 1326 | return xstrdup(path); |
Adam Roben | 4e1a641 | 2019-08-24 15:38:56 -0700 | [diff] [blame] | 1327 | } |
| 1328 | } |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1329 | return NULL; |
| 1330 | } |
| 1331 | |
| 1332 | /* |
Ralf Wildenhues | 22e5e58 | 2010-08-22 13:12:12 +0200 | [diff] [blame] | 1333 | * Determines the absolute path of cmd using the split path in path. |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1334 | * If cmd contains a slash or backslash, no lookup is performed. |
| 1335 | */ |
René Scharfe | e0ca1ca | 2017-05-20 21:35:37 +0200 | [diff] [blame] | 1336 | static char *path_lookup(const char *cmd, int exe_only) |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1337 | { |
René Scharfe | e0ca1ca | 2017-05-20 21:35:37 +0200 | [diff] [blame] | 1338 | const char *path; |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1339 | char *prog = NULL; |
| 1340 | int len = strlen(cmd); |
| 1341 | int isexe = len >= 4 && !strcasecmp(cmd+len-4, ".exe"); |
| 1342 | |
René Scharfe | 2ce6d07 | 2020-02-22 19:51:19 +0100 | [diff] [blame] | 1343 | if (strpbrk(cmd, "/\\")) |
René Scharfe | e0ca1ca | 2017-05-20 21:35:37 +0200 | [diff] [blame] | 1344 | return xstrdup(cmd); |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1345 | |
René Scharfe | e0ca1ca | 2017-05-20 21:35:37 +0200 | [diff] [blame] | 1346 | path = mingw_getenv("PATH"); |
| 1347 | if (!path) |
| 1348 | return NULL; |
| 1349 | |
| 1350 | while (!prog) { |
| 1351 | const char *sep = strchrnul(path, ';'); |
| 1352 | int dirlen = sep - path; |
| 1353 | if (dirlen) |
| 1354 | prog = lookup_prog(path, dirlen, cmd, isexe, exe_only); |
| 1355 | if (!*sep) |
| 1356 | break; |
| 1357 | path = sep + 1; |
| 1358 | } |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1359 | |
| 1360 | return prog; |
| 1361 | } |
| 1362 | |
Matthias Aßhauer | 2bf46a9 | 2023-08-04 04:08:43 +0000 | [diff] [blame] | 1363 | char *mingw_locate_in_PATH(const char *cmd) |
| 1364 | { |
| 1365 | return path_lookup(cmd, 0); |
| 1366 | } |
| 1367 | |
Johannes Schindelin | fe21c6b | 2018-10-30 02:22:30 -0700 | [diff] [blame] | 1368 | static const wchar_t *wcschrnul(const wchar_t *s, wchar_t c) |
| 1369 | { |
| 1370 | while (*s && *s != c) |
| 1371 | s++; |
| 1372 | return s; |
| 1373 | } |
Karsten Blees | f279242 | 2014-07-17 17:38:02 +0200 | [diff] [blame] | 1374 | |
Johannes Schindelin | fe21c6b | 2018-10-30 02:22:30 -0700 | [diff] [blame] | 1375 | /* Compare only keys */ |
| 1376 | static int wenvcmp(const void *a, const void *b) |
| 1377 | { |
| 1378 | wchar_t *p = *(wchar_t **)a, *q = *(wchar_t **)b; |
| 1379 | size_t p_len, q_len; |
| 1380 | |
| 1381 | /* Find the keys */ |
| 1382 | p_len = wcschrnul(p, L'=') - p; |
| 1383 | q_len = wcschrnul(q, L'=') - q; |
| 1384 | |
| 1385 | /* If the length differs, include the shorter key's NUL */ |
| 1386 | if (p_len < q_len) |
| 1387 | p_len++; |
| 1388 | else if (p_len > q_len) |
| 1389 | p_len = q_len + 1; |
| 1390 | |
| 1391 | return _wcsnicmp(p, q, p_len); |
| 1392 | } |
| 1393 | |
Karsten Blees | df0e998 | 2014-07-17 17:38:00 +0200 | [diff] [blame] | 1394 | /* |
Johannes Schindelin | fe21c6b | 2018-10-30 02:22:30 -0700 | [diff] [blame] | 1395 | * Build an environment block combining the inherited environment |
| 1396 | * merged with the given list of settings. |
| 1397 | * |
| 1398 | * Values of the form "KEY=VALUE" in deltaenv override inherited values. |
| 1399 | * Values of the form "KEY" in deltaenv delete inherited values. |
| 1400 | * |
| 1401 | * Multiple entries in deltaenv for the same key are explicitly allowed. |
| 1402 | * |
| 1403 | * We return a contiguous block of UNICODE strings with a final trailing |
| 1404 | * zero word. |
Karsten Blees | df0e998 | 2014-07-17 17:38:00 +0200 | [diff] [blame] | 1405 | */ |
Karsten Blees | 77734da | 2014-07-17 17:38:01 +0200 | [diff] [blame] | 1406 | static wchar_t *make_environment_block(char **deltaenv) |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1407 | { |
Johannes Schindelin | fe21c6b | 2018-10-30 02:22:30 -0700 | [diff] [blame] | 1408 | wchar_t *wenv = GetEnvironmentStringsW(), *wdeltaenv, *result, *p; |
| 1409 | size_t wlen, s, delta_size, size; |
Karsten Blees | df0e998 | 2014-07-17 17:38:00 +0200 | [diff] [blame] | 1410 | |
Johannes Schindelin | fe21c6b | 2018-10-30 02:22:30 -0700 | [diff] [blame] | 1411 | wchar_t **array = NULL; |
| 1412 | size_t alloc = 0, nr = 0, i; |
Karsten Blees | df0e998 | 2014-07-17 17:38:00 +0200 | [diff] [blame] | 1413 | |
Johannes Schindelin | fe21c6b | 2018-10-30 02:22:30 -0700 | [diff] [blame] | 1414 | size = 1; /* for extra NUL at the end */ |
Karsten Blees | 77734da | 2014-07-17 17:38:01 +0200 | [diff] [blame] | 1415 | |
Johannes Schindelin | fe21c6b | 2018-10-30 02:22:30 -0700 | [diff] [blame] | 1416 | /* If there is no deltaenv to apply, simply return a copy. */ |
| 1417 | if (!deltaenv || !*deltaenv) { |
| 1418 | for (p = wenv; p && *p; ) { |
| 1419 | size_t s = wcslen(p) + 1; |
| 1420 | size += s; |
| 1421 | p += s; |
| 1422 | } |
Karsten Blees | 77734da | 2014-07-17 17:38:01 +0200 | [diff] [blame] | 1423 | |
René Scharfe | 6e57841 | 2023-01-01 22:16:48 +0100 | [diff] [blame] | 1424 | DUP_ARRAY(result, wenv, size); |
Johannes Schindelin | fe21c6b | 2018-10-30 02:22:30 -0700 | [diff] [blame] | 1425 | FreeEnvironmentStringsW(wenv); |
| 1426 | return result; |
Karsten Blees | df0e998 | 2014-07-17 17:38:00 +0200 | [diff] [blame] | 1427 | } |
Johannes Schindelin | fe21c6b | 2018-10-30 02:22:30 -0700 | [diff] [blame] | 1428 | |
| 1429 | /* |
| 1430 | * If there is a deltaenv, let's accumulate all keys into `array`, |
Johannes Schindelin | 97fff61 | 2019-09-30 10:21:54 -0700 | [diff] [blame] | 1431 | * sort them using the stable git_stable_qsort() and then copy, |
| 1432 | * skipping duplicate keys |
Johannes Schindelin | fe21c6b | 2018-10-30 02:22:30 -0700 | [diff] [blame] | 1433 | */ |
| 1434 | for (p = wenv; p && *p; ) { |
| 1435 | ALLOC_GROW(array, nr + 1, alloc); |
| 1436 | s = wcslen(p) + 1; |
| 1437 | array[nr++] = p; |
| 1438 | p += s; |
| 1439 | size += s; |
| 1440 | } |
| 1441 | |
| 1442 | /* (over-)assess size needed for wchar version of deltaenv */ |
| 1443 | for (delta_size = 0, i = 0; deltaenv[i]; i++) |
| 1444 | delta_size += strlen(deltaenv[i]) * 2 + 1; |
| 1445 | ALLOC_ARRAY(wdeltaenv, delta_size); |
| 1446 | |
| 1447 | /* convert the deltaenv, appending to array */ |
| 1448 | for (i = 0, p = wdeltaenv; deltaenv[i]; i++) { |
| 1449 | ALLOC_GROW(array, nr + 1, alloc); |
| 1450 | wlen = xutftowcs(p, deltaenv[i], wdeltaenv + delta_size - p); |
| 1451 | array[nr++] = p; |
| 1452 | p += wlen + 1; |
| 1453 | } |
| 1454 | |
Johannes Schindelin | 97fff61 | 2019-09-30 10:21:54 -0700 | [diff] [blame] | 1455 | git_stable_qsort(array, nr, sizeof(*array), wenvcmp); |
Johannes Schindelin | fe21c6b | 2018-10-30 02:22:30 -0700 | [diff] [blame] | 1456 | ALLOC_ARRAY(result, size + delta_size); |
| 1457 | |
| 1458 | for (p = result, i = 0; i < nr; i++) { |
| 1459 | /* Skip any duplicate keys; last one wins */ |
| 1460 | while (i + 1 < nr && !wenvcmp(array + i, array + i + 1)) |
| 1461 | i++; |
| 1462 | |
| 1463 | /* Skip "to delete" entry */ |
| 1464 | if (!wcschr(array[i], L'=')) |
| 1465 | continue; |
| 1466 | |
| 1467 | size = wcslen(array[i]) + 1; |
Denton Liu | 552fc50 | 2019-09-04 04:09:45 -0700 | [diff] [blame] | 1468 | COPY_ARRAY(p, array[i], size); |
Johannes Schindelin | fe21c6b | 2018-10-30 02:22:30 -0700 | [diff] [blame] | 1469 | p += size; |
| 1470 | } |
| 1471 | *p = L'\0'; |
| 1472 | |
| 1473 | free(array); |
| 1474 | free(wdeltaenv); |
| 1475 | FreeEnvironmentStringsW(wenv); |
| 1476 | return result; |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1477 | } |
| 1478 | |
Johannes Schindelin | 0e218f9 | 2018-10-30 11:40:07 -0700 | [diff] [blame] | 1479 | static void do_unset_environment_variables(void) |
| 1480 | { |
| 1481 | static int done; |
| 1482 | char *p = unset_environment_variables; |
| 1483 | |
| 1484 | if (done || !p) |
| 1485 | return; |
| 1486 | done = 1; |
| 1487 | |
| 1488 | for (;;) { |
| 1489 | char *comma = strchr(p, ','); |
| 1490 | |
| 1491 | if (comma) |
| 1492 | *comma = '\0'; |
| 1493 | unsetenv(p); |
| 1494 | if (!comma) |
| 1495 | break; |
| 1496 | p = comma + 1; |
| 1497 | } |
| 1498 | } |
| 1499 | |
Erik Faye-Lund | 52de4db | 2010-11-04 02:35:13 +0100 | [diff] [blame] | 1500 | struct pinfo_t { |
| 1501 | struct pinfo_t *next; |
| 1502 | pid_t pid; |
| 1503 | HANDLE proc; |
Ramsay Jones | 657b35f | 2013-04-27 20:18:55 +0100 | [diff] [blame] | 1504 | }; |
| 1505 | static struct pinfo_t *pinfo = NULL; |
Erik Faye-Lund | 52de4db | 2010-11-04 02:35:13 +0100 | [diff] [blame] | 1506 | CRITICAL_SECTION pinfo_cs; |
| 1507 | |
Johannes Schindelin | 9e9da23 | 2019-01-17 12:14:48 -0800 | [diff] [blame] | 1508 | /* Used to match and chomp off path components */ |
| 1509 | static inline int match_last_path_component(const char *path, size_t *len, |
| 1510 | const char *component) |
| 1511 | { |
| 1512 | size_t component_len = strlen(component); |
| 1513 | if (*len < component_len + 1 || |
| 1514 | !is_dir_sep(path[*len - component_len - 1]) || |
| 1515 | fspathncmp(path + *len - component_len, component, component_len)) |
| 1516 | return 0; |
| 1517 | *len -= component_len + 1; |
| 1518 | /* chomp off repeated dir separators */ |
| 1519 | while (*len > 0 && is_dir_sep(path[*len - 1])) |
| 1520 | (*len)--; |
| 1521 | return 1; |
| 1522 | } |
| 1523 | |
| 1524 | static int is_msys2_sh(const char *cmd) |
| 1525 | { |
Johannes Schindelin | e2ba3d6 | 2019-09-19 17:05:21 +0200 | [diff] [blame] | 1526 | if (!cmd) |
| 1527 | return 0; |
| 1528 | |
| 1529 | if (!strcmp(cmd, "sh")) { |
Johannes Schindelin | 9e9da23 | 2019-01-17 12:14:48 -0800 | [diff] [blame] | 1530 | static int ret = -1; |
| 1531 | char *p; |
| 1532 | |
| 1533 | if (ret >= 0) |
| 1534 | return ret; |
| 1535 | |
| 1536 | p = path_lookup(cmd, 0); |
| 1537 | if (!p) |
| 1538 | ret = 0; |
| 1539 | else { |
| 1540 | size_t len = strlen(p); |
| 1541 | |
| 1542 | ret = match_last_path_component(p, &len, "sh.exe") && |
| 1543 | match_last_path_component(p, &len, "bin") && |
| 1544 | match_last_path_component(p, &len, "usr"); |
| 1545 | free(p); |
| 1546 | } |
| 1547 | return ret; |
| 1548 | } |
Johannes Schindelin | e2ba3d6 | 2019-09-19 17:05:21 +0200 | [diff] [blame] | 1549 | |
| 1550 | if (ends_with(cmd, "\\sh.exe")) { |
| 1551 | static char *sh; |
| 1552 | |
| 1553 | if (!sh) |
| 1554 | sh = path_lookup("sh", 0); |
| 1555 | |
| 1556 | return !fspathcmp(cmd, sh); |
| 1557 | } |
| 1558 | |
Johannes Schindelin | 9e9da23 | 2019-01-17 12:14:48 -0800 | [diff] [blame] | 1559 | return 0; |
| 1560 | } |
| 1561 | |
Karsten Blees | 77734da | 2014-07-17 17:38:01 +0200 | [diff] [blame] | 1562 | static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaenv, |
Johannes Sixt | f9a2743 | 2010-04-11 22:40:12 +0200 | [diff] [blame] | 1563 | const char *dir, |
Johannes Sixt | 75301f9 | 2010-01-15 21:12:18 +0100 | [diff] [blame] | 1564 | int prepend_cmd, int fhin, int fhout, int fherr) |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1565 | { |
Johannes Schindelin | ac33519 | 2019-11-22 14:41:05 +0000 | [diff] [blame] | 1566 | static int restrict_handle_inheritance = -1; |
Johannes Schindelin | 9a780a3 | 2019-11-22 14:41:04 +0000 | [diff] [blame] | 1567 | STARTUPINFOEXW si; |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1568 | PROCESS_INFORMATION pi; |
Johannes Schindelin | 9a780a3 | 2019-11-22 14:41:04 +0000 | [diff] [blame] | 1569 | LPPROC_THREAD_ATTRIBUTE_LIST attr_list = NULL; |
| 1570 | HANDLE stdhandles[3]; |
| 1571 | DWORD stdhandles_count = 0; |
| 1572 | SIZE_T size; |
Karsten Blees | 7eb2619 | 2014-07-17 17:37:55 +0200 | [diff] [blame] | 1573 | struct strbuf args; |
| 1574 | wchar_t wcmd[MAX_PATH], wdir[MAX_PATH], *wargs, *wenvblk = NULL; |
| 1575 | unsigned flags = CREATE_UNICODE_ENVIRONMENT; |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1576 | BOOL ret; |
Johannes Schindelin | 0e218f9 | 2018-10-30 11:40:07 -0700 | [diff] [blame] | 1577 | HANDLE cons; |
Johannes Schindelin | 9e9da23 | 2019-01-17 12:14:48 -0800 | [diff] [blame] | 1578 | const char *(*quote_arg)(const char *arg) = |
Johannes Schindelin | 49f7a76 | 2019-09-19 17:05:45 +0200 | [diff] [blame] | 1579 | is_msys2_sh(cmd ? cmd : *argv) ? |
| 1580 | quote_arg_msys2 : quote_arg_msvc; |
Johannes Schindelin | 3efc128 | 2020-04-09 10:21:47 +0000 | [diff] [blame] | 1581 | const char *strace_env; |
Johannes Schindelin | 0e218f9 | 2018-10-30 11:40:07 -0700 | [diff] [blame] | 1582 | |
Johannes Schindelin | 4d0375c | 2019-11-30 10:36:39 +0000 | [diff] [blame] | 1583 | /* Make sure to override previous errors, if any */ |
| 1584 | errno = 0; |
| 1585 | |
Johannes Schindelin | ac33519 | 2019-11-22 14:41:05 +0000 | [diff] [blame] | 1586 | if (restrict_handle_inheritance < 0) |
| 1587 | restrict_handle_inheritance = core_restrict_inherited_handles; |
| 1588 | /* |
| 1589 | * The following code to restrict which handles are inherited seems |
| 1590 | * to work properly only on Windows 7 and later, so let's disable it |
| 1591 | * on Windows Vista and 2008. |
| 1592 | */ |
| 1593 | if (restrict_handle_inheritance < 0) |
| 1594 | restrict_handle_inheritance = GetVersion() >> 16 >= 7601; |
| 1595 | |
Johannes Schindelin | 0e218f9 | 2018-10-30 11:40:07 -0700 | [diff] [blame] | 1596 | do_unset_environment_variables(); |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1597 | |
| 1598 | /* Determine whether or not we are associated to a console */ |
Johannes Schindelin | 9423885 | 2019-06-27 02:37:19 -0700 | [diff] [blame] | 1599 | cons = CreateFileW(L"CONOUT$", GENERIC_WRITE, |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1600 | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, |
| 1601 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 1602 | if (cons == INVALID_HANDLE_VALUE) { |
| 1603 | /* There is no console associated with this process. |
| 1604 | * Since the child is a console process, Windows |
| 1605 | * would normally create a console window. But |
| 1606 | * since we'll be redirecting std streams, we do |
| 1607 | * not need the console. |
Alexander Gavrilov | 19fb896 | 2008-11-02 20:11:13 +0300 | [diff] [blame] | 1608 | * It is necessary to use DETACHED_PROCESS |
| 1609 | * instead of CREATE_NO_WINDOW to make ssh |
| 1610 | * recognize that it has no console. |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1611 | */ |
Karsten Blees | 7eb2619 | 2014-07-17 17:37:55 +0200 | [diff] [blame] | 1612 | flags |= DETACHED_PROCESS; |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1613 | } else { |
| 1614 | /* There is already a console. If we specified |
Alexander Gavrilov | 19fb896 | 2008-11-02 20:11:13 +0300 | [diff] [blame] | 1615 | * DETACHED_PROCESS here, too, Windows would |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1616 | * disassociate the child from the console. |
Alexander Gavrilov | 19fb896 | 2008-11-02 20:11:13 +0300 | [diff] [blame] | 1617 | * The same is true for CREATE_NO_WINDOW. |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1618 | * Go figure! |
| 1619 | */ |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1620 | CloseHandle(cons); |
| 1621 | } |
| 1622 | memset(&si, 0, sizeof(si)); |
Johannes Schindelin | 9a780a3 | 2019-11-22 14:41:04 +0000 | [diff] [blame] | 1623 | si.StartupInfo.cb = sizeof(si); |
| 1624 | si.StartupInfo.hStdInput = winansi_get_osfhandle(fhin); |
| 1625 | si.StartupInfo.hStdOutput = winansi_get_osfhandle(fhout); |
| 1626 | si.StartupInfo.hStdError = winansi_get_osfhandle(fherr); |
| 1627 | |
| 1628 | /* The list of handles cannot contain duplicates */ |
| 1629 | if (si.StartupInfo.hStdInput != INVALID_HANDLE_VALUE) |
| 1630 | stdhandles[stdhandles_count++] = si.StartupInfo.hStdInput; |
| 1631 | if (si.StartupInfo.hStdOutput != INVALID_HANDLE_VALUE && |
| 1632 | si.StartupInfo.hStdOutput != si.StartupInfo.hStdInput) |
| 1633 | stdhandles[stdhandles_count++] = si.StartupInfo.hStdOutput; |
| 1634 | if (si.StartupInfo.hStdError != INVALID_HANDLE_VALUE && |
| 1635 | si.StartupInfo.hStdError != si.StartupInfo.hStdInput && |
| 1636 | si.StartupInfo.hStdError != si.StartupInfo.hStdOutput) |
| 1637 | stdhandles[stdhandles_count++] = si.StartupInfo.hStdError; |
| 1638 | if (stdhandles_count) |
| 1639 | si.StartupInfo.dwFlags |= STARTF_USESTDHANDLES; |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1640 | |
Johannes Schindelin | eb7c786 | 2019-07-16 07:03:12 -0700 | [diff] [blame] | 1641 | if (*argv && !strcmp(cmd, *argv)) |
| 1642 | wcmd[0] = L'\0'; |
| 1643 | else if (xutftowcs_path(wcmd, cmd) < 0) |
Karsten Blees | 99c3c76 | 2011-01-16 18:27:53 +0100 | [diff] [blame] | 1644 | return -1; |
| 1645 | if (dir && xutftowcs_path(wdir, dir) < 0) |
| 1646 | return -1; |
| 1647 | |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1648 | /* concatenate argv, quoting args as we go */ |
| 1649 | strbuf_init(&args, 0); |
| 1650 | if (prepend_cmd) { |
| 1651 | char *quoted = (char *)quote_arg(cmd); |
| 1652 | strbuf_addstr(&args, quoted); |
| 1653 | if (quoted != cmd) |
| 1654 | free(quoted); |
| 1655 | } |
| 1656 | for (; *argv; argv++) { |
| 1657 | char *quoted = (char *)quote_arg(*argv); |
| 1658 | if (*args.buf) |
| 1659 | strbuf_addch(&args, ' '); |
| 1660 | strbuf_addstr(&args, quoted); |
| 1661 | if (quoted != *argv) |
| 1662 | free(quoted); |
| 1663 | } |
| 1664 | |
Johannes Schindelin | 3efc128 | 2020-04-09 10:21:47 +0000 | [diff] [blame] | 1665 | strace_env = getenv("GIT_STRACE_COMMANDS"); |
| 1666 | if (strace_env) { |
| 1667 | char *p = path_lookup("strace.exe", 1); |
| 1668 | if (!p) |
| 1669 | return error("strace not found!"); |
| 1670 | if (xutftowcs_path(wcmd, p) < 0) { |
| 1671 | free(p); |
| 1672 | return -1; |
| 1673 | } |
| 1674 | free(p); |
| 1675 | if (!strcmp("1", strace_env) || |
| 1676 | !strcasecmp("yes", strace_env) || |
| 1677 | !strcasecmp("true", strace_env)) |
| 1678 | strbuf_insert(&args, 0, "strace ", 7); |
| 1679 | else { |
| 1680 | const char *quoted = quote_arg(strace_env); |
| 1681 | struct strbuf buf = STRBUF_INIT; |
| 1682 | strbuf_addf(&buf, "strace -o %s ", quoted); |
| 1683 | if (quoted != strace_env) |
| 1684 | free((char *)quoted); |
| 1685 | strbuf_insert(&args, 0, buf.buf, buf.len); |
| 1686 | strbuf_release(&buf); |
| 1687 | } |
| 1688 | } |
| 1689 | |
Jeff King | 8d5b332 | 2016-02-29 05:02:59 -0500 | [diff] [blame] | 1690 | ALLOC_ARRAY(wargs, st_add(st_mult(2, args.len), 1)); |
Karsten Blees | 99c3c76 | 2011-01-16 18:27:53 +0100 | [diff] [blame] | 1691 | xutftowcs(wargs, args.buf, 2 * args.len + 1); |
| 1692 | strbuf_release(&args); |
| 1693 | |
Karsten Blees | 77734da | 2014-07-17 17:38:01 +0200 | [diff] [blame] | 1694 | wenvblk = make_environment_block(deltaenv); |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1695 | |
| 1696 | memset(&pi, 0, sizeof(pi)); |
Johannes Schindelin | 9a780a3 | 2019-11-22 14:41:04 +0000 | [diff] [blame] | 1697 | if (restrict_handle_inheritance && stdhandles_count && |
| 1698 | (InitializeProcThreadAttributeList(NULL, 1, 0, &size) || |
| 1699 | GetLastError() == ERROR_INSUFFICIENT_BUFFER) && |
| 1700 | (attr_list = (LPPROC_THREAD_ATTRIBUTE_LIST) |
| 1701 | (HeapAlloc(GetProcessHeap(), 0, size))) && |
| 1702 | InitializeProcThreadAttributeList(attr_list, 1, 0, &size) && |
| 1703 | UpdateProcThreadAttribute(attr_list, 0, |
| 1704 | PROC_THREAD_ATTRIBUTE_HANDLE_LIST, |
| 1705 | stdhandles, |
| 1706 | stdhandles_count * sizeof(HANDLE), |
| 1707 | NULL, NULL)) { |
| 1708 | si.lpAttributeList = attr_list; |
| 1709 | flags |= EXTENDED_STARTUPINFO_PRESENT; |
| 1710 | } |
| 1711 | |
| 1712 | ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL, |
| 1713 | stdhandles_count ? TRUE : FALSE, |
| 1714 | flags, wenvblk, dir ? wdir : NULL, |
| 1715 | &si.StartupInfo, &pi); |
| 1716 | |
| 1717 | /* |
| 1718 | * On Windows 2008 R2, it seems that specifying certain types of handles |
| 1719 | * (such as FILE_TYPE_CHAR or FILE_TYPE_PIPE) will always produce an |
| 1720 | * error. Rather than playing finicky and fragile games, let's just try |
| 1721 | * to detect this situation and simply try again without restricting any |
| 1722 | * handle inheritance. This is still better than failing to create |
| 1723 | * processes. |
| 1724 | */ |
| 1725 | if (!ret && restrict_handle_inheritance && stdhandles_count) { |
| 1726 | DWORD err = GetLastError(); |
| 1727 | struct strbuf buf = STRBUF_INIT; |
| 1728 | |
| 1729 | if (err != ERROR_NO_SYSTEM_RESOURCES && |
| 1730 | /* |
| 1731 | * On Windows 7 and earlier, handles on pipes and character |
| 1732 | * devices are inherited automatically, and cannot be |
| 1733 | * specified in the thread handle list. Rather than trying |
| 1734 | * to catch each and every corner case (and running the |
| 1735 | * chance of *still* forgetting a few), let's just fall |
| 1736 | * back to creating the process without trying to limit the |
| 1737 | * handle inheritance. |
| 1738 | */ |
| 1739 | !(err == ERROR_INVALID_PARAMETER && |
| 1740 | GetVersion() >> 16 < 9200) && |
| 1741 | !getenv("SUPPRESS_HANDLE_INHERITANCE_WARNING")) { |
| 1742 | DWORD fl = 0; |
| 1743 | int i; |
| 1744 | |
| 1745 | setenv("SUPPRESS_HANDLE_INHERITANCE_WARNING", "1", 1); |
| 1746 | |
| 1747 | for (i = 0; i < stdhandles_count; i++) { |
| 1748 | HANDLE h = stdhandles[i]; |
| 1749 | strbuf_addf(&buf, "handle #%d: %p (type %lx, " |
| 1750 | "handle info (%d) %lx\n", i, h, |
| 1751 | GetFileType(h), |
| 1752 | GetHandleInformation(h, &fl), |
| 1753 | fl); |
| 1754 | } |
| 1755 | strbuf_addstr(&buf, "\nThis is a bug; please report it " |
| 1756 | "at\nhttps://github.com/git-for-windows/" |
| 1757 | "git/issues/new\n\n" |
| 1758 | "To suppress this warning, please set " |
| 1759 | "the environment variable\n\n" |
| 1760 | "\tSUPPRESS_HANDLE_INHERITANCE_WARNING=1" |
| 1761 | "\n"); |
| 1762 | } |
| 1763 | restrict_handle_inheritance = 0; |
| 1764 | flags &= ~EXTENDED_STARTUPINFO_PRESENT; |
| 1765 | ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL, |
| 1766 | TRUE, flags, wenvblk, dir ? wdir : NULL, |
| 1767 | &si.StartupInfo, &pi); |
Johannes Schindelin | 4d0375c | 2019-11-30 10:36:39 +0000 | [diff] [blame] | 1768 | if (!ret) |
Johannes Schindelin | 9a780a3 | 2019-11-22 14:41:04 +0000 | [diff] [blame] | 1769 | errno = err_win_to_posix(GetLastError()); |
Johannes Schindelin | 4d0375c | 2019-11-30 10:36:39 +0000 | [diff] [blame] | 1770 | if (ret && buf.len) { |
Johannes Schindelin | 9a780a3 | 2019-11-22 14:41:04 +0000 | [diff] [blame] | 1771 | warning("failed to restrict file handles (%ld)\n\n%s", |
| 1772 | err, buf.buf); |
| 1773 | } |
| 1774 | strbuf_release(&buf); |
| 1775 | } else if (!ret) |
| 1776 | errno = err_win_to_posix(GetLastError()); |
| 1777 | |
| 1778 | if (si.lpAttributeList) |
| 1779 | DeleteProcThreadAttributeList(si.lpAttributeList); |
| 1780 | if (attr_list) |
| 1781 | HeapFree(GetProcessHeap(), 0, attr_list); |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1782 | |
Karsten Blees | 7eb2619 | 2014-07-17 17:37:55 +0200 | [diff] [blame] | 1783 | free(wenvblk); |
Karsten Blees | 99c3c76 | 2011-01-16 18:27:53 +0100 | [diff] [blame] | 1784 | free(wargs); |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1785 | |
Johannes Schindelin | 9a780a3 | 2019-11-22 14:41:04 +0000 | [diff] [blame] | 1786 | if (!ret) |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1787 | return -1; |
Johannes Schindelin | 9a780a3 | 2019-11-22 14:41:04 +0000 | [diff] [blame] | 1788 | |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1789 | CloseHandle(pi.hThread); |
Erik Faye-Lund | 52de4db | 2010-11-04 02:35:13 +0100 | [diff] [blame] | 1790 | |
| 1791 | /* |
| 1792 | * The process ID is the human-readable identifier of the process |
| 1793 | * that we want to present in log and error messages. The handle |
| 1794 | * is not useful for this purpose. But we cannot close it, either, |
| 1795 | * because it is not possible to turn a process ID into a process |
| 1796 | * handle after the process terminated. |
| 1797 | * Keep the handle in a list for waitpid. |
| 1798 | */ |
| 1799 | EnterCriticalSection(&pinfo_cs); |
| 1800 | { |
| 1801 | struct pinfo_t *info = xmalloc(sizeof(struct pinfo_t)); |
| 1802 | info->pid = pi.dwProcessId; |
| 1803 | info->proc = pi.hProcess; |
| 1804 | info->next = pinfo; |
| 1805 | pinfo = info; |
| 1806 | } |
| 1807 | LeaveCriticalSection(&pinfo_cs); |
| 1808 | |
| 1809 | return (pid_t)pi.dwProcessId; |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1810 | } |
| 1811 | |
Karsten Blees | 3e66e47 | 2011-11-25 21:33:17 +0100 | [diff] [blame] | 1812 | static pid_t mingw_spawnv(const char *cmd, const char **argv, int prepend_cmd) |
Johannes Sixt | 75301f9 | 2010-01-15 21:12:18 +0100 | [diff] [blame] | 1813 | { |
Karsten Blees | 77734da | 2014-07-17 17:38:01 +0200 | [diff] [blame] | 1814 | return mingw_spawnve_fd(cmd, argv, NULL, NULL, prepend_cmd, 0, 1, 2); |
Johannes Sixt | 75301f9 | 2010-01-15 21:12:18 +0100 | [diff] [blame] | 1815 | } |
| 1816 | |
Karsten Blees | 77734da | 2014-07-17 17:38:01 +0200 | [diff] [blame] | 1817 | pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **deltaenv, |
Johannes Sixt | f9a2743 | 2010-04-11 22:40:12 +0200 | [diff] [blame] | 1818 | const char *dir, |
Johannes Sixt | 75301f9 | 2010-01-15 21:12:18 +0100 | [diff] [blame] | 1819 | int fhin, int fhout, int fherr) |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1820 | { |
| 1821 | pid_t pid; |
René Scharfe | e0ca1ca | 2017-05-20 21:35:37 +0200 | [diff] [blame] | 1822 | char *prog = path_lookup(cmd, 0); |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1823 | |
| 1824 | if (!prog) { |
| 1825 | errno = ENOENT; |
| 1826 | pid = -1; |
| 1827 | } |
| 1828 | else { |
| 1829 | const char *interpr = parse_interpreter(prog); |
| 1830 | |
| 1831 | if (interpr) { |
| 1832 | const char *argv0 = argv[0]; |
René Scharfe | e0ca1ca | 2017-05-20 21:35:37 +0200 | [diff] [blame] | 1833 | char *iprog = path_lookup(interpr, 1); |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1834 | argv[0] = prog; |
| 1835 | if (!iprog) { |
| 1836 | errno = ENOENT; |
| 1837 | pid = -1; |
| 1838 | } |
| 1839 | else { |
Karsten Blees | 77734da | 2014-07-17 17:38:01 +0200 | [diff] [blame] | 1840 | pid = mingw_spawnve_fd(iprog, argv, deltaenv, dir, 1, |
Johannes Sixt | 75301f9 | 2010-01-15 21:12:18 +0100 | [diff] [blame] | 1841 | fhin, fhout, fherr); |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1842 | free(iprog); |
| 1843 | } |
| 1844 | argv[0] = argv0; |
| 1845 | } |
| 1846 | else |
Karsten Blees | 77734da | 2014-07-17 17:38:01 +0200 | [diff] [blame] | 1847 | pid = mingw_spawnve_fd(prog, argv, deltaenv, dir, 0, |
Johannes Sixt | 75301f9 | 2010-01-15 21:12:18 +0100 | [diff] [blame] | 1848 | fhin, fhout, fherr); |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1849 | free(prog); |
| 1850 | } |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1851 | return pid; |
| 1852 | } |
| 1853 | |
Karsten Blees | 3e66e47 | 2011-11-25 21:33:17 +0100 | [diff] [blame] | 1854 | static int try_shell_exec(const char *cmd, char *const *argv) |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1855 | { |
| 1856 | const char *interpr = parse_interpreter(cmd); |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1857 | char *prog; |
| 1858 | int pid = 0; |
| 1859 | |
| 1860 | if (!interpr) |
| 1861 | return 0; |
René Scharfe | e0ca1ca | 2017-05-20 21:35:37 +0200 | [diff] [blame] | 1862 | prog = path_lookup(interpr, 1); |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1863 | if (prog) { |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 1864 | int exec_id; |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1865 | int argc = 0; |
Jeff Hostetler | 12fb9bd | 2019-06-19 14:06:02 -0700 | [diff] [blame] | 1866 | char **argv2; |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1867 | while (argv[argc]) argc++; |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 1868 | ALLOC_ARRAY(argv2, argc + 1); |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 1869 | argv2[0] = (char *)cmd; /* full path to the script file */ |
René Scharfe | 51bd6be | 2019-11-12 22:41:14 +0100 | [diff] [blame] | 1870 | COPY_ARRAY(&argv2[1], &argv[1], argc); |
René Scharfe | 09884f3 | 2023-01-08 11:10:59 +0100 | [diff] [blame] | 1871 | exec_id = trace2_exec(prog, (const char **)argv2); |
| 1872 | pid = mingw_spawnv(prog, (const char **)argv2, 1); |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1873 | if (pid >= 0) { |
| 1874 | int status; |
| 1875 | if (waitpid(pid, &status, 0) < 0) |
| 1876 | status = 255; |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 1877 | trace2_exec_result(exec_id, status); |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1878 | exit(status); |
| 1879 | } |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 1880 | trace2_exec_result(exec_id, -1); |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1881 | pid = 1; /* indicate that we tried but failed */ |
| 1882 | free(prog); |
| 1883 | free(argv2); |
| 1884 | } |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1885 | return pid; |
| 1886 | } |
| 1887 | |
Karsten Blees | 3e66e47 | 2011-11-25 21:33:17 +0100 | [diff] [blame] | 1888 | int mingw_execv(const char *cmd, char *const *argv) |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1889 | { |
| 1890 | /* check if git_command is a shell script */ |
Karsten Blees | 3e66e47 | 2011-11-25 21:33:17 +0100 | [diff] [blame] | 1891 | if (!try_shell_exec(cmd, argv)) { |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1892 | int pid, status; |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 1893 | int exec_id; |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1894 | |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 1895 | exec_id = trace2_exec(cmd, (const char **)argv); |
Karsten Blees | 3e66e47 | 2011-11-25 21:33:17 +0100 | [diff] [blame] | 1896 | pid = mingw_spawnv(cmd, (const char **)argv, 0); |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 1897 | if (pid < 0) { |
| 1898 | trace2_exec_result(exec_id, -1); |
Johannes Schindelin | 570f1e6 | 2012-05-28 21:21:39 -0500 | [diff] [blame] | 1899 | return -1; |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 1900 | } |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1901 | if (waitpid(pid, &status, 0) < 0) |
| 1902 | status = 255; |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 1903 | trace2_exec_result(exec_id, status); |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1904 | exit(status); |
| 1905 | } |
Johannes Schindelin | 570f1e6 | 2012-05-28 21:21:39 -0500 | [diff] [blame] | 1906 | return -1; |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1907 | } |
| 1908 | |
Ramsay Jones | 1696d72 | 2012-04-05 18:48:46 +0100 | [diff] [blame] | 1909 | int mingw_execvp(const char *cmd, char *const *argv) |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1910 | { |
René Scharfe | e0ca1ca | 2017-05-20 21:35:37 +0200 | [diff] [blame] | 1911 | char *prog = path_lookup(cmd, 0); |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1912 | |
| 1913 | if (prog) { |
Karsten Blees | 3e66e47 | 2011-11-25 21:33:17 +0100 | [diff] [blame] | 1914 | mingw_execv(prog, argv); |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1915 | free(prog); |
| 1916 | } else |
| 1917 | errno = ENOENT; |
| 1918 | |
Ramsay Jones | 1696d72 | 2012-04-05 18:48:46 +0100 | [diff] [blame] | 1919 | return -1; |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 1920 | } |
| 1921 | |
Erik Faye-Lund | 82fc07b | 2010-11-04 02:35:15 +0100 | [diff] [blame] | 1922 | int mingw_kill(pid_t pid, int sig) |
| 1923 | { |
| 1924 | if (pid > 0 && sig == SIGTERM) { |
| 1925 | HANDLE h = OpenProcess(PROCESS_TERMINATE, FALSE, pid); |
| 1926 | |
| 1927 | if (TerminateProcess(h, -1)) { |
| 1928 | CloseHandle(h); |
| 1929 | return 0; |
| 1930 | } |
| 1931 | |
| 1932 | errno = err_win_to_posix(GetLastError()); |
| 1933 | CloseHandle(h); |
| 1934 | return -1; |
Nguyễn Thái Ngọc Duy | 64a99eb | 2013-08-08 18:05:38 +0700 | [diff] [blame] | 1935 | } else if (pid > 0 && sig == 0) { |
| 1936 | HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid); |
| 1937 | if (h) { |
| 1938 | CloseHandle(h); |
| 1939 | return 0; |
| 1940 | } |
Erik Faye-Lund | 82fc07b | 2010-11-04 02:35:15 +0100 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | errno = EINVAL; |
| 1944 | return -1; |
| 1945 | } |
| 1946 | |
Karsten Blees | 343ff06 | 2014-07-17 17:38:04 +0200 | [diff] [blame] | 1947 | /* |
Johannes Schindelin | fe21c6b | 2018-10-30 02:22:30 -0700 | [diff] [blame] | 1948 | * UTF-8 versions of getenv(), putenv() and unsetenv(). |
| 1949 | * Internally, they use the CRT's stock UNICODE routines |
| 1950 | * to avoid data loss. |
Karsten Blees | 343ff06 | 2014-07-17 17:38:04 +0200 | [diff] [blame] | 1951 | */ |
Johannes Sixt | 06bc4b7 | 2011-06-06 09:06:02 +0200 | [diff] [blame] | 1952 | char *mingw_getenv(const char *name) |
| 1953 | { |
Johannes Schindelin | ca1b411 | 2019-02-15 07:17:45 -0800 | [diff] [blame] | 1954 | #define GETENV_MAX_RETAIN 64 |
Johannes Schindelin | fe21c6b | 2018-10-30 02:22:30 -0700 | [diff] [blame] | 1955 | static char *values[GETENV_MAX_RETAIN]; |
| 1956 | static int value_counter; |
| 1957 | int len_key, len_value; |
| 1958 | wchar_t *w_key; |
Karsten Blees | 343ff06 | 2014-07-17 17:38:04 +0200 | [diff] [blame] | 1959 | char *value; |
Johannes Schindelin | fe21c6b | 2018-10-30 02:22:30 -0700 | [diff] [blame] | 1960 | wchar_t w_value[32768]; |
| 1961 | |
| 1962 | if (!name || !*name) |
Karsten Blees | 343ff06 | 2014-07-17 17:38:04 +0200 | [diff] [blame] | 1963 | return NULL; |
Johannes Schindelin | fe21c6b | 2018-10-30 02:22:30 -0700 | [diff] [blame] | 1964 | |
| 1965 | len_key = strlen(name) + 1; |
| 1966 | /* We cannot use xcalloc() here because that uses getenv() itself */ |
| 1967 | w_key = calloc(len_key, sizeof(wchar_t)); |
| 1968 | if (!w_key) |
| 1969 | die("Out of memory, (tried to allocate %u wchar_t's)", len_key); |
| 1970 | xutftowcs(w_key, name, len_key); |
Johannes Schindelin | 61d1d92 | 2019-10-04 08:09:30 -0700 | [diff] [blame] | 1971 | /* GetEnvironmentVariableW() only sets the last error upon failure */ |
| 1972 | SetLastError(ERROR_SUCCESS); |
Johannes Schindelin | fe21c6b | 2018-10-30 02:22:30 -0700 | [diff] [blame] | 1973 | len_value = GetEnvironmentVariableW(w_key, w_value, ARRAY_SIZE(w_value)); |
| 1974 | if (!len_value && GetLastError() == ERROR_ENVVAR_NOT_FOUND) { |
| 1975 | free(w_key); |
| 1976 | return NULL; |
| 1977 | } |
| 1978 | free(w_key); |
| 1979 | |
| 1980 | len_value = len_value * 3 + 1; |
| 1981 | /* We cannot use xcalloc() here because that uses getenv() itself */ |
| 1982 | value = calloc(len_value, sizeof(char)); |
| 1983 | if (!value) |
| 1984 | die("Out of memory, (tried to allocate %u bytes)", len_value); |
| 1985 | xwcstoutf(value, w_value, len_value); |
| 1986 | |
| 1987 | /* |
| 1988 | * We return `value` which is an allocated value and the caller is NOT |
| 1989 | * expecting to have to free it, so we keep a round-robin array, |
| 1990 | * invalidating the buffer after GETENV_MAX_RETAIN getenv() calls. |
| 1991 | */ |
| 1992 | free(values[value_counter]); |
| 1993 | values[value_counter++] = value; |
| 1994 | if (value_counter >= ARRAY_SIZE(values)) |
| 1995 | value_counter = 0; |
| 1996 | |
| 1997 | return value; |
Karsten Blees | 343ff06 | 2014-07-17 17:38:04 +0200 | [diff] [blame] | 1998 | } |
| 1999 | |
Karsten Blees | b729f98 | 2014-07-17 17:37:56 +0200 | [diff] [blame] | 2000 | int mingw_putenv(const char *namevalue) |
| 2001 | { |
Johannes Schindelin | fe21c6b | 2018-10-30 02:22:30 -0700 | [diff] [blame] | 2002 | int size; |
| 2003 | wchar_t *wide, *equal; |
| 2004 | BOOL result; |
| 2005 | |
| 2006 | if (!namevalue || !*namevalue) |
| 2007 | return 0; |
| 2008 | |
| 2009 | size = strlen(namevalue) * 2 + 1; |
| 2010 | wide = calloc(size, sizeof(wchar_t)); |
| 2011 | if (!wide) |
| 2012 | die("Out of memory, (tried to allocate %u wchar_t's)", size); |
| 2013 | xutftowcs(wide, namevalue, size); |
| 2014 | equal = wcschr(wide, L'='); |
| 2015 | if (!equal) |
| 2016 | result = SetEnvironmentVariableW(wide, NULL); |
| 2017 | else { |
| 2018 | *equal = L'\0'; |
| 2019 | result = SetEnvironmentVariableW(wide, equal + 1); |
| 2020 | } |
| 2021 | free(wide); |
| 2022 | |
| 2023 | if (!result) |
| 2024 | errno = err_win_to_posix(GetLastError()); |
| 2025 | |
| 2026 | return result ? 0 : -1; |
Johannes Sixt | 06bc4b7 | 2011-06-06 09:06:02 +0200 | [diff] [blame] | 2027 | } |
| 2028 | |
Martin Storsjö | b7cc9f8 | 2009-11-24 00:55:12 +0200 | [diff] [blame] | 2029 | static void ensure_socket_initialization(void) |
Johannes Sixt | 746fb85 | 2007-12-26 13:51:18 +0100 | [diff] [blame] | 2030 | { |
| 2031 | WSADATA wsa; |
Martin Storsjö | b7cc9f8 | 2009-11-24 00:55:12 +0200 | [diff] [blame] | 2032 | static int initialized = 0; |
| 2033 | |
| 2034 | if (initialized) |
| 2035 | return; |
Johannes Sixt | 746fb85 | 2007-12-26 13:51:18 +0100 | [diff] [blame] | 2036 | |
| 2037 | if (WSAStartup(MAKEWORD(2,2), &wsa)) |
| 2038 | die("unable to initialize winsock subsystem, error %d", |
| 2039 | WSAGetLastError()); |
Martin Storsjö | fe3b2b7 | 2009-11-24 00:55:50 +0200 | [diff] [blame] | 2040 | |
Tanushree Tumane | b9f0193 | 2019-02-27 00:43:13 -0800 | [diff] [blame] | 2041 | atexit((void(*)(void)) WSACleanup); |
Martin Storsjö | b7cc9f8 | 2009-11-24 00:55:12 +0200 | [diff] [blame] | 2042 | initialized = 1; |
| 2043 | } |
| 2044 | |
Pat Thoyts | 13d24b0 | 2011-10-15 15:05:20 +0100 | [diff] [blame] | 2045 | #undef gethostname |
| 2046 | int mingw_gethostname(char *name, int namelen) |
| 2047 | { |
| 2048 | ensure_socket_initialization(); |
| 2049 | return gethostname(name, namelen); |
| 2050 | } |
| 2051 | |
Martin Storsjö | b7cc9f8 | 2009-11-24 00:55:12 +0200 | [diff] [blame] | 2052 | #undef gethostbyname |
| 2053 | struct hostent *mingw_gethostbyname(const char *host) |
| 2054 | { |
| 2055 | ensure_socket_initialization(); |
Johannes Sixt | 746fb85 | 2007-12-26 13:51:18 +0100 | [diff] [blame] | 2056 | return gethostbyname(host); |
| 2057 | } |
| 2058 | |
Tanushree Tumane | b9f0193 | 2019-02-27 00:43:13 -0800 | [diff] [blame] | 2059 | #undef getaddrinfo |
Martin Storsjö | fe3b2b7 | 2009-11-24 00:55:50 +0200 | [diff] [blame] | 2060 | int mingw_getaddrinfo(const char *node, const char *service, |
| 2061 | const struct addrinfo *hints, struct addrinfo **res) |
| 2062 | { |
| 2063 | ensure_socket_initialization(); |
Tanushree Tumane | b9f0193 | 2019-02-27 00:43:13 -0800 | [diff] [blame] | 2064 | return getaddrinfo(node, service, hints, res); |
Martin Storsjö | fe3b2b7 | 2009-11-24 00:55:50 +0200 | [diff] [blame] | 2065 | } |
| 2066 | |
Johannes Sixt | 746fb85 | 2007-12-26 13:51:18 +0100 | [diff] [blame] | 2067 | int mingw_socket(int domain, int type, int protocol) |
| 2068 | { |
| 2069 | int sockfd; |
Mike Pape | 772991a | 2010-11-04 02:35:09 +0100 | [diff] [blame] | 2070 | SOCKET s; |
| 2071 | |
| 2072 | ensure_socket_initialization(); |
| 2073 | s = WSASocket(domain, type, protocol, NULL, 0, 0); |
Johannes Sixt | 746fb85 | 2007-12-26 13:51:18 +0100 | [diff] [blame] | 2074 | if (s == INVALID_SOCKET) { |
| 2075 | /* |
| 2076 | * WSAGetLastError() values are regular BSD error codes |
| 2077 | * biased by WSABASEERR. |
| 2078 | * However, strerror() does not know about networking |
| 2079 | * specific errors, which are values beginning at 38 or so. |
| 2080 | * Therefore, we choose to leave the biased error code |
| 2081 | * in errno so that _if_ someone looks up the code somewhere, |
| 2082 | * then it is at least the number that are usually listed. |
| 2083 | */ |
| 2084 | errno = WSAGetLastError(); |
| 2085 | return -1; |
| 2086 | } |
| 2087 | /* convert into a file descriptor */ |
| 2088 | if ((sockfd = _open_osfhandle(s, O_RDWR|O_BINARY)) < 0) { |
| 2089 | closesocket(s); |
| 2090 | return error("unable to make a socket file descriptor: %s", |
| 2091 | strerror(errno)); |
| 2092 | } |
| 2093 | return sockfd; |
| 2094 | } |
| 2095 | |
| 2096 | #undef connect |
| 2097 | int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz) |
| 2098 | { |
| 2099 | SOCKET s = (SOCKET)_get_osfhandle(sockfd); |
| 2100 | return connect(s, sa, sz); |
| 2101 | } |
| 2102 | |
Mike Pape | 772991a | 2010-11-04 02:35:09 +0100 | [diff] [blame] | 2103 | #undef bind |
| 2104 | int mingw_bind(int sockfd, struct sockaddr *sa, size_t sz) |
| 2105 | { |
| 2106 | SOCKET s = (SOCKET)_get_osfhandle(sockfd); |
| 2107 | return bind(s, sa, sz); |
| 2108 | } |
| 2109 | |
| 2110 | #undef setsockopt |
| 2111 | int mingw_setsockopt(int sockfd, int lvl, int optname, void *optval, int optlen) |
| 2112 | { |
| 2113 | SOCKET s = (SOCKET)_get_osfhandle(sockfd); |
| 2114 | return setsockopt(s, lvl, optname, (const char*)optval, optlen); |
| 2115 | } |
| 2116 | |
Johannes Sixt | a794179 | 2011-05-18 22:24:37 +0200 | [diff] [blame] | 2117 | #undef shutdown |
| 2118 | int mingw_shutdown(int sockfd, int how) |
| 2119 | { |
| 2120 | SOCKET s = (SOCKET)_get_osfhandle(sockfd); |
| 2121 | return shutdown(s, how); |
| 2122 | } |
| 2123 | |
Mike Pape | 772991a | 2010-11-04 02:35:09 +0100 | [diff] [blame] | 2124 | #undef listen |
| 2125 | int mingw_listen(int sockfd, int backlog) |
| 2126 | { |
| 2127 | SOCKET s = (SOCKET)_get_osfhandle(sockfd); |
| 2128 | return listen(s, backlog); |
| 2129 | } |
| 2130 | |
| 2131 | #undef accept |
| 2132 | int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz) |
| 2133 | { |
| 2134 | int sockfd2; |
| 2135 | |
| 2136 | SOCKET s1 = (SOCKET)_get_osfhandle(sockfd1); |
| 2137 | SOCKET s2 = accept(s1, sa, sz); |
| 2138 | |
| 2139 | /* convert into a file descriptor */ |
| 2140 | if ((sockfd2 = _open_osfhandle(s2, O_RDWR|O_BINARY)) < 0) { |
| 2141 | int err = errno; |
| 2142 | closesocket(s2); |
| 2143 | return error("unable to make a socket file descriptor: %s", |
| 2144 | strerror(err)); |
| 2145 | } |
| 2146 | return sockfd2; |
| 2147 | } |
| 2148 | |
Johannes Sixt | ea9e98c | 2007-12-07 22:19:40 +0100 | [diff] [blame] | 2149 | #undef rename |
| 2150 | int mingw_rename(const char *pold, const char *pnew) |
| 2151 | { |
Johannes Sixt | 6ac6f87 | 2009-04-03 08:49:59 +0200 | [diff] [blame] | 2152 | DWORD attrs, gle; |
| 2153 | int tries = 0; |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 2154 | wchar_t wpold[MAX_PATH], wpnew[MAX_PATH]; |
| 2155 | if (xutftowcs_path(wpold, pold) < 0 || xutftowcs_path(wpnew, pnew) < 0) |
| 2156 | return -1; |
Johannes Sixt | 632f701 | 2008-11-19 17:25:27 +0100 | [diff] [blame] | 2157 | |
Johannes Sixt | ea9e98c | 2007-12-07 22:19:40 +0100 | [diff] [blame] | 2158 | /* |
| 2159 | * Try native rename() first to get errno right. |
| 2160 | * It is based on MoveFile(), which cannot overwrite existing files. |
| 2161 | */ |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 2162 | if (!_wrename(wpold, wpnew)) |
Johannes Sixt | ea9e98c | 2007-12-07 22:19:40 +0100 | [diff] [blame] | 2163 | return 0; |
| 2164 | if (errno != EEXIST) |
| 2165 | return -1; |
Johannes Sixt | 6ac6f87 | 2009-04-03 08:49:59 +0200 | [diff] [blame] | 2166 | repeat: |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 2167 | if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING)) |
Johannes Sixt | ea9e98c | 2007-12-07 22:19:40 +0100 | [diff] [blame] | 2168 | return 0; |
| 2169 | /* TODO: translate more errors */ |
Johannes Sixt | 6ac6f87 | 2009-04-03 08:49:59 +0200 | [diff] [blame] | 2170 | gle = GetLastError(); |
| 2171 | if (gle == ERROR_ACCESS_DENIED && |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 2172 | (attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) { |
Johannes Sixt | 632f701 | 2008-11-19 17:25:27 +0100 | [diff] [blame] | 2173 | if (attrs & FILE_ATTRIBUTE_DIRECTORY) { |
마누엘 | 4426fb5 | 2016-01-26 15:34:47 +0100 | [diff] [blame] | 2174 | DWORD attrsold = GetFileAttributesW(wpold); |
| 2175 | if (attrsold == INVALID_FILE_ATTRIBUTES || |
| 2176 | !(attrsold & FILE_ATTRIBUTE_DIRECTORY)) |
| 2177 | errno = EISDIR; |
| 2178 | else if (!_wrmdir(wpnew)) |
| 2179 | goto repeat; |
Johannes Sixt | ea9e98c | 2007-12-07 22:19:40 +0100 | [diff] [blame] | 2180 | return -1; |
| 2181 | } |
Johannes Sixt | 632f701 | 2008-11-19 17:25:27 +0100 | [diff] [blame] | 2182 | if ((attrs & FILE_ATTRIBUTE_READONLY) && |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 2183 | SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) { |
| 2184 | if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING)) |
Johannes Sixt | 632f701 | 2008-11-19 17:25:27 +0100 | [diff] [blame] | 2185 | return 0; |
Johannes Sixt | 6ac6f87 | 2009-04-03 08:49:59 +0200 | [diff] [blame] | 2186 | gle = GetLastError(); |
Johannes Sixt | 632f701 | 2008-11-19 17:25:27 +0100 | [diff] [blame] | 2187 | /* revert file attributes on failure */ |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 2188 | SetFileAttributesW(wpnew, attrs); |
Johannes Sixt | 632f701 | 2008-11-19 17:25:27 +0100 | [diff] [blame] | 2189 | } |
Johannes Sixt | ea9e98c | 2007-12-07 22:19:40 +0100 | [diff] [blame] | 2190 | } |
Johannes Sixt | 6ac6f87 | 2009-04-03 08:49:59 +0200 | [diff] [blame] | 2191 | if (tries < ARRAY_SIZE(delay) && gle == ERROR_ACCESS_DENIED) { |
| 2192 | /* |
| 2193 | * We assume that some other process had the source or |
| 2194 | * destination file open at the wrong moment and retry. |
| 2195 | * In order to give the other process a higher chance to |
| 2196 | * complete its operation, we give up our time slice now. |
| 2197 | * If we have to retry again, we do sleep a bit. |
| 2198 | */ |
| 2199 | Sleep(delay[tries]); |
| 2200 | tries++; |
| 2201 | goto repeat; |
| 2202 | } |
Heiko Voigt | c9b7840 | 2011-02-07 21:51:21 +0100 | [diff] [blame] | 2203 | if (gle == ERROR_ACCESS_DENIED && |
| 2204 | ask_yes_no_if_possible("Rename from '%s' to '%s' failed. " |
| 2205 | "Should I try again?", pold, pnew)) |
| 2206 | goto repeat; |
| 2207 | |
Johannes Sixt | ea9e98c | 2007-12-07 22:19:40 +0100 | [diff] [blame] | 2208 | errno = EACCES; |
| 2209 | return -1; |
| 2210 | } |
| 2211 | |
Ramsay Jones | b1b9520 | 2009-11-07 20:10:31 +0000 | [diff] [blame] | 2212 | /* |
| 2213 | * Note that this doesn't return the actual pagesize, but |
| 2214 | * the allocation granularity. If future Windows specific git code |
| 2215 | * needs the real getpagesize function, we need to find another solution. |
| 2216 | */ |
| 2217 | int mingw_getpagesize(void) |
| 2218 | { |
| 2219 | SYSTEM_INFO si; |
| 2220 | GetSystemInfo(&si); |
| 2221 | return si.dwAllocationGranularity; |
| 2222 | } |
| 2223 | |
Johannes Schindelin | 564be79 | 2018-10-15 02:47:06 -0700 | [diff] [blame] | 2224 | /* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */ |
| 2225 | enum EXTENDED_NAME_FORMAT { |
| 2226 | NameDisplay = 3, |
| 2227 | NameUserPrincipal = 8 |
| 2228 | }; |
| 2229 | |
| 2230 | static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type) |
| 2231 | { |
Matthias Aßhauer | 4a9b204 | 2022-01-08 16:02:30 +0000 | [diff] [blame] | 2232 | DECLARE_PROC_ADDR(secur32.dll, BOOL, SEC_ENTRY, GetUserNameExW, |
Johannes Schindelin | 564be79 | 2018-10-15 02:47:06 -0700 | [diff] [blame] | 2233 | enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG); |
| 2234 | static wchar_t wbuffer[1024]; |
| 2235 | DWORD len; |
| 2236 | |
| 2237 | if (!INIT_PROC_ADDR(GetUserNameExW)) |
| 2238 | return NULL; |
| 2239 | |
| 2240 | len = ARRAY_SIZE(wbuffer); |
| 2241 | if (GetUserNameExW(type, wbuffer, &len)) { |
| 2242 | char *converted = xmalloc((len *= 3)); |
| 2243 | if (xwcstoutf(converted, wbuffer, len) >= 0) |
| 2244 | return converted; |
| 2245 | free(converted); |
| 2246 | } |
| 2247 | |
| 2248 | return NULL; |
| 2249 | } |
| 2250 | |
Johannes Schindelin | 501afcb | 2018-10-15 02:47:08 -0700 | [diff] [blame] | 2251 | char *mingw_query_user_email(void) |
| 2252 | { |
| 2253 | return get_extended_user_info(NameUserPrincipal); |
| 2254 | } |
| 2255 | |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 2256 | struct passwd *getpwuid(int uid) |
| 2257 | { |
Johannes Schindelin | 55b6513 | 2018-10-15 02:47:05 -0700 | [diff] [blame] | 2258 | static unsigned initialized; |
Johannes Sixt | f7597ac | 2007-12-01 22:09:17 +0100 | [diff] [blame] | 2259 | static char user_name[100]; |
Johannes Schindelin | 55b6513 | 2018-10-15 02:47:05 -0700 | [diff] [blame] | 2260 | static struct passwd *p; |
Johannes Schindelin | 39a98e9 | 2019-06-27 02:37:18 -0700 | [diff] [blame] | 2261 | wchar_t buf[100]; |
Johannes Schindelin | 55b6513 | 2018-10-15 02:47:05 -0700 | [diff] [blame] | 2262 | DWORD len; |
Johannes Sixt | f7597ac | 2007-12-01 22:09:17 +0100 | [diff] [blame] | 2263 | |
Johannes Schindelin | 55b6513 | 2018-10-15 02:47:05 -0700 | [diff] [blame] | 2264 | if (initialized) |
| 2265 | return p; |
| 2266 | |
Johannes Schindelin | 697bdd2 | 2019-07-04 15:36:57 -0700 | [diff] [blame] | 2267 | len = ARRAY_SIZE(buf); |
Johannes Schindelin | 39a98e9 | 2019-06-27 02:37:18 -0700 | [diff] [blame] | 2268 | if (!GetUserNameW(buf, &len)) { |
| 2269 | initialized = 1; |
| 2270 | return NULL; |
| 2271 | } |
| 2272 | |
| 2273 | if (xwcstoutf(user_name, buf, sizeof(user_name)) < 0) { |
Johannes Schindelin | 55b6513 | 2018-10-15 02:47:05 -0700 | [diff] [blame] | 2274 | initialized = 1; |
Johannes Sixt | f7597ac | 2007-12-01 22:09:17 +0100 | [diff] [blame] | 2275 | return NULL; |
Johannes Schindelin | 55b6513 | 2018-10-15 02:47:05 -0700 | [diff] [blame] | 2276 | } |
| 2277 | |
| 2278 | p = xmalloc(sizeof(*p)); |
| 2279 | p->pw_name = user_name; |
Johannes Schindelin | 564be79 | 2018-10-15 02:47:06 -0700 | [diff] [blame] | 2280 | p->pw_gecos = get_extended_user_info(NameDisplay); |
| 2281 | if (!p->pw_gecos) |
| 2282 | p->pw_gecos = "unknown"; |
Johannes Schindelin | 55b6513 | 2018-10-15 02:47:05 -0700 | [diff] [blame] | 2283 | p->pw_dir = NULL; |
| 2284 | |
| 2285 | initialized = 1; |
| 2286 | return p; |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 2287 | } |
| 2288 | |
Johannes Sixt | 6072fc3 | 2007-11-13 10:14:45 +0100 | [diff] [blame] | 2289 | static HANDLE timer_event; |
| 2290 | static HANDLE timer_thread; |
| 2291 | static int timer_interval; |
| 2292 | static int one_shot; |
Erik Faye-Lund | 176478a | 2012-12-04 09:10:38 +0100 | [diff] [blame] | 2293 | static sig_handler_t timer_fn = SIG_DFL, sigint_fn = SIG_DFL; |
Johannes Sixt | 6072fc3 | 2007-11-13 10:14:45 +0100 | [diff] [blame] | 2294 | |
| 2295 | /* The timer works like this: |
| 2296 | * The thread, ticktack(), is a trivial routine that most of the time |
| 2297 | * only waits to receive the signal to terminate. The main thread tells |
| 2298 | * the thread to terminate by setting the timer_event to the signalled |
| 2299 | * state. |
| 2300 | * But ticktack() interrupts the wait state after the timer's interval |
| 2301 | * length to call the signal handler. |
| 2302 | */ |
| 2303 | |
Frank Li | d7fa500 | 2009-09-16 10:20:21 +0200 | [diff] [blame] | 2304 | static unsigned __stdcall ticktack(void *dummy) |
Johannes Sixt | 6072fc3 | 2007-11-13 10:14:45 +0100 | [diff] [blame] | 2305 | { |
| 2306 | while (WaitForSingleObject(timer_event, timer_interval) == WAIT_TIMEOUT) { |
Erik Faye-Lund | 176478a | 2012-12-04 09:10:38 +0100 | [diff] [blame] | 2307 | mingw_raise(SIGALRM); |
Johannes Sixt | 6072fc3 | 2007-11-13 10:14:45 +0100 | [diff] [blame] | 2308 | if (one_shot) |
| 2309 | break; |
| 2310 | } |
| 2311 | return 0; |
| 2312 | } |
| 2313 | |
| 2314 | static int start_timer_thread(void) |
| 2315 | { |
| 2316 | timer_event = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 2317 | if (timer_event) { |
| 2318 | timer_thread = (HANDLE) _beginthreadex(NULL, 0, ticktack, NULL, 0, NULL); |
| 2319 | if (!timer_thread ) |
| 2320 | return errno = ENOMEM, |
| 2321 | error("cannot start timer thread"); |
| 2322 | } else |
| 2323 | return errno = ENOMEM, |
| 2324 | error("cannot allocate resources for timer"); |
| 2325 | return 0; |
| 2326 | } |
| 2327 | |
| 2328 | static void stop_timer_thread(void) |
| 2329 | { |
| 2330 | if (timer_event) |
| 2331 | SetEvent(timer_event); /* tell thread to terminate */ |
| 2332 | if (timer_thread) { |
Johannes Schindelin | 72d63b2 | 2019-01-29 06:19:31 -0800 | [diff] [blame] | 2333 | int rc = WaitForSingleObject(timer_thread, 10000); |
Johannes Sixt | 6072fc3 | 2007-11-13 10:14:45 +0100 | [diff] [blame] | 2334 | if (rc == WAIT_TIMEOUT) |
| 2335 | error("timer thread did not terminate timely"); |
| 2336 | else if (rc != WAIT_OBJECT_0) |
| 2337 | error("waiting for timer thread failed: %lu", |
| 2338 | GetLastError()); |
| 2339 | CloseHandle(timer_thread); |
| 2340 | } |
| 2341 | if (timer_event) |
| 2342 | CloseHandle(timer_event); |
| 2343 | timer_event = NULL; |
| 2344 | timer_thread = NULL; |
| 2345 | } |
| 2346 | |
| 2347 | static inline int is_timeval_eq(const struct timeval *i1, const struct timeval *i2) |
| 2348 | { |
| 2349 | return i1->tv_sec == i2->tv_sec && i1->tv_usec == i2->tv_usec; |
| 2350 | } |
| 2351 | |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 2352 | int setitimer(int type, struct itimerval *in, struct itimerval *out) |
| 2353 | { |
Johannes Sixt | 6072fc3 | 2007-11-13 10:14:45 +0100 | [diff] [blame] | 2354 | static const struct timeval zero; |
| 2355 | static int atexit_done; |
| 2356 | |
Junio C Hamano | afe8a90 | 2022-05-02 09:50:37 -0700 | [diff] [blame] | 2357 | if (out) |
Johannes Sixt | 6072fc3 | 2007-11-13 10:14:45 +0100 | [diff] [blame] | 2358 | return errno = EINVAL, |
| 2359 | error("setitimer param 3 != NULL not implemented"); |
| 2360 | if (!is_timeval_eq(&in->it_interval, &zero) && |
| 2361 | !is_timeval_eq(&in->it_interval, &in->it_value)) |
| 2362 | return errno = EINVAL, |
| 2363 | error("setitimer: it_interval must be zero or eq it_value"); |
| 2364 | |
| 2365 | if (timer_thread) |
| 2366 | stop_timer_thread(); |
| 2367 | |
| 2368 | if (is_timeval_eq(&in->it_value, &zero) && |
| 2369 | is_timeval_eq(&in->it_interval, &zero)) |
| 2370 | return 0; |
| 2371 | |
| 2372 | timer_interval = in->it_value.tv_sec * 1000 + in->it_value.tv_usec / 1000; |
| 2373 | one_shot = is_timeval_eq(&in->it_interval, &zero); |
| 2374 | if (!atexit_done) { |
| 2375 | atexit(stop_timer_thread); |
| 2376 | atexit_done = 1; |
| 2377 | } |
| 2378 | return start_timer_thread(); |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 2379 | } |
| 2380 | |
| 2381 | int sigaction(int sig, struct sigaction *in, struct sigaction *out) |
| 2382 | { |
Johannes Sixt | 6072fc3 | 2007-11-13 10:14:45 +0100 | [diff] [blame] | 2383 | if (sig != SIGALRM) |
| 2384 | return errno = EINVAL, |
| 2385 | error("sigaction only implemented for SIGALRM"); |
Junio C Hamano | afe8a90 | 2022-05-02 09:50:37 -0700 | [diff] [blame] | 2386 | if (out) |
Johannes Sixt | 6072fc3 | 2007-11-13 10:14:45 +0100 | [diff] [blame] | 2387 | return errno = EINVAL, |
| 2388 | error("sigaction: param 3 != NULL not implemented"); |
| 2389 | |
| 2390 | timer_fn = in->sa_handler; |
| 2391 | return 0; |
| 2392 | } |
| 2393 | |
| 2394 | #undef signal |
| 2395 | sig_handler_t mingw_signal(int sig, sig_handler_t handler) |
| 2396 | { |
Erik Faye-Lund | a454065 | 2013-06-10 07:48:17 +0200 | [diff] [blame] | 2397 | sig_handler_t old; |
Erik Faye-Lund | 176478a | 2012-12-04 09:10:38 +0100 | [diff] [blame] | 2398 | |
| 2399 | switch (sig) { |
| 2400 | case SIGALRM: |
Erik Faye-Lund | a454065 | 2013-06-10 07:48:17 +0200 | [diff] [blame] | 2401 | old = timer_fn; |
Erik Faye-Lund | 176478a | 2012-12-04 09:10:38 +0100 | [diff] [blame] | 2402 | timer_fn = handler; |
| 2403 | break; |
| 2404 | |
| 2405 | case SIGINT: |
Erik Faye-Lund | a454065 | 2013-06-10 07:48:17 +0200 | [diff] [blame] | 2406 | old = sigint_fn; |
Erik Faye-Lund | 176478a | 2012-12-04 09:10:38 +0100 | [diff] [blame] | 2407 | sigint_fn = handler; |
| 2408 | break; |
| 2409 | |
| 2410 | default: |
Johannes Sixt | 6072fc3 | 2007-11-13 10:14:45 +0100 | [diff] [blame] | 2411 | return signal(sig, handler); |
Erik Faye-Lund | 176478a | 2012-12-04 09:10:38 +0100 | [diff] [blame] | 2412 | } |
| 2413 | |
Johannes Sixt | 6072fc3 | 2007-11-13 10:14:45 +0100 | [diff] [blame] | 2414 | return old; |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 2415 | } |
Steffen Prohaska | 4804aab | 2008-07-13 22:31:20 +0200 | [diff] [blame] | 2416 | |
Erik Faye-Lund | 176478a | 2012-12-04 09:10:38 +0100 | [diff] [blame] | 2417 | #undef raise |
| 2418 | int mingw_raise(int sig) |
| 2419 | { |
| 2420 | switch (sig) { |
| 2421 | case SIGALRM: |
| 2422 | if (timer_fn == SIG_DFL) { |
| 2423 | if (isatty(STDERR_FILENO)) |
| 2424 | fputs("Alarm clock\n", stderr); |
| 2425 | exit(128 + SIGALRM); |
| 2426 | } else if (timer_fn != SIG_IGN) |
| 2427 | timer_fn(SIGALRM); |
| 2428 | return 0; |
| 2429 | |
| 2430 | case SIGINT: |
| 2431 | if (sigint_fn == SIG_DFL) |
| 2432 | exit(128 + SIGINT); |
| 2433 | else if (sigint_fn != SIG_IGN) |
| 2434 | sigint_fn(SIGINT); |
| 2435 | return 0; |
| 2436 | |
Jeff Hostetler | 446df60 | 2019-06-25 07:49:41 -0700 | [diff] [blame] | 2437 | #if defined(_MSC_VER) |
| 2438 | case SIGILL: |
| 2439 | case SIGFPE: |
| 2440 | case SIGSEGV: |
| 2441 | case SIGTERM: |
| 2442 | case SIGBREAK: |
| 2443 | case SIGABRT: |
| 2444 | case SIGABRT_COMPAT: |
| 2445 | /* |
| 2446 | * The <signal.h> header in the MS C Runtime defines 8 signals |
| 2447 | * as being supported on the platform. Anything else causes an |
| 2448 | * "Invalid signal or error" (which in DEBUG builds causes the |
| 2449 | * Abort/Retry/Ignore dialog). We by-pass the CRT for things we |
| 2450 | * already know will fail. |
| 2451 | */ |
| 2452 | return raise(sig); |
| 2453 | default: |
| 2454 | errno = EINVAL; |
| 2455 | return -1; |
| 2456 | |
| 2457 | #else |
| 2458 | |
Erik Faye-Lund | 176478a | 2012-12-04 09:10:38 +0100 | [diff] [blame] | 2459 | default: |
| 2460 | return raise(sig); |
Jeff Hostetler | 446df60 | 2019-06-25 07:49:41 -0700 | [diff] [blame] | 2461 | |
| 2462 | #endif |
| 2463 | |
Erik Faye-Lund | 176478a | 2012-12-04 09:10:38 +0100 | [diff] [blame] | 2464 | } |
| 2465 | } |
| 2466 | |
Petr Kodl | 7be401e | 2009-01-24 15:04:39 +0100 | [diff] [blame] | 2467 | int link(const char *oldpath, const char *newpath) |
| 2468 | { |
Karsten Blees | 85faec9 | 2012-03-15 18:21:28 +0100 | [diff] [blame] | 2469 | wchar_t woldpath[MAX_PATH], wnewpath[MAX_PATH]; |
| 2470 | if (xutftowcs_path(woldpath, oldpath) < 0 || |
| 2471 | xutftowcs_path(wnewpath, newpath) < 0) |
| 2472 | return -1; |
| 2473 | |
Johannes Schindelin | 1e1a876 | 2018-11-13 06:37:02 -0800 | [diff] [blame] | 2474 | if (!CreateHardLinkW(wnewpath, woldpath, NULL)) { |
Petr Kodl | 7be401e | 2009-01-24 15:04:39 +0100 | [diff] [blame] | 2475 | errno = err_win_to_posix(GetLastError()); |
| 2476 | return -1; |
| 2477 | } |
| 2478 | return 0; |
| 2479 | } |
Johannes Schindelin | 0dbbbc1 | 2009-05-23 10:04:50 +0200 | [diff] [blame] | 2480 | |
Erik Faye-Lund | 956d86d | 2011-12-08 20:39:57 +0100 | [diff] [blame] | 2481 | pid_t waitpid(pid_t pid, int *status, int options) |
Erik Faye-Lund | 52de4db | 2010-11-04 02:35:13 +0100 | [diff] [blame] | 2482 | { |
| 2483 | HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, |
| 2484 | FALSE, pid); |
| 2485 | if (!h) { |
| 2486 | errno = ECHILD; |
| 2487 | return -1; |
| 2488 | } |
| 2489 | |
Erik Faye-Lund | ef7108c | 2010-11-04 02:35:14 +0100 | [diff] [blame] | 2490 | if (pid > 0 && options & WNOHANG) { |
| 2491 | if (WAIT_OBJECT_0 != WaitForSingleObject(h, 0)) { |
| 2492 | CloseHandle(h); |
| 2493 | return 0; |
| 2494 | } |
| 2495 | options &= ~WNOHANG; |
| 2496 | } |
| 2497 | |
Erik Faye-Lund | 52de4db | 2010-11-04 02:35:13 +0100 | [diff] [blame] | 2498 | if (options == 0) { |
| 2499 | struct pinfo_t **ppinfo; |
| 2500 | if (WaitForSingleObject(h, INFINITE) != WAIT_OBJECT_0) { |
| 2501 | CloseHandle(h); |
| 2502 | return 0; |
| 2503 | } |
| 2504 | |
| 2505 | if (status) |
| 2506 | GetExitCodeProcess(h, (LPDWORD)status); |
| 2507 | |
| 2508 | EnterCriticalSection(&pinfo_cs); |
| 2509 | |
| 2510 | ppinfo = &pinfo; |
| 2511 | while (*ppinfo) { |
| 2512 | struct pinfo_t *info = *ppinfo; |
| 2513 | if (info->pid == pid) { |
| 2514 | CloseHandle(info->proc); |
| 2515 | *ppinfo = info->next; |
| 2516 | free(info); |
| 2517 | break; |
| 2518 | } |
| 2519 | ppinfo = &info->next; |
| 2520 | } |
| 2521 | |
| 2522 | LeaveCriticalSection(&pinfo_cs); |
| 2523 | |
| 2524 | CloseHandle(h); |
| 2525 | return pid; |
| 2526 | } |
| 2527 | CloseHandle(h); |
| 2528 | |
| 2529 | errno = EINVAL; |
| 2530 | return -1; |
| 2531 | } |
Cezary Zawadka | c2369bd | 2010-07-13 16:17:43 +0200 | [diff] [blame] | 2532 | |
Karsten Blees | 1c950a5 | 2011-11-25 21:05:06 +0100 | [diff] [blame] | 2533 | int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen) |
| 2534 | { |
| 2535 | int upos = 0, wpos = 0; |
| 2536 | const unsigned char *utf = (const unsigned char*) utfs; |
| 2537 | if (!utf || !wcs || wcslen < 1) { |
| 2538 | errno = EINVAL; |
| 2539 | return -1; |
| 2540 | } |
| 2541 | /* reserve space for \0 */ |
| 2542 | wcslen--; |
| 2543 | if (utflen < 0) |
| 2544 | utflen = INT_MAX; |
| 2545 | |
| 2546 | while (upos < utflen) { |
| 2547 | int c = utf[upos++] & 0xff; |
| 2548 | if (utflen == INT_MAX && c == 0) |
| 2549 | break; |
| 2550 | |
| 2551 | if (wpos >= wcslen) { |
| 2552 | wcs[wpos] = 0; |
| 2553 | errno = ERANGE; |
| 2554 | return -1; |
| 2555 | } |
| 2556 | |
| 2557 | if (c < 0x80) { |
| 2558 | /* ASCII */ |
| 2559 | wcs[wpos++] = c; |
| 2560 | } else if (c >= 0xc2 && c < 0xe0 && upos < utflen && |
| 2561 | (utf[upos] & 0xc0) == 0x80) { |
| 2562 | /* 2-byte utf-8 */ |
| 2563 | c = ((c & 0x1f) << 6); |
| 2564 | c |= (utf[upos++] & 0x3f); |
| 2565 | wcs[wpos++] = c; |
| 2566 | } else if (c >= 0xe0 && c < 0xf0 && upos + 1 < utflen && |
| 2567 | !(c == 0xe0 && utf[upos] < 0xa0) && /* over-long encoding */ |
| 2568 | (utf[upos] & 0xc0) == 0x80 && |
| 2569 | (utf[upos + 1] & 0xc0) == 0x80) { |
| 2570 | /* 3-byte utf-8 */ |
| 2571 | c = ((c & 0x0f) << 12); |
| 2572 | c |= ((utf[upos++] & 0x3f) << 6); |
| 2573 | c |= (utf[upos++] & 0x3f); |
| 2574 | wcs[wpos++] = c; |
| 2575 | } else if (c >= 0xf0 && c < 0xf5 && upos + 2 < utflen && |
| 2576 | wpos + 1 < wcslen && |
| 2577 | !(c == 0xf0 && utf[upos] < 0x90) && /* over-long encoding */ |
| 2578 | !(c == 0xf4 && utf[upos] >= 0x90) && /* > \u10ffff */ |
| 2579 | (utf[upos] & 0xc0) == 0x80 && |
| 2580 | (utf[upos + 1] & 0xc0) == 0x80 && |
| 2581 | (utf[upos + 2] & 0xc0) == 0x80) { |
| 2582 | /* 4-byte utf-8: convert to \ud8xx \udcxx surrogate pair */ |
| 2583 | c = ((c & 0x07) << 18); |
| 2584 | c |= ((utf[upos++] & 0x3f) << 12); |
| 2585 | c |= ((utf[upos++] & 0x3f) << 6); |
| 2586 | c |= (utf[upos++] & 0x3f); |
| 2587 | c -= 0x10000; |
| 2588 | wcs[wpos++] = 0xd800 | (c >> 10); |
| 2589 | wcs[wpos++] = 0xdc00 | (c & 0x3ff); |
| 2590 | } else if (c >= 0xa0) { |
| 2591 | /* invalid utf-8 byte, printable unicode char: convert 1:1 */ |
| 2592 | wcs[wpos++] = c; |
| 2593 | } else { |
| 2594 | /* invalid utf-8 byte, non-printable unicode: convert to hex */ |
| 2595 | static const char *hex = "0123456789abcdef"; |
| 2596 | wcs[wpos++] = hex[c >> 4]; |
| 2597 | if (wpos < wcslen) |
| 2598 | wcs[wpos++] = hex[c & 0x0f]; |
| 2599 | } |
| 2600 | } |
| 2601 | wcs[wpos] = 0; |
| 2602 | return wpos; |
| 2603 | } |
| 2604 | |
| 2605 | int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen) |
| 2606 | { |
| 2607 | if (!wcs || !utf || utflen < 1) { |
| 2608 | errno = EINVAL; |
| 2609 | return -1; |
| 2610 | } |
| 2611 | utflen = WideCharToMultiByte(CP_UTF8, 0, wcs, -1, utf, utflen, NULL, NULL); |
| 2612 | if (utflen) |
| 2613 | return utflen - 1; |
| 2614 | errno = ERANGE; |
| 2615 | return -1; |
| 2616 | } |
| 2617 | |
Johannes Schindelin | 0767172 | 2016-06-18 14:38:36 +0200 | [diff] [blame] | 2618 | static void setup_windows_environment(void) |
Karsten Blees | 02e6edc | 2016-01-26 15:34:38 +0100 | [diff] [blame] | 2619 | { |
Johannes Schindelin | 1fc7bf7 | 2016-01-26 15:34:43 +0100 | [diff] [blame] | 2620 | char *tmp = getenv("TMPDIR"); |
| 2621 | |
Karsten Blees | 02e6edc | 2016-01-26 15:34:38 +0100 | [diff] [blame] | 2622 | /* on Windows it is TMP and TEMP */ |
Johannes Schindelin | 1fc7bf7 | 2016-01-26 15:34:43 +0100 | [diff] [blame] | 2623 | if (!tmp) { |
| 2624 | if (!(tmp = getenv("TMP"))) |
Karsten Blees | 02e6edc | 2016-01-26 15:34:38 +0100 | [diff] [blame] | 2625 | tmp = getenv("TEMP"); |
Johannes Schindelin | 1fc7bf7 | 2016-01-26 15:34:43 +0100 | [diff] [blame] | 2626 | if (tmp) { |
Karsten Blees | 02e6edc | 2016-01-26 15:34:38 +0100 | [diff] [blame] | 2627 | setenv("TMPDIR", tmp, 1); |
Johannes Schindelin | 1fc7bf7 | 2016-01-26 15:34:43 +0100 | [diff] [blame] | 2628 | tmp = getenv("TMPDIR"); |
| 2629 | } |
| 2630 | } |
| 2631 | |
| 2632 | if (tmp) { |
| 2633 | /* |
| 2634 | * Convert all dir separators to forward slashes, |
| 2635 | * to help shell commands called from the Git |
| 2636 | * executable (by not mistaking the dir separators |
| 2637 | * for escape characters). |
| 2638 | */ |
Johannes Sixt | 8e9b208 | 2016-04-02 21:03:14 +0200 | [diff] [blame] | 2639 | convert_slashes(tmp); |
Karsten Blees | 02e6edc | 2016-01-26 15:34:38 +0100 | [diff] [blame] | 2640 | } |
| 2641 | |
| 2642 | /* simulate TERM to enable auto-color (see color.c) */ |
| 2643 | if (!getenv("TERM")) |
| 2644 | setenv("TERM", "cygwin", 1); |
Karsten Blees | e12a955 | 2019-07-04 02:20:33 -0700 | [diff] [blame] | 2645 | |
| 2646 | /* calculate HOME if not set */ |
| 2647 | if (!getenv("HOME")) { |
| 2648 | /* |
| 2649 | * try $HOMEDRIVE$HOMEPATH - the home share may be a network |
| 2650 | * location, thus also check if the path exists (i.e. is not |
| 2651 | * disconnected) |
| 2652 | */ |
| 2653 | if ((tmp = getenv("HOMEDRIVE"))) { |
| 2654 | struct strbuf buf = STRBUF_INIT; |
| 2655 | strbuf_addstr(&buf, tmp); |
| 2656 | if ((tmp = getenv("HOMEPATH"))) { |
| 2657 | strbuf_addstr(&buf, tmp); |
| 2658 | if (is_directory(buf.buf)) |
| 2659 | setenv("HOME", buf.buf, 1); |
| 2660 | else |
| 2661 | tmp = NULL; /* use $USERPROFILE */ |
| 2662 | } |
| 2663 | strbuf_release(&buf); |
| 2664 | } |
| 2665 | /* use $USERPROFILE if the home share is not available */ |
| 2666 | if (!tmp && (tmp = getenv("USERPROFILE"))) |
| 2667 | setenv("HOME", tmp, 1); |
| 2668 | } |
Karsten Blees | 02e6edc | 2016-01-26 15:34:38 +0100 | [diff] [blame] | 2669 | } |
| 2670 | |
Johannes Schindelin | bdc77d1 | 2022-03-02 11:06:24 +0100 | [diff] [blame] | 2671 | static PSID get_current_user_sid(void) |
| 2672 | { |
| 2673 | HANDLE token; |
| 2674 | DWORD len = 0; |
| 2675 | PSID result = NULL; |
| 2676 | |
| 2677 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) |
| 2678 | return NULL; |
| 2679 | |
| 2680 | if (!GetTokenInformation(token, TokenUser, NULL, 0, &len)) { |
| 2681 | TOKEN_USER *info = xmalloc((size_t)len); |
| 2682 | if (GetTokenInformation(token, TokenUser, info, len, &len)) { |
| 2683 | len = GetLengthSid(info->User.Sid); |
| 2684 | result = xmalloc(len); |
| 2685 | if (!CopySid(len, result, info->User.Sid)) { |
| 2686 | error(_("failed to copy SID (%ld)"), |
| 2687 | GetLastError()); |
| 2688 | FREE_AND_NULL(result); |
| 2689 | } |
| 2690 | } |
| 2691 | FREE_AND_NULL(info); |
| 2692 | } |
| 2693 | CloseHandle(token); |
| 2694 | |
| 2695 | return result; |
| 2696 | } |
| 2697 | |
Sören Krecker | f755e09 | 2024-01-08 18:38:37 +0100 | [diff] [blame] | 2698 | static BOOL user_sid_to_user_name(PSID sid, LPSTR *str) |
| 2699 | { |
| 2700 | SID_NAME_USE pe_use; |
| 2701 | DWORD len_user = 0, len_domain = 0; |
| 2702 | BOOL translate_sid_to_user; |
| 2703 | |
| 2704 | /* |
| 2705 | * returns only FALSE, because the string pointers are NULL |
| 2706 | */ |
| 2707 | LookupAccountSidA(NULL, sid, NULL, &len_user, NULL, &len_domain, |
| 2708 | &pe_use); |
| 2709 | /* |
| 2710 | * Alloc needed space of the strings |
| 2711 | */ |
| 2712 | ALLOC_ARRAY((*str), (size_t)len_domain + (size_t)len_user); |
| 2713 | translate_sid_to_user = LookupAccountSidA(NULL, sid, |
| 2714 | (*str) + len_domain, &len_user, *str, &len_domain, &pe_use); |
| 2715 | if (!translate_sid_to_user) |
| 2716 | FREE_AND_NULL(*str); |
| 2717 | else |
| 2718 | (*str)[len_domain] = '/'; |
| 2719 | return translate_sid_to_user; |
| 2720 | } |
| 2721 | |
Johannes Schindelin | 7c83470 | 2022-08-08 13:27:49 +0000 | [diff] [blame] | 2722 | static int acls_supported(const char *path) |
| 2723 | { |
| 2724 | size_t offset = offset_1st_component(path); |
| 2725 | WCHAR wroot[MAX_PATH]; |
| 2726 | DWORD file_system_flags; |
| 2727 | |
| 2728 | if (offset && |
| 2729 | xutftowcsn(wroot, path, MAX_PATH, offset) > 0 && |
| 2730 | GetVolumeInformationW(wroot, NULL, 0, NULL, NULL, |
| 2731 | &file_system_flags, NULL, 0)) |
| 2732 | return !!(file_system_flags & FILE_PERSISTENT_ACLS); |
| 2733 | |
| 2734 | return 0; |
| 2735 | } |
| 2736 | |
Johannes Schindelin | 17d3883 | 2022-08-08 13:27:47 +0000 | [diff] [blame] | 2737 | int is_path_owned_by_current_sid(const char *path, struct strbuf *report) |
Johannes Schindelin | bdc77d1 | 2022-03-02 11:06:24 +0100 | [diff] [blame] | 2738 | { |
| 2739 | WCHAR wpath[MAX_PATH]; |
| 2740 | PSID sid = NULL; |
| 2741 | PSECURITY_DESCRIPTOR descriptor = NULL; |
| 2742 | DWORD err; |
| 2743 | |
| 2744 | static wchar_t home[MAX_PATH]; |
| 2745 | |
| 2746 | int result = 0; |
| 2747 | |
| 2748 | if (xutftowcs_path(wpath, path) < 0) |
| 2749 | return 0; |
| 2750 | |
| 2751 | /* |
| 2752 | * On Windows, the home directory is owned by the administrator, but for |
| 2753 | * all practical purposes, it belongs to the user. Do pretend that it is |
| 2754 | * owned by the user. |
| 2755 | */ |
| 2756 | if (!*home) { |
| 2757 | DWORD size = ARRAY_SIZE(home); |
| 2758 | DWORD len = GetEnvironmentVariableW(L"HOME", home, size); |
| 2759 | if (!len || len > size) |
| 2760 | wcscpy(home, L"::N/A::"); |
| 2761 | } |
| 2762 | if (!wcsicmp(wpath, home)) |
| 2763 | return 1; |
| 2764 | |
| 2765 | /* Get the owner SID */ |
| 2766 | err = GetNamedSecurityInfoW(wpath, SE_FILE_OBJECT, |
| 2767 | OWNER_SECURITY_INFORMATION | |
| 2768 | DACL_SECURITY_INFORMATION, |
| 2769 | &sid, NULL, NULL, NULL, &descriptor); |
| 2770 | |
| 2771 | if (err != ERROR_SUCCESS) |
| 2772 | error(_("failed to get owner for '%s' (%ld)"), path, err); |
| 2773 | else if (sid && IsValidSid(sid)) { |
| 2774 | /* Now, verify that the SID matches the current user's */ |
| 2775 | static PSID current_user_sid; |
Johannes Schindelin | 3f7207e | 2022-08-08 13:27:50 +0000 | [diff] [blame] | 2776 | BOOL is_member; |
Johannes Schindelin | bdc77d1 | 2022-03-02 11:06:24 +0100 | [diff] [blame] | 2777 | |
| 2778 | if (!current_user_sid) |
| 2779 | current_user_sid = get_current_user_sid(); |
| 2780 | |
| 2781 | if (current_user_sid && |
| 2782 | IsValidSid(current_user_sid) && |
| 2783 | EqualSid(sid, current_user_sid)) |
| 2784 | result = 1; |
Johannes Schindelin | 3f7207e | 2022-08-08 13:27:50 +0000 | [diff] [blame] | 2785 | else if (IsWellKnownSid(sid, WinBuiltinAdministratorsSid) && |
| 2786 | CheckTokenMembership(NULL, sid, &is_member) && |
| 2787 | is_member) |
| 2788 | /* |
| 2789 | * If owned by the Administrators group, and the |
| 2790 | * current user is an administrator, we consider that |
| 2791 | * okay, too. |
| 2792 | */ |
| 2793 | result = 1; |
Johannes Schindelin | 7c83470 | 2022-08-08 13:27:49 +0000 | [diff] [blame] | 2794 | else if (report && |
| 2795 | IsWellKnownSid(sid, WinWorldSid) && |
| 2796 | !acls_supported(path)) { |
| 2797 | /* |
| 2798 | * On FAT32 volumes, ownership is not actually recorded. |
| 2799 | */ |
Daniël Haazen | 4eb1cce | 2022-12-19 02:26:50 +0000 | [diff] [blame] | 2800 | strbuf_addf(report, "'%s' is on a file system that does " |
Johannes Schindelin | 7c83470 | 2022-08-08 13:27:49 +0000 | [diff] [blame] | 2801 | "not record ownership\n", path); |
| 2802 | } else if (report) { |
Sören Krecker | f755e09 | 2024-01-08 18:38:37 +0100 | [diff] [blame] | 2803 | LPSTR str1, str2, str3, str4, to_free1 = NULL, |
| 2804 | to_free3 = NULL, to_local_free2 = NULL, |
| 2805 | to_local_free4 = NULL; |
Johannes Schindelin | e883e04 | 2022-08-08 13:27:48 +0000 | [diff] [blame] | 2806 | |
Sören Krecker | f755e09 | 2024-01-08 18:38:37 +0100 | [diff] [blame] | 2807 | if (user_sid_to_user_name(sid, &str1)) |
Johannes Schindelin | e883e04 | 2022-08-08 13:27:48 +0000 | [diff] [blame] | 2808 | to_free1 = str1; |
| 2809 | else |
| 2810 | str1 = "(inconvertible)"; |
Sören Krecker | f755e09 | 2024-01-08 18:38:37 +0100 | [diff] [blame] | 2811 | if (ConvertSidToStringSidA(sid, &str2)) |
| 2812 | to_local_free2 = str2; |
Johannes Schindelin | e883e04 | 2022-08-08 13:27:48 +0000 | [diff] [blame] | 2813 | else |
| 2814 | str2 = "(inconvertible)"; |
Sören Krecker | f755e09 | 2024-01-08 18:38:37 +0100 | [diff] [blame] | 2815 | |
| 2816 | if (!current_user_sid) { |
| 2817 | str3 = "(none)"; |
| 2818 | str4 = "(none)"; |
| 2819 | } |
| 2820 | else if (!IsValidSid(current_user_sid)) { |
| 2821 | str3 = "(invalid)"; |
| 2822 | str4 = "(invalid)"; |
| 2823 | } else { |
| 2824 | if (user_sid_to_user_name(current_user_sid, |
| 2825 | &str3)) |
| 2826 | to_free3 = str3; |
| 2827 | else |
| 2828 | str3 = "(inconvertible)"; |
| 2829 | if (ConvertSidToStringSidA(current_user_sid, |
| 2830 | &str4)) |
| 2831 | to_local_free4 = str4; |
| 2832 | else |
| 2833 | str4 = "(inconvertible)"; |
| 2834 | } |
Johannes Schindelin | e883e04 | 2022-08-08 13:27:48 +0000 | [diff] [blame] | 2835 | strbuf_addf(report, |
| 2836 | "'%s' is owned by:\n" |
Sören Krecker | f755e09 | 2024-01-08 18:38:37 +0100 | [diff] [blame] | 2837 | "\t%s (%s)\nbut the current user is:\n" |
| 2838 | "\t%s (%s)\n", |
| 2839 | path, str1, str2, str3, str4); |
| 2840 | free(to_free1); |
| 2841 | LocalFree(to_local_free2); |
| 2842 | free(to_free3); |
| 2843 | LocalFree(to_local_free4); |
Johannes Schindelin | e883e04 | 2022-08-08 13:27:48 +0000 | [diff] [blame] | 2844 | } |
Johannes Schindelin | bdc77d1 | 2022-03-02 11:06:24 +0100 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | /* |
| 2848 | * We can release the security descriptor struct only now because `sid` |
| 2849 | * actually points into this struct. |
| 2850 | */ |
| 2851 | if (descriptor) |
| 2852 | LocalFree(descriptor); |
| 2853 | |
| 2854 | return result; |
| 2855 | } |
| 2856 | |
Johannes Schindelin | 4dc42c6 | 2019-12-21 22:05:01 +0000 | [diff] [blame] | 2857 | int is_valid_win32_path(const char *path, int allow_literal_nul) |
Johannes Schindelin | d2c84da | 2019-09-05 13:27:53 +0200 | [diff] [blame] | 2858 | { |
Johannes Schindelin | 4dc42c6 | 2019-12-21 22:05:01 +0000 | [diff] [blame] | 2859 | const char *p = path; |
Johannes Schindelin | d2c84da | 2019-09-05 13:27:53 +0200 | [diff] [blame] | 2860 | int preceding_space_or_period = 0, i = 0, periods = 0; |
| 2861 | |
| 2862 | if (!protect_ntfs) |
| 2863 | return 1; |
| 2864 | |
Johannes Schindelin | 817ddd6 | 2019-09-05 13:44:21 +0200 | [diff] [blame] | 2865 | skip_dos_drive_prefix((char **)&path); |
Johannes Schindelin | 4dc42c6 | 2019-12-21 22:05:01 +0000 | [diff] [blame] | 2866 | goto segment_start; |
Johannes Schindelin | 817ddd6 | 2019-09-05 13:44:21 +0200 | [diff] [blame] | 2867 | |
Johannes Schindelin | d2c84da | 2019-09-05 13:27:53 +0200 | [diff] [blame] | 2868 | for (;;) { |
| 2869 | char c = *(path++); |
| 2870 | switch (c) { |
| 2871 | case '\0': |
| 2872 | case '/': case '\\': |
| 2873 | /* cannot end in ` ` or `.`, except for `.` and `..` */ |
| 2874 | if (preceding_space_or_period && |
| 2875 | (i != periods || periods > 2)) |
| 2876 | return 0; |
| 2877 | if (!c) |
| 2878 | return 1; |
| 2879 | |
| 2880 | i = periods = preceding_space_or_period = 0; |
Johannes Schindelin | 4dc42c6 | 2019-12-21 22:05:01 +0000 | [diff] [blame] | 2881 | |
| 2882 | segment_start: |
| 2883 | switch (*path) { |
| 2884 | case 'a': case 'A': /* AUX */ |
| 2885 | if (((c = path[++i]) != 'u' && c != 'U') || |
| 2886 | ((c = path[++i]) != 'x' && c != 'X')) { |
| 2887 | not_a_reserved_name: |
| 2888 | path += i; |
| 2889 | continue; |
| 2890 | } |
| 2891 | break; |
Johannes Schindelin | b6852e1 | 2020-04-08 18:06:49 +0000 | [diff] [blame] | 2892 | case 'c': case 'C': |
| 2893 | /* COM1 ... COM9, CON, CONIN$, CONOUT$ */ |
Johannes Schindelin | 4dc42c6 | 2019-12-21 22:05:01 +0000 | [diff] [blame] | 2894 | if ((c = path[++i]) != 'o' && c != 'O') |
| 2895 | goto not_a_reserved_name; |
| 2896 | c = path[++i]; |
Johannes Schindelin | b6852e1 | 2020-04-08 18:06:49 +0000 | [diff] [blame] | 2897 | if (c == 'm' || c == 'M') { /* COM1 ... COM9 */ |
| 2898 | c = path[++i]; |
| 2899 | if (c < '1' || c > '9') |
Johannes Schindelin | 4dc42c6 | 2019-12-21 22:05:01 +0000 | [diff] [blame] | 2900 | goto not_a_reserved_name; |
| 2901 | } else if (c == 'n' || c == 'N') { /* CON */ |
| 2902 | c = path[i + 1]; |
| 2903 | if ((c == 'i' || c == 'I') && |
| 2904 | ((c = path[i + 2]) == 'n' || |
| 2905 | c == 'N') && |
| 2906 | path[i + 3] == '$') |
| 2907 | i += 3; /* CONIN$ */ |
| 2908 | else if ((c == 'o' || c == 'O') && |
| 2909 | ((c = path[i + 2]) == 'u' || |
| 2910 | c == 'U') && |
| 2911 | ((c = path[i + 3]) == 't' || |
| 2912 | c == 'T') && |
| 2913 | path[i + 4] == '$') |
| 2914 | i += 4; /* CONOUT$ */ |
| 2915 | } else |
| 2916 | goto not_a_reserved_name; |
| 2917 | break; |
| 2918 | case 'l': case 'L': /* LPT<N> */ |
| 2919 | if (((c = path[++i]) != 'p' && c != 'P') || |
| 2920 | ((c = path[++i]) != 't' && c != 'T') || |
| 2921 | !isdigit(path[++i])) |
| 2922 | goto not_a_reserved_name; |
| 2923 | break; |
| 2924 | case 'n': case 'N': /* NUL */ |
| 2925 | if (((c = path[++i]) != 'u' && c != 'U') || |
| 2926 | ((c = path[++i]) != 'l' && c != 'L') || |
| 2927 | (allow_literal_nul && |
| 2928 | !path[i + 1] && p == path)) |
| 2929 | goto not_a_reserved_name; |
| 2930 | break; |
| 2931 | case 'p': case 'P': /* PRN */ |
| 2932 | if (((c = path[++i]) != 'r' && c != 'R') || |
| 2933 | ((c = path[++i]) != 'n' && c != 'N')) |
| 2934 | goto not_a_reserved_name; |
| 2935 | break; |
| 2936 | default: |
| 2937 | continue; |
| 2938 | } |
| 2939 | |
| 2940 | /* |
| 2941 | * So far, this looks like a reserved name. Let's see |
| 2942 | * whether it actually is one: trailing spaces, a file |
| 2943 | * extension, or an NTFS Alternate Data Stream do not |
| 2944 | * matter, the name is still reserved if any of those |
| 2945 | * follow immediately after the actual name. |
| 2946 | */ |
| 2947 | i++; |
| 2948 | if (path[i] == ' ') { |
| 2949 | preceding_space_or_period = 1; |
| 2950 | while (path[++i] == ' ') |
| 2951 | ; /* skip all spaces */ |
| 2952 | } |
| 2953 | |
| 2954 | c = path[i]; |
Ævar Arnfjörð Bjarmason | 9fd512c | 2022-05-16 20:10:59 +0000 | [diff] [blame] | 2955 | if (c && c != '.' && c != ':' && !is_xplatform_dir_sep(c)) |
Johannes Schindelin | 4dc42c6 | 2019-12-21 22:05:01 +0000 | [diff] [blame] | 2956 | goto not_a_reserved_name; |
| 2957 | |
| 2958 | /* contains reserved name */ |
| 2959 | return 0; |
Johannes Schindelin | d2c84da | 2019-09-05 13:27:53 +0200 | [diff] [blame] | 2960 | case '.': |
| 2961 | periods++; |
| 2962 | /* fallthru */ |
| 2963 | case ' ': |
| 2964 | preceding_space_or_period = 1; |
| 2965 | i++; |
| 2966 | continue; |
Johannes Schindelin | 817ddd6 | 2019-09-05 13:44:21 +0200 | [diff] [blame] | 2967 | case ':': /* DOS drive prefix was already skipped */ |
| 2968 | case '<': case '>': case '"': case '|': case '?': case '*': |
| 2969 | /* illegal character */ |
| 2970 | return 0; |
| 2971 | default: |
| 2972 | if (c > '\0' && c < '\x20') |
| 2973 | /* illegal character */ |
| 2974 | return 0; |
Johannes Schindelin | d2c84da | 2019-09-05 13:27:53 +0200 | [diff] [blame] | 2975 | } |
| 2976 | preceding_space_or_period = 0; |
| 2977 | i++; |
| 2978 | } |
| 2979 | } |
| 2980 | |
Johannes Schindelin | 396ff75 | 2019-06-19 14:05:59 -0700 | [diff] [blame] | 2981 | #if !defined(_MSC_VER) |
Karsten Blees | 5901dc6 | 2011-01-07 19:52:20 +0100 | [diff] [blame] | 2982 | /* |
| 2983 | * Disable MSVCRT command line wildcard expansion (__getmainargs called from |
| 2984 | * mingw startup code, see init.c in mingw runtime). |
| 2985 | */ |
| 2986 | int _CRT_glob = 0; |
Johannes Schindelin | 396ff75 | 2019-06-19 14:05:59 -0700 | [diff] [blame] | 2987 | #endif |
Karsten Blees | 3f04614 | 2011-01-16 18:28:27 +0100 | [diff] [blame] | 2988 | |
Johannes Schindelin | 0767172 | 2016-06-18 14:38:36 +0200 | [diff] [blame] | 2989 | static NORETURN void die_startup(void) |
Karsten Blees | 3f04614 | 2011-01-16 18:28:27 +0100 | [diff] [blame] | 2990 | { |
| 2991 | fputs("fatal: not enough memory for initialization", stderr); |
| 2992 | exit(128); |
| 2993 | } |
| 2994 | |
Karsten Blees | 6f1c189 | 2014-07-17 17:38:03 +0200 | [diff] [blame] | 2995 | static void *malloc_startup(size_t size) |
| 2996 | { |
| 2997 | void *result = malloc(size); |
| 2998 | if (!result) |
| 2999 | die_startup(); |
| 3000 | return result; |
| 3001 | } |
| 3002 | |
| 3003 | static char *wcstoutfdup_startup(char *buffer, const wchar_t *wcs, size_t len) |
| 3004 | { |
| 3005 | len = xwcstoutf(buffer, wcs, len) + 1; |
| 3006 | return memcpy(malloc_startup(len), buffer, len); |
| 3007 | } |
| 3008 | |
Johannes Schindelin | 3f94442 | 2017-11-01 18:10:25 +0100 | [diff] [blame] | 3009 | static void maybe_redirect_std_handle(const wchar_t *key, DWORD std_id, int fd, |
| 3010 | DWORD desired_access, DWORD flags) |
| 3011 | { |
| 3012 | DWORD create_flag = fd ? OPEN_ALWAYS : OPEN_EXISTING; |
| 3013 | wchar_t buf[MAX_PATH]; |
| 3014 | DWORD max = ARRAY_SIZE(buf); |
| 3015 | HANDLE handle; |
| 3016 | DWORD ret = GetEnvironmentVariableW(key, buf, max); |
| 3017 | |
| 3018 | if (!ret || ret >= max) |
| 3019 | return; |
| 3020 | |
| 3021 | /* make sure this does not leak into child processes */ |
| 3022 | SetEnvironmentVariableW(key, NULL); |
| 3023 | if (!wcscmp(buf, L"off")) { |
| 3024 | close(fd); |
| 3025 | handle = GetStdHandle(std_id); |
| 3026 | if (handle != INVALID_HANDLE_VALUE) |
| 3027 | CloseHandle(handle); |
| 3028 | return; |
| 3029 | } |
Johannes Schindelin | 1a172e4 | 2017-11-01 18:10:30 +0100 | [diff] [blame] | 3030 | if (std_id == STD_ERROR_HANDLE && !wcscmp(buf, L"2>&1")) { |
| 3031 | handle = GetStdHandle(STD_OUTPUT_HANDLE); |
| 3032 | if (handle == INVALID_HANDLE_VALUE) { |
| 3033 | close(fd); |
| 3034 | handle = GetStdHandle(std_id); |
| 3035 | if (handle != INVALID_HANDLE_VALUE) |
| 3036 | CloseHandle(handle); |
| 3037 | } else { |
| 3038 | int new_fd = _open_osfhandle((intptr_t)handle, O_BINARY); |
| 3039 | SetStdHandle(std_id, handle); |
| 3040 | dup2(new_fd, fd); |
| 3041 | /* do *not* close the new_fd: that would close stdout */ |
| 3042 | } |
| 3043 | return; |
| 3044 | } |
Johannes Schindelin | 3f94442 | 2017-11-01 18:10:25 +0100 | [diff] [blame] | 3045 | handle = CreateFileW(buf, desired_access, 0, NULL, create_flag, |
| 3046 | flags, NULL); |
| 3047 | if (handle != INVALID_HANDLE_VALUE) { |
| 3048 | int new_fd = _open_osfhandle((intptr_t)handle, O_BINARY); |
| 3049 | SetStdHandle(std_id, handle); |
| 3050 | dup2(new_fd, fd); |
| 3051 | close(new_fd); |
| 3052 | } |
| 3053 | } |
| 3054 | |
| 3055 | static void maybe_redirect_std_handles(void) |
| 3056 | { |
| 3057 | maybe_redirect_std_handle(L"GIT_REDIRECT_STDIN", STD_INPUT_HANDLE, 0, |
| 3058 | GENERIC_READ, FILE_ATTRIBUTE_NORMAL); |
| 3059 | maybe_redirect_std_handle(L"GIT_REDIRECT_STDOUT", STD_OUTPUT_HANDLE, 1, |
| 3060 | GENERIC_WRITE, FILE_ATTRIBUTE_NORMAL); |
| 3061 | maybe_redirect_std_handle(L"GIT_REDIRECT_STDERR", STD_ERROR_HANDLE, 2, |
| 3062 | GENERIC_WRITE, FILE_FLAG_NO_BUFFERING); |
| 3063 | } |
| 3064 | |
Jeff Hostetler | dce7d29 | 2019-06-25 07:49:39 -0700 | [diff] [blame] | 3065 | #ifdef _MSC_VER |
| 3066 | #ifdef _DEBUG |
| 3067 | #include <crtdbg.h> |
| 3068 | #endif |
| 3069 | #endif |
| 3070 | |
Johannes Schindelin | 396ff75 | 2019-06-19 14:05:59 -0700 | [diff] [blame] | 3071 | /* |
| 3072 | * We implement wmain() and compile with -municode, which would |
| 3073 | * normally ignore main(), but we call the latter from the former |
| 3074 | * so that we can handle non-ASCII command-line parameters |
| 3075 | * appropriately. |
| 3076 | * |
| 3077 | * To be more compatible with the core git code, we convert |
| 3078 | * argv into UTF8 and pass them directly to main(). |
| 3079 | */ |
| 3080 | int wmain(int argc, const wchar_t **wargv) |
Karsten Blees | 13f1df4 | 2011-01-07 19:47:23 +0100 | [diff] [blame] | 3081 | { |
Johannes Schindelin | 396ff75 | 2019-06-19 14:05:59 -0700 | [diff] [blame] | 3082 | int i, maxlen, exit_status; |
| 3083 | char *buffer, **save; |
| 3084 | const char **argv; |
Karsten Blees | 3f04614 | 2011-01-16 18:28:27 +0100 | [diff] [blame] | 3085 | |
Jeff Hostetler | a089724 | 2019-04-15 13:39:43 -0700 | [diff] [blame] | 3086 | trace2_initialize_clock(); |
| 3087 | |
Jeff Hostetler | 556702f | 2019-06-25 07:49:40 -0700 | [diff] [blame] | 3088 | #ifdef _MSC_VER |
Johannes Schindelin | f6a6393 | 2019-06-25 07:49:42 -0700 | [diff] [blame] | 3089 | #ifdef _DEBUG |
| 3090 | _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG); |
| 3091 | #endif |
| 3092 | |
Jeff Hostetler | 556702f | 2019-06-25 07:49:40 -0700 | [diff] [blame] | 3093 | #ifdef USE_MSVC_CRTDBG |
| 3094 | _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); |
| 3095 | #endif |
| 3096 | #endif |
| 3097 | |
Johannes Schindelin | 3f94442 | 2017-11-01 18:10:25 +0100 | [diff] [blame] | 3098 | maybe_redirect_std_handles(); |
| 3099 | |
Karsten Blees | 3f04614 | 2011-01-16 18:28:27 +0100 | [diff] [blame] | 3100 | /* determine size of argv and environ conversion buffer */ |
Johannes Schindelin | 86e2545 | 2018-04-10 11:05:46 -0400 | [diff] [blame] | 3101 | maxlen = wcslen(wargv[0]); |
Karsten Blees | 3f04614 | 2011-01-16 18:28:27 +0100 | [diff] [blame] | 3102 | for (i = 1; i < argc; i++) |
| 3103 | maxlen = max(maxlen, wcslen(wargv[i])); |
| 3104 | |
| 3105 | /* allocate buffer (wchar_t encodes to max 3 UTF-8 bytes) */ |
| 3106 | maxlen = 3 * maxlen + 1; |
Karsten Blees | 6f1c189 | 2014-07-17 17:38:03 +0200 | [diff] [blame] | 3107 | buffer = malloc_startup(maxlen); |
Karsten Blees | 3f04614 | 2011-01-16 18:28:27 +0100 | [diff] [blame] | 3108 | |
Johannes Schindelin | 396ff75 | 2019-06-19 14:05:59 -0700 | [diff] [blame] | 3109 | /* |
| 3110 | * Create a UTF-8 version of w_argv. Also create a "save" copy |
| 3111 | * to remember all the string pointers because parse_options() |
| 3112 | * will remove claimed items from the argv that we pass down. |
| 3113 | */ |
| 3114 | ALLOC_ARRAY(argv, argc + 1); |
| 3115 | ALLOC_ARRAY(save, argc + 1); |
Johannes Schindelin | 86e2545 | 2018-04-10 11:05:46 -0400 | [diff] [blame] | 3116 | for (i = 0; i < argc; i++) |
Johannes Schindelin | 396ff75 | 2019-06-19 14:05:59 -0700 | [diff] [blame] | 3117 | argv[i] = save[i] = wcstoutfdup_startup(buffer, wargv[i], maxlen); |
| 3118 | argv[i] = save[i] = NULL; |
Karsten Blees | 3f04614 | 2011-01-16 18:28:27 +0100 | [diff] [blame] | 3119 | free(buffer); |
Karsten Blees | 13f1df4 | 2011-01-07 19:47:23 +0100 | [diff] [blame] | 3120 | |
Karsten Blees | 6dc7154 | 2014-07-17 17:38:05 +0200 | [diff] [blame] | 3121 | /* fix Windows specific environment settings */ |
Karsten Blees | 02e6edc | 2016-01-26 15:34:38 +0100 | [diff] [blame] | 3122 | setup_windows_environment(); |
Karsten Blees | baea068 | 2014-07-17 17:38:06 +0200 | [diff] [blame] | 3123 | |
Johannes Schindelin | 0e218f9 | 2018-10-30 11:40:07 -0700 | [diff] [blame] | 3124 | unset_environment_variables = xstrdup("PERL5LIB"); |
| 3125 | |
Karsten Blees | 13f1df4 | 2011-01-07 19:47:23 +0100 | [diff] [blame] | 3126 | /* initialize critical section for waitpid pinfo_t list */ |
| 3127 | InitializeCriticalSection(&pinfo_cs); |
| 3128 | |
| 3129 | /* set up default file mode and file modes for stdin/out/err */ |
| 3130 | _fmode = _O_BINARY; |
| 3131 | _setmode(_fileno(stdin), _O_BINARY); |
| 3132 | _setmode(_fileno(stdout), _O_BINARY); |
| 3133 | _setmode(_fileno(stderr), _O_BINARY); |
Karsten Blees | eac14f8 | 2012-01-14 22:24:19 +0100 | [diff] [blame] | 3134 | |
| 3135 | /* initialize Unicode console */ |
| 3136 | winansi_init(); |
Johannes Schindelin | 396ff75 | 2019-06-19 14:05:59 -0700 | [diff] [blame] | 3137 | |
| 3138 | /* invoke the real main() using our utf8 version of argv. */ |
| 3139 | exit_status = main(argc, argv); |
| 3140 | |
| 3141 | for (i = 0; i < argc; i++) |
| 3142 | free(save[i]); |
| 3143 | free(save); |
| 3144 | free(argv); |
| 3145 | |
| 3146 | return exit_status; |
Karsten Blees | 13f1df4 | 2011-01-07 19:47:23 +0100 | [diff] [blame] | 3147 | } |
Nguyễn Thái Ngọc Duy | 7b6aff0 | 2015-03-08 17:12:45 +0700 | [diff] [blame] | 3148 | |
| 3149 | int uname(struct utsname *buf) |
| 3150 | { |
Johannes Sixt | d62c89a | 2015-10-23 08:02:52 +0200 | [diff] [blame] | 3151 | unsigned v = (unsigned)GetVersion(); |
Nguyễn Thái Ngọc Duy | 7b6aff0 | 2015-03-08 17:12:45 +0700 | [diff] [blame] | 3152 | memset(buf, 0, sizeof(*buf)); |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 3153 | xsnprintf(buf->sysname, sizeof(buf->sysname), "Windows"); |
| 3154 | xsnprintf(buf->release, sizeof(buf->release), |
| 3155 | "%u.%u", v & 0xff, (v >> 8) & 0xff); |
Nguyễn Thái Ngọc Duy | 7b6aff0 | 2015-03-08 17:12:45 +0700 | [diff] [blame] | 3156 | /* assuming NT variants only.. */ |
Jeff King | 5096d49 | 2015-09-24 17:06:08 -0400 | [diff] [blame] | 3157 | xsnprintf(buf->version, sizeof(buf->version), |
| 3158 | "%u", (v >> 16) & 0x7fff); |
Nguyễn Thái Ngọc Duy | 7b6aff0 | 2015-03-08 17:12:45 +0700 | [diff] [blame] | 3159 | return 0; |
| 3160 | } |