Sync with 2.17.6
[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         write_or_die(fd, "0000", 4);
92 }
93
94 void packet_delim(int fd)
95 {
96         packet_trace("0001", 4, 1);
97         write_or_die(fd, "0001", 4);
98 }
99
100 int packet_flush_gently(int fd)
101 {
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;
106 }
107
108 void packet_buf_flush(struct strbuf *buf)
109 {
110         packet_trace("0000", 4, 1);
111         strbuf_add(buf, "0000", 4);
112 }
113
114 void packet_buf_delim(struct strbuf *buf)
115 {
116         packet_trace("0001", 4, 1);
117         strbuf_add(buf, "0001", 4);
118 }
119
120 static void set_packet_header(char *buf, const int size)
121 {
122         static char hexchar[] = "0123456789abcdef";
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
132 static void format_packet(struct strbuf *out, const char *fmt, va_list args)
133 {
134         size_t orig_len, n;
135
136         orig_len = out->len;
137         strbuf_addstr(out, "0000");
138         strbuf_vaddf(out, fmt, args);
139         n = out->len - orig_len;
140
141         if (n > LARGE_PACKET_MAX)
142                 die("protocol error: impossibly long line");
143
144         set_packet_header(&out->buf[orig_len], n);
145         packet_trace(out->buf + orig_len + 4, n - 4, 1);
146 }
147
148 static int packet_write_fmt_1(int fd, int gently,
149                               const char *fmt, va_list args)
150 {
151         static struct strbuf buf = STRBUF_INIT;
152
153         strbuf_reset(&buf);
154         format_packet(&buf, fmt, args);
155         if (write_in_full(fd, buf.buf, buf.len) < 0) {
156                 if (!gently) {
157                         check_pipe(errno);
158                         die_errno("packet write with format failed");
159                 }
160                 return error("packet write with format failed");
161         }
162
163         return 0;
164 }
165
166 void packet_write_fmt(int fd, const char *fmt, ...)
167 {
168         va_list args;
169
170         va_start(args, fmt);
171         packet_write_fmt_1(fd, 0, fmt, args);
172         va_end(args);
173 }
174
175 int packet_write_fmt_gently(int fd, const char *fmt, ...)
176 {
177         int status;
178         va_list args;
179
180         va_start(args, fmt);
181         status = packet_write_fmt_1(fd, 1, fmt, args);
182         va_end(args);
183         return status;
184 }
185
186 static int packet_write_gently(const int fd_out, const char *buf, size_t size)
187 {
188         static char packet_write_buffer[LARGE_PACKET_MAX];
189         size_t packet_size;
190
191         if (size > sizeof(packet_write_buffer) - 4)
192                 return error("packet write failed - data exceeds max packet size");
193
194         packet_trace(buf, size, 1);
195         packet_size = size + 4;
196         set_packet_header(packet_write_buffer, packet_size);
197         memcpy(packet_write_buffer + 4, buf, size);
198         if (write_in_full(fd_out, packet_write_buffer, packet_size) < 0)
199                 return error("packet write failed");
200         return 0;
201 }
202
203 void packet_write(int fd_out, const char *buf, size_t size)
204 {
205         if (packet_write_gently(fd_out, buf, size))
206                 die_errno("packet write failed");
207 }
208
209 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
210 {
211         va_list args;
212
213         va_start(args, fmt);
214         format_packet(buf, fmt, args);
215         va_end(args);
216 }
217
218 void packet_buf_write_len(struct strbuf *buf, const char *data, size_t len)
219 {
220         size_t orig_len, n;
221
222         orig_len = buf->len;
223         strbuf_addstr(buf, "0000");
224         strbuf_add(buf, data, len);
225         n = buf->len - orig_len;
226
227         if (n > LARGE_PACKET_MAX)
228                 die("protocol error: impossibly long line");
229
230         set_packet_header(&buf->buf[orig_len], n);
231         packet_trace(data, len, 1);
232 }
233
234 int write_packetized_from_fd(int fd_in, int fd_out)
235 {
236         static char buf[LARGE_PACKET_DATA_MAX];
237         int err = 0;
238         ssize_t bytes_to_write;
239
240         while (!err) {
241                 bytes_to_write = xread(fd_in, buf, sizeof(buf));
242                 if (bytes_to_write < 0)
243                         return COPY_READ_ERROR;
244                 if (bytes_to_write == 0)
245                         break;
246                 err = packet_write_gently(fd_out, buf, bytes_to_write);
247         }
248         if (!err)
249                 err = packet_flush_gently(fd_out);
250         return err;
251 }
252
253 int write_packetized_from_buf(const char *src_in, size_t len, int fd_out)
254 {
255         int err = 0;
256         size_t bytes_written = 0;
257         size_t bytes_to_write;
258
259         while (!err) {
260                 if ((len - bytes_written) > LARGE_PACKET_DATA_MAX)
261                         bytes_to_write = LARGE_PACKET_DATA_MAX;
262                 else
263                         bytes_to_write = len - bytes_written;
264                 if (bytes_to_write == 0)
265                         break;
266                 err = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);
267                 bytes_written += bytes_to_write;
268         }
269         if (!err)
270                 err = packet_flush_gently(fd_out);
271         return err;
272 }
273
274 static int get_packet_data(int fd, char **src_buf, size_t *src_size,
275                            void *dst, unsigned size, int options)
276 {
277         ssize_t ret;
278
279         if (fd >= 0 && src_buf && *src_buf)
280                 BUG("multiple sources given to packet_read");
281
282         /* Read up to "size" bytes from our source, whatever it is. */
283         if (src_buf && *src_buf) {
284                 ret = size < *src_size ? size : *src_size;
285                 memcpy(dst, *src_buf, ret);
286                 *src_buf += ret;
287                 *src_size -= ret;
288         } else {
289                 ret = read_in_full(fd, dst, size);
290                 if (ret < 0)
291                         die_errno("read error");
292         }
293
294         /* And complain if we didn't get enough bytes to satisfy the read. */
295         if (ret != size) {
296                 if (options & PACKET_READ_GENTLE_ON_EOF)
297                         return -1;
298
299                 die("The remote end hung up unexpectedly");
300         }
301
302         return ret;
303 }
304
305 static int packet_length(const char *linelen)
306 {
307         int val = hex2chr(linelen);
308         return (val < 0) ? val : (val << 8) | hex2chr(linelen + 2);
309 }
310
311 enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
312                                                 size_t *src_len, char *buffer,
313                                                 unsigned size, int *pktlen,
314                                                 int options)
315 {
316         int len;
317         char linelen[4];
318
319         if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) {
320                 *pktlen = -1;
321                 return PACKET_READ_EOF;
322         }
323
324         len = packet_length(linelen);
325
326         if (len < 0) {
327                 die("protocol error: bad line length character: %.4s", linelen);
328         } else if (!len) {
329                 packet_trace("0000", 4, 0);
330                 *pktlen = 0;
331                 return PACKET_READ_FLUSH;
332         } else if (len == 1) {
333                 packet_trace("0001", 4, 0);
334                 *pktlen = 0;
335                 return PACKET_READ_DELIM;
336         } else if (len < 4) {
337                 die("protocol error: bad line length %d", len);
338         }
339
340         len -= 4;
341         if ((unsigned)len >= size)
342                 die("protocol error: bad line length %d", len);
343
344         if (get_packet_data(fd, src_buffer, src_len, buffer, len, options) < 0) {
345                 *pktlen = -1;
346                 return PACKET_READ_EOF;
347         }
348
349         if ((options & PACKET_READ_CHOMP_NEWLINE) &&
350             len && buffer[len-1] == '\n')
351                 len--;
352
353         buffer[len] = 0;
354         packet_trace(buffer, len, 0);
355         *pktlen = len;
356         return PACKET_READ_NORMAL;
357 }
358
359 int packet_read(int fd, char **src_buffer, size_t *src_len,
360                 char *buffer, unsigned size, int options)
361 {
362         int pktlen = -1;
363
364         packet_read_with_status(fd, src_buffer, src_len, buffer, size,
365                                 &pktlen, options);
366
367         return pktlen;
368 }
369
370 static char *packet_read_line_generic(int fd,
371                                       char **src, size_t *src_len,
372                                       int *dst_len)
373 {
374         int len = packet_read(fd, src, src_len,
375                               packet_buffer, sizeof(packet_buffer),
376                               PACKET_READ_CHOMP_NEWLINE);
377         if (dst_len)
378                 *dst_len = len;
379         return (len > 0) ? packet_buffer : NULL;
380 }
381
382 char *packet_read_line(int fd, int *len_p)
383 {
384         return packet_read_line_generic(fd, NULL, NULL, len_p);
385 }
386
387 int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
388 {
389         int len = packet_read(fd, NULL, NULL,
390                               packet_buffer, sizeof(packet_buffer),
391                               PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
392         if (dst_len)
393                 *dst_len = len;
394         if (dst_line)
395                 *dst_line = (len > 0) ? packet_buffer : NULL;
396         return len;
397 }
398
399 char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
400 {
401         return packet_read_line_generic(-1, src, src_len, dst_len);
402 }
403
404 ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out)
405 {
406         int packet_len;
407
408         size_t orig_len = sb_out->len;
409         size_t orig_alloc = sb_out->alloc;
410
411         for (;;) {
412                 strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);
413                 packet_len = packet_read(fd_in, NULL, NULL,
414                         /* strbuf_grow() above always allocates one extra byte to
415                          * store a '\0' at the end of the string. packet_read()
416                          * writes a '\0' extra byte at the end, too. Let it know
417                          * that there is already room for the extra byte.
418                          */
419                         sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,
420                         PACKET_READ_GENTLE_ON_EOF);
421                 if (packet_len <= 0)
422                         break;
423                 sb_out->len += packet_len;
424         }
425
426         if (packet_len < 0) {
427                 if (orig_alloc == 0)
428                         strbuf_release(sb_out);
429                 else
430                         strbuf_setlen(sb_out, orig_len);
431                 return packet_len;
432         }
433         return sb_out->len - orig_len;
434 }
435
436 /* Packet Reader Functions */
437 void packet_reader_init(struct packet_reader *reader, int fd,
438                         char *src_buffer, size_t src_len,
439                         int options)
440 {
441         memset(reader, 0, sizeof(*reader));
442
443         reader->fd = fd;
444         reader->src_buffer = src_buffer;
445         reader->src_len = src_len;
446         reader->buffer = packet_buffer;
447         reader->buffer_size = sizeof(packet_buffer);
448         reader->options = options;
449 }
450
451 enum packet_read_status packet_reader_read(struct packet_reader *reader)
452 {
453         if (reader->line_peeked) {
454                 reader->line_peeked = 0;
455                 return reader->status;
456         }
457
458         reader->status = packet_read_with_status(reader->fd,
459                                                  &reader->src_buffer,
460                                                  &reader->src_len,
461                                                  reader->buffer,
462                                                  reader->buffer_size,
463                                                  &reader->pktlen,
464                                                  reader->options);
465
466         if (reader->status == PACKET_READ_NORMAL)
467                 reader->line = reader->buffer;
468         else
469                 reader->line = NULL;
470
471         return reader->status;
472 }
473
474 enum packet_read_status packet_reader_peek(struct packet_reader *reader)
475 {
476         /* Only allow peeking a single line */
477         if (reader->line_peeked)
478                 return reader->status;
479
480         /* Peek a line by reading it and setting peeked flag */
481         packet_reader_read(reader);
482         reader->line_peeked = 1;
483         return reader->status;
484 }