The fifth batch
[alt-git.git] / pkt-line.c
bloba5bcbc96fb340f50ce6083aa2b7e9611d80de179
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 struct strbuf out;
43 static int in_pack, sideband;
45 if (!trace_want(&trace_packet) && !trace_want(&trace_pack))
46 return;
48 if (in_pack) {
49 if (packet_trace_pack(buf, len, sideband))
50 return;
51 } else if (starts_with(buf, "PACK") || starts_with(buf, "\1PACK")) {
52 in_pack = 1;
53 sideband = *buf == '\1';
54 packet_trace_pack(buf, len, sideband);
57 * Make a note in the human-readable trace that the pack data
58 * started.
60 buf = "PACK ...";
61 len = strlen(buf);
64 if (!trace_want(&trace_packet))
65 return;
67 /* +32 is just a guess for header + quoting */
68 strbuf_init(&out, len+32);
70 strbuf_addf(&out, "packet: %12s%c ",
71 get_trace_prefix(), write ? '>' : '<');
73 /* XXX we should really handle printable utf8 */
74 for (unsigned int i = 0; i < len; i++) {
75 /* suppress newlines */
76 if (buf[i] == '\n')
77 continue;
78 if (buf[i] >= 0x20 && buf[i] <= 0x7e)
79 strbuf_addch(&out, buf[i]);
80 else
81 strbuf_addf(&out, "\\%o", buf[i]);
84 strbuf_addch(&out, '\n');
85 trace_strbuf(&trace_packet, &out);
86 strbuf_release(&out);
90 * If we buffered things up above (we don't, but we should),
91 * we'd flush it here
93 void packet_flush(int fd)
95 packet_trace("0000", 4, 1);
96 if (write_in_full(fd, "0000", 4) < 0)
97 die_errno(_("unable to write flush packet"));
100 void packet_delim(int fd)
102 packet_trace("0001", 4, 1);
103 if (write_in_full(fd, "0001", 4) < 0)
104 die_errno(_("unable to write delim packet"));
107 void packet_response_end(int fd)
109 packet_trace("0002", 4, 1);
110 if (write_in_full(fd, "0002", 4) < 0)
111 die_errno(_("unable to write response end packet"));
114 int packet_flush_gently(int fd)
116 packet_trace("0000", 4, 1);
117 if (write_in_full(fd, "0000", 4) < 0)
118 return error(_("flush packet write failed"));
119 return 0;
122 void packet_buf_flush(struct strbuf *buf)
124 packet_trace("0000", 4, 1);
125 strbuf_add(buf, "0000", 4);
128 void packet_buf_delim(struct strbuf *buf)
130 packet_trace("0001", 4, 1);
131 strbuf_add(buf, "0001", 4);
134 void set_packet_header(char *buf, int size)
136 static char hexchar[] = "0123456789abcdef";
138 #define hex(a) (hexchar[(a) & 15])
139 buf[0] = hex(size >> 12);
140 buf[1] = hex(size >> 8);
141 buf[2] = hex(size >> 4);
142 buf[3] = hex(size);
143 #undef hex
146 static void format_packet(struct strbuf *out, const char *prefix,
147 const char *fmt, va_list args)
149 size_t orig_len, n;
151 orig_len = out->len;
152 strbuf_addstr(out, "0000");
153 strbuf_addstr(out, prefix);
154 strbuf_vaddf(out, fmt, args);
155 n = out->len - orig_len;
157 if (n > LARGE_PACKET_MAX)
158 die(_("protocol error: impossibly long line"));
160 set_packet_header(&out->buf[orig_len], n);
161 packet_trace(out->buf + orig_len + 4, n - 4, 1);
164 static int packet_write_fmt_1(int fd, int gently, const char *prefix,
165 const char *fmt, va_list args)
167 static struct strbuf buf = STRBUF_INIT;
169 strbuf_reset(&buf);
170 format_packet(&buf, prefix, fmt, args);
171 if (write_in_full(fd, buf.buf, buf.len) < 0) {
172 if (!gently) {
173 check_pipe(errno);
174 die_errno(_("packet write with format failed"));
176 return error(_("packet write with format failed"));
179 return 0;
182 void packet_write_fmt(int fd, const char *fmt, ...)
184 va_list args;
186 va_start(args, fmt);
187 packet_write_fmt_1(fd, 0, "", fmt, args);
188 va_end(args);
191 int packet_write_fmt_gently(int fd, const char *fmt, ...)
193 int status;
194 va_list args;
196 va_start(args, fmt);
197 status = packet_write_fmt_1(fd, 1, "", fmt, args);
198 va_end(args);
199 return status;
202 static int do_packet_write(const int fd_out, const char *buf, size_t size,
203 struct strbuf *err)
205 char header[4];
206 size_t packet_size;
208 if (size > LARGE_PACKET_DATA_MAX) {
209 strbuf_addstr(err, _("packet write failed - data exceeds max packet size"));
210 return -1;
213 packet_trace(buf, size, 1);
214 packet_size = size + 4;
216 set_packet_header(header, packet_size);
219 * Write the header and the buffer in 2 parts so that we do
220 * not need to allocate a buffer or rely on a static buffer.
221 * This also avoids putting a large buffer on the stack which
222 * might have multi-threading issues.
225 if (write_in_full(fd_out, header, 4) < 0 ||
226 write_in_full(fd_out, buf, size) < 0) {
227 strbuf_addf(err, _("packet write failed: %s"), strerror(errno));
228 return -1;
230 return 0;
233 static int packet_write_gently(const int fd_out, const char *buf, size_t size)
235 struct strbuf err = STRBUF_INIT;
236 if (do_packet_write(fd_out, buf, size, &err)) {
237 error("%s", err.buf);
238 strbuf_release(&err);
239 return -1;
241 return 0;
244 void packet_write(int fd_out, const char *buf, size_t size)
246 struct strbuf err = STRBUF_INIT;
247 if (do_packet_write(fd_out, buf, size, &err))
248 die("%s", err.buf);
251 void packet_fwrite(FILE *f, const char *buf, size_t size)
253 size_t packet_size;
254 char header[4];
256 if (size > LARGE_PACKET_DATA_MAX)
257 die(_("packet write failed - data exceeds max packet size"));
259 packet_trace(buf, size, 1);
260 packet_size = size + 4;
262 set_packet_header(header, packet_size);
263 fwrite_or_die(f, header, 4);
264 fwrite_or_die(f, buf, size);
267 void packet_fwrite_fmt(FILE *fh, const char *fmt, ...)
269 static struct strbuf buf = STRBUF_INIT;
270 va_list args;
272 strbuf_reset(&buf);
274 va_start(args, fmt);
275 format_packet(&buf, "", fmt, args);
276 va_end(args);
278 fwrite_or_die(fh, buf.buf, buf.len);
281 void packet_fflush(FILE *f)
283 packet_trace("0000", 4, 1);
284 fwrite_or_die(f, "0000", 4);
285 fflush_or_die(f);
288 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
290 va_list args;
292 va_start(args, fmt);
293 format_packet(buf, "", fmt, args);
294 va_end(args);
297 int write_packetized_from_fd_no_flush(int fd_in, int fd_out)
299 char *buf = xmalloc(LARGE_PACKET_DATA_MAX);
300 int err = 0;
301 ssize_t bytes_to_write;
303 while (!err) {
304 bytes_to_write = xread(fd_in, buf, LARGE_PACKET_DATA_MAX);
305 if (bytes_to_write < 0) {
306 free(buf);
307 return COPY_READ_ERROR;
309 if (bytes_to_write == 0)
310 break;
311 err = packet_write_gently(fd_out, buf, bytes_to_write);
313 free(buf);
314 return err;
317 int write_packetized_from_buf_no_flush_count(const char *src_in, size_t len,
318 int fd_out, int *packet_counter)
320 int err = 0;
321 size_t bytes_written = 0;
322 size_t bytes_to_write;
324 while (!err) {
325 if ((len - bytes_written) > LARGE_PACKET_DATA_MAX)
326 bytes_to_write = LARGE_PACKET_DATA_MAX;
327 else
328 bytes_to_write = len - bytes_written;
329 if (bytes_to_write == 0)
330 break;
331 err = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);
332 bytes_written += bytes_to_write;
333 if (packet_counter)
334 (*packet_counter)++;
336 return err;
339 static int get_packet_data(int fd, char **src_buf, size_t *src_size,
340 void *dst, size_t size, int options)
342 size_t bytes_read;
344 if (fd >= 0 && src_buf && *src_buf)
345 BUG("multiple sources given to packet_read");
347 /* Read up to "size" bytes from our source, whatever it is. */
348 if (src_buf && *src_buf) {
349 bytes_read = size < *src_size ? size : *src_size;
350 memcpy(dst, *src_buf, bytes_read);
351 *src_buf += bytes_read;
352 *src_size -= bytes_read;
353 } else {
354 ssize_t ret = read_in_full(fd, dst, size);
355 if (ret < 0) {
356 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
357 return error_errno(_("read error"));
358 die_errno(_("read error"));
361 bytes_read = (size_t) ret;
364 /* And complain if we didn't get enough bytes to satisfy the read. */
365 if (bytes_read != size) {
366 if (options & PACKET_READ_GENTLE_ON_EOF)
367 return -1;
369 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
370 return error(_("the remote end hung up unexpectedly"));
371 die(_("the remote end hung up unexpectedly"));
374 return 0;
377 int packet_length(const char lenbuf_hex[4], size_t size)
379 if (size < 4)
380 BUG("buffer too small");
381 return hexval(lenbuf_hex[0]) << 12 |
382 hexval(lenbuf_hex[1]) << 8 |
383 hexval(lenbuf_hex[2]) << 4 |
384 hexval(lenbuf_hex[3]);
387 static char *find_packfile_uri_path(const char *buffer)
389 const char *URI_MARK = "://";
390 char *path;
391 int len;
393 /* First char is sideband mark */
394 buffer += 1;
396 len = strspn(buffer, "0123456789abcdefABCDEF");
397 /* size of SHA1 and SHA256 hash */
398 if (!(len == 40 || len == 64) || buffer[len] != ' ')
399 return NULL; /* required "<hash>SP" not seen */
401 path = strstr(buffer + len + 1, URI_MARK);
402 if (!path)
403 return NULL;
405 path = strchr(path + strlen(URI_MARK), '/');
406 if (!path || !*(path + 1))
407 return NULL;
409 /* position after '/' */
410 return ++path;
413 enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
414 size_t *src_len, char *buffer,
415 unsigned size, int *pktlen,
416 int options)
418 int len;
419 char linelen[4];
420 char *uri_path_start;
422 if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) {
423 *pktlen = -1;
424 return PACKET_READ_EOF;
427 len = packet_length(linelen, sizeof(linelen));
429 if (len < 0) {
430 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
431 return error(_("protocol error: bad line length "
432 "character: %.4s"), linelen);
433 die(_("protocol error: bad line length character: %.4s"), linelen);
434 } else if (!len) {
435 packet_trace("0000", 4, 0);
436 *pktlen = 0;
437 return PACKET_READ_FLUSH;
438 } else if (len == 1) {
439 packet_trace("0001", 4, 0);
440 *pktlen = 0;
441 return PACKET_READ_DELIM;
442 } else if (len == 2) {
443 packet_trace("0002", 4, 0);
444 *pktlen = 0;
445 return PACKET_READ_RESPONSE_END;
446 } else if (len < 4) {
447 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
448 return error(_("protocol error: bad line length %d"),
449 len);
450 die(_("protocol error: bad line length %d"), len);
453 len -= 4;
454 if ((unsigned)len >= size) {
455 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
456 return error(_("protocol error: bad line length %d"),
457 len);
458 die(_("protocol error: bad line length %d"), len);
461 if (get_packet_data(fd, src_buffer, src_len, buffer, len, options) < 0) {
462 *pktlen = -1;
463 return PACKET_READ_EOF;
466 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
467 len && buffer[len-1] == '\n') {
468 if (options & PACKET_READ_USE_SIDEBAND) {
469 int band = *buffer & 0xff;
470 switch (band) {
471 case 1:
472 /* Chomp newline for payload */
473 len--;
474 break;
475 case 2:
476 case 3:
478 * Do not chomp newline for progress and error
479 * message.
481 break;
482 default:
484 * Bad sideband, let's leave it to
485 * demultiplex_sideband() to catch this error.
487 break;
489 } else {
490 len--;
494 buffer[len] = 0;
495 if (options & PACKET_READ_REDACT_URI_PATH &&
496 (uri_path_start = find_packfile_uri_path(buffer))) {
497 const char *redacted = "<redacted>";
498 struct strbuf tracebuf = STRBUF_INIT;
499 strbuf_insert(&tracebuf, 0, buffer, len);
500 strbuf_splice(&tracebuf, uri_path_start - buffer,
501 strlen(uri_path_start), redacted, strlen(redacted));
502 packet_trace(tracebuf.buf, tracebuf.len, 0);
503 strbuf_release(&tracebuf);
504 } else {
505 packet_trace(buffer, len, 0);
508 if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
509 starts_with(buffer, "ERR "))
510 die(_("remote error: %s"), buffer + 4);
512 *pktlen = len;
513 return PACKET_READ_NORMAL;
516 int packet_read(int fd, char *buffer, unsigned size, int options)
518 int pktlen = -1;
520 packet_read_with_status(fd, NULL, NULL, buffer, size, &pktlen,
521 options);
523 return pktlen;
526 char *packet_read_line(int fd, int *dst_len)
528 int len = packet_read(fd, packet_buffer, sizeof(packet_buffer),
529 PACKET_READ_CHOMP_NEWLINE);
530 if (dst_len)
531 *dst_len = len;
532 return (len > 0) ? packet_buffer : NULL;
535 int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
537 int len = packet_read(fd, packet_buffer, sizeof(packet_buffer),
538 PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
539 if (dst_len)
540 *dst_len = len;
541 if (dst_line)
542 *dst_line = (len > 0) ? packet_buffer : NULL;
543 return len;
546 ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out, int options)
548 int packet_len;
550 size_t orig_len = sb_out->len;
551 size_t orig_alloc = sb_out->alloc;
553 for (;;) {
554 strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);
555 packet_len = packet_read(fd_in,
556 /* strbuf_grow() above always allocates one extra byte to
557 * store a '\0' at the end of the string. packet_read()
558 * writes a '\0' extra byte at the end, too. Let it know
559 * that there is already room for the extra byte.
561 sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,
562 options);
563 if (packet_len <= 0)
564 break;
565 sb_out->len += packet_len;
568 if (packet_len < 0) {
569 if (orig_alloc == 0)
570 strbuf_release(sb_out);
571 else
572 strbuf_setlen(sb_out, orig_len);
573 return packet_len;
575 return sb_out->len - orig_len;
578 int recv_sideband(const char *me, int in_stream, int out)
580 char buf[LARGE_PACKET_MAX + 1];
581 int len;
582 struct strbuf scratch = STRBUF_INIT;
583 enum sideband_type sideband_type;
585 while (1) {
586 int status = packet_read_with_status(in_stream, NULL, NULL,
587 buf, LARGE_PACKET_MAX,
588 &len,
589 PACKET_READ_GENTLE_ON_EOF);
590 if (!demultiplex_sideband(me, status, buf, len, 0, &scratch,
591 &sideband_type))
592 continue;
593 switch (sideband_type) {
594 case SIDEBAND_PRIMARY:
595 write_or_die(out, buf + 1, len - 1);
596 break;
597 default: /* errors: message already written */
598 if (scratch.len > 0)
599 BUG("unhandled incomplete sideband: '%s'",
600 scratch.buf);
601 return sideband_type;
606 /* Packet Reader Functions */
607 void packet_reader_init(struct packet_reader *reader, int fd,
608 char *src_buffer, size_t src_len,
609 int options)
611 memset(reader, 0, sizeof(*reader));
613 reader->fd = fd;
614 reader->src_buffer = src_buffer;
615 reader->src_len = src_len;
616 reader->buffer = packet_buffer;
617 reader->buffer_size = sizeof(packet_buffer);
618 reader->options = options;
619 reader->me = "git";
620 reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
621 strbuf_init(&reader->scratch, 0);
624 enum packet_read_status packet_reader_read(struct packet_reader *reader)
626 if (reader->line_peeked) {
627 reader->line_peeked = 0;
628 return reader->status;
631 if (reader->use_sideband)
632 reader->options |= PACKET_READ_USE_SIDEBAND;
635 * Consume all progress packets until a primary payload packet is
636 * received
638 while (1) {
639 enum sideband_type sideband_type;
640 reader->status = packet_read_with_status(reader->fd,
641 &reader->src_buffer,
642 &reader->src_len,
643 reader->buffer,
644 reader->buffer_size,
645 &reader->pktlen,
646 reader->options);
647 if (!reader->use_sideband)
648 break;
649 if (demultiplex_sideband(reader->me, reader->status,
650 reader->buffer, reader->pktlen, 1,
651 &reader->scratch, &sideband_type))
652 break;
655 if (reader->status == PACKET_READ_NORMAL)
656 /* Skip the sideband designator if sideband is used */
657 reader->line = reader->use_sideband ?
658 reader->buffer + 1 : reader->buffer;
659 else
660 reader->line = NULL;
662 return reader->status;
665 enum packet_read_status packet_reader_peek(struct packet_reader *reader)
667 /* Only allow peeking a single line */
668 if (reader->line_peeked)
669 return reader->status;
671 /* Peek a line by reading it and setting peeked flag */
672 packet_reader_read(reader);
673 reader->line_peeked = 1;
674 return reader->status;
677 void packet_writer_init(struct packet_writer *writer, int dest_fd)
679 writer->dest_fd = dest_fd;
680 writer->use_sideband = 0;
683 void packet_writer_write(struct packet_writer *writer, const char *fmt, ...)
685 va_list args;
687 va_start(args, fmt);
688 packet_write_fmt_1(writer->dest_fd, 0,
689 writer->use_sideband ? "\001" : "", fmt, args);
690 va_end(args);
693 void packet_writer_error(struct packet_writer *writer, const char *fmt, ...)
695 va_list args;
697 va_start(args, fmt);
698 packet_write_fmt_1(writer->dest_fd, 0,
699 writer->use_sideband ? "\003" : "ERR ", fmt, args);
700 va_end(args);
703 void packet_writer_delim(struct packet_writer *writer)
705 packet_delim(writer->dest_fd);
708 void packet_writer_flush(struct packet_writer *writer)
710 packet_flush(writer->dest_fd);