Skip to content

Commit d4636ae

Browse files
committed
Merge branch 'jc/xwrite-cleanup'
Uses of xwrite() helper have been audited and updated for better error checking and simpler code. * jc/xwrite-cleanup: repack: check error writing to pack-objects subprocess sideband: avoid short write(2) unpack: replace xwrite() loop with write_in_full()
2 parents 06ac518 + 4c9355f commit d4636ae

File tree

4 files changed

+9
-25
lines changed

4 files changed

+9
-25
lines changed

builtin/index-pack.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,14 +1524,12 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
15241524
struct strbuf pack_name = STRBUF_INIT;
15251525
struct strbuf index_name = STRBUF_INIT;
15261526
struct strbuf rev_index_name = STRBUF_INIT;
1527-
int err;
15281527

15291528
if (!from_stdin) {
15301529
close(input_fd);
15311530
} else {
15321531
fsync_component_or_die(FSYNC_COMPONENT_PACK, output_fd, curr_pack_name);
1533-
err = close(output_fd);
1534-
if (err)
1532+
if (close(output_fd))
15351533
die_errno(_("error while closing pack file"));
15361534
}
15371535

@@ -1566,17 +1564,8 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
15661564
write_or_die(1, buf.buf, buf.len);
15671565
strbuf_release(&buf);
15681566

1569-
/*
1570-
* Let's just mimic git-unpack-objects here and write
1571-
* the last part of the input buffer to stdout.
1572-
*/
1573-
while (input_len) {
1574-
err = xwrite(1, input_buffer + input_offset, input_len);
1575-
if (err <= 0)
1576-
break;
1577-
input_len -= err;
1578-
input_offset += err;
1579-
}
1567+
/* Write the last part of the buffer to stdout */
1568+
write_in_full(1, input_buffer + input_offset, input_len);
15801569
}
15811570

15821571
strbuf_release(&rev_index_name);

builtin/repack.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,9 @@ static int write_oid(const struct object_id *oid,
314314
die(_("could not start pack-objects to repack promisor objects"));
315315
}
316316

317-
xwrite(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz);
318-
xwrite(cmd->in, "\n", 1);
317+
if (write_in_full(cmd->in, oid_to_hex(oid), the_hash_algo->hexsz) < 0 ||
318+
write_in_full(cmd->in, "\n", 1) < 0)
319+
die(_("failed to feed promisor objects to pack-objects"));
319320
return 0;
320321
}
321322

builtin/unpack-objects.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -679,13 +679,7 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix UNUSED)
679679
use(the_hash_algo->rawsz);
680680

681681
/* Write the last part of the buffer to stdout */
682-
while (len) {
683-
int ret = xwrite(1, buffer + offset, len);
684-
if (ret <= 0)
685-
break;
686-
len -= ret;
687-
offset += ret;
688-
}
682+
write_in_full(1, buffer + offset, len);
689683

690684
/* All done */
691685
return has_errors;

sideband.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ int demultiplex_sideband(const char *me, int status,
220220
}
221221

222222
strbuf_addch(scratch, *brk);
223-
xwrite(2, scratch->buf, scratch->len);
223+
write_in_full(2, scratch->buf, scratch->len);
224224
strbuf_reset(scratch);
225225

226226
b = brk + 1;
@@ -247,7 +247,7 @@ int demultiplex_sideband(const char *me, int status,
247247
die("%s", scratch->buf);
248248
if (scratch->len) {
249249
strbuf_addch(scratch, '\n');
250-
xwrite(2, scratch->buf, scratch->len);
250+
write_in_full(2, scratch->buf, scratch->len);
251251
}
252252
strbuf_release(scratch);
253253
return 1;

0 commit comments

Comments
 (0)