pkt-line.c 13 KB
Newer Older
1 2
#include "cache.h"
#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 11 12 13 14

void packet_trace_identity(const char *prog)
{
	packet_trace_prefix = xstrdup(prog);
}

15 16 17 18 19
static const char *get_trace_prefix(void)
{
	return in_async() ? "sideband" : packet_trace_prefix;
}

20 21 22 23 24 25 26 27 28 29 30 31 32 33
static int packet_trace_pack(const char *buf, unsigned int len, int sideband)
{
	if (!sideband) {
		trace_verbatim(&trace_pack, buf, len);
		return 1;
	} else if (len && *buf == '\1') {
		trace_verbatim(&trace_pack, buf + 1, len - 1);
		return 1;
	} else {
		/* it's another non-pack sideband */
		return 0;
	}
}

34 35 36 37
static void packet_trace(const char *buf, unsigned int len, int write)
{
	int i;
	struct strbuf out;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	static int in_pack, sideband;

	if (!trace_want(&trace_packet) && !trace_want(&trace_pack))
		return;

	if (in_pack) {
		if (packet_trace_pack(buf, len, sideband))
			return;
	} else if (starts_with(buf, "PACK") || starts_with(buf, "\1PACK")) {
		in_pack = 1;
		sideband = *buf == '\1';
		packet_trace_pack(buf, len, sideband);

		/*
		 * Make a note in the human-readable trace that the pack data
		 * started.
		 */
		buf = "PACK ...";
		len = strlen(buf);
	}
58

59
	if (!trace_want(&trace_packet))
60 61 62 63 64 65
		return;

	/* +32 is just a guess for header + quoting */
	strbuf_init(&out, len+32);

	strbuf_addf(&out, "packet: %12s%c ",
66
		    get_trace_prefix(), write ? '>' : '<');
67

68 69 70 71 72 73 74 75 76
	/* XXX we should really handle printable utf8 */
	for (i = 0; i < len; i++) {
		/* suppress newlines */
		if (buf[i] == '\n')
			continue;
		if (buf[i] >= 0x20 && buf[i] <= 0x7e)
			strbuf_addch(&out, buf[i]);
		else
			strbuf_addf(&out, "\\%o", buf[i]);
77 78 79
	}

	strbuf_addch(&out, '\n');
80
	trace_strbuf(&trace_packet, &out);
81 82 83
	strbuf_release(&out);
}

84 85 86 87 88 89
/*
 * If we buffered things up above (we don't, but we should),
 * we'd flush it here
 */
void packet_flush(int fd)
{
90
	packet_trace("0000", 4, 1);
91 92
	if (write_in_full(fd, "0000", 4) < 0)
		die_errno(_("unable to write flush packet"));
93 94
}

95 96 97
void packet_delim(int fd)
{
	packet_trace("0001", 4, 1);
98 99
	if (write_in_full(fd, "0001", 4) < 0)
		die_errno(_("unable to write delim packet"));
100 101
}

102 103 104
int packet_flush_gently(int fd)
{
	packet_trace("0000", 4, 1);
105
	if (write_in_full(fd, "0000", 4) < 0)
106
		return error(_("flush packet write failed"));
107
	return 0;
108 109
}

110 111
void packet_buf_flush(struct strbuf *buf)
{
112
	packet_trace("0000", 4, 1);
113 114 115
	strbuf_add(buf, "0000", 4);
}

116 117 118 119 120 121
void packet_buf_delim(struct strbuf *buf)
{
	packet_trace("0001", 4, 1);
	strbuf_add(buf, "0001", 4);
}

122
void set_packet_header(char *buf, const int size)
123 124
{
	static char hexchar[] = "0123456789abcdef";
125 126 127 128 129 130 131 132 133

	#define hex(a) (hexchar[(a) & 15])
	buf[0] = hex(size >> 12);
	buf[1] = hex(size >> 8);
	buf[2] = hex(size >> 4);
	buf[3] = hex(size);
	#undef hex
}

134 135
static void format_packet(struct strbuf *out, const char *prefix,
			  const char *fmt, va_list args)
136
{
137
	size_t orig_len, n;
138

139 140
	orig_len = out->len;
	strbuf_addstr(out, "0000");
141
	strbuf_addstr(out, prefix);
142 143 144 145
	strbuf_vaddf(out, fmt, args);
	n = out->len - orig_len;

	if (n > LARGE_PACKET_MAX)
146
		die(_("protocol error: impossibly long line"));
147

148
	set_packet_header(&out->buf[orig_len], n);
149
	packet_trace(out->buf + orig_len + 4, n - 4, 1);
150 151
}

