Skip to content

Commit

Permalink
imap-send: use separate read and write fds
Browse files Browse the repository at this point in the history
This is a patch that enables us to use the run-command
API, which is supported on Windows.

Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Erik Faye-Lund authored and Junio C Hamano committed Oct 20, 2009
1 parent 3a7cba9 commit 7a7796e
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions imap-send.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ struct imap_list {
};

struct imap_socket {
int fd;
int fd[2];
SSL *ssl;
};

Expand Down Expand Up @@ -301,8 +301,12 @@ static int ssl_socket_connect(struct imap_socket *sock, int use_tls_only, int ve
ssl_socket_perror("SSL_new");
return -1;
}
if (!SSL_set_fd(sock->ssl, sock->fd)) {
ssl_socket_perror("SSL_set_fd");
if (!SSL_set_rfd(sock->ssl, sock->fd[0])) {
ssl_socket_perror("SSL_set_rfd");
return -1;
}
if (!SSL_set_wfd(sock->ssl, sock->fd[1])) {
ssl_socket_perror("SSL_set_wfd");
return -1;
}

Expand All @@ -324,11 +328,12 @@ static int socket_read(struct imap_socket *sock, char *buf, int len)
n = SSL_read(sock->ssl, buf, len);
else
#endif
n = xread(sock->fd, buf, len);
n = xread(sock->fd[0], buf, len);
if (n <= 0) {
socket_perror("read", sock, n);
close(sock->fd);
sock->fd = -1;
close(sock->fd[0]);
close(sock->fd[1]);
sock->fd[0] = sock->fd[1] = -1;
}
return n;
}
Expand All @@ -341,11 +346,12 @@ static int socket_write(struct imap_socket *sock, const char *buf, int len)
n = SSL_write(sock->ssl, buf, len);
else
#endif
n = write_in_full(sock->fd, buf, len);
n = write_in_full(sock->fd[1], buf, len);
if (n != len) {
socket_perror("write", sock, n);
close(sock->fd);
sock->fd = -1;
close(sock->fd[0]);
close(sock->fd[1]);
sock->fd[0] = sock->fd[1] = -1;
}
return n;
}
Expand All @@ -358,7 +364,8 @@ static void socket_shutdown(struct imap_socket *sock)
SSL_free(sock->ssl);
}
#endif
close(sock->fd);
close(sock->fd[0]);
close(sock->fd[1]);
}

/* simple line buffering */
Expand Down Expand Up @@ -911,7 +918,7 @@ static void imap_close_server(struct imap_store *ictx)
{
struct imap *imap = ictx->imap;

if (imap->buf.sock.fd != -1) {
if (imap->buf.sock.fd[0] != -1) {
imap_exec(ictx, NULL, "LOGOUT");
socket_shutdown(&imap->buf.sock);
}
Expand Down Expand Up @@ -939,7 +946,7 @@ static struct store *imap_open_store(struct imap_server_conf *srvc)
ctx = xcalloc(sizeof(*ctx), 1);

ctx->imap = imap = xcalloc(sizeof(*imap), 1);
imap->buf.sock.fd = -1;
imap->buf.sock.fd[0] = imap->buf.sock.fd[1] = -1;
imap->in_progress_append = &imap->in_progress;

/* open connection to IMAP server */
Expand All @@ -966,7 +973,8 @@ static struct store *imap_open_store(struct imap_server_conf *srvc)

close(a[0]);

imap->buf.sock.fd = a[1];
imap->buf.sock.fd[0] = a[1];
imap->buf.sock.fd[1] = dup(a[1]);

imap_info("ok\n");
} else {
Expand Down Expand Up @@ -1043,7 +1051,8 @@ static struct store *imap_open_store(struct imap_server_conf *srvc)
goto bail;
}

imap->buf.sock.fd = s;
imap->buf.sock.fd[0] = s;
imap->buf.sock.fd[1] = dup(s);

if (srvc->use_ssl &&
ssl_socket_connect(&imap->buf.sock, 0, srvc->ssl_verify)) {
Expand Down

0 comments on commit 7a7796e

Please sign in to comment.