trace: add trace_strbuf
[git/git.git] / pkt-line.c
CommitLineData
f3a3214e
LT
1#include "cache.h"
2#include "pkt-line.h"
3
4/*
5 * Write a packetized stream, where each line is preceded by
6 * its length (including the header) as a 4-byte hex number.
7 * A length of 'zero' means end of stream (and a length of 1-3
a6080a0a 8 * would be an error).
f3a3214e
LT
9 *
10 * This is all pretty stupid, but we use this packetized line
11 * format to make a streaming format possible without ever
12 * over-running the read buffers. That way we'll never read
13 * into what might be the pack data (which should go to another
14 * process entirely).
15 *
16 * The writing side could use stdio, but since the reading
17 * side can't, we stay with pure read/write interfaces.
18 */
583b7ea3 19ssize_t safe_write(int fd, const void *buf, ssize_t n)
f3a3214e 20{
583b7ea3 21 ssize_t nn = n;
f3a3214e 22 while (n) {
1c15afb9 23 int ret = xwrite(fd, buf, n);
f3a3214e 24 if (ret > 0) {
1d7f171c 25 buf = (char *) buf + ret;
f3a3214e
LT
26 n -= ret;
27 continue;
28 }
29 if (!ret)
30 die("write error (disk full?)");
d824cbba 31 die_errno("write error");
f3a3214e 32 }
583b7ea3 33 return nn;
f3a3214e
LT
34}
35
36/*
37 * If we buffered things up above (we don't, but we should),
38 * we'd flush it here
39 */
40void packet_flush(int fd)
41{
42 safe_write(fd, "0000", 4);
43}
44
f5615d24
SP
45void packet_buf_flush(struct strbuf *buf)
46{
47 strbuf_add(buf, "0000", 4);
48}
49
f3a3214e 50#define hex(a) (hexchar[(a) & 15])
f5615d24
SP
51static char buffer[1000];
52static unsigned format_packet(const char *fmt, va_list args)
f3a3214e 53{
f3a3214e 54 static char hexchar[] = "0123456789abcdef";
f3a3214e
LT
55 unsigned n;
56
f3a3214e 57 n = vsnprintf(buffer + 4, sizeof(buffer) - 4, fmt, args);
f3a3214e
LT
58 if (n >= sizeof(buffer)-4)
59 die("protocol error: impossibly long line");
60 n += 4;
61 buffer[0] = hex(n >> 12);
62 buffer[1] = hex(n >> 8);
63 buffer[2] = hex(n >> 4);
64 buffer[3] = hex(n);
f5615d24
SP
65 return n;
66}
67
68void packet_write(int fd, const char *fmt, ...)
69{
70 va_list args;
71 unsigned n;
72
73 va_start(args, fmt);
74 n = format_packet(fmt, args);
75 va_end(args);
f3a3214e
LT
76 safe_write(fd, buffer, n);
77}
78
f5615d24
SP
79void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
80{
81 va_list args;
82 unsigned n;
83
84 va_start(args, fmt);
85 n = format_packet(fmt, args);
86 va_end(args);
87 strbuf_add(buf, buffer, n);
88}
89
f3a3214e
LT
90static void safe_read(int fd, void *buffer, unsigned size)
91{
c697ad14
HO
92 ssize_t ret = read_in_full(fd, buffer, size);
93 if (ret < 0)
d824cbba 94 die_errno("read error");
c697ad14
HO
95 else if (ret < size)
96 die("The remote end hung up unexpectedly");
f3a3214e
LT
97}
98
f5615d24 99static int packet_length(const char *linelen)
f3a3214e
LT
100{
101 int n;
f5615d24 102 int len = 0;
f3a3214e 103
f3a3214e
LT
104 for (n = 0; n < 4; n++) {
105 unsigned char c = linelen[n];
106 len <<= 4;
107 if (c >= '0' && c <= '9') {
108 len += c - '0';
109 continue;
110 }
111 if (c >= 'a' && c <= 'f') {
112 len += c - 'a' + 10;
113 continue;
114 }
115 if (c >= 'A' && c <= 'F') {
116 len += c - 'A' + 10;
117 continue;
118 }
f5615d24 119 return -1;
f3a3214e 120 }
f5615d24
SP
121 return len;
122}
123
124int packet_read_line(int fd, char *buffer, unsigned size)
125{
126 int len;
127 char linelen[4];
128
129 safe_read(fd, linelen, 4);
130 len = packet_length(linelen);
131 if (len < 0)
743c4b7b 132 die("protocol error: bad line length character: %.4s", linelen);
f3a3214e
LT
133 if (!len)
134 return 0;
135 len -= 4;
136 if (len >= size)
137 die("protocol error: bad line length %d", len);
138 safe_read(fd, buffer, len);
139 buffer[len] = 0;
140 return len;
141}
f5615d24
SP
142
143int packet_get_line(struct strbuf *out,
144 char **src_buf, size_t *src_len)
145{
146 int len;
147
148 if (*src_len < 4)
149 return -1;
150 len = packet_length(*src_buf);
151 if (len < 0)
152 return -1;
153 if (!len) {
154 *src_buf += 4;
155 *src_len -= 4;
156 return 0;
157 }
158 if (*src_len < len)
159 return -2;
160
161 *src_buf += 4;
162 *src_len -= 4;
163 len -= 4;
164
165 strbuf_add(out, *src_buf, len);
166 *src_buf += len;
167 *src_len -= len;
168 return len;
169}