Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 217 lines (183 sloc) 4.772 kb
f3a3214 Make send/receive-pack be closer to doing something interesting
Linus Torvalds authored
1 #include "cache.h"
2 #include "pkt-line.h"
3
74543a0 @peff pkt-line: provide a LARGE_PACKET_MAX static buffer
peff authored
4 char packet_buffer[LARGE_PACKET_MAX];
1e4cd68 @bebarino sparse: Fix errors and silence warnings
bebarino authored
5 static const char *packet_trace_prefix = "git";
6aa3085 @kblees trace: improve trace performance
kblees authored
6 static struct trace_key trace_packet = TRACE_KEY_INIT(PACKET);
bbc30f9 @peff add packet tracing debug code
peff authored
7
8 void packet_trace_identity(const char *prog)
9 {
10 packet_trace_prefix = xstrdup(prog);
11 }
12
13 static void packet_trace(const char *buf, unsigned int len, int write)
14 {
15 int i;
16 struct strbuf out;
17
6aa3085 @kblees trace: improve trace performance
kblees authored
18 if (!trace_want(&trace_packet))
bbc30f9 @peff add packet tracing debug code
peff authored
19 return;
20
21 /* +32 is just a guess for header + quoting */
22 strbuf_init(&out, len+32);
23
24 strbuf_addf(&out, "packet: %12s%c ",
25 packet_trace_prefix, write ? '>' : '<');
26
5955654 @chriscool replace {pre,suf}fixcmp() with {starts,ends}_with()
chriscool authored
27 if ((len >= 4 && starts_with(buf, "PACK")) ||
28 (len >= 5 && starts_with(buf+1, "PACK"))) {
bbc30f9 @peff add packet tracing debug code
peff authored
29 strbuf_addstr(&out, "PACK ...");
6aa3085 @kblees trace: improve trace performance
kblees authored
30 trace_disable(&trace_packet);
bbc30f9 @peff add packet tracing debug code
peff authored
31 }
32 else {
33 /* XXX we should really handle printable utf8 */
34 for (i = 0; i < len; i++) {
35 /* suppress newlines */
36 if (buf[i] == '\n')
37 continue;
38 if (buf[i] >= 0x20 && buf[i] <= 0x7e)
39 strbuf_addch(&out, buf[i]);
40 else
41 strbuf_addf(&out, "\\%o", buf[i]);
42 }
43 }
44
45 strbuf_addch(&out, '\n');
6aa3085 @kblees trace: improve trace performance
kblees authored
46 trace_strbuf(&trace_packet, &out);
bbc30f9 @peff add packet tracing debug code
peff authored
47 strbuf_release(&out);
48 }
49
f3a3214 Make send/receive-pack be closer to doing something interesting
Linus Torvalds authored
50 /*
51 * If we buffered things up above (we don't, but we should),
52 * we'd flush it here
53 */
54 void packet_flush(int fd)
55 {
bbc30f9 @peff add packet tracing debug code
peff authored
56 packet_trace("0000", 4, 1);
cdf4fb8 @peff pkt-line: drop safe_write function
peff authored
57 write_or_die(fd, "0000", 4);
f3a3214 Make send/receive-pack be closer to doing something interesting
Linus Torvalds authored
58 }
59
f5615d2 @spearce pkt-line: Add strbuf based functions
spearce authored
60 void packet_buf_flush(struct strbuf *buf)
61 {
bbc30f9 @peff add packet tracing debug code
peff authored
62 packet_trace("0000", 4, 1);
f5615d2 @spearce pkt-line: Add strbuf based functions
spearce authored
63 strbuf_add(buf, "0000", 4);
64 }
65
f3a3214 Make send/receive-pack be closer to doing something interesting
Linus Torvalds authored
66 #define hex(a) (hexchar[(a) & 15])
8e9faf2 @peff pkt-line: allow writing of LARGE_PACKET_MAX buffers
peff authored
67 static void format_packet(struct strbuf *out, const char *fmt, va_list args)
f3a3214 Make send/receive-pack be closer to doing something interesting
Linus Torvalds authored
68 {
69 static char hexchar[] = "0123456789abcdef";
8e9faf2 @peff pkt-line: allow writing of LARGE_PACKET_MAX buffers
peff authored
70 size_t orig_len, n;
f3a3214 Make send/receive-pack be closer to doing something interesting
Linus Torvalds authored
71
8e9faf2 @peff pkt-line: allow writing of LARGE_PACKET_MAX buffers
peff authored
72 orig_len = out->len;
73 strbuf_addstr(out, "0000");
74 strbuf_vaddf(out, fmt, args);
75 n = out->len - orig_len;
76
77 if (n > LARGE_PACKET_MAX)
f3a3214 Make send/receive-pack be closer to doing something interesting
Linus Torvalds authored
78 die("protocol error: impossibly long line");
8e9faf2 @peff pkt-line: allow writing of LARGE_PACKET_MAX buffers
peff authored
79
80 out->buf[orig_len + 0] = hex(n >> 12);
81 out->buf[orig_len + 1] = hex(n >> 8);
82 out->buf[orig_len + 2] = hex(n >> 4);
83 out->buf[orig_len + 3] = hex(n);
84 packet_trace(out->buf + orig_len + 4, n - 4, 1);
f5615d2 @spearce pkt-line: Add strbuf based functions
spearce authored
85 }
86
87 void packet_write(int fd, const char *fmt, ...)
88 {
8e9faf2 @peff pkt-line: allow writing of LARGE_PACKET_MAX buffers
peff authored
89 static struct strbuf buf = STRBUF_INIT;
f5615d2 @spearce pkt-line: Add strbuf based functions
spearce authored
90 va_list args;
91
8e9faf2 @peff pkt-line: allow writing of LARGE_PACKET_MAX buffers
peff authored
92 strbuf_reset(&buf);
f5615d2 @spearce pkt-line: Add strbuf based functions
spearce authored
93 va_start(args, fmt);
8e9faf2 @peff pkt-line: allow writing of LARGE_PACKET_MAX buffers
peff authored
94 format_packet(&buf, fmt, args);
f5615d2 @spearce pkt-line: Add strbuf based functions
spearce authored
95 va_end(args);
8e9faf2 @peff pkt-line: allow writing of LARGE_PACKET_MAX buffers
peff authored
96 write_or_die(fd, buf.buf, buf.len);
f3a3214 Make send/receive-pack be closer to doing something interesting
Linus Torvalds authored
97 }
98
f5615d2 @spearce pkt-line: Add strbuf based functions
spearce authored
99 void packet_buf_write(struct strbuf *buf, const char *fmt, ...)
100 {
101 va_list args;
102
103 va_start(args, fmt);
8e9faf2 @peff pkt-line: allow writing of LARGE_PACKET_MAX buffers
peff authored
104 format_packet(buf, fmt, args);
f5615d2 @spearce pkt-line: Add strbuf based functions
spearce authored
105 va_end(args);
106 }
107
4981fe7 @peff pkt-line: share buffer/descriptor reading implementation
peff authored
108 static int get_packet_data(int fd, char **src_buf, size_t *src_size,
109 void *dst, unsigned size, int options)
f3a3214 Make send/receive-pack be closer to doing something interesting
Linus Torvalds authored
110 {
4981fe7 @peff pkt-line: share buffer/descriptor reading implementation
peff authored
111 ssize_t ret;
112
113 if (fd >= 0 && src_buf && *src_buf)
114 die("BUG: multiple sources given to packet_read");
115
116 /* Read up to "size" bytes from our source, whatever it is. */
117 if (src_buf && *src_buf) {
118 ret = size < *src_size ? size : *src_size;
119 memcpy(dst, *src_buf, ret);
120 *src_buf += ret;
121 *src_size -= ret;
122 } else {
123 ret = read_in_full(fd, dst, size);
124 if (ret < 0)
125 die_errno("read error");
126 }
127
128 /* And complain if we didn't get enough bytes to satisfy the read. */
129 if (ret < size) {
0380942 @peff pkt-line: provide a generic reading function with options
peff authored
130 if (options & PACKET_READ_GENTLE_ON_EOF)
46284dd remove the impression of unexpectedness when access is denied
Heiko Voigt authored
131 return -1;
132
c697ad1 @heikkiorsila Cleanup xread() loops to use read_in_full()
heikkiorsila authored
133 die("The remote end hung up unexpectedly");
46284dd remove the impression of unexpectedness when access is denied
Heiko Voigt authored
134 }
135
136 return ret;
f3a3214 Make send/receive-pack be closer to doing something interesting
Linus Torvalds authored
137 }
138
f5615d2 @spearce pkt-line: Add strbuf based functions
spearce authored
139 static int packet_length(const char *linelen)
f3a3214 Make send/receive-pack be closer to doing something interesting
Linus Torvalds authored
140 {
141 int n;
f5615d2 @spearce pkt-line: Add strbuf based functions
spearce authored
142 int len = 0;
f3a3214 Make send/receive-pack be closer to doing something interesting
Linus Torvalds authored
143
144 for (n = 0; n < 4; n++) {
145 unsigned char c = linelen[n];
146 len <<= 4;
147 if (c >= '0' && c <= '9') {
148 len += c - '0';
149 continue;
150 }
151 if (c >= 'a' && c <= 'f') {
152 len += c - 'a' + 10;
153 continue;
154 }
155 if (c >= 'A' && c <= 'F') {
156 len += c - 'A' + 10;
157 continue;
158 }
f5615d2 @spearce pkt-line: Add strbuf based functions
spearce authored
159 return -1;
f3a3214 Make send/receive-pack be closer to doing something interesting
Linus Torvalds authored
160 }
f5615d2 @spearce pkt-line: Add strbuf based functions
spearce authored
161 return len;
162 }
163
4981fe7 @peff pkt-line: share buffer/descriptor reading implementation
peff authored
164 int packet_read(int fd, char **src_buf, size_t *src_len,
165 char *buffer, unsigned size, int options)
f5615d2 @spearce pkt-line: Add strbuf based functions
spearce authored
166 {
46284dd remove the impression of unexpectedness when access is denied
Heiko Voigt authored
167 int len, ret;
f5615d2 @spearce pkt-line: Add strbuf based functions
spearce authored
168 char linelen[4];
169
4981fe7 @peff pkt-line: share buffer/descriptor reading implementation
peff authored
170 ret = get_packet_data(fd, src_buf, src_len, linelen, 4, options);
0380942 @peff pkt-line: provide a generic reading function with options
peff authored
171 if (ret < 0)
46284dd remove the impression of unexpectedness when access is denied
Heiko Voigt authored
172 return ret;
f5615d2 @spearce pkt-line: Add strbuf based functions
spearce authored
173 len = packet_length(linelen);
174 if (len < 0)
743c4b7 @spearce pkt-line: Make packet_read_line easier to debug
spearce authored
175 die("protocol error: bad line length character: %.4s", linelen);
bbc30f9 @peff add packet tracing debug code
peff authored
176 if (!len) {
177 packet_trace("0000", 4, 0);
f3a3214 Make send/receive-pack be closer to doing something interesting
Linus Torvalds authored
178 return 0;
bbc30f9 @peff add packet tracing debug code
peff authored
179 }
f3a3214 Make send/receive-pack be closer to doing something interesting
Linus Torvalds authored
180 len -= 4;
181 if (len >= size)
182 die("protocol error: bad line length %d", len);
4981fe7 @peff pkt-line: share buffer/descriptor reading implementation
peff authored
183 ret = get_packet_data(fd, src_buf, src_len, buffer, len, options);
0380942 @peff pkt-line: provide a generic reading function with options
peff authored
184 if (ret < 0)
46284dd remove the impression of unexpectedness when access is denied
Heiko Voigt authored
185 return ret;
819b929 @peff pkt-line: teach packet_read_line to chomp newlines
peff authored
186
187 if ((options & PACKET_READ_CHOMP_NEWLINE) &&
188 len && buffer[len-1] == '\n')
189 len--;
190
f3a3214 Make send/receive-pack be closer to doing something interesting
Linus Torvalds authored
191 buffer[len] = 0;
bbc30f9 @peff add packet tracing debug code
peff authored
192 packet_trace(buffer, len, 0);
f3a3214 Make send/receive-pack be closer to doing something interesting
Linus Torvalds authored
193 return len;
194 }
f5615d2 @spearce pkt-line: Add strbuf based functions
spearce authored
195
4981fe7 @peff pkt-line: share buffer/descriptor reading implementation
peff authored
196 static char *packet_read_line_generic(int fd,
197 char **src, size_t *src_len,
198 int *dst_len)
46284dd remove the impression of unexpectedness when access is denied
Heiko Voigt authored
199 {
4981fe7 @peff pkt-line: share buffer/descriptor reading implementation
peff authored
200 int len = packet_read(fd, src, src_len,
201 packet_buffer, sizeof(packet_buffer),
74543a0 @peff pkt-line: provide a LARGE_PACKET_MAX static buffer
peff authored
202 PACKET_READ_CHOMP_NEWLINE);
4981fe7 @peff pkt-line: share buffer/descriptor reading implementation
peff authored
203 if (dst_len)
204 *dst_len = len;
74543a0 @peff pkt-line: provide a LARGE_PACKET_MAX static buffer
peff authored
205 return len ? packet_buffer : NULL;
46284dd remove the impression of unexpectedness when access is denied
Heiko Voigt authored
206 }
207
4981fe7 @peff pkt-line: share buffer/descriptor reading implementation
peff authored
208 char *packet_read_line(int fd, int *len_p)
f5615d2 @spearce pkt-line: Add strbuf based functions
spearce authored
209 {
4981fe7 @peff pkt-line: share buffer/descriptor reading implementation
peff authored
210 return packet_read_line_generic(fd, NULL, NULL, len_p);
211 }
f5615d2 @spearce pkt-line: Add strbuf based functions
spearce authored
212
4981fe7 @peff pkt-line: share buffer/descriptor reading implementation
peff authored
213 char *packet_read_line_buf(char **src, size_t *src_len, int *dst_len)
214 {
215 return packet_read_line_generic(-1, src, src_len, dst_len);
f5615d2 @spearce pkt-line: Add strbuf based functions
spearce authored
216 }
Something went wrong with that request. Please try again.