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>
23 #include <sys/queue.h>
25 #include <sys/socket.h>
27 #include <sys/resource.h>
28 #include <sys/socket.h>
43 #include "got_error.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
49 #include "got_object.h"
50 #include "got_opentemp.h"
52 #include "got_repository_admin.h"
53 #include "got_commit_graph.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_inflate.h"
57 #include "got_lib_object.h"
58 #include "got_lib_object_parse.h"
59 #include "got_lib_object_create.h"
60 #include "got_lib_pack.h"
61 #include "got_lib_hash.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"
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
74 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
78 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
81 const struct got_error *
82 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
83 const char *host, const char *port, const char *server_path, int verbosity)
85 const struct got_error *err = NULL;
90 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
91 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
92 GOT_DIAL_CMD_SEND, verbosity);
93 else if (strcmp(proto, "git") == 0)
94 err = got_dial_git(sendfd, host, port, server_path,
96 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
97 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
99 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
103 struct pack_progress_arg {
104 got_send_progress_cb progress_cb;
117 static const struct got_error *
118 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
119 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
122 const struct got_error *err;
123 struct pack_progress_arg *a = arg;
125 err = a->progress_cb(a->progress_arg, ncolored, nfound, ntrees,
126 packfile_size, ncommits, nobj_total, nobj_deltify,
127 nobj_written, 0, NULL, NULL, 0);
131 a->ncolored= ncolored;
134 a->packfile_size = packfile_size;
135 a->ncommits = ncommits;
136 a->nobj_total = nobj_total;
137 a->nobj_deltify = nobj_deltify;
138 a->nobj_written = nobj_written;
142 static const struct got_error *
143 insert_sendable_ref(struct got_pathlist_head *refs, const char *refname,
144 struct got_repository *repo)
146 const struct got_error *err;
147 struct got_reference *ref;
148 struct got_object_id *id = NULL;
151 err = got_ref_open(&ref, repo, refname, 0);
155 if (got_ref_is_symbolic(ref)) {
156 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
157 "cannot send symbolic reference %s", refname);
161 err = got_ref_resolve(&id, repo, ref);
164 err = got_object_get_type(&obj_type, repo, id);
168 case GOT_OBJ_TYPE_COMMIT:
169 case GOT_OBJ_TYPE_TAG:
172 err = got_error_fmt(GOT_ERR_OBJ_TYPE," cannot send %s",
177 err = got_pathlist_insert(NULL, refs, refname, id);
186 static const struct got_error *
187 check_common_ancestry(const char *refname, struct got_object_id *my_id,
188 struct got_object_id *their_id, struct got_repository *repo,
189 got_cancel_cb cancel_cb, void *cancel_arg)
191 const struct got_error *err = NULL;
192 struct got_object_id *yca_id;
195 err = got_object_get_type(&obj_type, repo, their_id);
198 if (obj_type != GOT_OBJ_TYPE_COMMIT)
199 return got_error_fmt(GOT_ERR_OBJ_TYPE,
200 "bad object type on server for %s", refname);
202 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
203 my_id, their_id, 0, repo, cancel_cb, cancel_arg);
207 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
209 if (got_object_id_cmp(their_id, yca_id) != 0)
210 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
216 static const struct got_error *
217 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
219 struct got_object_id **new;
220 const size_t alloc_chunksz = 256;
225 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
226 sizeof(struct got_object_id));
228 return got_error_from_errno("recallocarray");
231 *nalloc += alloc_chunksz;
235 static struct got_pathlist_entry *
236 find_ref(struct got_pathlist_head *refs, const char *refname)
238 struct got_pathlist_entry *pe;
240 TAILQ_FOREACH(pe, refs, entry) {
241 if (got_path_cmp(pe->path, refname, strlen(pe->path),
242 strlen(refname)) == 0) {
250 static const struct got_error *
251 get_remote_refname(char **remote_refname, const char *remote_name,
254 if (strncmp(refname, "refs/", 5) == 0)
256 if (strncmp(refname, "heads/", 6) == 0)
259 if (asprintf(remote_refname, "refs/remotes/%s/%s",
260 remote_name, refname) == -1)
261 return got_error_from_errno("asprintf");
266 static const struct got_error *
267 update_remote_ref(struct got_pathlist_entry *my_ref, const char *remote_name,
268 struct got_repository *repo)
270 const struct got_error *err, *unlock_err;
271 const char *refname = my_ref->path;
272 struct got_object_id *my_id = my_ref->data;
273 struct got_reference *ref = NULL;
274 char *remote_refname = NULL;
277 err = get_remote_refname(&remote_refname, remote_name, refname);
281 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
283 if (err->code != GOT_ERR_NOT_REF)
285 err = got_ref_alloc(&ref, remote_refname, my_id);
290 err = got_ref_change_ref(ref, my_id);
295 err = got_ref_write(ref, repo);
299 unlock_err = got_ref_unlock(ref);
300 if (unlock_err && err == NULL)
305 free(remote_refname);
309 const struct got_error*
310 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
311 struct got_pathlist_head *tag_names,
312 struct got_pathlist_head *delete_branches,
313 int verbosity, int overwrite_refs, int sendfd,
314 struct got_repository *repo, got_send_progress_cb progress_cb,
315 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
318 int npackfd = -1, nsendfd = -1;
319 int sendstatus, done = 0;
320 const struct got_error *err;
321 struct imsgbuf sendibuf;
323 struct got_pathlist_head have_refs;
324 struct got_pathlist_head their_refs;
325 struct got_pathlist_entry *pe;
326 struct got_object_id **our_ids = NULL;
327 struct got_object_id **their_ids = NULL;
328 int nours = 0, ntheirs = 0;
329 size_t nalloc_ours = 0, nalloc_theirs = 0;
330 int refs_to_send = 0, refs_to_delete = 0;
331 off_t bytes_sent = 0, bytes_sent_cur = 0;
332 struct pack_progress_arg ppa;
333 uint8_t packsha1[SHA1_DIGEST_LENGTH];
335 FILE *delta_cache = NULL;
338 TAILQ_INIT(&have_refs);
339 TAILQ_INIT(&their_refs);
341 TAILQ_FOREACH(pe, branch_names, entry) {
342 const char *branchname = pe->path;
343 if (strncmp(branchname, "refs/heads/", 11) != 0) {
344 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
345 err = got_error_from_errno("asprintf");
349 if ((s = strdup(branchname)) == NULL) {
350 err = got_error_from_errno("strdup");
354 err = insert_sendable_ref(&have_refs, s, repo);
360 TAILQ_FOREACH(pe, delete_branches, entry) {
361 const char *branchname = pe->path;
362 struct got_pathlist_entry *ref;
363 if (strncmp(branchname, "refs/heads/", 11) != 0) {
364 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
368 ref = find_ref(&have_refs, branchname);
370 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
371 "changes on %s will be sent to server",
377 TAILQ_FOREACH(pe, tag_names, entry) {
378 const char *tagname = pe->path;
379 if (strncmp(tagname, "refs/tags/", 10) != 0) {
380 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
381 err = got_error_from_errno("asprintf");
385 if ((s = strdup(pe->path)) == NULL) {
386 err = got_error_from_errno("strdup");
390 err = insert_sendable_ref(&have_refs, s, repo);
396 if (TAILQ_EMPTY(&have_refs) && TAILQ_EMPTY(delete_branches)) {
397 err = got_error(GOT_ERR_SEND_EMPTY);
401 packfd = got_opentempfd();
403 err = got_error_from_errno("got_opentempfd");
407 delta_cache = got_opentemp();
408 if (delta_cache == NULL) {
409 err = got_error_from_errno("got_opentemp");
413 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
414 err = got_error_from_errno("socketpair");
420 err = got_error_from_errno("fork");
422 } else if (sendpid == 0){
423 got_privsep_exec_child(imsg_sendfds,
424 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
427 if (close(imsg_sendfds[1]) == -1) {
428 err = got_error_from_errno("close");
431 imsg_init(&sendibuf, imsg_sendfds[0]);
432 nsendfd = dup(sendfd);
434 err = got_error_from_errno("dup");
439 * Prepare the array of our object IDs which
440 * will be needed for generating a pack file.
442 TAILQ_FOREACH(pe, &have_refs, entry) {
443 struct got_object_id *id = pe->data;
445 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
452 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
453 delete_branches, verbosity);
458 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
463 * Process references reported by the server.
464 * Push appropriate object IDs onto the "their IDs" array.
465 * This array will be used to exclude objects which already
466 * exist on the server from our pack file.
468 TAILQ_FOREACH(pe, &their_refs, entry) {
469 const char *refname = pe->path;
470 struct got_object_id *their_id = pe->data;
472 struct got_object *obj;
473 struct got_pathlist_entry *my_ref = NULL;
476 /* Don't blindly trust the server to send us valid names. */
477 if (!got_ref_name_is_valid(refname))
480 if (strncmp(refname, "refs/tags/", 10) == 0)
483 * Find out whether this is a reference we want to upload.
484 * Otherwise we can still use this reference as a hint to
485 * avoid uploading any objects the server already has.
487 my_ref = find_ref(&have_refs, refname);
489 struct got_object_id *my_id = my_ref->data;
490 if (got_object_id_cmp(my_id, their_id) != 0) {
491 if (!overwrite_refs && is_tag) {
493 GOT_ERR_SEND_TAG_EXISTS,
501 /* Check if their object exists locally. */
502 err = got_object_open(&obj, repo, their_id);
504 if (err->code != GOT_ERR_NO_OBJ)
506 if (!overwrite_refs && my_ref != NULL) {
507 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
513 got_object_close(obj);
517 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
522 /* Enforce linear ancestry if required. */
523 if (!overwrite_refs && my_ref && !is_tag) {
524 struct got_object_id *my_id = my_ref->data;
525 err = check_common_ancestry(refname, my_id,
526 their_id, repo, cancel_cb, cancel_arg);
530 /* Exclude any objects reachable via their ID. */
531 their_ids[ntheirs] = their_id;
533 } else if (!is_tag) {
534 char *remote_refname;
535 struct got_reference *ref;
537 * Exclude any objects which exist on the server
538 * according to a locally cached remote reference.
540 err = get_remote_refname(&remote_refname,
541 remote_name, refname);
544 err = got_ref_open(&ref, repo, remote_refname, 0);
545 free(remote_refname);
547 if (err->code != GOT_ERR_NOT_REF)
550 err = got_ref_resolve(&their_ids[ntheirs],
560 /* Account for any new references we are going to upload. */
561 TAILQ_FOREACH(pe, &have_refs, entry) {
562 const char *refname = pe->path;
563 if (find_ref(&their_refs, refname) == NULL)
567 /* Account for any existing references we are going to delete. */
568 TAILQ_FOREACH(pe, delete_branches, entry) {
569 const char *branchname = pe->path;
570 if (find_ref(&their_refs, branchname))
574 if (refs_to_send == 0 && refs_to_delete == 0) {
575 got_privsep_send_stop(imsg_sendfds[0]);
579 if (refs_to_send > 0) {
580 struct got_ratelimit rl;
581 got_ratelimit_init(&rl, 0, 500);
582 memset(&ppa, 0, sizeof(ppa));
583 ppa.progress_cb = progress_cb;
584 ppa.progress_arg = progress_arg;
585 err = got_pack_create(packsha1, packfd, delta_cache,
586 their_ids, ntheirs, our_ids, nours, repo, 0, 1, 0,
587 pack_progress, &ppa, &rl, cancel_cb, cancel_arg);
591 npackfd = dup(packfd);
593 err = got_error_from_errno("dup");
596 err = got_privsep_send_packfd(&sendibuf, npackfd);
601 err = got_privsep_send_packfd(&sendibuf, -1);
608 char *refname = NULL;
612 err = (*cancel_cb)(cancel_arg);
616 err = got_privsep_recv_send_progress(&done, &bytes_sent,
617 &success, &refname, &errmsg, &sendibuf);
620 if (refname && got_ref_name_is_valid(refname) && success &&
621 strncmp(refname, "refs/tags/", 10) != 0) {
622 struct got_pathlist_entry *my_ref;
624 * The server has accepted our changes.
625 * Update our reference in refs/remotes/ accordingly.
627 my_ref = find_ref(&have_refs, refname);
629 err = update_remote_ref(my_ref, remote_name,
635 if (refname != NULL ||
636 bytes_sent_cur != bytes_sent) {
637 err = progress_cb(progress_arg, ppa.ncolored,
638 ppa.nfound, ppa.ntrees, ppa.packfile_size,
639 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
640 ppa.nobj_written, bytes_sent,
641 refname, errmsg, success);
647 bytes_sent_cur = bytes_sent;
655 got_privsep_send_stop(imsg_sendfds[0]);
656 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
657 err = got_error_from_errno("waitpid");
659 if (packfd != -1 && close(packfd) == -1 && err == NULL)
660 err = got_error_from_errno("close");
661 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
662 err = got_error_from_errno("fclose");
663 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
664 err = got_error_from_errno("close");
665 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
666 err = got_error_from_errno("close");
668 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
669 got_pathlist_free(&their_refs, GOT_PATHLIST_FREE_ALL);
671 * Object ids are owned by have_refs/their_refs and are already freed;
672 * Only the arrays must be freed.