Skip to content

Commit 0380942

Browse files
peffgitster
authored andcommitted
pkt-line: provide a generic reading function with options
Originally we had a single function for reading packetized data: packet_read_line. Commit 46284dd grew a more "gentle" form, packet_read, that returns an error instead of dying upon reading a truncated input stream. However, it is not clear from the names which should be called, or what the difference is. Let's instead make packet_read be a generic public interface that can take option flags, and update the single callsite that uses it. This is less code, more clear, and paves the way for introducing more options into the generic interface later. The function signature is changed, so there should be no hidden conflicts with topics in flight. While we're at it, we'll document how error conditions are handled based on the options, and rename the confusing "return_line_fail" option to "gentle_on_eof". While we are cleaning up the names, we can drop the "return_line_fail" checks in packet_read_internal entirely. They look like this: ret = safe_read(..., return_line_fail); if (return_line_fail && ret < 0) ... The check for return_line_fail is a no-op; safe_read will only ever return an error value if return_line_fail was true in the first place. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent cdf4fb8 commit 0380942

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

connect.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ struct ref **get_remote_heads(int in, struct ref **list,
7676
char *name;
7777
int len, name_len;
7878

79-
len = packet_read(in, buffer, sizeof(buffer));
79+
len = packet_read(in, buffer, sizeof(buffer),
80+
PACKET_READ_GENTLE_ON_EOF);
8081
if (len < 0)
8182
die_initial_contact(got_at_least_one_head);
8283

pkt-line.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
103103
strbuf_add(buf, buffer, n);
104104
}
105105

106-
static int safe_read(int fd, void *buffer, unsigned size, int return_line_fail)
106+
static int safe_read(int fd, void *buffer, unsigned size, int options)
107107
{
108108
ssize_t ret = read_in_full(fd, buffer, size);
109109
if (ret < 0)
110110
die_errno("read error");
111111
else if (ret < size) {
112-
if (return_line_fail)
112+
if (options & PACKET_READ_GENTLE_ON_EOF)
113113
return -1;
114114

115115
die("The remote end hung up unexpectedly");
@@ -143,13 +143,13 @@ static int packet_length(const char *linelen)
143143
return len;
144144
}
145145

146-
static int packet_read_internal(int fd, char *buffer, unsigned size, int return_line_fail)
146+
int packet_read(int fd, char *buffer, unsigned size, int options)
147147
{
148148
int len, ret;
149149
char linelen[4];
150150

151-
ret = safe_read(fd, linelen, 4, return_line_fail);
152-
if (return_line_fail && ret < 0)
151+
ret = safe_read(fd, linelen, 4, options);
152+
if (ret < 0)
153153
return ret;
154154
len = packet_length(linelen);
155155
if (len < 0)
@@ -161,22 +161,17 @@ static int packet_read_internal(int fd, char *buffer, unsigned size, int return_
161161
len -= 4;
162162
if (len >= size)
163163
die("protocol error: bad line length %d", len);
164-
ret = safe_read(fd, buffer, len, return_line_fail);
165-
if (return_line_fail && ret < 0)
164+
ret = safe_read(fd, buffer, len, options);
165+
if (ret < 0)
166166
return ret;
167167
buffer[len] = 0;
168168
packet_trace(buffer, len, 0);
169169
return len;
170170
}
171171

172-
int packet_read(int fd, char *buffer, unsigned size)
173-
{
174-
return packet_read_internal(fd, buffer, size, 1);
175-
}
176-
177172
int packet_read_line(int fd, char *buffer, unsigned size)
178173
{
179-
return packet_read_internal(fd, buffer, size, 0);
174+
return packet_read(fd, buffer, size, 0);
180175
}
181176

182177
int packet_get_line(struct strbuf *out,

pkt-line.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,33 @@ void packet_write(int fd, const char *fmt, ...) __attribute__((format (printf, 2
2424
void packet_buf_flush(struct strbuf *buf);
2525
void packet_buf_write(struct strbuf *buf, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
2626

27+
/*
28+
* Read a packetized line from the descriptor into the buffer, which must be at
29+
* least size bytes long. The return value specifies the number of bytes read
30+
* into the buffer.
31+
*
32+
* If options does not contain PACKET_READ_GENTLE_ON_EOF, we will die under any
33+
* of the following conditions:
34+
*
35+
* 1. Read error from descriptor.
36+
*
37+
* 2. Protocol error from the remote (e.g., bogus length characters).
38+
*
39+
* 3. Receiving a packet larger than "size" bytes.
40+
*
41+
* 4. Truncated output from the remote (e.g., we expected a packet but got
42+
* EOF, or we got a partial packet followed by EOF).
43+
*
44+
* If options does contain PACKET_READ_GENTLE_ON_EOF, we will not die on
45+
* condition 4 (truncated input), but instead return -1. However, we will still
46+
* die for the other 3 conditions.
47+
*/
48+
#define PACKET_READ_GENTLE_ON_EOF (1u<<0)
49+
int packet_read(int fd, char *buffer, unsigned size, int options);
50+
51+
/* Historical convenience wrapper for packet_read that sets no options */
2752
int packet_read_line(int fd, char *buffer, unsigned size);
28-
int packet_read(int fd, char *buffer, unsigned size);
53+
2954
int packet_get_line(struct strbuf *out, char **src_buf, size_t *src_len);
3055

3156
#endif

0 commit comments

Comments
 (0)