4 #include "run-command.h"
6 char packet_buffer
[LARGE_PACKET_MAX
];
7 static const char *packet_trace_prefix
= "git";
8 static struct trace_key trace_packet
= TRACE_KEY_INIT(PACKET
);
9 static struct trace_key trace_pack
= TRACE_KEY_INIT(PACKFILE
);
11 void packet_trace_identity(const char *prog
)
13 packet_trace_prefix
= xstrdup(prog
);
16 static const char *get_trace_prefix(void)
18 return in_async() ? "sideband" : packet_trace_prefix
;
21 static int packet_trace_pack(const char *buf
, unsigned int len
, int sideband
)
24 trace_verbatim(&trace_pack
, buf
, len
);
26 } else if (len
&& *buf
== '\1') {
27 trace_verbatim(&trace_pack
, buf
+ 1, len
- 1);
30 /* it's another non-pack sideband */
35 static void packet_trace(const char *buf
, unsigned int len
, int write
)
39 static int in_pack
, sideband
;
41 if (!trace_want(&trace_packet
) && !trace_want(&trace_pack
))
45 if (packet_trace_pack(buf
, len
, sideband
))
47 } else if (starts_with(buf
, "PACK") || starts_with(buf
, "\1PACK")) {
49 sideband
= *buf
== '\1';
50 packet_trace_pack(buf
, len
, sideband
);
53 * Make a note in the human-readable trace that the pack data
60 if (!trace_want(&trace_packet
))
63 /* +32 is just a guess for header + quoting */
64 strbuf_init(&out
, len
+32);
66 strbuf_addf(&out
, "packet: %12s%c ",
67 get_trace_prefix(), write
? '>' : '<');
69 /* XXX we should really handle printable utf8 */
70 for (i
= 0; i
< len
; i
++) {
71 /* suppress newlines */
74 if (buf
[i
] >= 0x20 && buf
[i
] <= 0x7e)
75 strbuf_addch(&out
, buf
[i
]);
77 strbuf_addf(&out
, "\\%o", buf
[i
]);
80 strbuf_addch(&out
, '\n');
81 trace_strbuf(&trace_packet
, &out
);
86 * If we buffered things up above (we don't, but we should),
89 void packet_flush(int fd
)
91 packet_trace("0000", 4, 1);
92 if (write_in_full(fd
, "0000", 4) < 0)
93 die_errno(_("unable to write flush packet"));
96 void packet_delim(int fd
)
98 packet_trace("0001", 4, 1);
99 if (write_in_full(fd
, "0001", 4) < 0)
100 die_errno(_("unable to write delim packet"));
103 void packet_response_end(int fd
)
105 packet_trace("0002", 4, 1);
106 if (write_in_full(fd
, "0002", 4) < 0)
107 die_errno(_("unable to write response end packet"));
110 int packet_flush_gently(int fd
)
112 packet_trace("0000", 4, 1);
113 if (write_in_full(fd
, "0000", 4) < 0)
114 return error(_("flush packet write failed"));
118 void packet_buf_flush(struct strbuf
*buf
)
120 packet_trace("0000", 4, 1);
121 strbuf_add(buf
, "0000", 4);
124 void packet_buf_delim(struct strbuf
*buf
)
126 packet_trace("0001", 4, 1);
127 strbuf_add(buf
, "0001", 4);
130 void set_packet_header(char *buf
, int size
)
132 static char hexchar
[] = "0123456789abcdef";
134 #define hex(a) (hexchar[(a) & 15])
135 buf
[0] = hex(size
>> 12);
136 buf
[1] = hex(size
>> 8);
137 buf
[2] = hex(size
>> 4);
142 static void format_packet(struct strbuf
*out
, const char *prefix
,
143 const char *fmt
, va_list args
)
148 strbuf_addstr(out
, "0000");
149 strbuf_addstr(out
, prefix
);
150 strbuf_vaddf(out
, fmt
, args
);
151 n
= out
->len
- orig_len
;
153 if (n
> LARGE_PACKET_MAX
)
154 die(_("protocol error: impossibly long line"));
156 set_packet_header(&out
->buf
[orig_len
], n
);
157 packet_trace(out
->buf
+ orig_len
+ 4, n
- 4, 1);
160 static int packet_write_fmt_1(int fd
, int gently
, const char *prefix
,
161 const char *fmt
, va_list args
)
163 static struct strbuf buf
= STRBUF_INIT
;
166 format_packet(&buf
, prefix
, fmt
, args
);
167 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
170 die_errno(_("packet write with format failed"));
172 return error(_("packet write with format failed"));
178 void packet_write_fmt(int fd
, const char *fmt
, ...)
183 packet_write_fmt_1(fd
, 0, "", fmt
, args
);
187 int packet_write_fmt_gently(int fd
, const char *fmt
, ...)
193 status
= packet_write_fmt_1(fd
, 1, "", fmt
, args
);
198 static int do_packet_write(const int fd_out
, const char *buf
, size_t size
,
204 if (size
> LARGE_PACKET_DATA_MAX
) {
205 strbuf_addstr(err
, _("packet write failed - data exceeds max packet size"));
209 packet_trace(buf
, size
, 1);
210 packet_size
= size
+ 4;
212 set_packet_header(header
, packet_size
);
215 * Write the header and the buffer in 2 parts so that we do
216 * not need to allocate a buffer or rely on a static buffer.
217 * This also avoids putting a large buffer on the stack which
218 * might have multi-threading issues.
221 if (write_in_full(fd_out
, header
, 4) < 0 ||
222 write_in_full(fd_out
, buf
, size
) < 0) {
223 strbuf_addf(err
, _("packet write failed: %s"), strerror(errno
));
229 static int packet_write_gently(const int fd_out
, const char *buf
, size_t size
)
231 struct strbuf err
= STRBUF_INIT
;
232 if (do_packet_write(fd_out
, buf
, size
, &err
)) {
233 error("%s", err
.buf
);
234 strbuf_release(&err
);
240 void packet_write(int fd_out
, const char *buf
, size_t size
)
242 struct strbuf err
= STRBUF_INIT
;
243 if (do_packet_write(fd_out
, buf
, size
, &err
))
247 void packet_fwrite(FILE *f
, const char *buf
, size_t size
)
252 if (size
> LARGE_PACKET_DATA_MAX
)
253 die(_("packet write failed - data exceeds max packet size"));
255 packet_trace(buf
, size
, 1);
256 packet_size
= size
+ 4;
258 set_packet_header(header
, packet_size
);
259 fwrite_or_die(f
, header
, 4);
260 fwrite_or_die(f
, buf
, size
);
263 void packet_fwrite_fmt(FILE *fh
, const char *fmt
, ...)
265 static struct strbuf buf
= STRBUF_INIT
;
271 format_packet(&buf
, "", fmt
, args
);
274 fwrite_or_die(fh
, buf
.buf
, buf
.len
);
277 void packet_fflush(FILE *f
)
279 packet_trace("0000", 4, 1);
280 fwrite_or_die(f
, "0000", 4);
284 void packet_buf_write(struct strbuf
*buf
, const char *fmt
, ...)
289 format_packet(buf
, "", fmt
, args
);
293 int write_packetized_from_fd_no_flush(int fd_in
, int fd_out
)
295 char *buf
= xmalloc(LARGE_PACKET_DATA_MAX
);
297 ssize_t bytes_to_write
;
300 bytes_to_write
= xread(fd_in
, buf
, LARGE_PACKET_DATA_MAX
);
301 if (bytes_to_write
< 0) {
303 return COPY_READ_ERROR
;
305 if (bytes_to_write
== 0)
307 err
= packet_write_gently(fd_out
, buf
, bytes_to_write
);
313 int write_packetized_from_buf_no_flush_count(const char *src_in
, size_t len
,
314 int fd_out
, int *packet_counter
)
317 size_t bytes_written
= 0;
318 size_t bytes_to_write
;
321 if ((len
- bytes_written
) > LARGE_PACKET_DATA_MAX
)
322 bytes_to_write
= LARGE_PACKET_DATA_MAX
;
324 bytes_to_write
= len
- bytes_written
;
325 if (bytes_to_write
== 0)
327 err
= packet_write_gently(fd_out
, src_in
+ bytes_written
, bytes_to_write
);
328 bytes_written
+= bytes_to_write
;
335 static int get_packet_data(int fd
, char **src_buf
, size_t *src_size
,
336 void *dst
, unsigned size
, int options
)
340 if (fd
>= 0 && src_buf
&& *src_buf
)
341 BUG("multiple sources given to packet_read");
343 /* Read up to "size" bytes from our source, whatever it is. */
344 if (src_buf
&& *src_buf
) {
345 ret
= size
< *src_size
? size
: *src_size
;
346 memcpy(dst
, *src_buf
, ret
);
350 ret
= read_in_full(fd
, dst
, size
);
352 if (options
& PACKET_READ_GENTLE_ON_READ_ERROR
)
353 return error_errno(_("read error"));
354 die_errno(_("read error"));
358 /* And complain if we didn't get enough bytes to satisfy the read. */
360 if (options
& PACKET_READ_GENTLE_ON_EOF
)
363 if (options
& PACKET_READ_GENTLE_ON_READ_ERROR
)
364 return error(_("the remote end hung up unexpectedly"));
365 die(_("the remote end hung up unexpectedly"));
371 int packet_length(const char lenbuf_hex
[4])
373 int val
= hex2chr(lenbuf_hex
);
374 return (val
< 0) ? val
: (val
<< 8) | hex2chr(lenbuf_hex
+ 2);
377 static char *find_packfile_uri_path(const char *buffer
)
379 const char *URI_MARK
= "://";
383 /* First char is sideband mark */
386 len
= strspn(buffer
, "0123456789abcdefABCDEF");
387 /* size of SHA1 and SHA256 hash */
388 if (!(len
== 40 || len
== 64) || buffer
[len
] != ' ')
389 return NULL
; /* required "<hash>SP" not seen */
391 path
= strstr(buffer
+ len
+ 1, URI_MARK
);
395 path
= strchr(path
+ strlen(URI_MARK
), '/');
396 if (!path
|| !*(path
+ 1))
399 /* position after '/' */
403 enum packet_read_status
packet_read_with_status(int fd
, char **src_buffer
,
404 size_t *src_len
, char *buffer
,
405 unsigned size
, int *pktlen
,
410 char *uri_path_start
;
412 if (get_packet_data(fd
, src_buffer
, src_len
, linelen
, 4, options
) < 0) {
414 return PACKET_READ_EOF
;
417 len
= packet_length(linelen
);
420 if (options
& PACKET_READ_GENTLE_ON_READ_ERROR
)
421 return error(_("protocol error: bad line length "
422 "character: %.4s"), linelen
);
423 die(_("protocol error: bad line length character: %.4s"), linelen
);
425 packet_trace("0000", 4, 0);
427 return PACKET_READ_FLUSH
;
428 } else if (len
== 1) {
429 packet_trace("0001", 4, 0);
431 return PACKET_READ_DELIM
;
432 } else if (len
== 2) {
433 packet_trace("0002", 4, 0);
435 return PACKET_READ_RESPONSE_END
;
436 } else if (len
< 4) {
437 if (options
& PACKET_READ_GENTLE_ON_READ_ERROR
)
438 return error(_("protocol error: bad line length %d"),
440 die(_("protocol error: bad line length %d"), len
);
444 if ((unsigned)len
>= size
) {
445 if (options
& PACKET_READ_GENTLE_ON_READ_ERROR
)
446 return error(_("protocol error: bad line length %d"),
448 die(_("protocol error: bad line length %d"), len
);
451 if (get_packet_data(fd
, src_buffer
, src_len
, buffer
, len
, options
) < 0) {
453 return PACKET_READ_EOF
;
456 if ((options
& PACKET_READ_CHOMP_NEWLINE
) &&
457 len
&& buffer
[len
-1] == '\n')
461 if (options
& PACKET_READ_REDACT_URI_PATH
&&
462 (uri_path_start
= find_packfile_uri_path(buffer
))) {
463 const char *redacted
= "<redacted>";
464 struct strbuf tracebuf
= STRBUF_INIT
;
465 strbuf_insert(&tracebuf
, 0, buffer
, len
);
466 strbuf_splice(&tracebuf
, uri_path_start
- buffer
,
467 strlen(uri_path_start
), redacted
, strlen(redacted
));
468 packet_trace(tracebuf
.buf
, tracebuf
.len
, 0);
469 strbuf_release(&tracebuf
);
471 packet_trace(buffer
, len
, 0);
474 if ((options
& PACKET_READ_DIE_ON_ERR_PACKET
) &&
475 starts_with(buffer
, "ERR "))
476 die(_("remote error: %s"), buffer
+ 4);
479 return PACKET_READ_NORMAL
;
482 int packet_read(int fd
, char *buffer
, unsigned size
, int options
)
486 packet_read_with_status(fd
, NULL
, NULL
, buffer
, size
, &pktlen
,
492 char *packet_read_line(int fd
, int *dst_len
)
494 int len
= packet_read(fd
, packet_buffer
, sizeof(packet_buffer
),
495 PACKET_READ_CHOMP_NEWLINE
);
498 return (len
> 0) ? packet_buffer
: NULL
;
501 int packet_read_line_gently(int fd
, int *dst_len
, char **dst_line
)
503 int len
= packet_read(fd
, packet_buffer
, sizeof(packet_buffer
),
504 PACKET_READ_CHOMP_NEWLINE
|PACKET_READ_GENTLE_ON_EOF
);
508 *dst_line
= (len
> 0) ? packet_buffer
: NULL
;
512 ssize_t
read_packetized_to_strbuf(int fd_in
, struct strbuf
*sb_out
, int options
)
516 size_t orig_len
= sb_out
->len
;
517 size_t orig_alloc
= sb_out
->alloc
;
520 strbuf_grow(sb_out
, LARGE_PACKET_DATA_MAX
);
521 packet_len
= packet_read(fd_in
,
522 /* strbuf_grow() above always allocates one extra byte to
523 * store a '\0' at the end of the string. packet_read()
524 * writes a '\0' extra byte at the end, too. Let it know
525 * that there is already room for the extra byte.
527 sb_out
->buf
+ sb_out
->len
, LARGE_PACKET_DATA_MAX
+1,
531 sb_out
->len
+= packet_len
;
534 if (packet_len
< 0) {
536 strbuf_release(sb_out
);
538 strbuf_setlen(sb_out
, orig_len
);
541 return sb_out
->len
- orig_len
;
544 int recv_sideband(const char *me
, int in_stream
, int out
)
546 char buf
[LARGE_PACKET_MAX
+ 1];
548 struct strbuf scratch
= STRBUF_INIT
;
549 enum sideband_type sideband_type
;
552 int status
= packet_read_with_status(in_stream
, NULL
, NULL
,
553 buf
, LARGE_PACKET_MAX
,
555 PACKET_READ_GENTLE_ON_EOF
);
556 if (!demultiplex_sideband(me
, status
, buf
, len
, 0, &scratch
,
559 switch (sideband_type
) {
560 case SIDEBAND_PRIMARY
:
561 write_or_die(out
, buf
+ 1, len
- 1);
563 default: /* errors: message already written */
565 BUG("unhandled incomplete sideband: '%s'",
567 return sideband_type
;
572 /* Packet Reader Functions */
573 void packet_reader_init(struct packet_reader
*reader
, int fd
,
574 char *src_buffer
, size_t src_len
,
577 memset(reader
, 0, sizeof(*reader
));
580 reader
->src_buffer
= src_buffer
;
581 reader
->src_len
= src_len
;
582 reader
->buffer
= packet_buffer
;
583 reader
->buffer_size
= sizeof(packet_buffer
);
584 reader
->options
= options
;
586 reader
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
589 enum packet_read_status
packet_reader_read(struct packet_reader
*reader
)
591 struct strbuf scratch
= STRBUF_INIT
;
593 if (reader
->line_peeked
) {
594 reader
->line_peeked
= 0;
595 return reader
->status
;
599 * Consume all progress packets until a primary payload packet is
603 enum sideband_type sideband_type
;
604 reader
->status
= packet_read_with_status(reader
->fd
,
611 if (!reader
->use_sideband
)
613 if (demultiplex_sideband(reader
->me
, reader
->status
,
614 reader
->buffer
, reader
->pktlen
, 1,
615 &scratch
, &sideband_type
))
619 if (reader
->status
== PACKET_READ_NORMAL
)
620 /* Skip the sideband designator if sideband is used */
621 reader
->line
= reader
->use_sideband
?
622 reader
->buffer
+ 1 : reader
->buffer
;
626 return reader
->status
;
629 enum packet_read_status
packet_reader_peek(struct packet_reader
*reader
)
631 /* Only allow peeking a single line */
632 if (reader
->line_peeked
)
633 return reader
->status
;
635 /* Peek a line by reading it and setting peeked flag */
636 packet_reader_read(reader
);
637 reader
->line_peeked
= 1;
638 return reader
->status
;
641 void packet_writer_init(struct packet_writer
*writer
, int dest_fd
)
643 writer
->dest_fd
= dest_fd
;
644 writer
->use_sideband
= 0;
647 void packet_writer_write(struct packet_writer
*writer
, const char *fmt
, ...)
652 packet_write_fmt_1(writer
->dest_fd
, 0,
653 writer
->use_sideband
? "\001" : "", fmt
, args
);
657 void packet_writer_error(struct packet_writer
*writer
, const char *fmt
, ...)
662 packet_write_fmt_1(writer
->dest_fd
, 0,
663 writer
->use_sideband
? "\003" : "ERR ", fmt
, args
);
667 void packet_writer_delim(struct packet_writer
*writer
)
669 packet_delim(writer
->dest_fd
);
672 void packet_writer_flush(struct packet_writer
*writer
)
674 packet_flush(writer
->dest_fd
);