2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
39 #include "got_error.h"
40 #include "got_object.h"
42 #include "got_version.h"
43 #include "got_fetch.h"
44 #include "got_reference.h"
46 #include "got_lib_hash.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_object.h"
49 #include "got_lib_object_parse.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_pack.h"
52 #include "got_lib_pkt.h"
53 #include "got_lib_gitproto.h"
54 #include "got_lib_ratelimit.h"
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 struct got_object *indexed;
67 static const struct got_capability got_capabilities[] = {
68 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
69 { GOT_CAPA_OFS_DELTA, NULL },
70 { GOT_CAPA_SIDE_BAND_64K, NULL },
74 match_remote_ref(struct got_pathlist_head *have_refs,
75 struct got_object_id *my_id, const char *refname)
77 struct got_pathlist_entry *pe;
79 /* XXX zero-hash signifies we don't have this ref;
80 * we should use a flag instead */
81 memset(my_id, 0, sizeof(*my_id));
83 TAILQ_FOREACH(pe, have_refs, entry) {
84 struct got_object_id *id = pe->data;
85 if (strcmp(pe->path, refname) == 0) {
86 memcpy(my_id, id, sizeof(*my_id));
93 match_branch(const char *branch, const char *wanted_branch)
95 if (strncmp(branch, "refs/heads/", 11) != 0)
98 if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
101 return (strcmp(branch + 11, wanted_branch) == 0);
105 match_wanted_ref(const char *refname, const char *wanted_ref)
107 if (strncmp(refname, "refs/", 5) != 0)
112 * Prevent fetching of references that won't make any
113 * sense outside of the remote repository's context.
115 if (strncmp(refname, "got/", 4) == 0)
117 if (strncmp(refname, "remotes/", 8) == 0)
120 if (strncmp(wanted_ref, "refs/", 5) == 0)
123 /* Allow prefix match. */
124 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
127 /* Allow exact match. */
128 return (strcmp(refname, wanted_ref) == 0);
131 static const struct got_error *
132 send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
134 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
135 return got_error(GOT_ERR_NO_SPACE);
140 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
142 return got_error_from_errno(
143 "imsg_compose FETCH_SERVER_PROGRESS");
145 return got_privsep_flush_imsg(ibuf);
148 static const struct got_error *
149 send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes,
150 struct got_ratelimit *rl)
152 const struct got_error *err;
156 err = got_ratelimit_check(&elapsed, rl);
161 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
162 &bytes, sizeof(bytes)) == -1)
163 return got_error_from_errno(
164 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
166 return got_privsep_flush_imsg(ibuf);
169 static const struct got_error *
170 send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
172 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
173 pack_sha1, SHA1_DIGEST_LENGTH) == -1)
174 return got_error_from_errno("imsg_compose FETCH");
175 return got_privsep_flush_imsg(ibuf);
178 static const struct got_error *
179 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
187 * Truncate messages which exceed the maximum imsg payload size.
188 * Server may send up to 64k.
190 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
191 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
193 /* Only allow printable ASCII. */
194 for (i = 0; i < len; i++) {
195 if (isprint((unsigned char)buf[i]) ||
196 isspace((unsigned char)buf[i]))
198 return got_error_msg(GOT_ERR_BAD_PACKET,
199 "non-printable progress message received from server");
202 return send_fetch_server_progress(ibuf, buf, len);
205 static const struct got_error *
206 fetch_error(const char *buf, size_t len)
208 static char msg[1024];
211 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
212 if (!isprint((unsigned char)buf[i]))
213 return got_error_msg(GOT_ERR_BAD_PACKET,
214 "non-printable error message received from server");
218 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
221 static const struct got_error *
222 send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
225 size_t len, nsymrefs = 0;
226 struct got_pathlist_entry *pe;
228 len = sizeof(struct got_imsg_fetch_symrefs);
229 TAILQ_FOREACH(pe, symrefs, entry) {
230 const char *target = pe->data;
231 len += sizeof(struct got_imsg_fetch_symref) +
232 pe->path_len + strlen(target);
236 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
237 return got_error(GOT_ERR_NO_SPACE);
239 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
241 return got_error_from_errno("imsg_create FETCH_SYMREFS");
243 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
244 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1)
245 return got_error_from_errno("imsg_add FETCH_SYMREFS");
247 TAILQ_FOREACH(pe, symrefs, entry) {
248 const char *name = pe->path;
249 size_t name_len = pe->path_len;
250 const char *target = pe->data;
251 size_t target_len = strlen(target);
253 /* Keep in sync with struct got_imsg_fetch_symref definition! */
254 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
255 return got_error_from_errno("imsg_add FETCH_SYMREFS");
256 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1)
257 return got_error_from_errno("imsg_add FETCH_SYMREFS");
258 if (imsg_add(wbuf, name, name_len) == -1)
259 return got_error_from_errno("imsg_add FETCH_SYMREFS");
260 if (imsg_add(wbuf, target, target_len) == -1)
261 return got_error_from_errno("imsg_add FETCH_SYMREFS");
265 imsg_close(ibuf, wbuf);
266 return got_privsep_flush_imsg(ibuf);
269 static const struct got_error *
270 send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
274 size_t len, reflen = strlen(refname);
276 len = sizeof(struct got_imsg_fetch_ref) + reflen;
277 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
278 return got_error(GOT_ERR_NO_SPACE);
280 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
282 return got_error_from_errno("imsg_create FETCH_REF");
284 /* Keep in sync with struct got_imsg_fetch_ref definition! */
285 if (imsg_add(wbuf, refid, sizeof(*refid)) == -1)
286 return got_error_from_errno("imsg_add FETCH_REF");
287 if (imsg_add(wbuf, refname, reflen) == -1)
288 return got_error_from_errno("imsg_add FETCH_REF");
291 imsg_close(ibuf, wbuf);
292 return got_privsep_flush_imsg(ibuf);
295 static const struct got_error *
296 fetch_ref(struct imsgbuf *ibuf, struct got_pathlist_head *have_refs,
297 struct got_object_id *have, struct got_object_id *want,
298 const char *refname, const char *id_str)
300 const struct got_error *err;
301 char *theirs = NULL, *mine = NULL;
303 memset(want, 0, sizeof(*want));
304 if (!got_parse_sha1_digest(want->hash, id_str)) {
305 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
309 match_remote_ref(have_refs, have, refname);
310 err = send_fetch_ref(ibuf, want, refname);
315 fprintf(stderr, "%s: %s will be fetched\n",
316 getprogname(), refname);
318 err = got_object_id_str(&theirs, want);
321 err = got_object_id_str(&mine, have);
324 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
325 getprogname(), theirs, getprogname(), mine);
333 static const struct got_error *
334 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
335 struct got_pathlist_head *have_refs, int fetch_all_branches,
336 struct got_pathlist_head *wanted_branches,
337 struct got_pathlist_head *wanted_refs, int list_refs_only,
338 const char *worktree_branch, struct imsgbuf *ibuf)
340 const struct got_error *err = NULL;
341 char buf[GOT_PKT_MAX];
342 char hashstr[SHA1_DIGEST_STRING_LENGTH];
343 struct got_object_id *have, *want;
344 int is_firstpkt = 1, nref = 0, refsz = 16;
345 int i, n, nwant = 0, nhave = 0, acked = 0;
346 off_t packsz = 0, last_reported_packsz = 0;
347 char *id_str = NULL, *default_id_str = NULL, *refname = NULL;
348 char *server_capabilities = NULL, *my_capabilities = NULL;
349 const char *default_branch = NULL;
350 struct got_pathlist_head symrefs;
351 struct got_pathlist_entry *pe;
352 int sent_my_capabilites = 0, have_sidebands = 0;
353 int found_branch = 0;
355 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
356 size_t sha1_buf_len = 0;
358 struct got_ratelimit rl;
360 TAILQ_INIT(&symrefs);
362 got_ratelimit_init(&rl, 0, 500);
364 have = malloc(refsz * sizeof(have[0]));
366 return got_error_from_errno("malloc");
367 want = malloc(refsz * sizeof(want[0]));
369 err = got_error_from_errno("malloc");
373 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
378 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
379 err = fetch_error(&buf[4], n - 4);
384 err = got_gitproto_parse_refline(&id_str, &refname,
385 &server_capabilities, buf, n);
389 if (refsz == nref + 1) {
390 struct got_object_id *h, *w;
393 h = reallocarray(have, refsz, sizeof(have[0]));
395 err = got_error_from_errno("reallocarray");
399 w = reallocarray(want, refsz, sizeof(want[0]));
401 err = got_error_from_errno("reallocarray");
408 if (chattygot && server_capabilities[0] != '\0')
409 fprintf(stderr, "%s: server capabilities: %s\n",
410 getprogname(), server_capabilities);
411 err = got_gitproto_match_capabilities(&my_capabilities,
412 &symrefs, server_capabilities,
413 got_capabilities, nitems(got_capabilities));
417 fprintf(stderr, "%s: my capabilities:%s\n",
418 getprogname(), my_capabilities != NULL ?
419 my_capabilities : "");
420 err = send_fetch_symrefs(ibuf, &symrefs);
424 if (!fetch_all_branches) {
425 TAILQ_FOREACH(pe, &symrefs, entry) {
426 const char *name = pe->path;
427 const char *symref_target = pe->data;
428 if (strcmp(name, GOT_REF_HEAD) != 0)
430 default_branch = symref_target;
437 if (strstr(refname, "^{}")) {
439 fprintf(stderr, "%s: ignoring %s\n",
440 getprogname(), refname);
444 if (default_branch && default_id_str == NULL &&
445 strcmp(refname, default_branch) == 0) {
446 default_id_str = strdup(id_str);
447 if (default_id_str == NULL) {
448 err = got_error_from_errno("strdup");
453 if (list_refs_only || strncmp(refname, "refs/tags/", 10) == 0) {
454 err = fetch_ref(ibuf, have_refs, &have[nref],
455 &want[nref], refname, id_str);
459 } else if (strncmp(refname, "refs/heads/", 11) == 0) {
460 if (fetch_all_branches) {
461 err = fetch_ref(ibuf, have_refs, &have[nref],
462 &want[nref], refname, id_str);
469 TAILQ_FOREACH(pe, wanted_branches, entry) {
470 if (match_branch(refname, pe->path))
473 if (pe != NULL || (worktree_branch != NULL &&
474 match_branch(refname, worktree_branch))) {
475 err = fetch_ref(ibuf, have_refs, &have[nref],
476 &want[nref], refname, id_str);
481 } else if (chattygot) {
482 fprintf(stderr, "%s: ignoring %s\n",
483 getprogname(), refname);
486 TAILQ_FOREACH(pe, wanted_refs, entry) {
487 if (match_wanted_ref(refname, pe->path))
491 err = fetch_ref(ibuf, have_refs, &have[nref],
492 &want[nref], refname, id_str);
496 } else if (chattygot) {
497 fprintf(stderr, "%s: ignoring %s\n",
498 getprogname(), refname);
506 if (!found_branch && default_branch && default_id_str &&
507 strncmp(default_branch, "refs/heads/", 11) == 0) {
508 err = fetch_ref(ibuf, have_refs, &have[nref],
509 &want[nref], default_branch, default_id_str);
516 /* Abort if we haven't found anything to fetch. */
518 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
522 for (i = 0; i < nref; i++) {
523 if (got_object_id_cmp(&have[i], &want[i]) == 0)
525 got_sha1_digest_to_str(want[i].hash, hashstr, sizeof(hashstr));
526 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
527 sent_my_capabilites || my_capabilities == NULL ?
528 "" : my_capabilities);
529 if (n < 0 || (size_t)n >= sizeof(buf)) {
530 err = got_error(GOT_ERR_NO_SPACE);
533 err = got_pkt_writepkt(fd, buf, n, chattygot);
536 sent_my_capabilites = 1;
539 err = got_pkt_flushpkt(fd, chattygot);
546 TAILQ_FOREACH(pe, have_refs, entry) {
547 struct got_object_id *id = pe->data;
548 got_sha1_digest_to_str(id->hash, hashstr, sizeof(hashstr));
549 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
550 if (n < 0 || (size_t)n >= sizeof(buf)) {
551 err = got_error(GOT_ERR_NO_SPACE);
554 err = got_pkt_writepkt(fd, buf, n, chattygot);
560 while (nhave > 0 && !acked) {
561 struct got_object_id common_id;
563 /* The server should ACK the object IDs we need. */
564 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
567 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
568 err = fetch_error(&buf[4], n - 4);
571 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
572 /* Server has not located our objects yet. */
575 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
576 strncmp(buf, "ACK ", 4) != 0) {
577 err = got_error_msg(GOT_ERR_BAD_PACKET,
578 "unexpected message from server");
582 memset(&common_id, 0, sizeof(common_id));
583 if (!got_parse_sha1_digest(common_id.hash, buf + 4)) {
584 err = got_error_msg(GOT_ERR_BAD_PACKET,
585 "bad object ID in ACK packet from server");
591 n = strlcpy(buf, "done\n", sizeof(buf));
592 err = got_pkt_writepkt(fd, buf, n, chattygot);
597 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
600 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
601 err = got_error_msg(GOT_ERR_BAD_PACKET,
602 "unexpected message from server");
608 fprintf(stderr, "%s: fetching...\n", getprogname());
610 if (my_capabilities != NULL &&
611 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
618 if (have_sidebands) {
619 err = got_pkt_readhdr(&datalen, fd, chattygot);
625 /* Read sideband channel ID (one byte). */
626 r = read(fd, buf, 1);
628 err = got_error_from_errno("read");
632 err = got_error_msg(GOT_ERR_BAD_PACKET,
636 if (datalen > sizeof(buf) - 5) {
637 err = got_error_msg(GOT_ERR_BAD_PACKET,
638 "bad packet length");
641 datalen--; /* sideband ID has been read */
642 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
643 /* Read packfile data. */
644 err = got_pkt_readn(&r, fd, buf, datalen);
648 err = got_error_msg(GOT_ERR_BAD_PACKET,
652 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
653 err = got_pkt_readn(&r, fd, buf, datalen);
657 err = got_error_msg(GOT_ERR_BAD_PACKET,
661 err = fetch_progress(ibuf, buf, r);
665 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
666 err = got_pkt_readn(&r, fd, buf, datalen);
670 err = got_error_msg(GOT_ERR_BAD_PACKET,
674 err = fetch_error(buf, r);
676 } else if (buf[0] == 'A') {
677 err = got_pkt_readn(&r, fd, buf, datalen);
681 err = got_error_msg(GOT_ERR_BAD_PACKET,
686 * Git server responds with ACK after 'done'
687 * even though multi_ack is disabled?!?
690 if (strncmp(buf, "CK ", 3) == 0)
691 continue; /* ignore */
692 err = got_error_msg(GOT_ERR_BAD_PACKET,
693 "unexpected message from server");
696 err = got_error_msg(GOT_ERR_BAD_PACKET,
697 "unknown side-band received from server");
701 /* No sideband channel. Every byte is packfile data. */
702 err = got_pkt_readn(&r, fd, buf, sizeof buf);
710 * An expected SHA1 checksum sits at the end of the pack file.
711 * Since we don't know the file size ahead of time we have to
712 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
713 * those bytes into our SHA1 checksum computation until we
714 * know for sure that additional pack file data bytes follow.
716 * We can assume r > 0 since otherwise the loop would exit.
718 if (r < SHA1_DIGEST_LENGTH) {
719 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
721 * If there's enough buffered + read data to
722 * fill up the buffer then shift a sufficient
723 * amount of bytes out at the front to make
724 * room, mixing those bytes into the checksum.
726 if (sha1_buf_len > 0 &&
727 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
728 size_t nshift = MIN(sha1_buf_len + r -
729 SHA1_DIGEST_LENGTH, sha1_buf_len);
730 SHA1Update(&sha1_ctx, sha1_buf, nshift);
731 memmove(sha1_buf, sha1_buf + nshift,
732 sha1_buf_len - nshift);
733 sha1_buf_len -= nshift;
736 /* Buffer potential checksum bytes. */
737 memcpy(sha1_buf + sha1_buf_len, buf, r);
741 * Mix in previously buffered bytes which
742 * are not part of the checksum after all.
744 SHA1Update(&sha1_ctx, sha1_buf, r);
746 /* Update potential checksum buffer. */
747 memmove(sha1_buf, sha1_buf + r,
749 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
752 /* Mix in any previously buffered bytes. */
753 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
755 /* Mix in bytes read minus potential checksum bytes. */
756 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
758 /* Buffer potential checksum bytes. */
759 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
761 sha1_buf_len = SHA1_DIGEST_LENGTH;
764 /* Write packfile data to temporary pack file. */
765 w = write(packfd, buf, r);
767 err = got_error_from_errno("write");
771 err = got_error(GOT_ERR_IO);
776 /* Don't send too many progress privsep messages. */
777 if (packsz > last_reported_packsz + 1024) {
778 err = send_fetch_download_progress(ibuf, packsz, &rl);
781 last_reported_packsz = packsz;
784 err = send_fetch_download_progress(ibuf, packsz, NULL);
788 SHA1Final(pack_sha1, &sha1_ctx);
789 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
790 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
791 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
792 "pack file checksum mismatch");
795 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
799 free(default_id_str);
801 free(server_capabilities);
807 main(int argc, char **argv)
809 const struct got_error *err = NULL;
810 int fetchfd = -1, packfd = -1;
811 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
814 struct got_pathlist_head have_refs;
815 struct got_pathlist_head wanted_branches;
816 struct got_pathlist_head wanted_refs;
817 struct got_imsg_fetch_request fetch_req;
818 struct got_imsg_fetch_have_ref href;
819 struct got_imsg_fetch_wanted_branch wbranch;
820 struct got_imsg_fetch_wanted_ref wref;
822 char *worktree_branch = NULL;
829 TAILQ_INIT(&have_refs);
830 TAILQ_INIT(&wanted_branches);
831 TAILQ_INIT(&wanted_refs);
833 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
835 /* revoke access to most system calls */
836 if (pledge("stdio recvfd", NULL) == -1) {
837 err = got_error_from_errno("pledge");
838 got_privsep_send_error(&ibuf, err);
842 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
844 if (err->code == GOT_ERR_PRIVSEP_PIPE)
848 if (imsg.hdr.type == GOT_IMSG_STOP)
850 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
851 err = got_error(GOT_ERR_PRIVSEP_MSG);
854 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
855 if (datalen < sizeof(fetch_req)) {
856 err = got_error(GOT_ERR_PRIVSEP_LEN);
859 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
862 if (datalen != sizeof(fetch_req) +
863 fetch_req.worktree_branch_len) {
864 err = got_error(GOT_ERR_PRIVSEP_LEN);
868 if (fetch_req.worktree_branch_len != 0) {
869 worktree_branch = strndup(imsg.data +
870 sizeof(fetch_req), fetch_req.worktree_branch_len);
871 if (worktree_branch == NULL) {
872 err = got_error_from_errno("strndup");
879 if (fetch_req.verbosity > 0)
880 chattygot += fetch_req.verbosity;
882 for (i = 0; i < fetch_req.n_have_refs; i++) {
883 struct got_object_id *id;
886 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
888 if (err->code == GOT_ERR_PRIVSEP_PIPE)
892 if (imsg.hdr.type == GOT_IMSG_STOP)
894 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
895 err = got_error(GOT_ERR_PRIVSEP_MSG);
898 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
899 if (datalen < sizeof(href)) {
900 err = got_error(GOT_ERR_PRIVSEP_LEN);
903 memcpy(&href, imsg.data, sizeof(href));
904 if (datalen - sizeof(href) < href.name_len) {
905 err = got_error(GOT_ERR_PRIVSEP_LEN);
908 refname = strndup(imsg.data + sizeof(href), href.name_len);
909 if (refname == NULL) {
910 err = got_error_from_errno("strndup");
914 id = malloc(sizeof(*id));
917 err = got_error_from_errno("malloc");
920 memcpy(id, &href.id, sizeof(*id));
921 err = got_pathlist_append(&have_refs, refname, id);
931 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
934 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
936 if (err->code == GOT_ERR_PRIVSEP_PIPE)
940 if (imsg.hdr.type == GOT_IMSG_STOP)
942 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
943 err = got_error(GOT_ERR_PRIVSEP_MSG);
946 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
947 if (datalen < sizeof(wbranch)) {
948 err = got_error(GOT_ERR_PRIVSEP_LEN);
951 memcpy(&wbranch, imsg.data, sizeof(wbranch));
952 if (datalen - sizeof(wbranch) < wbranch.name_len) {
953 err = got_error(GOT_ERR_PRIVSEP_LEN);
956 refname = strndup(imsg.data + sizeof(wbranch),
958 if (refname == NULL) {
959 err = got_error_from_errno("strndup");
963 err = got_pathlist_append(&wanted_branches, refname, NULL);
972 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
975 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
977 if (err->code == GOT_ERR_PRIVSEP_PIPE)
981 if (imsg.hdr.type == GOT_IMSG_STOP)
983 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
984 err = got_error(GOT_ERR_PRIVSEP_MSG);
987 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
988 if (datalen < sizeof(wref)) {
989 err = got_error(GOT_ERR_PRIVSEP_LEN);
992 memcpy(&wref, imsg.data, sizeof(wref));
993 if (datalen - sizeof(wref) < wref.name_len) {
994 err = got_error(GOT_ERR_PRIVSEP_LEN);
997 refname = strndup(imsg.data + sizeof(wref), wref.name_len);
998 if (refname == NULL) {
999 err = got_error_from_errno("strndup");
1003 err = got_pathlist_append(&wanted_refs, refname, NULL);
1012 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1014 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1018 if (imsg.hdr.type == GOT_IMSG_STOP)
1020 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1021 err = got_error(GOT_ERR_PRIVSEP_MSG);
1024 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1025 err = got_error(GOT_ERR_PRIVSEP_LEN);
1030 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1031 fetch_req.fetch_all_branches, &wanted_branches,
1032 &wanted_refs, fetch_req.list_refs_only,
1033 worktree_branch, &ibuf);
1035 free(worktree_branch);
1036 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
1037 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_PATH);
1038 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1039 err = got_error_from_errno("close");
1040 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1041 err = got_error_from_errno("close");
1043 got_privsep_send_error(&ibuf, err);
1045 err = send_fetch_done(&ibuf, pack_sha1);
1047 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1048 got_privsep_send_error(&ibuf, err);