Blame


1 93658fb9 2020-03-18 stsp /*
2 93658fb9 2020-03-18 stsp * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 93658fb9 2020-03-18 stsp *
4 93658fb9 2020-03-18 stsp * Permission to use, copy, modify, and distribute this software for any
5 93658fb9 2020-03-18 stsp * purpose with or without fee is hereby granted, provided that the above
6 93658fb9 2020-03-18 stsp * copyright notice and this permission notice appear in all copies.
7 93658fb9 2020-03-18 stsp *
8 93658fb9 2020-03-18 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 93658fb9 2020-03-18 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 93658fb9 2020-03-18 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 93658fb9 2020-03-18 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 93658fb9 2020-03-18 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 93658fb9 2020-03-18 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 93658fb9 2020-03-18 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 93658fb9 2020-03-18 stsp */
16 93658fb9 2020-03-18 stsp
17 93658fb9 2020-03-18 stsp #include <sys/types.h>
18 93658fb9 2020-03-18 stsp #include <sys/queue.h>
19 93658fb9 2020-03-18 stsp #include <sys/uio.h>
20 93658fb9 2020-03-18 stsp #include <sys/time.h>
21 93658fb9 2020-03-18 stsp #include <sys/stat.h>
22 93658fb9 2020-03-18 stsp
23 93658fb9 2020-03-18 stsp #include <stdint.h>
24 93658fb9 2020-03-18 stsp #include <errno.h>
25 93658fb9 2020-03-18 stsp #include <imsg.h>
26 93658fb9 2020-03-18 stsp #include <limits.h>
27 93658fb9 2020-03-18 stsp #include <signal.h>
28 93658fb9 2020-03-18 stsp #include <stdio.h>
29 93658fb9 2020-03-18 stsp #include <stdlib.h>
30 93658fb9 2020-03-18 stsp #include <string.h>
31 93658fb9 2020-03-18 stsp #include <ctype.h>
32 93658fb9 2020-03-18 stsp #include <sha1.h>
33 93658fb9 2020-03-18 stsp #include <fcntl.h>
34 81a12da5 2020-09-09 naddy #include <unistd.h>
35 93658fb9 2020-03-18 stsp #include <zlib.h>
36 93658fb9 2020-03-18 stsp #include <err.h>
37 93658fb9 2020-03-18 stsp
38 93658fb9 2020-03-18 stsp #include "got_error.h"
39 93658fb9 2020-03-18 stsp #include "got_object.h"
40 abe0f35f 2020-03-18 stsp #include "got_path.h"
41 8a29a085 2020-03-18 stsp #include "got_version.h"
42 f1c6967f 2020-03-19 stsp #include "got_fetch.h"
43 659e7fbd 2020-03-20 stsp #include "got_reference.h"
44 93658fb9 2020-03-18 stsp
45 93658fb9 2020-03-18 stsp #include "got_lib_sha1.h"
46 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
47 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
48 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
49 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
50 0872c0b0 2020-03-18 stsp #include "got_lib_pack.h"
51 93658fb9 2020-03-18 stsp
52 8a29a085 2020-03-18 stsp #ifndef nitems
53 8a29a085 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 8a29a085 2020-03-18 stsp #endif
55 8a29a085 2020-03-18 stsp
56 93658fb9 2020-03-18 stsp struct got_object *indexed;
57 858b0dfb 2020-03-20 stsp static int chattygot;
58 93658fb9 2020-03-18 stsp static struct got_object_id zhash = {.sha1={0}};
59 93658fb9 2020-03-18 stsp
60 fe53745c 2020-03-18 stsp static const struct got_error *
61 fe53745c 2020-03-18 stsp readn(ssize_t *off, int fd, void *buf, size_t n)
62 93658fb9 2020-03-18 stsp {
63 fe53745c 2020-03-18 stsp ssize_t r;
64 93658fb9 2020-03-18 stsp
65 fe53745c 2020-03-18 stsp *off = 0;
66 fe53745c 2020-03-18 stsp while (*off != n) {
67 fe53745c 2020-03-18 stsp r = read(fd, buf + *off, n - *off);
68 fe53745c 2020-03-18 stsp if (r == -1)
69 fe53745c 2020-03-18 stsp return got_error_from_errno("read");
70 93658fb9 2020-03-18 stsp if (r == 0)
71 fe53745c 2020-03-18 stsp return NULL;
72 fe53745c 2020-03-18 stsp *off += r;
73 93658fb9 2020-03-18 stsp }
74 fe53745c 2020-03-18 stsp return NULL;
75 93658fb9 2020-03-18 stsp }
76 93658fb9 2020-03-18 stsp
77 38c670f1 2020-03-18 stsp static const struct got_error *
78 93658fb9 2020-03-18 stsp flushpkt(int fd)
79 93658fb9 2020-03-18 stsp {
80 38c670f1 2020-03-18 stsp ssize_t w;
81 38c670f1 2020-03-18 stsp
82 2690194b 2020-03-21 stsp if (chattygot > 1)
83 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: writepkt: 0000\n", getprogname());
84 858b0dfb 2020-03-20 stsp
85 62d463ca 2020-10-20 naddy w = write(fd, "0000", 4);
86 62d463ca 2020-10-20 naddy if (w == -1)
87 38c670f1 2020-03-18 stsp return got_error_from_errno("write");
88 38c670f1 2020-03-18 stsp if (w != 4)
89 38c670f1 2020-03-18 stsp return got_error(GOT_ERR_IO);
90 38c670f1 2020-03-18 stsp return NULL;
91 93658fb9 2020-03-18 stsp }
92 93658fb9 2020-03-18 stsp
93 531c3985 2020-03-18 stsp /*
94 531c3985 2020-03-18 stsp * Packet header contains a 4-byte hexstring which specifies the length
95 531c3985 2020-03-18 stsp * of data which follows.
96 531c3985 2020-03-18 stsp */
97 fe53745c 2020-03-18 stsp static const struct got_error *
98 531c3985 2020-03-18 stsp read_pkthdr(int *datalen, int fd)
99 93658fb9 2020-03-18 stsp {
100 531c3985 2020-03-18 stsp static const struct got_error *err = NULL;
101 4dc8ee09 2020-03-18 stsp char lenstr[5];
102 4dc8ee09 2020-03-18 stsp long len;
103 93658fb9 2020-03-18 stsp char *e;
104 54d1a70f 2020-03-18 stsp int n, i;
105 fe53745c 2020-03-18 stsp ssize_t r;
106 93658fb9 2020-03-18 stsp
107 531c3985 2020-03-18 stsp *datalen = 0;
108 fe53745c 2020-03-18 stsp
109 4dc8ee09 2020-03-18 stsp err = readn(&r, fd, lenstr, 4);
110 fe53745c 2020-03-18 stsp if (err)
111 fe53745c 2020-03-18 stsp return err;
112 858b0dfb 2020-03-20 stsp if (r == 0) {
113 858b0dfb 2020-03-20 stsp /* implicit "0000" */
114 2690194b 2020-03-21 stsp if (chattygot > 1)
115 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: readpkt: 0000\n", getprogname());
116 531c3985 2020-03-18 stsp return NULL;
117 858b0dfb 2020-03-20 stsp }
118 4dc8ee09 2020-03-18 stsp if (r != 4)
119 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
120 531c3985 2020-03-18 stsp "wrong packet header length");
121 fe53745c 2020-03-18 stsp
122 4dc8ee09 2020-03-18 stsp lenstr[4] = '\0';
123 54d1a70f 2020-03-18 stsp for (i = 0; i < 4; i++) {
124 858b0dfb 2020-03-20 stsp if (!isprint((unsigned char)lenstr[i]))
125 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
126 858b0dfb 2020-03-20 stsp "unprintable character in packet length field");
127 858b0dfb 2020-03-20 stsp }
128 858b0dfb 2020-03-20 stsp for (i = 0; i < 4; i++) {
129 858b0dfb 2020-03-20 stsp if (!isxdigit((unsigned char)lenstr[i])) {
130 858b0dfb 2020-03-20 stsp if (chattygot)
131 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: bad length: '%s'\n",
132 858b0dfb 2020-03-20 stsp getprogname(), lenstr);
133 858b0dfb 2020-03-20 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
134 531c3985 2020-03-18 stsp "packet length not specified in hex");
135 858b0dfb 2020-03-20 stsp }
136 54d1a70f 2020-03-18 stsp }
137 4dc8ee09 2020-03-18 stsp errno = 0;
138 4dc8ee09 2020-03-18 stsp len = strtol(lenstr, &e, 16);
139 4dc8ee09 2020-03-18 stsp if (lenstr[0] == '\0' || *e != '\0')
140 4dc8ee09 2020-03-18 stsp return got_error(GOT_ERR_BAD_PACKET);
141 4dc8ee09 2020-03-18 stsp if (errno == ERANGE && (len == LONG_MAX || len == LONG_MIN))
142 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
143 4dc8ee09 2020-03-18 stsp if (len > INT_MAX || len < INT_MIN)
144 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "bad packet length");
145 4dc8ee09 2020-03-18 stsp n = len;
146 531c3985 2020-03-18 stsp if (n == 0)
147 fe53745c 2020-03-18 stsp return NULL;
148 4dc8ee09 2020-03-18 stsp if (n <= 4)
149 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "packet too short");
150 93658fb9 2020-03-18 stsp n -= 4;
151 531c3985 2020-03-18 stsp
152 531c3985 2020-03-18 stsp *datalen = n;
153 531c3985 2020-03-18 stsp return NULL;
154 531c3985 2020-03-18 stsp }
155 531c3985 2020-03-18 stsp
156 531c3985 2020-03-18 stsp static const struct got_error *
157 531c3985 2020-03-18 stsp readpkt(int *outlen, int fd, char *buf, int buflen)
158 531c3985 2020-03-18 stsp {
159 531c3985 2020-03-18 stsp const struct got_error *err = NULL;
160 858b0dfb 2020-03-20 stsp int datalen, i;
161 531c3985 2020-03-18 stsp ssize_t n;
162 531c3985 2020-03-18 stsp
163 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
164 531c3985 2020-03-18 stsp if (err)
165 531c3985 2020-03-18 stsp return err;
166 531c3985 2020-03-18 stsp
167 531c3985 2020-03-18 stsp if (datalen > buflen)
168 fe53745c 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
169 fe53745c 2020-03-18 stsp
170 531c3985 2020-03-18 stsp err = readn(&n, fd, buf, datalen);
171 fe53745c 2020-03-18 stsp if (err)
172 fe53745c 2020-03-18 stsp return err;
173 531c3985 2020-03-18 stsp if (n != datalen)
174 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "short packet");
175 fe53745c 2020-03-18 stsp
176 2690194b 2020-03-21 stsp if (chattygot > 1) {
177 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: readpkt: %zd:\t", getprogname(), n);
178 858b0dfb 2020-03-20 stsp for (i = 0; i < n; i++) {
179 858b0dfb 2020-03-20 stsp if (isprint(buf[i]))
180 858b0dfb 2020-03-20 stsp fputc(buf[i], stderr);
181 858b0dfb 2020-03-20 stsp else
182 858b0dfb 2020-03-20 stsp fprintf(stderr, "[0x%.2x]", buf[i]);
183 858b0dfb 2020-03-20 stsp }
184 858b0dfb 2020-03-20 stsp fputc('\n', stderr);
185 858b0dfb 2020-03-20 stsp }
186 858b0dfb 2020-03-20 stsp
187 fe53745c 2020-03-18 stsp *outlen = n;
188 fe53745c 2020-03-18 stsp return NULL;
189 93658fb9 2020-03-18 stsp }
190 93658fb9 2020-03-18 stsp
191 344e4747 2020-03-18 stsp static const struct got_error *
192 93658fb9 2020-03-18 stsp writepkt(int fd, char *buf, int nbuf)
193 93658fb9 2020-03-18 stsp {
194 93658fb9 2020-03-18 stsp char len[5];
195 858b0dfb 2020-03-20 stsp int i;
196 344e4747 2020-03-18 stsp ssize_t w;
197 93658fb9 2020-03-18 stsp
198 93658fb9 2020-03-18 stsp if (snprintf(len, sizeof(len), "%04x", nbuf + 4) >= sizeof(len))
199 344e4747 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
200 344e4747 2020-03-18 stsp w = write(fd, len, 4);
201 344e4747 2020-03-18 stsp if (w == -1)
202 344e4747 2020-03-18 stsp return got_error_from_errno("write");
203 344e4747 2020-03-18 stsp if (w != 4)
204 344e4747 2020-03-18 stsp return got_error(GOT_ERR_IO);
205 344e4747 2020-03-18 stsp w = write(fd, buf, nbuf);
206 344e4747 2020-03-18 stsp if (w == -1)
207 344e4747 2020-03-18 stsp return got_error_from_errno("write");
208 344e4747 2020-03-18 stsp if (w != nbuf)
209 344e4747 2020-03-18 stsp return got_error(GOT_ERR_IO);
210 2690194b 2020-03-21 stsp if (chattygot > 1) {
211 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: writepkt: %s:\t", getprogname(), len);
212 858b0dfb 2020-03-20 stsp for (i = 0; i < nbuf; i++) {
213 858b0dfb 2020-03-20 stsp if (isprint(buf[i]))
214 858b0dfb 2020-03-20 stsp fputc(buf[i], stderr);
215 858b0dfb 2020-03-20 stsp else
216 858b0dfb 2020-03-20 stsp fprintf(stderr, "[0x%.2x]", buf[i]);
217 858b0dfb 2020-03-20 stsp }
218 858b0dfb 2020-03-20 stsp fputc('\n', stderr);
219 858b0dfb 2020-03-20 stsp }
220 344e4747 2020-03-18 stsp return NULL;
221 93658fb9 2020-03-18 stsp }
222 93658fb9 2020-03-18 stsp
223 7848a0e1 2020-03-19 stsp static void
224 7848a0e1 2020-03-19 stsp match_remote_ref(struct got_pathlist_head *have_refs,
225 7848a0e1 2020-03-19 stsp struct got_object_id *my_id, char *refname)
226 93658fb9 2020-03-18 stsp {
227 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
228 93658fb9 2020-03-18 stsp
229 7848a0e1 2020-03-19 stsp /* XXX zero-hash signifies we don't have this ref;
230 7848a0e1 2020-03-19 stsp * we should use a flag instead */
231 7848a0e1 2020-03-19 stsp memset(my_id, 0, sizeof(*my_id));
232 93658fb9 2020-03-18 stsp
233 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
234 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
235 7848a0e1 2020-03-19 stsp if (strcmp(pe->path, refname) == 0) {
236 7848a0e1 2020-03-19 stsp memcpy(my_id, id, sizeof(*my_id));
237 33501562 2020-03-18 stsp break;
238 33501562 2020-03-18 stsp }
239 93658fb9 2020-03-18 stsp }
240 93658fb9 2020-03-18 stsp }
241 93658fb9 2020-03-18 stsp
242 93658fb9 2020-03-18 stsp static int
243 659e7fbd 2020-03-20 stsp match_branch(const char *branch, const char *wanted_branch)
244 93658fb9 2020-03-18 stsp {
245 659e7fbd 2020-03-20 stsp if (strncmp(branch, "refs/heads/", 11) != 0)
246 659e7fbd 2020-03-20 stsp return 0;
247 93658fb9 2020-03-18 stsp
248 659e7fbd 2020-03-20 stsp if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
249 659e7fbd 2020-03-20 stsp wanted_branch += 11;
250 659e7fbd 2020-03-20 stsp
251 659e7fbd 2020-03-20 stsp return (strcmp(branch + 11, wanted_branch) == 0);
252 0e4002ca 2020-03-21 stsp }
253 0e4002ca 2020-03-21 stsp
254 0e4002ca 2020-03-21 stsp static int
255 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
256 0e4002ca 2020-03-21 stsp {
257 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
258 0e4002ca 2020-03-21 stsp return 0;
259 0e4002ca 2020-03-21 stsp refname += 5;
260 0e4002ca 2020-03-21 stsp
261 0e4002ca 2020-03-21 stsp /*
262 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
263 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
264 0e4002ca 2020-03-21 stsp */
265 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
266 0e4002ca 2020-03-21 stsp return 0;
267 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
268 0e4002ca 2020-03-21 stsp return 0;
269 0e4002ca 2020-03-21 stsp
270 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
271 0e4002ca 2020-03-21 stsp wanted_ref += 5;
272 0e4002ca 2020-03-21 stsp
273 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
274 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
275 0e4002ca 2020-03-21 stsp return 1;
276 0e4002ca 2020-03-21 stsp
277 0e4002ca 2020-03-21 stsp /* Allow exact match. */
278 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
279 93658fb9 2020-03-18 stsp }
280 93658fb9 2020-03-18 stsp
281 0d0a341c 2020-03-18 stsp static const struct got_error *
282 00cd0e0a 2020-03-18 stsp tokenize_refline(char **tokens, char *line, int len, int maxtokens)
283 93658fb9 2020-03-18 stsp {
284 0d0a341c 2020-03-18 stsp const struct got_error *err = NULL;
285 93658fb9 2020-03-18 stsp char *p;
286 0d0a341c 2020-03-18 stsp size_t i, n = 0;
287 93658fb9 2020-03-18 stsp
288 0d0a341c 2020-03-18 stsp for (i = 0; i < maxtokens; i++)
289 0d0a341c 2020-03-18 stsp tokens[i] = NULL;
290 0d0a341c 2020-03-18 stsp
291 0d0a341c 2020-03-18 stsp for (i = 0; n < len && i < maxtokens; i++) {
292 0d0a341c 2020-03-18 stsp while (isspace(*line)) {
293 93658fb9 2020-03-18 stsp line++;
294 0d0a341c 2020-03-18 stsp n++;
295 0d0a341c 2020-03-18 stsp }
296 93658fb9 2020-03-18 stsp p = line;
297 8991a328 2021-08-26 stsp while (*line != '\0' && n < len &&
298 0d0a341c 2020-03-18 stsp (!isspace(*line) || i == maxtokens - 1)) {
299 93658fb9 2020-03-18 stsp line++;
300 0d0a341c 2020-03-18 stsp n++;
301 0d0a341c 2020-03-18 stsp }
302 0d0a341c 2020-03-18 stsp tokens[i] = strndup(p, line - p);
303 0d0a341c 2020-03-18 stsp if (tokens[i] == NULL) {
304 0d0a341c 2020-03-18 stsp err = got_error_from_errno("strndup");
305 0d0a341c 2020-03-18 stsp goto done;
306 0d0a341c 2020-03-18 stsp }
307 0d0a341c 2020-03-18 stsp /* Skip \0 field-delimiter at end of token. */
308 0d0a341c 2020-03-18 stsp while (line[0] == '\0' && n < len) {
309 0d0a341c 2020-03-18 stsp line++;
310 0d0a341c 2020-03-18 stsp n++;
311 0d0a341c 2020-03-18 stsp }
312 93658fb9 2020-03-18 stsp }
313 0d0a341c 2020-03-18 stsp if (i <= 2)
314 14d2b281 2021-08-25 stsp err = got_error(GOT_ERR_BAD_PACKET);
315 0d0a341c 2020-03-18 stsp done:
316 0d0a341c 2020-03-18 stsp if (err) {
317 0d0a341c 2020-03-18 stsp int j;
318 631179de 2020-07-31 naddy for (j = 0; j < i; j++) {
319 0d0a341c 2020-03-18 stsp free(tokens[j]);
320 0d0a341c 2020-03-18 stsp tokens[j] = NULL;
321 631179de 2020-07-31 naddy }
322 0d0a341c 2020-03-18 stsp }
323 0d0a341c 2020-03-18 stsp return err;
324 8a29a085 2020-03-18 stsp }
325 00cd0e0a 2020-03-18 stsp
326 00cd0e0a 2020-03-18 stsp static const struct got_error *
327 00cd0e0a 2020-03-18 stsp parse_refline(char **id_str, char **refname, char **server_capabilities,
328 00cd0e0a 2020-03-18 stsp char *line, int len)
329 00cd0e0a 2020-03-18 stsp {
330 00cd0e0a 2020-03-18 stsp const struct got_error *err = NULL;
331 00cd0e0a 2020-03-18 stsp char *tokens[3];
332 8a29a085 2020-03-18 stsp
333 00cd0e0a 2020-03-18 stsp err = tokenize_refline(tokens, line, len, nitems(tokens));
334 00cd0e0a 2020-03-18 stsp if (err)
335 00cd0e0a 2020-03-18 stsp return err;
336 00cd0e0a 2020-03-18 stsp
337 00cd0e0a 2020-03-18 stsp if (tokens[0])
338 00cd0e0a 2020-03-18 stsp *id_str = tokens[0];
339 00cd0e0a 2020-03-18 stsp if (tokens[1])
340 00cd0e0a 2020-03-18 stsp *refname = tokens[1];
341 2690194b 2020-03-21 stsp if (tokens[2]) {
342 2690194b 2020-03-21 stsp char *p;
343 00cd0e0a 2020-03-18 stsp *server_capabilities = tokens[2];
344 2690194b 2020-03-21 stsp p = strrchr(*server_capabilities, '\n');
345 2690194b 2020-03-21 stsp if (p)
346 2690194b 2020-03-21 stsp *p = '\0';
347 2690194b 2020-03-21 stsp }
348 3168e5da 2020-09-10 stsp
349 00cd0e0a 2020-03-18 stsp return NULL;
350 00cd0e0a 2020-03-18 stsp }
351 00cd0e0a 2020-03-18 stsp
352 531c3985 2020-03-18 stsp #define GOT_CAPA_AGENT "agent"
353 531c3985 2020-03-18 stsp #define GOT_CAPA_OFS_DELTA "ofs-delta"
354 531c3985 2020-03-18 stsp #define GOT_CAPA_SIDE_BAND_64K "side-band-64k"
355 531c3985 2020-03-18 stsp
356 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_PACKFILE_DATA 1
357 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_PROGRESS_INFO 2
358 531c3985 2020-03-18 stsp #define GOT_SIDEBAND_ERROR_INFO 3
359 531c3985 2020-03-18 stsp
360 531c3985 2020-03-18 stsp
361 8a29a085 2020-03-18 stsp struct got_capability {
362 8a29a085 2020-03-18 stsp const char *key;
363 8a29a085 2020-03-18 stsp const char *value;
364 8a29a085 2020-03-18 stsp };
365 8a29a085 2020-03-18 stsp static const struct got_capability got_capabilities[] = {
366 531c3985 2020-03-18 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
367 531c3985 2020-03-18 stsp { GOT_CAPA_OFS_DELTA, NULL },
368 531c3985 2020-03-18 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
369 8a29a085 2020-03-18 stsp };
370 8a29a085 2020-03-18 stsp
371 8a29a085 2020-03-18 stsp static const struct got_error *
372 8a29a085 2020-03-18 stsp match_capability(char **my_capabilities, const char *capa,
373 8a29a085 2020-03-18 stsp const struct got_capability *mycapa)
374 8a29a085 2020-03-18 stsp {
375 8a29a085 2020-03-18 stsp char *equalsign;
376 8a29a085 2020-03-18 stsp char *s;
377 8a29a085 2020-03-18 stsp
378 8a29a085 2020-03-18 stsp equalsign = strchr(capa, '=');
379 8a29a085 2020-03-18 stsp if (equalsign) {
380 8a29a085 2020-03-18 stsp if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
381 8a29a085 2020-03-18 stsp return NULL;
382 8a29a085 2020-03-18 stsp } else {
383 8a29a085 2020-03-18 stsp if (strcmp(capa, mycapa->key) != 0)
384 8a29a085 2020-03-18 stsp return NULL;
385 8a29a085 2020-03-18 stsp }
386 8a29a085 2020-03-18 stsp
387 406106ee 2020-03-20 stsp if (asprintf(&s, "%s %s%s%s",
388 8a29a085 2020-03-18 stsp *my_capabilities != NULL ? *my_capabilities : "",
389 8a29a085 2020-03-18 stsp mycapa->key,
390 8a29a085 2020-03-18 stsp mycapa->value != NULL ? "=" : "",
391 8a29a085 2020-03-18 stsp mycapa->value != NULL? mycapa->value : "") == -1)
392 8a29a085 2020-03-18 stsp return got_error_from_errno("asprintf");
393 8a29a085 2020-03-18 stsp
394 8a29a085 2020-03-18 stsp free(*my_capabilities);
395 8a29a085 2020-03-18 stsp *my_capabilities = s;
396 8a29a085 2020-03-18 stsp return NULL;
397 93658fb9 2020-03-18 stsp }
398 93658fb9 2020-03-18 stsp
399 9ff10419 2020-03-18 stsp static const struct got_error *
400 01538ce4 2020-03-18 stsp add_symref(struct got_pathlist_head *symrefs, char *capa)
401 8a29a085 2020-03-18 stsp {
402 8a29a085 2020-03-18 stsp const struct got_error *err = NULL;
403 abe0f35f 2020-03-18 stsp char *colon, *name = NULL, *target = NULL;
404 abe0f35f 2020-03-18 stsp
405 abe0f35f 2020-03-18 stsp /* Need at least "A:B" */
406 abe0f35f 2020-03-18 stsp if (strlen(capa) < 3)
407 abe0f35f 2020-03-18 stsp return NULL;
408 abe0f35f 2020-03-18 stsp
409 abe0f35f 2020-03-18 stsp colon = strchr(capa, ':');
410 abe0f35f 2020-03-18 stsp if (colon == NULL)
411 abe0f35f 2020-03-18 stsp return NULL;
412 abe0f35f 2020-03-18 stsp
413 abe0f35f 2020-03-18 stsp *colon = '\0';
414 abe0f35f 2020-03-18 stsp name = strdup(capa);
415 abe0f35f 2020-03-18 stsp if (name == NULL)
416 abe0f35f 2020-03-18 stsp return got_error_from_errno("strdup");
417 abe0f35f 2020-03-18 stsp
418 abe0f35f 2020-03-18 stsp target = strdup(colon + 1);
419 abe0f35f 2020-03-18 stsp if (target == NULL) {
420 abe0f35f 2020-03-18 stsp err = got_error_from_errno("strdup");
421 abe0f35f 2020-03-18 stsp goto done;
422 abe0f35f 2020-03-18 stsp }
423 abe0f35f 2020-03-18 stsp
424 abe0f35f 2020-03-18 stsp /* We can't validate the ref itself here. The main process will. */
425 abe0f35f 2020-03-18 stsp err = got_pathlist_append(symrefs, name, target);
426 abe0f35f 2020-03-18 stsp done:
427 abe0f35f 2020-03-18 stsp if (err) {
428 abe0f35f 2020-03-18 stsp free(name);
429 abe0f35f 2020-03-18 stsp free(target);
430 abe0f35f 2020-03-18 stsp }
431 abe0f35f 2020-03-18 stsp return err;
432 abe0f35f 2020-03-18 stsp }
433 abe0f35f 2020-03-18 stsp
434 abe0f35f 2020-03-18 stsp static const struct got_error *
435 abe0f35f 2020-03-18 stsp match_capabilities(char **my_capabilities, struct got_pathlist_head *symrefs,
436 abe0f35f 2020-03-18 stsp char *server_capabilities)
437 abe0f35f 2020-03-18 stsp {
438 abe0f35f 2020-03-18 stsp const struct got_error *err = NULL;
439 abe0f35f 2020-03-18 stsp char *capa, *equalsign;
440 6059809a 2020-12-17 stsp size_t i;
441 8a29a085 2020-03-18 stsp
442 8a29a085 2020-03-18 stsp *my_capabilities = NULL;
443 8a29a085 2020-03-18 stsp do {
444 8a29a085 2020-03-18 stsp capa = strsep(&server_capabilities, " ");
445 8a29a085 2020-03-18 stsp if (capa == NULL)
446 8a29a085 2020-03-18 stsp return NULL;
447 8a29a085 2020-03-18 stsp
448 abe0f35f 2020-03-18 stsp equalsign = strchr(capa, '=');
449 abe0f35f 2020-03-18 stsp if (equalsign != NULL &&
450 abe0f35f 2020-03-18 stsp strncmp(capa, "symref", equalsign - capa) == 0) {
451 abe0f35f 2020-03-18 stsp err = add_symref(symrefs, equalsign + 1);
452 abe0f35f 2020-03-18 stsp if (err)
453 abe0f35f 2020-03-18 stsp break;
454 abe0f35f 2020-03-18 stsp continue;
455 abe0f35f 2020-03-18 stsp }
456 abe0f35f 2020-03-18 stsp
457 8a29a085 2020-03-18 stsp for (i = 0; i < nitems(got_capabilities); i++) {
458 8a29a085 2020-03-18 stsp err = match_capability(my_capabilities,
459 8a29a085 2020-03-18 stsp capa, &got_capabilities[i]);
460 8a29a085 2020-03-18 stsp if (err)
461 8a29a085 2020-03-18 stsp break;
462 8a29a085 2020-03-18 stsp }
463 8a29a085 2020-03-18 stsp } while (capa);
464 8a29a085 2020-03-18 stsp
465 406106ee 2020-03-20 stsp if (*my_capabilities == NULL) {
466 406106ee 2020-03-20 stsp *my_capabilities = strdup("");
467 406106ee 2020-03-20 stsp if (*my_capabilities == NULL)
468 406106ee 2020-03-20 stsp err = got_error_from_errno("strdup");
469 406106ee 2020-03-20 stsp }
470 8a29a085 2020-03-18 stsp return err;
471 e70bf110 2020-03-22 stsp }
472 e70bf110 2020-03-22 stsp
473 e70bf110 2020-03-22 stsp static const struct got_error *
474 e70bf110 2020-03-22 stsp send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
475 e70bf110 2020-03-22 stsp {
476 e70bf110 2020-03-22 stsp if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
477 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
478 e70bf110 2020-03-22 stsp
479 e70bf110 2020-03-22 stsp if (msglen == 0)
480 e70bf110 2020-03-22 stsp return NULL;
481 e70bf110 2020-03-22 stsp
482 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
483 e70bf110 2020-03-22 stsp msg, msglen) == -1)
484 e70bf110 2020-03-22 stsp return got_error_from_errno(
485 e70bf110 2020-03-22 stsp "imsg_compose FETCH_SERVER_PROGRESS");
486 e70bf110 2020-03-22 stsp
487 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
488 531c3985 2020-03-18 stsp }
489 531c3985 2020-03-18 stsp
490 531c3985 2020-03-18 stsp static const struct got_error *
491 e70bf110 2020-03-22 stsp send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
492 e70bf110 2020-03-22 stsp {
493 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
494 e70bf110 2020-03-22 stsp &bytes, sizeof(bytes)) == -1)
495 e70bf110 2020-03-22 stsp return got_error_from_errno(
496 e70bf110 2020-03-22 stsp "imsg_compose FETCH_DOWNLOAD_PROGRESS");
497 e70bf110 2020-03-22 stsp
498 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
499 e70bf110 2020-03-22 stsp }
500 e70bf110 2020-03-22 stsp
501 e70bf110 2020-03-22 stsp static const struct got_error *
502 1d72a2a0 2020-03-24 stsp send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
503 e70bf110 2020-03-22 stsp {
504 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
505 1d72a2a0 2020-03-24 stsp pack_sha1, SHA1_DIGEST_LENGTH) == -1)
506 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose FETCH");
507 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
508 e70bf110 2020-03-22 stsp }
509 e70bf110 2020-03-22 stsp
510 e70bf110 2020-03-22 stsp
511 e70bf110 2020-03-22 stsp
512 e70bf110 2020-03-22 stsp static const struct got_error *
513 531c3985 2020-03-18 stsp fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
514 531c3985 2020-03-18 stsp {
515 6059809a 2020-12-17 stsp size_t i;
516 531c3985 2020-03-18 stsp
517 531c3985 2020-03-18 stsp if (len == 0)
518 531c3985 2020-03-18 stsp return NULL;
519 531c3985 2020-03-18 stsp
520 531c3985 2020-03-18 stsp /*
521 531c3985 2020-03-18 stsp * Truncate messages which exceed the maximum imsg payload size.
522 531c3985 2020-03-18 stsp * Server may send up to 64k.
523 531c3985 2020-03-18 stsp */
524 531c3985 2020-03-18 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
525 531c3985 2020-03-18 stsp len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
526 531c3985 2020-03-18 stsp
527 531c3985 2020-03-18 stsp /* Only allow printable ASCII. */
528 531c3985 2020-03-18 stsp for (i = 0; i < len; i++) {
529 531c3985 2020-03-18 stsp if (isprint((unsigned char)buf[i]) ||
530 531c3985 2020-03-18 stsp isspace((unsigned char)buf[i]))
531 531c3985 2020-03-18 stsp continue;
532 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
533 531c3985 2020-03-18 stsp "non-printable progress message received from server");
534 531c3985 2020-03-18 stsp }
535 531c3985 2020-03-18 stsp
536 e70bf110 2020-03-22 stsp return send_fetch_server_progress(ibuf, buf, len);
537 8a29a085 2020-03-18 stsp }
538 abe0f35f 2020-03-18 stsp
539 8a29a085 2020-03-18 stsp static const struct got_error *
540 531c3985 2020-03-18 stsp fetch_error(const char *buf, size_t len)
541 531c3985 2020-03-18 stsp {
542 531c3985 2020-03-18 stsp static char msg[1024];
543 6059809a 2020-12-17 stsp size_t i;
544 531c3985 2020-03-18 stsp
545 531c3985 2020-03-18 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
546 531c3985 2020-03-18 stsp if (!isprint(buf[i]))
547 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
548 531c3985 2020-03-18 stsp "non-printable error message received from server");
549 531c3985 2020-03-18 stsp msg[i] = buf[i];
550 531c3985 2020-03-18 stsp }
551 531c3985 2020-03-18 stsp msg[i] = '\0';
552 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
553 531c3985 2020-03-18 stsp }
554 531c3985 2020-03-18 stsp
555 531c3985 2020-03-18 stsp static const struct got_error *
556 e70bf110 2020-03-22 stsp send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
557 e70bf110 2020-03-22 stsp {
558 e70bf110 2020-03-22 stsp const struct got_error *err = NULL;
559 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
560 e70bf110 2020-03-22 stsp size_t len, nsymrefs = 0;
561 e70bf110 2020-03-22 stsp struct got_pathlist_entry *pe;
562 e70bf110 2020-03-22 stsp
563 e70bf110 2020-03-22 stsp len = sizeof(struct got_imsg_fetch_symrefs);
564 e70bf110 2020-03-22 stsp TAILQ_FOREACH(pe, symrefs, entry) {
565 e70bf110 2020-03-22 stsp const char *target = pe->data;
566 e70bf110 2020-03-22 stsp len += sizeof(struct got_imsg_fetch_symref) +
567 e70bf110 2020-03-22 stsp pe->path_len + strlen(target);
568 e70bf110 2020-03-22 stsp nsymrefs++;
569 e70bf110 2020-03-22 stsp }
570 e70bf110 2020-03-22 stsp
571 e70bf110 2020-03-22 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
572 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
573 e70bf110 2020-03-22 stsp
574 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
575 e70bf110 2020-03-22 stsp if (wbuf == NULL)
576 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create FETCH_SYMREFS");
577 e70bf110 2020-03-22 stsp
578 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
579 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
580 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
581 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
582 e70bf110 2020-03-22 stsp return err;
583 e70bf110 2020-03-22 stsp }
584 e70bf110 2020-03-22 stsp
585 e70bf110 2020-03-22 stsp TAILQ_FOREACH(pe, symrefs, entry) {
586 e70bf110 2020-03-22 stsp const char *name = pe->path;
587 e70bf110 2020-03-22 stsp size_t name_len = pe->path_len;
588 e70bf110 2020-03-22 stsp const char *target = pe->data;
589 e70bf110 2020-03-22 stsp size_t target_len = strlen(target);
590 e70bf110 2020-03-22 stsp
591 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_symref definition! */
592 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
593 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
594 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
595 e70bf110 2020-03-22 stsp return err;
596 e70bf110 2020-03-22 stsp }
597 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
598 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
599 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
600 e70bf110 2020-03-22 stsp return err;
601 e70bf110 2020-03-22 stsp }
602 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, name, name_len) == -1) {
603 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
604 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
605 e70bf110 2020-03-22 stsp return err;
606 e70bf110 2020-03-22 stsp }
607 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, target, target_len) == -1) {
608 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
609 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
610 e70bf110 2020-03-22 stsp return err;
611 e70bf110 2020-03-22 stsp }
612 e70bf110 2020-03-22 stsp }
613 e70bf110 2020-03-22 stsp
614 e70bf110 2020-03-22 stsp wbuf->fd = -1;
615 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
616 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
617 e70bf110 2020-03-22 stsp }
618 e70bf110 2020-03-22 stsp
619 e70bf110 2020-03-22 stsp static const struct got_error *
620 e70bf110 2020-03-22 stsp send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
621 e70bf110 2020-03-22 stsp const char *refname)
622 e70bf110 2020-03-22 stsp {
623 e70bf110 2020-03-22 stsp const struct got_error *err = NULL;
624 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
625 e70bf110 2020-03-22 stsp size_t len, reflen = strlen(refname);
626 e70bf110 2020-03-22 stsp
627 e70bf110 2020-03-22 stsp len = sizeof(struct got_imsg_fetch_ref) + reflen;
628 e70bf110 2020-03-22 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
629 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
630 e70bf110 2020-03-22 stsp
631 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
632 e70bf110 2020-03-22 stsp if (wbuf == NULL)
633 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create FETCH_REF");
634 e70bf110 2020-03-22 stsp
635 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_ref definition! */
636 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
637 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_REF");
638 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
639 e70bf110 2020-03-22 stsp return err;
640 e70bf110 2020-03-22 stsp }
641 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, refname, reflen) == -1) {
642 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_REF");
643 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
644 e70bf110 2020-03-22 stsp return err;
645 e70bf110 2020-03-22 stsp }
646 e70bf110 2020-03-22 stsp
647 e70bf110 2020-03-22 stsp wbuf->fd = -1;
648 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
649 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
650 e70bf110 2020-03-22 stsp }
651 729743d1 2020-03-23 stsp
652 e70bf110 2020-03-22 stsp static const struct got_error *
653 1d72a2a0 2020-03-24 stsp fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
654 659e7fbd 2020-03-20 stsp struct got_pathlist_head *have_refs, int fetch_all_branches,
655 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_branches,
656 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_refs, int list_refs_only,
657 41b0de12 2020-03-21 stsp struct imsgbuf *ibuf)
658 93658fb9 2020-03-18 stsp {
659 9ff10419 2020-03-18 stsp const struct got_error *err = NULL;
660 f1c6967f 2020-03-19 stsp char buf[GOT_FETCH_PKTMAX];
661 93658fb9 2020-03-18 stsp char hashstr[SHA1_DIGEST_STRING_LENGTH];
662 93658fb9 2020-03-18 stsp struct got_object_id *have, *want;
663 8a29a085 2020-03-18 stsp int is_firstpkt = 1, nref = 0, refsz = 16;
664 7848a0e1 2020-03-19 stsp int i, n, nwant = 0, nhave = 0, acked = 0;
665 5672d305 2020-03-18 stsp off_t packsz = 0, last_reported_packsz = 0;
666 00cd0e0a 2020-03-18 stsp char *id_str = NULL, *refname = NULL;
667 00cd0e0a 2020-03-18 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
668 659e7fbd 2020-03-20 stsp const char *default_branch = NULL;
669 abe0f35f 2020-03-18 stsp struct got_pathlist_head symrefs;
670 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
671 406106ee 2020-03-20 stsp int sent_my_capabilites = 0, have_sidebands = 0;
672 659e7fbd 2020-03-20 stsp int found_branch = 0;
673 dc671e91 2020-03-24 stsp SHA1_CTX sha1_ctx;
674 dc671e91 2020-03-24 stsp uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
675 dc671e91 2020-03-24 stsp size_t sha1_buf_len = 0;
676 dc671e91 2020-03-24 stsp ssize_t w;
677 93658fb9 2020-03-18 stsp
678 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
679 dc671e91 2020-03-24 stsp SHA1Init(&sha1_ctx);
680 abe0f35f 2020-03-18 stsp
681 93658fb9 2020-03-18 stsp have = malloc(refsz * sizeof(have[0]));
682 9ff10419 2020-03-18 stsp if (have == NULL)
683 9ff10419 2020-03-18 stsp return got_error_from_errno("malloc");
684 93658fb9 2020-03-18 stsp want = malloc(refsz * sizeof(want[0]));
685 9ff10419 2020-03-18 stsp if (want == NULL) {
686 9ff10419 2020-03-18 stsp err = got_error_from_errno("malloc");
687 9ff10419 2020-03-18 stsp goto done;
688 9ff10419 2020-03-18 stsp }
689 9ff10419 2020-03-18 stsp while (1) {
690 fe53745c 2020-03-18 stsp err = readpkt(&n, fd, buf, sizeof(buf));
691 fe53745c 2020-03-18 stsp if (err)
692 9ff10419 2020-03-18 stsp goto done;
693 9ff10419 2020-03-18 stsp if (n == 0)
694 93658fb9 2020-03-18 stsp break;
695 a6f88e33 2020-03-18 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
696 531c3985 2020-03-18 stsp err = fetch_error(&buf[4], n - 4);
697 9ff10419 2020-03-18 stsp goto done;
698 9ff10419 2020-03-18 stsp }
699 00cd0e0a 2020-03-18 stsp err = parse_refline(&id_str, &refname, &server_capabilities,
700 00cd0e0a 2020-03-18 stsp buf, n);
701 0d0a341c 2020-03-18 stsp if (err)
702 9ff10419 2020-03-18 stsp goto done;
703 8a29a085 2020-03-18 stsp if (is_firstpkt) {
704 858b0dfb 2020-03-20 stsp if (chattygot && server_capabilities[0] != '\0')
705 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: server capabilities: %s\n",
706 858b0dfb 2020-03-20 stsp getprogname(), server_capabilities);
707 abe0f35f 2020-03-18 stsp err = match_capabilities(&my_capabilities, &symrefs,
708 00cd0e0a 2020-03-18 stsp server_capabilities);
709 8a29a085 2020-03-18 stsp if (err)
710 8a29a085 2020-03-18 stsp goto done;
711 858b0dfb 2020-03-20 stsp if (chattygot)
712 2690194b 2020-03-21 stsp fprintf(stderr, "%s: my capabilities:%s\n",
713 a90356f7 2021-08-26 stsp getprogname(), my_capabilities != NULL ?
714 a90356f7 2021-08-26 stsp my_capabilities : "");
715 e70bf110 2020-03-22 stsp err = send_fetch_symrefs(ibuf, &symrefs);
716 abe0f35f 2020-03-18 stsp if (err)
717 abe0f35f 2020-03-18 stsp goto done;
718 7848a0e1 2020-03-19 stsp is_firstpkt = 0;
719 659e7fbd 2020-03-20 stsp if (!fetch_all_branches) {
720 659e7fbd 2020-03-20 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
721 659e7fbd 2020-03-20 stsp const char *name = pe->path;
722 659e7fbd 2020-03-20 stsp const char *symref_target = pe->data;
723 659e7fbd 2020-03-20 stsp if (strcmp(name, GOT_REF_HEAD) != 0)
724 659e7fbd 2020-03-20 stsp continue;
725 659e7fbd 2020-03-20 stsp default_branch = symref_target;
726 659e7fbd 2020-03-20 stsp break;
727 659e7fbd 2020-03-20 stsp }
728 659e7fbd 2020-03-20 stsp }
729 7848a0e1 2020-03-19 stsp continue;
730 8a29a085 2020-03-18 stsp }
731 2690194b 2020-03-21 stsp if (strstr(refname, "^{}")) {
732 2690194b 2020-03-21 stsp if (chattygot) {
733 2690194b 2020-03-21 stsp fprintf(stderr, "%s: ignoring %s\n",
734 2690194b 2020-03-21 stsp getprogname(), refname);
735 2690194b 2020-03-21 stsp }
736 93658fb9 2020-03-18 stsp continue;
737 2690194b 2020-03-21 stsp }
738 659e7fbd 2020-03-20 stsp
739 659e7fbd 2020-03-20 stsp if (strncmp(refname, "refs/heads/", 11) == 0) {
740 41b0de12 2020-03-21 stsp if (fetch_all_branches || list_refs_only) {
741 4ba14133 2020-03-20 stsp found_branch = 1;
742 4ba14133 2020-03-20 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
743 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
744 4ba14133 2020-03-20 stsp if (match_branch(refname, pe->path))
745 4ba14133 2020-03-20 stsp break;
746 4ba14133 2020-03-20 stsp }
747 2690194b 2020-03-21 stsp if (pe == NULL) {
748 2690194b 2020-03-21 stsp if (chattygot) {
749 2690194b 2020-03-21 stsp fprintf(stderr,
750 2690194b 2020-03-21 stsp "%s: ignoring %s\n",
751 2690194b 2020-03-21 stsp getprogname(), refname);
752 2690194b 2020-03-21 stsp }
753 4ba14133 2020-03-20 stsp continue;
754 2690194b 2020-03-21 stsp }
755 4ba14133 2020-03-20 stsp found_branch = 1;
756 4ba14133 2020-03-20 stsp } else if (default_branch != NULL) {
757 2690194b 2020-03-21 stsp if (!match_branch(refname, default_branch)) {
758 2690194b 2020-03-21 stsp if (chattygot) {
759 2690194b 2020-03-21 stsp fprintf(stderr,
760 2690194b 2020-03-21 stsp "%s: ignoring %s\n",
761 2690194b 2020-03-21 stsp getprogname(), refname);
762 2690194b 2020-03-21 stsp }
763 4ba14133 2020-03-20 stsp continue;
764 2690194b 2020-03-21 stsp }
765 4ba14133 2020-03-20 stsp found_branch = 1;
766 4ba14133 2020-03-20 stsp }
767 659e7fbd 2020-03-20 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0) {
768 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(wanted_refs)) {
769 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
770 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
771 0e4002ca 2020-03-21 stsp break;
772 0e4002ca 2020-03-21 stsp }
773 0e4002ca 2020-03-21 stsp if (pe == NULL) {
774 0e4002ca 2020-03-21 stsp if (chattygot) {
775 0e4002ca 2020-03-21 stsp fprintf(stderr,
776 0e4002ca 2020-03-21 stsp "%s: ignoring %s\n",
777 0e4002ca 2020-03-21 stsp getprogname(), refname);
778 0e4002ca 2020-03-21 stsp }
779 0e4002ca 2020-03-21 stsp continue;
780 0e4002ca 2020-03-21 stsp }
781 0e4002ca 2020-03-21 stsp found_branch = 1;
782 0e4002ca 2020-03-21 stsp } else if (!list_refs_only) {
783 2690194b 2020-03-21 stsp if (chattygot) {
784 2690194b 2020-03-21 stsp fprintf(stderr, "%s: ignoring %s\n",
785 2690194b 2020-03-21 stsp getprogname(), refname);
786 2690194b 2020-03-21 stsp }
787 4515a796 2020-03-21 stsp continue;
788 2690194b 2020-03-21 stsp }
789 659e7fbd 2020-03-20 stsp }
790 659e7fbd 2020-03-20 stsp
791 cf875574 2020-03-18 stsp if (refsz == nref + 1) {
792 93658fb9 2020-03-18 stsp refsz *= 2;
793 14778466 2020-03-18 stsp have = reallocarray(have, refsz, sizeof(have[0]));
794 9ff10419 2020-03-18 stsp if (have == NULL) {
795 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
796 9ff10419 2020-03-18 stsp goto done;
797 9ff10419 2020-03-18 stsp }
798 14778466 2020-03-18 stsp want = reallocarray(want, refsz, sizeof(want[0]));
799 9ff10419 2020-03-18 stsp if (want == NULL) {
800 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
801 9ff10419 2020-03-18 stsp goto done;
802 9ff10419 2020-03-18 stsp }
803 93658fb9 2020-03-18 stsp }
804 00cd0e0a 2020-03-18 stsp if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
805 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
806 9ff10419 2020-03-18 stsp goto done;
807 9ff10419 2020-03-18 stsp }
808 7848a0e1 2020-03-19 stsp match_remote_ref(have_refs, &have[nref], refname);
809 e70bf110 2020-03-22 stsp err = send_fetch_ref(ibuf, &want[nref], refname);
810 8f2d01a6 2020-03-18 stsp if (err)
811 8f2d01a6 2020-03-18 stsp goto done;
812 858b0dfb 2020-03-20 stsp
813 2690194b 2020-03-21 stsp if (chattygot)
814 2690194b 2020-03-21 stsp fprintf(stderr, "%s: %s will be fetched\n",
815 2690194b 2020-03-21 stsp getprogname(), refname);
816 2690194b 2020-03-21 stsp if (chattygot > 1) {
817 858b0dfb 2020-03-20 stsp char *theirs, *mine;
818 858b0dfb 2020-03-20 stsp err = got_object_id_str(&theirs, &want[nref]);
819 858b0dfb 2020-03-20 stsp if (err)
820 858b0dfb 2020-03-20 stsp goto done;
821 858b0dfb 2020-03-20 stsp err = got_object_id_str(&mine, &have[nref]);
822 858b0dfb 2020-03-20 stsp if (err) {
823 858b0dfb 2020-03-20 stsp free(theirs);
824 858b0dfb 2020-03-20 stsp goto done;
825 858b0dfb 2020-03-20 stsp }
826 2690194b 2020-03-21 stsp fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
827 659e7fbd 2020-03-20 stsp getprogname(), theirs, getprogname(), mine);
828 858b0dfb 2020-03-20 stsp free(theirs);
829 858b0dfb 2020-03-20 stsp free(mine);
830 858b0dfb 2020-03-20 stsp }
831 93658fb9 2020-03-18 stsp nref++;
832 93658fb9 2020-03-18 stsp }
833 93658fb9 2020-03-18 stsp
834 41b0de12 2020-03-21 stsp if (list_refs_only)
835 41b0de12 2020-03-21 stsp goto done;
836 41b0de12 2020-03-21 stsp
837 659e7fbd 2020-03-20 stsp /* Abort if we haven't found any branch to fetch. */
838 659e7fbd 2020-03-20 stsp if (!found_branch) {
839 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_FETCH_NO_BRANCH);
840 659e7fbd 2020-03-20 stsp goto done;
841 659e7fbd 2020-03-20 stsp }
842 659e7fbd 2020-03-20 stsp
843 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
844 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
845 93658fb9 2020-03-18 stsp continue;
846 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
847 406106ee 2020-03-20 stsp n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
848 a90356f7 2021-08-26 stsp sent_my_capabilites || my_capabilities == NULL ?
849 a90356f7 2021-08-26 stsp "" : my_capabilities);
850 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
851 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
852 9ff10419 2020-03-18 stsp goto done;
853 9ff10419 2020-03-18 stsp }
854 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
855 344e4747 2020-03-18 stsp if (err)
856 9ff10419 2020-03-18 stsp goto done;
857 858b0dfb 2020-03-20 stsp sent_my_capabilites = 1;
858 7848a0e1 2020-03-19 stsp nwant++;
859 93658fb9 2020-03-18 stsp }
860 38c670f1 2020-03-18 stsp err = flushpkt(fd);
861 38c670f1 2020-03-18 stsp if (err)
862 38c670f1 2020-03-18 stsp goto done;
863 7848a0e1 2020-03-19 stsp
864 3c912d14 2020-03-19 stsp if (nwant == 0)
865 7848a0e1 2020-03-19 stsp goto done;
866 7848a0e1 2020-03-19 stsp
867 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
868 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &zhash) == 0)
869 93658fb9 2020-03-18 stsp continue;
870 7848a0e1 2020-03-19 stsp got_sha1_digest_to_str(have[i].sha1, hashstr, sizeof(hashstr));
871 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
872 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
873 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
874 9ff10419 2020-03-18 stsp goto done;
875 9ff10419 2020-03-18 stsp }
876 c20695fb 2020-03-20 stsp err = writepkt(fd, buf, n);
877 344e4747 2020-03-18 stsp if (err)
878 9ff10419 2020-03-18 stsp goto done;
879 7848a0e1 2020-03-19 stsp nhave++;
880 93658fb9 2020-03-18 stsp }
881 7848a0e1 2020-03-19 stsp
882 7848a0e1 2020-03-19 stsp while (nhave > 0 && !acked) {
883 7848a0e1 2020-03-19 stsp struct got_object_id common_id;
884 7848a0e1 2020-03-19 stsp
885 7848a0e1 2020-03-19 stsp /* The server should ACK the object IDs we need. */
886 7848a0e1 2020-03-19 stsp err = readpkt(&n, fd, buf, sizeof(buf));
887 38c670f1 2020-03-18 stsp if (err)
888 38c670f1 2020-03-18 stsp goto done;
889 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
890 7848a0e1 2020-03-19 stsp err = fetch_error(&buf[4], n - 4);
891 7848a0e1 2020-03-19 stsp goto done;
892 7848a0e1 2020-03-19 stsp }
893 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
894 7848a0e1 2020-03-19 stsp /* Server has not located our objects yet. */
895 7848a0e1 2020-03-19 stsp continue;
896 7848a0e1 2020-03-19 stsp }
897 7848a0e1 2020-03-19 stsp if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
898 7848a0e1 2020-03-19 stsp strncmp(buf, "ACK ", 4) != 0) {
899 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
900 7848a0e1 2020-03-19 stsp "unexpected message from server");
901 7848a0e1 2020-03-19 stsp goto done;
902 7848a0e1 2020-03-19 stsp }
903 7848a0e1 2020-03-19 stsp if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
904 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
905 7848a0e1 2020-03-19 stsp "bad object ID in ACK packet from server");
906 7848a0e1 2020-03-19 stsp goto done;
907 7848a0e1 2020-03-19 stsp }
908 7848a0e1 2020-03-19 stsp acked++;
909 93658fb9 2020-03-18 stsp }
910 7848a0e1 2020-03-19 stsp
911 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "done\n");
912 344e4747 2020-03-18 stsp err = writepkt(fd, buf, n);
913 344e4747 2020-03-18 stsp if (err)
914 9ff10419 2020-03-18 stsp goto done;
915 93658fb9 2020-03-18 stsp
916 7848a0e1 2020-03-19 stsp if (nhave == 0) {
917 7848a0e1 2020-03-19 stsp err = readpkt(&n, fd, buf, sizeof(buf));
918 7848a0e1 2020-03-19 stsp if (err)
919 7848a0e1 2020-03-19 stsp goto done;
920 7848a0e1 2020-03-19 stsp if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
921 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
922 7848a0e1 2020-03-19 stsp "unexpected message from server");
923 7848a0e1 2020-03-19 stsp goto done;
924 7848a0e1 2020-03-19 stsp }
925 04c53c18 2020-03-18 stsp }
926 93658fb9 2020-03-18 stsp
927 858b0dfb 2020-03-20 stsp if (chattygot)
928 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: fetching...\n", getprogname());
929 858b0dfb 2020-03-20 stsp
930 531c3985 2020-03-18 stsp if (my_capabilities != NULL &&
931 531c3985 2020-03-18 stsp strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
932 531c3985 2020-03-18 stsp have_sidebands = 1;
933 531c3985 2020-03-18 stsp
934 9ff10419 2020-03-18 stsp while (1) {
935 dc671e91 2020-03-24 stsp ssize_t r = 0;
936 531c3985 2020-03-18 stsp int datalen = -1;
937 531c3985 2020-03-18 stsp
938 531c3985 2020-03-18 stsp if (have_sidebands) {
939 531c3985 2020-03-18 stsp err = read_pkthdr(&datalen, fd);
940 531c3985 2020-03-18 stsp if (err)
941 531c3985 2020-03-18 stsp goto done;
942 531c3985 2020-03-18 stsp if (datalen <= 0)
943 531c3985 2020-03-18 stsp break;
944 531c3985 2020-03-18 stsp
945 531c3985 2020-03-18 stsp /* Read sideband channel ID (one byte). */
946 531c3985 2020-03-18 stsp r = read(fd, buf, 1);
947 531c3985 2020-03-18 stsp if (r == -1) {
948 531c3985 2020-03-18 stsp err = got_error_from_errno("read");
949 531c3985 2020-03-18 stsp goto done;
950 531c3985 2020-03-18 stsp }
951 531c3985 2020-03-18 stsp if (r != 1) {
952 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
953 531c3985 2020-03-18 stsp "short packet");
954 531c3985 2020-03-18 stsp goto done;
955 531c3985 2020-03-18 stsp }
956 531c3985 2020-03-18 stsp if (datalen > sizeof(buf) - 5) {
957 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
958 531c3985 2020-03-18 stsp "bad packet length");
959 531c3985 2020-03-18 stsp goto done;
960 531c3985 2020-03-18 stsp }
961 531c3985 2020-03-18 stsp datalen--; /* sideband ID has been read */
962 531c3985 2020-03-18 stsp if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
963 531c3985 2020-03-18 stsp /* Read packfile data. */
964 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
965 531c3985 2020-03-18 stsp if (err)
966 531c3985 2020-03-18 stsp goto done;
967 531c3985 2020-03-18 stsp if (r != datalen) {
968 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
969 531c3985 2020-03-18 stsp "packet too short");
970 531c3985 2020-03-18 stsp goto done;
971 531c3985 2020-03-18 stsp }
972 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
973 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
974 531c3985 2020-03-18 stsp if (err)
975 531c3985 2020-03-18 stsp goto done;
976 531c3985 2020-03-18 stsp if (r != datalen) {
977 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
978 531c3985 2020-03-18 stsp "packet too short");
979 531c3985 2020-03-18 stsp goto done;
980 531c3985 2020-03-18 stsp }
981 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
982 531c3985 2020-03-18 stsp if (err)
983 531c3985 2020-03-18 stsp goto done;
984 531c3985 2020-03-18 stsp continue;
985 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
986 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, datalen);
987 531c3985 2020-03-18 stsp if (err)
988 531c3985 2020-03-18 stsp goto done;
989 531c3985 2020-03-18 stsp if (r != datalen) {
990 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
991 531c3985 2020-03-18 stsp "packet too short");
992 531c3985 2020-03-18 stsp goto done;
993 531c3985 2020-03-18 stsp }
994 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
995 531c3985 2020-03-18 stsp goto done;
996 98f64f14 2021-01-05 stsp } else if (buf[0] == 'A') {
997 98f64f14 2021-01-05 stsp err = readn(&r, fd, buf, datalen);
998 98f64f14 2021-01-05 stsp if (err)
999 98f64f14 2021-01-05 stsp goto done;
1000 98f64f14 2021-01-05 stsp if (r != datalen) {
1001 98f64f14 2021-01-05 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
1002 98f64f14 2021-01-05 stsp "packet too short");
1003 98f64f14 2021-01-05 stsp goto done;
1004 98f64f14 2021-01-05 stsp }
1005 98f64f14 2021-01-05 stsp /*
1006 98f64f14 2021-01-05 stsp * Git server responds with ACK after 'done'
1007 98f64f14 2021-01-05 stsp * even though multi_ack is disabled?!?
1008 98f64f14 2021-01-05 stsp */
1009 98f64f14 2021-01-05 stsp buf[r] = '\0';
1010 98f64f14 2021-01-05 stsp if (strncmp(buf, "CK ", 3) == 0)
1011 98f64f14 2021-01-05 stsp continue; /* ignore */
1012 98f64f14 2021-01-05 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
1013 98f64f14 2021-01-05 stsp "unexpected message from server");
1014 98f64f14 2021-01-05 stsp goto done;
1015 531c3985 2020-03-18 stsp } else {
1016 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
1017 531c3985 2020-03-18 stsp "unknown side-band received from server");
1018 531c3985 2020-03-18 stsp goto done;
1019 531c3985 2020-03-18 stsp }
1020 531c3985 2020-03-18 stsp } else {
1021 531c3985 2020-03-18 stsp /* No sideband channel. Every byte is packfile data. */
1022 531c3985 2020-03-18 stsp err = readn(&r, fd, buf, sizeof buf);
1023 531c3985 2020-03-18 stsp if (err)
1024 531c3985 2020-03-18 stsp goto done;
1025 531c3985 2020-03-18 stsp if (r <= 0)
1026 531c3985 2020-03-18 stsp break;
1027 531c3985 2020-03-18 stsp }
1028 dc671e91 2020-03-24 stsp
1029 dc671e91 2020-03-24 stsp /*
1030 dc671e91 2020-03-24 stsp * An expected SHA1 checksum sits at the end of the pack file.
1031 dc671e91 2020-03-24 stsp * Since we don't know the file size ahead of time we have to
1032 dc671e91 2020-03-24 stsp * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
1033 dc671e91 2020-03-24 stsp * those bytes into our SHA1 checksum computation until we
1034 dc671e91 2020-03-24 stsp * know for sure that additional pack file data bytes follow.
1035 dc671e91 2020-03-24 stsp *
1036 dc671e91 2020-03-24 stsp * We can assume r > 0 since otherwise the loop would exit.
1037 dc671e91 2020-03-24 stsp */
1038 dc671e91 2020-03-24 stsp if (r < SHA1_DIGEST_LENGTH) {
1039 dc671e91 2020-03-24 stsp if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
1040 dc671e91 2020-03-24 stsp /*
1041 dc671e91 2020-03-24 stsp * If there's enough buffered + read data to
1042 dc671e91 2020-03-24 stsp * fill up the buffer then shift a sufficient
1043 dc671e91 2020-03-24 stsp * amount of bytes out at the front to make
1044 dc671e91 2020-03-24 stsp * room, mixing those bytes into the checksum.
1045 dc671e91 2020-03-24 stsp */
1046 dc671e91 2020-03-24 stsp while (sha1_buf_len > 0 &&
1047 dc671e91 2020-03-24 stsp sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
1048 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, sha1_buf, 1);
1049 dc671e91 2020-03-24 stsp memmove(sha1_buf, sha1_buf + 1, 1);
1050 dc671e91 2020-03-24 stsp sha1_buf_len--;
1051 dc671e91 2020-03-24 stsp }
1052 dc671e91 2020-03-24 stsp
1053 dc671e91 2020-03-24 stsp /* Buffer potential checksum bytes. */
1054 dc671e91 2020-03-24 stsp memcpy(sha1_buf + sha1_buf_len, buf, r);
1055 dc671e91 2020-03-24 stsp sha1_buf_len += r;
1056 dc671e91 2020-03-24 stsp } else {
1057 dc671e91 2020-03-24 stsp /*
1058 dc671e91 2020-03-24 stsp * Mix in previously buffered bytes which
1059 dc671e91 2020-03-24 stsp * are not part of the checksum after all.
1060 dc671e91 2020-03-24 stsp */
1061 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, sha1_buf, r);
1062 531c3985 2020-03-18 stsp
1063 dc671e91 2020-03-24 stsp /* Update potential checksum buffer. */
1064 dc671e91 2020-03-24 stsp memmove(sha1_buf, sha1_buf + r,
1065 dc671e91 2020-03-24 stsp sha1_buf_len - r);
1066 dc671e91 2020-03-24 stsp memcpy(sha1_buf + sha1_buf_len - r, buf, r);
1067 dc671e91 2020-03-24 stsp }
1068 dc671e91 2020-03-24 stsp } else {
1069 dc671e91 2020-03-24 stsp /* Mix in any previously buffered bytes. */
1070 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
1071 dc671e91 2020-03-24 stsp
1072 dc671e91 2020-03-24 stsp /* Mix in bytes read minus potential checksum bytes. */
1073 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
1074 dc671e91 2020-03-24 stsp
1075 dc671e91 2020-03-24 stsp /* Buffer potential checksum bytes. */
1076 dc671e91 2020-03-24 stsp memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
1077 dc671e91 2020-03-24 stsp SHA1_DIGEST_LENGTH);
1078 dc671e91 2020-03-24 stsp sha1_buf_len = SHA1_DIGEST_LENGTH;
1079 dc671e91 2020-03-24 stsp }
1080 3168e5da 2020-09-10 stsp
1081 531c3985 2020-03-18 stsp /* Write packfile data to temporary pack file. */
1082 fe53745c 2020-03-18 stsp w = write(packfd, buf, r);
1083 9ff10419 2020-03-18 stsp if (w == -1) {
1084 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
1085 9ff10419 2020-03-18 stsp goto done;
1086 9ff10419 2020-03-18 stsp }
1087 fe53745c 2020-03-18 stsp if (w != r) {
1088 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
1089 9ff10419 2020-03-18 stsp goto done;
1090 9ff10419 2020-03-18 stsp }
1091 531c3985 2020-03-18 stsp packsz += w;
1092 5672d305 2020-03-18 stsp
1093 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
1094 5672d305 2020-03-18 stsp if (packsz > last_reported_packsz + 1024) {
1095 e70bf110 2020-03-22 stsp err = send_fetch_download_progress(ibuf, packsz);
1096 5672d305 2020-03-18 stsp if (err)
1097 5672d305 2020-03-18 stsp goto done;
1098 5672d305 2020-03-18 stsp last_reported_packsz = packsz;
1099 5672d305 2020-03-18 stsp }
1100 93658fb9 2020-03-18 stsp }
1101 e70bf110 2020-03-22 stsp err = send_fetch_download_progress(ibuf, packsz);
1102 5672d305 2020-03-18 stsp if (err)
1103 5672d305 2020-03-18 stsp goto done;
1104 dc671e91 2020-03-24 stsp
1105 dc671e91 2020-03-24 stsp SHA1Final(pack_sha1, &sha1_ctx);
1106 dc671e91 2020-03-24 stsp if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
1107 dc671e91 2020-03-24 stsp memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
1108 dc671e91 2020-03-24 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
1109 dc671e91 2020-03-24 stsp "pack file checksum mismatch");
1110 dc671e91 2020-03-24 stsp }
1111 9ff10419 2020-03-18 stsp done:
1112 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1113 abe0f35f 2020-03-18 stsp free((void *)pe->path);
1114 abe0f35f 2020-03-18 stsp free(pe->data);
1115 abe0f35f 2020-03-18 stsp }
1116 abe0f35f 2020-03-18 stsp got_pathlist_free(&symrefs);
1117 9ff10419 2020-03-18 stsp free(have);
1118 9ff10419 2020-03-18 stsp free(want);
1119 00cd0e0a 2020-03-18 stsp free(id_str);
1120 00cd0e0a 2020-03-18 stsp free(refname);
1121 00cd0e0a 2020-03-18 stsp free(server_capabilities);
1122 8f2d01a6 2020-03-18 stsp return err;
1123 93658fb9 2020-03-18 stsp }
1124 93658fb9 2020-03-18 stsp
1125 93658fb9 2020-03-18 stsp
1126 93658fb9 2020-03-18 stsp int
1127 93658fb9 2020-03-18 stsp main(int argc, char **argv)
1128 93658fb9 2020-03-18 stsp {
1129 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
1130 7848a0e1 2020-03-19 stsp int fetchfd, packfd = -1, i;
1131 1d72a2a0 2020-03-24 stsp uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
1132 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
1133 93658fb9 2020-03-18 stsp struct imsg imsg;
1134 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
1135 4ba14133 2020-03-20 stsp struct got_pathlist_head wanted_branches;
1136 0e4002ca 2020-03-21 stsp struct got_pathlist_head wanted_refs;
1137 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1138 4ba14133 2020-03-20 stsp struct got_imsg_fetch_request fetch_req;
1139 659e7fbd 2020-03-20 stsp struct got_imsg_fetch_have_ref href;
1140 4ba14133 2020-03-20 stsp struct got_imsg_fetch_wanted_branch wbranch;
1141 0e4002ca 2020-03-21 stsp struct got_imsg_fetch_wanted_ref wref;
1142 4ba14133 2020-03-20 stsp size_t datalen;
1143 7848a0e1 2020-03-19 stsp #if 0
1144 7848a0e1 2020-03-19 stsp static int attached;
1145 7848a0e1 2020-03-19 stsp while (!attached)
1146 7848a0e1 2020-03-19 stsp sleep (1);
1147 7848a0e1 2020-03-19 stsp #endif
1148 33501562 2020-03-18 stsp
1149 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
1150 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1151 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1152 858b0dfb 2020-03-20 stsp
1153 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1154 ffb5f621 2020-03-18 stsp #ifndef PROFILE
1155 ffb5f621 2020-03-18 stsp /* revoke access to most system calls */
1156 ffb5f621 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
1157 ffb5f621 2020-03-18 stsp err = got_error_from_errno("pledge");
1158 ffb5f621 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1159 ffb5f621 2020-03-18 stsp return 1;
1160 ffb5f621 2020-03-18 stsp }
1161 ffb5f621 2020-03-18 stsp #endif
1162 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1163 9ca26ac3 2021-08-06 stsp if (err) {
1164 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1165 93658fb9 2020-03-18 stsp err = NULL;
1166 93658fb9 2020-03-18 stsp goto done;
1167 93658fb9 2020-03-18 stsp }
1168 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1169 93658fb9 2020-03-18 stsp goto done;
1170 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
1171 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1172 93658fb9 2020-03-18 stsp goto done;
1173 93658fb9 2020-03-18 stsp }
1174 33501562 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1175 4ba14133 2020-03-20 stsp if (datalen < sizeof(fetch_req)) {
1176 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1177 93658fb9 2020-03-18 stsp goto done;
1178 93658fb9 2020-03-18 stsp }
1179 4ba14133 2020-03-20 stsp memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
1180 4ba14133 2020-03-20 stsp fetchfd = imsg.fd;
1181 4ba14133 2020-03-20 stsp imsg_free(&imsg);
1182 4ba14133 2020-03-20 stsp
1183 2690194b 2020-03-21 stsp if (fetch_req.verbosity > 0)
1184 2690194b 2020-03-21 stsp chattygot += fetch_req.verbosity;
1185 2690194b 2020-03-21 stsp
1186 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_have_refs; i++) {
1187 7848a0e1 2020-03-19 stsp struct got_object_id *id;
1188 7848a0e1 2020-03-19 stsp char *refname;
1189 7848a0e1 2020-03-19 stsp
1190 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1191 9ca26ac3 2021-08-06 stsp if (err) {
1192 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1193 4ba14133 2020-03-20 stsp err = NULL;
1194 4ba14133 2020-03-20 stsp goto done;
1195 4ba14133 2020-03-20 stsp }
1196 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1197 4ba14133 2020-03-20 stsp goto done;
1198 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
1199 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1200 4ba14133 2020-03-20 stsp goto done;
1201 4ba14133 2020-03-20 stsp }
1202 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1203 4ba14133 2020-03-20 stsp if (datalen < sizeof(href)) {
1204 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1205 659e7fbd 2020-03-20 stsp goto done;
1206 659e7fbd 2020-03-20 stsp }
1207 4ba14133 2020-03-20 stsp memcpy(&href, imsg.data, sizeof(href));
1208 4ba14133 2020-03-20 stsp if (datalen - sizeof(href) < href.name_len) {
1209 7848a0e1 2020-03-19 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1210 7848a0e1 2020-03-19 stsp goto done;
1211 7848a0e1 2020-03-19 stsp }
1212 659e7fbd 2020-03-20 stsp refname = malloc(href.name_len + 1);
1213 7848a0e1 2020-03-19 stsp if (refname == NULL) {
1214 659e7fbd 2020-03-20 stsp err = got_error_from_errno("malloc");
1215 7848a0e1 2020-03-19 stsp goto done;
1216 7848a0e1 2020-03-19 stsp }
1217 4ba14133 2020-03-20 stsp memcpy(refname, imsg.data + sizeof(href), href.name_len);
1218 659e7fbd 2020-03-20 stsp refname[href.name_len] = '\0';
1219 659e7fbd 2020-03-20 stsp
1220 7848a0e1 2020-03-19 stsp id = malloc(sizeof(*id));
1221 7848a0e1 2020-03-19 stsp if (id == NULL) {
1222 7848a0e1 2020-03-19 stsp free(refname);
1223 7848a0e1 2020-03-19 stsp err = got_error_from_errno("malloc");
1224 7848a0e1 2020-03-19 stsp goto done;
1225 7848a0e1 2020-03-19 stsp }
1226 659e7fbd 2020-03-20 stsp memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
1227 7848a0e1 2020-03-19 stsp err = got_pathlist_append(&have_refs, refname, id);
1228 7848a0e1 2020-03-19 stsp if (err) {
1229 7848a0e1 2020-03-19 stsp free(refname);
1230 7848a0e1 2020-03-19 stsp free(id);
1231 7848a0e1 2020-03-19 stsp goto done;
1232 7848a0e1 2020-03-19 stsp }
1233 4ba14133 2020-03-20 stsp
1234 4ba14133 2020-03-20 stsp imsg_free(&imsg);
1235 659e7fbd 2020-03-20 stsp }
1236 93658fb9 2020-03-18 stsp
1237 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_wanted_branches; i++) {
1238 4ba14133 2020-03-20 stsp char *refname;
1239 4ba14133 2020-03-20 stsp
1240 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1241 9ca26ac3 2021-08-06 stsp if (err) {
1242 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1243 4ba14133 2020-03-20 stsp err = NULL;
1244 4ba14133 2020-03-20 stsp goto done;
1245 4ba14133 2020-03-20 stsp }
1246 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1247 4ba14133 2020-03-20 stsp goto done;
1248 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
1249 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1250 4ba14133 2020-03-20 stsp goto done;
1251 4ba14133 2020-03-20 stsp }
1252 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1253 4ba14133 2020-03-20 stsp if (datalen < sizeof(wbranch)) {
1254 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1255 4ba14133 2020-03-20 stsp goto done;
1256 4ba14133 2020-03-20 stsp }
1257 4ba14133 2020-03-20 stsp memcpy(&wbranch, imsg.data, sizeof(wbranch));
1258 4ba14133 2020-03-20 stsp if (datalen - sizeof(wbranch) < wbranch.name_len) {
1259 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1260 4ba14133 2020-03-20 stsp goto done;
1261 4ba14133 2020-03-20 stsp }
1262 4ba14133 2020-03-20 stsp refname = malloc(wbranch.name_len + 1);
1263 4ba14133 2020-03-20 stsp if (refname == NULL) {
1264 4ba14133 2020-03-20 stsp err = got_error_from_errno("malloc");
1265 4ba14133 2020-03-20 stsp goto done;
1266 4ba14133 2020-03-20 stsp }
1267 4ba14133 2020-03-20 stsp memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
1268 4ba14133 2020-03-20 stsp refname[wbranch.name_len] = '\0';
1269 4ba14133 2020-03-20 stsp
1270 4ba14133 2020-03-20 stsp err = got_pathlist_append(&wanted_branches, refname, NULL);
1271 4ba14133 2020-03-20 stsp if (err) {
1272 4ba14133 2020-03-20 stsp free(refname);
1273 4ba14133 2020-03-20 stsp goto done;
1274 4ba14133 2020-03-20 stsp }
1275 4ba14133 2020-03-20 stsp
1276 4ba14133 2020-03-20 stsp imsg_free(&imsg);
1277 4ba14133 2020-03-20 stsp }
1278 4ba14133 2020-03-20 stsp
1279 0e4002ca 2020-03-21 stsp for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1280 0e4002ca 2020-03-21 stsp char *refname;
1281 0e4002ca 2020-03-21 stsp
1282 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1283 9ca26ac3 2021-08-06 stsp if (err) {
1284 0e4002ca 2020-03-21 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1285 0e4002ca 2020-03-21 stsp err = NULL;
1286 0e4002ca 2020-03-21 stsp goto done;
1287 0e4002ca 2020-03-21 stsp }
1288 0e4002ca 2020-03-21 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1289 0e4002ca 2020-03-21 stsp goto done;
1290 0e4002ca 2020-03-21 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1291 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1292 0e4002ca 2020-03-21 stsp goto done;
1293 0e4002ca 2020-03-21 stsp }
1294 0e4002ca 2020-03-21 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1295 0e4002ca 2020-03-21 stsp if (datalen < sizeof(wref)) {
1296 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1297 0e4002ca 2020-03-21 stsp goto done;
1298 0e4002ca 2020-03-21 stsp }
1299 0e4002ca 2020-03-21 stsp memcpy(&wref, imsg.data, sizeof(wref));
1300 0e4002ca 2020-03-21 stsp if (datalen - sizeof(wref) < wref.name_len) {
1301 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1302 0e4002ca 2020-03-21 stsp goto done;
1303 0e4002ca 2020-03-21 stsp }
1304 0e4002ca 2020-03-21 stsp refname = malloc(wref.name_len + 1);
1305 0e4002ca 2020-03-21 stsp if (refname == NULL) {
1306 0e4002ca 2020-03-21 stsp err = got_error_from_errno("malloc");
1307 0e4002ca 2020-03-21 stsp goto done;
1308 0e4002ca 2020-03-21 stsp }
1309 0e4002ca 2020-03-21 stsp memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
1310 0e4002ca 2020-03-21 stsp refname[wref.name_len] = '\0';
1311 0e4002ca 2020-03-21 stsp
1312 0e4002ca 2020-03-21 stsp err = got_pathlist_append(&wanted_refs, refname, NULL);
1313 0e4002ca 2020-03-21 stsp if (err) {
1314 0e4002ca 2020-03-21 stsp free(refname);
1315 0e4002ca 2020-03-21 stsp goto done;
1316 0e4002ca 2020-03-21 stsp }
1317 0e4002ca 2020-03-21 stsp
1318 0e4002ca 2020-03-21 stsp imsg_free(&imsg);
1319 0e4002ca 2020-03-21 stsp }
1320 0e4002ca 2020-03-21 stsp
1321 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1322 9ca26ac3 2021-08-06 stsp if (err) {
1323 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1324 93658fb9 2020-03-18 stsp err = NULL;
1325 93658fb9 2020-03-18 stsp goto done;
1326 93658fb9 2020-03-18 stsp }
1327 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1328 93658fb9 2020-03-18 stsp goto done;
1329 f826addf 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1330 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1331 93658fb9 2020-03-18 stsp goto done;
1332 93658fb9 2020-03-18 stsp }
1333 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1334 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1335 93658fb9 2020-03-18 stsp goto done;
1336 93658fb9 2020-03-18 stsp }
1337 93658fb9 2020-03-18 stsp packfd = imsg.fd;
1338 93658fb9 2020-03-18 stsp
1339 1d72a2a0 2020-03-24 stsp err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1340 41b0de12 2020-03-21 stsp fetch_req.fetch_all_branches, &wanted_branches,
1341 0e4002ca 2020-03-21 stsp &wanted_refs, fetch_req.list_refs_only, &ibuf);
1342 93658fb9 2020-03-18 stsp done:
1343 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &have_refs, entry) {
1344 7848a0e1 2020-03-19 stsp free((char *)pe->path);
1345 7848a0e1 2020-03-19 stsp free(pe->data);
1346 7848a0e1 2020-03-19 stsp }
1347 7848a0e1 2020-03-19 stsp got_pathlist_free(&have_refs);
1348 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry)
1349 4ba14133 2020-03-20 stsp free((char *)pe->path);
1350 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1351 0bec957e 2020-03-21 stsp if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1352 0bec957e 2020-03-21 stsp err = got_error_from_errno("close");
1353 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
1354 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
1355 9ff10419 2020-03-18 stsp if (err != NULL)
1356 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1357 93658fb9 2020-03-18 stsp else
1358 1d72a2a0 2020-03-24 stsp err = send_fetch_done(&ibuf, pack_sha1);
1359 cf875574 2020-03-18 stsp if (err != NULL) {
1360 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1361 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1362 93658fb9 2020-03-18 stsp }
1363 93658fb9 2020-03-18 stsp
1364 93658fb9 2020-03-18 stsp exit(0);
1365 93658fb9 2020-03-18 stsp }