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>
39 #include "got_error.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_object.h"
46 #include "got_opentemp.h"
47 #include "got_send.h"
48 #include "got_repository_admin.h"
49 #include "got_commit_graph.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_sha1.h"
58 #include "got_lib_privsep.h"
59 #include "got_lib_object_cache.h"
60 #include "got_lib_repository.h"
61 #include "got_lib_pack_create.h"
62 #include "got_lib_dial.h"
64 #ifndef nitems
65 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 #endif
68 #ifndef ssizeof
69 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
70 #endif
72 #ifndef MIN
73 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
74 #endif
76 const struct got_error *
77 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
78 const char *host, const char *port, const char *server_path, int verbosity)
79 {
80 const struct got_error *err = NULL;
82 *sendpid = -1;
83 *sendfd = -1;
85 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
86 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
87 GOT_DIAL_DIRECTION_SEND, verbosity);
88 else if (strcmp(proto, "git") == 0)
89 err = got_dial_git(sendfd, host, port, server_path,
90 GOT_DIAL_DIRECTION_SEND);
91 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
92 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
93 else
94 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
95 return err;
96 }
98 struct pack_progress_arg {
99 got_send_progress_cb progress_cb;
100 void *progress_arg;
102 int ncolored;
103 int nfound;
104 int ntrees;
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, int ncolored, int nfound, int ntrees,
114 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
115 int nobj_written)
117 const struct got_error *err;
118 struct pack_progress_arg *a = arg;
120 err = a->progress_cb(a->progress_arg, ncolored, nfound, ntrees,
121 packfile_size, ncommits, nobj_total, nobj_deltify,
122 nobj_written, 0, NULL, 0);
123 if (err)
124 return err;
126 a->ncolored= ncolored;
127 a->nfound = nfound;
128 a->ntrees = ntrees;
129 a->packfile_size = packfile_size;
130 a->ncommits = ncommits;
131 a->nobj_total = nobj_total;
132 a->nobj_deltify = nobj_deltify;
133 a->nobj_written = nobj_written;
134 return NULL;
137 static const struct got_error *
138 insert_ref(struct got_reflist_head *refs, const char *refname,
139 struct got_repository *repo)
141 const struct got_error *err;
142 struct got_reference *ref;
143 struct got_reflist_entry *new;
145 err = got_ref_open(&ref, repo, refname, 0);
146 if (err)
147 return err;
149 err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
150 if (err || new == NULL /* duplicate */)
151 got_ref_close(ref);
153 return err;
156 static const struct got_error *
157 check_linear_ancestry(const char *refname, struct got_object_id *my_id,
158 struct got_object_id *their_id, struct got_repository *repo,
159 got_cancel_cb cancel_cb, void *cancel_arg)
161 const struct got_error *err = NULL;
162 struct got_object_id *yca_id;
163 int obj_type;
165 err = got_object_get_type(&obj_type, repo, their_id);
166 if (err)
167 return err;
168 if (obj_type != GOT_OBJ_TYPE_COMMIT)
169 return got_error_fmt(GOT_ERR_OBJ_TYPE,
170 "bad object type on server for %s", refname);
172 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
173 my_id, their_id, 1, repo, cancel_cb, cancel_arg);
174 if (err)
175 return err;
176 if (yca_id == NULL)
177 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
179 /*
180 * Require a straight line of history between the two commits,
181 * with their commit being older than my commit.
183 * Non-linear situations such as this require a rebase:
185 * (theirs) D F (mine)
186 * \ /
187 * C E
188 * \ /
189 * B (yca)
190 * |
191 * A
192 */
193 if (got_object_id_cmp(their_id, yca_id) != 0)
194 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
196 free(yca_id);
197 return err;
200 static const struct got_error *
201 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
203 struct got_object_id **new;
204 const size_t alloc_chunksz = 256;
206 if (*nalloc >= n)
207 return NULL;
209 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
210 sizeof(struct got_object_id));
211 if (new == NULL)
212 return got_error_from_errno("recallocarray");
214 *ids = new;
215 *nalloc += alloc_chunksz;
216 return NULL;
219 static struct got_reference *
220 find_ref(struct got_reflist_head *refs, const char *refname)
222 struct got_reflist_entry *re;
224 TAILQ_FOREACH(re, refs, entry) {
225 if (got_path_cmp(got_ref_get_name(re->ref), refname,
226 strlen(got_ref_get_name(re->ref)),
227 strlen(refname)) == 0) {
228 return re->ref;
232 return NULL;
235 static struct got_pathlist_entry *
236 find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
238 struct got_pathlist_entry *pe;
240 TAILQ_FOREACH(pe, their_refs, entry) {
241 const char *their_refname = pe->path;
242 if (got_path_cmp(their_refname, refname,
243 strlen(their_refname), strlen(refname)) == 0) {
244 return pe;
248 return NULL;
251 static const struct got_error *
252 get_remote_refname(char **remote_refname, const char *remote_name,
253 const char *refname)
255 if (strncmp(refname, "refs/", 5) == 0)
256 refname += 5;
257 if (strncmp(refname, "heads/", 6) == 0)
258 refname += 6;
260 if (asprintf(remote_refname, "refs/remotes/%s/%s",
261 remote_name, refname) == -1)
262 return got_error_from_errno("asprintf");
264 return NULL;
267 static const struct got_error *
268 update_remote_ref(struct got_reference *my_ref, const char *remote_name,
269 struct got_repository *repo)
271 const struct got_error *err, *unlock_err;
272 struct got_object_id *my_id;
273 struct got_reference *ref = NULL;
274 char *remote_refname = NULL;
275 int ref_locked = 0;
277 err = got_ref_resolve(&my_id, repo, my_ref);
278 if (err)
279 return err;
281 err = get_remote_refname(&remote_refname, remote_name,
282 got_ref_get_name(my_ref));
283 if (err)
284 goto done;
286 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
287 if (err) {
288 if (err->code != GOT_ERR_NOT_REF)
289 goto done;
290 err = got_ref_alloc(&ref, remote_refname, my_id);
291 if (err)
292 goto done;
293 } else {
294 ref_locked = 1;
295 err = got_ref_change_ref(ref, my_id);
296 if (err)
297 goto done;
300 err = got_ref_write(ref, repo);
301 done:
302 if (ref) {
303 if (ref_locked) {
304 unlock_err = got_ref_unlock(ref);
305 if (unlock_err && err == NULL)
306 err = unlock_err;
308 got_ref_close(ref);
310 free(my_id);
311 free(remote_refname);
312 return err;
315 const struct got_error*
316 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
317 struct got_pathlist_head *tag_names,
318 struct got_pathlist_head *delete_branches,
319 int verbosity, int overwrite_refs, int sendfd,
320 struct got_repository *repo, got_send_progress_cb progress_cb,
321 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
323 int imsg_sendfds[2];
324 int npackfd = -1, nsendfd = -1;
325 int sendstatus, done = 0;
326 const struct got_error *err;
327 struct imsgbuf sendibuf;
328 pid_t sendpid = -1;
329 struct got_reflist_head refs;
330 struct got_pathlist_head have_refs;
331 struct got_pathlist_head their_refs;
332 struct got_pathlist_entry *pe;
333 struct got_reflist_entry *re;
334 struct got_object_id **our_ids = NULL;
335 struct got_object_id **their_ids = NULL;
336 int i, nours = 0, ntheirs = 0;
337 size_t nalloc_ours = 0, nalloc_theirs = 0;
338 int refs_to_send = 0, refs_to_delete = 0;
339 off_t bytes_sent = 0;
340 struct pack_progress_arg ppa;
341 uint8_t packsha1[SHA1_DIGEST_LENGTH];
342 FILE *packfile = NULL;
344 TAILQ_INIT(&refs);
345 TAILQ_INIT(&have_refs);
346 TAILQ_INIT(&their_refs);
348 TAILQ_FOREACH(pe, branch_names, entry) {
349 const char *branchname = pe->path;
350 if (strncmp(branchname, "refs/heads/", 11) != 0) {
351 char *s;
352 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
353 err = got_error_from_errno("asprintf");
354 goto done;
356 err = insert_ref(&refs, s, repo);
357 free(s);
358 } else {
359 err = insert_ref(&refs, branchname, repo);
361 if (err)
362 goto done;
365 TAILQ_FOREACH(pe, delete_branches, entry) {
366 const char *branchname = pe->path;
367 struct got_reference *ref;
368 if (strncmp(branchname, "refs/heads/", 11) != 0) {
369 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
370 branchname);
371 goto done;
373 ref = find_ref(&refs, branchname);
374 if (ref) {
375 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
376 "changes on %s will be sent to server",
377 branchname);
378 goto done;
382 TAILQ_FOREACH(pe, tag_names, entry) {
383 const char *tagname = pe->path;
384 if (strncmp(tagname, "refs/tags/", 10) != 0) {
385 char *s;
386 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
387 err = got_error_from_errno("asprintf");
388 goto done;
390 err = insert_ref(&refs, s, repo);
391 free(s);
392 } else {
393 err = insert_ref(&refs, tagname, repo);
395 if (err)
396 goto done;
399 if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
400 err = got_error(GOT_ERR_SEND_EMPTY);
401 goto done;
404 TAILQ_FOREACH(re, &refs, entry) {
405 struct got_object_id *id;
406 int obj_type;
408 if (got_ref_is_symbolic(re->ref)) {
409 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
410 "cannot send symbolic reference %s",
411 got_ref_get_name(re->ref));
412 goto done;
415 err = got_ref_resolve(&id, repo, re->ref);
416 if (err)
417 goto done;
418 err = got_object_get_type(&obj_type, repo, id);
419 free(id);
420 if (err)
421 goto done;
422 switch (obj_type) {
423 case GOT_OBJ_TYPE_COMMIT:
424 case GOT_OBJ_TYPE_TAG:
425 break;
426 default:
427 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
428 "cannot send %s", got_ref_get_name(re->ref));
429 goto done;
433 packfile = got_opentemp();
434 if (packfile == NULL) {
435 err = got_error_from_errno("got_opentemp");
436 goto done;
439 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
440 err = got_error_from_errno("socketpair");
441 goto done;
444 sendpid = fork();
445 if (sendpid == -1) {
446 err = got_error_from_errno("fork");
447 goto done;
448 } else if (sendpid == 0){
449 got_privsep_exec_child(imsg_sendfds,
450 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
453 if (close(imsg_sendfds[1]) == -1) {
454 err = got_error_from_errno("close");
455 goto done;
457 imsg_init(&sendibuf, imsg_sendfds[0]);
458 nsendfd = dup(sendfd);
459 if (nsendfd == -1) {
460 err = got_error_from_errno("dup");
461 goto done;
464 /*
465 * Convert reflist to pathlist since the privsep layer
466 * is linked into helper programs which lack reference.c.
467 */
468 TAILQ_FOREACH(re, &refs, entry) {
469 struct got_object_id *id;
470 err = got_ref_resolve(&id, repo, re->ref);
471 if (err)
472 goto done;
473 err = got_pathlist_append(&have_refs,
474 got_ref_get_name(re->ref), id);
475 if (err)
476 goto done;
477 /*
478 * Also prepare the array of our object IDs which
479 * will be needed for generating a pack file.
480 */
481 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
482 if (err)
483 goto done;
484 our_ids[nours] = id;
485 nours++;
488 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
489 delete_branches, verbosity);
490 if (err)
491 goto done;
492 nsendfd = -1;
494 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
495 if (err)
496 goto done;
498 /*
499 * Process references reported by the server.
500 * Push appropriate object IDs onto the "their IDs" array.
501 * This array will be used to exclude objects which already
502 * exist on the server from our pack file.
503 */
504 TAILQ_FOREACH(pe, &their_refs, entry) {
505 const char *refname = pe->path;
506 struct got_object_id *their_id = pe->data;
507 int have_their_id;
508 struct got_object *obj;
509 struct got_reference *my_ref = NULL;
510 int is_tag = 0;
512 /* Don't blindly trust the server to send us valid names. */
513 if (!got_ref_name_is_valid(refname))
514 continue;
516 if (strncmp(refname, "refs/tags/", 10) == 0)
517 is_tag = 1;
518 /*
519 * Find out whether this is a reference we want to upload.
520 * Otherwise we can still use this reference as a hint to
521 * avoid uploading any objects the server already has.
522 */
523 my_ref = find_ref(&refs, refname);
524 if (my_ref) {
525 struct got_object_id *my_id;
526 err = got_ref_resolve(&my_id, repo, my_ref);
527 if (err)
528 goto done;
529 if (got_object_id_cmp(my_id, their_id) != 0) {
530 if (!overwrite_refs && is_tag) {
531 err = got_error_fmt(
532 GOT_ERR_SEND_TAG_EXISTS,
533 "%s", refname);
534 free(my_id);
535 goto done;
537 refs_to_send++;
539 free(my_id);
542 /* Check if their object exists locally. */
543 err = got_object_open(&obj, repo, their_id);
544 if (err) {
545 if (err->code != GOT_ERR_NO_OBJ)
546 goto done;
547 if (!overwrite_refs && my_ref != NULL) {
548 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
549 "%s", refname);
550 goto done;
552 have_their_id = 0;
553 } else {
554 got_object_close(obj);
555 have_their_id = 1;
558 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
559 if (err)
560 goto done;
562 if (have_their_id) {
563 /* Enforce linear ancestry if required. */
564 if (!overwrite_refs && my_ref && !is_tag) {
565 struct got_object_id *my_id;
566 err = got_ref_resolve(&my_id, repo, my_ref);
567 if (err)
568 goto done;
569 err = check_linear_ancestry(refname, my_id,
570 their_id, repo, cancel_cb, cancel_arg);
571 free(my_id);
572 my_id = NULL;
573 if (err)
574 goto done;
576 /* Exclude any objects reachable via their ID. */
577 their_ids[ntheirs] = got_object_id_dup(their_id);
578 if (their_ids[ntheirs] == NULL) {
579 err = got_error_from_errno("got_object_id_dup");
580 goto done;
582 ntheirs++;
583 } else if (!is_tag) {
584 char *remote_refname;
585 struct got_reference *ref;
586 /*
587 * Exclude any objects which exist on the server
588 * according to a locally cached remote reference.
589 */
590 err = get_remote_refname(&remote_refname,
591 remote_name, refname);
592 if (err)
593 goto done;
594 err = got_ref_open(&ref, repo, remote_refname, 0);
595 free(remote_refname);
596 if (err) {
597 if (err->code != GOT_ERR_NOT_REF)
598 goto done;
599 } else {
600 err = got_ref_resolve(&their_ids[ntheirs],
601 repo, ref);
602 got_ref_close(ref);
603 if (err)
604 goto done;
605 ntheirs++;
610 /* Account for any new references we are going to upload. */
611 TAILQ_FOREACH(re, &refs, entry) {
612 if (find_their_ref(&their_refs,
613 got_ref_get_name(re->ref)) == NULL)
614 refs_to_send++;
617 /* Account for any existing references we are going to delete. */
618 TAILQ_FOREACH(pe, delete_branches, entry) {
619 const char *branchname = pe->path;
620 if (find_their_ref(&their_refs, branchname))
621 refs_to_delete++;
624 if (refs_to_send == 0 && refs_to_delete == 0) {
625 got_privsep_send_stop(imsg_sendfds[0]);
626 goto done;
629 if (refs_to_send > 0) {
630 memset(&ppa, 0, sizeof(ppa));
631 ppa.progress_cb = progress_cb;
632 ppa.progress_arg = progress_arg;
633 err = got_pack_create(packsha1, packfile, their_ids, ntheirs,
634 our_ids, nours, repo, 0, 1, pack_progress, &ppa,
635 cancel_cb, cancel_arg);
636 if (err)
637 goto done;
639 if (fflush(packfile) == -1) {
640 err = got_error_from_errno("fflush");
641 goto done;
644 npackfd = dup(fileno(packfile));
645 if (npackfd == -1) {
646 err = got_error_from_errno("dup");
647 goto done;
649 err = got_privsep_send_packfd(&sendibuf, npackfd);
650 if (err != NULL)
651 goto done;
652 npackfd = -1;
653 } else {
654 err = got_privsep_send_packfd(&sendibuf, -1);
655 if (err != NULL)
656 goto done;
659 while (!done) {
660 int success = 0;
661 char *refname = NULL;
662 off_t bytes_sent_cur = 0;
663 if (cancel_cb) {
664 err = (*cancel_cb)(cancel_arg);
665 if (err)
666 goto done;
668 err = got_privsep_recv_send_progress(&done, &bytes_sent,
669 &success, &refname, &sendibuf);
670 if (err)
671 goto done;
672 if (refname && got_ref_name_is_valid(refname) && success &&
673 strncmp(refname, "refs/tags/", 10) != 0) {
674 struct got_reference *my_ref;
675 /*
676 * The server has accepted our changes.
677 * Update our reference in refs/remotes/ accordingly.
678 */
679 my_ref = find_ref(&refs, refname);
680 if (my_ref) {
681 err = update_remote_ref(my_ref, remote_name,
682 repo);
683 if (err)
684 goto done;
687 if (refname != NULL ||
688 bytes_sent_cur != bytes_sent) {
689 err = progress_cb(progress_arg, ppa.ncolored,
690 ppa.nfound, ppa.ntrees, ppa.packfile_size,
691 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
692 ppa.nobj_written, bytes_sent,
693 refname, success);
694 if (err) {
695 free(refname);
696 goto done;
698 bytes_sent_cur = bytes_sent;
700 free(refname);
702 done:
703 if (sendpid != -1) {
704 if (err)
705 got_privsep_send_stop(imsg_sendfds[0]);
706 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
707 err = got_error_from_errno("waitpid");
709 if (packfile && fclose(packfile) == EOF && err == NULL)
710 err = got_error_from_errno("fclose");
711 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
712 err = got_error_from_errno("close");
713 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
714 err = got_error_from_errno("close");
716 got_ref_list_free(&refs);
717 got_pathlist_free(&have_refs);
718 got_pathlist_free(&their_refs);
719 for (i = 0; i < nours; i++)
720 free(our_ids[i]);
721 free(our_ids);
722 for (i = 0; i < ntheirs; i++)
723 free(their_ids[i]);
724 free(their_ids);
725 return err;