Thread (27 messages) flat view 27 messages, 3 authors, 2016-09-13

Re: [PATCH v7 05/10] pkt-line: add packet_write_gently()

From: Stefan Beller <hidden>
Date: 2016-09-08 21:24:08

On Thu, Sep 8, 2016 at 11:21 AM,  [off-list ref] wrote:
From: Lars Schneider <redacted>

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.
Makes sense.
Add packet_write_gently() which writes arbitrary data and returns `0`
for success and `-1` for an error.
I think documenting the return code is better done in either the header file
or in a commend preceding the implementation instead of the commit message?

Maybe just a generic comment for *_gently is good enough, maybe even no
comment. So the commit is fine, too. I dunno.

This function is used by other
pkt-line functions in a subsequent patch.
That's what I figured. Do we also need to mention that in the preceding patch
for packet_flush_gently ?
quoted hunk ↗ jump to hunk
Signed-off-by: Lars Schneider <redacted>
---
 pkt-line.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
diff --git a/pkt-line.c b/pkt-line.c
index 37345ca..1d3d725 100644
--- a/pkt-line.c
+++ b/pkt-line.c
@@ -181,6 +181,25 @@ int packet_write_fmt_gently(int fd, const char *fmt, ...)
        return status;
 }

+int packet_write_gently(const int fd_out, const char *buf, size_t size)
+{
+       static char packet_write_buffer[LARGE_PACKET_MAX];
+
+       if (size > sizeof(packet_write_buffer) - 4) {
+               error("packet write failed");
+               return -1;
+       }
+       packet_trace(buf, size, 1);
+       size += 4;
+       set_packet_header(packet_write_buffer, size);
+       memcpy(packet_write_buffer + 4, buf, size - 4);
+       if (write_in_full(fd_out, packet_write_buffer, size) == size)
+               return 0;
+
+       error("packet write failed");
+       return -1;
+}
+
 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
 {
        va_list args;
--
2.10.0
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help