l10n: git.pot: v2.34.0 round 3 (1 new)
[alt-git.git] / pkt-line.c
blob2dc8ac274bd0c03b5612e73fb587dde911865f0d
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 if (write_in_full(fd, "0000", 4) < 0)
92 die_errno(_("unable to write flush packet"));
95 void packet_delim(int fd)
97 packet_trace("0001", 4, 1);
98 if (write_in_full(fd, "0001", 4) < 0)
99 die_errno(_("unable to write delim packet"));
102 void packet_response_end(int fd)
104 packet_trace("0002", 4, 1);
105 if (write_in_full(fd, "0002", 4) < 0)
106 die_errno(_("unable to write response end packet"));
109 int packet_flush_gently(int fd)
111 packet_trace("0000", 4, 1);
112 if (write_in_full(fd, "0000", 4) < 0)
113 return error(_("flush packet write failed"));
114 return 0;
117 void packet_buf_flush(struct strbuf *buf)
119 packet_trace("0000", 4, 1);
120 strbuf_add(buf, "0000", 4);
123 void packet_buf_delim(struct strbuf *buf)
125 packet_trace("0001", 4, 1);
126 strbuf_add(buf, "0001", 4);
129 void set_packet_header(char *buf, int size)
131 static char hexchar[] = "0123456789abcdef";
133 #define hex(a) (hexchar[(a) & 15])
134 buf[0] = hex(size >> 12);
135 buf[1] = hex(size >> 8);
136 buf[2] = hex(size >> 4);
137 buf[3] = hex(size);
138 #undef hex
141 static void format_packet(struct strbuf *out, const char *prefix,
142 const char *fmt, va_list args)
144 size_t orig_len, n;
146 orig_len = out->len;
147 strbuf_addstr(out, "0000");
148 strbuf_addstr(out, prefix);
149 strbuf_vaddf(out, fmt, args);
150 n = out->len - orig_len;
152 if (n > LARGE_PACKET_MAX)
153 die(_("protocol error: impossibly long line"));
155 set_packet_header(&out->buf[orig_len], n);
156 packet_trace(out->buf + orig_len + 4, n - 4, 1);
159 static int packet_write_fmt_1(int fd, int gently, const char *prefix,
160 const char *fmt, va_list args)
162 static struct strbuf buf = STRBUF_INIT;
164 strbuf_reset(&buf);
165 format_packet(&buf, prefix, fmt, args);
166 if (write_in_full(fd, buf.buf, buf.len) < 0) {
167 if (!gently) {
168 check_pipe(errno);
169 die_errno(_("packet write with format failed"));
171 return error(_("packet write with format failed"));
174 return 0;
177 void packet_write_fmt(int fd, const char *fmt, ...)
179 va_list args;
181 va_start(args, fmt);
182 packet_write_fmt_1(fd, 0, "", fmt, args);
183 va_end(args);
186 int packet_write_fmt_gently(int fd, const char *fmt, ...)
188 int status;
189 va_list args;
191 va_start(args, fmt);
192 status = packet_write_fmt_1(fd, 1, "", fmt, args);
193 va_end(args);
194 return status;
197 static int do_packet_write(const int fd_out, const char *buf, size_t size,
198 struct strbuf *err)
200 char header[4];
201 size_t packet_size;
203 if (size > LARGE_PACKET_DATA_MAX) {
204 strbuf_addstr(err, _("packet write failed - data exceeds max packet size"));
205 return -1;
208 packet_trace(buf, size, 1);
209 packet_size = size + 4;
211 set_packet_header(header, packet_size);
214 * Write the header and the buffer in 2 parts so that we do
215 * not need to allocate a buffer or rely on a static buffer.
216 * This also avoids putting a large buffer on the stack which
217 * might have multi-threading issues.
220 if (write_in_full(fd_out, header, 4) < 0 ||
221 write_in_full(fd_out, buf, size) < 0) {
222 strbuf_addf(err, _("packet write failed: %s"), strerror(errno));
223 return -1;
225 return 0;
228 static int packet_write_gently(const int fd_out, const char *buf, size_t size)
230 struct strbuf err = STRBUF_INIT;
231 if (do_packet_write(fd_out, buf, size, &err)) {
232 error("%s", err.buf);
233 strbuf_release(&err);
234 return -1;
236 return 0;
239 void packet_write(int fd_out, const char *buf, size_t size)
241 struct strbuf err = STRBUF_INIT;
242 if (do_packet_write(fd_out, buf, size, &err))
243 die("%s", err.buf);
246 void packet_fwrite(FILE *f, const char *buf, size_t size)
248 size_t packet_size;
249 char header[4];
251 if (size > LARGE_PACKET_DATA_MAX)
252 die(_("packet write failed - data exceeds max packet size"));
254 packet_trace(buf, size, 1);
255 packet_size = size + 4;
257 set_packet_header(header, packet_size);
258 fwrite_or_die(f, header, 4);
259 fwrite_or_die(f, buf, size);
262 void packet_fwrite_fmt(FILE *fh, const char *fmt, ...)
264 static struct strbuf buf = STRBUF_INIT;
265 va_list args;
267 strbuf_reset(&buf);
269 va_start(args, fmt);
270 format_packet(&buf, "", fmt, args);
271 va_end(args);
273 fwrite_or_die(fh, buf.buf, buf.len);
276 void packet_fflush(FILE *f)
278 packet_trace("0000", 4, 1);
279 fwrite_or_die(f, "0000", 4);
280 fflush_or_die(f);
283 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
285 va_list args;
287 va_start(args, fmt);
288 format_packet(buf, "", fmt, args);
289 va_end(args);
292 int write_packetized_from_fd_no_flush(int fd_in, int fd_out)
294 char *buf = xmalloc(LARGE_PACKET_DATA_MAX);
295 int err = 0;
296 ssize_t bytes_to_write;
298 while (!err) {
299 bytes_to_write = xread(fd_in, buf, LARGE_PACKET_DATA_MAX);
300 if (bytes_to_write < 0) {
301 free(buf);
302 return COPY_READ_ERROR;
304 if (bytes_to_write == 0)
305 break;
306 err = packet_write_gently(fd_out, buf, bytes_to_write);
308 free(buf);
309 return err;
312 int write_packetized_from_buf_no_flush(const char *src_in, size_t len, int fd_out)
314 int err = 0;
315 size_t bytes_written = 0;
316 size_t bytes_to_write;
318 while (!err) {
319 if ((len - bytes_written) > LARGE_PACKET_DATA_MAX)
320 bytes_to_write = LARGE_PACKET_DATA_MAX;
321 else
322 bytes_to_write = len - bytes_written;
323 if (bytes_to_write == 0)
324 break;
325 err = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);
326 bytes_written += bytes_to_write;
328 return err;
331 static int get_packet_data(int fd, char **src_buf, size_t *src_size,
332 void *dst, unsigned size, int options)
334 ssize_t ret;
336 if (fd >= 0 && src_buf && *src_buf)
337 BUG("multiple sources given to packet_read");
339 /* Read up to "size" bytes from our source, whatever it is. */
340 if (src_buf && *src_buf) {
341 ret = size < *src_size ? size : *src_size;
342 memcpy(dst, *src_buf, ret);
343 *src_buf += ret;
344 *src_size -= ret;
345 } else {
346 ret = read_in_full(fd, dst, size);
347 if (ret < 0) {
348 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
349 return error_errno(_("read error"));
350 die_errno(_("read error"));
354 /* And complain if we didn't get enough bytes to satisfy the read. */
355 if (ret != size) {
356 if (options & PACKET_READ_GENTLE_ON_EOF)
357 return -1;
359 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
360 return error(_("the remote end hung up unexpectedly"));
361 die(_("the remote end hung up unexpectedly"));
364 return ret;
367 int packet_length(const char lenbuf_hex[4])
369 int val = hex2chr(lenbuf_hex);
370 return (val < 0) ? val : (val << 8) | hex2chr(lenbuf_hex + 2);
373 enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
374 size_t *src_len, char *buffer,
375 unsigned size, int *pktlen,
376 int options)
378 int len;
379 char linelen[4];
381 if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) {
382 *pktlen = -1;
383 return PACKET_READ_EOF;
386 len = packet_length(linelen);
388 if (len < 0) {
389 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
390 return error(_("protocol error: bad line length "
391 "character: %.4s"), linelen);
392 die(_("protocol error: bad line length character: %.4s"), linelen);
393 } else if (!len) {
394 packet_trace("0000", 4, 0);
395 *pktlen = 0;
396 return PACKET_READ_FLUSH;
397 } else if (len == 1) {
398 packet_trace("0001", 4, 0);
399 *pktlen = 0;
400 return PACKET_READ_DELIM;
401 } else if (len == 2) {
402 packet_trace("0002", 4, 0);
403 *pktlen = 0;
404 return PACKET_READ_RESPONSE_END;
405 } else if (len < 4) {
406 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
407 return error(_("protocol error: bad line length %d"),
408 len);
409 die(_("protocol error: bad line length %d"), len);
412 len -= 4;
413 if ((unsigned)len >= size) {
414 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
415 return error(_("protocol error: bad line length %d"),
416 len);
417 die(_("protocol error: bad line length %d"), len);
420 if (get_packet_data(fd, src_buffer, src_len, buffer, len, options) < 0) {
421 *pktlen = -1;
422 return PACKET_READ_EOF;
425 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
426 len && buffer[len-1] == '\n')
427 len--;
429 buffer[len] = 0;
430 packet_trace(buffer, len, 0);
432 if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
433 starts_with(buffer, "ERR "))
434 die(_("remote error: %s"), buffer + 4);
436 *pktlen = len;
437 return PACKET_READ_NORMAL;
440 int packet_read(int fd, char *buffer, unsigned size, int options)
442 int pktlen = -1;
444 packet_read_with_status(fd, NULL, NULL, buffer, size, &pktlen,
445 options);
447 return pktlen;
450 char *packet_read_line(int fd, int *dst_len)
452 int len = packet_read(fd, packet_buffer, sizeof(packet_buffer),
453 PACKET_READ_CHOMP_NEWLINE);
454 if (dst_len)
455 *dst_len = len;
456 return (len > 0) ? packet_buffer : NULL;
459 int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
461 int len = packet_read(fd, packet_buffer, sizeof(packet_buffer),
462 PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
463 if (dst_len)
464 *dst_len = len;
465 if (dst_line)
466 *dst_line = (len > 0) ? packet_buffer : NULL;
467 return len;
470 ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out, int options)
472 int packet_len;
474 size_t orig_len = sb_out->len;
475 size_t orig_alloc = sb_out->alloc;
477 for (;;) {
478 strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);
479 packet_len = packet_read(fd_in,
480 /* strbuf_grow() above always allocates one extra byte to
481 * store a '\0' at the end of the string. packet_read()
482 * writes a '\0' extra byte at the end, too. Let it know
483 * that there is already room for the extra byte.
485 sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,
486 options);
487 if (packet_len <= 0)
488 break;
489 sb_out->len += packet_len;
492 if (packet_len < 0) {
493 if (orig_alloc == 0)
494 strbuf_release(sb_out);
495 else
496 strbuf_setlen(sb_out, orig_len);
497 return packet_len;
499 return sb_out->len - orig_len;
502 int recv_sideband(const char *me, int in_stream, int out)
504 char buf[LARGE_PACKET_MAX + 1];
505 int len;
506 struct strbuf scratch = STRBUF_INIT;
507 enum sideband_type sideband_type;
509 while (1) {
510 int status = packet_read_with_status(in_stream, NULL, NULL,
511 buf, LARGE_PACKET_MAX,
512 &len,
513 PACKET_READ_GENTLE_ON_EOF);
514 if (!demultiplex_sideband(me, status, buf, len, 0, &scratch,
515 &sideband_type))
516 continue;
517 switch (sideband_type) {
518 case SIDEBAND_PRIMARY:
519 write_or_die(out, buf + 1, len - 1);
520 break;
521 default: /* errors: message already written */
522 if (scratch.len > 0)
523 BUG("unhandled incomplete sideband: '%s'",
524 scratch.buf);
525 return sideband_type;
530 /* Packet Reader Functions */
531 void packet_reader_init(struct packet_reader *reader, int fd,
532 char *src_buffer, size_t src_len,
533 int options)
535 memset(reader, 0, sizeof(*reader));
537 reader->fd = fd;
538 reader->src_buffer = src_buffer;
539 reader->src_len = src_len;
540 reader->buffer = packet_buffer;
541 reader->buffer_size = sizeof(packet_buffer);
542 reader->options = options;
543 reader->me = "git";
544 reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
547 enum packet_read_status packet_reader_read(struct packet_reader *reader)
549 struct strbuf scratch = STRBUF_INIT;
551 if (reader->line_peeked) {
552 reader->line_peeked = 0;
553 return reader->status;
557 * Consume all progress packets until a primary payload packet is
558 * received
560 while (1) {
561 enum sideband_type sideband_type;
562 reader->status = packet_read_with_status(reader->fd,
563 &reader->src_buffer,
564 &reader->src_len,
565 reader->buffer,
566 reader->buffer_size,
567 &reader->pktlen,
568 reader->options);
569 if (!reader->use_sideband)
570 break;
571 if (demultiplex_sideband(reader->me, reader->status,
572 reader->buffer, reader->pktlen, 1,
573 &scratch, &sideband_type))
574 break;
577 if (reader->status == PACKET_READ_NORMAL)
578 /* Skip the sideband designator if sideband is used */
579 reader->line = reader->use_sideband ?
580 reader->buffer + 1 : reader->buffer;
581 else
582 reader->line = NULL;
584 return reader->status;
587 enum packet_read_status packet_reader_peek(struct packet_reader *reader)
589 /* Only allow peeking a single line */
590 if (reader->line_peeked)
591 return reader->status;
593 /* Peek a line by reading it and setting peeked flag */
594 packet_reader_read(reader);
595 reader->line_peeked = 1;
596 return reader->status;
599 void packet_writer_init(struct packet_writer *writer, int dest_fd)
601 writer->dest_fd = dest_fd;
602 writer->use_sideband = 0;
605 void packet_writer_write(struct packet_writer *writer, const char *fmt, ...)
607 va_list args;
609 va_start(args, fmt);
610 packet_write_fmt_1(writer->dest_fd, 0,
611 writer->use_sideband ? "\001" : "", fmt, args);
612 va_end(args);
615 void packet_writer_error(struct packet_writer *writer, const char *fmt, ...)
617 va_list args;
619 va_start(args, fmt);
620 packet_write_fmt_1(writer->dest_fd, 0,
621 writer->use_sideband ? "\003" : "ERR ", fmt, args);
622 va_end(args);
625 void packet_writer_delim(struct packet_writer *writer)
627 packet_delim(writer->dest_fd);
630 void packet_writer_flush(struct packet_writer *writer)
632 packet_flush(writer->dest_fd);