cocci: simplify check for trivial format strings
[git/debian.git] / pkt-line.c
blob2827ca772a3703f71bc588d0f6cacd5caa318fe7
1 #include "cache.h"
2 #include "pkt-line.h"
3 #include "run-command.h"
5 char packet_buffer[LARGE_PACKET_MAX];
6 static const char *packet_trace_prefix = "git";
7 static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET);
8 static struct trace_key trace_pack = TRACE_KEY_INIT(PACKFILE);
10 void packet_trace_identity(const char *prog)
12 packet_trace_prefix = xstrdup(prog);
15 static const char *get_trace_prefix(void)
17 return in_async() ? "sideband" : packet_trace_prefix;
20 static int packet_trace_pack(const char *buf, unsigned int len, int sideband)
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;
34 static void packet_trace(const char *buf, unsigned int len, int write)
36 int i;
37 struct strbuf out;
38 static int in_pack, sideband;
40 if (!trace_want(&trace_packet) && !trace_want(&trace_pack))
41 return;
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);
52 * Make a note in the human-readable trace that the pack data
53 * started.
55 buf = "PACK ...";
56 len = strlen(buf);
59 if (!trace_want(&trace_packet))
60 return;
62 /* +32 is just a guess for header + quoting */
63 strbuf_init(&out, len+32);
65 strbuf_addf(&out, "packet: %12s%c ",
66 get_trace_prefix(), write ? '>' : '<');
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]);
79 strbuf_addch(&out, '\n');
80 trace_strbuf(&trace_packet, &out);
81 strbuf_release(&out);
85 * If we buffered things up above (we don't, but we should),
86 * we'd flush it here
88 void packet_flush(int fd)
90 packet_trace("0000", 4, 1);
91 write_or_die(fd, "0000", 4);
94 int packet_flush_gently(int fd)
96 packet_trace("0000", 4, 1);
97 if (write_in_full(fd, "0000", 4) < 0)
98 return error("flush packet write failed");
99 return 0;
102 void packet_buf_flush(struct strbuf *buf)
104 packet_trace("0000", 4, 1);
105 strbuf_add(buf, "0000", 4);
108 static void set_packet_header(char *buf, const int size)
110 static char hexchar[] = "0123456789abcdef";
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
120 static void format_packet(struct strbuf *out, const char *fmt, va_list args)
122 size_t orig_len, n;
124 orig_len = out->len;
125 strbuf_addstr(out, "0000");
126 strbuf_vaddf(out, fmt, args);
127 n = out->len - orig_len;
129 if (n > LARGE_PACKET_MAX)
130 die("protocol error: impossibly long line");
132 set_packet_header(&out->buf[orig_len], n);
133 packet_trace(out->buf + orig_len + 4, n - 4, 1);
136 static int packet_write_fmt_1(int fd, int gently,
137 const char *fmt, va_list args)
139 static struct strbuf buf = STRBUF_INIT;
141 strbuf_reset(&buf);
142 format_packet(&buf, fmt, args);
143 if (write_in_full(fd, buf.buf, buf.len) < 0) {
144 if (!gently) {
145 check_pipe(errno);
146 die_errno("packet write with format failed");
148 return error("packet write with format failed");
151 return 0;
154 void packet_write_fmt(int fd, const char *fmt, ...)
156 va_list args;
158 va_start(args, fmt);
159 packet_write_fmt_1(fd, 0, fmt, args);
160 va_end(args);
163 int packet_write_fmt_gently(int fd, const char *fmt, ...)
165 int status;
166 va_list args;
168 va_start(args, fmt);
169 status = packet_write_fmt_1(fd, 1, fmt, args);
170 va_end(args);
171 return status;
174 static int packet_write_gently(const int fd_out, const char *buf, size_t size)
176 static char packet_write_buffer[LARGE_PACKET_MAX];
177 size_t packet_size;
179 if (size > sizeof(packet_write_buffer) - 4)
180 return error("packet write failed - data exceeds max packet size");
182 packet_trace(buf, size, 1);
183 packet_size = size + 4;
184 set_packet_header(packet_write_buffer, packet_size);
185 memcpy(packet_write_buffer + 4, buf, size);
186 if (write_in_full(fd_out, packet_write_buffer, packet_size) < 0)
187 return error("packet write failed");
188 return 0;
191 void packet_write(int fd_out, const char *buf, size_t size)
193 if (packet_write_gently(fd_out, buf, size))
194 die_errno("packet write failed");
197 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
199 va_list args;
201 va_start(args, fmt);
202 format_packet(buf, fmt, args);
203 va_end(args);
206 int write_packetized_from_fd(int fd_in, int fd_out)
208 static char buf[LARGE_PACKET_DATA_MAX];
209 int err = 0;
210 ssize_t bytes_to_write;
212 while (!err) {
213 bytes_to_write = xread(fd_in, buf, sizeof(buf));
214 if (bytes_to_write < 0)
215 return COPY_READ_ERROR;
216 if (bytes_to_write == 0)
217 break;
218 err = packet_write_gently(fd_out, buf, bytes_to_write);
220 if (!err)
221 err = packet_flush_gently(fd_out);
222 return err;
225 int write_packetized_from_buf(const char *src_in, size_t len, int fd_out)
227 int err = 0;
228 size_t bytes_written = 0;
229 size_t bytes_to_write;
231 while (!err) {
232 if ((len - bytes_written) > LARGE_PACKET_DATA_MAX)
233 bytes_to_write = LARGE_PACKET_DATA_MAX;
234 else
235 bytes_to_write = len - bytes_written;
236 if (bytes_to_write == 0)
237 break;
238 err = packet_write_gently(fd_out, src_in + bytes_written, bytes_to_write);
239 bytes_written += bytes_to_write;
241 if (!err)
242 err = packet_flush_gently(fd_out);
243 return err;
246 static int get_packet_data(int fd, char **src_buf, size_t *src_size,
247 void *dst, unsigned size, int options)
249 ssize_t ret;
251 if (fd >= 0 && src_buf && *src_buf)
252 die("BUG: multiple sources given to packet_read");
254 /* Read up to "size" bytes from our source, whatever it is. */
255 if (src_buf && *src_buf) {
256 ret = size < *src_size ? size : *src_size;
257 memcpy(dst, *src_buf, ret);
258 *src_buf += ret;
259 *src_size -= ret;
260 } else {
261 ret = read_in_full(fd, dst, size);
262 if (ret < 0)
263 die_errno("read error");
266 /* And complain if we didn't get enough bytes to satisfy the read. */
267 if (ret != size) {
268 if (options & PACKET_READ_GENTLE_ON_EOF)
269 return -1;
271 die("The remote end hung up unexpectedly");
274 return ret;
277 static int packet_length(const char *linelen)
279 int val = hex2chr(linelen);
280 return (val < 0) ? val : (val << 8) | hex2chr(linelen + 2);
283 int packet_read(int fd, char **src_buf, size_t *src_len,
284 char *buffer, unsigned size, int options)
286 int len, ret;
287 char linelen[4];
289 ret = get_packet_data(fd, src_buf, src_len, linelen, 4, options);
290 if (ret < 0)
291 return ret;
292 len = packet_length(linelen);
293 if (len < 0)
294 die("protocol error: bad line length character: %.4s", linelen);
295 if (!len) {
296 packet_trace("0000", 4, 0);
297 return 0;
299 len -= 4;
300 if (len >= size)
301 die("protocol error: bad line length %d", len);
302 ret = get_packet_data(fd, src_buf, src_len, buffer, len, options);
303 if (ret < 0)
304 return ret;
306 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
307 len && buffer[len-1] == '\n')
308 len--;
310 buffer[len] = 0;
311 packet_trace(buffer, len, 0);
312 return len;
315 static char *packet_read_line_generic(int fd,
316 char **src, size_t *src_len,
317 int *dst_len)
319 int len = packet_read(fd, src, src_len,
320 packet_buffer, sizeof(packet_buffer),
321 PACKET_READ_CHOMP_NEWLINE);
322 if (dst_len)
323 *dst_len = len;
324 return (len > 0) ? packet_buffer : NULL;
327 char *packet_read_line(int fd, int *len_p)
329 return packet_read_line_generic(fd, NULL, NULL, len_p);
332 int packet_read_line_gently(int fd, int *dst_len, char **dst_line)
334 int len = packet_read(fd, NULL, NULL,
335 packet_buffer, sizeof(packet_buffer),
336 PACKET_READ_CHOMP_NEWLINE|PACKET_READ_GENTLE_ON_EOF);
337 if (dst_len)
338 *dst_len = len;
339 if (dst_line)
340 *dst_line = (len > 0) ? packet_buffer : NULL;
341 return len;
344 char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
346 return packet_read_line_generic(-1, src, src_len, dst_len);
349 ssize_t read_packetized_to_strbuf(int fd_in, struct strbuf *sb_out)
351 int packet_len;
353 size_t orig_len = sb_out->len;
354 size_t orig_alloc = sb_out->alloc;
356 for (;;) {
357 strbuf_grow(sb_out, LARGE_PACKET_DATA_MAX);
358 packet_len = packet_read(fd_in, NULL, NULL,
359 /* strbuf_grow() above always allocates one extra byte to
360 * store a '\0' at the end of the string. packet_read()
361 * writes a '\0' extra byte at the end, too. Let it know
362 * that there is already room for the extra byte.
364 sb_out->buf + sb_out->len, LARGE_PACKET_DATA_MAX+1,
365 PACKET_READ_GENTLE_ON_EOF);
366 if (packet_len <= 0)
367 break;
368 sb_out->len += packet_len;
371 if (packet_len < 0) {
372 if (orig_alloc == 0)
373 strbuf_release(sb_out);
374 else
375 strbuf_setlen(sb_out, orig_len);
376 return packet_len;
378 return sb_out->len - orig_len;