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 93658fb9 2020-03-18 stsp #include <sys/queue.h>
19 93658fb9 2020-03-18 stsp #include <sys/uio.h>
20 93658fb9 2020-03-18 stsp #include <sys/time.h>
21 93658fb9 2020-03-18 stsp #include <sys/stat.h>
23 93658fb9 2020-03-18 stsp #include <stdint.h>
24 93658fb9 2020-03-18 stsp #include <errno.h>
25 93658fb9 2020-03-18 stsp #include <imsg.h>
26 93658fb9 2020-03-18 stsp #include <limits.h>
27 0fb910a4 2024-05-05 stsp #include <poll.h>
28 93658fb9 2020-03-18 stsp #include <signal.h>
29 93658fb9 2020-03-18 stsp #include <stdio.h>
30 93658fb9 2020-03-18 stsp #include <stdlib.h>
31 93658fb9 2020-03-18 stsp #include <string.h>
32 93658fb9 2020-03-18 stsp #include <ctype.h>
33 93658fb9 2020-03-18 stsp #include <sha1.h>
34 5822e79e 2023-02-23 op #include <sha2.h>
35 93658fb9 2020-03-18 stsp #include <fcntl.h>
36 81a12da5 2020-09-09 naddy #include <unistd.h>
37 93658fb9 2020-03-18 stsp #include <zlib.h>
38 93658fb9 2020-03-18 stsp #include <err.h>
40 93658fb9 2020-03-18 stsp #include "got_error.h"
41 93658fb9 2020-03-18 stsp #include "got_object.h"
42 abe0f35f 2020-03-18 stsp #include "got_path.h"
43 8a29a085 2020-03-18 stsp #include "got_version.h"
44 f1c6967f 2020-03-19 stsp #include "got_fetch.h"
45 659e7fbd 2020-03-20 stsp #include "got_reference.h"
47 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
48 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
49 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
50 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
51 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
52 0872c0b0 2020-03-18 stsp #include "got_lib_pack.h"
53 f024663d 2021-09-05 stsp #include "got_lib_pkt.h"
54 bd3d9e54 2021-09-05 stsp #include "got_lib_gitproto.h"
55 dcb64fea 2022-02-23 stsp #include "got_lib_ratelimit.h"
58 74737945 2022-10-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 8a29a085 2020-03-18 stsp #ifndef nitems
62 8a29a085 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 93658fb9 2020-03-18 stsp struct got_object *indexed;
66 858b0dfb 2020-03-20 stsp static int chattygot;
68 bd3d9e54 2021-09-05 stsp static const struct got_capability got_capabilities[] = {
69 bd3d9e54 2021-09-05 stsp { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
70 bd3d9e54 2021-09-05 stsp { GOT_CAPA_OFS_DELTA, NULL },
71 bd3d9e54 2021-09-05 stsp { GOT_CAPA_SIDE_BAND_64K, NULL },
75 7848a0e1 2020-03-19 stsp match_remote_ref(struct got_pathlist_head *have_refs,
76 188f8dcf 2023-02-07 stsp struct got_object_id *my_id, const char *refname)
78 33501562 2020-03-18 stsp struct got_pathlist_entry *pe;
80 7848a0e1 2020-03-19 stsp /* XXX zero-hash signifies we don't have this ref;
81 7848a0e1 2020-03-19 stsp * we should use a flag instead */
82 7848a0e1 2020-03-19 stsp memset(my_id, 0, sizeof(*my_id));
84 33501562 2020-03-18 stsp TAILQ_FOREACH(pe, have_refs, entry) {
85 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
86 5e91dae4 2022-08-30 stsp if (strcmp(pe->path, refname) == 0) {
87 7848a0e1 2020-03-19 stsp memcpy(my_id, id, sizeof(*my_id));
94 659e7fbd 2020-03-20 stsp match_branch(const char *branch, const char *wanted_branch)
96 659e7fbd 2020-03-20 stsp if (strncmp(branch, "refs/heads/", 11) != 0)
99 659e7fbd 2020-03-20 stsp if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
100 659e7fbd 2020-03-20 stsp wanted_branch += 11;
102 659e7fbd 2020-03-20 stsp return (strcmp(branch + 11, wanted_branch) == 0);
106 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
108 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
110 0e4002ca 2020-03-21 stsp refname += 5;
113 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
114 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
116 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
118 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
121 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
122 0e4002ca 2020-03-21 stsp wanted_ref += 5;
124 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
125 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
128 0e4002ca 2020-03-21 stsp /* Allow exact match. */
129 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
132 abe0f35f 2020-03-18 stsp static const struct got_error *
133 e70bf110 2020-03-22 stsp send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
135 e70bf110 2020-03-22 stsp if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
136 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
138 e70bf110 2020-03-22 stsp if (msglen == 0)
139 e70bf110 2020-03-22 stsp return NULL;
141 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
142 e70bf110 2020-03-22 stsp msg, msglen) == -1)
143 e70bf110 2020-03-22 stsp return got_error_from_errno(
144 e70bf110 2020-03-22 stsp "imsg_compose FETCH_SERVER_PROGRESS");
146 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
149 531c3985 2020-03-18 stsp static const struct got_error *
150 dcb64fea 2022-02-23 stsp send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes,
151 dcb64fea 2022-02-23 stsp struct got_ratelimit *rl)
153 dcb64fea 2022-02-23 stsp const struct got_error *err;
154 dcb64fea 2022-02-23 stsp int elapsed = 0;
157 dcb64fea 2022-02-23 stsp err = got_ratelimit_check(&elapsed, rl);
158 dcb64fea 2022-02-23 stsp if (err || !elapsed)
159 dcb64fea 2022-02-23 stsp return err;
162 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
163 e70bf110 2020-03-22 stsp &bytes, sizeof(bytes)) == -1)
164 e70bf110 2020-03-22 stsp return got_error_from_errno(
165 e70bf110 2020-03-22 stsp "imsg_compose FETCH_DOWNLOAD_PROGRESS");
167 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
170 e70bf110 2020-03-22 stsp static const struct got_error *
171 1d72a2a0 2020-03-24 stsp send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
173 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
174 1d72a2a0 2020-03-24 stsp pack_sha1, SHA1_DIGEST_LENGTH) == -1)
175 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose FETCH");
176 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
179 e70bf110 2020-03-22 stsp static const struct got_error *
180 531c3985 2020-03-18 stsp fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
184 531c3985 2020-03-18 stsp if (len == 0)
185 531c3985 2020-03-18 stsp return NULL;
188 531c3985 2020-03-18 stsp * Truncate messages which exceed the maximum imsg payload size.
189 531c3985 2020-03-18 stsp * Server may send up to 64k.
191 531c3985 2020-03-18 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
192 531c3985 2020-03-18 stsp len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
194 531c3985 2020-03-18 stsp /* Only allow printable ASCII. */
195 531c3985 2020-03-18 stsp for (i = 0; i < len; i++) {
196 531c3985 2020-03-18 stsp if (isprint((unsigned char)buf[i]) ||
197 531c3985 2020-03-18 stsp isspace((unsigned char)buf[i]))
199 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
200 531c3985 2020-03-18 stsp "non-printable progress message received from server");
203 e70bf110 2020-03-22 stsp return send_fetch_server_progress(ibuf, buf, len);
206 8a29a085 2020-03-18 stsp static const struct got_error *
207 531c3985 2020-03-18 stsp fetch_error(const char *buf, size_t len)
209 531c3985 2020-03-18 stsp static char msg[1024];
212 531c3985 2020-03-18 stsp for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
213 99fd9ff4 2022-11-17 op if (!isprint((unsigned char)buf[i]))
214 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKET,
215 531c3985 2020-03-18 stsp "non-printable error message received from server");
216 531c3985 2020-03-18 stsp msg[i] = buf[i];
218 531c3985 2020-03-18 stsp msg[i] = '\0';
219 531c3985 2020-03-18 stsp return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
222 531c3985 2020-03-18 stsp static const struct got_error *
223 e70bf110 2020-03-22 stsp send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
225 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
226 e70bf110 2020-03-22 stsp size_t len, nsymrefs = 0;
227 e70bf110 2020-03-22 stsp struct got_pathlist_entry *pe;
229 e70bf110 2020-03-22 stsp len = sizeof(struct got_imsg_fetch_symrefs);
230 e70bf110 2020-03-22 stsp TAILQ_FOREACH(pe, symrefs, entry) {
231 e70bf110 2020-03-22 stsp const char *target = pe->data;
232 e70bf110 2020-03-22 stsp len += sizeof(struct got_imsg_fetch_symref) +
233 e70bf110 2020-03-22 stsp pe->path_len + strlen(target);
234 e70bf110 2020-03-22 stsp nsymrefs++;
237 e70bf110 2020-03-22 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
238 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
240 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
241 e70bf110 2020-03-22 stsp if (wbuf == NULL)
242 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create FETCH_SYMREFS");
244 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
245 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1)
246 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add FETCH_SYMREFS");
248 e70bf110 2020-03-22 stsp TAILQ_FOREACH(pe, symrefs, entry) {
249 e70bf110 2020-03-22 stsp const char *name = pe->path;
250 e70bf110 2020-03-22 stsp size_t name_len = pe->path_len;
251 e70bf110 2020-03-22 stsp const char *target = pe->data;
252 e70bf110 2020-03-22 stsp size_t target_len = strlen(target);
254 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_symref definition! */
255 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
256 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add FETCH_SYMREFS");
257 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1)
258 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add FETCH_SYMREFS");
259 1453347d 2022-05-19 stsp if (imsg_add(wbuf, name, name_len) == -1)
260 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add FETCH_SYMREFS");
261 1453347d 2022-05-19 stsp if (imsg_add(wbuf, target, target_len) == -1)
262 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add FETCH_SYMREFS");
265 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
266 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
269 e70bf110 2020-03-22 stsp static const struct got_error *
270 e70bf110 2020-03-22 stsp send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
271 e70bf110 2020-03-22 stsp const char *refname)
273 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
274 e70bf110 2020-03-22 stsp size_t len, reflen = strlen(refname);
276 e70bf110 2020-03-22 stsp len = sizeof(struct got_imsg_fetch_ref) + reflen;
277 e70bf110 2020-03-22 stsp if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
278 e70bf110 2020-03-22 stsp return got_error(GOT_ERR_NO_SPACE);
280 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
281 e70bf110 2020-03-22 stsp if (wbuf == NULL)
282 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create FETCH_REF");
284 e70bf110 2020-03-22 stsp /* Keep in sync with struct got_imsg_fetch_ref definition! */
285 0701e66c 2023-02-01 op if (imsg_add(wbuf, refid, sizeof(*refid)) == -1)
286 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add FETCH_REF");
287 1453347d 2022-05-19 stsp if (imsg_add(wbuf, refname, reflen) == -1)
288 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add FETCH_REF");
290 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
291 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
294 188f8dcf 2023-02-07 stsp static const struct got_error *
295 188f8dcf 2023-02-07 stsp fetch_ref(struct imsgbuf *ibuf, struct got_pathlist_head *have_refs,
296 188f8dcf 2023-02-07 stsp struct got_object_id *have, struct got_object_id *want,
297 188f8dcf 2023-02-07 stsp const char *refname, const char *id_str)
299 188f8dcf 2023-02-07 stsp const struct got_error *err;
300 188f8dcf 2023-02-07 stsp char *theirs = NULL, *mine = NULL;
302 87a3ab84 2023-02-23 op if (!got_parse_object_id(want, id_str, GOT_HASH_SHA1)) {
303 188f8dcf 2023-02-07 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
307 188f8dcf 2023-02-07 stsp match_remote_ref(have_refs, have, refname);
308 188f8dcf 2023-02-07 stsp err = send_fetch_ref(ibuf, want, refname);
312 188f8dcf 2023-02-07 stsp if (chattygot)
313 188f8dcf 2023-02-07 stsp fprintf(stderr, "%s: %s will be fetched\n",
314 188f8dcf 2023-02-07 stsp getprogname(), refname);
315 188f8dcf 2023-02-07 stsp if (chattygot > 1) {
316 188f8dcf 2023-02-07 stsp err = got_object_id_str(&theirs, want);
319 188f8dcf 2023-02-07 stsp err = got_object_id_str(&mine, have);
322 188f8dcf 2023-02-07 stsp fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
323 188f8dcf 2023-02-07 stsp getprogname(), theirs, getprogname(), mine);
326 188f8dcf 2023-02-07 stsp free(theirs);
327 188f8dcf 2023-02-07 stsp free(mine);
328 188f8dcf 2023-02-07 stsp 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 118a625d 2023-02-18 mark const char *worktree_branch, const char *remote_head,
337 118a625d 2023-02-18 mark int no_head, struct imsgbuf *ibuf)
339 9ff10419 2020-03-18 stsp const struct got_error *err = NULL;
340 77d7d3bb 2021-09-05 stsp char buf[GOT_PKT_MAX];
341 93658fb9 2020-03-18 stsp char hashstr[SHA1_DIGEST_STRING_LENGTH];
342 93658fb9 2020-03-18 stsp struct got_object_id *have, *want;
343 8a29a085 2020-03-18 stsp int is_firstpkt = 1, nref = 0, refsz = 16;
344 7848a0e1 2020-03-19 stsp int i, n, nwant = 0, nhave = 0, acked = 0;
345 5672d305 2020-03-18 stsp off_t packsz = 0, last_reported_packsz = 0;
346 4bff57b4 2023-02-14 stsp char *id_str = NULL, *default_id_str = NULL, *refname = NULL;
347 00cd0e0a 2020-03-18 stsp char *server_capabilities = NULL, *my_capabilities = NULL;
348 659e7fbd 2020-03-20 stsp const char *default_branch = NULL;
349 abe0f35f 2020-03-18 stsp struct got_pathlist_head symrefs;
350 abe0f35f 2020-03-18 stsp struct got_pathlist_entry *pe;
351 406106ee 2020-03-20 stsp int sent_my_capabilites = 0, have_sidebands = 0;
352 4bff57b4 2023-02-14 stsp int found_branch = 0;
353 ae25a666 2023-02-23 op struct got_hash ctx;
354 dc671e91 2020-03-24 stsp uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
355 dc671e91 2020-03-24 stsp size_t sha1_buf_len = 0;
357 dcb64fea 2022-02-23 stsp struct got_ratelimit rl;
359 abe0f35f 2020-03-18 stsp TAILQ_INIT(&symrefs);
360 ae25a666 2023-02-23 op got_hash_init(&ctx, GOT_HASH_SHA1);
361 dcb64fea 2022-02-23 stsp got_ratelimit_init(&rl, 0, 500);
363 93658fb9 2020-03-18 stsp have = malloc(refsz * sizeof(have[0]));
364 9ff10419 2020-03-18 stsp if (have == NULL)
365 9ff10419 2020-03-18 stsp return got_error_from_errno("malloc");
366 93658fb9 2020-03-18 stsp want = malloc(refsz * sizeof(want[0]));
367 9ff10419 2020-03-18 stsp if (want == NULL) {
368 9ff10419 2020-03-18 stsp err = got_error_from_errno("malloc");
371 9ff10419 2020-03-18 stsp while (1) {
372 0fb910a4 2024-05-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot,
376 9ff10419 2020-03-18 stsp if (n == 0)
378 a6f88e33 2020-03-18 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
379 531c3985 2020-03-18 stsp err = fetch_error(&buf[4], n - 4);
382 e99d9267 2021-10-07 stsp free(id_str);
383 e99d9267 2021-10-07 stsp free(refname);
384 bd3d9e54 2021-09-05 stsp err = got_gitproto_parse_refline(&id_str, &refname,
385 bd3d9e54 2021-09-05 stsp &server_capabilities, buf, n);
389 188f8dcf 2023-02-07 stsp if (refsz == nref + 1) {
390 ec218e16 2023-02-07 mark struct got_object_id *h, *w;
392 188f8dcf 2023-02-07 stsp refsz *= 2;
393 ec218e16 2023-02-07 mark h = reallocarray(have, refsz, sizeof(have[0]));
394 ec218e16 2023-02-07 mark if (h == NULL) {
395 188f8dcf 2023-02-07 stsp err = got_error_from_errno("reallocarray");
399 ec218e16 2023-02-07 mark w = reallocarray(want, refsz, sizeof(want[0]));
400 ec218e16 2023-02-07 mark if (w == NULL) {
401 188f8dcf 2023-02-07 stsp err = got_error_from_errno("reallocarray");
407 8a29a085 2020-03-18 stsp if (is_firstpkt) {
408 858b0dfb 2020-03-20 stsp if (chattygot && server_capabilities[0] != '\0')
409 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: server capabilities: %s\n",
410 858b0dfb 2020-03-20 stsp getprogname(), server_capabilities);
411 bd3d9e54 2021-09-05 stsp err = got_gitproto_match_capabilities(&my_capabilities,
412 bd3d9e54 2021-09-05 stsp &symrefs, server_capabilities,
413 bd3d9e54 2021-09-05 stsp got_capabilities, nitems(got_capabilities));
416 858b0dfb 2020-03-20 stsp if (chattygot)
417 2690194b 2020-03-21 stsp fprintf(stderr, "%s: my capabilities:%s\n",
418 a90356f7 2021-08-26 stsp getprogname(), my_capabilities != NULL ?
419 a90356f7 2021-08-26 stsp my_capabilities : "");
420 e70bf110 2020-03-22 stsp err = send_fetch_symrefs(ibuf, &symrefs);
423 7848a0e1 2020-03-19 stsp is_firstpkt = 0;
424 659e7fbd 2020-03-20 stsp if (!fetch_all_branches) {
425 659e7fbd 2020-03-20 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
426 659e7fbd 2020-03-20 stsp const char *name = pe->path;
427 659e7fbd 2020-03-20 stsp const char *symref_target = pe->data;
428 659e7fbd 2020-03-20 stsp if (strcmp(name, GOT_REF_HEAD) != 0)
430 659e7fbd 2020-03-20 stsp default_branch = symref_target;
434 c9f1ac46 2022-11-08 stsp if (default_branch)
437 2690194b 2020-03-21 stsp if (strstr(refname, "^{}")) {
438 2690194b 2020-03-21 stsp if (chattygot) {
439 2690194b 2020-03-21 stsp fprintf(stderr, "%s: ignoring %s\n",
440 2690194b 2020-03-21 stsp getprogname(), refname);
444 4bff57b4 2023-02-14 stsp if (default_branch && default_id_str == NULL &&
445 4bff57b4 2023-02-14 stsp strcmp(refname, default_branch) == 0) {
446 4bff57b4 2023-02-14 stsp default_id_str = strdup(id_str);
447 4bff57b4 2023-02-14 stsp if (default_id_str == NULL) {
448 4bff57b4 2023-02-14 stsp err = got_error_from_errno("strdup");
453 188f8dcf 2023-02-07 stsp if (list_refs_only || strncmp(refname, "refs/tags/", 10) == 0) {
454 188f8dcf 2023-02-07 stsp err = fetch_ref(ibuf, have_refs, &have[nref],
455 188f8dcf 2023-02-07 stsp &want[nref], refname, id_str);
459 188f8dcf 2023-02-07 stsp } else if (strncmp(refname, "refs/heads/", 11) == 0) {
460 188f8dcf 2023-02-07 stsp if (fetch_all_branches) {
461 188f8dcf 2023-02-07 stsp err = fetch_ref(ibuf, have_refs, &have[nref],
462 188f8dcf 2023-02-07 stsp &want[nref], refname, id_str);
466 4bff57b4 2023-02-14 stsp found_branch = 1;
469 188f8dcf 2023-02-07 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
470 188f8dcf 2023-02-07 stsp if (match_branch(refname, pe->path))
473 188f8dcf 2023-02-07 stsp if (pe != NULL || (worktree_branch != NULL &&
474 188f8dcf 2023-02-07 stsp match_branch(refname, worktree_branch))) {
475 188f8dcf 2023-02-07 stsp err = fetch_ref(ibuf, have_refs, &have[nref],
476 188f8dcf 2023-02-07 stsp &want[nref], refname, id_str);
480 4bff57b4 2023-02-14 stsp found_branch = 1;
481 188f8dcf 2023-02-07 stsp } else if (chattygot) {
482 188f8dcf 2023-02-07 stsp fprintf(stderr, "%s: ignoring %s\n",
483 188f8dcf 2023-02-07 stsp getprogname(), refname);
486 188f8dcf 2023-02-07 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
487 188f8dcf 2023-02-07 stsp if (match_wanted_ref(refname, pe->path))
490 188f8dcf 2023-02-07 stsp if (pe != NULL) {
491 188f8dcf 2023-02-07 stsp err = fetch_ref(ibuf, have_refs, &have[nref],
492 188f8dcf 2023-02-07 stsp &want[nref], refname, id_str);
496 188f8dcf 2023-02-07 stsp } else if (chattygot) {
497 188f8dcf 2023-02-07 stsp fprintf(stderr, "%s: ignoring %s\n",
498 188f8dcf 2023-02-07 stsp getprogname(), refname);
503 41b0de12 2020-03-21 stsp if (list_refs_only)
507 118a625d 2023-02-18 mark * If -b was not used and either none of the requested branches
508 118a625d 2023-02-18 mark * (got.conf, worktree) were found or the client already has the
509 118a625d 2023-02-18 mark * remote HEAD symref but its target changed, fetch remote's HEAD.
511 118a625d 2023-02-18 mark if (!no_head && default_branch && default_id_str &&
512 4bff57b4 2023-02-14 stsp strncmp(default_branch, "refs/heads/", 11) == 0) {
513 118a625d 2023-02-18 mark int remote_head_changed = 0;
515 118a625d 2023-02-18 mark if (remote_head) {
516 118a625d 2023-02-18 mark if (strcmp(remote_head, default_branch + 11) != 0)
517 118a625d 2023-02-18 mark remote_head_changed = 1;
520 118a625d 2023-02-18 mark if (!found_branch || remote_head_changed) {
521 118a625d 2023-02-18 mark err = fetch_ref(ibuf, have_refs, &have[nref],
522 118a625d 2023-02-18 mark &want[nref], default_branch, default_id_str);
529 188f8dcf 2023-02-07 stsp /* Abort if we haven't found anything to fetch. */
530 188f8dcf 2023-02-07 stsp if (nref == 0) {
531 f72ce919 2023-02-13 mark struct got_pathlist_entry *pe;
532 f72ce919 2023-02-13 mark static char msg[PATH_MAX + 33];
534 f72ce919 2023-02-13 mark pe = TAILQ_FIRST(wanted_branches);
536 f72ce919 2023-02-13 mark snprintf(msg, sizeof(msg),
537 f72ce919 2023-02-13 mark "branch \"%s\" not found on server", pe->path);
538 f72ce919 2023-02-13 mark err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
542 f72ce919 2023-02-13 mark pe = TAILQ_FIRST(wanted_refs);
544 f72ce919 2023-02-13 mark snprintf(msg, sizeof(msg),
545 f72ce919 2023-02-13 mark "reference \"%s\" not found on server", pe->path);
546 f72ce919 2023-02-13 mark err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
550 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_FETCH_NO_BRANCH);
554 cf875574 2020-03-18 stsp for (i = 0; i < nref; i++) {
555 93658fb9 2020-03-18 stsp if (got_object_id_cmp(&have[i], &want[i]) == 0)
557 93658fb9 2020-03-18 stsp got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
558 406106ee 2020-03-20 stsp n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
559 a90356f7 2021-08-26 stsp sent_my_capabilites || my_capabilities == NULL ?
560 a90356f7 2021-08-26 stsp "" : my_capabilities);
561 438d0cc3 2022-08-16 op if (n < 0 || (size_t)n >= sizeof(buf)) {
562 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
565 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
568 858b0dfb 2020-03-20 stsp sent_my_capabilites = 1;
571 f024663d 2021-09-05 stsp err = got_pkt_flushpkt(fd, chattygot);
575 3c912d14 2020-03-19 stsp if (nwant == 0)
578 dd088d95 2021-10-06 stsp TAILQ_FOREACH(pe, have_refs, entry) {
579 dd088d95 2021-10-06 stsp struct got_object_id *id = pe->data;
580 dd088d95 2021-10-06 stsp got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
581 93658fb9 2020-03-18 stsp n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
582 438d0cc3 2022-08-16 op if (n < 0 || (size_t)n >= sizeof(buf)) {
583 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_NO_SPACE);
586 f024663d 2021-09-05 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
592 1a0d06a3 2023-02-20 stsp n = strlcpy(buf, "done\n", sizeof(buf));
593 1a0d06a3 2023-02-20 stsp err = got_pkt_writepkt(fd, buf, n, chattygot);
597 7848a0e1 2020-03-19 stsp while (nhave > 0 && !acked) {
598 7848a0e1 2020-03-19 stsp struct got_object_id common_id;
600 7848a0e1 2020-03-19 stsp /* The server should ACK the object IDs we need. */
601 0fb910a4 2024-05-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot,
605 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
606 7848a0e1 2020-03-19 stsp err = fetch_error(&buf[4], n - 4);
609 7848a0e1 2020-03-19 stsp if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
611 1a0d06a3 2023-02-20 stsp * Server could not find a common ancestor.
612 1a0d06a3 2023-02-20 stsp * Perhaps it is an out-of-date mirror, or there
613 1a0d06a3 2023-02-20 stsp * is a repository with unrelated history.
617 7848a0e1 2020-03-19 stsp if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
618 7848a0e1 2020-03-19 stsp strncmp(buf, "ACK ", 4) != 0) {
619 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
620 7848a0e1 2020-03-19 stsp "unexpected message from server");
623 87a3ab84 2023-02-23 op if (!got_parse_object_id(&common_id, buf + 4,
624 87a3ab84 2023-02-23 op GOT_HASH_SHA1)) {
625 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
626 7848a0e1 2020-03-19 stsp "bad object ID in ACK packet from server");
632 7848a0e1 2020-03-19 stsp if (nhave == 0) {
633 0fb910a4 2024-05-05 stsp err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot,
637 7848a0e1 2020-03-19 stsp if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
638 7848a0e1 2020-03-19 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
639 7848a0e1 2020-03-19 stsp "unexpected message from server");
644 858b0dfb 2020-03-20 stsp if (chattygot)
645 858b0dfb 2020-03-20 stsp fprintf(stderr, "%s: fetching...\n", getprogname());
647 531c3985 2020-03-18 stsp if (my_capabilities != NULL &&
648 531c3985 2020-03-18 stsp strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
649 531c3985 2020-03-18 stsp have_sidebands = 1;
651 9ff10419 2020-03-18 stsp while (1) {
652 dc671e91 2020-03-24 stsp ssize_t r = 0;
653 531c3985 2020-03-18 stsp int datalen = -1;
655 531c3985 2020-03-18 stsp if (have_sidebands) {
656 0fb910a4 2024-05-05 stsp err = got_pkt_readhdr(&datalen, fd, chattygot, INFTIM);
659 531c3985 2020-03-18 stsp if (datalen <= 0)
662 531c3985 2020-03-18 stsp /* Read sideband channel ID (one byte). */
663 531c3985 2020-03-18 stsp r = read(fd, buf, 1);
664 531c3985 2020-03-18 stsp if (r == -1) {
665 531c3985 2020-03-18 stsp err = got_error_from_errno("read");
668 531c3985 2020-03-18 stsp if (r != 1) {
669 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
670 531c3985 2020-03-18 stsp "short packet");
673 531c3985 2020-03-18 stsp if (datalen > sizeof(buf) - 5) {
674 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
675 531c3985 2020-03-18 stsp "bad packet length");
678 531c3985 2020-03-18 stsp datalen--; /* sideband ID has been read */
679 531c3985 2020-03-18 stsp if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
680 531c3985 2020-03-18 stsp /* Read packfile data. */
681 0fb910a4 2024-05-05 stsp err = got_pkt_readn(&r, fd, buf, datalen,
685 531c3985 2020-03-18 stsp if (r != datalen) {
686 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
687 531c3985 2020-03-18 stsp "packet too short");
690 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
691 0fb910a4 2024-05-05 stsp err = got_pkt_readn(&r, fd, buf, datalen,
695 531c3985 2020-03-18 stsp if (r != datalen) {
696 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
697 531c3985 2020-03-18 stsp "packet too short");
700 531c3985 2020-03-18 stsp err = fetch_progress(ibuf, buf, r);
704 531c3985 2020-03-18 stsp } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
705 0fb910a4 2024-05-05 stsp err = got_pkt_readn(&r, fd, buf, datalen,
709 531c3985 2020-03-18 stsp if (r != datalen) {
710 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
711 531c3985 2020-03-18 stsp "packet too short");
714 531c3985 2020-03-18 stsp err = fetch_error(buf, r);
716 98f64f14 2021-01-05 stsp } else if (buf[0] == 'A') {
717 0fb910a4 2024-05-05 stsp err = got_pkt_readn(&r, fd, buf, datalen,
721 98f64f14 2021-01-05 stsp if (r != datalen) {
722 98f64f14 2021-01-05 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
723 98f64f14 2021-01-05 stsp "packet too short");
727 98f64f14 2021-01-05 stsp * Git server responds with ACK after 'done'
728 98f64f14 2021-01-05 stsp * even though multi_ack is disabled?!?
730 98f64f14 2021-01-05 stsp buf[r] = '\0';
731 98f64f14 2021-01-05 stsp if (strncmp(buf, "CK ", 3) == 0)
732 98f64f14 2021-01-05 stsp continue; /* ignore */
733 98f64f14 2021-01-05 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
734 98f64f14 2021-01-05 stsp "unexpected message from server");
737 531c3985 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
738 531c3985 2020-03-18 stsp "unknown side-band received from server");
742 531c3985 2020-03-18 stsp /* No sideband channel. Every byte is packfile data. */
743 0fb910a4 2024-05-05 stsp err = got_pkt_readn(&r, fd, buf, sizeof buf, INFTIM);
746 531c3985 2020-03-18 stsp if (r <= 0)
751 dc671e91 2020-03-24 stsp * An expected SHA1 checksum sits at the end of the pack file.
752 dc671e91 2020-03-24 stsp * Since we don't know the file size ahead of time we have to
753 dc671e91 2020-03-24 stsp * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
754 dc671e91 2020-03-24 stsp * those bytes into our SHA1 checksum computation until we
755 dc671e91 2020-03-24 stsp * know for sure that additional pack file data bytes follow.
757 dc671e91 2020-03-24 stsp * We can assume r > 0 since otherwise the loop would exit.
759 dc671e91 2020-03-24 stsp if (r < SHA1_DIGEST_LENGTH) {
760 dc671e91 2020-03-24 stsp if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
762 dc671e91 2020-03-24 stsp * If there's enough buffered + read data to
763 dc671e91 2020-03-24 stsp * fill up the buffer then shift a sufficient
764 dc671e91 2020-03-24 stsp * amount of bytes out at the front to make
765 dc671e91 2020-03-24 stsp * room, mixing those bytes into the checksum.
767 74737945 2022-10-30 stsp if (sha1_buf_len > 0 &&
768 dc671e91 2020-03-24 stsp sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
769 74737945 2022-10-30 stsp size_t nshift = MIN(sha1_buf_len + r -
770 74737945 2022-10-30 stsp SHA1_DIGEST_LENGTH, sha1_buf_len);
771 ae25a666 2023-02-23 op got_hash_update(&ctx, sha1_buf,
773 74737945 2022-10-30 stsp memmove(sha1_buf, sha1_buf + nshift,
774 74737945 2022-10-30 stsp sha1_buf_len - nshift);
775 74737945 2022-10-30 stsp sha1_buf_len -= nshift;
778 dc671e91 2020-03-24 stsp /* Buffer potential checksum bytes. */
779 dc671e91 2020-03-24 stsp memcpy(sha1_buf + sha1_buf_len, buf, r);
780 dc671e91 2020-03-24 stsp sha1_buf_len += r;
783 dc671e91 2020-03-24 stsp * Mix in previously buffered bytes which
784 dc671e91 2020-03-24 stsp * are not part of the checksum after all.
786 ae25a666 2023-02-23 op got_hash_update(&ctx, sha1_buf, r);
788 dc671e91 2020-03-24 stsp /* Update potential checksum buffer. */
789 dc671e91 2020-03-24 stsp memmove(sha1_buf, sha1_buf + r,
790 dc671e91 2020-03-24 stsp sha1_buf_len - r);
791 dc671e91 2020-03-24 stsp memcpy(sha1_buf + sha1_buf_len - r, buf, r);
794 dc671e91 2020-03-24 stsp /* Mix in any previously buffered bytes. */
795 ae25a666 2023-02-23 op got_hash_update(&ctx, sha1_buf, sha1_buf_len);
797 dc671e91 2020-03-24 stsp /* Mix in bytes read minus potential checksum bytes. */
798 ae25a666 2023-02-23 op got_hash_update(&ctx, buf, r - SHA1_DIGEST_LENGTH);
800 dc671e91 2020-03-24 stsp /* Buffer potential checksum bytes. */
801 dc671e91 2020-03-24 stsp memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
802 dc671e91 2020-03-24 stsp SHA1_DIGEST_LENGTH);
803 dc671e91 2020-03-24 stsp sha1_buf_len = SHA1_DIGEST_LENGTH;
806 531c3985 2020-03-18 stsp /* Write packfile data to temporary pack file. */
807 fe53745c 2020-03-18 stsp w = write(packfd, buf, r);
808 9ff10419 2020-03-18 stsp if (w == -1) {
809 9ff10419 2020-03-18 stsp err = got_error_from_errno("write");
812 fe53745c 2020-03-18 stsp if (w != r) {
813 9ff10419 2020-03-18 stsp err = got_error(GOT_ERR_IO);
816 531c3985 2020-03-18 stsp packsz += w;
818 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
819 5672d305 2020-03-18 stsp if (packsz > last_reported_packsz + 1024) {
820 dcb64fea 2022-02-23 stsp err = send_fetch_download_progress(ibuf, packsz, &rl);
823 5672d305 2020-03-18 stsp last_reported_packsz = packsz;
826 dcb64fea 2022-02-23 stsp err = send_fetch_download_progress(ibuf, packsz, NULL);
830 ae25a666 2023-02-23 op got_hash_final(&ctx, pack_sha1);
831 dc671e91 2020-03-24 stsp if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
832 dc671e91 2020-03-24 stsp memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
833 dc671e91 2020-03-24 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
834 dc671e91 2020-03-24 stsp "pack file checksum mismatch");
837 d8bacb93 2023-01-10 mark got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
838 9ff10419 2020-03-18 stsp free(have);
839 9ff10419 2020-03-18 stsp free(want);
840 00cd0e0a 2020-03-18 stsp free(id_str);
841 4bff57b4 2023-02-14 stsp free(default_id_str);
842 00cd0e0a 2020-03-18 stsp free(refname);
843 00cd0e0a 2020-03-18 stsp free(server_capabilities);
844 8f2d01a6 2020-03-18 stsp return err;
849 93658fb9 2020-03-18 stsp main(int argc, char **argv)
851 93658fb9 2020-03-18 stsp const struct got_error *err = NULL;
852 9114dd43 2023-01-10 mark int fetchfd = -1, packfd = -1;
853 1d72a2a0 2020-03-24 stsp uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
854 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
855 93658fb9 2020-03-18 stsp struct imsg imsg;
856 33501562 2020-03-18 stsp struct got_pathlist_head have_refs;
857 4ba14133 2020-03-20 stsp struct got_pathlist_head wanted_branches;
858 0e4002ca 2020-03-21 stsp struct got_pathlist_head wanted_refs;
859 4ba14133 2020-03-20 stsp struct got_imsg_fetch_request fetch_req;
860 659e7fbd 2020-03-20 stsp struct got_imsg_fetch_have_ref href;
861 4ba14133 2020-03-20 stsp struct got_imsg_fetch_wanted_branch wbranch;
862 0e4002ca 2020-03-21 stsp struct got_imsg_fetch_wanted_ref wref;
863 030daac8 2021-09-25 stsp size_t datalen, i;
864 118a625d 2023-02-18 mark char *remote_head = NULL, *worktree_branch = NULL;
866 7848a0e1 2020-03-19 stsp static int attached;
867 7848a0e1 2020-03-19 stsp while (!attached)
871 33501562 2020-03-18 stsp TAILQ_INIT(&have_refs);
872 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
873 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
875 93658fb9 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
876 ffb5f621 2020-03-18 stsp #ifndef PROFILE
877 ffb5f621 2020-03-18 stsp /* revoke access to most system calls */
878 ffb5f621 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
879 ffb5f621 2020-03-18 stsp err = got_error_from_errno("pledge");
880 ffb5f621 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
884 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
886 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
887 93658fb9 2020-03-18 stsp err = NULL;
890 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
892 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
893 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
896 33501562 2020-03-18 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
897 4ba14133 2020-03-20 stsp if (datalen < sizeof(fetch_req)) {
898 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
901 4ba14133 2020-03-20 stsp memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
902 2c52c623 2024-01-30 op fetchfd = imsg_get_fd(&imsg);
904 188f8dcf 2023-02-07 stsp if (datalen != sizeof(fetch_req) +
905 118a625d 2023-02-18 mark fetch_req.worktree_branch_len + fetch_req.remote_head_len) {
906 188f8dcf 2023-02-07 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
910 188f8dcf 2023-02-07 stsp if (fetch_req.worktree_branch_len != 0) {
911 188f8dcf 2023-02-07 stsp worktree_branch = strndup(imsg.data +
912 188f8dcf 2023-02-07 stsp sizeof(fetch_req), fetch_req.worktree_branch_len);
913 188f8dcf 2023-02-07 stsp if (worktree_branch == NULL) {
914 118a625d 2023-02-18 mark err = got_error_from_errno("strndup");
919 118a625d 2023-02-18 mark if (fetch_req.remote_head_len != 0) {
920 118a625d 2023-02-18 mark remote_head = strndup(imsg.data + sizeof(fetch_req) +
921 118a625d 2023-02-18 mark fetch_req.worktree_branch_len, fetch_req.remote_head_len);
922 118a625d 2023-02-18 mark if (remote_head == NULL) {
923 188f8dcf 2023-02-07 stsp err = got_error_from_errno("strndup");
928 4ba14133 2020-03-20 stsp imsg_free(&imsg);
930 2690194b 2020-03-21 stsp if (fetch_req.verbosity > 0)
931 2690194b 2020-03-21 stsp chattygot += fetch_req.verbosity;
933 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_have_refs; i++) {
934 7848a0e1 2020-03-19 stsp struct got_object_id *id;
935 7848a0e1 2020-03-19 stsp char *refname;
937 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
939 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
940 4ba14133 2020-03-20 stsp err = NULL;
943 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
945 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
946 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
949 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
950 4ba14133 2020-03-20 stsp if (datalen < sizeof(href)) {
951 659e7fbd 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
954 4ba14133 2020-03-20 stsp memcpy(&href, imsg.data, sizeof(href));
955 4ba14133 2020-03-20 stsp if (datalen - sizeof(href) < href.name_len) {
956 7848a0e1 2020-03-19 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
959 00b3e9ae 2023-01-11 op refname = strndup(imsg.data + sizeof(href), href.name_len);
960 7848a0e1 2020-03-19 stsp if (refname == NULL) {
961 00b3e9ae 2023-01-11 op err = got_error_from_errno("strndup");
965 7848a0e1 2020-03-19 stsp id = malloc(sizeof(*id));
966 7848a0e1 2020-03-19 stsp if (id == NULL) {
967 7848a0e1 2020-03-19 stsp free(refname);
968 7848a0e1 2020-03-19 stsp err = got_error_from_errno("malloc");
971 4b4da3bb 2023-02-01 op memcpy(id, &href.id, sizeof(*id));
972 7848a0e1 2020-03-19 stsp err = got_pathlist_append(&have_refs, refname, id);
974 7848a0e1 2020-03-19 stsp free(refname);
979 4ba14133 2020-03-20 stsp imsg_free(&imsg);
982 4ba14133 2020-03-20 stsp for (i = 0; i < fetch_req.n_wanted_branches; i++) {
983 4ba14133 2020-03-20 stsp char *refname;
985 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
987 4ba14133 2020-03-20 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
988 4ba14133 2020-03-20 stsp err = NULL;
991 4ba14133 2020-03-20 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
993 4ba14133 2020-03-20 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
994 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
997 4ba14133 2020-03-20 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
998 4ba14133 2020-03-20 stsp if (datalen < sizeof(wbranch)) {
999 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1000 4ba14133 2020-03-20 stsp goto done;
1002 4ba14133 2020-03-20 stsp memcpy(&wbranch, imsg.data, sizeof(wbranch));
1003 4ba14133 2020-03-20 stsp if (datalen - sizeof(wbranch) < wbranch.name_len) {
1004 4ba14133 2020-03-20 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1005 4ba14133 2020-03-20 stsp goto done;
1007 00b3e9ae 2023-01-11 op refname = strndup(imsg.data + sizeof(wbranch),
1008 00b3e9ae 2023-01-11 op wbranch.name_len);
1009 4ba14133 2020-03-20 stsp if (refname == NULL) {
1010 00b3e9ae 2023-01-11 op err = got_error_from_errno("strndup");
1011 4ba14133 2020-03-20 stsp goto done;
1014 4ba14133 2020-03-20 stsp err = got_pathlist_append(&wanted_branches, refname, NULL);
1015 4ba14133 2020-03-20 stsp if (err) {
1016 4ba14133 2020-03-20 stsp free(refname);
1017 4ba14133 2020-03-20 stsp goto done;
1020 4ba14133 2020-03-20 stsp imsg_free(&imsg);
1023 0e4002ca 2020-03-21 stsp for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1024 0e4002ca 2020-03-21 stsp char *refname;
1026 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1027 9ca26ac3 2021-08-06 stsp if (err) {
1028 0e4002ca 2020-03-21 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1029 0e4002ca 2020-03-21 stsp err = NULL;
1030 0e4002ca 2020-03-21 stsp goto done;
1032 0e4002ca 2020-03-21 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1033 0e4002ca 2020-03-21 stsp goto done;
1034 0e4002ca 2020-03-21 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1035 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1036 0e4002ca 2020-03-21 stsp goto done;
1038 0e4002ca 2020-03-21 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1039 0e4002ca 2020-03-21 stsp if (datalen < sizeof(wref)) {
1040 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1041 0e4002ca 2020-03-21 stsp goto done;
1043 0e4002ca 2020-03-21 stsp memcpy(&wref, imsg.data, sizeof(wref));
1044 0e4002ca 2020-03-21 stsp if (datalen - sizeof(wref) < wref.name_len) {
1045 0e4002ca 2020-03-21 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1046 0e4002ca 2020-03-21 stsp goto done;
1048 00b3e9ae 2023-01-11 op refname = strndup(imsg.data + sizeof(wref), wref.name_len);
1049 0e4002ca 2020-03-21 stsp if (refname == NULL) {
1050 00b3e9ae 2023-01-11 op err = got_error_from_errno("strndup");
1051 0e4002ca 2020-03-21 stsp goto done;
1054 0e4002ca 2020-03-21 stsp err = got_pathlist_append(&wanted_refs, refname, NULL);
1055 0e4002ca 2020-03-21 stsp if (err) {
1056 0e4002ca 2020-03-21 stsp free(refname);
1057 0e4002ca 2020-03-21 stsp goto done;
1060 0e4002ca 2020-03-21 stsp imsg_free(&imsg);
1063 9ca26ac3 2021-08-06 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1064 9ca26ac3 2021-08-06 stsp if (err) {
1065 93658fb9 2020-03-18 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1066 93658fb9 2020-03-18 stsp err = NULL;
1067 93658fb9 2020-03-18 stsp goto done;
1069 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1070 93658fb9 2020-03-18 stsp goto done;
1071 f826addf 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1072 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1073 93658fb9 2020-03-18 stsp goto done;
1075 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1076 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1077 93658fb9 2020-03-18 stsp goto done;
1079 2c52c623 2024-01-30 op packfd = imsg_get_fd(&imsg);
1081 1d72a2a0 2020-03-24 stsp err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1082 41b0de12 2020-03-21 stsp fetch_req.fetch_all_branches, &wanted_branches,
1083 188f8dcf 2023-02-07 stsp &wanted_refs, fetch_req.list_refs_only,
1084 118a625d 2023-02-18 mark worktree_branch, remote_head, fetch_req.no_head, &ibuf);
1086 188f8dcf 2023-02-07 stsp free(worktree_branch);
1087 118a625d 2023-02-18 mark free(remote_head);
1088 d8bacb93 2023-01-10 mark got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
1089 d8bacb93 2023-01-10 mark got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_PATH);
1090 0bec957e 2020-03-21 stsp if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1091 0bec957e 2020-03-21 stsp err = got_error_from_errno("close");
1092 9ff10419 2020-03-18 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
1093 9ff10419 2020-03-18 stsp err = got_error_from_errno("close");
1094 9ff10419 2020-03-18 stsp if (err != NULL)
1095 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1097 1d72a2a0 2020-03-24 stsp err = send_fetch_done(&ibuf, pack_sha1);
1098 cf875574 2020-03-18 stsp if (err != NULL) {
1099 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1100 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);