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_pack_create.h"
63 #include "got_lib_dial.h"
65 #ifndef nitems
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 #endif
69 #ifndef ssizeof
70 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
71 #endif
73 #ifndef MIN
74 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
75 #endif
77 const struct got_error *
78 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
79 const char *host, const char *port, const char *server_path, int verbosity)
80 {
81 const struct got_error *err = NULL;
83 *sendpid = -1;
84 *sendfd = -1;
86 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
87 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
88 GOT_DIAL_DIRECTION_SEND, verbosity);
89 else if (strcmp(proto, "git") == 0)
90 err = got_dial_git(sendfd, host, port, server_path,
91 GOT_DIAL_DIRECTION_SEND);
92 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
93 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
94 else
95 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
96 return err;
97 }
99 struct pack_progress_arg {
100 got_send_progress_cb progress_cb;
101 void *progress_arg;
103 int ncolored;
104 int nfound;
105 int ntrees;
106 off_t packfile_size;
107 int ncommits;
108 int nobj_total;
109 int nobj_deltify;
110 int nobj_written;
111 };
113 static const struct got_error *
114 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
115 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
116 int nobj_written)
118 const struct got_error *err;
119 struct pack_progress_arg *a = arg;
121 err = a->progress_cb(a->progress_arg, ncolored, nfound, ntrees,
122 packfile_size, ncommits, nobj_total, nobj_deltify,
123 nobj_written, 0, NULL, 0);
124 if (err)
125 return err;
127 a->ncolored= ncolored;
128 a->nfound = nfound;
129 a->ntrees = ntrees;
130 a->packfile_size = packfile_size;
131 a->ncommits = ncommits;
132 a->nobj_total = nobj_total;
133 a->nobj_deltify = nobj_deltify;
134 a->nobj_written = nobj_written;
135 return NULL;
138 static const struct got_error *
139 insert_ref(struct got_reflist_head *refs, const char *refname,
140 struct got_repository *repo)
142 const struct got_error *err;
143 struct got_reference *ref;
144 struct got_reflist_entry *new;
146 err = got_ref_open(&ref, repo, refname, 0);
147 if (err)
148 return err;
150 err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
151 if (err || new == NULL /* duplicate */)
152 got_ref_close(ref);
154 return err;
157 static const struct got_error *
158 check_linear_ancestry(const char *refname, struct got_object_id *my_id,
159 struct got_object_id *their_id, struct got_repository *repo,
160 got_cancel_cb cancel_cb, void *cancel_arg)
162 const struct got_error *err = NULL;
163 struct got_object_id *yca_id;
164 int obj_type;
166 err = got_object_get_type(&obj_type, repo, their_id);
167 if (err)
168 return err;
169 if (obj_type != GOT_OBJ_TYPE_COMMIT)
170 return got_error_fmt(GOT_ERR_OBJ_TYPE,
171 "bad object type on server for %s", refname);
173 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
174 my_id, their_id, 1, repo, cancel_cb, cancel_arg);
175 if (err)
176 return err;
177 if (yca_id == NULL)
178 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
180 /*
181 * Require a straight line of history between the two commits,
182 * with their commit being older than my commit.
184 * Non-linear situations such as this require a rebase:
186 * (theirs) D F (mine)
187 * \ /
188 * C E
189 * \ /
190 * B (yca)
191 * |
192 * A
193 */
194 if (got_object_id_cmp(their_id, yca_id) != 0)
195 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
197 free(yca_id);
198 return err;
201 static const struct got_error *
202 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
204 struct got_object_id **new;
205 const size_t alloc_chunksz = 256;
207 if (*nalloc >= n)
208 return NULL;
210 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
211 sizeof(struct got_object_id));
212 if (new == NULL)
213 return got_error_from_errno("recallocarray");
215 *ids = new;
216 *nalloc += alloc_chunksz;
217 return NULL;
220 static struct got_reference *
221 find_ref(struct got_reflist_head *refs, const char *refname)
223 struct got_reflist_entry *re;
225 TAILQ_FOREACH(re, refs, entry) {
226 if (got_path_cmp(got_ref_get_name(re->ref), refname,
227 strlen(got_ref_get_name(re->ref)),
228 strlen(refname)) == 0) {
229 return re->ref;
233 return NULL;
236 static struct got_pathlist_entry *
237 find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
239 struct got_pathlist_entry *pe;
241 TAILQ_FOREACH(pe, their_refs, entry) {
242 const char *their_refname = pe->path;
243 if (got_path_cmp(their_refname, refname,
244 strlen(their_refname), strlen(refname)) == 0) {
245 return pe;
249 return NULL;
252 static const struct got_error *
253 get_remote_refname(char **remote_refname, const char *remote_name,
254 const char *refname)
256 if (strncmp(refname, "refs/", 5) == 0)
257 refname += 5;
258 if (strncmp(refname, "heads/", 6) == 0)
259 refname += 6;
261 if (asprintf(remote_refname, "refs/remotes/%s/%s",
262 remote_name, refname) == -1)
263 return got_error_from_errno("asprintf");
265 return NULL;
268 static const struct got_error *
269 update_remote_ref(struct got_reference *my_ref, const char *remote_name,
270 struct got_repository *repo)
272 const struct got_error *err, *unlock_err;
273 struct got_object_id *my_id;
274 struct got_reference *ref = NULL;
275 char *remote_refname = NULL;
276 int ref_locked = 0;
278 err = got_ref_resolve(&my_id, repo, my_ref);
279 if (err)
280 return err;
282 err = get_remote_refname(&remote_refname, remote_name,
283 got_ref_get_name(my_ref));
284 if (err)
285 goto done;
287 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
288 if (err) {
289 if (err->code != GOT_ERR_NOT_REF)
290 goto done;
291 err = got_ref_alloc(&ref, remote_refname, my_id);
292 if (err)
293 goto done;
294 } else {
295 ref_locked = 1;
296 err = got_ref_change_ref(ref, my_id);
297 if (err)
298 goto done;
301 err = got_ref_write(ref, repo);
302 done:
303 if (ref) {
304 if (ref_locked) {
305 unlock_err = got_ref_unlock(ref);
306 if (unlock_err && err == NULL)
307 err = unlock_err;
309 got_ref_close(ref);
311 free(my_id);
312 free(remote_refname);
313 return err;
316 const struct got_error*
317 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
318 struct got_pathlist_head *tag_names,
319 struct got_pathlist_head *delete_branches,
320 int verbosity, int overwrite_refs, int sendfd,
321 struct got_repository *repo, got_send_progress_cb progress_cb,
322 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
324 int imsg_sendfds[2];
325 int npackfd = -1, nsendfd = -1;
326 int sendstatus, done = 0;
327 const struct got_error *err;
328 struct imsgbuf sendibuf;
329 pid_t sendpid = -1;
330 struct got_reflist_head refs;
331 struct got_pathlist_head have_refs;
332 struct got_pathlist_head their_refs;
333 struct got_pathlist_entry *pe;
334 struct got_reflist_entry *re;
335 struct got_object_id **our_ids = NULL;
336 struct got_object_id **their_ids = NULL;
337 int i, nours = 0, ntheirs = 0;
338 size_t nalloc_ours = 0, nalloc_theirs = 0;
339 int refs_to_send = 0, refs_to_delete = 0;
340 off_t bytes_sent = 0, bytes_sent_cur = 0;
341 struct pack_progress_arg ppa;
342 uint8_t packsha1[SHA1_DIGEST_LENGTH];
343 FILE *packfile = NULL;
345 TAILQ_INIT(&refs);
346 TAILQ_INIT(&have_refs);
347 TAILQ_INIT(&their_refs);
349 TAILQ_FOREACH(pe, branch_names, entry) {
350 const char *branchname = pe->path;
351 if (strncmp(branchname, "refs/heads/", 11) != 0) {
352 char *s;
353 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
354 err = got_error_from_errno("asprintf");
355 goto done;
357 err = insert_ref(&refs, s, repo);
358 free(s);
359 } else {
360 err = insert_ref(&refs, branchname, repo);
362 if (err)
363 goto done;
366 TAILQ_FOREACH(pe, delete_branches, entry) {
367 const char *branchname = pe->path;
368 struct got_reference *ref;
369 if (strncmp(branchname, "refs/heads/", 11) != 0) {
370 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
371 branchname);
372 goto done;
374 ref = find_ref(&refs, branchname);
375 if (ref) {
376 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
377 "changes on %s will be sent to server",
378 branchname);
379 goto done;
383 TAILQ_FOREACH(pe, tag_names, entry) {
384 const char *tagname = pe->path;
385 if (strncmp(tagname, "refs/tags/", 10) != 0) {
386 char *s;
387 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
388 err = got_error_from_errno("asprintf");
389 goto done;
391 err = insert_ref(&refs, s, repo);
392 free(s);
393 } else {
394 err = insert_ref(&refs, tagname, repo);
396 if (err)
397 goto done;
400 if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
401 err = got_error(GOT_ERR_SEND_EMPTY);
402 goto done;
405 TAILQ_FOREACH(re, &refs, entry) {
406 struct got_object_id *id;
407 int obj_type;
409 if (got_ref_is_symbolic(re->ref)) {
410 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
411 "cannot send symbolic reference %s",
412 got_ref_get_name(re->ref));
413 goto done;
416 err = got_ref_resolve(&id, repo, re->ref);
417 if (err)
418 goto done;
419 err = got_object_get_type(&obj_type, repo, id);
420 free(id);
421 if (err)
422 goto done;
423 switch (obj_type) {
424 case GOT_OBJ_TYPE_COMMIT:
425 case GOT_OBJ_TYPE_TAG:
426 break;
427 default:
428 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
429 "cannot send %s", got_ref_get_name(re->ref));
430 goto done;
434 packfile = got_opentemp();
435 if (packfile == NULL) {
436 err = got_error_from_errno("got_opentemp");
437 goto done;
440 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
441 err = got_error_from_errno("socketpair");
442 goto done;
445 sendpid = fork();
446 if (sendpid == -1) {
447 err = got_error_from_errno("fork");
448 goto done;
449 } else if (sendpid == 0){
450 got_privsep_exec_child(imsg_sendfds,
451 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
454 if (close(imsg_sendfds[1]) == -1) {
455 err = got_error_from_errno("close");
456 goto done;
458 imsg_init(&sendibuf, imsg_sendfds[0]);
459 nsendfd = dup(sendfd);
460 if (nsendfd == -1) {
461 err = got_error_from_errno("dup");
462 goto done;
465 /*
466 * Convert reflist to pathlist since the privsep layer
467 * is linked into helper programs which lack reference.c.
468 */
469 TAILQ_FOREACH(re, &refs, entry) {
470 struct got_object_id *id;
471 err = got_ref_resolve(&id, repo, re->ref);
472 if (err)
473 goto done;
474 err = got_pathlist_append(&have_refs,
475 got_ref_get_name(re->ref), id);
476 if (err)
477 goto done;
478 /*
479 * Also prepare the array of our object IDs which
480 * will be needed for generating a pack file.
481 */
482 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
483 if (err)
484 goto done;
485 our_ids[nours] = id;
486 nours++;
489 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
490 delete_branches, verbosity);
491 if (err)
492 goto done;
493 nsendfd = -1;
495 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
496 if (err)
497 goto done;
499 /*
500 * Process references reported by the server.
501 * Push appropriate object IDs onto the "their IDs" array.
502 * This array will be used to exclude objects which already
503 * exist on the server from our pack file.
504 */
505 TAILQ_FOREACH(pe, &their_refs, entry) {
506 const char *refname = pe->path;
507 struct got_object_id *their_id = pe->data;
508 int have_their_id;
509 struct got_object *obj;
510 struct got_reference *my_ref = NULL;
511 int is_tag = 0;
513 /* Don't blindly trust the server to send us valid names. */
514 if (!got_ref_name_is_valid(refname))
515 continue;
517 if (strncmp(refname, "refs/tags/", 10) == 0)
518 is_tag = 1;
519 /*
520 * Find out whether this is a reference we want to upload.
521 * Otherwise we can still use this reference as a hint to
522 * avoid uploading any objects the server already has.
523 */
524 my_ref = find_ref(&refs, refname);
525 if (my_ref) {
526 struct got_object_id *my_id;
527 err = got_ref_resolve(&my_id, repo, my_ref);
528 if (err)
529 goto done;
530 if (got_object_id_cmp(my_id, their_id) != 0) {
531 if (!overwrite_refs && is_tag) {
532 err = got_error_fmt(
533 GOT_ERR_SEND_TAG_EXISTS,
534 "%s", refname);
535 free(my_id);
536 goto done;
538 refs_to_send++;
540 free(my_id);
543 /* Check if their object exists locally. */
544 err = got_object_open(&obj, repo, their_id);
545 if (err) {
546 if (err->code != GOT_ERR_NO_OBJ)
547 goto done;
548 if (!overwrite_refs && my_ref != NULL) {
549 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
550 "%s", refname);
551 goto done;
553 have_their_id = 0;
554 } else {
555 got_object_close(obj);
556 have_their_id = 1;
559 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
560 if (err)
561 goto done;
563 if (have_their_id) {
564 /* Enforce linear ancestry if required. */
565 if (!overwrite_refs && my_ref && !is_tag) {
566 struct got_object_id *my_id;
567 err = got_ref_resolve(&my_id, repo, my_ref);
568 if (err)
569 goto done;
570 err = check_linear_ancestry(refname, my_id,
571 their_id, repo, cancel_cb, cancel_arg);
572 free(my_id);
573 my_id = NULL;
574 if (err)
575 goto done;
577 /* Exclude any objects reachable via their ID. */
578 their_ids[ntheirs] = got_object_id_dup(their_id);
579 if (their_ids[ntheirs] == NULL) {
580 err = got_error_from_errno("got_object_id_dup");
581 goto done;
583 ntheirs++;
584 } else if (!is_tag) {
585 char *remote_refname;
586 struct got_reference *ref;
587 /*
588 * Exclude any objects which exist on the server
589 * according to a locally cached remote reference.
590 */
591 err = get_remote_refname(&remote_refname,
592 remote_name, refname);
593 if (err)
594 goto done;
595 err = got_ref_open(&ref, repo, remote_refname, 0);
596 free(remote_refname);
597 if (err) {
598 if (err->code != GOT_ERR_NOT_REF)
599 goto done;
600 } else {
601 err = got_ref_resolve(&their_ids[ntheirs],
602 repo, ref);
603 got_ref_close(ref);
604 if (err)
605 goto done;
606 ntheirs++;
611 /* Account for any new references we are going to upload. */
612 TAILQ_FOREACH(re, &refs, entry) {
613 if (find_their_ref(&their_refs,
614 got_ref_get_name(re->ref)) == NULL)
615 refs_to_send++;
618 /* Account for any existing references we are going to delete. */
619 TAILQ_FOREACH(pe, delete_branches, entry) {
620 const char *branchname = pe->path;
621 if (find_their_ref(&their_refs, branchname))
622 refs_to_delete++;
625 if (refs_to_send == 0 && refs_to_delete == 0) {
626 got_privsep_send_stop(imsg_sendfds[0]);
627 goto done;
630 if (refs_to_send > 0) {
631 memset(&ppa, 0, sizeof(ppa));
632 ppa.progress_cb = progress_cb;
633 ppa.progress_arg = progress_arg;
634 err = got_pack_create(packsha1, packfile, their_ids, ntheirs,
635 our_ids, nours, repo, 0, 1, pack_progress, &ppa,
636 cancel_cb, cancel_arg);
637 if (err)
638 goto done;
640 if (fflush(packfile) == -1) {
641 err = got_error_from_errno("fflush");
642 goto done;
645 npackfd = dup(fileno(packfile));
646 if (npackfd == -1) {
647 err = got_error_from_errno("dup");
648 goto done;
650 err = got_privsep_send_packfd(&sendibuf, npackfd);
651 if (err != NULL)
652 goto done;
653 npackfd = -1;
654 } else {
655 err = got_privsep_send_packfd(&sendibuf, -1);
656 if (err != NULL)
657 goto done;
660 while (!done) {
661 int success = 0;
662 char *refname = NULL;
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;