ci: run test round with parallel-checkout enabled
[git/debian.git] / pkt-line.c
blob0194137528c3ce909ab76f55200e2ed31ce742f7
1 #include "cache.h"
2 #include "pkt-line.h"
3 #include "run-command.h"
5 char packet_buffer[LARGE_PACKET_MAX];
6 static const char *packet_trace_prefix = "git";
7 static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET);
8 static struct trace_key trace_pack = TRACE_KEY_INIT(PACKFILE);
10 void packet_trace_identity(const char *prog)
12 packet_trace_prefix = xstrdup(prog);
15 static const char *get_trace_prefix(void)
17 return in_async() ? "sideband" : packet_trace_prefix;
20 static int packet_trace_pack(const char *buf, unsigned int len, int sideband)
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;
34 static void packet_trace(const char *buf, unsigned int len, int write)
36 int i;
37 struct strbuf out;
38 static int in_pack, sideband;
40 if (!trace_want(&trace_packet) && !trace_want(&trace_pack))
41 return;
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);
52 * Make a note in the human-readable trace that the pack data
53 * started.
55 buf = "PACK ...";
56 len = strlen(buf);
59 if (!trace_want(&trace_packet))
60 return;
62 /* +32 is just a guess for header + quoting */
63 strbuf_init(&out, len+32);
65 strbuf_addf(&out, "packet: %12s%c ",
66 get_trace_prefix(), write ? '>' : '<');
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]);
79 strbuf_addch(&out, '\n');
80 trace_strbuf(&trace_packet, &out);
81 strbuf_release(&out);
85 * If we buffered things up above (we don't, but we should),
86 * we'd flush it here
88 void packet_flush(int fd)
90 packet_trace("0000", 4, 1);
91 if (write_in_full(fd, "0000", 4) < 0)
92 die_errno(_("unable to write flush packet"));
95 void packet_delim(int fd)
97 packet_trace("0001", 4, 1);
98 if (write_in_full(fd, "0001", 4) < 0)
99 die_errno(_("unable to write delim packet"));
102 void packet_response_end(int fd)
104 packet_trace("0002", 4, 1);
105 if (write_in_full(fd, "0002", 4) < 0)
106 die_errno(_("unable to write stateless separator packet"));
109 int packet_flush_gently(int fd)
111 packet_trace("0000", 4, 1);
112 if (write_in_full(fd, "0000", 4) < 0)
113 return error(_("flush packet write failed"));
114 return 0;
117 void packet_buf_flush(struct strbuf *buf)
119 packet_trace("0000", 4, 1);
120 strbuf_add(buf, "0000", 4);
123 void packet_buf_delim(struct strbuf *buf)
125 packet_trace("0001", 4, 1);
126 strbuf_add(buf, "0001", 4);
129 void set_packet_header(char *buf, int size)
131 static char hexchar[] = "0123456789abcdef";
133 #define hex(a) (hexchar[(a) & 15])
134 buf[0] = hex(size >> 12);
135 buf[1] = hex(size >> 8);
136 buf[2] = hex(size >> 4);
137 buf[3] = hex(size);
138 #undef hex
141 static void format_packet(struct strbuf *out, const char *prefix,
142 const char *fmt, va_list args)
144 size_t orig_len, n;
146 orig_len = out->len;
147 strbuf_addstr(out, "0000");
148 strbuf_addstr(out, prefix);
149 strbuf_vaddf(out, fmt, args);
150 n = out->len - orig_len;
152 if (n > LARGE_PACKET_MAX)
153 die(_("protocol error: impossibly long line"));
155 set_packet_header(&out->buf[orig_len], n);
156 packet_trace(out->buf + orig_len + 4, n - 4, 1);
159 static int packet_write_fmt_1(int fd, int gently, const char *prefix,
160 const char *fmt, va_list args)
162 static struct strbuf buf = STRBUF_INIT;
164 strbuf_reset(&buf);
165 format_packet(&buf, prefix, fmt, args);
166 if (write_in_full(fd, buf.buf, buf.len) < 0) {
167 if (!gently) {
168 check_pipe(errno);
169 die_errno(_("packet write with format failed"));
171 return error(_("packet write with format failed"));
174 return 0;
177 void packet_write_fmt(int fd, const char *fmt, ...)
179 va_list args;
181 va_start(args, fmt);
182 packet_write_fmt_1(fd, 0, "", fmt, args);
183 va_end(args);
186 int packet_write_fmt_gently(int fd, const char *fmt, ...)
188 int status;
189 va_list args;
191 va_start(args, fmt);
192 status = packet_write_fmt_1(fd, 1, "", fmt, args);
193 va_end(args);
194 return status;
197 static int packet_write_gently(const int fd_out, const char *buf, size_t size)
199 char header[4];
200 size_t packet_size;
202 if (size > LARGE_PACKET_DATA_MAX)
203 return error(_("packet write failed - data exceeds max packet size"));
205 packet_trace(buf, size, 1);
206 packet_size = size + 4;
208 set_packet_header(header, packet_size);
211 * Write the header and the buffer in 2 parts so that we do
212 * not need to allocate a buffer or rely on a static buffer.
213 * This also avoids putting a large buffer on the stack which
214 * might have multi-threading issues.
217 if (write_in_full(fd_out, header, 4) < 0 ||
218 write_in_full(fd_out, buf, size) < 0)
219 return error(_("packet write failed"));
220 return 0;
223 void packet_write(int fd_out, const char *buf, size_t size)
225 if (packet_write_gently(fd_out, buf, size))
226 die_errno(_("packet write failed"));
229 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
231 va_list args;
233 va_start(args, fmt);
234 format_packet(buf, "", fmt, args);
235 va_end(args);
238 void packet_buf_write_len(struct strbuf *buf, const char *data, size_t len)
240 size_t orig_len, n;
242 orig_len = buf->len;
243 strbuf_addstr(buf, "0000");
244 strbuf_add(buf, data, len);
245 n = buf->len - orig_len;
247 if (n > LARGE_PACKET_MAX)
248 die(_("protocol error: impossibly long line"));
250 set_packet_header(&buf->buf[orig_len], n);
251 packet_trace(data, len, 1);
254 int write_packetized_from_fd_no_flush(int fd_in, int fd_out)
256 char *buf = xmalloc(LARGE_PACKET_DATA_MAX);
257 int err = 0;
258 ssize_t bytes_to_write;
260 while (!err) {
261 bytes_to_write = xread(fd_in, buf, LARGE_PACKET_DATA_MAX);
262 if (bytes_to_write < 0) {
263 free(buf);
264 return COPY_READ_ERROR;
266 if (bytes_to_write == 0)
267 break;
268 err = packet_write_gently(fd_out, buf, bytes_to_write);
270 free(buf);
271 return err;
274 int write_packetized_from_buf_no_flush(const char *src_in, size_t len, int fd_out)
276 int err = 0;
277 size_t bytes_written = 0;
278 size_t bytes_to_write;
280 while (!err) {
281 if ((len - bytes_written) > LARGE_PACKET_DATA_MAX)
282 bytes_to_write = LARGE_PACKET_DATA_MAX;
283 else
284 bytes_to_write = len - bytes_written;
285 if (bytes_to_write == 0)
286 break;
287 err = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);
288 bytes_written += bytes_to_write;
290 return err;
293 static int get_packet_data(int fd, char **src_buf, size_t *src_size,
294 void *dst, unsigned size, int options)
296 ssize_t ret;
298 if (fd >= 0 && src_buf && *src_buf)
299 BUG("multiple sources given to packet_read");
301 /* Read up to "size" bytes from our source, whatever it is. */
302 if (src_buf && *src_buf) {
303 ret = size < *src_size ? size : *src_size;
304 memcpy(dst, *src_buf, ret);
305 *src_buf += ret;
306 *src_size -= ret;
307 } else {
308 ret = read_in_full(fd, dst, size);
309 if (ret < 0) {
310 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
311 return error_errno(_("read error"));
312 die_errno(_("read error"));
316 /* And complain if we didn't get enough bytes to satisfy the read. */
317 if (ret != size) {
318 if (options & PACKET_READ_GENTLE_ON_EOF)
319 return -1;
321 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
322 return error(_("the remote end hung up unexpectedly"));
323 die(_("the remote end hung up unexpectedly"));
326 return ret;
329 int packet_length(const char lenbuf_hex[4])
331 int val = hex2chr(lenbuf_hex);
332 return (val < 0) ? val : (val << 8) | hex2chr(lenbuf_hex + 2);
335 enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
336 size_t *src_len, char *buffer,
337 unsigned size, int *pktlen,
338 int options)
340 int len;
341 char linelen[4];
343 if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) {
344 *pktlen = -1;
345 return PACKET_READ_EOF;
348 len = packet_length(linelen);
350 if (len < 0) {
351 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
352 return error(_("protocol error: bad line length "
353 "character: %.4s"), linelen);
354 die(_("protocol error: bad line length character: %.4s"), linelen);
355 } else if (!len) {
356 packet_trace("0000", 4, 0);
357 *pktlen = 0;
358 return PACKET_READ_FLUSH;
359 } else if (len == 1) {
360 packet_trace("0001", 4, 0);
361 *pktlen = 0;
362 return PACKET_READ_DELIM;
363 } else if (len == 2) {
364 packet_trace("0002", 4, 0);
365 *pktlen = 0;
366 return PACKET_READ_RESPONSE_END;
367 } else if (len < 4) {
368 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
369 return error(_("protocol error: bad line length %d"),
370 len);
371 die(_("protocol error: bad line length %d"), len);
374 len -= 4;
375 if ((unsigned)len >= size) {
376 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
377 return error(_("protocol error: bad line length %d"),
378 len);
379 die(_("protocol error: bad line length %d"), len);
382 if (get_packet_data(fd, src_buffer, src_len, buffer, len, options) < 0) {
383 *pktlen = -1;
384 return PACKET_READ_EOF;
387 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
388 len && buffer[len-1] == '\n')
389 len--;
391 buffer[len] = 0;
392 packet_trace(buffer, len, 0);
394 if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
395 starts_with(buffer, "ERR "))
396 die(_("remote error: %s"), buffer + 4);
398 *pktlen = len;
399 return PACKET_READ_NORMAL;
402 int packet_read(int fd, char **src_buffer, size_t *src_len,
403 char *buffer, unsigned size, int options)
405 int pktlen = -1;
407 packet_read_with_status(fd, src_buffer, src_len, buffer, size,
408 &pktlen, options);
410 return pktlen;
413 static char *packet_read_line_generic(int fd,
414 char **src, size_t *src_len,
415 int *dst_len)
417 int len = packet_read(fd, src, src_len,
418 packet_buffer, sizeof(packet_buffer),
419 PACKET_READ_CHOMP_NEWLINE);
420 if (dst_len)
421 *dst_len = len;
422 return (len > 0) ? packet_buffer : NULL;
425 char *packet_read_line(int fd, int *len_p)
427 return packet_read_line_generic(fd, NULL, NULL, len_p);
430 int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
432 int len = packet_read(fd, NULL, NULL,
433 packet_buffer, sizeof(packet_buffer),
434 PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
435 if (dst_len)
436 *dst_len = len;
437 if (dst_line)
438 *dst_line = (len > 0) ? packet_buffer : NULL;
439 return len;
442 char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
444 return packet_read_line_generic(-1, src, src_len, dst_len);
447 ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out, int options)
449 int packet_len;
451 size_t orig_len = sb_out->len;
452 size_t orig_alloc = sb_out->alloc;
454 for (;;) {
455 strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);
456 packet_len = packet_read(fd_in, NULL, NULL,
457 /* strbuf_grow() above always allocates one extra byte to
458 * store a '\0' at the end of the string. packet_read()
459 * writes a '\0' extra byte at the end, too. Let it know
460 * that there is already room for the extra byte.
462 sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,
463 options);
464 if (packet_len <= 0)
465 break;
466 sb_out->len += packet_len;
469 if (packet_len < 0) {
470 if (orig_alloc == 0)
471 strbuf_release(sb_out);
472 else
473 strbuf_setlen(sb_out, orig_len);
474 return packet_len;
476 return sb_out->len - orig_len;
479 int recv_sideband(const char *me, int in_stream, int out)
481 char buf[LARGE_PACKET_MAX + 1];
482 int len;
483 struct strbuf scratch = STRBUF_INIT;
484 enum sideband_type sideband_type;
486 while (1) {
487 int status = packet_read_with_status(in_stream, NULL, NULL,
488 buf, LARGE_PACKET_MAX,
489 &len,
490 PACKET_READ_GENTLE_ON_EOF);
491 if (!demultiplex_sideband(me, status, buf, len, 0, &scratch,
492 &sideband_type))
493 continue;
494 switch (sideband_type) {
495 case SIDEBAND_PRIMARY:
496 write_or_die(out, buf + 1, len - 1);
497 break;
498 default: /* errors: message already written */
499 if (scratch.len > 0)
500 BUG("unhandled incomplete sideband: '%s'",
501 scratch.buf);
502 return sideband_type;
507 /* Packet Reader Functions */
508 void packet_reader_init(struct packet_reader *reader, int fd,
509 char *src_buffer, size_t src_len,
510 int options)
512 memset(reader, 0, sizeof(*reader));
514 reader->fd = fd;
515 reader->src_buffer = src_buffer;
516 reader->src_len = src_len;
517 reader->buffer = packet_buffer;
518 reader->buffer_size = sizeof(packet_buffer);
519 reader->options = options;
520 reader->me = "git";
521 reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
524 enum packet_read_status packet_reader_read(struct packet_reader *reader)
526 struct strbuf scratch = STRBUF_INIT;
528 if (reader->line_peeked) {
529 reader->line_peeked = 0;
530 return reader->status;
534 * Consume all progress packets until a primary payload packet is
535 * received
537 while (1) {
538 enum sideband_type sideband_type;
539 reader->status = packet_read_with_status(reader->fd,
540 &reader->src_buffer,
541 &reader->src_len,
542 reader->buffer,
543 reader->buffer_size,
544 &reader->pktlen,
545 reader->options);
546 if (!reader->use_sideband)
547 break;
548 if (demultiplex_sideband(reader->me, reader->status,
549 reader->buffer, reader->pktlen, 1,
550 &scratch, &sideband_type))
551 break;
554 if (reader->status == PACKET_READ_NORMAL)
555 /* Skip the sideband designator if sideband is used */
556 reader->line = reader->use_sideband ?
557 reader->buffer + 1 : reader->buffer;
558 else
559 reader->line = NULL;
561 return reader->status;
564 enum packet_read_status packet_reader_peek(struct packet_reader *reader)
566 /* Only allow peeking a single line */
567 if (reader->line_peeked)
568 return reader->status;
570 /* Peek a line by reading it and setting peeked flag */
571 packet_reader_read(reader);
572 reader->line_peeked = 1;
573 return reader->status;
576 void packet_writer_init(struct packet_writer *writer, int dest_fd)
578 writer->dest_fd = dest_fd;
579 writer->use_sideband = 0;
582 void packet_writer_write(struct packet_writer *writer, const char *fmt, ...)
584 va_list args;
586 va_start(args, fmt);
587 packet_write_fmt_1(writer->dest_fd, 0,
588 writer->use_sideband ? "\001" : "", fmt, args);
589 va_end(args);
592 void packet_writer_error(struct packet_writer *writer, const char *fmt, ...)
594 va_list args;
596 va_start(args, fmt);
597 packet_write_fmt_1(writer->dest_fd, 0,
598 writer->use_sideband ? "\003" : "ERR ", fmt, args);
599 va_end(args);
602 void packet_writer_delim(struct packet_writer *writer)
604 packet_delim(writer->dest_fd);
607 void packet_writer_flush(struct packet_writer *writer)
609 packet_flush(writer->dest_fd);