Skip to content

Commit edfb780

Browse files
larsxschneidergitster
authored andcommitted
pkt-line: add packet_write_gently()
packet_write_fmt_gently() uses format_packet() which lets the caller only send string data via "%s". That means it cannot be used for arbitrary data that may contain NULs. Add packet_write_gently() which writes arbitrary data and does not die in case of an error. The function is used by other pkt-line functions in a subsequent patch. Signed-off-by: Lars Schneider <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 038ce90 commit edfb780

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

pkt-line.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,23 @@ int packet_write_fmt_gently(int fd, const char *fmt, ...)
171171
return status;
172172
}
173173

174+
static int packet_write_gently(const int fd_out, const char *buf, size_t size)
175+
{
176+
static char packet_write_buffer[LARGE_PACKET_MAX];
177+
size_t packet_size;
178+
179+
if (size > sizeof(packet_write_buffer) - 4)
180+
return error("packet write failed - data exceeds max packet size");
181+
182+
packet_trace(buf, size, 1);
183+
packet_size = size + 4;
184+
set_packet_header(packet_write_buffer, packet_size);
185+
memcpy(packet_write_buffer + 4, buf, size);
186+
if (write_in_full(fd_out, packet_write_buffer, packet_size) == packet_size)
187+
return 0;
188+
return error("packet write failed");
189+
}
190+
174191
void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
175192
{
176193
va_list args;

0 commit comments

Comments
 (0)