152
static int packet_write_fmt_1(int fd, int gently, const char *prefix,
153 154
			      const char *fmt, va_list args)
{
155
	static struct strbuf buf = STRBUF_INIT;
156

157
	strbuf_reset(&buf);
158
	format_packet(&buf, prefix, fmt, args);
159 160 161
	if (write_in_full(fd, buf.buf, buf.len) < 0) {
		if (!gently) {
			check_pipe(errno);
162
			die_errno(_("packet write with format failed"));
163
		}
164
		return error(_("packet write with format failed"));
165
	}
166 167

	return 0;
168 169
}

170
void packet_write_fmt(int fd, const char *fmt, ...)
171 172 173 174
{
	va_list args;

	va_start(args, fmt);
175
	packet_write_fmt_1(fd, 0, "", fmt, args);
176 177 178 179 180 181 182 183 184
	va_end(args);
}

int packet_write_fmt_gently(int fd, const char *fmt, ...)
{
	int status;
	va_list args;

	va_start(args, fmt);
185
	status = packet_write_fmt_1(fd, 1, "", fmt, args);
186
	va_end(args);
187
	return status;
188 189
}

190 191 192 193 194 195
static int packet_write_gently(const int fd_out, const char *buf, size_t size)
{
	static char packet_write_buffer[LARGE_PACKET_MAX];
	size_t packet_size;

	if (size > sizeof(packet_write_buffer) - 4)
196
		return error(_("packet write failed - data exceeds max packet size"));
197 198 199 200 201

	packet_trace(buf, size, 1);
	packet_size = size + 4;
	set_packet_header(packet_write_buffer, packet_size);
	memcpy(packet_write_buffer + 4, buf, size);
202
	if (write_in_full(fd_out, packet_write_buffer, packet_size) < 0)
203
		return error(_("packet write failed"));
204
	return 0;
205 206
}

207 208 209
void packet_write(int fd_out, const char *buf, size_t size)
{
	if (packet_write_gently(fd_out, buf, size))
210
		die_errno(_("packet write failed"));
211 212
}

213 214 215 216 217
void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
218
	format_packet(buf, "", fmt, args);
219 220 221
	va_end(args);
}

222 223 224 225 226 227 228 229 230 231
void packet_buf_write_len(struct strbuf *buf, const char *data, size_t len)
{
	size_t orig_len, n;

	orig_len = buf->len;
	strbuf_addstr(buf, "0000");
	strbuf_add(buf, data, len);
	n = buf->len - orig_len;

	if (n > LARGE_PACKET_MAX)
232
		die(_("protocol error: impossibly long line"));
233 234 235 236 237

	set_packet_header(&buf->buf[orig_len], n);
	packet_trace(data, len, 1);
}

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
int write_packetized_from_fd(int fd_in, int fd_out)
{
	static char buf[LARGE_PACKET_DATA_MAX];
	int err = 0;
	ssize_t bytes_to_write;

	while (!err) {
		bytes_to_write = xread(fd_in, buf, sizeof(buf));
		if (bytes_to_write < 0)
			return COPY_READ_ERROR;
		if (bytes_to_write == 0)
			break;
		err = packet_write_gently(fd_out, buf, bytes_to_write);
	}
	if (!err)
		err = packet_flush_gently(fd_out);
	return err;
}

int write_packetized_from_buf(const char *src_in, size_t len, int fd_out)
{
	int err = 0;
	size_t bytes_written = 0;
	size_t bytes_to_write;

	while (!err) {
		if ((len - bytes_written) > LARGE_PACKET_DATA_MAX)
			bytes_to_write = LARGE_PACKET_DATA_MAX;
		else
			bytes_to_write = len - bytes_written;
		if (bytes_to_write == 0)
			break;
		err = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);
		bytes_written += bytes_to_write;
	}
	if (!err)
		err = packet_flush_gently(fd_out);
	return err;
}

278 279
static int get_packet_data(int fd, char **src_buf, size_t *src_size,
			   void *dst, unsigned size, int options)
