Skip to content

Commit 70428d1

Browse files
larsxschneidergitster
authored andcommitted
pkt-line: add packet_write_fmt_gently()
packet_write_fmt() would die in case of a write error even though for some callers an error would be acceptable. Add packet_write_fmt_gently() which writes a formatted pkt-line like packet_write_fmt() but does not die in case of an error. The function is used in a subsequent patch. Signed-off-by: Lars Schneider <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2f60bdd commit 70428d1

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

pkt-line.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,42 @@ static void format_packet(struct strbuf *out, const char *fmt, va_list args)
125125
packet_trace(out->buf + orig_len + 4, n - 4, 1);
126126
}
127127

128+
static int packet_write_fmt_1(int fd, int gently,
129+
const char *fmt, va_list args)
130+
{
131+
struct strbuf buf = STRBUF_INIT;
132+
ssize_t count;
133+
134+
format_packet(&buf, fmt, args);
135+
count = write_in_full(fd, buf.buf, buf.len);
136+
if (count == buf.len)
137+
return 0;
138+
139+
if (!gently) {
140+
check_pipe(errno);
141+
die_errno("packet write with format failed");
142+
}
143+
return error("packet write with format failed");
144+
}
145+
128146
void packet_write_fmt(int fd, const char *fmt, ...)
129147
{
130-
static struct strbuf buf = STRBUF_INIT;
131148
va_list args;
132149

133-
strbuf_reset(&buf);
134150
va_start(args, fmt);
135-
format_packet(&buf, fmt, args);
151+
packet_write_fmt_1(fd, 0, fmt, args);
152+
va_end(args);
153+
}
154+
155+
int packet_write_fmt_gently(int fd, const char *fmt, ...)
156+
{
157+
int status;
158+
va_list args;
159+
160+
va_start(args, fmt);
161+
status = packet_write_fmt_1(fd, 1, fmt, args);
136162
va_end(args);
137-
write_or_die(fd, buf.buf, buf.len);
163+
return status;
138164
}
139165

140166
void packet_buf_write(struct strbuf *buf, const char *fmt, ...)

pkt-line.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ void packet_flush(int fd);
2323
void packet_write_fmt(int fd, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
2424
void packet_buf_flush(struct strbuf *buf);
2525
void packet_buf_write(struct strbuf *buf, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
26+
int packet_write_fmt_gently(int fd, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
2627

2728
/*
2829
* Read a packetized line into the buffer, which must be at least size bytes

0 commit comments

Comments
 (0)