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 <unistd.h>
38 #include <zlib.h>
39 #include <ctype.h>
40 #include <limits.h>
41 #include <imsg.h>
42 #include <time.h>
43 #include <uuid.h>
45 #include "got_error.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_path.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
51 #include "got_object.h"
52 #include "got_opentemp.h"
53 #include "got_send.h"
54 #include "got_repository_admin.h"
55 #include "got_commit_graph.h"
57 #include "got_lib_delta.h"
58 #include "got_lib_inflate.h"
59 #include "got_lib_object.h"
60 #include "got_lib_object_parse.h"
61 #include "got_lib_object_create.h"
62 #include "got_lib_pack.h"
63 #include "got_lib_sha1.h"
64 #include "got_lib_privsep.h"
65 #include "got_lib_object_cache.h"
66 #include "got_lib_repository.h"
67 #include "got_lib_ratelimit.h"
68 #include "got_lib_pack_create.h"
69 #include "got_lib_dial.h"
71 #ifndef nitems
72 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
73 #endif
75 #ifndef ssizeof
76 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
77 #endif
79 #ifndef MIN
80 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
81 #endif
83 const struct got_error *
84 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
85 const char *host, const char *port, const char *server_path, int verbosity)
86 {
87 const struct got_error *err = NULL;
89 *sendpid = -1;
90 *sendfd = -1;
92 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
93 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
94 GOT_DIAL_DIRECTION_SEND, verbosity);
95 else if (strcmp(proto, "git") == 0)
96 err = got_dial_git(sendfd, host, port, server_path,
97 GOT_DIAL_DIRECTION_SEND);
98 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
99 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
100 else
101 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
102 return err;
105 struct pack_progress_arg {
106 got_send_progress_cb progress_cb;
107 void *progress_arg;
109 int ncolored;
110 int nfound;
111 int ntrees;
112 off_t packfile_size;
113 int ncommits;
114 int nobj_total;
115 int nobj_deltify;
116 int nobj_written;
117 };
119 static const struct got_error *
120 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
121 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
122 int nobj_written)
124 const struct got_error *err;
125 struct pack_progress_arg *a = arg;
127 err = a->progress_cb(a->progress_arg, ncolored, nfound, ntrees,
128 packfile_size, ncommits, nobj_total, nobj_deltify,
129 nobj_written, 0, NULL, NULL, 0);
130 if (err)
131 return err;
133 a->ncolored= ncolored;
134 a->nfound = nfound;
135 a->ntrees = ntrees;
136 a->packfile_size = packfile_size;
137 a->ncommits = ncommits;
138 a->nobj_total = nobj_total;
139 a->nobj_deltify = nobj_deltify;
140 a->nobj_written = nobj_written;
141 return NULL;
144 static const struct got_error *
145 insert_ref(struct got_reflist_head *refs, const char *refname,
146 struct got_repository *repo)
148 const struct got_error *err;
149 struct got_reference *ref;
150 struct got_reflist_entry *new;
152 err = got_ref_open(&ref, repo, refname, 0);
153 if (err)
154 return err;
156 err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
157 if (err || new == NULL /* duplicate */)
158 got_ref_close(ref);
160 return err;
163 static const struct got_error *
164 check_linear_ancestry(const char *refname, struct got_object_id *my_id,
165 struct got_object_id *their_id, struct got_repository *repo,
166 got_cancel_cb cancel_cb, void *cancel_arg)
168 const struct got_error *err = NULL;
169 struct got_object_id *yca_id;
170 int obj_type;
172 err = got_object_get_type(&obj_type, repo, their_id);
173 if (err)
174 return err;
175 if (obj_type != GOT_OBJ_TYPE_COMMIT)
176 return got_error_fmt(GOT_ERR_OBJ_TYPE,
177 "bad object type on server for %s", refname);
179 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
180 my_id, their_id, 1, repo, cancel_cb, cancel_arg);
181 if (err)
182 return err;
183 if (yca_id == NULL)
184 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
186 /*
187 * Require a straight line of history between the two commits,
188 * with their commit being older than my commit.
190 * Non-linear situations such as this require a rebase:
192 * (theirs) D F (mine)
193 * \ /
194 * C E
195 * \ /
196 * B (yca)
197 * |
198 * A
199 */
200 if (got_object_id_cmp(their_id, yca_id) != 0)
201 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
203 free(yca_id);
204 return err;
207 static const struct got_error *
208 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
210 struct got_object_id **new;
211 const size_t alloc_chunksz = 256;
213 if (*nalloc >= n)
214 return NULL;
216 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
217 sizeof(struct got_object_id));
218 if (new == NULL)
219 return got_error_from_errno("recallocarray");
221 *ids = new;
222 *nalloc += alloc_chunksz;
223 return NULL;
226 static struct got_reference *
227 find_ref(struct got_reflist_head *refs, const char *refname)
229 struct got_reflist_entry *re;
231 TAILQ_FOREACH(re, refs, entry) {
232 if (got_path_cmp(got_ref_get_name(re->ref), refname,
233 strlen(got_ref_get_name(re->ref)),
234 strlen(refname)) == 0) {
235 return re->ref;
239 return NULL;
242 static struct got_pathlist_entry *
243 find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
245 struct got_pathlist_entry *pe;
247 TAILQ_FOREACH(pe, their_refs, entry) {
248 const char *their_refname = pe->path;
249 if (got_path_cmp(their_refname, refname,
250 strlen(their_refname), strlen(refname)) == 0) {
251 return pe;
255 return NULL;
258 static const struct got_error *
259 get_remote_refname(char **remote_refname, const char *remote_name,
260 const char *refname)
262 if (strncmp(refname, "refs/", 5) == 0)
263 refname += 5;
264 if (strncmp(refname, "heads/", 6) == 0)
265 refname += 6;
267 if (asprintf(remote_refname, "refs/remotes/%s/%s",
268 remote_name, refname) == -1)
269 return got_error_from_errno("asprintf");
271 return NULL;
274 static const struct got_error *
275 update_remote_ref(struct got_reference *my_ref, const char *remote_name,
276 struct got_repository *repo)
278 const struct got_error *err, *unlock_err;
279 struct got_object_id *my_id;
280 struct got_reference *ref = NULL;
281 char *remote_refname = NULL;
282 int ref_locked = 0;
284 err = got_ref_resolve(&my_id, repo, my_ref);
285 if (err)
286 return err;
288 err = get_remote_refname(&remote_refname, remote_name,
289 got_ref_get_name(my_ref));
290 if (err)
291 goto done;
293 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
294 if (err) {
295 if (err->code != GOT_ERR_NOT_REF)
296 goto done;
297 err = got_ref_alloc(&ref, remote_refname, my_id);
298 if (err)
299 goto done;
300 } else {
301 ref_locked = 1;
302 err = got_ref_change_ref(ref, my_id);
303 if (err)
304 goto done;
307 err = got_ref_write(ref, repo);
308 done:
309 if (ref) {
310 if (ref_locked) {
311 unlock_err = got_ref_unlock(ref);
312 if (unlock_err && err == NULL)
313 err = unlock_err;
315 got_ref_close(ref);
317 free(my_id);
318 free(remote_refname);
319 return err;
322 const struct got_error*
323 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
324 struct got_pathlist_head *tag_names,
325 struct got_pathlist_head *delete_branches,
326 int verbosity, int overwrite_refs, int sendfd,
327 struct got_repository *repo, got_send_progress_cb progress_cb,
328 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
330 int imsg_sendfds[2];
331 int npackfd = -1, nsendfd = -1;
332 int sendstatus, done = 0;
333 const struct got_error *err;
334 struct imsgbuf sendibuf;
335 pid_t sendpid = -1;
336 struct got_reflist_head refs;
337 struct got_pathlist_head have_refs;
338 struct got_pathlist_head their_refs;
339 struct got_pathlist_entry *pe;
340 struct got_reflist_entry *re;
341 struct got_object_id **our_ids = NULL;
342 struct got_object_id **their_ids = NULL;
343 int i, nours = 0, ntheirs = 0;
344 size_t nalloc_ours = 0, nalloc_theirs = 0;
345 int refs_to_send = 0, refs_to_delete = 0;
346 off_t bytes_sent = 0, bytes_sent_cur = 0;
347 struct pack_progress_arg ppa;
348 uint8_t packsha1[SHA1_DIGEST_LENGTH];
349 int packfd = -1;
350 FILE *delta_cache = NULL;
352 TAILQ_INIT(&refs);
353 TAILQ_INIT(&have_refs);
354 TAILQ_INIT(&their_refs);
356 TAILQ_FOREACH(pe, branch_names, entry) {
357 const char *branchname = pe->path;
358 if (strncmp(branchname, "refs/heads/", 11) != 0) {
359 char *s;
360 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
361 err = got_error_from_errno("asprintf");
362 goto done;
364 err = insert_ref(&refs, s, repo);
365 free(s);
366 } else {
367 err = insert_ref(&refs, branchname, repo);
369 if (err)
370 goto done;
373 TAILQ_FOREACH(pe, delete_branches, entry) {
374 const char *branchname = pe->path;
375 struct got_reference *ref;
376 if (strncmp(branchname, "refs/heads/", 11) != 0) {
377 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
378 branchname);
379 goto done;
381 ref = find_ref(&refs, branchname);
382 if (ref) {
383 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
384 "changes on %s will be sent to server",
385 branchname);
386 goto done;
390 TAILQ_FOREACH(pe, tag_names, entry) {
391 const char *tagname = pe->path;
392 if (strncmp(tagname, "refs/tags/", 10) != 0) {
393 char *s;
394 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
395 err = got_error_from_errno("asprintf");
396 goto done;
398 err = insert_ref(&refs, s, repo);
399 free(s);
400 } else {
401 err = insert_ref(&refs, tagname, repo);
403 if (err)
404 goto done;
407 if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
408 err = got_error(GOT_ERR_SEND_EMPTY);
409 goto done;
412 TAILQ_FOREACH(re, &refs, entry) {
413 struct got_object_id *id;
414 int obj_type;
416 if (got_ref_is_symbolic(re->ref)) {
417 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
418 "cannot send symbolic reference %s",
419 got_ref_get_name(re->ref));
420 goto done;
423 err = got_ref_resolve(&id, repo, re->ref);
424 if (err)
425 goto done;
426 err = got_object_get_type(&obj_type, repo, id);
427 free(id);
428 if (err)
429 goto done;
430 switch (obj_type) {
431 case GOT_OBJ_TYPE_COMMIT:
432 case GOT_OBJ_TYPE_TAG:
433 break;
434 default:
435 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
436 "cannot send %s", got_ref_get_name(re->ref));
437 goto done;
441 packfd = got_opentempfd();
442 if (packfd == -1) {
443 err = got_error_from_errno("got_opentempfd");
444 goto done;
447 delta_cache = got_opentemp();
448 if (delta_cache == NULL) {
449 err = got_error_from_errno("got_opentemp");
450 goto done;
453 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
454 err = got_error_from_errno("socketpair");
455 goto done;
458 sendpid = fork();
459 if (sendpid == -1) {
460 err = got_error_from_errno("fork");
461 goto done;
462 } else if (sendpid == 0){
463 got_privsep_exec_child(imsg_sendfds,
464 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
467 if (close(imsg_sendfds[1]) == -1) {
468 err = got_error_from_errno("close");
469 goto done;
471 imsg_init(&sendibuf, imsg_sendfds[0]);
472 nsendfd = dup(sendfd);
473 if (nsendfd == -1) {
474 err = got_error_from_errno("dup");
475 goto done;
478 /*
479 * Convert reflist to pathlist since the privsep layer
480 * is linked into helper programs which lack reference.c.
481 */
482 TAILQ_FOREACH(re, &refs, entry) {
483 struct got_object_id *id;
484 err = got_ref_resolve(&id, repo, re->ref);
485 if (err)
486 goto done;
487 err = got_pathlist_append(&have_refs,
488 got_ref_get_name(re->ref), id);
489 if (err)
490 goto done;
491 /*
492 * Also prepare the array of our object IDs which
493 * will be needed for generating a pack file.
494 */
495 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
496 if (err)
497 goto done;
498 our_ids[nours] = id;
499 nours++;
502 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
503 delete_branches, verbosity);
504 if (err)
505 goto done;
506 nsendfd = -1;
508 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
509 if (err)
510 goto done;
512 /*
513 * Process references reported by the server.
514 * Push appropriate object IDs onto the "their IDs" array.
515 * This array will be used to exclude objects which already
516 * exist on the server from our pack file.
517 */
518 TAILQ_FOREACH(pe, &their_refs, entry) {
519 const char *refname = pe->path;
520 struct got_object_id *their_id = pe->data;
521 int have_their_id;
522 struct got_object *obj;
523 struct got_reference *my_ref = NULL;
524 int is_tag = 0;
526 /* Don't blindly trust the server to send us valid names. */
527 if (!got_ref_name_is_valid(refname))
528 continue;
530 if (strncmp(refname, "refs/tags/", 10) == 0)
531 is_tag = 1;
532 /*
533 * Find out whether this is a reference we want to upload.
534 * Otherwise we can still use this reference as a hint to
535 * avoid uploading any objects the server already has.
536 */
537 my_ref = find_ref(&refs, refname);
538 if (my_ref) {
539 struct got_object_id *my_id;
540 err = got_ref_resolve(&my_id, repo, my_ref);
541 if (err)
542 goto done;
543 if (got_object_id_cmp(my_id, their_id) != 0) {
544 if (!overwrite_refs && is_tag) {
545 err = got_error_fmt(
546 GOT_ERR_SEND_TAG_EXISTS,
547 "%s", refname);
548 free(my_id);
549 goto done;
551 refs_to_send++;
553 free(my_id);
556 /* Check if their object exists locally. */
557 err = got_object_open(&obj, repo, their_id);
558 if (err) {
559 if (err->code != GOT_ERR_NO_OBJ)
560 goto done;
561 if (!overwrite_refs && my_ref != NULL) {
562 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
563 "%s", refname);
564 goto done;
566 have_their_id = 0;
567 } else {
568 got_object_close(obj);
569 have_their_id = 1;
572 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
573 if (err)
574 goto done;
576 if (have_their_id) {
577 /* Enforce linear ancestry if required. */
578 if (!overwrite_refs && my_ref && !is_tag) {
579 struct got_object_id *my_id;
580 err = got_ref_resolve(&my_id, repo, my_ref);
581 if (err)
582 goto done;
583 err = check_linear_ancestry(refname, my_id,
584 their_id, repo, cancel_cb, cancel_arg);
585 free(my_id);
586 my_id = NULL;
587 if (err)
588 goto done;
590 /* Exclude any objects reachable via their ID. */
591 their_ids[ntheirs] = got_object_id_dup(their_id);
592 if (their_ids[ntheirs] == NULL) {
593 err = got_error_from_errno("got_object_id_dup");
594 goto done;
596 ntheirs++;
597 } else if (!is_tag) {
598 char *remote_refname;
599 struct got_reference *ref;
600 /*
601 * Exclude any objects which exist on the server
602 * according to a locally cached remote reference.
603 */
604 err = get_remote_refname(&remote_refname,
605 remote_name, refname);
606 if (err)
607 goto done;
608 err = got_ref_open(&ref, repo, remote_refname, 0);
609 free(remote_refname);
610 if (err) {
611 if (err->code != GOT_ERR_NOT_REF)
612 goto done;
613 } else {
614 err = got_ref_resolve(&their_ids[ntheirs],
615 repo, ref);
616 got_ref_close(ref);
617 if (err)
618 goto done;
619 ntheirs++;
624 /* Account for any new references we are going to upload. */
625 TAILQ_FOREACH(re, &refs, entry) {
626 if (find_their_ref(&their_refs,
627 got_ref_get_name(re->ref)) == NULL)
628 refs_to_send++;
631 /* Account for any existing references we are going to delete. */
632 TAILQ_FOREACH(pe, delete_branches, entry) {
633 const char *branchname = pe->path;
634 if (find_their_ref(&their_refs, branchname))
635 refs_to_delete++;
638 if (refs_to_send == 0 && refs_to_delete == 0) {
639 got_privsep_send_stop(imsg_sendfds[0]);
640 goto done;
643 if (refs_to_send > 0) {
644 struct got_ratelimit rl;
645 got_ratelimit_init(&rl, 0, 500);
646 memset(&ppa, 0, sizeof(ppa));
647 ppa.progress_cb = progress_cb;
648 ppa.progress_arg = progress_arg;
649 err = got_pack_create(packsha1, packfd, delta_cache,
650 their_ids, ntheirs, our_ids, nours, repo, 0, 1,
651 pack_progress, &ppa, &rl, cancel_cb, cancel_arg);
652 if (err)
653 goto done;
655 npackfd = dup(packfd);
656 if (npackfd == -1) {
657 err = got_error_from_errno("dup");
658 goto done;
660 err = got_privsep_send_packfd(&sendibuf, npackfd);
661 if (err != NULL)
662 goto done;
663 npackfd = -1;
664 } else {
665 err = got_privsep_send_packfd(&sendibuf, -1);
666 if (err != NULL)
667 goto done;
670 while (!done) {
671 int success = 0;
672 char *refname = NULL;
673 char *errmsg = NULL;
675 if (cancel_cb) {
676 err = (*cancel_cb)(cancel_arg);
677 if (err)
678 goto done;
680 err = got_privsep_recv_send_progress(&done, &bytes_sent,
681 &success, &refname, &errmsg, &sendibuf);
682 if (err)
683 goto done;
684 if (refname && got_ref_name_is_valid(refname) && success &&
685 strncmp(refname, "refs/tags/", 10) != 0) {
686 struct got_reference *my_ref;
687 /*
688 * The server has accepted our changes.
689 * Update our reference in refs/remotes/ accordingly.
690 */
691 my_ref = find_ref(&refs, refname);
692 if (my_ref) {
693 err = update_remote_ref(my_ref, remote_name,
694 repo);
695 if (err)
696 goto done;
699 if (refname != NULL ||
700 bytes_sent_cur != bytes_sent) {
701 err = progress_cb(progress_arg, ppa.ncolored,
702 ppa.nfound, ppa.ntrees, ppa.packfile_size,
703 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
704 ppa.nobj_written, bytes_sent,
705 refname, errmsg, success);
706 if (err) {
707 free(refname);
708 free(errmsg);
709 goto done;
711 bytes_sent_cur = bytes_sent;
713 free(refname);
714 free(errmsg);
716 done:
717 if (sendpid != -1) {
718 if (err)
719 got_privsep_send_stop(imsg_sendfds[0]);
720 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
721 err = got_error_from_errno("waitpid");
723 if (packfd != -1 && close(packfd) == -1 && err == NULL)
724 err = got_error_from_errno("close");
725 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
726 err = got_error_from_errno("fclose");
727 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
728 err = got_error_from_errno("close");
729 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
730 err = got_error_from_errno("close");
732 got_ref_list_free(&refs);
733 got_pathlist_free(&have_refs);
734 got_pathlist_free(&their_refs);
735 for (i = 0; i < nours; i++)
736 free(our_ids[i]);
737 free(our_ids);
738 for (i = 0; i < ntheirs; i++)
739 free(their_ids[i]);
740 free(their_ids);
741 return err;