Skip to content

Commit f1f4d8a

Browse files
bmwillgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent edc9caf commit f1f4d8a

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

pkt-line.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,22 @@ void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
215215
va_end(args);
216216
}
217217

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+
218234
int write_packetized_from_fd(int fd_in, int fd_out)
219235
{
220236
static char buf[LARGE_PACKET_DATA_MAX];

pkt-line.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void packet_buf_flush(struct strbuf *buf);
2626
void packet_buf_delim(struct strbuf *buf);
2727
void packet_write(int fd_out, const char *buf, size_t size);
2828
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);
2930
int packet_flush_gently(int fd);
3031
int packet_write_fmt_gently(int fd, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
3132
int write_packetized_from_fd(int fd_in, int fd_out);

0 commit comments

Comments
 (0)