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 <errno.h>
29 #include <err.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdint.h>
35 #include <unistd.h>
36 #include <zlib.h>
37 #include <ctype.h>
38 #include <limits.h>
39 #include <time.h>
40 #include <uuid.h>
42 #include "got_error.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_path.h"
46 #include "got_cancel.h"
47 #include "got_worktree.h"
48 #include "got_object.h"
49 #include "got_opentemp.h"
50 #include "got_send.h"
51 #include "got_repository_admin.h"
52 #include "got_commit_graph.h"
54 #include "got_lib_delta.h"
55 #include "got_lib_inflate.h"
56 #include "got_lib_object.h"
57 #include "got_lib_object_parse.h"
58 #include "got_lib_object_create.h"
59 #include "got_lib_pack.h"
60 #include "got_lib_sha1.h"
61 #include "got_lib_privsep.h"
62 #include "got_lib_object_cache.h"
63 #include "got_lib_repository.h"
64 #include "got_lib_pack_create.h"
65 #include "got_lib_dial.h"
67 #ifndef nitems
68 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 #endif
71 #ifndef ssizeof
72 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
73 #endif
75 #ifndef MIN
76 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
77 #endif
79 const struct got_error *
80 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
81 const char *host, const char *port, const char *server_path, int verbosity)
82 {
83 const struct got_error *err = NULL;
85 *sendpid = -1;
86 *sendfd = -1;
88 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
89 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
90 GOT_DIAL_DIRECTION_SEND, verbosity);
91 else if (strcmp(proto, "git") == 0)
92 err = got_dial_git(sendfd, host, port, server_path,
93 GOT_DIAL_DIRECTION_SEND);
94 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
95 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
96 else
97 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
98 return err;
99 }
101 struct pack_progress_arg {
102 got_send_progress_cb progress_cb;
103 void *progress_arg;
105 off_t packfile_size;
106 int ncommits;
107 int nobj_total;
108 int nobj_deltify;
109 int nobj_written;
110 };
112 static const struct got_error *
113 pack_progress(void *arg, off_t packfile_size, int ncommits,
114 int nobj_total, int nobj_deltify, int nobj_written)
116 const struct got_error *err;
117 struct pack_progress_arg *a = arg;
119 err = a->progress_cb(a->progress_arg, packfile_size, ncommits,
120 nobj_total, nobj_deltify, nobj_written, 0, NULL, 0);
121 if (err)
122 return err;
124 a->packfile_size = packfile_size;
125 a->ncommits = ncommits;
126 a->nobj_total = nobj_total;
127 a->nobj_deltify = nobj_deltify;
128 a->nobj_written = nobj_written;
129 return NULL;
132 static const struct got_error *
133 insert_ref(struct got_reflist_head *refs, const char *refname,
134 struct got_repository *repo)
136 const struct got_error *err;
137 struct got_reference *ref;
138 struct got_reflist_entry *new;
140 err = got_ref_open(&ref, repo, refname, 0);
141 if (err)
142 return err;
144 err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
145 if (err || new == NULL /* duplicate */)
146 got_ref_close(ref);
148 return err;
151 static const struct got_error *
152 check_linear_ancestry(const char *refname, struct got_object_id *my_id,
153 struct got_object_id *their_id, struct got_repository *repo,
154 got_cancel_cb cancel_cb, void *cancel_arg)
156 const struct got_error *err = NULL;
157 struct got_object_id *yca_id;
158 int obj_type;
160 err = got_object_get_type(&obj_type, repo, their_id);
161 if (err)
162 return err;
163 if (obj_type != GOT_OBJ_TYPE_COMMIT)
164 return got_error_fmt(GOT_ERR_OBJ_TYPE,
165 "bad object type on server for %s", refname);
167 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
168 my_id, their_id, 1, repo, cancel_cb, cancel_arg);
169 if (err)
170 return err;
171 if (yca_id == NULL)
172 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
174 /*
175 * Require a straight line of history between the two commits,
176 * with their commit being older than my commit.
178 * Non-linear situations such as this require a rebase:
180 * (theirs) D F (mine)
181 * \ /
182 * C E
183 * \ /
184 * B (yca)
185 * |
186 * A
187 */
188 if (got_object_id_cmp(their_id, yca_id) != 0)
189 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
191 free(yca_id);
192 return err;
195 static const struct got_error *
196 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
198 struct got_object_id **new;
199 const size_t alloc_chunksz = 256;
201 if (*nalloc >= n)
202 return NULL;
204 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
205 sizeof(struct got_object_id));
206 if (new == NULL)
207 return got_error_from_errno("recallocarray");
209 *ids = new;
210 *nalloc += alloc_chunksz;
211 return NULL;
214 static struct got_reference *
215 find_ref(struct got_reflist_head *refs, const char *refname)
217 struct got_reflist_entry *re;
219 TAILQ_FOREACH(re, refs, entry) {
220 if (got_path_cmp(got_ref_get_name(re->ref), refname,
221 strlen(got_ref_get_name(re->ref)),
222 strlen(refname)) == 0) {
223 return re->ref;
227 return NULL;
230 static struct got_pathlist_entry *
231 find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
233 struct got_pathlist_entry *pe;
235 TAILQ_FOREACH(pe, their_refs, entry) {
236 const char *their_refname = pe->path;
237 if (got_path_cmp(their_refname, refname,
238 strlen(their_refname), strlen(refname)) == 0) {
239 return pe;
243 return NULL;
246 static const struct got_error *
247 get_remote_refname(char **remote_refname, const char *remote_name,
248 const char *refname)
250 if (strncmp(refname, "refs/", 5) == 0)
251 refname += 5;
252 if (strncmp(refname, "heads/", 6) == 0)
253 refname += 6;
255 if (asprintf(remote_refname, "refs/remotes/%s/%s",
256 remote_name, refname) == -1)
257 return got_error_from_errno("asprintf");
259 return NULL;
262 static const struct got_error *
263 update_remote_ref(struct got_reference *my_ref, const char *remote_name,
264 struct got_repository *repo)
266 const struct got_error *err, *unlock_err;
267 struct got_object_id *my_id;
268 struct got_reference *ref = NULL;
269 char *remote_refname = NULL;
270 int ref_locked = 0;
272 err = got_ref_resolve(&my_id, repo, my_ref);
273 if (err)
274 return err;
276 err = get_remote_refname(&remote_refname, remote_name,
277 got_ref_get_name(my_ref));
278 if (err)
279 goto done;
281 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
282 if (err) {
283 if (err->code != GOT_ERR_NOT_REF)
284 goto done;
285 err = got_ref_alloc(&ref, remote_refname, my_id);
286 if (err)
287 goto done;
288 } else {
289 ref_locked = 1;
290 err = got_ref_change_ref(ref, my_id);
291 if (err)
292 goto done;
295 err = got_ref_write(ref, repo);
296 done:
297 if (ref) {
298 if (ref_locked) {
299 unlock_err = got_ref_unlock(ref);
300 if (unlock_err && err == NULL)
301 err = unlock_err;
303 got_ref_close(ref);
305 free(my_id);
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_reflist_head refs;
325 struct got_pathlist_head have_refs;
326 struct got_pathlist_head their_refs;
327 struct got_pathlist_entry *pe;
328 struct got_reflist_entry *re;
329 struct got_object_id **our_ids = NULL;
330 struct got_object_id **their_ids = NULL;
331 int i, nours = 0, ntheirs = 0;
332 size_t nalloc_ours = 0, nalloc_theirs = 0;
333 int refs_to_send = 0, refs_to_delete = 0;
334 off_t bytes_sent = 0;
335 struct pack_progress_arg ppa;
336 uint8_t packsha1[SHA1_DIGEST_LENGTH];
337 FILE *packfile = NULL;
339 TAILQ_INIT(&refs);
340 TAILQ_INIT(&have_refs);
341 TAILQ_INIT(&their_refs);
343 TAILQ_FOREACH(pe, branch_names, entry) {
344 const char *branchname = pe->path;
345 if (strncmp(branchname, "refs/heads/", 11) != 0) {
346 char *s;
347 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
348 err = got_error_from_errno("asprintf");
349 goto done;
351 err = insert_ref(&refs, s, repo);
352 free(s);
353 } else {
354 err = insert_ref(&refs, branchname, repo);
356 if (err)
357 goto done;
360 TAILQ_FOREACH(pe, delete_branches, entry) {
361 const char *branchname = pe->path;
362 struct got_reference *ref;
363 if (strncmp(branchname, "refs/heads/", 11) != 0) {
364 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
365 branchname);
366 goto done;
368 ref = find_ref(&refs, branchname);
369 if (ref) {
370 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
371 "changes on %s will be sent to server",
372 branchname);
373 goto done;
377 TAILQ_FOREACH(pe, tag_names, entry) {
378 const char *tagname = pe->path;
379 if (strncmp(tagname, "refs/tags/", 10) != 0) {
380 char *s;
381 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
382 err = got_error_from_errno("asprintf");
383 goto done;
385 err = insert_ref(&refs, s, repo);
386 free(s);
387 } else {
388 err = insert_ref(&refs, tagname, repo);
390 if (err)
391 goto done;
394 if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
395 err = got_error(GOT_ERR_SEND_EMPTY);
396 goto done;
399 TAILQ_FOREACH(re, &refs, entry) {
400 struct got_object_id *id;
401 int obj_type;
403 if (got_ref_is_symbolic(re->ref)) {
404 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
405 "cannot send symbolic reference %s",
406 got_ref_get_name(re->ref));
407 goto done;
410 err = got_ref_resolve(&id, repo, re->ref);
411 if (err)
412 goto done;
413 err = got_object_get_type(&obj_type, repo, id);
414 free(id);
415 if (err)
416 goto done;
417 switch (obj_type) {
418 case GOT_OBJ_TYPE_COMMIT:
419 case GOT_OBJ_TYPE_TAG:
420 break;
421 default:
422 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
423 "cannot send %s", got_ref_get_name(re->ref));
424 goto done;
428 packfile = got_opentemp();
429 if (packfile == NULL) {
430 err = got_error_from_errno("got_opentemp");
431 goto done;
434 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
435 err = got_error_from_errno("socketpair");
436 goto done;
439 sendpid = fork();
440 if (sendpid == -1) {
441 err = got_error_from_errno("fork");
442 goto done;
443 } else if (sendpid == 0){
444 got_privsep_exec_child(imsg_sendfds,
445 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
448 if (close(imsg_sendfds[1]) == -1) {
449 err = got_error_from_errno("close");
450 goto done;
452 imsg_init(&sendibuf, imsg_sendfds[0]);
453 nsendfd = dup(sendfd);
454 if (nsendfd == -1) {
455 err = got_error_from_errno("dup");
456 goto done;
459 /*
460 * Convert reflist to pathlist since the privsep layer
461 * is linked into helper programs which lack reference.c.
462 */
463 TAILQ_FOREACH(re, &refs, entry) {
464 struct got_object_id *id;
465 err = got_ref_resolve(&id, repo, re->ref);
466 if (err)
467 goto done;
468 err = got_pathlist_append(&have_refs,
469 got_ref_get_name(re->ref), id);
470 if (err)
471 goto done;
472 /*
473 * Also prepare the array of our object IDs which
474 * will be needed for generating a pack file.
475 */
476 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
477 if (err)
478 goto done;
479 our_ids[nours] = id;
480 nours++;
483 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
484 delete_branches, verbosity);
485 if (err)
486 goto done;
487 nsendfd = -1;
489 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
490 if (err)
491 goto done;
493 /*
494 * Process references reported by the server.
495 * Push appropriate object IDs onto the "their IDs" array.
496 * This array will be used to exclude objects which already
497 * exist on the server from our pack file.
498 */
499 TAILQ_FOREACH(pe, &their_refs, entry) {
500 const char *refname = pe->path;
501 struct got_object_id *their_id = pe->data;
502 int have_their_id;
503 struct got_object *obj;
504 struct got_reference *my_ref = NULL;
505 int is_tag = 0;
507 /* Don't blindly trust the server to send us valid names. */
508 if (!got_ref_name_is_valid(refname))
509 continue;
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 refs_to_send++;
524 free(my_id);
528 if (strncmp(refname, "refs/tags/", 10) == 0)
529 is_tag = 1;
531 /* Prevent tags from being overwritten by default. */
532 if (!overwrite_refs && my_ref && is_tag) {
533 err = got_error_fmt(GOT_ERR_SEND_TAG_EXISTS,
534 "%s", refname);
535 goto done;
538 /* Check if their object exists locally. */
539 err = got_object_open(&obj, repo, their_id);
540 if (err) {
541 if (err->code != GOT_ERR_NO_OBJ)
542 goto done;
543 if (!overwrite_refs && my_ref != NULL) {
544 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
545 "%s", refname);
546 goto done;
548 have_their_id = 0;
549 } else {
550 got_object_close(obj);
551 have_their_id = 1;
554 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
555 if (err)
556 goto done;
558 if (have_their_id) {
559 /* Enforce linear ancestry if required. */
560 if (!overwrite_refs && my_ref && !is_tag) {
561 struct got_object_id *my_id;
562 err = got_ref_resolve(&my_id, repo, my_ref);
563 if (err)
564 goto done;
565 err = check_linear_ancestry(refname, my_id,
566 their_id, repo, cancel_cb, cancel_arg);
567 free(my_id);
568 my_id = NULL;
569 if (err)
570 goto done;
572 /* Exclude any objects reachable via their ID. */
573 their_ids[ntheirs] = got_object_id_dup(their_id);
574 if (their_ids[ntheirs] == NULL) {
575 err = got_error_from_errno("got_object_id_dup");
576 goto done;
578 ntheirs++;
579 } else if (!is_tag) {
580 char *remote_refname;
581 struct got_reference *ref;
582 /*
583 * Exclude any objects which exist on the server
584 * according to a locally cached remote reference.
585 */
586 err = get_remote_refname(&remote_refname,
587 remote_name, refname);
588 if (err)
589 goto done;
590 err = got_ref_open(&ref, repo, remote_refname, 0);
591 free(remote_refname);
592 if (err) {
593 if (err->code != GOT_ERR_NOT_REF)
594 goto done;
595 } else {
596 err = got_ref_resolve(&their_ids[ntheirs],
597 repo, ref);
598 got_ref_close(ref);
599 if (err)
600 goto done;
601 ntheirs++;
606 /* Account for any new references we are going to upload. */
607 TAILQ_FOREACH(re, &refs, entry) {
608 if (find_their_ref(&their_refs,
609 got_ref_get_name(re->ref)) == NULL)
610 refs_to_send++;
613 /* Account for any existing references we are going to delete. */
614 TAILQ_FOREACH(pe, delete_branches, entry) {
615 const char *branchname = pe->path;
616 if (find_their_ref(&their_refs, branchname))
617 refs_to_delete++;
620 if (refs_to_send == 0 && refs_to_delete == 0) {
621 got_privsep_send_stop(imsg_sendfds[0]);
622 goto done;
625 if (refs_to_send > 0) {
626 memset(&ppa, 0, sizeof(ppa));
627 ppa.progress_cb = progress_cb;
628 ppa.progress_arg = progress_arg;
629 err = got_pack_create(packsha1, packfile, their_ids, ntheirs,
630 our_ids, nours, repo, 0, 1, pack_progress, &ppa,
631 cancel_cb, cancel_arg);
632 if (err)
633 goto done;
635 if (fflush(packfile) == -1) {
636 err = got_error_from_errno("fflush");
637 goto done;
640 npackfd = dup(fileno(packfile));
641 if (npackfd == -1) {
642 err = got_error_from_errno("dup");
643 goto done;
645 err = got_privsep_send_packfd(&sendibuf, npackfd);
646 if (err != NULL)
647 goto done;
648 npackfd = -1;
649 } else {
650 err = got_privsep_send_packfd(&sendibuf, -1);
651 if (err != NULL)
652 goto done;
655 while (!done) {
656 int success = 0;
657 char *refname = NULL;
658 off_t bytes_sent_cur = 0;
659 if (cancel_cb) {
660 err = (*cancel_cb)(cancel_arg);
661 if (err)
662 goto done;
664 err = got_privsep_recv_send_progress(&done, &bytes_sent,
665 &success, &refname, &sendibuf);
666 if (err)
667 goto done;
668 if (refname && got_ref_name_is_valid(refname) && success &&
669 strncmp(refname, "refs/tags/", 10) != 0) {
670 struct got_reference *my_ref;
671 /*
672 * The server has accepted our changes.
673 * Update our reference in refs/remotes/ accordingly.
674 */
675 my_ref = find_ref(&refs, refname);
676 if (my_ref) {
677 err = update_remote_ref(my_ref, remote_name,
678 repo);
679 if (err)
680 goto done;
683 if (refname != NULL ||
684 bytes_sent_cur != bytes_sent) {
685 err = progress_cb(progress_arg, ppa.packfile_size,
686 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
687 ppa.nobj_written, bytes_sent,
688 refname, success);
689 if (err) {
690 free(refname);
691 goto done;
693 bytes_sent_cur = bytes_sent;
695 free(refname);
697 done:
698 if (sendpid != -1) {
699 if (err)
700 got_privsep_send_stop(imsg_sendfds[0]);
701 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
702 err = got_error_from_errno("waitpid");
704 if (packfile && fclose(packfile) == EOF && err == NULL)
705 err = got_error_from_errno("fclose");
706 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
707 err = got_error_from_errno("close");
708 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
709 err = got_error_from_errno("close");
711 got_ref_list_free(&refs);
712 got_pathlist_free(&have_refs);
713 got_pathlist_free(&their_refs);
714 for (i = 0; i < nours; i++)
715 free(our_ids[i]);
716 free(our_ids);
717 for (i = 0; i < ntheirs; i++)
718 free(their_ids[i]);
719 free(their_ids);
720 return err;