2 93658fb9 2020-03-18 stsp * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
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.
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.
17 93658fb9 2020-03-18 stsp #include <sys/types.h>
18 8b925c6c 2022-07-16 thomas #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>
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 <limits.h>
26 93658fb9 2020-03-18 stsp #include <signal.h>
27 93658fb9 2020-03-18 stsp #include <stdio.h>
28 93658fb9 2020-03-18 stsp #include <stdlib.h>
29 93658fb9 2020-03-18 stsp #include <string.h>
30 93658fb9 2020-03-18 stsp #include <ctype.h>
31 93658fb9 2020-03-18 stsp #include <fcntl.h>
32 81a12da5 2020-09-09 naddy #include <unistd.h>
33 93658fb9 2020-03-18 stsp #include <zlib.h>
34 93658fb9 2020-03-18 stsp #include <err.h>
36 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
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"
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 b637eb2e 2022-02-23 thomas #include "got_lib_ratelimit.h"
55 9439b99a 2022-10-30 thomas #ifndef MIN
56 9439b99a 2022-10-30 thomas #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 8a29a085 2020-03-18 stsp #ifndef nitems
60 8a29a085 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 93658fb9 2020-03-18 stsp struct got_object *indexed;
64 858b0dfb 2020-03-20 stsp static int chattygot;
66 bd3d9e54 2021-09-05 stsp static const struct got_capability got_capabilities[] = {
67 bd3d9e54 2021-09-05 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
68 bd3d9e54 2021-09-05 stsp { GOT_CAPA_OFS_DELTA, NULL },
69 bd3d9e54 2021-09-05 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
73 7848a0e1 2020-03-19 stsp match_remote_ref(struct got_pathlist_head *have_refs,
74 9d0a7ee3 2023-02-07 thomas struct got_object_id *my_id, const char *refname)
76 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
78 7848a0e1 2020-03-19 stsp /* XXX zero-hash signifies we don't have this ref;
79 7848a0e1 2020-03-19 stsp * we should use a flag instead */
80 7848a0e1 2020-03-19 stsp memset(my_id, 0, sizeof(*my_id));
82 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
83 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
84 b6b86fd1 2022-08-30 thomas if (strcmp(pe->path, refname) == 0) {
85 7848a0e1 2020-03-19 stsp memcpy(my_id, id, sizeof(*my_id));
92 659e7fbd 2020-03-20 stsp match_branch(const char *branch, const char *wanted_branch)
94 659e7fbd 2020-03-20 stsp if (strncmp(branch, "refs/heads/", 11) != 0)
97 659e7fbd 2020-03-20 stsp if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
98 659e7fbd 2020-03-20 stsp wanted_branch += 11;
100 659e7fbd 2020-03-20 stsp return (strcmp(branch + 11, wanted_branch) == 0);
104 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
106 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
108 0e4002ca 2020-03-21 stsp refname += 5;
111 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
112 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
114 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
116 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
119 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
120 0e4002ca 2020-03-21 stsp wanted_ref += 5;
122 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
123 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
126 0e4002ca 2020-03-21 stsp /* Allow exact match. */
127 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
130 abe0f35f 2020-03-18 stsp static const struct got_error *
131 e70bf110 2020-03-22 stsp send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
133 e70bf110 2020-03-22 stsp if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
134 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
136 e70bf110 2020-03-22 stsp if (msglen == 0)
137 e70bf110 2020-03-22 stsp return NULL;
139 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
140 e70bf110 2020-03-22 stsp msg, msglen) == -1)
141 e70bf110 2020-03-22 stsp return got_error_from_errno(
142 e70bf110 2020-03-22 stsp "imsg_compose FETCH_SERVER_PROGRESS");
144 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
147 531c3985 2020-03-18 stsp static const struct got_error *
148 b637eb2e 2022-02-23 thomas send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes,
149 b637eb2e 2022-02-23 thomas struct got_ratelimit *rl)
151 b637eb2e 2022-02-23 thomas const struct got_error *err;
152 b637eb2e 2022-02-23 thomas int elapsed = 0;
154 b637eb2e 2022-02-23 thomas if (rl) {
155 b637eb2e 2022-02-23 thomas err = got_ratelimit_check(&elapsed, rl);
156 b637eb2e 2022-02-23 thomas if (err || !elapsed)
157 b637eb2e 2022-02-23 thomas return err;
160 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
161 e70bf110 2020-03-22 stsp &bytes, sizeof(bytes)) == -1)
162 e70bf110 2020-03-22 stsp return got_error_from_errno(
163 e70bf110 2020-03-22 stsp "imsg_compose FETCH_DOWNLOAD_PROGRESS");
165 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
168 e70bf110 2020-03-22 stsp static const struct got_error *
169 1d72a2a0 2020-03-24 stsp send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
171 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
172 1d72a2a0 2020-03-24 stsp pack_sha1, SHA1_DIGEST_LENGTH) == -1)
173 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose FETCH");
174 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
177 e70bf110 2020-03-22 stsp static const struct got_error *
178 531c3985 2020-03-18 stsp fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
182 531c3985 2020-03-18 stsp if (len == 0)
183 531c3985 2020-03-18 stsp return NULL;
186 531c3985 2020-03-18 stsp * Truncate messages which exceed the maximum imsg payload size.
187 531c3985 2020-03-18 stsp * Server may send up to 64k.
189 531c3985 2020-03-18 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
190 531c3985 2020-03-18 stsp len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
192 531c3985 2020-03-18 stsp /* Only allow printable ASCII. */
193 531c3985 2020-03-18 stsp for (i = 0; i < len; i++) {
194 531c3985 2020-03-18 stsp if (isprint((unsigned char)buf[i]) ||
195 531c3985 2020-03-18 stsp isspace((unsigned char)buf[i]))
197 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
198 531c3985 2020-03-18 stsp "non-printable progress message received from server");
201 e70bf110 2020-03-22 stsp return send_fetch_server_progress(ibuf, buf, len);
204 8a29a085 2020-03-18 stsp static const struct got_error *
205 531c3985 2020-03-18 stsp fetch_error(const char *buf, size_t len)
207 531c3985 2020-03-18 stsp static char msg[1024];
210 531c3985 2020-03-18 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
211 6771d425 2022-11-17 thomas if (!isprint((unsigned char)buf[i]))
212 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
213 531c3985 2020-03-18 stsp "non-printable error message received from server");
214 531c3985 2020-03-18 stsp msg[i] = buf[i];
216 531c3985 2020-03-18 stsp msg[i] = '\0';
217 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
220 531c3985 2020-03-18 stsp static const struct got_error *
221 e70bf110 2020-03-22 stsp send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
223 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
224 e70bf110 2020-03-22 stsp size_t len, nsymrefs = 0;
225 e70bf110 2020-03-22 stsp struct got_pathlist_entry *pe;
227 e70bf110 2020-03-22 stsp len = sizeof(struct got_imsg_fetch_symrefs);
228 e70bf110 2020-03-22 stsp TAILQ_FOREACH(pe, symrefs, entry) {
229 e70bf110 2020-03-22 stsp const char *target = pe->data;
230 e70bf110 2020-03-22 stsp len += sizeof(struct got_imsg_fetch_symref) +
231 e70bf110 2020-03-22 stsp pe->path_len + strlen(target);
232 e70bf110 2020-03-22 stsp nsymrefs++;
235 e70bf110 2020-03-22 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
236 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
238 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
239 e70bf110 2020-03-22 stsp if (wbuf == NULL)
240 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create FETCH_SYMREFS");
242 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
243 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1)
244 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_SYMREFS");
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);
252 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_symref definition! */
253 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
254 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_SYMREFS");
255 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1)
256 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_SYMREFS");
257 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, name, name_len) == -1)
258 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_SYMREFS");
259 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, target, target_len) == -1)
260 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_SYMREFS");
263 e70bf110 2020-03-22 stsp wbuf->fd = -1;
264 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
265 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
268 e70bf110 2020-03-22 stsp static const struct got_error *
269 e70bf110 2020-03-22 stsp send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
270 e70bf110 2020-03-22 stsp const char *refname)
272 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
273 e70bf110 2020-03-22 stsp size_t len, reflen = strlen(refname);
275 e70bf110 2020-03-22 stsp len = sizeof(struct got_imsg_fetch_ref) + reflen;
276 e70bf110 2020-03-22 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
277 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
279 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
280 e70bf110 2020-03-22 stsp if (wbuf == NULL)
281 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create FETCH_REF");
283 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_ref definition! */
284 9228a15c 2023-02-03 thomas if (imsg_add(wbuf, refid, sizeof(*refid)) == -1)
285 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_REF");
286 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, refname, reflen) == -1)
287 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add FETCH_REF");
289 e70bf110 2020-03-22 stsp wbuf->fd = -1;
290 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
291 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
294 9d0a7ee3 2023-02-07 thomas static const struct got_error *
295 9d0a7ee3 2023-02-07 thomas fetch_ref(struct imsgbuf *ibuf, struct got_pathlist_head *have_refs,
296 9d0a7ee3 2023-02-07 thomas struct got_object_id *have, struct got_object_id *want,
297 9d0a7ee3 2023-02-07 thomas const char *refname, const char *id_str)
299 9d0a7ee3 2023-02-07 thomas const struct got_error *err;
300 9d0a7ee3 2023-02-07 thomas char *theirs = NULL, *mine = NULL;
302 9d0a7ee3 2023-02-07 thomas if (!got_parse_sha1_digest(want->sha1, id_str)) {
303 9d0a7ee3 2023-02-07 thomas err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
304 9d0a7ee3 2023-02-07 thomas goto done;
307 9d0a7ee3 2023-02-07 thomas match_remote_ref(have_refs, have, refname);
308 9d0a7ee3 2023-02-07 thomas err = send_fetch_ref(ibuf, want, refname);
310 9d0a7ee3 2023-02-07 thomas goto done;
312 9d0a7ee3 2023-02-07 thomas if (chattygot)
313 9d0a7ee3 2023-02-07 thomas fprintf(stderr, "%s: %s will be fetched\n",
314 9d0a7ee3 2023-02-07 thomas getprogname(), refname);
315 9d0a7ee3 2023-02-07 thomas if (chattygot > 1) {
316 9d0a7ee3 2023-02-07 thomas err = got_object_id_str(&theirs, want);
318 9d0a7ee3 2023-02-07 thomas goto done;
319 9d0a7ee3 2023-02-07 thomas err = got_object_id_str(&mine, have);
321 9d0a7ee3 2023-02-07 thomas goto done;
322 9d0a7ee3 2023-02-07 thomas fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
323 9d0a7ee3 2023-02-07 thomas getprogname(), theirs, getprogname(), mine);
326 9d0a7ee3 2023-02-07 thomas free(theirs);
327 9d0a7ee3 2023-02-07 thomas free(mine);
328 9d0a7ee3 2023-02-07 thomas return err;
331 e70bf110 2020-03-22 stsp static const struct got_error *
332 1d72a2a0 2020-03-24 stsp fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
333 659e7fbd 2020-03-20 stsp struct got_pathlist_head *have_refs, int fetch_all_branches,
334 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_branches,
335 0e4002ca 2020-03-21 stsp struct got_pathlist_head *wanted_refs, int list_refs_only,
336 e0380e3d 2023-02-17 thomas const char *worktree_branch, int no_head, struct imsgbuf *ibuf)
338 9ff10419 2020-03-18 stsp const struct got_error *err = NULL;
339 77d7d3bb 2021-09-05 stsp char buf[GOT_PKT_MAX];
340 93658fb9 2020-03-18 stsp char hashstr[SHA1_DIGEST_STRING_LENGTH];
341 93658fb9 2020-03-18 stsp struct got_object_id *have, *want;
342 8a29a085 2020-03-18 stsp int is_firstpkt = 1, nref = 0, refsz = 16;
343 7848a0e1 2020-03-19 stsp int i, n, nwant = 0, nhave = 0, acked = 0;
344 5672d305 2020-03-18 stsp off_t packsz = 0, last_reported_packsz = 0;
345 9d0a7ee3 2023-02-07 thomas char *id_str = NULL, *default_id_str = NULL, *refname = NULL;
346 00cd0e0a 2020-03-18 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
347 659e7fbd 2020-03-20 stsp const char *default_branch = NULL;
348 abe0f35f 2020-03-18 stsp struct got_pathlist_head symrefs;
349 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
350 406106ee 2020-03-20 stsp int sent_my_capabilites = 0, have_sidebands = 0;
351 659e7fbd 2020-03-20 stsp int found_branch = 0;
352 dc671e91 2020-03-24 stsp SHA1_CTX sha1_ctx;
353 dc671e91 2020-03-24 stsp uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
354 dc671e91 2020-03-24 stsp size_t sha1_buf_len = 0;
356 b637eb2e 2022-02-23 thomas struct got_ratelimit rl;
358 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
359 dc671e91 2020-03-24 stsp SHA1Init(&sha1_ctx);
360 b637eb2e 2022-02-23 thomas got_ratelimit_init(&rl, 0, 500);
362 93658fb9 2020-03-18 stsp have = malloc(refsz * sizeof(have[0]));
363 9ff10419 2020-03-18 stsp if (have == NULL)
364 9ff10419 2020-03-18 stsp return got_error_from_errno("malloc");
365 93658fb9 2020-03-18 stsp want = malloc(refsz * sizeof(want[0]));
366 9ff10419 2020-03-18 stsp if (want == NULL) {
367 9ff10419 2020-03-18 stsp err = got_error_from_errno("malloc");
370 9ff10419 2020-03-18 stsp while (1) {
371 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
374 9ff10419 2020-03-18 stsp if (n == 0)
376 a6f88e33 2020-03-18 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
377 531c3985 2020-03-18 stsp err = fetch_error(&buf[4], n - 4);
380 35add24a 2021-10-08 thomas free(id_str);
381 35add24a 2021-10-08 thomas free(refname);
382 bd3d9e54 2021-09-05 stsp err = got_gitproto_parse_refline(&id_str, &refname,
383 bd3d9e54 2021-09-05 stsp &server_capabilities, buf, n);
387 9d0a7ee3 2023-02-07 thomas if (refsz == nref + 1) {
388 84073f62 2023-02-07 thomas struct got_object_id *h, *w;
390 9d0a7ee3 2023-02-07 thomas refsz *= 2;
391 84073f62 2023-02-07 thomas h = reallocarray(have, refsz, sizeof(have[0]));
392 84073f62 2023-02-07 thomas if (h == NULL) {
393 9d0a7ee3 2023-02-07 thomas err = got_error_from_errno("reallocarray");
394 9d0a7ee3 2023-02-07 thomas goto done;
396 84073f62 2023-02-07 thomas have = h;
397 84073f62 2023-02-07 thomas w = reallocarray(want, refsz, sizeof(want[0]));
398 84073f62 2023-02-07 thomas if (w == NULL) {
399 9d0a7ee3 2023-02-07 thomas err = got_error_from_errno("reallocarray");
400 9d0a7ee3 2023-02-07 thomas goto done;
402 84073f62 2023-02-07 thomas want = w;
405 8a29a085 2020-03-18 stsp if (is_firstpkt) {
406 858b0dfb 2020-03-20 stsp if (chattygot && server_capabilities[0] != '\0')
407 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: server capabilities: %s\n",
408 858b0dfb 2020-03-20 stsp getprogname(), server_capabilities);
409 bd3d9e54 2021-09-05 stsp err = got_gitproto_match_capabilities(&my_capabilities,
410 bd3d9e54 2021-09-05 stsp &symrefs, server_capabilities,
411 bd3d9e54 2021-09-05 stsp got_capabilities, nitems(got_capabilities));
414 858b0dfb 2020-03-20 stsp if (chattygot)
415 2690194b 2020-03-21 stsp fprintf(stderr, "%s: my capabilities:%s\n",
416 a90356f7 2021-08-26 stsp getprogname(), my_capabilities != NULL ?
417 a90356f7 2021-08-26 stsp my_capabilities : "");
418 e70bf110 2020-03-22 stsp err = send_fetch_symrefs(ibuf, &symrefs);
421 7848a0e1 2020-03-19 stsp is_firstpkt = 0;
422 659e7fbd 2020-03-20 stsp if (!fetch_all_branches) {
423 659e7fbd 2020-03-20 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
424 659e7fbd 2020-03-20 stsp const char *name = pe->path;
425 659e7fbd 2020-03-20 stsp const char *symref_target = pe->data;
426 659e7fbd 2020-03-20 stsp if (strcmp(name, GOT_REF_HEAD) != 0)
428 659e7fbd 2020-03-20 stsp default_branch = symref_target;
432 c22ed3f5 2022-11-08 thomas if (default_branch)
433 c22ed3f5 2022-11-08 thomas continue;
435 2690194b 2020-03-21 stsp if (strstr(refname, "^{}")) {
436 2690194b 2020-03-21 stsp if (chattygot) {
437 2690194b 2020-03-21 stsp fprintf(stderr, "%s: ignoring %s\n",
438 2690194b 2020-03-21 stsp getprogname(), refname);
442 9d0a7ee3 2023-02-07 thomas if (default_branch && default_id_str == NULL &&
443 9d0a7ee3 2023-02-07 thomas strcmp(refname, default_branch) == 0) {
444 9d0a7ee3 2023-02-07 thomas default_id_str = strdup(id_str);
445 9d0a7ee3 2023-02-07 thomas if (default_id_str == NULL) {
446 9d0a7ee3 2023-02-07 thomas err = got_error_from_errno("strdup");
451 9d0a7ee3 2023-02-07 thomas if (list_refs_only || strncmp(refname, "refs/tags/", 10) == 0) {
452 9d0a7ee3 2023-02-07 thomas err = fetch_ref(ibuf, have_refs, &have[nref],
453 9d0a7ee3 2023-02-07 thomas &want[nref], refname, id_str);
457 9d0a7ee3 2023-02-07 thomas } else if (strncmp(refname, "refs/heads/", 11) == 0) {
458 9d0a7ee3 2023-02-07 thomas if (fetch_all_branches) {
459 9d0a7ee3 2023-02-07 thomas err = fetch_ref(ibuf, have_refs, &have[nref],
460 9d0a7ee3 2023-02-07 thomas &want[nref], refname, id_str);
462 9d0a7ee3 2023-02-07 thomas goto done;
464 9d0a7ee3 2023-02-07 thomas found_branch = 1;
465 9d0a7ee3 2023-02-07 thomas continue;
467 9d0a7ee3 2023-02-07 thomas TAILQ_FOREACH(pe, wanted_branches, entry) {
468 9d0a7ee3 2023-02-07 thomas if (match_branch(refname, pe->path))
471 9d0a7ee3 2023-02-07 thomas if (pe != NULL || (worktree_branch != NULL &&
472 9d0a7ee3 2023-02-07 thomas match_branch(refname, worktree_branch))) {
473 9d0a7ee3 2023-02-07 thomas err = fetch_ref(ibuf, have_refs, &have[nref],
474 9d0a7ee3 2023-02-07 thomas &want[nref], refname, id_str);
476 9d0a7ee3 2023-02-07 thomas goto done;
478 9d0a7ee3 2023-02-07 thomas found_branch = 1;
479 9d0a7ee3 2023-02-07 thomas } else if (chattygot) {
480 9d0a7ee3 2023-02-07 thomas fprintf(stderr, "%s: ignoring %s\n",
481 9d0a7ee3 2023-02-07 thomas getprogname(), refname);
484 9d0a7ee3 2023-02-07 thomas TAILQ_FOREACH(pe, wanted_refs, entry) {
485 9d0a7ee3 2023-02-07 thomas if (match_wanted_ref(refname, pe->path))
488 9d0a7ee3 2023-02-07 thomas if (pe != NULL) {
489 9d0a7ee3 2023-02-07 thomas err = fetch_ref(ibuf, have_refs, &have[nref],
490 9d0a7ee3 2023-02-07 thomas &want[nref], refname, id_str);
492 9d0a7ee3 2023-02-07 thomas goto done;
494 9d0a7ee3 2023-02-07 thomas } else if (chattygot) {
495 9d0a7ee3 2023-02-07 thomas fprintf(stderr, "%s: ignoring %s\n",
496 9d0a7ee3 2023-02-07 thomas getprogname(), refname);
501 41b0de12 2020-03-21 stsp if (list_refs_only)
504 e0380e3d 2023-02-17 thomas if (!found_branch && !no_head && default_branch && default_id_str &&
505 9d0a7ee3 2023-02-07 thomas strncmp(default_branch, "refs/heads/", 11) == 0) {
506 9d0a7ee3 2023-02-07 thomas err = fetch_ref(ibuf, have_refs, &have[nref],
507 9d0a7ee3 2023-02-07 thomas &want[nref], default_branch, default_id_str);
509 9d0a7ee3 2023-02-07 thomas goto done;
511 9d0a7ee3 2023-02-07 thomas found_branch = 1;
514 9d0a7ee3 2023-02-07 thomas /* Abort if we haven't found anything to fetch. */
515 9d0a7ee3 2023-02-07 thomas if (nref == 0) {
516 d392cc0a 2023-02-17 thomas struct got_pathlist_entry *pe;
517 d392cc0a 2023-02-17 thomas static char msg[PATH_MAX + 33];
519 d392cc0a 2023-02-17 thomas pe = TAILQ_FIRST(wanted_branches);
520 d392cc0a 2023-02-17 thomas if (pe) {
521 d392cc0a 2023-02-17 thomas snprintf(msg, sizeof(msg),
522 d392cc0a 2023-02-17 thomas "branch \"%s\" not found on server", pe->path);
523 d392cc0a 2023-02-17 thomas err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
524 d392cc0a 2023-02-17 thomas goto done;
527 d392cc0a 2023-02-17 thomas pe = TAILQ_FIRST(wanted_refs);
528 d392cc0a 2023-02-17 thomas if (pe) {
529 d392cc0a 2023-02-17 thomas snprintf(msg, sizeof(msg),
530 d392cc0a 2023-02-17 thomas "reference \"%s\" not found on server", pe->path);
531 d392cc0a 2023-02-17 thomas err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
532 d392cc0a 2023-02-17 thomas goto done;
535 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_FETCH_NO_BRANCH);
539 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
540 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
542 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
543 406106ee 2020-03-20 stsp n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
544 a90356f7 2021-08-26 stsp sent_my_capabilites || my_capabilities == NULL ?
545 a90356f7 2021-08-26 stsp "" : my_capabilities);
546 717a78d4 2022-08-16 thomas if (n < 0 || (size_t)n >= sizeof(buf)) {
547 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
550 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
553 858b0dfb 2020-03-20 stsp sent_my_capabilites = 1;
556 f024663d 2021-09-05 stsp err = got_pkt_flushpkt(fd, chattygot);
560 3c912d14 2020-03-19 stsp if (nwant == 0)
563 ea83355f 2021-10-08 thomas TAILQ_FOREACH(pe, have_refs, entry) {
564 ea83355f 2021-10-08 thomas struct got_object_id *id = pe->data;
565 ea83355f 2021-10-08 thomas got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
566 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
567 717a78d4 2022-08-16 thomas if (n < 0 || (size_t)n >= sizeof(buf)) {
568 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
571 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
577 7848a0e1 2020-03-19 stsp while (nhave > 0 && !acked) {
578 7848a0e1 2020-03-19 stsp struct got_object_id common_id;
580 7848a0e1 2020-03-19 stsp /* The server should ACK the object IDs we need. */
581 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
584 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
585 7848a0e1 2020-03-19 stsp err = fetch_error(&buf[4], n - 4);
588 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
589 7848a0e1 2020-03-19 stsp /* Server has not located our objects yet. */
592 7848a0e1 2020-03-19 stsp if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
593 7848a0e1 2020-03-19 stsp strncmp(buf, "ACK ", 4) != 0) {
594 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
595 7848a0e1 2020-03-19 stsp "unexpected message from server");
598 7848a0e1 2020-03-19 stsp if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
599 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
600 7848a0e1 2020-03-19 stsp "bad object ID in ACK packet from server");
606 406d5a24 2022-08-16 thomas n = strlcpy(buf, "done\n", sizeof(buf));
607 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
611 7848a0e1 2020-03-19 stsp if (nhave == 0) {
612 f024663d 2021-09-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
615 7848a0e1 2020-03-19 stsp if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
616 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
617 7848a0e1 2020-03-19 stsp "unexpected message from server");
622 858b0dfb 2020-03-20 stsp if (chattygot)
623 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: fetching...\n", getprogname());
625 531c3985 2020-03-18 stsp if (my_capabilities != NULL &&
626 531c3985 2020-03-18 stsp strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
627 531c3985 2020-03-18 stsp have_sidebands = 1;
629 9ff10419 2020-03-18 stsp while (1) {
630 dc671e91 2020-03-24 stsp ssize_t r = 0;
631 531c3985 2020-03-18 stsp int datalen = -1;
633 531c3985 2020-03-18 stsp if (have_sidebands) {
634 f024663d 2021-09-05 stsp err = got_pkt_readhdr(&datalen, fd, chattygot);
637 531c3985 2020-03-18 stsp if (datalen <= 0)
640 531c3985 2020-03-18 stsp /* Read sideband channel ID (one byte). */
641 531c3985 2020-03-18 stsp r = read(fd, buf, 1);
642 531c3985 2020-03-18 stsp if (r == -1) {
643 531c3985 2020-03-18 stsp err = got_error_from_errno("read");
646 531c3985 2020-03-18 stsp if (r != 1) {
647 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
648 531c3985 2020-03-18 stsp "short packet");
651 531c3985 2020-03-18 stsp if (datalen > sizeof(buf) - 5) {
652 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
653 531c3985 2020-03-18 stsp "bad packet length");
656 531c3985 2020-03-18 stsp datalen--; /* sideband ID has been read */
657 531c3985 2020-03-18 stsp if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
658 531c3985 2020-03-18 stsp /* Read packfile data. */
659 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
662 531c3985 2020-03-18 stsp if (r != datalen) {
663 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
664 531c3985 2020-03-18 stsp "packet too short");
667 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
668 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
671 531c3985 2020-03-18 stsp if (r != datalen) {
672 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
673 531c3985 2020-03-18 stsp "packet too short");
676 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
680 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
681 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
684 531c3985 2020-03-18 stsp if (r != datalen) {
685 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
686 531c3985 2020-03-18 stsp "packet too short");
689 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
691 98f64f14 2021-01-05 stsp } else if (buf[0] == 'A') {
692 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, datalen);
695 98f64f14 2021-01-05 stsp if (r != datalen) {
696 98f64f14 2021-01-05 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
697 98f64f14 2021-01-05 stsp "packet too short");
701 98f64f14 2021-01-05 stsp * Git server responds with ACK after 'done'
702 98f64f14 2021-01-05 stsp * even though multi_ack is disabled?!?
704 98f64f14 2021-01-05 stsp buf[r] = '\0';
705 98f64f14 2021-01-05 stsp if (strncmp(buf, "CK ", 3) == 0)
706 98f64f14 2021-01-05 stsp continue; /* ignore */
707 98f64f14 2021-01-05 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
708 98f64f14 2021-01-05 stsp "unexpected message from server");
711 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
712 531c3985 2020-03-18 stsp "unknown side-band received from server");
716 531c3985 2020-03-18 stsp /* No sideband channel. Every byte is packfile data. */
717 f024663d 2021-09-05 stsp err = got_pkt_readn(&r, fd, buf, sizeof buf);
720 531c3985 2020-03-18 stsp if (r <= 0)
725 dc671e91 2020-03-24 stsp * An expected SHA1 checksum sits at the end of the pack file.
726 dc671e91 2020-03-24 stsp * Since we don't know the file size ahead of time we have to
727 dc671e91 2020-03-24 stsp * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
728 dc671e91 2020-03-24 stsp * those bytes into our SHA1 checksum computation until we
729 dc671e91 2020-03-24 stsp * know for sure that additional pack file data bytes follow.
731 dc671e91 2020-03-24 stsp * We can assume r > 0 since otherwise the loop would exit.
733 dc671e91 2020-03-24 stsp if (r < SHA1_DIGEST_LENGTH) {
734 dc671e91 2020-03-24 stsp if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
736 dc671e91 2020-03-24 stsp * If there's enough buffered + read data to
737 dc671e91 2020-03-24 stsp * fill up the buffer then shift a sufficient
738 dc671e91 2020-03-24 stsp * amount of bytes out at the front to make
739 dc671e91 2020-03-24 stsp * room, mixing those bytes into the checksum.
741 9439b99a 2022-10-30 thomas if (sha1_buf_len > 0 &&
742 dc671e91 2020-03-24 stsp sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
743 9439b99a 2022-10-30 thomas size_t nshift = MIN(sha1_buf_len + r -
744 9439b99a 2022-10-30 thomas SHA1_DIGEST_LENGTH, sha1_buf_len);
745 9439b99a 2022-10-30 thomas SHA1Update(&sha1_ctx, sha1_buf, nshift);
746 9439b99a 2022-10-30 thomas memmove(sha1_buf, sha1_buf + nshift,
747 9439b99a 2022-10-30 thomas sha1_buf_len - nshift);
748 9439b99a 2022-10-30 thomas sha1_buf_len -= nshift;
751 dc671e91 2020-03-24 stsp /* Buffer potential checksum bytes. */
752 dc671e91 2020-03-24 stsp memcpy(sha1_buf + sha1_buf_len, buf, r);
753 dc671e91 2020-03-24 stsp sha1_buf_len += r;
756 dc671e91 2020-03-24 stsp * Mix in previously buffered bytes which
757 dc671e91 2020-03-24 stsp * are not part of the checksum after all.
759 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, sha1_buf, r);
761 dc671e91 2020-03-24 stsp /* Update potential checksum buffer. */
762 dc671e91 2020-03-24 stsp memmove(sha1_buf, sha1_buf + r,
763 dc671e91 2020-03-24 stsp sha1_buf_len - r);
764 dc671e91 2020-03-24 stsp memcpy(sha1_buf + sha1_buf_len - r, buf, r);
767 dc671e91 2020-03-24 stsp /* Mix in any previously buffered bytes. */
768 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
770 dc671e91 2020-03-24 stsp /* Mix in bytes read minus potential checksum bytes. */
771 dc671e91 2020-03-24 stsp SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
773 dc671e91 2020-03-24 stsp /* Buffer potential checksum bytes. */
774 dc671e91 2020-03-24 stsp memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
775 dc671e91 2020-03-24 stsp SHA1_DIGEST_LENGTH);
776 dc671e91 2020-03-24 stsp sha1_buf_len = SHA1_DIGEST_LENGTH;
779 531c3985 2020-03-18 stsp /* Write packfile data to temporary pack file. */
780 fe53745c 2020-03-18 stsp w = write(packfd, buf, r);
781 9ff10419 2020-03-18 stsp if (w == -1) {
782 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
785 fe53745c 2020-03-18 stsp if (w != r) {
786 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
789 531c3985 2020-03-18 stsp packsz += w;
791 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
792 5672d305 2020-03-18 stsp if (packsz > last_reported_packsz + 1024) {
793 b637eb2e 2022-02-23 thomas err = send_fetch_download_progress(ibuf, packsz, &rl);
796 5672d305 2020-03-18 stsp last_reported_packsz = packsz;
799 b637eb2e 2022-02-23 thomas err = send_fetch_download_progress(ibuf, packsz, NULL);
803 dc671e91 2020-03-24 stsp SHA1Final(pack_sha1, &sha1_ctx);
804 dc671e91 2020-03-24 stsp if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
805 dc671e91 2020-03-24 stsp memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
806 dc671e91 2020-03-24 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
807 dc671e91 2020-03-24 stsp "pack file checksum mismatch");
810 21c2d8be 2023-01-10 thomas got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
811 9ff10419 2020-03-18 stsp free(have);
812 9ff10419 2020-03-18 stsp free(want);
813 00cd0e0a 2020-03-18 stsp free(id_str);
814 9d0a7ee3 2023-02-07 thomas free(default_id_str);
815 00cd0e0a 2020-03-18 stsp free(refname);
816 00cd0e0a 2020-03-18 stsp free(server_capabilities);
817 8f2d01a6 2020-03-18 stsp return err;
822 93658fb9 2020-03-18 stsp main(int argc, char **argv)
824 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
825 d639f28b 2023-01-10 thomas int fetchfd = -1, packfd = -1;
826 1d72a2a0 2020-03-24 stsp uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
827 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
828 93658fb9 2020-03-18 stsp struct imsg imsg;
829 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
830 4ba14133 2020-03-20 stsp struct got_pathlist_head wanted_branches;
831 0e4002ca 2020-03-21 stsp struct got_pathlist_head wanted_refs;
832 4ba14133 2020-03-20 stsp struct got_imsg_fetch_request fetch_req;
833 659e7fbd 2020-03-20 stsp struct got_imsg_fetch_have_ref href;
834 4ba14133 2020-03-20 stsp struct got_imsg_fetch_wanted_branch wbranch;
835 0e4002ca 2020-03-21 stsp struct got_imsg_fetch_wanted_ref wref;
836 01bb5a15 2021-09-25 thomas.ad size_t datalen, i;
837 9d0a7ee3 2023-02-07 thomas char *worktree_branch = NULL;
839 7848a0e1 2020-03-19 stsp static int attached;
840 7848a0e1 2020-03-19 stsp while (!attached)
844 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
845 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
846 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
848 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
849 ffb5f621 2020-03-18 stsp #ifndef PROFILE
850 ffb5f621 2020-03-18 stsp /* revoke access to most system calls */
851 ffb5f621 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
852 ffb5f621 2020-03-18 stsp err = got_error_from_errno("pledge");
853 ffb5f621 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
857 97799ccd 2022-02-06 thomas /* revoke fs access */
858 97799ccd 2022-02-06 thomas if (landlock_no_fs() == -1) {
859 97799ccd 2022-02-06 thomas err = got_error_from_errno("landlock_no_fs");
860 97799ccd 2022-02-06 thomas got_privsep_send_error(&ibuf, err);
861 97799ccd 2022-02-06 thomas return 1;
863 5d120ea8 2022-06-23 op if (cap_enter() == -1) {
864 5d120ea8 2022-06-23 op err = got_error_from_errno("cap_enter");
865 5d120ea8 2022-06-23 op got_privsep_send_error(&ibuf, err);
869 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
871 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
872 93658fb9 2020-03-18 stsp err = NULL;
875 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
877 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
878 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
881 33501562 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
882 4ba14133 2020-03-20 stsp if (datalen < sizeof(fetch_req)) {
883 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
886 4ba14133 2020-03-20 stsp memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
887 4ba14133 2020-03-20 stsp fetchfd = imsg.fd;
889 9d0a7ee3 2023-02-07 thomas if (datalen != sizeof(fetch_req) +
890 9d0a7ee3 2023-02-07 thomas fetch_req.worktree_branch_len) {
891 9d0a7ee3 2023-02-07 thomas err = got_error(GOT_ERR_PRIVSEP_LEN);
892 9d0a7ee3 2023-02-07 thomas goto done;
895 9d0a7ee3 2023-02-07 thomas if (fetch_req.worktree_branch_len != 0) {
896 9d0a7ee3 2023-02-07 thomas worktree_branch = strndup(imsg.data +
897 9d0a7ee3 2023-02-07 thomas sizeof(fetch_req), fetch_req.worktree_branch_len);
898 9d0a7ee3 2023-02-07 thomas if (worktree_branch == NULL) {
899 9d0a7ee3 2023-02-07 thomas err = got_error_from_errno("strndup");
900 9d0a7ee3 2023-02-07 thomas goto done;
904 4ba14133 2020-03-20 stsp imsg_free(&imsg);
906 2690194b 2020-03-21 stsp if (fetch_req.verbosity > 0)
907 2690194b 2020-03-21 stsp chattygot += fetch_req.verbosity;
909 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_have_refs; i++) {
910 7848a0e1 2020-03-19 stsp struct got_object_id *id;
911 7848a0e1 2020-03-19 stsp char *refname;
913 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
915 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
916 4ba14133 2020-03-20 stsp err = NULL;
919 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
921 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
922 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
925 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
926 4ba14133 2020-03-20 stsp if (datalen < sizeof(href)) {
927 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
930 4ba14133 2020-03-20 stsp memcpy(&href, imsg.data, sizeof(href));
931 4ba14133 2020-03-20 stsp if (datalen - sizeof(href) < href.name_len) {
932 7848a0e1 2020-03-19 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
935 fcbb06bf 2023-01-14 thomas refname = strndup(imsg.data + sizeof(href), href.name_len);
936 7848a0e1 2020-03-19 stsp if (refname == NULL) {
937 fcbb06bf 2023-01-14 thomas err = got_error_from_errno("strndup");
941 7848a0e1 2020-03-19 stsp id = malloc(sizeof(*id));
942 7848a0e1 2020-03-19 stsp if (id == NULL) {
943 7848a0e1 2020-03-19 stsp free(refname);
944 7848a0e1 2020-03-19 stsp err = got_error_from_errno("malloc");
947 f9f54468 2023-02-03 thomas memcpy(id, &href.id, sizeof(*id));
948 7848a0e1 2020-03-19 stsp err = got_pathlist_append(&have_refs, refname, id);
950 7848a0e1 2020-03-19 stsp free(refname);
955 4ba14133 2020-03-20 stsp imsg_free(&imsg);
958 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_wanted_branches; i++) {
959 4ba14133 2020-03-20 stsp char *refname;
961 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
963 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
964 4ba14133 2020-03-20 stsp err = NULL;
967 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
969 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
970 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
973 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
974 4ba14133 2020-03-20 stsp if (datalen < sizeof(wbranch)) {
975 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
978 4ba14133 2020-03-20 stsp memcpy(&wbranch, imsg.data, sizeof(wbranch));
979 4ba14133 2020-03-20 stsp if (datalen - sizeof(wbranch) < wbranch.name_len) {
980 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
983 fcbb06bf 2023-01-14 thomas refname = strndup(imsg.data + sizeof(wbranch),
984 fcbb06bf 2023-01-14 thomas wbranch.name_len);
985 4ba14133 2020-03-20 stsp if (refname == NULL) {
986 fcbb06bf 2023-01-14 thomas err = got_error_from_errno("strndup");
990 4ba14133 2020-03-20 stsp err = got_pathlist_append(&wanted_branches, refname, NULL);
992 4ba14133 2020-03-20 stsp free(refname);
996 4ba14133 2020-03-20 stsp imsg_free(&imsg);
999 0e4002ca 2020-03-21 stsp for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1000 0e4002ca 2020-03-21 stsp char *refname;
1002 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1003 9ca26ac3 2021-08-06 stsp if (err) {
1004 0e4002ca 2020-03-21 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1005 0e4002ca 2020-03-21 stsp err = NULL;
1006 0e4002ca 2020-03-21 stsp goto done;
1008 0e4002ca 2020-03-21 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1009 0e4002ca 2020-03-21 stsp goto done;
1010 0e4002ca 2020-03-21 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1011 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1012 0e4002ca 2020-03-21 stsp goto done;
1014 0e4002ca 2020-03-21 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1015 0e4002ca 2020-03-21 stsp if (datalen < sizeof(wref)) {
1016 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1017 0e4002ca 2020-03-21 stsp goto done;
1019 0e4002ca 2020-03-21 stsp memcpy(&wref, imsg.data, sizeof(wref));
1020 0e4002ca 2020-03-21 stsp if (datalen - sizeof(wref) < wref.name_len) {
1021 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1022 0e4002ca 2020-03-21 stsp goto done;
1024 fcbb06bf 2023-01-14 thomas refname = strndup(imsg.data + sizeof(wref), wref.name_len);
1025 0e4002ca 2020-03-21 stsp if (refname == NULL) {
1026 fcbb06bf 2023-01-14 thomas err = got_error_from_errno("strndup");
1027 0e4002ca 2020-03-21 stsp goto done;
1030 0e4002ca 2020-03-21 stsp err = got_pathlist_append(&wanted_refs, refname, NULL);
1031 0e4002ca 2020-03-21 stsp if (err) {
1032 0e4002ca 2020-03-21 stsp free(refname);
1033 0e4002ca 2020-03-21 stsp goto done;
1036 0e4002ca 2020-03-21 stsp imsg_free(&imsg);
1039 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1040 9ca26ac3 2021-08-06 stsp if (err) {
1041 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1042 93658fb9 2020-03-18 stsp err = NULL;
1043 93658fb9 2020-03-18 stsp goto done;
1045 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1046 93658fb9 2020-03-18 stsp goto done;
1047 f826addf 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1048 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1049 93658fb9 2020-03-18 stsp goto done;
1051 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1052 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1053 93658fb9 2020-03-18 stsp goto done;
1055 93658fb9 2020-03-18 stsp packfd = imsg.fd;
1057 1d72a2a0 2020-03-24 stsp err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1058 41b0de12 2020-03-21 stsp fetch_req.fetch_all_branches, &wanted_branches,
1059 9d0a7ee3 2023-02-07 thomas &wanted_refs, fetch_req.list_refs_only,
1060 e0380e3d 2023-02-17 thomas worktree_branch, fetch_req.no_head, &ibuf);
1062 9d0a7ee3 2023-02-07 thomas free(worktree_branch);
1063 21c2d8be 2023-01-10 thomas got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
1064 21c2d8be 2023-01-10 thomas got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_PATH);
1065 0bec957e 2020-03-21 stsp if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1066 0bec957e 2020-03-21 stsp err = got_error_from_errno("close");
1067 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
1068 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
1069 9ff10419 2020-03-18 stsp if (err != NULL)
1070 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1072 1d72a2a0 2020-03-24 stsp err = send_fetch_done(&ibuf, pack_sha1);
1073 cf875574 2020-03-18 stsp if (err != NULL) {
1074 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1075 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);