4 static const char *packet_trace_prefix = "git";
5 static const char trace_key[] = "GIT_TRACE_PACKET";
7 void packet_trace_identity(const char *prog)
9 packet_trace_prefix = xstrdup(prog);
12 static void packet_trace(const char *buf, unsigned int len, int write)
17 if (!trace_want(trace_key))
20 /* +32 is just a guess for header + quoting */
21 strbuf_init(&out, len+32);
23 strbuf_addf(&out, "packet: %12s%c ",
24 packet_trace_prefix, write ? '>' : '<');
26 if ((len >= 4 && !prefixcmp(buf, "PACK")) ||
27 (len >= 5 && !prefixcmp(buf+1, "PACK"))) {
28 strbuf_addstr(&out, "PACK ...");
32 /* XXX we should really handle printable utf8 */
33 for (i = 0; i < len; i++) {
34 /* suppress newlines */
37 if (buf[i] >= 0x20 && buf[i] <= 0x7e)
38 strbuf_addch(&out, buf[i]);
40 strbuf_addf(&out, "\\%o", buf[i]);
44 strbuf_addch(&out, '\n');
45 trace_strbuf(trace_key, &out);
50 * Write a packetized stream, where each line is preceded by
51 * its length (including the header) as a 4-byte hex number.
52 * A length of 'zero' means end of stream (and a length of 1-3
55 * This is all pretty stupid, but we use this packetized line
56 * format to make a streaming format possible without ever
57 * over-running the read buffers. That way we'll never read
58 * into what might be the pack data (which should go to another
61 * The writing side could use stdio, but since the reading
62 * side can't, we stay with pure read/write interfaces.
64 ssize_t safe_write(int fd, const void *buf, ssize_t n)
68 int ret = xwrite(fd, buf, n);
70 buf = (char *) buf + ret;
75 die("write error (disk full?)");
76 die_errno("write error");
82 * If we buffered things up above (we don't, but we should),
85 void packet_flush(int fd)
87 packet_trace("0000", 4, 1);
88 safe_write(fd, "0000", 4);
91 void packet_buf_flush(struct strbuf *buf)
93 packet_trace("0000", 4, 1);
94 strbuf_add(buf, "0000", 4);
97 #define hex(a) (hexchar[(a) & 15])
98 static char buffer[1000];
99 static unsigned format_packet(const char *fmt, va_list args)
101 static char hexchar[] = "0123456789abcdef";
104 n = vsnprintf(buffer + 4, sizeof(buffer) - 4, fmt, args);
105 if (n >= sizeof(buffer)-4)
106 die("protocol error: impossibly long line");
108 buffer[0] = hex(n >> 12);
109 buffer[1] = hex(n >> 8);
110 buffer[2] = hex(n >> 4);
112 packet_trace(buffer+4, n-4, 1);
116 void packet_write(int fd, const char *fmt, ...)
122 n = format_packet(fmt, args);
124 safe_write(fd, buffer, n);
127 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
133 n = format_packet(fmt, args);
135 strbuf_add(buf, buffer, n);
138 static int safe_read(int fd, void *buffer, unsigned size, int return_line_fail)
140 ssize_t ret = read_in_full(fd, buffer, size);
142 die_errno("read error");
143 else if (ret < size) {
144 if (return_line_fail)
147 die("The remote end hung up unexpectedly");
153 static int packet_length(const char *linelen)
158 for (n = 0; n < 4; n++) {
159 unsigned char c = linelen[n];
161 if (c >= '0' && c <= '9') {
165 if (c >= 'a' && c <= 'f') {
169 if (c >= 'A' && c <= 'F') {
178 static int packet_read_internal(int fd, char *buffer, unsigned size, int return_line_fail)
183 ret = safe_read(fd, linelen, 4, return_line_fail);
184 if (return_line_fail && ret < 0)
186 len = packet_length(linelen);
188 die("protocol error: bad line length character: %.4s", linelen);
190 packet_trace("0000", 4, 0);
195 die("protocol error: bad line length %d", len);
196 ret = safe_read(fd, buffer, len, return_line_fail);
197 if (return_line_fail && ret < 0)
200 packet_trace(buffer, len, 0);
204 int packet_read(int fd, char *buffer, unsigned size)
206 return packet_read_internal(fd, buffer, size, 1);
209 int packet_read_line(int fd, char *buffer, unsigned size)
211 return packet_read_internal(fd, buffer, size, 0);
214 int packet_get_line(struct strbuf *out,
215 char **src_buf, size_t *src_len)
221 len = packet_length(*src_buf);
227 packet_trace("0000", 4, 0);
237 strbuf_add(out, *src_buf, len);
240 packet_trace(out->buf, out->len, 0);