Skip to content

Commit 24b75fa

Browse files
rscharfegitster
authored andcommitted
connected: use buffered I/O to talk to rev-list
Like f0bca72 (send-pack: use buffered I/O to talk to pack-objects, 2016-06-08), significantly reduce the number of system calls and simplify the code for sending object IDs to rev-list by using stdio's buffering. Take care to handle errors immediately to get the correct error code, and to flush the buffer explicitly before closing the stream in order to catch any write errors for these last bytes. Helped-by: Chris Torek <[email protected]> Helped-by: Johannes Sixt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 47ae905 commit 24b75fa

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

connected.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ int check_connected(oid_iterate_fn fn, void *cb_data,
2222
struct check_connected_options *opt)
2323
{
2424
struct child_process rev_list = CHILD_PROCESS_INIT;
25+
FILE *rev_list_in;
2526
struct check_connected_options defaults = CHECK_CONNECTED_INIT;
26-
char commit[GIT_MAX_HEXSZ + 1];
2727
struct object_id oid;
2828
int err = 0;
2929
struct packed_git *new_pack = NULL;
3030
struct transport *transport;
3131
size_t base_len;
32-
const unsigned hexsz = the_hash_algo->hexsz;
3332

3433
if (!opt)
3534
opt = &defaults;
@@ -122,7 +121,8 @@ int check_connected(oid_iterate_fn fn, void *cb_data,
122121

123122
sigchain_push(SIGPIPE, SIG_IGN);
124123

125-
commit[hexsz] = '\n';
124+
rev_list_in = xfdopen(rev_list.in, "w");
125+
126126
do {
127127
/*
128128
* If index-pack already checked that:
@@ -135,16 +135,17 @@ int check_connected(oid_iterate_fn fn, void *cb_data,
135135
if (new_pack && find_pack_entry_one(oid.hash, new_pack))
136136
continue;
137137

138-
memcpy(commit, oid_to_hex(&oid), hexsz);
139-
if (write_in_full(rev_list.in, commit, hexsz + 1) < 0) {
140-
if (errno != EPIPE && errno != EINVAL)
141-
error_errno(_("failed write to rev-list"));
142-
err = -1;
138+
if (fprintf(rev_list_in, "%s\n", oid_to_hex(&oid)) < 0)
143139
break;
144-
}
145140
} while (!fn(cb_data, &oid));
146141

147-
if (close(rev_list.in))
142+
if (ferror(rev_list_in) || fflush(rev_list_in)) {
143+
if (errno != EPIPE && errno != EINVAL)
144+
error_errno(_("failed write to rev-list"));
145+
err = -1;
146+
}
147+
148+
if (fclose(rev_list_in))
148149
err = error_errno(_("failed to close rev-list's stdin"));
149150

150151
sigchain_pop(SIGPIPE);

0 commit comments

Comments
 (0)