1 #include "git-compat-util.h"
6 #include "run-command.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
)
29 trace_verbatim(&trace_pack
, buf
, len
);
31 } else if (len
&& *buf
== '\1') {
32 trace_verbatim(&trace_pack
, buf
+ 1, len
- 1);
35 /* it's another non-pack sideband */
40 static void packet_trace(const char *buf
, unsigned int len
, int write
)
44 static int in_pack
, sideband
;
46 if (!trace_want(&trace_packet
) && !trace_want(&trace_pack
))
50 if (packet_trace_pack(buf
, len
, sideband
))
52 } else if (starts_with(buf
, "PACK") || starts_with(buf
, "\1PACK")) {
54 sideband
= *buf
== '\1';
55 packet_trace_pack(buf
, len
, sideband
);
58 * Make a note in the human-readable trace that the pack data
65 if (!trace_want(&trace_packet
))
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 */
79 if (buf
[i
] >= 0x20 && buf
[i
] <= 0x7e)
80 strbuf_addch(&out
, buf
[i
]);
82 strbuf_addf(&out
, "\\%o", buf
[i
]);
85 strbuf_addch(&out
, '\n');
86 trace_strbuf(&trace_packet
, &out
);
91 * If we buffered things up above (we don't, but we should),
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"));
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);
147 static void format_packet(struct strbuf
*out
, const char *prefix
,
148 const char *fmt
, va_list args
)
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
;
171 format_packet(&buf
, prefix
, fmt
, args
);
172 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
175 die_errno(_("packet write with format failed"));
177 return error(_("packet write with format failed"));
183 void packet_write_fmt(int fd
, const char *fmt
, ...)
188 packet_write_fmt_1(fd
, 0, "", fmt
, args
);
192 int packet_write_fmt_gently(int fd
, const char *fmt
, ...)
198 status
= packet_write_fmt_1(fd
, 1, "", fmt
, args
);
203 static int do_packet_write(const int fd_out
, const char *buf
, size_t size
,
209 if (size
> LARGE_PACKET_DATA_MAX
) {
210 strbuf_addstr(err
, _("packet write failed - data exceeds max packet size"));
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
));
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
);
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
))
252 void packet_fwrite(FILE *f
, const char *buf
, size_t size
)
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
;
276 format_packet(&buf
, "", fmt
, 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);
289 void packet_buf_write(struct strbuf
*buf
, const char *fmt
, ...)
294 format_packet(buf
, "", fmt
, args
);
298 int write_packetized_from_fd_no_flush(int fd_in
, int fd_out
)
300 char *buf
= xmalloc(LARGE_PACKET_DATA_MAX
);
302 ssize_t bytes_to_write
;
305 bytes_to_write
= xread(fd_in
, buf
, LARGE_PACKET_DATA_MAX
);
306 if (bytes_to_write
< 0) {
308 return COPY_READ_ERROR
;
310 if (bytes_to_write
== 0)
312 err
= packet_write_gently(fd_out
, buf
, bytes_to_write
);
318 int write_packetized_from_buf_no_flush_count(const char *src_in
, size_t len
,
319 int fd_out
, int *packet_counter
)
322 size_t bytes_written
= 0;
323 size_t bytes_to_write
;
326 if ((len
- bytes_written
) > LARGE_PACKET_DATA_MAX
)
327 bytes_to_write
= LARGE_PACKET_DATA_MAX
;
329 bytes_to_write
= len
- bytes_written
;
330 if (bytes_to_write
== 0)
332 err
= packet_write_gently(fd_out
, src_in
+ bytes_written
, bytes_to_write
);
333 bytes_written
+= bytes_to_write
;
340 static int get_packet_data(int fd
, char **src_buf
, size_t *src_size
,
341 void *dst
, unsigned size
, int options
)
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
);
355 ret
= read_in_full(fd
, dst
, size
);
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. */
365 if (options
& PACKET_READ_GENTLE_ON_EOF
)
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"));
376 int packet_length(const char lenbuf_hex
[4], size_t size
)
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
= "://";
392 /* First char is sideband mark */
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
);
404 path
= strchr(path
+ strlen(URI_MARK
), '/');
405 if (!path
|| !*(path
+ 1))
408 /* position after '/' */
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
,
419 char *uri_path_start
;
421 if (get_packet_data(fd
, src_buffer
, src_len
, linelen
, 4, options
) < 0) {
423 return PACKET_READ_EOF
;
426 len
= packet_length(linelen
, sizeof(linelen
));
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
);
434 packet_trace("0000", 4, 0);
436 return PACKET_READ_FLUSH
;
437 } else if (len
== 1) {
438 packet_trace("0001", 4, 0);
440 return PACKET_READ_DELIM
;
441 } else if (len
== 2) {
442 packet_trace("0002", 4, 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"),
449 die(_("protocol error: bad line length %d"), len
);
453 if ((unsigned)len
>= size
) {
454 if (options
& PACKET_READ_GENTLE_ON_READ_ERROR
)
455 return error(_("protocol error: bad line length %d"),
457 die(_("protocol error: bad line length %d"), len
);
460 if (get_packet_data(fd
, src_buffer
, src_len
, buffer
, len
, options
) < 0) {
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;
471 /* Chomp newline for payload */
477 * Do not chomp newline for progress and error
483 * Bad sideband, let's leave it to
484 * demultiplex_sideband() to catch this error.
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
);
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);
512 return PACKET_READ_NORMAL
;
515 int packet_read(int fd
, char *buffer
, unsigned size
, int options
)
519 packet_read_with_status(fd
, NULL
, NULL
, buffer
, size
, &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
);
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
);
541 *dst_line
= (len
> 0) ? packet_buffer
: NULL
;
545 ssize_t
read_packetized_to_strbuf(int fd_in
, struct strbuf
*sb_out
, int options
)
549 size_t orig_len
= sb_out
->len
;
550 size_t orig_alloc
= sb_out
->alloc
;
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,
564 sb_out
->len
+= packet_len
;
567 if (packet_len
< 0) {
569 strbuf_release(sb_out
);
571 strbuf_setlen(sb_out
, orig_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];
581 struct strbuf scratch
= STRBUF_INIT
;
582 enum sideband_type sideband_type
;
585 int status
= packet_read_with_status(in_stream
, NULL
, NULL
,
586 buf
, LARGE_PACKET_MAX
,
588 PACKET_READ_GENTLE_ON_EOF
);
589 if (!demultiplex_sideband(me
, status
, buf
, len
, 0, &scratch
,
592 switch (sideband_type
) {
593 case SIDEBAND_PRIMARY
:
594 write_or_die(out
, buf
+ 1, len
- 1);
596 default: /* errors: message already written */
598 BUG("unhandled incomplete sideband: '%s'",
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
,
610 memset(reader
, 0, sizeof(*reader
));
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
;
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
638 enum sideband_type sideband_type
;
639 reader
->status
= packet_read_with_status(reader
->fd
,
646 if (!reader
->use_sideband
)
648 if (demultiplex_sideband(reader
->me
, reader
->status
,
649 reader
->buffer
, reader
->pktlen
, 1,
650 &reader
->scratch
, &sideband_type
))
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
;
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
, ...)
687 packet_write_fmt_1(writer
->dest_fd
, 0,
688 writer
->use_sideband
? "\001" : "", fmt
, args
);
692 void packet_writer_error(struct packet_writer
*writer
, const char *fmt
, ...)
697 packet_write_fmt_1(writer
->dest_fd
, 0,
698 writer
->use_sideband
? "\003" : "ERR ", fmt
, 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
);