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>
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, 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(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->sha1, SHA1_DIGEST_LENGTH) == -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_pack(int fd, int packfd, uint8_t *pack_sha1,
296 struct got_pathlist_head *have_refs, int fetch_all_branches,
297 struct got_pathlist_head *wanted_branches,
298 struct got_pathlist_head *wanted_refs, int list_refs_only,
299 struct imsgbuf *ibuf)
301 const struct got_error *err = NULL;
302 char buf[GOT_PKT_MAX];
303 char hashstr[SHA1_DIGEST_STRING_LENGTH];
304 struct got_object_id *have, *want;
305 int is_firstpkt = 1, nref = 0, refsz = 16;
306 int i, n, nwant = 0, nhave = 0, acked = 0;
307 off_t packsz = 0, last_reported_packsz = 0;
308 char *id_str = NULL, *refname = NULL;
309 char *server_capabilities = NULL, *my_capabilities = NULL;
310 const char *default_branch = NULL;
311 struct got_pathlist_head symrefs;
312 struct got_pathlist_entry *pe;
313 int sent_my_capabilites = 0, have_sidebands = 0;
314 int found_branch = 0;
316 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
317 size_t sha1_buf_len = 0;
319 struct got_ratelimit rl;
321 TAILQ_INIT(&symrefs);
323 got_ratelimit_init(&rl, 0, 500);
325 have = malloc(refsz * sizeof(have[0]));
327 return got_error_from_errno("malloc");
328 want = malloc(refsz * sizeof(want[0]));
330 err = got_error_from_errno("malloc");
334 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
339 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
340 err = fetch_error(&buf[4], n - 4);
345 err = got_gitproto_parse_refline(&id_str, &refname,
346 &server_capabilities, buf, n);
350 if (chattygot && server_capabilities[0] != '\0')
351 fprintf(stderr, "%s: server capabilities: %s\n",
352 getprogname(), server_capabilities);
353 err = got_gitproto_match_capabilities(&my_capabilities,
354 &symrefs, server_capabilities,
355 got_capabilities, nitems(got_capabilities));
359 fprintf(stderr, "%s: my capabilities:%s\n",
360 getprogname(), my_capabilities != NULL ?
361 my_capabilities : "");
362 err = send_fetch_symrefs(ibuf, &symrefs);
366 if (!fetch_all_branches) {
367 TAILQ_FOREACH(pe, &symrefs, entry) {
368 const char *name = pe->path;
369 const char *symref_target = pe->data;
370 if (strcmp(name, GOT_REF_HEAD) != 0)
372 default_branch = symref_target;
378 if (strstr(refname, "^{}")) {
380 fprintf(stderr, "%s: ignoring %s\n",
381 getprogname(), refname);
386 if (strncmp(refname, "refs/heads/", 11) == 0) {
387 if (fetch_all_branches || list_refs_only) {
389 } else if (!TAILQ_EMPTY(wanted_branches)) {
390 TAILQ_FOREACH(pe, wanted_branches, entry) {
391 if (match_branch(refname, pe->path))
398 getprogname(), refname);
403 } else if (default_branch != NULL) {
404 if (!match_branch(refname, default_branch)) {
408 getprogname(), refname);
414 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
415 if (!TAILQ_EMPTY(wanted_refs)) {
416 TAILQ_FOREACH(pe, wanted_refs, entry) {
417 if (match_wanted_ref(refname, pe->path))
424 getprogname(), refname);
429 } else if (!list_refs_only) {
431 fprintf(stderr, "%s: ignoring %s\n",
432 getprogname(), refname);
438 if (refsz == nref + 1) {
440 have = reallocarray(have, refsz, sizeof(have[0]));
442 err = got_error_from_errno("reallocarray");
445 want = reallocarray(want, refsz, sizeof(want[0]));
447 err = got_error_from_errno("reallocarray");
451 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
452 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
455 match_remote_ref(have_refs, &have[nref], refname);
456 err = send_fetch_ref(ibuf, &want[nref], refname);
461 fprintf(stderr, "%s: %s will be fetched\n",
462 getprogname(), refname);
465 err = got_object_id_str(&theirs, &want[nref]);
468 err = got_object_id_str(&mine, &have[nref]);
473 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
474 getprogname(), theirs, getprogname(), mine);
484 /* Abort if we haven't found any branch to fetch. */
486 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
490 for (i = 0; i < nref; i++) {
491 if (got_object_id_cmp(&have[i], &want[i]) == 0)
493 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
494 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
495 sent_my_capabilites || my_capabilities == NULL ?
496 "" : my_capabilities);
497 if (n < 0 || (size_t)n >= sizeof(buf)) {
498 err = got_error(GOT_ERR_NO_SPACE);
501 err = got_pkt_writepkt(fd, buf, n, chattygot);
504 sent_my_capabilites = 1;
507 err = got_pkt_flushpkt(fd, chattygot);
514 TAILQ_FOREACH(pe, have_refs, entry) {
515 struct got_object_id *id = pe->data;
516 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
517 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
518 if (n < 0 || (size_t)n >= sizeof(buf)) {
519 err = got_error(GOT_ERR_NO_SPACE);
522 err = got_pkt_writepkt(fd, buf, n, chattygot);
528 while (nhave > 0 && !acked) {
529 struct got_object_id common_id;
531 /* The server should ACK the object IDs we need. */
532 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
535 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
536 err = fetch_error(&buf[4], n - 4);
539 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
540 /* Server has not located our objects yet. */
543 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
544 strncmp(buf, "ACK ", 4) != 0) {
545 err = got_error_msg(GOT_ERR_BAD_PACKET,
546 "unexpected message from server");
549 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
550 err = got_error_msg(GOT_ERR_BAD_PACKET,
551 "bad object ID in ACK packet from server");
557 n = strlcpy(buf, "done\n", sizeof(buf));
558 err = got_pkt_writepkt(fd, buf, n, chattygot);
563 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
566 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
567 err = got_error_msg(GOT_ERR_BAD_PACKET,
568 "unexpected message from server");
574 fprintf(stderr, "%s: fetching...\n", getprogname());
576 if (my_capabilities != NULL &&
577 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
584 if (have_sidebands) {
585 err = got_pkt_readhdr(&datalen, fd, chattygot);
591 /* Read sideband channel ID (one byte). */
592 r = read(fd, buf, 1);
594 err = got_error_from_errno("read");
598 err = got_error_msg(GOT_ERR_BAD_PACKET,
602 if (datalen > sizeof(buf) - 5) {
603 err = got_error_msg(GOT_ERR_BAD_PACKET,
604 "bad packet length");
607 datalen--; /* sideband ID has been read */
608 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
609 /* Read packfile data. */
610 err = got_pkt_readn(&r, fd, buf, datalen);
614 err = got_error_msg(GOT_ERR_BAD_PACKET,
618 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
619 err = got_pkt_readn(&r, fd, buf, datalen);
623 err = got_error_msg(GOT_ERR_BAD_PACKET,
627 err = fetch_progress(ibuf, buf, r);
631 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
632 err = got_pkt_readn(&r, fd, buf, datalen);
636 err = got_error_msg(GOT_ERR_BAD_PACKET,
640 err = fetch_error(buf, r);
642 } else if (buf[0] == 'A') {
643 err = got_pkt_readn(&r, fd, buf, datalen);
647 err = got_error_msg(GOT_ERR_BAD_PACKET,
652 * Git server responds with ACK after 'done'
653 * even though multi_ack is disabled?!?
656 if (strncmp(buf, "CK ", 3) == 0)
657 continue; /* ignore */
658 err = got_error_msg(GOT_ERR_BAD_PACKET,
659 "unexpected message from server");
662 err = got_error_msg(GOT_ERR_BAD_PACKET,
663 "unknown side-band received from server");
667 /* No sideband channel. Every byte is packfile data. */
668 err = got_pkt_readn(&r, fd, buf, sizeof buf);
676 * An expected SHA1 checksum sits at the end of the pack file.
677 * Since we don't know the file size ahead of time we have to
678 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
679 * those bytes into our SHA1 checksum computation until we
680 * know for sure that additional pack file data bytes follow.
682 * We can assume r > 0 since otherwise the loop would exit.
684 if (r < SHA1_DIGEST_LENGTH) {
685 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
687 * If there's enough buffered + read data to
688 * fill up the buffer then shift a sufficient
689 * amount of bytes out at the front to make
690 * room, mixing those bytes into the checksum.
692 if (sha1_buf_len > 0 &&
693 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
694 size_t nshift = MIN(sha1_buf_len + r -
695 SHA1_DIGEST_LENGTH, sha1_buf_len);
696 SHA1Update(&sha1_ctx, sha1_buf, nshift);
697 memmove(sha1_buf, sha1_buf + nshift,
698 sha1_buf_len - nshift);
699 sha1_buf_len -= nshift;
702 /* Buffer potential checksum bytes. */
703 memcpy(sha1_buf + sha1_buf_len, buf, r);
707 * Mix in previously buffered bytes which
708 * are not part of the checksum after all.
710 SHA1Update(&sha1_ctx, sha1_buf, r);
712 /* Update potential checksum buffer. */
713 memmove(sha1_buf, sha1_buf + r,
715 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
718 /* Mix in any previously buffered bytes. */
719 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
721 /* Mix in bytes read minus potential checksum bytes. */
722 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
724 /* Buffer potential checksum bytes. */
725 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
727 sha1_buf_len = SHA1_DIGEST_LENGTH;
730 /* Write packfile data to temporary pack file. */
731 w = write(packfd, buf, r);
733 err = got_error_from_errno("write");
737 err = got_error(GOT_ERR_IO);
742 /* Don't send too many progress privsep messages. */
743 if (packsz > last_reported_packsz + 1024) {
744 err = send_fetch_download_progress(ibuf, packsz, &rl);
747 last_reported_packsz = packsz;
750 err = send_fetch_download_progress(ibuf, packsz, NULL);
754 SHA1Final(pack_sha1, &sha1_ctx);
755 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
756 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
757 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
758 "pack file checksum mismatch");
761 TAILQ_FOREACH(pe, &symrefs, entry) {
762 free((void *)pe->path);
765 got_pathlist_free(&symrefs);
770 free(server_capabilities);
776 main(int argc, char **argv)
778 const struct got_error *err = NULL;
779 int fetchfd, packfd = -1;
780 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
783 struct got_pathlist_head have_refs;
784 struct got_pathlist_head wanted_branches;
785 struct got_pathlist_head wanted_refs;
786 struct got_pathlist_entry *pe;
787 struct got_imsg_fetch_request fetch_req;
788 struct got_imsg_fetch_have_ref href;
789 struct got_imsg_fetch_wanted_branch wbranch;
790 struct got_imsg_fetch_wanted_ref wref;
798 TAILQ_INIT(&have_refs);
799 TAILQ_INIT(&wanted_branches);
800 TAILQ_INIT(&wanted_refs);
802 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
804 /* revoke access to most system calls */
805 if (pledge("stdio recvfd", NULL) == -1) {
806 err = got_error_from_errno("pledge");
807 got_privsep_send_error(&ibuf, err);
811 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
813 if (err->code == GOT_ERR_PRIVSEP_PIPE)
817 if (imsg.hdr.type == GOT_IMSG_STOP)
819 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
820 err = got_error(GOT_ERR_PRIVSEP_MSG);
823 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
824 if (datalen < sizeof(fetch_req)) {
825 err = got_error(GOT_ERR_PRIVSEP_LEN);
828 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
832 if (fetch_req.verbosity > 0)
833 chattygot += fetch_req.verbosity;
835 for (i = 0; i < fetch_req.n_have_refs; i++) {
836 struct got_object_id *id;
839 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
841 if (err->code == GOT_ERR_PRIVSEP_PIPE)
845 if (imsg.hdr.type == GOT_IMSG_STOP)
847 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
848 err = got_error(GOT_ERR_PRIVSEP_MSG);
851 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
852 if (datalen < sizeof(href)) {
853 err = got_error(GOT_ERR_PRIVSEP_LEN);
856 memcpy(&href, imsg.data, sizeof(href));
857 if (datalen - sizeof(href) < href.name_len) {
858 err = got_error(GOT_ERR_PRIVSEP_LEN);
861 refname = malloc(href.name_len + 1);
862 if (refname == NULL) {
863 err = got_error_from_errno("malloc");
866 memcpy(refname, imsg.data + sizeof(href), href.name_len);
867 refname[href.name_len] = '\0';
869 id = malloc(sizeof(*id));
872 err = got_error_from_errno("malloc");
875 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
876 err = got_pathlist_append(&have_refs, refname, id);
886 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
889 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
891 if (err->code == GOT_ERR_PRIVSEP_PIPE)
895 if (imsg.hdr.type == GOT_IMSG_STOP)
897 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
898 err = got_error(GOT_ERR_PRIVSEP_MSG);
901 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
902 if (datalen < sizeof(wbranch)) {
903 err = got_error(GOT_ERR_PRIVSEP_LEN);
906 memcpy(&wbranch, imsg.data, sizeof(wbranch));
907 if (datalen - sizeof(wbranch) < wbranch.name_len) {
908 err = got_error(GOT_ERR_PRIVSEP_LEN);
911 refname = malloc(wbranch.name_len + 1);
912 if (refname == NULL) {
913 err = got_error_from_errno("malloc");
916 memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
917 refname[wbranch.name_len] = '\0';
919 err = got_pathlist_append(&wanted_branches, refname, NULL);
928 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
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_WANTED_REF) {
940 err = got_error(GOT_ERR_PRIVSEP_MSG);
943 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
944 if (datalen < sizeof(wref)) {
945 err = got_error(GOT_ERR_PRIVSEP_LEN);
948 memcpy(&wref, imsg.data, sizeof(wref));
949 if (datalen - sizeof(wref) < wref.name_len) {
950 err = got_error(GOT_ERR_PRIVSEP_LEN);
953 refname = malloc(wref.name_len + 1);
954 if (refname == NULL) {
955 err = got_error_from_errno("malloc");
958 memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
959 refname[wref.name_len] = '\0';
961 err = got_pathlist_append(&wanted_refs, refname, NULL);
970 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
972 if (err->code == GOT_ERR_PRIVSEP_PIPE)
976 if (imsg.hdr.type == GOT_IMSG_STOP)
978 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
979 err = got_error(GOT_ERR_PRIVSEP_MSG);
982 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
983 err = got_error(GOT_ERR_PRIVSEP_LEN);
988 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
989 fetch_req.fetch_all_branches, &wanted_branches,
990 &wanted_refs, fetch_req.list_refs_only, &ibuf);
992 TAILQ_FOREACH(pe, &have_refs, entry) {
993 free((char *)pe->path);
996 got_pathlist_free(&have_refs);
997 TAILQ_FOREACH(pe, &wanted_branches, entry)
998 free((char *)pe->path);
999 got_pathlist_free(&wanted_branches);
1000 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1001 err = got_error_from_errno("close");
1002 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1003 err = got_error_from_errno("close");
1005 got_privsep_send_error(&ibuf, err);
1007 err = send_fetch_done(&ibuf, pack_sha1);
1009 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1010 got_privsep_send_error(&ibuf, err);