280
{
281 282 283
	ssize_t ret;

	if (fd >= 0 && src_buf && *src_buf)
284
		BUG("multiple sources given to packet_read");
285 286 287 288 289 290 291 292 293 294

	/* Read up to "size" bytes from our source, whatever it is. */
	if (src_buf && *src_buf) {
		ret = size < *src_size ? size : *src_size;
		memcpy(dst, *src_buf, ret);
		*src_buf += ret;
		*src_size -= ret;
	} else {
		ret = read_in_full(fd, dst, size);
		if (ret < 0)
295
			die_errno(_("read error"));
296 297 298
	}

	/* And complain if we didn't get enough bytes to satisfy the read. */
299
	if (ret != size) {
300
		if (options & PACKET_READ_GENTLE_ON_EOF)
301 302
			return -1;

303
		die(_("the remote end hung up unexpectedly"));
304 305 306
	}

	return ret;
307 308
}

309
static int packet_length(const char *linelen)
310
{
311 312
	int val = hex2chr(linelen);
	return (val < 0) ? val : (val << 8) | hex2chr(linelen + 2);
313 314
}

315 316 317 318
enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
						size_t *src_len, char *buffer,
						unsigned size, int *pktlen,
						int options)
319
{
320
	int len;
321 322
	char linelen[4];

323 324 325 326 327
	if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) {
		*pktlen = -1;
		return PACKET_READ_EOF;
	}

328
	len = packet_length(linelen);
329 330

	if (len < 0) {
331
		die(_("protocol error: bad line length character: %.4s"), linelen);
332
	} else if (!len) {
333
		packet_trace("0000", 4, 0);
334 335
		*pktlen = 0;
		return PACKET_READ_FLUSH;
336 337 338 339
	} else if (len == 1) {
		packet_trace("0001", 4, 0);
		*pktlen = 0;
		return PACKET_READ_DELIM;
340
	} else if (len < 4) {
341
		die(_("protocol error: bad line length %d"), len);
342
	}
343

344
	len -= 4;
345
	if ((unsigned)len >= size)
346
		die(_("protocol error: bad line length %d"), len);
347 348 349 350 351

	if (get_packet_data(fd, src_buffer, src_len, buffer, len, options) < 0) {
		*pktlen = -1;
		return PACKET_READ_EOF;
	}
352

353 354 355 356
	if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
	    starts_with(buffer, "ERR "))
		die(_("remote error: %s"), buffer + 4);

357 358 359 360
	if ((options & PACKET_READ_CHOMP_NEWLINE) &&
	    len && buffer[len-1] == '\n')
		len--;

361
	buffer[len] = 0;
362
	packet_trace(buffer, len, 0);
363 364 365 366 367 368 369 370 371 372 373 374 375
	*pktlen = len;
	return PACKET_READ_NORMAL;
}

int packet_read(int fd, char **src_buffer, size_t *src_len,
		char *buffer, unsigned size, int options)
{
	int pktlen = -1;

	packet_read_with_status(fd, src_buffer, src_len, buffer, size,
				&pktlen, options);

	return pktlen;
376
}
377

378 379 380
static char *packet_read_line_generic(int fd,
				      char **src, size_t *src_len,
				      int *dst_len)
381
{
382 383
	int len = packet_read(fd, src, src_len,
			      packet_buffer, sizeof(packet_buffer),
384
			      PACKET_READ_CHOMP_NEWLINE);
385 386
	if (dst_len)
		*dst_len = len;
387
	return (len > 0) ? packet_buffer : NULL;
388 389
}

390
char *packet_read_line(int fd, int *len_p)
391
{
392 393
	return packet_read_line_generic(fd, NULL, NULL, len_p);
}
394

395 396 397 398 399 400 401 402 403 404 405 406
int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
{
	int len = packet_read(fd, NULL, NULL,
			      packet_buffer, sizeof(packet_buffer),
			      PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
	if (dst_len)
		*dst_len = len;
	if (dst_line)
		*dst_line = (len > 0) ? packet_buffer : NULL;
	return len;
}

407 408 409
char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
{
	return packet_read_line_generic(-1, src, src_len, dst_len);
410
}
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442

ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out)
{
	int packet_len;

	size_t orig_len = sb_out->len;
	size_t orig_alloc = sb_out->alloc;

	for (;;) {
		strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);
		packet_len = packet_read(fd_in, NULL, NULL,
			/* strbuf_grow() above always allocates one extra byte to
			 * store a '\0' at the end of the string. packet_read()
			 * writes a '\0' extra byte at the end, too. Let it know
			 * that there is already room for the extra byte.
			 */
			sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,
			PACKET_READ_GENTLE_ON_EOF);
		if (packet_len <= 0)
			break;
		sb_out->len += packet_len;
	}

	if (packet_len < 0) {
		if (orig_alloc == 0)
			strbuf_release(sb_out);
		else
			strbuf_setlen(sb_out, orig_len);
		return packet_len;
	}
	return sb_out->len - orig_len;
}
443

