2 * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
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.
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.
18 #include <sys/types.h>
20 #include <sys/queue.h>
23 #include <sys/socket.h>
25 #include <sys/resource.h>
26 #include <sys/socket.h>
46 #include "got_error.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_cancel.h"
51 #include "got_worktree.h"
52 #include "got_object.h"
53 #include "got_opentemp.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_hash.h"
65 #include "got_lib_privsep.h"
66 #include "got_lib_object_cache.h"
67 #include "got_lib_repository.h"
68 #include "got_lib_ratelimit.h"
69 #include "got_lib_pack_create.h"
70 #include "got_lib_dial.h"
73 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
77 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
81 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
84 const struct got_error *
85 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
86 const char *host, const char *port, const char *server_path, int verbosity)
88 const struct got_error *err = NULL;
93 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
94 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
95 GOT_DIAL_CMD_SEND, verbosity);
96 else if (strcmp(proto, "git") == 0)
97 err = got_dial_git(sendfd, host, port, server_path,
99 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
100 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
102 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
106 struct pack_progress_arg {
107 got_send_progress_cb progress_cb;
120 static const struct got_error *
121 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
122 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
125 const struct got_error *err;
126 struct pack_progress_arg *a = arg;
128 err = a->progress_cb(a->progress_arg, ncolored, nfound, ntrees,
129 packfile_size, ncommits, nobj_total, nobj_deltify,
130 nobj_written, 0, NULL, NULL, 0);
134 a->ncolored= ncolored;
137 a->packfile_size = packfile_size;
138 a->ncommits = ncommits;
139 a->nobj_total = nobj_total;
140 a->nobj_deltify = nobj_deltify;
141 a->nobj_written = nobj_written;
145 static const struct got_error *
146 insert_ref(struct got_reflist_head *refs, const char *refname,
147 struct got_repository *repo)
149 const struct got_error *err;
150 struct got_reference *ref;
151 struct got_reflist_entry *new;
153 err = got_ref_open(&ref, repo, refname, 0);
157 err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
158 if (err || new == NULL /* duplicate */)
164 static const struct got_error *
165 check_common_ancestry(const char *refname, struct got_object_id *my_id,
166 struct got_object_id *their_id, struct got_repository *repo,
167 got_cancel_cb cancel_cb, void *cancel_arg)
169 const struct got_error *err = NULL;
170 struct got_object_id *yca_id;
173 err = got_object_get_type(&obj_type, repo, their_id);
176 if (obj_type != GOT_OBJ_TYPE_COMMIT)
177 return got_error_fmt(GOT_ERR_OBJ_TYPE,
178 "bad object type on server for %s", refname);
180 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
181 my_id, their_id, 0, repo, cancel_cb, cancel_arg);
185 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
187 if (got_object_id_cmp(their_id, yca_id) != 0)
188 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
194 static const struct got_error *
195 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
197 struct got_object_id **new;
198 const size_t alloc_chunksz = 256;
203 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
204 sizeof(struct got_object_id));
206 return got_error_from_errno("recallocarray");
209 *nalloc += alloc_chunksz;
213 static struct got_reference *
214 find_ref(struct got_reflist_head *refs, const char *refname)
216 struct got_reflist_entry *re;
218 TAILQ_FOREACH(re, refs, entry) {
219 if (got_path_cmp(got_ref_get_name(re->ref), refname,
220 strlen(got_ref_get_name(re->ref)),
221 strlen(refname)) == 0) {
229 static struct got_pathlist_entry *
230 find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
232 struct got_pathlist_entry *pe;
234 TAILQ_FOREACH(pe, their_refs, entry) {
235 const char *their_refname = pe->path;
236 if (got_path_cmp(their_refname, refname,
237 strlen(their_refname), strlen(refname)) == 0) {
245 static const struct got_error *
246 get_remote_refname(char **remote_refname, const char *remote_name,
249 if (strncmp(refname, "refs/", 5) == 0)
251 if (strncmp(refname, "heads/", 6) == 0)
254 if (asprintf(remote_refname, "refs/remotes/%s/%s",
255 remote_name, refname) == -1)
256 return got_error_from_errno("asprintf");
261 static const struct got_error *
262 update_remote_ref(struct got_reference *my_ref, const char *remote_name,
263 struct got_repository *repo)
265 const struct got_error *err, *unlock_err;
266 struct got_object_id *my_id;
267 struct got_reference *ref = NULL;
268 char *remote_refname = NULL;
271 err = got_ref_resolve(&my_id, repo, my_ref);
275 err = get_remote_refname(&remote_refname, remote_name,
276 got_ref_get_name(my_ref));
280 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
282 if (err->code != GOT_ERR_NOT_REF)
284 err = got_ref_alloc(&ref, remote_refname, my_id);
289 err = got_ref_change_ref(ref, my_id);
294 err = got_ref_write(ref, repo);
298 unlock_err = got_ref_unlock(ref);
299 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_reflist_head refs;
324 struct got_pathlist_head have_refs;
325 struct got_pathlist_head their_refs;
326 struct got_pathlist_entry *pe;
327 struct got_reflist_entry *re;
328 struct got_object_id **our_ids = NULL;
329 struct got_object_id **their_ids = NULL;
330 int i, nours = 0, ntheirs = 0;
331 size_t nalloc_ours = 0, nalloc_theirs = 0;
332 int refs_to_send = 0, refs_to_delete = 0;
333 off_t bytes_sent = 0, bytes_sent_cur = 0;
334 struct pack_progress_arg ppa;
335 uint8_t packsha1[SHA1_DIGEST_LENGTH];
337 FILE *delta_cache = NULL;
340 TAILQ_INIT(&have_refs);
341 TAILQ_INIT(&their_refs);
343 TAILQ_FOREACH(pe, branch_names, entry) {
344 const char *branchname = pe->path;
345 if (strncmp(branchname, "refs/heads/", 11) != 0) {
347 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
348 err = got_error_from_errno("asprintf");
351 err = insert_ref(&refs, s, repo);
354 err = insert_ref(&refs, branchname, repo);
360 TAILQ_FOREACH(pe, delete_branches, entry) {
361 const char *branchname = pe->path;
362 struct got_reference *ref;
363 if (strncmp(branchname, "refs/heads/", 11) != 0) {
364 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
368 ref = find_ref(&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) {
381 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
382 err = got_error_from_errno("asprintf");
385 err = insert_ref(&refs, s, repo);
388 err = insert_ref(&refs, tagname, repo);
394 if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
395 err = got_error(GOT_ERR_SEND_EMPTY);
399 TAILQ_FOREACH(re, &refs, entry) {
400 struct got_object_id *id;
403 if (got_ref_is_symbolic(re->ref)) {
404 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
405 "cannot send symbolic reference %s",
406 got_ref_get_name(re->ref));
410 err = got_ref_resolve(&id, repo, re->ref);
413 err = got_object_get_type(&obj_type, repo, id);
418 case GOT_OBJ_TYPE_COMMIT:
419 case GOT_OBJ_TYPE_TAG:
422 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
423 "cannot send %s", got_ref_get_name(re->ref));
428 packfd = got_opentempfd();
430 err = got_error_from_errno("got_opentempfd");
434 delta_cache = got_opentemp();
435 if (delta_cache == NULL) {
436 err = got_error_from_errno("got_opentemp");
440 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
441 err = got_error_from_errno("socketpair");
447 err = got_error_from_errno("fork");
449 } else if (sendpid == 0){
450 got_privsep_exec_child(imsg_sendfds,
451 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
454 if (close(imsg_sendfds[1]) == -1) {
455 err = got_error_from_errno("close");
458 imsg_init(&sendibuf, imsg_sendfds[0]);
459 nsendfd = dup(sendfd);
461 err = got_error_from_errno("dup");
466 * Convert reflist to pathlist since the privsep layer
467 * is linked into helper programs which lack reference.c.
469 TAILQ_FOREACH(re, &refs, entry) {
470 struct got_object_id *id;
471 err = got_ref_resolve(&id, repo, re->ref);
474 err = got_pathlist_append(&have_refs,
475 got_ref_get_name(re->ref), id);
479 * Also prepare the array of our object IDs which
480 * will be needed for generating a pack file.
482 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
489 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
490 delete_branches, verbosity);
495 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
500 * Process references reported by the server.
501 * Push appropriate object IDs onto the "their IDs" array.
502 * This array will be used to exclude objects which already
503 * exist on the server from our pack file.
505 TAILQ_FOREACH(pe, &their_refs, entry) {
506 const char *refname = pe->path;
507 struct got_object_id *their_id = pe->data;
509 struct got_object *obj;
510 struct got_reference *my_ref = NULL;
513 /* Don't blindly trust the server to send us valid names. */
514 if (!got_ref_name_is_valid(refname))
517 if (strncmp(refname, "refs/tags/", 10) == 0)
520 * Find out whether this is a reference we want to upload.
521 * Otherwise we can still use this reference as a hint to
522 * avoid uploading any objects the server already has.
524 my_ref = find_ref(&refs, refname);
526 struct got_object_id *my_id;
527 err = got_ref_resolve(&my_id, repo, my_ref);
530 if (got_object_id_cmp(my_id, their_id) != 0) {
531 if (!overwrite_refs && is_tag) {
533 GOT_ERR_SEND_TAG_EXISTS,
543 /* Check if their object exists locally. */
544 err = got_object_open(&obj, repo, their_id);
546 if (err->code != GOT_ERR_NO_OBJ)
548 if (!overwrite_refs && my_ref != NULL) {
549 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
555 got_object_close(obj);
559 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
564 /* Enforce linear ancestry if required. */
565 if (!overwrite_refs && my_ref && !is_tag) {
566 struct got_object_id *my_id;
567 err = got_ref_resolve(&my_id, repo, my_ref);
570 err = check_common_ancestry(refname, my_id,
571 their_id, repo, cancel_cb, cancel_arg);
577 /* Exclude any objects reachable via their ID. */
578 their_ids[ntheirs] = got_object_id_dup(their_id);
579 if (their_ids[ntheirs] == NULL) {
580 err = got_error_from_errno("got_object_id_dup");
584 } else if (!is_tag) {
585 char *remote_refname;
586 struct got_reference *ref;
588 * Exclude any objects which exist on the server
589 * according to a locally cached remote reference.
591 err = get_remote_refname(&remote_refname,
592 remote_name, refname);
595 err = got_ref_open(&ref, repo, remote_refname, 0);
596 free(remote_refname);
598 if (err->code != GOT_ERR_NOT_REF)
601 err = got_ref_resolve(&their_ids[ntheirs],
611 /* Account for any new references we are going to upload. */
612 TAILQ_FOREACH(re, &refs, entry) {
613 if (find_their_ref(&their_refs,
614 got_ref_get_name(re->ref)) == NULL)
618 /* Account for any existing references we are going to delete. */
619 TAILQ_FOREACH(pe, delete_branches, entry) {
620 const char *branchname = pe->path;
621 if (find_their_ref(&their_refs, branchname))
625 if (refs_to_send == 0 && refs_to_delete == 0) {
626 got_privsep_send_stop(imsg_sendfds[0]);
630 if (refs_to_send > 0) {
631 struct got_ratelimit rl;
632 got_ratelimit_init(&rl, 0, 500);
633 memset(&ppa, 0, sizeof(ppa));
634 ppa.progress_cb = progress_cb;
635 ppa.progress_arg = progress_arg;
636 err = got_pack_create(packsha1, packfd, delta_cache,
637 their_ids, ntheirs, our_ids, nours, repo, 0, 1, 0,
638 pack_progress, &ppa, &rl, cancel_cb, cancel_arg);
642 npackfd = dup(packfd);
644 err = got_error_from_errno("dup");
647 err = got_privsep_send_packfd(&sendibuf, npackfd);
652 err = got_privsep_send_packfd(&sendibuf, -1);
659 char *refname = NULL;
663 err = (*cancel_cb)(cancel_arg);
667 err = got_privsep_recv_send_progress(&done, &bytes_sent,
668 &success, &refname, &errmsg, &sendibuf);
671 if (refname && got_ref_name_is_valid(refname) && success &&
672 strncmp(refname, "refs/tags/", 10) != 0) {
673 struct got_reference *my_ref;
675 * The server has accepted our changes.
676 * Update our reference in refs/remotes/ accordingly.
678 my_ref = find_ref(&refs, refname);
680 err = update_remote_ref(my_ref, remote_name,
686 if (refname != NULL ||
687 bytes_sent_cur != bytes_sent) {
688 err = progress_cb(progress_arg, ppa.ncolored,
689 ppa.nfound, ppa.ntrees, ppa.packfile_size,
690 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
691 ppa.nobj_written, bytes_sent,
692 refname, errmsg, success);
698 bytes_sent_cur = bytes_sent;
706 got_privsep_send_stop(imsg_sendfds[0]);
707 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
708 err = got_error_from_errno("waitpid");
710 if (packfd != -1 && close(packfd) == -1 && err == NULL)
711 err = got_error_from_errno("close");
712 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
713 err = got_error_from_errno("fclose");
714 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
715 err = got_error_from_errno("close");
716 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
717 err = got_error_from_errno("close");
719 got_ref_list_free(&refs);
720 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_NONE);
721 got_pathlist_free(&their_refs, GOT_PATHLIST_FREE_NONE);
722 for (i = 0; i < nours; i++)
725 for (i = 0; i < ntheirs; i++)