netops: abstract away socket closing Winsock wants us to use closesocket() instead of close(), so introduce the gitno_close function, which does the right thing. Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
diff --git a/src/netops.c b/src/netops.c
index c8fe376..7d8a7b2 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -138,6 +138,18 @@ int gitno_send(GIT_SOCKET s, const char *msg, size_t len, int flags)
return off;
}
+#ifdef GIT_WIN32
+int gitno_close(GIT_SOCKET s)
+{
+ return closesocket(s) == SOCKET_ERROR ? -1 : 0;
+}
+#else
+int gitno_close(GIT_SOCKET s)
+{
+ return close(s);
+}
+#endif
+
int gitno_select_in(gitno_buffer *buf, long int sec, long int usec)
{
fd_set fds;
diff --git a/src/netops.h b/src/netops.h
index b0425ae..203df85 100644
--- a/src/netops.h
+++ b/src/netops.h
@@ -27,6 +27,7 @@ void gitno_consume_n(gitno_buffer *buf, size_t cons);
int gitno_connect(const char *host, const char *port);
int gitno_send(GIT_SOCKET s, const char *msg, size_t len, int flags);
+int gitno_close(GIT_SOCKET s);
int gitno_select_in(gitno_buffer *buf, long int sec, long int usec);
int gitno_extract_host_and_port(char **host, char **port, const char *url, const char *default_port);
diff --git a/src/transport-http.c b/src/transport-http.c
index 3ee4025..1da56e1 100644
--- a/src/transport-http.c
+++ b/src/transport-http.c
@@ -362,7 +362,7 @@ static int http_close(git_transport *transport)
transport_http *t = (transport_http *) transport;
int error;
- error = close(t->socket);
+ error = gitno_close(t->socket);
if (error < 0)
return git__throw(GIT_EOSERR, "Failed to close the socket: %s", strerror(errno));
diff --git a/src/transport_git.c b/src/transport_git.c
index 42503e1..8529fd4 100644
--- a/src/transport_git.c
+++ b/src/transport_git.c
@@ -505,15 +505,18 @@ static int git_download_pack(char **out, git_transport *transport, git_repositor
static int git_close(git_transport *transport)
{
transport_git *t = (transport_git*) transport;
- int s = t->socket;
int error;
/* Can't do anything if there's an error, so don't bother checking */
- git_pkt_send_flush(s);
- error = close(s);
+ git_pkt_send_flush(t->socket);
+ error = gitno_close(t->socket);
if (error < 0)
error = git__throw(GIT_EOSERR, "Failed to close socket");
+#ifdef GIT_WIN32
+ WSACleanup();
+#endif
+
return error;
}
@@ -528,10 +531,6 @@ static void git_free(git_transport *transport)
git_pkt_free(p);
}
-#ifdef GIT_WIN32
- WSACleanup();
-#endif
-
git_vector_free(refs);
free(t->heads);
free(t->parent.url);