archive-tar: factor out write_block()

All tar archive writes have the same size and are done to the same file descriptor. Move them to a common function, write_block(), to reduce code duplication and make it easy to change the destination. Original-patch-by: Rohit Ashiwal <rohit.ashiwal265@gmail.com> Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jun 15, 2022 at 19:01 UTC dfce1186c6034d6f4ea283f5178fd25cbd8f4fc0
1 file changed +9 -4
archive-tar.c
+9 -4
@@ -38,11 +38,16 @@ static int write_tar_filter_archive(const struct archiver *ar,
38 #define USTAR_MAX_MTIME 077777777777ULL
39 #endif
40
41 +static void write_block(const void *buf)
42 +{
43 + write_or_die(1, buf, BLOCKSIZE);
44 +}
45 +
46 /* writes out the whole block, but only if it is full */
47 static void write_if_needed(void)
48 {
49 if (offset == BLOCKSIZE) {
45 - write_or_die(1, block, BLOCKSIZE);
50 + write_block(block);
51 offset = 0;
52 }
53 }
@@ -66,7 +71,7 @@ static void do_write_blocked(const void *data, unsigned long size)
71 write_if_needed();
72 }
73 while (size >= BLOCKSIZE) {
69 - write_or_die(1, buf, BLOCKSIZE);
74 + write_block(buf);
75 size -= BLOCKSIZE;
76 buf += BLOCKSIZE;
77 }
@@ -101,10 +106,10 @@ static void write_trailer(void)
106 {
107 int tail = BLOCKSIZE - offset;
108 memset(block + offset, 0, tail);
104 - write_or_die(1, block, BLOCKSIZE);
109 + write_block(block);
110 if (tail < 2 * RECORDSIZE) {
111 memset(block, 0, offset);
107 - write_or_die(1, block, BLOCKSIZE);
112 + write_block(block);
113 }
114 }
115