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 int packet_flush_gently(int fd
)
104 packet_trace("0000", 4, 1);
105 if (write_in_full(fd
, "0000", 4) < 0)
106 return error(_("flush packet write failed"));
110 void packet_buf_flush(struct strbuf
*buf
)
112 packet_trace("0000", 4, 1);
113 strbuf_add(buf
, "0000", 4);
116 void packet_buf_delim(struct strbuf
*buf
)
118 packet_trace("0001", 4, 1);
119 strbuf_add(buf
, "0001", 4);
122 void set_packet_header(char *buf
, const int size
)
124 static char hexchar
[] = "0123456789abcdef";
126 #define hex(a) (hexchar[(a) & 15])
127 buf
[0] = hex(size
>> 12);
128 buf
[1] = hex(size
>> 8);
129 buf
[2] = hex(size
>> 4);
134 static void format_packet(struct strbuf
*out
, const char *prefix
,
135 const char *fmt
, va_list args
)
140 strbuf_addstr(out
, "0000");
141 strbuf_addstr(out
, prefix
);
142 strbuf_vaddf(out
, fmt
, args
);
143 n
= out
->len
- orig_len
;
145 if (n
> LARGE_PACKET_MAX
)
146 die(_("protocol error: impossibly long line"));
148 set_packet_header(&out
->buf
[orig_len
], n
);
149 packet_trace(out
->buf
+ orig_len
+ 4, n
- 4, 1);
152 static int packet_write_fmt_1(int fd
, int gently
, const char *prefix
,
153 const char *fmt
, va_list args
)
155 static struct strbuf buf
= STRBUF_INIT
;
158 format_packet(&buf
, prefix
, fmt
, args
);
159 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
162 die_errno(_("packet write with format failed"));
164 return error(_("packet write with format failed"));
170 void packet_write_fmt(int fd
, const char *fmt
, ...)
175 packet_write_fmt_1(fd
, 0, "", fmt
, args
);
179 int packet_write_fmt_gently(int fd
, const char *fmt
, ...)
185 status
= packet_write_fmt_1(fd
, 1, "", fmt
, args
);
190 static int packet_write_gently(const int fd_out
, const char *buf
, size_t size
)
192 static char packet_write_buffer
[LARGE_PACKET_MAX
];
195 if (size
> sizeof(packet_write_buffer
) - 4)
196 return error(_("packet write failed - data exceeds max packet size"));
198 packet_trace(buf
, size
, 1);
199 packet_size
= size
+ 4;
200 set_packet_header(packet_write_buffer
, packet_size
);
201 memcpy(packet_write_buffer
+ 4, buf
, size
);
202 if (write_in_full(fd_out
, packet_write_buffer
, packet_size
) < 0)
203 return error(_("packet write failed"));
207 void packet_write(int fd_out
, const char *buf
, size_t size
)
209 if (packet_write_gently(fd_out
, buf
, size
))
210 die_errno(_("packet write failed"));
213 void packet_buf_write(struct strbuf
*buf
, const char *fmt
, ...)
218 format_packet(buf
, "", fmt
, args
);
222 void packet_buf_write_len(struct strbuf
*buf
, const char *data
, size_t len
)
227 strbuf_addstr(buf
, "0000");
228 strbuf_add(buf
, data
, len
);
229 n
= buf
->len
- orig_len
;
231 if (n
> LARGE_PACKET_MAX
)
232 die(_("protocol error: impossibly long line"));
234 set_packet_header(&buf
->buf
[orig_len
], n
);
235 packet_trace(data
, len
, 1);
238 int write_packetized_from_fd(int fd_in
, int fd_out
)
240 static char buf
[LARGE_PACKET_DATA_MAX
];
242 ssize_t bytes_to_write
;
245 bytes_to_write
= xread(fd_in
, buf
, sizeof(buf
));
246 if (bytes_to_write
< 0)
247 return COPY_READ_ERROR
;
248 if (bytes_to_write
== 0)
250 err
= packet_write_gently(fd_out
, buf
, bytes_to_write
);
253 err
= packet_flush_gently(fd_out
);
257 int write_packetized_from_buf(const char *src_in
, size_t len
, int fd_out
)
260 size_t bytes_written
= 0;
261 size_t bytes_to_write
;
264 if ((len
- bytes_written
) > LARGE_PACKET_DATA_MAX
)
265 bytes_to_write
= LARGE_PACKET_DATA_MAX
;
267 bytes_to_write
= len
- bytes_written
;
268 if (bytes_to_write
== 0)
270 err
= packet_write_gently(fd_out
, src_in
+ bytes_written
, bytes_to_write
);
271 bytes_written
+= bytes_to_write
;
274 err
= packet_flush_gently(fd_out
);
278 static int get_packet_data(int fd
, char **src_buf
, size_t *src_size
,
279 void *dst
, unsigned size
, int options
)
283 if (fd
>= 0 && src_buf
&& *src_buf
)
284 BUG("multiple sources given to packet_read");
286 /* Read up to "size" bytes from our source, whatever it is. */
287 if (src_buf
&& *src_buf
) {
288 ret
= size
< *src_size
? size
: *src_size
;
289 memcpy(dst
, *src_buf
, ret
);
293 ret
= read_in_full(fd
, dst
, size
);
295 die_errno(_("read error"));
298 /* And complain if we didn't get enough bytes to satisfy the read. */
300 if (options
& PACKET_READ_GENTLE_ON_EOF
)
303 die(_("the remote end hung up unexpectedly"));
309 static int packet_length(const char *linelen
)
311 int val
= hex2chr(linelen
);
312 return (val
< 0) ? val
: (val
<< 8) | hex2chr(linelen
+ 2);
315 enum packet_read_status
packet_read_with_status(int fd
, char **src_buffer
,
316 size_t *src_len
, char *buffer
,
317 unsigned size
, int *pktlen
,
323 if (get_packet_data(fd
, src_buffer
, src_len
, linelen
, 4, options
) < 0) {
325 return PACKET_READ_EOF
;
328 len
= packet_length(linelen
);
331 die(_("protocol error: bad line length character: %.4s"), linelen
);
333 packet_trace("0000", 4, 0);
335 return PACKET_READ_FLUSH
;
336 } else if (len
== 1) {
337 packet_trace("0001", 4, 0);
339 return PACKET_READ_DELIM
;
340 } else if (len
< 4) {
341 die(_("protocol error: bad line length %d"), len
);
345 if ((unsigned)len
>= size
)
346 die(_("protocol error: bad line length %d"), len
);
348 if (get_packet_data(fd
, src_buffer
, src_len
, buffer
, len
, options
) < 0) {
350 return PACKET_READ_EOF
;
353 if ((options
& PACKET_READ_DIE_ON_ERR_PACKET
) &&
354 starts_with(buffer
, "ERR "))
355 die(_("remote error: %s"), buffer
+ 4);
357 if ((options
& PACKET_READ_CHOMP_NEWLINE
) &&
358 len
&& buffer
[len
-1] == '\n')
362 packet_trace(buffer
, len
, 0);
364 return PACKET_READ_NORMAL
;
367 int packet_read(int fd
, char **src_buffer
, size_t *src_len
,
368 char *buffer
, unsigned size
, int options
)
372 packet_read_with_status(fd
, src_buffer
, src_len
, buffer
, size
,
378 static char *packet_read_line_generic(int fd
,
379 char **src
, size_t *src_len
,
382 int len
= packet_read(fd
, src
, src_len
,
383 packet_buffer
, sizeof(packet_buffer
),
384 PACKET_READ_CHOMP_NEWLINE
);
387 return (len
> 0) ? packet_buffer
: NULL
;
390 char *packet_read_line(int fd
, int *len_p
)
392 return packet_read_line_generic(fd
, NULL
, NULL
, len_p
);
395 int packet_read_line_gently(int fd
, int *dst_len
, char **dst_line
)
397 int len
= packet_read(fd
, NULL
, NULL
,
398 packet_buffer
, sizeof(packet_buffer
),
399 PACKET_READ_CHOMP_NEWLINE
|PACKET_READ_GENTLE_ON_EOF
);
403 *dst_line
= (len
> 0) ? packet_buffer
: NULL
;
407 char *packet_read_line_buf(char **src
, size_t *src_len
, int *dst_len
)
409 return packet_read_line_generic(-1, src
, src_len
, dst_len
);
412 ssize_t
read_packetized_to_strbuf(int fd_in
, struct strbuf
*sb_out
)
416 size_t orig_len
= sb_out
->len
;
417 size_t orig_alloc
= sb_out
->alloc
;
420 strbuf_grow(sb_out
, LARGE_PACKET_DATA_MAX
);
421 packet_len
= packet_read(fd_in
, NULL
, NULL
,
422 /* strbuf_grow() above always allocates one extra byte to
423 * store a '\0' at the end of the string. packet_read()
424 * writes a '\0' extra byte at the end, too. Let it know
425 * that there is already room for the extra byte.
427 sb_out
->buf
+ sb_out
->len
, LARGE_PACKET_DATA_MAX
+1,
428 PACKET_READ_GENTLE_ON_EOF
);
431 sb_out
->len
+= packet_len
;
434 if (packet_len
< 0) {
436 strbuf_release(sb_out
);
438 strbuf_setlen(sb_out
, orig_len
);
441 return sb_out
->len
- orig_len
;
444 int recv_sideband(const char *me
, int in_stream
, int out
)
446 char buf
[LARGE_PACKET_MAX
+ 1];
448 struct strbuf scratch
= STRBUF_INIT
;
449 enum sideband_type sideband_type
;
452 len
= packet_read(in_stream
, NULL
, NULL
, buf
, LARGE_PACKET_MAX
,
454 if (!demultiplex_sideband(me
, buf
, len
, 0, &scratch
,
457 switch (sideband_type
) {
458 case SIDEBAND_PRIMARY
:
459 write_or_die(out
, buf
+ 1, len
- 1);
461 default: /* errors: message already written */
462 return sideband_type
;
467 /* Packet Reader Functions */
468 void packet_reader_init(struct packet_reader
*reader
, int fd
,
469 char *src_buffer
, size_t src_len
,
472 memset(reader
, 0, sizeof(*reader
));
475 reader
->src_buffer
= src_buffer
;
476 reader
->src_len
= src_len
;
477 reader
->buffer
= packet_buffer
;
478 reader
->buffer_size
= sizeof(packet_buffer
);
479 reader
->options
= options
;
483 enum packet_read_status
packet_reader_read(struct packet_reader
*reader
)
485 struct strbuf scratch
= STRBUF_INIT
;
487 if (reader
->line_peeked
) {
488 reader
->line_peeked
= 0;
489 return reader
->status
;
493 * Consume all progress packets until a primary payload packet is
497 enum sideband_type sideband_type
;
498 reader
->status
= packet_read_with_status(reader
->fd
,
505 if (!reader
->use_sideband
)
507 if (demultiplex_sideband(reader
->me
, reader
->buffer
,
508 reader
->pktlen
, 1, &scratch
,
513 if (reader
->status
== PACKET_READ_NORMAL
)
514 /* Skip the sideband designator if sideband is used */
515 reader
->line
= reader
->use_sideband
?
516 reader
->buffer
+ 1 : reader
->buffer
;
520 return reader
->status
;
523 enum packet_read_status
packet_reader_peek(struct packet_reader
*reader
)
525 /* Only allow peeking a single line */
526 if (reader
->line_peeked
)
527 return reader
->status
;
529 /* Peek a line by reading it and setting peeked flag */
530 packet_reader_read(reader
);
531 reader
->line_peeked
= 1;
532 return reader
->status
;
535 void packet_writer_init(struct packet_writer
*writer
, int dest_fd
)
537 writer
->dest_fd
= dest_fd
;
538 writer
->use_sideband
= 0;
541 void packet_writer_write(struct packet_writer
*writer
, const char *fmt
, ...)
546 packet_write_fmt_1(writer
->dest_fd
, 0,
547 writer
->use_sideband
? "\001" : "", fmt
, args
);
551 void packet_writer_error(struct packet_writer
*writer
, const char *fmt
, ...)
556 packet_write_fmt_1(writer
->dest_fd
, 0,
557 writer
->use_sideband
? "\003" : "ERR ", fmt
, args
);
561 void packet_writer_delim(struct packet_writer
*writer
)
563 packet_delim(writer
->dest_fd
);
566 void packet_writer_flush(struct packet_writer
*writer
)
568 packet_flush(writer
->dest_fd
);