Hash :
dcb64fea
Author :
Date :
2022-02-23T12:24:25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
/*
* Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <stdint.h>
#include <errno.h>
#include <imsg.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sha1.h>
#include <fcntl.h>
#include <unistd.h>
#include <zlib.h>
#include <err.h>
#include "got_error.h"
#include "got_object.h"
#include "got_path.h"
#include "got_version.h"
#include "got_fetch.h"
#include "got_reference.h"
#include "got_lib_sha1.h"
#include "got_lib_delta.h"
#include "got_lib_object.h"
#include "got_lib_object_parse.h"
#include "got_lib_privsep.h"
#include "got_lib_pack.h"
#include "got_lib_pkt.h"
#include "got_lib_gitproto.h"
#include "got_lib_ratelimit.h"
#ifndef nitems
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
#endif
struct got_object *indexed;
static int chattygot;
static const struct got_capability got_capabilities[] = {
{ GOT_CAPA_AGENT, "got/" GOT_VERSION_STR },
{ GOT_CAPA_OFS_DELTA, NULL },
{ GOT_CAPA_SIDE_BAND_64K, NULL },
};
static void
match_remote_ref(struct got_pathlist_head *have_refs,
struct got_object_id *my_id, char *refname)
{
struct got_pathlist_entry *pe;
/* XXX zero-hash signifies we don't have this ref;
* we should use a flag instead */
memset(my_id, 0, sizeof(*my_id));
TAILQ_FOREACH(pe, have_refs, entry) {
struct got_object_id *id = pe->data;
if (strcmp(pe->path, refname) == 0) {
memcpy(my_id, id, sizeof(*my_id));
break;
}
}
}
static int
match_branch(const char *branch, const char *wanted_branch)
{
if (strncmp(branch, "refs/heads/", 11) != 0)
return 0;
if (strncmp(wanted_branch, "refs/heads/", 11) == 0)
wanted_branch += 11;
return (strcmp(branch + 11, wanted_branch) == 0);
}
static int
match_wanted_ref(const char *refname, const char *wanted_ref)
{
if (strncmp(refname, "refs/", 5) != 0)
return 0;
refname += 5;
/*
* Prevent fetching of references that won't make any
* sense outside of the remote repository's context.
*/
if (strncmp(refname, "got/", 4) == 0)
return 0;
if (strncmp(refname, "remotes/", 8) == 0)
return 0;
if (strncmp(wanted_ref, "refs/", 5) == 0)
wanted_ref += 5;
/* Allow prefix match. */
if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
return 1;
/* Allow exact match. */
return (strcmp(refname, wanted_ref) == 0);
}
static const struct got_error *
send_fetch_server_progress(struct imsgbuf *ibuf, const char *msg, size_t msglen)
{
if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
return got_error(GOT_ERR_NO_SPACE);
if (msglen == 0)
return NULL;
if (imsg_compose(ibuf, GOT_IMSG_FETCH_SERVER_PROGRESS, 0, 0, -1,
msg, msglen) == -1)
return got_error_from_errno(
"imsg_compose FETCH_SERVER_PROGRESS");
return got_privsep_flush_imsg(ibuf);
}
static const struct got_error *
send_fetch_download_progress(struct imsgbuf *ibuf, off_t bytes,
struct got_ratelimit *rl)
{
const struct got_error *err;
int elapsed = 0;
if (rl) {
err = got_ratelimit_check(&elapsed, rl);
if (err || !elapsed)
return err;
}
if (imsg_compose(ibuf, GOT_IMSG_FETCH_DOWNLOAD_PROGRESS, 0, 0, -1,
&bytes, sizeof(bytes)) == -1)
return got_error_from_errno(
"imsg_compose FETCH_DOWNLOAD_PROGRESS");
return got_privsep_flush_imsg(ibuf);
}
static const struct got_error *
send_fetch_done(struct imsgbuf *ibuf, uint8_t *pack_sha1)
{
if (imsg_compose(ibuf, GOT_IMSG_FETCH_DONE, 0, 0, -1,
pack_sha1, SHA1_DIGEST_LENGTH) == -1)
return got_error_from_errno("imsg_compose FETCH");
return got_privsep_flush_imsg(ibuf);
}
static const struct got_error *
fetch_progress(struct imsgbuf *ibuf, const char *buf, size_t len)
{
size_t i;
if (len == 0)
return NULL;
/*
* Truncate messages which exceed the maximum imsg payload size.
* Server may send up to 64k.
*/
if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
len = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
/* Only allow printable ASCII. */
for (i = 0; i < len; i++) {
if (isprint((unsigned char)buf[i]) ||
isspace((unsigned char)buf[i]))
continue;
return got_error_msg(GOT_ERR_BAD_PACKET,
"non-printable progress message received from server");
}
return send_fetch_server_progress(ibuf, buf, len);
}
static const struct got_error *
fetch_error(const char *buf, size_t len)
{
static char msg[1024];
size_t i;
for (i = 0; i < len && i < sizeof(msg) - 1; i++) {
if (!isprint(buf[i]))
return got_error_msg(GOT_ERR_BAD_PACKET,
"non-printable error message received from server");
msg[i] = buf[i];
}
msg[i] = '\0';
return got_error_msg(GOT_ERR_FETCH_FAILED, msg);
}
static const struct got_error *
send_fetch_symrefs(struct imsgbuf *ibuf, struct got_pathlist_head *symrefs)
{
const struct got_error *err = NULL;
struct ibuf *wbuf;
size_t len, nsymrefs = 0;
struct got_pathlist_entry *pe;
len = sizeof(struct got_imsg_fetch_symrefs);
TAILQ_FOREACH(pe, symrefs, entry) {
const char *target = pe->data;
len += sizeof(struct got_imsg_fetch_symref) +
pe->path_len + strlen(target);
nsymrefs++;
}
if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
return got_error(GOT_ERR_NO_SPACE);
wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_SYMREFS, 0, 0, len);
if (wbuf == NULL)
return got_error_from_errno("imsg_create FETCH_SYMREFS");
/* Keep in sync with struct got_imsg_fetch_symrefs definition! */
if (imsg_add(wbuf, &nsymrefs, sizeof(nsymrefs)) == -1) {
err = got_error_from_errno("imsg_add FETCH_SYMREFS");
ibuf_free(wbuf);
return err;
}
TAILQ_FOREACH(pe, symrefs, entry) {
const char *name = pe->path;
size_t name_len = pe->path_len;
const char *target = pe->data;
size_t target_len = strlen(target);
/* Keep in sync with struct got_imsg_fetch_symref definition! */
if (imsg_add(wbuf, &name_len, sizeof(name_len)) == -1) {
err = got_error_from_errno("imsg_add FETCH_SYMREFS");
ibuf_free(wbuf);
return err;
}
if (imsg_add(wbuf, &target_len, sizeof(target_len)) == -1) {
err = got_error_from_errno("imsg_add FETCH_SYMREFS");
ibuf_free(wbuf);
return err;
}
if (imsg_add(wbuf, name, name_len) == -1) {
err = got_error_from_errno("imsg_add FETCH_SYMREFS");
ibuf_free(wbuf);
return err;
}
if (imsg_add(wbuf, target, target_len) == -1) {
err = got_error_from_errno("imsg_add FETCH_SYMREFS");
ibuf_free(wbuf);
return err;
}
}
wbuf->fd = -1;
imsg_close(ibuf, wbuf);
return got_privsep_flush_imsg(ibuf);
}
static const struct got_error *
send_fetch_ref(struct imsgbuf *ibuf, struct got_object_id *refid,
const char *refname)
{
const struct got_error *err = NULL;
struct ibuf *wbuf;
size_t len, reflen = strlen(refname);
len = sizeof(struct got_imsg_fetch_ref) + reflen;
if (len >= MAX_IMSGSIZE - IMSG_HEADER_SIZE)
return got_error(GOT_ERR_NO_SPACE);
wbuf = imsg_create(ibuf, GOT_IMSG_FETCH_REF, 0, 0, len);
if (wbuf == NULL)
return got_error_from_errno("imsg_create FETCH_REF");
/* Keep in sync with struct got_imsg_fetch_ref definition! */
if (imsg_add(wbuf, refid->sha1, SHA1_DIGEST_LENGTH) == -1) {
err = got_error_from_errno("imsg_add FETCH_REF");
ibuf_free(wbuf);
return err;
}
if (imsg_add(wbuf, refname, reflen) == -1) {
err = got_error_from_errno("imsg_add FETCH_REF");
ibuf_free(wbuf);
return err;
}
wbuf->fd = -1;
imsg_close(ibuf, wbuf);
return got_privsep_flush_imsg(ibuf);
}
static const struct got_error *
fetch_pack(int fd, int packfd, uint8_t *pack_sha1,
struct got_pathlist_head *have_refs, int fetch_all_branches,
struct got_pathlist_head *wanted_branches,
struct got_pathlist_head *wanted_refs, int list_refs_only,
struct imsgbuf *ibuf)
{
const struct got_error *err = NULL;
char buf[GOT_PKT_MAX];
char hashstr[SHA1_DIGEST_STRING_LENGTH];
struct got_object_id *have, *want;
int is_firstpkt = 1, nref = 0, refsz = 16;
int i, n, nwant = 0, nhave = 0, acked = 0;
off_t packsz = 0, last_reported_packsz = 0;
char *id_str = NULL, *refname = NULL;
char *server_capabilities = NULL, *my_capabilities = NULL;
const char *default_branch = NULL;
struct got_pathlist_head symrefs;
struct got_pathlist_entry *pe;
int sent_my_capabilites = 0, have_sidebands = 0;
int found_branch = 0;
SHA1_CTX sha1_ctx;
uint8_t sha1_buf[SHA1_DIGEST_LENGTH];
size_t sha1_buf_len = 0;
ssize_t w;
struct got_ratelimit rl;
TAILQ_INIT(&symrefs);
SHA1Init(&sha1_ctx);
got_ratelimit_init(&rl, 0, 500);
have = malloc(refsz * sizeof(have[0]));
if (have == NULL)
return got_error_from_errno("malloc");
want = malloc(refsz * sizeof(want[0]));
if (want == NULL) {
err = got_error_from_errno("malloc");
goto done;
}
while (1) {
err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
if (err)
goto done;
if (n == 0)
break;
if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
err = fetch_error(&buf[4], n - 4);
goto done;
}
free(id_str);
free(refname);
err = got_gitproto_parse_refline(&id_str, &refname,
&server_capabilities, buf, n);
if (err)
goto done;
if (is_firstpkt) {
if (chattygot && server_capabilities[0] != '\0')
fprintf(stderr, "%s: server capabilities: %s\n",
getprogname(), server_capabilities);
err = got_gitproto_match_capabilities(&my_capabilities,
&symrefs, server_capabilities,
got_capabilities, nitems(got_capabilities));
if (err)
goto done;
if (chattygot)
fprintf(stderr, "%s: my capabilities:%s\n",
getprogname(), my_capabilities != NULL ?
my_capabilities : "");
err = send_fetch_symrefs(ibuf, &symrefs);
if (err)
goto done;
is_firstpkt = 0;
if (!fetch_all_branches) {
TAILQ_FOREACH(pe, &symrefs, entry) {
const char *name = pe->path;
const char *symref_target = pe->data;
if (strcmp(name, GOT_REF_HEAD) != 0)
continue;
default_branch = symref_target;
break;
}
}
continue;
}
if (strstr(refname, "^{}")) {
if (chattygot) {
fprintf(stderr, "%s: ignoring %s\n",
getprogname(), refname);
}
continue;
}
if (strncmp(refname, "refs/heads/", 11) == 0) {
if (fetch_all_branches || list_refs_only) {
found_branch = 1;
} else if (!TAILQ_EMPTY(wanted_branches)) {
TAILQ_FOREACH(pe, wanted_branches, entry) {
if (match_branch(refname, pe->path))
break;
}
if (pe == NULL) {
if (chattygot) {
fprintf(stderr,
"%s: ignoring %s\n",
getprogname(), refname);
}
continue;
}
found_branch = 1;
} else if (default_branch != NULL) {
if (!match_branch(refname, default_branch)) {
if (chattygot) {
fprintf(stderr,
"%s: ignoring %s\n",
getprogname(), refname);
}
continue;
}
found_branch = 1;
}
} else if (strncmp(refname, "refs/tags/", 10) != 0) {
if (!TAILQ_EMPTY(wanted_refs)) {
TAILQ_FOREACH(pe, wanted_refs, entry) {
if (match_wanted_ref(refname, pe->path))
break;
}
if (pe == NULL) {
if (chattygot) {
fprintf(stderr,
"%s: ignoring %s\n",
getprogname(), refname);
}
continue;
}
found_branch = 1;
} else if (!list_refs_only) {
if (chattygot) {
fprintf(stderr, "%s: ignoring %s\n",
getprogname(), refname);
}
continue;
}
}
if (refsz == nref + 1) {
refsz *= 2;
have = reallocarray(have, refsz, sizeof(have[0]));
if (have == NULL) {
err = got_error_from_errno("reallocarray");
goto done;
}
want = reallocarray(want, refsz, sizeof(want[0]));
if (want == NULL) {
err = got_error_from_errno("reallocarray");
goto done;
}
}
if (!got_parse_sha1_digest(want[nref].sha1, id_str)) {
err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
goto done;
}
match_remote_ref(have_refs, &have[nref], refname);
err = send_fetch_ref(ibuf, &want[nref], refname);
if (err)
goto done;
if (chattygot)
fprintf(stderr, "%s: %s will be fetched\n",
getprogname(), refname);
if (chattygot > 1) {
char *theirs, *mine;
err = got_object_id_str(&theirs, &want[nref]);
if (err)
goto done;
err = got_object_id_str(&mine, &have[nref]);
if (err) {
free(theirs);
goto done;
}
fprintf(stderr, "%s: remote: %s\n%s: local: %s\n",
getprogname(), theirs, getprogname(), mine);
free(theirs);
free(mine);
}
nref++;
}
if (list_refs_only)
goto done;
/* Abort if we haven't found any branch to fetch. */
if (!found_branch) {
err = got_error(GOT_ERR_FETCH_NO_BRANCH);
goto done;
}
for (i = 0; i < nref; i++) {
if (got_object_id_cmp(&have[i], &want[i]) == 0)
continue;
got_sha1_digest_to_str(want[i].sha1, hashstr, sizeof(hashstr));
n = snprintf(buf, sizeof(buf), "want %s%s\n", hashstr,
sent_my_capabilites || my_capabilities == NULL ?
"" : my_capabilities);
if (n >= sizeof(buf)) {
err = got_error(GOT_ERR_NO_SPACE);
goto done;
}
err = got_pkt_writepkt(fd, buf, n, chattygot);
if (err)
goto done;
sent_my_capabilites = 1;
nwant++;
}
err = got_pkt_flushpkt(fd, chattygot);
if (err)
goto done;
if (nwant == 0)
goto done;
TAILQ_FOREACH(pe, have_refs, entry) {
struct got_object_id *id = pe->data;
got_sha1_digest_to_str(id->sha1, hashstr, sizeof(hashstr));
n = snprintf(buf, sizeof(buf), "have %s\n", hashstr);
if (n >= sizeof(buf)) {
err = got_error(GOT_ERR_NO_SPACE);
goto done;
}
err = got_pkt_writepkt(fd, buf, n, chattygot);
if (err)
goto done;
nhave++;
}
while (nhave > 0 && !acked) {
struct got_object_id common_id;
/* The server should ACK the object IDs we need. */
err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
if (err)
goto done;
if (n >= 4 && strncmp(buf, "ERR ", 4) == 0) {
err = fetch_error(&buf[4], n - 4);
goto done;
}
if (n >= 4 && strncmp(buf, "NAK\n", 4) == 0) {
/* Server has not located our objects yet. */
continue;
}
if (n < 4 + SHA1_DIGEST_STRING_LENGTH ||
strncmp(buf, "ACK ", 4) != 0) {
err = got_error_msg(GOT_ERR_BAD_PACKET,
"unexpected message from server");
goto done;
}
if (!got_parse_sha1_digest(common_id.sha1, buf + 4)) {
err = got_error_msg(GOT_ERR_BAD_PACKET,
"bad object ID in ACK packet from server");
goto done;
}
acked++;
}
n = snprintf(buf, sizeof(buf), "done\n");
err = got_pkt_writepkt(fd, buf, n, chattygot);
if (err)
goto done;
if (nhave == 0) {
err = got_pkt_readpkt(&n, fd, buf, sizeof(buf), chattygot);
if (err)
goto done;
if (n != 4 || strncmp(buf, "NAK\n", n) != 0) {
err = got_error_msg(GOT_ERR_BAD_PACKET,
"unexpected message from server");
goto done;
}
}
if (chattygot)
fprintf(stderr, "%s: fetching...\n", getprogname());
if (my_capabilities != NULL &&
strstr(my_capabilities, GOT_CAPA_SIDE_BAND_64K) != NULL)
have_sidebands = 1;
while (1) {
ssize_t r = 0;
int datalen = -1;
if (have_sidebands) {
err = got_pkt_readhdr(&datalen, fd, chattygot);
if (err)
goto done;
if (datalen <= 0)
break;
/* Read sideband channel ID (one byte). */
r = read(fd, buf, 1);
if (r == -1) {
err = got_error_from_errno("read");
goto done;
}
if (r != 1) {
err = got_error_msg(GOT_ERR_BAD_PACKET,
"short packet");
goto done;
}
if (datalen > sizeof(buf) - 5) {
err = got_error_msg(GOT_ERR_BAD_PACKET,
"bad packet length");
goto done;
}
datalen--; /* sideband ID has been read */
if (buf[0] == GOT_SIDEBAND_PACKFILE_DATA) {
/* Read packfile data. */
err = got_pkt_readn(&r, fd, buf, datalen);
if (err)
goto done;
if (r != datalen) {
err = got_error_msg(GOT_ERR_BAD_PACKET,
"packet too short");
goto done;
}
} else if (buf[0] == GOT_SIDEBAND_PROGRESS_INFO) {
err = got_pkt_readn(&r, fd, buf, datalen);
if (err)
goto done;
if (r != datalen) {
err = got_error_msg(GOT_ERR_BAD_PACKET,
"packet too short");
goto done;
}
err = fetch_progress(ibuf, buf, r);
if (err)
goto done;
continue;
} else if (buf[0] == GOT_SIDEBAND_ERROR_INFO) {
err = got_pkt_readn(&r, fd, buf, datalen);
if (err)
goto done;
if (r != datalen) {
err = got_error_msg(GOT_ERR_BAD_PACKET,
"packet too short");
goto done;
}
err = fetch_error(buf, r);
goto done;
} else if (buf[0] == 'A') {
err = got_pkt_readn(&r, fd, buf, datalen);
if (err)
goto done;
if (r != datalen) {
err = got_error_msg(GOT_ERR_BAD_PACKET,
"packet too short");
goto done;
}
/*
* Git server responds with ACK after 'done'
* even though multi_ack is disabled?!?
*/
buf[r] = '\0';
if (strncmp(buf, "CK ", 3) == 0)
continue; /* ignore */
err = got_error_msg(GOT_ERR_BAD_PACKET,
"unexpected message from server");
goto done;
} else {
err = got_error_msg(GOT_ERR_BAD_PACKET,
"unknown side-band received from server");
goto done;
}
} else {
/* No sideband channel. Every byte is packfile data. */
err = got_pkt_readn(&r, fd, buf, sizeof buf);
if (err)
goto done;
if (r <= 0)
break;
}
/*
* An expected SHA1 checksum sits at the end of the pack file.
* Since we don't know the file size ahead of time we have to
* keep SHA1_DIGEST_LENGTH bytes buffered and avoid mixing
* those bytes into our SHA1 checksum computation until we
* know for sure that additional pack file data bytes follow.
*
* We can assume r > 0 since otherwise the loop would exit.
*/
if (r < SHA1_DIGEST_LENGTH) {
if (sha1_buf_len < SHA1_DIGEST_LENGTH) {
/*
* If there's enough buffered + read data to
* fill up the buffer then shift a sufficient
* amount of bytes out at the front to make
* room, mixing those bytes into the checksum.
*/
while (sha1_buf_len > 0 &&
sha1_buf_len + r > SHA1_DIGEST_LENGTH) {
SHA1Update(&sha1_ctx, sha1_buf, 1);
memmove(sha1_buf, sha1_buf + 1, 1);
sha1_buf_len--;
}
/* Buffer potential checksum bytes. */
memcpy(sha1_buf + sha1_buf_len, buf, r);
sha1_buf_len += r;
} else {
/*
* Mix in previously buffered bytes which
* are not part of the checksum after all.
*/
SHA1Update(&sha1_ctx, sha1_buf, r);
/* Update potential checksum buffer. */
memmove(sha1_buf, sha1_buf + r,
sha1_buf_len - r);
memcpy(sha1_buf + sha1_buf_len - r, buf, r);
}
} else {
/* Mix in any previously buffered bytes. */
SHA1Update(&sha1_ctx, sha1_buf, sha1_buf_len);
/* Mix in bytes read minus potential checksum bytes. */
SHA1Update(&sha1_ctx, buf, r - SHA1_DIGEST_LENGTH);
/* Buffer potential checksum bytes. */
memcpy(sha1_buf, buf + r - SHA1_DIGEST_LENGTH,
SHA1_DIGEST_LENGTH);
sha1_buf_len = SHA1_DIGEST_LENGTH;
}
/* Write packfile data to temporary pack file. */
w = write(packfd, buf, r);
if (w == -1) {
err = got_error_from_errno("write");
goto done;
}
if (w != r) {
err = got_error(GOT_ERR_IO);
goto done;
}
packsz += w;
/* Don't send too many progress privsep messages. */
if (packsz > last_reported_packsz + 1024) {
err = send_fetch_download_progress(ibuf, packsz, &rl);
if (err)
goto done;
last_reported_packsz = packsz;
}
}
err = send_fetch_download_progress(ibuf, packsz, NULL);
if (err)
goto done;
SHA1Final(pack_sha1, &sha1_ctx);
if (sha1_buf_len != SHA1_DIGEST_LENGTH ||
memcmp(pack_sha1, sha1_buf, sha1_buf_len) != 0) {
err = got_error_msg(GOT_ERR_BAD_PACKFILE,
"pack file checksum mismatch");
}
done:
TAILQ_FOREACH(pe, &symrefs, entry) {
free((void *)pe->path);
free(pe->data);
}
got_pathlist_free(&symrefs);
free(have);
free(want);
free(id_str);
free(refname);
free(server_capabilities);
return err;
}
int
main(int argc, char **argv)
{
const struct got_error *err = NULL;
int fetchfd, packfd = -1;
uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
struct imsgbuf ibuf;
struct imsg imsg;
struct got_pathlist_head have_refs;
struct got_pathlist_head wanted_branches;
struct got_pathlist_head wanted_refs;
struct got_pathlist_entry *pe;
struct got_imsg_fetch_request fetch_req;
struct got_imsg_fetch_have_ref href;
struct got_imsg_fetch_wanted_branch wbranch;
struct got_imsg_fetch_wanted_ref wref;
size_t datalen, i;
#if 0
static int attached;
while (!attached)
sleep (1);
#endif
TAILQ_INIT(&have_refs);
TAILQ_INIT(&wanted_branches);
TAILQ_INIT(&wanted_refs);
imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
#ifndef PROFILE
/* revoke access to most system calls */
if (pledge("stdio recvfd", NULL) == -1) {
err = got_error_from_errno("pledge");
got_privsep_send_error(&ibuf, err);
return 1;
}
#endif
err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
if (err) {
if (err->code == GOT_ERR_PRIVSEP_PIPE)
err = NULL;
goto done;
}
if (imsg.hdr.type == GOT_IMSG_STOP)
goto done;
if (imsg.hdr.type != GOT_IMSG_FETCH_REQUEST) {
err = got_error(GOT_ERR_PRIVSEP_MSG);
goto done;
}
datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
if (datalen < sizeof(fetch_req)) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
goto done;
}
memcpy(&fetch_req, imsg.data, sizeof(fetch_req));
fetchfd = imsg.fd;
imsg_free(&imsg);
if (fetch_req.verbosity > 0)
chattygot += fetch_req.verbosity;
for (i = 0; i < fetch_req.n_have_refs; i++) {
struct got_object_id *id;
char *refname;
err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
if (err) {
if (err->code == GOT_ERR_PRIVSEP_PIPE)
err = NULL;
goto done;
}
if (imsg.hdr.type == GOT_IMSG_STOP)
goto done;
if (imsg.hdr.type != GOT_IMSG_FETCH_HAVE_REF) {
err = got_error(GOT_ERR_PRIVSEP_MSG);
goto done;
}
datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
if (datalen < sizeof(href)) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
goto done;
}
memcpy(&href, imsg.data, sizeof(href));
if (datalen - sizeof(href) < href.name_len) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
goto done;
}
refname = malloc(href.name_len + 1);
if (refname == NULL) {
err = got_error_from_errno("malloc");
goto done;
}
memcpy(refname, imsg.data + sizeof(href), href.name_len);
refname[href.name_len] = '\0';
id = malloc(sizeof(*id));
if (id == NULL) {
free(refname);
err = got_error_from_errno("malloc");
goto done;
}
memcpy(id->sha1, href.id, SHA1_DIGEST_LENGTH);
err = got_pathlist_append(&have_refs, refname, id);
if (err) {
free(refname);
free(id);
goto done;
}
imsg_free(&imsg);
}
for (i = 0; i < fetch_req.n_wanted_branches; i++) {
char *refname;
err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
if (err) {
if (err->code == GOT_ERR_PRIVSEP_PIPE)
err = NULL;
goto done;
}
if (imsg.hdr.type == GOT_IMSG_STOP)
goto done;
if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_BRANCH) {
err = got_error(GOT_ERR_PRIVSEP_MSG);
goto done;
}
datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
if (datalen < sizeof(wbranch)) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
goto done;
}
memcpy(&wbranch, imsg.data, sizeof(wbranch));
if (datalen - sizeof(wbranch) < wbranch.name_len) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
goto done;
}
refname = malloc(wbranch.name_len + 1);
if (refname == NULL) {
err = got_error_from_errno("malloc");
goto done;
}
memcpy(refname, imsg.data + sizeof(wbranch), wbranch.name_len);
refname[wbranch.name_len] = '\0';
err = got_pathlist_append(&wanted_branches, refname, NULL);
if (err) {
free(refname);
goto done;
}
imsg_free(&imsg);
}
for (i = 0; i < fetch_req.n_wanted_refs; i++) {
char *refname;
err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
if (err) {
if (err->code == GOT_ERR_PRIVSEP_PIPE)
err = NULL;
goto done;
}
if (imsg.hdr.type == GOT_IMSG_STOP)
goto done;
if (imsg.hdr.type != GOT_IMSG_FETCH_WANTED_REF) {
err = got_error(GOT_ERR_PRIVSEP_MSG);
goto done;
}
datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
if (datalen < sizeof(wref)) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
goto done;
}
memcpy(&wref, imsg.data, sizeof(wref));
if (datalen - sizeof(wref) < wref.name_len) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
goto done;
}
refname = malloc(wref.name_len + 1);
if (refname == NULL) {
err = got_error_from_errno("malloc");
goto done;
}
memcpy(refname, imsg.data + sizeof(wref), wref.name_len);
refname[wref.name_len] = '\0';
err = got_pathlist_append(&wanted_refs, refname, NULL);
if (err) {
free(refname);
goto done;
}
imsg_free(&imsg);
}
err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
if (err) {
if (err->code == GOT_ERR_PRIVSEP_PIPE)
err = NULL;
goto done;
}
if (imsg.hdr.type == GOT_IMSG_STOP)
goto done;
if (imsg.hdr.type != GOT_IMSG_FETCH_OUTFD) {
err = got_error(GOT_ERR_PRIVSEP_MSG);
goto done;
}
if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
goto done;
}
packfd = imsg.fd;
err = fetch_pack(fetchfd, packfd, pack_sha1, &have_refs,
fetch_req.fetch_all_branches, &wanted_branches,
&wanted_refs, fetch_req.list_refs_only, &ibuf);
done:
TAILQ_FOREACH(pe, &have_refs, entry) {
free((char *)pe->path);
free(pe->data);
}
got_pathlist_free(&have_refs);
TAILQ_FOREACH(pe, &wanted_branches, entry)
free((char *)pe->path);
got_pathlist_free(&wanted_branches);
if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
err = got_error_from_errno("close");
if (packfd != -1 && close(packfd) == -1 && err == NULL)
err = got_error_from_errno("close");
if (err != NULL)
got_privsep_send_error(&ibuf, err);
else
err = send_fetch_done(&ibuf, pack_sha1);
if (err != NULL) {
fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
got_privsep_send_error(&ibuf, err);
}
exit(0);
}