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"
74 #ifndef nitems
75 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 #endif
78 #ifndef ssizeof
79 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
80 #endif
82 #ifndef MIN
83 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
84 #endif
86 const struct got_error *
87 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
88 const char *host, const char *port, const char *server_path, int verbosity)
89 {
90 const struct got_error *err = NULL;
92 *sendpid = -1;
93 *sendfd = -1;
95 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
96 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
97 GOT_DIAL_CMD_SEND, verbosity);
98 else if (strcmp(proto, "git") == 0)
99 err = got_dial_git(sendfd, host, port, server_path,
100 GOT_DIAL_CMD_SEND);
101 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
102 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
103 else
104 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
105 return err;
108 struct pack_progress_arg {
109 got_send_progress_cb progress_cb;
110 void *progress_arg;
112 int ncolored;
113 int nfound;
114 int ntrees;
115 off_t packfile_size;
116 int ncommits;
117 int nobj_total;
118 int nobj_deltify;
119 int nobj_written;
120 };
122 static const struct got_error *
123 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
124 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
125 int nobj_written)
127 const struct got_error *err;
128 struct pack_progress_arg *a = arg;
130 err = a->progress_cb(a->progress_arg, ncolored, nfound, ntrees,
131 packfile_size, ncommits, nobj_total, nobj_deltify,
132 nobj_written, 0, NULL, NULL, 0);
133 if (err)
134 return err;
136 a->ncolored= ncolored;
137 a->nfound = nfound;
138 a->ntrees = ntrees;
139 a->packfile_size = packfile_size;
140 a->ncommits = ncommits;
141 a->nobj_total = nobj_total;
142 a->nobj_deltify = nobj_deltify;
143 a->nobj_written = nobj_written;
144 return NULL;
147 static const struct got_error *
148 insert_sendable_ref(struct got_pathlist_head *refs, const char *refname,
149 const char *target_refname, struct got_repository *repo)
151 const struct got_error *err;
152 struct got_reference *ref;
153 struct got_object_id *id = NULL;
154 int obj_type;
156 err = got_ref_open(&ref, repo, refname, 0);
157 if (err)
158 return err;
160 if (got_ref_is_symbolic(ref)) {
161 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
162 "cannot send symbolic reference %s", refname);
163 goto done;
166 err = got_ref_resolve(&id, repo, ref);
167 if (err)
168 goto done;
169 err = got_object_get_type(&obj_type, repo, id);
170 if (err)
171 goto done;
172 switch (obj_type) {
173 case GOT_OBJ_TYPE_COMMIT:
174 case GOT_OBJ_TYPE_TAG:
175 break;
176 default:
177 err = got_error_fmt(GOT_ERR_OBJ_TYPE," cannot send %s",
178 refname);
179 goto done;
182 err = got_pathlist_insert(NULL, refs, target_refname, id);
183 done:
184 if (ref)
185 got_ref_close(ref);
186 if (err)
187 free(id);
188 return err;
191 static const struct got_error *
192 check_common_ancestry(const char *refname, struct got_object_id *my_id,
193 struct got_object_id *their_id, struct got_repository *repo,
194 got_cancel_cb cancel_cb, void *cancel_arg)
196 const struct got_error *err = NULL;
197 struct got_object_id *yca_id;
198 int obj_type;
200 err = got_object_get_type(&obj_type, repo, their_id);
201 if (err)
202 return err;
203 if (obj_type != GOT_OBJ_TYPE_COMMIT)
204 return got_error_fmt(GOT_ERR_OBJ_TYPE,
205 "bad object type on server for %s", refname);
207 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
208 my_id, their_id, 0, repo, cancel_cb, cancel_arg);
209 if (err)
210 return err;
211 if (yca_id == NULL)
212 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
214 if (got_object_id_cmp(their_id, yca_id) != 0)
215 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
217 free(yca_id);
218 return err;
221 static const struct got_error *
222 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
224 struct got_object_id **new;
225 const size_t alloc_chunksz = 256;
227 if (*nalloc >= n)
228 return NULL;
230 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
231 sizeof(struct got_object_id));
232 if (new == NULL)
233 return got_error_from_errno("recallocarray");
235 *ids = new;
236 *nalloc += alloc_chunksz;
237 return NULL;
240 static struct got_pathlist_entry *
241 find_ref(struct got_pathlist_head *refs, const char *refname)
243 struct got_pathlist_entry *pe;
245 TAILQ_FOREACH(pe, refs, entry) {
246 if (got_path_cmp(pe->path, refname, strlen(pe->path),
247 strlen(refname)) == 0) {
248 return pe;
252 return NULL;
255 static const struct got_error *
256 get_remote_refname(char **remote_refname, const char *remote_name,
257 const char *refname)
259 if (strncmp(refname, "refs/", 5) == 0)
260 refname += 5;
261 if (strncmp(refname, "heads/", 6) == 0)
262 refname += 6;
264 if (asprintf(remote_refname, "refs/remotes/%s/%s",
265 remote_name, refname) == -1)
266 return got_error_from_errno("asprintf");
268 return NULL;
271 static const struct got_error *
272 update_remote_ref(struct got_pathlist_entry *my_ref, const char *remote_name,
273 struct got_repository *repo)
275 const struct got_error *err, *unlock_err;
276 const char *refname = my_ref->path;
277 struct got_object_id *my_id = my_ref->data;
278 struct got_reference *ref = NULL;
279 char *remote_refname = NULL;
280 int ref_locked = 0;
282 err = get_remote_refname(&remote_refname, remote_name, refname);
283 if (err)
284 goto done;
286 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
287 if (err) {
288 if (err->code != GOT_ERR_NOT_REF)
289 goto done;
290 err = got_ref_alloc(&ref, remote_refname, my_id);
291 if (err)
292 goto done;
293 } else {
294 ref_locked = 1;
295 err = got_ref_change_ref(ref, my_id);
296 if (err)
297 goto done;
300 err = got_ref_write(ref, repo);
301 done:
302 if (ref) {
303 if (ref_locked) {
304 unlock_err = got_ref_unlock(ref);
305 if (unlock_err && err == NULL)
306 err = unlock_err;
308 got_ref_close(ref);
310 free(remote_refname);
311 return err;
314 const struct got_error*
315 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
316 struct got_pathlist_head *tag_names,
317 struct got_pathlist_head *delete_branches,
318 int verbosity, int overwrite_refs, int sendfd,
319 struct got_repository *repo, got_send_progress_cb progress_cb,
320 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
322 int imsg_sendfds[2];
323 int npackfd = -1, nsendfd = -1;
324 int sendstatus, done = 0;
325 const struct got_error *err;
326 struct imsgbuf sendibuf;
327 pid_t sendpid = -1;
328 struct got_pathlist_head have_refs;
329 struct got_pathlist_head their_refs;
330 struct got_pathlist_entry *pe;
331 struct got_object_id **our_ids = NULL;
332 struct got_object_id **their_ids = NULL;
333 int nours = 0, ntheirs = 0;
334 size_t nalloc_ours = 0, nalloc_theirs = 0;
335 int refs_to_send = 0, refs_to_delete = 0;
336 off_t bytes_sent = 0, bytes_sent_cur = 0;
337 struct pack_progress_arg ppa;
338 uint8_t packsha1[SHA1_DIGEST_LENGTH];
339 int packfd = -1;
340 FILE *delta_cache = NULL;
341 char *s = NULL;
343 TAILQ_INIT(&have_refs);
344 TAILQ_INIT(&their_refs);
346 TAILQ_FOREACH(pe, branch_names, entry) {
347 const char *branchname = pe->path;
348 const char *targetname = pe->data;
350 if (targetname == NULL)
351 targetname = branchname;
353 if (strncmp(targetname, "refs/heads/", 11) != 0) {
354 if (asprintf(&s, "refs/heads/%s", targetname) == -1) {
355 err = got_error_from_errno("asprintf");
356 goto done;
358 } else {
359 if ((s = strdup(targetname)) == NULL) {
360 err = got_error_from_errno("strdup");
361 goto done;
364 err = insert_sendable_ref(&have_refs, branchname, s, repo);
365 if (err)
366 goto done;
367 s = NULL;
370 TAILQ_FOREACH(pe, delete_branches, entry) {
371 const char *branchname = pe->path;
372 struct got_pathlist_entry *ref;
373 if (strncmp(branchname, "refs/heads/", 11) != 0) {
374 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
375 branchname);
376 goto done;
378 ref = find_ref(&have_refs, branchname);
379 if (ref) {
380 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
381 "changes on %s will be sent to server",
382 branchname);
383 goto done;
387 TAILQ_FOREACH(pe, tag_names, entry) {
388 const char *tagname = pe->path;
389 if (strncmp(tagname, "refs/tags/", 10) != 0) {
390 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
391 err = got_error_from_errno("asprintf");
392 goto done;
394 } else {
395 if ((s = strdup(pe->path)) == NULL) {
396 err = got_error_from_errno("strdup");
397 goto done;
400 err = insert_sendable_ref(&have_refs, s, s, repo);
401 if (err)
402 goto done;
403 s = NULL;
406 if (TAILQ_EMPTY(&have_refs) && TAILQ_EMPTY(delete_branches)) {
407 err = got_error(GOT_ERR_SEND_EMPTY);
408 goto done;
411 packfd = got_opentempfd();
412 if (packfd == -1) {
413 err = got_error_from_errno("got_opentempfd");
414 goto done;
417 delta_cache = got_opentemp();
418 if (delta_cache == NULL) {
419 err = got_error_from_errno("got_opentemp");
420 goto done;
423 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
424 err = got_error_from_errno("socketpair");
425 goto done;
428 sendpid = fork();
429 if (sendpid == -1) {
430 err = got_error_from_errno("fork");
431 goto done;
432 } else if (sendpid == 0){
433 got_privsep_exec_child(imsg_sendfds,
434 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
437 if (close(imsg_sendfds[1]) == -1) {
438 err = got_error_from_errno("close");
439 goto done;
441 imsg_init(&sendibuf, imsg_sendfds[0]);
442 nsendfd = dup(sendfd);
443 if (nsendfd == -1) {
444 err = got_error_from_errno("dup");
445 goto done;
448 /*
449 * Prepare the array of our object IDs which
450 * will be needed for generating a pack file.
451 */
452 TAILQ_FOREACH(pe, &have_refs, entry) {
453 struct got_object_id *id = pe->data;
455 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
456 if (err)
457 goto done;
458 our_ids[nours] = id;
459 nours++;
462 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
463 delete_branches, verbosity);
464 if (err)
465 goto done;
466 nsendfd = -1;
468 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
469 if (err)
470 goto done;
471 /*
472 * Process references reported by the server.
473 * Push appropriate object IDs onto the "their IDs" array.
474 * This array will be used to exclude objects which already
475 * exist on the server from our pack file.
476 */
477 TAILQ_FOREACH(pe, &their_refs, entry) {
478 const char *refname = pe->path;
479 struct got_object_id *their_id = pe->data;
480 int have_their_id;
481 struct got_object *obj;
482 struct got_pathlist_entry *my_ref = NULL;
483 int is_tag = 0;
485 /* Don't blindly trust the server to send us valid names. */
486 if (!got_ref_name_is_valid(refname))
487 continue;
489 if (strncmp(refname, "refs/tags/", 10) == 0)
490 is_tag = 1;
491 /*
492 * Find out whether this is a reference we want to upload.
493 * Otherwise we can still use this reference as a hint to
494 * avoid uploading any objects the server already has.
495 */
496 my_ref = find_ref(&have_refs, refname);
497 if (my_ref) {
498 struct got_object_id *my_id = my_ref->data;
499 if (got_object_id_cmp(my_id, their_id) != 0) {
500 if (!overwrite_refs && is_tag) {
501 err = got_error_fmt(
502 GOT_ERR_SEND_TAG_EXISTS,
503 "%s", refname);
504 goto done;
506 refs_to_send++;
510 /* Check if their object exists locally. */
511 err = got_object_open(&obj, repo, their_id);
512 if (err) {
513 if (err->code != GOT_ERR_NO_OBJ)
514 goto done;
515 if (!overwrite_refs && my_ref != NULL) {
516 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
517 "%s", refname);
518 goto done;
520 have_their_id = 0;
521 } else {
522 got_object_close(obj);
523 have_their_id = 1;
526 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
527 if (err)
528 goto done;
530 if (have_their_id) {
531 /* Enforce linear ancestry if required. */
532 if (!overwrite_refs && my_ref && !is_tag) {
533 struct got_object_id *my_id = my_ref->data;
534 err = check_common_ancestry(refname, my_id,
535 their_id, repo, cancel_cb, cancel_arg);
536 if (err)
537 goto done;
539 /* Exclude any objects reachable via their ID. */
540 their_ids[ntheirs] = their_id;
541 ntheirs++;
542 } else if (!is_tag) {
543 char *remote_refname;
544 struct got_reference *ref;
545 /*
546 * Exclude any objects which exist on the server
547 * according to a locally cached remote reference.
548 */
549 err = get_remote_refname(&remote_refname,
550 remote_name, refname);
551 if (err)
552 goto done;
553 err = got_ref_open(&ref, repo, remote_refname, 0);
554 free(remote_refname);
555 if (err) {
556 if (err->code != GOT_ERR_NOT_REF)
557 goto done;
558 } else {
559 err = got_ref_resolve(&their_ids[ntheirs],
560 repo, ref);
561 got_ref_close(ref);
562 if (err)
563 goto done;
564 ntheirs++;
569 /* Account for any new references we are going to upload. */
570 TAILQ_FOREACH(pe, &have_refs, entry) {
571 const char *refname = pe->path;
572 if (find_ref(&their_refs, refname) == NULL)
573 refs_to_send++;
576 /* Account for any existing references we are going to delete. */
577 TAILQ_FOREACH(pe, delete_branches, entry) {
578 const char *branchname = pe->path;
579 if (find_ref(&their_refs, branchname))
580 refs_to_delete++;
583 if (refs_to_send == 0 && refs_to_delete == 0) {
584 got_privsep_send_stop(imsg_sendfds[0]);
585 goto done;
588 if (refs_to_send > 0) {
589 struct got_ratelimit rl;
590 got_ratelimit_init(&rl, 0, 500);
591 memset(&ppa, 0, sizeof(ppa));
592 ppa.progress_cb = progress_cb;
593 ppa.progress_arg = progress_arg;
594 err = got_pack_create(packsha1, packfd, delta_cache,
595 their_ids, ntheirs, our_ids, nours, repo, 0, 1, 0,
596 pack_progress, &ppa, &rl, cancel_cb, cancel_arg);
597 if (err)
598 goto done;
600 npackfd = dup(packfd);
601 if (npackfd == -1) {
602 err = got_error_from_errno("dup");
603 goto done;
605 err = got_privsep_send_packfd(&sendibuf, npackfd);
606 if (err != NULL)
607 goto done;
608 npackfd = -1;
609 } else {
610 err = got_privsep_send_packfd(&sendibuf, -1);
611 if (err != NULL)
612 goto done;
615 while (!done) {
616 int success = 0;
617 char *refname = NULL;
618 char *errmsg = NULL;
620 if (cancel_cb) {
621 err = (*cancel_cb)(cancel_arg);
622 if (err)
623 goto done;
625 err = got_privsep_recv_send_progress(&done, &bytes_sent,
626 &success, &refname, &errmsg, &sendibuf);
627 if (err)
628 goto done;
629 if (refname && got_ref_name_is_valid(refname) && success &&
630 strncmp(refname, "refs/tags/", 10) != 0) {
631 struct got_pathlist_entry *my_ref;
632 /*
633 * The server has accepted our changes.
634 * Update our reference in refs/remotes/ accordingly.
635 */
636 my_ref = find_ref(&have_refs, refname);
637 if (my_ref) {
638 err = update_remote_ref(my_ref, remote_name,
639 repo);
640 if (err)
641 goto done;
644 if (refname != NULL ||
645 bytes_sent_cur != bytes_sent) {
646 err = progress_cb(progress_arg, ppa.ncolored,
647 ppa.nfound, ppa.ntrees, ppa.packfile_size,
648 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
649 ppa.nobj_written, bytes_sent,
650 refname, errmsg, success);
651 if (err) {
652 free(refname);
653 free(errmsg);
654 goto done;
656 bytes_sent_cur = bytes_sent;
658 free(refname);
659 free(errmsg);
661 done:
662 if (sendpid != -1) {
663 if (err)
664 got_privsep_send_stop(imsg_sendfds[0]);
665 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
666 err = got_error_from_errno("waitpid");
668 if (packfd != -1 && close(packfd) == -1 && err == NULL)
669 err = got_error_from_errno("close");
670 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
671 err = got_error_from_errno("fclose");
672 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
673 err = got_error_from_errno("close");
674 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
675 err = got_error_from_errno("close");
677 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
678 got_pathlist_free(&their_refs, GOT_PATHLIST_FREE_ALL);
679 /*
680 * Object ids are owned by have_refs/their_refs and are already freed;
681 * Only the arrays must be freed.
682 */
683 free(our_ids);
684 free(their_ids);
685 free(s);
686 return err;