Blob


1 /*
2 * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/queue.h>
21 #include <sys/uio.h>
22 #include <sys/socket.h>
23 #include <sys/wait.h>
24 #include <sys/resource.h>
25 #include <sys/socket.h>
27 #include <endian.h>
28 #include <errno.h>
29 #include <err.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdint.h>
35 #include <sha1.h>
36 #include <unistd.h>
37 #include <zlib.h>
38 #include <ctype.h>
39 #include <limits.h>
40 #include <imsg.h>
41 #include <time.h>
42 #include <uuid.h>
43 #include <netdb.h>
44 #include <netinet/in.h>
46 #include "got_error.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
49 #include "got_path.h"
50 #include "got_cancel.h"
51 #include "got_worktree.h"
52 #include "got_object.h"
53 #include "got_opentemp.h"
54 #include "got_send.h"
55 #include "got_repository_admin.h"
56 #include "got_commit_graph.h"
58 #include "got_lib_delta.h"
59 #include "got_lib_inflate.h"
60 #include "got_lib_object.h"
61 #include "got_lib_object_parse.h"
62 #include "got_lib_object_create.h"
63 #include "got_lib_pack.h"
64 #include "got_lib_sha1.h"
65 #include "got_lib_privsep.h"
66 #include "got_lib_object_cache.h"
67 #include "got_lib_repository.h"
68 #include "got_lib_pack_create.h"
70 #ifndef nitems
71 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
72 #endif
74 #ifndef ssizeof
75 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
76 #endif
78 #ifndef MIN
79 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
80 #endif
82 static const struct got_error *
83 dial_ssh(pid_t *sendpid, int *sendfd, const char *host, const char *port,
84 const char *path, const char *direction, int verbosity)
85 {
86 const struct got_error *error = NULL;
87 int pid, pfd[2];
88 char cmd[64];
89 char *argv[11];
90 int i = 0, j;
92 *sendpid = -1;
93 *sendfd = -1;
95 argv[i++] = GOT_SEND_PATH_SSH;
96 if (port != NULL) {
97 argv[i++] = "-p";
98 argv[i++] = (char *)port;
99 }
100 if (verbosity == -1) {
101 argv[i++] = "-q";
102 } else {
103 /* ssh(1) allows up to 3 "-v" options. */
104 for (j = 0; j < MIN(3, verbosity); j++)
105 argv[i++] = "-v";
107 argv[i++] = "--";
108 argv[i++] = (char *)host;
109 argv[i++] = (char *)cmd;
110 argv[i++] = (char *)path;
111 argv[i++] = NULL;
113 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
114 return got_error_from_errno("socketpair");
116 pid = fork();
117 if (pid == -1) {
118 error = got_error_from_errno("fork");
119 close(pfd[0]);
120 close(pfd[1]);
121 return error;
122 } else if (pid == 0) {
123 int n;
124 if (close(pfd[1]) == -1)
125 err(1, "close");
126 if (dup2(pfd[0], 0) == -1)
127 err(1, "dup2");
128 if (dup2(pfd[0], 1) == -1)
129 err(1, "dup2");
130 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
131 if (n < 0 || n >= ssizeof(cmd))
132 err(1, "snprintf");
133 if (execv(GOT_SEND_PATH_SSH, argv) == -1)
134 err(1, "execv");
135 abort(); /* not reached */
136 } else {
137 if (close(pfd[0]) == -1)
138 return got_error_from_errno("close");
139 *sendpid = pid;
140 *sendfd = pfd[1];
141 return NULL;
145 static const struct got_error *
146 dial_git(int *sendfd, const char *host, const char *port, const char *path,
147 const char *direction)
149 const struct got_error *err = NULL;
150 struct addrinfo hints, *servinfo, *p;
151 char *cmd = NULL;
152 int fd = -1, len, r, eaicode;
154 *sendfd = -1;
156 if (port == NULL)
157 port = GOT_DEFAULT_GIT_PORT_STR;
159 memset(&hints, 0, sizeof hints);
160 hints.ai_family = AF_UNSPEC;
161 hints.ai_socktype = SOCK_STREAM;
162 eaicode = getaddrinfo(host, port, &hints, &servinfo);
163 if (eaicode) {
164 char msg[512];
165 snprintf(msg, sizeof(msg), "%s: %s", host,
166 gai_strerror(eaicode));
167 return got_error_msg(GOT_ERR_ADDRINFO, msg);
170 for (p = servinfo; p != NULL; p = p->ai_next) {
171 if ((fd = socket(p->ai_family, p->ai_socktype,
172 p->ai_protocol)) == -1)
173 continue;
174 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
175 err = NULL;
176 break;
178 err = got_error_from_errno("connect");
179 close(fd);
181 if (p == NULL)
182 goto done;
184 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
185 err = got_error_from_errno("asprintf");
186 goto done;
188 len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
189 r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
190 if (r < 0)
191 err = got_error_from_errno("dprintf");
192 done:
193 free(cmd);
194 if (err) {
195 if (fd != -1)
196 close(fd);
197 } else
198 *sendfd = fd;
199 return err;
202 const struct got_error *
203 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
204 const char *host, const char *port, const char *server_path, int verbosity)
206 const struct got_error *err = NULL;
208 *sendpid = -1;
209 *sendfd = -1;
211 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
212 err = dial_ssh(sendpid, sendfd, host, port, server_path,
213 "receive", verbosity);
214 else if (strcmp(proto, "git") == 0)
215 err = dial_git(sendfd, host, port, server_path, "receive");
216 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
217 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
218 else
219 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
220 return err;
223 struct pack_progress_arg {
224 got_send_progress_cb progress_cb;
225 void *progress_arg;
227 off_t packfile_size;
228 int ncommits;
229 int nobj_total;
230 int nobj_deltify;
231 int nobj_written;
232 };
234 static const struct got_error *
235 pack_progress(void *arg, off_t packfile_size, int ncommits,
236 int nobj_total, int nobj_deltify, int nobj_written)
238 const struct got_error *err;
239 struct pack_progress_arg *a = arg;
241 err = a->progress_cb(a->progress_arg, packfile_size, ncommits,
242 nobj_total, nobj_deltify, nobj_written, 0, NULL, 0);
243 if (err)
244 return err;
246 a->packfile_size = packfile_size;
247 a->ncommits = ncommits;
248 a->nobj_total = nobj_total;
249 a->nobj_deltify = nobj_deltify;
250 a->nobj_written = nobj_written;
251 return NULL;
254 static const struct got_error *
255 insert_ref(struct got_reflist_head *refs, const char *refname,
256 struct got_repository *repo)
258 const struct got_error *err;
259 struct got_reference *ref;
260 struct got_reflist_entry *new;
262 err = got_ref_open(&ref, repo, refname, 0);
263 if (err)
264 return err;
266 err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
267 if (err || new == NULL /* duplicate */)
268 got_ref_close(ref);
270 return err;
273 static const struct got_error *
274 check_linear_ancestry(const char *refname, struct got_object_id *my_id,
275 struct got_object_id *their_id, struct got_repository *repo,
276 got_cancel_cb cancel_cb, void *cancel_arg)
278 const struct got_error *err = NULL;
279 struct got_object_id *yca_id;
280 int obj_type;
282 err = got_object_get_type(&obj_type, repo, their_id);
283 if (err)
284 return err;
285 if (obj_type != GOT_OBJ_TYPE_COMMIT)
286 return got_error_fmt(GOT_ERR_OBJ_TYPE,
287 "bad object type on server for %s", refname);
289 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
290 my_id, their_id, repo, cancel_cb, cancel_arg);
291 if (err)
292 return err;
293 if (yca_id == NULL)
294 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
296 /*
297 * Require a straight line of history between the two commits,
298 * with their commit being older than my commit.
300 * Non-linear situations such as this require a rebase:
302 * (theirs) D F (mine)
303 * \ /
304 * C E
305 * \ /
306 * B (yca)
307 * |
308 * A
309 */
310 if (got_object_id_cmp(their_id, yca_id) != 0)
311 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
313 free(yca_id);
314 return err;
317 static const struct got_error *
318 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
320 struct got_object_id **new;
321 const size_t alloc_chunksz = 256;
323 if (*nalloc >= n)
324 return NULL;
326 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
327 sizeof(struct got_object_id));
328 if (new == NULL)
329 return got_error_from_errno("recallocarray");
331 *ids = new;
332 *nalloc += alloc_chunksz;
333 return NULL;
336 static struct got_reference *
337 find_ref(struct got_reflist_head *refs, const char *refname)
339 struct got_reflist_entry *re;
341 TAILQ_FOREACH(re, refs, entry) {
342 if (got_path_cmp(got_ref_get_name(re->ref), refname,
343 strlen(got_ref_get_name(re->ref)),
344 strlen(refname)) == 0) {
345 return re->ref;
349 return NULL;
352 static struct got_pathlist_entry *
353 find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
355 struct got_pathlist_entry *pe;
357 TAILQ_FOREACH(pe, their_refs, entry) {
358 const char *their_refname = pe->path;
359 if (got_path_cmp(their_refname, refname,
360 strlen(their_refname), strlen(refname)) == 0) {
361 return pe;
365 return NULL;
368 static const struct got_error *
369 get_remote_refname(char **remote_refname, const char *remote_name,
370 const char *refname)
372 if (strncmp(refname, "refs/", 5) == 0)
373 refname += 5;
374 if (strncmp(refname, "heads/", 6) == 0)
375 refname += 6;
377 if (asprintf(remote_refname, "refs/remotes/%s/%s",
378 remote_name, refname) == -1)
379 return got_error_from_errno("asprintf");
381 return NULL;
384 static const struct got_error *
385 update_remote_ref(struct got_reference *my_ref, const char *remote_name,
386 struct got_repository *repo)
388 const struct got_error *err, *unlock_err;
389 struct got_object_id *my_id;
390 struct got_reference *ref = NULL;
391 char *remote_refname = NULL;
392 int ref_locked = 0;
394 err = got_ref_resolve(&my_id, repo, my_ref);
395 if (err)
396 return err;
398 err = get_remote_refname(&remote_refname, remote_name,
399 got_ref_get_name(my_ref));
400 if (err)
401 goto done;
403 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
404 if (err) {
405 if (err->code != GOT_ERR_NOT_REF)
406 goto done;
407 err = got_ref_alloc(&ref, remote_refname, my_id);
408 if (err)
409 goto done;
410 } else {
411 ref_locked = 1;
412 err = got_ref_change_ref(ref, my_id);
413 if (err)
414 goto done;
417 err = got_ref_write(ref, repo);
418 done:
419 if (ref) {
420 if (ref_locked) {
421 unlock_err = got_ref_unlock(ref);
422 if (unlock_err && err == NULL)
423 err = unlock_err;
425 got_ref_close(ref);
427 free(my_id);
428 free(remote_refname);
429 return err;
432 const struct got_error*
433 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
434 struct got_pathlist_head *tag_names,
435 struct got_pathlist_head *delete_branches,
436 int verbosity, int overwrite_refs, int sendfd,
437 struct got_repository *repo, got_send_progress_cb progress_cb,
438 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
440 int imsg_sendfds[2];
441 int npackfd = -1, nsendfd = -1;
442 int sendstatus, done = 0;
443 const struct got_error *err;
444 struct imsgbuf sendibuf;
445 pid_t sendpid = -1;
446 struct got_reflist_head refs;
447 struct got_pathlist_head have_refs;
448 struct got_pathlist_head their_refs;
449 struct got_pathlist_entry *pe;
450 struct got_reflist_entry *re;
451 struct got_object_id **our_ids = NULL;
452 struct got_object_id **their_ids = NULL;
453 struct got_object_id *my_id = NULL;
454 int i, nours = 0, ntheirs = 0;
455 size_t nalloc_ours = 0, nalloc_theirs = 0;
456 int refs_to_send = 0, refs_to_delete = 0;
457 off_t bytes_sent = 0;
458 struct pack_progress_arg ppa;
459 uint8_t packsha1[SHA1_DIGEST_LENGTH];
460 FILE *packfile = NULL;
462 TAILQ_INIT(&refs);
463 TAILQ_INIT(&have_refs);
464 TAILQ_INIT(&their_refs);
466 TAILQ_FOREACH(pe, branch_names, entry) {
467 const char *branchname = pe->path;
468 if (strncmp(branchname, "refs/heads/", 11) != 0) {
469 char *s;
470 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
471 err = got_error_from_errno("asprintf");
472 goto done;
474 err = insert_ref(&refs, s, repo);
475 free(s);
476 } else {
477 err = insert_ref(&refs, branchname, repo);
479 if (err)
480 goto done;
483 TAILQ_FOREACH(pe, delete_branches, entry) {
484 const char *branchname = pe->path;
485 struct got_reference *ref;
486 if (strncmp(branchname, "refs/heads/", 11) != 0) {
487 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
488 branchname);
489 goto done;
491 ref = find_ref(&refs, branchname);
492 if (ref) {
493 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
494 "changes on %s will be sent to server",
495 branchname);
496 goto done;
500 TAILQ_FOREACH(pe, tag_names, entry) {
501 const char *tagname = pe->path;
502 if (strncmp(tagname, "refs/tags/", 10) != 0) {
503 char *s;
504 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
505 err = got_error_from_errno("asprintf");
506 goto done;
508 err = insert_ref(&refs, s, repo);
509 free(s);
510 } else {
511 err = insert_ref(&refs, tagname, repo);
513 if (err)
514 goto done;
517 if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
518 err = got_error(GOT_ERR_SEND_EMPTY);
519 goto done;
522 TAILQ_FOREACH(re, &refs, entry) {
523 struct got_object_id *id;
524 int obj_type;
526 if (got_ref_is_symbolic(re->ref)) {
527 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
528 "cannot send symbolic reference %s",
529 got_ref_get_name(re->ref));
530 goto done;
533 err = got_ref_resolve(&id, repo, re->ref);
534 if (err)
535 goto done;
536 err = got_object_get_type(&obj_type, repo, id);
537 free(id);
538 if (err)
539 goto done;
540 switch (obj_type) {
541 case GOT_OBJ_TYPE_COMMIT:
542 case GOT_OBJ_TYPE_TAG:
543 break;
544 default:
545 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
546 "cannot send %s", got_ref_get_name(re->ref));
547 goto done;
551 packfile = got_opentemp();
552 if (packfile == NULL) {
553 err = got_error_from_errno("got_opentemp");
554 goto done;
557 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
558 err = got_error_from_errno("socketpair");
559 goto done;
562 sendpid = fork();
563 if (sendpid == -1) {
564 err = got_error_from_errno("fork");
565 goto done;
566 } else if (sendpid == 0){
567 got_privsep_exec_child(imsg_sendfds,
568 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
571 if (close(imsg_sendfds[1]) == -1) {
572 err = got_error_from_errno("close");
573 goto done;
575 imsg_init(&sendibuf, imsg_sendfds[0]);
576 nsendfd = dup(sendfd);
577 if (nsendfd == -1) {
578 err = got_error_from_errno("dup");
579 goto done;
582 /*
583 * Convert reflist to pathlist since the privsep layer
584 * is linked into helper programs which lack reference.c.
585 */
586 TAILQ_FOREACH(re, &refs, entry) {
587 struct got_object_id *id;
588 err = got_ref_resolve(&id, repo, re->ref);
589 if (err)
590 goto done;
591 err = got_pathlist_append(&have_refs,
592 got_ref_get_name(re->ref), id);
593 if (err)
594 goto done;
595 /*
596 * Also prepare the array of our object IDs which
597 * will be needed for generating a pack file.
598 */
599 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
600 if (err)
601 goto done;
602 our_ids[nours] = id;
603 nours++;
606 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
607 delete_branches, verbosity);
608 if (err)
609 goto done;
610 nsendfd = -1;
612 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
613 if (err)
614 goto done;
616 /*
617 * Process references reported by the server.
618 * Push appropriate object IDs onto the "their IDs" array.
619 * This array will be used to exclude objects which already
620 * exist on the server from our pack file.
621 */
622 TAILQ_FOREACH(pe, &their_refs, entry) {
623 const char *refname = pe->path;
624 struct got_object_id *their_id = pe->data;
625 int have_their_id;
626 struct got_object *obj;
627 struct got_reference *my_ref = NULL;
628 int is_tag = 0;
630 /* Don't blindly trust the server to send us valid names. */
631 if (!got_ref_name_is_valid(refname))
632 continue;
634 /*
635 * Find out whether this is a reference we want to upload.
636 * Otherwise we can still use this reference as a hint to
637 * avoid uploading any objects the server already has.
638 */
639 my_ref = find_ref(&refs, refname);
640 if (my_ref) {
641 err = got_ref_resolve(&my_id, repo, my_ref);
642 if (err)
643 goto done;
644 if (got_object_id_cmp(my_id, their_id) == 0) {
645 free(my_id);
646 my_id = NULL;
647 continue;
649 refs_to_send++;
653 if (strncmp(refname, "refs/tags/", 10) == 0)
654 is_tag = 1;
656 /* Prevent tags from being overwritten by default. */
657 if (!overwrite_refs && my_ref && is_tag) {
658 err = got_error_fmt(GOT_ERR_SEND_TAG_EXISTS,
659 "%s", refname);
660 goto done;
663 /* Check if their object exists locally. */
664 err = got_object_open(&obj, repo, their_id);
665 if (err) {
666 if (err->code != GOT_ERR_NO_OBJ)
667 goto done;
668 if (!overwrite_refs && my_ref != NULL) {
669 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
670 "%s", refname);
671 goto done;
673 have_their_id = 0;
674 } else {
675 got_object_close(obj);
676 have_their_id = 1;
679 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
680 if (err)
681 goto done;
683 if (have_their_id) {
684 /* Enforce linear ancestry if required. */
685 if (!overwrite_refs && my_ref && !is_tag) {
686 struct got_object_id *my_id;
687 err = got_ref_resolve(&my_id, repo, my_ref);
688 if (err)
689 goto done;
690 err = check_linear_ancestry(refname, my_id,
691 their_id, repo, cancel_cb, cancel_arg);
692 free(my_id);
693 my_id = NULL;
694 if (err)
695 goto done;
697 /* Exclude any objects reachable via their ID. */
698 their_ids[ntheirs] = got_object_id_dup(their_id);
699 if (their_ids[ntheirs] == NULL) {
700 err = got_error_from_errno("got_object_id_dup");
701 goto done;
703 ntheirs++;
704 } else if (!is_tag) {
705 char *remote_refname;
706 struct got_reference *ref;
707 /*
708 * Exclude any objects which exist on the server
709 * according to a locally cached remote reference.
710 */
711 err = get_remote_refname(&remote_refname,
712 remote_name, refname);
713 if (err)
714 goto done;
715 err = got_ref_open(&ref, repo, remote_refname, 0);
716 free(remote_refname);
717 if (err) {
718 if (err->code != GOT_ERR_NOT_REF)
719 goto done;
720 } else {
721 err = got_ref_resolve(&their_ids[ntheirs],
722 repo, ref);
723 got_ref_close(ref);
724 if (err)
725 goto done;
726 ntheirs++;
731 /* Account for any new references we are going to upload. */
732 TAILQ_FOREACH(re, &refs, entry) {
733 if (find_their_ref(&their_refs,
734 got_ref_get_name(re->ref)) == NULL)
735 refs_to_send++;
738 /* Account for any existing references we are going to delete. */
739 TAILQ_FOREACH(pe, delete_branches, entry) {
740 const char *branchname = pe->path;
741 if (find_their_ref(&their_refs, branchname))
742 refs_to_delete++;
745 if (refs_to_send == 0 && refs_to_delete == 0) {
746 got_privsep_send_stop(imsg_sendfds[0]);
747 goto done;
750 if (refs_to_send > 0) {
751 memset(&ppa, 0, sizeof(ppa));
752 ppa.progress_cb = progress_cb;
753 ppa.progress_arg = progress_arg;
754 err = got_pack_create(packsha1, packfile, their_ids, ntheirs,
755 our_ids, nours, repo, 0, 1, pack_progress, &ppa,
756 cancel_cb, cancel_arg);
757 if (err)
758 goto done;
760 if (fflush(packfile) == -1) {
761 err = got_error_from_errno("fflush");
762 goto done;
765 npackfd = dup(fileno(packfile));
766 if (npackfd == -1) {
767 err = got_error_from_errno("dup");
768 goto done;
770 err = got_privsep_send_packfd(&sendibuf, npackfd);
771 if (err != NULL)
772 goto done;
773 npackfd = -1;
774 } else {
775 err = got_privsep_send_packfd(&sendibuf, -1);
776 if (err != NULL)
777 goto done;
780 while (!done) {
781 int success = 0;
782 char *refname = NULL;
783 off_t bytes_sent_cur = 0;
784 if (cancel_cb) {
785 err = (*cancel_cb)(cancel_arg);
786 if (err)
787 goto done;
789 err = got_privsep_recv_send_progress(&done, &bytes_sent,
790 &success, &refname, &sendibuf);
791 if (err)
792 goto done;
793 if (refname && got_ref_name_is_valid(refname) && success &&
794 strncmp(refname, "refs/tags/", 10) != 0) {
795 struct got_reference *my_ref;
796 /*
797 * The server has accepted our changes.
798 * Update our reference in refs/remotes/ accordingly.
799 */
800 my_ref = find_ref(&refs, refname);
801 if (my_ref) {
802 err = update_remote_ref(my_ref, remote_name,
803 repo);
804 if (err)
805 goto done;
808 if (refname != NULL ||
809 bytes_sent_cur != bytes_sent) {
810 err = progress_cb(progress_arg, ppa.packfile_size,
811 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
812 ppa.nobj_written, bytes_sent,
813 refname, success);
814 if (err) {
815 free(refname);
816 goto done;
818 bytes_sent_cur = bytes_sent;
820 free(refname);
822 done:
823 if (sendpid != -1) {
824 if (err)
825 got_privsep_send_stop(imsg_sendfds[0]);
826 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
827 err = got_error_from_errno("waitpid");
829 if (packfile && fclose(packfile) == EOF && err == NULL)
830 err = got_error_from_errno("fclose");
831 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
832 err = got_error_from_errno("close");
833 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
834 err = got_error_from_errno("close");
836 got_ref_list_free(&refs);
837 got_pathlist_free(&have_refs);
838 got_pathlist_free(&their_refs);
839 for (i = 0; i < nours; i++)
840 free(our_ids[i]);
841 free(our_ids);
842 for (i = 0; i < ntheirs; i++)
843 free(their_ids[i]);
844 free(their_ids);
845 free(my_id);
846 return err;