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