pkt-line: introduce packet_read_with_status

The current pkt-line API encodes the status of a pkt-line read in the length of the read content. An error is indicated with '-1', a flush with '0' (which can be confusing since a return value of '0' can also indicate an empty pkt-line), and a positive integer for the length of the read content otherwise. This doesn't leave much room for allowing the addition of additional special packets in the future. To solve this introduce 'packet_read_with_status()' which reads a packet and returns the status of the read encoded as an 'enum packet_status' type. This allows for easily identifying between special and normal packets as well as errors. It also enables easily adding a new special packet in the future. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Mar 14, 2018 at 11:31 UTC 2153d478b74cfef58ee49ee0305cccf5e8a77b4f
2 files changed +53 -14
pkt-line.c
+37 -14
@@ -280,28 +280,39 @@ static int packet_length(const char *linelen)
280 return (val < 0) ? val : (val << 8) | hex2chr(linelen + 2);
281 }
282
283 -int packet_read(int fd, char **src_buf, size_t *src_len,
284 - char *buffer, unsigned size, int options)
283 +enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
284 + size_t *src_len, char *buffer,
285 + unsigned size, int *pktlen,
286 + int options)
287 {
286 - int len, ret;
288 + int len;
289 char linelen[4];
290
289 - ret = get_packet_data(fd, src_buf, src_len, linelen, 4, options);
290 - if (ret < 0)
291 - return ret;
291 + if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) {
292 + *pktlen = -1;
293 + return PACKET_READ_EOF;
294 + }
295 +
296 len = packet_length(linelen);
293 - if (len < 0)
297 +
298 + if (len < 0) {
299 die("protocol error: bad line length character: %.4s", linelen);
295 - if (!len) {
300 + } else if (!len) {
301 packet_trace("0000", 4, 0);
297 - return 0;
302 + *pktlen = 0;
303 + return PACKET_READ_FLUSH;
304 + } else if (len < 4) {
305 + die("protocol error: bad line length %d", len);
306 }
307 +
308 len -= 4;
300 - if (len >= size)
309 + if ((unsigned)len >= size)
310 die("protocol error: bad line length %d", len);
302 - ret = get_packet_data(fd, src_buf, src_len, buffer, len, options);
303 - if (ret < 0)
304 - return ret;
311 +
312 + if (get_packet_data(fd, src_buffer, src_len, buffer, len, options) < 0) {
313 + *pktlen = -1;
314 + return PACKET_READ_EOF;
315 + }
316
317 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
318 len && buffer[len-1] == '\n')
@@ -309,7 +320,19 @@ int packet_read(int fd, char **src_buf, size_t *src_len,
320
321 buffer[len] = 0;
322 packet_trace(buffer, len, 0);
312 - return len;
323 + *pktlen = len;
324 + return PACKET_READ_NORMAL;
325 +}
326 +
327 +int packet_read(int fd, char **src_buffer, size_t *src_len,
328 + char *buffer, unsigned size, int options)
329 +{
330 + int pktlen = -1;
331 +
332 + packet_read_with_status(fd, src_buffer, src_len, buffer, size,
333 + &pktlen, options);
334 +
335 + return pktlen;
336 }
337
338 static char *packet_read_line_generic(int fd,
pkt-line.h
+16
@@ -65,6 +65,22 @@ int write_packetized_from_buf(const char *src_in, size_t len, int fd_out);
65 int packet_read(int fd, char **src_buffer, size_t *src_len, char
66 *buffer, unsigned size, int options);
67
68 +/*
69 + * Read a packetized line into a buffer like the 'packet_read()' function but
70 + * returns an 'enum packet_read_status' which indicates the status of the read.
71 + * The number of bytes read will be assigined to *pktlen if the status of the
72 + * read was 'PACKET_READ_NORMAL'.
73 + */
74 +enum packet_read_status {
75 + PACKET_READ_EOF,
76 + PACKET_READ_NORMAL,
77 + PACKET_READ_FLUSH,
78 +};
79 +enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
80 + size_t *src_len, char *buffer,
81 + unsigned size, int *pktlen,
82 + int options);
83 +
84 /*
85 * Convenience wrapper for packet_read that is not gentle, and sets the
86 * CHOMP_NEWLINE option. The return value is NULL for a flush packet,