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 <sha2.h>
38 #include <unistd.h>
39 #include <zlib.h>
40 #include <ctype.h>
41 #include <limits.h>
42 #include <imsg.h>
43 #include <time.h>
44 #include <uuid.h>
46 #include "got_error.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
49 #include "got_path.h"
50 #include "got_cancel.h"
51 #include "got_worktree.h"
52 #include "got_object.h"
53 #include "got_opentemp.h"
54 #include "got_send.h"
55 #include "got_repository_admin.h"
56 #include "got_commit_graph.h"
58 #include "got_lib_delta.h"
59 #include "got_lib_inflate.h"
60 #include "got_lib_object.h"
61 #include "got_lib_object_parse.h"
62 #include "got_lib_object_create.h"
63 #include "got_lib_pack.h"
64 #include "got_lib_hash.h"
65 #include "got_lib_privsep.h"
66 #include "got_lib_object_cache.h"
67 #include "got_lib_repository.h"
68 #include "got_lib_ratelimit.h"
69 #include "got_lib_pack_create.h"
70 #include "got_lib_dial.h"
72 #ifndef nitems
73 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
74 #endif
76 #ifndef ssizeof
77 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
78 #endif
80 #ifndef MIN
81 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
82 #endif
84 const struct got_error *
85 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
86 const char *host, const char *port, const char *server_path, int verbosity)
87 {
88 const struct got_error *err = NULL;
90 *sendpid = -1;
91 *sendfd = -1;
93 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
94 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
95 GOT_DIAL_CMD_SEND, verbosity);
96 else if (strcmp(proto, "git") == 0)
97 err = got_dial_git(sendfd, host, port, server_path,
98 GOT_DIAL_CMD_SEND);
99 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
100 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
101 else
102 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
103 return err;
106 struct pack_progress_arg {
107 got_send_progress_cb progress_cb;
108 void *progress_arg;
110 int ncolored;
111 int nfound;
112 int ntrees;
113 off_t packfile_size;
114 int ncommits;
115 int nobj_total;
116 int nobj_deltify;
117 int nobj_written;
118 };
120 static const struct got_error *
121 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
122 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
123 int nobj_written)
125 const struct got_error *err;
126 struct pack_progress_arg *a = arg;
128 err = a->progress_cb(a->progress_arg, ncolored, nfound, ntrees,
129 packfile_size, ncommits, nobj_total, nobj_deltify,
130 nobj_written, 0, NULL, NULL, 0);
131 if (err)
132 return err;
134 a->ncolored= ncolored;
135 a->nfound = nfound;
136 a->ntrees = ntrees;
137 a->packfile_size = packfile_size;
138 a->ncommits = ncommits;
139 a->nobj_total = nobj_total;
140 a->nobj_deltify = nobj_deltify;
141 a->nobj_written = nobj_written;
142 return NULL;
145 static const struct got_error *
146 insert_ref(struct got_reflist_head *refs, const char *refname,
147 struct got_repository *repo)
149 const struct got_error *err;
150 struct got_reference *ref;
151 struct got_reflist_entry *new;
153 err = got_ref_open(&ref, repo, refname, 0);
154 if (err)
155 return err;
157 err = got_reflist_insert(&new, refs, ref, got_ref_cmp_by_name, NULL);
158 if (err || new == NULL /* duplicate */)
159 got_ref_close(ref);
161 return err;
164 static const struct got_error *
165 check_common_ancestry(const char *refname, struct got_object_id *my_id,
166 struct got_object_id *their_id, struct got_repository *repo,
167 got_cancel_cb cancel_cb, void *cancel_arg)
169 const struct got_error *err = NULL;
170 struct got_object_id *yca_id;
171 int obj_type;
173 err = got_object_get_type(&obj_type, repo, their_id);
174 if (err)
175 return err;
176 if (obj_type != GOT_OBJ_TYPE_COMMIT)
177 return got_error_fmt(GOT_ERR_OBJ_TYPE,
178 "bad object type on server for %s", refname);
180 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
181 my_id, their_id, 0, repo, cancel_cb, cancel_arg);
182 if (err)
183 return err;
184 if (yca_id == NULL)
185 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
187 if (got_object_id_cmp(their_id, yca_id) != 0)
188 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
190 free(yca_id);
191 return err;
194 static const struct got_error *
195 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
197 struct got_object_id **new;
198 const size_t alloc_chunksz = 256;
200 if (*nalloc >= n)
201 return NULL;
203 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
204 sizeof(struct got_object_id));
205 if (new == NULL)
206 return got_error_from_errno("recallocarray");
208 *ids = new;
209 *nalloc += alloc_chunksz;
210 return NULL;
213 static struct got_reference *
214 find_ref(struct got_reflist_head *refs, const char *refname)
216 struct got_reflist_entry *re;
218 TAILQ_FOREACH(re, refs, entry) {
219 if (got_path_cmp(got_ref_get_name(re->ref), refname,
220 strlen(got_ref_get_name(re->ref)),
221 strlen(refname)) == 0) {
222 return re->ref;
226 return NULL;
229 static struct got_pathlist_entry *
230 find_their_ref(struct got_pathlist_head *their_refs, const char *refname)
232 struct got_pathlist_entry *pe;
234 TAILQ_FOREACH(pe, their_refs, entry) {
235 const char *their_refname = pe->path;
236 if (got_path_cmp(their_refname, refname,
237 strlen(their_refname), strlen(refname)) == 0) {
238 return pe;
242 return NULL;
245 static const struct got_error *
246 get_remote_refname(char **remote_refname, const char *remote_name,
247 const char *refname)
249 if (strncmp(refname, "refs/", 5) == 0)
250 refname += 5;
251 if (strncmp(refname, "heads/", 6) == 0)
252 refname += 6;
254 if (asprintf(remote_refname, "refs/remotes/%s/%s",
255 remote_name, refname) == -1)
256 return got_error_from_errno("asprintf");
258 return NULL;
261 static const struct got_error *
262 update_remote_ref(struct got_reference *my_ref, const char *remote_name,
263 struct got_repository *repo)
265 const struct got_error *err, *unlock_err;
266 struct got_object_id *my_id;
267 struct got_reference *ref = NULL;
268 char *remote_refname = NULL;
269 int ref_locked = 0;
271 err = got_ref_resolve(&my_id, repo, my_ref);
272 if (err)
273 return err;
275 err = get_remote_refname(&remote_refname, remote_name,
276 got_ref_get_name(my_ref));
277 if (err)
278 goto done;
280 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
281 if (err) {
282 if (err->code != GOT_ERR_NOT_REF)
283 goto done;
284 err = got_ref_alloc(&ref, remote_refname, my_id);
285 if (err)
286 goto done;
287 } else {
288 ref_locked = 1;
289 err = got_ref_change_ref(ref, my_id);
290 if (err)
291 goto done;
294 err = got_ref_write(ref, repo);
295 done:
296 if (ref) {
297 if (ref_locked) {
298 unlock_err = got_ref_unlock(ref);
299 if (unlock_err && err == NULL)
300 err = unlock_err;
302 got_ref_close(ref);
304 free(my_id);
305 free(remote_refname);
306 return err;
309 const struct got_error*
310 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
311 struct got_pathlist_head *tag_names,
312 struct got_pathlist_head *delete_branches,
313 int verbosity, int overwrite_refs, int sendfd,
314 struct got_repository *repo, got_send_progress_cb progress_cb,
315 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
317 int imsg_sendfds[2];
318 int npackfd = -1, nsendfd = -1;
319 int sendstatus, done = 0;
320 const struct got_error *err;
321 struct imsgbuf sendibuf;
322 pid_t sendpid = -1;
323 struct got_reflist_head refs;
324 struct got_pathlist_head have_refs;
325 struct got_pathlist_head their_refs;
326 struct got_pathlist_entry *pe;
327 struct got_reflist_entry *re;
328 struct got_object_id **our_ids = NULL;
329 struct got_object_id **their_ids = NULL;
330 int i, nours = 0, ntheirs = 0;
331 size_t nalloc_ours = 0, nalloc_theirs = 0;
332 int refs_to_send = 0, refs_to_delete = 0;
333 off_t bytes_sent = 0, bytes_sent_cur = 0;
334 struct pack_progress_arg ppa;
335 uint8_t packsha1[SHA1_DIGEST_LENGTH];
336 int packfd = -1;
337 FILE *delta_cache = NULL;
339 TAILQ_INIT(&refs);
340 TAILQ_INIT(&have_refs);
341 TAILQ_INIT(&their_refs);
343 TAILQ_FOREACH(pe, branch_names, entry) {
344 const char *branchname = pe->path;
345 if (strncmp(branchname, "refs/heads/", 11) != 0) {
346 char *s;
347 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
348 err = got_error_from_errno("asprintf");
349 goto done;
351 err = insert_ref(&refs, s, repo);
352 free(s);
353 } else {
354 err = insert_ref(&refs, branchname, repo);
356 if (err)
357 goto done;
360 TAILQ_FOREACH(pe, delete_branches, entry) {
361 const char *branchname = pe->path;
362 struct got_reference *ref;
363 if (strncmp(branchname, "refs/heads/", 11) != 0) {
364 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
365 branchname);
366 goto done;
368 ref = find_ref(&refs, branchname);
369 if (ref) {
370 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
371 "changes on %s will be sent to server",
372 branchname);
373 goto done;
377 TAILQ_FOREACH(pe, tag_names, entry) {
378 const char *tagname = pe->path;
379 if (strncmp(tagname, "refs/tags/", 10) != 0) {
380 char *s;
381 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
382 err = got_error_from_errno("asprintf");
383 goto done;
385 err = insert_ref(&refs, s, repo);
386 free(s);
387 } else {
388 err = insert_ref(&refs, tagname, repo);
390 if (err)
391 goto done;
394 if (TAILQ_EMPTY(&refs) && TAILQ_EMPTY(delete_branches)) {
395 err = got_error(GOT_ERR_SEND_EMPTY);
396 goto done;
399 TAILQ_FOREACH(re, &refs, entry) {
400 struct got_object_id *id;
401 int obj_type;
403 if (got_ref_is_symbolic(re->ref)) {
404 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
405 "cannot send symbolic reference %s",
406 got_ref_get_name(re->ref));
407 goto done;
410 err = got_ref_resolve(&id, repo, re->ref);
411 if (err)
412 goto done;
413 err = got_object_get_type(&obj_type, repo, id);
414 free(id);
415 if (err)
416 goto done;
417 switch (obj_type) {
418 case GOT_OBJ_TYPE_COMMIT:
419 case GOT_OBJ_TYPE_TAG:
420 break;
421 default:
422 err = got_error_fmt(GOT_ERR_OBJ_TYPE,
423 "cannot send %s", got_ref_get_name(re->ref));
424 goto done;
428 packfd = got_opentempfd();
429 if (packfd == -1) {
430 err = got_error_from_errno("got_opentempfd");
431 goto done;
434 delta_cache = got_opentemp();
435 if (delta_cache == 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_common_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 struct got_ratelimit rl;
632 got_ratelimit_init(&rl, 0, 500);
633 memset(&ppa, 0, sizeof(ppa));
634 ppa.progress_cb = progress_cb;
635 ppa.progress_arg = progress_arg;
636 err = got_pack_create(packsha1, packfd, delta_cache,
637 their_ids, ntheirs, our_ids, nours, repo, 0, 1, 0,
638 pack_progress, &ppa, &rl, cancel_cb, cancel_arg);
639 if (err)
640 goto done;
642 npackfd = dup(packfd);
643 if (npackfd == -1) {
644 err = got_error_from_errno("dup");
645 goto done;
647 err = got_privsep_send_packfd(&sendibuf, npackfd);
648 if (err != NULL)
649 goto done;
650 npackfd = -1;
651 } else {
652 err = got_privsep_send_packfd(&sendibuf, -1);
653 if (err != NULL)
654 goto done;
657 while (!done) {
658 int success = 0;
659 char *refname = NULL;
660 char *errmsg = NULL;
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, &errmsg, &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.ncolored,
689 ppa.nfound, ppa.ntrees, ppa.packfile_size,
690 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
691 ppa.nobj_written, bytes_sent,
692 refname, errmsg, success);
693 if (err) {
694 free(refname);
695 free(errmsg);
696 goto done;
698 bytes_sent_cur = bytes_sent;
700 free(refname);
701 free(errmsg);
703 done:
704 if (sendpid != -1) {
705 if (err)
706 got_privsep_send_stop(imsg_sendfds[0]);
707 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
708 err = got_error_from_errno("waitpid");
710 if (packfd != -1 && close(packfd) == -1 && err == NULL)
711 err = got_error_from_errno("close");
712 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
713 err = got_error_from_errno("fclose");
714 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
715 err = got_error_from_errno("close");
716 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
717 err = got_error_from_errno("close");
719 got_ref_list_free(&refs);
720 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_NONE);
721 got_pathlist_free(&their_refs, GOT_PATHLIST_FREE_NONE);
722 for (i = 0; i < nours; i++)
723 free(our_ids[i]);
724 free(our_ids);
725 for (i = 0; i < ntheirs; i++)
726 free(their_ids[i]);
727 free(their_ids);
728 return err;