Skip to content

Commit f0bca72

Browse files
peffgitster
authored andcommitted
send-pack: use buffered I/O to talk to pack-objects
We start a pack-objects process and then write all of the positive and negative sha1s to it over a pipe. We do so by formatting each item into a fixed-size buffer and then writing each individually. This has two drawbacks: 1. There's some manual computation of the buffer size, which is not immediately obvious is correct (though it is). 2. We write() once per sha1, which means a lot more system calls than are necessary. We can solve both by wrapping the pipe descriptor in a stdio handle; this is the same technique used by upload-pack when serving fetches. Note that we can also simplify and improve the error handling here. The original detected a single write error and broke out of the loop (presumably to avoid writing the error message over and over), but never actually acted on seeing an error; we just fed truncated input and took whatever pack-objects returned. In practice, this probably didn't matter, as the likely errors would be caused by pack-objects dying (and we'd probably just die with SIGPIPE anyway). But we can easily make this simpler and more robust; the stdio handle keeps an error flag, which we can check at the end. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0b65a8d commit f0bca72

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

send-pack.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,15 @@ int option_parse_push_signed(const struct option *opt,
3636
die("bad %s argument: %s", opt->long_name, arg);
3737
}
3838

39-
static int feed_object(const unsigned char *sha1, int fd, int negative)
39+
static void feed_object(const unsigned char *sha1, FILE *fh, int negative)
4040
{
41-
char buf[42];
42-
4341
if (negative && !has_sha1_file(sha1))
44-
return 1;
42+
return;
4543

46-
memcpy(buf + negative, sha1_to_hex(sha1), 40);
4744
if (negative)
48-
buf[0] = '^';
49-
buf[40 + negative] = '\n';
50-
return write_or_whine(fd, buf, 41 + negative, "send-pack: send refs");
45+
putc('^', fh);
46+
fputs(sha1_to_hex(sha1), fh);
47+
putc('\n', fh);
5148
}
5249

5350
/*
@@ -73,6 +70,7 @@ static int pack_objects(int fd, struct ref *refs, struct sha1_array *extra, stru
7370
NULL,
7471
};
7572
struct child_process po = CHILD_PROCESS_INIT;
73+
FILE *po_in;
7674
int i;
7775

7876
i = 4;
@@ -97,21 +95,22 @@ static int pack_objects(int fd, struct ref *refs, struct sha1_array *extra, stru
9795
* We feed the pack-objects we just spawned with revision
9896
* parameters by writing to the pipe.
9997
*/
98+
po_in = xfdopen(po.in, "w");
10099
for (i = 0; i < extra->nr; i++)
101-
if (!feed_object(extra->sha1[i], po.in, 1))
102-
break;
100+
feed_object(extra->sha1[i], po_in, 1);
103101

104102
while (refs) {
105-
if (!is_null_oid(&refs->old_oid) &&
106-
!feed_object(refs->old_oid.hash, po.in, 1))
107-
break;
108-
if (!is_null_oid(&refs->new_oid) &&
109-
!feed_object(refs->new_oid.hash, po.in, 0))
110-
break;
103+
if (!is_null_oid(&refs->old_oid))
104+
feed_object(refs->old_oid.hash, po_in, 1);
105+
if (!is_null_oid(&refs->new_oid))
106+
feed_object(refs->new_oid.hash, po_in, 0);
111107
refs = refs->next;
112108
}
113109

114-
close(po.in);
110+
fflush(po_in);
111+
if (ferror(po_in))
112+
die_errno("error writing to pack-objects");
113+
fclose(po_in);
115114

116115
if (args->stateless_rpc) {
117116
char *buf = xmalloc(LARGE_PACKET_MAX);

0 commit comments

Comments
 (0)