fix number of queued ids passed from findtwixt() to got_pack_paint_commits()
[got-portable.git] / lib / send.c
blob805b395066961cc2893ecbb2132559c999f43ece
1 /*
2 * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2023 Josh Rickmar <jrick@zettaport.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include "got_compat.h"
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/queue.h>
24 #include <sys/uio.h>
25 #include <sys/socket.h>
26 #include <sys/wait.h>
27 #include <sys/resource.h>
28 #include <sys/socket.h>
30 #include <errno.h>
31 #include <err.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <unistd.h>
38 #include <zlib.h>
39 #include <ctype.h>
40 #include <limits.h>
41 #include <time.h>
43 #include "got_error.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_path.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
49 #include "got_object.h"
50 #include "got_opentemp.h"
51 #include "got_send.h"
52 #include "got_repository_admin.h"
53 #include "got_commit_graph.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_hash.h"
57 #include "got_lib_inflate.h"
58 #include "got_lib_object.h"
59 #include "got_lib_object_parse.h"
60 #include "got_lib_object_create.h"
61 #include "got_lib_pack.h"
62 #include "got_lib_privsep.h"
63 #include "got_lib_object_cache.h"
64 #include "got_lib_repository.h"
65 #include "got_lib_ratelimit.h"
66 #include "got_lib_pack_create.h"
67 #include "got_lib_dial.h"
68 #include "got_lib_worktree_cvg.h"
69 #include "got_lib_poll.h"
71 #ifndef nitems
72 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
73 #endif
75 #ifndef ssizeof
76 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
77 #endif
79 #ifndef MIN
80 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
81 #endif
83 const struct got_error *
84 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
85 const char *host, const char *port, const char *server_path,
86 const char *jumphost, const char *identity_file, int verbosity)
88 const struct got_error *err = NULL;
90 *sendpid = -1;
91 *sendfd = -1;
93 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
94 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
95 jumphost, identity_file, GOT_DIAL_CMD_SEND, verbosity);
96 else if (strcmp(proto, "git") == 0)
97 err = got_dial_git(sendfd, host, port, server_path,
98 GOT_DIAL_CMD_SEND);
99 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
100 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
101 else
102 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
103 return err;
106 struct pack_progress_arg {
107 got_send_progress_cb progress_cb;
108 void *progress_arg;
109 int sendfd;
111 int ncolored;
112 int nfound;
113 int ntrees;
114 off_t packfile_size;
115 int ncommits;
116 int nobj_total;
117 int nobj_deltify;
118 int nobj_written;
121 static const struct got_error *
122 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
123 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
124 int nobj_written, int pack_done)
126 const struct got_error *err;
127 struct pack_progress_arg *a = arg;
129 err = a->progress_cb(a->progress_arg, ncolored, nfound, ntrees,
130 packfile_size, ncommits, nobj_total, nobj_deltify,
131 nobj_written, 0, NULL, NULL, 0);
132 if (err)
133 return err;
136 * Detect the server closing our connection while we are
137 * busy creating a pack file.
139 * XXX This should be a temporary workaround. A better fix would
140 * be to avoid use of an on-disk tempfile for pack file data.
141 * Instead we could stream pack file data to got-send-pack while
142 * the pack file is being generated. Write errors in got-send-pack
143 * would then automatically abort the creation of pack file data.
145 err = got_poll_fd(a->sendfd, 0, 0);
146 if (err && err->code != GOT_ERR_TIMEOUT) {
147 if (err->code == GOT_ERR_EOF) {
148 err = got_error_msg(GOT_ERR_EOF,
149 "server unexpectedly closed the connection");
151 return err;
154 a->ncolored= ncolored;
155 a->nfound = nfound;
156 a->ntrees = ntrees;
157 a->packfile_size = packfile_size;
158 a->ncommits = ncommits;
159 a->nobj_total = nobj_total;
160 a->nobj_deltify = nobj_deltify;
161 a->nobj_written = nobj_written;
162 return NULL;
165 static const struct got_error *
166 insert_sendable_ref(struct got_pathlist_head *refs, const char *refname,
167 const char *target_refname, struct got_repository *repo)
169 const struct got_error *err;
170 struct got_reference *ref;
171 struct got_object_id *id = NULL;
172 struct got_pathlist_entry *new = NULL;
173 int obj_type;
175 err = got_ref_open(&ref, repo, refname, 0);
176 if (err)
177 return err;
179 if (got_ref_is_symbolic(ref)) {
180 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
181 "cannot send symbolic reference %s", refname);
182 goto done;
185 err = got_ref_resolve(&id, repo, ref);
186 if (err)
187 goto done;
188 err = got_object_get_type(&obj_type, repo, id);
189 if (err)
190 goto done;
191 switch (obj_type) {
192 case GOT_OBJ_TYPE_COMMIT:
193 case GOT_OBJ_TYPE_TAG:
194 break;
195 default:
196 err = got_error_fmt(GOT_ERR_OBJ_TYPE, "cannot send %s",
197 refname);
198 goto done;
201 err = got_pathlist_insert(&new, refs, target_refname, id);
202 if (new == NULL && err == NULL)
203 err = got_error(GOT_ERR_REF_DUP_ENTRY);
205 done:
206 if (ref)
207 got_ref_close(ref);
208 if (err)
209 free(id);
210 return err;
213 static const struct got_error *
214 check_common_ancestry(const char *refname, struct got_object_id *my_id,
215 struct got_object_id *their_id, struct got_repository *repo,
216 got_cancel_cb cancel_cb, void *cancel_arg)
218 const struct got_error *err = NULL;
219 struct got_object_id *yca_id;
220 int obj_type;
222 err = got_object_get_type(&obj_type, repo, their_id);
223 if (err)
224 return err;
225 if (obj_type != GOT_OBJ_TYPE_COMMIT)
226 return got_error_fmt(GOT_ERR_OBJ_TYPE,
227 "bad object type on server for %s", refname);
229 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
230 my_id, their_id, 0, 1, repo, cancel_cb, cancel_arg);
231 if (err)
232 return err;
233 if (yca_id == NULL)
234 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
236 if (got_object_id_cmp(their_id, yca_id) != 0)
237 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
239 free(yca_id);
240 return err;
243 static const struct got_error *
244 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
246 struct got_object_id **new;
247 const size_t alloc_chunksz = 256;
249 if (*nalloc >= n)
250 return NULL;
252 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
253 sizeof(struct got_object_id));
254 if (new == NULL)
255 return got_error_from_errno("recallocarray");
257 *ids = new;
258 *nalloc += alloc_chunksz;
259 return NULL;
262 static struct got_pathlist_entry *
263 find_ref(struct got_pathlist_head *refs, const char *refname)
265 struct got_pathlist_entry find;
267 find.path = refname;
268 find.path_len = strlen(refname);
269 return RB_FIND(got_pathlist_head, refs, &find);
273 static const struct got_error *
274 get_remote_refname(char **remote_refname, const char *remote_name,
275 const char *refname)
277 if (strncmp(refname, "refs/", 5) == 0)
278 refname += 5;
279 if (strncmp(refname, "heads/", 6) == 0)
280 refname += 6;
282 if (asprintf(remote_refname, "refs/remotes/%s/%s",
283 remote_name, refname) == -1)
284 return got_error_from_errno("asprintf");
286 return NULL;
289 static const struct got_error *
290 update_remote_ref(struct got_pathlist_entry *my_ref, const char *remote_name,
291 struct got_repository *repo)
293 const struct got_error *err, *unlock_err;
294 const char *refname = my_ref->path;
295 struct got_object_id *my_id = my_ref->data;
296 struct got_reference *ref = NULL;
297 char *remote_refname = NULL;
298 int ref_locked = 0;
300 err = get_remote_refname(&remote_refname, remote_name, refname);
301 if (err)
302 goto done;
304 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
305 if (err) {
306 if (err->code != GOT_ERR_NOT_REF)
307 goto done;
308 err = got_ref_alloc(&ref, remote_refname, my_id);
309 if (err)
310 goto done;
311 } else {
312 ref_locked = 1;
313 err = got_ref_change_ref(ref, my_id);
314 if (err)
315 goto done;
318 err = got_ref_write(ref, repo);
319 done:
320 if (ref) {
321 if (ref_locked) {
322 unlock_err = got_ref_unlock(ref);
323 if (unlock_err && err == NULL)
324 err = unlock_err;
326 got_ref_close(ref);
328 free(remote_refname);
329 return err;
332 const struct got_error*
333 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
334 struct got_pathlist_head *tag_names,
335 struct got_pathlist_head *delete_branches,
336 int verbosity, int overwrite_refs, int sendfd,
337 struct got_repository *repo, got_send_progress_cb progress_cb,
338 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
340 int imsg_sendfds[2] = { -1, -1 };
341 int npackfd = -1, nsendfd = -1;
342 int sendstatus, done = 0;
343 const struct got_error *err;
344 struct imsgbuf sendibuf;
345 pid_t sendpid = -1;
346 struct got_pathlist_head have_refs;
347 struct got_pathlist_head their_refs;
348 struct got_pathlist_entry *pe;
349 struct got_object_id **our_ids = NULL;
350 struct got_object_id **their_ids = NULL;
351 int nours = 0, ntheirs = 0;
352 size_t nalloc_ours = 0, nalloc_theirs = 0;
353 int refs_to_send = 0, refs_to_delete = 0;
354 off_t bytes_sent = 0, bytes_sent_cur = 0;
355 struct pack_progress_arg ppa;
356 struct got_object_id packhash;
357 int packfd = -1;
358 FILE *delta_cache = NULL;
359 char *s = NULL;
361 RB_INIT(&have_refs);
362 RB_INIT(&their_refs);
364 memset(&sendibuf, 0, sizeof(sendibuf));
366 if (got_repo_get_object_format(repo) != GOT_HASH_SHA1)
367 return got_error_fmt(GOT_ERR_NOT_IMPL,
368 "sha256 object IDs unsupported in network protocol");
370 RB_FOREACH(pe, got_pathlist_head, branch_names) {
371 const char *branchname = pe->path;
372 const char *targetname = pe->data;
374 if (targetname == NULL)
375 targetname = branchname;
377 if (strncmp(targetname, "refs/heads/", 11) != 0) {
378 if (asprintf(&s, "refs/heads/%s", targetname) == -1) {
379 err = got_error_from_errno("asprintf");
380 goto done;
382 } else {
383 if ((s = strdup(targetname)) == NULL) {
384 err = got_error_from_errno("strdup");
385 goto done;
388 err = insert_sendable_ref(&have_refs, branchname, s, repo);
389 if (err) {
390 if (err->code != GOT_ERR_REF_DUP_ENTRY)
391 goto done;
392 err = NULL;
393 free(s);
395 s = NULL;
398 RB_FOREACH(pe, got_pathlist_head, delete_branches) {
399 const char *branchname = pe->path;
400 struct got_pathlist_entry *ref;
401 if (strncmp(branchname, "refs/heads/", 11) != 0) {
402 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
403 branchname);
404 goto done;
406 ref = find_ref(&have_refs, branchname);
407 if (ref) {
408 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
409 "changes on %s will be sent to server",
410 branchname);
411 goto done;
415 RB_FOREACH(pe, got_pathlist_head, tag_names) {
416 const char *tagname = pe->path;
417 if (strncmp(tagname, "refs/tags/", 10) != 0) {
418 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
419 err = got_error_from_errno("asprintf");
420 goto done;
422 } else {
423 if ((s = strdup(pe->path)) == NULL) {
424 err = got_error_from_errno("strdup");
425 goto done;
428 err = insert_sendable_ref(&have_refs, s, s, repo);
429 if (err) {
430 if (err->code != GOT_ERR_REF_DUP_ENTRY)
431 goto done;
432 err = NULL;
433 free(s);
435 s = NULL;
438 if (RB_EMPTY(&have_refs) && RB_EMPTY(delete_branches)) {
439 err = got_error(GOT_ERR_SEND_EMPTY);
440 goto done;
443 packfd = got_opentempfd();
444 if (packfd == -1) {
445 err = got_error_from_errno("got_opentempfd");
446 goto done;
449 delta_cache = got_opentemp();
450 if (delta_cache == NULL) {
451 err = got_error_from_errno("got_opentemp");
452 goto done;
455 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
456 err = got_error_from_errno("socketpair");
457 goto done;
460 sendpid = fork();
461 if (sendpid == -1) {
462 err = got_error_from_errno("fork");
463 goto done;
464 } else if (sendpid == 0) {
465 got_privsep_exec_child(imsg_sendfds,
466 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
469 if (close(imsg_sendfds[1]) == -1) {
470 err = got_error_from_errno("close");
471 goto done;
473 imsg_sendfds[1] = -1;
474 if (imsgbuf_init(&sendibuf, imsg_sendfds[0]) == -1) {
475 err = got_error_from_errno("imsgbuf_init");
476 goto done;
478 imsgbuf_allow_fdpass(&sendibuf);
479 nsendfd = dup(sendfd);
480 if (nsendfd == -1) {
481 err = got_error_from_errno("dup");
482 goto done;
486 * Prepare the array of our object IDs which
487 * will be needed for generating a pack file.
489 RB_FOREACH(pe, got_pathlist_head, &have_refs) {
490 struct got_object_id *id = pe->data;
492 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
493 if (err)
494 goto done;
495 our_ids[nours] = id;
496 nours++;
499 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
500 delete_branches, verbosity);
501 if (err)
502 goto done;
503 nsendfd = -1;
505 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
506 if (err)
507 goto done;
509 * Process references reported by the server.
510 * Push appropriate object IDs onto the "their IDs" array.
511 * This array will be used to exclude objects which already
512 * exist on the server from our pack file.
514 RB_FOREACH(pe, got_pathlist_head, &their_refs) {
515 const char *refname = pe->path;
516 struct got_object_id *their_id = pe->data;
517 int have_their_id;
518 struct got_object *obj;
519 struct got_pathlist_entry *my_ref = NULL;
520 int is_tag = 0;
522 /* Don't blindly trust the server to send us valid names. */
523 if (!got_ref_name_is_valid(refname))
524 continue;
526 if (strncmp(refname, "refs/tags/", 10) == 0)
527 is_tag = 1;
529 * Find out whether this is a reference we want to upload.
530 * Otherwise we can still use this reference as a hint to
531 * avoid uploading any objects the server already has.
533 my_ref = find_ref(&have_refs, refname);
534 if (my_ref) {
535 struct got_object_id *my_id = my_ref->data;
536 if (got_object_id_cmp(my_id, their_id) != 0) {
537 if (!overwrite_refs && is_tag) {
538 err = got_error_fmt(
539 GOT_ERR_SEND_TAG_EXISTS,
540 "%s", refname);
541 goto done;
543 refs_to_send++;
547 /* Check if their object exists locally. */
548 err = got_object_open(&obj, repo, their_id);
549 if (err) {
550 if (err->code != GOT_ERR_NO_OBJ)
551 goto done;
552 if (!overwrite_refs && my_ref != NULL) {
553 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
554 "%s", refname);
555 goto done;
557 have_their_id = 0;
558 } else {
559 got_object_close(obj);
560 have_their_id = 1;
563 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
564 if (err)
565 goto done;
567 if (have_their_id) {
568 /* Enforce linear ancestry if required. */
569 if (!overwrite_refs && my_ref && !is_tag) {
570 struct got_object_id *my_id = my_ref->data;
571 err = check_common_ancestry(refname, my_id,
572 their_id, repo, cancel_cb, cancel_arg);
573 if (err)
574 goto done;
576 /* Exclude any objects reachable via their ID. */
577 their_ids[ntheirs] = their_id;
578 ntheirs++;
579 } else if (!is_tag) {
580 char *remote_refname;
581 struct got_reference *ref;
583 * Exclude any objects which exist on the server
584 * according to a locally cached remote reference.
586 err = get_remote_refname(&remote_refname,
587 remote_name, refname);
588 if (err)
589 goto done;
590 err = got_ref_open(&ref, repo, remote_refname, 0);
591 free(remote_refname);
592 if (err) {
593 if (err->code != GOT_ERR_NOT_REF)
594 goto done;
595 err = NULL;
596 } else {
597 err = got_ref_resolve(&their_ids[ntheirs],
598 repo, ref);
599 got_ref_close(ref);
600 if (err)
601 goto done;
602 ntheirs++;
607 /* Account for any new references we are going to upload. */
608 RB_FOREACH(pe, got_pathlist_head, &have_refs) {
609 const char *refname = pe->path;
610 if (find_ref(&their_refs, refname) == NULL)
611 refs_to_send++;
614 /* Account for any existing references we are going to delete. */
615 RB_FOREACH(pe, got_pathlist_head, delete_branches) {
616 const char *branchname = pe->path;
617 if (find_ref(&their_refs, branchname))
618 refs_to_delete++;
621 if (refs_to_send == 0 && refs_to_delete == 0) {
622 got_privsep_send_stop(imsg_sendfds[0]);
623 goto done;
626 if (refs_to_send > 0) {
627 struct got_ratelimit rl;
628 got_ratelimit_init(&rl, 0, 500);
629 memset(&ppa, 0, sizeof(ppa));
630 ppa.progress_cb = progress_cb;
631 ppa.progress_arg = progress_arg;
632 ppa.sendfd = sendfd;
633 err = got_pack_create(&packhash, packfd, delta_cache,
634 their_ids, ntheirs, our_ids, nours, repo, 0, 1, 0,
635 pack_progress, &ppa, &rl, cancel_cb, cancel_arg);
636 if (err)
637 goto done;
639 npackfd = dup(packfd);
640 if (npackfd == -1) {
641 err = got_error_from_errno("dup");
642 goto done;
644 err = got_privsep_send_packfd(&sendibuf, npackfd);
645 if (err != NULL)
646 goto done;
647 npackfd = -1;
648 } else {
649 err = got_privsep_send_packfd(&sendibuf, -1);
650 if (err != NULL)
651 goto done;
654 while (!done) {
655 int success = 0;
656 char *refname = NULL;
657 char *errmsg = NULL;
659 if (cancel_cb) {
660 err = (*cancel_cb)(cancel_arg);
661 if (err)
662 goto done;
664 err = got_privsep_recv_send_progress(&done, &bytes_sent,
665 &success, &refname, &errmsg, &sendibuf);
666 if (err)
667 goto done;
668 if (refname && got_ref_name_is_valid(refname) && success &&
669 strncmp(refname, "refs/tags/", 10) != 0) {
670 struct got_pathlist_entry *my_ref;
672 * The server has accepted our changes.
673 * Update our reference in refs/remotes/ accordingly.
675 my_ref = find_ref(&have_refs, refname);
676 if (my_ref) {
677 err = update_remote_ref(my_ref, remote_name,
678 repo);
679 if (err)
680 goto done;
683 if (refname != NULL ||
684 bytes_sent_cur != bytes_sent) {
685 err = progress_cb(progress_arg, ppa.ncolored,
686 ppa.nfound, ppa.ntrees, ppa.packfile_size,
687 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
688 ppa.nobj_written, bytes_sent,
689 refname, errmsg, success);
690 if (err) {
691 free(refname);
692 free(errmsg);
693 goto done;
695 bytes_sent_cur = bytes_sent;
697 free(refname);
698 free(errmsg);
700 done:
701 if (sendibuf.w)
702 imsgbuf_clear(&sendibuf);
703 if (sendpid != -1) {
704 if (err)
705 got_privsep_send_stop(imsg_sendfds[0]);
706 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
707 err = got_error_from_errno("waitpid");
709 if (imsg_sendfds[0] != -1 && close(imsg_sendfds[0]) == -1 && err == NULL)
710 err = got_error_from_errno("close");
711 if (imsg_sendfds[1] != -1 && close(imsg_sendfds[1]) == -1 && err == NULL)
712 err = got_error_from_errno("close");
713 if (packfd != -1 && close(packfd) == -1 && err == NULL)
714 err = got_error_from_errno("close");
715 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
716 err = got_error_from_errno("fclose");
717 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
718 err = got_error_from_errno("close");
719 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
720 err = got_error_from_errno("close");
722 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
723 got_pathlist_free(&their_refs, GOT_PATHLIST_FREE_ALL);
725 * Object ids are owned by have_refs/their_refs and are already freed;
726 * Only the arrays must be freed.
728 free(our_ids);
729 free(their_ids);
730 free(s);
731 return err;