reftable/reader: introduce refcounting
[git/gitster.git] / pkt-line.c
blob24479eae4dbe2a44b15a10f86b15bfc6f865bb34
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 "sideband.h"
8 #include "trace.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], size_t size)
378 if (size < 4)
379 BUG("buffer too small");
380 return hexval(lenbuf_hex[0]) << 12 |
381 hexval(lenbuf_hex[1]) << 8 |
382 hexval(lenbuf_hex[2]) << 4 |
383 hexval(lenbuf_hex[3]);
386 static char *find_packfile_uri_path(const char *buffer)
388 const char *URI_MARK = "://";
389 char *path;
390 int len;
392 /* First char is sideband mark */
393 buffer += 1;
395 len = strspn(buffer, "0123456789abcdefABCDEF");
396 /* size of SHA1 and SHA256 hash */
397 if (!(len == 40 || len == 64) || buffer[len] != ' ')
398 return NULL; /* required "<hash>SP" not seen */
400 path = strstr(buffer + len + 1, URI_MARK);
401 if (!path)
402 return NULL;
404 path = strchr(path + strlen(URI_MARK), '/');
405 if (!path || !*(path + 1))
406 return NULL;
408 /* position after '/' */
409 return ++path;
412 enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
413 size_t *src_len, char *buffer,
414 unsigned size, int *pktlen,
415 int options)
417 int len;
418 char linelen[4];
419 char *uri_path_start;
421 if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) {
422 *pktlen = -1;
423 return PACKET_READ_EOF;
426 len = packet_length(linelen, sizeof(linelen));
428 if (len < 0) {
429 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
430 return error(_("protocol error: bad line length "
431 "character: %.4s"), linelen);
432 die(_("protocol error: bad line length character: %.4s"), linelen);
433 } else if (!len) {
434 packet_trace("0000", 4, 0);
435 *pktlen = 0;
436 return PACKET_READ_FLUSH;
437 } else if (len == 1) {
438 packet_trace("0001", 4, 0);
439 *pktlen = 0;
440 return PACKET_READ_DELIM;
441 } else if (len == 2) {
442 packet_trace("0002", 4, 0);
443 *pktlen = 0;
444 return PACKET_READ_RESPONSE_END;
445 } else if (len < 4) {
446 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
447 return error(_("protocol error: bad line length %d"),
448 len);
449 die(_("protocol error: bad line length %d"), len);
452 len -= 4;
453 if ((unsigned)len >= size) {
454 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
455 return error(_("protocol error: bad line length %d"),
456 len);
457 die(_("protocol error: bad line length %d"), len);
460 if (get_packet_data(fd, src_buffer, src_len, buffer, len, options) < 0) {
461 *pktlen = -1;
462 return PACKET_READ_EOF;
465 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
466 len && buffer[len-1] == '\n') {
467 if (options & PACKET_READ_USE_SIDEBAND) {
468 int band = *buffer & 0xff;
469 switch (band) {
470 case 1:
471 /* Chomp newline for payload */
472 len--;
473 break;
474 case 2:
475 case 3:
477 * Do not chomp newline for progress and error
478 * message.
480 break;
481 default:
483 * Bad sideband, let's leave it to
484 * demultiplex_sideband() to catch this error.
486 break;
488 } else {
489 len--;
493 buffer[len] = 0;
494 if (options & PACKET_READ_REDACT_URI_PATH &&
495 (uri_path_start = find_packfile_uri_path(buffer))) {
496 const char *redacted = "<redacted>";
497 struct strbuf tracebuf = STRBUF_INIT;
498 strbuf_insert(&tracebuf, 0, buffer, len);
499 strbuf_splice(&tracebuf, uri_path_start - buffer,
500 strlen(uri_path_start), redacted, strlen(redacted));
501 packet_trace(tracebuf.buf, tracebuf.len, 0);
502 strbuf_release(&tracebuf);
503 } else {
504 packet_trace(buffer, len, 0);
507 if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
508 starts_with(buffer, "ERR "))
509 die(_("remote error: %s"), buffer + 4);
511 *pktlen = len;
512 return PACKET_READ_NORMAL;
515 int packet_read(int fd, char *buffer, unsigned size, int options)
517 int pktlen = -1;
519 packet_read_with_status(fd, NULL, NULL, buffer, size, &pktlen,
520 options);
522 return pktlen;
525 char *packet_read_line(int fd, int *dst_len)
527 int len = packet_read(fd, packet_buffer, sizeof(packet_buffer),
528 PACKET_READ_CHOMP_NEWLINE);
529 if (dst_len)
530 *dst_len = len;
531 return (len > 0) ? packet_buffer : NULL;
534 int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
536 int len = packet_read(fd, packet_buffer, sizeof(packet_buffer),
537 PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
538 if (dst_len)
539 *dst_len = len;
540 if (dst_line)
541 *dst_line = (len > 0) ? packet_buffer : NULL;
542 return len;
545 ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out, int options)
547 int packet_len;
549 size_t orig_len = sb_out->len;
550 size_t orig_alloc = sb_out->alloc;
552 for (;;) {
553 strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);
554 packet_len = packet_read(fd_in,
555 /* strbuf_grow() above always allocates one extra byte to
556 * store a '\0' at the end of the string. packet_read()
557 * writes a '\0' extra byte at the end, too. Let it know
558 * that there is already room for the extra byte.
560 sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,
561 options);
562 if (packet_len <= 0)
563 break;
564 sb_out->len += packet_len;
567 if (packet_len < 0) {
568 if (orig_alloc == 0)
569 strbuf_release(sb_out);
570 else
571 strbuf_setlen(sb_out, orig_len);
572 return packet_len;
574 return sb_out->len - orig_len;
577 int recv_sideband(const char *me, int in_stream, int out)
579 char buf[LARGE_PACKET_MAX + 1];
580 int len;
581 struct strbuf scratch = STRBUF_INIT;
582 enum sideband_type sideband_type;
584 while (1) {
585 int status = packet_read_with_status(in_stream, NULL, NULL,
586 buf, LARGE_PACKET_MAX,
587 &len,
588 PACKET_READ_GENTLE_ON_EOF);
589 if (!demultiplex_sideband(me, status, buf, len, 0, &scratch,
590 &sideband_type))
591 continue;
592 switch (sideband_type) {
593 case SIDEBAND_PRIMARY:
594 write_or_die(out, buf + 1, len - 1);
595 break;
596 default: /* errors: message already written */
597 if (scratch.len > 0)
598 BUG("unhandled incomplete sideband: '%s'",
599 scratch.buf);
600 return sideband_type;
605 /* Packet Reader Functions */
606 void packet_reader_init(struct packet_reader *reader, int fd,
607 char *src_buffer, size_t src_len,
608 int options)
610 memset(reader, 0, sizeof(*reader));
612 reader->fd = fd;
613 reader->src_buffer = src_buffer;
614 reader->src_len = src_len;
615 reader->buffer = packet_buffer;
616 reader->buffer_size = sizeof(packet_buffer);
617 reader->options = options;
618 reader->me = "git";
619 reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
620 strbuf_init(&reader->scratch, 0);
623 enum packet_read_status packet_reader_read(struct packet_reader *reader)
625 if (reader->line_peeked) {
626 reader->line_peeked = 0;
627 return reader->status;
630 if (reader->use_sideband)
631 reader->options |= PACKET_READ_USE_SIDEBAND;
634 * Consume all progress packets until a primary payload packet is
635 * received
637 while (1) {
638 enum sideband_type sideband_type;
639 reader->status = packet_read_with_status(reader->fd,
640 &reader->src_buffer,
641 &reader->src_len,
642 reader->buffer,
643 reader->buffer_size,
644 &reader->pktlen,
645 reader->options);
646 if (!reader->use_sideband)
647 break;
648 if (demultiplex_sideband(reader->me, reader->status,
649 reader->buffer, reader->pktlen, 1,
650 &reader->scratch, &sideband_type))
651 break;
654 if (reader->status == PACKET_READ_NORMAL)
655 /* Skip the sideband designator if sideband is used */
656 reader->line = reader->use_sideband ?
657 reader->buffer + 1 : reader->buffer;
658 else
659 reader->line = NULL;
661 return reader->status;
664 enum packet_read_status packet_reader_peek(struct packet_reader *reader)
666 /* Only allow peeking a single line */
667 if (reader->line_peeked)
668 return reader->status;
670 /* Peek a line by reading it and setting peeked flag */
671 packet_reader_read(reader);
672 reader->line_peeked = 1;
673 return reader->status;
676 void packet_writer_init(struct packet_writer *writer, int dest_fd)
678 writer->dest_fd = dest_fd;
679 writer->use_sideband = 0;
682 void packet_writer_write(struct packet_writer *writer, const char *fmt, ...)
684 va_list args;
686 va_start(args, fmt);
687 packet_write_fmt_1(writer->dest_fd, 0,
688 writer->use_sideband ? "\001" : "", fmt, args);
689 va_end(args);
692 void packet_writer_error(struct packet_writer *writer, const char *fmt, ...)
694 va_list args;
696 va_start(args, fmt);
697 packet_write_fmt_1(writer->dest_fd, 0,
698 writer->use_sideband ? "\003" : "ERR ", fmt, args);
699 va_end(args);
702 void packet_writer_delim(struct packet_writer *writer)
704 packet_delim(writer->dest_fd);
707 void packet_writer_flush(struct packet_writer *writer)
709 packet_flush(writer->dest_fd);