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