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 if (!got_parse_object_id(want, id_str, GOT_HASH_SHA1)) {
304 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
308 match_remote_ref(have_refs, have, refname);
309 err = send_fetch_ref(ibuf, want, refname);
314 fprintf(stderr, "%s: %s will be fetched\n",
315 getprogname(), refname);
317 err = got_object_id_str(&theirs, want);
320 err = got_object_id_str(&mine, have);
323 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
324 getprogname(), theirs, getprogname(), mine);
332 static const struct got_error *
333 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
334 struct got_pathlist_head *have_refs, int fetch_all_branches,
335 struct got_pathlist_head *wanted_branches,
336 struct got_pathlist_head *wanted_refs, int list_refs_only,
337 const char *worktree_branch, const char *remote_head,
338 int no_head, 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);
361 got_hash_init(&ctx, GOT_HASH_SHA1);
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);
507 * If -b was not used and either none of the requested branches
508 * (got.conf, worktree) were found or the client already has the
509 * remote HEAD symref but its target changed, fetch remote's HEAD.
511 if (!no_head && default_branch && default_id_str &&
512 strncmp(default_branch, "refs/heads/", 11) == 0) {
513 int remote_head_changed = 0;
516 if (strcmp(remote_head, default_branch + 11) != 0)
517 remote_head_changed = 1;
520 if (!found_branch || remote_head_changed) {
521 err = fetch_ref(ibuf, have_refs, &have[nref],
522 &want[nref], default_branch, default_id_str);
529 /* Abort if we haven't found anything to fetch. */
531 struct got_pathlist_entry *pe;
532 static char msg[PATH_MAX + 33];
534 pe = TAILQ_FIRST(wanted_branches);
536 snprintf(msg, sizeof(msg),
537 "branch \"%s\" not found on server", pe->path);
538 err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
542 pe = TAILQ_FIRST(wanted_refs);
544 snprintf(msg, sizeof(msg),
545 "reference \"%s\" not found on server", pe->path);
546 err = got_error_msg(GOT_ERR_FETCH_NO_BRANCH, msg);
550 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
554 for (i = 0; i < nref; i++) {
555 if (got_object_id_cmp(&have[i], &want[i]) == 0)
557 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
558 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
559 sent_my_capabilites || my_capabilities == NULL ?
560 "" : my_capabilities);
561 if (n < 0 || (size_t)n >= sizeof(buf)) {
562 err = got_error(GOT_ERR_NO_SPACE);
565 err = got_pkt_writepkt(fd, buf, n, chattygot);
568 sent_my_capabilites = 1;
571 err = got_pkt_flushpkt(fd, chattygot);
578 TAILQ_FOREACH(pe, have_refs, entry) {
579 struct got_object_id *id = pe->data;
580 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
581 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
582 if (n < 0 || (size_t)n >= sizeof(buf)) {
583 err = got_error(GOT_ERR_NO_SPACE);
586 err = got_pkt_writepkt(fd, buf, n, chattygot);
592 n = strlcpy(buf, "done\n", sizeof(buf));
593 err = got_pkt_writepkt(fd, buf, n, chattygot);
597 while (nhave > 0 && !acked) {
598 struct got_object_id common_id;
600 /* The server should ACK the object IDs we need. */
601 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
604 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
605 err = fetch_error(&buf[4], n - 4);
608 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
610 * Server could not find a common ancestor.
611 * Perhaps it is an out-of-date mirror, or there
612 * is a repository with unrelated history.
616 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
617 strncmp(buf, "ACK ", 4) != 0) {
618 err = got_error_msg(GOT_ERR_BAD_PACKET,
619 "unexpected message from server");
622 if (!got_parse_object_id(&common_id, buf + 4,
624 err = got_error_msg(GOT_ERR_BAD_PACKET,
625 "bad object ID in ACK packet from server");
632 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
635 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
636 err = got_error_msg(GOT_ERR_BAD_PACKET,
637 "unexpected message from server");
643 fprintf(stderr, "%s: fetching...\n", getprogname());
645 if (my_capabilities != NULL &&
646 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
653 if (have_sidebands) {
654 err = got_pkt_readhdr(&datalen, fd, chattygot);
660 /* Read sideband channel ID (one byte). */
661 r = read(fd, buf, 1);
663 err = got_error_from_errno("read");
667 err = got_error_msg(GOT_ERR_BAD_PACKET,
671 if (datalen > sizeof(buf) - 5) {
672 err = got_error_msg(GOT_ERR_BAD_PACKET,
673 "bad packet length");
676 datalen--; /* sideband ID has been read */
677 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
678 /* Read packfile data. */
679 err = got_pkt_readn(&r, fd, buf, datalen);
683 err = got_error_msg(GOT_ERR_BAD_PACKET,
687 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
688 err = got_pkt_readn(&r, fd, buf, datalen);
692 err = got_error_msg(GOT_ERR_BAD_PACKET,
696 err = fetch_progress(ibuf, buf, r);
700 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
701 err = got_pkt_readn(&r, fd, buf, datalen);
705 err = got_error_msg(GOT_ERR_BAD_PACKET,
709 err = fetch_error(buf, r);
711 } else if (buf[0] == 'A') {
712 err = got_pkt_readn(&r, fd, buf, datalen);
716 err = got_error_msg(GOT_ERR_BAD_PACKET,
721 * Git server responds with ACK after 'done'
722 * even though multi_ack is disabled?!?
725 if (strncmp(buf, "CK ", 3) == 0)
726 continue; /* ignore */
727 err = got_error_msg(GOT_ERR_BAD_PACKET,
728 "unexpected message from server");
731 err = got_error_msg(GOT_ERR_BAD_PACKET,
732 "unknown side-band received from server");
736 /* No sideband channel. Every byte is packfile data. */
737 err = got_pkt_readn(&r, fd, buf, sizeof buf);
745 * An expected SHA1 checksum sits at the end of the pack file.
746 * Since we don't know the file size ahead of time we have to
747 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
748 * those bytes into our SHA1 checksum computation until we
749 * know for sure that additional pack file data bytes follow.
751 * We can assume r > 0 since otherwise the loop would exit.
753 if (r < SHA1_DIGEST_LENGTH) {
754 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
756 * If there's enough buffered + read data to
757 * fill up the buffer then shift a sufficient
758 * amount of bytes out at the front to make
759 * room, mixing those bytes into the checksum.
761 if (sha1_buf_len > 0 &&
762 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
763 size_t nshift = MIN(sha1_buf_len + r -
764 SHA1_DIGEST_LENGTH, sha1_buf_len);
765 got_hash_update(&ctx, sha1_buf,
767 memmove(sha1_buf, sha1_buf + nshift,
768 sha1_buf_len - nshift);
769 sha1_buf_len -= nshift;
772 /* Buffer potential checksum bytes. */
773 memcpy(sha1_buf + sha1_buf_len, buf, r);
777 * Mix in previously buffered bytes which
778 * are not part of the checksum after all.
780 got_hash_update(&ctx, sha1_buf, r);
782 /* Update potential checksum buffer. */
783 memmove(sha1_buf, sha1_buf + r,
785 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
788 /* Mix in any previously buffered bytes. */
789 got_hash_update(&ctx, sha1_buf, sha1_buf_len);
791 /* Mix in bytes read minus potential checksum bytes. */
792 got_hash_update(&ctx, buf, r - SHA1_DIGEST_LENGTH);
794 /* Buffer potential checksum bytes. */
795 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
797 sha1_buf_len = SHA1_DIGEST_LENGTH;
800 /* Write packfile data to temporary pack file. */
801 w = write(packfd, buf, r);
803 err = got_error_from_errno("write");
807 err = got_error(GOT_ERR_IO);
812 /* Don't send too many progress privsep messages. */
813 if (packsz > last_reported_packsz + 1024) {
814 err = send_fetch_download_progress(ibuf, packsz, &rl);
817 last_reported_packsz = packsz;
820 err = send_fetch_download_progress(ibuf, packsz, NULL);
824 got_hash_final(&ctx, pack_sha1);
825 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
826 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
827 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
828 "pack file checksum mismatch");
831 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
835 free(default_id_str);
837 free(server_capabilities);
843 main(int argc, char **argv)
845 const struct got_error *err = NULL;
846 int fetchfd = -1, packfd = -1;
847 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
850 struct got_pathlist_head have_refs;
851 struct got_pathlist_head wanted_branches;
852 struct got_pathlist_head wanted_refs;
853 struct got_imsg_fetch_request fetch_req;
854 struct got_imsg_fetch_have_ref href;
855 struct got_imsg_fetch_wanted_branch wbranch;
856 struct got_imsg_fetch_wanted_ref wref;
858 char *remote_head = NULL, *worktree_branch = NULL;
865 TAILQ_INIT(&have_refs);
866 TAILQ_INIT(&wanted_branches);
867 TAILQ_INIT(&wanted_refs);
869 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
871 /* revoke access to most system calls */
872 if (pledge("stdio recvfd", NULL) == -1) {
873 err = got_error_from_errno("pledge");
874 got_privsep_send_error(&ibuf, err);
878 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
880 if (err->code == GOT_ERR_PRIVSEP_PIPE)
884 if (imsg.hdr.type == GOT_IMSG_STOP)
886 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
887 err = got_error(GOT_ERR_PRIVSEP_MSG);
890 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
891 if (datalen < sizeof(fetch_req)) {
892 err = got_error(GOT_ERR_PRIVSEP_LEN);
895 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
898 if (datalen != sizeof(fetch_req) +
899 fetch_req.worktree_branch_len + fetch_req.remote_head_len) {
900 err = got_error(GOT_ERR_PRIVSEP_LEN);
904 if (fetch_req.worktree_branch_len != 0) {
905 worktree_branch = strndup(imsg.data +
906 sizeof(fetch_req), fetch_req.worktree_branch_len);
907 if (worktree_branch == NULL) {
908 err = got_error_from_errno("strndup");
913 if (fetch_req.remote_head_len != 0) {
914 remote_head = strndup(imsg.data + sizeof(fetch_req) +
915 fetch_req.worktree_branch_len, fetch_req.remote_head_len);
916 if (remote_head == NULL) {
917 err = got_error_from_errno("strndup");
924 if (fetch_req.verbosity > 0)
925 chattygot += fetch_req.verbosity;
927 for (i = 0; i < fetch_req.n_have_refs; i++) {
928 struct got_object_id *id;
931 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
933 if (err->code == GOT_ERR_PRIVSEP_PIPE)
937 if (imsg.hdr.type == GOT_IMSG_STOP)
939 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
940 err = got_error(GOT_ERR_PRIVSEP_MSG);
943 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
944 if (datalen < sizeof(href)) {
945 err = got_error(GOT_ERR_PRIVSEP_LEN);
948 memcpy(&href, imsg.data, sizeof(href));
949 if (datalen - sizeof(href) < href.name_len) {
950 err = got_error(GOT_ERR_PRIVSEP_LEN);
953 refname = strndup(imsg.data + sizeof(href), href.name_len);
954 if (refname == NULL) {
955 err = got_error_from_errno("strndup");
959 id = malloc(sizeof(*id));
962 err = got_error_from_errno("malloc");
965 memcpy(id, &href.id, sizeof(*id));
966 err = got_pathlist_append(&have_refs, refname, id);
976 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
979 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
981 if (err->code == GOT_ERR_PRIVSEP_PIPE)
985 if (imsg.hdr.type == GOT_IMSG_STOP)
987 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
988 err = got_error(GOT_ERR_PRIVSEP_MSG);
991 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
992 if (datalen < sizeof(wbranch)) {
993 err = got_error(GOT_ERR_PRIVSEP_LEN);
996 memcpy(&wbranch, imsg.data, sizeof(wbranch));
997 if (datalen - sizeof(wbranch) < wbranch.name_len) {
998 err = got_error(GOT_ERR_PRIVSEP_LEN);
1001 refname = strndup(imsg.data + sizeof(wbranch),
1003 if (refname == NULL) {
1004 err = got_error_from_errno("strndup");
1008 err = got_pathlist_append(&wanted_branches, refname, NULL);
1017 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
1020 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1022 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1026 if (imsg.hdr.type == GOT_IMSG_STOP)
1028 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
1029 err = got_error(GOT_ERR_PRIVSEP_MSG);
1032 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1033 if (datalen < sizeof(wref)) {
1034 err = got_error(GOT_ERR_PRIVSEP_LEN);
1037 memcpy(&wref, imsg.data, sizeof(wref));
1038 if (datalen - sizeof(wref) < wref.name_len) {
1039 err = got_error(GOT_ERR_PRIVSEP_LEN);
1042 refname = strndup(imsg.data + sizeof(wref), wref.name_len);
1043 if (refname == NULL) {
1044 err = got_error_from_errno("strndup");
1048 err = got_pathlist_append(&wanted_refs, refname, NULL);
1057 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1059 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1063 if (imsg.hdr.type == GOT_IMSG_STOP)
1065 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1066 err = got_error(GOT_ERR_PRIVSEP_MSG);
1069 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1070 err = got_error(GOT_ERR_PRIVSEP_LEN);
1075 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1076 fetch_req.fetch_all_branches, &wanted_branches,
1077 &wanted_refs, fetch_req.list_refs_only,
1078 worktree_branch, remote_head, fetch_req.no_head, &ibuf);
1080 free(worktree_branch);
1082 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
1083 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_PATH);
1084 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1085 err = got_error_from_errno("close");
1086 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1087 err = got_error_from_errno("close");
1089 got_privsep_send_error(&ibuf, err);
1091 err = send_fetch_done(&ibuf, pack_sha1);
1093 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1094 got_privsep_send_error(&ibuf, err);