4 char packet_buffer[LARGE_PACKET_MAX];
5 static const char *packet_trace_prefix = "git";
6 static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET);
8 void packet_trace_identity(const char *prog)
10 packet_trace_prefix = xstrdup(prog);
13 static void packet_trace(const char *buf, unsigned int len, int write)
18 if (!trace_want(&trace_packet))
21 /* +32 is just a guess for header + quoting */
22 strbuf_init(&out, len+32);
24 strbuf_addf(&out, "packet: %12s%c ",
25 packet_trace_prefix, write ? '>' : '<');
27 if (starts_with(buf, "PACK") || starts_with(buf + 1, "PACK")) {
28 strbuf_addstr(&out, "PACK ...");
29 trace_disable(&trace_packet);
32 /* XXX we should really handle printable utf8 */
33 for (i = 0; i < len; i++) {
34 /* suppress newlines */
37 if (buf[i] >= 0x20 && buf[i] <= 0x7e)
38 strbuf_addch(&out, buf[i]);
40 strbuf_addf(&out, "\\%o", buf[i]);
44 strbuf_addch(&out, '\n');
45 trace_strbuf(&trace_packet, &out);
50 * If we buffered things up above (we don't, but we should),
53 void packet_flush(int fd)
55 packet_trace("0000", 4, 1);
56 write_or_die(fd, "0000", 4);
59 void packet_buf_flush(struct strbuf *buf)
61 packet_trace("0000", 4, 1);
62 strbuf_add(buf, "0000", 4);
65 #define hex(a) (hexchar[(a) & 15])
66 static void format_packet(struct strbuf *out, const char *fmt, va_list args)
68 static char hexchar[] = "0123456789abcdef";
72 strbuf_addstr(out, "0000");
73 strbuf_vaddf(out, fmt, args);
74 n = out->len - orig_len;
76 if (n > LARGE_PACKET_MAX)
77 die("protocol error: impossibly long line");
79 out->buf[orig_len + 0] = hex(n >> 12);
80 out->buf[orig_len + 1] = hex(n >> 8);
81 out->buf[orig_len + 2] = hex(n >> 4);
82 out->buf[orig_len + 3] = hex(n);
83 packet_trace(out->buf + orig_len + 4, n - 4, 1);
86 void packet_write(int fd, const char *fmt, ...)
88 static struct strbuf buf = STRBUF_INIT;
93 format_packet(&buf, fmt, args);
95 write_or_die(fd, buf.buf, buf.len);
98 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
103 format_packet(buf, fmt, args);
107 static int get_packet_data(int fd, char **src_buf, size_t *src_size,
108 void *dst, unsigned size, int options)
112 if (fd >= 0 && src_buf && *src_buf)
113 die("BUG: multiple sources given to packet_read");
115 /* Read up to "size" bytes from our source, whatever it is. */
116 if (src_buf && *src_buf) {
117 ret = size < *src_size ? size : *src_size;
118 memcpy(dst, *src_buf, ret);
122 ret = read_in_full(fd, dst, size);
124 die_errno("read error");
127 /* And complain if we didn't get enough bytes to satisfy the read. */
129 if (options & PACKET_READ_GENTLE_ON_EOF)
132 die("The remote end hung up unexpectedly");
138 static int packet_length(const char *linelen)
143 for (n = 0; n < 4; n++) {
144 unsigned char c = linelen[n];
146 if (c >= '0' && c <= '9') {
150 if (c >= 'a' && c <= 'f') {
154 if (c >= 'A' && c <= 'F') {
163 int packet_read(int fd, char **src_buf, size_t *src_len,
164 char *buffer, unsigned size, int options)
169 ret = get_packet_data(fd, src_buf, src_len, linelen, 4, options);
172 len = packet_length(linelen);
174 die("protocol error: bad line length character: %.4s", linelen);
176 packet_trace("0000", 4, 0);
181 die("protocol error: bad line length %d", len);
182 ret = get_packet_data(fd, src_buf, src_len, buffer, len, options);
186 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
187 len && buffer[len-1] == '\n')
191 packet_trace(buffer, len, 0);
195 static char *packet_read_line_generic(int fd,
196 char **src, size_t *src_len,
199 int len = packet_read(fd, src, src_len,
200 packet_buffer, sizeof(packet_buffer),
201 PACKET_READ_CHOMP_NEWLINE);
204 return len ? packet_buffer : NULL;
207 char *packet_read_line(int fd, int *len_p)
209 return packet_read_line_generic(fd, NULL, NULL, len_p);
212 char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
214 return packet_read_line_generic(-1, src, src_len, dst_len);