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 f024663d 2021-09-05 stsp #include "got_lib_pkt.h"
52 bd3d9e54 2021-09-05 stsp #include "got_lib_gitproto.h"
53 dcb64fea 2022-02-23 stsp #include "got_lib_ratelimit.h"
54 93658fb9 2020-03-18 stsp
55 8a29a085 2020-03-18 stsp #ifndef nitems
56 8a29a085 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 8a29a085 2020-03-18 stsp #endif
58 8a29a085 2020-03-18 stsp
59 93658fb9 2020-03-18 stsp struct got_object *indexed;
60 858b0dfb 2020-03-20 stsp static int chattygot;
61 93658fb9 2020-03-18 stsp
62 bd3d9e54 2021-09-05 stsp static const struct got_capability got_capabilities[] = {
63 bd3d9e54 2021-09-05 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
64 bd3d9e54 2021-09-05 stsp { GOT_CAPA_OFS_DELTA, NULL },
65 bd3d9e54 2021-09-05 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
66 bd3d9e54 2021-09-05 stsp };
67 bd3d9e54 2021-09-05 stsp
68 7848a0e1 2020-03-19 stsp static void
69 7848a0e1 2020-03-19 stsp match_remote_ref(struct got_pathlist_head *have_refs,
70 7848a0e1 2020-03-19 stsp struct got_object_id *my_id, char *refname)
71 93658fb9 2020-03-18 stsp {
72 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
73 93658fb9 2020-03-18 stsp
74 7848a0e1 2020-03-19 stsp /* XXX zero-hash signifies we don't have this ref;
75 7848a0e1 2020-03-19 stsp * we should use a flag instead */
76 7848a0e1 2020-03-19 stsp memset(my_id, 0, sizeof(*my_id));
77 93658fb9 2020-03-18 stsp
78 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
79 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
80 7848a0e1 2020-03-19 stsp if (strcmp(pe->path, refname) == 0) {
81 7848a0e1 2020-03-19 stsp memcpy(my_id, id, sizeof(*my_id));
82 33501562 2020-03-18 stsp break;
83 33501562 2020-03-18 stsp }
84 93658fb9 2020-03-18 stsp }
85 93658fb9 2020-03-18 stsp }
86 93658fb9 2020-03-18 stsp
87 93658fb9 2020-03-18 stsp static int
88 659e7fbd 2020-03-20 stsp match_branch(const char *branch, const char *wanted_branch)
89 93658fb9 2020-03-18 stsp {
90 659e7fbd 2020-03-20 stsp if (strncmp(branch, "refs/heads/", 11) != 0)
91 659e7fbd 2020-03-20 stsp return 0;
92 93658fb9 2020-03-18 stsp
93 659e7fbd 2020-03-20 stsp if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
94 659e7fbd 2020-03-20 stsp wanted_branch += 11;
95 659e7fbd 2020-03-20 stsp
96 659e7fbd 2020-03-20 stsp return (strcmp(branch + 11, wanted_branch) == 0);
97 0e4002ca 2020-03-21 stsp }
98 0e4002ca 2020-03-21 stsp
99 0e4002ca 2020-03-21 stsp static int
100 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
101 0e4002ca 2020-03-21 stsp {
102 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
103 0e4002ca 2020-03-21 stsp return 0;
104 0e4002ca 2020-03-21 stsp refname += 5;
105 0e4002ca 2020-03-21 stsp
106 0e4002ca 2020-03-21 stsp /*
107 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
108 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
109 0e4002ca 2020-03-21 stsp */
110 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
111 0e4002ca 2020-03-21 stsp return 0;
112 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
113 0e4002ca 2020-03-21 stsp return 0;
114 0e4002ca 2020-03-21 stsp
115 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
116 0e4002ca 2020-03-21 stsp wanted_ref += 5;
117 0e4002ca 2020-03-21 stsp
118 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
119 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
120 0e4002ca 2020-03-21 stsp return 1;
121 0e4002ca 2020-03-21 stsp
122 0e4002ca 2020-03-21 stsp /* Allow exact match. */
123 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
124 abe0f35f 2020-03-18 stsp }
125 abe0f35f 2020-03-18 stsp
126 abe0f35f 2020-03-18 stsp static const struct got_error *
127 e70bf110 2020-03-22 stsp send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
128 e70bf110 2020-03-22 stsp {
129 e70bf110 2020-03-22 stsp if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
130 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
131 e70bf110 2020-03-22 stsp
132 e70bf110 2020-03-22 stsp if (msglen == 0)
133 e70bf110 2020-03-22 stsp return NULL;
134 e70bf110 2020-03-22 stsp
135 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
136 e70bf110 2020-03-22 stsp msg, msglen) == -1)
137 e70bf110 2020-03-22 stsp return got_error_from_errno(
138 e70bf110 2020-03-22 stsp "imsg_compose FETCH_SERVER_PROGRESS");
139 e70bf110 2020-03-22 stsp
140 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
141 531c3985 2020-03-18 stsp }
142 531c3985 2020-03-18 stsp
143 531c3985 2020-03-18 stsp static const struct got_error *
144 dcb64fea 2022-02-23 stsp send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes,
145 dcb64fea 2022-02-23 stsp struct got_ratelimit *rl)
146 e70bf110 2020-03-22 stsp {
147 dcb64fea 2022-02-23 stsp const struct got_error *err;
148 dcb64fea 2022-02-23 stsp int elapsed = 0;
149 dcb64fea 2022-02-23 stsp
150 dcb64fea 2022-02-23 stsp if (rl) {
151 dcb64fea 2022-02-23 stsp err = got_ratelimit_check(&elapsed, rl);
152 dcb64fea 2022-02-23 stsp if (err || !elapsed)
153 dcb64fea 2022-02-23 stsp return err;
154 dcb64fea 2022-02-23 stsp }
155 dcb64fea 2022-02-23 stsp
156 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
157 e70bf110 2020-03-22 stsp &bytes, sizeof(bytes)) == -1)
158 e70bf110 2020-03-22 stsp return got_error_from_errno(
159 e70bf110 2020-03-22 stsp "imsg_compose FETCH_DOWNLOAD_PROGRESS");
160 e70bf110 2020-03-22 stsp
161 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
162 e70bf110 2020-03-22 stsp }
163 e70bf110 2020-03-22 stsp
164 e70bf110 2020-03-22 stsp static const struct got_error *
165 1d72a2a0 2020-03-24 stsp send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
166 e70bf110 2020-03-22 stsp {
167 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
168 1d72a2a0 2020-03-24 stsp pack_sha1, SHA1_DIGEST_LENGTH) == -1)
169 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose FETCH");
170 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
171 e70bf110 2020-03-22 stsp }
172 e70bf110 2020-03-22 stsp
173 e70bf110 2020-03-22 stsp static const struct got_error *
174 531c3985 2020-03-18 stsp fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
175 531c3985 2020-03-18 stsp {
176 6059809a 2020-12-17 stsp size_t i;
177 531c3985 2020-03-18 stsp
178 531c3985 2020-03-18 stsp if (len == 0)
179 531c3985 2020-03-18 stsp return NULL;
180 531c3985 2020-03-18 stsp
181 531c3985 2020-03-18 stsp /*
182 531c3985 2020-03-18 stsp * Truncate messages which exceed the maximum imsg payload size.
183 531c3985 2020-03-18 stsp * Server may send up to 64k.
184 531c3985 2020-03-18 stsp */
185 531c3985 2020-03-18 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
186 531c3985 2020-03-18 stsp len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
187 531c3985 2020-03-18 stsp
188 531c3985 2020-03-18 stsp /* Only allow printable ASCII. */
189 531c3985 2020-03-18 stsp for (i = 0; i < len; i++) {
190 531c3985 2020-03-18 stsp if (isprint((unsigned char)buf[i]) ||
191 531c3985 2020-03-18 stsp isspace((unsigned char)buf[i]))
192 531c3985 2020-03-18 stsp continue;
193 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
194 531c3985 2020-03-18 stsp "non-printable progress message received from server");
195 531c3985 2020-03-18 stsp }
196 531c3985 2020-03-18 stsp
197 e70bf110 2020-03-22 stsp return send_fetch_server_progress(ibuf, buf, len);
198 8a29a085 2020-03-18 stsp }
199 abe0f35f 2020-03-18 stsp
200 8a29a085 2020-03-18 stsp static const struct got_error *
201 531c3985 2020-03-18 stsp fetch_error(const char *buf, size_t len)
202 531c3985 2020-03-18 stsp {
203 531c3985 2020-03-18 stsp static char msg[1024];
204 6059809a 2020-12-17 stsp size_t i;
205 531c3985 2020-03-18 stsp
206 531c3985 2020-03-18 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
207 531c3985 2020-03-18 stsp if (!isprint(buf[i]))
208 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
209 531c3985 2020-03-18 stsp "non-printable error message received from server");
210 531c3985 2020-03-18 stsp msg[i] = buf[i];
211 531c3985 2020-03-18 stsp }
212 531c3985 2020-03-18 stsp msg[i] = '\0';
213 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
214 531c3985 2020-03-18 stsp }
215 531c3985 2020-03-18 stsp
216 531c3985 2020-03-18 stsp static const struct got_error *
217 e70bf110 2020-03-22 stsp send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
218 e70bf110 2020-03-22 stsp {
219 e70bf110 2020-03-22 stsp const struct got_error *err = NULL;
220 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
221 e70bf110 2020-03-22 stsp size_t len, nsymrefs = 0;
222 e70bf110 2020-03-22 stsp struct got_pathlist_entry *pe;
223 e70bf110 2020-03-22 stsp
224 e70bf110 2020-03-22 stsp len = sizeof(struct got_imsg_fetch_symrefs);
225 e70bf110 2020-03-22 stsp TAILQ_FOREACH(pe, symrefs, entry) {
226 e70bf110 2020-03-22 stsp const char *target = pe->data;
227 e70bf110 2020-03-22 stsp len += sizeof(struct got_imsg_fetch_symref) +
228 e70bf110 2020-03-22 stsp pe->path_len + strlen(target);
229 e70bf110 2020-03-22 stsp nsymrefs++;
230 e70bf110 2020-03-22 stsp }
231 e70bf110 2020-03-22 stsp
232 e70bf110 2020-03-22 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
233 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
234 e70bf110 2020-03-22 stsp
235 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
236 e70bf110 2020-03-22 stsp if (wbuf == NULL)
237 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create FETCH_SYMREFS");
238 e70bf110 2020-03-22 stsp
239 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
240 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
241 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
242 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
243 e70bf110 2020-03-22 stsp return err;
244 e70bf110 2020-03-22 stsp }
245 e70bf110 2020-03-22 stsp
246 e70bf110 2020-03-22 stsp TAILQ_FOREACH(pe, symrefs, entry) {
247 e70bf110 2020-03-22 stsp const char *name = pe->path;
248 e70bf110 2020-03-22 stsp size_t name_len = pe->path_len;
249 e70bf110 2020-03-22 stsp const char *target = pe->data;
250 e70bf110 2020-03-22 stsp size_t target_len = strlen(target);
251 e70bf110 2020-03-22 stsp
252 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_symref definition! */
253 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
254 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
255 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
256 e70bf110 2020-03-22 stsp return err;
257 e70bf110 2020-03-22 stsp }
258 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
259 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
260 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
261 e70bf110 2020-03-22 stsp return err;
262 e70bf110 2020-03-22 stsp }
263 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, name, name_len) == -1) {
264 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
265 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
266 e70bf110 2020-03-22 stsp return err;
267 e70bf110 2020-03-22 stsp }
268 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, target, target_len) == -1) {
269 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_SYMREFS");
270 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
271 e70bf110 2020-03-22 stsp return err;
272 e70bf110 2020-03-22 stsp }
273 e70bf110 2020-03-22 stsp }
274 e70bf110 2020-03-22 stsp
275 e70bf110 2020-03-22 stsp wbuf->fd = -1;
276 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
277 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
278 e70bf110 2020-03-22 stsp }
279 e70bf110 2020-03-22 stsp
280 e70bf110 2020-03-22 stsp static const struct got_error *
281 e70bf110 2020-03-22 stsp send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
282 e70bf110 2020-03-22 stsp const char *refname)
283 e70bf110 2020-03-22 stsp {
284 e70bf110 2020-03-22 stsp const struct got_error *err = NULL;
285 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
286 e70bf110 2020-03-22 stsp size_t len, reflen = strlen(refname);
287 e70bf110 2020-03-22 stsp
288 e70bf110 2020-03-22 stsp len = sizeof(struct got_imsg_fetch_ref) + reflen;
289 e70bf110 2020-03-22 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
290 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
291 e70bf110 2020-03-22 stsp
292 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
293 e70bf110 2020-03-22 stsp if (wbuf == NULL)
294 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create FETCH_REF");
295 e70bf110 2020-03-22 stsp
296 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_ref definition! */
297 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
298 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_REF");
299 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
300 e70bf110 2020-03-22 stsp return err;
301 e70bf110 2020-03-22 stsp }
302 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, refname, reflen) == -1) {
303 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add FETCH_REF");
304 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
305 e70bf110 2020-03-22 stsp return err;
306 e70bf110 2020-03-22 stsp }
307 e70bf110 2020-03-22 stsp
308 e70bf110 2020-03-22 stsp wbuf->fd = -1;
309 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
310 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
311 e70bf110 2020-03-22 stsp }
312 729743d1 2020-03-23 stsp
313 e70bf110 2020-03-22 stsp static const struct got_error *
314 1d72a2a0 2020-03-24 stsp fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
315 659e7fbd 2020-03-20 stsp struct got_pathlist_head *have_refs, int fetch_all_branches,
316 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_branches,
317 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_refs, int list_refs_only,
318 41b0de12 2020-03-21 stsp struct imsgbuf *ibuf)
319 93658fb9 2020-03-18 stsp {
320 9ff10419 2020-03-18 stsp const struct got_error *err = NULL;
321 77d7d3bb 2021-09-05 stsp char buf[GOT_PKT_MAX];
322 93658fb9 2020-03-18 stsp char hashstr[SHA1_DIGEST_STRING_LENGTH];
323 93658fb9 2020-03-18 stsp struct got_object_id *have, *want;
324 8a29a085 2020-03-18 stsp int is_firstpkt = 1, nref = 0, refsz = 16;
325 7848a0e1 2020-03-19 stsp int i, n, nwant = 0, nhave = 0, acked = 0;
326 5672d305 2020-03-18 stsp off_t packsz = 0, last_reported_packsz = 0;
327 00cd0e0a 2020-03-18 stsp char *id_str = NULL, *refname = NULL;
328 00cd0e0a 2020-03-18 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
329 659e7fbd 2020-03-20 stsp const char *default_branch = NULL;
330 abe0f35f 2020-03-18 stsp struct got_pathlist_head symrefs;
331 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
332 406106ee 2020-03-20 stsp int sent_my_capabilites = 0, have_sidebands = 0;
333 659e7fbd 2020-03-20 stsp int found_branch = 0;
334 dc671e91 2020-03-24 stsp SHA1_CTX sha1_ctx;
335 dc671e91 2020-03-24 stsp uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
336 dc671e91 2020-03-24 stsp size_t sha1_buf_len = 0;
337 dc671e91 2020-03-24 stsp ssize_t w;
338 dcb64fea 2022-02-23 stsp struct got_ratelimit rl;
339 93658fb9 2020-03-18 stsp
340 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
341 dc671e91 2020-03-24 stsp SHA1Init(&sha1_ctx);
342 dcb64fea 2022-02-23 stsp got_ratelimit_init(&rl, 0, 500);
343 abe0f35f 2020-03-18 stsp
344 93658fb9 2020-03-18 stsp have = malloc(refsz * sizeof(have[0]));
345 9ff10419 2020-03-18 stsp if (have == NULL)
346 9ff10419 2020-03-18 stsp return got_error_from_errno("malloc");
347 93658fb9 2020-03-18 stsp want = malloc(refsz * sizeof(want[0]));
348 9ff10419 2020-03-18 stsp if (want == NULL) {
349 9ff10419 2020-03-18 stsp err = got_error_from_errno("malloc");
350 9ff10419 2020-03-18 stsp goto done;
351 9ff10419 2020-03-18 stsp }
352 9ff10419 2020-03-18 stsp while (1) {
353 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
354 fe53745c 2020-03-18 stsp if (err)
355 9ff10419 2020-03-18 stsp goto done;
356 9ff10419 2020-03-18 stsp if (n == 0)
357 93658fb9 2020-03-18 stsp break;
358 a6f88e33 2020-03-18 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
359 531c3985 2020-03-18 stsp err = fetch_error(&buf[4], n - 4);
360 9ff10419 2020-03-18 stsp goto done;
361 9ff10419 2020-03-18 stsp }
362 e99d9267 2021-10-07 stsp free(id_str);
363 e99d9267 2021-10-07 stsp free(refname);
364 bd3d9e54 2021-09-05 stsp err = got_gitproto_parse_refline(&id_str, &refname,
365 bd3d9e54 2021-09-05 stsp &server_capabilities, buf, n);
366 0d0a341c 2020-03-18 stsp if (err)
367 9ff10419 2020-03-18 stsp goto done;
368 8a29a085 2020-03-18 stsp if (is_firstpkt) {
369 858b0dfb 2020-03-20 stsp if (chattygot && server_capabilities[0] != '\0')
370 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: server capabilities: %s\n",
371 858b0dfb 2020-03-20 stsp getprogname(), server_capabilities);
372 bd3d9e54 2021-09-05 stsp err = got_gitproto_match_capabilities(&my_capabilities,
373 bd3d9e54 2021-09-05 stsp &symrefs, server_capabilities,
374 bd3d9e54 2021-09-05 stsp got_capabilities, nitems(got_capabilities));
375 8a29a085 2020-03-18 stsp if (err)
376 8a29a085 2020-03-18 stsp goto done;
377 858b0dfb 2020-03-20 stsp if (chattygot)
378 2690194b 2020-03-21 stsp fprintf(stderr, "%s: my capabilities:%s\n",
379 a90356f7 2021-08-26 stsp getprogname(), my_capabilities != NULL ?
380 a90356f7 2021-08-26 stsp my_capabilities : "");
381 e70bf110 2020-03-22 stsp err = send_fetch_symrefs(ibuf, &symrefs);
382 abe0f35f 2020-03-18 stsp if (err)
383 abe0f35f 2020-03-18 stsp goto done;
384 7848a0e1 2020-03-19 stsp is_firstpkt = 0;
385 659e7fbd 2020-03-20 stsp if (!fetch_all_branches) {
386 659e7fbd 2020-03-20 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
387 659e7fbd 2020-03-20 stsp const char *name = pe->path;
388 659e7fbd 2020-03-20 stsp const char *symref_target = pe->data;
389 659e7fbd 2020-03-20 stsp if (strcmp(name, GOT_REF_HEAD) != 0)
390 659e7fbd 2020-03-20 stsp continue;
391 659e7fbd 2020-03-20 stsp default_branch = symref_target;
392 659e7fbd 2020-03-20 stsp break;
393 659e7fbd 2020-03-20 stsp }
394 659e7fbd 2020-03-20 stsp }
395 7848a0e1 2020-03-19 stsp continue;
396 8a29a085 2020-03-18 stsp }
397 2690194b 2020-03-21 stsp if (strstr(refname, "^{}")) {
398 2690194b 2020-03-21 stsp if (chattygot) {
399 2690194b 2020-03-21 stsp fprintf(stderr, "%s: ignoring %s\n",
400 2690194b 2020-03-21 stsp getprogname(), refname);
401 2690194b 2020-03-21 stsp }
402 93658fb9 2020-03-18 stsp continue;
403 2690194b 2020-03-21 stsp }
404 659e7fbd 2020-03-20 stsp
405 659e7fbd 2020-03-20 stsp if (strncmp(refname, "refs/heads/", 11) == 0) {
406 41b0de12 2020-03-21 stsp if (fetch_all_branches || list_refs_only) {
407 4ba14133 2020-03-20 stsp found_branch = 1;
408 4ba14133 2020-03-20 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
409 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
410 4ba14133 2020-03-20 stsp if (match_branch(refname, pe->path))
411 4ba14133 2020-03-20 stsp break;
412 4ba14133 2020-03-20 stsp }
413 2690194b 2020-03-21 stsp if (pe == NULL) {
414 2690194b 2020-03-21 stsp if (chattygot) {
415 2690194b 2020-03-21 stsp fprintf(stderr,
416 2690194b 2020-03-21 stsp "%s: ignoring %s\n",
417 2690194b 2020-03-21 stsp getprogname(), refname);
418 2690194b 2020-03-21 stsp }
419 4ba14133 2020-03-20 stsp continue;
420 2690194b 2020-03-21 stsp }
421 4ba14133 2020-03-20 stsp found_branch = 1;
422 4ba14133 2020-03-20 stsp } else if (default_branch != NULL) {
423 2690194b 2020-03-21 stsp if (!match_branch(refname, default_branch)) {
424 2690194b 2020-03-21 stsp if (chattygot) {
425 2690194b 2020-03-21 stsp fprintf(stderr,
426 2690194b 2020-03-21 stsp "%s: ignoring %s\n",
427 2690194b 2020-03-21 stsp getprogname(), refname);
428 2690194b 2020-03-21 stsp }
429 4ba14133 2020-03-20 stsp continue;
430 2690194b 2020-03-21 stsp }
431 4ba14133 2020-03-20 stsp found_branch = 1;
432 4ba14133 2020-03-20 stsp }
433 659e7fbd 2020-03-20 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0) {
434 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(wanted_refs)) {
435 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
436 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
437 0e4002ca 2020-03-21 stsp break;
438 0e4002ca 2020-03-21 stsp }
439 0e4002ca 2020-03-21 stsp if (pe == NULL) {
440 0e4002ca 2020-03-21 stsp if (chattygot) {
441 0e4002ca 2020-03-21 stsp fprintf(stderr,
442 0e4002ca 2020-03-21 stsp "%s: ignoring %s\n",
443 0e4002ca 2020-03-21 stsp getprogname(), refname);
444 0e4002ca 2020-03-21 stsp }
445 0e4002ca 2020-03-21 stsp continue;
446 0e4002ca 2020-03-21 stsp }
447 0e4002ca 2020-03-21 stsp found_branch = 1;
448 0e4002ca 2020-03-21 stsp } else if (!list_refs_only) {
449 2690194b 2020-03-21 stsp if (chattygot) {
450 2690194b 2020-03-21 stsp fprintf(stderr, "%s: ignoring %s\n",
451 2690194b 2020-03-21 stsp getprogname(), refname);
452 2690194b 2020-03-21 stsp }
453 4515a796 2020-03-21 stsp continue;
454 2690194b 2020-03-21 stsp }
455 659e7fbd 2020-03-20 stsp }
456 659e7fbd 2020-03-20 stsp
457 cf875574 2020-03-18 stsp if (refsz == nref + 1) {
458 93658fb9 2020-03-18 stsp refsz *= 2;
459 14778466 2020-03-18 stsp have = reallocarray(have, refsz, sizeof(have[0]));
460 9ff10419 2020-03-18 stsp if (have == NULL) {
461 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
462 9ff10419 2020-03-18 stsp goto done;
463 9ff10419 2020-03-18 stsp }
464 14778466 2020-03-18 stsp want = reallocarray(want, refsz, sizeof(want[0]));
465 9ff10419 2020-03-18 stsp if (want == NULL) {
466 14778466 2020-03-18 stsp err = got_error_from_errno("reallocarray");
467 9ff10419 2020-03-18 stsp goto done;
468 9ff10419 2020-03-18 stsp }
469 93658fb9 2020-03-18 stsp }
470 00cd0e0a 2020-03-18 stsp if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
471 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
472 9ff10419 2020-03-18 stsp goto done;
473 9ff10419 2020-03-18 stsp }
474 7848a0e1 2020-03-19 stsp match_remote_ref(have_refs, &have[nref], refname);
475 e70bf110 2020-03-22 stsp err = send_fetch_ref(ibuf, &want[nref], refname);
476 8f2d01a6 2020-03-18 stsp if (err)
477 8f2d01a6 2020-03-18 stsp goto done;
478 858b0dfb 2020-03-20 stsp
479 2690194b 2020-03-21 stsp if (chattygot)
480 2690194b 2020-03-21 stsp fprintf(stderr, "%s: %s will be fetched\n",
481 2690194b 2020-03-21 stsp getprogname(), refname);
482 2690194b 2020-03-21 stsp if (chattygot > 1) {
483 858b0dfb 2020-03-20 stsp char *theirs, *mine;
484 858b0dfb 2020-03-20 stsp err = got_object_id_str(&theirs, &want[nref]);
485 858b0dfb 2020-03-20 stsp if (err)
486 858b0dfb 2020-03-20 stsp goto done;
487 858b0dfb 2020-03-20 stsp err = got_object_id_str(&mine, &have[nref]);
488 858b0dfb 2020-03-20 stsp if (err) {
489 858b0dfb 2020-03-20 stsp free(theirs);
490 858b0dfb 2020-03-20 stsp goto done;
491 858b0dfb 2020-03-20 stsp }
492 2690194b 2020-03-21 stsp fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
493 659e7fbd 2020-03-20 stsp getprogname(), theirs, getprogname(), mine);
494 858b0dfb 2020-03-20 stsp free(theirs);
495 858b0dfb 2020-03-20 stsp free(mine);
496 858b0dfb 2020-03-20 stsp }
497 93658fb9 2020-03-18 stsp nref++;
498 93658fb9 2020-03-18 stsp }
499 93658fb9 2020-03-18 stsp
500 41b0de12 2020-03-21 stsp if (list_refs_only)
501 41b0de12 2020-03-21 stsp goto done;
502 41b0de12 2020-03-21 stsp
503 659e7fbd 2020-03-20 stsp /* Abort if we haven't found any branch to fetch. */
504 659e7fbd 2020-03-20 stsp if (!found_branch) {
505 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_FETCH_NO_BRANCH);
506 659e7fbd 2020-03-20 stsp goto done;
507 659e7fbd 2020-03-20 stsp }
508 659e7fbd 2020-03-20 stsp
509 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
510 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
511 93658fb9 2020-03-18 stsp continue;
512 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
513 406106ee 2020-03-20 stsp n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
514 a90356f7 2021-08-26 stsp sent_my_capabilites || my_capabilities == NULL ?
515 a90356f7 2021-08-26 stsp "" : my_capabilities);
516 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
517 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
518 9ff10419 2020-03-18 stsp goto done;
519 9ff10419 2020-03-18 stsp }
520 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
521 344e4747 2020-03-18 stsp if (err)
522 9ff10419 2020-03-18 stsp goto done;
523 858b0dfb 2020-03-20 stsp sent_my_capabilites = 1;
524 7848a0e1 2020-03-19 stsp nwant++;
525 93658fb9 2020-03-18 stsp }
526 f024663d 2021-09-05 stsp err = got_pkt_flushpkt(fd, chattygot);
527 38c670f1 2020-03-18 stsp if (err)
528 38c670f1 2020-03-18 stsp goto done;
529 7848a0e1 2020-03-19 stsp
530 3c912d14 2020-03-19 stsp if (nwant == 0)
531 7848a0e1 2020-03-19 stsp goto done;
532 7848a0e1 2020-03-19 stsp
533 dd088d95 2021-10-06 stsp TAILQ_FOREACH(pe, have_refs, entry) {
534 dd088d95 2021-10-06 stsp struct got_object_id *id = pe->data;
535 dd088d95 2021-10-06 stsp got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
536 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
537 9ff10419 2020-03-18 stsp if (n >= sizeof(buf)) {
538 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
539 9ff10419 2020-03-18 stsp goto done;
540 9ff10419 2020-03-18 stsp }
541 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
542 344e4747 2020-03-18 stsp if (err)
543 9ff10419 2020-03-18 stsp goto done;
544 7848a0e1 2020-03-19 stsp nhave++;
545 93658fb9 2020-03-18 stsp }
546 7848a0e1 2020-03-19 stsp
547 7848a0e1 2020-03-19 stsp while (nhave > 0 && !acked) {
548 7848a0e1 2020-03-19 stsp struct got_object_id common_id;
549 7848a0e1 2020-03-19 stsp
550 7848a0e1 2020-03-19 stsp /* The server should ACK the object IDs we need. */
551 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
552 38c670f1 2020-03-18 stsp if (err)
553 38c670f1 2020-03-18 stsp goto done;
554 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
555 7848a0e1 2020-03-19 stsp err = fetch_error(&buf[4], n - 4);
556 7848a0e1 2020-03-19 stsp goto done;
557 7848a0e1 2020-03-19 stsp }
558 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
559 7848a0e1 2020-03-19 stsp /* Server has not located our objects yet. */
560 7848a0e1 2020-03-19 stsp continue;
561 7848a0e1 2020-03-19 stsp }
562 7848a0e1 2020-03-19 stsp if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
563 7848a0e1 2020-03-19 stsp strncmp(buf, "ACK ", 4) != 0) {
564 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
565 7848a0e1 2020-03-19 stsp "unexpected message from server");
566 7848a0e1 2020-03-19 stsp goto done;
567 7848a0e1 2020-03-19 stsp }
568 7848a0e1 2020-03-19 stsp if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
569 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
570 7848a0e1 2020-03-19 stsp "bad object ID in ACK packet from server");
571 7848a0e1 2020-03-19 stsp goto done;
572 7848a0e1 2020-03-19 stsp }
573 7848a0e1 2020-03-19 stsp acked++;
574 93658fb9 2020-03-18 stsp }
575 7848a0e1 2020-03-19 stsp
576 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "done\n");
577 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
578 344e4747 2020-03-18 stsp if (err)
579 9ff10419 2020-03-18 stsp goto done;
580 93658fb9 2020-03-18 stsp
581 7848a0e1 2020-03-19 stsp if (nhave == 0) {
582 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
583 7848a0e1 2020-03-19 stsp if (err)
584 7848a0e1 2020-03-19 stsp goto done;
585 7848a0e1 2020-03-19 stsp if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
586 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
587 7848a0e1 2020-03-19 stsp "unexpected message from server");
588 7848a0e1 2020-03-19 stsp goto done;
589 7848a0e1 2020-03-19 stsp }
590 04c53c18 2020-03-18 stsp }
591 93658fb9 2020-03-18 stsp
592 858b0dfb 2020-03-20 stsp if (chattygot)
593 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: fetching...\n", getprogname());
594 858b0dfb 2020-03-20 stsp
595 531c3985 2020-03-18 stsp if (my_capabilities != NULL &&
596 531c3985 2020-03-18 stsp strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
597 531c3985 2020-03-18 stsp have_sidebands = 1;
598 531c3985 2020-03-18 stsp
599 9ff10419 2020-03-18 stsp while (1) {
600 dc671e91 2020-03-24 stsp ssize_t r = 0;
601 531c3985 2020-03-18 stsp int datalen = -1;
602 531c3985 2020-03-18 stsp
603 531c3985 2020-03-18 stsp if (have_sidebands) {
604 f024663d 2021-09-05 stsp err = got_pkt_readhdr(&datalen, fd, chattygot);
605 531c3985 2020-03-18 stsp if (err)
606 531c3985 2020-03-18 stsp goto done;
607 531c3985 2020-03-18 stsp if (datalen <= 0)
608 531c3985 2020-03-18 stsp break;
609 531c3985 2020-03-18 stsp
610 531c3985 2020-03-18 stsp /* Read sideband channel ID (one byte). */
611 531c3985 2020-03-18 stsp r = read(fd, buf, 1);
612 531c3985 2020-03-18 stsp if (r == -1) {
613 531c3985 2020-03-18 stsp err = got_error_from_errno("read");
614 531c3985 2020-03-18 stsp goto done;
615 531c3985 2020-03-18 stsp }
616 531c3985 2020-03-18 stsp if (r != 1) {
617 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
618 531c3985 2020-03-18 stsp "short packet");
619 531c3985 2020-03-18 stsp goto done;
620 531c3985 2020-03-18 stsp }
621 531c3985 2020-03-18 stsp if (datalen > sizeof(buf) - 5) {
622 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
623 531c3985 2020-03-18 stsp "bad packet length");
624 531c3985 2020-03-18 stsp goto done;
625 531c3985 2020-03-18 stsp }
626 531c3985 2020-03-18 stsp datalen--; /* sideband ID has been read */
627 531c3985 2020-03-18 stsp if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
628 531c3985 2020-03-18 stsp /* Read packfile data. */
629 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
630 531c3985 2020-03-18 stsp if (err)
631 531c3985 2020-03-18 stsp goto done;
632 531c3985 2020-03-18 stsp if (r != datalen) {
633 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
634 531c3985 2020-03-18 stsp "packet too short");
635 531c3985 2020-03-18 stsp goto done;
636 531c3985 2020-03-18 stsp }
637 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
638 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
639 531c3985 2020-03-18 stsp if (err)
640 531c3985 2020-03-18 stsp goto done;
641 531c3985 2020-03-18 stsp if (r != datalen) {
642 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
643 531c3985 2020-03-18 stsp "packet too short");
644 531c3985 2020-03-18 stsp goto done;
645 531c3985 2020-03-18 stsp }
646 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
647 531c3985 2020-03-18 stsp if (err)
648 531c3985 2020-03-18 stsp goto done;
649 531c3985 2020-03-18 stsp continue;
650 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
651 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
652 531c3985 2020-03-18 stsp if (err)
653 531c3985 2020-03-18 stsp goto done;
654 531c3985 2020-03-18 stsp if (r != datalen) {
655 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
656 531c3985 2020-03-18 stsp "packet too short");
657 531c3985 2020-03-18 stsp goto done;
658 531c3985 2020-03-18 stsp }
659 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
660 531c3985 2020-03-18 stsp goto done;
661 98f64f14 2021-01-05 stsp } else if (buf[0] == 'A') {
662 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
663 98f64f14 2021-01-05 stsp if (err)
664 98f64f14 2021-01-05 stsp goto done;
665 98f64f14 2021-01-05 stsp if (r != datalen) {
666 98f64f14 2021-01-05 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
667 98f64f14 2021-01-05 stsp "packet too short");
668 98f64f14 2021-01-05 stsp goto done;
669 98f64f14 2021-01-05 stsp }
670 98f64f14 2021-01-05 stsp /*
671 98f64f14 2021-01-05 stsp * Git server responds with ACK after 'done'
672 98f64f14 2021-01-05 stsp * even though multi_ack is disabled?!?
673 98f64f14 2021-01-05 stsp */
674 98f64f14 2021-01-05 stsp buf[r] = '\0';
675 98f64f14 2021-01-05 stsp if (strncmp(buf, "CK ", 3) == 0)
676 98f64f14 2021-01-05 stsp continue; /* ignore */
677 98f64f14 2021-01-05 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
678 98f64f14 2021-01-05 stsp "unexpected message from server");
679 98f64f14 2021-01-05 stsp goto done;
680 531c3985 2020-03-18 stsp } else {
681 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
682 531c3985 2020-03-18 stsp "unknown side-band received from server");
683 531c3985 2020-03-18 stsp goto done;
684 531c3985 2020-03-18 stsp }
685 531c3985 2020-03-18 stsp } else {
686 531c3985 2020-03-18 stsp /* No sideband channel. Every byte is packfile data. */
687 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, sizeof buf);
688 531c3985 2020-03-18 stsp if (err)
689 531c3985 2020-03-18 stsp goto done;
690 531c3985 2020-03-18 stsp if (r <= 0)
691 531c3985 2020-03-18 stsp break;
692 531c3985 2020-03-18 stsp }
693 dc671e91 2020-03-24 stsp
694 dc671e91 2020-03-24 stsp /*
695 dc671e91 2020-03-24 stsp * An expected SHA1 checksum sits at the end of the pack file.
696 dc671e91 2020-03-24 stsp * Since we don't know the file size ahead of time we have to
697 dc671e91 2020-03-24 stsp * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
698 dc671e91 2020-03-24 stsp * those bytes into our SHA1 checksum computation until we
699 dc671e91 2020-03-24 stsp * know for sure that additional pack file data bytes follow.
700 dc671e91 2020-03-24 stsp *
701 dc671e91 2020-03-24 stsp * We can assume r > 0 since otherwise the loop would exit.
702 dc671e91 2020-03-24 stsp */
703 dc671e91 2020-03-24 stsp if (r < SHA1_DIGEST_LENGTH) {
704 dc671e91 2020-03-24 stsp if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
705 dc671e91 2020-03-24 stsp /*
706 dc671e91 2020-03-24 stsp * If there's enough buffered + read data to
707 dc671e91 2020-03-24 stsp * fill up the buffer then shift a sufficient
708 dc671e91 2020-03-24 stsp * amount of bytes out at the front to make
709 dc671e91 2020-03-24 stsp * room, mixing those bytes into the checksum.
710 dc671e91 2020-03-24 stsp */
711 dc671e91 2020-03-24 stsp while (sha1_buf_len > 0 &&
712 dc671e91 2020-03-24 stsp sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
713 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, sha1_buf, 1);
714 dc671e91 2020-03-24 stsp memmove(sha1_buf, sha1_buf + 1, 1);
715 dc671e91 2020-03-24 stsp sha1_buf_len--;
716 dc671e91 2020-03-24 stsp }
717 dc671e91 2020-03-24 stsp
718 dc671e91 2020-03-24 stsp /* Buffer potential checksum bytes. */
719 dc671e91 2020-03-24 stsp memcpy(sha1_buf + sha1_buf_len, buf, r);
720 dc671e91 2020-03-24 stsp sha1_buf_len += r;
721 dc671e91 2020-03-24 stsp } else {
722 dc671e91 2020-03-24 stsp /*
723 dc671e91 2020-03-24 stsp * Mix in previously buffered bytes which
724 dc671e91 2020-03-24 stsp * are not part of the checksum after all.
725 dc671e91 2020-03-24 stsp */
726 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, sha1_buf, r);
727 531c3985 2020-03-18 stsp
728 dc671e91 2020-03-24 stsp /* Update potential checksum buffer. */
729 dc671e91 2020-03-24 stsp memmove(sha1_buf, sha1_buf + r,
730 dc671e91 2020-03-24 stsp sha1_buf_len - r);
731 dc671e91 2020-03-24 stsp memcpy(sha1_buf + sha1_buf_len - r, buf, r);
732 dc671e91 2020-03-24 stsp }
733 dc671e91 2020-03-24 stsp } else {
734 dc671e91 2020-03-24 stsp /* Mix in any previously buffered bytes. */
735 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
736 dc671e91 2020-03-24 stsp
737 dc671e91 2020-03-24 stsp /* Mix in bytes read minus potential checksum bytes. */
738 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
739 dc671e91 2020-03-24 stsp
740 dc671e91 2020-03-24 stsp /* Buffer potential checksum bytes. */
741 dc671e91 2020-03-24 stsp memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
742 dc671e91 2020-03-24 stsp SHA1_DIGEST_LENGTH);
743 dc671e91 2020-03-24 stsp sha1_buf_len = SHA1_DIGEST_LENGTH;
744 dc671e91 2020-03-24 stsp }
745 3168e5da 2020-09-10 stsp
746 531c3985 2020-03-18 stsp /* Write packfile data to temporary pack file. */
747 fe53745c 2020-03-18 stsp w = write(packfd, buf, r);
748 9ff10419 2020-03-18 stsp if (w == -1) {
749 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
750 9ff10419 2020-03-18 stsp goto done;
751 9ff10419 2020-03-18 stsp }
752 fe53745c 2020-03-18 stsp if (w != r) {
753 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
754 9ff10419 2020-03-18 stsp goto done;
755 9ff10419 2020-03-18 stsp }
756 531c3985 2020-03-18 stsp packsz += w;
757 5672d305 2020-03-18 stsp
758 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
759 5672d305 2020-03-18 stsp if (packsz > last_reported_packsz + 1024) {
760 dcb64fea 2022-02-23 stsp err = send_fetch_download_progress(ibuf, packsz, &rl);
761 5672d305 2020-03-18 stsp if (err)
762 5672d305 2020-03-18 stsp goto done;
763 5672d305 2020-03-18 stsp last_reported_packsz = packsz;
764 5672d305 2020-03-18 stsp }
765 93658fb9 2020-03-18 stsp }
766 dcb64fea 2022-02-23 stsp err = send_fetch_download_progress(ibuf, packsz, NULL);
767 5672d305 2020-03-18 stsp if (err)
768 5672d305 2020-03-18 stsp goto done;
769 dc671e91 2020-03-24 stsp
770 dc671e91 2020-03-24 stsp SHA1Final(pack_sha1, &sha1_ctx);
771 dc671e91 2020-03-24 stsp if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
772 dc671e91 2020-03-24 stsp memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
773 dc671e91 2020-03-24 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
774 dc671e91 2020-03-24 stsp "pack file checksum mismatch");
775 dc671e91 2020-03-24 stsp }
776 9ff10419 2020-03-18 stsp done:
777 abe0f35f 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
778 abe0f35f 2020-03-18 stsp free((void *)pe->path);
779 abe0f35f 2020-03-18 stsp free(pe->data);
780 abe0f35f 2020-03-18 stsp }
781 abe0f35f 2020-03-18 stsp got_pathlist_free(&symrefs);
782 9ff10419 2020-03-18 stsp free(have);
783 9ff10419 2020-03-18 stsp free(want);
784 00cd0e0a 2020-03-18 stsp free(id_str);
785 00cd0e0a 2020-03-18 stsp free(refname);
786 00cd0e0a 2020-03-18 stsp free(server_capabilities);
787 8f2d01a6 2020-03-18 stsp return err;
788 93658fb9 2020-03-18 stsp }
789 93658fb9 2020-03-18 stsp
790 93658fb9 2020-03-18 stsp
791 93658fb9 2020-03-18 stsp int
792 93658fb9 2020-03-18 stsp main(int argc, char **argv)
793 93658fb9 2020-03-18 stsp {
794 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
795 030daac8 2021-09-25 stsp int fetchfd, packfd = -1;
796 1d72a2a0 2020-03-24 stsp uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
797 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
798 93658fb9 2020-03-18 stsp struct imsg imsg;
799 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
800 4ba14133 2020-03-20 stsp struct got_pathlist_head wanted_branches;
801 0e4002ca 2020-03-21 stsp struct got_pathlist_head wanted_refs;
802 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
803 4ba14133 2020-03-20 stsp struct got_imsg_fetch_request fetch_req;
804 659e7fbd 2020-03-20 stsp struct got_imsg_fetch_have_ref href;
805 4ba14133 2020-03-20 stsp struct got_imsg_fetch_wanted_branch wbranch;
806 0e4002ca 2020-03-21 stsp struct got_imsg_fetch_wanted_ref wref;
807 030daac8 2021-09-25 stsp size_t datalen, i;
808 7848a0e1 2020-03-19 stsp #if 0
809 7848a0e1 2020-03-19 stsp static int attached;
810 7848a0e1 2020-03-19 stsp while (!attached)
811 7848a0e1 2020-03-19 stsp sleep (1);
812 7848a0e1 2020-03-19 stsp #endif
813 33501562 2020-03-18 stsp
814 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
815 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
816 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
817 858b0dfb 2020-03-20 stsp
818 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
819 ffb5f621 2020-03-18 stsp #ifndef PROFILE
820 ffb5f621 2020-03-18 stsp /* revoke access to most system calls */
821 ffb5f621 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
822 ffb5f621 2020-03-18 stsp err = got_error_from_errno("pledge");
823 ffb5f621 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
824 ffb5f621 2020-03-18 stsp return 1;
825 ffb5f621 2020-03-18 stsp }
826 ffb5f621 2020-03-18 stsp #endif
827 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
828 9ca26ac3 2021-08-06 stsp if (err) {
829 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
830 93658fb9 2020-03-18 stsp err = NULL;
831 93658fb9 2020-03-18 stsp goto done;
832 93658fb9 2020-03-18 stsp }
833 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
834 93658fb9 2020-03-18 stsp goto done;
835 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
836 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
837 93658fb9 2020-03-18 stsp goto done;
838 93658fb9 2020-03-18 stsp }
839 33501562 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
840 4ba14133 2020-03-20 stsp if (datalen < sizeof(fetch_req)) {
841 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
842 93658fb9 2020-03-18 stsp goto done;
843 93658fb9 2020-03-18 stsp }
844 4ba14133 2020-03-20 stsp memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
845 4ba14133 2020-03-20 stsp fetchfd = imsg.fd;
846 4ba14133 2020-03-20 stsp imsg_free(&imsg);
847 4ba14133 2020-03-20 stsp
848 2690194b 2020-03-21 stsp if (fetch_req.verbosity > 0)
849 2690194b 2020-03-21 stsp chattygot += fetch_req.verbosity;
850 2690194b 2020-03-21 stsp
851 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_have_refs; i++) {
852 7848a0e1 2020-03-19 stsp struct got_object_id *id;
853 7848a0e1 2020-03-19 stsp char *refname;
854 7848a0e1 2020-03-19 stsp
855 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
856 9ca26ac3 2021-08-06 stsp if (err) {
857 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
858 4ba14133 2020-03-20 stsp err = NULL;
859 4ba14133 2020-03-20 stsp goto done;
860 4ba14133 2020-03-20 stsp }
861 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
862 4ba14133 2020-03-20 stsp goto done;
863 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
864 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
865 4ba14133 2020-03-20 stsp goto done;
866 4ba14133 2020-03-20 stsp }
867 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
868 4ba14133 2020-03-20 stsp if (datalen < sizeof(href)) {
869 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
870 659e7fbd 2020-03-20 stsp goto done;
871 659e7fbd 2020-03-20 stsp }
872 4ba14133 2020-03-20 stsp memcpy(&href, imsg.data, sizeof(href));
873 4ba14133 2020-03-20 stsp if (datalen - sizeof(href) < href.name_len) {
874 7848a0e1 2020-03-19 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
875 7848a0e1 2020-03-19 stsp goto done;
876 7848a0e1 2020-03-19 stsp }
877 659e7fbd 2020-03-20 stsp refname = malloc(href.name_len + 1);
878 7848a0e1 2020-03-19 stsp if (refname == NULL) {
879 659e7fbd 2020-03-20 stsp err = got_error_from_errno("malloc");
880 7848a0e1 2020-03-19 stsp goto done;
881 7848a0e1 2020-03-19 stsp }
882 4ba14133 2020-03-20 stsp memcpy(refname, imsg.data + sizeof(href), href.name_len);
883 659e7fbd 2020-03-20 stsp refname[href.name_len] = '\0';
884 659e7fbd 2020-03-20 stsp
885 7848a0e1 2020-03-19 stsp id = malloc(sizeof(*id));
886 7848a0e1 2020-03-19 stsp if (id == NULL) {
887 7848a0e1 2020-03-19 stsp free(refname);
888 7848a0e1 2020-03-19 stsp err = got_error_from_errno("malloc");
889 7848a0e1 2020-03-19 stsp goto done;
890 7848a0e1 2020-03-19 stsp }
891 659e7fbd 2020-03-20 stsp memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
892 7848a0e1 2020-03-19 stsp err = got_pathlist_append(&have_refs, refname, id);
893 7848a0e1 2020-03-19 stsp if (err) {
894 7848a0e1 2020-03-19 stsp free(refname);
895 7848a0e1 2020-03-19 stsp free(id);
896 7848a0e1 2020-03-19 stsp goto done;
897 7848a0e1 2020-03-19 stsp }
898 4ba14133 2020-03-20 stsp
899 4ba14133 2020-03-20 stsp imsg_free(&imsg);
900 659e7fbd 2020-03-20 stsp }
901 93658fb9 2020-03-18 stsp
902 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_wanted_branches; i++) {
903 4ba14133 2020-03-20 stsp char *refname;
904 4ba14133 2020-03-20 stsp
905 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
906 9ca26ac3 2021-08-06 stsp if (err) {
907 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
908 4ba14133 2020-03-20 stsp err = NULL;
909 4ba14133 2020-03-20 stsp goto done;
910 4ba14133 2020-03-20 stsp }
911 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
912 4ba14133 2020-03-20 stsp goto done;
913 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
914 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
915 4ba14133 2020-03-20 stsp goto done;
916 4ba14133 2020-03-20 stsp }
917 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
918 4ba14133 2020-03-20 stsp if (datalen < sizeof(wbranch)) {
919 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
920 4ba14133 2020-03-20 stsp goto done;
921 4ba14133 2020-03-20 stsp }
922 4ba14133 2020-03-20 stsp memcpy(&wbranch, imsg.data, sizeof(wbranch));
923 4ba14133 2020-03-20 stsp if (datalen - sizeof(wbranch) < wbranch.name_len) {
924 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
925 4ba14133 2020-03-20 stsp goto done;
926 4ba14133 2020-03-20 stsp }
927 4ba14133 2020-03-20 stsp refname = malloc(wbranch.name_len + 1);
928 4ba14133 2020-03-20 stsp if (refname == NULL) {
929 4ba14133 2020-03-20 stsp err = got_error_from_errno("malloc");
930 4ba14133 2020-03-20 stsp goto done;
931 4ba14133 2020-03-20 stsp }
932 4ba14133 2020-03-20 stsp memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
933 4ba14133 2020-03-20 stsp refname[wbranch.name_len] = '\0';
934 4ba14133 2020-03-20 stsp
935 4ba14133 2020-03-20 stsp err = got_pathlist_append(&wanted_branches, refname, NULL);
936 4ba14133 2020-03-20 stsp if (err) {
937 4ba14133 2020-03-20 stsp free(refname);
938 4ba14133 2020-03-20 stsp goto done;
939 4ba14133 2020-03-20 stsp }
940 4ba14133 2020-03-20 stsp
941 4ba14133 2020-03-20 stsp imsg_free(&imsg);
942 4ba14133 2020-03-20 stsp }
943 4ba14133 2020-03-20 stsp
944 0e4002ca 2020-03-21 stsp for (i = 0; i < fetch_req.n_wanted_refs; i++) {
945 0e4002ca 2020-03-21 stsp char *refname;
946 0e4002ca 2020-03-21 stsp
947 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
948 9ca26ac3 2021-08-06 stsp if (err) {
949 0e4002ca 2020-03-21 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
950 0e4002ca 2020-03-21 stsp err = NULL;
951 0e4002ca 2020-03-21 stsp goto done;
952 0e4002ca 2020-03-21 stsp }
953 0e4002ca 2020-03-21 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
954 0e4002ca 2020-03-21 stsp goto done;
955 0e4002ca 2020-03-21 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
956 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
957 0e4002ca 2020-03-21 stsp goto done;
958 0e4002ca 2020-03-21 stsp }
959 0e4002ca 2020-03-21 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
960 0e4002ca 2020-03-21 stsp if (datalen < sizeof(wref)) {
961 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
962 0e4002ca 2020-03-21 stsp goto done;
963 0e4002ca 2020-03-21 stsp }
964 0e4002ca 2020-03-21 stsp memcpy(&wref, imsg.data, sizeof(wref));
965 0e4002ca 2020-03-21 stsp if (datalen - sizeof(wref) < wref.name_len) {
966 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
967 0e4002ca 2020-03-21 stsp goto done;
968 0e4002ca 2020-03-21 stsp }
969 0e4002ca 2020-03-21 stsp refname = malloc(wref.name_len + 1);
970 0e4002ca 2020-03-21 stsp if (refname == NULL) {
971 0e4002ca 2020-03-21 stsp err = got_error_from_errno("malloc");
972 0e4002ca 2020-03-21 stsp goto done;
973 0e4002ca 2020-03-21 stsp }
974 0e4002ca 2020-03-21 stsp memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
975 0e4002ca 2020-03-21 stsp refname[wref.name_len] = '\0';
976 0e4002ca 2020-03-21 stsp
977 0e4002ca 2020-03-21 stsp err = got_pathlist_append(&wanted_refs, refname, NULL);
978 0e4002ca 2020-03-21 stsp if (err) {
979 0e4002ca 2020-03-21 stsp free(refname);
980 0e4002ca 2020-03-21 stsp goto done;
981 0e4002ca 2020-03-21 stsp }
982 0e4002ca 2020-03-21 stsp
983 0e4002ca 2020-03-21 stsp imsg_free(&imsg);
984 0e4002ca 2020-03-21 stsp }
985 0e4002ca 2020-03-21 stsp
986 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
987 9ca26ac3 2021-08-06 stsp if (err) {
988 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
989 93658fb9 2020-03-18 stsp err = NULL;
990 93658fb9 2020-03-18 stsp goto done;
991 93658fb9 2020-03-18 stsp }
992 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
993 93658fb9 2020-03-18 stsp goto done;
994 f826addf 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
995 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
996 93658fb9 2020-03-18 stsp goto done;
997 93658fb9 2020-03-18 stsp }
998 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
999 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1000 93658fb9 2020-03-18 stsp goto done;
1001 93658fb9 2020-03-18 stsp }
1002 93658fb9 2020-03-18 stsp packfd = imsg.fd;
1003 93658fb9 2020-03-18 stsp
1004 1d72a2a0 2020-03-24 stsp err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1005 41b0de12 2020-03-21 stsp fetch_req.fetch_all_branches, &wanted_branches,
1006 0e4002ca 2020-03-21 stsp &wanted_refs, fetch_req.list_refs_only, &ibuf);
1007 93658fb9 2020-03-18 stsp done:
1008 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &have_refs, entry) {
1009 7848a0e1 2020-03-19 stsp free((char *)pe->path);
1010 7848a0e1 2020-03-19 stsp free(pe->data);
1011 7848a0e1 2020-03-19 stsp }
1012 7848a0e1 2020-03-19 stsp got_pathlist_free(&have_refs);
1013 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry)
1014 4ba14133 2020-03-20 stsp free((char *)pe->path);
1015 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1016 0bec957e 2020-03-21 stsp if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1017 0bec957e 2020-03-21 stsp err = got_error_from_errno("close");
1018 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
1019 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
1020 9ff10419 2020-03-18 stsp if (err != NULL)
1021 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1022 93658fb9 2020-03-18 stsp else
1023 1d72a2a0 2020-03-24 stsp err = send_fetch_done(&ibuf, pack_sha1);
1024 cf875574 2020-03-18 stsp if (err != NULL) {
1025 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1026 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1027 93658fb9 2020-03-18 stsp }
1028 93658fb9 2020-03-18 stsp
1029 93658fb9 2020-03-18 stsp exit(0);
1030 93658fb9 2020-03-18 stsp }