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"
55 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 struct got_object *indexed;
60 static struct got_object_id zhash = {.sha1={0}};
62 static const struct got_capability got_capabilities[] = {
63 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
64 { GOT_CAPA_OFS_DELTA, NULL },
65 { GOT_CAPA_SIDE_BAND_64K, NULL },
69 match_remote_ref(struct got_pathlist_head *have_refs,
70 struct got_object_id *my_id, char *refname)
72 struct got_pathlist_entry *pe;
74 /* XXX zero-hash signifies we don't have this ref;
75 * we should use a flag instead */
76 memset(my_id, 0, sizeof(*my_id));
78 TAILQ_FOREACH(pe, have_refs, entry) {
79 struct got_object_id *id = pe->data;
80 if (strcmp(pe->path, refname) == 0) {
81 memcpy(my_id, id, sizeof(*my_id));
88 match_branch(const char *branch, const char *wanted_branch)
90 if (strncmp(branch, "refs/heads/", 11) != 0)
93 if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
96 return (strcmp(branch + 11, wanted_branch) == 0);
100 match_wanted_ref(const char *refname, const char *wanted_ref)
102 if (strncmp(refname, "refs/", 5) != 0)
107 * Prevent fetching of references that won't make any
108 * sense outside of the remote repository's context.
110 if (strncmp(refname, "got/", 4) == 0)
112 if (strncmp(refname, "remotes/", 8) == 0)
115 if (strncmp(wanted_ref, "refs/", 5) == 0)
118 /* Allow prefix match. */
119 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
122 /* Allow exact match. */
123 return (strcmp(refname, wanted_ref) == 0);
126 static const struct got_error *
127 send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
129 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
130 return got_error(GOT_ERR_NO_SPACE);
135 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
137 return got_error_from_errno(
138 "imsg_compose FETCH_SERVER_PROGRESS");
140 return got_privsep_flush_imsg(ibuf);
143 static const struct got_error *
144 send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
146 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
147 &bytes, sizeof(bytes)) == -1)
148 return got_error_from_errno(
149 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
151 return got_privsep_flush_imsg(ibuf);
154 static const struct got_error *
155 send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
157 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
158 pack_sha1, SHA1_DIGEST_LENGTH) == -1)
159 return got_error_from_errno("imsg_compose FETCH");
160 return got_privsep_flush_imsg(ibuf);
163 static const struct got_error *
164 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
172 * Truncate messages which exceed the maximum imsg payload size.
173 * Server may send up to 64k.
175 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
176 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
178 /* Only allow printable ASCII. */
179 for (i = 0; i < len; i++) {
180 if (isprint((unsigned char)buf[i]) ||
181 isspace((unsigned char)buf[i]))
183 return got_error_msg(GOT_ERR_BAD_PACKET,
184 "non-printable progress message received from server");
187 return send_fetch_server_progress(ibuf, buf, len);
190 static const struct got_error *
191 fetch_error(const char *buf, size_t len)
193 static char msg[1024];
196 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
197 if (!isprint(buf[i]))
198 return got_error_msg(GOT_ERR_BAD_PACKET,
199 "non-printable error message received from server");
203 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
206 static const struct got_error *
207 send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
209 const struct got_error *err = NULL;
211 size_t len, nsymrefs = 0;
212 struct got_pathlist_entry *pe;
214 len = sizeof(struct got_imsg_fetch_symrefs);
215 TAILQ_FOREACH(pe, symrefs, entry) {
216 const char *target = pe->data;
217 len += sizeof(struct got_imsg_fetch_symref) +
218 pe->path_len + strlen(target);
222 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
223 return got_error(GOT_ERR_NO_SPACE);
225 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
227 return got_error_from_errno("imsg_create FETCH_SYMREFS");
229 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
230 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
231 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
236 TAILQ_FOREACH(pe, symrefs, entry) {
237 const char *name = pe->path;
238 size_t name_len = pe->path_len;
239 const char *target = pe->data;
240 size_t target_len = strlen(target);
242 /* Keep in sync with struct got_imsg_fetch_symref definition! */
243 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
244 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
248 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
249 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
253 if (imsg_add(wbuf, name, name_len) == -1) {
254 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
258 if (imsg_add(wbuf, target, target_len) == -1) {
259 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
266 imsg_close(ibuf, wbuf);
267 return got_privsep_flush_imsg(ibuf);
270 static const struct got_error *
271 send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
274 const struct got_error *err = NULL;
276 size_t len, reflen = strlen(refname);
278 len = sizeof(struct got_imsg_fetch_ref) + reflen;
279 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
280 return got_error(GOT_ERR_NO_SPACE);
282 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
284 return got_error_from_errno("imsg_create FETCH_REF");
286 /* Keep in sync with struct got_imsg_fetch_ref definition! */
287 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
288 err = got_error_from_errno("imsg_add FETCH_REF");
292 if (imsg_add(wbuf, refname, reflen) == -1) {
293 err = got_error_from_errno("imsg_add FETCH_REF");
299 imsg_close(ibuf, wbuf);
300 return got_privsep_flush_imsg(ibuf);
303 static const struct got_error *
304 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
305 struct got_pathlist_head *have_refs, int fetch_all_branches,
306 struct got_pathlist_head *wanted_branches,
307 struct got_pathlist_head *wanted_refs, int list_refs_only,
308 struct imsgbuf *ibuf)
310 const struct got_error *err = NULL;
311 char buf[GOT_PKT_MAX];
312 char hashstr[SHA1_DIGEST_STRING_LENGTH];
313 struct got_object_id *have, *want;
314 int is_firstpkt = 1, nref = 0, refsz = 16;
315 int i, n, nwant = 0, nhave = 0, acked = 0;
316 off_t packsz = 0, last_reported_packsz = 0;
317 char *id_str = NULL, *refname = NULL;
318 char *server_capabilities = NULL, *my_capabilities = NULL;
319 const char *default_branch = NULL;
320 struct got_pathlist_head symrefs;
321 struct got_pathlist_entry *pe;
322 int sent_my_capabilites = 0, have_sidebands = 0;
323 int found_branch = 0;
325 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
326 size_t sha1_buf_len = 0;
329 TAILQ_INIT(&symrefs);
332 have = malloc(refsz * sizeof(have[0]));
334 return got_error_from_errno("malloc");
335 want = malloc(refsz * sizeof(want[0]));
337 err = got_error_from_errno("malloc");
341 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
346 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
347 err = fetch_error(&buf[4], n - 4);
350 err = got_gitproto_parse_refline(&id_str, &refname,
351 &server_capabilities, buf, n);
355 if (chattygot && server_capabilities[0] != '\0')
356 fprintf(stderr, "%s: server capabilities: %s\n",
357 getprogname(), server_capabilities);
358 err = got_gitproto_match_capabilities(&my_capabilities,
359 &symrefs, server_capabilities,
360 got_capabilities, nitems(got_capabilities));
364 fprintf(stderr, "%s: my capabilities:%s\n",
365 getprogname(), my_capabilities != NULL ?
366 my_capabilities : "");
367 err = send_fetch_symrefs(ibuf, &symrefs);
371 if (!fetch_all_branches) {
372 TAILQ_FOREACH(pe, &symrefs, entry) {
373 const char *name = pe->path;
374 const char *symref_target = pe->data;
375 if (strcmp(name, GOT_REF_HEAD) != 0)
377 default_branch = symref_target;
383 if (strstr(refname, "^{}")) {
385 fprintf(stderr, "%s: ignoring %s\n",
386 getprogname(), refname);
391 if (strncmp(refname, "refs/heads/", 11) == 0) {
392 if (fetch_all_branches || list_refs_only) {
394 } else if (!TAILQ_EMPTY(wanted_branches)) {
395 TAILQ_FOREACH(pe, wanted_branches, entry) {
396 if (match_branch(refname, pe->path))
403 getprogname(), refname);
408 } else if (default_branch != NULL) {
409 if (!match_branch(refname, default_branch)) {
413 getprogname(), refname);
419 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
420 if (!TAILQ_EMPTY(wanted_refs)) {
421 TAILQ_FOREACH(pe, wanted_refs, entry) {
422 if (match_wanted_ref(refname, pe->path))
429 getprogname(), refname);
434 } else if (!list_refs_only) {
436 fprintf(stderr, "%s: ignoring %s\n",
437 getprogname(), refname);
443 if (refsz == nref + 1) {
445 have = reallocarray(have, refsz, sizeof(have[0]));
447 err = got_error_from_errno("reallocarray");
450 want = reallocarray(want, refsz, sizeof(want[0]));
452 err = got_error_from_errno("reallocarray");
456 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
457 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
460 match_remote_ref(have_refs, &have[nref], refname);
461 err = send_fetch_ref(ibuf, &want[nref], refname);
466 fprintf(stderr, "%s: %s will be fetched\n",
467 getprogname(), refname);
470 err = got_object_id_str(&theirs, &want[nref]);
473 err = got_object_id_str(&mine, &have[nref]);
478 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
479 getprogname(), theirs, getprogname(), mine);
489 /* Abort if we haven't found any branch to fetch. */
491 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
495 for (i = 0; i < nref; i++) {
496 if (got_object_id_cmp(&have[i], &want[i]) == 0)
498 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
499 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
500 sent_my_capabilites || my_capabilities == NULL ?
501 "" : my_capabilities);
502 if (n >= sizeof(buf)) {
503 err = got_error(GOT_ERR_NO_SPACE);
506 err = got_pkt_writepkt(fd, buf, n, chattygot);
509 sent_my_capabilites = 1;
512 err = got_pkt_flushpkt(fd, chattygot);
519 for (i = 0; i < nref; i++) {
520 if (got_object_id_cmp(&have[i], &zhash) == 0)
522 got_sha1_digest_to_str(have[i].sha1, hashstr, sizeof(hashstr));
523 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
524 if (n >= sizeof(buf)) {
525 err = got_error(GOT_ERR_NO_SPACE);
528 err = got_pkt_writepkt(fd, buf, n, chattygot);
534 while (nhave > 0 && !acked) {
535 struct got_object_id common_id;
537 /* The server should ACK the object IDs we need. */
538 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
541 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
542 err = fetch_error(&buf[4], n - 4);
545 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
546 /* Server has not located our objects yet. */
549 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
550 strncmp(buf, "ACK ", 4) != 0) {
551 err = got_error_msg(GOT_ERR_BAD_PACKET,
552 "unexpected message from server");
555 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
556 err = got_error_msg(GOT_ERR_BAD_PACKET,
557 "bad object ID in ACK packet from server");
563 n = snprintf(buf, sizeof(buf), "done\n");
564 err = got_pkt_writepkt(fd, buf, n, chattygot);
569 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
572 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
573 err = got_error_msg(GOT_ERR_BAD_PACKET,
574 "unexpected message from server");
580 fprintf(stderr, "%s: fetching...\n", getprogname());
582 if (my_capabilities != NULL &&
583 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
590 if (have_sidebands) {
591 err = got_pkt_readhdr(&datalen, fd, chattygot);
597 /* Read sideband channel ID (one byte). */
598 r = read(fd, buf, 1);
600 err = got_error_from_errno("read");
604 err = got_error_msg(GOT_ERR_BAD_PACKET,
608 if (datalen > sizeof(buf) - 5) {
609 err = got_error_msg(GOT_ERR_BAD_PACKET,
610 "bad packet length");
613 datalen--; /* sideband ID has been read */
614 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
615 /* Read packfile data. */
616 err = got_pkt_readn(&r, fd, buf, datalen);
620 err = got_error_msg(GOT_ERR_BAD_PACKET,
624 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
625 err = got_pkt_readn(&r, fd, buf, datalen);
629 err = got_error_msg(GOT_ERR_BAD_PACKET,
633 err = fetch_progress(ibuf, buf, r);
637 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
638 err = got_pkt_readn(&r, fd, buf, datalen);
642 err = got_error_msg(GOT_ERR_BAD_PACKET,
646 err = fetch_error(buf, r);
648 } else if (buf[0] == 'A') {
649 err = got_pkt_readn(&r, fd, buf, datalen);
653 err = got_error_msg(GOT_ERR_BAD_PACKET,
658 * Git server responds with ACK after 'done'
659 * even though multi_ack is disabled?!?
662 if (strncmp(buf, "CK ", 3) == 0)
663 continue; /* ignore */
664 err = got_error_msg(GOT_ERR_BAD_PACKET,
665 "unexpected message from server");
668 err = got_error_msg(GOT_ERR_BAD_PACKET,
669 "unknown side-band received from server");
673 /* No sideband channel. Every byte is packfile data. */
674 err = got_pkt_readn(&r, fd, buf, sizeof buf);
682 * An expected SHA1 checksum sits at the end of the pack file.
683 * Since we don't know the file size ahead of time we have to
684 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
685 * those bytes into our SHA1 checksum computation until we
686 * know for sure that additional pack file data bytes follow.
688 * We can assume r > 0 since otherwise the loop would exit.
690 if (r < SHA1_DIGEST_LENGTH) {
691 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
693 * If there's enough buffered + read data to
694 * fill up the buffer then shift a sufficient
695 * amount of bytes out at the front to make
696 * room, mixing those bytes into the checksum.
698 while (sha1_buf_len > 0 &&
699 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
700 SHA1Update(&sha1_ctx, sha1_buf, 1);
701 memmove(sha1_buf, sha1_buf + 1, 1);
705 /* Buffer potential checksum bytes. */
706 memcpy(sha1_buf + sha1_buf_len, buf, r);
710 * Mix in previously buffered bytes which
711 * are not part of the checksum after all.
713 SHA1Update(&sha1_ctx, sha1_buf, r);
715 /* Update potential checksum buffer. */
716 memmove(sha1_buf, sha1_buf + r,
718 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
721 /* Mix in any previously buffered bytes. */
722 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
724 /* Mix in bytes read minus potential checksum bytes. */
725 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
727 /* Buffer potential checksum bytes. */
728 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
730 sha1_buf_len = SHA1_DIGEST_LENGTH;
733 /* Write packfile data to temporary pack file. */
734 w = write(packfd, buf, r);
736 err = got_error_from_errno("write");
740 err = got_error(GOT_ERR_IO);
745 /* Don't send too many progress privsep messages. */
746 if (packsz > last_reported_packsz + 1024) {
747 err = send_fetch_download_progress(ibuf, packsz);
750 last_reported_packsz = packsz;
753 err = send_fetch_download_progress(ibuf, packsz);
757 SHA1Final(pack_sha1, &sha1_ctx);
758 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
759 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
760 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
761 "pack file checksum mismatch");
764 TAILQ_FOREACH(pe, &symrefs, entry) {
765 free((void *)pe->path);
768 got_pathlist_free(&symrefs);
773 free(server_capabilities);
779 main(int argc, char **argv)
781 const struct got_error *err = NULL;
782 int fetchfd, packfd = -1, i;
783 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
786 struct got_pathlist_head have_refs;
787 struct got_pathlist_head wanted_branches;
788 struct got_pathlist_head wanted_refs;
789 struct got_pathlist_entry *pe;
790 struct got_imsg_fetch_request fetch_req;
791 struct got_imsg_fetch_have_ref href;
792 struct got_imsg_fetch_wanted_branch wbranch;
793 struct got_imsg_fetch_wanted_ref wref;
801 TAILQ_INIT(&have_refs);
802 TAILQ_INIT(&wanted_branches);
803 TAILQ_INIT(&wanted_refs);
805 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
807 /* revoke access to most system calls */
808 if (pledge("stdio recvfd", NULL) == -1) {
809 err = got_error_from_errno("pledge");
810 got_privsep_send_error(&ibuf, err);
814 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
816 if (err->code == GOT_ERR_PRIVSEP_PIPE)
820 if (imsg.hdr.type == GOT_IMSG_STOP)
822 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
823 err = got_error(GOT_ERR_PRIVSEP_MSG);
826 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
827 if (datalen < sizeof(fetch_req)) {
828 err = got_error(GOT_ERR_PRIVSEP_LEN);
831 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
835 if (fetch_req.verbosity > 0)
836 chattygot += fetch_req.verbosity;
838 for (i = 0; i < fetch_req.n_have_refs; i++) {
839 struct got_object_id *id;
842 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
844 if (err->code == GOT_ERR_PRIVSEP_PIPE)
848 if (imsg.hdr.type == GOT_IMSG_STOP)
850 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
851 err = got_error(GOT_ERR_PRIVSEP_MSG);
854 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
855 if (datalen < sizeof(href)) {
856 err = got_error(GOT_ERR_PRIVSEP_LEN);
859 memcpy(&href, imsg.data, sizeof(href));
860 if (datalen - sizeof(href) < href.name_len) {
861 err = got_error(GOT_ERR_PRIVSEP_LEN);
864 refname = malloc(href.name_len + 1);
865 if (refname == NULL) {
866 err = got_error_from_errno("malloc");
869 memcpy(refname, imsg.data + sizeof(href), href.name_len);
870 refname[href.name_len] = '\0';
872 id = malloc(sizeof(*id));
875 err = got_error_from_errno("malloc");
878 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
879 err = got_pathlist_append(&have_refs, refname, id);
889 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
892 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
894 if (err->code == GOT_ERR_PRIVSEP_PIPE)
898 if (imsg.hdr.type == GOT_IMSG_STOP)
900 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
901 err = got_error(GOT_ERR_PRIVSEP_MSG);
904 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
905 if (datalen < sizeof(wbranch)) {
906 err = got_error(GOT_ERR_PRIVSEP_LEN);
909 memcpy(&wbranch, imsg.data, sizeof(wbranch));
910 if (datalen - sizeof(wbranch) < wbranch.name_len) {
911 err = got_error(GOT_ERR_PRIVSEP_LEN);
914 refname = malloc(wbranch.name_len + 1);
915 if (refname == NULL) {
916 err = got_error_from_errno("malloc");
919 memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
920 refname[wbranch.name_len] = '\0';
922 err = got_pathlist_append(&wanted_branches, refname, NULL);
931 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
934 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
936 if (err->code == GOT_ERR_PRIVSEP_PIPE)
940 if (imsg.hdr.type == GOT_IMSG_STOP)
942 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
943 err = got_error(GOT_ERR_PRIVSEP_MSG);
946 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
947 if (datalen < sizeof(wref)) {
948 err = got_error(GOT_ERR_PRIVSEP_LEN);
951 memcpy(&wref, imsg.data, sizeof(wref));
952 if (datalen - sizeof(wref) < wref.name_len) {
953 err = got_error(GOT_ERR_PRIVSEP_LEN);
956 refname = malloc(wref.name_len + 1);
957 if (refname == NULL) {
958 err = got_error_from_errno("malloc");
961 memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
962 refname[wref.name_len] = '\0';
964 err = got_pathlist_append(&wanted_refs, refname, NULL);
973 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
975 if (err->code == GOT_ERR_PRIVSEP_PIPE)
979 if (imsg.hdr.type == GOT_IMSG_STOP)
981 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
982 err = got_error(GOT_ERR_PRIVSEP_MSG);
985 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
986 err = got_error(GOT_ERR_PRIVSEP_LEN);
991 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
992 fetch_req.fetch_all_branches, &wanted_branches,
993 &wanted_refs, fetch_req.list_refs_only, &ibuf);
995 TAILQ_FOREACH(pe, &have_refs, entry) {
996 free((char *)pe->path);
999 got_pathlist_free(&have_refs);
1000 TAILQ_FOREACH(pe, &wanted_branches, entry)
1001 free((char *)pe->path);
1002 got_pathlist_free(&wanted_branches);
1003 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1004 err = got_error_from_errno("close");
1005 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1006 err = got_error_from_errno("close");
1008 got_privsep_send_error(&ibuf, err);
1010 err = send_fetch_done(&ibuf, pack_sha1);
1012 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1013 got_privsep_send_error(&ibuf, err);