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 write_or_die(fd, "0000", 4);
94 int packet_flush_gently(int fd)
96 packet_trace("0000", 4, 1);
97 if (write_in_full(fd, "0000", 4) == 4)
99 return error("flush packet write failed");
102 void packet_buf_flush(struct strbuf *buf)
104 packet_trace("0000", 4, 1);
105 strbuf_add(buf, "0000", 4);
108 static void set_packet_header(char *buf, const int size)
110 static char hexchar[] = "0123456789abcdef";
112 #define hex(a) (hexchar[(a) & 15])
113 buf[0] = hex(size >> 12);
114 buf[1] = hex(size >> 8);
115 buf[2] = hex(size >> 4);
120 static void format_packet(struct strbuf *out, const char *fmt, va_list args)
125 strbuf_addstr(out, "0000");
126 strbuf_vaddf(out, fmt, args);
127 n = out->len - orig_len;
129 if (n > LARGE_PACKET_MAX)
130 die("protocol error: impossibly long line");
132 set_packet_header(&out->buf[orig_len], n);
133 packet_trace(out->buf + orig_len + 4, n - 4, 1);
136 static int packet_write_fmt_1(int fd, int gently,
137 const char *fmt, va_list args)
139 static struct strbuf buf = STRBUF_INIT;
143 format_packet(&buf, fmt, args);
144 count = write_in_full(fd, buf.buf, buf.len);
145 if (count == buf.len)
150 die_errno("packet write with format failed");
152 return error("packet write with format failed");
155 void packet_write_fmt(int fd, const char *fmt, ...)
160 packet_write_fmt_1(fd, 0, fmt, args);
164 int packet_write_fmt_gently(int fd, const char *fmt, ...)
170 status = packet_write_fmt_1(fd, 1, fmt, args);
175 static int packet_write_gently(const int fd_out, const char *buf, size_t size)
177 static char packet_write_buffer[LARGE_PACKET_MAX];
180 if (size > sizeof(packet_write_buffer) - 4)
181 return error("packet write failed - data exceeds max packet size");
183 packet_trace(buf, size, 1);
184 packet_size = size + 4;
185 set_packet_header(packet_write_buffer, packet_size);
186 memcpy(packet_write_buffer + 4, buf, size);
187 if (write_in_full(fd_out, packet_write_buffer, packet_size) == packet_size)
189 return error("packet write failed");
192 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
197 format_packet(buf, fmt, args);
201 int write_packetized_from_fd(int fd_in, int fd_out)
203 static char buf[LARGE_PACKET_DATA_MAX];
205 ssize_t bytes_to_write;
208 bytes_to_write = xread(fd_in, buf, sizeof(buf));
209 if (bytes_to_write < 0)
210 return COPY_READ_ERROR;
211 if (bytes_to_write == 0)
213 err = packet_write_gently(fd_out, buf, bytes_to_write);
216 err = packet_flush_gently(fd_out);
220 int write_packetized_from_buf(const char *src_in, size_t len, int fd_out)
223 size_t bytes_written = 0;
224 size_t bytes_to_write;
227 if ((len - bytes_written) > LARGE_PACKET_DATA_MAX)
228 bytes_to_write = LARGE_PACKET_DATA_MAX;
230 bytes_to_write = len - bytes_written;
231 if (bytes_to_write == 0)
233 err = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);
234 bytes_written += bytes_to_write;
237 err = packet_flush_gently(fd_out);
241 static int get_packet_data(int fd, char **src_buf, size_t *src_size,
242 void *dst, unsigned size, int options)
246 if (fd >= 0 && src_buf && *src_buf)
247 die("BUG: multiple sources given to packet_read");
249 /* Read up to "size" bytes from our source, whatever it is. */
250 if (src_buf && *src_buf) {
251 ret = size < *src_size ? size : *src_size;
252 memcpy(dst, *src_buf, ret);
256 ret = read_in_full(fd, dst, size);
258 die_errno("read error");
261 /* And complain if we didn't get enough bytes to satisfy the read. */
263 if (options & PACKET_READ_GENTLE_ON_EOF)
266 die("The remote end hung up unexpectedly");
272 static int packet_length(const char *linelen)
274 int val = hex2chr(linelen);
275 return (val < 0) ? val : (val << 8) | hex2chr(linelen + 2);
278 int packet_read(int fd, char **src_buf, size_t *src_len,
279 char *buffer, unsigned size, int options)
284 ret = get_packet_data(fd, src_buf, src_len, linelen, 4, options);
287 len = packet_length(linelen);
289 die("protocol error: bad line length character: %.4s", linelen);
291 packet_trace("0000", 4, 0);
296 die("protocol error: bad line length %d", len);
297 ret = get_packet_data(fd, src_buf, src_len, buffer, len, options);
301 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
302 len && buffer[len-1] == '\n')
306 packet_trace(buffer, len, 0);
310 static char *packet_read_line_generic(int fd,
311 char **src, size_t *src_len,
314 int len = packet_read(fd, src, src_len,
315 packet_buffer, sizeof(packet_buffer),
316 PACKET_READ_CHOMP_NEWLINE);
319 return (len > 0) ? packet_buffer : NULL;
322 char *packet_read_line(int fd, int *len_p)
324 return packet_read_line_generic(fd, NULL, NULL, len_p);
327 int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
329 int len = packet_read(fd, NULL, NULL,
330 packet_buffer, sizeof(packet_buffer),
331 PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
335 *dst_line = (len > 0) ? packet_buffer : NULL;
339 char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
341 return packet_read_line_generic(-1, src, src_len, dst_len);
344 ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out)
348 size_t orig_len = sb_out->len;
349 size_t orig_alloc = sb_out->alloc;
352 strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);
353 packet_len = packet_read(fd_in, NULL, NULL,
354 /* strbuf_grow() above always allocates one extra byte to
355 * store a '\0' at the end of the string. packet_read()
356 * writes a '\0' extra byte at the end, too. Let it know
357 * that there is already room for the extra byte.
359 sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,
360 PACKET_READ_GENTLE_ON_EOF);
363 sb_out->len += packet_len;
366 if (packet_len < 0) {
368 strbuf_release(sb_out);
370 strbuf_setlen(sb_out, orig_len);
373 return sb_out->len - orig_len;