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"
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 struct got_object *indexed;
60 static const struct got_capability got_capabilities[] = {
61 { GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
62 { GOT_CAPA_OFS_DELTA, NULL },
63 { GOT_CAPA_SIDE_BAND_64K, NULL },
67 match_remote_ref(struct got_pathlist_head *have_refs,
68 struct got_object_id *my_id, char *refname)
70 struct got_pathlist_entry *pe;
72 /* XXX zero-hash signifies we don't have this ref;
73 * we should use a flag instead */
74 memset(my_id, 0, sizeof(*my_id));
76 TAILQ_FOREACH(pe, have_refs, entry) {
77 struct got_object_id *id = pe->data;
78 if (strcmp(pe->path, refname) == 0) {
79 memcpy(my_id, id, sizeof(*my_id));
86 match_branch(const char *branch, const char *wanted_branch)
88 if (strncmp(branch, "refs/heads/", 11) != 0)
91 if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
94 return (strcmp(branch + 11, wanted_branch) == 0);
98 match_wanted_ref(const char *refname, const char *wanted_ref)
100 if (strncmp(refname, "refs/", 5) != 0)
105 * Prevent fetching of references that won't make any
106 * sense outside of the remote repository's context.
108 if (strncmp(refname, "got/", 4) == 0)
110 if (strncmp(refname, "remotes/", 8) == 0)
113 if (strncmp(wanted_ref, "refs/", 5) == 0)
116 /* Allow prefix match. */
117 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
120 /* Allow exact match. */
121 return (strcmp(refname, wanted_ref) == 0);
124 static const struct got_error *
125 send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
127 if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
128 return got_error(GOT_ERR_NO_SPACE);
133 if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
135 return got_error_from_errno(
136 "imsg_compose FETCH_SERVER_PROGRESS");
138 return got_privsep_flush_imsg(ibuf);
141 static const struct got_error *
142 send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes)
144 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
145 &bytes, sizeof(bytes)) == -1)
146 return got_error_from_errno(
147 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
149 return got_privsep_flush_imsg(ibuf);
152 static const struct got_error *
153 send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
155 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
156 pack_sha1, SHA1_DIGEST_LENGTH) == -1)
157 return got_error_from_errno("imsg_compose FETCH");
158 return got_privsep_flush_imsg(ibuf);
161 static const struct got_error *
162 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
170 * Truncate messages which exceed the maximum imsg payload size.
171 * Server may send up to 64k.
173 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
174 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
176 /* Only allow printable ASCII. */
177 for (i = 0; i < len; i++) {
178 if (isprint((unsigned char)buf[i]) ||
179 isspace((unsigned char)buf[i]))
181 return got_error_msg(GOT_ERR_BAD_PACKET,
182 "non-printable progress message received from server");
185 return send_fetch_server_progress(ibuf, buf, len);
188 static const struct got_error *
189 fetch_error(const char *buf, size_t len)
191 static char msg[1024];
194 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
195 if (!isprint(buf[i]))
196 return got_error_msg(GOT_ERR_BAD_PACKET,
197 "non-printable error message received from server");
201 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
204 static const struct got_error *
205 send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
207 const struct got_error *err = NULL;
209 size_t len, nsymrefs = 0;
210 struct got_pathlist_entry *pe;
212 len = sizeof(struct got_imsg_fetch_symrefs);
213 TAILQ_FOREACH(pe, symrefs, entry) {
214 const char *target = pe->data;
215 len += sizeof(struct got_imsg_fetch_symref) +
216 pe->path_len + strlen(target);
220 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
221 return got_error(GOT_ERR_NO_SPACE);
223 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
225 return got_error_from_errno("imsg_create FETCH_SYMREFS");
227 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
228 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
229 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
234 TAILQ_FOREACH(pe, symrefs, entry) {
235 const char *name = pe->path;
236 size_t name_len = pe->path_len;
237 const char *target = pe->data;
238 size_t target_len = strlen(target);
240 /* Keep in sync with struct got_imsg_fetch_symref definition! */
241 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
242 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
246 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
247 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
251 if (imsg_add(wbuf, name, name_len) == -1) {
252 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
256 if (imsg_add(wbuf, target, target_len) == -1) {
257 err = 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,
272 const struct got_error *err = NULL;
274 size_t len, reflen = strlen(refname);
276 len = sizeof(struct got_imsg_fetch_ref) + reflen;
277 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
278 return got_error(GOT_ERR_NO_SPACE);
280 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
282 return got_error_from_errno("imsg_create FETCH_REF");
284 /* Keep in sync with struct got_imsg_fetch_ref definition! */
285 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
286 err = got_error_from_errno("imsg_add FETCH_REF");
290 if (imsg_add(wbuf, refname, reflen) == -1) {
291 err = got_error_from_errno("imsg_add FETCH_REF");
297 imsg_close(ibuf, wbuf);
298 return got_privsep_flush_imsg(ibuf);
301 static const struct got_error *
302 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
303 struct got_pathlist_head *have_refs, int fetch_all_branches,
304 struct got_pathlist_head *wanted_branches,
305 struct got_pathlist_head *wanted_refs, int list_refs_only,
306 struct imsgbuf *ibuf)
308 const struct got_error *err = NULL;
309 char buf[GOT_PKT_MAX];
310 char hashstr[SHA1_DIGEST_STRING_LENGTH];
311 struct got_object_id *have, *want;
312 int is_firstpkt = 1, nref = 0, refsz = 16;
313 int i, n, nwant = 0, nhave = 0, acked = 0;
314 off_t packsz = 0, last_reported_packsz = 0;
315 char *id_str = NULL, *refname = NULL;
316 char *server_capabilities = NULL, *my_capabilities = NULL;
317 const char *default_branch = NULL;
318 struct got_pathlist_head symrefs;
319 struct got_pathlist_entry *pe;
320 int sent_my_capabilites = 0, have_sidebands = 0;
321 int found_branch = 0;
323 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
324 size_t sha1_buf_len = 0;
327 TAILQ_INIT(&symrefs);
330 have = malloc(refsz * sizeof(have[0]));
332 return got_error_from_errno("malloc");
333 want = malloc(refsz * sizeof(want[0]));
335 err = got_error_from_errno("malloc");
339 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
344 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
345 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 TAILQ_FOREACH(pe, have_refs, entry) {
520 struct got_object_id *id = pe->data;
521 got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
522 n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
523 if (n >= sizeof(buf)) {
524 err = got_error(GOT_ERR_NO_SPACE);
527 err = got_pkt_writepkt(fd, buf, n, chattygot);
533 while (nhave > 0 && !acked) {
534 struct got_object_id common_id;
536 /* The server should ACK the object IDs we need. */
537 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
540 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
541 err = fetch_error(&buf[4], n - 4);
544 if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
545 /* Server has not located our objects yet. */
548 if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
549 strncmp(buf, "ACK ", 4) != 0) {
550 err = got_error_msg(GOT_ERR_BAD_PACKET,
551 "unexpected message from server");
554 if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
555 err = got_error_msg(GOT_ERR_BAD_PACKET,
556 "bad object ID in ACK packet from server");
562 n = snprintf(buf, sizeof(buf), "done\n");
563 err = got_pkt_writepkt(fd, buf, n, chattygot);
568 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
571 if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
572 err = got_error_msg(GOT_ERR_BAD_PACKET,
573 "unexpected message from server");
579 fprintf(stderr, "%s: fetching...\n", getprogname());
581 if (my_capabilities != NULL &&
582 strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
589 if (have_sidebands) {
590 err = got_pkt_readhdr(&datalen, fd, chattygot);
596 /* Read sideband channel ID (one byte). */
597 r = read(fd, buf, 1);
599 err = got_error_from_errno("read");
603 err = got_error_msg(GOT_ERR_BAD_PACKET,
607 if (datalen > sizeof(buf) - 5) {
608 err = got_error_msg(GOT_ERR_BAD_PACKET,
609 "bad packet length");
612 datalen--; /* sideband ID has been read */
613 if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
614 /* Read packfile data. */
615 err = got_pkt_readn(&r, fd, buf, datalen);
619 err = got_error_msg(GOT_ERR_BAD_PACKET,
623 } else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
624 err = got_pkt_readn(&r, fd, buf, datalen);
628 err = got_error_msg(GOT_ERR_BAD_PACKET,
632 err = fetch_progress(ibuf, buf, r);
636 } else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
637 err = got_pkt_readn(&r, fd, buf, datalen);
641 err = got_error_msg(GOT_ERR_BAD_PACKET,
645 err = fetch_error(buf, r);
647 } else if (buf[0] == 'A') {
648 err = got_pkt_readn(&r, fd, buf, datalen);
652 err = got_error_msg(GOT_ERR_BAD_PACKET,
657 * Git server responds with ACK after 'done'
658 * even though multi_ack is disabled?!?
661 if (strncmp(buf, "CK ", 3) == 0)
662 continue; /* ignore */
663 err = got_error_msg(GOT_ERR_BAD_PACKET,
664 "unexpected message from server");
667 err = got_error_msg(GOT_ERR_BAD_PACKET,
668 "unknown side-band received from server");
672 /* No sideband channel. Every byte is packfile data. */
673 err = got_pkt_readn(&r, fd, buf, sizeof buf);
681 * An expected SHA1 checksum sits at the end of the pack file.
682 * Since we don't know the file size ahead of time we have to
683 * keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
684 * those bytes into our SHA1 checksum computation until we
685 * know for sure that additional pack file data bytes follow.
687 * We can assume r > 0 since otherwise the loop would exit.
689 if (r < SHA1_DIGEST_LENGTH) {
690 if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
692 * If there's enough buffered + read data to
693 * fill up the buffer then shift a sufficient
694 * amount of bytes out at the front to make
695 * room, mixing those bytes into the checksum.
697 while (sha1_buf_len > 0 &&
698 sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
699 SHA1Update(&sha1_ctx, sha1_buf, 1);
700 memmove(sha1_buf, sha1_buf + 1, 1);
704 /* Buffer potential checksum bytes. */
705 memcpy(sha1_buf + sha1_buf_len, buf, r);
709 * Mix in previously buffered bytes which
710 * are not part of the checksum after all.
712 SHA1Update(&sha1_ctx, sha1_buf, r);
714 /* Update potential checksum buffer. */
715 memmove(sha1_buf, sha1_buf + r,
717 memcpy(sha1_buf + sha1_buf_len - r, buf, r);
720 /* Mix in any previously buffered bytes. */
721 SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
723 /* Mix in bytes read minus potential checksum bytes. */
724 SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
726 /* Buffer potential checksum bytes. */
727 memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
729 sha1_buf_len = SHA1_DIGEST_LENGTH;
732 /* Write packfile data to temporary pack file. */
733 w = write(packfd, buf, r);
735 err = got_error_from_errno("write");
739 err = got_error(GOT_ERR_IO);
744 /* Don't send too many progress privsep messages. */
745 if (packsz > last_reported_packsz + 1024) {
746 err = send_fetch_download_progress(ibuf, packsz);
749 last_reported_packsz = packsz;
752 err = send_fetch_download_progress(ibuf, packsz);
756 SHA1Final(pack_sha1, &sha1_ctx);
757 if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
758 memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
759 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
760 "pack file checksum mismatch");
763 TAILQ_FOREACH(pe, &symrefs, entry) {
764 free((void *)pe->path);
767 got_pathlist_free(&symrefs);
772 free(server_capabilities);
778 main(int argc, char **argv)
780 const struct got_error *err = NULL;
781 int fetchfd, packfd = -1;
782 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
785 struct got_pathlist_head have_refs;
786 struct got_pathlist_head wanted_branches;
787 struct got_pathlist_head wanted_refs;
788 struct got_pathlist_entry *pe;
789 struct got_imsg_fetch_request fetch_req;
790 struct got_imsg_fetch_have_ref href;
791 struct got_imsg_fetch_wanted_branch wbranch;
792 struct got_imsg_fetch_wanted_ref wref;
800 TAILQ_INIT(&have_refs);
801 TAILQ_INIT(&wanted_branches);
802 TAILQ_INIT(&wanted_refs);
804 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
806 /* revoke access to most system calls */
807 if (pledge("stdio recvfd", NULL) == -1) {
808 err = got_error_from_errno("pledge");
809 got_privsep_send_error(&ibuf, err);
813 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
815 if (err->code == GOT_ERR_PRIVSEP_PIPE)
819 if (imsg.hdr.type == GOT_IMSG_STOP)
821 if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
822 err = got_error(GOT_ERR_PRIVSEP_MSG);
825 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
826 if (datalen < sizeof(fetch_req)) {
827 err = got_error(GOT_ERR_PRIVSEP_LEN);
830 memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
834 if (fetch_req.verbosity > 0)
835 chattygot += fetch_req.verbosity;
837 for (i = 0; i < fetch_req.n_have_refs; i++) {
838 struct got_object_id *id;
841 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
843 if (err->code == GOT_ERR_PRIVSEP_PIPE)
847 if (imsg.hdr.type == GOT_IMSG_STOP)
849 if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
850 err = got_error(GOT_ERR_PRIVSEP_MSG);
853 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
854 if (datalen < sizeof(href)) {
855 err = got_error(GOT_ERR_PRIVSEP_LEN);
858 memcpy(&href, imsg.data, sizeof(href));
859 if (datalen - sizeof(href) < href.name_len) {
860 err = got_error(GOT_ERR_PRIVSEP_LEN);
863 refname = malloc(href.name_len + 1);
864 if (refname == NULL) {
865 err = got_error_from_errno("malloc");
868 memcpy(refname, imsg.data + sizeof(href), href.name_len);
869 refname[href.name_len] = '\0';
871 id = malloc(sizeof(*id));
874 err = got_error_from_errno("malloc");
877 memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
878 err = got_pathlist_append(&have_refs, refname, id);
888 for (i = 0; i < fetch_req.n_wanted_branches; i++) {
891 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
893 if (err->code == GOT_ERR_PRIVSEP_PIPE)
897 if (imsg.hdr.type == GOT_IMSG_STOP)
899 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
900 err = got_error(GOT_ERR_PRIVSEP_MSG);
903 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
904 if (datalen < sizeof(wbranch)) {
905 err = got_error(GOT_ERR_PRIVSEP_LEN);
908 memcpy(&wbranch, imsg.data, sizeof(wbranch));
909 if (datalen - sizeof(wbranch) < wbranch.name_len) {
910 err = got_error(GOT_ERR_PRIVSEP_LEN);
913 refname = malloc(wbranch.name_len + 1);
914 if (refname == NULL) {
915 err = got_error_from_errno("malloc");
918 memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
919 refname[wbranch.name_len] = '\0';
921 err = got_pathlist_append(&wanted_branches, refname, NULL);
930 for (i = 0; i < fetch_req.n_wanted_refs; i++) {
933 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
935 if (err->code == GOT_ERR_PRIVSEP_PIPE)
939 if (imsg.hdr.type == GOT_IMSG_STOP)
941 if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
942 err = got_error(GOT_ERR_PRIVSEP_MSG);
945 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
946 if (datalen < sizeof(wref)) {
947 err = got_error(GOT_ERR_PRIVSEP_LEN);
950 memcpy(&wref, imsg.data, sizeof(wref));
951 if (datalen - sizeof(wref) < wref.name_len) {
952 err = got_error(GOT_ERR_PRIVSEP_LEN);
955 refname = malloc(wref.name_len + 1);
956 if (refname == NULL) {
957 err = got_error_from_errno("malloc");
960 memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
961 refname[wref.name_len] = '\0';
963 err = got_pathlist_append(&wanted_refs, refname, NULL);
972 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
974 if (err->code == GOT_ERR_PRIVSEP_PIPE)
978 if (imsg.hdr.type == GOT_IMSG_STOP)
980 if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
981 err = got_error(GOT_ERR_PRIVSEP_MSG);
984 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
985 err = got_error(GOT_ERR_PRIVSEP_LEN);
990 err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
991 fetch_req.fetch_all_branches, &wanted_branches,
992 &wanted_refs, fetch_req.list_refs_only, &ibuf);
994 TAILQ_FOREACH(pe, &have_refs, entry) {
995 free((char *)pe->path);
998 got_pathlist_free(&have_refs);
999 TAILQ_FOREACH(pe, &wanted_branches, entry)
1000 free((char *)pe->path);
1001 got_pathlist_free(&wanted_branches);
1002 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1003 err = got_error_from_errno("close");
1004 if (packfd != -1 && close(packfd) == -1 && err == NULL)
1005 err = got_error_from_errno("close");
1007 got_privsep_send_error(&ibuf, err);
1009 err = send_fetch_done(&ibuf, pack_sha1);
1011 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1012 got_privsep_send_error(&ibuf, err);