unpack: replace xwrite() loop with write_in_full()
We have two packfile stream consumers, index-pack and unpack-objects, that allow excess payload after the packfile stream data. Their code to relay excess data hasn't changed significantly since their original implementation that appeared in 67e5a5ec (git-unpack-objects: re-write to read from stdin, 2005-06-28) and 9bee2478 (mimic unpack-objects when --stdin is used with index-pack, 2006-10-25). These code blocks contain hand-rolled loops using xwrite(), written before our write_in_full() helper existed. This helper now provides the same functionality. Replace these loops with write_in_full() for shorter, clearer code. Update related variables accordingly. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Mar 2, 2024 at 11:03 UTC
fa6c383309557b9d2942c47b75a895ca960ad9f5
2 files changed
+4
-21
builtin/index-pack.c
+3
-14
@@ -1524,14 +1524,12 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
1524
struct strbuf pack_name = STRBUF_INIT;
1525
struct strbuf index_name = STRBUF_INIT;
1526
struct strbuf rev_index_name = STRBUF_INIT;
1527
- int err;
1527
1528
if (!from_stdin) {
1529
close(input_fd);
1530
} else {
1531
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))
1533
die_errno(_("error while closing pack file"));
1534
}
1535
@@ -1566,17 +1564,8 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
1564
write_or_die(1, buf.buf, buf.len);
1565
strbuf_release(&buf);
1566
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);
1569
}
1570
1571
strbuf_release(&rev_index_name);
builtin/unpack-objects.c
+1
-7
@@ -679,13 +679,7 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix UNUSED)
679
use(the_hash_algo->rawsz);
680
681
/* 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);
683
684
/* All done */
685
return has_errors;