Hash :
535e07c7
Author :
Date :
2021-08-29T13:09:21
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
/*
* Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
* Copyright (c) 2021 Stefan Sperling <stsp@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/stat.h>
#include <sys/queue.h>
#include <sys/uio.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <endian.h>
#include <errno.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sha1.h>
#include <unistd.h>
#include <zlib.h>
#include <ctype.h>
#include <limits.h>
#include <imsg.h>
#include <time.h>
#include <uuid.h>
#include <netdb.h>
#include <netinet/in.h>
#include "got_error.h"
#include "got_reference.h"
#include "got_repository.h"
#include "got_path.h"
#include "got_cancel.h"
#include "got_worktree.h"
#include "got_object.h"
#include "got_opentemp.h"
#include "got_send.h"
#include "got_repository_admin.h"
#include "got_commit_graph.h"
#include "got_lib_delta.h"
#include "got_lib_inflate.h"
#include "got_lib_object.h"
#include "got_lib_object_parse.h"
#include "got_lib_object_create.h"
#include "got_lib_pack.h"
#include "got_lib_sha1.h"
#include "got_lib_privsep.h"
#include "got_lib_object_cache.h"
#include "got_lib_repository.h"
#include "got_lib_pack_create.h"
#ifndef nitems
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
#endif
#ifndef ssizeof
#define ssizeof(_x) ((ssize_t)(sizeof(_x)))
#endif
#ifndef MIN
#define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
#endif
static const struct got_error *
dial_ssh(pid_t *sendpid, int *sendfd, const char *host, const char *port,
const char *path, const char *direction, int verbosity)
{
const struct got_error *error = NULL;
int pid, pfd[2];
char cmd[64];
char *argv[11];
int i = 0, j;
*sendpid = -1;
*sendfd = -1;
argv[i++] = GOT_SEND_PATH_SSH;
if (port != NULL) {
argv[i++] = "-p";
argv[i++] = (char *)port;
}
if (verbosity == -1) {
argv[i++] = "-q";
} else {
/* ssh(1) allows up to 3 "-v" options. */
for (j = 0; j < MIN(3, verbosity); j++)
argv[i++] = "-v";
}
argv[i++] = "--";
argv[i++] = (char *)host;
argv[i++] = (char *)cmd;
argv[i++] = (char *)path;
argv[i++] = NULL;
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
return got_error_from_errno("socketpair");
pid = fork();
if (pid == -1) {
error = got_error_from_errno("fork");
close(pfd[0]);
close(pfd[1]);
return error;
} else if (pid == 0) {
int n;
if (close(pfd[1]) == -1)
err(1, "close");
if (dup2(pfd[0], 0) == -1)
err(1, "dup2");
if (dup2(pfd[0], 1) == -1)
err(1, "dup2");
n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
if (n < 0 || n >= ssizeof(cmd))
err(1, "snprintf");
if (execv(GOT_SEND_PATH_SSH, argv) == -1)
err(1, "execv");
abort(); /* not reached */
} else {
if (close(pfd[0]) == -1)
return got_error_from_errno("close");
*sendpid = pid;
*sendfd = pfd[1];
return NULL;
}
}
static const struct got_error *
dial_git(int *sendfd, const char *host, const char *port, const char *path,
const char *direction)
{
const struct got_error *err = NULL;
struct addrinfo hints, *servinfo, *p;
char *cmd = NULL;
int fd = -1, len, r, eaicode;
*sendfd = -1;
if (port == NULL)
port = GOT_DEFAULT_GIT_PORT_STR;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
eaicode = getaddrinfo(host, port, &hints, &servinfo);
if (eaicode) {
char msg[512];
snprintf(msg, sizeof(msg), "%s: %s", host,
gai_strerror(eaicode));
return got_error_msg(GOT_ERR_ADDRINFO, msg);
}
for (p = servinfo; p != NULL; p = p->ai_next) {
if ((fd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1)
continue;
if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
err = NULL;
break;
}
err = got_error_from_errno("connect");
close(fd);
}
if (p == NULL)
goto done;
if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
err = got_error_from_errno("asprintf");
goto done;
}
len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
if (r < 0)
err = got_error_from_errno("dprintf");
done:
free(cmd);
if (err) {
if (fd != -1)
close(fd);
} else
*sendfd = fd;
return err;
}
const struct got_error *
got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
const char *host, const char *port, const char *server_path, int verbosity)
{
const struct got_error *err = NULL;
*sendpid = -1;
*sendfd = -1;
if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
err = dial_ssh(sendpid, sendfd, host, port, server_path,
"receive", verbosity);
else if (strcmp(proto, "git") == 0)
err = dial_git(sendfd, host, port, server_path, "receive");
else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
err = got_error_path(proto, GOT_ERR_NOT_IMPL);
else
err = got_error_path(proto, GOT_ERR_BAD_PROTO);
return err;
}
struct pack_progress_arg {
got_send_progress_cb progress_cb;
void *progress_arg;
off_t packfile_size;
int ncommits;
int nobj_total;
int nobj_deltify;
int nobj_written;
};
static const struct got_error *
pack_progress(void *arg, off_t packfile_size, int ncommits,
int nobj_total, int nobj_deltify, int nobj_written)
{
const struct got_error *err;
struct pack_progress_arg *a = arg;
err = a->progress_cb(a->progress_arg, packfile_size, ncommits,
nobj_total, nobj_deltify, nobj_written, 0, NULL, 0);
if (err)
return err;
a->packfile_size = packfile_size;
a->ncommits = ncommits;
a->nobj_total = nobj_total;
a->nobj_deltify = nobj_deltify;
a->nobj_written = nobj_written;
return NULL;
}
static const struct got_error *
insert_ref(struct got_reflist_head *refs, const char *refname,
struct got_repository *repo)
{
const struct got_error *err;
struct got_reference *ref;
struct got_reflist_entry *new;
err = got_ref_open(&ref, repo, refname, 0);
if (err)
return err;
err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
if (err || new == NULL /* duplicate */)
got_ref_close(ref);
return err;
}
static const struct got_error *
check_linear_ancestry(const char *refname, struct got_object_id *my_id,
struct got_object_id *their_id, struct got_repository *repo,
got_cancel_cb cancel_cb, void *cancel_arg)
{
const struct got_error *err = NULL;
struct got_object_id *yca_id;
int obj_type;
err = got_object_get_type(&obj_type, repo, their_id);
if (err)
return err;
if (obj_type != GOT_OBJ_TYPE_COMMIT)
return got_error_fmt(GOT_ERR_OBJ_TYPE,
"bad object type on server for %s", refname);
err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
my_id, their_id, repo, cancel_cb, cancel_arg);
if (err)
return err;
if (yca_id == NULL)
return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
/*
* Require a straight line of history between the two commits,
* with their commit being older than my commit.
*
* Non-linear situations such as this require a rebase:
*
* (theirs) D F (mine)
* \ /
* C E
* \ /
* B (yca)
* |
* A
*/
if (got_object_id_cmp(their_id, yca_id) != 0)
err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
free(yca_id);
return err;
}
static const struct got_error *
realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
{
struct got_object_id **new;
const size_t alloc_chunksz = 256;
if (*nalloc >= n)
return NULL;
new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
sizeof(struct got_object_id));
if (new == NULL)
return got_error_from_errno("recallocarray");
*ids = new;
*nalloc += alloc_chunksz;
return NULL;
}
static struct got_reference *
find_ref(struct got_reflist_head *refs, const char *refname)
{
struct got_reflist_entry *re;
TAILQ_FOREACH(re, refs, entry) {
if (got_path_cmp(got_ref_get_name(re->ref), refname,
strlen(got_ref_get_name(re->ref)),
strlen(refname)) == 0) {
return re->ref;
}
}
return NULL;
}
static struct got_pathlist_entry *
find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
{
struct got_pathlist_entry *pe;
TAILQ_FOREACH(pe, their_refs, entry) {
const char *their_refname = pe->path;
if (got_path_cmp(their_refname, refname,
strlen(their_refname), strlen(refname)) == 0) {
return pe;
}
}
return NULL;
}
static const struct got_error *
get_remote_refname(char **remote_refname, const char *remote_name,
const char *refname)
{
if (strncmp(refname, "refs/", 5) == 0)
refname += 5;
if (strncmp(refname, "heads/", 6) == 0)
refname += 6;
if (asprintf(remote_refname, "refs/remotes/%s/%s",
remote_name, refname) == -1)
return got_error_from_errno("asprintf");
return NULL;
}
static const struct got_error *
update_remote_ref(struct got_reference *my_ref, const char *remote_name,
struct got_repository *repo)
{
const struct got_error *err, *unlock_err;
struct got_object_id *my_id;
struct got_reference *ref = NULL;
char *remote_refname = NULL;
int ref_locked = 0;
err = got_ref_resolve(&my_id, repo, my_ref);
if (err)
return err;
err = get_remote_refname(&remote_refname, remote_name,
got_ref_get_name(my_ref));
if (err)
goto done;
err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
if (err) {
if (err->code != GOT_ERR_NOT_REF)
goto done;
err = got_ref_alloc(&ref, remote_refname, my_id);
if (err)
goto done;
} else {
ref_locked = 1;
err = got_ref_change_ref(ref, my_id);
if (err)
goto done;
}
err = got_ref_write(ref, repo);
done:
if (ref) {
if (ref_locked) {
unlock_err = got_ref_unlock(ref);
if (unlock_err && err == NULL)
err = unlock_err;
}
got_ref_close(ref);
}
free(my_id);
free(remote_refname);
return err;
}
const struct got_error*
got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
struct got_pathlist_head *tag_names,
struct got_pathlist_head *delete_branches,
int verbosity, int overwrite_refs, int sendfd,
struct got_repository *repo, got_send_progress_cb progress_cb,
void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
{
int imsg_sendfds[2];
int npackfd = -1, nsendfd = -1;
int sendstatus, done = 0;
const struct got_error *err;
struct imsgbuf sendibuf;
pid_t sendpid = -1;
struct got_reflist_head refs;
struct got_pathlist_head have_refs;
struct got_pathlist_head their_refs;
struct got_pathlist_entry *pe;
struct got_reflist_entry *re;
struct got_object_id **our_ids = NULL;
struct got_object_id **their_ids = NULL;
struct got_object_id *my_id = NULL;
int i, nours = 0, ntheirs = 0;
size_t nalloc_ours = 0, nalloc_theirs = 0;
int refs_to_send = 0, refs_to_delete = 0;
off_t bytes_sent = 0;
struct pack_progress_arg ppa;
uint8_t packsha1[SHA1_DIGEST_LENGTH];
FILE *packfile = NULL;
TAILQ_INIT(&refs);
TAILQ_INIT(&have_refs);
TAILQ_INIT(&their_refs);
TAILQ_FOREACH(pe, branch_names, entry) {
const char *branchname = pe->path;
if (strncmp(branchname, "refs/heads/", 11) != 0) {
char *s;
if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
err = got_error_from_errno("asprintf");
goto done;
}
err = insert_ref(&refs, s, repo);
free(s);
} else {
err = insert_ref(&refs, branchname, repo);
}
if (err)
goto done;
}
TAILQ_FOREACH(pe, delete_branches, entry) {
const char *branchname = pe->path;
struct got_reference *ref;
if (strncmp(branchname, "refs/heads/", 11) != 0) {
err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
branchname);
goto done;
}
ref = find_ref(&refs, branchname);
if (ref) {
err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
"changes on %s will be sent to server",
branchname);
goto done;
}
}
TAILQ_FOREACH(pe, tag_names, entry) {
const char *tagname = pe->path;
if (strncmp(tagname, "refs/tags/", 10) != 0) {
char *s;
if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
err = got_error_from_errno("asprintf");
goto done;
}
err = insert_ref(&refs, s, repo);
free(s);
} else {
err = insert_ref(&refs, tagname, repo);
}
if (err)
goto done;
}
if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
err = got_error(GOT_ERR_SEND_EMPTY);
goto done;
}
TAILQ_FOREACH(re, &refs, entry) {
struct got_object_id *id;
int obj_type;
if (got_ref_is_symbolic(re->ref)) {
err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
"cannot send symbolic reference %s",
got_ref_get_name(re->ref));
goto done;
}
err = got_ref_resolve(&id, repo, re->ref);
if (err)
goto done;
err = got_object_get_type(&obj_type, repo, id);
free(id);
if (err)
goto done;
switch (obj_type) {
case GOT_OBJ_TYPE_COMMIT:
case GOT_OBJ_TYPE_TAG:
break;
default:
err = got_error_fmt(GOT_ERR_OBJ_TYPE,
"cannot send %s", got_ref_get_name(re->ref));
goto done;
}
}
packfile = got_opentemp();
if (packfile == NULL) {
err = got_error_from_errno("got_opentemp");
goto done;
}
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
err = got_error_from_errno("socketpair");
goto done;
}
sendpid = fork();
if (sendpid == -1) {
err = got_error_from_errno("fork");
goto done;
} else if (sendpid == 0){
got_privsep_exec_child(imsg_sendfds,
GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
}
if (close(imsg_sendfds[1]) == -1) {
err = got_error_from_errno("close");
goto done;
}
imsg_init(&sendibuf, imsg_sendfds[0]);
nsendfd = dup(sendfd);
if (nsendfd == -1) {
err = got_error_from_errno("dup");
goto done;
}
/*
* Convert reflist to pathlist since the privsep layer
* is linked into helper programs which lack reference.c.
*/
TAILQ_FOREACH(re, &refs, entry) {
struct got_object_id *id;
err = got_ref_resolve(&id, repo, re->ref);
if (err)
goto done;
err = got_pathlist_append(&have_refs,
got_ref_get_name(re->ref), id);
if (err)
goto done;
/*
* Also prepare the array of our object IDs which
* will be needed for generating a pack file.
*/
err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
if (err)
goto done;
our_ids[nours] = id;
nours++;
}
err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
delete_branches, verbosity);
if (err)
goto done;
nsendfd = -1;
err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
if (err)
goto done;
/*
* Process references reported by the server.
* Push appropriate object IDs onto the "their IDs" array.
* This array will be used to exclude objects which already
* exist on the server from our pack file.
*/
TAILQ_FOREACH(pe, &their_refs, entry) {
const char *refname = pe->path;
struct got_object_id *their_id = pe->data;
int have_their_id;
struct got_object *obj;
struct got_reference *my_ref = NULL;
int is_tag = 0;
/* Don't blindly trust the server to send us valid names. */
if (!got_ref_name_is_valid(refname))
continue;
/*
* Find out whether this is a reference we want to upload.
* Otherwise we can still use this reference as a hint to
* avoid uploading any objects the server already has.
*/
my_ref = find_ref(&refs, refname);
if (my_ref) {
err = got_ref_resolve(&my_id, repo, my_ref);
if (err)
goto done;
if (got_object_id_cmp(my_id, their_id) == 0) {
free(my_id);
my_id = NULL;
continue;
}
refs_to_send++;
}
if (strncmp(refname, "refs/tags/", 10) == 0)
is_tag = 1;
/* Prevent tags from being overwritten by default. */
if (!overwrite_refs && my_ref && is_tag) {
err = got_error_fmt(GOT_ERR_SEND_TAG_EXISTS,
"%s", refname);
goto done;
}
/* Check if their object exists locally. */
err = got_object_open(&obj, repo, their_id);
if (err) {
if (err->code != GOT_ERR_NO_OBJ)
goto done;
if (!overwrite_refs && my_ref != NULL) {
err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
"%s", refname);
goto done;
}
have_their_id = 0;
} else {
got_object_close(obj);
have_their_id = 1;
}
err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
if (err)
goto done;
if (have_their_id) {
/* Enforce linear ancestry if required. */
if (!overwrite_refs && my_ref && !is_tag) {
struct got_object_id *my_id;
err = got_ref_resolve(&my_id, repo, my_ref);
if (err)
goto done;
err = check_linear_ancestry(refname, my_id,
their_id, repo, cancel_cb, cancel_arg);
free(my_id);
my_id = NULL;
if (err)
goto done;
}
/* Exclude any objects reachable via their ID. */
their_ids[ntheirs] = got_object_id_dup(their_id);
if (their_ids[ntheirs] == NULL) {
err = got_error_from_errno("got_object_id_dup");
goto done;
}
ntheirs++;
} else if (!is_tag) {
char *remote_refname;
struct got_reference *ref;
/*
* Exclude any objects which exist on the server
* according to a locally cached remote reference.
*/
err = get_remote_refname(&remote_refname,
remote_name, refname);
if (err)
goto done;
err = got_ref_open(&ref, repo, remote_refname, 0);
free(remote_refname);
if (err) {
if (err->code != GOT_ERR_NOT_REF)
goto done;
} else {
err = got_ref_resolve(&their_ids[ntheirs],
repo, ref);
got_ref_close(ref);
if (err)
goto done;
ntheirs++;
}
}
}
/* Account for any new references we are going to upload. */
TAILQ_FOREACH(re, &refs, entry) {
if (find_their_ref(&their_refs,
got_ref_get_name(re->ref)) == NULL)
refs_to_send++;
}
/* Account for any existing references we are going to delete. */
TAILQ_FOREACH(pe, delete_branches, entry) {
const char *branchname = pe->path;
if (find_their_ref(&their_refs, branchname))
refs_to_delete++;
}
if (refs_to_send == 0 && refs_to_delete == 0) {
got_privsep_send_stop(imsg_sendfds[0]);
goto done;
}
if (refs_to_send > 0) {
memset(&ppa, 0, sizeof(ppa));
ppa.progress_cb = progress_cb;
ppa.progress_arg = progress_arg;
err = got_pack_create(packsha1, packfile, their_ids, ntheirs,
our_ids, nours, repo, 0, 1, pack_progress, &ppa,
cancel_cb, cancel_arg);
if (err)
goto done;
if (fflush(packfile) == -1) {
err = got_error_from_errno("fflush");
goto done;
}
npackfd = dup(fileno(packfile));
if (npackfd == -1) {
err = got_error_from_errno("dup");
goto done;
}
err = got_privsep_send_packfd(&sendibuf, npackfd);
if (err != NULL)
goto done;
npackfd = -1;
} else {
err = got_privsep_send_packfd(&sendibuf, -1);
if (err != NULL)
goto done;
}
while (!done) {
int success = 0;
char *refname = NULL;
off_t bytes_sent_cur = 0;
if (cancel_cb) {
err = (*cancel_cb)(cancel_arg);
if (err)
goto done;
}
err = got_privsep_recv_send_progress(&done, &bytes_sent,
&success, &refname, &sendibuf);
if (err)
goto done;
if (refname && got_ref_name_is_valid(refname) && success &&
strncmp(refname, "refs/tags/", 10) != 0) {
struct got_reference *my_ref;
/*
* The server has accepted our changes.
* Update our reference in refs/remotes/ accordingly.
*/
my_ref = find_ref(&refs, refname);
if (my_ref) {
err = update_remote_ref(my_ref, remote_name,
repo);
if (err)
goto done;
}
}
if (refname != NULL ||
bytes_sent_cur != bytes_sent) {
err = progress_cb(progress_arg, ppa.packfile_size,
ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
ppa.nobj_written, bytes_sent,
refname, success);
if (err) {
free(refname);
goto done;
}
bytes_sent_cur = bytes_sent;
}
free(refname);
}
done:
if (sendpid != -1) {
if (err)
got_privsep_send_stop(imsg_sendfds[0]);
if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
err = got_error_from_errno("waitpid");
}
if (packfile && fclose(packfile) == EOF && err == NULL)
err = got_error_from_errno("fclose");
if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
err = got_error_from_errno("close");
if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
err = got_error_from_errno("close");
got_ref_list_free(&refs);
got_pathlist_free(&have_refs);
got_pathlist_free(&their_refs);
for (i = 0; i < nours; i++)
free(our_ids[i]);
free(our_ids);
for (i = 0; i < ntheirs; i++)
free(their_ids[i]);
free(their_ids);
free(my_id);
return err;
}