1#include "cache.h"
2#include "pkt-line.h"
3#include "run-command.h"
4
5char packet_buffer[LARGE_PACKET_MAX];
6static const char *packet_trace_prefix = "git";
7static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET);
8static struct trace_key trace_pack = TRACE_KEY_INIT(PACKFILE);
9
10void packet_trace_identity(const char *prog)
11{
12 packet_trace_prefix = xstrdup(prog);
13}
14
15static const char *get_trace_prefix(void)
16{
17 return in_async() ? "sideband" : packet_trace_prefix;
18}
19
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
34static 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 */
88void packet_flush(int fd)
89{
90 packet_trace("0000", 4, 1);
91 write_or_die(fd, "0000", 4);
92}
93
94void packet_buf_flush(struct strbuf *buf)
95{
96 packet_trace("0000", 4, 1);
97 strbuf_add(buf, "0000", 4);
98}
99
100static void set_packet_header(char *buf, const int size)
101{
102 static char hexchar[] = "0123456789abcdef";
103
104 #define hex(a) (hexchar[(a) & 15])
105 buf[0] = hex(size >> 12);
106 buf[1] = hex(size >> 8);
107 buf[2] = hex(size >> 4);
108 buf[3] = hex(size);
109 #undef hex
110}
111
112static void format_packet(struct strbuf *out, const char *fmt, va_list args)
113{
114 size_t orig_len, n;
115
116 orig_len = out->len;
117 strbuf_addstr(out, "0000");
118 strbuf_vaddf(out, fmt, args);
119 n = out->len - orig_len;
120
121 if (n > LARGE_PACKET_MAX)
122 die("protocol error: impossibly long line");
123
124 set_packet_header(&out->buf[orig_len], n);
125 packet_trace(out->buf + orig_len + 4, n - 4, 1);
126}
127
128static int packet_write_fmt_1(int fd, int gently,
129 const char *fmt, va_list args)
130{
131 struct strbuf buf = STRBUF_INIT;
132 ssize_t count;
133
134 format_packet(&buf, fmt, args);
135 count = write_in_full(fd, buf.buf, buf.len);
136 if (count == buf.len)
137 return 0;
138
139 if (!gently) {
140 check_pipe(errno);
141 die_errno("packet write with format failed");
142 }
143 return error("packet write with format failed");
144}
145
146void packet_write_fmt(int fd, const char *fmt, ...)
147{
148 va_list args;
149
150 va_start(args, fmt);
151 packet_write_fmt_1(fd, 0, fmt, args);
152 va_end(args);
153}
154
155int packet_write_fmt_gently(int fd, const char *fmt, ...)
156{
157 int status;
158 va_list args;
159
160 va_start(args, fmt);
161 status = packet_write_fmt_1(fd, 1, fmt, args);
162 va_end(args);
163 return status;
164}
165
166void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
167{
168 va_list args;
169
170 va_start(args, fmt);
171 format_packet(buf, fmt, args);
172 va_end(args);
173}
174
175static int get_packet_data(int fd, char **src_buf, size_t *src_size,
176 void *dst, unsigned size, int options)
177{
178 ssize_t ret;
179
180 if (fd >= 0 && src_buf && *src_buf)
181 die("BUG: multiple sources given to packet_read");
182
183 /* Read up to "size" bytes from our source, whatever it is. */
184 if (src_buf && *src_buf) {
185 ret = size < *src_size ? size : *src_size;
186 memcpy(dst, *src_buf, ret);
187 *src_buf += ret;
188 *src_size -= ret;
189 } else {
190 ret = read_in_full(fd, dst, size);
191 if (ret < 0)
192 die_errno("read error");
193 }
194
195 /* And complain if we didn't get enough bytes to satisfy the read. */
196 if (ret < size) {
197 if (options & PACKET_READ_GENTLE_ON_EOF)
198 return -1;
199
200 die("The remote end hung up unexpectedly");
201 }
202
203 return ret;
204}
205
206static int packet_length(const char *linelen)
207{
208 int val = hex2chr(linelen);
209 return (val < 0) ? val : (val << 8) | hex2chr(linelen + 2);
210}
211
212int packet_read(int fd, char **src_buf, size_t *src_len,
213 char *buffer, unsigned size, int options)
214{
215 int len, ret;
216 char linelen[4];
217
218 ret = get_packet_data(fd, src_buf, src_len, linelen, 4, options);
219 if (ret < 0)
220 return ret;
221 len = packet_length(linelen);
222 if (len < 0)
223 die("protocol error: bad line length character: %.4s", linelen);
224 if (!len) {
225 packet_trace("0000", 4, 0);
226 return 0;
227 }
228 len -= 4;
229 if (len >= size)
230 die("protocol error: bad line length %d", len);
231 ret = get_packet_data(fd, src_buf, src_len, buffer, len, options);
232 if (ret < 0)
233 return ret;
234
235 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
236 len && buffer[len-1] == '\n')
237 len--;
238
239 buffer[len] = 0;
240 packet_trace(buffer, len, 0);
241 return len;
242}
243
244static char *packet_read_line_generic(int fd,
245 char **src, size_t *src_len,
246 int *dst_len)
247{
248 int len = packet_read(fd, src, src_len,
249 packet_buffer, sizeof(packet_buffer),
250 PACKET_READ_CHOMP_NEWLINE);
251 if (dst_len)
252 *dst_len = len;
253 return len ? packet_buffer : NULL;
254}
255
256char *packet_read_line(int fd, int *len_p)
257{
258 return packet_read_line_generic(fd, NULL, NULL, len_p);
259}
260
261char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
262{
263 return packet_read_line_generic(-1, src, src_len, dst_len);
264}