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 * If we buffered things up above (we don't, but we should),
53 void packet_flush(int fd
)
55 packet_trace("0000", 4, 1);
56 write_or_die(fd
, "0000", 4);
59 void packet_buf_flush(struct strbuf
*buf
)
61 packet_trace("0000", 4, 1);
62 strbuf_add(buf
, "0000", 4);
65 #define hex(a) (hexchar[(a) & 15])
66 static char buffer
[1000];
67 static unsigned format_packet(const char *fmt
, va_list args
)
69 static char hexchar
[] = "0123456789abcdef";
72 n
= vsnprintf(buffer
+ 4, sizeof(buffer
) - 4, fmt
, args
);
73 if (n
>= sizeof(buffer
)-4)
74 die("protocol error: impossibly long line");
76 buffer
[0] = hex(n
>> 12);
77 buffer
[1] = hex(n
>> 8);
78 buffer
[2] = hex(n
>> 4);
80 packet_trace(buffer
+4, n
-4, 1);
84 void packet_write(int fd
, const char *fmt
, ...)
90 n
= format_packet(fmt
, args
);
92 write_or_die(fd
, buffer
, n
);
95 void packet_buf_write(struct strbuf
*buf
, const char *fmt
, ...)
101 n
= format_packet(fmt
, args
);
103 strbuf_add(buf
, buffer
, n
);
106 static int safe_read(int fd
, void *buffer
, unsigned size
, int options
)
108 ssize_t ret
= read_in_full(fd
, buffer
, size
);
110 die_errno("read error");
111 else if (ret
< size
) {
112 if (options
& PACKET_READ_GENTLE_ON_EOF
)
115 die("The remote end hung up unexpectedly");
121 static int packet_length(const char *linelen
)
126 for (n
= 0; n
< 4; n
++) {
127 unsigned char c
= linelen
[n
];
129 if (c
>= '0' && c
<= '9') {
133 if (c
>= 'a' && c
<= 'f') {
137 if (c
>= 'A' && c
<= 'F') {
146 int packet_read(int fd
, char *buffer
, unsigned size
, int options
)
151 ret
= safe_read(fd
, linelen
, 4, options
);
154 len
= packet_length(linelen
);
156 die("protocol error: bad line length character: %.4s", linelen
);
158 packet_trace("0000", 4, 0);
163 die("protocol error: bad line length %d", len
);
164 ret
= safe_read(fd
, buffer
, len
, options
);
168 packet_trace(buffer
, len
, 0);
172 int packet_read_line(int fd
, char *buffer
, unsigned size
)
174 return packet_read(fd
, buffer
, size
, 0);
177 int packet_get_line(struct strbuf
*out
,
178 char **src_buf
, size_t *src_len
)
184 len
= packet_length(*src_buf
);
190 packet_trace("0000", 4, 0);
200 strbuf_add(out
, *src_buf
, len
);
203 packet_trace(out
->buf
, out
->len
, 0);