Merge branch 'au/complete-restore-s'
[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 stateless separator 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 packet_write_gently(const int fd_out, const char *buf, size_t size)
198 {
199         static char packet_write_buffer[LARGE_PACKET_MAX];
200         size_t packet_size;
201
202         if (size > sizeof(packet_write_buffer) - 4)
203                 return error(_("packet write failed - data exceeds max packet size"));
204
205         packet_trace(buf, size, 1);
206         packet_size = size + 4;
207         set_packet_header(packet_write_buffer, packet_size);
208         memcpy(packet_write_buffer + 4, buf, size);
209         if (write_in_full(fd_out, packet_write_buffer, packet_size) < 0)
210                 return error(_("packet write failed"));
211         return 0;
212 }
213
214 void packet_write(int fd_out, const char *buf, size_t size)
215 {
216         if (packet_write_gently(fd_out, buf, size))
217                 die_errno(_("packet write failed"));
218 }
219
220 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
221 {
222         va_list args;
223
224         va_start(args, fmt);
225         format_packet(buf, "", fmt, args);
226         va_end(args);
227 }
228
229 void packet_buf_write_len(struct strbuf *buf, const char *data, size_t len)
230 {
231         size_t orig_len, n;
232
233         orig_len = buf->len;
234         strbuf_addstr(buf, "0000");
235         strbuf_add(buf, data, len);
236         n = buf->len - orig_len;
237
238         if (n > LARGE_PACKET_MAX)
239                 die(_("protocol error: impossibly long line"));
240
241         set_packet_header(&buf->buf[orig_len], n);
242         packet_trace(data, len, 1);
243 }
244
245 int write_packetized_from_fd(int fd_in, int fd_out)
246 {
247         static char buf[LARGE_PACKET_DATA_MAX];
248         int err = 0;
249         ssize_t bytes_to_write;
250
251         while (!err) {
252                 bytes_to_write = xread(fd_in, buf, sizeof(buf));
253                 if (bytes_to_write < 0)
254                         return COPY_READ_ERROR;
255                 if (bytes_to_write == 0)
256                         break;
257                 err = packet_write_gently(fd_out, buf, bytes_to_write);
258         }
259         if (!err)
260                 err = packet_flush_gently(fd_out);
261         return err;
262 }
263
264 int write_packetized_from_buf(const char *src_in, size_t len, int fd_out)
265 {
266         int err = 0;
267         size_t bytes_written = 0;
268         size_t bytes_to_write;
269
270         while (!err) {
271                 if ((len - bytes_written) > LARGE_PACKET_DATA_MAX)
272                         bytes_to_write = LARGE_PACKET_DATA_MAX;
273                 else
274                         bytes_to_write = len - bytes_written;
275                 if (bytes_to_write == 0)
276                         break;
277                 err = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);
278                 bytes_written += bytes_to_write;
279         }
280         if (!err)
281                 err = packet_flush_gently(fd_out);
282         return err;
283 }
284
285 static int get_packet_data(int fd, char **src_buf, size_t *src_size,
286                            void *dst, unsigned size, int options)
287 {
288         ssize_t ret;
289
290         if (fd >= 0 && src_buf && *src_buf)
291                 BUG("multiple sources given to packet_read");
292
293         /* Read up to "size" bytes from our source, whatever it is. */
294         if (src_buf && *src_buf) {
295                 ret = size < *src_size ? size : *src_size;
296                 memcpy(dst, *src_buf, ret);
297                 *src_buf += ret;
298                 *src_size -= ret;
299         } else {
300                 ret = read_in_full(fd, dst, size);
301                 if (ret < 0)
302                         die_errno(_("read error"));
303         }
304
305         /* And complain if we didn't get enough bytes to satisfy the read. */
306         if (ret != size) {
307                 if (options & PACKET_READ_GENTLE_ON_EOF)
308                         return -1;
309
310                 die(_("the remote end hung up unexpectedly"));
311         }
312
313         return ret;
314 }
315
316 int packet_length(const char lenbuf_hex[4])
317 {
318         int val = hex2chr(lenbuf_hex);
319         return (val < 0) ? val : (val << 8) | hex2chr(lenbuf_hex + 2);
320 }
321
322 enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
323                                                 size_t *src_len, char *buffer,
324                                                 unsigned size, int *pktlen,
325                                                 int options)
326 {
327         int len;
328         char linelen[4];
329
330         if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) {
331                 *pktlen = -1;
332                 return PACKET_READ_EOF;
333         }
334
335         len = packet_length(linelen);
336
337         if (len < 0) {
338                 die(_("protocol error: bad line length character: %.4s"), linelen);
339         } else if (!len) {
340                 packet_trace("0000", 4, 0);
341                 *pktlen = 0;
342                 return PACKET_READ_FLUSH;
343         } else if (len == 1) {
344                 packet_trace("0001", 4, 0);
345                 *pktlen = 0;
346                 return PACKET_READ_DELIM;
347         } else if (len == 2) {
348                 packet_trace("0002", 4, 0);
349                 *pktlen = 0;
350                 return PACKET_READ_RESPONSE_END;
351         } else if (len < 4) {
352                 die(_("protocol error: bad line length %d"), len);
353         }
354
355         len -= 4;
356         if ((unsigned)len >= size)
357                 die(_("protocol error: bad line length %d"), len);
358
359         if (get_packet_data(fd, src_buffer, src_len, buffer, len, options) < 0) {
360                 *pktlen = -1;
361                 return PACKET_READ_EOF;
362         }
363
364         if ((options & PACKET_READ_CHOMP_NEWLINE) &&
365             len && buffer[len-1] == '\n')
366                 len--;
367
368         buffer[len] = 0;
369         packet_trace(buffer, len, 0);
370
371         if ((options & PACKET_READ_DIE_ON_ERR_PACKET) &&
372             starts_with(buffer, "ERR "))
373                 die(_("remote error: %s"), buffer + 4);
374
375         *pktlen = len;
376         return PACKET_READ_NORMAL;
377 }
378
379 int packet_read(int fd, char **src_buffer, size_t *src_len,
380                 char *buffer, unsigned size, int options)
381 {
382         int pktlen = -1;
383
384         packet_read_with_status(fd, src_buffer, src_len, buffer, size,
385                                 &pktlen, options);
386
387         return pktlen;
388 }
389
390 static char *packet_read_line_generic(int fd,
391                                       char **src, size_t *src_len,
392                                       int *dst_len)
393 {
394         int len = packet_read(fd, src, src_len,
395                               packet_buffer, sizeof(packet_buffer),
396                               PACKET_READ_CHOMP_NEWLINE);
397         if (dst_len)
398                 *dst_len = len;
399         return (len > 0) ? packet_buffer : NULL;
400 }
401
402 char *packet_read_line(int fd, int *len_p)
403 {
404         return packet_read_line_generic(fd, NULL, NULL, len_p);
405 }
406
407 int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
408 {
409         int len = packet_read(fd, NULL, NULL,
410                               packet_buffer, sizeof(packet_buffer),
411                               PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
412         if (dst_len)
413                 *dst_len = len;
414         if (dst_line)
415                 *dst_line = (len > 0) ? packet_buffer : NULL;
416         return len;
417 }
418
419 char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
420 {
421         return packet_read_line_generic(-1, src, src_len, dst_len);
422 }
423
424 ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out)
425 {
426         int packet_len;
427
428         size_t orig_len = sb_out->len;
429         size_t orig_alloc = sb_out->alloc;
430
431         for (;;) {
432                 strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);
433                 packet_len = packet_read(fd_in, NULL, NULL,
434                         /* strbuf_grow() above always allocates one extra byte to
435                          * store a '\0' at the end of the string. packet_read()
436                          * writes a '\0' extra byte at the end, too. Let it know
437                          * that there is already room for the extra byte.
438                          */
439                         sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,
440                         PACKET_READ_GENTLE_ON_EOF);
441                 if (packet_len <= 0)
442                         break;
443                 sb_out->len += packet_len;
444         }
445
446         if (packet_len < 0) {
447                 if (orig_alloc == 0)
448                         strbuf_release(sb_out);
449                 else
450                         strbuf_setlen(sb_out, orig_len);
451                 return packet_len;
452         }
453         return sb_out->len - orig_len;
454 }
455
456 int recv_sideband(const char *me, int in_stream, int out)
457 {
458         char buf[LARGE_PACKET_MAX + 1];
459         int len;
460         struct strbuf scratch = STRBUF_INIT;
461         enum sideband_type sideband_type;
462
463         while (1) {
464                 len = packet_read(in_stream, NULL, NULL, buf, LARGE_PACKET_MAX,
465                                   0);
466                 if (!demultiplex_sideband(me, buf, len, 0, &scratch,
467                                           &sideband_type))
468                         continue;
469                 switch (sideband_type) {
470                 case SIDEBAND_PRIMARY:
471                         write_or_die(out, buf + 1, len - 1);
472                         break;
473                 default: /* errors: message already written */
474                         return sideband_type;
475                 }
476         }
477 }
478
479 /* Packet Reader Functions */
480 void packet_reader_init(struct packet_reader *reader, int fd,
481                         char *src_buffer, size_t src_len,
482                         int options)
483 {
484         memset(reader, 0, sizeof(*reader));
485
486         reader->fd = fd;
487         reader->src_buffer = src_buffer;
488         reader->src_len = src_len;
489         reader->buffer = packet_buffer;
490         reader->buffer_size = sizeof(packet_buffer);
491         reader->options = options;
492         reader->me = "git";
493         reader->hash_algo = &hash_algos[GIT_HASH_SHA1];
494 }
495
496 enum packet_read_status packet_reader_read(struct packet_reader *reader)
497 {
498         struct strbuf scratch = STRBUF_INIT;
499
500         if (reader->line_peeked) {
501                 reader->line_peeked = 0;
502                 return reader->status;
503         }
504
505         /*
506          * Consume all progress packets until a primary payload packet is
507          * received
508          */
509         while (1) {
510                 enum sideband_type sideband_type;
511                 reader->status = packet_read_with_status(reader->fd,
512                                                          &reader->src_buffer,
513                                                          &reader->src_len,
514                                                          reader->buffer,
515                                                          reader->buffer_size,
516                                                          &reader->pktlen,
517                                                          reader->options);
518                 if (!reader->use_sideband)
519                         break;
520                 if (demultiplex_sideband(reader->me, reader->buffer,
521                                          reader->pktlen, 1, &scratch,
522                                          &sideband_type))
523                         break;
524         }
525
526         if (reader->status == PACKET_READ_NORMAL)
527                 /* Skip the sideband designator if sideband is used */
528                 reader->line = reader->use_sideband ?
529                         reader->buffer + 1 : reader->buffer;
530         else
531                 reader->line = NULL;
532
533         return reader->status;
534 }
535
536 enum packet_read_status packet_reader_peek(struct packet_reader *reader)
537 {
538         /* Only allow peeking a single line */
539         if (reader->line_peeked)
540                 return reader->status;
541
542         /* Peek a line by reading it and setting peeked flag */
543         packet_reader_read(reader);
544         reader->line_peeked = 1;
545         return reader->status;
546 }
547
548 void packet_writer_init(struct packet_writer *writer, int dest_fd)
549 {
550         writer->dest_fd = dest_fd;
551         writer->use_sideband = 0;
552 }
553
554 void packet_writer_write(struct packet_writer *writer, const char *fmt, ...)
555 {
556         va_list args;
557
558         va_start(args, fmt);
559         packet_write_fmt_1(writer->dest_fd, 0,
560                            writer->use_sideband ? "\001" : "", fmt, args);
561         va_end(args);
562 }
563
564 void packet_writer_error(struct packet_writer *writer, const char *fmt, ...)
565 {
566         va_list args;
567
568         va_start(args, fmt);
569         packet_write_fmt_1(writer->dest_fd, 0,
570                            writer->use_sideband ? "\003" : "ERR ", fmt, args);
571         va_end(args);
572 }
573
574 void packet_writer_delim(struct packet_writer *writer)
575 {
576         packet_delim(writer->dest_fd);
577 }
578
579 void packet_writer_flush(struct packet_writer *writer)
580 {
581         packet_flush(writer->dest_fd);
582 }