pkt-line: add stdio packet write functions

This adds three new functions to pkt-line.c: packet_fwrite, packet_fwrite_fmt and packet_fflush. Besides writing a pktline flush packet, packet_fflush also flushes the stdio buffer of the stream. Helped-by: Patrick Steinhardt <ps@pks.im> Helped-by: Jeff King <peff@peff.net> Signed-off-by: Jacob Vosmaer <jacob@gitlab.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jacob Vosmaer committed Sep 1, 2021 at 14:54 UTC 96328398b3803c96f6a91edc39ccebd4bb9dd12c
4 files changed +62
cache.h
+2
@@ -1738,6 +1738,8 @@ extern const char *git_mailmap_blob;
1738 void maybe_flush_or_die(FILE *, const char *);
1739 __attribute__((format (printf, 2, 3)))
1740 void fprintf_or_die(FILE *, const char *fmt, ...);
1741 +void fwrite_or_die(FILE *f, const void *buf, size_t count);
1742 +void fflush_or_die(FILE *f);
1743
1744 #define COPY_READ_ERROR (-2)
1745 #define COPY_WRITE_ERROR (-3)
pkt-line.c
+37
@@ -243,6 +243,43 @@ void packet_write(int fd_out, const char *buf, size_t size)
243 die("%s", err.buf);
244 }
245
246 +void packet_fwrite(FILE *f, const char *buf, size_t size)
247 +{
248 + size_t packet_size;
249 + char header[4];
250 +
251 + if (size > LARGE_PACKET_DATA_MAX)
252 + die(_("packet write failed - data exceeds max packet size"));
253 +
254 + packet_trace(buf, size, 1);
255 + packet_size = size + 4;
256 +
257 + set_packet_header(header, packet_size);
258 + fwrite_or_die(f, header, 4);
259 + fwrite_or_die(f, buf, size);
260 +}
261 +
262 +void packet_fwrite_fmt(FILE *fh, const char *fmt, ...)
263 +{
264 + static struct strbuf buf = STRBUF_INIT;
265 + va_list args;
266 +
267 + strbuf_reset(&buf);
268 +
269 + va_start(args, fmt);
270 + format_packet(&buf, "", fmt, args);
271 + va_end(args);
272 +
273 + fwrite_or_die(fh, buf.buf, buf.len);
274 +}
275 +
276 +void packet_fflush(FILE *f)
277 +{
278 + packet_trace("0000", 4, 1);
279 + fwrite_or_die(f, "0000", 4);
280 + fflush_or_die(f);
281 +}
282 +
283 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
284 {
285 va_list args;
pkt-line.h
+11
@@ -35,6 +35,17 @@ int packet_write_fmt_gently(int fd, const char *fmt, ...) __attribute__((format
35 int write_packetized_from_fd_no_flush(int fd_in, int fd_out);
36 int write_packetized_from_buf_no_flush(const char *src_in, size_t len, int fd_out);
37
38 +/*
39 + * Stdio versions of packet_write functions. When mixing these with fd
40 + * based functions, take care to call fflush(3) before doing fd writes or
41 + * closing the fd.
42 + */
43 +void packet_fwrite(FILE *f, const char *buf, size_t size);
44 +void packet_fwrite_fmt(FILE *f, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
45 +
46 +/* packet_fflush writes a flush packet and flushes the stdio buffer of f */
47 +void packet_fflush(FILE *f);
48 +
49 /*
50 * Read a packetized line into the buffer, which must be at least size bytes
51 * long. The return value specifies the number of bytes read into the buffer.
write-or-die.c
+12
@@ -70,3 +70,15 @@ void write_or_die(int fd, const void *buf, size_t count)
70 die_errno("write error");
71 }
72 }
73 +
74 +void fwrite_or_die(FILE *f, const void *buf, size_t count)
75 +{
76 + if (fwrite(buf, 1, count, f) != count)
77 + die_errno("fwrite error");
78 +}
79 +
80 +void fflush_or_die(FILE *f)
81 +{
82 + if (fflush(f))
83 + die_errno("fflush error");
84 +}