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 <larsxschneider@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Lars Schneider committed
Oct 16, 2016 at 16:20 UTC
70428d1a5209f8c9996e9a4e0cc2f7aa0f83f3e8
2 files changed
+31
-4
pkt-line.c
+30
-4
@@ -125,16 +125,42 @@ static void format_packet(struct strbuf *out, const char *fmt, va_list args)
125
packet_trace(out->buf + orig_len + 4, n - 4, 1);
126
}
127
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
+
146
void packet_write_fmt(int fd, const char *fmt, ...)
147
{
130
- static struct strbuf buf = STRBUF_INIT;
148
va_list args;
149
133
- strbuf_reset(&buf);
150
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);
162
va_end(args);
137
- write_or_die(fd, buf.buf, buf.len);
163
+ return status;
164
}
165
166
void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
pkt-line.h
+1
@@ -23,6 +23,7 @@ void packet_flush(int fd);
23
void packet_write_fmt(int fd, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
24
void packet_buf_flush(struct strbuf *buf);
25
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)));
27
28
/*
29
* Read a packetized line into the buffer, which must be at least size bytes