3 #include "run-command.h"
5 char packet_buffer
[LARGE_PACKET_MAX
];
6 static const char *packet_trace_prefix
= "git";
7 static struct trace_key trace_packet
= TRACE_KEY_INIT(PACKET
);
8 static struct trace_key trace_pack
= TRACE_KEY_INIT(PACKFILE
);
10 void packet_trace_identity(const char *prog
)
12 packet_trace_prefix
= xstrdup(prog
);
15 static const char *get_trace_prefix(void)
17 return in_async() ? "sideband" : packet_trace_prefix
;
20 static int packet_trace_pack(const char *buf
, unsigned int len
, int sideband
)
23 trace_verbatim(&trace_pack
, buf
, len
);
25 } else if (len
&& *buf
== '\1') {
26 trace_verbatim(&trace_pack
, buf
+ 1, len
- 1);
29 /* it's another non-pack sideband */
34 static void packet_trace(const char *buf
, unsigned int len
, int write
)
38 static int in_pack
, sideband
;
40 if (!trace_want(&trace_packet
) && !trace_want(&trace_pack
))
44 if (packet_trace_pack(buf
, len
, sideband
))
46 } else if (starts_with(buf
, "PACK") || starts_with(buf
, "\1PACK")) {
48 sideband
= *buf
== '\1';
49 packet_trace_pack(buf
, len
, sideband
);
52 * Make a note in the human-readable trace that the pack data
59 if (!trace_want(&trace_packet
))
62 /* +32 is just a guess for header + quoting */
63 strbuf_init(&out
, len
+32);
65 strbuf_addf(&out
, "packet: %12s%c ",
66 get_trace_prefix(), write
? '>' : '<');
68 /* XXX we should really handle printable utf8 */
69 for (i
= 0; i
< len
; i
++) {
70 /* suppress newlines */
73 if (buf
[i
] >= 0x20 && buf
[i
] <= 0x7e)
74 strbuf_addch(&out
, buf
[i
]);
76 strbuf_addf(&out
, "\\%o", buf
[i
]);
79 strbuf_addch(&out
, '\n');
80 trace_strbuf(&trace_packet
, &out
);
85 * If we buffered things up above (we don't, but we should),
88 void packet_flush(int fd
)
90 packet_trace("0000", 4, 1);
91 if (write_in_full(fd
, "0000", 4) < 0)
92 die_errno(_("unable to write flush packet"));
95 void packet_delim(int fd
)
97 packet_trace("0001", 4, 1);
98 if (write_in_full(fd
, "0001", 4) < 0)
99 die_errno(_("unable to write delim packet"));
102 void packet_response_end(int fd
)
104 packet_trace("0002", 4, 1);
105 if (write_in_full(fd
, "0002", 4) < 0)
106 die_errno(_("unable to write response end packet"));
109 int packet_flush_gently(int fd
)
111 packet_trace("0000", 4, 1);
112 if (write_in_full(fd
, "0000", 4) < 0)
113 return error(_("flush packet write failed"));
117 void packet_buf_flush(struct strbuf
*buf
)
119 packet_trace("0000", 4, 1);
120 strbuf_add(buf
, "0000", 4);
123 void packet_buf_delim(struct strbuf
*buf
)
125 packet_trace("0001", 4, 1);
126 strbuf_add(buf
, "0001", 4);
129 void set_packet_header(char *buf
, int size
)
131 static char hexchar
[] = "0123456789abcdef";
133 #define hex(a) (hexchar[(a) & 15])
134 buf
[0] = hex(size
>> 12);
135 buf
[1] = hex(size
>> 8);
136 buf
[2] = hex(size
>> 4);
141 static void format_packet(struct strbuf
*out
, const char *prefix
,
142 const char *fmt
, va_list args
)
147 strbuf_addstr(out
, "0000");
148 strbuf_addstr(out
, prefix
);
149 strbuf_vaddf(out
, fmt
, args
);
150 n
= out
->len
- orig_len
;
152 if (n
> LARGE_PACKET_MAX
)
153 die(_("protocol error: impossibly long line"));
155 set_packet_header(&out
->buf
[orig_len
], n
);
156 packet_trace(out
->buf
+ orig_len
+ 4, n
- 4, 1);
159 static int packet_write_fmt_1(int fd
, int gently
, const char *prefix
,
160 const char *fmt
, va_list args
)
162 static struct strbuf buf
= STRBUF_INIT
;
165 format_packet(&buf
, prefix
, fmt
, args
);
166 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
169 die_errno(_("packet write with format failed"));
171 return error(_("packet write with format failed"));
177 void packet_write_fmt(int fd
, const char *fmt
, ...)
182 packet_write_fmt_1(fd
, 0, "", fmt
, args
);
186 int packet_write_fmt_gently(int fd
, const char *fmt
, ...)
192 status
= packet_write_fmt_1(fd
, 1, "", fmt
, args
);
197 static int do_packet_write(const int fd_out
, const char *buf
, size_t size
,
203 if (size
> LARGE_PACKET_DATA_MAX
) {
204 strbuf_addstr(err
, _("packet write failed - data exceeds max packet size"));
208 packet_trace(buf
, size
, 1);
209 packet_size
= size
+ 4;
211 set_packet_header(header
, packet_size
);
214 * Write the header and the buffer in 2 parts so that we do
215 * not need to allocate a buffer or rely on a static buffer.
216 * This also avoids putting a large buffer on the stack which
217 * might have multi-threading issues.
220 if (write_in_full(fd_out
, header
, 4) < 0 ||
221 write_in_full(fd_out
, buf
, size
) < 0) {
222 strbuf_addf(err
, _("packet write failed: %s"), strerror(errno
));
228 static int packet_write_gently(const int fd_out
, const char *buf
, size_t size
)
230 struct strbuf err
= STRBUF_INIT
;
231 if (do_packet_write(fd_out
, buf
, size
, &err
)) {
232 error("%s", err
.buf
);
233 strbuf_release(&err
);
239 void packet_write(int fd_out
, const char *buf
, size_t size
)
241 struct strbuf err
= STRBUF_INIT
;
242 if (do_packet_write(fd_out
, buf
, size
, &err
))
246 void packet_fwrite(FILE *f
, const char *buf
, size_t size
)
251 if (size
> LARGE_PACKET_DATA_MAX
)
252 die(_("packet write failed - data exceeds max packet size"));
254 packet_trace(buf
, size
, 1);
255 packet_size
= size
+ 4;
257 set_packet_header(header
, packet_size
);
258 fwrite_or_die(f
, header
, 4);
259 fwrite_or_die(f
, buf
, size
);
262 void packet_fwrite_fmt(FILE *fh
, const char *fmt
, ...)
264 static struct strbuf buf
= STRBUF_INIT
;
270 format_packet(&buf
, "", fmt
, args
);
273 fwrite_or_die(fh
, buf
.buf
, buf
.len
);
276 void packet_fflush(FILE *f
)
278 packet_trace("0000", 4, 1);
279 fwrite_or_die(f
, "0000", 4);
283 void packet_buf_write(struct strbuf
*buf
, const char *fmt
, ...)
288 format_packet(buf
, "", fmt
, args
);
292 void packet_buf_write_len(struct strbuf
*buf
, const char *data
, size_t len
)
297 strbuf_addstr(buf
, "0000");
298 strbuf_add(buf
, data
, len
);
299 n
= buf
->len
- orig_len
;
301 if (n
> LARGE_PACKET_MAX
)
302 die(_("protocol error: impossibly long line"));
304 set_packet_header(&buf
->buf
[orig_len
], n
);
305 packet_trace(data
, len
, 1);
308 int write_packetized_from_fd_no_flush(int fd_in
, int fd_out
)
310 char *buf
= xmalloc(LARGE_PACKET_DATA_MAX
);
312 ssize_t bytes_to_write
;
315 bytes_to_write
= xread(fd_in
, buf
, LARGE_PACKET_DATA_MAX
);
316 if (bytes_to_write
< 0) {
318 return COPY_READ_ERROR
;
320 if (bytes_to_write
== 0)
322 err
= packet_write_gently(fd_out
, buf
, bytes_to_write
);
328 int write_packetized_from_buf_no_flush(const char *src_in
, size_t len
, int fd_out
)
331 size_t bytes_written
= 0;
332 size_t bytes_to_write
;
335 if ((len
- bytes_written
) > LARGE_PACKET_DATA_MAX
)
336 bytes_to_write
= LARGE_PACKET_DATA_MAX
;
338 bytes_to_write
= len
- bytes_written
;
339 if (bytes_to_write
== 0)
341 err
= packet_write_gently(fd_out
, src_in
+ bytes_written
, bytes_to_write
);
342 bytes_written
+= bytes_to_write
;
347 static int get_packet_data(int fd
, char **src_buf
, size_t *src_size
,
348 void *dst
, unsigned size
, int options
)
352 if (fd
>= 0 && src_buf
&& *src_buf
)
353 BUG("multiple sources given to packet_read");
355 /* Read up to "size" bytes from our source, whatever it is. */
356 if (src_buf
&& *src_buf
) {
357 ret
= size
< *src_size
? size
: *src_size
;
358 memcpy(dst
, *src_buf
, ret
);
362 ret
= read_in_full(fd
, dst
, size
);
364 if (options
& PACKET_READ_GENTLE_ON_READ_ERROR
)
365 return error_errno(_("read error"));
366 die_errno(_("read error"));
370 /* And complain if we didn't get enough bytes to satisfy the read. */
372 if (options
& PACKET_READ_GENTLE_ON_EOF
)
375 if (options
& PACKET_READ_GENTLE_ON_READ_ERROR
)
376 return error(_("the remote end hung up unexpectedly"));
377 die(_("the remote end hung up unexpectedly"));
383 int packet_length(const char lenbuf_hex
[4])
385 int val
= hex2chr(lenbuf_hex
);
386 return (val
< 0) ? val
: (val
<< 8) | hex2chr(lenbuf_hex
+ 2);
389 enum packet_read_status
packet_read_with_status(int fd
, char **src_buffer
,
390 size_t *src_len
, char *buffer
,
391 unsigned size
, int *pktlen
,
397 if (get_packet_data(fd
, src_buffer
, src_len
, linelen
, 4, options
) < 0) {
399 return PACKET_READ_EOF
;
402 len
= packet_length(linelen
);
405 if (options
& PACKET_READ_GENTLE_ON_READ_ERROR
)
406 return error(_("protocol error: bad line length "
407 "character: %.4s"), linelen
);
408 die(_("protocol error: bad line length character: %.4s"), linelen
);
410 packet_trace("0000", 4, 0);
412 return PACKET_READ_FLUSH
;
413 } else if (len
== 1) {
414 packet_trace("0001", 4, 0);
416 return PACKET_READ_DELIM
;
417 } else if (len
== 2) {
418 packet_trace("0002", 4, 0);
420 return PACKET_READ_RESPONSE_END
;
421 } else if (len
< 4) {
422 if (options
& PACKET_READ_GENTLE_ON_READ_ERROR
)
423 return error(_("protocol error: bad line length %d"),
425 die(_("protocol error: bad line length %d"), len
);
429 if ((unsigned)len
>= size
) {
430 if (options
& PACKET_READ_GENTLE_ON_READ_ERROR
)
431 return error(_("protocol error: bad line length %d"),
433 die(_("protocol error: bad line length %d"), len
);
436 if (get_packet_data(fd
, src_buffer
, src_len
, buffer
, len
, options
) < 0) {
438 return PACKET_READ_EOF
;
441 if ((options
& PACKET_READ_CHOMP_NEWLINE
) &&
442 len
&& buffer
[len
-1] == '\n')
446 packet_trace(buffer
, len
, 0);
448 if ((options
& PACKET_READ_DIE_ON_ERR_PACKET
) &&
449 starts_with(buffer
, "ERR "))
450 die(_("remote error: %s"), buffer
+ 4);
453 return PACKET_READ_NORMAL
;
456 int packet_read(int fd
, char **src_buffer
, size_t *src_len
,
457 char *buffer
, unsigned size
, int options
)
461 packet_read_with_status(fd
, src_buffer
, src_len
, buffer
, size
,
467 static char *packet_read_line_generic(int fd
,
468 char **src
, size_t *src_len
,
471 int len
= packet_read(fd
, src
, src_len
,
472 packet_buffer
, sizeof(packet_buffer
),
473 PACKET_READ_CHOMP_NEWLINE
);
476 return (len
> 0) ? packet_buffer
: NULL
;
479 char *packet_read_line(int fd
, int *len_p
)
481 return packet_read_line_generic(fd
, NULL
, NULL
, len_p
);
484 int packet_read_line_gently(int fd
, int *dst_len
, char **dst_line
)
486 int len
= packet_read(fd
, NULL
, NULL
,
487 packet_buffer
, sizeof(packet_buffer
),
488 PACKET_READ_CHOMP_NEWLINE
|PACKET_READ_GENTLE_ON_EOF
);
492 *dst_line
= (len
> 0) ? packet_buffer
: NULL
;
496 char *packet_read_line_buf(char **src
, size_t *src_len
, int *dst_len
)
498 return packet_read_line_generic(-1, src
, src_len
, dst_len
);
501 ssize_t
read_packetized_to_strbuf(int fd_in
, struct strbuf
*sb_out
, int options
)
505 size_t orig_len
= sb_out
->len
;
506 size_t orig_alloc
= sb_out
->alloc
;
509 strbuf_grow(sb_out
, LARGE_PACKET_DATA_MAX
);
510 packet_len
= packet_read(fd_in
, NULL
, NULL
,
511 /* strbuf_grow() above always allocates one extra byte to
512 * store a '\0' at the end of the string. packet_read()
513 * writes a '\0' extra byte at the end, too. Let it know
514 * that there is already room for the extra byte.
516 sb_out
->buf
+ sb_out
->len
, LARGE_PACKET_DATA_MAX
+1,
520 sb_out
->len
+= packet_len
;
523 if (packet_len
< 0) {
525 strbuf_release(sb_out
);
527 strbuf_setlen(sb_out
, orig_len
);
530 return sb_out
->len
- orig_len
;
533 int recv_sideband(const char *me
, int in_stream
, int out
)
535 char buf
[LARGE_PACKET_MAX
+ 1];
537 struct strbuf scratch
= STRBUF_INIT
;
538 enum sideband_type sideband_type
;
541 int status
= packet_read_with_status(in_stream
, NULL
, NULL
,
542 buf
, LARGE_PACKET_MAX
,
544 PACKET_READ_GENTLE_ON_EOF
);
545 if (!demultiplex_sideband(me
, status
, buf
, len
, 0, &scratch
,
548 switch (sideband_type
) {
549 case SIDEBAND_PRIMARY
:
550 write_or_die(out
, buf
+ 1, len
- 1);
552 default: /* errors: message already written */
554 BUG("unhandled incomplete sideband: '%s'",
556 return sideband_type
;
561 /* Packet Reader Functions */
562 void packet_reader_init(struct packet_reader
*reader
, int fd
,
563 char *src_buffer
, size_t src_len
,
566 memset(reader
, 0, sizeof(*reader
));
569 reader
->src_buffer
= src_buffer
;
570 reader
->src_len
= src_len
;
571 reader
->buffer
= packet_buffer
;
572 reader
->buffer_size
= sizeof(packet_buffer
);
573 reader
->options
= options
;
575 reader
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
578 enum packet_read_status
packet_reader_read(struct packet_reader
*reader
)
580 struct strbuf scratch
= STRBUF_INIT
;
582 if (reader
->line_peeked
) {
583 reader
->line_peeked
= 0;
584 return reader
->status
;
588 * Consume all progress packets until a primary payload packet is
592 enum sideband_type sideband_type
;
593 reader
->status
= packet_read_with_status(reader
->fd
,
600 if (!reader
->use_sideband
)
602 if (demultiplex_sideband(reader
->me
, reader
->status
,
603 reader
->buffer
, reader
->pktlen
, 1,
604 &scratch
, &sideband_type
))
608 if (reader
->status
== PACKET_READ_NORMAL
)
609 /* Skip the sideband designator if sideband is used */
610 reader
->line
= reader
->use_sideband
?
611 reader
->buffer
+ 1 : reader
->buffer
;
615 return reader
->status
;
618 enum packet_read_status
packet_reader_peek(struct packet_reader
*reader
)
620 /* Only allow peeking a single line */
621 if (reader
->line_peeked
)
622 return reader
->status
;
624 /* Peek a line by reading it and setting peeked flag */
625 packet_reader_read(reader
);
626 reader
->line_peeked
= 1;
627 return reader
->status
;
630 void packet_writer_init(struct packet_writer
*writer
, int dest_fd
)
632 writer
->dest_fd
= dest_fd
;
633 writer
->use_sideband
= 0;
636 void packet_writer_write(struct packet_writer
*writer
, const char *fmt
, ...)
641 packet_write_fmt_1(writer
->dest_fd
, 0,
642 writer
->use_sideband
? "\001" : "", fmt
, args
);
646 void packet_writer_error(struct packet_writer
*writer
, const char *fmt
, ...)
651 packet_write_fmt_1(writer
->dest_fd
, 0,
652 writer
->use_sideband
? "\003" : "ERR ", fmt
, args
);
656 void packet_writer_delim(struct packet_writer
*writer
)
658 packet_delim(writer
->dest_fd
);
661 void packet_writer_flush(struct packet_writer
*writer
)
663 packet_flush(writer
->dest_fd
);