Git 2.9.3
[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
f5615d24
SP
94void packet_buf_flush(struct strbuf *buf)
95{
bbc30f99 96 packet_trace("0000", 4, 1);
f5615d24
SP
97 strbuf_add(buf, "0000", 4);
98}
99
f3a3214e 100#define hex(a) (hexchar[(a) & 15])
8e9faf27 101static void format_packet(struct strbuf *out, const char *fmt, va_list args)
f3a3214e 102{
f3a3214e 103 static char hexchar[] = "0123456789abcdef";
8e9faf27 104 size_t orig_len, n;
f3a3214e 105
8e9faf27
JK
106 orig_len = out->len;
107 strbuf_addstr(out, "0000");
108 strbuf_vaddf(out, fmt, args);
109 n = out->len - orig_len;
110
111 if (n > LARGE_PACKET_MAX)
f3a3214e 112 die("protocol error: impossibly long line");
8e9faf27
JK
113
114 out->buf[orig_len + 0] = hex(n >> 12);
115 out->buf[orig_len + 1] = hex(n >> 8);
116 out->buf[orig_len + 2] = hex(n >> 4);
117 out->buf[orig_len + 3] = hex(n);
118 packet_trace(out->buf + orig_len + 4, n - 4, 1);
f5615d24
SP
119}
120
121void packet_write(int fd, const char *fmt, ...)
122{
8e9faf27 123 static struct strbuf buf = STRBUF_INIT;
f5615d24 124 va_list args;
f5615d24 125
8e9faf27 126 strbuf_reset(&buf);
f5615d24 127 va_start(args, fmt);
8e9faf27 128 format_packet(&buf, fmt, args);
f5615d24 129 va_end(args);
8e9faf27 130 write_or_die(fd, buf.buf, buf.len);
f3a3214e
LT
131}
132
f5615d24
SP
133void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
134{
135 va_list args;
f5615d24
SP
136
137 va_start(args, fmt);
8e9faf27 138 format_packet(buf, fmt, args);
f5615d24 139 va_end(args);
f5615d24
SP
140}
141
4981fe75
JK
142static int get_packet_data(int fd, char **src_buf, size_t *src_size,
143 void *dst, unsigned size, int options)
f3a3214e 144{
4981fe75
JK
145 ssize_t ret;
146
147 if (fd >= 0 && src_buf && *src_buf)
148 die("BUG: multiple sources given to packet_read");
149
150 /* Read up to "size" bytes from our source, whatever it is. */
151 if (src_buf && *src_buf) {
152 ret = size < *src_size ? size : *src_size;
153 memcpy(dst, *src_buf, ret);
154 *src_buf += ret;
155 *src_size -= ret;
156 } else {
157 ret = read_in_full(fd, dst, size);
158 if (ret < 0)
159 die_errno("read error");
160 }
161
162 /* And complain if we didn't get enough bytes to satisfy the read. */
163 if (ret < size) {
03809429 164 if (options & PACKET_READ_GENTLE_ON_EOF)
46284dd1
HV
165 return -1;
166
c697ad14 167 die("The remote end hung up unexpectedly");
46284dd1
HV
168 }
169
170 return ret;
f3a3214e
LT
171}
172
f5615d24 173static int packet_length(const char *linelen)
f3a3214e
LT
174{
175 int n;
f5615d24 176 int len = 0;
f3a3214e 177
f3a3214e
LT
178 for (n = 0; n < 4; n++) {
179 unsigned char c = linelen[n];
180 len <<= 4;
181 if (c >= '0' && c <= '9') {
182 len += c - '0';
183 continue;
184 }
185 if (c >= 'a' && c <= 'f') {
186 len += c - 'a' + 10;
187 continue;
188 }
189 if (c >= 'A' && c <= 'F') {
190 len += c - 'A' + 10;
191 continue;
192 }
f5615d24 193 return -1;
f3a3214e 194 }
f5615d24
SP
195 return len;
196}
197
4981fe75
JK
198int packet_read(int fd, char **src_buf, size_t *src_len,
199 char *buffer, unsigned size, int options)
f5615d24 200{
46284dd1 201 int len, ret;
f5615d24
SP
202 char linelen[4];
203
4981fe75 204 ret = get_packet_data(fd, src_buf, src_len, linelen, 4, options);
03809429 205 if (ret < 0)
46284dd1 206 return ret;
f5615d24
SP
207 len = packet_length(linelen);
208 if (len < 0)
743c4b7b 209 die("protocol error: bad line length character: %.4s", linelen);
bbc30f99
JK
210 if (!len) {
211 packet_trace("0000", 4, 0);
f3a3214e 212 return 0;
bbc30f99 213 }
f3a3214e
LT
214 len -= 4;
215 if (len >= size)
216 die("protocol error: bad line length %d", len);
4981fe75 217 ret = get_packet_data(fd, src_buf, src_len, buffer, len, options);
03809429 218 if (ret < 0)
46284dd1 219 return ret;
819b929d
JK
220
221 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
222 len && buffer[len-1] == '\n')
223 len--;
224
f3a3214e 225 buffer[len] = 0;
bbc30f99 226 packet_trace(buffer, len, 0);
f3a3214e
LT
227 return len;
228}
f5615d24 229
4981fe75
JK
230static char *packet_read_line_generic(int fd,
231 char **src, size_t *src_len,
232 int *dst_len)
46284dd1 233{
4981fe75
JK
234 int len = packet_read(fd, src, src_len,
235 packet_buffer, sizeof(packet_buffer),
74543a04 236 PACKET_READ_CHOMP_NEWLINE);
4981fe75
JK
237 if (dst_len)
238 *dst_len = len;
74543a04 239 return len ? packet_buffer : NULL;
46284dd1
HV
240}
241
4981fe75 242char *packet_read_line(int fd, int *len_p)
f5615d24 243{
4981fe75
JK
244 return packet_read_line_generic(fd, NULL, NULL, len_p);
245}
f5615d24 246
4981fe75
JK
247char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
248{
249 return packet_read_line_generic(-1, src, src_len, dst_len);
f5615d24 250}