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 "got_compat.h"
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/queue.h>
24 #include <sys/uio.h>
25 #include <sys/socket.h>
26 #include <sys/wait.h>
27 #include <sys/resource.h>
28 #include <sys/socket.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 <unistd.h>
38 #include <zlib.h>
39 #include <ctype.h>
40 #include <limits.h>
41 #include <time.h>
43 #include "got_error.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_path.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
49 #include "got_object.h"
50 #include "got_opentemp.h"
51 #include "got_send.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"
69 #ifndef nitems
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 #endif
73 #ifndef ssizeof
74 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
75 #endif
77 #ifndef MIN
78 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
79 #endif
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)
84 {
85 const struct got_error *err = NULL;
87 *sendpid = -1;
88 *sendfd = -1;
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,
95 GOT_DIAL_CMD_SEND);
96 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
97 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
98 else
99 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
100 return err;
103 struct pack_progress_arg {
104 got_send_progress_cb progress_cb;
105 void *progress_arg;
107 int ncolored;
108 int nfound;
109 int ntrees;
110 off_t packfile_size;
111 int ncommits;
112 int nobj_total;
113 int nobj_deltify;
114 int nobj_written;
115 };
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,
120 int nobj_written)
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);
128 if (err)
129 return err;
131 a->ncolored= ncolored;
132 a->nfound = nfound;
133 a->ntrees = ntrees;
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;
139 return NULL;
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;
149 int obj_type;
151 err = got_ref_open(&ref, repo, refname, 0);
152 if (err)
153 return err;
155 if (got_ref_is_symbolic(ref)) {
156 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
157 "cannot send symbolic reference %s", refname);
158 goto done;
161 err = got_ref_resolve(&id, repo, ref);
162 if (err)
163 goto done;
164 err = got_object_get_type(&obj_type, repo, id);
165 if (err)
166 goto done;
167 switch (obj_type) {
168 case GOT_OBJ_TYPE_COMMIT:
169 case GOT_OBJ_TYPE_TAG:
170 break;
171 default:
172 err = got_error_fmt(GOT_ERR_OBJ_TYPE," cannot send %s",
173 refname);
174 goto done;
177 err = got_pathlist_insert(NULL, refs, refname, id);
178 done:
179 if (ref)
180 got_ref_close(ref);
181 if (err)
182 free(id);
183 return err;
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;
193 int obj_type;
195 err = got_object_get_type(&obj_type, repo, their_id);
196 if (err)
197 return err;
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);
204 if (err)
205 return err;
206 if (yca_id == NULL)
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);
212 free(yca_id);
213 return err;
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;
222 if (*nalloc >= n)
223 return NULL;
225 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
226 sizeof(struct got_object_id));
227 if (new == NULL)
228 return got_error_from_errno("recallocarray");
230 *ids = new;
231 *nalloc += alloc_chunksz;
232 return NULL;
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) {
243 return pe;
247 return NULL;
250 static const struct got_error *
251 get_remote_refname(char **remote_refname, const char *remote_name,
252 const char *refname)
254 if (strncmp(refname, "refs/", 5) == 0)
255 refname += 5;
256 if (strncmp(refname, "heads/", 6) == 0)
257 refname += 6;
259 if (asprintf(remote_refname, "refs/remotes/%s/%s",
260 remote_name, refname) == -1)
261 return got_error_from_errno("asprintf");
263 return NULL;
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;
275 int ref_locked = 0;
277 err = get_remote_refname(&remote_refname, remote_name, refname);
278 if (err)
279 goto done;
281 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
282 if (err) {
283 if (err->code != GOT_ERR_NOT_REF)
284 goto done;
285 err = got_ref_alloc(&ref, remote_refname, my_id);
286 if (err)
287 goto done;
288 } else {
289 ref_locked = 1;
290 err = got_ref_change_ref(ref, my_id);
291 if (err)
292 goto done;
295 err = got_ref_write(ref, repo);
296 done:
297 if (ref) {
298 if (ref_locked) {
299 unlock_err = got_ref_unlock(ref);
300 if (unlock_err && err == NULL)
301 err = unlock_err;
303 got_ref_close(ref);
305 free(remote_refname);
306 return err;
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)
317 int imsg_sendfds[2];
318 int npackfd = -1, nsendfd = -1;
319 int sendstatus, done = 0;
320 const struct got_error *err;
321 struct imsgbuf sendibuf;
322 pid_t sendpid = -1;
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];
334 int packfd = -1;
335 FILE *delta_cache = NULL;
336 char *s = 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");
346 goto done;
348 } else {
349 if ((s = strdup(branchname)) == NULL) {
350 err = got_error_from_errno("strdup");
351 goto done;
354 err = insert_sendable_ref(&have_refs, s, repo);
355 if (err)
356 goto done;
357 s = NULL;
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",
365 branchname);
366 goto done;
368 ref = find_ref(&have_refs, branchname);
369 if (ref) {
370 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
371 "changes on %s will be sent to server",
372 branchname);
373 goto done;
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");
382 goto done;
384 } else {
385 if ((s = strdup(pe->path)) == NULL) {
386 err = got_error_from_errno("strdup");
387 goto done;
390 err = insert_sendable_ref(&have_refs, s, repo);
391 if (err)
392 goto done;
393 s = NULL;
396 if (TAILQ_EMPTY(&have_refs) && TAILQ_EMPTY(delete_branches)) {
397 err = got_error(GOT_ERR_SEND_EMPTY);
398 goto done;
401 packfd = got_opentempfd();
402 if (packfd == -1) {
403 err = got_error_from_errno("got_opentempfd");
404 goto done;
407 delta_cache = got_opentemp();
408 if (delta_cache == NULL) {
409 err = got_error_from_errno("got_opentemp");
410 goto done;
413 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
414 err = got_error_from_errno("socketpair");
415 goto done;
418 sendpid = fork();
419 if (sendpid == -1) {
420 err = got_error_from_errno("fork");
421 goto done;
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");
429 goto done;
431 imsg_init(&sendibuf, imsg_sendfds[0]);
432 nsendfd = dup(sendfd);
433 if (nsendfd == -1) {
434 err = got_error_from_errno("dup");
435 goto done;
438 /*
439 * Prepare the array of our object IDs which
440 * will be needed for generating a pack file.
441 */
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);
446 if (err)
447 goto done;
448 our_ids[nours] = id;
449 nours++;
452 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
453 delete_branches, verbosity);
454 if (err)
455 goto done;
456 nsendfd = -1;
458 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
459 if (err)
460 goto done;
462 /*
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.
467 */
468 TAILQ_FOREACH(pe, &their_refs, entry) {
469 const char *refname = pe->path;
470 struct got_object_id *their_id = pe->data;
471 int have_their_id;
472 struct got_object *obj;
473 struct got_pathlist_entry *my_ref = NULL;
474 int is_tag = 0;
476 /* Don't blindly trust the server to send us valid names. */
477 if (!got_ref_name_is_valid(refname))
478 continue;
480 if (strncmp(refname, "refs/tags/", 10) == 0)
481 is_tag = 1;
482 /*
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.
486 */
487 my_ref = find_ref(&have_refs, refname);
488 if (my_ref) {
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) {
492 err = got_error_fmt(
493 GOT_ERR_SEND_TAG_EXISTS,
494 "%s", refname);
495 goto done;
497 refs_to_send++;
501 /* Check if their object exists locally. */
502 err = got_object_open(&obj, repo, their_id);
503 if (err) {
504 if (err->code != GOT_ERR_NO_OBJ)
505 goto done;
506 if (!overwrite_refs && my_ref != NULL) {
507 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
508 "%s", refname);
509 goto done;
511 have_their_id = 0;
512 } else {
513 got_object_close(obj);
514 have_their_id = 1;
517 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
518 if (err)
519 goto done;
521 if (have_their_id) {
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);
527 if (err)
528 goto done;
530 /* Exclude any objects reachable via their ID. */
531 their_ids[ntheirs] = their_id;
532 ntheirs++;
533 } else if (!is_tag) {
534 char *remote_refname;
535 struct got_reference *ref;
536 /*
537 * Exclude any objects which exist on the server
538 * according to a locally cached remote reference.
539 */
540 err = get_remote_refname(&remote_refname,
541 remote_name, refname);
542 if (err)
543 goto done;
544 err = got_ref_open(&ref, repo, remote_refname, 0);
545 free(remote_refname);
546 if (err) {
547 if (err->code != GOT_ERR_NOT_REF)
548 goto done;
549 } else {
550 err = got_ref_resolve(&their_ids[ntheirs],
551 repo, ref);
552 got_ref_close(ref);
553 if (err)
554 goto done;
555 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)
564 refs_to_send++;
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))
571 refs_to_delete++;
574 if (refs_to_send == 0 && refs_to_delete == 0) {
575 got_privsep_send_stop(imsg_sendfds[0]);
576 goto done;
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);
588 if (err)
589 goto done;
591 npackfd = dup(packfd);
592 if (npackfd == -1) {
593 err = got_error_from_errno("dup");
594 goto done;
596 err = got_privsep_send_packfd(&sendibuf, npackfd);
597 if (err != NULL)
598 goto done;
599 npackfd = -1;
600 } else {
601 err = got_privsep_send_packfd(&sendibuf, -1);
602 if (err != NULL)
603 goto done;
606 while (!done) {
607 int success = 0;
608 char *refname = NULL;
609 char *errmsg = NULL;
611 if (cancel_cb) {
612 err = (*cancel_cb)(cancel_arg);
613 if (err)
614 goto done;
616 err = got_privsep_recv_send_progress(&done, &bytes_sent,
617 &success, &refname, &errmsg, &sendibuf);
618 if (err)
619 goto done;
620 if (refname && got_ref_name_is_valid(refname) && success &&
621 strncmp(refname, "refs/tags/", 10) != 0) {
622 struct got_pathlist_entry *my_ref;
623 /*
624 * The server has accepted our changes.
625 * Update our reference in refs/remotes/ accordingly.
626 */
627 my_ref = find_ref(&have_refs, refname);
628 if (my_ref) {
629 err = update_remote_ref(my_ref, remote_name,
630 repo);
631 if (err)
632 goto done;
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);
642 if (err) {
643 free(refname);
644 free(errmsg);
645 goto done;
647 bytes_sent_cur = bytes_sent;
649 free(refname);
650 free(errmsg);
652 done:
653 if (sendpid != -1) {
654 if (err)
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);
670 /*
671 * Object ids are owned by have_refs/their_refs and are already freed;
672 * Only the arrays must be freed.
673 */
674 free(our_ids);
675 free(their_ids);
676 free(s);
677 return err;