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((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->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;
379 if (strstr(refname, "^{}")) {
381 fprintf(stderr, "%s: ignoring %s\n",
382 getprogname(), refname);
387 if (strncmp(refname, "refs/heads/", 11) == 0) {
388 if (fetch_all_branches || list_refs_only) {
390 } else if (!TAILQ_EMPTY(wanted_branches)) {
391 TAILQ_FOREACH(pe, wanted_branches, entry) {
392 if (match_branch(refname, pe->path))
399 getprogname(), refname);
404 } else if (default_branch != NULL) {
405 if (!match_branch(refname, default_branch)) {
409 getprogname(), refname);
415 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
416 if (!TAILQ_EMPTY(wanted_refs)) {
417 TAILQ_FOREACH(pe, wanted_refs, entry) {
418 if (match_wanted_ref(refname, pe->path))
425 getprogname(), refname);
430 } else if (!list_refs_only) {
432 fprintf(stderr, "%s: ignoring %s\n",
433 getprogname(), refname);
439 if (refsz == nref + 1) {
441 have = reallocarray(have, refsz, sizeof(have[0]));
443 err = got_error_from_errno("reallocarray");
446 want = reallocarray(want, refsz, sizeof(want[0]));
448 err = got_error_from_errno("reallocarray");
452 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
453 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
456 match_remote_ref(have_refs, &have[nref], refname);
457 err = send_fetch_ref(ibuf, &want[nref], refname);
462 fprintf(stderr, "%s: %s will be fetched\n",
463 getprogname(), refname);
466 err = got_object_id_str(&theirs, &want[nref]);
469 err = got_object_id_str(&mine, &have[nref]);
474 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
475 getprogname(), theirs, getprogname(), mine);
485 /* Abort if we haven't found any branch to fetch. */
487 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
491 for (i = 0; i < nref; i++) {
492 if (got_object_id_cmp(&have[i], &want[i]) == 0)
494 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
495 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
496 sent_my_capabilites || my_capabilities == NULL ?
497 "" : my_capabilities);
498 if (n < 0 || (size_t)n >= sizeof(buf)) {
499 err = got_error(GOT_ERR_NO_SPACE);
502 err = got_pkt_writepkt(fd, buf, n, chattygot);
505 sent_my_capabilites = 1;
508 err = got_pkt_flushpkt(fd, chattygot);
515 TAILQ_FOREACH(pe, have_refs, entry) {
516 struct got_object_id *id = pe->data;
517 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
518 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
519 if (n < 0 || (size_t)n >= sizeof(buf)) {
520 err = got_error(GOT_ERR_NO_SPACE);
523 err = got_pkt_writepkt(fd, buf, n, chattygot);
529 while (nhave > 0 && !acked) {
530 struct got_object_id common_id;
532 /* The server should ACK the object IDs we need. */
533 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
536 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
537 err = fetch_error(&buf[4], n - 4);
540 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
541 /* Server has not located our objects yet. */
544 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
545 strncmp(buf, "ACK ", 4) != 0) {
546 err = got_error_msg(GOT_ERR_BAD_PACKET,
547 "unexpected message from server");
550 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
551 err = got_error_msg(GOT_ERR_BAD_PACKET,
552 "bad object ID in ACK packet from server");
558 n = strlcpy(buf, "done\n", sizeof(buf));
559 err = got_pkt_writepkt(fd, buf, n, chattygot);
564 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
567 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
568 err = got_error_msg(GOT_ERR_BAD_PACKET,
569 "unexpected message from server");
575 fprintf(stderr, "%s: fetching...\n", getprogname());
577 if (my_capabilities != NULL &&
578 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
585 if (have_sidebands) {
586 err = got_pkt_readhdr(&datalen, fd, chattygot);
592 /* Read sideband channel ID (one byte). */
593 r = read(fd, buf, 1);
595 err = got_error_from_errno("read");
599 err = got_error_msg(GOT_ERR_BAD_PACKET,
603 if (datalen > sizeof(buf) - 5) {
604 err = got_error_msg(GOT_ERR_BAD_PACKET,
605 "bad packet length");
608 datalen--; /* sideband ID has been read */
609 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
610 /* Read packfile data. */
611 err = got_pkt_readn(&r, fd, buf, datalen);
615 err = got_error_msg(GOT_ERR_BAD_PACKET,
619 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
620 err = got_pkt_readn(&r, fd, buf, datalen);
624 err = got_error_msg(GOT_ERR_BAD_PACKET,
628 err = fetch_progress(ibuf, buf, r);
632 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
633 err = got_pkt_readn(&r, fd, buf, datalen);
637 err = got_error_msg(GOT_ERR_BAD_PACKET,
641 err = fetch_error(buf, r);
643 } else if (buf[0] == 'A') {
644 err = got_pkt_readn(&r, fd, buf, datalen);
648 err = got_error_msg(GOT_ERR_BAD_PACKET,
653 * Git server responds with ACK after 'done'
654 * even though multi_ack is disabled?!?
657 if (strncmp(buf, "CK ", 3) == 0)
658 continue; /* ignore */
659 err = got_error_msg(GOT_ERR_BAD_PACKET,
660 "unexpected message from server");
663 err = got_error_msg(GOT_ERR_BAD_PACKET,
664 "unknown side-band received from server");
668 /* No sideband channel. Every byte is packfile data. */
669 err = got_pkt_readn(&r, fd, buf, sizeof buf);
677 * An expected SHA1 checksum sits at the end of the pack file.
678 * Since we don't know the file size ahead of time we have to
679 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
680 * those bytes into our SHA1 checksum computation until we
681 * know for sure that additional pack file data bytes follow.
683 * We can assume r > 0 since otherwise the loop would exit.
685 if (r < SHA1_DIGEST_LENGTH) {
686 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
688 * If there's enough buffered + read data to
689 * fill up the buffer then shift a sufficient
690 * amount of bytes out at the front to make
691 * room, mixing those bytes into the checksum.
693 if (sha1_buf_len > 0 &&
694 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
695 size_t nshift = MIN(sha1_buf_len + r -
696 SHA1_DIGEST_LENGTH, sha1_buf_len);
697 SHA1Update(&sha1_ctx, sha1_buf, nshift);
698 memmove(sha1_buf, sha1_buf + nshift,
699 sha1_buf_len - nshift);
700 sha1_buf_len -= nshift;
703 /* Buffer potential checksum bytes. */
704 memcpy(sha1_buf + sha1_buf_len, buf, r);
708 * Mix in previously buffered bytes which
709 * are not part of the checksum after all.
711 SHA1Update(&sha1_ctx, sha1_buf, r);
713 /* Update potential checksum buffer. */
714 memmove(sha1_buf, sha1_buf + r,
716 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
719 /* Mix in any previously buffered bytes. */
720 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
722 /* Mix in bytes read minus potential checksum bytes. */
723 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
725 /* Buffer potential checksum bytes. */
726 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
728 sha1_buf_len = SHA1_DIGEST_LENGTH;
731 /* Write packfile data to temporary pack file. */
732 w = write(packfd, buf, r);
734 err = got_error_from_errno("write");
738 err = got_error(GOT_ERR_IO);
743 /* Don't send too many progress privsep messages. */
744 if (packsz > last_reported_packsz + 1024) {
745 err = send_fetch_download_progress(ibuf, packsz, &rl);
748 last_reported_packsz = packsz;
751 err = send_fetch_download_progress(ibuf, packsz, NULL);
755 SHA1Final(pack_sha1, &sha1_ctx);
756 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
757 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
758 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
759 "pack file checksum mismatch");
762 TAILQ_FOREACH(pe, &symrefs, entry) {
763 free((void *)pe->path);
766 got_pathlist_free(&symrefs);
771 free(server_capabilities);
777 main(int argc, char **argv)
779 const struct got_error *err = NULL;
780 int fetchfd, packfd = -1;
781 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
784 struct got_pathlist_head have_refs;
785 struct got_pathlist_head wanted_branches;
786 struct got_pathlist_head wanted_refs;
787 struct got_pathlist_entry *pe;
788 struct got_imsg_fetch_request fetch_req;
789 struct got_imsg_fetch_have_ref href;
790 struct got_imsg_fetch_wanted_branch wbranch;
791 struct got_imsg_fetch_wanted_ref wref;
799 TAILQ_INIT(&have_refs);
800 TAILQ_INIT(&wanted_branches);
801 TAILQ_INIT(&wanted_refs);
803 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
805 /* revoke access to most system calls */
806 if (pledge("stdio recvfd", NULL) == -1) {
807 err = got_error_from_errno("pledge");
808 got_privsep_send_error(&ibuf, err);
812 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
814 if (err->code == GOT_ERR_PRIVSEP_PIPE)
818 if (imsg.hdr.type == GOT_IMSG_STOP)
820 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
821 err = got_error(GOT_ERR_PRIVSEP_MSG);
824 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
825 if (datalen < sizeof(fetch_req)) {
826 err = got_error(GOT_ERR_PRIVSEP_LEN);
829 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
833 if (fetch_req.verbosity > 0)
834 chattygot += fetch_req.verbosity;
836 for (i = 0; i < fetch_req.n_have_refs; i++) {
837 struct got_object_id *id;
840 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
842 if (err->code == GOT_ERR_PRIVSEP_PIPE)
846 if (imsg.hdr.type == GOT_IMSG_STOP)
848 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
849 err = got_error(GOT_ERR_PRIVSEP_MSG);
852 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
853 if (datalen < sizeof(href)) {
854 err = got_error(GOT_ERR_PRIVSEP_LEN);
857 memcpy(&href, imsg.data, sizeof(href));
858 if (datalen - sizeof(href) < href.name_len) {
859 err = got_error(GOT_ERR_PRIVSEP_LEN);
862 refname = malloc(href.name_len + 1);
863 if (refname == NULL) {
864 err = got_error_from_errno("malloc");
867 memcpy(refname, imsg.data + sizeof(href), href.name_len);
868 refname[href.name_len] = '\0';
870 id = malloc(sizeof(*id));
873 err = got_error_from_errno("malloc");
876 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
877 err = got_pathlist_append(&have_refs, refname, id);
887 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
890 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
892 if (err->code == GOT_ERR_PRIVSEP_PIPE)
896 if (imsg.hdr.type == GOT_IMSG_STOP)
898 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
899 err = got_error(GOT_ERR_PRIVSEP_MSG);
902 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
903 if (datalen < sizeof(wbranch)) {
904 err = got_error(GOT_ERR_PRIVSEP_LEN);
907 memcpy(&wbranch, imsg.data, sizeof(wbranch));
908 if (datalen - sizeof(wbranch) < wbranch.name_len) {
909 err = got_error(GOT_ERR_PRIVSEP_LEN);
912 refname = malloc(wbranch.name_len + 1);
913 if (refname == NULL) {
914 err = got_error_from_errno("malloc");
917 memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
918 refname[wbranch.name_len] = '\0';
920 err = got_pathlist_append(&wanted_branches, refname, NULL);
929 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
932 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
934 if (err->code == GOT_ERR_PRIVSEP_PIPE)
938 if (imsg.hdr.type == GOT_IMSG_STOP)
940 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
941 err = got_error(GOT_ERR_PRIVSEP_MSG);
944 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
945 if (datalen < sizeof(wref)) {
946 err = got_error(GOT_ERR_PRIVSEP_LEN);
949 memcpy(&wref, imsg.data, sizeof(wref));
950 if (datalen - sizeof(wref) < wref.name_len) {
951 err = got_error(GOT_ERR_PRIVSEP_LEN);
954 refname = malloc(wref.name_len + 1);
955 if (refname == NULL) {
956 err = got_error_from_errno("malloc");
959 memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
960 refname[wref.name_len] = '\0';
962 err = got_pathlist_append(&wanted_refs, refname, NULL);
971 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
973 if (err->code == GOT_ERR_PRIVSEP_PIPE)
977 if (imsg.hdr.type == GOT_IMSG_STOP)
979 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
980 err = got_error(GOT_ERR_PRIVSEP_MSG);
983 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
984 err = got_error(GOT_ERR_PRIVSEP_LEN);
989 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
990 fetch_req.fetch_all_branches, &wanted_branches,
991 &wanted_refs, fetch_req.list_refs_only, &ibuf);
993 TAILQ_FOREACH(pe, &have_refs, entry) {
994 free((char *)pe->path);
997 got_pathlist_free(&have_refs);
998 TAILQ_FOREACH(pe, &wanted_branches, entry)
999 free((char *)pe->path);
1000 got_pathlist_free(&wanted_branches);
1001 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1002 err = got_error_from_errno("close");
1003 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1004 err = got_error_from_errno("close");
1006 got_privsep_send_error(&ibuf, err);
1008 err = send_fetch_done(&ibuf, pack_sha1);
1010 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1011 got_privsep_send_error(&ibuf, err);