pkt-line: allow peeking a packet line without consuming it
[git/git.git] / pkt-line.c
CommitLineData
f3a3214e
LT
1#include "cache.h"
2#include "pkt-line.h"
fd89433d 3#include "run-command.h"
f3a3214e 4
74543a04 5char packet_buffer[LARGE_PACKET_MAX];
1e4cd68c 6static const char *packet_trace_prefix = "git";
6aa30857 7static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET);
32359838 8static struct trace_key trace_pack = TRACE_KEY_INIT(PACKFILE);
bbc30f99
JK
9
10void packet_trace_identity(const char *prog)
11{
12 packet_trace_prefix = xstrdup(prog);
13}
14
fd89433d
JK
15static const char *get_trace_prefix(void)
16{
17 return in_async() ? "sideband" : packet_trace_prefix;
18}
19
32359838
JK
20static int packet_trace_pack(const char *buf, unsigned int len, int sideband)
21{
22 if (!sideband) {
23 trace_verbatim(&trace_pack, buf, len);
24 return 1;
25 } else if (len && *buf == '\1') {
26 trace_verbatim(&trace_pack, buf + 1, len - 1);
27 return 1;
28 } else {
29 /* it's another non-pack sideband */
30 return 0;
31 }
32}
33
bbc30f99
JK
34static void packet_trace(const char *buf, unsigned int len, int write)
35{
36 int i;
37 struct strbuf out;
32359838
JK
38 static int in_pack, sideband;
39
40 if (!trace_want(&trace_packet) && !trace_want(&trace_pack))
41 return;
42
43 if (in_pack) {
44 if (packet_trace_pack(buf, len, sideband))
45 return;
46 } else if (starts_with(buf, "PACK") || starts_with(buf, "\1PACK")) {
47 in_pack = 1;
48 sideband = *buf == '\1';
49 packet_trace_pack(buf, len, sideband);
50
51 /*
52 * Make a note in the human-readable trace that the pack data
53 * started.
54 */
55 buf = "PACK ...";
56 len = strlen(buf);
57 }
bbc30f99 58
6aa30857 59 if (!trace_want(&trace_packet))
bbc30f99
JK
60 return;
61
62 /* +32 is just a guess for header + quoting */
63 strbuf_init(&out, len+32);
64
65 strbuf_addf(&out, "packet: %12s%c ",
fd89433d 66 get_trace_prefix(), write ? '>' : '<');
bbc30f99 67
32359838
JK
68 /* XXX we should really handle printable utf8 */
69 for (i = 0; i < len; i++) {
70 /* suppress newlines */
71 if (buf[i] == '\n')
72 continue;
73 if (buf[i] >= 0x20 && buf[i] <= 0x7e)
74 strbuf_addch(&out, buf[i]);
75 else
76 strbuf_addf(&out, "\\%o", buf[i]);
bbc30f99
JK
77 }
78
79 strbuf_addch(&out, '\n');
6aa30857 80 trace_strbuf(&trace_packet, &out);
bbc30f99
JK
81 strbuf_release(&out);
82}
83
f3a3214e
LT
84/*
85 * If we buffered things up above (we don't, but we should),
86 * we'd flush it here
87 */
88void packet_flush(int fd)
89{
bbc30f99 90 packet_trace("0000", 4, 1);
cdf4fb8e 91 write_or_die(fd, "0000", 4);
f3a3214e
LT
92}
93
038ce90f
LS
94int packet_flush_gently(int fd)
95{
96 packet_trace("0000", 4, 1);
4c95e3dd
JK
97 if (write_in_full(fd, "0000", 4) < 0)
98 return error("flush packet write failed");
99 return 0;
038ce90f
LS
100}
101
f5615d24
SP
102void packet_buf_flush(struct strbuf *buf)
103{
bbc30f99 104 packet_trace("0000", 4, 1);
f5615d24
SP
105 strbuf_add(buf, "0000", 4);
106}
107
2f60bdd1 108static void set_packet_header(char *buf, const int size)
f3a3214e 109{
f3a3214e 110 static char hexchar[] = "0123456789abcdef";
2f60bdd1
LS
111
112 #define hex(a) (hexchar[(a) & 15])
113 buf[0] = hex(size >> 12);
114 buf[1] = hex(size >> 8);
115 buf[2] = hex(size >> 4);
116 buf[3] = hex(size);
117 #undef hex
118}
119
120static void format_packet(struct strbuf *out, const char *fmt, va_list args)
121{
8e9faf27 122 size_t orig_len, n;
f3a3214e 123
8e9faf27
JK
124 orig_len = out->len;
125 strbuf_addstr(out, "0000");
126 strbuf_vaddf(out, fmt, args);
127 n = out->len - orig_len;
128
129 if (n > LARGE_PACKET_MAX)
f3a3214e 130 die("protocol error: impossibly long line");
8e9faf27 131
2f60bdd1 132 set_packet_header(&out->buf[orig_len], n);
8e9faf27 133 packet_trace(out->buf + orig_len + 4, n - 4, 1);
f5615d24
SP
134}
135
70428d1a
LS
136static int packet_write_fmt_1(int fd, int gently,
137 const char *fmt, va_list args)
138{
150efef1 139 static struct strbuf buf = STRBUF_INIT;
70428d1a 140
150efef1 141 strbuf_reset(&buf);
70428d1a 142 format_packet(&buf, fmt, args);
4c95e3dd
JK
143 if (write_in_full(fd, buf.buf, buf.len) < 0) {
144 if (!gently) {
145 check_pipe(errno);
146 die_errno("packet write with format failed");
147 }
148 return error("packet write with format failed");
70428d1a 149 }
4c95e3dd
JK
150
151 return 0;
70428d1a
LS
152}
153
81c634e9 154void packet_write_fmt(int fd, const char *fmt, ...)
f5615d24
SP
155{
156 va_list args;
f5615d24
SP
157
158 va_start(args, fmt);
70428d1a
LS
159 packet_write_fmt_1(fd, 0, fmt, args);
160 va_end(args);
161}
162
163int packet_write_fmt_gently(int fd, const char *fmt, ...)
164{
165 int status;
166 va_list args;
167
168 va_start(args, fmt);
169 status = packet_write_fmt_1(fd, 1, fmt, args);
f5615d24 170 va_end(args);
70428d1a 171 return status;
f3a3214e
LT
172}
173
edfb780c
LS
174static int packet_write_gently(const int fd_out, const char *buf, size_t size)
175{
176 static char packet_write_buffer[LARGE_PACKET_MAX];
177 size_t packet_size;
178
179 if (size > sizeof(packet_write_buffer) - 4)
180 return error("packet write failed - data exceeds max packet size");
181
182 packet_trace(buf, size, 1);
183 packet_size = size + 4;
184 set_packet_header(packet_write_buffer, packet_size);
185 memcpy(packet_write_buffer + 4, buf, size);
4c95e3dd
JK
186 if (write_in_full(fd_out, packet_write_buffer, packet_size) < 0)
187 return error("packet write failed");
188 return 0;
edfb780c
LS
189}
190
5d2124b3
BW
191void packet_write(int fd_out, const char *buf, size_t size)
192{
193 if (packet_write_gently(fd_out, buf, size))
194 die_errno("packet write failed");
195}
196
f5615d24
SP
197void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
198{
199 va_list args;
f5615d24
SP
200
201 va_start(args, fmt);
8e9faf27 202 format_packet(buf, fmt, args);
f5615d24 203 va_end(args);
f5615d24
SP
204}
205
bb643d8b
LS
206int write_packetized_from_fd(int fd_in, int fd_out)
207{
208 static char buf[LARGE_PACKET_DATA_MAX];
209 int err = 0;
210 ssize_t bytes_to_write;
211
212 while (!err) {
213 bytes_to_write = xread(fd_in, buf, sizeof(buf));
214 if (bytes_to_write < 0)
215 return COPY_READ_ERROR;
216 if (bytes_to_write == 0)
217 break;
218 err = packet_write_gently(fd_out, buf, bytes_to_write);
219 }
220 if (!err)
221 err = packet_flush_gently(fd_out);
222 return err;
223}
224
225int write_packetized_from_buf(const char *src_in, size_t len, int fd_out)
226{
227 int err = 0;
228 size_t bytes_written = 0;
229 size_t bytes_to_write;
230
231 while (!err) {
232 if ((len - bytes_written) > LARGE_PACKET_DATA_MAX)
233 bytes_to_write = LARGE_PACKET_DATA_MAX;
234 else
235 bytes_to_write = len - bytes_written;
236 if (bytes_to_write == 0)
237 break;
238 err = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);
239 bytes_written += bytes_to_write;
240 }
241 if (!err)
242 err = packet_flush_gently(fd_out);
243 return err;
244}
245
4981fe75
JK
246static int get_packet_data(int fd, char **src_buf, size_t *src_size,
247 void *dst, unsigned size, int options)
f3a3214e 248{
4981fe75
JK
249 ssize_t ret;
250
251 if (fd >= 0 && src_buf && *src_buf)
252 die("BUG: multiple sources given to packet_read");
253
254 /* Read up to "size" bytes from our source, whatever it is. */
255 if (src_buf && *src_buf) {
256 ret = size < *src_size ? size : *src_size;
257 memcpy(dst, *src_buf, ret);
258 *src_buf += ret;
259 *src_size -= ret;
260 } else {
261 ret = read_in_full(fd, dst, size);
262 if (ret < 0)
263 die_errno("read error");
264 }
265
266 /* And complain if we didn't get enough bytes to satisfy the read. */
61d36330 267 if (ret != size) {
03809429 268 if (options & PACKET_READ_GENTLE_ON_EOF)
46284dd1
HV
269 return -1;
270
c697ad14 271 die("The remote end hung up unexpectedly");
46284dd1
HV
272 }
273
274 return ret;
f3a3214e
LT
275}
276
f5615d24 277static int packet_length(const char *linelen)
f3a3214e 278{
d2330973
RS
279 int val = hex2chr(linelen);
280 return (val < 0) ? val : (val << 8) | hex2chr(linelen + 2);
f5615d24
SP
281}
282
2153d478
BW
283enum 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)
f5615d24 287{
2153d478 288 int len;
f5615d24
SP
289 char linelen[4];
290
2153d478
BW
291 if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) {
292 *pktlen = -1;
293 return PACKET_READ_EOF;
294 }
295
f5615d24 296 len = packet_length(linelen);
2153d478
BW
297
298 if (len < 0) {
743c4b7b 299 die("protocol error: bad line length character: %.4s", linelen);
2153d478 300 } else if (!len) {
bbc30f99 301 packet_trace("0000", 4, 0);
2153d478
BW
302 *pktlen = 0;
303 return PACKET_READ_FLUSH;
304 } else if (len < 4) {
305 die("protocol error: bad line length %d", len);
bbc30f99 306 }
2153d478 307
f3a3214e 308 len -= 4;
2153d478 309 if ((unsigned)len >= size)
f3a3214e 310 die("protocol error: bad line length %d", len);
2153d478
BW
311
312 if (get_packet_data(fd, src_buffer, src_len, buffer, len, options) < 0) {
313 *pktlen = -1;
314 return PACKET_READ_EOF;
315 }
819b929d
JK
316
317 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
318 len && buffer[len-1] == '\n')
319 len--;
320
f3a3214e 321 buffer[len] = 0;
bbc30f99 322 packet_trace(buffer, len, 0);
2153d478
BW
323 *pktlen = len;
324 return PACKET_READ_NORMAL;
325}
326
327int 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;
f3a3214e 336}
f5615d24 337
4981fe75
JK
338static char *packet_read_line_generic(int fd,
339 char **src, size_t *src_len,
340 int *dst_len)
46284dd1 341{
4981fe75
JK
342 int len = packet_read(fd, src, src_len,
343 packet_buffer, sizeof(packet_buffer),
74543a04 344 PACKET_READ_CHOMP_NEWLINE);
4981fe75
JK
345 if (dst_len)
346 *dst_len = len;
974b50c5 347 return (len > 0) ? packet_buffer : NULL;
46284dd1
HV
348}
349
4981fe75 350char *packet_read_line(int fd, int *len_p)
f5615d24 351{
4981fe75
JK
352 return packet_read_line_generic(fd, NULL, NULL, len_p);
353}
f5615d24 354
825b9226
BP
355int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
356{
357 int len = packet_read(fd, NULL, NULL,
358 packet_buffer, sizeof(packet_buffer),
359 PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
360 if (dst_len)
361 *dst_len = len;
362 if (dst_line)
363 *dst_line = (len > 0) ? packet_buffer : NULL;
364 return len;
365}
366
4981fe75
JK
367char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
368{
369 return packet_read_line_generic(-1, src, src_len, dst_len);
f5615d24 370}
bb643d8b
LS
371
372ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out)
373{
374 int packet_len;
375
376 size_t orig_len = sb_out->len;
377 size_t orig_alloc = sb_out->alloc;
378
379 for (;;) {
380 strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);
381 packet_len = packet_read(fd_in, NULL, NULL,
382 /* strbuf_grow() above always allocates one extra byte to
383 * store a '\0' at the end of the string. packet_read()
384 * writes a '\0' extra byte at the end, too. Let it know
385 * that there is already room for the extra byte.
386 */
387 sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,
388 PACKET_READ_GENTLE_ON_EOF);
389 if (packet_len <= 0)
390 break;
391 sb_out->len += packet_len;
392 }
393
394 if (packet_len < 0) {
395 if (orig_alloc == 0)
396 strbuf_release(sb_out);
397 else
398 strbuf_setlen(sb_out, orig_len);
399 return packet_len;
400 }
401 return sb_out->len - orig_len;
402}
77dabc14
BW
403
404/* Packet Reader Functions */
405void packet_reader_init(struct packet_reader *reader, int fd,
406 char *src_buffer, size_t src_len,
407 int options)
408{
409 memset(reader, 0, sizeof(*reader));
410
411 reader->fd = fd;
412 reader->src_buffer = src_buffer;
413 reader->src_len = src_len;
414 reader->buffer = packet_buffer;
415 reader->buffer_size = sizeof(packet_buffer);
416 reader->options = options;
417}
418
419enum packet_read_status packet_reader_read(struct packet_reader *reader)
420{
421 if (reader->line_peeked) {
422 reader->line_peeked = 0;
423 return reader->status;
424 }
425
426 reader->status = packet_read_with_status(reader->fd,
427 &reader->src_buffer,
428 &reader->src_len,
429 reader->buffer,
430 reader->buffer_size,
431 &reader->pktlen,
432 reader->options);
433
434 if (reader->status == PACKET_READ_NORMAL)
435 reader->line = reader->buffer;
436 else
437 reader->line = NULL;
438
439 return reader->status;
440}
441
442enum packet_read_status packet_reader_peek(struct packet_reader *reader)
443{
444 /* Only allow peeking a single line */
445 if (reader->line_peeked)
446 return reader->status;
447
448 /* Peek a line by reading it and setting peeked flag */
449 packet_reader_read(reader);
450 reader->line_peeked = 1;
451 return reader->status;
452}