slight code simplification, seeing that clear_meta(NULL) is fine
[got-portable.git] / lib / send.c
blob2b7c36baf89ab7470fda3802cbe1bf5fcea0195f
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>
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.
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.
19 #include "got_compat.h"
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/queue.h>
24 #include <sys/uio.h>
25 #include <sys/socket.h>
26 #include <sys/wait.h>
27 #include <sys/resource.h>
28 #include <sys/socket.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 <unistd.h>
38 #include <zlib.h>
39 #include <ctype.h>
40 #include <limits.h>
41 #include <time.h>
43 #include "got_error.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_path.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
49 #include "got_object.h"
50 #include "got_opentemp.h"
51 #include "got_send.h"
52 #include "got_repository_admin.h"
53 #include "got_commit_graph.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_hash.h"
57 #include "got_lib_inflate.h"
58 #include "got_lib_object.h"
59 #include "got_lib_object_parse.h"
60 #include "got_lib_object_create.h"
61 #include "got_lib_pack.h"
62 #include "got_lib_privsep.h"
63 #include "got_lib_object_cache.h"
64 #include "got_lib_repository.h"
65 #include "got_lib_ratelimit.h"
66 #include "got_lib_pack_create.h"
67 #include "got_lib_dial.h"
68 #include "got_lib_worktree_cvg.h"
69 #include "got_lib_poll.h"
71 #ifndef nitems
72 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
73 #endif
75 #ifndef ssizeof
76 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
77 #endif
79 #ifndef MIN
80 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
81 #endif
83 const struct got_error *
84 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
85 const char *host, const char *port, const char *server_path, int verbosity)
87 const struct got_error *err = NULL;
89 *sendpid = -1;
90 *sendfd = -1;
92 if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
93 err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
94 GOT_DIAL_CMD_SEND, verbosity);
95 else if (strcmp(proto, "git") == 0)
96 err = got_dial_git(sendfd, host, port, server_path,
97 GOT_DIAL_CMD_SEND);
98 else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
99 err = got_error_path(proto, GOT_ERR_NOT_IMPL);
100 else
101 err = got_error_path(proto, GOT_ERR_BAD_PROTO);
102 return err;
105 struct pack_progress_arg {
106 got_send_progress_cb progress_cb;
107 void *progress_arg;
108 int sendfd;
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;
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;
135 * Detect the server closing our connection while we are
136 * busy creating a pack file.
138 * XXX This should be a temporary workaround. A better fix would
139 * be to avoid use of an on-disk tempfile for pack file data.
140 * Instead we could stream pack file data to got-send-pack while
141 * the pack file is being generated. Write errors in got-send-pack
142 * would then automatically abort the creation of pack file data.
144 err = got_poll_fd(a->sendfd, 0, 0);
145 if (err && err->code != GOT_ERR_TIMEOUT) {
146 if (err->code == GOT_ERR_EOF) {
147 err = got_error_msg(GOT_ERR_EOF,
148 "server unexpectedly closed the connection");
150 return err;
153 a->ncolored= ncolored;
154 a->nfound = nfound;
155 a->ntrees = ntrees;
156 a->packfile_size = packfile_size;
157 a->ncommits = ncommits;
158 a->nobj_total = nobj_total;
159 a->nobj_deltify = nobj_deltify;
160 a->nobj_written = nobj_written;
161 return NULL;
164 static const struct got_error *
165 insert_sendable_ref(struct got_pathlist_head *refs, const char *refname,
166 const char *target_refname, struct got_repository *repo)
168 const struct got_error *err;
169 struct got_reference *ref;
170 struct got_object_id *id = NULL;
171 struct got_pathlist_entry *new = NULL;
172 int obj_type;
174 err = got_ref_open(&ref, repo, refname, 0);
175 if (err)
176 return err;
178 if (got_ref_is_symbolic(ref)) {
179 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
180 "cannot send symbolic reference %s", refname);
181 goto done;
184 err = got_ref_resolve(&id, repo, ref);
185 if (err)
186 goto done;
187 err = got_object_get_type(&obj_type, repo, id);
188 if (err)
189 goto done;
190 switch (obj_type) {
191 case GOT_OBJ_TYPE_COMMIT:
192 case GOT_OBJ_TYPE_TAG:
193 break;
194 default:
195 err = got_error_fmt(GOT_ERR_OBJ_TYPE," cannot send %s",
196 refname);
197 goto done;
200 err = got_pathlist_insert(&new, refs, target_refname, id);
201 done:
202 if (ref)
203 got_ref_close(ref);
204 if (err || new == NULL /* duplicate */)
205 free(id);
206 return err;
209 static const struct got_error *
210 check_common_ancestry(const char *refname, struct got_object_id *my_id,
211 struct got_object_id *their_id, struct got_repository *repo,
212 got_cancel_cb cancel_cb, void *cancel_arg)
214 const struct got_error *err = NULL;
215 struct got_object_id *yca_id;
216 int obj_type;
218 err = got_object_get_type(&obj_type, repo, their_id);
219 if (err)
220 return err;
221 if (obj_type != GOT_OBJ_TYPE_COMMIT)
222 return got_error_fmt(GOT_ERR_OBJ_TYPE,
223 "bad object type on server for %s", refname);
225 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
226 my_id, their_id, 0, 1, repo, cancel_cb, cancel_arg);
227 if (err)
228 return err;
229 if (yca_id == NULL)
230 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
232 if (got_object_id_cmp(their_id, yca_id) != 0)
233 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
235 free(yca_id);
236 return err;
239 static const struct got_error *
240 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
242 struct got_object_id **new;
243 const size_t alloc_chunksz = 256;
245 if (*nalloc >= n)
246 return NULL;
248 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
249 sizeof(struct got_object_id));
250 if (new == NULL)
251 return got_error_from_errno("recallocarray");
253 *ids = new;
254 *nalloc += alloc_chunksz;
255 return NULL;
258 static struct got_pathlist_entry *
259 find_ref(struct got_pathlist_head *refs, const char *refname)
261 struct got_pathlist_entry *pe;
263 TAILQ_FOREACH(pe, refs, entry) {
264 if (got_path_cmp(pe->path, refname, strlen(pe->path),
265 strlen(refname)) == 0) {
266 return pe;
270 return NULL;
273 static const struct got_error *
274 get_remote_refname(char **remote_refname, const char *remote_name,
275 const char *refname)
277 if (strncmp(refname, "refs/", 5) == 0)
278 refname += 5;
279 if (strncmp(refname, "heads/", 6) == 0)
280 refname += 6;
282 if (asprintf(remote_refname, "refs/remotes/%s/%s",
283 remote_name, refname) == -1)
284 return got_error_from_errno("asprintf");
286 return NULL;
289 static const struct got_error *
290 update_remote_ref(struct got_pathlist_entry *my_ref, const char *remote_name,
291 struct got_repository *repo)
293 const struct got_error *err, *unlock_err;
294 const char *refname = my_ref->path;
295 struct got_object_id *my_id = my_ref->data;
296 struct got_reference *ref = NULL;
297 char *remote_refname = NULL;
298 int ref_locked = 0;
300 err = get_remote_refname(&remote_refname, remote_name, refname);
301 if (err)
302 goto done;
304 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
305 if (err) {
306 if (err->code != GOT_ERR_NOT_REF)
307 goto done;
308 err = got_ref_alloc(&ref, remote_refname, my_id);
309 if (err)
310 goto done;
311 } else {
312 ref_locked = 1;
313 err = got_ref_change_ref(ref, my_id);
314 if (err)
315 goto done;
318 err = got_ref_write(ref, repo);
319 done:
320 if (ref) {
321 if (ref_locked) {
322 unlock_err = got_ref_unlock(ref);
323 if (unlock_err && err == NULL)
324 err = unlock_err;
326 got_ref_close(ref);
328 free(remote_refname);
329 return err;
332 const struct got_error*
333 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
334 struct got_pathlist_head *tag_names,
335 struct got_pathlist_head *delete_branches,
336 int verbosity, int overwrite_refs, int sendfd,
337 struct got_repository *repo, got_send_progress_cb progress_cb,
338 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
340 int imsg_sendfds[2] = { -1, -1 };
341 int npackfd = -1, nsendfd = -1;
342 int sendstatus, done = 0;
343 const struct got_error *err;
344 struct imsgbuf sendibuf;
345 pid_t sendpid = -1;
346 struct got_pathlist_head have_refs;
347 struct got_pathlist_head their_refs;
348 struct got_pathlist_entry *pe;
349 struct got_object_id **our_ids = NULL;
350 struct got_object_id **their_ids = NULL;
351 int nours = 0, ntheirs = 0;
352 size_t nalloc_ours = 0, nalloc_theirs = 0;
353 int refs_to_send = 0, refs_to_delete = 0;
354 off_t bytes_sent = 0, bytes_sent_cur = 0;
355 struct pack_progress_arg ppa;
356 struct got_object_id packhash;
357 int packfd = -1;
358 FILE *delta_cache = NULL;
359 char *s = NULL;
361 TAILQ_INIT(&have_refs);
362 TAILQ_INIT(&their_refs);
364 if (got_repo_get_object_format(repo) != GOT_HASH_SHA1)
365 return got_error_fmt(GOT_ERR_NOT_IMPL,
366 "sha256 object IDs unsupported in network protocol");
368 TAILQ_FOREACH(pe, branch_names, entry) {
369 const char *branchname = pe->path;
370 const char *targetname = pe->data;
372 if (targetname == NULL)
373 targetname = branchname;
375 if (strncmp(targetname, "refs/heads/", 11) != 0) {
376 if (asprintf(&s, "refs/heads/%s", targetname) == -1) {
377 err = got_error_from_errno("asprintf");
378 goto done;
380 } else {
381 if ((s = strdup(targetname)) == NULL) {
382 err = got_error_from_errno("strdup");
383 goto done;
386 err = insert_sendable_ref(&have_refs, branchname, s, repo);
387 if (err)
388 goto done;
389 s = NULL;
392 TAILQ_FOREACH(pe, delete_branches, entry) {
393 const char *branchname = pe->path;
394 struct got_pathlist_entry *ref;
395 if (strncmp(branchname, "refs/heads/", 11) != 0) {
396 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
397 branchname);
398 goto done;
400 ref = find_ref(&have_refs, branchname);
401 if (ref) {
402 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
403 "changes on %s will be sent to server",
404 branchname);
405 goto done;
409 TAILQ_FOREACH(pe, tag_names, entry) {
410 const char *tagname = pe->path;
411 if (strncmp(tagname, "refs/tags/", 10) != 0) {
412 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
413 err = got_error_from_errno("asprintf");
414 goto done;
416 } else {
417 if ((s = strdup(pe->path)) == NULL) {
418 err = got_error_from_errno("strdup");
419 goto done;
422 err = insert_sendable_ref(&have_refs, s, s, repo);
423 if (err)
424 goto done;
425 s = NULL;
428 if (TAILQ_EMPTY(&have_refs) && TAILQ_EMPTY(delete_branches)) {
429 err = got_error(GOT_ERR_SEND_EMPTY);
430 goto done;
433 packfd = got_opentempfd();
434 if (packfd == -1) {
435 err = got_error_from_errno("got_opentempfd");
436 goto done;
439 delta_cache = got_opentemp();
440 if (delta_cache == NULL) {
441 err = got_error_from_errno("got_opentemp");
442 goto done;
445 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
446 err = got_error_from_errno("socketpair");
447 goto done;
450 sendpid = fork();
451 if (sendpid == -1) {
452 err = got_error_from_errno("fork");
453 goto done;
454 } else if (sendpid == 0) {
455 got_privsep_exec_child(imsg_sendfds,
456 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
459 if (close(imsg_sendfds[1]) == -1) {
460 err = got_error_from_errno("close");
461 goto done;
463 imsg_sendfds[1] = -1;
464 imsg_init(&sendibuf, imsg_sendfds[0]);
465 nsendfd = dup(sendfd);
466 if (nsendfd == -1) {
467 err = got_error_from_errno("dup");
468 goto done;
472 * Prepare the array of our object IDs which
473 * will be needed for generating a pack file.
475 TAILQ_FOREACH(pe, &have_refs, entry) {
476 struct got_object_id *id = pe->data;
478 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
479 if (err)
480 goto done;
481 our_ids[nours] = id;
482 nours++;
485 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
486 delete_branches, verbosity);
487 if (err)
488 goto done;
489 nsendfd = -1;
491 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
492 if (err)
493 goto done;
495 * Process references reported by the server.
496 * Push appropriate object IDs onto the "their IDs" array.
497 * This array will be used to exclude objects which already
498 * exist on the server from our pack file.
500 TAILQ_FOREACH(pe, &their_refs, entry) {
501 const char *refname = pe->path;
502 struct got_object_id *their_id = pe->data;
503 int have_their_id;
504 struct got_object *obj;
505 struct got_pathlist_entry *my_ref = NULL;
506 int is_tag = 0;
508 /* Don't blindly trust the server to send us valid names. */
509 if (!got_ref_name_is_valid(refname))
510 continue;
512 if (strncmp(refname, "refs/tags/", 10) == 0)
513 is_tag = 1;
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.
519 my_ref = find_ref(&have_refs, refname);
520 if (my_ref) {
521 struct got_object_id *my_id = my_ref->data;
522 if (got_object_id_cmp(my_id, their_id) != 0) {
523 if (!overwrite_refs && is_tag) {
524 err = got_error_fmt(
525 GOT_ERR_SEND_TAG_EXISTS,
526 "%s", refname);
527 goto done;
529 refs_to_send++;
533 /* Check if their object exists locally. */
534 err = got_object_open(&obj, repo, their_id);
535 if (err) {
536 if (err->code != GOT_ERR_NO_OBJ)
537 goto done;
538 if (!overwrite_refs && my_ref != NULL) {
539 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
540 "%s", refname);
541 goto done;
543 have_their_id = 0;
544 } else {
545 got_object_close(obj);
546 have_their_id = 1;
549 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
550 if (err)
551 goto done;
553 if (have_their_id) {
554 /* Enforce linear ancestry if required. */
555 if (!overwrite_refs && my_ref && !is_tag) {
556 struct got_object_id *my_id = my_ref->data;
557 err = check_common_ancestry(refname, my_id,
558 their_id, repo, cancel_cb, cancel_arg);
559 if (err)
560 goto done;
562 /* Exclude any objects reachable via their ID. */
563 their_ids[ntheirs] = their_id;
564 ntheirs++;
565 } else if (!is_tag) {
566 char *remote_refname;
567 struct got_reference *ref;
569 * Exclude any objects which exist on the server
570 * according to a locally cached remote reference.
572 err = get_remote_refname(&remote_refname,
573 remote_name, refname);
574 if (err)
575 goto done;
576 err = got_ref_open(&ref, repo, remote_refname, 0);
577 free(remote_refname);
578 if (err) {
579 if (err->code != GOT_ERR_NOT_REF)
580 goto done;
581 } else {
582 err = got_ref_resolve(&their_ids[ntheirs],
583 repo, ref);
584 got_ref_close(ref);
585 if (err)
586 goto done;
587 ntheirs++;
592 /* Account for any new references we are going to upload. */
593 TAILQ_FOREACH(pe, &have_refs, entry) {
594 const char *refname = pe->path;
595 if (find_ref(&their_refs, refname) == NULL)
596 refs_to_send++;
599 /* Account for any existing references we are going to delete. */
600 TAILQ_FOREACH(pe, delete_branches, entry) {
601 const char *branchname = pe->path;
602 if (find_ref(&their_refs, branchname))
603 refs_to_delete++;
606 if (refs_to_send == 0 && refs_to_delete == 0) {
607 got_privsep_send_stop(imsg_sendfds[0]);
608 goto done;
611 if (refs_to_send > 0) {
612 struct got_ratelimit rl;
613 got_ratelimit_init(&rl, 0, 500);
614 memset(&ppa, 0, sizeof(ppa));
615 ppa.progress_cb = progress_cb;
616 ppa.progress_arg = progress_arg;
617 ppa.sendfd = sendfd;
618 err = got_pack_create(&packhash, packfd, delta_cache,
619 their_ids, ntheirs, our_ids, nours, repo, 0, 1, 0,
620 pack_progress, &ppa, &rl, cancel_cb, cancel_arg);
621 if (err)
622 goto done;
624 npackfd = dup(packfd);
625 if (npackfd == -1) {
626 err = got_error_from_errno("dup");
627 goto done;
629 err = got_privsep_send_packfd(&sendibuf, npackfd);
630 if (err != NULL)
631 goto done;
632 npackfd = -1;
633 } else {
634 err = got_privsep_send_packfd(&sendibuf, -1);
635 if (err != NULL)
636 goto done;
639 while (!done) {
640 int success = 0;
641 char *refname = NULL;
642 char *errmsg = NULL;
644 if (cancel_cb) {
645 err = (*cancel_cb)(cancel_arg);
646 if (err)
647 goto done;
649 err = got_privsep_recv_send_progress(&done, &bytes_sent,
650 &success, &refname, &errmsg, &sendibuf);
651 if (err)
652 goto done;
653 if (refname && got_ref_name_is_valid(refname) && success &&
654 strncmp(refname, "refs/tags/", 10) != 0) {
655 struct got_pathlist_entry *my_ref;
657 * The server has accepted our changes.
658 * Update our reference in refs/remotes/ accordingly.
660 my_ref = find_ref(&have_refs, refname);
661 if (my_ref) {
662 err = update_remote_ref(my_ref, remote_name,
663 repo);
664 if (err)
665 goto done;
668 if (refname != NULL ||
669 bytes_sent_cur != bytes_sent) {
670 err = progress_cb(progress_arg, ppa.ncolored,
671 ppa.nfound, ppa.ntrees, ppa.packfile_size,
672 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
673 ppa.nobj_written, bytes_sent,
674 refname, errmsg, success);
675 if (err) {
676 free(refname);
677 free(errmsg);
678 goto done;
680 bytes_sent_cur = bytes_sent;
682 free(refname);
683 free(errmsg);
685 done:
686 if (sendpid != -1) {
687 if (err)
688 got_privsep_send_stop(imsg_sendfds[0]);
689 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
690 err = got_error_from_errno("waitpid");
692 if (imsg_sendfds[0] != -1 && close(imsg_sendfds[0]) == -1 && err == NULL)
693 err = got_error_from_errno("close");
694 if (imsg_sendfds[1] != -1 && close(imsg_sendfds[1]) == -1 && err == NULL)
695 err = got_error_from_errno("close");
696 if (packfd != -1 && close(packfd) == -1 && err == NULL)
697 err = got_error_from_errno("close");
698 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
699 err = got_error_from_errno("fclose");
700 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
701 err = got_error_from_errno("close");
702 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
703 err = got_error_from_errno("close");
705 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
706 got_pathlist_free(&their_refs, GOT_PATHLIST_FREE_ALL);
708 * Object ids are owned by have_refs/their_refs and are already freed;
709 * Only the arrays must be freed.
711 free(our_ids);
712 free(their_ids);
713 free(s);
714 return err;