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 @@ ...@@ -38,7 +38,7 @@
#include "common.h" #include "common.h"
#include "netops.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(buf, 0x0, sizeof(gitno_buffer));
memset(data, 0x0, len); memset(data, 0x0, len);
...@@ -64,17 +64,17 @@ int gitno_recv(gitno_buffer *buf) ...@@ -64,17 +64,17 @@ int gitno_recv(gitno_buffer *buf)
} }
/* Consume up to ptr and move the rest of the buffer to the beginning */ /* 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); assert(ptr - buf->data <= (int) buf->len);
left = buf->len - (ptr - buf->data); consumed = ptr - buf->data;
memmove(buf->data, ptr, left); memmove(buf->data, ptr, buf->offset - consumed);
memset(ptr, 0x0, left); memset(buf->data + buf->offset, 0x0, buf->len - buf->offset);
buf->offset = left; buf->offset -= consumed;
} }
/* Consume const bytes and move the rest of the buffer to the beginning */ /* Consume const bytes and move the rest of the buffer to the beginning */
......
...@@ -5,15 +5,15 @@ ...@@ -5,15 +5,15 @@
#define INCLUDE_netops_h__ #define INCLUDE_netops_h__
typedef struct gitno_buffer { typedef struct gitno_buffer {
void *data; char *data;
unsigned int len; unsigned int len;
unsigned int offset; unsigned int offset;
int fd; int fd;
} gitno_buffer; } 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); 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); void gitno_consume_n(gitno_buffer *buf, unsigned int cons);
int gitno_connect(const char *host, const char *port); 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_ ...@@ -152,8 +152,9 @@ int git_pkt_parse_line(git_pkt **head, const char *line, const char **out, size_
return GIT_ESHORTBUFFER; return GIT_ESHORTBUFFER;
error = parse_len(line); error = parse_len(line);
if (error < GIT_SUCCESS) if (error < GIT_SUCCESS) {
return git__throw(error, "Failed to parse pkt length"); return git__throw(error, "Failed to parse pkt length");
}
len = error; len = error;
......
...@@ -175,30 +175,28 @@ static int do_connect(transport_git *t, const char *url) ...@@ -175,30 +175,28 @@ static int do_connect(transport_git *t, const char *url)
*/ */
static int store_refs(transport_git *t) static int store_refs(transport_git *t)
{ {
gitno_buffer buf;
int s = t->socket; int s = t->socket;
git_vector *refs = &t->refs; git_vector *refs = &t->refs;
int error = GIT_SUCCESS; int error = GIT_SUCCESS;
char buffer[1024]; char buffer[1024];
const char *line_end, *ptr; const char *line_end, *ptr;
int off = 0, ret;
unsigned int bufflen = 0;
git_pkt *pkt; git_pkt *pkt;
memset(buffer, 0x0, sizeof(buffer)); gitno_buffer_setup(&buf, buffer, sizeof(buffer), s);
while (1) { while (1) {
ret = recv(s, buffer + off, sizeof(buffer) - off, 0); error = gitno_recv(&buf);
if (ret < 0) if (error < GIT_SUCCESS)
return git__throw(GIT_EOSERR, "Failed to receive data"); return git__rethrow(GIT_EOSERR, "Failed to receive data");
if (ret == 0) /* Orderly shutdown, so exit */ if (error == GIT_SUCCESS) /* Orderly shutdown, so exit */
return GIT_SUCCESS; return GIT_SUCCESS;
bufflen += ret; ptr = buf.data;
ptr = buffer;
while (1) { while (1) {
if (bufflen == 0) if (buf.offset == 0)
break; 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 * If the error is GIT_ESHORTBUFFER, it means the buffer
* isn't long enough to satisfy the request. Break out and * isn't long enough to satisfy the request. Break out and
...@@ -206,13 +204,15 @@ static int store_refs(transport_git *t) ...@@ -206,13 +204,15 @@ static int store_refs(transport_git *t)
* On any other error, fail. * On any other error, fail.
*/ */
if (error == GIT_ESHORTBUFFER) { if (error == GIT_ESHORTBUFFER) {
line_end = ptr;
break; break;
} }
if (error < GIT_SUCCESS) { if (error < GIT_SUCCESS) {
return error; return error;
} }
/* Get rid of the part we've used already */
gitno_consume(&buf, line_end);
error = git_vector_insert(refs, pkt); error = git_vector_insert(refs, pkt);
if (error < GIT_SUCCESS) if (error < GIT_SUCCESS)
return error; return error;
...@@ -220,16 +220,7 @@ static int store_refs(transport_git *t) ...@@ -220,16 +220,7 @@ static int store_refs(transport_git *t)
if (pkt->type == GIT_PKT_FLUSH) if (pkt->type == GIT_PKT_FLUSH)
return GIT_SUCCESS; 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; 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