444 445 446 447 448 449 450 451 452 453
int recv_sideband(const char *me, int in_stream, int out)
{
	char buf[LARGE_PACKET_MAX + 1];
	int len;
	struct strbuf scratch = STRBUF_INIT;
	enum sideband_type sideband_type;

	while (1) {
		len = packet_read(in_stream, NULL, NULL, buf, LARGE_PACKET_MAX,
				  0);
454
		if (!demultiplex_sideband(me, buf, len, 0, &scratch,
455 456 457 458 459 460 461 462 463 464 465 466
					  &sideband_type))
			continue;
		switch (sideband_type) {
		case SIDEBAND_PRIMARY:
			write_or_die(out, buf + 1, len - 1);
			break;
		default: /* errors: message already written */
			return sideband_type;
		}
	}
}

467 468 469 470 471 472 473 474 475 476 477 478 479
/* Packet Reader Functions */
void packet_reader_init(struct packet_reader *reader, int fd,
			char *src_buffer, size_t src_len,
			int options)
{
	memset(reader, 0, sizeof(*reader));

	reader->fd = fd;
	reader->src_buffer = src_buffer;
	reader->src_len = src_len;
	reader->buffer = packet_buffer;
	reader->buffer_size = sizeof(packet_buffer);
	reader->options = options;
480
	reader->me = "git";
481 482 483 484
}

enum packet_read_status packet_reader_read(struct packet_reader *reader)
{
485 486
	struct strbuf scratch = STRBUF_INIT;

487 488 489 490 491
	if (reader->line_peeked) {
		reader->line_peeked = 0;
		return reader->status;
	}

492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
	/*
	 * Consume all progress packets until a primary payload packet is
	 * received
	 */
	while (1) {
		enum sideband_type sideband_type;
		reader->status = packet_read_with_status(reader->fd,
							 &reader->src_buffer,
							 &reader->src_len,
							 reader->buffer,
							 reader->buffer_size,
							 &reader->pktlen,
							 reader->options);
		if (!reader->use_sideband)
			break;
		if (demultiplex_sideband(reader->me, reader->buffer,
					 reader->pktlen, 1, &scratch,
					 &sideband_type))
			break;
	}
512 513

	if (reader->status == PACKET_READ_NORMAL)
514 515 516
		/* Skip the sideband designator if sideband is used */
		reader->line = reader->use_sideband ?
			reader->buffer + 1 : reader->buffer;
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
	else
		reader->line = NULL;

	return reader->status;
}

enum packet_read_status packet_reader_peek(struct packet_reader *reader)
{
	/* Only allow peeking a single line */
	if (reader->line_peeked)
		return reader->status;

	/* Peek a line by reading it and setting peeked flag */
	packet_reader_read(reader);
	reader->line_peeked = 1;
	return reader->status;
}
534 535 536 537

void packet_writer_init(struct packet_writer *writer, int dest_fd)
{
	writer->dest_fd = dest_fd;
538
	writer->use_sideband = 0;
539 540 541 542 543 544 545
}

void packet_writer_write(struct packet_writer *writer, const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
546 547
	packet_write_fmt_1(writer->dest_fd, 0,
			   writer->use_sideband ? "\001" : "", fmt, args);
548 549 550 551 552 553 554 555
	va_end(args);
}

void packet_writer_error(struct packet_writer *writer, const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
556 557
	packet_write_fmt_1(writer->dest_fd, 0,
			   writer->use_sideband ? "\003" : "ERR ", fmt, args);
558 559 560 561 562 563 564 565 566 567 568 569
	va_end(args);
}

void packet_writer_delim(struct packet_writer *writer)
{
	packet_delim(writer->dest_fd);
}

void packet_writer_flush(struct packet_writer *writer)
{
	packet_flush(writer->dest_fd);
}