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>
36 #include "got_compat.h"
38 #include "got_error.h"
39 #include "got_object.h"
41 #include "got_version.h"
42 #include "got_fetch.h"
43 #include "got_reference.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_parse.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_pkt.h"
52 #include "got_lib_gitproto.h"
53 #include "got_lib_ratelimit.h"
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 struct got_object *indexed;
66 static const struct got_capability got_capabilities[] = {
67 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
68 { GOT_CAPA_OFS_DELTA, NULL },
69 { GOT_CAPA_SIDE_BAND_64K, NULL },
73 match_remote_ref(struct got_pathlist_head *have_refs,
74 struct got_object_id *my_id, const char *refname)
76 struct got_pathlist_entry *pe;
78 /* XXX zero-hash signifies we don't have this ref;
79 * we should use a flag instead */
80 memset(my_id, 0, sizeof(*my_id));
82 TAILQ_FOREACH(pe, have_refs, entry) {
83 struct got_object_id *id = pe->data;
84 if (strcmp(pe->path, refname) == 0) {
85 memcpy(my_id, id, sizeof(*my_id));
92 match_branch(const char *branch, const char *wanted_branch)
94 if (strncmp(branch, "refs/heads/", 11) != 0)
97 if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
100 return (strcmp(branch + 11, wanted_branch) == 0);
104 match_wanted_ref(const char *refname, const char *wanted_ref)
106 if (strncmp(refname, "refs/", 5) != 0)
111 * Prevent fetching of references that won't make any
112 * sense outside of the remote repository's context.
114 if (strncmp(refname, "got/", 4) == 0)
116 if (strncmp(refname, "remotes/", 8) == 0)
119 if (strncmp(wanted_ref, "refs/", 5) == 0)
122 /* Allow prefix match. */
123 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
126 /* Allow exact match. */
127 return (strcmp(refname, wanted_ref) == 0);
130 static const struct got_error *
131 send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
133 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
134 return got_error(GOT_ERR_NO_SPACE);
139 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
141 return got_error_from_errno(
142 "imsg_compose FETCH_SERVER_PROGRESS");
144 return got_privsep_flush_imsg(ibuf);
147 static const struct got_error *
148 send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes,
149 struct got_ratelimit *rl)
151 const struct got_error *err;
155 err = got_ratelimit_check(&elapsed, rl);
160 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
161 &bytes, sizeof(bytes)) == -1)
162 return got_error_from_errno(
163 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
165 return got_privsep_flush_imsg(ibuf);
168 static const struct got_error *
169 send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
171 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
172 pack_sha1, SHA1_DIGEST_LENGTH) == -1)
173 return got_error_from_errno("imsg_compose FETCH");
174 return got_privsep_flush_imsg(ibuf);
177 static const struct got_error *
178 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
186 * Truncate messages which exceed the maximum imsg payload size.
187 * Server may send up to 64k.
189 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
190 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
192 /* Only allow printable ASCII. */
193 for (i = 0; i < len; i++) {
194 if (isprint((unsigned char)buf[i]) ||
195 isspace((unsigned char)buf[i]))
197 return got_error_msg(GOT_ERR_BAD_PACKET,
198 "non-printable progress message received from server");
201 return send_fetch_server_progress(ibuf, buf, len);
204 static const struct got_error *
205 fetch_error(const char *buf, size_t len)
207 static char msg[1024];
210 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
211 if (!isprint((unsigned char)buf[i]))
212 return got_error_msg(GOT_ERR_BAD_PACKET,
213 "non-printable error message received from server");
217 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
220 static const struct got_error *
221 send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
224 size_t len, nsymrefs = 0;
225 struct got_pathlist_entry *pe;
227 len = sizeof(struct got_imsg_fetch_symrefs);
228 TAILQ_FOREACH(pe, symrefs, entry) {
229 const char *target = pe->data;
230 len += sizeof(struct got_imsg_fetch_symref) +
231 pe->path_len + strlen(target);
235 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
236 return got_error(GOT_ERR_NO_SPACE);
238 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
240 return got_error_from_errno("imsg_create FETCH_SYMREFS");
242 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
243 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1)
244 return got_error_from_errno("imsg_add FETCH_SYMREFS");
246 TAILQ_FOREACH(pe, symrefs, entry) {
247 const char *name = pe->path;
248 size_t name_len = pe->path_len;
249 const char *target = pe->data;
250 size_t target_len = strlen(target);
252 /* Keep in sync with struct got_imsg_fetch_symref definition! */
253 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
254 return got_error_from_errno("imsg_add FETCH_SYMREFS");
255 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1)
256 return got_error_from_errno("imsg_add FETCH_SYMREFS");
257 if (imsg_add(wbuf, name, name_len) == -1)
258 return got_error_from_errno("imsg_add FETCH_SYMREFS");
259 if (imsg_add(wbuf, target, target_len) == -1)
260 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");
290 imsg_close(ibuf, wbuf);
291 return got_privsep_flush_imsg(ibuf);
294 static const struct got_error *
295 fetch_ref(struct imsgbuf *ibuf, struct got_pathlist_head *have_refs,
296 struct got_object_id *have, struct got_object_id *want,
297 const char *refname, const char *id_str)
299 const struct got_error *err;
300 char *theirs = NULL, *mine = NULL;
302 if (!got_parse_sha1_digest(want->sha1, id_str)) {
303 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
307 match_remote_ref(have_refs, have, refname);
308 err = send_fetch_ref(ibuf, want, refname);
313 fprintf(stderr, "%s: %s will be fetched\n",
314 getprogname(), refname);
316 err = got_object_id_str(&theirs, want);
319 err = got_object_id_str(&mine, have);
322 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
323 getprogname(), theirs, getprogname(), mine);
331 static const struct got_error *
332 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
333 struct got_pathlist_head *have_refs, int fetch_all_branches,
334 struct got_pathlist_head *wanted_branches,
335 struct got_pathlist_head *wanted_refs, int list_refs_only,
336 const char *worktree_branch, 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);
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);
504 if (!found_branch && default_branch && default_id_str &&
505 strncmp(default_branch, "refs/heads/", 11) == 0) {
506 err = fetch_ref(ibuf, have_refs, &have[nref],
507 &want[nref], default_branch, default_id_str);
514 /* Abort if we haven't found anything to fetch. */
516 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
520 for (i = 0; i < nref; i++) {
521 if (got_object_id_cmp(&have[i], &want[i]) == 0)
523 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
524 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
525 sent_my_capabilites || my_capabilities == NULL ?
526 "" : my_capabilities);
527 if (n < 0 || (size_t)n >= sizeof(buf)) {
528 err = got_error(GOT_ERR_NO_SPACE);
531 err = got_pkt_writepkt(fd, buf, n, chattygot);
534 sent_my_capabilites = 1;
537 err = got_pkt_flushpkt(fd, chattygot);
544 TAILQ_FOREACH(pe, have_refs, entry) {
545 struct got_object_id *id = pe->data;
546 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
547 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
548 if (n < 0 || (size_t)n >= sizeof(buf)) {
549 err = got_error(GOT_ERR_NO_SPACE);
552 err = got_pkt_writepkt(fd, buf, n, chattygot);
558 while (nhave > 0 && !acked) {
559 struct got_object_id common_id;
561 /* The server should ACK the object IDs we need. */
562 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
565 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
566 err = fetch_error(&buf[4], n - 4);
569 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
570 /* Server has not located our objects yet. */
573 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
574 strncmp(buf, "ACK ", 4) != 0) {
575 err = got_error_msg(GOT_ERR_BAD_PACKET,
576 "unexpected message from server");
579 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
580 err = got_error_msg(GOT_ERR_BAD_PACKET,
581 "bad object ID in ACK packet from server");
587 n = strlcpy(buf, "done\n", sizeof(buf));
588 err = got_pkt_writepkt(fd, buf, n, chattygot);
593 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
596 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
597 err = got_error_msg(GOT_ERR_BAD_PACKET,
598 "unexpected message from server");
604 fprintf(stderr, "%s: fetching...\n", getprogname());
606 if (my_capabilities != NULL &&
607 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
614 if (have_sidebands) {
615 err = got_pkt_readhdr(&datalen, fd, chattygot);
621 /* Read sideband channel ID (one byte). */
622 r = read(fd, buf, 1);
624 err = got_error_from_errno("read");
628 err = got_error_msg(GOT_ERR_BAD_PACKET,
632 if (datalen > sizeof(buf) - 5) {
633 err = got_error_msg(GOT_ERR_BAD_PACKET,
634 "bad packet length");
637 datalen--; /* sideband ID has been read */
638 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
639 /* Read packfile data. */
640 err = got_pkt_readn(&r, fd, buf, datalen);
644 err = got_error_msg(GOT_ERR_BAD_PACKET,
648 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
649 err = got_pkt_readn(&r, fd, buf, datalen);
653 err = got_error_msg(GOT_ERR_BAD_PACKET,
657 err = fetch_progress(ibuf, buf, r);
661 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
662 err = got_pkt_readn(&r, fd, buf, datalen);
666 err = got_error_msg(GOT_ERR_BAD_PACKET,
670 err = fetch_error(buf, r);
672 } else if (buf[0] == 'A') {
673 err = got_pkt_readn(&r, fd, buf, datalen);
677 err = got_error_msg(GOT_ERR_BAD_PACKET,
682 * Git server responds with ACK after 'done'
683 * even though multi_ack is disabled?!?
686 if (strncmp(buf, "CK ", 3) == 0)
687 continue; /* ignore */
688 err = got_error_msg(GOT_ERR_BAD_PACKET,
689 "unexpected message from server");
692 err = got_error_msg(GOT_ERR_BAD_PACKET,
693 "unknown side-band received from server");
697 /* No sideband channel. Every byte is packfile data. */
698 err = got_pkt_readn(&r, fd, buf, sizeof buf);
706 * An expected SHA1 checksum sits at the end of the pack file.
707 * Since we don't know the file size ahead of time we have to
708 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
709 * those bytes into our SHA1 checksum computation until we
710 * know for sure that additional pack file data bytes follow.
712 * We can assume r > 0 since otherwise the loop would exit.
714 if (r < SHA1_DIGEST_LENGTH) {
715 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
717 * If there's enough buffered + read data to
718 * fill up the buffer then shift a sufficient
719 * amount of bytes out at the front to make
720 * room, mixing those bytes into the checksum.
722 if (sha1_buf_len > 0 &&
723 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
724 size_t nshift = MIN(sha1_buf_len + r -
725 SHA1_DIGEST_LENGTH, sha1_buf_len);
726 SHA1Update(&sha1_ctx, sha1_buf, nshift);
727 memmove(sha1_buf, sha1_buf + nshift,
728 sha1_buf_len - nshift);
729 sha1_buf_len -= nshift;
732 /* Buffer potential checksum bytes. */
733 memcpy(sha1_buf + sha1_buf_len, buf, r);
737 * Mix in previously buffered bytes which
738 * are not part of the checksum after all.
740 SHA1Update(&sha1_ctx, sha1_buf, r);
742 /* Update potential checksum buffer. */
743 memmove(sha1_buf, sha1_buf + r,
745 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
748 /* Mix in any previously buffered bytes. */
749 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
751 /* Mix in bytes read minus potential checksum bytes. */
752 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
754 /* Buffer potential checksum bytes. */
755 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
757 sha1_buf_len = SHA1_DIGEST_LENGTH;
760 /* Write packfile data to temporary pack file. */
761 w = write(packfd, buf, r);
763 err = got_error_from_errno("write");
767 err = got_error(GOT_ERR_IO);
772 /* Don't send too many progress privsep messages. */
773 if (packsz > last_reported_packsz + 1024) {
774 err = send_fetch_download_progress(ibuf, packsz, &rl);
777 last_reported_packsz = packsz;
780 err = send_fetch_download_progress(ibuf, packsz, NULL);
784 SHA1Final(pack_sha1, &sha1_ctx);
785 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
786 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
787 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
788 "pack file checksum mismatch");
791 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
795 free(default_id_str);
797 free(server_capabilities);
803 main(int argc, char **argv)
805 const struct got_error *err = NULL;
806 int fetchfd = -1, packfd = -1;
807 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
810 struct got_pathlist_head have_refs;
811 struct got_pathlist_head wanted_branches;
812 struct got_pathlist_head wanted_refs;
813 struct got_imsg_fetch_request fetch_req;
814 struct got_imsg_fetch_have_ref href;
815 struct got_imsg_fetch_wanted_branch wbranch;
816 struct got_imsg_fetch_wanted_ref wref;
818 char *worktree_branch = NULL;
825 TAILQ_INIT(&have_refs);
826 TAILQ_INIT(&wanted_branches);
827 TAILQ_INIT(&wanted_refs);
829 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
831 /* revoke access to most system calls */
832 if (pledge("stdio recvfd", NULL) == -1) {
833 err = got_error_from_errno("pledge");
834 got_privsep_send_error(&ibuf, err);
838 /* revoke fs access */
839 if (landlock_no_fs() == -1) {
840 err = got_error_from_errno("landlock_no_fs");
841 got_privsep_send_error(&ibuf, err);
844 if (cap_enter() == -1) {
845 err = got_error_from_errno("cap_enter");
846 got_privsep_send_error(&ibuf, err);
850 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
852 if (err->code == GOT_ERR_PRIVSEP_PIPE)
856 if (imsg.hdr.type == GOT_IMSG_STOP)
858 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
859 err = got_error(GOT_ERR_PRIVSEP_MSG);
862 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
863 if (datalen < sizeof(fetch_req)) {
864 err = got_error(GOT_ERR_PRIVSEP_LEN);
867 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
870 if (datalen != sizeof(fetch_req) +
871 fetch_req.worktree_branch_len) {
872 err = got_error(GOT_ERR_PRIVSEP_LEN);
876 if (fetch_req.worktree_branch_len != 0) {
877 worktree_branch = strndup(imsg.data +
878 sizeof(fetch_req), fetch_req.worktree_branch_len);
879 if (worktree_branch == NULL) {
880 err = got_error_from_errno("strndup");
887 if (fetch_req.verbosity > 0)
888 chattygot += fetch_req.verbosity;
890 for (i = 0; i < fetch_req.n_have_refs; i++) {
891 struct got_object_id *id;
894 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
896 if (err->code == GOT_ERR_PRIVSEP_PIPE)
900 if (imsg.hdr.type == GOT_IMSG_STOP)
902 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
903 err = got_error(GOT_ERR_PRIVSEP_MSG);
906 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
907 if (datalen < sizeof(href)) {
908 err = got_error(GOT_ERR_PRIVSEP_LEN);
911 memcpy(&href, imsg.data, sizeof(href));
912 if (datalen - sizeof(href) < href.name_len) {
913 err = got_error(GOT_ERR_PRIVSEP_LEN);
916 refname = strndup(imsg.data + sizeof(href), href.name_len);
917 if (refname == NULL) {
918 err = got_error_from_errno("strndup");
922 id = malloc(sizeof(*id));
925 err = got_error_from_errno("malloc");
928 memcpy(id, &href.id, sizeof(*id));
929 err = got_pathlist_append(&have_refs, refname, id);
939 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
942 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
944 if (err->code == GOT_ERR_PRIVSEP_PIPE)
948 if (imsg.hdr.type == GOT_IMSG_STOP)
950 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
951 err = got_error(GOT_ERR_PRIVSEP_MSG);
954 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
955 if (datalen < sizeof(wbranch)) {
956 err = got_error(GOT_ERR_PRIVSEP_LEN);
959 memcpy(&wbranch, imsg.data, sizeof(wbranch));
960 if (datalen - sizeof(wbranch) < wbranch.name_len) {
961 err = got_error(GOT_ERR_PRIVSEP_LEN);
964 refname = strndup(imsg.data + sizeof(wbranch),
966 if (refname == NULL) {
967 err = got_error_from_errno("strndup");
971 err = got_pathlist_append(&wanted_branches, refname, NULL);
980 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
983 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
985 if (err->code == GOT_ERR_PRIVSEP_PIPE)
989 if (imsg.hdr.type == GOT_IMSG_STOP)
991 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
992 err = got_error(GOT_ERR_PRIVSEP_MSG);
995 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
996 if (datalen < sizeof(wref)) {
997 err = got_error(GOT_ERR_PRIVSEP_LEN);
1000 memcpy(&wref, imsg.data, sizeof(wref));
1001 if (datalen - sizeof(wref) < wref.name_len) {
1002 err = got_error(GOT_ERR_PRIVSEP_LEN);
1005 refname = strndup(imsg.data + sizeof(wref), wref.name_len);
1006 if (refname == NULL) {
1007 err = got_error_from_errno("strndup");
1011 err = got_pathlist_append(&wanted_refs, refname, NULL);
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_OUTFD) {
1029 err = got_error(GOT_ERR_PRIVSEP_MSG);
1032 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1033 err = got_error(GOT_ERR_PRIVSEP_LEN);
1038 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1039 fetch_req.fetch_all_branches, &wanted_branches,
1040 &wanted_refs, fetch_req.list_refs_only,
1041 worktree_branch, &ibuf);
1043 free(worktree_branch);
1044 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
1045 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_PATH);
1046 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1047 err = got_error_from_errno("close");
1048 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1049 err = got_error_from_errno("close");
1051 got_privsep_send_error(&ibuf, err);
1053 err = send_fetch_done(&ibuf, pack_sha1);
1055 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1056 got_privsep_send_error(&ibuf, err);