2007-12-01 21:24:59 +01:00
|
|
|
#include "../git-compat-util.h"
|
2008-09-27 10:43:01 +02:00
|
|
|
#include "win32.h"
|
2009-06-01 08:41:45 +02:00
|
|
|
#include <conio.h>
|
2007-11-24 22:49:16 +01:00
|
|
|
#include "../strbuf.h"
|
2007-12-01 21:24:59 +01:00
|
|
|
|
2010-01-15 21:12:20 +01:00
|
|
|
int err_win_to_posix(DWORD winerr)
|
2009-01-24 15:04:39 +01: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;
|
|
|
|
}
|
|
|
|
|
2007-11-15 22:22:47 +01:00
|
|
|
#undef open
|
|
|
|
int mingw_open (const char *filename, int oflags, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
unsigned mode;
|
2009-09-16 10:20:17 +02:00
|
|
|
int fd;
|
|
|
|
|
2007-11-15 22:22:47 +01:00
|
|
|
va_start(args, oflags);
|
|
|
|
mode = va_arg(args, int);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (!strcmp(filename, "/dev/null"))
|
|
|
|
filename = "nul";
|
2009-09-16 10:20:17 +02:00
|
|
|
|
|
|
|
fd = open(filename, oflags, mode);
|
|
|
|
|
2007-11-15 22:22:47 +01:00
|
|
|
if (fd < 0 && (oflags & O_CREAT) && errno == EACCES) {
|
|
|
|
DWORD attrs = GetFileAttributes(filename);
|
|
|
|
if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY))
|
|
|
|
errno = EISDIR;
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2010-01-15 21:12:21 +01: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-03 20:40:26 +02:00
|
|
|
{
|
|
|
|
long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime;
|
2010-01-15 21:12:21 +01: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-03 20:40:26 +02: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.
|
|
|
|
*/
|
|
|
|
static int do_lstat(const char *file_name, struct stat *buf)
|
|
|
|
{
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fdata;
|
|
|
|
|
2008-09-27 10:43:01 +02:00
|
|
|
if (!(errno = get_file_attr(file_name, &fdata))) {
|
2007-09-03 20:40:26 +02:00
|
|
|
buf->st_ino = 0;
|
|
|
|
buf->st_gid = 0;
|
|
|
|
buf->st_uid = 0;
|
2008-08-18 22:01:06 +02:00
|
|
|
buf->st_nlink = 1;
|
2008-09-27 10:43:01 +02:00
|
|
|
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes);
|
2009-03-05 17:05:12 +01:00
|
|
|
buf->st_size = fdata.nFileSizeLow |
|
|
|
|
(((off_t)fdata.nFileSizeHigh)<<32);
|
2008-09-27 10:39:45 +02:00
|
|
|
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
|
2007-09-03 20:40:26 +02: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;
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
*/
|
2008-08-18 22:01:06 +02:00
|
|
|
int mingw_lstat(const char *file_name, struct stat *buf)
|
2007-09-03 20:40:26 +02:00
|
|
|
{
|
|
|
|
int namelen;
|
|
|
|
static char alt_name[PATH_MAX];
|
|
|
|
|
|
|
|
if (!do_lstat(file_name, buf))
|
|
|
|
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;
|
|
|
|
return do_lstat(alt_name, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef fstat
|
2008-08-18 22:01:06 +02:00
|
|
|
int mingw_fstat(int fd, struct stat *buf)
|
2007-09-03 20:40:26 +02: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-18 22:01:06 +02:00
|
|
|
if (GetFileType(fh) != FILE_TYPE_DISK)
|
2009-03-05 17:05:12 +01:00
|
|
|
return _fstati64(fd, buf);
|
2007-09-03 20:40:26 +02:00
|
|
|
|
|
|
|
if (GetFileInformationByHandle(fh, &fdata)) {
|
|
|
|
buf->st_ino = 0;
|
|
|
|
buf->st_gid = 0;
|
|
|
|
buf->st_uid = 0;
|
2008-08-18 22:01:06 +02:00
|
|
|
buf->st_nlink = 1;
|
2008-09-27 10:43:01 +02:00
|
|
|
buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes);
|
2009-03-05 17:05:12 +01:00
|
|
|
buf->st_size = fdata.nFileSizeLow |
|
|
|
|
(((off_t)fdata.nFileSizeHigh)<<32);
|
2008-09-27 10:39:45 +02:00
|
|
|
buf->st_dev = buf->st_rdev = 0; /* not used by Git */
|
2007-09-03 20:40:26 +02: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 13:05:00 +02: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;
|
|
|
|
|
|
|
|
/* must have write permission */
|
2010-03-30 09:46:23 +02:00
|
|
|
DWORD attrs = GetFileAttributes(file_name);
|
|
|
|
if (attrs != INVALID_FILE_ATTRIBUTES &&
|
|
|
|
(attrs & FILE_ATTRIBUTE_READONLY)) {
|
|
|
|
/* ignore errors here; open() will report them */
|
|
|
|
SetFileAttributes(file_name, attrs & ~FILE_ATTRIBUTE_READONLY);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fh = open(file_name, O_RDWR | O_BINARY)) < 0) {
|
|
|
|
rc = -1;
|
|
|
|
goto revert_attrs;
|
|
|
|
}
|
2007-09-07 13:05:00 +02:00
|
|
|
|
|
|
|
time_t_to_filetime(times->modtime, &mft);
|
|
|
|
time_t_to_filetime(times->actime, &aft);
|
|
|
|
if (!SetFileTime((HANDLE)_get_osfhandle(fh), NULL, &aft, &mft)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
rc = -1;
|
|
|
|
} else
|
|
|
|
rc = 0;
|
|
|
|
close(fh);
|
2010-03-30 09:46:23 +02:00
|
|
|
|
|
|
|
revert_attrs:
|
|
|
|
if (attrs != INVALID_FILE_ATTRIBUTES &&
|
|
|
|
(attrs & FILE_ATTRIBUTE_READONLY)) {
|
|
|
|
/* ignore errors again */
|
|
|
|
SetFileAttributes(file_name, attrs);
|
|
|
|
}
|
2007-09-07 13:05:00 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2007-12-01 21:24:59 +01:00
|
|
|
unsigned int sleep (unsigned int seconds)
|
|
|
|
{
|
|
|
|
Sleep(seconds*1000);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-15 21:12:21 +01: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-01 21:51:20 +01:00
|
|
|
return 0;
|
2007-12-01 21:24:59 +01: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-07 22:05:36 +01:00
|
|
|
int pipe(int filedes[2])
|
|
|
|
{
|
2010-01-15 21:12:17 +01: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-07 22:05:36 +01:00
|
|
|
|
2010-01-15 21:12:17 +01: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-07 22:05:36 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-01-15 21:12:17 +01:00
|
|
|
filedes[0] = _open_osfhandle((int)h[0], O_NOINHERIT);
|
|
|
|
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-07 22:05:36 +01:00
|
|
|
CloseHandle(h[0]);
|
|
|
|
CloseHandle(h[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-01-15 21:12:17 +01:00
|
|
|
filedes[1] = _open_osfhandle((int)h[1], O_NOINHERIT);
|
|
|
|
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-07 22:05:36 +01:00
|
|
|
close(filedes[0]);
|
|
|
|
CloseHandle(h[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-12-01 21:24:59 +01:00
|
|
|
int poll(struct pollfd *ufds, unsigned int nfds, int timeout)
|
|
|
|
{
|
2007-12-01 22:00:56 +01:00
|
|
|
int i, pending;
|
|
|
|
|
2008-09-23 08:52:16 +02:00
|
|
|
if (timeout >= 0) {
|
|
|
|
if (nfds == 0) {
|
|
|
|
Sleep(timeout);
|
|
|
|
return 0;
|
|
|
|
}
|
2007-12-01 22:00:56 +01:00
|
|
|
return errno = EINVAL, error("poll timeout not supported");
|
2008-09-23 08:52:16 +02:00
|
|
|
}
|
2007-12-01 22:00:56 +01:00
|
|
|
|
|
|
|
/* When there is only one fd to wait for, then we pretend that
|
|
|
|
* input is available and let the actual wait happen when the
|
|
|
|
* caller invokes read().
|
|
|
|
*/
|
|
|
|
if (nfds == 1) {
|
|
|
|
if (!(ufds[0].events & POLLIN))
|
|
|
|
return errno = EINVAL, error("POLLIN not set");
|
|
|
|
ufds[0].revents = POLLIN;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
repeat:
|
|
|
|
pending = 0;
|
|
|
|
for (i = 0; i < nfds; i++) {
|
|
|
|
DWORD avail = 0;
|
|
|
|
HANDLE h = (HANDLE) _get_osfhandle(ufds[i].fd);
|
|
|
|
if (h == INVALID_HANDLE_VALUE)
|
|
|
|
return -1; /* errno was set */
|
|
|
|
|
|
|
|
if (!(ufds[i].events & POLLIN))
|
|
|
|
return errno = EINVAL, error("POLLIN not set");
|
|
|
|
|
|
|
|
/* this emulation works only for pipes */
|
|
|
|
if (!PeekNamedPipe(h, NULL, 0, NULL, &avail, NULL)) {
|
|
|
|
int err = GetLastError();
|
|
|
|
if (err == ERROR_BROKEN_PIPE) {
|
|
|
|
ufds[i].revents = POLLHUP;
|
|
|
|
pending++;
|
|
|
|
} else {
|
|
|
|
errno = EINVAL;
|
|
|
|
return error("PeekNamedPipe failed,"
|
|
|
|
" GetLastError: %u", err);
|
|
|
|
}
|
|
|
|
} else if (avail) {
|
|
|
|
ufds[i].revents = POLLIN;
|
|
|
|
pending++;
|
|
|
|
} else
|
|
|
|
ufds[i].revents = 0;
|
|
|
|
}
|
|
|
|
if (!pending) {
|
|
|
|
/* The only times that we spin here is when the process
|
|
|
|
* that is connected through the pipes is waiting for
|
|
|
|
* its own input data to become available. But since
|
|
|
|
* the process (pack-objects) is itself CPU intensive,
|
|
|
|
* it will happily pick up the time slice that we are
|
2009-04-17 20:13:30 +02:00
|
|
|
* relinquishing here.
|
2007-12-01 22:00:56 +01:00
|
|
|
*/
|
|
|
|
Sleep(0);
|
|
|
|
goto repeat;
|
|
|
|
}
|
|
|
|
return 0;
|
2007-12-01 21:24:59 +01: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-05 21:51:27 +01:00
|
|
|
#undef getcwd
|
|
|
|
char *mingw_getcwd(char *pointer, int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *ret = getcwd(pointer, len);
|
|
|
|
if (!ret)
|
|
|
|
return ret;
|
|
|
|
for (i = 0; pointer[i]; i++)
|
|
|
|
if (pointer[i] == '\\')
|
|
|
|
pointer[i] = '/';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-06-22 11:35:21 +02:00
|
|
|
#undef getenv
|
|
|
|
char *mingw_getenv(const char *name)
|
|
|
|
{
|
|
|
|
char *result = getenv(name);
|
|
|
|
if (!result && !strcmp(name, "TMPDIR")) {
|
|
|
|
/* on Windows it is TMP and TEMP */
|
|
|
|
result = getenv("TMP");
|
|
|
|
if (!result)
|
|
|
|
result = getenv("TEMP");
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-11-24 22:49:16 +01: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-24 21:43:02 +01:00
|
|
|
if (isspace(*p) || *p == '*' || *p == '?' || *p == '{' || *p == '\'')
|
2007-11-24 22:49:16 +01: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 */
|
|
|
|
d = q = xmalloc(len+n+3);
|
|
|
|
*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 12:38:32 +01: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 10:04:47 +02:00
|
|
|
p = buf + strcspn(buf, "\r\n");
|
|
|
|
if (!*p)
|
2007-12-04 12:38:32 +01: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)
|
|
|
|
{
|
|
|
|
char *p, **path, *envpath = getenv("PATH");
|
|
|
|
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;
|
|
|
|
|
2009-05-01 11:06:36 +02:00
|
|
|
path = xmalloc((n+1)*sizeof(char *));
|
2007-12-04 12:38:32 +01: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 10:20:17 +02:00
|
|
|
char **p = path;
|
|
|
|
|
2007-12-04 12:38:32 +01: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 09:34:42 +02:00
|
|
|
if (!(GetFileAttributes(path) & FILE_ATTRIBUTE_DIRECTORY))
|
|
|
|
return xstrdup(path);
|
2007-12-04 12:38:32 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determines the absolute path of cmd using the the split path in path.
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2007-11-24 22:49:16 +01:00
|
|
|
static int env_compare(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
char *const *ea = a;
|
|
|
|
char *const *eb = b;
|
|
|
|
return strcasecmp(*ea, *eb);
|
|
|
|
}
|
|
|
|
|
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-15 21:12:18 +01:00
|
|
|
static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **env,
|
|
|
|
int prepend_cmd, int fhin, int fhout, int fherr)
|
2007-11-24 22:49:16 +01:00
|
|
|
{
|
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
struct strbuf envblk, args;
|
|
|
|
unsigned flags;
|
|
|
|
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-02 18:11:13 +01:00
|
|
|
* It is necessary to use DETACHED_PROCESS
|
|
|
|
* instead of CREATE_NO_WINDOW to make ssh
|
|
|
|
* recognize that it has no console.
|
2007-11-24 22:49:16 +01:00
|
|
|
*/
|
2008-11-02 18:11:13 +01:00
|
|
|
flags = DETACHED_PROCESS;
|
2007-11-24 22:49:16 +01:00
|
|
|
} else {
|
|
|
|
/* There is already a console. If we specified
|
2008-11-02 18:11:13 +01:00
|
|
|
* DETACHED_PROCESS here, too, Windows would
|
2007-11-24 22:49:16 +01:00
|
|
|
* disassociate the child from the console.
|
2008-11-02 18:11:13 +01:00
|
|
|
* The same is true for CREATE_NO_WINDOW.
|
2007-11-24 22:49:16 +01:00
|
|
|
* Go figure!
|
|
|
|
*/
|
|
|
|
flags = 0;
|
|
|
|
CloseHandle(cons);
|
|
|
|
}
|
|
|
|
memset(&si, 0, sizeof(si));
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
si.dwFlags = STARTF_USESTDHANDLES;
|
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-15 21:12:18 +01:00
|
|
|
si.hStdInput = (HANDLE) _get_osfhandle(fhin);
|
|
|
|
si.hStdOutput = (HANDLE) _get_osfhandle(fhout);
|
|
|
|
si.hStdError = (HANDLE) _get_osfhandle(fherr);
|
2007-11-24 22:49:16 +01: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (env) {
|
|
|
|
int count = 0;
|
|
|
|
char **e, **sorted_env;
|
|
|
|
|
|
|
|
for (e = env; *e; e++)
|
|
|
|
count++;
|
|
|
|
|
|
|
|
/* environment must be sorted */
|
|
|
|
sorted_env = xmalloc(sizeof(*sorted_env) * (count + 1));
|
|
|
|
memcpy(sorted_env, env, sizeof(*sorted_env) * (count + 1));
|
|
|
|
qsort(sorted_env, count, sizeof(*sorted_env), env_compare);
|
|
|
|
|
|
|
|
strbuf_init(&envblk, 0);
|
|
|
|
for (e = sorted_env; *e; e++) {
|
|
|
|
strbuf_addstr(&envblk, *e);
|
|
|
|
strbuf_addch(&envblk, '\0');
|
|
|
|
}
|
|
|
|
free(sorted_env);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&pi, 0, sizeof(pi));
|
|
|
|
ret = CreateProcess(cmd, args.buf, NULL, NULL, TRUE, flags,
|
|
|
|
env ? envblk.buf : NULL, NULL, &si, &pi);
|
|
|
|
|
|
|
|
if (env)
|
|
|
|
strbuf_release(&envblk);
|
|
|
|
strbuf_release(&args);
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
return (pid_t)pi.hProcess;
|
|
|
|
}
|
|
|
|
|
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-15 21:12:18 +01:00
|
|
|
static pid_t mingw_spawnve(const char *cmd, const char **argv, char **env,
|
|
|
|
int prepend_cmd)
|
|
|
|
{
|
|
|
|
return mingw_spawnve_fd(cmd, argv, env, prepend_cmd, 0, 1, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env,
|
|
|
|
int fhin, int fhout, int fherr)
|
2007-11-24 22:49:16 +01: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 {
|
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-15 21:12:18 +01:00
|
|
|
pid = mingw_spawnve_fd(iprog, argv, env, 1,
|
|
|
|
fhin, fhout, fherr);
|
2007-11-24 22:49:16 +01:00
|
|
|
free(iprog);
|
|
|
|
}
|
|
|
|
argv[0] = argv0;
|
|
|
|
}
|
|
|
|
else
|
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-15 21:12:18 +01:00
|
|
|
pid = mingw_spawnve_fd(prog, argv, env, 0,
|
|
|
|
fhin, fhout, fherr);
|
2007-11-24 22:49:16 +01:00
|
|
|
free(prog);
|
|
|
|
}
|
|
|
|
free_path_split(path);
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2007-12-04 12:38:32 +01:00
|
|
|
static int try_shell_exec(const char *cmd, char *const *argv, char **env)
|
|
|
|
{
|
|
|
|
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++;
|
2007-11-24 22:49:16 +01:00
|
|
|
argv2 = xmalloc(sizeof(*argv) * (argc+1));
|
|
|
|
argv2[0] = (char *)cmd; /* full path to the script file */
|
|
|
|
memcpy(&argv2[1], &argv[1], sizeof(*argv) * argc);
|
|
|
|
pid = mingw_spawnve(prog, argv2, env, 1);
|
2007-12-04 12:38:32 +01: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mingw_execve(const char *cmd, char *const *argv, char *const *env)
|
|
|
|
{
|
|
|
|
/* check if git_command is a shell script */
|
|
|
|
if (!try_shell_exec(cmd, argv, (char **)env)) {
|
|
|
|
int pid, status;
|
|
|
|
|
2007-11-24 22:49:16 +01:00
|
|
|
pid = mingw_spawnve(cmd, (const char **)argv, (char **)env, 0);
|
2007-12-04 12:38:32 +01:00
|
|
|
if (pid < 0)
|
|
|
|
return;
|
|
|
|
if (waitpid(pid, &status, 0) < 0)
|
|
|
|
status = 255;
|
|
|
|
exit(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mingw_execvp(const char *cmd, char *const *argv)
|
|
|
|
{
|
|
|
|
char **path = get_path_split();
|
|
|
|
char *prog = path_lookup(cmd, path, 0);
|
|
|
|
|
|
|
|
if (prog) {
|
|
|
|
mingw_execve(prog, argv, environ);
|
|
|
|
free(prog);
|
|
|
|
} else
|
|
|
|
errno = ENOENT;
|
|
|
|
|
|
|
|
free_path_split(path);
|
|
|
|
}
|
|
|
|
|
2009-09-11 19:40:08 +02:00
|
|
|
static char **copy_environ(void)
|
2007-12-07 22:08:59 +01:00
|
|
|
{
|
|
|
|
char **env;
|
|
|
|
int i = 0;
|
|
|
|
while (environ[i])
|
|
|
|
i++;
|
|
|
|
env = xmalloc((i+1)*sizeof(*env));
|
|
|
|
for (i = 0; environ[i]; i++)
|
|
|
|
env[i] = xstrdup(environ[i]);
|
|
|
|
env[i] = NULL;
|
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_environ(char **env)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; env[i]; i++)
|
|
|
|
free(env[i]);
|
|
|
|
free(env);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lookup_env(char **env, const char *name, size_t nmln)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; env[i]; i++) {
|
|
|
|
if (0 == strncmp(env[i], name, nmln)
|
|
|
|
&& '=' == env[i][nmln])
|
|
|
|
/* matches */
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If name contains '=', then sets the variable, otherwise it unsets it
|
|
|
|
*/
|
2009-09-11 19:40:08 +02:00
|
|
|
static char **env_setenv(char **env, const char *name)
|
2007-12-07 22:08:59 +01:00
|
|
|
{
|
|
|
|
char *eq = strchrnul(name, '=');
|
|
|
|
int i = lookup_env(env, name, eq-name);
|
|
|
|
|
|
|
|
if (i < 0) {
|
|
|
|
if (*eq) {
|
|
|
|
for (i = 0; env[i]; i++)
|
|
|
|
;
|
|
|
|
env = xrealloc(env, (i+2)*sizeof(*env));
|
|
|
|
env[i] = xstrdup(name);
|
|
|
|
env[i+1] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
free(env[i]);
|
|
|
|
if (*eq)
|
|
|
|
env[i] = xstrdup(name);
|
|
|
|
else
|
|
|
|
for (; env[i]; i++)
|
|
|
|
env[i] = env[i+1];
|
|
|
|
}
|
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
2009-09-11 19:40:08 +02:00
|
|
|
/*
|
|
|
|
* Copies global environ and adjusts variables as specified by vars.
|
|
|
|
*/
|
|
|
|
char **make_augmented_environ(const char *const *vars)
|
|
|
|
{
|
|
|
|
char **env = copy_environ();
|
|
|
|
|
|
|
|
while (*vars)
|
|
|
|
env = env_setenv(env, *vars++);
|
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
2009-11-23 23:55:50 +01:00
|
|
|
/*
|
|
|
|
* Note, this isn't a complete replacement for getaddrinfo. It assumes
|
|
|
|
* that service contains a numerical port, or that it it is null. It
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
struct hostent *h = gethostbyname(node);
|
|
|
|
struct addrinfo *ai;
|
|
|
|
struct sockaddr_in *sin;
|
|
|
|
|
|
|
|
if (!h)
|
|
|
|
return WSAGetLastError();
|
|
|
|
|
|
|
|
ai = xmalloc(sizeof(struct addrinfo));
|
|
|
|
*res = ai;
|
|
|
|
ai->ai_flags = 0;
|
|
|
|
ai->ai_family = AF_INET;
|
|
|
|
ai->ai_socktype = hints->ai_socktype;
|
|
|
|
switch (hints->ai_socktype) {
|
|
|
|
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);
|
|
|
|
ai->ai_canonname = strdup(h->h_name);
|
|
|
|
|
|
|
|
sin = xmalloc(ai->ai_addrlen);
|
|
|
|
memset(sin, 0, ai->ai_addrlen);
|
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
if (service)
|
|
|
|
sin->sin_port = htons(atoi(service));
|
|
|
|
sin->sin_addr = *(struct in_addr *)h->h_addr;
|
|
|
|
ai->ai_addr = (struct sockaddr *)sin;
|
|
|
|
ai->ai_next = 0;
|
|
|
|
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-23 23:55:12 +01:00
|
|
|
static void ensure_socket_initialization(void)
|
2007-12-26 13:51:18 +01:00
|
|
|
{
|
|
|
|
WSADATA wsa;
|
2009-11-23 23:55:12 +01:00
|
|
|
static int initialized = 0;
|
2009-11-23 23:55:50 +01:00
|
|
|
const char *libraries[] = { "ws2_32.dll", "wship6.dll", NULL };
|
|
|
|
const char **name;
|
2009-11-23 23:55:12 +01:00
|
|
|
|
|
|
|
if (initialized)
|
|
|
|
return;
|
2007-12-26 13:51:18 +01:00
|
|
|
|
|
|
|
if (WSAStartup(MAKEWORD(2,2), &wsa))
|
|
|
|
die("unable to initialize winsock subsystem, error %d",
|
|
|
|
WSAGetLastError());
|
2009-11-23 23:55:50 +01: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-23 23:55:12 +01:00
|
|
|
initialized = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef gethostbyname
|
|
|
|
struct hostent *mingw_gethostbyname(const char *host)
|
|
|
|
{
|
|
|
|
ensure_socket_initialization();
|
2007-12-26 13:51:18 +01:00
|
|
|
return gethostbyname(host);
|
|
|
|
}
|
|
|
|
|
2009-11-23 23:55:50 +01: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 13:51:18 +01:00
|
|
|
int mingw_socket(int domain, int type, int protocol)
|
|
|
|
{
|
|
|
|
int sockfd;
|
|
|
|
SOCKET s = WSASocket(domain, type, protocol, NULL, 0, 0);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-12-07 22:19:40 +01:00
|
|
|
#undef rename
|
|
|
|
int mingw_rename(const char *pold, const char *pnew)
|
|
|
|
{
|
2009-04-03 08:49:59 +02:00
|
|
|
DWORD attrs, gle;
|
|
|
|
int tries = 0;
|
|
|
|
static const int delay[] = { 0, 1, 10, 20, 40 };
|
2008-11-19 17:25:27 +01:00
|
|
|
|
2007-12-07 22:19:40 +01:00
|
|
|
/*
|
|
|
|
* Try native rename() first to get errno right.
|
|
|
|
* It is based on MoveFile(), which cannot overwrite existing files.
|
|
|
|
*/
|
|
|
|
if (!rename(pold, pnew))
|
|
|
|
return 0;
|
|
|
|
if (errno != EEXIST)
|
|
|
|
return -1;
|
2009-04-03 08:49:59 +02:00
|
|
|
repeat:
|
2007-12-07 22:19:40 +01:00
|
|
|
if (MoveFileEx(pold, pnew, MOVEFILE_REPLACE_EXISTING))
|
|
|
|
return 0;
|
|
|
|
/* TODO: translate more errors */
|
2009-04-03 08:49:59 +02:00
|
|
|
gle = GetLastError();
|
|
|
|
if (gle == ERROR_ACCESS_DENIED &&
|
2008-11-19 17:25:27 +01:00
|
|
|
(attrs = GetFileAttributes(pnew)) != INVALID_FILE_ATTRIBUTES) {
|
|
|
|
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
|
2007-12-07 22:19:40 +01:00
|
|
|
errno = EISDIR;
|
|
|
|
return -1;
|
|
|
|
}
|
2008-11-19 17:25:27 +01:00
|
|
|
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
|
|
|
|
SetFileAttributes(pnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
|
|
|
|
if (MoveFileEx(pold, pnew, MOVEFILE_REPLACE_EXISTING))
|
|
|
|
return 0;
|
2009-04-03 08:49:59 +02:00
|
|
|
gle = GetLastError();
|
2008-11-19 17:25:27 +01:00
|
|
|
/* revert file attributes on failure */
|
|
|
|
SetFileAttributes(pnew, attrs);
|
|
|
|
}
|
2007-12-07 22:19:40 +01:00
|
|
|
}
|
2009-04-03 08:49:59 +02: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;
|
|
|
|
}
|
2007-12-07 22:19:40 +01:00
|
|
|
errno = EACCES;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-11-07 21:10:31 +01: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-01 21:24:59 +01:00
|
|
|
struct passwd *getpwuid(int uid)
|
|
|
|
{
|
2007-12-01 22:09:17 +01:00
|
|
|
static char user_name[100];
|
2007-12-01 21:24:59 +01:00
|
|
|
static struct passwd p;
|
2007-12-01 22:09:17 +01: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-01 21:24:59 +01:00
|
|
|
return &p;
|
|
|
|
}
|
|
|
|
|
2007-11-13 10:14:45 +01:00
|
|
|
static HANDLE timer_event;
|
|
|
|
static HANDLE timer_thread;
|
|
|
|
static int timer_interval;
|
|
|
|
static int one_shot;
|
|
|
|
static sig_handler_t timer_fn = SIG_DFL;
|
|
|
|
|
|
|
|
/* 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 10:20:21 +02:00
|
|
|
static unsigned __stdcall ticktack(void *dummy)
|
2007-11-13 10:14:45 +01:00
|
|
|
{
|
|
|
|
while (WaitForSingleObject(timer_event, timer_interval) == WAIT_TIMEOUT) {
|
|
|
|
if (timer_fn == SIG_DFL)
|
|
|
|
die("Alarm");
|
|
|
|
if (timer_fn != SIG_IGN)
|
|
|
|
timer_fn(SIGALRM);
|
|
|
|
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-01 21:24:59 +01:00
|
|
|
int setitimer(int type, struct itimerval *in, struct itimerval *out)
|
|
|
|
{
|
2007-11-13 10:14:45 +01: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-01 21:24:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int sigaction(int sig, struct sigaction *in, struct sigaction *out)
|
|
|
|
{
|
2007-11-13 10:14:45 +01: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)
|
|
|
|
{
|
2009-09-16 10:20:17 +02:00
|
|
|
sig_handler_t old = timer_fn;
|
2007-11-13 10:14:45 +01:00
|
|
|
if (sig != SIGALRM)
|
|
|
|
return signal(sig, handler);
|
|
|
|
timer_fn = handler;
|
|
|
|
return old;
|
2007-12-01 21:24:59 +01:00
|
|
|
}
|
2008-07-13 22:31:20 +02: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-15 21:12:16 +01:00
|
|
|
typedef HINSTANCE (WINAPI *T)(HWND, const char *,
|
|
|
|
const char *, const char *, const char *, INT);
|
|
|
|
T ShellExecute;
|
|
|
|
HMODULE shell32;
|
|
|
|
|
|
|
|
shell32 = LoadLibrary("shell32.dll");
|
|
|
|
if (!shell32)
|
|
|
|
die("cannot load shell32.dll");
|
|
|
|
ShellExecute = (T)GetProcAddress(shell32, "ShellExecuteA");
|
|
|
|
if (!ShellExecute)
|
|
|
|
die("cannot run browser");
|
|
|
|
|
2008-07-13 22:31:20 +02:00
|
|
|
printf("Launching default browser to display HTML ...\n");
|
|
|
|
ShellExecute(NULL, "open", htmlpath, NULL, "\\", 0);
|
2010-01-15 21:12:16 +01:00
|
|
|
|
|
|
|
FreeLibrary(shell32);
|
2008-07-13 22:31:20 +02:00
|
|
|
}
|
2009-01-24 15:04:39 +01:00
|
|
|
|
|
|
|
int link(const char *oldpath, const char *newpath)
|
|
|
|
{
|
2009-09-16 10:20:21 +02:00
|
|
|
typedef BOOL (WINAPI *T)(const char*, const char*, LPSECURITY_ATTRIBUTES);
|
2009-01-24 15:04:39 +01:00
|
|
|
static T create_hard_link = NULL;
|
|
|
|
if (!create_hard_link) {
|
|
|
|
create_hard_link = (T) GetProcAddress(
|
|
|
|
GetModuleHandle("kernel32.dll"), "CreateHardLinkA");
|
|
|
|
if (!create_hard_link)
|
|
|
|
create_hard_link = (T)-1;
|
|
|
|
}
|
|
|
|
if (create_hard_link == (T)-1) {
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!create_hard_link(newpath, oldpath, NULL)) {
|
|
|
|
errno = err_win_to_posix(GetLastError());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2009-05-23 10:04:50 +02:00
|
|
|
|
|
|
|
char *getpass(const char *prompt)
|
|
|
|
{
|
|
|
|
struct strbuf buf = STRBUF_INIT;
|
|
|
|
|
|
|
|
fputs(prompt, stderr);
|
|
|
|
for (;;) {
|
|
|
|
char c = _getch();
|
|
|
|
if (c == '\r' || c == '\n')
|
|
|
|
break;
|
|
|
|
strbuf_addch(&buf, c);
|
|
|
|
}
|
|
|
|
fputs("\n", stderr);
|
|
|
|
return strbuf_detach(&buf, NULL);
|
|
|
|
}
|
2009-05-31 18:15:22 +02:00
|
|
|
|
|
|
|
#ifndef NO_MINGW_REPLACE_READDIR
|
|
|
|
/* MinGW readdir implementation to avoid extra lstats for Git */
|
|
|
|
struct mingw_DIR
|
|
|
|
{
|
|
|
|
struct _finddata_t dd_dta; /* disk transfer area for this dir */
|
|
|
|
struct mingw_dirent dd_dir; /* Our own implementation, including d_type */
|
|
|
|
long dd_handle; /* _findnext handle */
|
|
|
|
int dd_stat; /* 0 = next entry to read is first entry, -1 = off the end, positive = 0 based index of next entry */
|
|
|
|
char dd_name[1]; /* given path for dir with search pattern (struct is extended) */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct dirent *mingw_readdir(DIR *dir)
|
|
|
|
{
|
|
|
|
WIN32_FIND_DATAA buf;
|
|
|
|
HANDLE handle;
|
|
|
|
struct mingw_DIR *mdir = (struct mingw_DIR*)dir;
|
|
|
|
|
|
|
|
if (!dir->dd_handle) {
|
|
|
|
errno = EBADF; /* No set_errno for mingw */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dir->dd_handle == (long)INVALID_HANDLE_VALUE && dir->dd_stat == 0)
|
|
|
|
{
|
2009-09-16 10:20:17 +02:00
|
|
|
DWORD lasterr;
|
2009-05-31 18:15:22 +02:00
|
|
|
handle = FindFirstFileA(dir->dd_name, &buf);
|
2009-09-16 10:20:17 +02:00
|
|
|
lasterr = GetLastError();
|
2009-05-31 18:15:22 +02:00
|
|
|
dir->dd_handle = (long)handle;
|
|
|
|
if (handle == INVALID_HANDLE_VALUE && (lasterr != ERROR_NO_MORE_FILES)) {
|
|
|
|
errno = err_win_to_posix(lasterr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else if (dir->dd_handle == (long)INVALID_HANDLE_VALUE) {
|
|
|
|
return NULL;
|
|
|
|
} else if (!FindNextFileA((HANDLE)dir->dd_handle, &buf)) {
|
|
|
|
DWORD lasterr = GetLastError();
|
|
|
|
FindClose((HANDLE)dir->dd_handle);
|
|
|
|
dir->dd_handle = (long)INVALID_HANDLE_VALUE;
|
|
|
|
/* POSIX says you shouldn't set errno when readdir can't
|
|
|
|
find any more files; so, if another error we leave it set. */
|
|
|
|
if (lasterr != ERROR_NO_MORE_FILES)
|
|
|
|
errno = err_win_to_posix(lasterr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We get here if `buf' contains valid data. */
|
|
|
|
strcpy(dir->dd_dir.d_name, buf.cFileName);
|
|
|
|
++dir->dd_stat;
|
|
|
|
|
|
|
|
/* Set file type, based on WIN32_FIND_DATA */
|
|
|
|
mdir->dd_dir.d_type = 0;
|
|
|
|
if (buf.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
|
|
|
mdir->dd_dir.d_type |= DT_DIR;
|
|
|
|
else
|
|
|
|
mdir->dd_dir.d_type |= DT_REG;
|
|
|
|
|
|
|
|
return (struct dirent*)&dir->dd_dir;
|
|
|
|
}
|
|
|
|
#endif // !NO_MINGW_REPLACE_READDIR
|