Commit c7c787ce by Carlos Martín Nieto

Use gitno_buffer in the git transport

This allows us to leave out the buffer handling logic.

Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>
parent ea7a5452
......@@ -38,7 +38,7 @@
#include "common.h"
#include "netops.h"
void gitno_buffer_setup(gitno_buffer *buf, void*data, unsigned int len, int fd)
void gitno_buffer_setup(gitno_buffer *buf, char *data, unsigned int len, int fd)
{
memset(buf, 0x0, sizeof(gitno_buffer));
memset(data, 0x0, len);
......@@ -64,17 +64,17 @@ int gitno_recv(gitno_buffer *buf)
}
/* Consume up to ptr and move the rest of the buffer to the beginning */
void gitno_consume(gitno_buffer *buf, void *ptr)
void gitno_consume(gitno_buffer *buf, const char *ptr)
{
int left;
int consumed;
assert(ptr - buf->data <= (int) buf->len);
left = buf->len - (ptr - buf->data);
consumed = ptr - buf->data;
memmove(buf->data, ptr, left);
memset(ptr, 0x0, left);
buf->offset = left;
memmove(buf->data, ptr, buf->offset - consumed);
memset(buf->data + buf->offset, 0x0, buf->len - buf->offset);
buf->offset -= consumed;
}
/* Consume const bytes and move the rest of the buffer to the beginning */
......
......@@ -5,15 +5,15 @@
#define INCLUDE_netops_h__
typedef struct gitno_buffer {
void *data;
char *data;
unsigned int len;
unsigned int offset;
int fd;
} gitno_buffer;
void gitno_buffer_setup(gitno_buffer *buf, void *data, unsigned int len, int fd);
void gitno_buffer_setup(gitno_buffer *buf, char *data, unsigned int len, int fd);
int gitno_recv(gitno_buffer *buf);
void gitno_consume(gitno_buffer *buf, void *ptr);
void gitno_consume(gitno_buffer *buf, const char *ptr);
void gitno_consume_n(gitno_buffer *buf, unsigned int cons);
int gitno_connect(const char *host, const char *port);
......
......@@ -152,8 +152,9 @@ int git_pkt_parse_line(git_pkt **head, const char *line, const char **out, size_
return GIT_ESHORTBUFFER;
error = parse_len(line);
if (error < GIT_SUCCESS)
if (error < GIT_SUCCESS) {
return git__throw(error, "Failed to parse pkt length");
}
len = error;
......
......@@ -175,30 +175,28 @@ static int do_connect(transport_git *t, const char *url)
*/
static int store_refs(transport_git *t)
{
gitno_buffer buf;
int s = t->socket;
git_vector *refs = &t->refs;
int error = GIT_SUCCESS;
char buffer[1024];
const char *line_end, *ptr;
int off = 0, ret;
unsigned int bufflen = 0;
git_pkt *pkt;
memset(buffer, 0x0, sizeof(buffer));
gitno_buffer_setup(&buf, buffer, sizeof(buffer), s);
while (1) {
ret = recv(s, buffer + off, sizeof(buffer) - off, 0);
if (ret < 0)
return git__throw(GIT_EOSERR, "Failed to receive data");
if (ret == 0) /* Orderly shutdown, so exit */
error = gitno_recv(&buf);
if (error < GIT_SUCCESS)
return git__rethrow(GIT_EOSERR, "Failed to receive data");
if (error == GIT_SUCCESS) /* Orderly shutdown, so exit */
return GIT_SUCCESS;
bufflen += ret;
ptr = buffer;
ptr = buf.data;
while (1) {
if (bufflen == 0)
if (buf.offset == 0)
break;
error = git_pkt_parse_line(&pkt, ptr, &line_end, bufflen);
error = git_pkt_parse_line(&pkt, ptr, &line_end, buf.offset);
/*
* If the error is GIT_ESHORTBUFFER, it means the buffer
* isn't long enough to satisfy the request. Break out and
......@@ -206,13 +204,15 @@ static int store_refs(transport_git *t)
* On any other error, fail.
*/
if (error == GIT_ESHORTBUFFER) {
line_end = ptr;
break;
}
if (error < GIT_SUCCESS) {
return error;
}
/* Get rid of the part we've used already */
gitno_consume(&buf, line_end);
error = git_vector_insert(refs, pkt);
if (error < GIT_SUCCESS)
return error;
......@@ -220,16 +220,7 @@ static int store_refs(transport_git *t)
if (pkt->type == GIT_PKT_FLUSH)
return GIT_SUCCESS;
bufflen -= line_end - ptr;
ptr = line_end;
}
/*
* Move the rest to the start of the buffer
*/
memmove(buffer, line_end, bufflen);
off = bufflen;
memset(buffer + off, 0x0, sizeof(buffer) - off);
}
return error;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment