pkt-line: re-'static'-ify buffer in packet_write_fmt_1()
[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
038ce90f
LS
94int packet_flush_gently(int fd)
95{
96 packet_trace("0000", 4, 1);
97 if (write_in_full(fd, "0000", 4) == 4)
98 return 0;
99 return error("flush packet write failed");
100}
101
f5615d24
SP
102void packet_buf_flush(struct strbuf *buf)
103{
bbc30f99 104 packet_trace("0000", 4, 1);
f5615d24
SP
105 strbuf_add(buf, "0000", 4);
106}
107
2f60bdd1 108static void set_packet_header(char *buf, const int size)
f3a3214e 109{
f3a3214e 110 static char hexchar[] = "0123456789abcdef";
2f60bdd1
LS
111
112 #define hex(a) (hexchar[(a) & 15])
113 buf[0] = hex(size >> 12);
114 buf[1] = hex(size >> 8);
115 buf[2] = hex(size >> 4);
116 buf[3] = hex(size);
117 #undef hex
118}
119
120static void format_packet(struct strbuf *out, const char *fmt, va_list args)
121{
8e9faf27 122 size_t orig_len, n;
f3a3214e 123
8e9faf27
JK
124 orig_len = out->len;
125 strbuf_addstr(out, "0000");
126 strbuf_vaddf(out, fmt, args);
127 n = out->len - orig_len;
128
129 if (n > LARGE_PACKET_MAX)
f3a3214e 130 die("protocol error: impossibly long line");
8e9faf27 131
2f60bdd1 132 set_packet_header(&out->buf[orig_len], n);
8e9faf27 133 packet_trace(out->buf + orig_len + 4, n - 4, 1);
f5615d24
SP
134}
135
70428d1a
LS
136static int packet_write_fmt_1(int fd, int gently,
137 const char *fmt, va_list args)
138{
150efef1 139 static struct strbuf buf = STRBUF_INIT;
70428d1a
LS
140 ssize_t count;
141
150efef1 142 strbuf_reset(&buf);
70428d1a
LS
143 format_packet(&buf, fmt, args);
144 count = write_in_full(fd, buf.buf, buf.len);
145 if (count == buf.len)
146 return 0;
147
148 if (!gently) {
149 check_pipe(errno);
150 die_errno("packet write with format failed");
151 }
152 return error("packet write with format failed");
153}
154
81c634e9 155void packet_write_fmt(int fd, const char *fmt, ...)
f5615d24
SP
156{
157 va_list args;
f5615d24
SP
158
159 va_start(args, fmt);
70428d1a
LS
160 packet_write_fmt_1(fd, 0, fmt, args);
161 va_end(args);
162}
163
164int packet_write_fmt_gently(int fd, const char *fmt, ...)
165{
166 int status;
167 va_list args;
168
169 va_start(args, fmt);
170 status = packet_write_fmt_1(fd, 1, fmt, args);
f5615d24 171 va_end(args);
70428d1a 172 return status;
f3a3214e
LT
173}
174
edfb780c
LS
175static int packet_write_gently(const int fd_out, const char *buf, size_t size)
176{
177 static char packet_write_buffer[LARGE_PACKET_MAX];
178 size_t packet_size;
179
180 if (size > sizeof(packet_write_buffer) - 4)
181 return error("packet write failed - data exceeds max packet size");
182
183 packet_trace(buf, size, 1);
184 packet_size = size + 4;
185 set_packet_header(packet_write_buffer, packet_size);
186 memcpy(packet_write_buffer + 4, buf, size);
187 if (write_in_full(fd_out, packet_write_buffer, packet_size) == packet_size)
188 return 0;
189 return error("packet write failed");
190}
191
f5615d24
SP
192void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
193{
194 va_list args;
f5615d24
SP
195
196 va_start(args, fmt);
8e9faf27 197 format_packet(buf, fmt, args);
f5615d24 198 va_end(args);
f5615d24
SP
199}
200
bb643d8b
LS
201int write_packetized_from_fd(int fd_in, int fd_out)
202{
203 static char buf[LARGE_PACKET_DATA_MAX];
204 int err = 0;
205 ssize_t bytes_to_write;
206
207 while (!err) {
208 bytes_to_write = xread(fd_in, buf, sizeof(buf));
209 if (bytes_to_write < 0)
210 return COPY_READ_ERROR;
211 if (bytes_to_write == 0)
212 break;
213 err = packet_write_gently(fd_out, buf, bytes_to_write);
214 }
215 if (!err)
216 err = packet_flush_gently(fd_out);
217 return err;
218}
219
220int write_packetized_from_buf(const char *src_in, size_t len, int fd_out)
221{
222 int err = 0;
223 size_t bytes_written = 0;
224 size_t bytes_to_write;
225
226 while (!err) {
227 if ((len - bytes_written) > LARGE_PACKET_DATA_MAX)
228 bytes_to_write = LARGE_PACKET_DATA_MAX;
229 else
230 bytes_to_write = len - bytes_written;
231 if (bytes_to_write == 0)
232 break;
233 err = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);
234 bytes_written += bytes_to_write;
235 }
236 if (!err)
237 err = packet_flush_gently(fd_out);
238 return err;
239}
240
4981fe75
JK
241static int get_packet_data(int fd, char **src_buf, size_t *src_size,
242 void *dst, unsigned size, int options)
f3a3214e 243{
4981fe75
JK
244 ssize_t ret;
245
246 if (fd >= 0 && src_buf && *src_buf)
247 die("BUG: multiple sources given to packet_read");
248
249 /* Read up to "size" bytes from our source, whatever it is. */
250 if (src_buf && *src_buf) {
251 ret = size < *src_size ? size : *src_size;
252 memcpy(dst, *src_buf, ret);
253 *src_buf += ret;
254 *src_size -= ret;
255 } else {
256 ret = read_in_full(fd, dst, size);
257 if (ret < 0)
258 die_errno("read error");
259 }
260
261 /* And complain if we didn't get enough bytes to satisfy the read. */
262 if (ret < size) {
03809429 263 if (options & PACKET_READ_GENTLE_ON_EOF)
46284dd1
HV
264 return -1;
265
c697ad14 266 die("The remote end hung up unexpectedly");
46284dd1
HV
267 }
268
269 return ret;
f3a3214e
LT
270}
271
f5615d24 272static int packet_length(const char *linelen)
f3a3214e 273{
d2330973
RS
274 int val = hex2chr(linelen);
275 return (val < 0) ? val : (val << 8) | hex2chr(linelen + 2);
f5615d24
SP
276}
277
4981fe75
JK
278int packet_read(int fd, char **src_buf, size_t *src_len,
279 char *buffer, unsigned size, int options)
f5615d24 280{
46284dd1 281 int len, ret;
f5615d24
SP
282 char linelen[4];
283
4981fe75 284 ret = get_packet_data(fd, src_buf, src_len, linelen, 4, options);
03809429 285 if (ret < 0)
46284dd1 286 return ret;
f5615d24
SP
287 len = packet_length(linelen);
288 if (len < 0)
743c4b7b 289 die("protocol error: bad line length character: %.4s", linelen);
bbc30f99
JK
290 if (!len) {
291 packet_trace("0000", 4, 0);
f3a3214e 292 return 0;
bbc30f99 293 }
f3a3214e
LT
294 len -= 4;
295 if (len >= size)
296 die("protocol error: bad line length %d", len);
4981fe75 297 ret = get_packet_data(fd, src_buf, src_len, buffer, len, options);
03809429 298 if (ret < 0)
46284dd1 299 return ret;
819b929d
JK
300
301 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
302 len && buffer[len-1] == '\n')
303 len--;
304
f3a3214e 305 buffer[len] = 0;
bbc30f99 306 packet_trace(buffer, len, 0);
f3a3214e
LT
307 return len;
308}
f5615d24 309
4981fe75
JK
310static char *packet_read_line_generic(int fd,
311 char **src, size_t *src_len,
312 int *dst_len)
46284dd1 313{
4981fe75
JK
314 int len = packet_read(fd, src, src_len,
315 packet_buffer, sizeof(packet_buffer),
74543a04 316 PACKET_READ_CHOMP_NEWLINE);
4981fe75
JK
317 if (dst_len)
318 *dst_len = len;
74543a04 319 return len ? packet_buffer : NULL;
46284dd1
HV
320}
321
4981fe75 322char *packet_read_line(int fd, int *len_p)
f5615d24 323{
4981fe75
JK
324 return packet_read_line_generic(fd, NULL, NULL, len_p);
325}
f5615d24 326
4981fe75
JK
327char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
328{
329 return packet_read_line_generic(-1, src, src_len, dst_len);
f5615d24 330}
bb643d8b
LS
331
332ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out)
333{
334 int packet_len;
335
336 size_t orig_len = sb_out->len;
337 size_t orig_alloc = sb_out->alloc;
338
339 for (;;) {
340 strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);
341 packet_len = packet_read(fd_in, NULL, NULL,
342 /* strbuf_grow() above always allocates one extra byte to
343 * store a '\0' at the end of the string. packet_read()
344 * writes a '\0' extra byte at the end, too. Let it know
345 * that there is already room for the extra byte.
346 */
347 sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,
348 PACKET_READ_GENTLE_ON_EOF);
349 if (packet_len <= 0)
350 break;
351 sb_out->len += packet_len;
352 }
353
354 if (packet_len < 0) {
355 if (orig_alloc == 0)
356 strbuf_release(sb_out);
357 else
358 strbuf_setlen(sb_out, orig_len);
359 return packet_len;
360 }
361 return sb_out->len - orig_len;
362}