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 off_t packfile_size;
103 int ncommits;
104 int nobj_total;
105 int nobj_deltify;
106 int nobj_written;
107 };
109 static const struct got_error *
110 pack_progress(void *arg, off_t packfile_size, int ncommits,
111 int nobj_total, int nobj_deltify, int nobj_written)
113 const struct got_error *err;
114 struct pack_progress_arg *a = arg;
116 err = a->progress_cb(a->progress_arg, packfile_size, ncommits,
117 nobj_total, nobj_deltify, nobj_written, 0, NULL, 0);
118 if (err)
119 return err;
121 a->packfile_size = packfile_size;
122 a->ncommits = ncommits;
123 a->nobj_total = nobj_total;
124 a->nobj_deltify = nobj_deltify;
125 a->nobj_written = nobj_written;
126 return NULL;
129 static const struct got_error *
130 insert_ref(struct got_reflist_head *refs, const char *refname,
131 struct got_repository *repo)
133 const struct got_error *err;
134 struct got_reference *ref;
135 struct got_reflist_entry *new;
137 err = got_ref_open(&ref, repo, refname, 0);
138 if (err)
139 return err;
141 err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
142 if (err || new == NULL /* duplicate */)
143 got_ref_close(ref);
145 return err;
148 static const struct got_error *
149 check_linear_ancestry(const char *refname, struct got_object_id *my_id,
150 struct got_object_id *their_id, struct got_repository *repo,
151 got_cancel_cb cancel_cb, void *cancel_arg)
153 const struct got_error *err = NULL;
154 struct got_object_id *yca_id;
155 int obj_type;
157 err = got_object_get_type(&obj_type, repo, their_id);
158 if (err)
159 return err;
160 if (obj_type != GOT_OBJ_TYPE_COMMIT)
161 return got_error_fmt(GOT_ERR_OBJ_TYPE,
162 "bad object type on server for %s", refname);
164 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
165 my_id, their_id, 1, repo, cancel_cb, cancel_arg);
166 if (err)
167 return err;
168 if (yca_id == NULL)
169 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
171 /*
172 * Require a straight line of history between the two commits,
173 * with their commit being older than my commit.
175 * Non-linear situations such as this require a rebase:
177 * (theirs) D F (mine)
178 * \ /
179 * C E
180 * \ /
181 * B (yca)
182 * |
183 * A
184 */
185 if (got_object_id_cmp(their_id, yca_id) != 0)
186 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
188 free(yca_id);
189 return err;
192 static const struct got_error *
193 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
195 struct got_object_id **new;
196 const size_t alloc_chunksz = 256;
198 if (*nalloc >= n)
199 return NULL;
201 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
202 sizeof(struct got_object_id));
203 if (new == NULL)
204 return got_error_from_errno("recallocarray");
206 *ids = new;
207 *nalloc += alloc_chunksz;
208 return NULL;
211 static struct got_reference *
212 find_ref(struct got_reflist_head *refs, const char *refname)
214 struct got_reflist_entry *re;
216 TAILQ_FOREACH(re, refs, entry) {
217 if (got_path_cmp(got_ref_get_name(re->ref), refname,
218 strlen(got_ref_get_name(re->ref)),
219 strlen(refname)) == 0) {
220 return re->ref;
224 return NULL;
227 static struct got_pathlist_entry *
228 find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
230 struct got_pathlist_entry *pe;
232 TAILQ_FOREACH(pe, their_refs, entry) {
233 const char *their_refname = pe->path;
234 if (got_path_cmp(their_refname, refname,
235 strlen(their_refname), strlen(refname)) == 0) {
236 return pe;
240 return NULL;
243 static const struct got_error *
244 get_remote_refname(char **remote_refname, const char *remote_name,
245 const char *refname)
247 if (strncmp(refname, "refs/", 5) == 0)
248 refname += 5;
249 if (strncmp(refname, "heads/", 6) == 0)
250 refname += 6;
252 if (asprintf(remote_refname, "refs/remotes/%s/%s",
253 remote_name, refname) == -1)
254 return got_error_from_errno("asprintf");
256 return NULL;
259 static const struct got_error *
260 update_remote_ref(struct got_reference *my_ref, const char *remote_name,
261 struct got_repository *repo)
263 const struct got_error *err, *unlock_err;
264 struct got_object_id *my_id;
265 struct got_reference *ref = NULL;
266 char *remote_refname = NULL;
267 int ref_locked = 0;
269 err = got_ref_resolve(&my_id, repo, my_ref);
270 if (err)
271 return err;
273 err = get_remote_refname(&remote_refname, remote_name,
274 got_ref_get_name(my_ref));
275 if (err)
276 goto done;
278 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
279 if (err) {
280 if (err->code != GOT_ERR_NOT_REF)
281 goto done;
282 err = got_ref_alloc(&ref, remote_refname, my_id);
283 if (err)
284 goto done;
285 } else {
286 ref_locked = 1;
287 err = got_ref_change_ref(ref, my_id);
288 if (err)
289 goto done;
292 err = got_ref_write(ref, repo);
293 done:
294 if (ref) {
295 if (ref_locked) {
296 unlock_err = got_ref_unlock(ref);
297 if (unlock_err && err == NULL)
298 err = unlock_err;
300 got_ref_close(ref);
302 free(my_id);
303 free(remote_refname);
304 return err;
307 const struct got_error*
308 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
309 struct got_pathlist_head *tag_names,
310 struct got_pathlist_head *delete_branches,
311 int verbosity, int overwrite_refs, int sendfd,
312 struct got_repository *repo, got_send_progress_cb progress_cb,
313 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
315 int imsg_sendfds[2];
316 int npackfd = -1, nsendfd = -1;
317 int sendstatus, done = 0;
318 const struct got_error *err;
319 struct imsgbuf sendibuf;
320 pid_t sendpid = -1;
321 struct got_reflist_head refs;
322 struct got_pathlist_head have_refs;
323 struct got_pathlist_head their_refs;
324 struct got_pathlist_entry *pe;
325 struct got_reflist_entry *re;
326 struct got_object_id **our_ids = NULL;
327 struct got_object_id **their_ids = NULL;
328 int i, nours = 0, ntheirs = 0;
329 size_t nalloc_ours = 0, nalloc_theirs = 0;
330 int refs_to_send = 0, refs_to_delete = 0;
331 off_t bytes_sent = 0;
332 struct pack_progress_arg ppa;
333 uint8_t packsha1[SHA1_DIGEST_LENGTH];
334 FILE *packfile = NULL;
336 TAILQ_INIT(&refs);
337 TAILQ_INIT(&have_refs);
338 TAILQ_INIT(&their_refs);
340 TAILQ_FOREACH(pe, branch_names, entry) {
341 const char *branchname = pe->path;
342 if (strncmp(branchname, "refs/heads/", 11) != 0) {
343 char *s;
344 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
345 err = got_error_from_errno("asprintf");
346 goto done;
348 err = insert_ref(&refs, s, repo);
349 free(s);
350 } else {
351 err = insert_ref(&refs, branchname, repo);
353 if (err)
354 goto done;
357 TAILQ_FOREACH(pe, delete_branches, entry) {
358 const char *branchname = pe->path;
359 struct got_reference *ref;
360 if (strncmp(branchname, "refs/heads/", 11) != 0) {
361 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
362 branchname);
363 goto done;
365 ref = find_ref(&refs, branchname);
366 if (ref) {
367 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
368 "changes on %s will be sent to server",
369 branchname);
370 goto done;
374 TAILQ_FOREACH(pe, tag_names, entry) {
375 const char *tagname = pe->path;
376 if (strncmp(tagname, "refs/tags/", 10) != 0) {
377 char *s;
378 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
379 err = got_error_from_errno("asprintf");
380 goto done;
382 err = insert_ref(&refs, s, repo);
383 free(s);
384 } else {
385 err = insert_ref(&refs, tagname, repo);
387 if (err)
388 goto done;
391 if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
392 err = got_error(GOT_ERR_SEND_EMPTY);
393 goto done;
396 TAILQ_FOREACH(re, &refs, entry) {
397 struct got_object_id *id;
398 int obj_type;
400 if (got_ref_is_symbolic(re->ref)) {
401 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
402 "cannot send symbolic reference %s",
403 got_ref_get_name(re->ref));
404 goto done;
407 err = got_ref_resolve(&id, repo, re->ref);
408 if (err)
409 goto done;
410 err = got_object_get_type(&obj_type, repo, id);
411 free(id);
412 if (err)
413 goto done;
414 switch (obj_type) {
415 case GOT_OBJ_TYPE_COMMIT:
416 case GOT_OBJ_TYPE_TAG:
417 break;
418 default:
419 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
420 "cannot send %s", got_ref_get_name(re->ref));
421 goto done;
425 packfile = got_opentemp();
426 if (packfile == NULL) {
427 err = got_error_from_errno("got_opentemp");
428 goto done;
431 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
432 err = got_error_from_errno("socketpair");
433 goto done;
436 sendpid = fork();
437 if (sendpid == -1) {
438 err = got_error_from_errno("fork");
439 goto done;
440 } else if (sendpid == 0){
441 got_privsep_exec_child(imsg_sendfds,
442 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
445 if (close(imsg_sendfds[1]) == -1) {
446 err = got_error_from_errno("close");
447 goto done;
449 imsg_init(&sendibuf, imsg_sendfds[0]);
450 nsendfd = dup(sendfd);
451 if (nsendfd == -1) {
452 err = got_error_from_errno("dup");
453 goto done;
456 /*
457 * Convert reflist to pathlist since the privsep layer
458 * is linked into helper programs which lack reference.c.
459 */
460 TAILQ_FOREACH(re, &refs, entry) {
461 struct got_object_id *id;
462 err = got_ref_resolve(&id, repo, re->ref);
463 if (err)
464 goto done;
465 err = got_pathlist_append(&have_refs,
466 got_ref_get_name(re->ref), id);
467 if (err)
468 goto done;
469 /*
470 * Also prepare the array of our object IDs which
471 * will be needed for generating a pack file.
472 */
473 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
474 if (err)
475 goto done;
476 our_ids[nours] = id;
477 nours++;
480 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
481 delete_branches, verbosity);
482 if (err)
483 goto done;
484 nsendfd = -1;
486 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
487 if (err)
488 goto done;
490 /*
491 * Process references reported by the server.
492 * Push appropriate object IDs onto the "their IDs" array.
493 * This array will be used to exclude objects which already
494 * exist on the server from our pack file.
495 */
496 TAILQ_FOREACH(pe, &their_refs, entry) {
497 const char *refname = pe->path;
498 struct got_object_id *their_id = pe->data;
499 int have_their_id;
500 struct got_object *obj;
501 struct got_reference *my_ref = NULL;
502 int is_tag = 0;
504 /* Don't blindly trust the server to send us valid names. */
505 if (!got_ref_name_is_valid(refname))
506 continue;
508 if (strncmp(refname, "refs/tags/", 10) == 0)
509 is_tag = 1;
510 /*
511 * Find out whether this is a reference we want to upload.
512 * Otherwise we can still use this reference as a hint to
513 * avoid uploading any objects the server already has.
514 */
515 my_ref = find_ref(&refs, refname);
516 if (my_ref) {
517 struct got_object_id *my_id;
518 err = got_ref_resolve(&my_id, repo, my_ref);
519 if (err)
520 goto done;
521 if (got_object_id_cmp(my_id, their_id) != 0) {
522 if (!overwrite_refs && is_tag) {
523 err = got_error_fmt(
524 GOT_ERR_SEND_TAG_EXISTS,
525 "%s", refname);
526 free(my_id);
527 goto done;
529 refs_to_send++;
531 free(my_id);
534 /* Check if their object exists locally. */
535 err = got_object_open(&obj, repo, their_id);
536 if (err) {
537 if (err->code != GOT_ERR_NO_OBJ)
538 goto done;
539 if (!overwrite_refs && my_ref != NULL) {
540 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
541 "%s", refname);
542 goto done;
544 have_their_id = 0;
545 } else {
546 got_object_close(obj);
547 have_their_id = 1;
550 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
551 if (err)
552 goto done;
554 if (have_their_id) {
555 /* Enforce linear ancestry if required. */
556 if (!overwrite_refs && my_ref && !is_tag) {
557 struct got_object_id *my_id;
558 err = got_ref_resolve(&my_id, repo, my_ref);
559 if (err)
560 goto done;
561 err = check_linear_ancestry(refname, my_id,
562 their_id, repo, cancel_cb, cancel_arg);
563 free(my_id);
564 my_id = NULL;
565 if (err)
566 goto done;
568 /* Exclude any objects reachable via their ID. */
569 their_ids[ntheirs] = got_object_id_dup(their_id);
570 if (their_ids[ntheirs] == NULL) {
571 err = got_error_from_errno("got_object_id_dup");
572 goto done;
574 ntheirs++;
575 } else if (!is_tag) {
576 char *remote_refname;
577 struct got_reference *ref;
578 /*
579 * Exclude any objects which exist on the server
580 * according to a locally cached remote reference.
581 */
582 err = get_remote_refname(&remote_refname,
583 remote_name, refname);
584 if (err)
585 goto done;
586 err = got_ref_open(&ref, repo, remote_refname, 0);
587 free(remote_refname);
588 if (err) {
589 if (err->code != GOT_ERR_NOT_REF)
590 goto done;
591 } else {
592 err = got_ref_resolve(&their_ids[ntheirs],
593 repo, ref);
594 got_ref_close(ref);
595 if (err)
596 goto done;
597 ntheirs++;
602 /* Account for any new references we are going to upload. */
603 TAILQ_FOREACH(re, &refs, entry) {
604 if (find_their_ref(&their_refs,
605 got_ref_get_name(re->ref)) == NULL)
606 refs_to_send++;
609 /* Account for any existing references we are going to delete. */
610 TAILQ_FOREACH(pe, delete_branches, entry) {
611 const char *branchname = pe->path;
612 if (find_their_ref(&their_refs, branchname))
613 refs_to_delete++;
616 if (refs_to_send == 0 && refs_to_delete == 0) {
617 got_privsep_send_stop(imsg_sendfds[0]);
618 goto done;
621 if (refs_to_send > 0) {
622 memset(&ppa, 0, sizeof(ppa));
623 ppa.progress_cb = progress_cb;
624 ppa.progress_arg = progress_arg;
625 err = got_pack_create(packsha1, packfile, their_ids, ntheirs,
626 our_ids, nours, repo, 0, 1, pack_progress, &ppa,
627 cancel_cb, cancel_arg);
628 if (err)
629 goto done;
631 if (fflush(packfile) == -1) {
632 err = got_error_from_errno("fflush");
633 goto done;
636 npackfd = dup(fileno(packfile));
637 if (npackfd == -1) {
638 err = got_error_from_errno("dup");
639 goto done;
641 err = got_privsep_send_packfd(&sendibuf, npackfd);
642 if (err != NULL)
643 goto done;
644 npackfd = -1;
645 } else {
646 err = got_privsep_send_packfd(&sendibuf, -1);
647 if (err != NULL)
648 goto done;
651 while (!done) {
652 int success = 0;
653 char *refname = NULL;
654 off_t bytes_sent_cur = 0;
655 if (cancel_cb) {
656 err = (*cancel_cb)(cancel_arg);
657 if (err)
658 goto done;
660 err = got_privsep_recv_send_progress(&done, &bytes_sent,
661 &success, &refname, &sendibuf);
662 if (err)
663 goto done;
664 if (refname && got_ref_name_is_valid(refname) && success &&
665 strncmp(refname, "refs/tags/", 10) != 0) {
666 struct got_reference *my_ref;
667 /*
668 * The server has accepted our changes.
669 * Update our reference in refs/remotes/ accordingly.
670 */
671 my_ref = find_ref(&refs, refname);
672 if (my_ref) {
673 err = update_remote_ref(my_ref, remote_name,
674 repo);
675 if (err)
676 goto done;
679 if (refname != NULL ||
680 bytes_sent_cur != bytes_sent) {
681 err = progress_cb(progress_arg, ppa.packfile_size,
682 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
683 ppa.nobj_written, bytes_sent,
684 refname, success);
685 if (err) {
686 free(refname);
687 goto done;
689 bytes_sent_cur = bytes_sent;
691 free(refname);
693 done:
694 if (sendpid != -1) {
695 if (err)
696 got_privsep_send_stop(imsg_sendfds[0]);
697 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
698 err = got_error_from_errno("waitpid");
700 if (packfile && fclose(packfile) == EOF && err == NULL)
701 err = got_error_from_errno("fclose");
702 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
703 err = got_error_from_errno("close");
704 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
705 err = got_error_from_errno("close");
707 got_ref_list_free(&refs);
708 got_pathlist_free(&have_refs);
709 got_pathlist_free(&their_refs);
710 for (i = 0; i < nours; i++)
711 free(our_ids[i]);
712 free(our_ids);
713 for (i = 0; i < ntheirs; i++)
714 free(their_ids[i]);
715 free(their_ids);
716 return err;