Merge branch 'jc/fsck-dropped-errors' into maint
[git/debian.git] / pkt-line.c
blob08a1427c0d4f6743182b235ec9d9c710c3c99b5b
1 #include "cache.h"
2 #include "pkt-line.h"
4 char packet_buffer[LARGE_PACKET_MAX];
5 static const char *packet_trace_prefix = "git";
6 static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET);
7 static struct trace_key trace_pack = TRACE_KEY_INIT(PACKFILE);
9 void packet_trace_identity(const char *prog)
11 packet_trace_prefix = xstrdup(prog);
14 static int packet_trace_pack(const char *buf, unsigned int len, int sideband)
16 if (!sideband) {
17 trace_verbatim(&trace_pack, buf, len);
18 return 1;
19 } else if (len && *buf == '\1') {
20 trace_verbatim(&trace_pack, buf + 1, len - 1);
21 return 1;
22 } else {
23 /* it's another non-pack sideband */
24 return 0;
28 static void packet_trace(const char *buf, unsigned int len, int write)
30 int i;
31 struct strbuf out;
32 static int in_pack, sideband;
34 if (!trace_want(&trace_packet) && !trace_want(&trace_pack))
35 return;
37 if (in_pack) {
38 if (packet_trace_pack(buf, len, sideband))
39 return;
40 } else if (starts_with(buf, "PACK") || starts_with(buf, "\1PACK")) {
41 in_pack = 1;
42 sideband = *buf == '\1';
43 packet_trace_pack(buf, len, sideband);
46 * Make a note in the human-readable trace that the pack data
47 * started.
49 buf = "PACK ...";
50 len = strlen(buf);
53 if (!trace_want(&trace_packet))
54 return;
56 /* +32 is just a guess for header + quoting */
57 strbuf_init(&out, len+32);
59 strbuf_addf(&out, "packet: %12s%c ",
60 packet_trace_prefix, write ? '>' : '<');
62 /* XXX we should really handle printable utf8 */
63 for (i = 0; i < len; i++) {
64 /* suppress newlines */
65 if (buf[i] == '\n')
66 continue;
67 if (buf[i] >= 0x20 && buf[i] <= 0x7e)
68 strbuf_addch(&out, buf[i]);
69 else
70 strbuf_addf(&out, "\\%o", buf[i]);
73 strbuf_addch(&out, '\n');
74 trace_strbuf(&trace_packet, &out);
75 strbuf_release(&out);
79 * If we buffered things up above (we don't, but we should),
80 * we'd flush it here
82 void packet_flush(int fd)
84 packet_trace("0000", 4, 1);
85 write_or_die(fd, "0000", 4);
88 void packet_buf_flush(struct strbuf *buf)
90 packet_trace("0000", 4, 1);
91 strbuf_add(buf, "0000", 4);
94 #define hex(a) (hexchar[(a) & 15])
95 static void format_packet(struct strbuf *out, const char *fmt, va_list args)
97 static char hexchar[] = "0123456789abcdef";
98 size_t orig_len, n;
100 orig_len = out->len;
101 strbuf_addstr(out, "0000");
102 strbuf_vaddf(out, fmt, args);
103 n = out->len - orig_len;
105 if (n > LARGE_PACKET_MAX)
106 die("protocol error: impossibly long line");
108 out->buf[orig_len + 0] = hex(n >> 12);
109 out->buf[orig_len + 1] = hex(n >> 8);
110 out->buf[orig_len + 2] = hex(n >> 4);
111 out->buf[orig_len + 3] = hex(n);
112 packet_trace(out->buf + orig_len + 4, n - 4, 1);
115 void packet_write(int fd, const char *fmt, ...)
117 static struct strbuf buf = STRBUF_INIT;
118 va_list args;
120 strbuf_reset(&buf);
121 va_start(args, fmt);
122 format_packet(&buf, fmt, args);
123 va_end(args);
124 write_or_die(fd, buf.buf, buf.len);
127 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
129 va_list args;
131 va_start(args, fmt);
132 format_packet(buf, fmt, args);
133 va_end(args);
136 static int get_packet_data(int fd, char **src_buf, size_t *src_size,
137 void *dst, unsigned size, int options)
139 ssize_t ret;
141 if (fd >= 0 && src_buf && *src_buf)
142 die("BUG: multiple sources given to packet_read");
144 /* Read up to "size" bytes from our source, whatever it is. */
145 if (src_buf && *src_buf) {
146 ret = size < *src_size ? size : *src_size;
147 memcpy(dst, *src_buf, ret);
148 *src_buf += ret;
149 *src_size -= ret;
150 } else {
151 ret = read_in_full(fd, dst, size);
152 if (ret < 0)
153 die_errno("read error");
156 /* And complain if we didn't get enough bytes to satisfy the read. */
157 if (ret < size) {
158 if (options & PACKET_READ_GENTLE_ON_EOF)
159 return -1;
161 die("The remote end hung up unexpectedly");
164 return ret;
167 static int packet_length(const char *linelen)
169 int n;
170 int len = 0;
172 for (n = 0; n < 4; n++) {
173 unsigned char c = linelen[n];
174 len <<= 4;
175 if (c >= '0' && c <= '9') {
176 len += c - '0';
177 continue;
179 if (c >= 'a' && c <= 'f') {
180 len += c - 'a' + 10;
181 continue;
183 if (c >= 'A' && c <= 'F') {
184 len += c - 'A' + 10;
185 continue;
187 return -1;
189 return len;
192 int packet_read(int fd, char **src_buf, size_t *src_len,
193 char *buffer, unsigned size, int options)
195 int len, ret;
196 char linelen[4];
198 ret = get_packet_data(fd, src_buf, src_len, linelen, 4, options);
199 if (ret < 0)
200 return ret;
201 len = packet_length(linelen);
202 if (len < 0)
203 die("protocol error: bad line length character: %.4s", linelen);
204 if (!len) {
205 packet_trace("0000", 4, 0);
206 return 0;
208 len -= 4;
209 if (len >= size)
210 die("protocol error: bad line length %d", len);
211 ret = get_packet_data(fd, src_buf, src_len, buffer, len, options);
212 if (ret < 0)
213 return ret;
215 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
216 len && buffer[len-1] == '\n')
217 len--;
219 buffer[len] = 0;
220 packet_trace(buffer, len, 0);
221 return len;
224 static char *packet_read_line_generic(int fd,
225 char **src, size_t *src_len,
226 int *dst_len)
228 int len = packet_read(fd, src, src_len,
229 packet_buffer, sizeof(packet_buffer),
230 PACKET_READ_CHOMP_NEWLINE);
231 if (dst_len)
232 *dst_len = len;
233 return len ? packet_buffer : NULL;
236 char *packet_read_line(int fd, int *len_p)
238 return packet_read_line_generic(fd, NULL, NULL, len_p);
241 char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
243 return packet_read_line_generic(-1, src, src_len, dst_len);