tests: fix protocol version for overspecifications
[git.git] / pkt-line.c
blobd4b71d3e82b0e54ef4105748fdfdc15b438badfb
1 #include "cache.h"
2 #include "pkt-line.h"
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)
22 if (!sideband) {
23 trace_verbatim(&trace_pack, buf, len);
24 return 1;
25 } else if (len && *buf == '\1') {
26 trace_verbatim(&trace_pack, buf + 1, len - 1);
27 return 1;
28 } else {
29 /* it's another non-pack sideband */
30 return 0;
34 static void packet_trace(const char *buf, unsigned int len, int write)
36 int i;
37 struct strbuf out;
38 static int in_pack, sideband;
40 if (!trace_want(&trace_packet) && !trace_want(&trace_pack))
41 return;
43 if (in_pack) {
44 if (packet_trace_pack(buf, len, sideband))
45 return;
46 } else if (starts_with(buf, "PACK") || starts_with(buf, "\1PACK")) {
47 in_pack = 1;
48 sideband = *buf == '\1';
49 packet_trace_pack(buf, len, sideband);
52 * Make a note in the human-readable trace that the pack data
53 * started.
55 buf = "PACK ...";
56 len = strlen(buf);
59 if (!trace_want(&trace_packet))
60 return;
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 */
71 if (buf[i] == '\n')
72 continue;
73 if (buf[i] >= 0x20 && buf[i] <= 0x7e)
74 strbuf_addch(&out, buf[i]);
75 else
76 strbuf_addf(&out, "\\%o", buf[i]);
79 strbuf_addch(&out, '\n');
80 trace_strbuf(&trace_packet, &out);
81 strbuf_release(&out);
85 * If we buffered things up above (we don't, but we should),
86 * we'd flush it here
88 void packet_flush(int fd)
90 packet_trace("0000", 4, 1);
91 write_or_die(fd, "0000", 4);
94 void packet_delim(int fd)
96 packet_trace("0001", 4, 1);
97 write_or_die(fd, "0001", 4);
100 int packet_flush_gently(int fd)
102 packet_trace("0000", 4, 1);
103 if (write_in_full(fd, "0000", 4) < 0)
104 return error(_("flush packet write failed"));
105 return 0;
108 void packet_buf_flush(struct strbuf *buf)
110 packet_trace("0000", 4, 1);
111 strbuf_add(buf, "0000", 4);
114 void packet_buf_delim(struct strbuf *buf)
116 packet_trace("0001", 4, 1);
117 strbuf_add(buf, "0001", 4);
120 static void set_packet_header(char *buf, const int size)
122 static char hexchar[] = "0123456789abcdef";
124 #define hex(a) (hexchar[(a) & 15])
125 buf[0] = hex(size >> 12);
126 buf[1] = hex(size >> 8);
127 buf[2] = hex(size >> 4);
128 buf[3] = hex(size);
129 #undef hex
132 static void format_packet(struct strbuf *out, const char *prefix,
133 const char *fmt, va_list args)
135 size_t orig_len, n;
137 orig_len = out->len;
138 strbuf_addstr(out, "0000");
139 strbuf_addstr(out, prefix);
140 strbuf_vaddf(out, fmt, args);
141 n = out->len - orig_len;
143 if (n > LARGE_PACKET_MAX)
144 die(_("protocol error: impossibly long line"));
146 set_packet_header(&out->buf[orig_len], n);
147 packet_trace(out->buf + orig_len + 4, n - 4, 1);
150 static int packet_write_fmt_1(int fd, int gently, const char *prefix,
151 const char *fmt, va_list args)
153 static struct strbuf buf = STRBUF_INIT;
155 strbuf_reset(&buf);
156 format_packet(&buf, prefix, fmt, args);
157 if (write_in_full(fd, buf.buf, buf.len) < 0) {
158 if (!gently) {
159 check_pipe(errno);
160 die_errno(_("packet write with format failed"));
162 return error(_("packet write with format failed"));
165 return 0;
168 void packet_write_fmt(int fd, const char *fmt, ...)
170 va_list args;
172 va_start(args, fmt);
173 packet_write_fmt_1(fd, 0, "", fmt, args);
174 va_end(args);
177 int packet_write_fmt_gently(int fd, const char *fmt, ...)
179 int status;
180 va_list args;
182 va_start(args, fmt);
183 status = packet_write_fmt_1(fd, 1, "", fmt, args);
184 va_end(args);
185 return status;
188 static int packet_write_gently(const int fd_out, const char *buf, size_t size)
190 static char packet_write_buffer[LARGE_PACKET_MAX];
191 size_t packet_size;
193 if (size > sizeof(packet_write_buffer) - 4)
194 return error(_("packet write failed - data exceeds max packet size"));
196 packet_trace(buf, size, 1);
197 packet_size = size + 4;
198 set_packet_header(packet_write_buffer, packet_size);
199 memcpy(packet_write_buffer + 4, buf, size);
200 if (write_in_full(fd_out, packet_write_buffer, packet_size) < 0)
201 return error(_("packet write failed"));
202 return 0;
205 void packet_write(int fd_out, const char *buf, size_t size)
207 if (packet_write_gently(fd_out, buf, size))
208 die_errno(_("packet write failed"));
211 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
213 va_list args;
215 va_start(args, fmt);
216 format_packet(buf, "", fmt, args);
217 va_end(args);
220 void packet_buf_write_len(struct strbuf *buf, const char *data, size_t len)
222 size_t orig_len, n;
224 orig_len = buf->len;
225 strbuf_addstr(buf, "0000");
226 strbuf_add(buf, data, len);
227 n = buf->len - orig_len;
229 if (n > LARGE_PACKET_MAX)
230 die(_("protocol error: impossibly long line"));
232 set_packet_header(&buf->buf[orig_len], n);
233 packet_trace(data, len, 1);
236 int write_packetized_from_fd(int fd_in, int fd_out)
238 static char buf[LARGE_PACKET_DATA_MAX];
239 int err = 0;
240 ssize_t bytes_to_write;
242 while (!err) {
243 bytes_to_write = xread(fd_in, buf, sizeof(buf));
244 if (bytes_to_write < 0)
245 return COPY_READ_ERROR;
246 if (bytes_to_write == 0)
247 break;
248 err = packet_write_gently(fd_out, buf, bytes_to_write);
250 if (!err)
251 err = packet_flush_gently(fd_out);
252 return err;
255 int write_packetized_from_buf(const char *src_in, size_t len, int fd_out)
257 int err = 0;
258 size_t bytes_written = 0;
259 size_t bytes_to_write;
261 while (!err) {
262 if ((len - bytes_written) > LARGE_PACKET_DATA_MAX)
263 bytes_to_write = LARGE_PACKET_DATA_MAX;
264 else
265 bytes_to_write = len - bytes_written;
266 if (bytes_to_write == 0)
267 break;
268 err = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);
269 bytes_written += bytes_to_write;
271 if (!err)
272 err = packet_flush_gently(fd_out);
273 return err;
276 static int get_packet_data(int fd, char **src_buf, size_t *src_size,
277 void *dst, unsigned size, int options)
279 ssize_t ret;
281 if (fd >= 0 && src_buf && *src_buf)
282 BUG("multiple sources given to packet_read");
284 /* Read up to "size" bytes from our source, whatever it is. */
285 if (src_buf && *src_buf) {
286 ret = size < *src_size ? size : *src_size;
287 memcpy(dst, *src_buf, ret);
288 *src_buf += ret;
289 *src_size -= ret;
290 } else {
291 ret = read_in_full(fd, dst, size);
292 if (ret < 0)
293 die_errno(_("read error"));
296 /* And complain if we didn't get enough bytes to satisfy the read. */
297 if (ret != size) {
298 if (options & PACKET_READ_GENTLE_ON_EOF)
299 return -1;
301 die(_("the remote end hung up unexpectedly"));
304 return ret;
307 static int packet_length(const char *linelen)
309 int val = hex2chr(linelen);
310 return (val < 0) ? val : (val << 8) | hex2chr(linelen + 2);
313 enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
314 size_t *src_len, char *buffer,
315 unsigned size, int *pktlen,
316 int options)
318 int len;
319 char linelen[4];
321 if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) {
322 *pktlen = -1;
323 return PACKET_READ_EOF;
326 len = packet_length(linelen);
328 if (len < 0) {
329 die(_("protocol error: bad line length character: %.4s"), linelen);
330 } else if (!len) {
331 packet_trace("0000", 4, 0);
332 *pktlen = 0;
333 return PACKET_READ_FLUSH;
334 } else if (len == 1) {
335 packet_trace("0001", 4, 0);
336 *pktlen = 0;
337 return PACKET_READ_DELIM;
338 } else if (len < 4) {
339 die(_("protocol error: bad line length %d"), len);
342 len -= 4;
343 if ((unsigned)len >= size)
344 die(_("protocol error: bad line length %d"), len);
346 if (get_packet_data(fd, src_buffer, src_len, buffer, len, options) < 0) {
347 *pktlen = -1;
348 return PACKET_READ_EOF;
351 if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
352 starts_with(buffer, "ERR "))
353 die(_("remote error: %s"), buffer + 4);
355 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
356 len && buffer[len-1] == '\n')
357 len--;
359 buffer[len] = 0;
360 packet_trace(buffer, len, 0);
361 *pktlen = len;
362 return PACKET_READ_NORMAL;
365 int packet_read(int fd, char **src_buffer, size_t *src_len,
366 char *buffer, unsigned size, int options)
368 int pktlen = -1;
370 packet_read_with_status(fd, src_buffer, src_len, buffer, size,
371 &pktlen, options);
373 return pktlen;
376 static char *packet_read_line_generic(int fd,
377 char **src, size_t *src_len,
378 int *dst_len)
380 int len = packet_read(fd, src, src_len,
381 packet_buffer, sizeof(packet_buffer),
382 PACKET_READ_CHOMP_NEWLINE);
383 if (dst_len)
384 *dst_len = len;
385 return (len > 0) ? packet_buffer : NULL;
388 char *packet_read_line(int fd, int *len_p)
390 return packet_read_line_generic(fd, NULL, NULL, len_p);
393 int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
395 int len = packet_read(fd, NULL, NULL,
396 packet_buffer, sizeof(packet_buffer),
397 PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
398 if (dst_len)
399 *dst_len = len;
400 if (dst_line)
401 *dst_line = (len > 0) ? packet_buffer : NULL;
402 return len;
405 char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
407 return packet_read_line_generic(-1, src, src_len, dst_len);
410 ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out)
412 int packet_len;
414 size_t orig_len = sb_out->len;
415 size_t orig_alloc = sb_out->alloc;
417 for (;;) {
418 strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);
419 packet_len = packet_read(fd_in, NULL, NULL,
420 /* strbuf_grow() above always allocates one extra byte to
421 * store a '\0' at the end of the string. packet_read()
422 * writes a '\0' extra byte at the end, too. Let it know
423 * that there is already room for the extra byte.
425 sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,
426 PACKET_READ_GENTLE_ON_EOF);
427 if (packet_len <= 0)
428 break;
429 sb_out->len += packet_len;
432 if (packet_len < 0) {
433 if (orig_alloc == 0)
434 strbuf_release(sb_out);
435 else
436 strbuf_setlen(sb_out, orig_len);
437 return packet_len;
439 return sb_out->len - orig_len;
442 int recv_sideband(const char *me, int in_stream, int out)
444 char buf[LARGE_PACKET_MAX + 1];
445 int len;
446 struct strbuf scratch = STRBUF_INIT;
447 enum sideband_type sideband_type;
449 while (1) {
450 len = packet_read(in_stream, NULL, NULL, buf, LARGE_PACKET_MAX,
452 if (!demultiplex_sideband(me, buf, len, 0, &scratch,
453 &sideband_type))
454 continue;
455 switch (sideband_type) {
456 case SIDEBAND_PRIMARY:
457 write_or_die(out, buf + 1, len - 1);
458 break;
459 default: /* errors: message already written */
460 return sideband_type;
465 /* Packet Reader Functions */
466 void packet_reader_init(struct packet_reader *reader, int fd,
467 char *src_buffer, size_t src_len,
468 int options)
470 memset(reader, 0, sizeof(*reader));
472 reader->fd = fd;
473 reader->src_buffer = src_buffer;
474 reader->src_len = src_len;
475 reader->buffer = packet_buffer;
476 reader->buffer_size = sizeof(packet_buffer);
477 reader->options = options;
478 reader->me = "git";
481 enum packet_read_status packet_reader_read(struct packet_reader *reader)
483 struct strbuf scratch = STRBUF_INIT;
485 if (reader->line_peeked) {
486 reader->line_peeked = 0;
487 return reader->status;
491 * Consume all progress packets until a primary payload packet is
492 * received
494 while (1) {
495 enum sideband_type sideband_type;
496 reader->status = packet_read_with_status(reader->fd,
497 &reader->src_buffer,
498 &reader->src_len,
499 reader->buffer,
500 reader->buffer_size,
501 &reader->pktlen,
502 reader->options);
503 if (!reader->use_sideband)
504 break;
505 if (demultiplex_sideband(reader->me, reader->buffer,
506 reader->pktlen, 1, &scratch,
507 &sideband_type))
508 break;
511 if (reader->status == PACKET_READ_NORMAL)
512 /* Skip the sideband designator if sideband is used */
513 reader->line = reader->use_sideband ?
514 reader->buffer + 1 : reader->buffer;
515 else
516 reader->line = NULL;
518 return reader->status;
521 enum packet_read_status packet_reader_peek(struct packet_reader *reader)
523 /* Only allow peeking a single line */
524 if (reader->line_peeked)
525 return reader->status;
527 /* Peek a line by reading it and setting peeked flag */
528 packet_reader_read(reader);
529 reader->line_peeked = 1;
530 return reader->status;
533 void packet_writer_init(struct packet_writer *writer, int dest_fd)
535 writer->dest_fd = dest_fd;
536 writer->use_sideband = 0;
539 void packet_writer_write(struct packet_writer *writer, const char *fmt, ...)
541 va_list args;
543 va_start(args, fmt);
544 packet_write_fmt_1(writer->dest_fd, 0,
545 writer->use_sideband ? "\001" : "", fmt, args);
546 va_end(args);
549 void packet_writer_error(struct packet_writer *writer, const char *fmt, ...)
551 va_list args;
553 va_start(args, fmt);
554 packet_write_fmt_1(writer->dest_fd, 0,
555 writer->use_sideband ? "\003" : "ERR ", fmt, args);
556 va_end(args);
559 void packet_writer_delim(struct packet_writer *writer)
561 packet_delim(writer->dest_fd);
564 void packet_writer_flush(struct packet_writer *writer)
566 packet_flush(writer->dest_fd);