Commit c7c787ce0cd944c0e904d47c5ef1088de2fcf85a

Carlos Martín Nieto 2011-06-24T18:19:00

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>

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;