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/tree.h>
22 #include <sys/uio.h>
23 #include <sys/socket.h>
24 #include <sys/wait.h>
25 #include <sys/resource.h>
26 #include <sys/socket.h>
28 #include <endian.h>
29 #include <errno.h>
30 #include <err.h>
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdint.h>
36 #include <sha1.h>
37 #include <unistd.h>
38 #include <zlib.h>
39 #include <ctype.h>
40 #include <limits.h>
41 #include <imsg.h>
42 #include <time.h>
43 #include <uuid.h>
45 #include "got_error.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_path.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
51 #include "got_object.h"
52 #include "got_opentemp.h"
53 #include "got_send.h"
54 #include "got_repository_admin.h"
55 #include "got_commit_graph.h"
57 #include "got_lib_delta.h"
58 #include "got_lib_inflate.h"
59 #include "got_lib_object.h"
60 #include "got_lib_object_parse.h"
61 #include "got_lib_object_create.h"
62 #include "got_lib_pack.h"
63 #include "got_lib_sha1.h"
64 #include "got_lib_privsep.h"
65 #include "got_lib_object_cache.h"
66 #include "got_lib_repository.h"
67 #include "got_lib_pack_create.h"
68 #include "got_lib_dial.h"
70 #ifndef nitems
71 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
72 #endif
74 #ifndef ssizeof
75 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
76 #endif
78 #ifndef MIN
79 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
80 #endif
82 const struct got_error *
83 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
84 const char *host, const char *port, const char *server_path, int verbosity)
85 {
86 const struct got_error *err = NULL;
88 *sendpid = -1;
89 *sendfd = -1;
91 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
92 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
93 GOT_DIAL_DIRECTION_SEND, verbosity);
94 else if (strcmp(proto, "git") == 0)
95 err = got_dial_git(sendfd, host, port, server_path,
96 GOT_DIAL_DIRECTION_SEND);
97 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
98 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
99 else
100 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
101 return err;
104 struct pack_progress_arg {
105 got_send_progress_cb progress_cb;
106 void *progress_arg;
108 off_t packfile_size;
109 int ncommits;
110 int nobj_total;
111 int nobj_deltify;
112 int nobj_written;
113 };
115 static const struct got_error *
116 pack_progress(void *arg, off_t packfile_size, int ncommits,
117 int nobj_total, int nobj_deltify, int nobj_written)
119 const struct got_error *err;
120 struct pack_progress_arg *a = arg;
122 err = a->progress_cb(a->progress_arg, packfile_size, ncommits,
123 nobj_total, nobj_deltify, nobj_written, 0, NULL, 0);
124 if (err)
125 return err;
127 a->packfile_size = packfile_size;
128 a->ncommits = ncommits;
129 a->nobj_total = nobj_total;
130 a->nobj_deltify = nobj_deltify;
131 a->nobj_written = nobj_written;
132 return NULL;
135 static const struct got_error *
136 insert_ref(struct got_reflist_head *refs, const char *refname,
137 struct got_repository *repo)
139 const struct got_error *err;
140 struct got_reference *ref;
141 struct got_reflist_entry *new;
143 err = got_ref_open(&ref, repo, refname, 0);
144 if (err)
145 return err;
147 err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
148 if (err || new == NULL /* duplicate */)
149 got_ref_close(ref);
151 return err;
154 static const struct got_error *
155 check_linear_ancestry(const char *refname, struct got_object_id *my_id,
156 struct got_object_id *their_id, struct got_repository *repo,
157 got_cancel_cb cancel_cb, void *cancel_arg)
159 const struct got_error *err = NULL;
160 struct got_object_id *yca_id;
161 int obj_type;
163 err = got_object_get_type(&obj_type, repo, their_id);
164 if (err)
165 return err;
166 if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 return got_error_fmt(GOT_ERR_OBJ_TYPE,
168 "bad object type on server for %s", refname);
170 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
171 my_id, their_id, 1, repo, cancel_cb, cancel_arg);
172 if (err)
173 return err;
174 if (yca_id == NULL)
175 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
177 /*
178 * Require a straight line of history between the two commits,
179 * with their commit being older than my commit.
181 * Non-linear situations such as this require a rebase:
183 * (theirs) D F (mine)
184 * \ /
185 * C E
186 * \ /
187 * B (yca)
188 * |
189 * A
190 */
191 if (got_object_id_cmp(their_id, yca_id) != 0)
192 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
194 free(yca_id);
195 return err;
198 static const struct got_error *
199 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
201 struct got_object_id **new;
202 const size_t alloc_chunksz = 256;
204 if (*nalloc >= n)
205 return NULL;
207 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
208 sizeof(struct got_object_id));
209 if (new == NULL)
210 return got_error_from_errno("recallocarray");
212 *ids = new;
213 *nalloc += alloc_chunksz;
214 return NULL;
217 static struct got_reference *
218 find_ref(struct got_reflist_head *refs, const char *refname)
220 struct got_reflist_entry *re;
222 TAILQ_FOREACH(re, refs, entry) {
223 if (got_path_cmp(got_ref_get_name(re->ref), refname,
224 strlen(got_ref_get_name(re->ref)),
225 strlen(refname)) == 0) {
226 return re->ref;
230 return NULL;
233 static struct got_pathlist_entry *
234 find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
236 struct got_pathlist_entry *pe;
238 TAILQ_FOREACH(pe, their_refs, entry) {
239 const char *their_refname = pe->path;
240 if (got_path_cmp(their_refname, refname,
241 strlen(their_refname), strlen(refname)) == 0) {
242 return pe;
246 return NULL;
249 static const struct got_error *
250 get_remote_refname(char **remote_refname, const char *remote_name,
251 const char *refname)
253 if (strncmp(refname, "refs/", 5) == 0)
254 refname += 5;
255 if (strncmp(refname, "heads/", 6) == 0)
256 refname += 6;
258 if (asprintf(remote_refname, "refs/remotes/%s/%s",
259 remote_name, refname) == -1)
260 return got_error_from_errno("asprintf");
262 return NULL;
265 static const struct got_error *
266 update_remote_ref(struct got_reference *my_ref, const char *remote_name,
267 struct got_repository *repo)
269 const struct got_error *err, *unlock_err;
270 struct got_object_id *my_id;
271 struct got_reference *ref = NULL;
272 char *remote_refname = NULL;
273 int ref_locked = 0;
275 err = got_ref_resolve(&my_id, repo, my_ref);
276 if (err)
277 return err;
279 err = get_remote_refname(&remote_refname, remote_name,
280 got_ref_get_name(my_ref));
281 if (err)
282 goto done;
284 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
285 if (err) {
286 if (err->code != GOT_ERR_NOT_REF)
287 goto done;
288 err = got_ref_alloc(&ref, remote_refname, my_id);
289 if (err)
290 goto done;
291 } else {
292 ref_locked = 1;
293 err = got_ref_change_ref(ref, my_id);
294 if (err)
295 goto done;
298 err = got_ref_write(ref, repo);
299 done:
300 if (ref) {
301 if (ref_locked) {
302 unlock_err = got_ref_unlock(ref);
303 if (unlock_err && err == NULL)
304 err = unlock_err;
306 got_ref_close(ref);
308 free(my_id);
309 free(remote_refname);
310 return err;
313 const struct got_error*
314 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
315 struct got_pathlist_head *tag_names,
316 struct got_pathlist_head *delete_branches,
317 int verbosity, int overwrite_refs, int sendfd,
318 struct got_repository *repo, got_send_progress_cb progress_cb,
319 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
321 int imsg_sendfds[2];
322 int npackfd = -1, nsendfd = -1;
323 int sendstatus, done = 0;
324 const struct got_error *err;
325 struct imsgbuf sendibuf;
326 pid_t sendpid = -1;
327 struct got_reflist_head refs;
328 struct got_pathlist_head have_refs;
329 struct got_pathlist_head their_refs;
330 struct got_pathlist_entry *pe;
331 struct got_reflist_entry *re;
332 struct got_object_id **our_ids = NULL;
333 struct got_object_id **their_ids = NULL;
334 int i, nours = 0, ntheirs = 0;
335 size_t nalloc_ours = 0, nalloc_theirs = 0;
336 int refs_to_send = 0, refs_to_delete = 0;
337 off_t bytes_sent = 0;
338 struct pack_progress_arg ppa;
339 uint8_t packsha1[SHA1_DIGEST_LENGTH];
340 FILE *packfile = NULL;
342 TAILQ_INIT(&refs);
343 TAILQ_INIT(&have_refs);
344 TAILQ_INIT(&their_refs);
346 TAILQ_FOREACH(pe, branch_names, entry) {
347 const char *branchname = pe->path;
348 if (strncmp(branchname, "refs/heads/", 11) != 0) {
349 char *s;
350 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
351 err = got_error_from_errno("asprintf");
352 goto done;
354 err = insert_ref(&refs, s, repo);
355 free(s);
356 } else {
357 err = insert_ref(&refs, branchname, repo);
359 if (err)
360 goto done;
363 TAILQ_FOREACH(pe, delete_branches, entry) {
364 const char *branchname = pe->path;
365 struct got_reference *ref;
366 if (strncmp(branchname, "refs/heads/", 11) != 0) {
367 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
368 branchname);
369 goto done;
371 ref = find_ref(&refs, branchname);
372 if (ref) {
373 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
374 "changes on %s will be sent to server",
375 branchname);
376 goto done;
380 TAILQ_FOREACH(pe, tag_names, entry) {
381 const char *tagname = pe->path;
382 if (strncmp(tagname, "refs/tags/", 10) != 0) {
383 char *s;
384 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
385 err = got_error_from_errno("asprintf");
386 goto done;
388 err = insert_ref(&refs, s, repo);
389 free(s);
390 } else {
391 err = insert_ref(&refs, tagname, repo);
393 if (err)
394 goto done;
397 if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
398 err = got_error(GOT_ERR_SEND_EMPTY);
399 goto done;
402 TAILQ_FOREACH(re, &refs, entry) {
403 struct got_object_id *id;
404 int obj_type;
406 if (got_ref_is_symbolic(re->ref)) {
407 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
408 "cannot send symbolic reference %s",
409 got_ref_get_name(re->ref));
410 goto done;
413 err = got_ref_resolve(&id, repo, re->ref);
414 if (err)
415 goto done;
416 err = got_object_get_type(&obj_type, repo, id);
417 free(id);
418 if (err)
419 goto done;
420 switch (obj_type) {
421 case GOT_OBJ_TYPE_COMMIT:
422 case GOT_OBJ_TYPE_TAG:
423 break;
424 default:
425 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
426 "cannot send %s", got_ref_get_name(re->ref));
427 goto done;
431 packfile = got_opentemp();
432 if (packfile == NULL) {
433 err = got_error_from_errno("got_opentemp");
434 goto done;
437 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
438 err = got_error_from_errno("socketpair");
439 goto done;
442 sendpid = fork();
443 if (sendpid == -1) {
444 err = got_error_from_errno("fork");
445 goto done;
446 } else if (sendpid == 0){
447 got_privsep_exec_child(imsg_sendfds,
448 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
451 if (close(imsg_sendfds[1]) == -1) {
452 err = got_error_from_errno("close");
453 goto done;
455 imsg_init(&sendibuf, imsg_sendfds[0]);
456 nsendfd = dup(sendfd);
457 if (nsendfd == -1) {
458 err = got_error_from_errno("dup");
459 goto done;
462 /*
463 * Convert reflist to pathlist since the privsep layer
464 * is linked into helper programs which lack reference.c.
465 */
466 TAILQ_FOREACH(re, &refs, entry) {
467 struct got_object_id *id;
468 err = got_ref_resolve(&id, repo, re->ref);
469 if (err)
470 goto done;
471 err = got_pathlist_append(&have_refs,
472 got_ref_get_name(re->ref), id);
473 if (err)
474 goto done;
475 /*
476 * Also prepare the array of our object IDs which
477 * will be needed for generating a pack file.
478 */
479 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
480 if (err)
481 goto done;
482 our_ids[nours] = id;
483 nours++;
486 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
487 delete_branches, verbosity);
488 if (err)
489 goto done;
490 nsendfd = -1;
492 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
493 if (err)
494 goto done;
496 /*
497 * Process references reported by the server.
498 * Push appropriate object IDs onto the "their IDs" array.
499 * This array will be used to exclude objects which already
500 * exist on the server from our pack file.
501 */
502 TAILQ_FOREACH(pe, &their_refs, entry) {
503 const char *refname = pe->path;
504 struct got_object_id *their_id = pe->data;
505 int have_their_id;
506 struct got_object *obj;
507 struct got_reference *my_ref = NULL;
508 int is_tag = 0;
510 /* Don't blindly trust the server to send us valid names. */
511 if (!got_ref_name_is_valid(refname))
512 continue;
514 /*
515 * Find out whether this is a reference we want to upload.
516 * Otherwise we can still use this reference as a hint to
517 * avoid uploading any objects the server already has.
518 */
519 my_ref = find_ref(&refs, refname);
520 if (my_ref) {
521 struct got_object_id *my_id;
522 err = got_ref_resolve(&my_id, repo, my_ref);
523 if (err)
524 goto done;
525 if (got_object_id_cmp(my_id, their_id) != 0)
526 refs_to_send++;
527 free(my_id);
531 if (strncmp(refname, "refs/tags/", 10) == 0)
532 is_tag = 1;
534 /* Prevent tags from being overwritten by default. */
535 if (!overwrite_refs && my_ref && is_tag) {
536 err = got_error_fmt(GOT_ERR_SEND_TAG_EXISTS,
537 "%s", refname);
538 goto done;
541 /* Check if their object exists locally. */
542 err = got_object_open(&obj, repo, their_id);
543 if (err) {
544 if (err->code != GOT_ERR_NO_OBJ)
545 goto done;
546 if (!overwrite_refs && my_ref != NULL) {
547 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
548 "%s", refname);
549 goto done;
551 have_their_id = 0;
552 } else {
553 got_object_close(obj);
554 have_their_id = 1;
557 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
558 if (err)
559 goto done;
561 if (have_their_id) {
562 /* Enforce linear ancestry if required. */
563 if (!overwrite_refs && my_ref && !is_tag) {
564 struct got_object_id *my_id;
565 err = got_ref_resolve(&my_id, repo, my_ref);
566 if (err)
567 goto done;
568 err = check_linear_ancestry(refname, my_id,
569 their_id, repo, cancel_cb, cancel_arg);
570 free(my_id);
571 my_id = NULL;
572 if (err)
573 goto done;
575 /* Exclude any objects reachable via their ID. */
576 their_ids[ntheirs] = got_object_id_dup(their_id);
577 if (their_ids[ntheirs] == NULL) {
578 err = got_error_from_errno("got_object_id_dup");
579 goto done;
581 ntheirs++;
582 } else if (!is_tag) {
583 char *remote_refname;
584 struct got_reference *ref;
585 /*
586 * Exclude any objects which exist on the server
587 * according to a locally cached remote reference.
588 */
589 err = get_remote_refname(&remote_refname,
590 remote_name, refname);
591 if (err)
592 goto done;
593 err = got_ref_open(&ref, repo, remote_refname, 0);
594 free(remote_refname);
595 if (err) {
596 if (err->code != GOT_ERR_NOT_REF)
597 goto done;
598 } else {
599 err = got_ref_resolve(&their_ids[ntheirs],
600 repo, ref);
601 got_ref_close(ref);
602 if (err)
603 goto done;
604 ntheirs++;
609 /* Account for any new references we are going to upload. */
610 TAILQ_FOREACH(re, &refs, entry) {
611 if (find_their_ref(&their_refs,
612 got_ref_get_name(re->ref)) == NULL)
613 refs_to_send++;
616 /* Account for any existing references we are going to delete. */
617 TAILQ_FOREACH(pe, delete_branches, entry) {
618 const char *branchname = pe->path;
619 if (find_their_ref(&their_refs, branchname))
620 refs_to_delete++;
623 if (refs_to_send == 0 && refs_to_delete == 0) {
624 got_privsep_send_stop(imsg_sendfds[0]);
625 goto done;
628 if (refs_to_send > 0) {
629 memset(&ppa, 0, sizeof(ppa));
630 ppa.progress_cb = progress_cb;
631 ppa.progress_arg = progress_arg;
632 err = got_pack_create(packsha1, packfile, their_ids, ntheirs,
633 our_ids, nours, repo, 0, 1, pack_progress, &ppa,
634 cancel_cb, cancel_arg);
635 if (err)
636 goto done;
638 if (fflush(packfile) == -1) {
639 err = got_error_from_errno("fflush");
640 goto done;
643 npackfd = dup(fileno(packfile));
644 if (npackfd == -1) {
645 err = got_error_from_errno("dup");
646 goto done;
648 err = got_privsep_send_packfd(&sendibuf, npackfd);
649 if (err != NULL)
650 goto done;
651 npackfd = -1;
652 } else {
653 err = got_privsep_send_packfd(&sendibuf, -1);
654 if (err != NULL)
655 goto done;
658 while (!done) {
659 int success = 0;
660 char *refname = NULL;
661 off_t bytes_sent_cur = 0;
662 if (cancel_cb) {
663 err = (*cancel_cb)(cancel_arg);
664 if (err)
665 goto done;
667 err = got_privsep_recv_send_progress(&done, &bytes_sent,
668 &success, &refname, &sendibuf);
669 if (err)
670 goto done;
671 if (refname && got_ref_name_is_valid(refname) && success &&
672 strncmp(refname, "refs/tags/", 10) != 0) {
673 struct got_reference *my_ref;
674 /*
675 * The server has accepted our changes.
676 * Update our reference in refs/remotes/ accordingly.
677 */
678 my_ref = find_ref(&refs, refname);
679 if (my_ref) {
680 err = update_remote_ref(my_ref, remote_name,
681 repo);
682 if (err)
683 goto done;
686 if (refname != NULL ||
687 bytes_sent_cur != bytes_sent) {
688 err = progress_cb(progress_arg, ppa.packfile_size,
689 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
690 ppa.nobj_written, bytes_sent,
691 refname, success);
692 if (err) {
693 free(refname);
694 goto done;
696 bytes_sent_cur = bytes_sent;
698 free(refname);
700 done:
701 if (sendpid != -1) {
702 if (err)
703 got_privsep_send_stop(imsg_sendfds[0]);
704 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
705 err = got_error_from_errno("waitpid");
707 if (packfile && fclose(packfile) == EOF && err == NULL)
708 err = got_error_from_errno("fclose");
709 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
710 err = got_error_from_errno("close");
711 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
712 err = got_error_from_errno("close");
714 got_ref_list_free(&refs);
715 got_pathlist_free(&have_refs);
716 got_pathlist_free(&their_refs);
717 for (i = 0; i < nours; i++)
718 free(our_ids[i]);
719 free(our_ids);
720 for (i = 0; i < ntheirs; i++)
721 free(their_ids[i]);
722 free(their_ids);
723 return err;