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/uio.h>
22 #include <sys/socket.h>
23 #include <sys/wait.h>
24 #include <sys/resource.h>
25 #include <sys/socket.h>
27 #include <endian.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 <sha1.h>
36 #include <unistd.h>
37 #include <zlib.h>
38 #include <ctype.h>
39 #include <limits.h>
40 #include <imsg.h>
41 #include <time.h>
42 #include <uuid.h>
44 #include "got_error.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
47 #include "got_path.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
50 #include "got_object.h"
51 #include "got_opentemp.h"
52 #include "got_send.h"
53 #include "got_repository_admin.h"
54 #include "got_commit_graph.h"
56 #include "got_lib_delta.h"
57 #include "got_lib_inflate.h"
58 #include "got_lib_object.h"
59 #include "got_lib_object_parse.h"
60 #include "got_lib_object_create.h"
61 #include "got_lib_pack.h"
62 #include "got_lib_sha1.h"
63 #include "got_lib_privsep.h"
64 #include "got_lib_object_cache.h"
65 #include "got_lib_repository.h"
66 #include "got_lib_pack_create.h"
67 #include "got_lib_dial.h"
69 #ifndef nitems
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 #endif
73 #ifndef ssizeof
74 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
75 #endif
77 #ifndef MIN
78 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
79 #endif
81 const struct got_error *
82 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
83 const char *host, const char *port, const char *server_path, int verbosity)
84 {
85 const struct got_error *err = NULL;
87 *sendpid = -1;
88 *sendfd = -1;
90 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
91 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
92 GOT_DIAL_DIRECTION_SEND, verbosity);
93 else if (strcmp(proto, "git") == 0)
94 err = got_dial_git(sendfd, host, port, server_path,
95 GOT_DIAL_DIRECTION_SEND);
96 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
97 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
98 else
99 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
100 return err;
103 struct pack_progress_arg {
104 got_send_progress_cb progress_cb;
105 void *progress_arg;
107 off_t packfile_size;
108 int ncommits;
109 int nobj_total;
110 int nobj_deltify;
111 int nobj_written;
112 };
114 static const struct got_error *
115 pack_progress(void *arg, off_t packfile_size, int ncommits,
116 int nobj_total, int nobj_deltify, int nobj_written)
118 const struct got_error *err;
119 struct pack_progress_arg *a = arg;
121 err = a->progress_cb(a->progress_arg, packfile_size, ncommits,
122 nobj_total, nobj_deltify, nobj_written, 0, NULL, 0);
123 if (err)
124 return err;
126 a->packfile_size = packfile_size;
127 a->ncommits = ncommits;
128 a->nobj_total = nobj_total;
129 a->nobj_deltify = nobj_deltify;
130 a->nobj_written = nobj_written;
131 return NULL;
134 static const struct got_error *
135 insert_ref(struct got_reflist_head *refs, const char *refname,
136 struct got_repository *repo)
138 const struct got_error *err;
139 struct got_reference *ref;
140 struct got_reflist_entry *new;
142 err = got_ref_open(&ref, repo, refname, 0);
143 if (err)
144 return err;
146 err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
147 if (err || new == NULL /* duplicate */)
148 got_ref_close(ref);
150 return err;
153 static const struct got_error *
154 check_linear_ancestry(const char *refname, struct got_object_id *my_id,
155 struct got_object_id *their_id, struct got_repository *repo,
156 got_cancel_cb cancel_cb, void *cancel_arg)
158 const struct got_error *err = NULL;
159 struct got_object_id *yca_id;
160 int obj_type;
162 err = got_object_get_type(&obj_type, repo, their_id);
163 if (err)
164 return err;
165 if (obj_type != GOT_OBJ_TYPE_COMMIT)
166 return got_error_fmt(GOT_ERR_OBJ_TYPE,
167 "bad object type on server for %s", refname);
169 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
170 my_id, their_id, 1, repo, cancel_cb, cancel_arg);
171 if (err)
172 return err;
173 if (yca_id == NULL)
174 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
176 /*
177 * Require a straight line of history between the two commits,
178 * with their commit being older than my commit.
180 * Non-linear situations such as this require a rebase:
182 * (theirs) D F (mine)
183 * \ /
184 * C E
185 * \ /
186 * B (yca)
187 * |
188 * A
189 */
190 if (got_object_id_cmp(their_id, yca_id) != 0)
191 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
193 free(yca_id);
194 return err;
197 static const struct got_error *
198 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
200 struct got_object_id **new;
201 const size_t alloc_chunksz = 256;
203 if (*nalloc >= n)
204 return NULL;
206 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
207 sizeof(struct got_object_id));
208 if (new == NULL)
209 return got_error_from_errno("recallocarray");
211 *ids = new;
212 *nalloc += alloc_chunksz;
213 return NULL;
216 static struct got_reference *
217 find_ref(struct got_reflist_head *refs, const char *refname)
219 struct got_reflist_entry *re;
221 TAILQ_FOREACH(re, refs, entry) {
222 if (got_path_cmp(got_ref_get_name(re->ref), refname,
223 strlen(got_ref_get_name(re->ref)),
224 strlen(refname)) == 0) {
225 return re->ref;
229 return NULL;
232 static struct got_pathlist_entry *
233 find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
235 struct got_pathlist_entry *pe;
237 TAILQ_FOREACH(pe, their_refs, entry) {
238 const char *their_refname = pe->path;
239 if (got_path_cmp(their_refname, refname,
240 strlen(their_refname), strlen(refname)) == 0) {
241 return pe;
245 return NULL;
248 static const struct got_error *
249 get_remote_refname(char **remote_refname, const char *remote_name,
250 const char *refname)
252 if (strncmp(refname, "refs/", 5) == 0)
253 refname += 5;
254 if (strncmp(refname, "heads/", 6) == 0)
255 refname += 6;
257 if (asprintf(remote_refname, "refs/remotes/%s/%s",
258 remote_name, refname) == -1)
259 return got_error_from_errno("asprintf");
261 return NULL;
264 static const struct got_error *
265 update_remote_ref(struct got_reference *my_ref, const char *remote_name,
266 struct got_repository *repo)
268 const struct got_error *err, *unlock_err;
269 struct got_object_id *my_id;
270 struct got_reference *ref = NULL;
271 char *remote_refname = NULL;
272 int ref_locked = 0;
274 err = got_ref_resolve(&my_id, repo, my_ref);
275 if (err)
276 return err;
278 err = get_remote_refname(&remote_refname, remote_name,
279 got_ref_get_name(my_ref));
280 if (err)
281 goto done;
283 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
284 if (err) {
285 if (err->code != GOT_ERR_NOT_REF)
286 goto done;
287 err = got_ref_alloc(&ref, remote_refname, my_id);
288 if (err)
289 goto done;
290 } else {
291 ref_locked = 1;
292 err = got_ref_change_ref(ref, my_id);
293 if (err)
294 goto done;
297 err = got_ref_write(ref, repo);
298 done:
299 if (ref) {
300 if (ref_locked) {
301 unlock_err = got_ref_unlock(ref);
302 if (unlock_err && err == NULL)
303 err = unlock_err;
305 got_ref_close(ref);
307 free(my_id);
308 free(remote_refname);
309 return err;
312 const struct got_error*
313 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
314 struct got_pathlist_head *tag_names,
315 struct got_pathlist_head *delete_branches,
316 int verbosity, int overwrite_refs, int sendfd,
317 struct got_repository *repo, got_send_progress_cb progress_cb,
318 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
320 int imsg_sendfds[2];
321 int npackfd = -1, nsendfd = -1;
322 int sendstatus, done = 0;
323 const struct got_error *err;
324 struct imsgbuf sendibuf;
325 pid_t sendpid = -1;
326 struct got_reflist_head refs;
327 struct got_pathlist_head have_refs;
328 struct got_pathlist_head their_refs;
329 struct got_pathlist_entry *pe;
330 struct got_reflist_entry *re;
331 struct got_object_id **our_ids = NULL;
332 struct got_object_id **their_ids = NULL;
333 int i, nours = 0, ntheirs = 0;
334 size_t nalloc_ours = 0, nalloc_theirs = 0;
335 int refs_to_send = 0, refs_to_delete = 0;
336 off_t bytes_sent = 0;
337 struct pack_progress_arg ppa;
338 uint8_t packsha1[SHA1_DIGEST_LENGTH];
339 FILE *packfile = NULL;
341 TAILQ_INIT(&refs);
342 TAILQ_INIT(&have_refs);
343 TAILQ_INIT(&their_refs);
345 TAILQ_FOREACH(pe, branch_names, entry) {
346 const char *branchname = pe->path;
347 if (strncmp(branchname, "refs/heads/", 11) != 0) {
348 char *s;
349 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
350 err = got_error_from_errno("asprintf");
351 goto done;
353 err = insert_ref(&refs, s, repo);
354 free(s);
355 } else {
356 err = insert_ref(&refs, branchname, repo);
358 if (err)
359 goto done;
362 TAILQ_FOREACH(pe, delete_branches, entry) {
363 const char *branchname = pe->path;
364 struct got_reference *ref;
365 if (strncmp(branchname, "refs/heads/", 11) != 0) {
366 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
367 branchname);
368 goto done;
370 ref = find_ref(&refs, branchname);
371 if (ref) {
372 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
373 "changes on %s will be sent to server",
374 branchname);
375 goto done;
379 TAILQ_FOREACH(pe, tag_names, entry) {
380 const char *tagname = pe->path;
381 if (strncmp(tagname, "refs/tags/", 10) != 0) {
382 char *s;
383 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
384 err = got_error_from_errno("asprintf");
385 goto done;
387 err = insert_ref(&refs, s, repo);
388 free(s);
389 } else {
390 err = insert_ref(&refs, tagname, repo);
392 if (err)
393 goto done;
396 if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
397 err = got_error(GOT_ERR_SEND_EMPTY);
398 goto done;
401 TAILQ_FOREACH(re, &refs, entry) {
402 struct got_object_id *id;
403 int obj_type;
405 if (got_ref_is_symbolic(re->ref)) {
406 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
407 "cannot send symbolic reference %s",
408 got_ref_get_name(re->ref));
409 goto done;
412 err = got_ref_resolve(&id, repo, re->ref);
413 if (err)
414 goto done;
415 err = got_object_get_type(&obj_type, repo, id);
416 free(id);
417 if (err)
418 goto done;
419 switch (obj_type) {
420 case GOT_OBJ_TYPE_COMMIT:
421 case GOT_OBJ_TYPE_TAG:
422 break;
423 default:
424 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
425 "cannot send %s", got_ref_get_name(re->ref));
426 goto done;
430 packfile = got_opentemp();
431 if (packfile == NULL) {
432 err = got_error_from_errno("got_opentemp");
433 goto done;
436 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
437 err = got_error_from_errno("socketpair");
438 goto done;
441 sendpid = fork();
442 if (sendpid == -1) {
443 err = got_error_from_errno("fork");
444 goto done;
445 } else if (sendpid == 0){
446 got_privsep_exec_child(imsg_sendfds,
447 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
450 if (close(imsg_sendfds[1]) == -1) {
451 err = got_error_from_errno("close");
452 goto done;
454 imsg_init(&sendibuf, imsg_sendfds[0]);
455 nsendfd = dup(sendfd);
456 if (nsendfd == -1) {
457 err = got_error_from_errno("dup");
458 goto done;
461 /*
462 * Convert reflist to pathlist since the privsep layer
463 * is linked into helper programs which lack reference.c.
464 */
465 TAILQ_FOREACH(re, &refs, entry) {
466 struct got_object_id *id;
467 err = got_ref_resolve(&id, repo, re->ref);
468 if (err)
469 goto done;
470 err = got_pathlist_append(&have_refs,
471 got_ref_get_name(re->ref), id);
472 if (err)
473 goto done;
474 /*
475 * Also prepare the array of our object IDs which
476 * will be needed for generating a pack file.
477 */
478 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
479 if (err)
480 goto done;
481 our_ids[nours] = id;
482 nours++;
485 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
486 delete_branches, verbosity);
487 if (err)
488 goto done;
489 nsendfd = -1;
491 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
492 if (err)
493 goto done;
495 /*
496 * Process references reported by the server.
497 * Push appropriate object IDs onto the "their IDs" array.
498 * This array will be used to exclude objects which already
499 * exist on the server from our pack file.
500 */
501 TAILQ_FOREACH(pe, &their_refs, entry) {
502 const char *refname = pe->path;
503 struct got_object_id *their_id = pe->data;
504 int have_their_id;
505 struct got_object *obj;
506 struct got_reference *my_ref = NULL;
507 int is_tag = 0;
509 /* Don't blindly trust the server to send us valid names. */
510 if (!got_ref_name_is_valid(refname))
511 continue;
513 /*
514 * Find out whether this is a reference we want to upload.
515 * Otherwise we can still use this reference as a hint to
516 * avoid uploading any objects the server already has.
517 */
518 my_ref = find_ref(&refs, refname);
519 if (my_ref) {
520 struct got_object_id *my_id;
521 err = got_ref_resolve(&my_id, repo, my_ref);
522 if (err)
523 goto done;
524 if (got_object_id_cmp(my_id, their_id) != 0)
525 refs_to_send++;
526 free(my_id);
530 if (strncmp(refname, "refs/tags/", 10) == 0)
531 is_tag = 1;
533 /* Prevent tags from being overwritten by default. */
534 if (!overwrite_refs && my_ref && is_tag) {
535 err = got_error_fmt(GOT_ERR_SEND_TAG_EXISTS,
536 "%s", refname);
537 goto done;
540 /* Check if their object exists locally. */
541 err = got_object_open(&obj, repo, their_id);
542 if (err) {
543 if (err->code != GOT_ERR_NO_OBJ)
544 goto done;
545 if (!overwrite_refs && my_ref != NULL) {
546 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
547 "%s", refname);
548 goto done;
550 have_their_id = 0;
551 } else {
552 got_object_close(obj);
553 have_their_id = 1;
556 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
557 if (err)
558 goto done;
560 if (have_their_id) {
561 /* Enforce linear ancestry if required. */
562 if (!overwrite_refs && my_ref && !is_tag) {
563 struct got_object_id *my_id;
564 err = got_ref_resolve(&my_id, repo, my_ref);
565 if (err)
566 goto done;
567 err = check_linear_ancestry(refname, my_id,
568 their_id, repo, cancel_cb, cancel_arg);
569 free(my_id);
570 my_id = NULL;
571 if (err)
572 goto done;
574 /* Exclude any objects reachable via their ID. */
575 their_ids[ntheirs] = got_object_id_dup(their_id);
576 if (their_ids[ntheirs] == NULL) {
577 err = got_error_from_errno("got_object_id_dup");
578 goto done;
580 ntheirs++;
581 } else if (!is_tag) {
582 char *remote_refname;
583 struct got_reference *ref;
584 /*
585 * Exclude any objects which exist on the server
586 * according to a locally cached remote reference.
587 */
588 err = get_remote_refname(&remote_refname,
589 remote_name, refname);
590 if (err)
591 goto done;
592 err = got_ref_open(&ref, repo, remote_refname, 0);
593 free(remote_refname);
594 if (err) {
595 if (err->code != GOT_ERR_NOT_REF)
596 goto done;
597 } else {
598 err = got_ref_resolve(&their_ids[ntheirs],
599 repo, ref);
600 got_ref_close(ref);
601 if (err)
602 goto done;
603 ntheirs++;
608 /* Account for any new references we are going to upload. */
609 TAILQ_FOREACH(re, &refs, entry) {
610 if (find_their_ref(&their_refs,
611 got_ref_get_name(re->ref)) == NULL)
612 refs_to_send++;
615 /* Account for any existing references we are going to delete. */
616 TAILQ_FOREACH(pe, delete_branches, entry) {
617 const char *branchname = pe->path;
618 if (find_their_ref(&their_refs, branchname))
619 refs_to_delete++;
622 if (refs_to_send == 0 && refs_to_delete == 0) {
623 got_privsep_send_stop(imsg_sendfds[0]);
624 goto done;
627 if (refs_to_send > 0) {
628 memset(&ppa, 0, sizeof(ppa));
629 ppa.progress_cb = progress_cb;
630 ppa.progress_arg = progress_arg;
631 err = got_pack_create(packsha1, packfile, their_ids, ntheirs,
632 our_ids, nours, repo, 0, 1, pack_progress, &ppa,
633 cancel_cb, cancel_arg);
634 if (err)
635 goto done;
637 if (fflush(packfile) == -1) {
638 err = got_error_from_errno("fflush");
639 goto done;
642 npackfd = dup(fileno(packfile));
643 if (npackfd == -1) {
644 err = got_error_from_errno("dup");
645 goto done;
647 err = got_privsep_send_packfd(&sendibuf, npackfd);
648 if (err != NULL)
649 goto done;
650 npackfd = -1;
651 } else {
652 err = got_privsep_send_packfd(&sendibuf, -1);
653 if (err != NULL)
654 goto done;
657 while (!done) {
658 int success = 0;
659 char *refname = NULL;
660 off_t bytes_sent_cur = 0;
661 if (cancel_cb) {
662 err = (*cancel_cb)(cancel_arg);
663 if (err)
664 goto done;
666 err = got_privsep_recv_send_progress(&done, &bytes_sent,
667 &success, &refname, &sendibuf);
668 if (err)
669 goto done;
670 if (refname && got_ref_name_is_valid(refname) && success &&
671 strncmp(refname, "refs/tags/", 10) != 0) {
672 struct got_reference *my_ref;
673 /*
674 * The server has accepted our changes.
675 * Update our reference in refs/remotes/ accordingly.
676 */
677 my_ref = find_ref(&refs, refname);
678 if (my_ref) {
679 err = update_remote_ref(my_ref, remote_name,
680 repo);
681 if (err)
682 goto done;
685 if (refname != NULL ||
686 bytes_sent_cur != bytes_sent) {
687 err = progress_cb(progress_arg, ppa.packfile_size,
688 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
689 ppa.nobj_written, bytes_sent,
690 refname, success);
691 if (err) {
692 free(refname);
693 goto done;
695 bytes_sent_cur = bytes_sent;
697 free(refname);
699 done:
700 if (sendpid != -1) {
701 if (err)
702 got_privsep_send_stop(imsg_sendfds[0]);
703 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
704 err = got_error_from_errno("waitpid");
706 if (packfile && fclose(packfile) == EOF && err == NULL)
707 err = got_error_from_errno("fclose");
708 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
709 err = got_error_from_errno("close");
710 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
711 err = got_error_from_errno("close");
713 got_ref_list_free(&refs);
714 got_pathlist_free(&have_refs);
715 got_pathlist_free(&their_refs);
716 for (i = 0; i < nours; i++)
717 free(our_ids[i]);
718 free(our_ids);
719 for (i = 0; i < ntheirs; i++)
720 free(their_ids[i]);
721 free(their_ids);
722 return err;