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)
219 size_t len, nsymrefs = 0;
220 struct got_pathlist_entry *pe;
222 len = sizeof(struct got_imsg_fetch_symrefs);
223 TAILQ_FOREACH(pe, symrefs, entry) {
224 const char *target = pe->data;
225 len += sizeof(struct got_imsg_fetch_symref) +
226 pe->path_len + strlen(target);
230 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
231 return got_error(GOT_ERR_NO_SPACE);
233 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
235 return got_error_from_errno("imsg_create FETCH_SYMREFS");
237 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
238 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1)
239 return got_error_from_errno("imsg_add FETCH_SYMREFS");
241 TAILQ_FOREACH(pe, symrefs, entry) {
242 const char *name = pe->path;
243 size_t name_len = pe->path_len;
244 const char *target = pe->data;
245 size_t target_len = strlen(target);
247 /* Keep in sync with struct got_imsg_fetch_symref definition! */
248 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1)
249 return got_error_from_errno("imsg_add FETCH_SYMREFS");
250 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1)
251 return got_error_from_errno("imsg_add FETCH_SYMREFS");
252 if (imsg_add(wbuf, name, name_len) == -1)
253 return got_error_from_errno("imsg_add FETCH_SYMREFS");
254 if (imsg_add(wbuf, target, target_len) == -1)
255 return got_error_from_errno("imsg_add FETCH_SYMREFS");
259 imsg_close(ibuf, wbuf);
260 return got_privsep_flush_imsg(ibuf);
263 static const struct got_error *
264 send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
268 size_t len, reflen = strlen(refname);
270 len = sizeof(struct got_imsg_fetch_ref) + reflen;
271 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
272 return got_error(GOT_ERR_NO_SPACE);
274 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
276 return got_error_from_errno("imsg_create FETCH_REF");
278 /* Keep in sync with struct got_imsg_fetch_ref definition! */
279 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1)
280 return got_error_from_errno("imsg_add FETCH_REF");
281 if (imsg_add(wbuf, refname, reflen) == -1)
282 return got_error_from_errno("imsg_add FETCH_REF");
285 imsg_close(ibuf, wbuf);
286 return got_privsep_flush_imsg(ibuf);
289 static const struct got_error *
290 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
291 struct got_pathlist_head *have_refs, int fetch_all_branches,
292 struct got_pathlist_head *wanted_branches,
293 struct got_pathlist_head *wanted_refs, int list_refs_only,
294 struct imsgbuf *ibuf)
296 const struct got_error *err = NULL;
297 char buf[GOT_PKT_MAX];
298 char hashstr[SHA1_DIGEST_STRING_LENGTH];
299 struct got_object_id *have, *want;
300 int is_firstpkt = 1, nref = 0, refsz = 16;
301 int i, n, nwant = 0, nhave = 0, acked = 0;
302 off_t packsz = 0, last_reported_packsz = 0;
303 char *id_str = NULL, *refname = NULL;
304 char *server_capabilities = NULL, *my_capabilities = NULL;
305 const char *default_branch = NULL;
306 struct got_pathlist_head symrefs;
307 struct got_pathlist_entry *pe;
308 int sent_my_capabilites = 0, have_sidebands = 0;
309 int found_branch = 0;
311 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
312 size_t sha1_buf_len = 0;
314 struct got_ratelimit rl;
316 TAILQ_INIT(&symrefs);
318 got_ratelimit_init(&rl, 0, 500);
320 have = malloc(refsz * sizeof(have[0]));
322 return got_error_from_errno("malloc");
323 want = malloc(refsz * sizeof(want[0]));
325 err = got_error_from_errno("malloc");
329 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
334 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
335 err = fetch_error(&buf[4], n - 4);
340 err = got_gitproto_parse_refline(&id_str, &refname,
341 &server_capabilities, buf, n);
345 if (chattygot && server_capabilities[0] != '\0')
346 fprintf(stderr, "%s: server capabilities: %s\n",
347 getprogname(), server_capabilities);
348 err = got_gitproto_match_capabilities(&my_capabilities,
349 &symrefs, server_capabilities,
350 got_capabilities, nitems(got_capabilities));
354 fprintf(stderr, "%s: my capabilities:%s\n",
355 getprogname(), my_capabilities != NULL ?
356 my_capabilities : "");
357 err = send_fetch_symrefs(ibuf, &symrefs);
361 if (!fetch_all_branches) {
362 TAILQ_FOREACH(pe, &symrefs, entry) {
363 const char *name = pe->path;
364 const char *symref_target = pe->data;
365 if (strcmp(name, GOT_REF_HEAD) != 0)
367 default_branch = symref_target;
373 if (strstr(refname, "^{}")) {
375 fprintf(stderr, "%s: ignoring %s\n",
376 getprogname(), refname);
381 if (strncmp(refname, "refs/heads/", 11) == 0) {
382 if (fetch_all_branches || list_refs_only) {
384 } else if (!TAILQ_EMPTY(wanted_branches)) {
385 TAILQ_FOREACH(pe, wanted_branches, entry) {
386 if (match_branch(refname, pe->path))
393 getprogname(), refname);
398 } else if (default_branch != NULL) {
399 if (!match_branch(refname, default_branch)) {
403 getprogname(), refname);
409 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
410 if (!TAILQ_EMPTY(wanted_refs)) {
411 TAILQ_FOREACH(pe, wanted_refs, entry) {
412 if (match_wanted_ref(refname, pe->path))
419 getprogname(), refname);
424 } else if (!list_refs_only) {
426 fprintf(stderr, "%s: ignoring %s\n",
427 getprogname(), refname);
433 if (refsz == nref + 1) {
435 have = reallocarray(have, refsz, sizeof(have[0]));
437 err = got_error_from_errno("reallocarray");
440 want = reallocarray(want, refsz, sizeof(want[0]));
442 err = got_error_from_errno("reallocarray");
446 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
447 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
450 match_remote_ref(have_refs, &have[nref], refname);
451 err = send_fetch_ref(ibuf, &want[nref], refname);
456 fprintf(stderr, "%s: %s will be fetched\n",
457 getprogname(), refname);
460 err = got_object_id_str(&theirs, &want[nref]);
463 err = got_object_id_str(&mine, &have[nref]);
468 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
469 getprogname(), theirs, getprogname(), mine);
479 /* Abort if we haven't found any branch to fetch. */
481 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
485 for (i = 0; i < nref; i++) {
486 if (got_object_id_cmp(&have[i], &want[i]) == 0)
488 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
489 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
490 sent_my_capabilites || my_capabilities == NULL ?
491 "" : my_capabilities);
492 if (n >= sizeof(buf)) {
493 err = got_error(GOT_ERR_NO_SPACE);
496 err = got_pkt_writepkt(fd, buf, n, chattygot);
499 sent_my_capabilites = 1;
502 err = got_pkt_flushpkt(fd, chattygot);
509 TAILQ_FOREACH(pe, have_refs, entry) {
510 struct got_object_id *id = pe->data;
511 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
512 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
513 if (n >= sizeof(buf)) {
514 err = got_error(GOT_ERR_NO_SPACE);
517 err = got_pkt_writepkt(fd, buf, n, chattygot);
523 while (nhave > 0 && !acked) {
524 struct got_object_id common_id;
526 /* The server should ACK the object IDs we need. */
527 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
530 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
531 err = fetch_error(&buf[4], n - 4);
534 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
535 /* Server has not located our objects yet. */
538 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
539 strncmp(buf, "ACK ", 4) != 0) {
540 err = got_error_msg(GOT_ERR_BAD_PACKET,
541 "unexpected message from server");
544 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
545 err = got_error_msg(GOT_ERR_BAD_PACKET,
546 "bad object ID in ACK packet from server");
552 n = snprintf(buf, sizeof(buf), "done\n");
553 err = got_pkt_writepkt(fd, buf, n, chattygot);
558 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
561 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
562 err = got_error_msg(GOT_ERR_BAD_PACKET,
563 "unexpected message from server");
569 fprintf(stderr, "%s: fetching...\n", getprogname());
571 if (my_capabilities != NULL &&
572 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
579 if (have_sidebands) {
580 err = got_pkt_readhdr(&datalen, fd, chattygot);
586 /* Read sideband channel ID (one byte). */
587 r = read(fd, buf, 1);
589 err = got_error_from_errno("read");
593 err = got_error_msg(GOT_ERR_BAD_PACKET,
597 if (datalen > sizeof(buf) - 5) {
598 err = got_error_msg(GOT_ERR_BAD_PACKET,
599 "bad packet length");
602 datalen--; /* sideband ID has been read */
603 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
604 /* Read packfile data. */
605 err = got_pkt_readn(&r, fd, buf, datalen);
609 err = got_error_msg(GOT_ERR_BAD_PACKET,
613 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
614 err = got_pkt_readn(&r, fd, buf, datalen);
618 err = got_error_msg(GOT_ERR_BAD_PACKET,
622 err = fetch_progress(ibuf, buf, r);
626 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
627 err = got_pkt_readn(&r, fd, buf, datalen);
631 err = got_error_msg(GOT_ERR_BAD_PACKET,
635 err = fetch_error(buf, r);
637 } else if (buf[0] == 'A') {
638 err = got_pkt_readn(&r, fd, buf, datalen);
642 err = got_error_msg(GOT_ERR_BAD_PACKET,
647 * Git server responds with ACK after 'done'
648 * even though multi_ack is disabled?!?
651 if (strncmp(buf, "CK ", 3) == 0)
652 continue; /* ignore */
653 err = got_error_msg(GOT_ERR_BAD_PACKET,
654 "unexpected message from server");
657 err = got_error_msg(GOT_ERR_BAD_PACKET,
658 "unknown side-band received from server");
662 /* No sideband channel. Every byte is packfile data. */
663 err = got_pkt_readn(&r, fd, buf, sizeof buf);
671 * An expected SHA1 checksum sits at the end of the pack file.
672 * Since we don't know the file size ahead of time we have to
673 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
674 * those bytes into our SHA1 checksum computation until we
675 * know for sure that additional pack file data bytes follow.
677 * We can assume r > 0 since otherwise the loop would exit.
679 if (r < SHA1_DIGEST_LENGTH) {
680 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
682 * If there's enough buffered + read data to
683 * fill up the buffer then shift a sufficient
684 * amount of bytes out at the front to make
685 * room, mixing those bytes into the checksum.
687 while (sha1_buf_len > 0 &&
688 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
689 SHA1Update(&sha1_ctx, sha1_buf, 1);
690 memmove(sha1_buf, sha1_buf + 1, 1);
694 /* Buffer potential checksum bytes. */
695 memcpy(sha1_buf + sha1_buf_len, buf, r);
699 * Mix in previously buffered bytes which
700 * are not part of the checksum after all.
702 SHA1Update(&sha1_ctx, sha1_buf, r);
704 /* Update potential checksum buffer. */
705 memmove(sha1_buf, sha1_buf + r,
707 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
710 /* Mix in any previously buffered bytes. */
711 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
713 /* Mix in bytes read minus potential checksum bytes. */
714 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
716 /* Buffer potential checksum bytes. */
717 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
719 sha1_buf_len = SHA1_DIGEST_LENGTH;
722 /* Write packfile data to temporary pack file. */
723 w = write(packfd, buf, r);
725 err = got_error_from_errno("write");
729 err = got_error(GOT_ERR_IO);
734 /* Don't send too many progress privsep messages. */
735 if (packsz > last_reported_packsz + 1024) {
736 err = send_fetch_download_progress(ibuf, packsz, &rl);
739 last_reported_packsz = packsz;
742 err = send_fetch_download_progress(ibuf, packsz, NULL);
746 SHA1Final(pack_sha1, &sha1_ctx);
747 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
748 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
749 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
750 "pack file checksum mismatch");
753 TAILQ_FOREACH(pe, &symrefs, entry) {
754 free((void *)pe->path);
757 got_pathlist_free(&symrefs);
762 free(server_capabilities);
768 main(int argc, char **argv)
770 const struct got_error *err = NULL;
771 int fetchfd, packfd = -1;
772 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
775 struct got_pathlist_head have_refs;
776 struct got_pathlist_head wanted_branches;
777 struct got_pathlist_head wanted_refs;
778 struct got_pathlist_entry *pe;
779 struct got_imsg_fetch_request fetch_req;
780 struct got_imsg_fetch_have_ref href;
781 struct got_imsg_fetch_wanted_branch wbranch;
782 struct got_imsg_fetch_wanted_ref wref;
790 TAILQ_INIT(&have_refs);
791 TAILQ_INIT(&wanted_branches);
792 TAILQ_INIT(&wanted_refs);
794 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
796 /* revoke access to most system calls */
797 if (pledge("stdio recvfd", NULL) == -1) {
798 err = got_error_from_errno("pledge");
799 got_privsep_send_error(&ibuf, err);
803 /* revoke fs access */
804 if (landlock_no_fs() == -1) {
805 err = got_error_from_errno("landlock_no_fs");
806 got_privsep_send_error(&ibuf, err);
809 if (cap_enter() == -1) {
810 err = got_error_from_errno("cap_enter");
811 got_privsep_send_error(&ibuf, err);
815 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
817 if (err->code == GOT_ERR_PRIVSEP_PIPE)
821 if (imsg.hdr.type == GOT_IMSG_STOP)
823 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
824 err = got_error(GOT_ERR_PRIVSEP_MSG);
827 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
828 if (datalen < sizeof(fetch_req)) {
829 err = got_error(GOT_ERR_PRIVSEP_LEN);
832 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
836 if (fetch_req.verbosity > 0)
837 chattygot += fetch_req.verbosity;
839 for (i = 0; i < fetch_req.n_have_refs; i++) {
840 struct got_object_id *id;
843 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
845 if (err->code == GOT_ERR_PRIVSEP_PIPE)
849 if (imsg.hdr.type == GOT_IMSG_STOP)
851 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
852 err = got_error(GOT_ERR_PRIVSEP_MSG);
855 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
856 if (datalen < sizeof(href)) {
857 err = got_error(GOT_ERR_PRIVSEP_LEN);
860 memcpy(&href, imsg.data, sizeof(href));
861 if (datalen - sizeof(href) < href.name_len) {
862 err = got_error(GOT_ERR_PRIVSEP_LEN);
865 refname = malloc(href.name_len + 1);
866 if (refname == NULL) {
867 err = got_error_from_errno("malloc");
870 memcpy(refname, imsg.data + sizeof(href), href.name_len);
871 refname[href.name_len] = '\0';
873 id = malloc(sizeof(*id));
876 err = got_error_from_errno("malloc");
879 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
880 err = got_pathlist_append(&have_refs, refname, id);
890 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
893 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
895 if (err->code == GOT_ERR_PRIVSEP_PIPE)
899 if (imsg.hdr.type == GOT_IMSG_STOP)
901 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
902 err = got_error(GOT_ERR_PRIVSEP_MSG);
905 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
906 if (datalen < sizeof(wbranch)) {
907 err = got_error(GOT_ERR_PRIVSEP_LEN);
910 memcpy(&wbranch, imsg.data, sizeof(wbranch));
911 if (datalen - sizeof(wbranch) < wbranch.name_len) {
912 err = got_error(GOT_ERR_PRIVSEP_LEN);
915 refname = malloc(wbranch.name_len + 1);
916 if (refname == NULL) {
917 err = got_error_from_errno("malloc");
920 memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
921 refname[wbranch.name_len] = '\0';
923 err = got_pathlist_append(&wanted_branches, refname, NULL);
932 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
935 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
937 if (err->code == GOT_ERR_PRIVSEP_PIPE)
941 if (imsg.hdr.type == GOT_IMSG_STOP)
943 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
944 err = got_error(GOT_ERR_PRIVSEP_MSG);
947 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
948 if (datalen < sizeof(wref)) {
949 err = got_error(GOT_ERR_PRIVSEP_LEN);
952 memcpy(&wref, imsg.data, sizeof(wref));
953 if (datalen - sizeof(wref) < wref.name_len) {
954 err = got_error(GOT_ERR_PRIVSEP_LEN);
957 refname = malloc(wref.name_len + 1);
958 if (refname == NULL) {
959 err = got_error_from_errno("malloc");
962 memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
963 refname[wref.name_len] = '\0';
965 err = got_pathlist_append(&wanted_refs, refname, NULL);
974 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
976 if (err->code == GOT_ERR_PRIVSEP_PIPE)
980 if (imsg.hdr.type == GOT_IMSG_STOP)
982 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
983 err = got_error(GOT_ERR_PRIVSEP_MSG);
986 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
987 err = got_error(GOT_ERR_PRIVSEP_LEN);
992 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
993 fetch_req.fetch_all_branches, &wanted_branches,
994 &wanted_refs, fetch_req.list_refs_only, &ibuf);
996 TAILQ_FOREACH(pe, &have_refs, entry) {
997 free((char *)pe->path);
1000 got_pathlist_free(&have_refs);
1001 TAILQ_FOREACH(pe, &wanted_branches, entry)
1002 free((char *)pe->path);
1003 got_pathlist_free(&wanted_branches);
1004 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1005 err = got_error_from_errno("close");
1006 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1007 err = got_error_from_errno("close");
1009 got_privsep_send_error(&ibuf, err);
1011 err = send_fetch_done(&ibuf, pack_sha1);
1013 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1014 got_privsep_send_error(&ibuf, err);