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_pack_create.h"
68 #include "got_lib_dial.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_DIRECTION_SEND, verbosity);
94 else if (strcmp(proto, "git") == 0)
95 err = got_dial_git(sendfd, host, port, server_path,
96 GOT_DIAL_DIRECTION_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, 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_ref(struct got_reflist_head *refs, const char *refname,
145 struct got_repository *repo)
147 const struct got_error *err;
148 struct got_reference *ref;
149 struct got_reflist_entry *new;
151 err = got_ref_open(&ref, repo, refname, 0);
152 if (err)
153 return err;
155 err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
156 if (err || new == NULL /* duplicate */)
157 got_ref_close(ref);
159 return err;
162 static const struct got_error *
163 check_linear_ancestry(const char *refname, struct got_object_id *my_id,
164 struct got_object_id *their_id, struct got_repository *repo,
165 got_cancel_cb cancel_cb, void *cancel_arg)
167 const struct got_error *err = NULL;
168 struct got_object_id *yca_id;
169 int obj_type;
171 err = got_object_get_type(&obj_type, repo, their_id);
172 if (err)
173 return err;
174 if (obj_type != GOT_OBJ_TYPE_COMMIT)
175 return got_error_fmt(GOT_ERR_OBJ_TYPE,
176 "bad object type on server for %s", refname);
178 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
179 my_id, their_id, 1, repo, cancel_cb, cancel_arg);
180 if (err)
181 return err;
182 if (yca_id == NULL)
183 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
185 /*
186 * Require a straight line of history between the two commits,
187 * with their commit being older than my commit.
189 * Non-linear situations such as this require a rebase:
191 * (theirs) D F (mine)
192 * \ /
193 * C E
194 * \ /
195 * B (yca)
196 * |
197 * A
198 */
199 if (got_object_id_cmp(their_id, yca_id) != 0)
200 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
202 free(yca_id);
203 return err;
206 static const struct got_error *
207 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
209 struct got_object_id **new;
210 const size_t alloc_chunksz = 256;
212 if (*nalloc >= n)
213 return NULL;
215 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
216 sizeof(struct got_object_id));
217 if (new == NULL)
218 return got_error_from_errno("recallocarray");
220 *ids = new;
221 *nalloc += alloc_chunksz;
222 return NULL;
225 static struct got_reference *
226 find_ref(struct got_reflist_head *refs, const char *refname)
228 struct got_reflist_entry *re;
230 TAILQ_FOREACH(re, refs, entry) {
231 if (got_path_cmp(got_ref_get_name(re->ref), refname,
232 strlen(got_ref_get_name(re->ref)),
233 strlen(refname)) == 0) {
234 return re->ref;
238 return NULL;
241 static struct got_pathlist_entry *
242 find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
244 struct got_pathlist_entry *pe;
246 TAILQ_FOREACH(pe, their_refs, entry) {
247 const char *their_refname = pe->path;
248 if (got_path_cmp(their_refname, refname,
249 strlen(their_refname), strlen(refname)) == 0) {
250 return pe;
254 return NULL;
257 static const struct got_error *
258 get_remote_refname(char **remote_refname, const char *remote_name,
259 const char *refname)
261 if (strncmp(refname, "refs/", 5) == 0)
262 refname += 5;
263 if (strncmp(refname, "heads/", 6) == 0)
264 refname += 6;
266 if (asprintf(remote_refname, "refs/remotes/%s/%s",
267 remote_name, refname) == -1)
268 return got_error_from_errno("asprintf");
270 return NULL;
273 static const struct got_error *
274 update_remote_ref(struct got_reference *my_ref, const char *remote_name,
275 struct got_repository *repo)
277 const struct got_error *err, *unlock_err;
278 struct got_object_id *my_id;
279 struct got_reference *ref = NULL;
280 char *remote_refname = NULL;
281 int ref_locked = 0;
283 err = got_ref_resolve(&my_id, repo, my_ref);
284 if (err)
285 return err;
287 err = get_remote_refname(&remote_refname, remote_name,
288 got_ref_get_name(my_ref));
289 if (err)
290 goto done;
292 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
293 if (err) {
294 if (err->code != GOT_ERR_NOT_REF)
295 goto done;
296 err = got_ref_alloc(&ref, remote_refname, my_id);
297 if (err)
298 goto done;
299 } else {
300 ref_locked = 1;
301 err = got_ref_change_ref(ref, my_id);
302 if (err)
303 goto done;
306 err = got_ref_write(ref, repo);
307 done:
308 if (ref) {
309 if (ref_locked) {
310 unlock_err = got_ref_unlock(ref);
311 if (unlock_err && err == NULL)
312 err = unlock_err;
314 got_ref_close(ref);
316 free(my_id);
317 free(remote_refname);
318 return err;
321 const struct got_error*
322 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
323 struct got_pathlist_head *tag_names,
324 struct got_pathlist_head *delete_branches,
325 int verbosity, int overwrite_refs, int sendfd,
326 struct got_repository *repo, got_send_progress_cb progress_cb,
327 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
329 int imsg_sendfds[2];
330 int npackfd = -1, nsendfd = -1;
331 int sendstatus, done = 0;
332 const struct got_error *err;
333 struct imsgbuf sendibuf;
334 pid_t sendpid = -1;
335 struct got_reflist_head refs;
336 struct got_pathlist_head have_refs;
337 struct got_pathlist_head their_refs;
338 struct got_pathlist_entry *pe;
339 struct got_reflist_entry *re;
340 struct got_object_id **our_ids = NULL;
341 struct got_object_id **their_ids = NULL;
342 int i, nours = 0, ntheirs = 0;
343 size_t nalloc_ours = 0, nalloc_theirs = 0;
344 int refs_to_send = 0, refs_to_delete = 0;
345 off_t bytes_sent = 0, bytes_sent_cur = 0;
346 struct pack_progress_arg ppa;
347 uint8_t packsha1[SHA1_DIGEST_LENGTH];
348 int packfd = -1;
349 FILE *delta_cache = NULL;
351 TAILQ_INIT(&refs);
352 TAILQ_INIT(&have_refs);
353 TAILQ_INIT(&their_refs);
355 TAILQ_FOREACH(pe, branch_names, entry) {
356 const char *branchname = pe->path;
357 if (strncmp(branchname, "refs/heads/", 11) != 0) {
358 char *s;
359 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
360 err = got_error_from_errno("asprintf");
361 goto done;
363 err = insert_ref(&refs, s, repo);
364 free(s);
365 } else {
366 err = insert_ref(&refs, branchname, repo);
368 if (err)
369 goto done;
372 TAILQ_FOREACH(pe, delete_branches, entry) {
373 const char *branchname = pe->path;
374 struct got_reference *ref;
375 if (strncmp(branchname, "refs/heads/", 11) != 0) {
376 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
377 branchname);
378 goto done;
380 ref = find_ref(&refs, branchname);
381 if (ref) {
382 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
383 "changes on %s will be sent to server",
384 branchname);
385 goto done;
389 TAILQ_FOREACH(pe, tag_names, entry) {
390 const char *tagname = pe->path;
391 if (strncmp(tagname, "refs/tags/", 10) != 0) {
392 char *s;
393 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
394 err = got_error_from_errno("asprintf");
395 goto done;
397 err = insert_ref(&refs, s, repo);
398 free(s);
399 } else {
400 err = insert_ref(&refs, tagname, repo);
402 if (err)
403 goto done;
406 if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
407 err = got_error(GOT_ERR_SEND_EMPTY);
408 goto done;
411 TAILQ_FOREACH(re, &refs, entry) {
412 struct got_object_id *id;
413 int obj_type;
415 if (got_ref_is_symbolic(re->ref)) {
416 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
417 "cannot send symbolic reference %s",
418 got_ref_get_name(re->ref));
419 goto done;
422 err = got_ref_resolve(&id, repo, re->ref);
423 if (err)
424 goto done;
425 err = got_object_get_type(&obj_type, repo, id);
426 free(id);
427 if (err)
428 goto done;
429 switch (obj_type) {
430 case GOT_OBJ_TYPE_COMMIT:
431 case GOT_OBJ_TYPE_TAG:
432 break;
433 default:
434 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
435 "cannot send %s", got_ref_get_name(re->ref));
436 goto done;
440 packfd = got_opentempfd();
441 if (packfd == -1) {
442 err = got_error_from_errno("got_opentempfd");
443 goto done;
446 delta_cache = got_opentemp();
447 if (delta_cache == NULL) {
448 err = got_error_from_errno("got_opentemp");
449 goto done;
452 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
453 err = got_error_from_errno("socketpair");
454 goto done;
457 sendpid = fork();
458 if (sendpid == -1) {
459 err = got_error_from_errno("fork");
460 goto done;
461 } else if (sendpid == 0){
462 got_privsep_exec_child(imsg_sendfds,
463 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
466 if (close(imsg_sendfds[1]) == -1) {
467 err = got_error_from_errno("close");
468 goto done;
470 imsg_init(&sendibuf, imsg_sendfds[0]);
471 nsendfd = dup(sendfd);
472 if (nsendfd == -1) {
473 err = got_error_from_errno("dup");
474 goto done;
477 /*
478 * Convert reflist to pathlist since the privsep layer
479 * is linked into helper programs which lack reference.c.
480 */
481 TAILQ_FOREACH(re, &refs, entry) {
482 struct got_object_id *id;
483 err = got_ref_resolve(&id, repo, re->ref);
484 if (err)
485 goto done;
486 err = got_pathlist_append(&have_refs,
487 got_ref_get_name(re->ref), id);
488 if (err)
489 goto done;
490 /*
491 * Also prepare the array of our object IDs which
492 * will be needed for generating a pack file.
493 */
494 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
495 if (err)
496 goto done;
497 our_ids[nours] = id;
498 nours++;
501 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
502 delete_branches, verbosity);
503 if (err)
504 goto done;
505 nsendfd = -1;
507 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
508 if (err)
509 goto done;
511 /*
512 * Process references reported by the server.
513 * Push appropriate object IDs onto the "their IDs" array.
514 * This array will be used to exclude objects which already
515 * exist on the server from our pack file.
516 */
517 TAILQ_FOREACH(pe, &their_refs, entry) {
518 const char *refname = pe->path;
519 struct got_object_id *their_id = pe->data;
520 int have_their_id;
521 struct got_object *obj;
522 struct got_reference *my_ref = NULL;
523 int is_tag = 0;
525 /* Don't blindly trust the server to send us valid names. */
526 if (!got_ref_name_is_valid(refname))
527 continue;
529 if (strncmp(refname, "refs/tags/", 10) == 0)
530 is_tag = 1;
531 /*
532 * Find out whether this is a reference we want to upload.
533 * Otherwise we can still use this reference as a hint to
534 * avoid uploading any objects the server already has.
535 */
536 my_ref = find_ref(&refs, refname);
537 if (my_ref) {
538 struct got_object_id *my_id;
539 err = got_ref_resolve(&my_id, repo, my_ref);
540 if (err)
541 goto done;
542 if (got_object_id_cmp(my_id, their_id) != 0) {
543 if (!overwrite_refs && is_tag) {
544 err = got_error_fmt(
545 GOT_ERR_SEND_TAG_EXISTS,
546 "%s", refname);
547 free(my_id);
548 goto done;
550 refs_to_send++;
552 free(my_id);
555 /* Check if their object exists locally. */
556 err = got_object_open(&obj, repo, their_id);
557 if (err) {
558 if (err->code != GOT_ERR_NO_OBJ)
559 goto done;
560 if (!overwrite_refs && my_ref != NULL) {
561 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
562 "%s", refname);
563 goto done;
565 have_their_id = 0;
566 } else {
567 got_object_close(obj);
568 have_their_id = 1;
571 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
572 if (err)
573 goto done;
575 if (have_their_id) {
576 /* Enforce linear ancestry if required. */
577 if (!overwrite_refs && my_ref && !is_tag) {
578 struct got_object_id *my_id;
579 err = got_ref_resolve(&my_id, repo, my_ref);
580 if (err)
581 goto done;
582 err = check_linear_ancestry(refname, my_id,
583 their_id, repo, cancel_cb, cancel_arg);
584 free(my_id);
585 my_id = NULL;
586 if (err)
587 goto done;
589 /* Exclude any objects reachable via their ID. */
590 their_ids[ntheirs] = got_object_id_dup(their_id);
591 if (their_ids[ntheirs] == NULL) {
592 err = got_error_from_errno("got_object_id_dup");
593 goto done;
595 ntheirs++;
596 } else if (!is_tag) {
597 char *remote_refname;
598 struct got_reference *ref;
599 /*
600 * Exclude any objects which exist on the server
601 * according to a locally cached remote reference.
602 */
603 err = get_remote_refname(&remote_refname,
604 remote_name, refname);
605 if (err)
606 goto done;
607 err = got_ref_open(&ref, repo, remote_refname, 0);
608 free(remote_refname);
609 if (err) {
610 if (err->code != GOT_ERR_NOT_REF)
611 goto done;
612 } else {
613 err = got_ref_resolve(&their_ids[ntheirs],
614 repo, ref);
615 got_ref_close(ref);
616 if (err)
617 goto done;
618 ntheirs++;
623 /* Account for any new references we are going to upload. */
624 TAILQ_FOREACH(re, &refs, entry) {
625 if (find_their_ref(&their_refs,
626 got_ref_get_name(re->ref)) == NULL)
627 refs_to_send++;
630 /* Account for any existing references we are going to delete. */
631 TAILQ_FOREACH(pe, delete_branches, entry) {
632 const char *branchname = pe->path;
633 if (find_their_ref(&their_refs, branchname))
634 refs_to_delete++;
637 if (refs_to_send == 0 && refs_to_delete == 0) {
638 got_privsep_send_stop(imsg_sendfds[0]);
639 goto done;
642 if (refs_to_send > 0) {
643 memset(&ppa, 0, sizeof(ppa));
644 ppa.progress_cb = progress_cb;
645 ppa.progress_arg = progress_arg;
646 err = got_pack_create(packsha1, packfd, delta_cache,
647 their_ids, ntheirs, our_ids, nours, repo, 0, 1,
648 pack_progress, &ppa, cancel_cb, cancel_arg);
649 if (err)
650 goto done;
652 npackfd = dup(packfd);
653 if (npackfd == -1) {
654 err = got_error_from_errno("dup");
655 goto done;
657 err = got_privsep_send_packfd(&sendibuf, npackfd);
658 if (err != NULL)
659 goto done;
660 npackfd = -1;
661 } else {
662 err = got_privsep_send_packfd(&sendibuf, -1);
663 if (err != NULL)
664 goto done;
667 while (!done) {
668 int success = 0;
669 char *refname = NULL;
670 if (cancel_cb) {
671 err = (*cancel_cb)(cancel_arg);
672 if (err)
673 goto done;
675 err = got_privsep_recv_send_progress(&done, &bytes_sent,
676 &success, &refname, &sendibuf);
677 if (err)
678 goto done;
679 if (refname && got_ref_name_is_valid(refname) && success &&
680 strncmp(refname, "refs/tags/", 10) != 0) {
681 struct got_reference *my_ref;
682 /*
683 * The server has accepted our changes.
684 * Update our reference in refs/remotes/ accordingly.
685 */
686 my_ref = find_ref(&refs, refname);
687 if (my_ref) {
688 err = update_remote_ref(my_ref, remote_name,
689 repo);
690 if (err)
691 goto done;
694 if (refname != NULL ||
695 bytes_sent_cur != bytes_sent) {
696 err = progress_cb(progress_arg, ppa.ncolored,
697 ppa.nfound, ppa.ntrees, ppa.packfile_size,
698 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
699 ppa.nobj_written, bytes_sent,
700 refname, success);
701 if (err) {
702 free(refname);
703 goto done;
705 bytes_sent_cur = bytes_sent;
707 free(refname);
709 done:
710 if (sendpid != -1) {
711 if (err)
712 got_privsep_send_stop(imsg_sendfds[0]);
713 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
714 err = got_error_from_errno("waitpid");
716 if (packfd != -1 && close(packfd) == -1 && err == NULL)
717 err = got_error_from_errno("close");
718 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
719 err = got_error_from_errno("fclose");
720 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
721 err = got_error_from_errno("close");
722 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
723 err = got_error_from_errno("close");
725 got_ref_list_free(&refs);
726 got_pathlist_free(&have_refs);
727 got_pathlist_free(&their_refs);
728 for (i = 0; i < nours; i++)
729 free(our_ids[i]);
730 free(our_ids);
731 for (i = 0; i < ntheirs; i++)
732 free(their_ids[i]);
733 free(their_ids);
734 return err;