summaryrefslogtreecommitdiff
path: root/compat/mingw.c
diff options
context:
space:
mode:
Diffstat (limited to 'compat/mingw.c')
-rw-r--r--compat/mingw.c297
1 files changed, 214 insertions, 83 deletions
diff --git a/compat/mingw.c b/compat/mingw.c
index f2d9e1f..fdbf093 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -127,7 +127,7 @@ int mingw_open (const char *filename, int oflags, ...)
mode = va_arg(args, int);
va_end(args);
- if (!strcmp(filename, "/dev/null"))
+ if (filename && !strcmp(filename, "/dev/null"))
filename = "nul";
fd = open(filename, oflags, mode);
@@ -160,7 +160,7 @@ ssize_t mingw_write(int fd, const void *buf, size_t count)
#undef fopen
FILE *mingw_fopen (const char *filename, const char *otype)
{
- if (!strcmp(filename, "/dev/null"))
+ if (filename && !strcmp(filename, "/dev/null"))
filename = "nul";
return fopen(filename, otype);
}
@@ -192,12 +192,16 @@ static inline time_t filetime_to_time_t(const FILETIME *ft)
/* 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.
+ *
+ * If follow is true then act like stat() and report on the link
+ * target. Otherwise report on the link itself.
*/
-static int do_lstat(const char *file_name, struct stat *buf)
+static int do_lstat(int follow, const char *file_name, struct stat *buf)
{
+ int err;
WIN32_FILE_ATTRIBUTE_DATA fdata;
- if (!(errno = get_file_attr(file_name, &fdata))) {
+ if (!(err = get_file_attr(file_name, &fdata))) {
buf->st_ino = 0;
buf->st_gid = 0;
buf->st_uid = 0;
@@ -209,8 +213,28 @@ static int do_lstat(const char *file_name, struct stat *buf)
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));
+ if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
+ WIN32_FIND_DATAA findbuf;
+ HANDLE handle = FindFirstFileA(file_name, &findbuf);
+ 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);
+ }
+ }
return 0;
}
+ errno = err;
return -1;
}
@@ -220,12 +244,12 @@ static int do_lstat(const char *file_name, struct stat *buf)
* complete. Note that Git stat()s are redirected to mingw_lstat()
* too, since Windows doesn't really handle symlinks that well.
*/
-int mingw_lstat(const char *file_name, struct stat *buf)
+static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
{
int namelen;
static char alt_name[PATH_MAX];
- if (!do_lstat(file_name, buf))
+ if (!do_lstat(follow, file_name, buf))
return 0;
/* if file_name ended in a '/', Windows returned ENOENT;
@@ -244,7 +268,16 @@ int mingw_lstat(const char *file_name, struct stat *buf)
memcpy(alt_name, file_name, namelen);
alt_name[namelen] = 0;
- return do_lstat(alt_name, buf);
+ 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);
}
#undef fstat
@@ -377,71 +410,6 @@ int pipe(int filedes[2])
return 0;
}
-int poll(struct pollfd *ufds, unsigned int nfds, int timeout)
-{
- int i, pending;
-
- if (timeout >= 0) {
- if (nfds == 0) {
- Sleep(timeout);
- return 0;
- }
- return errno = EINVAL, error("poll timeout not supported");
- }
-
- /* 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
- * relinquishing here.
- */
- Sleep(0);
- goto repeat;
- }
- return 0;
-}
-
struct tm *gmtime_r(const time_t *timep, struct tm *result)
{
/* gmtime() in MSVCRT.DLL is thread-safe, but not reentrant */
@@ -671,6 +639,14 @@ static int env_compare(const void *a, const void *b)
return strcasecmp(*ea, *eb);
}
+struct pinfo_t {
+ struct pinfo_t *next;
+ pid_t pid;
+ HANDLE proc;
+} pinfo_t;
+struct pinfo_t *pinfo = NULL;
+CRITICAL_SECTION pinfo_cs;
+
static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **env,
const char *dir,
int prepend_cmd, int fhin, int fhout, int fherr)
@@ -763,7 +739,26 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **env,
return -1;
}
CloseHandle(pi.hThread);
- return (pid_t)pi.hProcess;
+
+ /*
+ * 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;
}
static pid_t mingw_spawnve(const char *cmd, const char **argv, char **env,
@@ -873,6 +868,30 @@ void mingw_execvp(const char *cmd, char *const *argv)
free_path_split(path);
}
+void mingw_execv(const char *cmd, char *const *argv)
+{
+ mingw_execve(cmd, argv, environ);
+}
+
+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;
+ }
+
+ errno = EINVAL;
+ return -1;
+}
+
static char **copy_environ(void)
{
char **env;
@@ -957,19 +976,22 @@ static int WSAAPI getaddrinfo_stub(const char *node, const char *service,
const struct addrinfo *hints,
struct addrinfo **res)
{
- struct hostent *h = gethostbyname(node);
+ struct hostent *h = NULL;
struct addrinfo *ai;
struct sockaddr_in *sin;
- if (!h)
- return WSAGetLastError();
+ if (node) {
+ h = gethostbyname(node);
+ 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) {
+ ai->ai_socktype = hints ? hints->ai_socktype : 0;
+ switch (ai->ai_socktype) {
case SOCK_STREAM:
ai->ai_protocol = IPPROTO_TCP;
break;
@@ -981,14 +1003,25 @@ static int WSAAPI getaddrinfo_stub(const char *node, const char *service,
break;
}
ai->ai_addrlen = sizeof(struct sockaddr_in);
- ai->ai_canonname = strdup(h->h_name);
+ if (hints && (hints->ai_flags & AI_CANONNAME))
+ ai->ai_canonname = h ? strdup(h->h_name) : NULL;
+ else
+ ai->ai_canonname = NULL;
sin = xmalloc(ai->ai_addrlen);
memset(sin, 0, ai->ai_addrlen);
sin->sin_family = AF_INET;
+ /* Note: getaddrinfo is supposed to allow service to be a string,
+ * which should be looked up using getservbyname. This is
+ * currently not implemented */
if (service)
sin->sin_port = htons(atoi(service));
- sin->sin_addr = *(struct in_addr *)h->h_addr;
+ 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;
ai->ai_addr = (struct sockaddr *)sin;
ai->ai_next = 0;
return 0;
@@ -1139,7 +1172,10 @@ int mingw_getnameinfo(const struct sockaddr *sa, socklen_t salen,
int mingw_socket(int domain, int type, int protocol)
{
int sockfd;
- SOCKET s = WSASocket(domain, type, protocol, NULL, 0, 0);
+ SOCKET s;
+
+ ensure_socket_initialization();
+ s = WSASocket(domain, type, protocol, NULL, 0, 0);
if (s == INVALID_SOCKET) {
/*
* WSAGetLastError() values are regular BSD error codes
@@ -1169,6 +1205,45 @@ int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz)
return connect(s, sa, sz);
}
+#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);
+}
+
+#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;
+}
+
#undef rename
int mingw_rename(const char *pold, const char *pnew)
{
@@ -1386,6 +1461,7 @@ void mingw_open_html(const char *unixpath)
const char *, const char *, const char *, INT);
T ShellExecute;
HMODULE shell32;
+ int r;
shell32 = LoadLibrary("shell32.dll");
if (!shell32)
@@ -1395,9 +1471,12 @@ void mingw_open_html(const char *unixpath)
die("cannot run browser");
printf("Launching default browser to display HTML ...\n");
- ShellExecute(NULL, "open", htmlpath, NULL, "\\", 0);
-
+ r = (int)ShellExecute(NULL, "open", htmlpath, NULL, "\\", SW_SHOWNORMAL);
FreeLibrary(shell32);
+ /* see the MSDN documentation referring to the result codes here */
+ if (r <= 32) {
+ die("failed to launch browser for %.*s", MAX_PATH, unixpath);
+ }
}
int link(const char *oldpath, const char *newpath)
@@ -1436,6 +1515,58 @@ char *getpass(const char *prompt)
return strbuf_detach(&buf, NULL);
}
+pid_t waitpid(pid_t pid, int *status, unsigned options)
+{
+ HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,
+ FALSE, pid);
+ if (!h) {
+ errno = ECHILD;
+ return -1;
+ }
+
+ if (pid > 0 && options & WNOHANG) {
+ if (WAIT_OBJECT_0 != WaitForSingleObject(h, 0)) {
+ CloseHandle(h);
+ return 0;
+ }
+ options &= ~WNOHANG;
+ }
+
+ 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;
+}
+
#ifndef NO_MINGW_REPLACE_READDIR
/* MinGW readdir implementation to avoid extra lstats for Git */
struct mingw_DIR