Blob


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>
5 *
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.
9 *
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.
17 */
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/queue.h>
22 #include <sys/tree.h>
23 #include <sys/uio.h>
24 #include <sys/socket.h>
25 #include <sys/wait.h>
26 #include <sys/resource.h>
27 #include <sys/socket.h>
29 #include <endian.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 <sha1.h>
38 #include <sha2.h>
39 #include <unistd.h>
40 #include <zlib.h>
41 #include <ctype.h>
42 #include <limits.h>
43 #include <imsg.h>
44 #include <time.h>
45 #include <uuid.h>
47 #include "got_error.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
50 #include "got_path.h"
51 #include "got_cancel.h"
52 #include "got_worktree.h"
53 #include "got_object.h"
54 #include "got_opentemp.h"
55 #include "got_send.h"
56 #include "got_repository_admin.h"
57 #include "got_commit_graph.h"
59 #include "got_lib_delta.h"
60 #include "got_lib_inflate.h"
61 #include "got_lib_object.h"
62 #include "got_lib_object_parse.h"
63 #include "got_lib_object_create.h"
64 #include "got_lib_pack.h"
65 #include "got_lib_hash.h"
66 #include "got_lib_privsep.h"
67 #include "got_lib_object_cache.h"
68 #include "got_lib_repository.h"
69 #include "got_lib_ratelimit.h"
70 #include "got_lib_pack_create.h"
71 #include "got_lib_dial.h"
72 #include "got_lib_worktree_cvg.h"
73 #include "got_lib_poll.h"
75 #ifndef nitems
76 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
77 #endif
79 #ifndef ssizeof
80 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
81 #endif
83 #ifndef MIN
84 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
85 #endif
87 const struct got_error *
88 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
89 const char *host, const char *port, const char *server_path, int verbosity)
90 {
91 const struct got_error *err = NULL;
93 *sendpid = -1;
94 *sendfd = -1;
96 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
97 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
98 GOT_DIAL_CMD_SEND, verbosity);
99 else if (strcmp(proto, "git") == 0)
100 err = got_dial_git(sendfd, host, port, server_path,
101 GOT_DIAL_CMD_SEND);
102 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
103 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
104 else
105 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
106 return err;
109 struct pack_progress_arg {
110 got_send_progress_cb progress_cb;
111 void *progress_arg;
112 int sendfd;
114 int ncolored;
115 int nfound;
116 int ntrees;
117 off_t packfile_size;
118 int ncommits;
119 int nobj_total;
120 int nobj_deltify;
121 int nobj_written;
122 };
124 static const struct got_error *
125 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
126 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
127 int nobj_written)
129 const struct got_error *err;
130 struct pack_progress_arg *a = arg;
132 err = a->progress_cb(a->progress_arg, ncolored, nfound, ntrees,
133 packfile_size, ncommits, nobj_total, nobj_deltify,
134 nobj_written, 0, NULL, NULL, 0);
135 if (err)
136 return err;
138 /*
139 * Detect the server closing our connection while we are
140 * busy creating a pack file.
142 * XXX This should be a temporary workaround. A better fix would
143 * be to avoid use of an on-disk tempfile for pack file data.
144 * Instead we could stream pack file data to got-send-pack while
145 * the pack file is being generated. Write errors in got-send-pack
146 * would then automatically abort the creation of pack file data.
147 */
148 err = got_poll_fd(a->sendfd, 0, 0);
149 if (err && err->code != GOT_ERR_TIMEOUT) {
150 if (err->code == GOT_ERR_EOF) {
151 err = got_error_msg(GOT_ERR_EOF,
152 "server unexpectedly closed the connection");
154 return err;
157 a->ncolored= ncolored;
158 a->nfound = nfound;
159 a->ntrees = ntrees;
160 a->packfile_size = packfile_size;
161 a->ncommits = ncommits;
162 a->nobj_total = nobj_total;
163 a->nobj_deltify = nobj_deltify;
164 a->nobj_written = nobj_written;
165 return NULL;
168 static const struct got_error *
169 insert_sendable_ref(struct got_pathlist_head *refs, const char *refname,
170 const char *target_refname, struct got_repository *repo)
172 const struct got_error *err;
173 struct got_reference *ref;
174 struct got_object_id *id = NULL;
175 int obj_type;
177 err = got_ref_open(&ref, repo, refname, 0);
178 if (err)
179 return err;
181 if (got_ref_is_symbolic(ref)) {
182 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
183 "cannot send symbolic reference %s", refname);
184 goto done;
187 err = got_ref_resolve(&id, repo, ref);
188 if (err)
189 goto done;
190 err = got_object_get_type(&obj_type, repo, id);
191 if (err)
192 goto done;
193 switch (obj_type) {
194 case GOT_OBJ_TYPE_COMMIT:
195 case GOT_OBJ_TYPE_TAG:
196 break;
197 default:
198 err = got_error_fmt(GOT_ERR_OBJ_TYPE," cannot send %s",
199 refname);
200 goto done;
203 err = got_pathlist_insert(NULL, refs, target_refname, id);
204 done:
205 if (ref)
206 got_ref_close(ref);
207 if (err)
208 free(id);
209 return err;
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;
219 int obj_type;
221 err = got_object_get_type(&obj_type, repo, their_id);
222 if (err)
223 return err;
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, 0, repo, cancel_cb, cancel_arg);
230 if (err)
231 return err;
232 if (yca_id == NULL)
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);
238 free(yca_id);
239 return err;
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;
248 if (*nalloc >= n)
249 return NULL;
251 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
252 sizeof(struct got_object_id));
253 if (new == NULL)
254 return got_error_from_errno("recallocarray");
256 *ids = new;
257 *nalloc += alloc_chunksz;
258 return NULL;
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) {
269 return pe;
273 return NULL;
276 static const struct got_error *
277 get_remote_refname(char **remote_refname, const char *remote_name,
278 const char *refname)
280 if (strncmp(refname, "refs/", 5) == 0)
281 refname += 5;
282 if (strncmp(refname, "heads/", 6) == 0)
283 refname += 6;
285 if (asprintf(remote_refname, "refs/remotes/%s/%s",
286 remote_name, refname) == -1)
287 return got_error_from_errno("asprintf");
289 return NULL;
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;
301 int ref_locked = 0;
303 err = get_remote_refname(&remote_refname, remote_name, refname);
304 if (err)
305 goto done;
307 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
308 if (err) {
309 if (err->code != GOT_ERR_NOT_REF)
310 goto done;
311 err = got_ref_alloc(&ref, remote_refname, my_id);
312 if (err)
313 goto done;
314 } else {
315 ref_locked = 1;
316 err = got_ref_change_ref(ref, my_id);
317 if (err)
318 goto done;
321 err = got_ref_write(ref, repo);
322 done:
323 if (ref) {
324 if (ref_locked) {
325 unlock_err = got_ref_unlock(ref);
326 if (unlock_err && err == NULL)
327 err = unlock_err;
329 got_ref_close(ref);
331 free(remote_refname);
332 return err;
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];
344 int npackfd = -1, nsendfd = -1;
345 int sendstatus, done = 0;
346 const struct got_error *err;
347 struct imsgbuf sendibuf;
348 pid_t sendpid = -1;
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 uint8_t packsha1[SHA1_DIGEST_LENGTH];
360 int packfd = -1;
361 FILE *delta_cache = NULL;
362 char *s = NULL;
364 TAILQ_INIT(&have_refs);
365 TAILQ_INIT(&their_refs);
367 TAILQ_FOREACH(pe, branch_names, entry) {
368 const char *branchname = pe->path;
369 const char *targetname = pe->data;
371 if (targetname == NULL)
372 targetname = branchname;
374 if (strncmp(targetname, "refs/heads/", 11) != 0) {
375 if (asprintf(&s, "refs/heads/%s", targetname) == -1) {
376 err = got_error_from_errno("asprintf");
377 goto done;
379 } else {
380 if ((s = strdup(targetname)) == NULL) {
381 err = got_error_from_errno("strdup");
382 goto done;
385 err = insert_sendable_ref(&have_refs, branchname, s, repo);
386 if (err)
387 goto done;
388 s = NULL;
391 TAILQ_FOREACH(pe, delete_branches, entry) {
392 const char *branchname = pe->path;
393 struct got_pathlist_entry *ref;
394 if (strncmp(branchname, "refs/heads/", 11) != 0) {
395 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
396 branchname);
397 goto done;
399 ref = find_ref(&have_refs, branchname);
400 if (ref) {
401 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
402 "changes on %s will be sent to server",
403 branchname);
404 goto done;
408 TAILQ_FOREACH(pe, tag_names, entry) {
409 const char *tagname = pe->path;
410 if (strncmp(tagname, "refs/tags/", 10) != 0) {
411 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
412 err = got_error_from_errno("asprintf");
413 goto done;
415 } else {
416 if ((s = strdup(pe->path)) == NULL) {
417 err = got_error_from_errno("strdup");
418 goto done;
421 err = insert_sendable_ref(&have_refs, s, s, repo);
422 if (err)
423 goto done;
424 s = NULL;
427 if (TAILQ_EMPTY(&have_refs) && TAILQ_EMPTY(delete_branches)) {
428 err = got_error(GOT_ERR_SEND_EMPTY);
429 goto done;
432 packfd = got_opentempfd();
433 if (packfd == -1) {
434 err = got_error_from_errno("got_opentempfd");
435 goto done;
438 delta_cache = got_opentemp();
439 if (delta_cache == NULL) {
440 err = got_error_from_errno("got_opentemp");
441 goto done;
444 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
445 err = got_error_from_errno("socketpair");
446 goto done;
449 sendpid = fork();
450 if (sendpid == -1) {
451 err = got_error_from_errno("fork");
452 goto done;
453 } else if (sendpid == 0){
454 got_privsep_exec_child(imsg_sendfds,
455 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
458 if (close(imsg_sendfds[1]) == -1) {
459 err = got_error_from_errno("close");
460 goto done;
462 imsg_init(&sendibuf, imsg_sendfds[0]);
463 nsendfd = dup(sendfd);
464 if (nsendfd == -1) {
465 err = got_error_from_errno("dup");
466 goto done;
469 /*
470 * Prepare the array of our object IDs which
471 * will be needed for generating a pack file.
472 */
473 TAILQ_FOREACH(pe, &have_refs, entry) {
474 struct got_object_id *id = pe->data;
476 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
477 if (err)
478 goto done;
479 our_ids[nours] = id;
480 nours++;
483 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
484 delete_branches, verbosity);
485 if (err)
486 goto done;
487 nsendfd = -1;
489 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
490 if (err)
491 goto done;
492 /*
493 * Process references reported by the server.
494 * Push appropriate object IDs onto the "their IDs" array.
495 * This array will be used to exclude objects which already
496 * exist on the server from our pack file.
497 */
498 TAILQ_FOREACH(pe, &their_refs, entry) {
499 const char *refname = pe->path;
500 struct got_object_id *their_id = pe->data;
501 int have_their_id;
502 struct got_object *obj;
503 struct got_pathlist_entry *my_ref = NULL;
504 int is_tag = 0;
506 /* Don't blindly trust the server to send us valid names. */
507 if (!got_ref_name_is_valid(refname))
508 continue;
510 if (strncmp(refname, "refs/tags/", 10) == 0)
511 is_tag = 1;
512 /*
513 * Find out whether this is a reference we want to upload.
514 * Otherwise we can still use this reference as a hint to
515 * avoid uploading any objects the server already has.
516 */
517 my_ref = find_ref(&have_refs, refname);
518 if (my_ref) {
519 struct got_object_id *my_id = my_ref->data;
520 if (got_object_id_cmp(my_id, their_id) != 0) {
521 if (!overwrite_refs && is_tag) {
522 err = got_error_fmt(
523 GOT_ERR_SEND_TAG_EXISTS,
524 "%s", refname);
525 goto done;
527 refs_to_send++;
531 /* Check if their object exists locally. */
532 err = got_object_open(&obj, repo, their_id);
533 if (err) {
534 if (err->code != GOT_ERR_NO_OBJ)
535 goto done;
536 if (!overwrite_refs && my_ref != NULL) {
537 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
538 "%s", refname);
539 goto done;
541 have_their_id = 0;
542 } else {
543 got_object_close(obj);
544 have_their_id = 1;
547 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
548 if (err)
549 goto done;
551 if (have_their_id) {
552 /* Enforce linear ancestry if required. */
553 if (!overwrite_refs && my_ref && !is_tag) {
554 struct got_object_id *my_id = my_ref->data;
555 err = check_common_ancestry(refname, my_id,
556 their_id, repo, cancel_cb, cancel_arg);
557 if (err)
558 goto done;
560 /* Exclude any objects reachable via their ID. */
561 their_ids[ntheirs] = their_id;
562 ntheirs++;
563 } else if (!is_tag) {
564 char *remote_refname;
565 struct got_reference *ref;
566 /*
567 * Exclude any objects which exist on the server
568 * according to a locally cached remote reference.
569 */
570 err = get_remote_refname(&remote_refname,
571 remote_name, refname);
572 if (err)
573 goto done;
574 err = got_ref_open(&ref, repo, remote_refname, 0);
575 free(remote_refname);
576 if (err) {
577 if (err->code != GOT_ERR_NOT_REF)
578 goto done;
579 } else {
580 err = got_ref_resolve(&their_ids[ntheirs],
581 repo, ref);
582 got_ref_close(ref);
583 if (err)
584 goto done;
585 ntheirs++;
590 /* Account for any new references we are going to upload. */
591 TAILQ_FOREACH(pe, &have_refs, entry) {
592 const char *refname = pe->path;
593 if (find_ref(&their_refs, refname) == NULL)
594 refs_to_send++;
597 /* Account for any existing references we are going to delete. */
598 TAILQ_FOREACH(pe, delete_branches, entry) {
599 const char *branchname = pe->path;
600 if (find_ref(&their_refs, branchname))
601 refs_to_delete++;
604 if (refs_to_send == 0 && refs_to_delete == 0) {
605 got_privsep_send_stop(imsg_sendfds[0]);
606 goto done;
609 if (refs_to_send > 0) {
610 struct got_ratelimit rl;
611 got_ratelimit_init(&rl, 0, 500);
612 memset(&ppa, 0, sizeof(ppa));
613 ppa.progress_cb = progress_cb;
614 ppa.progress_arg = progress_arg;
615 ppa.sendfd = sendfd;
616 err = got_pack_create(packsha1, packfd, delta_cache,
617 their_ids, ntheirs, our_ids, nours, repo, 0, 1, 0,
618 pack_progress, &ppa, &rl, cancel_cb, cancel_arg);
619 if (err)
620 goto done;
622 npackfd = dup(packfd);
623 if (npackfd == -1) {
624 err = got_error_from_errno("dup");
625 goto done;
627 err = got_privsep_send_packfd(&sendibuf, npackfd);
628 if (err != NULL)
629 goto done;
630 npackfd = -1;
631 } else {
632 err = got_privsep_send_packfd(&sendibuf, -1);
633 if (err != NULL)
634 goto done;
637 while (!done) {
638 int success = 0;
639 char *refname = NULL;
640 char *errmsg = NULL;
642 if (cancel_cb) {
643 err = (*cancel_cb)(cancel_arg);
644 if (err)
645 goto done;
647 err = got_privsep_recv_send_progress(&done, &bytes_sent,
648 &success, &refname, &errmsg, &sendibuf);
649 if (err)
650 goto done;
651 if (refname && got_ref_name_is_valid(refname) && success &&
652 strncmp(refname, "refs/tags/", 10) != 0) {
653 struct got_pathlist_entry *my_ref;
654 /*
655 * The server has accepted our changes.
656 * Update our reference in refs/remotes/ accordingly.
657 */
658 my_ref = find_ref(&have_refs, refname);
659 if (my_ref) {
660 err = update_remote_ref(my_ref, remote_name,
661 repo);
662 if (err)
663 goto done;
666 if (refname != NULL ||
667 bytes_sent_cur != bytes_sent) {
668 err = progress_cb(progress_arg, ppa.ncolored,
669 ppa.nfound, ppa.ntrees, ppa.packfile_size,
670 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
671 ppa.nobj_written, bytes_sent,
672 refname, errmsg, success);
673 if (err) {
674 free(refname);
675 free(errmsg);
676 goto done;
678 bytes_sent_cur = bytes_sent;
680 free(refname);
681 free(errmsg);
683 done:
684 if (sendpid != -1) {
685 if (err)
686 got_privsep_send_stop(imsg_sendfds[0]);
687 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
688 err = got_error_from_errno("waitpid");
690 if (packfd != -1 && close(packfd) == -1 && err == NULL)
691 err = got_error_from_errno("close");
692 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
693 err = got_error_from_errno("fclose");
694 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
695 err = got_error_from_errno("close");
696 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
697 err = got_error_from_errno("close");
699 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
700 got_pathlist_free(&their_refs, GOT_PATHLIST_FREE_ALL);
701 /*
702 * Object ids are owned by have_refs/their_refs and are already freed;
703 * Only the arrays must be freed.
704 */
705 free(our_ids);
706 free(their_ids);
707 free(s);
708 return err;