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>
35 #include "got_compat.h"
37 #include "got_error.h"
38 #include "got_object.h"
40 #include "got_version.h"
41 #include "got_fetch.h"
42 #include "got_reference.h"
44 #include "got_lib_sha1.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_parse.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_pkt.h"
51 #include "got_lib_gitproto.h"
52 #include "got_lib_ratelimit.h"
55 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 struct got_object *indexed;
61 static const struct got_capability got_capabilities[] = {
62 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
63 { GOT_CAPA_OFS_DELTA, NULL },
64 { GOT_CAPA_SIDE_BAND_64K, NULL },
68 match_remote_ref(struct got_pathlist_head *have_refs,
69 struct got_object_id *my_id, char *refname)
71 struct got_pathlist_entry *pe;
73 /* XXX zero-hash signifies we don't have this ref;
74 * we should use a flag instead */
75 memset(my_id, 0, sizeof(*my_id));
77 TAILQ_FOREACH(pe, have_refs, entry) {
78 struct got_object_id *id = pe->data;
79 if (strcmp(pe->path, refname) == 0) {
80 memcpy(my_id, id, sizeof(*my_id));
87 match_branch(const char *branch, const char *wanted_branch)
89 if (strncmp(branch, "refs/heads/", 11) != 0)
92 if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
95 return (strcmp(branch + 11, wanted_branch) == 0);
99 match_wanted_ref(const char *refname, const char *wanted_ref)
101 if (strncmp(refname, "refs/", 5) != 0)
106 * Prevent fetching of references that won't make any
107 * sense outside of the remote repository's context.
109 if (strncmp(refname, "got/", 4) == 0)
111 if (strncmp(refname, "remotes/", 8) == 0)
114 if (strncmp(wanted_ref, "refs/", 5) == 0)
117 /* Allow prefix match. */
118 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
121 /* Allow exact match. */
122 return (strcmp(refname, wanted_ref) == 0);
125 static const struct got_error *
126 send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
128 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
129 return got_error(GOT_ERR_NO_SPACE);
134 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
136 return got_error_from_errno(
137 "imsg_compose FETCH_SERVER_PROGRESS");
139 return got_privsep_flush_imsg(ibuf);
142 static const struct got_error *
143 send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes,
144 struct got_ratelimit *rl)
146 const struct got_error *err;
150 err = got_ratelimit_check(&elapsed, rl);
155 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
156 &bytes, sizeof(bytes)) == -1)
157 return got_error_from_errno(
158 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
160 return got_privsep_flush_imsg(ibuf);
163 static const struct got_error *
164 send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
166 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
167 pack_sha1, SHA1_DIGEST_LENGTH) == -1)
168 return got_error_from_errno("imsg_compose FETCH");
169 return got_privsep_flush_imsg(ibuf);
172 static const struct got_error *
173 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
181 * Truncate messages which exceed the maximum imsg payload size.
182 * Server may send up to 64k.
184 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
185 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
187 /* Only allow printable ASCII. */
188 for (i = 0; i < len; i++) {
189 if (isprint((unsigned char)buf[i]) ||
190 isspace((unsigned char)buf[i]))
192 return got_error_msg(GOT_ERR_BAD_PACKET,
193 "non-printable progress message received from server");
196 return send_fetch_server_progress(ibuf, buf, len);
199 static const struct got_error *
200 fetch_error(const char *buf, size_t len)
202 static char msg[1024];
205 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
206 if (!isprint(buf[i]))
207 return got_error_msg(GOT_ERR_BAD_PACKET,
208 "non-printable error message received from server");
212 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
215 static const struct got_error *
216 send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
218 const struct got_error *err = NULL;
220 size_t len, nsymrefs = 0;
221 struct got_pathlist_entry *pe;
223 len = sizeof(struct got_imsg_fetch_symrefs);
224 TAILQ_FOREACH(pe, symrefs, entry) {
225 const char *target = pe->data;
226 len += sizeof(struct got_imsg_fetch_symref) +
227 pe->path_len + strlen(target);
231 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
232 return got_error(GOT_ERR_NO_SPACE);
234 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
236 return got_error_from_errno("imsg_create FETCH_SYMREFS");
238 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
239 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
240 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
245 TAILQ_FOREACH(pe, symrefs, entry) {
246 const char *name = pe->path;
247 size_t name_len = pe->path_len;
248 const char *target = pe->data;
249 size_t target_len = strlen(target);
251 /* Keep in sync with struct got_imsg_fetch_symref definition! */
252 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
253 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
257 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
258 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
262 if (imsg_add(wbuf, name, name_len) == -1) {
263 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
267 if (imsg_add(wbuf, target, target_len) == -1) {
268 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
275 imsg_close(ibuf, wbuf);
276 return got_privsep_flush_imsg(ibuf);
279 static const struct got_error *
280 send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
283 const struct got_error *err = NULL;
285 size_t len, reflen = strlen(refname);
287 len = sizeof(struct got_imsg_fetch_ref) + reflen;
288 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
289 return got_error(GOT_ERR_NO_SPACE);
291 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
293 return got_error_from_errno("imsg_create FETCH_REF");
295 /* Keep in sync with struct got_imsg_fetch_ref definition! */
296 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
297 err = got_error_from_errno("imsg_add FETCH_REF");
301 if (imsg_add(wbuf, refname, reflen) == -1) {
302 err = got_error_from_errno("imsg_add FETCH_REF");
308 imsg_close(ibuf, wbuf);
309 return got_privsep_flush_imsg(ibuf);
312 static const struct got_error *
313 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
314 struct got_pathlist_head *have_refs, int fetch_all_branches,
315 struct got_pathlist_head *wanted_branches,
316 struct got_pathlist_head *wanted_refs, int list_refs_only,
317 struct imsgbuf *ibuf)
319 const struct got_error *err = NULL;
320 char buf[GOT_PKT_MAX];
321 char hashstr[SHA1_DIGEST_STRING_LENGTH];
322 struct got_object_id *have, *want;
323 int is_firstpkt = 1, nref = 0, refsz = 16;
324 int i, n, nwant = 0, nhave = 0, acked = 0;
325 off_t packsz = 0, last_reported_packsz = 0;
326 char *id_str = NULL, *refname = NULL;
327 char *server_capabilities = NULL, *my_capabilities = NULL;
328 const char *default_branch = NULL;
329 struct got_pathlist_head symrefs;
330 struct got_pathlist_entry *pe;
331 int sent_my_capabilites = 0, have_sidebands = 0;
332 int found_branch = 0;
334 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
335 size_t sha1_buf_len = 0;
337 struct got_ratelimit rl;
339 TAILQ_INIT(&symrefs);
341 got_ratelimit_init(&rl, 0, 500);
343 have = malloc(refsz * sizeof(have[0]));
345 return got_error_from_errno("malloc");
346 want = malloc(refsz * sizeof(want[0]));
348 err = got_error_from_errno("malloc");
352 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
357 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
358 err = fetch_error(&buf[4], n - 4);
363 err = got_gitproto_parse_refline(&id_str, &refname,
364 &server_capabilities, buf, n);
368 if (chattygot && server_capabilities[0] != '\0')
369 fprintf(stderr, "%s: server capabilities: %s\n",
370 getprogname(), server_capabilities);
371 err = got_gitproto_match_capabilities(&my_capabilities,
372 &symrefs, server_capabilities,
373 got_capabilities, nitems(got_capabilities));
377 fprintf(stderr, "%s: my capabilities:%s\n",
378 getprogname(), my_capabilities != NULL ?
379 my_capabilities : "");
380 err = send_fetch_symrefs(ibuf, &symrefs);
384 if (!fetch_all_branches) {
385 TAILQ_FOREACH(pe, &symrefs, entry) {
386 const char *name = pe->path;
387 const char *symref_target = pe->data;
388 if (strcmp(name, GOT_REF_HEAD) != 0)
390 default_branch = symref_target;
396 if (strstr(refname, "^{}")) {
398 fprintf(stderr, "%s: ignoring %s\n",
399 getprogname(), refname);
404 if (strncmp(refname, "refs/heads/", 11) == 0) {
405 if (fetch_all_branches || list_refs_only) {
407 } else if (!TAILQ_EMPTY(wanted_branches)) {
408 TAILQ_FOREACH(pe, wanted_branches, entry) {
409 if (match_branch(refname, pe->path))
416 getprogname(), refname);
421 } else if (default_branch != NULL) {
422 if (!match_branch(refname, default_branch)) {
426 getprogname(), refname);
432 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
433 if (!TAILQ_EMPTY(wanted_refs)) {
434 TAILQ_FOREACH(pe, wanted_refs, entry) {
435 if (match_wanted_ref(refname, pe->path))
442 getprogname(), refname);
447 } else if (!list_refs_only) {
449 fprintf(stderr, "%s: ignoring %s\n",
450 getprogname(), refname);
456 if (refsz == nref + 1) {
458 have = reallocarray(have, refsz, sizeof(have[0]));
460 err = got_error_from_errno("reallocarray");
463 want = reallocarray(want, refsz, sizeof(want[0]));
465 err = got_error_from_errno("reallocarray");
469 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
470 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
473 match_remote_ref(have_refs, &have[nref], refname);
474 err = send_fetch_ref(ibuf, &want[nref], refname);
479 fprintf(stderr, "%s: %s will be fetched\n",
480 getprogname(), refname);
483 err = got_object_id_str(&theirs, &want[nref]);
486 err = got_object_id_str(&mine, &have[nref]);
491 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
492 getprogname(), theirs, getprogname(), mine);
502 /* Abort if we haven't found any branch to fetch. */
504 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
508 for (i = 0; i < nref; i++) {
509 if (got_object_id_cmp(&have[i], &want[i]) == 0)
511 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
512 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
513 sent_my_capabilites || my_capabilities == NULL ?
514 "" : my_capabilities);
515 if (n >= sizeof(buf)) {
516 err = got_error(GOT_ERR_NO_SPACE);
519 err = got_pkt_writepkt(fd, buf, n, chattygot);
522 sent_my_capabilites = 1;
525 err = got_pkt_flushpkt(fd, chattygot);
532 TAILQ_FOREACH(pe, have_refs, entry) {
533 struct got_object_id *id = pe->data;
534 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
535 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
536 if (n >= sizeof(buf)) {
537 err = got_error(GOT_ERR_NO_SPACE);
540 err = got_pkt_writepkt(fd, buf, n, chattygot);
546 while (nhave > 0 && !acked) {
547 struct got_object_id common_id;
549 /* The server should ACK the object IDs we need. */
550 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
553 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
554 err = fetch_error(&buf[4], n - 4);
557 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
558 /* Server has not located our objects yet. */
561 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
562 strncmp(buf, "ACK ", 4) != 0) {
563 err = got_error_msg(GOT_ERR_BAD_PACKET,
564 "unexpected message from server");
567 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
568 err = got_error_msg(GOT_ERR_BAD_PACKET,
569 "bad object ID in ACK packet from server");
575 n = snprintf(buf, sizeof(buf), "done\n");
576 err = got_pkt_writepkt(fd, buf, n, chattygot);
581 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
584 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
585 err = got_error_msg(GOT_ERR_BAD_PACKET,
586 "unexpected message from server");
592 fprintf(stderr, "%s: fetching...\n", getprogname());
594 if (my_capabilities != NULL &&
595 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
602 if (have_sidebands) {
603 err = got_pkt_readhdr(&datalen, fd, chattygot);
609 /* Read sideband channel ID (one byte). */
610 r = read(fd, buf, 1);
612 err = got_error_from_errno("read");
616 err = got_error_msg(GOT_ERR_BAD_PACKET,
620 if (datalen > sizeof(buf) - 5) {
621 err = got_error_msg(GOT_ERR_BAD_PACKET,
622 "bad packet length");
625 datalen--; /* sideband ID has been read */
626 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
627 /* Read packfile data. */
628 err = got_pkt_readn(&r, fd, buf, datalen);
632 err = got_error_msg(GOT_ERR_BAD_PACKET,
636 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
637 err = got_pkt_readn(&r, fd, buf, datalen);
641 err = got_error_msg(GOT_ERR_BAD_PACKET,
645 err = fetch_progress(ibuf, buf, r);
649 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
650 err = got_pkt_readn(&r, fd, buf, datalen);
654 err = got_error_msg(GOT_ERR_BAD_PACKET,
658 err = fetch_error(buf, r);
660 } else if (buf[0] == 'A') {
661 err = got_pkt_readn(&r, fd, buf, datalen);
665 err = got_error_msg(GOT_ERR_BAD_PACKET,
670 * Git server responds with ACK after 'done'
671 * even though multi_ack is disabled?!?
674 if (strncmp(buf, "CK ", 3) == 0)
675 continue; /* ignore */
676 err = got_error_msg(GOT_ERR_BAD_PACKET,
677 "unexpected message from server");
680 err = got_error_msg(GOT_ERR_BAD_PACKET,
681 "unknown side-band received from server");
685 /* No sideband channel. Every byte is packfile data. */
686 err = got_pkt_readn(&r, fd, buf, sizeof buf);
694 * An expected SHA1 checksum sits at the end of the pack file.
695 * Since we don't know the file size ahead of time we have to
696 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
697 * those bytes into our SHA1 checksum computation until we
698 * know for sure that additional pack file data bytes follow.
700 * We can assume r > 0 since otherwise the loop would exit.
702 if (r < SHA1_DIGEST_LENGTH) {
703 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
705 * If there's enough buffered + read data to
706 * fill up the buffer then shift a sufficient
707 * amount of bytes out at the front to make
708 * room, mixing those bytes into the checksum.
710 while (sha1_buf_len > 0 &&
711 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
712 SHA1Update(&sha1_ctx, sha1_buf, 1);
713 memmove(sha1_buf, sha1_buf + 1, 1);
717 /* Buffer potential checksum bytes. */
718 memcpy(sha1_buf + sha1_buf_len, buf, r);
722 * Mix in previously buffered bytes which
723 * are not part of the checksum after all.
725 SHA1Update(&sha1_ctx, sha1_buf, r);
727 /* Update potential checksum buffer. */
728 memmove(sha1_buf, sha1_buf + r,
730 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
733 /* Mix in any previously buffered bytes. */
734 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
736 /* Mix in bytes read minus potential checksum bytes. */
737 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
739 /* Buffer potential checksum bytes. */
740 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
742 sha1_buf_len = SHA1_DIGEST_LENGTH;
745 /* Write packfile data to temporary pack file. */
746 w = write(packfd, buf, r);
748 err = got_error_from_errno("write");
752 err = got_error(GOT_ERR_IO);
757 /* Don't send too many progress privsep messages. */
758 if (packsz > last_reported_packsz + 1024) {
759 err = send_fetch_download_progress(ibuf, packsz, &rl);
762 last_reported_packsz = packsz;
765 err = send_fetch_download_progress(ibuf, packsz, NULL);
769 SHA1Final(pack_sha1, &sha1_ctx);
770 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
771 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
772 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
773 "pack file checksum mismatch");
776 TAILQ_FOREACH(pe, &symrefs, entry) {
777 free((void *)pe->path);
780 got_pathlist_free(&symrefs);
785 free(server_capabilities);
791 main(int argc, char **argv)
793 const struct got_error *err = NULL;
794 int fetchfd, packfd = -1;
795 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
798 struct got_pathlist_head have_refs;
799 struct got_pathlist_head wanted_branches;
800 struct got_pathlist_head wanted_refs;
801 struct got_pathlist_entry *pe;
802 struct got_imsg_fetch_request fetch_req;
803 struct got_imsg_fetch_have_ref href;
804 struct got_imsg_fetch_wanted_branch wbranch;
805 struct got_imsg_fetch_wanted_ref wref;
813 TAILQ_INIT(&have_refs);
814 TAILQ_INIT(&wanted_branches);
815 TAILQ_INIT(&wanted_refs);
817 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
819 /* revoke access to most system calls */
820 if (pledge("stdio recvfd", NULL) == -1) {
821 err = got_error_from_errno("pledge");
822 got_privsep_send_error(&ibuf, err);
826 /* revoke fs access */
827 if (landlock_no_fs() == -1) {
828 err = got_error_from_errno("landlock_no_fs");
829 got_privsep_send_error(&ibuf, err);
833 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
835 if (err->code == GOT_ERR_PRIVSEP_PIPE)
839 if (imsg.hdr.type == GOT_IMSG_STOP)
841 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
842 err = got_error(GOT_ERR_PRIVSEP_MSG);
845 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
846 if (datalen < sizeof(fetch_req)) {
847 err = got_error(GOT_ERR_PRIVSEP_LEN);
850 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
854 if (fetch_req.verbosity > 0)
855 chattygot += fetch_req.verbosity;
857 for (i = 0; i < fetch_req.n_have_refs; i++) {
858 struct got_object_id *id;
861 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
863 if (err->code == GOT_ERR_PRIVSEP_PIPE)
867 if (imsg.hdr.type == GOT_IMSG_STOP)
869 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
870 err = got_error(GOT_ERR_PRIVSEP_MSG);
873 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
874 if (datalen < sizeof(href)) {
875 err = got_error(GOT_ERR_PRIVSEP_LEN);
878 memcpy(&href, imsg.data, sizeof(href));
879 if (datalen - sizeof(href) < href.name_len) {
880 err = got_error(GOT_ERR_PRIVSEP_LEN);
883 refname = malloc(href.name_len + 1);
884 if (refname == NULL) {
885 err = got_error_from_errno("malloc");
888 memcpy(refname, imsg.data + sizeof(href), href.name_len);
889 refname[href.name_len] = '\0';
891 id = malloc(sizeof(*id));
894 err = got_error_from_errno("malloc");
897 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
898 err = got_pathlist_append(&have_refs, refname, id);
908 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
911 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
913 if (err->code == GOT_ERR_PRIVSEP_PIPE)
917 if (imsg.hdr.type == GOT_IMSG_STOP)
919 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
920 err = got_error(GOT_ERR_PRIVSEP_MSG);
923 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
924 if (datalen < sizeof(wbranch)) {
925 err = got_error(GOT_ERR_PRIVSEP_LEN);
928 memcpy(&wbranch, imsg.data, sizeof(wbranch));
929 if (datalen - sizeof(wbranch) < wbranch.name_len) {
930 err = got_error(GOT_ERR_PRIVSEP_LEN);
933 refname = malloc(wbranch.name_len + 1);
934 if (refname == NULL) {
935 err = got_error_from_errno("malloc");
938 memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
939 refname[wbranch.name_len] = '\0';
941 err = got_pathlist_append(&wanted_branches, refname, NULL);
950 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
953 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
955 if (err->code == GOT_ERR_PRIVSEP_PIPE)
959 if (imsg.hdr.type == GOT_IMSG_STOP)
961 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
962 err = got_error(GOT_ERR_PRIVSEP_MSG);
965 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
966 if (datalen < sizeof(wref)) {
967 err = got_error(GOT_ERR_PRIVSEP_LEN);
970 memcpy(&wref, imsg.data, sizeof(wref));
971 if (datalen - sizeof(wref) < wref.name_len) {
972 err = got_error(GOT_ERR_PRIVSEP_LEN);
975 refname = malloc(wref.name_len + 1);
976 if (refname == NULL) {
977 err = got_error_from_errno("malloc");
980 memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
981 refname[wref.name_len] = '\0';
983 err = got_pathlist_append(&wanted_refs, refname, NULL);
992 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
994 if (err->code == GOT_ERR_PRIVSEP_PIPE)
998 if (imsg.hdr.type == GOT_IMSG_STOP)
1000 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
1001 err = got_error(GOT_ERR_PRIVSEP_MSG);
1004 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1005 err = got_error(GOT_ERR_PRIVSEP_LEN);
1010 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
1011 fetch_req.fetch_all_branches, &wanted_branches,
1012 &wanted_refs, fetch_req.list_refs_only, &ibuf);
1014 TAILQ_FOREACH(pe, &have_refs, entry) {
1015 free((char *)pe->path);
1018 got_pathlist_free(&have_refs);
1019 TAILQ_FOREACH(pe, &wanted_branches, entry)
1020 free((char *)pe->path);
1021 got_pathlist_free(&wanted_branches);
1022 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1023 err = got_error_from_errno("close");
1024 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1025 err = got_error_from_errno("close");
1027 got_privsep_send_error(&ibuf, err);
1029 err = send_fetch_done(&ibuf, pack_sha1);
1031 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1032 got_privsep_send_error(&ibuf, err);