pkt-line: introduce struct packet_writer
[git/git.git] / pkt-line.c
CommitLineData
f3a3214e
LT
1#include "cache.h"
2#include "pkt-line.h"
fd89433d 3#include "run-command.h"
f3a3214e 4
74543a04 5char packet_buffer[LARGE_PACKET_MAX];
1e4cd68c 6static const char *packet_trace_prefix = "git";
6aa30857 7static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET);
32359838 8static struct trace_key trace_pack = TRACE_KEY_INIT(PACKFILE);
bbc30f99
JK
9
10void packet_trace_identity(const char *prog)
11{
12 packet_trace_prefix = xstrdup(prog);
13}
14
fd89433d
JK
15static const char *get_trace_prefix(void)
16{
17 return in_async() ? "sideband" : packet_trace_prefix;
18}
19
32359838
JK
20static 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
bbc30f99
JK
34static void packet_trace(const char *buf, unsigned int len, int write)
35{
36 int i;
37 struct strbuf out;
32359838
JK
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 }
bbc30f99 58
6aa30857 59 if (!trace_want(&trace_packet))
bbc30f99
JK
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 ",
fd89433d 66 get_trace_prefix(), write ? '>' : '<');
bbc30f99 67
32359838
JK
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]);
bbc30f99
JK
77 }
78
79 strbuf_addch(&out, '\n');
6aa30857 80 trace_strbuf(&trace_packet, &out);
bbc30f99
JK
81 strbuf_release(&out);
82}
83
f3a3214e
LT
84/*
85 * If we buffered things up above (we don't, but we should),
86 * we'd flush it here
87 */
88void packet_flush(int fd)
89{
bbc30f99 90 packet_trace("0000", 4, 1);
cdf4fb8e 91 write_or_die(fd, "0000", 4);
f3a3214e
LT
92}
93
a4cfd41c
BW
94void packet_delim(int fd)
95{
96 packet_trace("0001", 4, 1);
97 write_or_die(fd, "0001", 4);
98}
99
038ce90f
LS
100int packet_flush_gently(int fd)
101{
102 packet_trace("0000", 4, 1);
4c95e3dd 103 if (write_in_full(fd, "0000", 4) < 0)
c60d7697 104 return error(_("flush packet write failed"));
4c95e3dd 105 return 0;
038ce90f
LS
106}
107
f5615d24
SP
108void packet_buf_flush(struct strbuf *buf)
109{
bbc30f99 110 packet_trace("0000", 4, 1);
f5615d24
SP
111 strbuf_add(buf, "0000", 4);
112}
113
a4cfd41c
BW
114void packet_buf_delim(struct strbuf *buf)
115{
116 packet_trace("0001", 4, 1);
117 strbuf_add(buf, "0001", 4);
118}
119
2f60bdd1 120static void set_packet_header(char *buf, const int size)
f3a3214e 121{
f3a3214e 122 static char hexchar[] = "0123456789abcdef";
2f60bdd1
LS
123
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
130}
131
bc2e795c
JT
132static void format_packet(struct strbuf *out, const char *prefix,
133 const char *fmt, va_list args)
2f60bdd1 134{
8e9faf27 135 size_t orig_len, n;
f3a3214e 136
8e9faf27
JK
137 orig_len = out->len;
138 strbuf_addstr(out, "0000");
bc2e795c 139 strbuf_addstr(out, prefix);
8e9faf27
JK
140 strbuf_vaddf(out, fmt, args);
141 n = out->len - orig_len;
142
143 if (n > LARGE_PACKET_MAX)
c60d7697 144 die(_("protocol error: impossibly long line"));
8e9faf27 145
2f60bdd1 146 set_packet_header(&out->buf[orig_len], n);
8e9faf27 147 packet_trace(out->buf + orig_len + 4, n - 4, 1);
f5615d24
SP
148}
149
bc2e795c 150static int packet_write_fmt_1(int fd, int gently, const char *prefix,
70428d1a
LS
151 const char *fmt, va_list args)
152{
150efef1 153 static struct strbuf buf = STRBUF_INIT;
70428d1a 154
150efef1 155 strbuf_reset(&buf);
bc2e795c 156 format_packet(&buf, prefix, fmt, args);
4c95e3dd
JK
157 if (write_in_full(fd, buf.buf, buf.len) < 0) {
158 if (!gently) {
159 check_pipe(errno);
c60d7697 160 die_errno(_("packet write with format failed"));
4c95e3dd 161 }
c60d7697 162 return error(_("packet write with format failed"));
70428d1a 163 }
4c95e3dd
JK
164
165 return 0;
70428d1a
LS
166}
167
81c634e9 168void packet_write_fmt(int fd, const char *fmt, ...)
f5615d24
SP
169{
170 va_list args;
f5615d24
SP
171
172 va_start(args, fmt);
bc2e795c 173 packet_write_fmt_1(fd, 0, "", fmt, args);
70428d1a
LS
174 va_end(args);
175}
176
177int packet_write_fmt_gently(int fd, const char *fmt, ...)
178{
179 int status;
180 va_list args;
181
182 va_start(args, fmt);
bc2e795c 183 status = packet_write_fmt_1(fd, 1, "", fmt, args);
f5615d24 184 va_end(args);
70428d1a 185 return status;
f3a3214e
LT
186}
187
edfb780c
LS
188static int packet_write_gently(const int fd_out, const char *buf, size_t size)
189{
190 static char packet_write_buffer[LARGE_PACKET_MAX];
191 size_t packet_size;
192
193 if (size > sizeof(packet_write_buffer) - 4)
c60d7697 194 return error(_("packet write failed - data exceeds max packet size"));
edfb780c
LS
195
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);
4c95e3dd 200 if (write_in_full(fd_out, packet_write_buffer, packet_size) < 0)
c60d7697 201 return error(_("packet write failed"));
4c95e3dd 202 return 0;
edfb780c
LS
203}
204
5d2124b3
BW
205void packet_write(int fd_out, const char *buf, size_t size)
206{
207 if (packet_write_gently(fd_out, buf, size))
c60d7697 208 die_errno(_("packet write failed"));
5d2124b3
BW
209}
210
f5615d24
SP
211void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
212{
213 va_list args;
f5615d24
SP
214
215 va_start(args, fmt);
bc2e795c 216 format_packet(buf, "", fmt, args);
f5615d24 217 va_end(args);
f5615d24
SP
218}
219
f1f4d8ac
BW
220void packet_buf_write_len(struct strbuf *buf, const char *data, size_t len)
221{
222 size_t orig_len, n;
223
224 orig_len = buf->len;
225 strbuf_addstr(buf, "0000");
226 strbuf_add(buf, data, len);
227 n = buf->len - orig_len;
228
229 if (n > LARGE_PACKET_MAX)
c60d7697 230 die(_("protocol error: impossibly long line"));
f1f4d8ac
BW
231
232 set_packet_header(&buf->buf[orig_len], n);
233 packet_trace(data, len, 1);
234}
235
bb643d8b
LS
236int write_packetized_from_fd(int fd_in, int fd_out)
237{
238 static char buf[LARGE_PACKET_DATA_MAX];
239 int err = 0;
240 ssize_t bytes_to_write;
241
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);
249 }
250 if (!err)
251 err = packet_flush_gently(fd_out);
252 return err;
253}
254
255int write_packetized_from_buf(const char *src_in, size_t len, int fd_out)
256{
257 int err = 0;
258 size_t bytes_written = 0;
259 size_t bytes_to_write;
260
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;
270 }
271 if (!err)
272 err = packet_flush_gently(fd_out);
273 return err;
274}
275
4981fe75
JK
276static int get_packet_data(int fd, char **src_buf, size_t *src_size,
277 void *dst, unsigned size, int options)
f3a3214e 278{
4981fe75
JK
279 ssize_t ret;
280
281 if (fd >= 0 && src_buf && *src_buf)
033abf97 282 BUG("multiple sources given to packet_read");
4981fe75
JK
283
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)
c60d7697 293 die_errno(_("read error"));
4981fe75
JK
294 }
295
296 /* And complain if we didn't get enough bytes to satisfy the read. */
61d36330 297 if (ret != size) {
03809429 298 if (options & PACKET_READ_GENTLE_ON_EOF)
46284dd1
HV
299 return -1;
300
c60d7697 301 die(_("the remote end hung up unexpectedly"));
46284dd1
HV
302 }
303
304 return ret;
f3a3214e
LT
305}
306
f5615d24 307static int packet_length(const char *linelen)
f3a3214e 308{
d2330973
RS
309 int val = hex2chr(linelen);
310 return (val < 0) ? val : (val << 8) | hex2chr(linelen + 2);
f5615d24
SP
311}
312
2153d478
BW
313enum 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)
f5615d24 317{
2153d478 318 int len;
f5615d24
SP
319 char linelen[4];
320
2153d478
BW
321 if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) {
322 *pktlen = -1;
323 return PACKET_READ_EOF;
324 }
325
f5615d24 326 len = packet_length(linelen);
2153d478
BW
327
328 if (len < 0) {
c60d7697 329 die(_("protocol error: bad line length character: %.4s"), linelen);
2153d478 330 } else if (!len) {
bbc30f99 331 packet_trace("0000", 4, 0);
2153d478
BW
332 *pktlen = 0;
333 return PACKET_READ_FLUSH;
a4cfd41c
BW
334 } else if (len == 1) {
335 packet_trace("0001", 4, 0);
336 *pktlen = 0;
337 return PACKET_READ_DELIM;
2153d478 338 } else if (len < 4) {
c60d7697 339 die(_("protocol error: bad line length %d"), len);
bbc30f99 340 }
2153d478 341
f3a3214e 342 len -= 4;
2153d478 343 if ((unsigned)len >= size)
c60d7697 344 die(_("protocol error: bad line length %d"), len);
2153d478
BW
345
346 if (get_packet_data(fd, src_buffer, src_len, buffer, len, options) < 0) {
347 *pktlen = -1;
348 return PACKET_READ_EOF;
349 }
819b929d 350
2d103c31
MS
351 if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
352 starts_with(buffer, "ERR "))
353 die(_("remote error: %s"), buffer + 4);
354
819b929d
JK
355 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
356 len && buffer[len-1] == '\n')
357 len--;
358
f3a3214e 359 buffer[len] = 0;
bbc30f99 360 packet_trace(buffer, len, 0);
2153d478
BW
361 *pktlen = len;
362 return PACKET_READ_NORMAL;
363}
364
365int packet_read(int fd, char **src_buffer, size_t *src_len,
366 char *buffer, unsigned size, int options)
367{
368 int pktlen = -1;
369
370 packet_read_with_status(fd, src_buffer, src_len, buffer, size,
371 &pktlen, options);
372
373 return pktlen;
f3a3214e 374}
f5615d24 375
4981fe75
JK
376static char *packet_read_line_generic(int fd,
377 char **src, size_t *src_len,
378 int *dst_len)
46284dd1 379{
4981fe75
JK
380 int len = packet_read(fd, src, src_len,
381 packet_buffer, sizeof(packet_buffer),
74543a04 382 PACKET_READ_CHOMP_NEWLINE);
4981fe75
JK
383 if (dst_len)
384 *dst_len = len;
974b50c5 385 return (len > 0) ? packet_buffer : NULL;
46284dd1
HV
386}
387
4981fe75 388char *packet_read_line(int fd, int *len_p)
f5615d24 389{
4981fe75
JK
390 return packet_read_line_generic(fd, NULL, NULL, len_p);
391}
f5615d24 392
825b9226
BP
393int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
394{
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;
403}
404
4981fe75
JK
405char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
406{
407 return packet_read_line_generic(-1, src, src_len, dst_len);
f5615d24 408}
bb643d8b
LS
409
410ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out)
411{
412 int packet_len;
413
414 size_t orig_len = sb_out->len;
415 size_t orig_alloc = sb_out->alloc;
416
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.
424 */
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;
430 }
431
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;
438 }
439 return sb_out->len - orig_len;
440}
77dabc14
BW
441
442/* Packet Reader Functions */
443void packet_reader_init(struct packet_reader *reader, int fd,
444 char *src_buffer, size_t src_len,
445 int options)
446{
447 memset(reader, 0, sizeof(*reader));
448
449 reader->fd = fd;
450 reader->src_buffer = src_buffer;
451 reader->src_len = src_len;
452 reader->buffer = packet_buffer;
453 reader->buffer_size = sizeof(packet_buffer);
454 reader->options = options;
455}
456
457enum packet_read_status packet_reader_read(struct packet_reader *reader)
458{
459 if (reader->line_peeked) {
460 reader->line_peeked = 0;
461 return reader->status;
462 }
463
464 reader->status = packet_read_with_status(reader->fd,
465 &reader->src_buffer,
466 &reader->src_len,
467 reader->buffer,
468 reader->buffer_size,
469 &reader->pktlen,
470 reader->options);
471
472 if (reader->status == PACKET_READ_NORMAL)
473 reader->line = reader->buffer;
474 else
475 reader->line = NULL;
476
477 return reader->status;
478}
479
480enum packet_read_status packet_reader_peek(struct packet_reader *reader)
481{
482 /* Only allow peeking a single line */
483 if (reader->line_peeked)
484 return reader->status;
485
486 /* Peek a line by reading it and setting peeked flag */
487 packet_reader_read(reader);
488 reader->line_peeked = 1;
489 return reader->status;
490}
bc2e795c
JT
491
492void packet_writer_init(struct packet_writer *writer, int dest_fd)
493{
494 writer->dest_fd = dest_fd;
495}
496
497void packet_writer_write(struct packet_writer *writer, const char *fmt, ...)
498{
499 va_list args;
500
501 va_start(args, fmt);
502 packet_write_fmt_1(writer->dest_fd, 0, "", fmt, args);
503 va_end(args);
504}
505
506void packet_writer_error(struct packet_writer *writer, const char *fmt, ...)
507{
508 va_list args;
509
510 va_start(args, fmt);
511 packet_write_fmt_1(writer->dest_fd, 0, "ERR ", fmt, args);
512 va_end(args);
513}
514
515void packet_writer_delim(struct packet_writer *writer)
516{
517 packet_delim(writer->dest_fd);
518}
519
520void packet_writer_flush(struct packet_writer *writer)
521{
522 packet_flush(writer->dest_fd);
523}