]> git.ipfire.org Git - thirdparty/git.git/blob - pkt-line.c
rebase --update-refs: fix loops
[thirdparty/git.git] / pkt-line.c
1 #include "cache.h"
2 #include "pkt-line.h"
3 #include "run-command.h"
4
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);
9
10 void packet_trace_identity(const char *prog)
11 {
12 packet_trace_prefix = xstrdup(prog);
13 }
14
15 static const char *get_trace_prefix(void)
16 {
17 return in_async() ? "sideband" : packet_trace_prefix;
18 }
19
20 static int packet_trace_pack(const char *buf, unsigned int len, int sideband)
21 {
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;
31 }
32 }
33
34 static void packet_trace(const char *buf, unsigned int len, int write)
35 {
36 int i;
37 struct strbuf out;
38 static int in_pack, sideband;
39
40 if (!trace_want(&trace_packet) && !trace_want(&trace_pack))
41 return;
42
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);
50
51 /*
52 * Make a note in the human-readable trace that the pack data
53 * started.
54 */
55 buf = "PACK ...";
56 len = strlen(buf);
57 }
58
59 if (!trace_want(&trace_packet))
60 return;
61
62 /* +32 is just a guess for header + quoting */
63 strbuf_init(&out, len+32);
64
65 strbuf_addf(&out, "packet: %12s%c ",
66 get_trace_prefix(), write ? '>' : '<');
67
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]);
77 }
78
79 strbuf_addch(&out, '\n');
80 trace_strbuf(&trace_packet, &out);
81 strbuf_release(&out);
82 }
83
84 /*
85 * If we buffered things up above (we don't, but we should),
86 * we'd flush it here
87 */
88 void packet_flush(int fd)
89 {
90 packet_trace("0000", 4, 1);
91 if (write_in_full(fd, "0000", 4) < 0)
92 die_errno(_("unable to write flush packet"));
93 }
94
95 void packet_delim(int fd)
96 {
97 packet_trace("0001", 4, 1);
98 if (write_in_full(fd, "0001", 4) < 0)
99 die_errno(_("unable to write delim packet"));
100 }
101
102 void packet_response_end(int fd)
103 {
104 packet_trace("0002", 4, 1);
105 if (write_in_full(fd, "0002", 4) < 0)
106 die_errno(_("unable to write response end packet"));
107 }
108
109 int packet_flush_gently(int fd)
110 {
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;
115 }
116
117 void packet_buf_flush(struct strbuf *buf)
118 {
119 packet_trace("0000", 4, 1);
120 strbuf_add(buf, "0000", 4);
121 }
122
123 void packet_buf_delim(struct strbuf *buf)
124 {
125 packet_trace("0001", 4, 1);
126 strbuf_add(buf, "0001", 4);
127 }
128
129 void set_packet_header(char *buf, int size)
130 {
131 static char hexchar[] = "0123456789abcdef";
132
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
139 }
140
141 static void format_packet(struct strbuf *out, const char *prefix,
142 const char *fmt, va_list args)
143 {
144 size_t orig_len, n;
145
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;
151
152 if (n > LARGE_PACKET_MAX)
153 die(_("protocol error: impossibly long line"));
154
155 set_packet_header(&out->buf[orig_len], n);
156 packet_trace(out->buf + orig_len + 4, n - 4, 1);
157 }
158
159 static int packet_write_fmt_1(int fd, int gently, const char *prefix,
160 const char *fmt, va_list args)
161 {
162 static struct strbuf buf = STRBUF_INIT;
163
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"));
170 }
171 return error(_("packet write with format failed"));
172 }
173
174 return 0;
175 }
176
177 void packet_write_fmt(int fd, const char *fmt, ...)
178 {
179 va_list args;
180
181 va_start(args, fmt);
182 packet_write_fmt_1(fd, 0, "", fmt, args);
183 va_end(args);
184 }
185
186 int packet_write_fmt_gently(int fd, const char *fmt, ...)
187 {
188 int status;
189 va_list args;
190
191 va_start(args, fmt);
192 status = packet_write_fmt_1(fd, 1, "", fmt, args);
193 va_end(args);
194 return status;
195 }
196
197 static int do_packet_write(const int fd_out, const char *buf, size_t size,
198 struct strbuf *err)
199 {
200 char header[4];
201 size_t packet_size;
202
203 if (size > LARGE_PACKET_DATA_MAX) {
204 strbuf_addstr(err, _("packet write failed - data exceeds max packet size"));
205 return -1;
206 }
207
208 packet_trace(buf, size, 1);
209 packet_size = size + 4;
210
211 set_packet_header(header, packet_size);
212
213 /*
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.
218 */
219
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;
224 }
225 return 0;
226 }
227
228 static int packet_write_gently(const int fd_out, const char *buf, size_t size)
229 {
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;
235 }
236 return 0;
237 }
238
239 void packet_write(int fd_out, const char *buf, size_t size)
240 {
241 struct strbuf err = STRBUF_INIT;
242 if (do_packet_write(fd_out, buf, size, &err))
243 die("%s", err.buf);
244 }
245
246 void packet_fwrite(FILE *f, const char *buf, size_t size)
247 {
248 size_t packet_size;
249 char header[4];
250
251 if (size > LARGE_PACKET_DATA_MAX)
252 die(_("packet write failed - data exceeds max packet size"));
253
254 packet_trace(buf, size, 1);
255 packet_size = size + 4;
256
257 set_packet_header(header, packet_size);
258 fwrite_or_die(f, header, 4);
259 fwrite_or_die(f, buf, size);
260 }
261
262 void packet_fwrite_fmt(FILE *fh, const char *fmt, ...)
263 {
264 static struct strbuf buf = STRBUF_INIT;
265 va_list args;
266
267 strbuf_reset(&buf);
268
269 va_start(args, fmt);
270 format_packet(&buf, "", fmt, args);
271 va_end(args);
272
273 fwrite_or_die(fh, buf.buf, buf.len);
274 }
275
276 void packet_fflush(FILE *f)
277 {
278 packet_trace("0000", 4, 1);
279 fwrite_or_die(f, "0000", 4);
280 fflush_or_die(f);
281 }
282
283 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
284 {
285 va_list args;
286
287 va_start(args, fmt);
288 format_packet(buf, "", fmt, args);
289 va_end(args);
290 }
291
292 int write_packetized_from_fd_no_flush(int fd_in, int fd_out)
293 {
294 char *buf = xmalloc(LARGE_PACKET_DATA_MAX);
295 int err = 0;
296 ssize_t bytes_to_write;
297
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;
303 }
304 if (bytes_to_write == 0)
305 break;
306 err = packet_write_gently(fd_out, buf, bytes_to_write);
307 }
308 free(buf);
309 return err;
310 }
311
312 int write_packetized_from_buf_no_flush_count(const char *src_in, size_t len,
313 int fd_out, int *packet_counter)
314 {
315 int err = 0;
316 size_t bytes_written = 0;
317 size_t bytes_to_write;
318
319 while (!err) {
320 if ((len - bytes_written) > LARGE_PACKET_DATA_MAX)
321 bytes_to_write = LARGE_PACKET_DATA_MAX;
322 else
323 bytes_to_write = len - bytes_written;
324 if (bytes_to_write == 0)
325 break;
326 err = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);
327 bytes_written += bytes_to_write;
328 if (packet_counter)
329 (*packet_counter)++;
330 }
331 return err;
332 }
333
334 static int get_packet_data(int fd, char **src_buf, size_t *src_size,
335 void *dst, unsigned size, int options)
336 {
337 ssize_t ret;
338
339 if (fd >= 0 && src_buf && *src_buf)
340 BUG("multiple sources given to packet_read");
341
342 /* Read up to "size" bytes from our source, whatever it is. */
343 if (src_buf && *src_buf) {
344 ret = size < *src_size ? size : *src_size;
345 memcpy(dst, *src_buf, ret);
346 *src_buf += ret;
347 *src_size -= ret;
348 } else {
349 ret = read_in_full(fd, dst, size);
350 if (ret < 0) {
351 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
352 return error_errno(_("read error"));
353 die_errno(_("read error"));
354 }
355 }
356
357 /* And complain if we didn't get enough bytes to satisfy the read. */
358 if (ret != size) {
359 if (options & PACKET_READ_GENTLE_ON_EOF)
360 return -1;
361
362 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
363 return error(_("the remote end hung up unexpectedly"));
364 die(_("the remote end hung up unexpectedly"));
365 }
366
367 return ret;
368 }
369
370 int packet_length(const char lenbuf_hex[4])
371 {
372 int val = hex2chr(lenbuf_hex);
373 return (val < 0) ? val : (val << 8) | hex2chr(lenbuf_hex + 2);
374 }
375
376 static char *find_packfile_uri_path(const char *buffer)
377 {
378 const char *URI_MARK = "://";
379 char *path;
380 int len;
381
382 /* First char is sideband mark */
383 buffer += 1;
384
385 len = strspn(buffer, "0123456789abcdefABCDEF");
386 /* size of SHA1 and SHA256 hash */
387 if (!(len == 40 || len == 64) || buffer[len] != ' ')
388 return NULL; /* required "<hash>SP" not seen */
389
390 path = strstr(buffer + len + 1, URI_MARK);
391 if (!path)
392 return NULL;
393
394 path = strchr(path + strlen(URI_MARK), '/');
395 if (!path || !*(path + 1))
396 return NULL;
397
398 /* position after '/' */
399 return ++path;
400 }
401
402 enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
403 size_t *src_len, char *buffer,
404 unsigned size, int *pktlen,
405 int options)
406 {
407 int len;
408 char linelen[4];
409 char *uri_path_start;
410
411 if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) {
412 *pktlen = -1;
413 return PACKET_READ_EOF;
414 }
415
416 len = packet_length(linelen);
417
418 if (len < 0) {
419 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
420 return error(_("protocol error: bad line length "
421 "character: %.4s"), linelen);
422 die(_("protocol error: bad line length character: %.4s"), linelen);
423 } else if (!len) {
424 packet_trace("0000", 4, 0);
425 *pktlen = 0;
426 return PACKET_READ_FLUSH;
427 } else if (len == 1) {
428 packet_trace("0001", 4, 0);
429 *pktlen = 0;
430 return PACKET_READ_DELIM;
431 } else if (len == 2) {
432 packet_trace("0002", 4, 0);
433 *pktlen = 0;
434 return PACKET_READ_RESPONSE_END;
435 } else if (len < 4) {
436 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
437 return error(_("protocol error: bad line length %d"),
438 len);
439 die(_("protocol error: bad line length %d"), len);
440 }
441
442 len -= 4;
443 if ((unsigned)len >= size) {
444 if (options & PACKET_READ_GENTLE_ON_READ_ERROR)
445 return error(_("protocol error: bad line length %d"),
446 len);
447 die(_("protocol error: bad line length %d"), len);
448 }
449
450 if (get_packet_data(fd, src_buffer, src_len, buffer, len, options) < 0) {
451 *pktlen = -1;
452 return PACKET_READ_EOF;
453 }
454
455 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
456 len && buffer[len-1] == '\n')
457 len--;
458
459 buffer[len] = 0;
460 if (options & PACKET_READ_REDACT_URI_PATH &&
461 (uri_path_start = find_packfile_uri_path(buffer))) {
462 const char *redacted = "<redacted>";
463 struct strbuf tracebuf = STRBUF_INIT;
464 strbuf_insert(&tracebuf, 0, buffer, len);
465 strbuf_splice(&tracebuf, uri_path_start - buffer,
466 strlen(uri_path_start), redacted, strlen(redacted));
467 packet_trace(tracebuf.buf, tracebuf.len, 0);
468 strbuf_release(&tracebuf);
469 } else {
470 packet_trace(buffer, len, 0);
471 }
472
473 if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
474 starts_with(buffer, "ERR "))
475 die(_("remote error: %s"), buffer + 4);
476
477 *pktlen = len;
478 return PACKET_READ_NORMAL;
479 }
480
481 int packet_read(int fd, char *buffer, unsigned size, int options)
482 {
483 int pktlen = -1;
484
485 packet_read_with_status(fd, NULL, NULL, buffer, size, &pktlen,
486 options);
487
488 return pktlen;
489 }
490
491 char *packet_read_line(int fd, int *dst_len)
492 {
493 int len = packet_read(fd, packet_buffer, sizeof(packet_buffer),
494 PACKET_READ_CHOMP_NEWLINE);
495 if (dst_len)
496 *dst_len = len;
497 return (len > 0) ? packet_buffer : NULL;
498 }
499
500 int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
501 {
502 int len = packet_read(fd, packet_buffer, sizeof(packet_buffer),
503 PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
504 if (dst_len)
505 *dst_len = len;
506 if (dst_line)
507 *dst_line = (len > 0) ? packet_buffer : NULL;
508 return len;
509 }
510
511 ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out, int options)
512 {
513 int packet_len;
514
515 size_t orig_len = sb_out->len;
516 size_t orig_alloc = sb_out->alloc;
517
518 for (;;) {
519 strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);
520 packet_len = packet_read(fd_in,
521 /* strbuf_grow() above always allocates one extra byte to
522 * store a '\0' at the end of the string. packet_read()
523 * writes a '\0' extra byte at the end, too. Let it know
524 * that there is already room for the extra byte.
525 */
526 sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,
527 options);
528 if (packet_len <= 0)
529 break;
530 sb_out->len += packet_len;
531 }
532
533 if (packet_len < 0) {
534 if (orig_alloc == 0)
535 strbuf_release(sb_out);
536 else
537 strbuf_setlen(sb_out, orig_len);
538 return packet_len;
539 }
540 return sb_out->len - orig_len;
541 }
542
543 int recv_sideband(const char *me, int in_stream, int out)
544 {
545 char buf[LARGE_PACKET_MAX + 1];
546 int len;
547 struct strbuf scratch = STRBUF_INIT;
548 enum sideband_type sideband_type;
549
550 while (1) {
551 int status = packet_read_with_status(in_stream, NULL, NULL,
552 buf, LARGE_PACKET_MAX,
553 &len,
554 PACKET_READ_GENTLE_ON_EOF);
555 if (!demultiplex_sideband(me, status, buf, len, 0, &scratch,
556 &sideband_type))
557 continue;
558 switch (sideband_type) {
559 case SIDEBAND_PRIMARY:
560 write_or_die(out, buf + 1, len - 1);
561 break;
562 default: /* errors: message already written */
563 if (scratch.len > 0)
564 BUG("unhandled incomplete sideband: '%s'",
565 scratch.buf);
566 return sideband_type;
567 }
568 }
569 }
570
571 /* Packet Reader Functions */
572 void packet_reader_init(struct packet_reader *reader, int fd,
573 char *src_buffer, size_t src_len,
574 int options)
575 {
576 memset(reader, 0, sizeof(*reader));
577
578 reader->fd = fd;
579 reader->src_buffer = src_buffer;
580 reader->src_len = src_len;
581 reader->buffer = packet_buffer;
582 reader->buffer_size = sizeof(packet_buffer);
583 reader->options = options;
584 reader->me = "git";
585 reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
586 }
587
588 enum packet_read_status packet_reader_read(struct packet_reader *reader)
589 {
590 struct strbuf scratch = STRBUF_INIT;
591
592 if (reader->line_peeked) {
593 reader->line_peeked = 0;
594 return reader->status;
595 }
596
597 /*
598 * Consume all progress packets until a primary payload packet is
599 * received
600 */
601 while (1) {
602 enum sideband_type sideband_type;
603 reader->status = packet_read_with_status(reader->fd,
604 &reader->src_buffer,
605 &reader->src_len,
606 reader->buffer,
607 reader->buffer_size,
608 &reader->pktlen,
609 reader->options);
610 if (!reader->use_sideband)
611 break;
612 if (demultiplex_sideband(reader->me, reader->status,
613 reader->buffer, reader->pktlen, 1,
614 &scratch, &sideband_type))
615 break;
616 }
617
618 if (reader->status == PACKET_READ_NORMAL)
619 /* Skip the sideband designator if sideband is used */
620 reader->line = reader->use_sideband ?
621 reader->buffer + 1 : reader->buffer;
622 else
623 reader->line = NULL;
624
625 return reader->status;
626 }
627
628 enum packet_read_status packet_reader_peek(struct packet_reader *reader)
629 {
630 /* Only allow peeking a single line */
631 if (reader->line_peeked)
632 return reader->status;
633
634 /* Peek a line by reading it and setting peeked flag */
635 packet_reader_read(reader);
636 reader->line_peeked = 1;
637 return reader->status;
638 }
639
640 void packet_writer_init(struct packet_writer *writer, int dest_fd)
641 {
642 writer->dest_fd = dest_fd;
643 writer->use_sideband = 0;
644 }
645
646 void packet_writer_write(struct packet_writer *writer, const char *fmt, ...)
647 {
648 va_list args;
649
650 va_start(args, fmt);
651 packet_write_fmt_1(writer->dest_fd, 0,
652 writer->use_sideband ? "\001" : "", fmt, args);
653 va_end(args);
654 }
655
656 void packet_writer_error(struct packet_writer *writer, const char *fmt, ...)
657 {
658 va_list args;
659
660 va_start(args, fmt);
661 packet_write_fmt_1(writer->dest_fd, 0,
662 writer->use_sideband ? "\003" : "ERR ", fmt, args);
663 va_end(args);
664 }
665
666 void packet_writer_delim(struct packet_writer *writer)
667 {
668 packet_delim(writer->dest_fd);
669 }
670
671 void packet_writer_flush(struct packet_writer *writer)
672 {
673 packet_flush(writer->dest_fd);
674 }