pkt-line: add packet_buf_write_len function
Add the 'packet_buf_write_len()' function which allows for writing an arbitrary length buffer into a 'struct strbuf' and formatting it in packet-line format. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Brandon Williams committed
Mar 15, 2018 at 10:31 UTC
f1f4d8acf400912a9e2a7b97080afced56719c29
2 files changed
+17
pkt-line.c
+16
@@ -215,6 +215,22 @@ void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
215
va_end(args);
216
}
217
218
+void packet_buf_write_len(struct strbuf *buf, const char *data, size_t len)
219
+{
220
+ size_t orig_len, n;
221
+
222
+ orig_len = buf->len;
223
+ strbuf_addstr(buf, "0000");
224
+ strbuf_add(buf, data, len);
225
+ n = buf->len - orig_len;
226
+
227
+ if (n > LARGE_PACKET_MAX)
228
+ die("protocol error: impossibly long line");
229
+
230
+ set_packet_header(&buf->buf[orig_len], n);
231
+ packet_trace(data, len, 1);
232
+}
233
+
234
int write_packetized_from_fd(int fd_in, int fd_out)
235
{
236
static char buf[LARGE_PACKET_DATA_MAX];
pkt-line.h
+1
@@ -26,6 +26,7 @@ void packet_buf_flush(struct strbuf *buf);
26
void packet_buf_delim(struct strbuf *buf);
27
void packet_write(int fd_out, const char *buf, size_t size);
28
void packet_buf_write(struct strbuf *buf, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
29
+void packet_buf_write_len(struct strbuf *buf, const char *data, size_t len);
30
int packet_flush_gently(int fd);
31
int packet_write_fmt_gently(int fd, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
32
int write_packetized_from_fd(int fd_in, int fd_out);