Skip to content

Commit 78ad917

Browse files
jonathantanmygitster
authored andcommitted
remote-curl: refactor reading into rpc_state's buf
Currently, whenever remote-curl reads pkt-lines from its response file descriptor, only the payload is written to its buf, not the 4 characters denoting the length. A future patch will require the ability to also write those 4 characters, so in preparation for that, refactor this read into its own function. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b359030 commit 78ad917

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

remote-curl.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,25 @@ struct rpc_state {
520520
unsigned initial_buffer : 1;
521521
};
522522

523+
/*
524+
* Appends the result of reading from rpc->out to the string represented by
525+
* rpc->buf and rpc->len if there is enough space. Returns 1 if there was
526+
* enough space, 0 otherwise.
527+
*
528+
* Writes the number of bytes appended into appended.
529+
*/
530+
static int rpc_read_from_out(struct rpc_state *rpc, size_t *appended) {
531+
size_t left = rpc->alloc - rpc->len;
532+
char *buf = rpc->buf + rpc->len;
533+
534+
if (left < LARGE_PACKET_MAX)
535+
return 0;
536+
537+
*appended = packet_read(rpc->out, NULL, NULL, buf, left, 0);
538+
rpc->len += *appended;
539+
return 1;
540+
}
541+
523542
static size_t rpc_out(void *ptr, size_t eltsize,
524543
size_t nmemb, void *buffer_)
525544
{
@@ -529,11 +548,12 @@ static size_t rpc_out(void *ptr, size_t eltsize,
529548

530549
if (!avail) {
531550
rpc->initial_buffer = 0;
532-
avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
551+
rpc->len = 0;
552+
if (!rpc_read_from_out(rpc, &avail))
553+
BUG("The entire rpc->buf should be larger than LARGE_PACKET_DATA_MAX");
533554
if (!avail)
534555
return 0;
535556
rpc->pos = 0;
536-
rpc->len = avail;
537557
}
538558

539559
if (max < avail)
@@ -677,20 +697,15 @@ static int post_rpc(struct rpc_state *rpc)
677697
* chunked encoding mess.
678698
*/
679699
while (1) {
680-
size_t left = rpc->alloc - rpc->len;
681-
char *buf = rpc->buf + rpc->len;
682-
int n;
700+
size_t n;
683701

684-
if (left < LARGE_PACKET_MAX) {
702+
if (!rpc_read_from_out(rpc, &n)) {
685703
large_request = 1;
686704
use_gzip = 0;
687705
break;
688706
}
689-
690-
n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
691707
if (!n)
692708
break;
693-
rpc->len += n;
694709
}
695710

696711
if (large_request) {

0 commit comments

Comments
 (0)