artifacts-tar: when including `.dll` files, don't forget the unit-tests
[git/gitster.git] / pkt-line.c
blob62b4208b66e15d72257f5a2b72f4fa6ce3684376
1 #include "git-compat-util.h"
2 #include "copy.h"
3 #include "pkt-line.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "run-command.h"
7 #include "trace.h"
8 #include "wrapper.h"
9 #include "write-or-die.h"
11 char packet_buffer[LARGE_PACKET_MAX];
12 static const char *packet_trace_prefix = "git";
13 static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET);
14 static struct trace_key trace_pack = TRACE_KEY_INIT(PACKFILE);
16 void packet_trace_identity(const char *prog)
18 packet_trace_prefix = xstrdup(prog);
21 static const char *get_trace_prefix(void)
23 return in_async() ? "sideband" : packet_trace_prefix;
26 static int packet_trace_pack(const char *buf, unsigned int len, int sideband)
28 if (!sideband) {
29 trace_verbatim(&trace_pack, buf, len);
30 return 1;
31 } else if (len && *buf == '\1') {
32 trace_verbatim(&trace_pack, buf + 1, len - 1);
33 return 1;
34 } else {
35 /* it's another non-pack sideband */
36 return 0;
40 static void packet_trace(const char *buf, unsigned int len, int write)
42 int i;
43 struct strbuf out;
44 static int in_pack, sideband;
46 if (!trace_want(&trace_packet) && !trace_want(&trace_pack))
47 return;
49 if (in_pack) {
50 if (packet_trace_pack(buf, len, sideband))
51 return;
52 } else if (starts_with(buf, "PACK") || starts_with(buf, "\1PACK")) {
53 in_pack = 1;
54 sideband = *buf == '\1';
55 packet_trace_pack(buf, len, sideband);
58 * Make a note in the human-readable trace that the pack data
59 * started.
61 buf = "PACK ...";
62 len = strlen(buf);
65 if (!trace_want(&trace_packet))
66 return;
68 /* +32 is just a guess for header + quoting */
69 strbuf_init(&out, len+32);
71 strbuf_addf(&out, "packet: %12s%c ",
72 get_trace_prefix(), write ? '>' : '<');
74 /* XXX we should really handle printable utf8 */
75 for (i = 0; i < len; i++) {
76 /* suppress newlines */
77 if (buf[i] == '\n')
78 continue;
79 if (buf[i] >= 0x20 && buf[i] <= 0x7e)
80 strbuf_addch(&out, buf[i]);
81 else
82 strbuf_addf(&out, "\\%o", buf[i]);
85 strbuf_addch(&out, '\n');
86 trace_strbuf(&trace_packet, &out);
87 strbuf_release(&out);
91 * If we buffered things up above (we don't, but we should),
92 * we'd flush it here
94 void packet_flush(int fd)
96 packet_trace("0000", 4, 1);
97 if (write_in_full(fd, "0000", 4) < 0)
98 die_errno(_("unable to write flush packet"));
101 void packet_delim(int fd)
103 packet_trace("0001", 4, 1);
104 if (write_in_full(fd, "0001", 4) < 0)
105 die_errno(_("unable to write delim packet"));
108 void packet_response_end(int fd)
110 packet_trace("0002", 4, 1);
111 if (write_in_full(fd, "0002", 4) < 0)
112 die_errno(_("unable to write response end packet"));
115 int packet_flush_gently(int fd)
117 packet_trace("0000", 4, 1);
118 if (write_in_full(fd, "0000", 4) < 0)
119 return error(_("flush packet write failed"));
120 return 0;
123 void packet_buf_flush(struct strbuf *buf)
125 packet_trace("0000", 4, 1);
126 strbuf_add(buf, "0000", 4);
129 void packet_buf_delim(struct strbuf *buf)
131 packet_trace("0001", 4, 1);
132 strbuf_add(buf, "0001", 4);
135 void set_packet_header(char *buf, int size)
137 static char hexchar[] = "0123456789abcdef";
139 #define hex(a) (hexchar[(a) & 15])
140 buf[0] = hex(size >> 12);
141 buf[1] = hex(size >> 8);
142 buf[2] = hex(size >> 4);
143 buf[3] = hex(size);
144 #undef hex
147 static void format_packet(struct strbuf *out, const char *prefix,
148 const char *fmt, va_list args)
150 size_t orig_len, n;
152 orig_len = out->len;
153 strbuf_addstr(out, "0000");
154 strbuf_addstr(out, prefix);
155 strbuf_vaddf(out, fmt, args);
156 n = out->len - orig_len;
158 if (n > LARGE_PACKET_MAX)
159 die(_("protocol error: impossibly long line"));
161 set_packet_header(&out->buf[orig_len], n);
162 packet_trace(out->buf + orig_len + 4, n - 4, 1);
165 static int packet_write_fmt_1(int fd, int gently, const char *prefix,
166 const char *fmt, va_list args)
168 static struct strbuf buf = STRBUF_INIT;
170 strbuf_reset(&buf);
171 format_packet(&buf, prefix, fmt, args);
172 if (write_in_full(fd, buf.buf, buf.len) < 0) {
173 if (!gently) {
174 check_pipe(errno);
175 die_errno(_("packet write with format failed"));
177 return error(_("packet write with format failed"));
180 return 0;
183 void packet_write_fmt(int fd, const char *fmt, ...)
185 va_list args;
187 va_start(args, fmt);
188 packet_write_fmt_1(fd, 0, "", fmt, args);
189 va_end(args);
192 int packet_write_fmt_gently(int fd, const char *fmt, ...)
194 int status;
195 va_list args;
197 va_start(args, fmt);
198 status = packet_write_fmt_1(fd, 1, "", fmt, args);
199 va_end(args);
200 return status;
203 static int do_packet_write(const int fd_out, const char *buf, size_t size,
204 struct strbuf *err)
206 char header[4];
207 size_t packet_size;
209 if (size > LARGE_PACKET_DATA_MAX) {
210 strbuf_addstr(err, _("packet write failed - data exceeds max packet size"));
211 return -1;
214 packet_trace(buf, size, 1);
215 packet_size = size + 4;
217 set_packet_header(header, packet_size);
220 * Write the header and the buffer in 2 parts so that we do
221 * not need to allocate a buffer or rely on a static buffer.
222 * This also avoids putting a large buffer on the stack which
223 * might have multi-threading issues.
226 if (write_in_full(fd_out, header, 4) < 0 ||
227 write_in_full(fd_out, buf, size) < 0) {
228 strbuf_addf(err, _("packet write failed: %s"), strerror(errno));
229 return -1;
231 return 0;
234 static int packet_write_gently(const int fd_out, const char *buf, size_t size)
236 struct strbuf err = STRBUF_INIT;
237 if (do_packet_write(fd_out, buf, size, &err)) {
238 error("%s", err.buf);
239 strbuf_release(&err);
240 return -1;
242 return 0;
245 void packet_write(int fd_out, const char *buf, size_t size)
247 struct strbuf err = STRBUF_INIT;
248 if (do_packet_write(fd_out, buf, size, &err))
249 die("%s", err.buf);
252 void packet_fwrite(FILE *f, const char *buf, size_t size)
254 size_t packet_size;
255 char header[4];
257 if (size > LARGE_PACKET_DATA_MAX)
258 die(_("packet write failed - data exceeds max packet size"));
260 packet_trace(buf, size, 1);
261 packet_size = size + 4;
263 set_packet_header(header, packet_size);
264 fwrite_or_die(f, header, 4);
265 fwrite_or_die(f, buf, size);
268 void packet_fwrite_fmt(FILE *fh, const char *fmt, ...)
270 static struct strbuf buf = STRBUF_INIT;
271 va_list args;
273 strbuf_reset(&buf);
275 va_start(args, fmt);
276 format_packet(&buf, "", fmt, args);
277 va_end(args);
279 fwrite_or_die(fh, buf.buf, buf.len);
282 void packet_fflush(FILE *f)
284 packet_trace("0000", 4, 1);
285 fwrite_or_die(f, "0000", 4);
286 fflush_or_die(f);
289 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
291 va_list args;
293 va_start(args, fmt);
294 format_packet(buf, "", fmt, args);
295 va_end(args);
298 int write_packetized_from_fd_no_flush(int fd_in, int fd_out)
300 char *buf = xmalloc(LARGE_PACKET_DATA_MAX);
301 int err = 0;
302 ssize_t bytes_to_write;
304 while (!err) {
305 bytes_to_write = xread(fd_in, buf, LARGE_PACKET_DATA_MAX);
306 if (bytes_to_write < 0) {
307 free(buf);
308 return COPY_READ_ERROR;
310 if (bytes_to_write == 0)
311 break;
312 err = packet_write_gently(fd_out, buf, bytes_to_write);
314 free(buf);
315 return err;
318 int write_packetized_from_buf_no_flush_count(const char *src_in, size_t len,
319 int fd_out, int *packet_counter)
321 int err = 0;
322 size_t bytes_written = 0;
323 size_t bytes_to_write;
325 while (!err) {
326 if ((len - bytes_written) > LARGE_PACKET_DATA_MAX)
327 bytes_to_write = LARGE_PACKET_DATA_MAX;
328 else
329 bytes_to_write = len - bytes_written;
330 if (bytes_to_write == 0)
331 break;
332 err = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);
333 bytes_written += bytes_to_write;
334 if (packet_counter)
335 (*packet_counter)++;
337 return err;
340 static int get_packet_data(int fd, char **src_buf, size_t *src_size,
341 void *dst, unsigned size, int options)
343 ssize_t ret;
345 if (fd >= 0 && src_buf && *src_buf)
346 BUG("multiple sources given to packet_read");
348 /* Read up to "size" bytes from our source, whatever it is. */
349 if (src_buf && *src_buf) {
350 ret = size < *src_size ? size : *src_size;
351 memcpy(dst, *src_buf, ret);
352 *src_buf += ret;
353 *src_size -= ret;
354 } else {
355 ret = read_in_full(fd, dst, size);
356 if (ret < 0) {
357 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
358 return error_errno(_("read error"));
359 die_errno(_("read error"));
363 /* And complain if we didn't get enough bytes to satisfy the read. */
364 if (ret != size) {
365 if (options & PACKET_READ_GENTLE_ON_EOF)
366 return -1;
368 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
369 return error(_("the remote end hung up unexpectedly"));
370 die(_("the remote end hung up unexpectedly"));
373 return ret;
376 int packet_length(const char lenbuf_hex[4])
378 int val = hex2chr(lenbuf_hex);
379 return (val < 0) ? val : (val << 8) | hex2chr(lenbuf_hex + 2);
382 static char *find_packfile_uri_path(const char *buffer)
384 const char *URI_MARK = "://";
385 char *path;
386 int len;
388 /* First char is sideband mark */
389 buffer += 1;
391 len = strspn(buffer, "0123456789abcdefABCDEF");
392 /* size of SHA1 and SHA256 hash */
393 if (!(len == 40 || len == 64) || buffer[len] != ' ')
394 return NULL; /* required "<hash>SP" not seen */
396 path = strstr(buffer + len + 1, URI_MARK);
397 if (!path)
398 return NULL;
400 path = strchr(path + strlen(URI_MARK), '/');
401 if (!path || !*(path + 1))
402 return NULL;
404 /* position after '/' */
405 return ++path;
408 enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
409 size_t *src_len, char *buffer,
410 unsigned size, int *pktlen,
411 int options)
413 int len;
414 char linelen[4];
415 char *uri_path_start;
417 if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) {
418 *pktlen = -1;
419 return PACKET_READ_EOF;
422 len = packet_length(linelen);
424 if (len < 0) {
425 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
426 return error(_("protocol error: bad line length "
427 "character: %.4s"), linelen);
428 die(_("protocol error: bad line length character: %.4s"), linelen);
429 } else if (!len) {
430 packet_trace("0000", 4, 0);
431 *pktlen = 0;
432 return PACKET_READ_FLUSH;
433 } else if (len == 1) {
434 packet_trace("0001", 4, 0);
435 *pktlen = 0;
436 return PACKET_READ_DELIM;
437 } else if (len == 2) {
438 packet_trace("0002", 4, 0);
439 *pktlen = 0;
440 return PACKET_READ_RESPONSE_END;
441 } else if (len < 4) {
442 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
443 return error(_("protocol error: bad line length %d"),
444 len);
445 die(_("protocol error: bad line length %d"), len);
448 len -= 4;
449 if ((unsigned)len >= size) {
450 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
451 return error(_("protocol error: bad line length %d"),
452 len);
453 die(_("protocol error: bad line length %d"), len);
456 if (get_packet_data(fd, src_buffer, src_len, buffer, len, options) < 0) {
457 *pktlen = -1;
458 return PACKET_READ_EOF;
461 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
462 len && buffer[len-1] == '\n')
463 len--;
465 buffer[len] = 0;
466 if (options & PACKET_READ_REDACT_URI_PATH &&
467 (uri_path_start = find_packfile_uri_path(buffer))) {
468 const char *redacted = "<redacted>";
469 struct strbuf tracebuf = STRBUF_INIT;
470 strbuf_insert(&tracebuf, 0, buffer, len);
471 strbuf_splice(&tracebuf, uri_path_start - buffer,
472 strlen(uri_path_start), redacted, strlen(redacted));
473 packet_trace(tracebuf.buf, tracebuf.len, 0);
474 strbuf_release(&tracebuf);
475 } else {
476 packet_trace(buffer, len, 0);
479 if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
480 starts_with(buffer, "ERR "))
481 die(_("remote error: %s"), buffer + 4);
483 *pktlen = len;
484 return PACKET_READ_NORMAL;
487 int packet_read(int fd, char *buffer, unsigned size, int options)
489 int pktlen = -1;
491 packet_read_with_status(fd, NULL, NULL, buffer, size, &pktlen,
492 options);
494 return pktlen;
497 char *packet_read_line(int fd, int *dst_len)
499 int len = packet_read(fd, packet_buffer, sizeof(packet_buffer),
500 PACKET_READ_CHOMP_NEWLINE);
501 if (dst_len)
502 *dst_len = len;
503 return (len > 0) ? packet_buffer : NULL;
506 int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
508 int len = packet_read(fd, packet_buffer, sizeof(packet_buffer),
509 PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
510 if (dst_len)
511 *dst_len = len;
512 if (dst_line)
513 *dst_line = (len > 0) ? packet_buffer : NULL;
514 return len;
517 ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out, int options)
519 int packet_len;
521 size_t orig_len = sb_out->len;
522 size_t orig_alloc = sb_out->alloc;
524 for (;;) {
525 strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);
526 packet_len = packet_read(fd_in,
527 /* strbuf_grow() above always allocates one extra byte to
528 * store a '\0' at the end of the string. packet_read()
529 * writes a '\0' extra byte at the end, too. Let it know
530 * that there is already room for the extra byte.
532 sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,
533 options);
534 if (packet_len <= 0)
535 break;
536 sb_out->len += packet_len;
539 if (packet_len < 0) {
540 if (orig_alloc == 0)
541 strbuf_release(sb_out);
542 else
543 strbuf_setlen(sb_out, orig_len);
544 return packet_len;
546 return sb_out->len - orig_len;
549 int recv_sideband(const char *me, int in_stream, int out)
551 char buf[LARGE_PACKET_MAX + 1];
552 int len;
553 struct strbuf scratch = STRBUF_INIT;
554 enum sideband_type sideband_type;
556 while (1) {
557 int status = packet_read_with_status(in_stream, NULL, NULL,
558 buf, LARGE_PACKET_MAX,
559 &len,
560 PACKET_READ_GENTLE_ON_EOF);
561 if (!demultiplex_sideband(me, status, buf, len, 0, &scratch,
562 &sideband_type))
563 continue;
564 switch (sideband_type) {
565 case SIDEBAND_PRIMARY:
566 write_or_die(out, buf + 1, len - 1);
567 break;
568 default: /* errors: message already written */
569 if (scratch.len > 0)
570 BUG("unhandled incomplete sideband: '%s'",
571 scratch.buf);
572 return sideband_type;
577 /* Packet Reader Functions */
578 void packet_reader_init(struct packet_reader *reader, int fd,
579 char *src_buffer, size_t src_len,
580 int options)
582 memset(reader, 0, sizeof(*reader));
584 reader->fd = fd;
585 reader->src_buffer = src_buffer;
586 reader->src_len = src_len;
587 reader->buffer = packet_buffer;
588 reader->buffer_size = sizeof(packet_buffer);
589 reader->options = options;
590 reader->me = "git";
591 reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
594 enum packet_read_status packet_reader_read(struct packet_reader *reader)
596 struct strbuf scratch = STRBUF_INIT;
598 if (reader->line_peeked) {
599 reader->line_peeked = 0;
600 return reader->status;
604 * Consume all progress packets until a primary payload packet is
605 * received
607 while (1) {
608 enum sideband_type sideband_type;
609 reader->status = packet_read_with_status(reader->fd,
610 &reader->src_buffer,
611 &reader->src_len,
612 reader->buffer,
613 reader->buffer_size,
614 &reader->pktlen,
615 reader->options);
616 if (!reader->use_sideband)
617 break;
618 if (demultiplex_sideband(reader->me, reader->status,
619 reader->buffer, reader->pktlen, 1,
620 &scratch, &sideband_type))
621 break;
624 if (reader->status == PACKET_READ_NORMAL)
625 /* Skip the sideband designator if sideband is used */
626 reader->line = reader->use_sideband ?
627 reader->buffer + 1 : reader->buffer;
628 else
629 reader->line = NULL;
631 return reader->status;
634 enum packet_read_status packet_reader_peek(struct packet_reader *reader)
636 /* Only allow peeking a single line */
637 if (reader->line_peeked)
638 return reader->status;
640 /* Peek a line by reading it and setting peeked flag */
641 packet_reader_read(reader);
642 reader->line_peeked = 1;
643 return reader->status;
646 void packet_writer_init(struct packet_writer *writer, int dest_fd)
648 writer->dest_fd = dest_fd;
649 writer->use_sideband = 0;
652 void packet_writer_write(struct packet_writer *writer, const char *fmt, ...)
654 va_list args;
656 va_start(args, fmt);
657 packet_write_fmt_1(writer->dest_fd, 0,
658 writer->use_sideband ? "\001" : "", fmt, args);
659 va_end(args);
662 void packet_writer_error(struct packet_writer *writer, const char *fmt, ...)
664 va_list args;
666 va_start(args, fmt);
667 packet_write_fmt_1(writer->dest_fd, 0,
668 writer->use_sideband ? "\003" : "ERR ", fmt, args);
669 va_end(args);
672 void packet_writer_delim(struct packet_writer *writer)
674 packet_delim(writer->dest_fd);
677 void packet_writer_flush(struct packet_writer *writer)
679 packet_flush(writer->dest_fd);