Skip to content

Commit 2153d47

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

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

pkt-line.c

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -280,36 +280,59 @@ static int packet_length(const char *linelen)
280280
return (val < 0) ? val : (val << 8) | hex2chr(linelen + 2);
281281
}
282282

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)
285287
{
286-
int len, ret;
288+
int len;
287289
char linelen[4];
288290

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+
292296
len = packet_length(linelen);
293-
if (len < 0)
297+
298+
if (len < 0) {
294299
die("protocol error: bad line length character: %.4s", linelen);
295-
if (!len) {
300+
} else if (!len) {
296301
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);
298306
}
307+
299308
len -= 4;
300-
if (len >= size)
309+
if ((unsigned)len >= size)
301310
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+
}
305316

306317
if ((options & PACKET_READ_CHOMP_NEWLINE) &&
307318
len && buffer[len-1] == '\n')
308319
len--;
309320

310321
buffer[len] = 0;
311322
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;
313336
}
314337

315338
static char *packet_read_line_generic(int fd,

pkt-line.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ int write_packetized_from_buf(const char *src_in, size_t len, int fd_out);
6565
int packet_read(int fd, char **src_buffer, size_t *src_len, char
6666
*buffer, unsigned size, int options);
6767

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+
6884
/*
6985
* Convenience wrapper for packet_read that is not gentle, and sets the
7086
* CHOMP_NEWLINE option. The return value is NULL for a flush packet,

0 commit comments

Comments
 (0)