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_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"
72 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
80 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
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
, int verbosity
)
87 const struct got_error
*err
= NULL
;
92 if (strcmp(proto
, "ssh") == 0 || strcmp(proto
, "git+ssh") == 0)
93 err
= got_dial_ssh(sendpid
, sendfd
, host
, port
, server_path
,
94 GOT_DIAL_CMD_SEND
, verbosity
);
95 else if (strcmp(proto
, "git") == 0)
96 err
= got_dial_git(sendfd
, host
, port
, server_path
,
98 else if (strcmp(proto
, "http") == 0 || strcmp(proto
, "git+http") == 0)
99 err
= got_error_path(proto
, GOT_ERR_NOT_IMPL
);
101 err
= got_error_path(proto
, GOT_ERR_BAD_PROTO
);
105 struct pack_progress_arg
{
106 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);
135 * Detect the server closing our connection while we are
136 * busy creating a pack file.
138 * XXX This should be a temporary workaround. A better fix would
139 * be to avoid use of an on-disk tempfile for pack file data.
140 * Instead we could stream pack file data to got-send-pack while
141 * the pack file is being generated. Write errors in got-send-pack
142 * would then automatically abort the creation of pack file data.
144 err
= got_poll_fd(a
->sendfd
, 0, 0);
145 if (err
&& err
->code
!= GOT_ERR_TIMEOUT
) {
146 if (err
->code
== GOT_ERR_EOF
) {
147 err
= got_error_msg(GOT_ERR_EOF
,
148 "server unexpectedly closed the connection");
153 a
->ncolored
= ncolored
;
156 a
->packfile_size
= packfile_size
;
157 a
->ncommits
= ncommits
;
158 a
->nobj_total
= nobj_total
;
159 a
->nobj_deltify
= nobj_deltify
;
160 a
->nobj_written
= nobj_written
;
164 static const struct got_error
*
165 insert_sendable_ref(struct got_pathlist_head
*refs
, const char *refname
,
166 const char *target_refname
, struct got_repository
*repo
)
168 const struct got_error
*err
;
169 struct got_reference
*ref
;
170 struct got_object_id
*id
= NULL
;
171 struct got_pathlist_entry
*new = NULL
;
174 err
= got_ref_open(&ref
, repo
, refname
, 0);
178 if (got_ref_is_symbolic(ref
)) {
179 err
= got_error_fmt(GOT_ERR_BAD_REF_TYPE
,
180 "cannot send symbolic reference %s", refname
);
184 err
= got_ref_resolve(&id
, repo
, ref
);
187 err
= got_object_get_type(&obj_type
, repo
, id
);
191 case GOT_OBJ_TYPE_COMMIT
:
192 case GOT_OBJ_TYPE_TAG
:
195 err
= got_error_fmt(GOT_ERR_OBJ_TYPE
, "cannot send %s",
200 err
= got_pathlist_insert(&new, refs
, target_refname
, id
);
201 if (new == NULL
&& err
== NULL
)
202 err
= got_error(GOT_ERR_REF_DUP_ENTRY
);
212 static const struct got_error
*
213 check_common_ancestry(const char *refname
, struct got_object_id
*my_id
,
214 struct got_object_id
*their_id
, struct got_repository
*repo
,
215 got_cancel_cb cancel_cb
, void *cancel_arg
)
217 const struct got_error
*err
= NULL
;
218 struct got_object_id
*yca_id
;
221 err
= got_object_get_type(&obj_type
, repo
, their_id
);
224 if (obj_type
!= GOT_OBJ_TYPE_COMMIT
)
225 return got_error_fmt(GOT_ERR_OBJ_TYPE
,
226 "bad object type on server for %s", refname
);
228 err
= got_commit_graph_find_youngest_common_ancestor(&yca_id
,
229 my_id
, their_id
, 0, 1, repo
, cancel_cb
, cancel_arg
);
233 return got_error_fmt(GOT_ERR_SEND_ANCESTRY
, "%s", refname
);
235 if (got_object_id_cmp(their_id
, yca_id
) != 0)
236 err
= got_error_fmt(GOT_ERR_SEND_ANCESTRY
, "%s", refname
);
242 static const struct got_error
*
243 realloc_ids(struct got_object_id
***ids
, size_t *nalloc
, size_t n
)
245 struct got_object_id
**new;
246 const size_t alloc_chunksz
= 256;
251 new = recallocarray(*ids
, *nalloc
, *nalloc
+ alloc_chunksz
,
252 sizeof(struct got_object_id
));
254 return got_error_from_errno("recallocarray");
257 *nalloc
+= alloc_chunksz
;
261 static struct got_pathlist_entry
*
262 find_ref(struct got_pathlist_head
*refs
, const char *refname
)
264 struct got_pathlist_entry
*pe
;
266 TAILQ_FOREACH(pe
, refs
, entry
) {
267 if (got_path_cmp(pe
->path
, refname
, strlen(pe
->path
),
268 strlen(refname
)) == 0) {
276 static const struct got_error
*
277 get_remote_refname(char **remote_refname
, const char *remote_name
,
280 if (strncmp(refname
, "refs/", 5) == 0)
282 if (strncmp(refname
, "heads/", 6) == 0)
285 if (asprintf(remote_refname
, "refs/remotes/%s/%s",
286 remote_name
, refname
) == -1)
287 return got_error_from_errno("asprintf");
292 static const struct got_error
*
293 update_remote_ref(struct got_pathlist_entry
*my_ref
, const char *remote_name
,
294 struct got_repository
*repo
)
296 const struct got_error
*err
, *unlock_err
;
297 const char *refname
= my_ref
->path
;
298 struct got_object_id
*my_id
= my_ref
->data
;
299 struct got_reference
*ref
= NULL
;
300 char *remote_refname
= NULL
;
303 err
= get_remote_refname(&remote_refname
, remote_name
, refname
);
307 err
= got_ref_open(&ref
, repo
, remote_refname
, 1 /* lock */);
309 if (err
->code
!= GOT_ERR_NOT_REF
)
311 err
= got_ref_alloc(&ref
, remote_refname
, my_id
);
316 err
= got_ref_change_ref(ref
, my_id
);
321 err
= got_ref_write(ref
, repo
);
325 unlock_err
= got_ref_unlock(ref
);
326 if (unlock_err
&& err
== NULL
)
331 free(remote_refname
);
335 const struct got_error
*
336 got_send_pack(const char *remote_name
, struct got_pathlist_head
*branch_names
,
337 struct got_pathlist_head
*tag_names
,
338 struct got_pathlist_head
*delete_branches
,
339 int verbosity
, int overwrite_refs
, int sendfd
,
340 struct got_repository
*repo
, got_send_progress_cb progress_cb
,
341 void *progress_arg
, got_cancel_cb cancel_cb
, void *cancel_arg
)
343 int imsg_sendfds
[2] = { -1, -1 };
344 int npackfd
= -1, nsendfd
= -1;
345 int sendstatus
, done
= 0;
346 const struct got_error
*err
;
347 struct imsgbuf sendibuf
;
349 struct got_pathlist_head have_refs
;
350 struct got_pathlist_head their_refs
;
351 struct got_pathlist_entry
*pe
;
352 struct got_object_id
**our_ids
= NULL
;
353 struct got_object_id
**their_ids
= NULL
;
354 int nours
= 0, ntheirs
= 0;
355 size_t nalloc_ours
= 0, nalloc_theirs
= 0;
356 int refs_to_send
= 0, refs_to_delete
= 0;
357 off_t bytes_sent
= 0, bytes_sent_cur
= 0;
358 struct pack_progress_arg ppa
;
359 struct got_object_id packhash
;
361 FILE *delta_cache
= NULL
;
364 TAILQ_INIT(&have_refs
);
365 TAILQ_INIT(&their_refs
);
367 if (got_repo_get_object_format(repo
) != GOT_HASH_SHA1
)
368 return got_error_fmt(GOT_ERR_NOT_IMPL
,
369 "sha256 object IDs unsupported in network protocol");
371 TAILQ_FOREACH(pe
, branch_names
, entry
) {
372 const char *branchname
= pe
->path
;
373 const char *targetname
= pe
->data
;
375 if (targetname
== NULL
)
376 targetname
= branchname
;
378 if (strncmp(targetname
, "refs/heads/", 11) != 0) {
379 if (asprintf(&s
, "refs/heads/%s", targetname
) == -1) {
380 err
= got_error_from_errno("asprintf");
384 if ((s
= strdup(targetname
)) == NULL
) {
385 err
= got_error_from_errno("strdup");
389 err
= insert_sendable_ref(&have_refs
, branchname
, s
, repo
);
391 if (err
->code
!= GOT_ERR_REF_DUP_ENTRY
)
399 TAILQ_FOREACH(pe
, delete_branches
, entry
) {
400 const char *branchname
= pe
->path
;
401 struct got_pathlist_entry
*ref
;
402 if (strncmp(branchname
, "refs/heads/", 11) != 0) {
403 err
= got_error_fmt(GOT_ERR_SEND_DELETE_REF
, "%s",
407 ref
= find_ref(&have_refs
, branchname
);
409 err
= got_error_fmt(GOT_ERR_SEND_DELETE_REF
,
410 "changes on %s will be sent to server",
416 TAILQ_FOREACH(pe
, tag_names
, entry
) {
417 const char *tagname
= pe
->path
;
418 if (strncmp(tagname
, "refs/tags/", 10) != 0) {
419 if (asprintf(&s
, "refs/tags/%s", tagname
) == -1) {
420 err
= got_error_from_errno("asprintf");
424 if ((s
= strdup(pe
->path
)) == NULL
) {
425 err
= got_error_from_errno("strdup");
429 err
= insert_sendable_ref(&have_refs
, s
, s
, repo
);
431 if (err
->code
!= GOT_ERR_REF_DUP_ENTRY
)
439 if (TAILQ_EMPTY(&have_refs
) && TAILQ_EMPTY(delete_branches
)) {
440 err
= got_error(GOT_ERR_SEND_EMPTY
);
444 packfd
= got_opentempfd();
446 err
= got_error_from_errno("got_opentempfd");
450 delta_cache
= got_opentemp();
451 if (delta_cache
== NULL
) {
452 err
= got_error_from_errno("got_opentemp");
456 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, imsg_sendfds
) == -1) {
457 err
= got_error_from_errno("socketpair");
463 err
= got_error_from_errno("fork");
465 } else if (sendpid
== 0) {
466 got_privsep_exec_child(imsg_sendfds
,
467 GOT_PATH_PROG_SEND_PACK
, got_repo_get_path(repo
));
470 if (close(imsg_sendfds
[1]) == -1) {
471 err
= got_error_from_errno("close");
474 imsg_sendfds
[1] = -1;
475 if (imsgbuf_init(&sendibuf
, imsg_sendfds
[0]) == -1) {
476 err
= got_error_from_errno("imsgbuf_init");
479 imsgbuf_allow_fdpass(&sendibuf
);
480 nsendfd
= dup(sendfd
);
482 err
= got_error_from_errno("dup");
487 * Prepare the array of our object IDs which
488 * will be needed for generating a pack file.
490 TAILQ_FOREACH(pe
, &have_refs
, entry
) {
491 struct got_object_id
*id
= pe
->data
;
493 err
= realloc_ids(&our_ids
, &nalloc_ours
, nours
+ 1);
500 err
= got_privsep_send_send_req(&sendibuf
, nsendfd
, &have_refs
,
501 delete_branches
, verbosity
);
506 err
= got_privsep_recv_send_remote_refs(&their_refs
, &sendibuf
);
510 * Process references reported by the server.
511 * Push appropriate object IDs onto the "their IDs" array.
512 * This array will be used to exclude objects which already
513 * exist on the server from our pack file.
515 TAILQ_FOREACH(pe
, &their_refs
, entry
) {
516 const char *refname
= pe
->path
;
517 struct got_object_id
*their_id
= pe
->data
;
519 struct got_object
*obj
;
520 struct got_pathlist_entry
*my_ref
= NULL
;
523 /* Don't blindly trust the server to send us valid names. */
524 if (!got_ref_name_is_valid(refname
))
527 if (strncmp(refname
, "refs/tags/", 10) == 0)
530 * Find out whether this is a reference we want to upload.
531 * Otherwise we can still use this reference as a hint to
532 * avoid uploading any objects the server already has.
534 my_ref
= find_ref(&have_refs
, refname
);
536 struct got_object_id
*my_id
= my_ref
->data
;
537 if (got_object_id_cmp(my_id
, their_id
) != 0) {
538 if (!overwrite_refs
&& is_tag
) {
540 GOT_ERR_SEND_TAG_EXISTS
,
548 /* Check if their object exists locally. */
549 err
= got_object_open(&obj
, repo
, their_id
);
551 if (err
->code
!= GOT_ERR_NO_OBJ
)
553 if (!overwrite_refs
&& my_ref
!= NULL
) {
554 err
= got_error_fmt(GOT_ERR_SEND_ANCESTRY
,
560 got_object_close(obj
);
564 err
= realloc_ids(&their_ids
, &nalloc_theirs
, ntheirs
+ 1);
569 /* Enforce linear ancestry if required. */
570 if (!overwrite_refs
&& my_ref
&& !is_tag
) {
571 struct got_object_id
*my_id
= my_ref
->data
;
572 err
= check_common_ancestry(refname
, my_id
,
573 their_id
, repo
, cancel_cb
, cancel_arg
);
577 /* Exclude any objects reachable via their ID. */
578 their_ids
[ntheirs
] = their_id
;
580 } else if (!is_tag
) {
581 char *remote_refname
;
582 struct got_reference
*ref
;
584 * Exclude any objects which exist on the server
585 * according to a locally cached remote reference.
587 err
= get_remote_refname(&remote_refname
,
588 remote_name
, refname
);
591 err
= got_ref_open(&ref
, repo
, remote_refname
, 0);
592 free(remote_refname
);
594 if (err
->code
!= GOT_ERR_NOT_REF
)
598 err
= got_ref_resolve(&their_ids
[ntheirs
],
608 /* Account for any new references we are going to upload. */
609 TAILQ_FOREACH(pe
, &have_refs
, entry
) {
610 const char *refname
= pe
->path
;
611 if (find_ref(&their_refs
, refname
) == NULL
)
615 /* Account for any existing references we are going to delete. */
616 TAILQ_FOREACH(pe
, delete_branches
, entry
) {
617 const char *branchname
= pe
->path
;
618 if (find_ref(&their_refs
, branchname
))
622 if (refs_to_send
== 0 && refs_to_delete
== 0) {
623 got_privsep_send_stop(imsg_sendfds
[0]);
627 if (refs_to_send
> 0) {
628 struct got_ratelimit rl
;
629 got_ratelimit_init(&rl
, 0, 500);
630 memset(&ppa
, 0, sizeof(ppa
));
631 ppa
.progress_cb
= progress_cb
;
632 ppa
.progress_arg
= progress_arg
;
634 err
= got_pack_create(&packhash
, packfd
, delta_cache
,
635 their_ids
, ntheirs
, our_ids
, nours
, repo
, 0, 1, 0,
636 pack_progress
, &ppa
, &rl
, cancel_cb
, cancel_arg
);
640 npackfd
= dup(packfd
);
642 err
= got_error_from_errno("dup");
645 err
= got_privsep_send_packfd(&sendibuf
, npackfd
);
650 err
= got_privsep_send_packfd(&sendibuf
, -1);
657 char *refname
= NULL
;
661 err
= (*cancel_cb
)(cancel_arg
);
665 err
= got_privsep_recv_send_progress(&done
, &bytes_sent
,
666 &success
, &refname
, &errmsg
, &sendibuf
);
669 if (refname
&& got_ref_name_is_valid(refname
) && success
&&
670 strncmp(refname
, "refs/tags/", 10) != 0) {
671 struct got_pathlist_entry
*my_ref
;
673 * The server has accepted our changes.
674 * Update our reference in refs/remotes/ accordingly.
676 my_ref
= find_ref(&have_refs
, refname
);
678 err
= update_remote_ref(my_ref
, remote_name
,
684 if (refname
!= NULL
||
685 bytes_sent_cur
!= bytes_sent
) {
686 err
= progress_cb(progress_arg
, ppa
.ncolored
,
687 ppa
.nfound
, ppa
.ntrees
, ppa
.packfile_size
,
688 ppa
.ncommits
, ppa
.nobj_total
, ppa
.nobj_deltify
,
689 ppa
.nobj_written
, bytes_sent
,
690 refname
, errmsg
, success
);
696 bytes_sent_cur
= bytes_sent
;
704 got_privsep_send_stop(imsg_sendfds
[0]);
705 if (waitpid(sendpid
, &sendstatus
, 0) == -1 && err
== NULL
)
706 err
= got_error_from_errno("waitpid");
708 if (imsg_sendfds
[0] != -1 && close(imsg_sendfds
[0]) == -1 && err
== NULL
)
709 err
= got_error_from_errno("close");
710 if (imsg_sendfds
[1] != -1 && close(imsg_sendfds
[1]) == -1 && err
== NULL
)
711 err
= got_error_from_errno("close");
712 if (packfd
!= -1 && close(packfd
) == -1 && err
== NULL
)
713 err
= got_error_from_errno("close");
714 if (delta_cache
&& fclose(delta_cache
) == EOF
&& err
== NULL
)
715 err
= got_error_from_errno("fclose");
716 if (nsendfd
!= -1 && close(nsendfd
) == -1 && err
== NULL
)
717 err
= got_error_from_errno("close");
718 if (npackfd
!= -1 && close(npackfd
) == -1 && err
== NULL
)
719 err
= got_error_from_errno("close");
721 got_pathlist_free(&have_refs
, GOT_PATHLIST_FREE_ALL
);
722 got_pathlist_free(&their_refs
, GOT_PATHLIST_FREE_ALL
);
724 * Object ids are owned by have_refs/their_refs and are already freed;
725 * Only the arrays must be freed.