Blob


1 /*
2 * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/queue.h>
21 #include <sys/tree.h>
22 #include <sys/uio.h>
23 #include <sys/socket.h>
24 #include <sys/wait.h>
25 #include <sys/resource.h>
26 #include <sys/socket.h>
28 #include <endian.h>
29 #include <errno.h>
30 #include <err.h>
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdint.h>
36 #include <sha1.h>
37 #include <sha2.h>
38 #include <unistd.h>
39 #include <zlib.h>
40 #include <ctype.h>
41 #include <limits.h>
42 #include <imsg.h>
43 #include <time.h>
44 #include <uuid.h>
46 #include "got_error.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
49 #include "got_path.h"
50 #include "got_cancel.h"
51 #include "got_worktree.h"
52 #include "got_object.h"
53 #include "got_opentemp.h"
54 #include "got_send.h"
55 #include "got_repository_admin.h"
56 #include "got_commit_graph.h"
58 #include "got_lib_delta.h"
59 #include "got_lib_inflate.h"
60 #include "got_lib_object.h"
61 #include "got_lib_object_parse.h"
62 #include "got_lib_object_create.h"
63 #include "got_lib_pack.h"
64 #include "got_lib_hash.h"
65 #include "got_lib_privsep.h"
66 #include "got_lib_object_cache.h"
67 #include "got_lib_repository.h"
68 #include "got_lib_ratelimit.h"
69 #include "got_lib_pack_create.h"
70 #include "got_lib_dial.h"
72 #ifndef nitems
73 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
74 #endif
76 #ifndef ssizeof
77 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
78 #endif
80 #ifndef MIN
81 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
82 #endif
84 const struct got_error *
85 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
86 const char *host, const char *port, const char *server_path, int verbosity)
87 {
88 const struct got_error *err = NULL;
90 *sendpid = -1;
91 *sendfd = -1;
93 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
94 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
95 GOT_DIAL_DIRECTION_SEND, verbosity);
96 else if (strcmp(proto, "git") == 0)
97 err = got_dial_git(sendfd, host, port, server_path,
98 GOT_DIAL_DIRECTION_SEND);
99 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
100 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
101 else
102 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
103 return err;
106 struct pack_progress_arg {
107 got_send_progress_cb progress_cb;
108 void *progress_arg;
110 int ncolored;
111 int nfound;
112 int ntrees;
113 off_t packfile_size;
114 int ncommits;
115 int nobj_total;
116 int nobj_deltify;
117 int nobj_written;
118 };
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,
123 int nobj_written)
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);
131 if (err)
132 return err;
134 a->ncolored= ncolored;
135 a->nfound = nfound;
136 a->ntrees = ntrees;
137 a->packfile_size = packfile_size;
138 a->ncommits = ncommits;
139 a->nobj_total = nobj_total;
140 a->nobj_deltify = nobj_deltify;
141 a->nobj_written = nobj_written;
142 return NULL;
145 static const struct got_error *
146 insert_ref(struct got_reflist_head *refs, const char *refname,
147 struct got_repository *repo)
149 const struct got_error *err;
150 struct got_reference *ref;
151 struct got_reflist_entry *new;
153 err = got_ref_open(&ref, repo, refname, 0);
154 if (err)
155 return err;
157 err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
158 if (err || new == NULL /* duplicate */)
159 got_ref_close(ref);
161 return err;
164 static const struct got_error *
165 check_linear_ancestry(const char *refname, struct got_object_id *my_id,
166 struct got_object_id *their_id, struct got_repository *repo,
167 got_cancel_cb cancel_cb, void *cancel_arg)
169 const struct got_error *err = NULL;
170 struct got_object_id *yca_id;
171 int obj_type;
173 err = got_object_get_type(&obj_type, repo, their_id);
174 if (err)
175 return err;
176 if (obj_type != GOT_OBJ_TYPE_COMMIT)
177 return got_error_fmt(GOT_ERR_OBJ_TYPE,
178 "bad object type on server for %s", refname);
180 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
181 my_id, their_id, 1, repo, cancel_cb, cancel_arg);
182 if (err)
183 return err;
184 if (yca_id == NULL)
185 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
187 /*
188 * Require a straight line of history between the two commits,
189 * with their commit being older than my commit.
191 * Non-linear situations such as this require a rebase:
193 * (theirs) D F (mine)
194 * \ /
195 * C E
196 * \ /
197 * B (yca)
198 * |
199 * A
200 */
201 if (got_object_id_cmp(their_id, yca_id) != 0)
202 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
204 free(yca_id);
205 return err;
208 static const struct got_error *
209 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
211 struct got_object_id **new;
212 const size_t alloc_chunksz = 256;
214 if (*nalloc >= n)
215 return NULL;
217 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
218 sizeof(struct got_object_id));
219 if (new == NULL)
220 return got_error_from_errno("recallocarray");
222 *ids = new;
223 *nalloc += alloc_chunksz;
224 return NULL;
227 static struct got_reference *
228 find_ref(struct got_reflist_head *refs, const char *refname)
230 struct got_reflist_entry *re;
232 TAILQ_FOREACH(re, refs, entry) {
233 if (got_path_cmp(got_ref_get_name(re->ref), refname,
234 strlen(got_ref_get_name(re->ref)),
235 strlen(refname)) == 0) {
236 return re->ref;
240 return NULL;
243 static struct got_pathlist_entry *
244 find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
246 struct got_pathlist_entry *pe;
248 TAILQ_FOREACH(pe, their_refs, entry) {
249 const char *their_refname = pe->path;
250 if (got_path_cmp(their_refname, refname,
251 strlen(their_refname), strlen(refname)) == 0) {
252 return pe;
256 return NULL;
259 static const struct got_error *
260 get_remote_refname(char **remote_refname, const char *remote_name,
261 const char *refname)
263 if (strncmp(refname, "refs/", 5) == 0)
264 refname += 5;
265 if (strncmp(refname, "heads/", 6) == 0)
266 refname += 6;
268 if (asprintf(remote_refname, "refs/remotes/%s/%s",
269 remote_name, refname) == -1)
270 return got_error_from_errno("asprintf");
272 return NULL;
275 static const struct got_error *
276 update_remote_ref(struct got_reference *my_ref, const char *remote_name,
277 struct got_repository *repo)
279 const struct got_error *err, *unlock_err;
280 struct got_object_id *my_id;
281 struct got_reference *ref = NULL;
282 char *remote_refname = NULL;
283 int ref_locked = 0;
285 err = got_ref_resolve(&my_id, repo, my_ref);
286 if (err)
287 return err;
289 err = get_remote_refname(&remote_refname, remote_name,
290 got_ref_get_name(my_ref));
291 if (err)
292 goto done;
294 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
295 if (err) {
296 if (err->code != GOT_ERR_NOT_REF)
297 goto done;
298 err = got_ref_alloc(&ref, remote_refname, my_id);
299 if (err)
300 goto done;
301 } else {
302 ref_locked = 1;
303 err = got_ref_change_ref(ref, my_id);
304 if (err)
305 goto done;
308 err = got_ref_write(ref, repo);
309 done:
310 if (ref) {
311 if (ref_locked) {
312 unlock_err = got_ref_unlock(ref);
313 if (unlock_err && err == NULL)
314 err = unlock_err;
316 got_ref_close(ref);
318 free(my_id);
319 free(remote_refname);
320 return err;
323 const struct got_error*
324 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
325 struct got_pathlist_head *tag_names,
326 struct got_pathlist_head *delete_branches,
327 int verbosity, int overwrite_refs, int sendfd,
328 struct got_repository *repo, got_send_progress_cb progress_cb,
329 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
331 int imsg_sendfds[2];
332 int npackfd = -1, nsendfd = -1;
333 int sendstatus, done = 0;
334 const struct got_error *err;
335 struct imsgbuf sendibuf;
336 pid_t sendpid = -1;
337 struct got_reflist_head refs;
338 struct got_pathlist_head have_refs;
339 struct got_pathlist_head their_refs;
340 struct got_pathlist_entry *pe;
341 struct got_reflist_entry *re;
342 struct got_object_id **our_ids = NULL;
343 struct got_object_id **their_ids = NULL;
344 int i, nours = 0, ntheirs = 0;
345 size_t nalloc_ours = 0, nalloc_theirs = 0;
346 int refs_to_send = 0, refs_to_delete = 0;
347 off_t bytes_sent = 0, bytes_sent_cur = 0;
348 struct pack_progress_arg ppa;
349 uint8_t packsha1[SHA1_DIGEST_LENGTH];
350 int packfd = -1;
351 FILE *delta_cache = NULL;
353 TAILQ_INIT(&refs);
354 TAILQ_INIT(&have_refs);
355 TAILQ_INIT(&their_refs);
357 TAILQ_FOREACH(pe, branch_names, entry) {
358 const char *branchname = pe->path;
359 if (strncmp(branchname, "refs/heads/", 11) != 0) {
360 char *s;
361 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
362 err = got_error_from_errno("asprintf");
363 goto done;
365 err = insert_ref(&refs, s, repo);
366 free(s);
367 } else {
368 err = insert_ref(&refs, branchname, repo);
370 if (err)
371 goto done;
374 TAILQ_FOREACH(pe, delete_branches, entry) {
375 const char *branchname = pe->path;
376 struct got_reference *ref;
377 if (strncmp(branchname, "refs/heads/", 11) != 0) {
378 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
379 branchname);
380 goto done;
382 ref = find_ref(&refs, branchname);
383 if (ref) {
384 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
385 "changes on %s will be sent to server",
386 branchname);
387 goto done;
391 TAILQ_FOREACH(pe, tag_names, entry) {
392 const char *tagname = pe->path;
393 if (strncmp(tagname, "refs/tags/", 10) != 0) {
394 char *s;
395 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
396 err = got_error_from_errno("asprintf");
397 goto done;
399 err = insert_ref(&refs, s, repo);
400 free(s);
401 } else {
402 err = insert_ref(&refs, tagname, repo);
404 if (err)
405 goto done;
408 if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
409 err = got_error(GOT_ERR_SEND_EMPTY);
410 goto done;
413 TAILQ_FOREACH(re, &refs, entry) {
414 struct got_object_id *id;
415 int obj_type;
417 if (got_ref_is_symbolic(re->ref)) {
418 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
419 "cannot send symbolic reference %s",
420 got_ref_get_name(re->ref));
421 goto done;
424 err = got_ref_resolve(&id, repo, re->ref);
425 if (err)
426 goto done;
427 err = got_object_get_type(&obj_type, repo, id);
428 free(id);
429 if (err)
430 goto done;
431 switch (obj_type) {
432 case GOT_OBJ_TYPE_COMMIT:
433 case GOT_OBJ_TYPE_TAG:
434 break;
435 default:
436 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
437 "cannot send %s", got_ref_get_name(re->ref));
438 goto done;
442 packfd = got_opentempfd();
443 if (packfd == -1) {
444 err = got_error_from_errno("got_opentempfd");
445 goto done;
448 delta_cache = got_opentemp();
449 if (delta_cache == NULL) {
450 err = got_error_from_errno("got_opentemp");
451 goto done;
454 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
455 err = got_error_from_errno("socketpair");
456 goto done;
459 sendpid = fork();
460 if (sendpid == -1) {
461 err = got_error_from_errno("fork");
462 goto done;
463 } else if (sendpid == 0){
464 got_privsep_exec_child(imsg_sendfds,
465 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
468 if (close(imsg_sendfds[1]) == -1) {
469 err = got_error_from_errno("close");
470 goto done;
472 imsg_init(&sendibuf, imsg_sendfds[0]);
473 nsendfd = dup(sendfd);
474 if (nsendfd == -1) {
475 err = got_error_from_errno("dup");
476 goto done;
479 /*
480 * Convert reflist to pathlist since the privsep layer
481 * is linked into helper programs which lack reference.c.
482 */
483 TAILQ_FOREACH(re, &refs, entry) {
484 struct got_object_id *id;
485 err = got_ref_resolve(&id, repo, re->ref);
486 if (err)
487 goto done;
488 err = got_pathlist_append(&have_refs,
489 got_ref_get_name(re->ref), id);
490 if (err)
491 goto done;
492 /*
493 * Also prepare the array of our object IDs which
494 * will be needed for generating a pack file.
495 */
496 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
497 if (err)
498 goto done;
499 our_ids[nours] = id;
500 nours++;
503 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
504 delete_branches, verbosity);
505 if (err)
506 goto done;
507 nsendfd = -1;
509 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
510 if (err)
511 goto done;
513 /*
514 * Process references reported by the server.
515 * Push appropriate object IDs onto the "their IDs" array.
516 * This array will be used to exclude objects which already
517 * exist on the server from our pack file.
518 */
519 TAILQ_FOREACH(pe, &their_refs, entry) {
520 const char *refname = pe->path;
521 struct got_object_id *their_id = pe->data;
522 int have_their_id;
523 struct got_object *obj;
524 struct got_reference *my_ref = NULL;
525 int is_tag = 0;
527 /* Don't blindly trust the server to send us valid names. */
528 if (!got_ref_name_is_valid(refname))
529 continue;
531 if (strncmp(refname, "refs/tags/", 10) == 0)
532 is_tag = 1;
533 /*
534 * Find out whether this is a reference we want to upload.
535 * Otherwise we can still use this reference as a hint to
536 * avoid uploading any objects the server already has.
537 */
538 my_ref = find_ref(&refs, refname);
539 if (my_ref) {
540 struct got_object_id *my_id;
541 err = got_ref_resolve(&my_id, repo, my_ref);
542 if (err)
543 goto done;
544 if (got_object_id_cmp(my_id, their_id) != 0) {
545 if (!overwrite_refs && is_tag) {
546 err = got_error_fmt(
547 GOT_ERR_SEND_TAG_EXISTS,
548 "%s", refname);
549 free(my_id);
550 goto done;
552 refs_to_send++;
554 free(my_id);
557 /* Check if their object exists locally. */
558 err = got_object_open(&obj, repo, their_id);
559 if (err) {
560 if (err->code != GOT_ERR_NO_OBJ)
561 goto done;
562 if (!overwrite_refs && my_ref != NULL) {
563 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
564 "%s", refname);
565 goto done;
567 have_their_id = 0;
568 } else {
569 got_object_close(obj);
570 have_their_id = 1;
573 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
574 if (err)
575 goto done;
577 if (have_their_id) {
578 /* Enforce linear ancestry if required. */
579 if (!overwrite_refs && my_ref && !is_tag) {
580 struct got_object_id *my_id;
581 err = got_ref_resolve(&my_id, repo, my_ref);
582 if (err)
583 goto done;
584 err = check_linear_ancestry(refname, my_id,
585 their_id, repo, cancel_cb, cancel_arg);
586 free(my_id);
587 my_id = NULL;
588 if (err)
589 goto done;
591 /* Exclude any objects reachable via their ID. */
592 their_ids[ntheirs] = got_object_id_dup(their_id);
593 if (their_ids[ntheirs] == NULL) {
594 err = got_error_from_errno("got_object_id_dup");
595 goto done;
597 ntheirs++;
598 } else if (!is_tag) {
599 char *remote_refname;
600 struct got_reference *ref;
601 /*
602 * Exclude any objects which exist on the server
603 * according to a locally cached remote reference.
604 */
605 err = get_remote_refname(&remote_refname,
606 remote_name, refname);
607 if (err)
608 goto done;
609 err = got_ref_open(&ref, repo, remote_refname, 0);
610 free(remote_refname);
611 if (err) {
612 if (err->code != GOT_ERR_NOT_REF)
613 goto done;
614 } else {
615 err = got_ref_resolve(&their_ids[ntheirs],
616 repo, ref);
617 got_ref_close(ref);
618 if (err)
619 goto done;
620 ntheirs++;
625 /* Account for any new references we are going to upload. */
626 TAILQ_FOREACH(re, &refs, entry) {
627 if (find_their_ref(&their_refs,
628 got_ref_get_name(re->ref)) == NULL)
629 refs_to_send++;
632 /* Account for any existing references we are going to delete. */
633 TAILQ_FOREACH(pe, delete_branches, entry) {
634 const char *branchname = pe->path;
635 if (find_their_ref(&their_refs, branchname))
636 refs_to_delete++;
639 if (refs_to_send == 0 && refs_to_delete == 0) {
640 got_privsep_send_stop(imsg_sendfds[0]);
641 goto done;
644 if (refs_to_send > 0) {
645 struct got_ratelimit rl;
646 got_ratelimit_init(&rl, 0, 500);
647 memset(&ppa, 0, sizeof(ppa));
648 ppa.progress_cb = progress_cb;
649 ppa.progress_arg = progress_arg;
650 err = got_pack_create(packsha1, packfd, delta_cache,
651 their_ids, ntheirs, our_ids, nours, repo, 0, 1,
652 pack_progress, &ppa, &rl, cancel_cb, cancel_arg);
653 if (err)
654 goto done;
656 npackfd = dup(packfd);
657 if (npackfd == -1) {
658 err = got_error_from_errno("dup");
659 goto done;
661 err = got_privsep_send_packfd(&sendibuf, npackfd);
662 if (err != NULL)
663 goto done;
664 npackfd = -1;
665 } else {
666 err = got_privsep_send_packfd(&sendibuf, -1);
667 if (err != NULL)
668 goto done;
671 while (!done) {
672 int success = 0;
673 char *refname = NULL;
674 char *errmsg = NULL;
676 if (cancel_cb) {
677 err = (*cancel_cb)(cancel_arg);
678 if (err)
679 goto done;
681 err = got_privsep_recv_send_progress(&done, &bytes_sent,
682 &success, &refname, &errmsg, &sendibuf);
683 if (err)
684 goto done;
685 if (refname && got_ref_name_is_valid(refname) && success &&
686 strncmp(refname, "refs/tags/", 10) != 0) {
687 struct got_reference *my_ref;
688 /*
689 * The server has accepted our changes.
690 * Update our reference in refs/remotes/ accordingly.
691 */
692 my_ref = find_ref(&refs, refname);
693 if (my_ref) {
694 err = update_remote_ref(my_ref, remote_name,
695 repo);
696 if (err)
697 goto done;
700 if (refname != NULL ||
701 bytes_sent_cur != bytes_sent) {
702 err = progress_cb(progress_arg, ppa.ncolored,
703 ppa.nfound, ppa.ntrees, ppa.packfile_size,
704 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
705 ppa.nobj_written, bytes_sent,
706 refname, errmsg, success);
707 if (err) {
708 free(refname);
709 free(errmsg);
710 goto done;
712 bytes_sent_cur = bytes_sent;
714 free(refname);
715 free(errmsg);
717 done:
718 if (sendpid != -1) {
719 if (err)
720 got_privsep_send_stop(imsg_sendfds[0]);
721 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
722 err = got_error_from_errno("waitpid");
724 if (packfd != -1 && close(packfd) == -1 && err == NULL)
725 err = got_error_from_errno("close");
726 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
727 err = got_error_from_errno("fclose");
728 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
729 err = got_error_from_errno("close");
730 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
731 err = got_error_from_errno("close");
733 got_ref_list_free(&refs);
734 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_NONE);
735 got_pathlist_free(&their_refs, GOT_PATHLIST_FREE_NONE);
736 for (i = 0; i < nours; i++)
737 free(our_ids[i]);
738 free(our_ids);
739 for (i = 0; i < ntheirs; i++)
740 free(their_ids[i]);
741 free(their_ids);
742 return err;