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);
7 static struct trace_key trace_pack = TRACE_KEY_INIT(PACKFILE);
9 void packet_trace_identity(const char *prog)
11 packet_trace_prefix = xstrdup(prog);
14 static int packet_trace_pack(const char *buf, unsigned int len, int sideband)
17 trace_verbatim(&trace_pack, buf, len);
19 } else if (len && *buf == '\1') {
20 trace_verbatim(&trace_pack, buf + 1, len - 1);
23 /* it's another non-pack sideband */
28 static void packet_trace(const char *buf, unsigned int len, int write)
32 static int in_pack, sideband;
34 if (!trace_want(&trace_packet) && !trace_want(&trace_pack))
38 if (packet_trace_pack(buf, len, sideband))
40 } else if (starts_with(buf, "PACK") || starts_with(buf, "\1PACK")) {
42 sideband = *buf == '\1';
43 packet_trace_pack(buf, len, sideband);
46 * Make a note in the human-readable trace that the pack data
53 if (!trace_want(&trace_packet))
56 /* +32 is just a guess for header + quoting */
57 strbuf_init(&out, len+32);
59 strbuf_addf(&out, "packet: %12s%c ",
60 packet_trace_prefix, write ? '>' : '<');
62 /* XXX we should really handle printable utf8 */
63 for (i = 0; i < len; i++) {
64 /* suppress newlines */
67 if (buf[i] >= 0x20 && buf[i] <= 0x7e)
68 strbuf_addch(&out, buf[i]);
70 strbuf_addf(&out, "\\%o", buf[i]);
73 strbuf_addch(&out, '\n');
74 trace_strbuf(&trace_packet, &out);
79 * If we buffered things up above (we don't, but we should),
82 void packet_flush(int fd)
84 packet_trace("0000", 4, 1);
85 write_or_die(fd, "0000", 4);
88 void packet_buf_flush(struct strbuf *buf)
90 packet_trace("0000", 4, 1);
91 strbuf_add(buf, "0000", 4);
94 #define hex(a) (hexchar[(a) & 15])
95 static void format_packet(struct strbuf *out, const char *fmt, va_list args)
97 static char hexchar[] = "0123456789abcdef";
101 strbuf_addstr(out, "0000");
102 strbuf_vaddf(out, fmt, args);
103 n = out->len - orig_len;
105 if (n > LARGE_PACKET_MAX)
106 die("protocol error: impossibly long line");
108 out->buf[orig_len + 0] = hex(n >> 12);
109 out->buf[orig_len + 1] = hex(n >> 8);
110 out->buf[orig_len + 2] = hex(n >> 4);
111 out->buf[orig_len + 3] = hex(n);
112 packet_trace(out->buf + orig_len + 4, n - 4, 1);
115 void packet_write(int fd, const char *fmt, ...)
117 static struct strbuf buf = STRBUF_INIT;
122 format_packet(&buf, fmt, args);
124 write_or_die(fd, buf.buf, buf.len);
127 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
132 format_packet(buf, fmt, args);
136 static int get_packet_data(int fd, char **src_buf, size_t *src_size,
137 void *dst, unsigned size, int options)
141 if (fd >= 0 && src_buf && *src_buf)
142 die("BUG: multiple sources given to packet_read");
144 /* Read up to "size" bytes from our source, whatever it is. */
145 if (src_buf && *src_buf) {
146 ret = size < *src_size ? size : *src_size;
147 memcpy(dst, *src_buf, ret);
151 ret = read_in_full(fd, dst, size);
153 die_errno("read error");
156 /* And complain if we didn't get enough bytes to satisfy the read. */
158 if (options & PACKET_READ_GENTLE_ON_EOF)
161 die("The remote end hung up unexpectedly");
167 static int packet_length(const char *linelen)
172 for (n = 0; n < 4; n++) {
173 unsigned char c = linelen[n];
175 if (c >= '0' && c <= '9') {
179 if (c >= 'a' && c <= 'f') {
183 if (c >= 'A' && c <= 'F') {
192 int packet_read(int fd, char **src_buf, size_t *src_len,
193 char *buffer, unsigned size, int options)
198 ret = get_packet_data(fd, src_buf, src_len, linelen, 4, options);
201 len = packet_length(linelen);
203 die("protocol error: bad line length character: %.4s", linelen);
205 packet_trace("0000", 4, 0);
210 die("protocol error: bad line length %d", len);
211 ret = get_packet_data(fd, src_buf, src_len, buffer, len, options);
215 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
216 len && buffer[len-1] == '\n')
220 packet_trace(buffer, len, 0);
224 static char *packet_read_line_generic(int fd,
225 char **src, size_t *src_len,
228 int len = packet_read(fd, src, src_len,
229 packet_buffer, sizeof(packet_buffer),
230 PACKET_READ_CHOMP_NEWLINE);
233 return len ? packet_buffer : NULL;
236 char *packet_read_line(int fd, int *len_p)
238 return packet_read_line_generic(fd, NULL, NULL, len_p);
241 char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
243 return packet_read_line_generic(-1, src, src_len, dst_len);