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 <errno.h>
28 #include <err.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdint.h>
34 #include <unistd.h>
35 #include <zlib.h>
36 #include <ctype.h>
37 #include <limits.h>
38 #include <time.h>
40 #include "got_error.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_object.h"
47 #include "got_opentemp.h"
48 #include "got_send.h"
49 #include "got_repository_admin.h"
50 #include "got_commit_graph.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_inflate.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_pack.h"
58 #include "got_lib_sha1.h"
59 #include "got_lib_privsep.h"
60 #include "got_lib_object_cache.h"
61 #include "got_lib_repository.h"
62 #include "got_lib_ratelimit.h"
63 #include "got_lib_pack_create.h"
64 #include "got_lib_dial.h"
66 #ifndef nitems
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 #endif
70 #ifndef ssizeof
71 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
72 #endif
74 #ifndef MIN
75 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
76 #endif
78 const struct got_error *
79 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
80 const char *host, const char *port, const char *server_path, int verbosity)
81 {
82 const struct got_error *err = NULL;
84 *sendpid = -1;
85 *sendfd = -1;
87 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
88 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
89 GOT_DIAL_DIRECTION_SEND, verbosity);
90 else if (strcmp(proto, "git") == 0)
91 err = got_dial_git(sendfd, host, port, server_path,
92 GOT_DIAL_DIRECTION_SEND);
93 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
94 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
95 else
96 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
97 return err;
98 }
100 struct pack_progress_arg {
101 got_send_progress_cb progress_cb;
102 void *progress_arg;
104 int ncolored;
105 int nfound;
106 int ntrees;
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, int ncolored, int nfound, int ntrees,
116 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
117 int nobj_written)
119 const struct got_error *err;
120 struct pack_progress_arg *a = arg;
122 err = a->progress_cb(a->progress_arg, ncolored, nfound, ntrees,
123 packfile_size, ncommits, nobj_total, nobj_deltify,
124 nobj_written, 0, NULL, NULL, 0);
125 if (err)
126 return err;
128 a->ncolored= ncolored;
129 a->nfound = nfound;
130 a->ntrees = ntrees;
131 a->packfile_size = packfile_size;
132 a->ncommits = ncommits;
133 a->nobj_total = nobj_total;
134 a->nobj_deltify = nobj_deltify;
135 a->nobj_written = nobj_written;
136 return NULL;
139 static const struct got_error *
140 insert_ref(struct got_reflist_head *refs, const char *refname,
141 struct got_repository *repo)
143 const struct got_error *err;
144 struct got_reference *ref;
145 struct got_reflist_entry *new;
147 err = got_ref_open(&ref, repo, refname, 0);
148 if (err)
149 return err;
151 err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
152 if (err || new == NULL /* duplicate */)
153 got_ref_close(ref);
155 return err;
158 static const struct got_error *
159 check_linear_ancestry(const char *refname, struct got_object_id *my_id,
160 struct got_object_id *their_id, struct got_repository *repo,
161 got_cancel_cb cancel_cb, void *cancel_arg)
163 const struct got_error *err = NULL;
164 struct got_object_id *yca_id;
165 int obj_type;
167 err = got_object_get_type(&obj_type, repo, their_id);
168 if (err)
169 return err;
170 if (obj_type != GOT_OBJ_TYPE_COMMIT)
171 return got_error_fmt(GOT_ERR_OBJ_TYPE,
172 "bad object type on server for %s", refname);
174 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
175 my_id, their_id, 1, repo, cancel_cb, cancel_arg);
176 if (err)
177 return err;
178 if (yca_id == NULL)
179 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
181 /*
182 * Require a straight line of history between the two commits,
183 * with their commit being older than my commit.
185 * Non-linear situations such as this require a rebase:
187 * (theirs) D F (mine)
188 * \ /
189 * C E
190 * \ /
191 * B (yca)
192 * |
193 * A
194 */
195 if (got_object_id_cmp(their_id, yca_id) != 0)
196 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
198 free(yca_id);
199 return err;
202 static const struct got_error *
203 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
205 struct got_object_id **new;
206 const size_t alloc_chunksz = 256;
208 if (*nalloc >= n)
209 return NULL;
211 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
212 sizeof(struct got_object_id));
213 if (new == NULL)
214 return got_error_from_errno("recallocarray");
216 *ids = new;
217 *nalloc += alloc_chunksz;
218 return NULL;
221 static struct got_reference *
222 find_ref(struct got_reflist_head *refs, const char *refname)
224 struct got_reflist_entry *re;
226 TAILQ_FOREACH(re, refs, entry) {
227 if (got_path_cmp(got_ref_get_name(re->ref), refname,
228 strlen(got_ref_get_name(re->ref)),
229 strlen(refname)) == 0) {
230 return re->ref;
234 return NULL;
237 static struct got_pathlist_entry *
238 find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
240 struct got_pathlist_entry *pe;
242 TAILQ_FOREACH(pe, their_refs, entry) {
243 const char *their_refname = pe->path;
244 if (got_path_cmp(their_refname, refname,
245 strlen(their_refname), strlen(refname)) == 0) {
246 return pe;
250 return NULL;
253 static const struct got_error *
254 get_remote_refname(char **remote_refname, const char *remote_name,
255 const char *refname)
257 if (strncmp(refname, "refs/", 5) == 0)
258 refname += 5;
259 if (strncmp(refname, "heads/", 6) == 0)
260 refname += 6;
262 if (asprintf(remote_refname, "refs/remotes/%s/%s",
263 remote_name, refname) == -1)
264 return got_error_from_errno("asprintf");
266 return NULL;
269 static const struct got_error *
270 update_remote_ref(struct got_reference *my_ref, const char *remote_name,
271 struct got_repository *repo)
273 const struct got_error *err, *unlock_err;
274 struct got_object_id *my_id;
275 struct got_reference *ref = NULL;
276 char *remote_refname = NULL;
277 int ref_locked = 0;
279 err = got_ref_resolve(&my_id, repo, my_ref);
280 if (err)
281 return err;
283 err = get_remote_refname(&remote_refname, remote_name,
284 got_ref_get_name(my_ref));
285 if (err)
286 goto done;
288 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
289 if (err) {
290 if (err->code != GOT_ERR_NOT_REF)
291 goto done;
292 err = got_ref_alloc(&ref, remote_refname, my_id);
293 if (err)
294 goto done;
295 } else {
296 ref_locked = 1;
297 err = got_ref_change_ref(ref, my_id);
298 if (err)
299 goto done;
302 err = got_ref_write(ref, repo);
303 done:
304 if (ref) {
305 if (ref_locked) {
306 unlock_err = got_ref_unlock(ref);
307 if (unlock_err && err == NULL)
308 err = unlock_err;
310 got_ref_close(ref);
312 free(my_id);
313 free(remote_refname);
314 return err;
317 const struct got_error*
318 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
319 struct got_pathlist_head *tag_names,
320 struct got_pathlist_head *delete_branches,
321 int verbosity, int overwrite_refs, int sendfd,
322 struct got_repository *repo, got_send_progress_cb progress_cb,
323 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
325 int imsg_sendfds[2];
326 int npackfd = -1, nsendfd = -1;
327 int sendstatus, done = 0;
328 const struct got_error *err;
329 struct imsgbuf sendibuf;
330 pid_t sendpid = -1;
331 struct got_reflist_head refs;
332 struct got_pathlist_head have_refs;
333 struct got_pathlist_head their_refs;
334 struct got_pathlist_entry *pe;
335 struct got_reflist_entry *re;
336 struct got_object_id **our_ids = NULL;
337 struct got_object_id **their_ids = NULL;
338 int i, nours = 0, ntheirs = 0;
339 size_t nalloc_ours = 0, nalloc_theirs = 0;
340 int refs_to_send = 0, refs_to_delete = 0;
341 off_t bytes_sent = 0, bytes_sent_cur = 0;
342 struct pack_progress_arg ppa;
343 uint8_t packsha1[SHA1_DIGEST_LENGTH];
344 int packfd = -1;
345 FILE *delta_cache = NULL;
347 TAILQ_INIT(&refs);
348 TAILQ_INIT(&have_refs);
349 TAILQ_INIT(&their_refs);
351 TAILQ_FOREACH(pe, branch_names, entry) {
352 const char *branchname = pe->path;
353 if (strncmp(branchname, "refs/heads/", 11) != 0) {
354 char *s;
355 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
356 err = got_error_from_errno("asprintf");
357 goto done;
359 err = insert_ref(&refs, s, repo);
360 free(s);
361 } else {
362 err = insert_ref(&refs, branchname, repo);
364 if (err)
365 goto done;
368 TAILQ_FOREACH(pe, delete_branches, entry) {
369 const char *branchname = pe->path;
370 struct got_reference *ref;
371 if (strncmp(branchname, "refs/heads/", 11) != 0) {
372 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
373 branchname);
374 goto done;
376 ref = find_ref(&refs, branchname);
377 if (ref) {
378 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
379 "changes on %s will be sent to server",
380 branchname);
381 goto done;
385 TAILQ_FOREACH(pe, tag_names, entry) {
386 const char *tagname = pe->path;
387 if (strncmp(tagname, "refs/tags/", 10) != 0) {
388 char *s;
389 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
390 err = got_error_from_errno("asprintf");
391 goto done;
393 err = insert_ref(&refs, s, repo);
394 free(s);
395 } else {
396 err = insert_ref(&refs, tagname, repo);
398 if (err)
399 goto done;
402 if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
403 err = got_error(GOT_ERR_SEND_EMPTY);
404 goto done;
407 TAILQ_FOREACH(re, &refs, entry) {
408 struct got_object_id *id;
409 int obj_type;
411 if (got_ref_is_symbolic(re->ref)) {
412 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
413 "cannot send symbolic reference %s",
414 got_ref_get_name(re->ref));
415 goto done;
418 err = got_ref_resolve(&id, repo, re->ref);
419 if (err)
420 goto done;
421 err = got_object_get_type(&obj_type, repo, id);
422 free(id);
423 if (err)
424 goto done;
425 switch (obj_type) {
426 case GOT_OBJ_TYPE_COMMIT:
427 case GOT_OBJ_TYPE_TAG:
428 break;
429 default:
430 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
431 "cannot send %s", got_ref_get_name(re->ref));
432 goto done;
436 packfd = got_opentempfd();
437 if (packfd == -1) {
438 err = got_error_from_errno("got_opentempfd");
439 goto done;
442 delta_cache = got_opentemp();
443 if (delta_cache == NULL) {
444 err = got_error_from_errno("got_opentemp");
445 goto done;
448 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
449 err = got_error_from_errno("socketpair");
450 goto done;
453 sendpid = fork();
454 if (sendpid == -1) {
455 err = got_error_from_errno("fork");
456 goto done;
457 } else if (sendpid == 0){
458 got_privsep_exec_child(imsg_sendfds,
459 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
462 if (close(imsg_sendfds[1]) == -1) {
463 err = got_error_from_errno("close");
464 goto done;
466 imsg_init(&sendibuf, imsg_sendfds[0]);
467 nsendfd = dup(sendfd);
468 if (nsendfd == -1) {
469 err = got_error_from_errno("dup");
470 goto done;
473 /*
474 * Convert reflist to pathlist since the privsep layer
475 * is linked into helper programs which lack reference.c.
476 */
477 TAILQ_FOREACH(re, &refs, entry) {
478 struct got_object_id *id;
479 err = got_ref_resolve(&id, repo, re->ref);
480 if (err)
481 goto done;
482 err = got_pathlist_append(&have_refs,
483 got_ref_get_name(re->ref), id);
484 if (err)
485 goto done;
486 /*
487 * Also prepare the array of our object IDs which
488 * will be needed for generating a pack file.
489 */
490 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
491 if (err)
492 goto done;
493 our_ids[nours] = id;
494 nours++;
497 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
498 delete_branches, verbosity);
499 if (err)
500 goto done;
501 nsendfd = -1;
503 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
504 if (err)
505 goto done;
507 /*
508 * Process references reported by the server.
509 * Push appropriate object IDs onto the "their IDs" array.
510 * This array will be used to exclude objects which already
511 * exist on the server from our pack file.
512 */
513 TAILQ_FOREACH(pe, &their_refs, entry) {
514 const char *refname = pe->path;
515 struct got_object_id *their_id = pe->data;
516 int have_their_id;
517 struct got_object *obj;
518 struct got_reference *my_ref = NULL;
519 int is_tag = 0;
521 /* Don't blindly trust the server to send us valid names. */
522 if (!got_ref_name_is_valid(refname))
523 continue;
525 if (strncmp(refname, "refs/tags/", 10) == 0)
526 is_tag = 1;
527 /*
528 * Find out whether this is a reference we want to upload.
529 * Otherwise we can still use this reference as a hint to
530 * avoid uploading any objects the server already has.
531 */
532 my_ref = find_ref(&refs, refname);
533 if (my_ref) {
534 struct got_object_id *my_id;
535 err = got_ref_resolve(&my_id, repo, my_ref);
536 if (err)
537 goto done;
538 if (got_object_id_cmp(my_id, their_id) != 0) {
539 if (!overwrite_refs && is_tag) {
540 err = got_error_fmt(
541 GOT_ERR_SEND_TAG_EXISTS,
542 "%s", refname);
543 free(my_id);
544 goto done;
546 refs_to_send++;
548 free(my_id);
551 /* Check if their object exists locally. */
552 err = got_object_open(&obj, repo, their_id);
553 if (err) {
554 if (err->code != GOT_ERR_NO_OBJ)
555 goto done;
556 if (!overwrite_refs && my_ref != NULL) {
557 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
558 "%s", refname);
559 goto done;
561 have_their_id = 0;
562 } else {
563 got_object_close(obj);
564 have_their_id = 1;
567 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
568 if (err)
569 goto done;
571 if (have_their_id) {
572 /* Enforce linear ancestry if required. */
573 if (!overwrite_refs && my_ref && !is_tag) {
574 struct got_object_id *my_id;
575 err = got_ref_resolve(&my_id, repo, my_ref);
576 if (err)
577 goto done;
578 err = check_linear_ancestry(refname, my_id,
579 their_id, repo, cancel_cb, cancel_arg);
580 free(my_id);
581 my_id = NULL;
582 if (err)
583 goto done;
585 /* Exclude any objects reachable via their ID. */
586 their_ids[ntheirs] = got_object_id_dup(their_id);
587 if (their_ids[ntheirs] == NULL) {
588 err = got_error_from_errno("got_object_id_dup");
589 goto done;
591 ntheirs++;
592 } else if (!is_tag) {
593 char *remote_refname;
594 struct got_reference *ref;
595 /*
596 * Exclude any objects which exist on the server
597 * according to a locally cached remote reference.
598 */
599 err = get_remote_refname(&remote_refname,
600 remote_name, refname);
601 if (err)
602 goto done;
603 err = got_ref_open(&ref, repo, remote_refname, 0);
604 free(remote_refname);
605 if (err) {
606 if (err->code != GOT_ERR_NOT_REF)
607 goto done;
608 } else {
609 err = got_ref_resolve(&their_ids[ntheirs],
610 repo, ref);
611 got_ref_close(ref);
612 if (err)
613 goto done;
614 ntheirs++;
619 /* Account for any new references we are going to upload. */
620 TAILQ_FOREACH(re, &refs, entry) {
621 if (find_their_ref(&their_refs,
622 got_ref_get_name(re->ref)) == NULL)
623 refs_to_send++;
626 /* Account for any existing references we are going to delete. */
627 TAILQ_FOREACH(pe, delete_branches, entry) {
628 const char *branchname = pe->path;
629 if (find_their_ref(&their_refs, branchname))
630 refs_to_delete++;
633 if (refs_to_send == 0 && refs_to_delete == 0) {
634 got_privsep_send_stop(imsg_sendfds[0]);
635 goto done;
638 if (refs_to_send > 0) {
639 struct got_ratelimit rl;
640 got_ratelimit_init(&rl, 0, 500);
641 memset(&ppa, 0, sizeof(ppa));
642 ppa.progress_cb = progress_cb;
643 ppa.progress_arg = progress_arg;
644 err = got_pack_create(packsha1, packfd, delta_cache,
645 their_ids, ntheirs, our_ids, nours, repo, 0, 1, 0,
646 pack_progress, &ppa, &rl, cancel_cb, cancel_arg);
647 if (err)
648 goto done;
650 npackfd = dup(packfd);
651 if (npackfd == -1) {
652 err = got_error_from_errno("dup");
653 goto done;
655 err = got_privsep_send_packfd(&sendibuf, npackfd);
656 if (err != NULL)
657 goto done;
658 npackfd = -1;
659 } else {
660 err = got_privsep_send_packfd(&sendibuf, -1);
661 if (err != NULL)
662 goto done;
665 while (!done) {
666 int success = 0;
667 char *refname = NULL;
668 char *errmsg = 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, &errmsg, &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, errmsg, success);
701 if (err) {
702 free(refname);
703 free(errmsg);
704 goto done;
706 bytes_sent_cur = bytes_sent;
708 free(refname);
709 free(errmsg);
711 done:
712 if (sendpid != -1) {
713 if (err)
714 got_privsep_send_stop(imsg_sendfds[0]);
715 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
716 err = got_error_from_errno("waitpid");
718 if (packfd != -1 && close(packfd) == -1 && err == NULL)
719 err = got_error_from_errno("close");
720 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
721 err = got_error_from_errno("fclose");
722 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
723 err = got_error_from_errno("close");
724 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
725 err = got_error_from_errno("close");
727 got_ref_list_free(&refs);
728 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_NONE);
729 got_pathlist_free(&their_refs, GOT_PATHLIST_FREE_NONE);
730 for (i = 0; i < nours; i++)
731 free(our_ids[i]);
732 free(our_ids);
733 for (i = 0; i < ntheirs; i++)
734 free(their_ids[i]);
735 free(their_ids);
736 return err;