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");
264 imsg_close(ibuf, wbuf);
265 return got_privsep_flush_imsg(ibuf);
268 static const struct got_error *
269 send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
273 size_t len, reflen = strlen(refname);
275 len = sizeof(struct got_imsg_fetch_ref) + reflen;
276 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
277 return got_error(GOT_ERR_NO_SPACE);
279 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
281 return got_error_from_errno("imsg_create FETCH_REF");
283 /* Keep in sync with struct got_imsg_fetch_ref definition! */
284 if (imsg_add(wbuf, refid, sizeof(*refid)) == -1)
285 return got_error_from_errno("imsg_add FETCH_REF");
286 if (imsg_add(wbuf, refname, reflen) == -1)
287 return got_error_from_errno("imsg_add FETCH_REF");
289 imsg_close(ibuf, wbuf);
290 return got_privsep_flush_imsg(ibuf);
293 static const struct got_error *
294 fetch_ref(struct imsgbuf *ibuf, struct got_pathlist_head *have_refs,
295 struct got_object_id *have, struct got_object_id *want,
296 const char *refname, const char *id_str)
298 const struct got_error *err;
299 char *theirs = NULL, *mine = NULL;
301 if (!got_parse_object_id(want, id_str, GOT_HASH_SHA1)) {
302 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
306 match_remote_ref(have_refs, have, refname);
307 err = send_fetch_ref(ibuf, want, refname);
312 fprintf(stderr, "%s: %s will be fetched\n",
313 getprogname(), refname);
315 err = got_object_id_str(&theirs, want);
318 err = got_object_id_str(&mine, have);
321 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
322 getprogname(), theirs, getprogname(), mine);
330 static const struct got_error *
331 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
332 struct got_pathlist_head *have_refs, int fetch_all_branches,
333 struct got_pathlist_head *wanted_branches,
334 struct got_pathlist_head *wanted_refs, int list_refs_only,
335 const char *worktree_branch, const char *remote_head,
336 int no_head, struct imsgbuf *ibuf)
338 const struct got_error *err = NULL;
339 char buf[GOT_PKT_MAX];
340 char hashstr[SHA1_DIGEST_STRING_LENGTH];
341 struct got_object_id *have, *want;
342 int is_firstpkt = 1, nref = 0, refsz = 16;
343 int i, n, nwant = 0, nhave = 0, acked = 0;
344 off_t packsz = 0, last_reported_packsz = 0;
345 char *id_str = NULL, *default_id_str = NULL, *refname = NULL;
346 char *server_capabilities = NULL, *my_capabilities = NULL;
347 const char *default_branch = NULL;
348 struct got_pathlist_head symrefs;
349 struct got_pathlist_entry *pe;
350 int sent_my_capabilites = 0, have_sidebands = 0;
351 int found_branch = 0;
353 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
354 size_t sha1_buf_len = 0;
356 struct got_ratelimit rl;
358 TAILQ_INIT(&symrefs);
359 got_hash_init(&ctx, GOT_HASH_SHA1);
360 got_ratelimit_init(&rl, 0, 500);
362 have = malloc(refsz * sizeof(have[0]));
364 return got_error_from_errno("malloc");
365 want = malloc(refsz * sizeof(want[0]));
367 err = got_error_from_errno("malloc");
371 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
376 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
377 err = fetch_error(&buf[4], n - 4);
382 err = got_gitproto_parse_refline(&id_str, &refname,
383 &server_capabilities, buf, n);
387 if (refsz == nref + 1) {
388 struct got_object_id *h, *w;
391 h = reallocarray(have, refsz, sizeof(have[0]));
393 err = got_error_from_errno("reallocarray");
397 w = reallocarray(want, refsz, sizeof(want[0]));
399 err = got_error_from_errno("reallocarray");
406 if (chattygot && server_capabilities[0] != '\0')
407 fprintf(stderr, "%s: server capabilities: %s\n",
408 getprogname(), server_capabilities);
409 err = got_gitproto_match_capabilities(&my_capabilities,
410 &symrefs, server_capabilities,
411 got_capabilities, nitems(got_capabilities));
415 fprintf(stderr, "%s: my capabilities:%s\n",
416 getprogname(), my_capabilities != NULL ?
417 my_capabilities : "");
418 err = send_fetch_symrefs(ibuf, &symrefs);
422 if (!fetch_all_branches) {
423 TAILQ_FOREACH(pe, &symrefs, entry) {
424 const char *name = pe->path;
425 const char *symref_target = pe->data;
426 if (strcmp(name, GOT_REF_HEAD) != 0)
428 default_branch = symref_target;
435 if (strstr(refname, "^{}")) {
437 fprintf(stderr, "%s: ignoring %s\n",
438 getprogname(), refname);
442 if (default_branch && default_id_str == NULL &&
443 strcmp(refname, default_branch) == 0) {
444 default_id_str = strdup(id_str);
445 if (default_id_str == NULL) {
446 err = got_error_from_errno("strdup");
451 if (list_refs_only || strncmp(refname, "refs/tags/", 10) == 0) {
452 err = fetch_ref(ibuf, have_refs, &have[nref],
453 &want[nref], refname, id_str);
457 } else if (strncmp(refname, "refs/heads/", 11) == 0) {
458 if (fetch_all_branches) {
459 err = fetch_ref(ibuf, have_refs, &have[nref],
460 &want[nref], refname, id_str);
467 TAILQ_FOREACH(pe, wanted_branches, entry) {
468 if (match_branch(refname, pe->path))
471 if (pe != NULL || (worktree_branch != NULL &&
472 match_branch(refname, worktree_branch))) {
473 err = fetch_ref(ibuf, have_refs, &have[nref],
474 &want[nref], refname, id_str);
479 } else if (chattygot) {
480 fprintf(stderr, "%s: ignoring %s\n",
481 getprogname(), refname);
484 TAILQ_FOREACH(pe, wanted_refs, entry) {
485 if (match_wanted_ref(refname, pe->path))
489 err = fetch_ref(ibuf, have_refs, &have[nref],
490 &want[nref], refname, id_str);
494 } else if (chattygot) {
495 fprintf(stderr, "%s: ignoring %s\n",
496 getprogname(), refname);
505 * If -b was not used and either none of the requested branches
506 * (got.conf, worktree) were found or the client already has the
507 * remote HEAD symref but its target changed, fetch remote's HEAD.
509 if (!no_head && default_branch && default_id_str &&
510 strncmp(default_branch, "refs/heads/", 11) == 0) {
511 int remote_head_changed = 0;
514 if (strcmp(remote_head, default_branch + 11) != 0)
515 remote_head_changed = 1;
518 if (!found_branch || remote_head_changed) {
519 err = fetch_ref(ibuf, have_refs, &have[nref],
520 &want[nref], default_branch, default_id_str);
527 /* Abort if we haven't found anything to fetch. */
529 struct got_pathlist_entry *pe;
530 static char msg[PATH_MAX + 33];
532 pe = TAILQ_FIRST(wanted_branches);
534 snprintf(msg, sizeof(msg),
535 "branch \"%s\" not found on server", pe->path);
536 err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
540 pe = TAILQ_FIRST(wanted_refs);
542 snprintf(msg, sizeof(msg),
543 "reference \"%s\" not found on server", pe->path);
544 err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
548 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
552 for (i = 0; i < nref; i++) {
553 if (got_object_id_cmp(&have[i], &want[i]) == 0)
555 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
556 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
557 sent_my_capabilites || my_capabilities == NULL ?
558 "" : my_capabilities);
559 if (n < 0 || (size_t)n >= sizeof(buf)) {
560 err = got_error(GOT_ERR_NO_SPACE);
563 err = got_pkt_writepkt(fd, buf, n, chattygot);
566 sent_my_capabilites = 1;
569 err = got_pkt_flushpkt(fd, chattygot);
576 TAILQ_FOREACH(pe, have_refs, entry) {
577 struct got_object_id *id = pe->data;
578 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
579 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
580 if (n < 0 || (size_t)n >= sizeof(buf)) {
581 err = got_error(GOT_ERR_NO_SPACE);
584 err = got_pkt_writepkt(fd, buf, n, chattygot);
590 n = strlcpy(buf, "done\n", sizeof(buf));
591 err = got_pkt_writepkt(fd, buf, n, chattygot);
595 while (nhave > 0 && !acked) {
596 struct got_object_id common_id;
598 /* The server should ACK the object IDs we need. */
599 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
602 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
603 err = fetch_error(&buf[4], n - 4);
606 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
608 * Server could not find a common ancestor.
609 * Perhaps it is an out-of-date mirror, or there
610 * is a repository with unrelated history.
614 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
615 strncmp(buf, "ACK ", 4) != 0) {
616 err = got_error_msg(GOT_ERR_BAD_PACKET,
617 "unexpected message from server");
620 if (!got_parse_object_id(&common_id, buf + 4,
622 err = got_error_msg(GOT_ERR_BAD_PACKET,
623 "bad object ID in ACK packet from server");
630 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
633 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
634 err = got_error_msg(GOT_ERR_BAD_PACKET,
635 "unexpected message from server");
641 fprintf(stderr, "%s: fetching...\n", getprogname());
643 if (my_capabilities != NULL &&
644 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
651 if (have_sidebands) {
652 err = got_pkt_readhdr(&datalen, fd, chattygot);
658 /* Read sideband channel ID (one byte). */
659 r = read(fd, buf, 1);
661 err = got_error_from_errno("read");
665 err = got_error_msg(GOT_ERR_BAD_PACKET,
669 if (datalen > sizeof(buf) - 5) {
670 err = got_error_msg(GOT_ERR_BAD_PACKET,
671 "bad packet length");
674 datalen--; /* sideband ID has been read */
675 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
676 /* Read packfile data. */
677 err = got_pkt_readn(&r, fd, buf, datalen);
681 err = got_error_msg(GOT_ERR_BAD_PACKET,
685 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
686 err = got_pkt_readn(&r, fd, buf, datalen);
690 err = got_error_msg(GOT_ERR_BAD_PACKET,
694 err = fetch_progress(ibuf, buf, r);
698 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
699 err = got_pkt_readn(&r, fd, buf, datalen);
703 err = got_error_msg(GOT_ERR_BAD_PACKET,
707 err = fetch_error(buf, r);
709 } else if (buf[0] == 'A') {
710 err = got_pkt_readn(&r, fd, buf, datalen);
714 err = got_error_msg(GOT_ERR_BAD_PACKET,
719 * Git server responds with ACK after 'done'
720 * even though multi_ack is disabled?!?
723 if (strncmp(buf, "CK ", 3) == 0)
724 continue; /* ignore */
725 err = got_error_msg(GOT_ERR_BAD_PACKET,
726 "unexpected message from server");
729 err = got_error_msg(GOT_ERR_BAD_PACKET,
730 "unknown side-band received from server");
734 /* No sideband channel. Every byte is packfile data. */
735 err = got_pkt_readn(&r, fd, buf, sizeof buf);
743 * An expected SHA1 checksum sits at the end of the pack file.
744 * Since we don't know the file size ahead of time we have to
745 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
746 * those bytes into our SHA1 checksum computation until we
747 * know for sure that additional pack file data bytes follow.
749 * We can assume r > 0 since otherwise the loop would exit.
751 if (r < SHA1_DIGEST_LENGTH) {
752 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
754 * If there's enough buffered + read data to
755 * fill up the buffer then shift a sufficient
756 * amount of bytes out at the front to make
757 * room, mixing those bytes into the checksum.
759 if (sha1_buf_len > 0 &&
760 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
761 size_t nshift = MIN(sha1_buf_len + r -
762 SHA1_DIGEST_LENGTH, sha1_buf_len);
763 got_hash_update(&ctx, sha1_buf,
765 memmove(sha1_buf, sha1_buf + nshift,
766 sha1_buf_len - nshift);
767 sha1_buf_len -= nshift;
770 /* Buffer potential checksum bytes. */
771 memcpy(sha1_buf + sha1_buf_len, buf, r);
775 * Mix in previously buffered bytes which
776 * are not part of the checksum after all.
778 got_hash_update(&ctx, sha1_buf, r);
780 /* Update potential checksum buffer. */
781 memmove(sha1_buf, sha1_buf + r,
783 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
786 /* Mix in any previously buffered bytes. */
787 got_hash_update(&ctx, sha1_buf, sha1_buf_len);
789 /* Mix in bytes read minus potential checksum bytes. */
790 got_hash_update(&ctx, buf, r - SHA1_DIGEST_LENGTH);
792 /* Buffer potential checksum bytes. */
793 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
795 sha1_buf_len = SHA1_DIGEST_LENGTH;
798 /* Write packfile data to temporary pack file. */
799 w = write(packfd, buf, r);
801 err = got_error_from_errno("write");
805 err = got_error(GOT_ERR_IO);
810 /* Don't send too many progress privsep messages. */
811 if (packsz > last_reported_packsz + 1024) {
812 err = send_fetch_download_progress(ibuf, packsz, &rl);
815 last_reported_packsz = packsz;
818 err = send_fetch_download_progress(ibuf, packsz, NULL);
822 got_hash_final(&ctx, pack_sha1);
823 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
824 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
825 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
826 "pack file checksum mismatch");
829 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
833 free(default_id_str);
835 free(server_capabilities);
841 main(int argc, char **argv)
843 const struct got_error *err = NULL;
844 int fetchfd = -1, packfd = -1;
845 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
848 struct got_pathlist_head have_refs;
849 struct got_pathlist_head wanted_branches;
850 struct got_pathlist_head wanted_refs;
851 struct got_imsg_fetch_request fetch_req;
852 struct got_imsg_fetch_have_ref href;
853 struct got_imsg_fetch_wanted_branch wbranch;
854 struct got_imsg_fetch_wanted_ref wref;
856 char *remote_head = NULL, *worktree_branch = NULL;
863 TAILQ_INIT(&have_refs);
864 TAILQ_INIT(&wanted_branches);
865 TAILQ_INIT(&wanted_refs);
867 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
869 /* revoke access to most system calls */
870 if (pledge("stdio recvfd", NULL) == -1) {
871 err = got_error_from_errno("pledge");
872 got_privsep_send_error(&ibuf, err);
876 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
878 if (err->code == GOT_ERR_PRIVSEP_PIPE)
882 if (imsg.hdr.type == GOT_IMSG_STOP)
884 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
885 err = got_error(GOT_ERR_PRIVSEP_MSG);
888 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
889 if (datalen < sizeof(fetch_req)) {
890 err = got_error(GOT_ERR_PRIVSEP_LEN);
893 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
894 fetchfd = imsg_get_fd(&imsg);
896 if (datalen != sizeof(fetch_req) +
897 fetch_req.worktree_branch_len + fetch_req.remote_head_len) {
898 err = got_error(GOT_ERR_PRIVSEP_LEN);
902 if (fetch_req.worktree_branch_len != 0) {
903 worktree_branch = strndup(imsg.data +
904 sizeof(fetch_req), fetch_req.worktree_branch_len);
905 if (worktree_branch == NULL) {
906 err = got_error_from_errno("strndup");
911 if (fetch_req.remote_head_len != 0) {
912 remote_head = strndup(imsg.data + sizeof(fetch_req) +
913 fetch_req.worktree_branch_len, fetch_req.remote_head_len);
914 if (remote_head == NULL) {
915 err = got_error_from_errno("strndup");
922 if (fetch_req.verbosity > 0)
923 chattygot += fetch_req.verbosity;
925 for (i = 0; i < fetch_req.n_have_refs; i++) {
926 struct got_object_id *id;
929 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
931 if (err->code == GOT_ERR_PRIVSEP_PIPE)
935 if (imsg.hdr.type == GOT_IMSG_STOP)
937 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
938 err = got_error(GOT_ERR_PRIVSEP_MSG);
941 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
942 if (datalen < sizeof(href)) {
943 err = got_error(GOT_ERR_PRIVSEP_LEN);
946 memcpy(&href, imsg.data, sizeof(href));
947 if (datalen - sizeof(href) < href.name_len) {
948 err = got_error(GOT_ERR_PRIVSEP_LEN);
951 refname = strndup(imsg.data + sizeof(href), href.name_len);
952 if (refname == NULL) {
953 err = got_error_from_errno("strndup");
957 id = malloc(sizeof(*id));
960 err = got_error_from_errno("malloc");
963 memcpy(id, &href.id, sizeof(*id));
964 err = got_pathlist_append(&have_refs, refname, id);
974 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
977 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
979 if (err->code == GOT_ERR_PRIVSEP_PIPE)
983 if (imsg.hdr.type == GOT_IMSG_STOP)
985 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
986 err = got_error(GOT_ERR_PRIVSEP_MSG);
989 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
990 if (datalen < sizeof(wbranch)) {
991 err = got_error(GOT_ERR_PRIVSEP_LEN);
994 memcpy(&wbranch, imsg.data, sizeof(wbranch));
995 if (datalen - sizeof(wbranch) < wbranch.name_len) {
996 err = got_error(GOT_ERR_PRIVSEP_LEN);
999 refname = strndup(imsg.data + sizeof(wbranch),
1001 if (refname == NULL) {
1002 err = got_error_from_errno("strndup");
1006 err = got_pathlist_append(&wanted_branches, refname, NULL);
1015 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1018 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1020 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1024 if (imsg.hdr.type == GOT_IMSG_STOP)
1026 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1027 err = got_error(GOT_ERR_PRIVSEP_MSG);
1030 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1031 if (datalen < sizeof(wref)) {
1032 err = got_error(GOT_ERR_PRIVSEP_LEN);
1035 memcpy(&wref, imsg.data, sizeof(wref));
1036 if (datalen - sizeof(wref) < wref.name_len) {
1037 err = got_error(GOT_ERR_PRIVSEP_LEN);
1040 refname = strndup(imsg.data + sizeof(wref), wref.name_len);
1041 if (refname == NULL) {
1042 err = got_error_from_errno("strndup");
1046 err = got_pathlist_append(&wanted_refs, refname, NULL);
1055 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1057 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1061 if (imsg.hdr.type == GOT_IMSG_STOP)
1063 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1064 err = got_error(GOT_ERR_PRIVSEP_MSG);
1067 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1068 err = got_error(GOT_ERR_PRIVSEP_LEN);
1071 packfd = imsg_get_fd(&imsg);
1073 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1074 fetch_req.fetch_all_branches, &wanted_branches,
1075 &wanted_refs, fetch_req.list_refs_only,
1076 worktree_branch, remote_head, fetch_req.no_head, &ibuf);
1078 free(worktree_branch);
1080 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
1081 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_PATH);
1082 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1083 err = got_error_from_errno("close");
1084 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1085 err = got_error_from_errno("close");
1087 got_privsep_send_error(&ibuf, err);
1089 err = send_fetch_done(&ibuf, pack_sha1);
1091 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1092 got_privsep_send_error(&ibuf, err);