2007-12-02 04:24:59 +08:00
|
|
|
#include "../git-compat-util.h"
|
2008-09-27 16:43:01 +08:00
|
|
|
#include "win32.h"
|
2009-06-01 14:41:45 +08:00
|
|
|
#include <conio.h>
|
2012-03-16 01:21:28 +08:00
|
|
|
#include <wchar.h>
|
2007-11-25 05:49:16 +08:00
|
|
|
#include "../strbuf.h"
|
2011-02-08 04:51:21 +08:00
|
|
|
#include "../run-command.h"
|
2014-07-17 23:37:55 +08:00
|
|
|
#include "../cache.h"
|
2007-12-02 04:24:59 +08:00
|
|
|
|
2016-01-15 21:24:34 +08:00
|
|
|
#define HCAST(type, handle) ((type)(intptr_t)handle)
|
|
|
|
|
2011-02-08 04:50:26 +08:00
|
|
|
static const int delay[] = { 0, 1, 10, 20, 40 };
|
|
|
|
|
2010-01-16 04:12:20 +08:00
|
|
|
int err_win_to_posix(DWORD winerr)
|
2009-01-24 22:04:39 +08:00
|
|
|
{
|
|
|
|
int error = ENOSYS;
|
|
|
|
switch(winerr) {
|
|
|
|
case ERROR_ACCESS_DENIED: error = EACCES; break;
|
|
|
|
case ERROR_ACCOUNT_DISABLED: error = EACCES; break;
|
|
|
|
case ERROR_ACCOUNT_RESTRICTION: error = EACCES; break;
|
|
|
|
case ERROR_ALREADY_ASSIGNED: error = EBUSY; break;
|
|
|
|
case ERROR_ALREADY_EXISTS: error = EEXIST; break;
|
|
|
|
case ERROR_ARITHMETIC_OVERFLOW: error = ERANGE; break;
|
|
|
|
case ERROR_BAD_COMMAND: error = EIO; break;
|
|
|
|
case ERROR_BAD_DEVICE: error = ENODEV; break;
|
|
|
|
case ERROR_BAD_DRIVER_LEVEL: error = ENXIO; break;
|
|
|
|
case ERROR_BAD_EXE_FORMAT: error = ENOEXEC; break;
|
|
|
|
case ERROR_BAD_FORMAT: error = ENOEXEC; break;
|
|
|
|
case ERROR_BAD_LENGTH: error = EINVAL; break;
|
|
|
|
case ERROR_BAD_PATHNAME: error = ENOENT; break;
|
|
|
|
case ERROR_BAD_PIPE: error = EPIPE; break;
|
|
|
|
case ERROR_BAD_UNIT: error = ENODEV; break;
|
|
|
|
case ERROR_BAD_USERNAME: error = EINVAL; break;
|
|
|
|
case ERROR_BROKEN_PIPE: error = EPIPE; break;
|
|
|
|
case ERROR_BUFFER_OVERFLOW: error = ENAMETOOLONG; break;
|
|
|
|
case ERROR_BUSY: error = EBUSY; break;
|
|
|
|
case ERROR_BUSY_DRIVE: error = EBUSY; break;
|
|
|
|
case ERROR_CALL_NOT_IMPLEMENTED: error = ENOSYS; break;
|
|
|
|
case ERROR_CANNOT_MAKE: error = EACCES; break;
|
|
|
|
case ERROR_CANTOPEN: error = EIO; break;
|
|
|
|
case ERROR_CANTREAD: error = EIO; break;
|
|
|
|
case ERROR_CANTWRITE: error = EIO; break;
|
|
|
|
case ERROR_CRC: error = EIO; break;
|
|
|
|
case ERROR_CURRENT_DIRECTORY: error = EACCES; break;
|
|
|
|
case ERROR_DEVICE_IN_USE: error = EBUSY; break;
|
|
|
|
case ERROR_DEV_NOT_EXIST: error = ENODEV; break;
|
|
|
|
case ERROR_DIRECTORY: error = EINVAL; break;
|
|
|
|
case ERROR_DIR_NOT_EMPTY: error = ENOTEMPTY; break;
|
|
|
|
case ERROR_DISK_CHANGE: error = EIO; break;
|
|
|
|
case ERROR_DISK_FULL: error = ENOSPC; break;
|
|
|
|
case ERROR_DRIVE_LOCKED: error = EBUSY; break;
|
|
|
|
case ERROR_ENVVAR_NOT_FOUND: error = EINVAL; break;
|
|
|
|
case ERROR_EXE_MARKED_INVALID: error = ENOEXEC; break;
|
|
|
|
case ERROR_FILENAME_EXCED_RANGE: error = ENAMETOOLONG; break;
|
|
|
|
case ERROR_FILE_EXISTS: error = EEXIST; break;
|
|
|
|
case ERROR_FILE_INVALID: error = ENODEV; break;
|
|
|
|
case ERROR_FILE_NOT_FOUND: error = ENOENT; break;
|
|
|
|
case ERROR_GEN_FAILURE: error = EIO; break;
|
|
|
|
case ERROR_HANDLE_DISK_FULL: error = ENOSPC; break;
|
|
|
|
case ERROR_INSUFFICIENT_BUFFER: error = ENOMEM; break;
|
|
|
|
case ERROR_INVALID_ACCESS: error = EACCES; break;
|
|
|
|
case ERROR_INVALID_ADDRESS: error = EFAULT; break;
|
|
|
|
case ERROR_INVALID_BLOCK: error = EFAULT; break;
|
|
|
|
case ERROR_INVALID_DATA: error = EINVAL; break;
|
|
|
|
case ERROR_INVALID_DRIVE: error = ENODEV; break;
|
|
|
|
case ERROR_INVALID_EXE_SIGNATURE: error = ENOEXEC; break;
|
|
|
|
case ERROR_INVALID_FLAGS: error = EINVAL; break;
|
|
|
|
case ERROR_INVALID_FUNCTION: error = ENOSYS; break;
|
|
|
|
case ERROR_INVALID_HANDLE: error = EBADF; break;
|
|
|
|
case ERROR_INVALID_LOGON_HOURS: error = EACCES; break;
|
|
|
|
case ERROR_INVALID_NAME: error = EINVAL; break;
|
|
|
|
case ERROR_INVALID_OWNER: error = EINVAL; break;
|
|
|
|
case ERROR_INVALID_PARAMETER: error = EINVAL; break;
|
|
|
|
case ERROR_INVALID_PASSWORD: error = EPERM; break;
|
|
|
|
case ERROR_INVALID_PRIMARY_GROUP: error = EINVAL; break;
|
|
|
|
case ERROR_INVALID_SIGNAL_NUMBER: error = EINVAL; break;
|
|
|
|
case ERROR_INVALID_TARGET_HANDLE: error = EIO; break;
|
|
|
|
case ERROR_INVALID_WORKSTATION: error = EACCES; break;
|
|
|
|
case ERROR_IO_DEVICE: error = EIO; break;
|
|
|
|
case ERROR_IO_INCOMPLETE: error = EINTR; break;
|
|
|
|
case ERROR_LOCKED: error = EBUSY; break;
|
|
|
|
case ERROR_LOCK_VIOLATION: error = EACCES; break;
|
|
|
|
case ERROR_LOGON_FAILURE: error = EACCES; break;
|
|
|
|
case ERROR_MAPPED_ALIGNMENT: error = EINVAL; break;
|
|
|
|
case ERROR_META_EXPANSION_TOO_LONG: error = E2BIG; break;
|
|
|
|
case ERROR_MORE_DATA: error = EPIPE; break;
|
|
|
|
case ERROR_NEGATIVE_SEEK: error = ESPIPE; break;
|
|
|
|
case ERROR_NOACCESS: error = EFAULT; break;
|
|
|
|
case ERROR_NONE_MAPPED: error = EINVAL; break;
|
|
|
|
case ERROR_NOT_ENOUGH_MEMORY: error = ENOMEM; break;
|
|
|
|
case ERROR_NOT_READY: error = EAGAIN; break;
|
|
|
|
case ERROR_NOT_SAME_DEVICE: error = EXDEV; break;
|
|
|
|
case ERROR_NO_DATA: error = EPIPE; break;
|
|
|
|
case ERROR_NO_MORE_SEARCH_HANDLES: error = EIO; break;
|
|
|
|
case ERROR_NO_PROC_SLOTS: error = EAGAIN; break;
|
|
|
|
case ERROR_NO_SUCH_PRIVILEGE: error = EACCES; break;
|
|
|
|
case ERROR_OPEN_FAILED: error = EIO; break;
|
|
|
|
case ERROR_OPEN_FILES: error = EBUSY; break;
|
|
|
|
case ERROR_OPERATION_ABORTED: error = EINTR; break;
|
|
|
|
case ERROR_OUTOFMEMORY: error = ENOMEM; break;
|
|
|
|
case ERROR_PASSWORD_EXPIRED: error = EACCES; break;
|
|
|
|
case ERROR_PATH_BUSY: error = EBUSY; break;
|
|
|
|
case ERROR_PATH_NOT_FOUND: error = ENOENT; break;
|
|
|
|
case ERROR_PIPE_BUSY: error = EBUSY; break;
|
|
|
|
case ERROR_PIPE_CONNECTED: error = EPIPE; break;
|
|
|
|
case ERROR_PIPE_LISTENING: error = EPIPE; break;
|
|
|
|
case ERROR_PIPE_NOT_CONNECTED: error = EPIPE; break;
|
|
|
|
case ERROR_PRIVILEGE_NOT_HELD: error = EACCES; break;
|
|
|
|
case ERROR_READ_FAULT: error = EIO; break;
|
|
|
|
case ERROR_SEEK: error = EIO; break;
|
|
|
|
case ERROR_SEEK_ON_DEVICE: error = ESPIPE; break;
|
|
|
|
case ERROR_SHARING_BUFFER_EXCEEDED: error = ENFILE; break;
|
|
|
|
case ERROR_SHARING_VIOLATION: error = EACCES; break;
|
|
|
|
case ERROR_STACK_OVERFLOW: error = ENOMEM; break;
|
|
|
|
case ERROR_SWAPERROR: error = ENOENT; break;
|
|
|
|
case ERROR_TOO_MANY_MODULES: error = EMFILE; break;
|
|
|
|
case ERROR_TOO_MANY_OPEN_FILES: error = EMFILE; break;
|
|
|
|
case ERROR_UNRECOGNIZED_MEDIA: error = ENXIO; break;
|
|
|
|
case ERROR_UNRECOGNIZED_VOLUME: error = ENODEV; break;
|
|
|
|
case ERROR_WAIT_NO_CHILDREN: error = ECHILD; break;
|
|
|
|
case ERROR_WRITE_FAULT: error = EIO; break;
|
|
|
|
case ERROR_WRITE_PROTECT: error = EROFS; break;
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2011-02-08 04:50:26 +08:00
|
|
|
static inline int is_file_in_use_error(DWORD errcode)
|
|
|
|
{
|
|
|
|
switch (errcode) {
|
|
|
|
case ERROR_SHARING_VIOLATION:
|
|
|
|
case ERROR_ACCESS_DENIED:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-08 04:51:21 +08:00
|
|
|
static int read_yes_no_answer(void)
|
|
|
|
{
|
|
|
|
char answer[1024];
|
|
|
|
|
|
|
|
if (fgets(answer, sizeof(answer), stdin)) {
|
|
|
|
size_t answer_len = strlen(answer);
|
|
|
|
int got_full_line = 0, c;
|
|
|
|
|
|
|
|
/* remove the newline */
|
|
|
|
if (answer_len >= 2 && answer[answer_len-2] == '\r') {
|
|
|
|
answer[answer_len-2] = '\0';
|
|
|
|
got_full_line = 1;
|
|
|
|
} else if (answer_len >= 1 && answer[answer_len-1] == '\n') {
|
|
|
|
answer[answer_len-1] = '\0';
|
|
|
|
got_full_line = 1;
|
|
|
|
}
|
|
|
|
/* flush the buffer in case we did not get the full line */
|
|
|
|
if (!got_full_line)
|
|
|
|
while ((c = getchar()) != EOF && c != '\n')
|
|
|
|
;
|
|
|
|
} else
|
|
|
|
/* we could not read, return the
|
|
|
|
* default answer which is no */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (tolower(answer[0]) == 'y' && !answer[1])
|
|
|
|
return 1;
|
|
|
|
if (!strncasecmp(answer, "yes", sizeof(answer)))
|
|
|
|
return 1;
|
|
|
|
if (tolower(answer[0]) == 'n' && !answer[1])
|
|
|
|
return 0;
|
|
|
|
if (!strncasecmp(answer, "no", sizeof(answer)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* did not find an answer we understand */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ask_yes_no_if_possible(const char *format, ...)
|
|
|
|
{
|
|
|
|
char question[4096];
|
|
|
|
const char *retry_hook[] = { NULL, NULL, NULL };
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
vsnprintf(question, sizeof(question), format, args);
|
|
|
|
va_end(args);
|
|
|
|
|
2011-06-06 15:06:02 +08:00
|
|
|
if ((retry_hook[0] = mingw_getenv("GIT_ASK_YESNO"))) {
|
2011-02-08 04:51:21 +08:00
|
|
|
retry_hook[1] = question;
|
|
|
|
return !run_command_v_opt(retry_hook, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isatty(_fileno(stdin)) || !isatty(_fileno(stderr)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int answer;
|
|
|
|
fprintf(stderr, "%s (y/n) ", question);
|
|
|
|
|
|
|
|
if ((answer = read_yes_no_answer()) >= 0)
|
|
|
|
return answer;
|
|
|
|
|
|
|
|
fprintf(stderr, "Sorry, I did not understand your answer. "
|
|
|
|
"Please type 'y' or 'n'\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-08 04:49:33 +08:00
|
|
|
int mingw_unlink(const char *pathname)
|
|
|
|
{
|
2011-02-08 04:50:26 +08:00
|
|
|
int ret, tries = 0;
|
2012-03-16 01:21:28 +08:00
|
|
|
wchar_t wpathname[MAX_PATH];
|
|
|
|
if (xutftowcs_path(wpathname, pathname) < 0)
|
|
|
|
return -1;
|
2011-02-08 04:50:26 +08:00
|
|
|
|
2011-02-08 04:49:33 +08:00
|
|
|
/* read-only files cannot be removed */
|
2012-03-16 01:21:28 +08:00
|
|
|
_wchmod(wpathname, 0666);
|
|
|
|
while ((ret = _wunlink(wpathname)) == -1 && tries < ARRAY_SIZE(delay)) {
|
2011-02-08 04:50:26 +08:00
|
|
|
if (!is_file_in_use_error(GetLastError()))
|
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* We assume that some other process had the source or
|
|
|
|
* destination file open at the wrong moment and retry.
|
|
|
|
* In order to give the other process a higher chance to
|
|
|
|
* complete its operation, we give up our time slice now.
|
|
|
|
* If we have to retry again, we do sleep a bit.
|
|
|
|
*/
|
|
|
|
Sleep(delay[tries]);
|
|
|
|
tries++;
|
|
|
|
}
|
2011-02-08 04:51:21 +08:00
|
|
|
while (ret == -1 && is_file_in_use_error(GetLastError()) &&
|
|
|
|
ask_yes_no_if_possible("Unlink of file '%s' failed. "
|
|
|
|
"Should I try again?", pathname))
|
2012-03-16 01:21:28 +08:00
|
|
|
ret = _wunlink(wpathname);
|
2011-02-08 04:50:26 +08:00
|
|
|
return ret;
|
2011-02-08 04:49:33 +08:00
|
|
|
}
|
|
|
|
|
2012-03-16 01:21:28 +08:00
|
|
|
static int is_dir_empty(const wchar_t *wpath)
|
2011-02-08 04:54:01 +08:00
|
|
|
{
|
2012-03-16 01:21:28 +08:00
|
|
|
WIN32_FIND_DATAW findbuf;
|
2011-02-08 04:54:01 +08:00
|
|
|
HANDLE handle;
|
2012-03-16 01:21:28 +08:00
|
|
|
wchar_t wbuf[MAX_PATH + 2];
|
|
|
|
wcscpy(wbuf, wpath);
|
|
|
|
wcscat(wbuf, L"\\*");
|
|
|
|
handle = FindFirstFileW(wbuf, &findbuf);
|
|
|
|
if (handle == INVALID_HANDLE_VALUE)
|
2011-02-08 04:54:01 +08:00
|
|
|
return GetLastError() == ERROR_NO_MORE_FILES;
|
|
|
|
|
2012-03-16 01:21:28 +08:00
|
|
|
while (!wcscmp(findbuf.cFileName, L".") ||
|
|
|
|
!wcscmp(findbuf.cFileName, L".."))
|
|
|
|
if (!FindNextFileW(handle, &findbuf)) {
|
|
|
|
DWORD err = GetLastError();
|
|
|
|
FindClose(handle);
|
|
|
|
return err == ERROR_NO_MORE_FILES;
|
2011-02-08 04:54:01 +08:00
|
|
|
}
|
|
|
|
FindClose(handle);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-08 04:52:34 +08:00
|
|
|
int mingw_rmdir(const char *pathname)
|
|
|
|
{
|
|
|
|
int ret, tries = 0;
|
2012-03-16 01:21:28 +08:00
|
|
|
wchar_t wpathname[MAX_PATH];
|
|
|
|
if (xutftowcs_path(wpathname, pathname) < 0)
|
|
|
|
return -1;
|
2011-02-08 04:52:34 +08:00
|
|
|
|
2012-03-16 01:21:28 +08:00
|
|
|
while ((ret = _wrmdir(wpathname)) == -1 && tries < ARRAY_SIZE(delay)) {
|
2011-02-08 04:52:34 +08:00
|
|
|
if (!is_file_in_use_error(GetLastError()))
|
2012-12-10 22:42:27 +08:00
|
|
|
errno = err_win_to_posix(GetLastError());
|
|
|
|
if (errno != EACCES)
|
2011-02-08 04:52:34 +08:00
|
|
|
break;
|
2012-03-16 01:21:28 +08:00
|
|
|
if (!is_dir_empty(wpathname)) {
|
2011-02-08 04:54:01 +08:00
|
|
|
errno = ENOTEMPTY;
|
|
|
|
break;
|
|
|
|
}
|
2011-02-08 04:52:34 +08:00
|
|
|
/*
|
|
|
|
* We assume that some other process had the source or
|
|
|
|
* destination file open at the wrong moment and retry.
|
|
|
|
* In order to give the other process a higher chance to
|
|
|
|
* complete its operation, we give up our time slice now.
|
|
|
|
* If we have to retry again, we do sleep a bit.
|
|
|
|
*/
|
|
|
|
Sleep(delay[tries]);
|
|
|
|
tries++;
|
|
|
|
}
|
2012-12-10 22:42:27 +08:00
|
|
|
while (ret == -1 && errno == EACCES && is_file_in_use_error(GetLastError()) &&
|
2011-02-08 04:52:34 +08:00
|
|
|
ask_yes_no_if_possible("Deletion of directory '%s' failed. "
|
|
|
|
"Should I try again?", pathname))
|
2012-03-16 01:21:28 +08:00
|
|
|
ret = _wrmdir(wpathname);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mingw_mkdir(const char *path, int mode)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
wchar_t wpath[MAX_PATH];
|
|
|
|
if (xutftowcs_path(wpath, path) < 0)
|
|
|
|
return -1;
|
|
|
|
ret = _wmkdir(wpath);
|
2011-02-08 04:52:34 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-11-16 05:22:47 +08:00
|
|
|
int mingw_open (const char *filename, int oflags, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
unsigned mode;
|
2009-09-16 16:20:17 +08:00
|
|
|
int fd;
|
2012-03-16 01:21:28 +08:00
|
|
|
wchar_t wfilename[MAX_PATH];
|
2009-09-16 16:20:17 +08:00
|
|
|
|
2007-11-16 05:22:47 +08:00
|
|
|
va_start(args, oflags);
|
|
|
|
mode = va_arg(args, int);
|
|
|
|
va_end(args);
|
|
|
|
|
2010-09-24 01:35:25 +08:00
|
|
|
if (filename && !strcmp(filename, "/dev/null"))
|
2007-11-16 05:22:47 +08:00
|
|
|
filename = "nul";
|
2009-09-16 16:20:17 +08:00
|
|
|
|
2012-03-16 01:21:28 +08:00
|
|
|
if (xutftowcs_path(wfilename, filename) < 0)
|
|
|
|
return -1;
|
|
|
|
fd = _wopen(wfilename, oflags, mode);
|
2009-09-16 16:20:17 +08:00
|
|
|
|
2014-11-17 05:06:26 +08:00
|
|
|
if (fd < 0 && (oflags & O_ACCMODE) != O_RDONLY && errno == EACCES) {
|
2012-03-16 01:21:28 +08:00
|
|
|
DWORD attrs = GetFileAttributesW(wfilename);
|
2007-11-16 05:22:47 +08:00
|
|
|
if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY))
|
|
|
|
errno = EISDIR;
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2012-12-04 16:10:38 +08:00
|
|
|
static BOOL WINAPI ctrl_ignore(DWORD type)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef fgetc
|
|
|
|
int mingw_fgetc(FILE *stream)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
if (!isatty(_fileno(stream)))
|
|
|
|
return fgetc(stream);
|
|
|
|
|
|
|
|
SetConsoleCtrlHandler(ctrl_ignore, TRUE);
|
|
|
|
while (1) {
|
|
|
|
ch = fgetc(stream);
|
|
|
|
if (ch != EOF || GetLastError() != ERROR_OPERATION_ABORTED)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Ctrl+C was pressed, simulate SIGINT and retry */
|
|
|
|
mingw_raise(SIGINT);
|
|
|
|
}
|
|
|
|
SetConsoleCtrlHandler(ctrl_ignore, FALSE);
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
2010-02-26 04:03:44 +08:00
|
|
|
#undef fopen
|
|
|
|
FILE *mingw_fopen (const char *filename, const char *otype)
|
|
|
|
{
|
2012-03-16 01:21:28 +08:00
|
|
|
FILE *file;
|
|
|
|
wchar_t wfilename[MAX_PATH], wotype[4];
|
2010-09-24 01:35:25 +08:00
|
|
|
if (filename && !strcmp(filename, "/dev/null"))
|
2010-02-26 04:03:44 +08:00
|
|
|
filename = "nul";
|
2012-03-16 01:21:28 +08:00
|
|
|
if (xutftowcs_path(wfilename, filename) < 0 ||
|
|
|
|
xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
|
|
|
|
return NULL;
|
|
|
|
file = _wfopen(wfilename, wotype);
|
|
|
|
return file;
|
2010-02-26 04:03:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
|
|
|
|
{
|
2012-03-16 01:21:28 +08:00
|
|
|
FILE *file;
|
|
|
|
wchar_t wfilename[MAX_PATH], wotype[4];
|
2010-02-26 04:03:44 +08:00
|
|
|
if (filename && !strcmp(filename, "/dev/null"))
|
|
|
|
filename = "nul";
|
2012-03-16 01:21:28 +08:00
|
|
|
if (xutftowcs_path(wfilename, filename) < 0 ||
|
|
|
|
xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
|
|
|
|
return NULL;
|
|
|
|
file = _wfreopen(wfilename, wotype, stream);
|
|
|
|
return file;
|
2010-02-26 04:03:44 +08:00
|
|
|
}
|
|
|
|
|
2012-10-17 15:05:51 +08:00
|
|
|
#undef fflush
|
|
|
|
int mingw_fflush(FILE *stream)
|
|
|
|
{
|
|
|
|
int ret = fflush(stream);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* write() is used behind the scenes of stdio output functions.
|
|
|
|
* Since git code does not check for errors after each stdio write
|
|
|
|
* operation, it can happen that write() is called by a later
|
|
|
|
* stdio function even if an earlier write() call failed. In the
|
|
|
|
* case of a pipe whose readable end was closed, only the first
|
|
|
|
* call to write() reports EPIPE on Windows. Subsequent write()
|
|
|
|
* calls report EINVAL. It is impossible to notice whether this
|
|
|
|
* fflush invocation triggered such a case, therefore, we have to
|
|
|
|
* catch all EINVAL errors whole-sale.
|
|
|
|
*/
|
|
|
|
if (ret && errno == EINVAL)
|
|
|
|
errno = EPIPE;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-12-18 01:08:15 +08:00
|
|
|
#undef write
|
|
|
|
ssize_t mingw_write(int fd, const void *buf, size_t len)
|
|
|
|
{
|
|
|
|
ssize_t result = write(fd, buf, len);
|
|
|
|
|
|
|
|
if (result < 0 && errno == EINVAL && buf) {
|
|
|
|
/* check if fd is a pipe */
|
|
|
|
HANDLE h = (HANDLE) _get_osfhandle(fd);
|
|
|
|
if (GetFileType(h) == FILE_TYPE_PIPE)
|
|
|
|
errno = EPIPE;
|
|
|
|
else
|
|
|
|
errno = EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-03-16 01:21:28 +08:00
|
|
|
int mingw_access(const char *filename, int mode)
|
|
|
|
{
|
|
|
|
wchar_t wfilename[MAX_PATH];
|
|
|
|
if (xutftowcs_path(wfilename, filename) < 0)
|
|
|
|
return -1;
|
|
|
|
/* X_OK is not supported by the MSVCRT version */
|
|
|
|
return _waccess(wfilename, mode & ~X_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
int mingw_chdir(const char *dirname)
|
|
|
|
{
|
|
|
|
wchar_t wdirname[MAX_PATH];
|
|
|
|
if (xutftowcs_path(wdirname, dirname) < 0)
|
|
|
|
return -1;
|
|
|
|
return _wchdir(wdirname);
|
|
|
|
}
|
|
|
|
|
|
|
|
int mingw_chmod(const char *filename, int mode)
|
|
|
|
{
|
|
|
|
wchar_t wfilename[MAX_PATH];
|
|
|
|
if (xutftowcs_path(wfilename, filename) < 0)
|
|
|
|
return -1;
|
|
|
|
return _wchmod(wfilename, mode);
|
|
|
|
}
|
|
|
|
|
2010-01-16 04:12:21 +08:00
|
|
|
/*
|
|
|
|
* The unit of FILETIME is 100-nanoseconds since January 1, 1601, UTC.
|
|
|
|
* Returns the 100-nanoseconds ("hekto nanoseconds") since the epoch.
|
|
|
|
*/
|
|
|
|
static inline long long filetime_to_hnsec(const FILETIME *ft)
|
2007-09-04 02:40:26 +08:00
|
|
|
{
|
|
|
|
long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime;
|
2010-01-16 04:12:21 +08:00
|
|
|
/* Windows to Unix Epoch conversion */
|
|
|
|
return winTime - 116444736000000000LL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline time_t filetime_to_time_t(const FILETIME *ft)
|
|
|
|
{
|
|
|
|
return (time_t)(filetime_to_hnsec(ft) / 10000000);
|
2007-09-04 02:40:26 +08:00
|
|
|
}
|
|
|
|
|
2016-01-26 22:34:52 +08:00
|
|
|
/**
|
|
|
|
* Verifies that safe_create_leading_directories() would succeed.
|
|
|
|
*/
|
|
|
|
static int has_valid_directory_prefix(wchar_t *wfilename)
|
|
|
|
{
|
|
|
|
int n = wcslen(wfilename);
|
|
|
|
|
|
|
|
while (n > 0) {
|
|
|
|
wchar_t c = wfilename[--n];
|
|
|
|
DWORD attributes;
|
|
|
|
|
|
|
|
if (!is_dir_sep(c))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
wfilename[n] = L'\0';
|
|
|
|
attributes = GetFileAttributesW(wfilename);
|
|
|
|
wfilename[n] = c;
|
|
|
|
if (attributes == FILE_ATTRIBUTE_DIRECTORY ||
|
|
|
|
attributes == FILE_ATTRIBUTE_DEVICE)
|
|
|
|
return 1;
|
|
|
|
if (attributes == INVALID_FILE_ATTRIBUTES)
|
|
|
|
switch (GetLastError()) {
|
|
|
|
case ERROR_PATH_NOT_FOUND:
|
|
|
|
continue;
|
|
|
|
case ERROR_FILE_NOT_FOUND:
|
|
|
|
/* This implies parent directory exists. */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-09-04 02:40:26 +08:00
|
|
|
/* We keep the do_lstat code in a separate function to avoid recursion.
|
|
|
|
* When a path ends with a slash, the stat will fail with ENOENT. In
|
|
|
|
* this case, we strip the trailing slashes and stat again.
|
2010-03-17 23:17:34 +08:00
|
|
|
*
|
|
|
|
* If follow is true then act like stat() and report on the link
|
|
|
|
* target. Otherwise report on the link itself.
|
2007-09-04 02:40:26 +08:00
|
|
|
*/
|
2010-03-17 23:17:34 +08:00
|
|
|
static int do_lstat(int follow, const char *file_name, struct stat *buf)
|
2007-09-04 02:40:26 +08:00
|
|
|
{
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fdata;
|
2012-03-16 01:21:28 +08:00
|
|
|
wchar_t wfilename[MAX_PATH];
|
|
|
|
if (xutftowcs_path(wfilename, file_name) < 0)
|
|
|
|
return -1;
|
2007-09-04 02:40:26 +08:00
|
|
|
|
2012-03-16 01:21:28 +08:00
|
|
|
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
|
2007-09-04 02:40:26 +08:00
|
|
|
buf->st_ino = 0;
|
|
|
|
buf->st_gid = 0;
|
|
|
|
buf->st_uid = 0;
|
2008-08-19 04:01:06 +08:00
|
|
|
buf->st_nlink = 1;
|
2008-09-27 16:43:01 +08:00
|
|
|
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes);
|
2009-03-06 00:05:12 +08:00
|
|
|
buf->st_size = fdata.nFileSizeLow |
|
|
|
|
(((off_t)fdata.nFileSizeHigh)<<32);
|
2008-09-27 16:39:45 +08:00
|
|
|
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
|
2007-09-04 02:40:26 +08:00
|
|
|
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
|
|
|
|
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
|
|
|
|
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
|
2010-03-17 23:17:34 +08:00
|
|
|
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
|
2012-03-16 01:21:28 +08:00
|
|
|
WIN32_FIND_DATAW findbuf;
|
|
|
|
HANDLE handle = FindFirstFileW(wfilename, &findbuf);
|
2010-03-17 23:17:34 +08:00
|
|
|
if (handle != INVALID_HANDLE_VALUE) {
|
|
|
|
if ((findbuf.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) &&
|
|
|
|
(findbuf.dwReserved0 == IO_REPARSE_TAG_SYMLINK)) {
|
|
|
|
if (follow) {
|
|
|
|
char buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
|
|
|
|
buf->st_size = readlink(file_name, buffer, MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
|
|
|
|
} else {
|
|
|
|
buf->st_mode = S_IFLNK;
|
|
|
|
}
|
|
|
|
buf->st_mode |= S_IREAD;
|
|
|
|
if (!(findbuf.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
|
|
|
|
buf->st_mode |= S_IWRITE;
|
|
|
|
}
|
|
|
|
FindClose(handle);
|
|
|
|
}
|
|
|
|
}
|
2007-09-04 02:40:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2012-03-16 01:21:28 +08:00
|
|
|
switch (GetLastError()) {
|
|
|
|
case ERROR_ACCESS_DENIED:
|
|
|
|
case ERROR_SHARING_VIOLATION:
|
|
|
|
case ERROR_LOCK_VIOLATION:
|
|
|
|
case ERROR_SHARING_BUFFER_EXCEEDED:
|
|
|
|
errno = EACCES;
|
|
|
|
break;
|
|
|
|
case ERROR_BUFFER_OVERFLOW:
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
break;
|
|
|
|
case ERROR_NOT_ENOUGH_MEMORY:
|
|
|
|
errno = ENOMEM;
|
|
|
|
break;
|
2016-01-26 22:34:52 +08:00
|
|
|
case ERROR_PATH_NOT_FOUND:
|
|
|
|
if (!has_valid_directory_prefix(wfilename)) {
|
|
|
|
errno = ENOTDIR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* fallthru */
|
2012-03-16 01:21:28 +08:00
|
|
|
default:
|
|
|
|
errno = ENOENT;
|
|
|
|
break;
|
|
|
|
}
|
2007-09-04 02:40:26 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We provide our own lstat/fstat functions, since the provided
|
|
|
|
* lstat/fstat functions are so slow. These stat functions are
|
|
|
|
* tailored for Git's usage (read: fast), and are not meant to be
|
|
|
|
* complete. Note that Git stat()s are redirected to mingw_lstat()
|
|
|
|
* too, since Windows doesn't really handle symlinks that well.
|
|
|
|
*/
|
2010-03-17 23:17:34 +08:00
|
|
|
static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
|
2007-09-04 02:40:26 +08:00
|
|
|
{
|
|
|
|
int namelen;
|
2011-01-08 01:04:16 +08:00
|
|
|
char alt_name[PATH_MAX];
|
2007-09-04 02:40:26 +08:00
|
|
|
|
2010-03-17 23:17:34 +08:00
|
|
|
if (!do_lstat(follow, file_name, buf))
|
2007-09-04 02:40:26 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* if file_name ended in a '/', Windows returned ENOENT;
|
|
|
|
* try again without trailing slashes
|
|
|
|
*/
|
|
|
|
if (errno != ENOENT)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
namelen = strlen(file_name);
|
|
|
|
if (namelen && file_name[namelen-1] != '/')
|
|
|
|
return -1;
|
|
|
|
while (namelen && file_name[namelen-1] == '/')
|
|
|
|
--namelen;
|
|
|
|
if (!namelen || namelen >= PATH_MAX)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memcpy(alt_name, file_name, namelen);
|
|
|
|
alt_name[namelen] = 0;
|
2010-03-17 23:17:34 +08:00
|
|
|
return do_lstat(follow, alt_name, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
int mingw_lstat(const char *file_name, struct stat *buf)
|
|
|
|
{
|
|
|
|
return do_stat_internal(0, file_name, buf);
|
|
|
|
}
|
|
|
|
int mingw_stat(const char *file_name, struct stat *buf)
|
|
|
|
{
|
|
|
|
return do_stat_internal(1, file_name, buf);
|
2007-09-04 02:40:26 +08:00
|
|
|
}
|
|
|
|
|
2008-08-19 04:01:06 +08:00
|
|
|
int mingw_fstat(int fd, struct stat *buf)
|
2007-09-04 02:40:26 +08:00
|
|
|
{
|
|
|
|
HANDLE fh = (HANDLE)_get_osfhandle(fd);
|
|
|
|
BY_HANDLE_FILE_INFORMATION fdata;
|
|
|
|
|
|
|
|
if (fh == INVALID_HANDLE_VALUE) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* direct non-file handles to MS's fstat() */
|
2008-08-19 04:01:06 +08:00
|
|
|
if (GetFileType(fh) != FILE_TYPE_DISK)
|
2009-03-06 00:05:12 +08:00
|
|
|
return _fstati64(fd, buf);
|
2007-09-04 02:40:26 +08:00
|
|
|
|
|
|
|
if (GetFileInformationByHandle(fh, &fdata)) {
|
|
|
|
buf->st_ino = 0;
|
|
|
|
buf->st_gid = 0;
|
|
|
|
buf->st_uid = 0;
|
2008-08-19 04:01:06 +08:00
|
|
|
buf->st_nlink = 1;
|
2008-09-27 16:43:01 +08:00
|
|
|
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes);
|
2009-03-06 00:05:12 +08:00
|
|
|
buf->st_size = fdata.nFileSizeLow |
|
|
|
|
(((off_t)fdata.nFileSizeHigh)<<32);
|
2008-09-27 16:39:45 +08:00
|
|
|
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
|
2007-09-04 02:40:26 +08:00
|
|
|
buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
|
|
|
|
buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
|
|
|
|
buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-09-07 19:05:00 +08:00
|
|
|
static inline void time_t_to_filetime(time_t t, FILETIME *ft)
|
|
|
|
{
|
|
|
|
long long winTime = t * 10000000LL + 116444736000000000LL;
|
|
|
|
ft->dwLowDateTime = winTime;
|
|
|
|
ft->dwHighDateTime = winTime >> 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mingw_utime (const char *file_name, const struct utimbuf *times)
|
|
|
|
{
|
|
|
|
FILETIME mft, aft;
|
|
|
|
int fh, rc;
|
2012-03-16 01:21:28 +08:00
|
|
|
DWORD attrs;
|
|
|
|
wchar_t wfilename[MAX_PATH];
|
|
|
|
if (xutftowcs_path(wfilename, file_name) < 0)
|
|
|
|
return -1;
|
2007-09-07 19:05:00 +08:00
|
|
|
|
|
|
|
/* must have write permission */
|
2012-03-16 01:21:28 +08:00
|
|
|
attrs = GetFileAttributesW(wfilename);
|
2010-03-30 15:46:23 +08:00
|
|
|
if (attrs != INVALID_FILE_ATTRIBUTES &&
|
|
|
|
(attrs & FILE_ATTRIBUTE_READONLY)) {
|
|
|
|
/* ignore errors here; open() will report them */
|
2012-03-16 01:21:28 +08:00
|
|
|
SetFileAttributesW(wfilename, attrs & ~FILE_ATTRIBUTE_READONLY);
|
2010-03-30 15:46:23 +08:00
|
|
|
}
|
|
|
|
|
2012-03-16 01:21:28 +08:00
|
|
|
if ((fh = _wopen(wfilename, O_RDWR | O_BINARY)) < 0) {
|
2010-03-30 15:46:23 +08:00
|
|
|
rc = -1;
|
|
|
|
goto revert_attrs;
|
|
|
|
}
|
2007-09-07 19:05:00 +08:00
|
|
|
|
2010-07-13 07:42:03 +08:00
|
|
|
if (times) {
|
|
|
|
time_t_to_filetime(times->modtime, &mft);
|
|
|
|
time_t_to_filetime(times->actime, &aft);
|
|
|
|
} else {
|
|
|
|
GetSystemTimeAsFileTime(&mft);
|
|
|
|
aft = mft;
|
|
|
|
}
|
2007-09-07 19:05:00 +08:00
|
|
|
if (!SetFileTime((HANDLE)_get_osfhandle(fh), NULL, &aft, &mft)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
rc = -1;
|
|
|
|
} else
|
|
|
|
rc = 0;
|
|
|
|
close(fh);
|
2010-03-30 15:46:23 +08:00
|
|
|
|
|
|
|
revert_attrs:
|
|
|
|
if (attrs != INVALID_FILE_ATTRIBUTES &&
|
|
|
|
(attrs & FILE_ATTRIBUTE_READONLY)) {
|
|
|
|
/* ignore errors again */
|
2012-03-16 01:21:28 +08:00
|
|
|
SetFileAttributesW(wfilename, attrs);
|
2010-03-30 15:46:23 +08:00
|
|
|
}
|
2007-09-07 19:05:00 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2007-12-02 04:24:59 +08:00
|
|
|
unsigned int sleep (unsigned int seconds)
|
|
|
|
{
|
|
|
|
Sleep(seconds*1000);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-16 01:21:28 +08:00
|
|
|
char *mingw_mktemp(char *template)
|
|
|
|
{
|
|
|
|
wchar_t wtemplate[MAX_PATH];
|
|
|
|
if (xutftowcs_path(wtemplate, template) < 0)
|
|
|
|
return NULL;
|
|
|
|
if (!_wmktemp(wtemplate))
|
|
|
|
return NULL;
|
|
|
|
if (xwcstoutf(template, wtemplate, strlen(template) + 1) < 0)
|
|
|
|
return NULL;
|
|
|
|
return template;
|
|
|
|
}
|
|
|
|
|
2007-12-02 04:24:59 +08:00
|
|
|
int mkstemp(char *template)
|
|
|
|
{
|
|
|
|
char *filename = mktemp(template);
|
|
|
|
if (filename == NULL)
|
|
|
|
return -1;
|
|
|
|
return open(filename, O_RDWR | O_CREAT, 0600);
|
|
|
|
}
|
|
|
|
|
|
|
|
int gettimeofday(struct timeval *tv, void *tz)
|
|
|
|
{
|
2010-01-16 04:12:21 +08:00
|
|
|
FILETIME ft;
|
|
|
|
long long hnsec;
|
|
|
|
|
|
|
|
GetSystemTimeAsFileTime(&ft);
|
|
|
|
hnsec = filetime_to_hnsec(&ft);
|
|
|
|
tv->tv_sec = hnsec / 10000000;
|
|
|
|
tv->tv_usec = (hnsec % 10000000) / 10;
|
2007-12-02 04:51:20 +08:00
|
|
|
return 0;
|
2007-12-02 04:24:59 +08:00
|
|
|
}
|
|
|
|
|
Windows: A pipe() replacement whose ends are not inherited to children.
On Unix the idiom to use a pipe is as follows:
pipe(fd);
pid = fork();
if (!pid) {
dup2(fd[1], 1);
close(fd[1]);
close(fd[0]);
...
}
close(fd[1]);
i.e. the child process closes the both pipe ends after duplicating one
to the file descriptors where they are needed.
On Windows, which does not have fork(), we never have an opportunity to
(1) duplicate a pipe end in the child, (2) close unused pipe ends. Instead,
we must use this idiom:
save1 = dup(1);
pipe(fd);
dup2(fd[1], 1);
spawn(...);
dup2(save1, 1);
close(fd[1]);
i.e. save away the descriptor at the destination slot, replace by the pipe
end, spawn process, restore the saved file.
But there is a problem: Notice that the child did not only inherit the
dup2()ed descriptor, but also *both* original pipe ends. Although the one
end that was dup()ed could be closed before the spawn(), we cannot close
the other end - the child inherits it, no matter what.
The solution is to generate non-inheritable pipes. At the first glance,
this looks strange: The purpose of pipes is usually to be inherited to
child processes. But notice that in the course of actions as outlined
above, the pipe descriptor that we want to inherit to the child is
dup2()ed, and as it so happens, Windows's dup2() creates inheritable
duplicates.
Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
2007-12-08 05:05:36 +08:00
|
|
|
int pipe(int filedes[2])
|
|
|
|
{
|
2010-01-16 04:12:17 +08:00
|
|
|
HANDLE h[2];
|
Windows: A pipe() replacement whose ends are not inherited to children.
On Unix the idiom to use a pipe is as follows:
pipe(fd);
pid = fork();
if (!pid) {
dup2(fd[1], 1);
close(fd[1]);
close(fd[0]);
...
}
close(fd[1]);
i.e. the child process closes the both pipe ends after duplicating one
to the file descriptors where they are needed.
On Windows, which does not have fork(), we never have an opportunity to
(1) duplicate a pipe end in the child, (2) close unused pipe ends. Instead,
we must use this idiom:
save1 = dup(1);
pipe(fd);
dup2(fd[1], 1);
spawn(...);
dup2(save1, 1);
close(fd[1]);
i.e. save away the descriptor at the destination slot, replace by the pipe
end, spawn process, restore the saved file.
But there is a problem: Notice that the child did not only inherit the
dup2()ed descriptor, but also *both* original pipe ends. Although the one
end that was dup()ed could be closed before the spawn(), we cannot close
the other end - the child inherits it, no matter what.
The solution is to generate non-inheritable pipes. At the first glance,
this looks strange: The purpose of pipes is usually to be inherited to
child processes. But notice that in the course of actions as outlined
above, the pipe descriptor that we want to inherit to the child is
dup2()ed, and as it so happens, Windows's dup2() creates inheritable
duplicates.
Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
2007-12-08 05:05:36 +08:00
|
|
|
|
2010-01-16 04:12:17 +08:00
|
|
|
/* this creates non-inheritable handles */
|
|
|
|
if (!CreatePipe(&h[0], &h[1], NULL, 8192)) {
|
|
|
|
errno = err_win_to_posix(GetLastError());
|
Windows: A pipe() replacement whose ends are not inherited to children.
On Unix the idiom to use a pipe is as follows:
pipe(fd);
pid = fork();
if (!pid) {
dup2(fd[1], 1);
close(fd[1]);
close(fd[0]);
...
}
close(fd[1]);
i.e. the child process closes the both pipe ends after duplicating one
to the file descriptors where they are needed.
On Windows, which does not have fork(), we never have an opportunity to
(1) duplicate a pipe end in the child, (2) close unused pipe ends. Instead,
we must use this idiom:
save1 = dup(1);
pipe(fd);
dup2(fd[1], 1);
spawn(...);
dup2(save1, 1);
close(fd[1]);
i.e. save away the descriptor at the destination slot, replace by the pipe
end, spawn process, restore the saved file.
But there is a problem: Notice that the child did not only inherit the
dup2()ed descriptor, but also *both* original pipe ends. Although the one
end that was dup()ed could be closed before the spawn(), we cannot close
the other end - the child inherits it, no matter what.
The solution is to generate non-inheritable pipes. At the first glance,
this looks strange: The purpose of pipes is usually to be inherited to
child processes. But notice that in the course of actions as outlined
above, the pipe descriptor that we want to inherit to the child is
dup2()ed, and as it so happens, Windows's dup2() creates inheritable
duplicates.
Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
2007-12-08 05:05:36 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2016-01-15 21:24:34 +08:00
|
|
|
filedes[0] = _open_osfhandle(HCAST(int, h[0]), O_NOINHERIT);
|
2010-01-16 04:12:17 +08:00
|
|
|
if (filedes[0] < 0) {
|
Windows: A pipe() replacement whose ends are not inherited to children.
On Unix the idiom to use a pipe is as follows:
pipe(fd);
pid = fork();
if (!pid) {
dup2(fd[1], 1);
close(fd[1]);
close(fd[0]);
...
}
close(fd[1]);
i.e. the child process closes the both pipe ends after duplicating one
to the file descriptors where they are needed.
On Windows, which does not have fork(), we never have an opportunity to
(1) duplicate a pipe end in the child, (2) close unused pipe ends. Instead,
we must use this idiom:
save1 = dup(1);
pipe(fd);
dup2(fd[1], 1);
spawn(...);
dup2(save1, 1);
close(fd[1]);
i.e. save away the descriptor at the destination slot, replace by the pipe
end, spawn process, restore the saved file.
But there is a problem: Notice that the child did not only inherit the
dup2()ed descriptor, but also *both* original pipe ends. Although the one
end that was dup()ed could be closed before the spawn(), we cannot close
the other end - the child inherits it, no matter what.
The solution is to generate non-inheritable pipes. At the first glance,
this looks strange: The purpose of pipes is usually to be inherited to
child processes. But notice that in the course of actions as outlined
above, the pipe descriptor that we want to inherit to the child is
dup2()ed, and as it so happens, Windows's dup2() creates inheritable
duplicates.
Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
2007-12-08 05:05:36 +08:00
|
|
|
CloseHandle(h[0]);
|
|
|
|
CloseHandle(h[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-01-15 21:24:34 +08:00
|
|
|
filedes[1] = _open_osfhandle(HCAST(int, h[1]), O_NOINHERIT);
|
2015-08-28 17:43:37 +08:00
|
|
|
if (filedes[1] < 0) {
|
Windows: A pipe() replacement whose ends are not inherited to children.
On Unix the idiom to use a pipe is as follows:
pipe(fd);
pid = fork();
if (!pid) {
dup2(fd[1], 1);
close(fd[1]);
close(fd[0]);
...
}
close(fd[1]);
i.e. the child process closes the both pipe ends after duplicating one
to the file descriptors where they are needed.
On Windows, which does not have fork(), we never have an opportunity to
(1) duplicate a pipe end in the child, (2) close unused pipe ends. Instead,
we must use this idiom:
save1 = dup(1);
pipe(fd);
dup2(fd[1], 1);
spawn(...);
dup2(save1, 1);
close(fd[1]);
i.e. save away the descriptor at the destination slot, replace by the pipe
end, spawn process, restore the saved file.
But there is a problem: Notice that the child did not only inherit the
dup2()ed descriptor, but also *both* original pipe ends. Although the one
end that was dup()ed could be closed before the spawn(), we cannot close
the other end - the child inherits it, no matter what.
The solution is to generate non-inheritable pipes. At the first glance,
this looks strange: The purpose of pipes is usually to be inherited to
child processes. But notice that in the course of actions as outlined
above, the pipe descriptor that we want to inherit to the child is
dup2()ed, and as it so happens, Windows's dup2() creates inheritable
duplicates.
Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
2007-12-08 05:05:36 +08:00
|
|
|
close(filedes[0]);
|
|
|
|
CloseHandle(h[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-12-02 04:24:59 +08:00
|
|
|
struct tm *gmtime_r(const time_t *timep, struct tm *result)
|
|
|
|
{
|
|
|
|
/* gmtime() in MSVCRT.DLL is thread-safe, but not reentrant */
|
|
|
|
memcpy(result, gmtime(timep), sizeof(struct tm));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tm *localtime_r(const time_t *timep, struct tm *result)
|
|
|
|
{
|
|
|
|
/* localtime() in MSVCRT.DLL is thread-safe, but not reentrant */
|
|
|
|
memcpy(result, localtime(timep), sizeof(struct tm));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-03-06 04:51:27 +08:00
|
|
|
char *mingw_getcwd(char *pointer, int len)
|
|
|
|
{
|
|
|
|
int i;
|
2012-03-16 01:21:28 +08:00
|
|
|
wchar_t wpointer[MAX_PATH];
|
|
|
|
if (!_wgetcwd(wpointer, ARRAY_SIZE(wpointer)))
|
|
|
|
return NULL;
|
|
|
|
if (xwcstoutf(pointer, wpointer, len) < 0)
|
|
|
|
return NULL;
|
2008-03-06 04:51:27 +08:00
|
|
|
for (i = 0; pointer[i]; i++)
|
|
|
|
if (pointer[i] == '\\')
|
|
|
|
pointer[i] = '/';
|
2012-03-16 01:21:28 +08:00
|
|
|
return pointer;
|
2008-03-06 04:51:27 +08:00
|
|
|
}
|
|
|
|
|
2007-11-25 05:49:16 +08:00
|
|
|
/*
|
|
|
|
* See http://msdn2.microsoft.com/en-us/library/17w5ykft(vs.71).aspx
|
|
|
|
* (Parsing C++ Command-Line Arguments)
|
|
|
|
*/
|
|
|
|
static const char *quote_arg(const char *arg)
|
|
|
|
{
|
|
|
|
/* count chars to quote */
|
|
|
|
int len = 0, n = 0;
|
|
|
|
int force_quotes = 0;
|
|
|
|
char *q, *d;
|
|
|
|
const char *p = arg;
|
|
|
|
if (!*p) force_quotes = 1;
|
|
|
|
while (*p) {
|
2009-03-25 04:43:02 +08:00
|
|
|
if (isspace(*p) || *p == '*' || *p == '?' || *p == '{' || *p == '\'')
|
2007-11-25 05:49:16 +08:00
|
|
|
force_quotes = 1;
|
|
|
|
else if (*p == '"')
|
|
|
|
n++;
|
|
|
|
else if (*p == '\\') {
|
|
|
|
int count = 0;
|
|
|
|
while (*p == '\\') {
|
|
|
|
count++;
|
|
|
|
p++;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
if (*p == '"')
|
|
|
|
n += count*2 + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
len++;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (!force_quotes && n == 0)
|
|
|
|
return arg;
|
|
|
|
|
|
|
|
/* insert \ where necessary */
|
2016-02-23 06:44:35 +08:00
|
|
|
d = q = xmalloc(st_add3(len, n, 3));
|
2007-11-25 05:49:16 +08:00
|
|
|
*d++ = '"';
|
|
|
|
while (*arg) {
|
|
|
|
if (*arg == '"')
|
|
|
|
*d++ = '\\';
|
|
|
|
else if (*arg == '\\') {
|
|
|
|
int count = 0;
|
|
|
|
while (*arg == '\\') {
|
|
|
|
count++;
|
|
|
|
*d++ = *arg++;
|
|
|
|
}
|
|
|
|
if (*arg == '"') {
|
|
|
|
while (count-- > 0)
|
|
|
|
*d++ = '\\';
|
|
|
|
*d++ = '\\';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*d++ = *arg++;
|
|
|
|
}
|
|
|
|
*d++ = '"';
|
|
|
|
*d++ = 0;
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2007-12-04 19:38:32 +08:00
|
|
|
static const char *parse_interpreter(const char *cmd)
|
|
|
|
{
|
|
|
|
static char buf[100];
|
|
|
|
char *p, *opt;
|
|
|
|
int n, fd;
|
|
|
|
|
|
|
|
/* don't even try a .exe */
|
|
|
|
n = strlen(cmd);
|
|
|
|
if (n >= 4 && !strcasecmp(cmd+n-4, ".exe"))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
fd = open(cmd, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return NULL;
|
|
|
|
n = read(fd, buf, sizeof(buf)-1);
|
|
|
|
close(fd);
|
|
|
|
if (n < 4) /* at least '#!/x' and not error */
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (buf[0] != '#' || buf[1] != '!')
|
|
|
|
return NULL;
|
|
|
|
buf[n] = '\0';
|
2009-05-23 16:04:47 +08:00
|
|
|
p = buf + strcspn(buf, "\r\n");
|
|
|
|
if (!*p)
|
2007-12-04 19:38:32 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
if (!(p = strrchr(buf+2, '/')) && !(p = strrchr(buf+2, '\\')))
|
|
|
|
return NULL;
|
|
|
|
/* strip options */
|
|
|
|
if ((opt = strchr(p+1, ' ')))
|
|
|
|
*opt = '\0';
|
|
|
|
return p+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Splits the PATH into parts.
|
|
|
|
*/
|
|
|
|
static char **get_path_split(void)
|
|
|
|
{
|
2011-06-06 15:06:02 +08:00
|
|
|
char *p, **path, *envpath = mingw_getenv("PATH");
|
2007-12-04 19:38:32 +08:00
|
|
|
int i, n = 0;
|
|
|
|
|
|
|
|
if (!envpath || !*envpath)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
envpath = xstrdup(envpath);
|
|
|
|
p = envpath;
|
|
|
|
while (p) {
|
|
|
|
char *dir = p;
|
|
|
|
p = strchr(p, ';');
|
|
|
|
if (p) *p++ = '\0';
|
|
|
|
if (*dir) { /* not earlier, catches series of ; */
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!n)
|
|
|
|
return NULL;
|
|
|
|
|
2016-02-23 06:44:25 +08:00
|
|
|
ALLOC_ARRAY(path, n + 1);
|
2007-12-04 19:38:32 +08:00
|
|
|
p = envpath;
|
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
if (*p)
|
|
|
|
path[i++] = xstrdup(p);
|
|
|
|
p = p+strlen(p)+1;
|
|
|
|
} while (i < n);
|
|
|
|
path[i] = NULL;
|
|
|
|
|
|
|
|
free(envpath);
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_path_split(char **path)
|
|
|
|
{
|
2009-09-16 16:20:17 +08:00
|
|
|
char **p = path;
|
|
|
|
|
2007-12-04 19:38:32 +08:00
|
|
|
if (!path)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (*p)
|
|
|
|
free(*p++);
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* exe_only means that we only want to detect .exe files, but not scripts
|
|
|
|
* (which do not have an extension)
|
|
|
|
*/
|
|
|
|
static char *lookup_prog(const char *dir, const char *cmd, int isexe, int exe_only)
|
|
|
|
{
|
|
|
|
char path[MAX_PATH];
|
|
|
|
snprintf(path, sizeof(path), "%s/%s.exe", dir, cmd);
|
|
|
|
|
|
|
|
if (!isexe && access(path, F_OK) == 0)
|
|
|
|
return xstrdup(path);
|
|
|
|
path[strlen(path)-4] = '\0';
|
|
|
|
if ((!exe_only || isexe) && access(path, F_OK) == 0)
|
2008-07-18 15:34:42 +08:00
|
|
|
if (!(GetFileAttributes(path) & FILE_ATTRIBUTE_DIRECTORY))
|
|
|
|
return xstrdup(path);
|
2007-12-04 19:38:32 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-08-22 19:12:12 +08:00
|
|
|
* Determines the absolute path of cmd using the split path in path.
|
2007-12-04 19:38:32 +08:00
|
|
|
* If cmd contains a slash or backslash, no lookup is performed.
|
|
|
|
*/
|
|
|
|
static char *path_lookup(const char *cmd, char **path, int exe_only)
|
|
|
|
{
|
|
|
|
char *prog = NULL;
|
|
|
|
int len = strlen(cmd);
|
|
|
|
int isexe = len >= 4 && !strcasecmp(cmd+len-4, ".exe");
|
|
|
|
|
|
|
|
if (strchr(cmd, '/') || strchr(cmd, '\\'))
|
|
|
|
prog = xstrdup(cmd);
|
|
|
|
|
|
|
|
while (!prog && *path)
|
|
|
|
prog = lookup_prog(*path++, cmd, isexe, exe_only);
|
|
|
|
|
|
|
|
return prog;
|
|
|
|
}
|
|
|
|
|
2014-07-17 23:38:02 +08:00
|
|
|
static int do_putenv(char **env, const char *name, int size, int free_old);
|
|
|
|
|
|
|
|
/* used number of elements of environ array, including terminating NULL */
|
|
|
|
static int environ_size = 0;
|
|
|
|
/* allocated size of environ array, in bytes */
|
|
|
|
static int environ_alloc = 0;
|
2014-07-17 23:38:01 +08:00
|
|
|
|
2014-07-17 23:38:00 +08:00
|
|
|
/*
|
2014-07-17 23:38:01 +08:00
|
|
|
* Create environment block suitable for CreateProcess. Merges current
|
|
|
|
* process environment and the supplied environment changes.
|
2014-07-17 23:38:00 +08:00
|
|
|
*/
|
2014-07-17 23:38:01 +08:00
|
|
|
static wchar_t *make_environment_block(char **deltaenv)
|
2007-11-25 05:49:16 +08:00
|
|
|
{
|
2014-07-17 23:38:00 +08:00
|
|
|
wchar_t *wenvblk = NULL;
|
2014-07-17 23:38:02 +08:00
|
|
|
char **tmpenv;
|
|
|
|
int i = 0, size = environ_size, wenvsz = 0, wenvpos = 0;
|
2014-07-17 23:38:00 +08:00
|
|
|
|
2014-07-17 23:38:02 +08:00
|
|
|
while (deltaenv && deltaenv[i])
|
|
|
|
i++;
|
2014-07-17 23:38:00 +08:00
|
|
|
|
2014-07-17 23:38:02 +08:00
|
|
|
/* copy the environment, leaving space for changes */
|
2016-02-23 06:44:25 +08:00
|
|
|
ALLOC_ARRAY(tmpenv, size + i);
|
2014-07-17 23:38:02 +08:00
|
|
|
memcpy(tmpenv, environ, size * sizeof(char*));
|
2014-07-17 23:38:01 +08:00
|
|
|
|
|
|
|
/* merge supplied environment changes into the temporary environment */
|
2014-07-17 23:38:02 +08:00
|
|
|
for (i = 0; deltaenv && deltaenv[i]; i++)
|
|
|
|
size = do_putenv(tmpenv, deltaenv[i], size, 0);
|
2014-07-17 23:38:01 +08:00
|
|
|
|
2014-07-17 23:38:00 +08:00
|
|
|
/* create environment block from temporary environment */
|
2014-07-17 23:38:02 +08:00
|
|
|
for (i = 0; tmpenv[i]; i++) {
|
|
|
|
size = 2 * strlen(tmpenv[i]) + 2; /* +2 for final \0 */
|
2014-07-17 23:38:00 +08:00
|
|
|
ALLOC_GROW(wenvblk, (wenvpos + size) * sizeof(wchar_t), wenvsz);
|
2014-07-17 23:38:02 +08:00
|
|
|
wenvpos += xutftowcs(&wenvblk[wenvpos], tmpenv[i], size) + 1;
|
2014-07-17 23:38:00 +08:00
|
|
|
}
|
|
|
|
/* add final \0 terminator */
|
|
|
|
wenvblk[wenvpos] = 0;
|
|
|
|
free(tmpenv);
|
|
|
|
return wenvblk;
|
2007-11-25 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2010-11-04 09:35:13 +08:00
|
|
|
struct pinfo_t {
|
|
|
|
struct pinfo_t *next;
|
|
|
|
pid_t pid;
|
|
|
|
HANDLE proc;
|
2013-04-28 03:18:55 +08:00
|
|
|
};
|
|
|
|
static struct pinfo_t *pinfo = NULL;
|
2010-11-04 09:35:13 +08:00
|
|
|
CRITICAL_SECTION pinfo_cs;
|
|
|
|
|
2014-07-17 23:38:01 +08:00
|
|
|
static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaenv,
|
2010-04-12 04:40:12 +08:00
|
|
|
const char *dir,
|
Windows: avoid the "dup dance" when spawning a child process
When stdin, stdout, or stderr must be redirected for a child process that
on Windows is spawned using one of the spawn() functions of Microsoft's
C runtime, then there is no choice other than to
1. make a backup copy of fd 0,1,2 with dup
2. dup2 the redirection source fd into 0,1,2
3. spawn
4. dup2 the backup back into 0,1,2
5. close the backup copy and the redirection source
We used this idiom as well -- but we are not using the spawn() functions
anymore!
Instead, we have our own implementation. We had hardcoded that stdin,
stdout, and stderr of the child process were inherited from the parent's
fds 0, 1, and 2. But we can actually specify any fd.
With this patch, the fds to inherit are passed from start_command()'s
WIN32 section to our spawn implementation. This way, we can avoid the
backup copies of the fds.
The backup copies were a bug waiting to surface: The OS handles underlying
the dup()ed fds were inherited by the child process (but were not
associated with a file descriptor in the child). Consequently, the file or
pipe represented by the OS handle remained open even after the backup copy
was closed in the parent process until the child exited.
Since our implementation of pipe() creates non-inheritable OS handles, we
still dup() file descriptors in start_command() because dup() happens to
create inheritable duplicates. (A nice side effect is that the fd cleanup
in start_command is the same for Windows and Unix and remains unchanged.)
Signed-off-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-01-16 04:12:18 +08:00
|
|
|
int prepend_cmd, int fhin, int fhout, int fherr)
|
2007-11-25 05:49:16 +08:00
|
|
|
{
|
2011-01-17 01:27:53 +08:00
|
|
|
STARTUPINFOW si;
|
2007-11-25 05:49:16 +08:00
|
|
|
PROCESS_INFORMATION pi;
|
2014-07-17 23:37:55 +08:00
|
|
|
struct strbuf args;
|
|
|
|
wchar_t wcmd[MAX_PATH], wdir[MAX_PATH], *wargs, *wenvblk = NULL;
|
|
|
|
unsigned flags = CREATE_UNICODE_ENVIRONMENT;
|
2007-11-25 05:49:16 +08:00
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
/* Determine whether or not we are associated to a console */
|
|
|
|
HANDLE cons = CreateFile("CONOUT$", GENERIC_WRITE,
|
|
|
|
FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (cons == INVALID_HANDLE_VALUE) {
|
|
|
|
/* There is no console associated with this process.
|
|
|
|
* Since the child is a console process, Windows
|
|
|
|
* would normally create a console window. But
|
|
|
|
* since we'll be redirecting std streams, we do
|
|
|
|
* not need the console.
|
2008-11-03 01:11:13 +08:00
|
|
|
* It is necessary to use DETACHED_PROCESS
|
|
|
|
* instead of CREATE_NO_WINDOW to make ssh
|
|
|
|
* recognize that it has no console.
|
2007-11-25 05:49:16 +08:00
|
|
|
*/
|
2014-07-17 23:37:55 +08:00
|
|
|
flags |= DETACHED_PROCESS;
|
2007-11-25 05:49:16 +08:00
|
|
|
} else {
|
|
|
|
/* There is already a console. If we specified
|
2008-11-03 01:11:13 +08:00
|
|
|
* DETACHED_PROCESS here, too, Windows would
|
2007-11-25 05:49:16 +08:00
|
|
|
* disassociate the child from the console.
|
2008-11-03 01:11:13 +08:00
|
|
|
* The same is true for CREATE_NO_WINDOW.
|
2007-11-25 05:49:16 +08:00
|
|
|
* Go figure!
|
|
|
|
*/
|
|
|
|
CloseHandle(cons);
|
|
|
|
}
|
|
|
|
memset(&si, 0, sizeof(si));
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
si.dwFlags = STARTF_USESTDHANDLES;
|
Win32: Thread-safe windows console output
Winansi.c has many static variables that are accessed and modified from
the [v][f]printf / fputs functions overridden in the file. This may cause
multi threaded git commands that print to the console to produce corrupted
output or even crash.
Additionally, winansi.c doesn't override all functions that can be used to
print to the console (e.g. fwrite, write, fputc are missing), so that ANSI
escapes don't work properly for some git commands (e.g. git-grep).
Instead of doing ANSI emulation in just a few wrapped functions on top of
the IO API, let's plug into the IO system and take advantage of the thread
safety inherent to the IO system.
Redirect stdout and stderr to a pipe if they point to the console. A
background thread reads from the pipe, handles ANSI escape sequences and
UTF-8 to UTF-16 conversion, then writes to the console.
The pipe-based stdout and stderr replacements must be set to unbuffered, as
MSVCRT doesn't support line buffering and fully buffered streams are
inappropriate for console output.
Due to the byte-oriented pipe, ANSI escape sequences and multi-byte UTF-8
sequences can no longer be expected to arrive in one piece. Replace the
string-based ansi_emulate() with a simple stateful parser (this also fixes
colored diff hunk headers, which were broken as of commit 2efcc977).
Override isatty to return true for the pipes redirecting to the console.
Exec/spawn obtain the original console handle to pass to the next process
via winansi_get_osfhandle().
All other overrides are gone, the default stdio implementations work as
expected with the piped stdout/stderr descriptors.
Global variables are either initialized on startup (single threaded) or
exclusively modified by the background thread. Threads communicate through
the pipe, no further synchronization is necessary.
The background thread is terminated by disonnecting the pipe after flushing
the stdio and pipe buffers. This doesn't work for anonymous pipes (created
via CreatePipe), as DisconnectNamedPipe only works on the read end, which
discards remaining data. Thus we have to setup the pipe manually, with the
write end beeing the server (opened with CreateNamedPipe) and the read end
the client (opened with CreateFile).
Limitations: doesn't track reopened or duped file descriptors, i.e.:
- fdopen(1/2) returns fully buffered streams
- dup(1/2), dup2(1/2) returns normal pipe descriptors (i.e. isatty() =
false, winansi_get_osfhandle won't return the original console handle)
Currently, only the git-format-patch command uses xfdopen(xdup(1)) (see
"realstdout" in builtin/log.c), but works well with these limitations.
Many thanks to Atsushi Nakagawa <atnak@chejz.com> for suggesting and
reviewing the thread-exit-mechanism.
Signed-off-by: Karsten Blees <blees@dcon.de>
Signed-off-by: Stepan Kasal <kasal@ucw.cz>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2012-01-15 05:24:19 +08:00
|
|
|
si.hStdInput = winansi_get_osfhandle(fhin);
|
|
|
|
si.hStdOutput = winansi_get_osfhandle(fhout);
|
|
|
|
si.hStdError = winansi_get_osfhandle(fherr);
|
2007-11-25 05:49:16 +08:00
|
|
|
|
2011-01-17 01:27:53 +08:00
|
|
|
if (xutftowcs_path(wcmd, cmd) < 0)
|
|
|
|
return -1;
|
|
|
|
if (dir && xutftowcs_path(wdir, dir) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2007-11-25 05:49:16 +08:00
|
|
|
/* concatenate argv, quoting args as we go */
|
|
|
|
strbuf_init(&args, 0);
|
|
|
|
if (prepend_cmd) {
|
|
|
|
char *quoted = (char *)quote_arg(cmd);
|
|
|
|
strbuf_addstr(&args, quoted);
|
|
|
|
if (quoted != cmd)
|
|
|
|
free(quoted);
|
|
|
|
}
|
|
|
|
for (; *argv; argv++) {
|
|
|
|
char *quoted = (char *)quote_arg(*argv);
|
|
|
|
if (*args.buf)
|
|
|
|
strbuf_addch(&args, ' ');
|
|
|
|
strbuf_addstr(&args, quoted);
|
|
|
|
if (quoted != *argv)
|
|
|
|
free(quoted);
|
|
|
|
}
|
|
|
|
|
2016-02-23 06:44:35 +08:00
|
|
|
wargs = xmalloc_array(st_add(st_mult(2, args.len), 1), sizeof(wchar_t));
|
2011-01-17 01:27:53 +08:00
|
|
|
xutftowcs(wargs, args.buf, 2 * args.len + 1);
|
|
|
|
strbuf_release(&args);
|
|
|
|
|
2014-07-17 23:38:01 +08:00
|
|
|
wenvblk = make_environment_block(deltaenv);
|
2007-11-25 05:49:16 +08:00
|
|
|
|
|
|
|
memset(&pi, 0, sizeof(pi));
|
2011-01-17 01:27:53 +08:00
|
|
|
ret = CreateProcessW(wcmd, wargs, NULL, NULL, TRUE, flags,
|
2014-07-17 23:37:55 +08:00
|
|
|
wenvblk, dir ? wdir : NULL, &si, &pi);
|
2007-11-25 05:49:16 +08:00
|
|
|
|
2014-07-17 23:37:55 +08:00
|
|
|
free(wenvblk);
|
2011-01-17 01:27:53 +08:00
|
|
|
free(wargs);
|
2007-11-25 05:49:16 +08:00
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
CloseHandle(pi.hThread);
|
2010-11-04 09:35:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The process ID is the human-readable identifier of the process
|
|
|
|
* that we want to present in log and error messages. The handle
|
|
|
|
* is not useful for this purpose. But we cannot close it, either,
|
|
|
|
* because it is not possible to turn a process ID into a process
|
|
|
|
* handle after the process terminated.
|
|
|
|
* Keep the handle in a list for waitpid.
|
|
|
|
*/
|
|
|
|
EnterCriticalSection(&pinfo_cs);
|
|
|
|
{
|
|
|
|
struct pinfo_t *info = xmalloc(sizeof(struct pinfo_t));
|
|
|
|
info->pid = pi.dwProcessId;
|
|
|
|
info->proc = pi.hProcess;
|
|
|
|
info->next = pinfo;
|
|
|
|
pinfo = info;
|
|
|
|
}
|
|
|
|
LeaveCriticalSection(&pinfo_cs);
|
|
|
|
|
|
|
|
return (pid_t)pi.dwProcessId;
|
2007-11-25 05:49:16 +08:00
|
|
|
}
|
|
|
|
|
2011-11-26 04:33:17 +08:00
|
|
|
static pid_t mingw_spawnv(const char *cmd, const char **argv, int prepend_cmd)
|
Windows: avoid the "dup dance" when spawning a child process
When stdin, stdout, or stderr must be redirected for a child process that
on Windows is spawned using one of the spawn() functions of Microsoft's
C runtime, then there is no choice other than to
1. make a backup copy of fd 0,1,2 with dup
2. dup2 the redirection source fd into 0,1,2
3. spawn
4. dup2 the backup back into 0,1,2
5. close the backup copy and the redirection source
We used this idiom as well -- but we are not using the spawn() functions
anymore!
Instead, we have our own implementation. We had hardcoded that stdin,
stdout, and stderr of the child process were inherited from the parent's
fds 0, 1, and 2. But we can actually specify any fd.
With this patch, the fds to inherit are passed from start_command()'s
WIN32 section to our spawn implementation. This way, we can avoid the
backup copies of the fds.
The backup copies were a bug waiting to surface: The OS handles underlying
the dup()ed fds were inherited by the child process (but were not
associated with a file descriptor in the child). Consequently, the file or
pipe represented by the OS handle remained open even after the backup copy
was closed in the parent process until the child exited.
Since our implementation of pipe() creates non-inheritable OS handles, we
still dup() file descriptors in start_command() because dup() happens to
create inheritable duplicates. (A nice side effect is that the fd cleanup
in start_command is the same for Windows and Unix and remains unchanged.)
Signed-off-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-01-16 04:12:18 +08:00
|
|
|
{
|
2014-07-17 23:38:01 +08:00
|
|
|
return mingw_spawnve_fd(cmd, argv, NULL, NULL, prepend_cmd, 0, 1, 2);
|
Windows: avoid the "dup dance" when spawning a child process
When stdin, stdout, or stderr must be redirected for a child process that
on Windows is spawned using one of the spawn() functions of Microsoft's
C runtime, then there is no choice other than to
1. make a backup copy of fd 0,1,2 with dup
2. dup2 the redirection source fd into 0,1,2
3. spawn
4. dup2 the backup back into 0,1,2
5. close the backup copy and the redirection source
We used this idiom as well -- but we are not using the spawn() functions
anymore!
Instead, we have our own implementation. We had hardcoded that stdin,
stdout, and stderr of the child process were inherited from the parent's
fds 0, 1, and 2. But we can actually specify any fd.
With this patch, the fds to inherit are passed from start_command()'s
WIN32 section to our spawn implementation. This way, we can avoid the
backup copies of the fds.
The backup copies were a bug waiting to surface: The OS handles underlying
the dup()ed fds were inherited by the child process (but were not
associated with a file descriptor in the child). Consequently, the file or
pipe represented by the OS handle remained open even after the backup copy
was closed in the parent process until the child exited.
Since our implementation of pipe() creates non-inheritable OS handles, we
still dup() file descriptors in start_command() because dup() happens to
create inheritable duplicates. (A nice side effect is that the fd cleanup
in start_command is the same for Windows and Unix and remains unchanged.)
Signed-off-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-01-16 04:12:18 +08:00
|
|
|
}
|
|
|
|
|
2014-07-17 23:38:01 +08:00
|
|
|
pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **deltaenv,
|
2010-04-12 04:40:12 +08:00
|
|
|
const char *dir,
|
Windows: avoid the "dup dance" when spawning a child process
When stdin, stdout, or stderr must be redirected for a child process that
on Windows is spawned using one of the spawn() functions of Microsoft's
C runtime, then there is no choice other than to
1. make a backup copy of fd 0,1,2 with dup
2. dup2 the redirection source fd into 0,1,2
3. spawn
4. dup2 the backup back into 0,1,2
5. close the backup copy and the redirection source
We used this idiom as well -- but we are not using the spawn() functions
anymore!
Instead, we have our own implementation. We had hardcoded that stdin,
stdout, and stderr of the child process were inherited from the parent's
fds 0, 1, and 2. But we can actually specify any fd.
With this patch, the fds to inherit are passed from start_command()'s
WIN32 section to our spawn implementation. This way, we can avoid the
backup copies of the fds.
The backup copies were a bug waiting to surface: The OS handles underlying
the dup()ed fds were inherited by the child process (but were not
associated with a file descriptor in the child). Consequently, the file or
pipe represented by the OS handle remained open even after the backup copy
was closed in the parent process until the child exited.
Since our implementation of pipe() creates non-inheritable OS handles, we
still dup() file descriptors in start_command() because dup() happens to
create inheritable duplicates. (A nice side effect is that the fd cleanup
in start_command is the same for Windows and Unix and remains unchanged.)
Signed-off-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-01-16 04:12:18 +08:00
|
|
|
int fhin, int fhout, int fherr)
|
2007-11-25 05:49:16 +08:00
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
char **path = get_path_split();
|
|
|
|
char *prog = path_lookup(cmd, path, 0);
|
|
|
|
|
|
|
|
if (!prog) {
|
|
|
|
errno = ENOENT;
|
|
|
|
pid = -1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const char *interpr = parse_interpreter(prog);
|
|
|
|
|
|
|
|
if (interpr) {
|
|
|
|
const char *argv0 = argv[0];
|
|
|
|
char *iprog = path_lookup(interpr, path, 1);
|
|
|
|
argv[0] = prog;
|
|
|
|
if (!iprog) {
|
|
|
|
errno = ENOENT;
|
|
|
|
pid = -1;
|
|
|
|
}
|
|
|
|
else {
|
2014-07-17 23:38:01 +08:00
|
|
|
pid = mingw_spawnve_fd(iprog, argv, deltaenv, dir, 1,
|
Windows: avoid the "dup dance" when spawning a child process
When stdin, stdout, or stderr must be redirected for a child process that
on Windows is spawned using one of the spawn() functions of Microsoft's
C runtime, then there is no choice other than to
1. make a backup copy of fd 0,1,2 with dup
2. dup2 the redirection source fd into 0,1,2
3. spawn
4. dup2 the backup back into 0,1,2
5. close the backup copy and the redirection source
We used this idiom as well -- but we are not using the spawn() functions
anymore!
Instead, we have our own implementation. We had hardcoded that stdin,
stdout, and stderr of the child process were inherited from the parent's
fds 0, 1, and 2. But we can actually specify any fd.
With this patch, the fds to inherit are passed from start_command()'s
WIN32 section to our spawn implementation. This way, we can avoid the
backup copies of the fds.
The backup copies were a bug waiting to surface: The OS handles underlying
the dup()ed fds were inherited by the child process (but were not
associated with a file descriptor in the child). Consequently, the file or
pipe represented by the OS handle remained open even after the backup copy
was closed in the parent process until the child exited.
Since our implementation of pipe() creates non-inheritable OS handles, we
still dup() file descriptors in start_command() because dup() happens to
create inheritable duplicates. (A nice side effect is that the fd cleanup
in start_command is the same for Windows and Unix and remains unchanged.)
Signed-off-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-01-16 04:12:18 +08:00
|
|
|
fhin, fhout, fherr);
|
2007-11-25 05:49:16 +08:00
|
|
|
free(iprog);
|
|
|
|
}
|
|
|
|
argv[0] = argv0;
|
|
|
|
}
|
|
|
|
else
|
2014-07-17 23:38:01 +08:00
|
|
|
pid = mingw_spawnve_fd(prog, argv, deltaenv, dir, 0,
|
Windows: avoid the "dup dance" when spawning a child process
When stdin, stdout, or stderr must be redirected for a child process that
on Windows is spawned using one of the spawn() functions of Microsoft's
C runtime, then there is no choice other than to
1. make a backup copy of fd 0,1,2 with dup
2. dup2 the redirection source fd into 0,1,2
3. spawn
4. dup2 the backup back into 0,1,2
5. close the backup copy and the redirection source
We used this idiom as well -- but we are not using the spawn() functions
anymore!
Instead, we have our own implementation. We had hardcoded that stdin,
stdout, and stderr of the child process were inherited from the parent's
fds 0, 1, and 2. But we can actually specify any fd.
With this patch, the fds to inherit are passed from start_command()'s
WIN32 section to our spawn implementation. This way, we can avoid the
backup copies of the fds.
The backup copies were a bug waiting to surface: The OS handles underlying
the dup()ed fds were inherited by the child process (but were not
associated with a file descriptor in the child). Consequently, the file or
pipe represented by the OS handle remained open even after the backup copy
was closed in the parent process until the child exited.
Since our implementation of pipe() creates non-inheritable OS handles, we
still dup() file descriptors in start_command() because dup() happens to
create inheritable duplicates. (A nice side effect is that the fd cleanup
in start_command is the same for Windows and Unix and remains unchanged.)
Signed-off-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2010-01-16 04:12:18 +08:00
|
|
|
fhin, fhout, fherr);
|
2007-11-25 05:49:16 +08:00
|
|
|
free(prog);
|
|
|
|
}
|
|
|
|
free_path_split(path);
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2011-11-26 04:33:17 +08:00
|
|
|
static int try_shell_exec(const char *cmd, char *const *argv)
|
2007-12-04 19:38:32 +08:00
|
|
|
{
|
|
|
|
const char *interpr = parse_interpreter(cmd);
|
|
|
|
char **path;
|
|
|
|
char *prog;
|
|
|
|
int pid = 0;
|
|
|
|
|
|
|
|
if (!interpr)
|
|
|
|
return 0;
|
|
|
|
path = get_path_split();
|
|
|
|
prog = path_lookup(interpr, path, 1);
|
|
|
|
if (prog) {
|
|
|
|
int argc = 0;
|
|
|
|
const char **argv2;
|
|
|
|
while (argv[argc]) argc++;
|
2016-02-23 06:44:25 +08:00
|
|
|
ALLOC_ARRAY(argv2, argc + 1);
|
2007-11-25 05:49:16 +08:00
|
|
|
argv2[0] = (char *)cmd; /* full path to the script file */
|
|
|
|
memcpy(&argv2[1], &argv[1], sizeof(*argv) * argc);
|
2011-11-26 04:33:17 +08:00
|
|
|
pid = mingw_spawnv(prog, argv2, 1);
|
2007-12-04 19:38:32 +08:00
|
|
|
if (pid >= 0) {
|
|
|
|
int status;
|
|
|
|
if (waitpid(pid, &status, 0) < 0)
|
|
|
|
status = 255;
|
|
|
|
exit(status);
|
|
|
|
}
|
|
|
|
pid = 1; /* indicate that we tried but failed */
|
|
|
|
free(prog);
|
|
|
|
free(argv2);
|
|
|
|
}
|
|
|
|
free_path_split(path);
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2011-11-26 04:33:17 +08:00
|
|
|
int mingw_execv(const char *cmd, char *const *argv)
|
2007-12-04 19:38:32 +08:00
|
|
|
{
|
|
|
|
/* check if git_command is a shell script */
|
2011-11-26 04:33:17 +08:00
|
|
|
if (!try_shell_exec(cmd, argv)) {
|
2007-12-04 19:38:32 +08:00
|
|
|
int pid, status;
|
|
|
|
|
2011-11-26 04:33:17 +08:00
|
|
|
pid = mingw_spawnv(cmd, (const char **)argv, 0);
|
2007-12-04 19:38:32 +08:00
|
|
|
if (pid < 0)
|
2012-05-29 10:21:39 +08:00
|
|
|
return -1;
|
2007-12-04 19:38:32 +08:00
|
|
|
if (waitpid(pid, &status, 0) < 0)
|
|
|
|
status = 255;
|
|
|
|
exit(status);
|
|
|
|
}
|
2012-05-29 10:21:39 +08:00
|
|
|
return -1;
|
2007-12-04 19:38:32 +08:00
|
|
|
}
|
|
|
|
|
2012-04-06 01:48:46 +08:00
|
|
|
int mingw_execvp(const char *cmd, char *const *argv)
|
2007-12-04 19:38:32 +08:00
|
|
|
{
|
|
|
|
char **path = get_path_split();
|
|
|
|
char *prog = path_lookup(cmd, path, 0);
|
|
|
|
|
|
|
|
if (prog) {
|
2011-11-26 04:33:17 +08:00
|
|
|
mingw_execv(prog, argv);
|
2007-12-04 19:38:32 +08:00
|
|
|
free(prog);
|
|
|
|
} else
|
|
|
|
errno = ENOENT;
|
|
|
|
|
|
|
|
free_path_split(path);
|
2012-04-06 01:48:46 +08:00
|
|
|
return -1;
|
2007-12-04 19:38:32 +08:00
|
|
|
}
|
|
|
|
|
2010-11-04 09:35:15 +08:00
|
|
|
int mingw_kill(pid_t pid, int sig)
|
|
|
|
{
|
|
|
|
if (pid > 0 && sig == SIGTERM) {
|
|
|
|
HANDLE h = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
|
|
|
|
|
|
|
|
if (TerminateProcess(h, -1)) {
|
|
|
|
CloseHandle(h);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = err_win_to_posix(GetLastError());
|
|
|
|
CloseHandle(h);
|
|
|
|
return -1;
|
2013-08-08 19:05:38 +08:00
|
|
|
} else if (pid > 0 && sig == 0) {
|
|
|
|
HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
|
|
|
|
if (h) {
|
|
|
|
CloseHandle(h);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-11-04 09:35:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-07-17 23:38:04 +08:00
|
|
|
/*
|
|
|
|
* Compare environment entries by key (i.e. stopping at '=' or '\0').
|
|
|
|
*/
|
|
|
|
static int compareenv(const void *v1, const void *v2)
|
|
|
|
{
|
|
|
|
const char *e1 = *(const char**)v1;
|
|
|
|
const char *e2 = *(const char**)v2;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int c1 = *e1++;
|
|
|
|
int c2 = *e2++;
|
|
|
|
c1 = (c1 == '=') ? 0 : tolower(c1);
|
|
|
|
c2 = (c2 == '=') ? 0 : tolower(c2);
|
|
|
|
if (c1 > c2)
|
|
|
|
return 1;
|
|
|
|
if (c1 < c2)
|
|
|
|
return -1;
|
|
|
|
if (c1 == 0)
|
|
|
|
return 0;
|
|
|
|
}
|
2007-12-08 05:08:59 +08:00
|
|
|
}
|
|
|
|
|
2014-07-17 23:38:04 +08:00
|
|
|
static int bsearchenv(char **env, const char *name, size_t size)
|
2007-12-08 05:08:59 +08:00
|
|
|
{
|
2014-07-17 23:38:04 +08:00
|
|
|
unsigned low = 0, high = size;
|
|
|
|
while (low < high) {
|
|
|
|
unsigned mid = low + ((high - low) >> 1);
|
|
|
|
int cmp = compareenv(&env[mid], &name);
|
|
|
|
if (cmp < 0)
|
|
|
|
low = mid + 1;
|
|
|
|
else if (cmp > 0)
|
|
|
|
high = mid;
|
|
|
|
else
|
|
|
|
return mid;
|
2007-12-08 05:08:59 +08:00
|
|
|
}
|
2014-07-17 23:38:04 +08:00
|
|
|
return ~low; /* not found, return 1's complement of insert position */
|
2007-12-08 05:08:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If name contains '=', then sets the variable, otherwise it unsets it
|
2014-07-17 23:38:02 +08:00
|
|
|
* Size includes the terminating NULL. Env must have room for size + 1 entries
|
|
|
|
* (in case of insert). Returns the new size. Optionally frees removed entries.
|
2007-12-08 05:08:59 +08:00
|
|
|
*/
|
2014-07-17 23:38:02 +08:00
|
|
|
static int do_putenv(char **env, const char *name, int size, int free_old)
|
2007-12-08 05:08:59 +08:00
|
|
|
{
|
2014-07-17 23:38:04 +08:00
|
|
|
int i = bsearchenv(env, name, size - 1);
|
2007-12-08 05:08:59 +08:00
|
|
|
|
2014-07-17 23:38:04 +08:00
|
|
|
/* optionally free removed / replaced entry */
|
|
|
|
if (i >= 0 && free_old)
|
2007-12-08 05:08:59 +08:00
|
|
|
free(env[i]);
|
|
|
|
|
2014-07-17 23:38:04 +08:00
|
|
|
if (strchr(name, '=')) {
|
|
|
|
/* if new value ('key=value') is specified, insert or replace entry */
|
|
|
|
if (i < 0) {
|
|
|
|
i = ~i;
|
|
|
|
memmove(&env[i + 1], &env[i], (size - i) * sizeof(char*));
|
2014-07-17 23:38:02 +08:00
|
|
|
size++;
|
2007-12-08 05:08:59 +08:00
|
|
|
}
|
2014-07-17 23:38:04 +08:00
|
|
|
env[i] = (char*) name;
|
|
|
|
} else if (i >= 0) {
|
|
|
|
/* otherwise ('key') remove existing entry */
|
|
|
|
size--;
|
|
|
|
memmove(&env[i], &env[i + 1], (size - i) * sizeof(char*));
|
2007-12-08 05:08:59 +08:00
|
|
|
}
|
2014-07-17 23:38:02 +08:00
|
|
|
return size;
|
2009-09-12 01:40:08 +08:00
|
|
|
}
|
|
|
|
|
2014-07-17 23:38:05 +08:00
|
|
|
char *mingw_getenv(const char *name)
|
2011-06-06 15:08:13 +08:00
|
|
|
{
|
2014-07-17 23:38:04 +08:00
|
|
|
char *value;
|
|
|
|
int pos = bsearchenv(environ, name, environ_size - 1);
|
|
|
|
if (pos < 0)
|
|
|
|
return NULL;
|
|
|
|
value = strchr(environ[pos], '=');
|
|
|
|
return value ? &value[1] : NULL;
|
2011-06-06 15:08:13 +08:00
|
|
|
}
|
|
|
|
|
2014-07-17 23:37:56 +08:00
|
|
|
int mingw_putenv(const char *namevalue)
|
2011-06-06 15:06:02 +08:00
|
|
|
{
|
2014-07-17 23:38:02 +08:00
|
|
|
ALLOC_GROW(environ, (environ_size + 1) * sizeof(char*), environ_alloc);
|
|
|
|
environ_size = do_putenv(environ, namevalue, environ_size, 1);
|
2014-07-17 23:37:56 +08:00
|
|
|
return 0;
|
2011-06-06 15:06:02 +08:00
|
|
|
}
|
|
|
|
|
2009-11-24 06:55:50 +08:00
|
|
|
/*
|
|
|
|
* Note, this isn't a complete replacement for getaddrinfo. It assumes
|
2011-04-13 23:39:40 +08:00
|
|
|
* that service contains a numerical port, or that it is null. It
|
2009-11-24 06:55:50 +08:00
|
|
|
* does a simple search using gethostbyname, and returns one IPv4 host
|
|
|
|
* if one was found.
|
|
|
|
*/
|
|
|
|
static int WSAAPI getaddrinfo_stub(const char *node, const char *service,
|
|
|
|
const struct addrinfo *hints,
|
|
|
|
struct addrinfo **res)
|
|
|
|
{
|
2010-11-04 09:35:18 +08:00
|
|
|
struct hostent *h = NULL;
|
2009-11-24 06:55:50 +08:00
|
|
|
struct addrinfo *ai;
|
|
|
|
struct sockaddr_in *sin;
|
|
|
|
|
2010-11-04 09:35:18 +08:00
|
|
|
if (node) {
|
|
|
|
h = gethostbyname(node);
|
|
|
|
if (!h)
|
|
|
|
return WSAGetLastError();
|
|
|
|
}
|
2009-11-24 06:55:50 +08:00
|
|
|
|
|
|
|
ai = xmalloc(sizeof(struct addrinfo));
|
|
|
|
*res = ai;
|
|
|
|
ai->ai_flags = 0;
|
|
|
|
ai->ai_family = AF_INET;
|
2010-11-04 09:35:18 +08:00
|
|
|
ai->ai_socktype = hints ? hints->ai_socktype : 0;
|
|
|
|
switch (ai->ai_socktype) {
|
2009-11-24 06:55:50 +08:00
|
|
|
case SOCK_STREAM:
|
|
|
|
ai->ai_protocol = IPPROTO_TCP;
|
|
|
|
break;
|
|
|
|
case SOCK_DGRAM:
|
|
|
|
ai->ai_protocol = IPPROTO_UDP;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ai->ai_protocol = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ai->ai_addrlen = sizeof(struct sockaddr_in);
|
2010-11-04 09:35:18 +08:00
|
|
|
if (hints && (hints->ai_flags & AI_CANONNAME))
|
2011-10-07 02:22:22 +08:00
|
|
|
ai->ai_canonname = h ? xstrdup(h->h_name) : NULL;
|
2010-11-04 09:35:18 +08:00
|
|
|
else
|
|
|
|
ai->ai_canonname = NULL;
|
2009-11-24 06:55:50 +08:00
|
|
|
|
2014-07-19 21:56:26 +08:00
|
|
|
sin = xcalloc(1, ai->ai_addrlen);
|
2009-11-24 06:55:50 +08:00
|
|
|
sin->sin_family = AF_INET;
|
2010-11-04 09:35:18 +08:00
|
|
|
/* Note: getaddrinfo is supposed to allow service to be a string,
|
|
|
|
* which should be looked up using getservbyname. This is
|
|
|
|
* currently not implemented */
|
2009-11-24 06:55:50 +08:00
|
|
|
if (service)
|
|
|
|
sin->sin_port = htons(atoi(service));
|
2010-11-04 09:35:18 +08:00
|
|
|
if (h)
|
|
|
|
sin->sin_addr = *(struct in_addr *)h->h_addr;
|
|
|
|
else if (hints && (hints->ai_flags & AI_PASSIVE))
|
|
|
|
sin->sin_addr.s_addr = INADDR_ANY;
|
|
|
|
else
|
|
|
|
sin->sin_addr.s_addr = INADDR_LOOPBACK;
|
2009-11-24 06:55:50 +08:00
|
|
|
ai->ai_addr = (struct sockaddr *)sin;
|
2013-04-28 03:18:55 +08:00
|
|
|
ai->ai_next = NULL;
|
2009-11-24 06:55:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void WSAAPI freeaddrinfo_stub(struct addrinfo *res)
|
|
|
|
{
|
|
|
|
free(res->ai_canonname);
|
|
|
|
free(res->ai_addr);
|
|
|
|
free(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int WSAAPI getnameinfo_stub(const struct sockaddr *sa, socklen_t salen,
|
|
|
|
char *host, DWORD hostlen,
|
|
|
|
char *serv, DWORD servlen, int flags)
|
|
|
|
{
|
|
|
|
const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
|
|
|
|
if (sa->sa_family != AF_INET)
|
|
|
|
return EAI_FAMILY;
|
|
|
|
if (!host && !serv)
|
|
|
|
return EAI_NONAME;
|
|
|
|
|
|
|
|
if (host && hostlen > 0) {
|
|
|
|
struct hostent *ent = NULL;
|
|
|
|
if (!(flags & NI_NUMERICHOST))
|
|
|
|
ent = gethostbyaddr((const char *)&sin->sin_addr,
|
|
|
|
sizeof(sin->sin_addr), AF_INET);
|
|
|
|
|
|
|
|
if (ent)
|
|
|
|
snprintf(host, hostlen, "%s", ent->h_name);
|
|
|
|
else if (flags & NI_NAMEREQD)
|
|
|
|
return EAI_NONAME;
|
|
|
|
else
|
|
|
|
snprintf(host, hostlen, "%s", inet_ntoa(sin->sin_addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (serv && servlen > 0) {
|
|
|
|
struct servent *ent = NULL;
|
|
|
|
if (!(flags & NI_NUMERICSERV))
|
|
|
|
ent = getservbyport(sin->sin_port,
|
|
|
|
flags & NI_DGRAM ? "udp" : "tcp");
|
|
|
|
|
|
|
|
if (ent)
|
|
|
|
snprintf(serv, servlen, "%s", ent->s_name);
|
|
|
|
else
|
|
|
|
snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HMODULE ipv6_dll = NULL;
|
|
|
|
static void (WSAAPI *ipv6_freeaddrinfo)(struct addrinfo *res);
|
|
|
|
static int (WSAAPI *ipv6_getaddrinfo)(const char *node, const char *service,
|
|
|
|
const struct addrinfo *hints,
|
|
|
|
struct addrinfo **res);
|
|
|
|
static int (WSAAPI *ipv6_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
|
|
|
|
char *host, DWORD hostlen,
|
|
|
|
char *serv, DWORD servlen, int flags);
|
|
|
|
/*
|
|
|
|
* gai_strerror is an inline function in the ws2tcpip.h header, so we
|
|
|
|
* don't need to try to load that one dynamically.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void socket_cleanup(void)
|
|
|
|
{
|
|
|
|
WSACleanup();
|
|
|
|
if (ipv6_dll)
|
|
|
|
FreeLibrary(ipv6_dll);
|
|
|
|
ipv6_dll = NULL;
|
|
|
|
ipv6_freeaddrinfo = freeaddrinfo_stub;
|
|
|
|
ipv6_getaddrinfo = getaddrinfo_stub;
|
|
|
|
ipv6_getnameinfo = getnameinfo_stub;
|
|
|
|
}
|
|
|
|
|
2009-11-24 06:55:12 +08:00
|
|
|
static void ensure_socket_initialization(void)
|
2007-12-26 20:51:18 +08:00
|
|
|
{
|
|
|
|
WSADATA wsa;
|
2009-11-24 06:55:12 +08:00
|
|
|
static int initialized = 0;
|
2009-11-24 06:55:50 +08:00
|
|
|
const char *libraries[] = { "ws2_32.dll", "wship6.dll", NULL };
|
|
|
|
const char **name;
|
2009-11-24 06:55:12 +08:00
|
|
|
|
|
|
|
if (initialized)
|
|
|
|
return;
|
2007-12-26 20:51:18 +08:00
|
|
|
|
|
|
|
if (WSAStartup(MAKEWORD(2,2), &wsa))
|
|
|
|
die("unable to initialize winsock subsystem, error %d",
|
|
|
|
WSAGetLastError());
|
2009-11-24 06:55:50 +08:00
|
|
|
|
|
|
|
for (name = libraries; *name; name++) {
|
|
|
|
ipv6_dll = LoadLibrary(*name);
|
|
|
|
if (!ipv6_dll)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ipv6_freeaddrinfo = (void (WSAAPI *)(struct addrinfo *))
|
|
|
|
GetProcAddress(ipv6_dll, "freeaddrinfo");
|
|
|
|
ipv6_getaddrinfo = (int (WSAAPI *)(const char *, const char *,
|
|
|
|
const struct addrinfo *,
|
|
|
|
struct addrinfo **))
|
|
|
|
GetProcAddress(ipv6_dll, "getaddrinfo");
|
|
|
|
ipv6_getnameinfo = (int (WSAAPI *)(const struct sockaddr *,
|
|
|
|
socklen_t, char *, DWORD,
|
|
|
|
char *, DWORD, int))
|
|
|
|
GetProcAddress(ipv6_dll, "getnameinfo");
|
|
|
|
if (!ipv6_freeaddrinfo || !ipv6_getaddrinfo || !ipv6_getnameinfo) {
|
|
|
|
FreeLibrary(ipv6_dll);
|
|
|
|
ipv6_dll = NULL;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!ipv6_freeaddrinfo || !ipv6_getaddrinfo || !ipv6_getnameinfo) {
|
|
|
|
ipv6_freeaddrinfo = freeaddrinfo_stub;
|
|
|
|
ipv6_getaddrinfo = getaddrinfo_stub;
|
|
|
|
ipv6_getnameinfo = getnameinfo_stub;
|
|
|
|
}
|
|
|
|
|
|
|
|
atexit(socket_cleanup);
|
2009-11-24 06:55:12 +08:00
|
|
|
initialized = 1;
|
|
|
|
}
|
|
|
|
|
2011-10-15 22:05:20 +08:00
|
|
|
#undef gethostname
|
|
|
|
int mingw_gethostname(char *name, int namelen)
|
|
|
|
{
|
|
|
|
ensure_socket_initialization();
|
|
|
|
return gethostname(name, namelen);
|
|
|
|
}
|
|
|
|
|
2009-11-24 06:55:12 +08:00
|
|
|
#undef gethostbyname
|
|
|
|
struct hostent *mingw_gethostbyname(const char *host)
|
|
|
|
{
|
|
|
|
ensure_socket_initialization();
|
2007-12-26 20:51:18 +08:00
|
|
|
return gethostbyname(host);
|
|
|
|
}
|
|
|
|
|
2009-11-24 06:55:50 +08:00
|
|
|
void mingw_freeaddrinfo(struct addrinfo *res)
|
|
|
|
{
|
|
|
|
ipv6_freeaddrinfo(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
int mingw_getaddrinfo(const char *node, const char *service,
|
|
|
|
const struct addrinfo *hints, struct addrinfo **res)
|
|
|
|
{
|
|
|
|
ensure_socket_initialization();
|
|
|
|
return ipv6_getaddrinfo(node, service, hints, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
int mingw_getnameinfo(const struct sockaddr *sa, socklen_t salen,
|
|
|
|
char *host, DWORD hostlen, char *serv, DWORD servlen,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
ensure_socket_initialization();
|
|
|
|
return ipv6_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
|
|
|
|
}
|
|
|
|
|
2007-12-26 20:51:18 +08:00
|
|
|
int mingw_socket(int domain, int type, int protocol)
|
|
|
|
{
|
|
|
|
int sockfd;
|
2010-11-04 09:35:09 +08:00
|
|
|
SOCKET s;
|
|
|
|
|
|
|
|
ensure_socket_initialization();
|
|
|
|
s = WSASocket(domain, type, protocol, NULL, 0, 0);
|
2007-12-26 20:51:18 +08:00
|
|
|
if (s == INVALID_SOCKET) {
|
|
|
|
/*
|
|
|
|
* WSAGetLastError() values are regular BSD error codes
|
|
|
|
* biased by WSABASEERR.
|
|
|
|
* However, strerror() does not know about networking
|
|
|
|
* specific errors, which are values beginning at 38 or so.
|
|
|
|
* Therefore, we choose to leave the biased error code
|
|
|
|
* in errno so that _if_ someone looks up the code somewhere,
|
|
|
|
* then it is at least the number that are usually listed.
|
|
|
|
*/
|
|
|
|
errno = WSAGetLastError();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* convert into a file descriptor */
|
|
|
|
if ((sockfd = _open_osfhandle(s, O_RDWR|O_BINARY)) < 0) {
|
|
|
|
closesocket(s);
|
|
|
|
return error("unable to make a socket file descriptor: %s",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
return sockfd;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef connect
|
|
|
|
int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz)
|
|
|
|
{
|
|
|
|
SOCKET s = (SOCKET)_get_osfhandle(sockfd);
|
|
|
|
return connect(s, sa, sz);
|
|
|
|
}
|
|
|
|
|
2010-11-04 09:35:09 +08:00
|
|
|
#undef bind
|
|
|
|
int mingw_bind(int sockfd, struct sockaddr *sa, size_t sz)
|
|
|
|
{
|
|
|
|
SOCKET s = (SOCKET)_get_osfhandle(sockfd);
|
|
|
|
return bind(s, sa, sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef setsockopt
|
|
|
|
int mingw_setsockopt(int sockfd, int lvl, int optname, void *optval, int optlen)
|
|
|
|
{
|
|
|
|
SOCKET s = (SOCKET)_get_osfhandle(sockfd);
|
|
|
|
return setsockopt(s, lvl, optname, (const char*)optval, optlen);
|
|
|
|
}
|
|
|
|
|
2011-05-19 04:24:37 +08:00
|
|
|
#undef shutdown
|
|
|
|
int mingw_shutdown(int sockfd, int how)
|
|
|
|
{
|
|
|
|
SOCKET s = (SOCKET)_get_osfhandle(sockfd);
|
|
|
|
return shutdown(s, how);
|
|
|
|
}
|
|
|
|
|
2010-11-04 09:35:09 +08:00
|
|
|
#undef listen
|
|
|
|
int mingw_listen(int sockfd, int backlog)
|
|
|
|
{
|
|
|
|
SOCKET s = (SOCKET)_get_osfhandle(sockfd);
|
|
|
|
return listen(s, backlog);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef accept
|
|
|
|
int mingw_accept(int sockfd1, struct sockaddr *sa, socklen_t *sz)
|
|
|
|
{
|
|
|
|
int sockfd2;
|
|
|
|
|
|
|
|
SOCKET s1 = (SOCKET)_get_osfhandle(sockfd1);
|
|
|
|
SOCKET s2 = accept(s1, sa, sz);
|
|
|
|
|
|
|
|
/* convert into a file descriptor */
|
|
|
|
if ((sockfd2 = _open_osfhandle(s2, O_RDWR|O_BINARY)) < 0) {
|
|
|
|
int err = errno;
|
|
|
|
closesocket(s2);
|
|
|
|
return error("unable to make a socket file descriptor: %s",
|
|
|
|
strerror(err));
|
|
|
|
}
|
|
|
|
return sockfd2;
|
|
|
|
}
|
|
|
|
|
2007-12-08 05:19:40 +08:00
|
|
|
#undef rename
|
|
|
|
int mingw_rename(const char *pold, const char *pnew)
|
|
|
|
{
|
2009-04-03 14:49:59 +08:00
|
|
|
DWORD attrs, gle;
|
|
|
|
int tries = 0;
|
2012-03-16 01:21:28 +08:00
|
|
|
wchar_t wpold[MAX_PATH], wpnew[MAX_PATH];
|
|
|
|
if (xutftowcs_path(wpold, pold) < 0 || xutftowcs_path(wpnew, pnew) < 0)
|
|
|
|
return -1;
|
2008-11-20 00:25:27 +08:00
|
|
|
|
2007-12-08 05:19:40 +08:00
|
|
|
/*
|
|
|
|
* Try native rename() first to get errno right.
|
|
|
|
* It is based on MoveFile(), which cannot overwrite existing files.
|
|
|
|
*/
|
2012-03-16 01:21:28 +08:00
|
|
|
if (!_wrename(wpold, wpnew))
|
2007-12-08 05:19:40 +08:00
|
|
|
return 0;
|
|
|
|
if (errno != EEXIST)
|
|
|
|
return -1;
|
2009-04-03 14:49:59 +08:00
|
|
|
repeat:
|
2012-03-16 01:21:28 +08:00
|
|
|
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
|
2007-12-08 05:19:40 +08:00
|
|
|
return 0;
|
|
|
|
/* TODO: translate more errors */
|
2009-04-03 14:49:59 +08:00
|
|
|
gle = GetLastError();
|
|
|
|
if (gle == ERROR_ACCESS_DENIED &&
|
2012-03-16 01:21:28 +08:00
|
|
|
(attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
|
2008-11-20 00:25:27 +08:00
|
|
|
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
|
2016-01-26 22:34:47 +08:00
|
|
|
DWORD attrsold = GetFileAttributesW(wpold);
|
|
|
|
if (attrsold == INVALID_FILE_ATTRIBUTES ||
|
|
|
|
!(attrsold & FILE_ATTRIBUTE_DIRECTORY))
|
|
|
|
errno = EISDIR;
|
|
|
|
else if (!_wrmdir(wpnew))
|
|
|
|
goto repeat;
|
2007-12-08 05:19:40 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2008-11-20 00:25:27 +08:00
|
|
|
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
|
2012-03-16 01:21:28 +08:00
|
|
|
SetFileAttributesW(wpnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
|
|
|
|
if (MoveFileExW(wpold, wpnew, MOVEFILE_REPLACE_EXISTING))
|
2008-11-20 00:25:27 +08:00
|
|
|
return 0;
|
2009-04-03 14:49:59 +08:00
|
|
|
gle = GetLastError();
|
2008-11-20 00:25:27 +08:00
|
|
|
/* revert file attributes on failure */
|
2012-03-16 01:21:28 +08:00
|
|
|
SetFileAttributesW(wpnew, attrs);
|
2008-11-20 00:25:27 +08:00
|
|
|
}
|
2007-12-08 05:19:40 +08:00
|
|
|
}
|
2009-04-03 14:49:59 +08:00
|
|
|
if (tries < ARRAY_SIZE(delay) && gle == ERROR_ACCESS_DENIED) {
|
|
|
|
/*
|
|
|
|
* We assume that some other process had the source or
|
|
|
|
* destination file open at the wrong moment and retry.
|
|
|
|
* In order to give the other process a higher chance to
|
|
|
|
* complete its operation, we give up our time slice now.
|
|
|
|
* If we have to retry again, we do sleep a bit.
|
|
|
|
*/
|
|
|
|
Sleep(delay[tries]);
|
|
|
|
tries++;
|
|
|
|
goto repeat;
|
|
|
|
}
|
2011-02-08 04:51:21 +08:00
|
|
|
if (gle == ERROR_ACCESS_DENIED &&
|
|
|
|
ask_yes_no_if_possible("Rename from '%s' to '%s' failed. "
|
|
|
|
"Should I try again?", pold, pnew))
|
|
|
|
goto repeat;
|
|
|
|
|
2007-12-08 05:19:40 +08:00
|
|
|
errno = EACCES;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-11-08 04:10:31 +08:00
|
|
|
/*
|
|
|
|
* Note that this doesn't return the actual pagesize, but
|
|
|
|
* the allocation granularity. If future Windows specific git code
|
|
|
|
* needs the real getpagesize function, we need to find another solution.
|
|
|
|
*/
|
|
|
|
int mingw_getpagesize(void)
|
|
|
|
{
|
|
|
|
SYSTEM_INFO si;
|
|
|
|
GetSystemInfo(&si);
|
|
|
|
return si.dwAllocationGranularity;
|
|
|
|
}
|
|
|
|
|
2007-12-02 04:24:59 +08:00
|
|
|
struct passwd *getpwuid(int uid)
|
|
|
|
{
|
2007-12-02 05:09:17 +08:00
|
|
|
static char user_name[100];
|
2007-12-02 04:24:59 +08:00
|
|
|
static struct passwd p;
|
2007-12-02 05:09:17 +08:00
|
|
|
|
|
|
|
DWORD len = sizeof(user_name);
|
|
|
|
if (!GetUserName(user_name, &len))
|
|
|
|
return NULL;
|
|
|
|
p.pw_name = user_name;
|
|
|
|
p.pw_gecos = "unknown";
|
|
|
|
p.pw_dir = NULL;
|
2007-12-02 04:24:59 +08:00
|
|
|
return &p;
|
|
|
|
}
|
|
|
|
|
2007-11-13 17:14:45 +08:00
|
|
|
static HANDLE timer_event;
|
|
|
|
static HANDLE timer_thread;
|
|
|
|
static int timer_interval;
|
|
|
|
static int one_shot;
|
2012-12-04 16:10:38 +08:00
|
|
|
static sig_handler_t timer_fn = SIG_DFL, sigint_fn = SIG_DFL;
|
2007-11-13 17:14:45 +08:00
|
|
|
|
|
|
|
/* The timer works like this:
|
|
|
|
* The thread, ticktack(), is a trivial routine that most of the time
|
|
|
|
* only waits to receive the signal to terminate. The main thread tells
|
|
|
|
* the thread to terminate by setting the timer_event to the signalled
|
|
|
|
* state.
|
|
|
|
* But ticktack() interrupts the wait state after the timer's interval
|
|
|
|
* length to call the signal handler.
|
|
|
|
*/
|
|
|
|
|
2009-09-16 16:20:21 +08:00
|
|
|
static unsigned __stdcall ticktack(void *dummy)
|
2007-11-13 17:14:45 +08:00
|
|
|
{
|
|
|
|
while (WaitForSingleObject(timer_event, timer_interval) == WAIT_TIMEOUT) {
|
2012-12-04 16:10:38 +08:00
|
|
|
mingw_raise(SIGALRM);
|
2007-11-13 17:14:45 +08:00
|
|
|
if (one_shot)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int start_timer_thread(void)
|
|
|
|
{
|
|
|
|
timer_event = CreateEvent(NULL, FALSE, FALSE, NULL);
|
|
|
|
if (timer_event) {
|
|
|
|
timer_thread = (HANDLE) _beginthreadex(NULL, 0, ticktack, NULL, 0, NULL);
|
|
|
|
if (!timer_thread )
|
|
|
|
return errno = ENOMEM,
|
|
|
|
error("cannot start timer thread");
|
|
|
|
} else
|
|
|
|
return errno = ENOMEM,
|
|
|
|
error("cannot allocate resources for timer");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stop_timer_thread(void)
|
|
|
|
{
|
|
|
|
if (timer_event)
|
|
|
|
SetEvent(timer_event); /* tell thread to terminate */
|
|
|
|
if (timer_thread) {
|
|
|
|
int rc = WaitForSingleObject(timer_thread, 1000);
|
|
|
|
if (rc == WAIT_TIMEOUT)
|
|
|
|
error("timer thread did not terminate timely");
|
|
|
|
else if (rc != WAIT_OBJECT_0)
|
|
|
|
error("waiting for timer thread failed: %lu",
|
|
|
|
GetLastError());
|
|
|
|
CloseHandle(timer_thread);
|
|
|
|
}
|
|
|
|
if (timer_event)
|
|
|
|
CloseHandle(timer_event);
|
|
|
|
timer_event = NULL;
|
|
|
|
timer_thread = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int is_timeval_eq(const struct timeval *i1, const struct timeval *i2)
|
|
|
|
{
|
|
|
|
return i1->tv_sec == i2->tv_sec && i1->tv_usec == i2->tv_usec;
|
|
|
|
}
|
|
|
|
|
2007-12-02 04:24:59 +08:00
|
|
|
int setitimer(int type, struct itimerval *in, struct itimerval *out)
|
|
|
|
{
|
2007-11-13 17:14:45 +08:00
|
|
|
static const struct timeval zero;
|
|
|
|
static int atexit_done;
|
|
|
|
|
|
|
|
if (out != NULL)
|
|
|
|
return errno = EINVAL,
|
|
|
|
error("setitimer param 3 != NULL not implemented");
|
|
|
|
if (!is_timeval_eq(&in->it_interval, &zero) &&
|
|
|
|
!is_timeval_eq(&in->it_interval, &in->it_value))
|
|
|
|
return errno = EINVAL,
|
|
|
|
error("setitimer: it_interval must be zero or eq it_value");
|
|
|
|
|
|
|
|
if (timer_thread)
|
|
|
|
stop_timer_thread();
|
|
|
|
|
|
|
|
if (is_timeval_eq(&in->it_value, &zero) &&
|
|
|
|
is_timeval_eq(&in->it_interval, &zero))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
timer_interval = in->it_value.tv_sec * 1000 + in->it_value.tv_usec / 1000;
|
|
|
|
one_shot = is_timeval_eq(&in->it_interval, &zero);
|
|
|
|
if (!atexit_done) {
|
|
|
|
atexit(stop_timer_thread);
|
|
|
|
atexit_done = 1;
|
|
|
|
}
|
|
|
|
return start_timer_thread();
|
2007-12-02 04:24:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int sigaction(int sig, struct sigaction *in, struct sigaction *out)
|
|
|
|
{
|
2007-11-13 17:14:45 +08:00
|
|
|
if (sig != SIGALRM)
|
|
|
|
return errno = EINVAL,
|
|
|
|
error("sigaction only implemented for SIGALRM");
|
|
|
|
if (out != NULL)
|
|
|
|
return errno = EINVAL,
|
|
|
|
error("sigaction: param 3 != NULL not implemented");
|
|
|
|
|
|
|
|
timer_fn = in->sa_handler;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef signal
|
|
|
|
sig_handler_t mingw_signal(int sig, sig_handler_t handler)
|
|
|
|
{
|
2013-06-10 13:48:17 +08:00
|
|
|
sig_handler_t old;
|
2012-12-04 16:10:38 +08:00
|
|
|
|
|
|
|
switch (sig) {
|
|
|
|
case SIGALRM:
|
2013-06-10 13:48:17 +08:00
|
|
|
old = timer_fn;
|
2012-12-04 16:10:38 +08:00
|
|
|
timer_fn = handler;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGINT:
|
2013-06-10 13:48:17 +08:00
|
|
|
old = sigint_fn;
|
2012-12-04 16:10:38 +08:00
|
|
|
sigint_fn = handler;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2007-11-13 17:14:45 +08:00
|
|
|
return signal(sig, handler);
|
2012-12-04 16:10:38 +08:00
|
|
|
}
|
|
|
|
|
2007-11-13 17:14:45 +08:00
|
|
|
return old;
|
2007-12-02 04:24:59 +08:00
|
|
|
}
|
2008-07-14 04:31:20 +08:00
|
|
|
|
2012-12-04 16:10:38 +08:00
|
|
|
#undef raise
|
|
|
|
int mingw_raise(int sig)
|
|
|
|
{
|
|
|
|
switch (sig) {
|
|
|
|
case SIGALRM:
|
|
|
|
if (timer_fn == SIG_DFL) {
|
|
|
|
if (isatty(STDERR_FILENO))
|
|
|
|
fputs("Alarm clock\n", stderr);
|
|
|
|
exit(128 + SIGALRM);
|
|
|
|
} else if (timer_fn != SIG_IGN)
|
|
|
|
timer_fn(SIGALRM);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SIGINT:
|
|
|
|
if (sigint_fn == SIG_DFL)
|
|
|
|
exit(128 + SIGINT);
|
|
|
|
else if (sigint_fn != SIG_IGN)
|
|
|
|
sigint_fn(SIGINT);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return raise(sig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-14 04:31:20 +08:00
|
|
|
static const char *make_backslash_path(const char *path)
|
|
|
|
{
|
|
|
|
static char buf[PATH_MAX + 1];
|
|
|
|
char *c;
|
|
|
|
|
|
|
|
if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
|
|
|
|
die("Too long path: %.*s", 60, path);
|
|
|
|
|
|
|
|
for (c = buf; *c; c++) {
|
|
|
|
if (*c == '/')
|
|
|
|
*c = '\\';
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mingw_open_html(const char *unixpath)
|
|
|
|
{
|
|
|
|
const char *htmlpath = make_backslash_path(unixpath);
|
2010-01-16 04:12:16 +08:00
|
|
|
typedef HINSTANCE (WINAPI *T)(HWND, const char *,
|
|
|
|
const char *, const char *, const char *, INT);
|
|
|
|
T ShellExecute;
|
|
|
|
HMODULE shell32;
|
2010-03-19 05:45:19 +08:00
|
|
|
int r;
|
2010-01-16 04:12:16 +08:00
|
|
|
|
|
|
|
shell32 = LoadLibrary("shell32.dll");
|
|
|
|
if (!shell32)
|
|
|
|
die("cannot load shell32.dll");
|
|
|
|
ShellExecute = (T)GetProcAddress(shell32, "ShellExecuteA");
|
|
|
|
if (!ShellExecute)
|
|
|
|
die("cannot run browser");
|
|
|
|
|
2008-07-14 04:31:20 +08:00
|
|
|
printf("Launching default browser to display HTML ...\n");
|
2016-01-15 21:24:34 +08:00
|
|
|
r = HCAST(int, ShellExecute(NULL, "open", htmlpath,
|
|
|
|
NULL, "\\", SW_SHOWNORMAL));
|
2010-01-16 04:12:16 +08:00
|
|
|
FreeLibrary(shell32);
|
2010-03-19 05:45:19 +08:00
|
|
|
/* see the MSDN documentation referring to the result codes here */
|
|
|
|
if (r <= 32) {
|
|
|
|
die("failed to launch browser for %.*s", MAX_PATH, unixpath);
|
|
|
|
}
|
2008-07-14 04:31:20 +08:00
|
|
|
}
|
2009-01-24 22:04:39 +08:00
|
|
|
|
|
|
|
int link(const char *oldpath, const char *newpath)
|
|
|
|
{
|
2012-03-16 01:21:28 +08:00
|
|
|
typedef BOOL (WINAPI *T)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES);
|
2009-01-24 22:04:39 +08:00
|
|
|
static T create_hard_link = NULL;
|
2012-03-16 01:21:28 +08:00
|
|
|
wchar_t woldpath[MAX_PATH], wnewpath[MAX_PATH];
|
|
|
|
if (xutftowcs_path(woldpath, oldpath) < 0 ||
|
|
|
|
xutftowcs_path(wnewpath, newpath) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2009-01-24 22:04:39 +08:00
|
|
|
if (!create_hard_link) {
|
|
|
|
create_hard_link = (T) GetProcAddress(
|
2012-03-16 01:21:28 +08:00
|
|
|
GetModuleHandle("kernel32.dll"), "CreateHardLinkW");
|
2009-01-24 22:04:39 +08:00
|
|
|
if (!create_hard_link)
|
|
|
|
create_hard_link = (T)-1;
|
|
|
|
}
|
|
|
|
if (create_hard_link == (T)-1) {
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
2012-03-16 01:21:28 +08:00
|
|
|
if (!create_hard_link(wnewpath, woldpath, NULL)) {
|
2009-01-24 22:04:39 +08:00
|
|
|
errno = err_win_to_posix(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2009-05-23 16:04:50 +08:00
|
|
|
|
2011-12-09 03:39:57 +08:00
|
|
|
pid_t waitpid(pid_t pid, int *status, int options)
|
2010-11-04 09:35:13 +08:00
|
|
|
{
|
|
|
|
HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,
|
|
|
|
FALSE, pid);
|
|
|
|
if (!h) {
|
|
|
|
errno = ECHILD;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-11-04 09:35:14 +08:00
|
|
|
if (pid > 0 && options & WNOHANG) {
|
|
|
|
if (WAIT_OBJECT_0 != WaitForSingleObject(h, 0)) {
|
|
|
|
CloseHandle(h);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
options &= ~WNOHANG;
|
|
|
|
}
|
|
|
|
|
2010-11-04 09:35:13 +08:00
|
|
|
if (options == 0) {
|
|
|
|
struct pinfo_t **ppinfo;
|
|
|
|
if (WaitForSingleObject(h, INFINITE) != WAIT_OBJECT_0) {
|
|
|
|
CloseHandle(h);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status)
|
|
|
|
GetExitCodeProcess(h, (LPDWORD)status);
|
|
|
|
|
|
|
|
EnterCriticalSection(&pinfo_cs);
|
|
|
|
|
|
|
|
ppinfo = &pinfo;
|
|
|
|
while (*ppinfo) {
|
|
|
|
struct pinfo_t *info = *ppinfo;
|
|
|
|
if (info->pid == pid) {
|
|
|
|
CloseHandle(info->proc);
|
|
|
|
*ppinfo = info->next;
|
|
|
|
free(info);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ppinfo = &info->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
LeaveCriticalSection(&pinfo_cs);
|
|
|
|
|
|
|
|
CloseHandle(h);
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
CloseHandle(h);
|
|
|
|
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-07-13 22:17:43 +08:00
|
|
|
|
2016-01-26 05:47:56 +08:00
|
|
|
int mingw_skip_dos_drive_prefix(char **path)
|
|
|
|
{
|
|
|
|
int ret = has_dos_drive_prefix(*path);
|
|
|
|
*path += ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-07-13 22:17:43 +08:00
|
|
|
int mingw_offset_1st_component(const char *path)
|
|
|
|
{
|
2016-01-12 15:57:22 +08:00
|
|
|
char *pos = (char *)path;
|
2010-07-13 22:17:43 +08:00
|
|
|
|
|
|
|
/* unc paths */
|
2016-01-12 15:57:22 +08:00
|
|
|
if (!skip_dos_drive_prefix(&pos) &&
|
|
|
|
is_dir_sep(pos[0]) && is_dir_sep(pos[1])) {
|
2010-07-13 22:17:43 +08:00
|
|
|
/* skip server name */
|
2016-01-12 15:57:22 +08:00
|
|
|
pos = strpbrk(pos + 2, "\\/");
|
2010-07-13 22:17:43 +08:00
|
|
|
if (!pos)
|
|
|
|
return 0; /* Error: malformed unc path */
|
|
|
|
|
|
|
|
do {
|
|
|
|
pos++;
|
|
|
|
} while (*pos && !is_dir_sep(*pos));
|
|
|
|
}
|
|
|
|
|
2016-01-12 15:57:22 +08:00
|
|
|
return pos + is_dir_sep(*pos) - path;
|
2010-07-13 22:17:43 +08:00
|
|
|
}
|
2011-01-08 02:47:23 +08:00
|
|
|
|
2011-11-26 04:05:06 +08:00
|
|
|
int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)
|
|
|
|
{
|
|
|
|
int upos = 0, wpos = 0;
|
|
|
|
const unsigned char *utf = (const unsigned char*) utfs;
|
|
|
|
if (!utf || !wcs || wcslen < 1) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* reserve space for \0 */
|
|
|
|
wcslen--;
|
|
|
|
if (utflen < 0)
|
|
|
|
utflen = INT_MAX;
|
|
|
|
|
|
|
|
while (upos < utflen) {
|
|
|
|
int c = utf[upos++] & 0xff;
|
|
|
|
if (utflen == INT_MAX && c == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (wpos >= wcslen) {
|
|
|
|
wcs[wpos] = 0;
|
|
|
|
errno = ERANGE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c < 0x80) {
|
|
|
|
/* ASCII */
|
|
|
|
wcs[wpos++] = c;
|
|
|
|
} else if (c >= 0xc2 && c < 0xe0 && upos < utflen &&
|
|
|
|
(utf[upos] & 0xc0) == 0x80) {
|
|
|
|
/* 2-byte utf-8 */
|
|
|
|
c = ((c & 0x1f) << 6);
|
|
|
|
c |= (utf[upos++] & 0x3f);
|
|
|
|
wcs[wpos++] = c;
|
|
|
|
} else if (c >= 0xe0 && c < 0xf0 && upos + 1 < utflen &&
|
|
|
|
!(c == 0xe0 && utf[upos] < 0xa0) && /* over-long encoding */
|
|
|
|
(utf[upos] & 0xc0) == 0x80 &&
|
|
|
|
(utf[upos + 1] & 0xc0) == 0x80) {
|
|
|
|
/* 3-byte utf-8 */
|
|
|
|
c = ((c & 0x0f) << 12);
|
|
|
|
c |= ((utf[upos++] & 0x3f) << 6);
|
|
|
|
c |= (utf[upos++] & 0x3f);
|
|
|
|
wcs[wpos++] = c;
|
|
|
|
} else if (c >= 0xf0 && c < 0xf5 && upos + 2 < utflen &&
|
|
|
|
wpos + 1 < wcslen &&
|
|
|
|
!(c == 0xf0 && utf[upos] < 0x90) && /* over-long encoding */
|
|
|
|
!(c == 0xf4 && utf[upos] >= 0x90) && /* > \u10ffff */
|
|
|
|
(utf[upos] & 0xc0) == 0x80 &&
|
|
|
|
(utf[upos + 1] & 0xc0) == 0x80 &&
|
|
|
|
(utf[upos + 2] & 0xc0) == 0x80) {
|
|
|
|
/* 4-byte utf-8: convert to \ud8xx \udcxx surrogate pair */
|
|
|
|
c = ((c & 0x07) << 18);
|
|
|
|
c |= ((utf[upos++] & 0x3f) << 12);
|
|
|
|
c |= ((utf[upos++] & 0x3f) << 6);
|
|
|
|
c |= (utf[upos++] & 0x3f);
|
|
|
|
c -= 0x10000;
|
|
|
|
wcs[wpos++] = 0xd800 | (c >> 10);
|
|
|
|
wcs[wpos++] = 0xdc00 | (c & 0x3ff);
|
|
|
|
} else if (c >= 0xa0) {
|
|
|
|
/* invalid utf-8 byte, printable unicode char: convert 1:1 */
|
|
|
|
wcs[wpos++] = c;
|
|
|
|
} else {
|
|
|
|
/* invalid utf-8 byte, non-printable unicode: convert to hex */
|
|
|
|
static const char *hex = "0123456789abcdef";
|
|
|
|
wcs[wpos++] = hex[c >> 4];
|
|
|
|
if (wpos < wcslen)
|
|
|
|
wcs[wpos++] = hex[c & 0x0f];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wcs[wpos] = 0;
|
|
|
|
return wpos;
|
|
|
|
}
|
|
|
|
|
|
|
|
int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
|
|
|
|
{
|
|
|
|
if (!wcs || !utf || utflen < 1) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
utflen = WideCharToMultiByte(CP_UTF8, 0, wcs, -1, utf, utflen, NULL, NULL);
|
|
|
|
if (utflen)
|
|
|
|
return utflen - 1;
|
|
|
|
errno = ERANGE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-01-26 22:34:38 +08:00
|
|
|
static void setup_windows_environment()
|
|
|
|
{
|
2016-01-26 22:34:43 +08:00
|
|
|
char *tmp = getenv("TMPDIR");
|
|
|
|
|
2016-01-26 22:34:38 +08:00
|
|
|
/* on Windows it is TMP and TEMP */
|
2016-01-26 22:34:43 +08:00
|
|
|
if (!tmp) {
|
|
|
|
if (!(tmp = getenv("TMP")))
|
2016-01-26 22:34:38 +08:00
|
|
|
tmp = getenv("TEMP");
|
2016-01-26 22:34:43 +08:00
|
|
|
if (tmp) {
|
2016-01-26 22:34:38 +08:00
|
|
|
setenv("TMPDIR", tmp, 1);
|
2016-01-26 22:34:43 +08:00
|
|
|
tmp = getenv("TMPDIR");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp) {
|
|
|
|
/*
|
|
|
|
* Convert all dir separators to forward slashes,
|
|
|
|
* to help shell commands called from the Git
|
|
|
|
* executable (by not mistaking the dir separators
|
|
|
|
* for escape characters).
|
|
|
|
*/
|
|
|
|
for (; *tmp; tmp++)
|
|
|
|
if (*tmp == '\\')
|
|
|
|
*tmp = '/';
|
2016-01-26 22:34:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* simulate TERM to enable auto-color (see color.c) */
|
|
|
|
if (!getenv("TERM"))
|
|
|
|
setenv("TERM", "cygwin", 1);
|
|
|
|
}
|
|
|
|
|
2011-01-08 02:52:20 +08:00
|
|
|
/*
|
|
|
|
* Disable MSVCRT command line wildcard expansion (__getmainargs called from
|
|
|
|
* mingw startup code, see init.c in mingw runtime).
|
|
|
|
*/
|
|
|
|
int _CRT_glob = 0;
|
|
|
|
|
2011-01-17 01:28:27 +08:00
|
|
|
typedef struct {
|
|
|
|
int newmode;
|
|
|
|
} _startupinfo;
|
|
|
|
|
|
|
|
extern int __wgetmainargs(int *argc, wchar_t ***argv, wchar_t ***env, int glob,
|
|
|
|
_startupinfo *si);
|
|
|
|
|
|
|
|
static NORETURN void die_startup()
|
|
|
|
{
|
|
|
|
fputs("fatal: not enough memory for initialization", stderr);
|
|
|
|
exit(128);
|
|
|
|
}
|
|
|
|
|
2014-07-17 23:38:03 +08:00
|
|
|
static void *malloc_startup(size_t size)
|
|
|
|
{
|
|
|
|
void *result = malloc(size);
|
|
|
|
if (!result)
|
|
|
|
die_startup();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *wcstoutfdup_startup(char *buffer, const wchar_t *wcs, size_t len)
|
|
|
|
{
|
|
|
|
len = xwcstoutf(buffer, wcs, len) + 1;
|
|
|
|
return memcpy(malloc_startup(len), buffer, len);
|
|
|
|
}
|
|
|
|
|
2011-01-08 02:47:23 +08:00
|
|
|
void mingw_startup()
|
|
|
|
{
|
2014-07-17 23:38:03 +08:00
|
|
|
int i, maxlen, argc;
|
2011-01-17 01:28:27 +08:00
|
|
|
char *buffer;
|
|
|
|
wchar_t **wenv, **wargv;
|
|
|
|
_startupinfo si;
|
|
|
|
|
|
|
|
/* get wide char arguments and environment */
|
|
|
|
si.newmode = 0;
|
|
|
|
if (__wgetmainargs(&argc, &wargv, &wenv, _CRT_glob, &si) < 0)
|
|
|
|
die_startup();
|
|
|
|
|
|
|
|
/* determine size of argv and environ conversion buffer */
|
|
|
|
maxlen = wcslen(_wpgmptr);
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
maxlen = max(maxlen, wcslen(wargv[i]));
|
2014-07-17 23:37:56 +08:00
|
|
|
for (i = 0; wenv[i]; i++)
|
|
|
|
maxlen = max(maxlen, wcslen(wenv[i]));
|
|
|
|
|
2014-07-17 23:38:03 +08:00
|
|
|
/*
|
|
|
|
* nedmalloc can't free CRT memory, allocate resizable environment
|
|
|
|
* list. Note that xmalloc / xmemdupz etc. call getenv, so we cannot
|
|
|
|
* use it while initializing the environment itself.
|
|
|
|
*/
|
2014-07-17 23:38:02 +08:00
|
|
|
environ_size = i + 1;
|
2014-07-17 23:38:03 +08:00
|
|
|
environ_alloc = alloc_nr(environ_size * sizeof(char*));
|
|
|
|
environ = malloc_startup(environ_alloc);
|
2011-01-17 01:28:27 +08:00
|
|
|
|
|
|
|
/* allocate buffer (wchar_t encodes to max 3 UTF-8 bytes) */
|
|
|
|
maxlen = 3 * maxlen + 1;
|
2014-07-17 23:38:03 +08:00
|
|
|
buffer = malloc_startup(maxlen);
|
2011-01-17 01:28:27 +08:00
|
|
|
|
|
|
|
/* convert command line arguments and environment to UTF-8 */
|
2014-07-17 23:38:03 +08:00
|
|
|
__argv[0] = wcstoutfdup_startup(buffer, _wpgmptr, maxlen);
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
__argv[i] = wcstoutfdup_startup(buffer, wargv[i], maxlen);
|
|
|
|
for (i = 0; wenv[i]; i++)
|
|
|
|
environ[i] = wcstoutfdup_startup(buffer, wenv[i], maxlen);
|
2014-07-17 23:38:02 +08:00
|
|
|
environ[i] = NULL;
|
2011-01-17 01:28:27 +08:00
|
|
|
free(buffer);
|
2011-01-08 02:47:23 +08:00
|
|
|
|
2014-07-17 23:38:04 +08:00
|
|
|
/* sort environment for O(log n) getenv / putenv */
|
|
|
|
qsort(environ, i, sizeof(char*), compareenv);
|
|
|
|
|
2014-07-17 23:38:05 +08:00
|
|
|
/* fix Windows specific environment settings */
|
2016-01-26 22:34:38 +08:00
|
|
|
setup_windows_environment();
|
2014-07-17 23:38:06 +08:00
|
|
|
|
2011-01-08 02:47:23 +08:00
|
|
|
/* initialize critical section for waitpid pinfo_t list */
|
|
|
|
InitializeCriticalSection(&pinfo_cs);
|
|
|
|
|
|
|
|
/* set up default file mode and file modes for stdin/out/err */
|
|
|
|
_fmode = _O_BINARY;
|
|
|
|
_setmode(_fileno(stdin), _O_BINARY);
|
|
|
|
_setmode(_fileno(stdout), _O_BINARY);
|
|
|
|
_setmode(_fileno(stderr), _O_BINARY);
|
Win32: Thread-safe windows console output
Winansi.c has many static variables that are accessed and modified from
the [v][f]printf / fputs functions overridden in the file. This may cause
multi threaded git commands that print to the console to produce corrupted
output or even crash.
Additionally, winansi.c doesn't override all functions that can be used to
print to the console (e.g. fwrite, write, fputc are missing), so that ANSI
escapes don't work properly for some git commands (e.g. git-grep).
Instead of doing ANSI emulation in just a few wrapped functions on top of
the IO API, let's plug into the IO system and take advantage of the thread
safety inherent to the IO system.
Redirect stdout and stderr to a pipe if they point to the console. A
background thread reads from the pipe, handles ANSI escape sequences and
UTF-8 to UTF-16 conversion, then writes to the console.
The pipe-based stdout and stderr replacements must be set to unbuffered, as
MSVCRT doesn't support line buffering and fully buffered streams are
inappropriate for console output.
Due to the byte-oriented pipe, ANSI escape sequences and multi-byte UTF-8
sequences can no longer be expected to arrive in one piece. Replace the
string-based ansi_emulate() with a simple stateful parser (this also fixes
colored diff hunk headers, which were broken as of commit 2efcc977).
Override isatty to return true for the pipes redirecting to the console.
Exec/spawn obtain the original console handle to pass to the next process
via winansi_get_osfhandle().
All other overrides are gone, the default stdio implementations work as
expected with the piped stdout/stderr descriptors.
Global variables are either initialized on startup (single threaded) or
exclusively modified by the background thread. Threads communicate through
the pipe, no further synchronization is necessary.
The background thread is terminated by disonnecting the pipe after flushing
the stdio and pipe buffers. This doesn't work for anonymous pipes (created
via CreatePipe), as DisconnectNamedPipe only works on the read end, which
discards remaining data. Thus we have to setup the pipe manually, with the
write end beeing the server (opened with CreateNamedPipe) and the read end
the client (opened with CreateFile).
Limitations: doesn't track reopened or duped file descriptors, i.e.:
- fdopen(1/2) returns fully buffered streams
- dup(1/2), dup2(1/2) returns normal pipe descriptors (i.e. isatty() =
false, winansi_get_osfhandle won't return the original console handle)
Currently, only the git-format-patch command uses xfdopen(xdup(1)) (see
"realstdout" in builtin/log.c), but works well with these limitations.
Many thanks to Atsushi Nakagawa <atnak@chejz.com> for suggesting and
reviewing the thread-exit-mechanism.
Signed-off-by: Karsten Blees <blees@dcon.de>
Signed-off-by: Stepan Kasal <kasal@ucw.cz>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2012-01-15 05:24:19 +08:00
|
|
|
|
|
|
|
/* initialize Unicode console */
|
|
|
|
winansi_init();
|
2011-01-08 02:47:23 +08:00
|
|
|
}
|
2015-03-08 18:12:45 +08:00
|
|
|
|
|
|
|
int uname(struct utsname *buf)
|
|
|
|
{
|
2015-10-23 14:02:52 +08:00
|
|
|
unsigned v = (unsigned)GetVersion();
|
2015-03-08 18:12:45 +08:00
|
|
|
memset(buf, 0, sizeof(*buf));
|
2015-09-25 05:06:08 +08:00
|
|
|
xsnprintf(buf->sysname, sizeof(buf->sysname), "Windows");
|
|
|
|
xsnprintf(buf->release, sizeof(buf->release),
|
|
|
|
"%u.%u", v & 0xff, (v >> 8) & 0xff);
|
2015-03-08 18:12:45 +08:00
|
|
|
/* assuming NT variants only.. */
|
2015-09-25 05:06:08 +08:00
|
|
|
xsnprintf(buf->version, sizeof(buf->version),
|
|
|
|
"%u", (v >> 16) & 0x7fff);
|
2015-03-08 18:12:45 +08:00
|
|
|
return 0;
|
|
|
|
}
|