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>
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
diff --git a/src/netops.c b/src/netops.c
index 6c164a9..613226d 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -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 */
diff --git a/src/netops.h b/src/netops.h
index 9c52798..c828ed9 100644
--- a/src/netops.h
+++ b/src/netops.h
@@ -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);
diff --git a/src/pkt.c b/src/pkt.c
index eb51fe9..f9ba8d0 100644
--- a/src/pkt.c
+++ b/src/pkt.c
@@ -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;
diff --git a/src/transport_git.c b/src/transport_git.c
index ca1f044..ad982ef 100644
--- a/src/transport_git.c
+++ b/src/transport_git.c
@@ -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;