Blob


1 /*
2 * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2023 Josh Rickmar <jrick@zettaport.com>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/queue.h>
22 #include <sys/tree.h>
23 #include <sys/uio.h>
24 #include <sys/socket.h>
25 #include <sys/wait.h>
26 #include <sys/resource.h>
27 #include <sys/socket.h>
29 #include <endian.h>
30 #include <errno.h>
31 #include <err.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <sha1.h>
38 #include <sha2.h>
39 #include <unistd.h>
40 #include <zlib.h>
41 #include <ctype.h>
42 #include <limits.h>
43 #include <imsg.h>
44 #include <time.h>
45 #include <uuid.h>
47 #include "got_error.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
50 #include "got_path.h"
51 #include "got_cancel.h"
52 #include "got_worktree.h"
53 #include "got_object.h"
54 #include "got_opentemp.h"
55 #include "got_send.h"
56 #include "got_repository_admin.h"
57 #include "got_commit_graph.h"
59 #include "got_lib_delta.h"
60 #include "got_lib_inflate.h"
61 #include "got_lib_object.h"
62 #include "got_lib_object_parse.h"
63 #include "got_lib_object_create.h"
64 #include "got_lib_pack.h"
65 #include "got_lib_hash.h"
66 #include "got_lib_privsep.h"
67 #include "got_lib_object_cache.h"
68 #include "got_lib_repository.h"
69 #include "got_lib_ratelimit.h"
70 #include "got_lib_pack_create.h"
71 #include "got_lib_dial.h"
73 #ifndef nitems
74 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 #endif
77 #ifndef ssizeof
78 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
79 #endif
81 #ifndef MIN
82 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
83 #endif
85 const struct got_error *
86 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
87 const char *host, const char *port, const char *server_path, int verbosity)
88 {
89 const struct got_error *err = NULL;
91 *sendpid = -1;
92 *sendfd = -1;
94 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
95 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
96 GOT_DIAL_CMD_SEND, verbosity);
97 else if (strcmp(proto, "git") == 0)
98 err = got_dial_git(sendfd, host, port, server_path,
99 GOT_DIAL_CMD_SEND);
100 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
101 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
102 else
103 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
104 return err;
107 struct pack_progress_arg {
108 got_send_progress_cb progress_cb;
109 void *progress_arg;
111 int ncolored;
112 int nfound;
113 int ntrees;
114 off_t packfile_size;
115 int ncommits;
116 int nobj_total;
117 int nobj_deltify;
118 int nobj_written;
119 };
121 static const struct got_error *
122 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
123 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
124 int nobj_written)
126 const struct got_error *err;
127 struct pack_progress_arg *a = arg;
129 err = a->progress_cb(a->progress_arg, ncolored, nfound, ntrees,
130 packfile_size, ncommits, nobj_total, nobj_deltify,
131 nobj_written, 0, NULL, NULL, 0);
132 if (err)
133 return err;
135 a->ncolored= ncolored;
136 a->nfound = nfound;
137 a->ntrees = ntrees;
138 a->packfile_size = packfile_size;
139 a->ncommits = ncommits;
140 a->nobj_total = nobj_total;
141 a->nobj_deltify = nobj_deltify;
142 a->nobj_written = nobj_written;
143 return NULL;
146 static const struct got_error *
147 insert_sendable_ref(struct got_pathlist_head *refs, const char *refname,
148 struct got_repository *repo)
150 const struct got_error *err;
151 struct got_reference *ref;
152 struct got_object_id *id = NULL;
153 int obj_type;
155 err = got_ref_open(&ref, repo, refname, 0);
156 if (err)
157 return err;
159 if (got_ref_is_symbolic(ref)) {
160 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
161 "cannot send symbolic reference %s", refname);
162 goto done;
165 err = got_ref_resolve(&id, repo, ref);
166 if (err)
167 goto done;
168 err = got_object_get_type(&obj_type, repo, id);
169 if (err)
170 goto done;
171 switch (obj_type) {
172 case GOT_OBJ_TYPE_COMMIT:
173 case GOT_OBJ_TYPE_TAG:
174 break;
175 default:
176 err = got_error_fmt(GOT_ERR_OBJ_TYPE," cannot send %s",
177 refname);
178 goto done;
181 err = got_pathlist_insert(NULL, refs, refname, id);
182 done:
183 if (ref)
184 got_ref_close(ref);
185 if (err)
186 free(id);
187 return err;
190 static const struct got_error *
191 check_common_ancestry(const char *refname, struct got_object_id *my_id,
192 struct got_object_id *their_id, struct got_repository *repo,
193 got_cancel_cb cancel_cb, void *cancel_arg)
195 const struct got_error *err = NULL;
196 struct got_object_id *yca_id;
197 int obj_type;
199 err = got_object_get_type(&obj_type, repo, their_id);
200 if (err)
201 return err;
202 if (obj_type != GOT_OBJ_TYPE_COMMIT)
203 return got_error_fmt(GOT_ERR_OBJ_TYPE,
204 "bad object type on server for %s", refname);
206 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
207 my_id, their_id, 0, repo, cancel_cb, cancel_arg);
208 if (err)
209 return err;
210 if (yca_id == NULL)
211 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
213 if (got_object_id_cmp(their_id, yca_id) != 0)
214 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
216 free(yca_id);
217 return err;
220 static const struct got_error *
221 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
223 struct got_object_id **new;
224 const size_t alloc_chunksz = 256;
226 if (*nalloc >= n)
227 return NULL;
229 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
230 sizeof(struct got_object_id));
231 if (new == NULL)
232 return got_error_from_errno("recallocarray");
234 *ids = new;
235 *nalloc += alloc_chunksz;
236 return NULL;
239 static struct got_pathlist_entry *
240 find_ref(struct got_pathlist_head *refs, const char *refname)
242 struct got_pathlist_entry *pe;
244 TAILQ_FOREACH(pe, refs, entry) {
245 if (got_path_cmp(pe->path, refname, strlen(pe->path),
246 strlen(refname)) == 0) {
247 return pe;
251 return NULL;
254 static const struct got_error *
255 get_remote_refname(char **remote_refname, const char *remote_name,
256 const char *refname)
258 if (strncmp(refname, "refs/", 5) == 0)
259 refname += 5;
260 if (strncmp(refname, "heads/", 6) == 0)
261 refname += 6;
263 if (asprintf(remote_refname, "refs/remotes/%s/%s",
264 remote_name, refname) == -1)
265 return got_error_from_errno("asprintf");
267 return NULL;
270 static const struct got_error *
271 update_remote_ref(struct got_pathlist_entry *my_ref, const char *remote_name,
272 struct got_repository *repo)
274 const struct got_error *err, *unlock_err;
275 const char *refname = my_ref->path;
276 struct got_object_id *my_id = my_ref->data;
277 struct got_reference *ref = NULL;
278 char *remote_refname = NULL;
279 int ref_locked = 0;
281 err = get_remote_refname(&remote_refname, remote_name, refname);
282 if (err)
283 goto done;
285 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
286 if (err) {
287 if (err->code != GOT_ERR_NOT_REF)
288 goto done;
289 err = got_ref_alloc(&ref, remote_refname, my_id);
290 if (err)
291 goto done;
292 } else {
293 ref_locked = 1;
294 err = got_ref_change_ref(ref, my_id);
295 if (err)
296 goto done;
299 err = got_ref_write(ref, repo);
300 done:
301 if (ref) {
302 if (ref_locked) {
303 unlock_err = got_ref_unlock(ref);
304 if (unlock_err && err == NULL)
305 err = unlock_err;
307 got_ref_close(ref);
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_pathlist_head have_refs;
328 struct got_pathlist_head their_refs;
329 struct got_pathlist_entry *pe;
330 struct got_object_id **our_ids = NULL;
331 struct got_object_id **their_ids = NULL;
332 int nours = 0, ntheirs = 0;
333 size_t nalloc_ours = 0, nalloc_theirs = 0;
334 int refs_to_send = 0, refs_to_delete = 0;
335 off_t bytes_sent = 0, bytes_sent_cur = 0;
336 struct pack_progress_arg ppa;
337 uint8_t packsha1[SHA1_DIGEST_LENGTH];
338 int packfd = -1;
339 FILE *delta_cache = NULL;
340 char *s = NULL;
342 TAILQ_INIT(&have_refs);
343 TAILQ_INIT(&their_refs);
345 TAILQ_FOREACH(pe, branch_names, entry) {
346 const char *branchname = pe->path;
347 if (strncmp(branchname, "refs/heads/", 11) != 0) {
348 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
349 err = got_error_from_errno("asprintf");
350 goto done;
352 } else {
353 if ((s = strdup(branchname)) == NULL) {
354 err = got_error_from_errno("strdup");
355 goto done;
358 err = insert_sendable_ref(&have_refs, s, repo);
359 if (err)
360 goto done;
361 s = NULL;
364 TAILQ_FOREACH(pe, delete_branches, entry) {
365 const char *branchname = pe->path;
366 struct got_pathlist_entry *ref;
367 if (strncmp(branchname, "refs/heads/", 11) != 0) {
368 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
369 branchname);
370 goto done;
372 ref = find_ref(&have_refs, branchname);
373 if (ref) {
374 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
375 "changes on %s will be sent to server",
376 branchname);
377 goto done;
381 TAILQ_FOREACH(pe, tag_names, entry) {
382 const char *tagname = pe->path;
383 if (strncmp(tagname, "refs/tags/", 10) != 0) {
384 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
385 err = got_error_from_errno("asprintf");
386 goto done;
388 } else {
389 if ((s = strdup(pe->path)) == NULL) {
390 err = got_error_from_errno("strdup");
391 goto done;
394 err = insert_sendable_ref(&have_refs, s, repo);
395 if (err)
396 goto done;
397 s = NULL;
400 if (TAILQ_EMPTY(&have_refs) && TAILQ_EMPTY(delete_branches)) {
401 err = got_error(GOT_ERR_SEND_EMPTY);
402 goto done;
405 packfd = got_opentempfd();
406 if (packfd == -1) {
407 err = got_error_from_errno("got_opentempfd");
408 goto done;
411 delta_cache = got_opentemp();
412 if (delta_cache == NULL) {
413 err = got_error_from_errno("got_opentemp");
414 goto done;
417 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
418 err = got_error_from_errno("socketpair");
419 goto done;
422 sendpid = fork();
423 if (sendpid == -1) {
424 err = got_error_from_errno("fork");
425 goto done;
426 } else if (sendpid == 0){
427 got_privsep_exec_child(imsg_sendfds,
428 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
431 if (close(imsg_sendfds[1]) == -1) {
432 err = got_error_from_errno("close");
433 goto done;
435 imsg_init(&sendibuf, imsg_sendfds[0]);
436 nsendfd = dup(sendfd);
437 if (nsendfd == -1) {
438 err = got_error_from_errno("dup");
439 goto done;
442 /*
443 * Prepare the array of our object IDs which
444 * will be needed for generating a pack file.
445 */
446 TAILQ_FOREACH(pe, &have_refs, entry) {
447 struct got_object_id *id = pe->data;
449 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
450 if (err)
451 goto done;
452 our_ids[nours] = id;
453 nours++;
456 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
457 delete_branches, verbosity);
458 if (err)
459 goto done;
460 nsendfd = -1;
462 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
463 if (err)
464 goto done;
466 /*
467 * Process references reported by the server.
468 * Push appropriate object IDs onto the "their IDs" array.
469 * This array will be used to exclude objects which already
470 * exist on the server from our pack file.
471 */
472 TAILQ_FOREACH(pe, &their_refs, entry) {
473 const char *refname = pe->path;
474 struct got_object_id *their_id = pe->data;
475 int have_their_id;
476 struct got_object *obj;
477 struct got_pathlist_entry *my_ref = NULL;
478 int is_tag = 0;
480 /* Don't blindly trust the server to send us valid names. */
481 if (!got_ref_name_is_valid(refname))
482 continue;
484 if (strncmp(refname, "refs/tags/", 10) == 0)
485 is_tag = 1;
486 /*
487 * Find out whether this is a reference we want to upload.
488 * Otherwise we can still use this reference as a hint to
489 * avoid uploading any objects the server already has.
490 */
491 my_ref = find_ref(&have_refs, refname);
492 if (my_ref) {
493 struct got_object_id *my_id = my_ref->data;
494 if (got_object_id_cmp(my_id, their_id) != 0) {
495 if (!overwrite_refs && is_tag) {
496 err = got_error_fmt(
497 GOT_ERR_SEND_TAG_EXISTS,
498 "%s", refname);
499 goto done;
501 refs_to_send++;
505 /* Check if their object exists locally. */
506 err = got_object_open(&obj, repo, their_id);
507 if (err) {
508 if (err->code != GOT_ERR_NO_OBJ)
509 goto done;
510 if (!overwrite_refs && my_ref != NULL) {
511 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
512 "%s", refname);
513 goto done;
515 have_their_id = 0;
516 } else {
517 got_object_close(obj);
518 have_their_id = 1;
521 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
522 if (err)
523 goto done;
525 if (have_their_id) {
526 /* Enforce linear ancestry if required. */
527 if (!overwrite_refs && my_ref && !is_tag) {
528 struct got_object_id *my_id = my_ref->data;
529 err = check_common_ancestry(refname, my_id,
530 their_id, repo, cancel_cb, cancel_arg);
531 if (err)
532 goto done;
534 /* Exclude any objects reachable via their ID. */
535 their_ids[ntheirs] = their_id;
536 ntheirs++;
537 } else if (!is_tag) {
538 char *remote_refname;
539 struct got_reference *ref;
540 /*
541 * Exclude any objects which exist on the server
542 * according to a locally cached remote reference.
543 */
544 err = get_remote_refname(&remote_refname,
545 remote_name, refname);
546 if (err)
547 goto done;
548 err = got_ref_open(&ref, repo, remote_refname, 0);
549 free(remote_refname);
550 if (err) {
551 if (err->code != GOT_ERR_NOT_REF)
552 goto done;
553 } else {
554 err = got_ref_resolve(&their_ids[ntheirs],
555 repo, ref);
556 got_ref_close(ref);
557 if (err)
558 goto done;
559 ntheirs++;
564 /* Account for any new references we are going to upload. */
565 TAILQ_FOREACH(pe, &have_refs, entry) {
566 const char *refname = pe->path;
567 if (find_ref(&their_refs, refname) == NULL)
568 refs_to_send++;
571 /* Account for any existing references we are going to delete. */
572 TAILQ_FOREACH(pe, delete_branches, entry) {
573 const char *branchname = pe->path;
574 if (find_ref(&their_refs, branchname))
575 refs_to_delete++;
578 if (refs_to_send == 0 && refs_to_delete == 0) {
579 got_privsep_send_stop(imsg_sendfds[0]);
580 goto done;
583 if (refs_to_send > 0) {
584 struct got_ratelimit rl;
585 got_ratelimit_init(&rl, 0, 500);
586 memset(&ppa, 0, sizeof(ppa));
587 ppa.progress_cb = progress_cb;
588 ppa.progress_arg = progress_arg;
589 err = got_pack_create(packsha1, packfd, delta_cache,
590 their_ids, ntheirs, our_ids, nours, repo, 0, 1, 0,
591 pack_progress, &ppa, &rl, cancel_cb, cancel_arg);
592 if (err)
593 goto done;
595 npackfd = dup(packfd);
596 if (npackfd == -1) {
597 err = got_error_from_errno("dup");
598 goto done;
600 err = got_privsep_send_packfd(&sendibuf, npackfd);
601 if (err != NULL)
602 goto done;
603 npackfd = -1;
604 } else {
605 err = got_privsep_send_packfd(&sendibuf, -1);
606 if (err != NULL)
607 goto done;
610 while (!done) {
611 int success = 0;
612 char *refname = NULL;
613 char *errmsg = NULL;
615 if (cancel_cb) {
616 err = (*cancel_cb)(cancel_arg);
617 if (err)
618 goto done;
620 err = got_privsep_recv_send_progress(&done, &bytes_sent,
621 &success, &refname, &errmsg, &sendibuf);
622 if (err)
623 goto done;
624 if (refname && got_ref_name_is_valid(refname) && success &&
625 strncmp(refname, "refs/tags/", 10) != 0) {
626 struct got_pathlist_entry *my_ref;
627 /*
628 * The server has accepted our changes.
629 * Update our reference in refs/remotes/ accordingly.
630 */
631 my_ref = find_ref(&have_refs, refname);
632 if (my_ref) {
633 err = update_remote_ref(my_ref, remote_name,
634 repo);
635 if (err)
636 goto done;
639 if (refname != NULL ||
640 bytes_sent_cur != bytes_sent) {
641 err = progress_cb(progress_arg, ppa.ncolored,
642 ppa.nfound, ppa.ntrees, ppa.packfile_size,
643 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
644 ppa.nobj_written, bytes_sent,
645 refname, errmsg, success);
646 if (err) {
647 free(refname);
648 free(errmsg);
649 goto done;
651 bytes_sent_cur = bytes_sent;
653 free(refname);
654 free(errmsg);
656 done:
657 if (sendpid != -1) {
658 if (err)
659 got_privsep_send_stop(imsg_sendfds[0]);
660 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
661 err = got_error_from_errno("waitpid");
663 if (packfd != -1 && close(packfd) == -1 && err == NULL)
664 err = got_error_from_errno("close");
665 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
666 err = got_error_from_errno("fclose");
667 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
668 err = got_error_from_errno("close");
669 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
670 err = got_error_from_errno("close");
672 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
673 got_pathlist_free(&their_refs, GOT_PATHLIST_FREE_ALL);
674 /*
675 * Object ids are owned by have_refs/their_refs and are already freed;
676 * Only the arrays must be freed.
677 */
678 free(our_ids);
679 free(their_ids);
680 free(s);
681 return err;