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])
378 int val
= hex2chr(lenbuf_hex
);
379 return (val
< 0) ? val
: (val
<< 8) | hex2chr(lenbuf_hex
+ 2);
382 static char *find_packfile_uri_path(const char *buffer
)
384 const char *URI_MARK
= "://";
388 /* First char is sideband mark */
391 len
= strspn(buffer
, "0123456789abcdefABCDEF");
392 /* size of SHA1 and SHA256 hash */
393 if (!(len
== 40 || len
== 64) || buffer
[len
] != ' ')
394 return NULL
; /* required "<hash>SP" not seen */
396 path
= strstr(buffer
+ len
+ 1, URI_MARK
);
400 path
= strchr(path
+ strlen(URI_MARK
), '/');
401 if (!path
|| !*(path
+ 1))
404 /* position after '/' */
408 enum packet_read_status
packet_read_with_status(int fd
, char **src_buffer
,
409 size_t *src_len
, char *buffer
,
410 unsigned size
, int *pktlen
,
415 char *uri_path_start
;
417 if (get_packet_data(fd
, src_buffer
, src_len
, linelen
, 4, options
) < 0) {
419 return PACKET_READ_EOF
;
422 len
= packet_length(linelen
);
425 if (options
& PACKET_READ_GENTLE_ON_READ_ERROR
)
426 return error(_("protocol error: bad line length "
427 "character: %.4s"), linelen
);
428 die(_("protocol error: bad line length character: %.4s"), linelen
);
430 packet_trace("0000", 4, 0);
432 return PACKET_READ_FLUSH
;
433 } else if (len
== 1) {
434 packet_trace("0001", 4, 0);
436 return PACKET_READ_DELIM
;
437 } else if (len
== 2) {
438 packet_trace("0002", 4, 0);
440 return PACKET_READ_RESPONSE_END
;
441 } else if (len
< 4) {
442 if (options
& PACKET_READ_GENTLE_ON_READ_ERROR
)
443 return error(_("protocol error: bad line length %d"),
445 die(_("protocol error: bad line length %d"), len
);
449 if ((unsigned)len
>= size
) {
450 if (options
& PACKET_READ_GENTLE_ON_READ_ERROR
)
451 return error(_("protocol error: bad line length %d"),
453 die(_("protocol error: bad line length %d"), len
);
456 if (get_packet_data(fd
, src_buffer
, src_len
, buffer
, len
, options
) < 0) {
458 return PACKET_READ_EOF
;
461 if ((options
& PACKET_READ_CHOMP_NEWLINE
) &&
462 len
&& buffer
[len
-1] == '\n')
466 if (options
& PACKET_READ_REDACT_URI_PATH
&&
467 (uri_path_start
= find_packfile_uri_path(buffer
))) {
468 const char *redacted
= "<redacted>";
469 struct strbuf tracebuf
= STRBUF_INIT
;
470 strbuf_insert(&tracebuf
, 0, buffer
, len
);
471 strbuf_splice(&tracebuf
, uri_path_start
- buffer
,
472 strlen(uri_path_start
), redacted
, strlen(redacted
));
473 packet_trace(tracebuf
.buf
, tracebuf
.len
, 0);
474 strbuf_release(&tracebuf
);
476 packet_trace(buffer
, len
, 0);
479 if ((options
& PACKET_READ_DIE_ON_ERR_PACKET
) &&
480 starts_with(buffer
, "ERR "))
481 die(_("remote error: %s"), buffer
+ 4);
484 return PACKET_READ_NORMAL
;
487 int packet_read(int fd
, char *buffer
, unsigned size
, int options
)
491 packet_read_with_status(fd
, NULL
, NULL
, buffer
, size
, &pktlen
,
497 char *packet_read_line(int fd
, int *dst_len
)
499 int len
= packet_read(fd
, packet_buffer
, sizeof(packet_buffer
),
500 PACKET_READ_CHOMP_NEWLINE
);
503 return (len
> 0) ? packet_buffer
: NULL
;
506 int packet_read_line_gently(int fd
, int *dst_len
, char **dst_line
)
508 int len
= packet_read(fd
, packet_buffer
, sizeof(packet_buffer
),
509 PACKET_READ_CHOMP_NEWLINE
|PACKET_READ_GENTLE_ON_EOF
);
513 *dst_line
= (len
> 0) ? packet_buffer
: NULL
;
517 ssize_t
read_packetized_to_strbuf(int fd_in
, struct strbuf
*sb_out
, int options
)
521 size_t orig_len
= sb_out
->len
;
522 size_t orig_alloc
= sb_out
->alloc
;
525 strbuf_grow(sb_out
, LARGE_PACKET_DATA_MAX
);
526 packet_len
= packet_read(fd_in
,
527 /* strbuf_grow() above always allocates one extra byte to
528 * store a '\0' at the end of the string. packet_read()
529 * writes a '\0' extra byte at the end, too. Let it know
530 * that there is already room for the extra byte.
532 sb_out
->buf
+ sb_out
->len
, LARGE_PACKET_DATA_MAX
+1,
536 sb_out
->len
+= packet_len
;
539 if (packet_len
< 0) {
541 strbuf_release(sb_out
);
543 strbuf_setlen(sb_out
, orig_len
);
546 return sb_out
->len
- orig_len
;
549 int recv_sideband(const char *me
, int in_stream
, int out
)
551 char buf
[LARGE_PACKET_MAX
+ 1];
553 struct strbuf scratch
= STRBUF_INIT
;
554 enum sideband_type sideband_type
;
557 int status
= packet_read_with_status(in_stream
, NULL
, NULL
,
558 buf
, LARGE_PACKET_MAX
,
560 PACKET_READ_GENTLE_ON_EOF
);
561 if (!demultiplex_sideband(me
, status
, buf
, len
, 0, &scratch
,
564 switch (sideband_type
) {
565 case SIDEBAND_PRIMARY
:
566 write_or_die(out
, buf
+ 1, len
- 1);
568 default: /* errors: message already written */
570 BUG("unhandled incomplete sideband: '%s'",
572 return sideband_type
;
577 /* Packet Reader Functions */
578 void packet_reader_init(struct packet_reader
*reader
, int fd
,
579 char *src_buffer
, size_t src_len
,
582 memset(reader
, 0, sizeof(*reader
));
585 reader
->src_buffer
= src_buffer
;
586 reader
->src_len
= src_len
;
587 reader
->buffer
= packet_buffer
;
588 reader
->buffer_size
= sizeof(packet_buffer
);
589 reader
->options
= options
;
591 reader
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
594 enum packet_read_status
packet_reader_read(struct packet_reader
*reader
)
596 struct strbuf scratch
= STRBUF_INIT
;
598 if (reader
->line_peeked
) {
599 reader
->line_peeked
= 0;
600 return reader
->status
;
604 * Consume all progress packets until a primary payload packet is
608 enum sideband_type sideband_type
;
609 reader
->status
= packet_read_with_status(reader
->fd
,
616 if (!reader
->use_sideband
)
618 if (demultiplex_sideband(reader
->me
, reader
->status
,
619 reader
->buffer
, reader
->pktlen
, 1,
620 &scratch
, &sideband_type
))
624 if (reader
->status
== PACKET_READ_NORMAL
)
625 /* Skip the sideband designator if sideband is used */
626 reader
->line
= reader
->use_sideband
?
627 reader
->buffer
+ 1 : reader
->buffer
;
631 return reader
->status
;
634 enum packet_read_status
packet_reader_peek(struct packet_reader
*reader
)
636 /* Only allow peeking a single line */
637 if (reader
->line_peeked
)
638 return reader
->status
;
640 /* Peek a line by reading it and setting peeked flag */
641 packet_reader_read(reader
);
642 reader
->line_peeked
= 1;
643 return reader
->status
;
646 void packet_writer_init(struct packet_writer
*writer
, int dest_fd
)
648 writer
->dest_fd
= dest_fd
;
649 writer
->use_sideband
= 0;
652 void packet_writer_write(struct packet_writer
*writer
, const char *fmt
, ...)
657 packet_write_fmt_1(writer
->dest_fd
, 0,
658 writer
->use_sideband
? "\001" : "", fmt
, args
);
662 void packet_writer_error(struct packet_writer
*writer
, const char *fmt
, ...)
667 packet_write_fmt_1(writer
->dest_fd
, 0,
668 writer
->use_sideband
? "\003" : "ERR ", fmt
, args
);
672 void packet_writer_delim(struct packet_writer
*writer
)
674 packet_delim(writer
->dest_fd
);
677 void packet_writer_flush(struct packet_writer
*writer
)
679 packet_flush(writer
->dest_fd
);