Skip to content

Commit 9632839

Browse files
jacobvosmaergitster
authored andcommitted
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 <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Jacob Vosmaer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c420321 commit 9632839

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,8 @@ extern const char *git_mailmap_blob;
17381738
void maybe_flush_or_die(FILE *, const char *);
17391739
__attribute__((format (printf, 2, 3)))
17401740
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);
17411743

17421744
#define COPY_READ_ERROR (-2)
17431745
#define COPY_WRITE_ERROR (-3)

pkt-line.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,43 @@ void packet_write(int fd_out, const char *buf, size_t size)
243243
die("%s", err.buf);
244244
}
245245

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+
246283
void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
247284
{
248285
va_list args;

pkt-line.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ int packet_write_fmt_gently(int fd, const char *fmt, ...) __attribute__((format
3535
int write_packetized_from_fd_no_flush(int fd_in, int fd_out);
3636
int write_packetized_from_buf_no_flush(const char *src_in, size_t len, int fd_out);
3737

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+
3849
/*
3950
* Read a packetized line into the buffer, which must be at least size bytes
4051
* long. The return value specifies the number of bytes read into the buffer.

write-or-die.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,15 @@ void write_or_die(int fd, const void *buf, size_t count)
7070
die_errno("write error");
7171
}
7272
}
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+
}

0 commit comments

Comments
 (0)