pkt-line: add packet_read_line_gently()
Add packet_read_line_gently() to enable reading a line without dying on EOF. Signed-off-by: Ben Peart <benpeart@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ben Peart committed
May 5, 2017 at 11:27 UTC
825b9226bf7a962bc76571319b1444bc02fdcdf8
2 files changed
+23
pkt-line.c
+12
@@ -323,6 +323,18 @@ char *packet_read_line(int fd, int *len_p)
323
return packet_read_line_generic(fd, NULL, NULL, len_p);
324
}
325
326
+int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
327
+{
328
+ int len = packet_read(fd, NULL, NULL,
329
+ packet_buffer, sizeof(packet_buffer),
330
+ PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
331
+ if (dst_len)
332
+ *dst_len = len;
333
+ if (dst_line)
334
+ *dst_line = (len > 0) ? packet_buffer : NULL;
335
+ return len;
336
+}
337
+
338
char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
339
{
340
return packet_read_line_generic(-1, src, src_len, dst_len);
pkt-line.h
+11
@@ -73,6 +73,17 @@ int packet_read(int fd, char **src_buffer, size_t *src_len, char
73
*/
74
char *packet_read_line(int fd, int *size);
75
76
+/*
77
+ * Convenience wrapper for packet_read that sets the PACKET_READ_GENTLE_ON_EOF
78
+ * and CHOMP_NEWLINE options. The return value specifies the number of bytes
79
+ * read into the buffer or -1 on truncated input. If the *dst_line parameter
80
+ * is not NULL it will return NULL for a flush packet or when the number of
81
+ * bytes copied is zero and otherwise points to a static buffer (that may be
82
+ * overwritten by subsequent calls). If the size parameter is not NULL, the
83
+ * length of the packet is written to it.
84
+ */
85
+int packet_read_line_gently(int fd, int *size, char **dst_line);
86
+
87
/*
88
* Same as packet_read_line, but read from a buf rather than a descriptor;
89
* see packet_read for details on how src_* is used.