Merge branch 'ms/packet-err-check' into jt/fetch-v2-sideband
[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
a4cfd41c
BW
94void packet_delim(int fd)
95{
96 packet_trace("0001", 4, 1);
97 write_or_die(fd, "0001", 4);
98}
99
038ce90f
LS
100int packet_flush_gently(int fd)
101{
102 packet_trace("0000", 4, 1);
4c95e3dd 103 if (write_in_full(fd, "0000", 4) < 0)
c60d7697 104 return error(_("flush packet write failed"));
4c95e3dd 105 return 0;
038ce90f
LS
106}
107
f5615d24
SP
108void packet_buf_flush(struct strbuf *buf)
109{
bbc30f99 110 packet_trace("0000", 4, 1);
f5615d24
SP
111 strbuf_add(buf, "0000", 4);
112}
113
a4cfd41c
BW
114void packet_buf_delim(struct strbuf *buf)
115{
116 packet_trace("0001", 4, 1);
117 strbuf_add(buf, "0001", 4);
118}
119
2f60bdd1 120static void set_packet_header(char *buf, const int size)
f3a3214e 121{
f3a3214e 122 static char hexchar[] = "0123456789abcdef";
2f60bdd1
LS
123
124 #define hex(a) (hexchar[(a) & 15])
125 buf[0] = hex(size >> 12);
126 buf[1] = hex(size >> 8);
127 buf[2] = hex(size >> 4);
128 buf[3] = hex(size);
129 #undef hex
130}
131
132static void format_packet(struct strbuf *out, const char *fmt, va_list args)
133{
8e9faf27 134 size_t orig_len, n;
f3a3214e 135
8e9faf27
JK
136 orig_len = out->len;
137 strbuf_addstr(out, "0000");
138 strbuf_vaddf(out, fmt, args);
139 n = out->len - orig_len;
140
141 if (n > LARGE_PACKET_MAX)
c60d7697 142 die(_("protocol error: impossibly long line"));
8e9faf27 143
2f60bdd1 144 set_packet_header(&out->buf[orig_len], n);
8e9faf27 145 packet_trace(out->buf + orig_len + 4, n - 4, 1);
f5615d24
SP
146}
147
70428d1a
LS
148static int packet_write_fmt_1(int fd, int gently,
149 const char *fmt, va_list args)
150{
150efef1 151 static struct strbuf buf = STRBUF_INIT;
70428d1a 152
150efef1 153 strbuf_reset(&buf);
70428d1a 154 format_packet(&buf, fmt, args);
4c95e3dd
JK
155 if (write_in_full(fd, buf.buf, buf.len) < 0) {
156 if (!gently) {
157 check_pipe(errno);
c60d7697 158 die_errno(_("packet write with format failed"));
4c95e3dd 159 }
c60d7697 160 return error(_("packet write with format failed"));
70428d1a 161 }
4c95e3dd
JK
162
163 return 0;
70428d1a
LS
164}
165
81c634e9 166void packet_write_fmt(int fd, const char *fmt, ...)
f5615d24
SP
167{
168 va_list args;
f5615d24
SP
169
170 va_start(args, fmt);
70428d1a
LS
171 packet_write_fmt_1(fd, 0, fmt, args);
172 va_end(args);
173}
174
175int packet_write_fmt_gently(int fd, const char *fmt, ...)
176{
177 int status;
178 va_list args;
179
180 va_start(args, fmt);
181 status = packet_write_fmt_1(fd, 1, fmt, args);
f5615d24 182 va_end(args);
70428d1a 183 return status;
f3a3214e
LT
184}
185
edfb780c
LS
186static int packet_write_gently(const int fd_out, const char *buf, size_t size)
187{
188 static char packet_write_buffer[LARGE_PACKET_MAX];
189 size_t packet_size;
190
191 if (size > sizeof(packet_write_buffer) - 4)
c60d7697 192 return error(_("packet write failed - data exceeds max packet size"));
edfb780c
LS
193
194 packet_trace(buf, size, 1);
195 packet_size = size + 4;
196 set_packet_header(packet_write_buffer, packet_size);
197 memcpy(packet_write_buffer + 4, buf, size);
4c95e3dd 198 if (write_in_full(fd_out, packet_write_buffer, packet_size) < 0)
c60d7697 199 return error(_("packet write failed"));
4c95e3dd 200 return 0;
edfb780c
LS
201}
202
5d2124b3
BW
203void packet_write(int fd_out, const char *buf, size_t size)
204{
205 if (packet_write_gently(fd_out, buf, size))
c60d7697 206 die_errno(_("packet write failed"));
5d2124b3
BW
207}
208
f5615d24
SP
209void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
210{
211 va_list args;
f5615d24
SP
212
213 va_start(args, fmt);
8e9faf27 214 format_packet(buf, fmt, args);
f5615d24 215 va_end(args);
f5615d24
SP
216}
217
f1f4d8ac
BW
218void packet_buf_write_len(struct strbuf *buf, const char *data, size_t len)
219{
220 size_t orig_len, n;
221
222 orig_len = buf->len;
223 strbuf_addstr(buf, "0000");
224 strbuf_add(buf, data, len);
225 n = buf->len - orig_len;
226
227 if (n > LARGE_PACKET_MAX)
c60d7697 228 die(_("protocol error: impossibly long line"));
f1f4d8ac
BW
229
230 set_packet_header(&buf->buf[orig_len], n);
231 packet_trace(data, len, 1);
232}
233
bb643d8b
LS
234int write_packetized_from_fd(int fd_in, int fd_out)
235{
236 static char buf[LARGE_PACKET_DATA_MAX];
237 int err = 0;
238 ssize_t bytes_to_write;
239
240 while (!err) {
241 bytes_to_write = xread(fd_in, buf, sizeof(buf));
242 if (bytes_to_write < 0)
243 return COPY_READ_ERROR;
244 if (bytes_to_write == 0)
245 break;
246 err = packet_write_gently(fd_out, buf, bytes_to_write);
247 }
248 if (!err)
249 err = packet_flush_gently(fd_out);
250 return err;
251}
252
253int write_packetized_from_buf(const char *src_in, size_t len, int fd_out)
254{
255 int err = 0;
256 size_t bytes_written = 0;
257 size_t bytes_to_write;
258
259 while (!err) {
260 if ((len - bytes_written) > LARGE_PACKET_DATA_MAX)
261 bytes_to_write = LARGE_PACKET_DATA_MAX;
262 else
263 bytes_to_write = len - bytes_written;
264 if (bytes_to_write == 0)
265 break;
266 err = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);
267 bytes_written += bytes_to_write;
268 }
269 if (!err)
270 err = packet_flush_gently(fd_out);
271 return err;
272}
273
4981fe75
JK
274static int get_packet_data(int fd, char **src_buf, size_t *src_size,
275 void *dst, unsigned size, int options)
f3a3214e 276{
4981fe75
JK
277 ssize_t ret;
278
279 if (fd >= 0 && src_buf && *src_buf)
033abf97 280 BUG("multiple sources given to packet_read");
4981fe75
JK
281
282 /* Read up to "size" bytes from our source, whatever it is. */
283 if (src_buf && *src_buf) {
284 ret = size < *src_size ? size : *src_size;
285 memcpy(dst, *src_buf, ret);
286 *src_buf += ret;
287 *src_size -= ret;
288 } else {
289 ret = read_in_full(fd, dst, size);
290 if (ret < 0)
c60d7697 291 die_errno(_("read error"));
4981fe75
JK
292 }
293
294 /* And complain if we didn't get enough bytes to satisfy the read. */
61d36330 295 if (ret != size) {
03809429 296 if (options & PACKET_READ_GENTLE_ON_EOF)
46284dd1
HV
297 return -1;
298
c60d7697 299 die(_("the remote end hung up unexpectedly"));
46284dd1
HV
300 }
301
302 return ret;
f3a3214e
LT
303}
304
f5615d24 305static int packet_length(const char *linelen)
f3a3214e 306{
d2330973
RS
307 int val = hex2chr(linelen);
308 return (val < 0) ? val : (val << 8) | hex2chr(linelen + 2);
f5615d24
SP
309}
310
2153d478
BW
311enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
312 size_t *src_len, char *buffer,
313 unsigned size, int *pktlen,
314 int options)
f5615d24 315{
2153d478 316 int len;
f5615d24
SP
317 char linelen[4];
318
2153d478
BW
319 if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) {
320 *pktlen = -1;
321 return PACKET_READ_EOF;
322 }
323
f5615d24 324 len = packet_length(linelen);
2153d478
BW
325
326 if (len < 0) {
c60d7697 327 die(_("protocol error: bad line length character: %.4s"), linelen);
2153d478 328 } else if (!len) {
bbc30f99 329 packet_trace("0000", 4, 0);
2153d478
BW
330 *pktlen = 0;
331 return PACKET_READ_FLUSH;
a4cfd41c
BW
332 } else if (len == 1) {
333 packet_trace("0001", 4, 0);
334 *pktlen = 0;
335 return PACKET_READ_DELIM;
2153d478 336 } else if (len < 4) {
c60d7697 337 die(_("protocol error: bad line length %d"), len);
bbc30f99 338 }
2153d478 339
f3a3214e 340 len -= 4;
2153d478 341 if ((unsigned)len >= size)
c60d7697 342 die(_("protocol error: bad line length %d"), len);
2153d478
BW
343
344 if (get_packet_data(fd, src_buffer, src_len, buffer, len, options) < 0) {
345 *pktlen = -1;
346 return PACKET_READ_EOF;
347 }
819b929d 348
2d103c31
MS
349 if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
350 starts_with(buffer, "ERR "))
351 die(_("remote error: %s"), buffer + 4);
352
819b929d
JK
353 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
354 len && buffer[len-1] == '\n')
355 len--;
356
f3a3214e 357 buffer[len] = 0;
bbc30f99 358 packet_trace(buffer, len, 0);
2153d478
BW
359 *pktlen = len;
360 return PACKET_READ_NORMAL;
361}
362
363int packet_read(int fd, char **src_buffer, size_t *src_len,
364 char *buffer, unsigned size, int options)
365{
366 int pktlen = -1;
367
368 packet_read_with_status(fd, src_buffer, src_len, buffer, size,
369 &pktlen, options);
370
371 return pktlen;
f3a3214e 372}
f5615d24 373
4981fe75
JK
374static char *packet_read_line_generic(int fd,
375 char **src, size_t *src_len,
376 int *dst_len)
46284dd1 377{
4981fe75
JK
378 int len = packet_read(fd, src, src_len,
379 packet_buffer, sizeof(packet_buffer),
74543a04 380 PACKET_READ_CHOMP_NEWLINE);
4981fe75
JK
381 if (dst_len)
382 *dst_len = len;
974b50c5 383 return (len > 0) ? packet_buffer : NULL;
46284dd1
HV
384}
385
4981fe75 386char *packet_read_line(int fd, int *len_p)
f5615d24 387{
4981fe75
JK
388 return packet_read_line_generic(fd, NULL, NULL, len_p);
389}
f5615d24 390
825b9226
BP
391int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
392{
393 int len = packet_read(fd, NULL, NULL,
394 packet_buffer, sizeof(packet_buffer),
395 PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
396 if (dst_len)
397 *dst_len = len;
398 if (dst_line)
399 *dst_line = (len > 0) ? packet_buffer : NULL;
400 return len;
401}
402
4981fe75
JK
403char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
404{
405 return packet_read_line_generic(-1, src, src_len, dst_len);
f5615d24 406}
bb643d8b
LS
407
408ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out)
409{
410 int packet_len;
411
412 size_t orig_len = sb_out->len;
413 size_t orig_alloc = sb_out->alloc;
414
415 for (;;) {
416 strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);
417 packet_len = packet_read(fd_in, NULL, NULL,
418 /* strbuf_grow() above always allocates one extra byte to
419 * store a '\0' at the end of the string. packet_read()
420 * writes a '\0' extra byte at the end, too. Let it know
421 * that there is already room for the extra byte.
422 */
423 sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,
424 PACKET_READ_GENTLE_ON_EOF);
425 if (packet_len <= 0)
426 break;
427 sb_out->len += packet_len;
428 }
429
430 if (packet_len < 0) {
431 if (orig_alloc == 0)
432 strbuf_release(sb_out);
433 else
434 strbuf_setlen(sb_out, orig_len);
435 return packet_len;
436 }
437 return sb_out->len - orig_len;
438}
77dabc14
BW
439
440/* Packet Reader Functions */
441void packet_reader_init(struct packet_reader *reader, int fd,
442 char *src_buffer, size_t src_len,
443 int options)
444{
445 memset(reader, 0, sizeof(*reader));
446
447 reader->fd = fd;
448 reader->src_buffer = src_buffer;
449 reader->src_len = src_len;
450 reader->buffer = packet_buffer;
451 reader->buffer_size = sizeof(packet_buffer);
452 reader->options = options;
453}
454
455enum packet_read_status packet_reader_read(struct packet_reader *reader)
456{
457 if (reader->line_peeked) {
458 reader->line_peeked = 0;
459 return reader->status;
460 }
461
462 reader->status = packet_read_with_status(reader->fd,
463 &reader->src_buffer,
464 &reader->src_len,
465 reader->buffer,
466 reader->buffer_size,
467 &reader->pktlen,
468 reader->options);
469
470 if (reader->status == PACKET_READ_NORMAL)
471 reader->line = reader->buffer;
472 else
473 reader->line = NULL;
474
475 return reader->status;
476}
477
478enum packet_read_status packet_reader_peek(struct packet_reader *reader)
479{
480 /* Only allow peeking a single line */
481 if (reader->line_peeked)
482 return reader->status;
483
484 /* Peek a line by reading it and setting peeked flag */
485 packet_reader_read(reader);
486 reader->line_peeked = 1;
487 return reader->status;
488}