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/uio.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/resource.h>
24 #include <sys/socket.h>
26 #include <errno.h>
27 #include <err.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdint.h>
33 #include <unistd.h>
34 #include <zlib.h>
35 #include <ctype.h>
36 #include <limits.h>
37 #include <time.h>
38 #include <uuid.h>
40 #include "got_error.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_object.h"
47 #include "got_opentemp.h"
48 #include "got_send.h"
49 #include "got_repository_admin.h"
50 #include "got_commit_graph.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_inflate.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_pack.h"
58 #include "got_lib_sha1.h"
59 #include "got_lib_privsep.h"
60 #include "got_lib_object_cache.h"
61 #include "got_lib_repository.h"
62 #include "got_lib_pack_create.h"
63 #include "got_lib_dial.h"
65 #ifndef nitems
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 #endif
69 #ifndef ssizeof
70 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
71 #endif
73 #ifndef MIN
74 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
75 #endif
77 const struct got_error *
78 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
79 const char *host, const char *port, const char *server_path, int verbosity)
80 {
81 const struct got_error *err = NULL;
83 *sendpid = -1;
84 *sendfd = -1;
86 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
87 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
88 GOT_DIAL_DIRECTION_SEND, verbosity);
89 else if (strcmp(proto, "git") == 0)
90 err = got_dial_git(sendfd, host, port, server_path,
91 GOT_DIAL_DIRECTION_SEND);
92 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
93 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
94 else
95 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
96 return err;
97 }
99 struct pack_progress_arg {
100 got_send_progress_cb progress_cb;
101 void *progress_arg;
103 off_t packfile_size;
104 int ncommits;
105 int nobj_total;
106 int nobj_deltify;
107 int nobj_written;
108 };
110 static const struct got_error *
111 pack_progress(void *arg, off_t packfile_size, int ncommits,
112 int nobj_total, int nobj_deltify, int nobj_written)
114 const struct got_error *err;
115 struct pack_progress_arg *a = arg;
117 err = a->progress_cb(a->progress_arg, packfile_size, ncommits,
118 nobj_total, nobj_deltify, nobj_written, 0, NULL, 0);
119 if (err)
120 return err;
122 a->packfile_size = packfile_size;
123 a->ncommits = ncommits;
124 a->nobj_total = nobj_total;
125 a->nobj_deltify = nobj_deltify;
126 a->nobj_written = nobj_written;
127 return NULL;
130 static const struct got_error *
131 insert_ref(struct got_reflist_head *refs, const char *refname,
132 struct got_repository *repo)
134 const struct got_error *err;
135 struct got_reference *ref;
136 struct got_reflist_entry *new;
138 err = got_ref_open(&ref, repo, refname, 0);
139 if (err)
140 return err;
142 err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
143 if (err || new == NULL /* duplicate */)
144 got_ref_close(ref);
146 return err;
149 static const struct got_error *
150 check_linear_ancestry(const char *refname, struct got_object_id *my_id,
151 struct got_object_id *their_id, struct got_repository *repo,
152 got_cancel_cb cancel_cb, void *cancel_arg)
154 const struct got_error *err = NULL;
155 struct got_object_id *yca_id;
156 int obj_type;
158 err = got_object_get_type(&obj_type, repo, their_id);
159 if (err)
160 return err;
161 if (obj_type != GOT_OBJ_TYPE_COMMIT)
162 return got_error_fmt(GOT_ERR_OBJ_TYPE,
163 "bad object type on server for %s", refname);
165 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
166 my_id, their_id, 1, repo, cancel_cb, cancel_arg);
167 if (err)
168 return err;
169 if (yca_id == NULL)
170 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
172 /*
173 * Require a straight line of history between the two commits,
174 * with their commit being older than my commit.
176 * Non-linear situations such as this require a rebase:
178 * (theirs) D F (mine)
179 * \ /
180 * C E
181 * \ /
182 * B (yca)
183 * |
184 * A
185 */
186 if (got_object_id_cmp(their_id, yca_id) != 0)
187 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
189 free(yca_id);
190 return err;
193 static const struct got_error *
194 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
196 struct got_object_id **new;
197 const size_t alloc_chunksz = 256;
199 if (*nalloc >= n)
200 return NULL;
202 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
203 sizeof(struct got_object_id));
204 if (new == NULL)
205 return got_error_from_errno("recallocarray");
207 *ids = new;
208 *nalloc += alloc_chunksz;
209 return NULL;
212 static struct got_reference *
213 find_ref(struct got_reflist_head *refs, const char *refname)
215 struct got_reflist_entry *re;
217 TAILQ_FOREACH(re, refs, entry) {
218 if (got_path_cmp(got_ref_get_name(re->ref), refname,
219 strlen(got_ref_get_name(re->ref)),
220 strlen(refname)) == 0) {
221 return re->ref;
225 return NULL;
228 static struct got_pathlist_entry *
229 find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
231 struct got_pathlist_entry *pe;
233 TAILQ_FOREACH(pe, their_refs, entry) {
234 const char *their_refname = pe->path;
235 if (got_path_cmp(their_refname, refname,
236 strlen(their_refname), strlen(refname)) == 0) {
237 return pe;
241 return NULL;
244 static const struct got_error *
245 get_remote_refname(char **remote_refname, const char *remote_name,
246 const char *refname)
248 if (strncmp(refname, "refs/", 5) == 0)
249 refname += 5;
250 if (strncmp(refname, "heads/", 6) == 0)
251 refname += 6;
253 if (asprintf(remote_refname, "refs/remotes/%s/%s",
254 remote_name, refname) == -1)
255 return got_error_from_errno("asprintf");
257 return NULL;
260 static const struct got_error *
261 update_remote_ref(struct got_reference *my_ref, const char *remote_name,
262 struct got_repository *repo)
264 const struct got_error *err, *unlock_err;
265 struct got_object_id *my_id;
266 struct got_reference *ref = NULL;
267 char *remote_refname = NULL;
268 int ref_locked = 0;
270 err = got_ref_resolve(&my_id, repo, my_ref);
271 if (err)
272 return err;
274 err = get_remote_refname(&remote_refname, remote_name,
275 got_ref_get_name(my_ref));
276 if (err)
277 goto done;
279 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
280 if (err) {
281 if (err->code != GOT_ERR_NOT_REF)
282 goto done;
283 err = got_ref_alloc(&ref, remote_refname, my_id);
284 if (err)
285 goto done;
286 } else {
287 ref_locked = 1;
288 err = got_ref_change_ref(ref, my_id);
289 if (err)
290 goto done;
293 err = got_ref_write(ref, repo);
294 done:
295 if (ref) {
296 if (ref_locked) {
297 unlock_err = got_ref_unlock(ref);
298 if (unlock_err && err == NULL)
299 err = unlock_err;
301 got_ref_close(ref);
303 free(my_id);
304 free(remote_refname);
305 return err;
308 const struct got_error*
309 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
310 struct got_pathlist_head *tag_names,
311 struct got_pathlist_head *delete_branches,
312 int verbosity, int overwrite_refs, int sendfd,
313 struct got_repository *repo, got_send_progress_cb progress_cb,
314 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
316 int imsg_sendfds[2];
317 int npackfd = -1, nsendfd = -1;
318 int sendstatus, done = 0;
319 const struct got_error *err;
320 struct imsgbuf sendibuf;
321 pid_t sendpid = -1;
322 struct got_reflist_head refs;
323 struct got_pathlist_head have_refs;
324 struct got_pathlist_head their_refs;
325 struct got_pathlist_entry *pe;
326 struct got_reflist_entry *re;
327 struct got_object_id **our_ids = NULL;
328 struct got_object_id **their_ids = NULL;
329 int i, 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;
333 struct pack_progress_arg ppa;
334 uint8_t packsha1[SHA1_DIGEST_LENGTH];
335 FILE *packfile = NULL;
337 TAILQ_INIT(&refs);
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 char *s;
345 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
346 err = got_error_from_errno("asprintf");
347 goto done;
349 err = insert_ref(&refs, s, repo);
350 free(s);
351 } else {
352 err = insert_ref(&refs, branchname, repo);
354 if (err)
355 goto done;
358 TAILQ_FOREACH(pe, delete_branches, entry) {
359 const char *branchname = pe->path;
360 struct got_reference *ref;
361 if (strncmp(branchname, "refs/heads/", 11) != 0) {
362 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
363 branchname);
364 goto done;
366 ref = find_ref(&refs, branchname);
367 if (ref) {
368 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
369 "changes on %s will be sent to server",
370 branchname);
371 goto done;
375 TAILQ_FOREACH(pe, tag_names, entry) {
376 const char *tagname = pe->path;
377 if (strncmp(tagname, "refs/tags/", 10) != 0) {
378 char *s;
379 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
380 err = got_error_from_errno("asprintf");
381 goto done;
383 err = insert_ref(&refs, s, repo);
384 free(s);
385 } else {
386 err = insert_ref(&refs, tagname, repo);
388 if (err)
389 goto done;
392 if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
393 err = got_error(GOT_ERR_SEND_EMPTY);
394 goto done;
397 TAILQ_FOREACH(re, &refs, entry) {
398 struct got_object_id *id;
399 int obj_type;
401 if (got_ref_is_symbolic(re->ref)) {
402 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
403 "cannot send symbolic reference %s",
404 got_ref_get_name(re->ref));
405 goto done;
408 err = got_ref_resolve(&id, repo, re->ref);
409 if (err)
410 goto done;
411 err = got_object_get_type(&obj_type, repo, id);
412 free(id);
413 if (err)
414 goto done;
415 switch (obj_type) {
416 case GOT_OBJ_TYPE_COMMIT:
417 case GOT_OBJ_TYPE_TAG:
418 break;
419 default:
420 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
421 "cannot send %s", got_ref_get_name(re->ref));
422 goto done;
426 packfile = got_opentemp();
427 if (packfile == NULL) {
428 err = got_error_from_errno("got_opentemp");
429 goto done;
432 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
433 err = got_error_from_errno("socketpair");
434 goto done;
437 sendpid = fork();
438 if (sendpid == -1) {
439 err = got_error_from_errno("fork");
440 goto done;
441 } else if (sendpid == 0){
442 got_privsep_exec_child(imsg_sendfds,
443 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
446 if (close(imsg_sendfds[1]) == -1) {
447 err = got_error_from_errno("close");
448 goto done;
450 imsg_init(&sendibuf, imsg_sendfds[0]);
451 nsendfd = dup(sendfd);
452 if (nsendfd == -1) {
453 err = got_error_from_errno("dup");
454 goto done;
457 /*
458 * Convert reflist to pathlist since the privsep layer
459 * is linked into helper programs which lack reference.c.
460 */
461 TAILQ_FOREACH(re, &refs, entry) {
462 struct got_object_id *id;
463 err = got_ref_resolve(&id, repo, re->ref);
464 if (err)
465 goto done;
466 err = got_pathlist_append(&have_refs,
467 got_ref_get_name(re->ref), id);
468 if (err)
469 goto done;
470 /*
471 * Also prepare the array of our object IDs which
472 * will be needed for generating a pack file.
473 */
474 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
475 if (err)
476 goto done;
477 our_ids[nours] = id;
478 nours++;
481 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
482 delete_branches, verbosity);
483 if (err)
484 goto done;
485 nsendfd = -1;
487 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
488 if (err)
489 goto done;
491 /*
492 * Process references reported by the server.
493 * Push appropriate object IDs onto the "their IDs" array.
494 * This array will be used to exclude objects which already
495 * exist on the server from our pack file.
496 */
497 TAILQ_FOREACH(pe, &their_refs, entry) {
498 const char *refname = pe->path;
499 struct got_object_id *their_id = pe->data;
500 int have_their_id;
501 struct got_object *obj;
502 struct got_reference *my_ref = NULL;
503 int is_tag = 0;
505 /* Don't blindly trust the server to send us valid names. */
506 if (!got_ref_name_is_valid(refname))
507 continue;
509 if (strncmp(refname, "refs/tags/", 10) == 0)
510 is_tag = 1;
511 /*
512 * Find out whether this is a reference we want to upload.
513 * Otherwise we can still use this reference as a hint to
514 * avoid uploading any objects the server already has.
515 */
516 my_ref = find_ref(&refs, refname);
517 if (my_ref) {
518 struct got_object_id *my_id;
519 err = got_ref_resolve(&my_id, repo, my_ref);
520 if (err)
521 goto done;
522 if (got_object_id_cmp(my_id, their_id) != 0) {
523 if (!overwrite_refs && is_tag) {
524 err = got_error_fmt(
525 GOT_ERR_SEND_TAG_EXISTS,
526 "%s", refname);
527 free(my_id);
528 goto done;
530 refs_to_send++;
532 free(my_id);
535 /* Check if their object exists locally. */
536 err = got_object_open(&obj, repo, their_id);
537 if (err) {
538 if (err->code != GOT_ERR_NO_OBJ)
539 goto done;
540 if (!overwrite_refs && my_ref != NULL) {
541 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
542 "%s", refname);
543 goto done;
545 have_their_id = 0;
546 } else {
547 got_object_close(obj);
548 have_their_id = 1;
551 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
552 if (err)
553 goto done;
555 if (have_their_id) {
556 /* Enforce linear ancestry if required. */
557 if (!overwrite_refs && my_ref && !is_tag) {
558 struct got_object_id *my_id;
559 err = got_ref_resolve(&my_id, repo, my_ref);
560 if (err)
561 goto done;
562 err = check_linear_ancestry(refname, my_id,
563 their_id, repo, cancel_cb, cancel_arg);
564 free(my_id);
565 my_id = NULL;
566 if (err)
567 goto done;
569 /* Exclude any objects reachable via their ID. */
570 their_ids[ntheirs] = got_object_id_dup(their_id);
571 if (their_ids[ntheirs] == NULL) {
572 err = got_error_from_errno("got_object_id_dup");
573 goto done;
575 ntheirs++;
576 } else if (!is_tag) {
577 char *remote_refname;
578 struct got_reference *ref;
579 /*
580 * Exclude any objects which exist on the server
581 * according to a locally cached remote reference.
582 */
583 err = get_remote_refname(&remote_refname,
584 remote_name, refname);
585 if (err)
586 goto done;
587 err = got_ref_open(&ref, repo, remote_refname, 0);
588 free(remote_refname);
589 if (err) {
590 if (err->code != GOT_ERR_NOT_REF)
591 goto done;
592 } else {
593 err = got_ref_resolve(&their_ids[ntheirs],
594 repo, ref);
595 got_ref_close(ref);
596 if (err)
597 goto done;
598 ntheirs++;
603 /* Account for any new references we are going to upload. */
604 TAILQ_FOREACH(re, &refs, entry) {
605 if (find_their_ref(&their_refs,
606 got_ref_get_name(re->ref)) == NULL)
607 refs_to_send++;
610 /* Account for any existing references we are going to delete. */
611 TAILQ_FOREACH(pe, delete_branches, entry) {
612 const char *branchname = pe->path;
613 if (find_their_ref(&their_refs, branchname))
614 refs_to_delete++;
617 if (refs_to_send == 0 && refs_to_delete == 0) {
618 got_privsep_send_stop(imsg_sendfds[0]);
619 goto done;
622 if (refs_to_send > 0) {
623 memset(&ppa, 0, sizeof(ppa));
624 ppa.progress_cb = progress_cb;
625 ppa.progress_arg = progress_arg;
626 err = got_pack_create(packsha1, packfile, their_ids, ntheirs,
627 our_ids, nours, repo, 0, 1, pack_progress, &ppa,
628 cancel_cb, cancel_arg);
629 if (err)
630 goto done;
632 if (fflush(packfile) == -1) {
633 err = got_error_from_errno("fflush");
634 goto done;
637 npackfd = dup(fileno(packfile));
638 if (npackfd == -1) {
639 err = got_error_from_errno("dup");
640 goto done;
642 err = got_privsep_send_packfd(&sendibuf, npackfd);
643 if (err != NULL)
644 goto done;
645 npackfd = -1;
646 } else {
647 err = got_privsep_send_packfd(&sendibuf, -1);
648 if (err != NULL)
649 goto done;
652 while (!done) {
653 int success = 0;
654 char *refname = NULL;
655 off_t bytes_sent_cur = 0;
656 if (cancel_cb) {
657 err = (*cancel_cb)(cancel_arg);
658 if (err)
659 goto done;
661 err = got_privsep_recv_send_progress(&done, &bytes_sent,
662 &success, &refname, &sendibuf);
663 if (err)
664 goto done;
665 if (refname && got_ref_name_is_valid(refname) && success &&
666 strncmp(refname, "refs/tags/", 10) != 0) {
667 struct got_reference *my_ref;
668 /*
669 * The server has accepted our changes.
670 * Update our reference in refs/remotes/ accordingly.
671 */
672 my_ref = find_ref(&refs, refname);
673 if (my_ref) {
674 err = update_remote_ref(my_ref, remote_name,
675 repo);
676 if (err)
677 goto done;
680 if (refname != NULL ||
681 bytes_sent_cur != bytes_sent) {
682 err = progress_cb(progress_arg, ppa.packfile_size,
683 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
684 ppa.nobj_written, bytes_sent,
685 refname, success);
686 if (err) {
687 free(refname);
688 goto done;
690 bytes_sent_cur = bytes_sent;
692 free(refname);
694 done:
695 if (sendpid != -1) {
696 if (err)
697 got_privsep_send_stop(imsg_sendfds[0]);
698 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
699 err = got_error_from_errno("waitpid");
701 if (packfile && fclose(packfile) == EOF && err == NULL)
702 err = got_error_from_errno("fclose");
703 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
704 err = got_error_from_errno("close");
705 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
706 err = got_error_from_errno("close");
708 got_ref_list_free(&refs);
709 got_pathlist_free(&have_refs);
710 got_pathlist_free(&their_refs);
711 for (i = 0; i < nours; i++)
712 free(our_ids[i]);
713 free(our_ids);
714 for (i = 0; i < ntheirs; i++)
715 free(their_ids[i]);
716 free(their_ids);
717 return err;