diff options
Diffstat (limited to 'pkt-line.c')
-rw-r--r-- | pkt-line.c | 263 |
1 files changed, 213 insertions, 50 deletions
diff --git a/pkt-line.c b/pkt-line.c index 187a2293e7..2827ca772a 100644 --- a/pkt-line.c +++ b/pkt-line.c @@ -1,19 +1,60 @@ #include "cache.h" #include "pkt-line.h" +#include "run-command.h" char packet_buffer[LARGE_PACKET_MAX]; static const char *packet_trace_prefix = "git"; static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET); +static struct trace_key trace_pack = TRACE_KEY_INIT(PACKFILE); void packet_trace_identity(const char *prog) { packet_trace_prefix = xstrdup(prog); } +static const char *get_trace_prefix(void) +{ + return in_async() ? "sideband" : packet_trace_prefix; +} + +static int packet_trace_pack(const char *buf, unsigned int len, int sideband) +{ + if (!sideband) { + trace_verbatim(&trace_pack, buf, len); + return 1; + } else if (len && *buf == '\1') { + trace_verbatim(&trace_pack, buf + 1, len - 1); + return 1; + } else { + /* it's another non-pack sideband */ + return 0; + } +} + static void packet_trace(const char *buf, unsigned int len, int write) { int i; struct strbuf out; + static int in_pack, sideband; + + if (!trace_want(&trace_packet) && !trace_want(&trace_pack)) + return; + + if (in_pack) { + if (packet_trace_pack(buf, len, sideband)) + return; + } else if (starts_with(buf, "PACK") || starts_with(buf, "\1PACK")) { + in_pack = 1; + sideband = *buf == '\1'; + packet_trace_pack(buf, len, sideband); + + /* + * Make a note in the human-readable trace that the pack data + * started. + */ + buf = "PACK ..."; + len = strlen(buf); + } if (!trace_want(&trace_packet)) return; @@ -22,24 +63,17 @@ static void packet_trace(const char *buf, unsigned int len, int write) strbuf_init(&out, len+32); strbuf_addf(&out, "packet: %12s%c ", - packet_trace_prefix, write ? '>' : '<'); + get_trace_prefix(), write ? '>' : '<'); - if ((len >= 4 && starts_with(buf, "PACK")) || - (len >= 5 && starts_with(buf+1, "PACK"))) { - strbuf_addstr(&out, "PACK ..."); - trace_disable(&trace_packet); - } - else { - /* XXX we should really handle printable utf8 */ - for (i = 0; i < len; i++) { - /* suppress newlines */ - if (buf[i] == '\n') - continue; - if (buf[i] >= 0x20 && buf[i] <= 0x7e) - strbuf_addch(&out, buf[i]); - else - strbuf_addf(&out, "\\%o", buf[i]); - } + /* XXX we should really handle printable utf8 */ + for (i = 0; i < len; i++) { + /* suppress newlines */ + if (buf[i] == '\n') + continue; + if (buf[i] >= 0x20 && buf[i] <= 0x7e) + strbuf_addch(&out, buf[i]); + else + strbuf_addf(&out, "\\%o", buf[i]); } strbuf_addch(&out, '\n'); @@ -57,16 +91,34 @@ void packet_flush(int fd) write_or_die(fd, "0000", 4); } +int packet_flush_gently(int fd) +{ + packet_trace("0000", 4, 1); + if (write_in_full(fd, "0000", 4) < 0) + return error("flush packet write failed"); + return 0; +} + void packet_buf_flush(struct strbuf *buf) { packet_trace("0000", 4, 1); strbuf_add(buf, "0000", 4); } -#define hex(a) (hexchar[(a) & 15]) -static void format_packet(struct strbuf *out, const char *fmt, va_list args) +static void set_packet_header(char *buf, const int size) { static char hexchar[] = "0123456789abcdef"; + + #define hex(a) (hexchar[(a) & 15]) + buf[0] = hex(size >> 12); + buf[1] = hex(size >> 8); + buf[2] = hex(size >> 4); + buf[3] = hex(size); + #undef hex +} + +static void format_packet(struct strbuf *out, const char *fmt, va_list args) +{ size_t orig_len, n; orig_len = out->len; @@ -77,23 +129,69 @@ static void format_packet(struct strbuf *out, const char *fmt, va_list args) if (n > LARGE_PACKET_MAX) die("protocol error: impossibly long line"); - out->buf[orig_len + 0] = hex(n >> 12); - out->buf[orig_len + 1] = hex(n >> 8); - out->buf[orig_len + 2] = hex(n >> 4); - out->buf[orig_len + 3] = hex(n); + set_packet_header(&out->buf[orig_len], n); packet_trace(out->buf + orig_len + 4, n - 4, 1); } -void packet_write(int fd, const char *fmt, ...) +static int packet_write_fmt_1(int fd, int gently, + const char *fmt, va_list args) { static struct strbuf buf = STRBUF_INIT; - va_list args; strbuf_reset(&buf); - va_start(args, fmt); format_packet(&buf, fmt, args); + if (write_in_full(fd, buf.buf, buf.len) < 0) { + if (!gently) { + check_pipe(errno); + die_errno("packet write with format failed"); + } + return error("packet write with format failed"); + } + + return 0; +} + +void packet_write_fmt(int fd, const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + packet_write_fmt_1(fd, 0, fmt, args); + va_end(args); +} + +int packet_write_fmt_gently(int fd, const char *fmt, ...) +{ + int status; + va_list args; + + va_start(args, fmt); + status = packet_write_fmt_1(fd, 1, fmt, args); va_end(args); - write_or_die(fd, buf.buf, buf.len); + return status; +} + +static int packet_write_gently(const int fd_out, const char *buf, size_t size) +{ + static char packet_write_buffer[LARGE_PACKET_MAX]; + size_t packet_size; + + if (size > sizeof(packet_write_buffer) - 4) + return error("packet write failed - data exceeds max packet size"); + + packet_trace(buf, size, 1); + packet_size = size + 4; + set_packet_header(packet_write_buffer, packet_size); + memcpy(packet_write_buffer + 4, buf, size); + if (write_in_full(fd_out, packet_write_buffer, packet_size) < 0) + return error("packet write failed"); + return 0; +} + +void packet_write(int fd_out, const char *buf, size_t size) +{ + if (packet_write_gently(fd_out, buf, size)) + die_errno("packet write failed"); } void packet_buf_write(struct strbuf *buf, const char *fmt, ...) @@ -105,6 +203,46 @@ void packet_buf_write(struct strbuf *buf, const char *fmt, ...) va_end(args); } +int write_packetized_from_fd(int fd_in, int fd_out) +{ + static char buf[LARGE_PACKET_DATA_MAX]; + int err = 0; + ssize_t bytes_to_write; + + while (!err) { + bytes_to_write = xread(fd_in, buf, sizeof(buf)); + if (bytes_to_write < 0) + return COPY_READ_ERROR; + if (bytes_to_write == 0) + break; + err = packet_write_gently(fd_out, buf, bytes_to_write); + } + if (!err) + err = packet_flush_gently(fd_out); + return err; +} + +int write_packetized_from_buf(const char *src_in, size_t len, int fd_out) +{ + int err = 0; + size_t bytes_written = 0; + size_t bytes_to_write; + + while (!err) { + if ((len - bytes_written) > LARGE_PACKET_DATA_MAX) + bytes_to_write = LARGE_PACKET_DATA_MAX; + else + bytes_to_write = len - bytes_written; + if (bytes_to_write == 0) + break; + err = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write); + bytes_written += bytes_to_write; + } + if (!err) + err = packet_flush_gently(fd_out); + return err; +} + static int get_packet_data(int fd, char **src_buf, size_t *src_size, void *dst, unsigned size, int options) { @@ -126,7 +264,7 @@ static int get_packet_data(int fd, char **src_buf, size_t *src_size, } /* And complain if we didn't get enough bytes to satisfy the read. */ - if (ret < size) { + if (ret != size) { if (options & PACKET_READ_GENTLE_ON_EOF) return -1; @@ -138,27 +276,8 @@ static int get_packet_data(int fd, char **src_buf, size_t *src_size, static int packet_length(const char *linelen) { - int n; - int len = 0; - - for (n = 0; n < 4; n++) { - unsigned char c = linelen[n]; - len <<= 4; - if (c >= '0' && c <= '9') { - len += c - '0'; - continue; - } - if (c >= 'a' && c <= 'f') { - len += c - 'a' + 10; - continue; - } - if (c >= 'A' && c <= 'F') { - len += c - 'A' + 10; - continue; - } - return -1; - } - return len; + int val = hex2chr(linelen); + return (val < 0) ? val : (val << 8) | hex2chr(linelen + 2); } int packet_read(int fd, char **src_buf, size_t *src_len, @@ -202,7 +321,7 @@ static char *packet_read_line_generic(int fd, PACKET_READ_CHOMP_NEWLINE); if (dst_len) *dst_len = len; - return len ? packet_buffer : NULL; + return (len > 0) ? packet_buffer : NULL; } char *packet_read_line(int fd, int *len_p) @@ -210,7 +329,51 @@ char *packet_read_line(int fd, int *len_p) return packet_read_line_generic(fd, NULL, NULL, len_p); } +int packet_read_line_gently(int fd, int *dst_len, char **dst_line) +{ + int len = packet_read(fd, NULL, NULL, + packet_buffer, sizeof(packet_buffer), + PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF); + if (dst_len) + *dst_len = len; + if (dst_line) + *dst_line = (len > 0) ? packet_buffer : NULL; + return len; +} + char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len) { return packet_read_line_generic(-1, src, src_len, dst_len); } + +ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out) +{ + int packet_len; + + size_t orig_len = sb_out->len; + size_t orig_alloc = sb_out->alloc; + + for (;;) { + strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX); + packet_len = packet_read(fd_in, NULL, NULL, + /* strbuf_grow() above always allocates one extra byte to + * store a '\0' at the end of the string. packet_read() + * writes a '\0' extra byte at the end, too. Let it know + * that there is already room for the extra byte. + */ + sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1, + PACKET_READ_GENTLE_ON_EOF); + if (packet_len <= 0) + break; + sb_out->len += packet_len; + } + + if (packet_len < 0) { + if (orig_alloc == 0) + strbuf_release(sb_out); + else + strbuf_setlen(sb_out, orig_len); + return packet_len; + } + return sb_out->len - orig_len; +} |