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"
68 #include "got_lib_worktree_cvg.h"
70 #ifndef nitems
71 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
72 #endif
74 #ifndef ssizeof
75 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
76 #endif
78 #ifndef MIN
79 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
80 #endif
82 const struct got_error *
83 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
84 const char *host, const char *port, const char *server_path, int verbosity)
85 {
86 const struct got_error *err = NULL;
88 *sendpid = -1;
89 *sendfd = -1;
91 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
92 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
93 GOT_DIAL_CMD_SEND, verbosity);
94 else if (strcmp(proto, "git") == 0)
95 err = got_dial_git(sendfd, host, port, server_path,
96 GOT_DIAL_CMD_SEND);
97 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
98 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
99 else
100 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
101 return err;
104 struct pack_progress_arg {
105 got_send_progress_cb progress_cb;
106 void *progress_arg;
108 int ncolored;
109 int nfound;
110 int ntrees;
111 off_t packfile_size;
112 int ncommits;
113 int nobj_total;
114 int nobj_deltify;
115 int nobj_written;
116 };
118 static const struct got_error *
119 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
120 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
121 int nobj_written)
123 const struct got_error *err;
124 struct pack_progress_arg *a = arg;
126 err = a->progress_cb(a->progress_arg, ncolored, nfound, ntrees,
127 packfile_size, ncommits, nobj_total, nobj_deltify,
128 nobj_written, 0, NULL, NULL, 0);
129 if (err)
130 return err;
132 a->ncolored= ncolored;
133 a->nfound = nfound;
134 a->ntrees = ntrees;
135 a->packfile_size = packfile_size;
136 a->ncommits = ncommits;
137 a->nobj_total = nobj_total;
138 a->nobj_deltify = nobj_deltify;
139 a->nobj_written = nobj_written;
140 return NULL;
143 static const struct got_error *
144 insert_sendable_ref(struct got_pathlist_head *refs, const char *refname,
145 const char *target_refname, struct got_repository *repo)
147 const struct got_error *err;
148 struct got_reference *ref;
149 struct got_object_id *id = NULL;
150 int obj_type;
152 err = got_ref_open(&ref, repo, refname, 0);
153 if (err)
154 return err;
156 if (got_ref_is_symbolic(ref)) {
157 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
158 "cannot send symbolic reference %s", refname);
159 goto done;
162 err = got_ref_resolve(&id, repo, ref);
163 if (err)
164 goto done;
165 err = got_object_get_type(&obj_type, repo, id);
166 if (err)
167 goto done;
168 switch (obj_type) {
169 case GOT_OBJ_TYPE_COMMIT:
170 case GOT_OBJ_TYPE_TAG:
171 break;
172 default:
173 err = got_error_fmt(GOT_ERR_OBJ_TYPE," cannot send %s",
174 refname);
175 goto done;
178 err = got_pathlist_insert(NULL, refs, target_refname, id);
179 done:
180 if (ref)
181 got_ref_close(ref);
182 if (err)
183 free(id);
184 return err;
187 static const struct got_error *
188 check_common_ancestry(const char *refname, struct got_object_id *my_id,
189 struct got_object_id *their_id, struct got_repository *repo,
190 got_cancel_cb cancel_cb, void *cancel_arg)
192 const struct got_error *err = NULL;
193 struct got_object_id *yca_id;
194 int obj_type;
196 err = got_object_get_type(&obj_type, repo, their_id);
197 if (err)
198 return err;
199 if (obj_type != GOT_OBJ_TYPE_COMMIT)
200 return got_error_fmt(GOT_ERR_OBJ_TYPE,
201 "bad object type on server for %s", refname);
203 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
204 my_id, their_id, 0, repo, cancel_cb, cancel_arg);
205 if (err)
206 return err;
207 if (yca_id == NULL)
208 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
210 if (got_object_id_cmp(their_id, yca_id) != 0)
211 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
213 free(yca_id);
214 return err;
217 static const struct got_error *
218 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
220 struct got_object_id **new;
221 const size_t alloc_chunksz = 256;
223 if (*nalloc >= n)
224 return NULL;
226 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
227 sizeof(struct got_object_id));
228 if (new == NULL)
229 return got_error_from_errno("recallocarray");
231 *ids = new;
232 *nalloc += alloc_chunksz;
233 return NULL;
236 static struct got_pathlist_entry *
237 find_ref(struct got_pathlist_head *refs, const char *refname)
239 struct got_pathlist_entry *pe;
241 TAILQ_FOREACH(pe, refs, entry) {
242 if (got_path_cmp(pe->path, refname, strlen(pe->path),
243 strlen(refname)) == 0) {
244 return pe;
248 return NULL;
251 static const struct got_error *
252 get_remote_refname(char **remote_refname, const char *remote_name,
253 const char *refname)
255 if (strncmp(refname, "refs/", 5) == 0)
256 refname += 5;
257 if (strncmp(refname, "heads/", 6) == 0)
258 refname += 6;
260 if (asprintf(remote_refname, "refs/remotes/%s/%s",
261 remote_name, refname) == -1)
262 return got_error_from_errno("asprintf");
264 return NULL;
267 static const struct got_error *
268 update_remote_ref(struct got_pathlist_entry *my_ref, const char *remote_name,
269 struct got_repository *repo)
271 const struct got_error *err, *unlock_err;
272 const char *refname = my_ref->path;
273 struct got_object_id *my_id = my_ref->data;
274 struct got_reference *ref = NULL;
275 char *remote_refname = NULL;
276 int ref_locked = 0;
278 err = get_remote_refname(&remote_refname, remote_name, refname);
279 if (err)
280 goto done;
282 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
283 if (err) {
284 if (err->code != GOT_ERR_NOT_REF)
285 goto done;
286 err = got_ref_alloc(&ref, remote_refname, my_id);
287 if (err)
288 goto done;
289 } else {
290 ref_locked = 1;
291 err = got_ref_change_ref(ref, my_id);
292 if (err)
293 goto done;
296 err = got_ref_write(ref, repo);
297 done:
298 if (ref) {
299 if (ref_locked) {
300 unlock_err = got_ref_unlock(ref);
301 if (unlock_err && err == NULL)
302 err = unlock_err;
304 got_ref_close(ref);
306 free(remote_refname);
307 return err;
310 const struct got_error*
311 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
312 struct got_pathlist_head *tag_names,
313 struct got_pathlist_head *delete_branches,
314 int verbosity, int overwrite_refs, int sendfd,
315 struct got_repository *repo, got_send_progress_cb progress_cb,
316 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
318 int imsg_sendfds[2];
319 int npackfd = -1, nsendfd = -1;
320 int sendstatus, done = 0;
321 const struct got_error *err;
322 struct imsgbuf sendibuf;
323 pid_t sendpid = -1;
324 struct got_pathlist_head have_refs;
325 struct got_pathlist_head their_refs;
326 struct got_pathlist_entry *pe;
327 struct got_object_id **our_ids = NULL;
328 struct got_object_id **their_ids = NULL;
329 int nours = 0, ntheirs = 0;
330 size_t nalloc_ours = 0, nalloc_theirs = 0;
331 int refs_to_send = 0, refs_to_delete = 0;
332 off_t bytes_sent = 0, bytes_sent_cur = 0;
333 struct pack_progress_arg ppa;
334 uint8_t packsha1[SHA1_DIGEST_LENGTH];
335 int packfd = -1;
336 FILE *delta_cache = NULL;
337 char *s = NULL;
339 TAILQ_INIT(&have_refs);
340 TAILQ_INIT(&their_refs);
342 TAILQ_FOREACH(pe, branch_names, entry) {
343 const char *branchname = pe->path;
344 const char *targetname = pe->data;
346 if (targetname == NULL)
347 targetname = branchname;
349 if (strncmp(targetname, "refs/heads/", 11) != 0) {
350 if (asprintf(&s, "refs/heads/%s", targetname) == -1) {
351 err = got_error_from_errno("asprintf");
352 goto done;
354 } else {
355 if ((s = strdup(targetname)) == NULL) {
356 err = got_error_from_errno("strdup");
357 goto done;
360 err = insert_sendable_ref(&have_refs, branchname, s, repo);
361 if (err)
362 goto done;
363 s = NULL;
366 TAILQ_FOREACH(pe, delete_branches, entry) {
367 const char *branchname = pe->path;
368 struct got_pathlist_entry *ref;
369 if (strncmp(branchname, "refs/heads/", 11) != 0) {
370 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
371 branchname);
372 goto done;
374 ref = find_ref(&have_refs, branchname);
375 if (ref) {
376 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
377 "changes on %s will be sent to server",
378 branchname);
379 goto done;
383 TAILQ_FOREACH(pe, tag_names, entry) {
384 const char *tagname = pe->path;
385 if (strncmp(tagname, "refs/tags/", 10) != 0) {
386 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
387 err = got_error_from_errno("asprintf");
388 goto done;
390 } else {
391 if ((s = strdup(pe->path)) == NULL) {
392 err = got_error_from_errno("strdup");
393 goto done;
396 err = insert_sendable_ref(&have_refs, s, s, repo);
397 if (err)
398 goto done;
399 s = NULL;
402 if (TAILQ_EMPTY(&have_refs) && TAILQ_EMPTY(delete_branches)) {
403 err = got_error(GOT_ERR_SEND_EMPTY);
404 goto done;
407 packfd = got_opentempfd();
408 if (packfd == -1) {
409 err = got_error_from_errno("got_opentempfd");
410 goto done;
413 delta_cache = got_opentemp();
414 if (delta_cache == NULL) {
415 err = got_error_from_errno("got_opentemp");
416 goto done;
419 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
420 err = got_error_from_errno("socketpair");
421 goto done;
424 sendpid = fork();
425 if (sendpid == -1) {
426 err = got_error_from_errno("fork");
427 goto done;
428 } else if (sendpid == 0){
429 got_privsep_exec_child(imsg_sendfds,
430 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
433 if (close(imsg_sendfds[1]) == -1) {
434 err = got_error_from_errno("close");
435 goto done;
437 imsg_init(&sendibuf, imsg_sendfds[0]);
438 nsendfd = dup(sendfd);
439 if (nsendfd == -1) {
440 err = got_error_from_errno("dup");
441 goto done;
444 /*
445 * Prepare the array of our object IDs which
446 * will be needed for generating a pack file.
447 */
448 TAILQ_FOREACH(pe, &have_refs, entry) {
449 struct got_object_id *id = pe->data;
451 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
452 if (err)
453 goto done;
454 our_ids[nours] = id;
455 nours++;
458 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
459 delete_branches, verbosity);
460 if (err)
461 goto done;
462 nsendfd = -1;
464 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
465 if (err)
466 goto done;
467 /*
468 * Process references reported by the server.
469 * Push appropriate object IDs onto the "their IDs" array.
470 * This array will be used to exclude objects which already
471 * exist on the server from our pack file.
472 */
473 TAILQ_FOREACH(pe, &their_refs, entry) {
474 const char *refname = pe->path;
475 struct got_object_id *their_id = pe->data;
476 int have_their_id;
477 struct got_object *obj;
478 struct got_pathlist_entry *my_ref = NULL;
479 int is_tag = 0;
481 /* Don't blindly trust the server to send us valid names. */
482 if (!got_ref_name_is_valid(refname))
483 continue;
485 if (strncmp(refname, "refs/tags/", 10) == 0)
486 is_tag = 1;
487 /*
488 * Find out whether this is a reference we want to upload.
489 * Otherwise we can still use this reference as a hint to
490 * avoid uploading any objects the server already has.
491 */
492 my_ref = find_ref(&have_refs, refname);
493 if (my_ref) {
494 struct got_object_id *my_id = my_ref->data;
495 if (got_object_id_cmp(my_id, their_id) != 0) {
496 if (!overwrite_refs && is_tag) {
497 err = got_error_fmt(
498 GOT_ERR_SEND_TAG_EXISTS,
499 "%s", refname);
500 goto done;
502 refs_to_send++;
506 /* Check if their object exists locally. */
507 err = got_object_open(&obj, repo, their_id);
508 if (err) {
509 if (err->code != GOT_ERR_NO_OBJ)
510 goto done;
511 if (!overwrite_refs && my_ref != NULL) {
512 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
513 "%s", refname);
514 goto done;
516 have_their_id = 0;
517 } else {
518 got_object_close(obj);
519 have_their_id = 1;
522 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
523 if (err)
524 goto done;
526 if (have_their_id) {
527 /* Enforce linear ancestry if required. */
528 if (!overwrite_refs && my_ref && !is_tag) {
529 struct got_object_id *my_id = my_ref->data;
530 err = check_common_ancestry(refname, my_id,
531 their_id, repo, cancel_cb, cancel_arg);
532 if (err)
533 goto done;
535 /* Exclude any objects reachable via their ID. */
536 their_ids[ntheirs] = their_id;
537 ntheirs++;
538 } else if (!is_tag) {
539 char *remote_refname;
540 struct got_reference *ref;
541 /*
542 * Exclude any objects which exist on the server
543 * according to a locally cached remote reference.
544 */
545 err = get_remote_refname(&remote_refname,
546 remote_name, refname);
547 if (err)
548 goto done;
549 err = got_ref_open(&ref, repo, remote_refname, 0);
550 free(remote_refname);
551 if (err) {
552 if (err->code != GOT_ERR_NOT_REF)
553 goto done;
554 } else {
555 err = got_ref_resolve(&their_ids[ntheirs],
556 repo, ref);
557 got_ref_close(ref);
558 if (err)
559 goto done;
560 ntheirs++;
565 /* Account for any new references we are going to upload. */
566 TAILQ_FOREACH(pe, &have_refs, entry) {
567 const char *refname = pe->path;
568 if (find_ref(&their_refs, refname) == NULL)
569 refs_to_send++;
572 /* Account for any existing references we are going to delete. */
573 TAILQ_FOREACH(pe, delete_branches, entry) {
574 const char *branchname = pe->path;
575 if (find_ref(&their_refs, branchname))
576 refs_to_delete++;
579 if (refs_to_send == 0 && refs_to_delete == 0) {
580 got_privsep_send_stop(imsg_sendfds[0]);
581 goto done;
584 if (refs_to_send > 0) {
585 struct got_ratelimit rl;
586 got_ratelimit_init(&rl, 0, 500);
587 memset(&ppa, 0, sizeof(ppa));
588 ppa.progress_cb = progress_cb;
589 ppa.progress_arg = progress_arg;
590 err = got_pack_create(packsha1, packfd, delta_cache,
591 their_ids, ntheirs, our_ids, nours, repo, 0, 1, 0,
592 pack_progress, &ppa, &rl, cancel_cb, cancel_arg);
593 if (err)
594 goto done;
596 npackfd = dup(packfd);
597 if (npackfd == -1) {
598 err = got_error_from_errno("dup");
599 goto done;
601 err = got_privsep_send_packfd(&sendibuf, npackfd);
602 if (err != NULL)
603 goto done;
604 npackfd = -1;
605 } else {
606 err = got_privsep_send_packfd(&sendibuf, -1);
607 if (err != NULL)
608 goto done;
611 while (!done) {
612 int success = 0;
613 char *refname = NULL;
614 char *errmsg = NULL;
616 if (cancel_cb) {
617 err = (*cancel_cb)(cancel_arg);
618 if (err)
619 goto done;
621 err = got_privsep_recv_send_progress(&done, &bytes_sent,
622 &success, &refname, &errmsg, &sendibuf);
623 if (err)
624 goto done;
625 if (refname && got_ref_name_is_valid(refname) && success &&
626 strncmp(refname, "refs/tags/", 10) != 0) {
627 struct got_pathlist_entry *my_ref;
628 /*
629 * The server has accepted our changes.
630 * Update our reference in refs/remotes/ accordingly.
631 */
632 my_ref = find_ref(&have_refs, refname);
633 if (my_ref) {
634 err = update_remote_ref(my_ref, remote_name,
635 repo);
636 if (err)
637 goto done;
640 if (refname != NULL ||
641 bytes_sent_cur != bytes_sent) {
642 err = progress_cb(progress_arg, ppa.ncolored,
643 ppa.nfound, ppa.ntrees, ppa.packfile_size,
644 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
645 ppa.nobj_written, bytes_sent,
646 refname, errmsg, success);
647 if (err) {
648 free(refname);
649 free(errmsg);
650 goto done;
652 bytes_sent_cur = bytes_sent;
654 free(refname);
655 free(errmsg);
657 done:
658 if (sendpid != -1) {
659 if (err)
660 got_privsep_send_stop(imsg_sendfds[0]);
661 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
662 err = got_error_from_errno("waitpid");
664 if (packfd != -1 && close(packfd) == -1 && err == NULL)
665 err = got_error_from_errno("close");
666 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
667 err = got_error_from_errno("fclose");
668 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
669 err = got_error_from_errno("close");
670 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
671 err = got_error_from_errno("close");
673 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
674 got_pathlist_free(&their_refs, GOT_PATHLIST_FREE_ALL);
675 /*
676 * Object ids are owned by have_refs/their_refs and are already freed;
677 * Only the arrays must be freed.
678 */
679 free(our_ids);
680 free(their_ids);
681 free(s);
682 return err;