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;
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)
145 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
146 &bytes, sizeof(bytes)) == -1)
147 return got_error_from_errno(
148 "imsg_compose FETCH_DOWNLOAD_PROGRESS");
150 return got_privsep_flush_imsg(ibuf);
153 static const struct got_error *
154 send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
156 if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
157 pack_sha1, SHA1_DIGEST_LENGTH) == -1)
158 return got_error_from_errno("imsg_compose FETCH");
159 return got_privsep_flush_imsg(ibuf);
162 static const struct got_error *
163 fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
171 * Truncate messages which exceed the maximum imsg payload size.
172 * Server may send up to 64k.
174 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
175 len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
177 /* Only allow printable ASCII. */
178 for (i = 0; i < len; i++) {
179 if (isprint((unsigned char)buf[i]) ||
180 isspace((unsigned char)buf[i]))
182 return got_error_msg(GOT_ERR_BAD_PACKET,
183 "non-printable progress message received from server");
186 return send_fetch_server_progress(ibuf, buf, len);
189 static const struct got_error *
190 fetch_error(const char *buf, size_t len)
192 static char msg[1024];
195 for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
196 if (!isprint(buf[i]))
197 return got_error_msg(GOT_ERR_BAD_PACKET,
198 "non-printable error message received from server");
202 return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
205 static const struct got_error *
206 send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
208 const struct got_error *err = NULL;
210 size_t len, nsymrefs = 0;
211 struct got_pathlist_entry *pe;
213 len = sizeof(struct got_imsg_fetch_symrefs);
214 TAILQ_FOREACH(pe, symrefs, entry) {
215 const char *target = pe->data;
216 len += sizeof(struct got_imsg_fetch_symref) +
217 pe->path_len + strlen(target);
221 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
222 return got_error(GOT_ERR_NO_SPACE);
224 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
226 return got_error_from_errno("imsg_create FETCH_SYMREFS");
228 /* Keep in sync with struct got_imsg_fetch_symrefs definition! */
229 if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
230 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
235 TAILQ_FOREACH(pe, symrefs, entry) {
236 const char *name = pe->path;
237 size_t name_len = pe->path_len;
238 const char *target = pe->data;
239 size_t target_len = strlen(target);
241 /* Keep in sync with struct got_imsg_fetch_symref definition! */
242 if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
243 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
247 if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
248 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
252 if (imsg_add(wbuf, name, name_len) == -1) {
253 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
257 if (imsg_add(wbuf, target, target_len) == -1) {
258 err = got_error_from_errno("imsg_add FETCH_SYMREFS");
265 imsg_close(ibuf, wbuf);
266 return got_privsep_flush_imsg(ibuf);
269 static const struct got_error *
270 send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
273 const struct got_error *err = NULL;
275 size_t len, reflen = strlen(refname);
277 len = sizeof(struct got_imsg_fetch_ref) + reflen;
278 if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
279 return got_error(GOT_ERR_NO_SPACE);
281 wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
283 return got_error_from_errno("imsg_create FETCH_REF");
285 /* Keep in sync with struct got_imsg_fetch_ref definition! */
286 if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
287 err = got_error_from_errno("imsg_add FETCH_REF");
291 if (imsg_add(wbuf, refname, reflen) == -1) {
292 err = got_error_from_errno("imsg_add FETCH_REF");
298 imsg_close(ibuf, wbuf);
299 return got_privsep_flush_imsg(ibuf);
302 static const struct got_error *
303 fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
304 struct got_pathlist_head *have_refs, int fetch_all_branches,
305 struct got_pathlist_head *wanted_branches,
306 struct got_pathlist_head *wanted_refs, int list_refs_only,
307 struct imsgbuf *ibuf)
309 const struct got_error *err = NULL;
310 char buf[GOT_PKT_MAX];
311 char hashstr[SHA1_DIGEST_STRING_LENGTH];
312 struct got_object_id *have, *want;
313 int is_firstpkt = 1, nref = 0, refsz = 16;
314 int i, n, nwant = 0, nhave = 0, acked = 0;
315 off_t packsz = 0, last_reported_packsz = 0;
316 char *id_str = NULL, *refname = NULL;
317 char *server_capabilities = NULL, *my_capabilities = NULL;
318 const char *default_branch = NULL;
319 struct got_pathlist_head symrefs;
320 struct got_pathlist_entry *pe;
321 int sent_my_capabilites = 0, have_sidebands = 0;
322 int found_branch = 0;
324 uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
325 size_t sha1_buf_len = 0;
328 TAILQ_INIT(&symrefs);
331 have = malloc(refsz * sizeof(have[0]));
333 return got_error_from_errno("malloc");
334 want = malloc(refsz * sizeof(want[0]));
336 err = got_error_from_errno("malloc");
340 err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
345 if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
346 err = fetch_error(&buf[4], n - 4);
351 err = got_gitproto_parse_refline(&id_str, &refname,
352 &server_capabilities, buf, n);
356 if (chattygot && server_capabilities[0] != '\0')
357 fprintf(stderr, "%s: server capabilities: %s\n",
358 getprogname(), server_capabilities);
359 err = got_gitproto_match_capabilities(&my_capabilities,
360 &symrefs, server_capabilities,
361 got_capabilities, nitems(got_capabilities));
365 fprintf(stderr, "%s: my capabilities:%s\n",
366 getprogname(), my_capabilities != NULL ?
367 my_capabilities : "");
368 err = send_fetch_symrefs(ibuf, &symrefs);
372 if (!fetch_all_branches) {
373 TAILQ_FOREACH(pe, &symrefs, entry) {
374 const char *name = pe->path;
375 const char *symref_target = pe->data;
376 if (strcmp(name, GOT_REF_HEAD) != 0)
378 default_branch = symref_target;
384 if (strstr(refname, "^{}")) {
386 fprintf(stderr, "%s: ignoring %s\n",
387 getprogname(), refname);
392 if (strncmp(refname, "refs/heads/", 11) == 0) {
393 if (fetch_all_branches || list_refs_only) {
395 } else if (!TAILQ_EMPTY(wanted_branches)) {
396 TAILQ_FOREACH(pe, wanted_branches, entry) {
397 if (match_branch(refname, pe->path))
404 getprogname(), refname);
409 } else if (default_branch != NULL) {
410 if (!match_branch(refname, default_branch)) {
414 getprogname(), refname);
420 } else if (strncmp(refname, "refs/tags/", 10) != 0) {
421 if (!TAILQ_EMPTY(wanted_refs)) {
422 TAILQ_FOREACH(pe, wanted_refs, entry) {
423 if (match_wanted_ref(refname, pe->path))
430 getprogname(), refname);
435 } else if (!list_refs_only) {
437 fprintf(stderr, "%s: ignoring %s\n",
438 getprogname(), refname);
444 if (refsz == nref + 1) {
446 have = reallocarray(have, refsz, sizeof(have[0]));
448 err = got_error_from_errno("reallocarray");
451 want = reallocarray(want, refsz, sizeof(want[0]));
453 err = got_error_from_errno("reallocarray");
457 if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
458 err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
461 match_remote_ref(have_refs, &have[nref], refname);
462 err = send_fetch_ref(ibuf, &want[nref], refname);
467 fprintf(stderr, "%s: %s will be fetched\n",
468 getprogname(), refname);
471 err = got_object_id_str(&theirs, &want[nref]);
474 err = got_object_id_str(&mine, &have[nref]);
479 fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
480 getprogname(), theirs, getprogname(), mine);
490 /* Abort if we haven't found any branch to fetch. */
492 err = got_error(GOT_ERR_FETCH_NO_BRANCH);
496 for (i = 0; i < nref; i++) {
497 if (got_object_id_cmp(&have[i], &want[i]) == 0)
499 got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
500 n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
501 sent_my_capabilites || my_capabilities == NULL ?
502 "" : my_capabilities);
503 if (n >= sizeof(buf)) {
504 err = got_error(GOT_ERR_NO_SPACE);
507 err = got_pkt_writepkt(fd, buf, n, chattygot);
510 sent_my_capabilites = 1;
513 err = got_pkt_flushpkt(fd, chattygot);
520 TAILQ_FOREACH(pe, have_refs, entry) {
521 struct got_object_id *id = pe->data;
522 got_sha1_digest_to_str(id->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;
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);