plug some leak around imsg_init() error paths
[got-portable.git] / lib / send.c
blob746c2864eb29a5a281a4c584b7a025275d80fff6
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 int obj_type;
173 err = got_ref_open(&ref, repo, refname, 0);
174 if (err)
175 return err;
177 if (got_ref_is_symbolic(ref)) {
178 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
179 "cannot send symbolic reference %s", refname);
180 goto done;
183 err = got_ref_resolve(&id, repo, ref);
184 if (err)
185 goto done;
186 err = got_object_get_type(&obj_type, repo, id);
187 if (err)
188 goto done;
189 switch (obj_type) {
190 case GOT_OBJ_TYPE_COMMIT:
191 case GOT_OBJ_TYPE_TAG:
192 break;
193 default:
194 err = got_error_fmt(GOT_ERR_OBJ_TYPE," cannot send %s",
195 refname);
196 goto done;
199 err = got_pathlist_insert(NULL, refs, target_refname, id);
200 done:
201 if (ref)
202 got_ref_close(ref);
203 if (err)
204 free(id);
205 return err;
208 static const struct got_error *
209 check_common_ancestry(const char *refname, struct got_object_id *my_id,
210 struct got_object_id *their_id, struct got_repository *repo,
211 got_cancel_cb cancel_cb, void *cancel_arg)
213 const struct got_error *err = NULL;
214 struct got_object_id *yca_id;
215 int obj_type;
217 err = got_object_get_type(&obj_type, repo, their_id);
218 if (err)
219 return err;
220 if (obj_type != GOT_OBJ_TYPE_COMMIT)
221 return got_error_fmt(GOT_ERR_OBJ_TYPE,
222 "bad object type on server for %s", refname);
224 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
225 my_id, their_id, 0, 1, repo, cancel_cb, cancel_arg);
226 if (err)
227 return err;
228 if (yca_id == NULL)
229 return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
231 if (got_object_id_cmp(their_id, yca_id) != 0)
232 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
234 free(yca_id);
235 return err;
238 static const struct got_error *
239 realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
241 struct got_object_id **new;
242 const size_t alloc_chunksz = 256;
244 if (*nalloc >= n)
245 return NULL;
247 new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
248 sizeof(struct got_object_id));
249 if (new == NULL)
250 return got_error_from_errno("recallocarray");
252 *ids = new;
253 *nalloc += alloc_chunksz;
254 return NULL;
257 static struct got_pathlist_entry *
258 find_ref(struct got_pathlist_head *refs, const char *refname)
260 struct got_pathlist_entry *pe;
262 TAILQ_FOREACH(pe, refs, entry) {
263 if (got_path_cmp(pe->path, refname, strlen(pe->path),
264 strlen(refname)) == 0) {
265 return pe;
269 return NULL;
272 static const struct got_error *
273 get_remote_refname(char **remote_refname, const char *remote_name,
274 const char *refname)
276 if (strncmp(refname, "refs/", 5) == 0)
277 refname += 5;
278 if (strncmp(refname, "heads/", 6) == 0)
279 refname += 6;
281 if (asprintf(remote_refname, "refs/remotes/%s/%s",
282 remote_name, refname) == -1)
283 return got_error_from_errno("asprintf");
285 return NULL;
288 static const struct got_error *
289 update_remote_ref(struct got_pathlist_entry *my_ref, const char *remote_name,
290 struct got_repository *repo)
292 const struct got_error *err, *unlock_err;
293 const char *refname = my_ref->path;
294 struct got_object_id *my_id = my_ref->data;
295 struct got_reference *ref = NULL;
296 char *remote_refname = NULL;
297 int ref_locked = 0;
299 err = get_remote_refname(&remote_refname, remote_name, refname);
300 if (err)
301 goto done;
303 err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
304 if (err) {
305 if (err->code != GOT_ERR_NOT_REF)
306 goto done;
307 err = got_ref_alloc(&ref, remote_refname, my_id);
308 if (err)
309 goto done;
310 } else {
311 ref_locked = 1;
312 err = got_ref_change_ref(ref, my_id);
313 if (err)
314 goto done;
317 err = got_ref_write(ref, repo);
318 done:
319 if (ref) {
320 if (ref_locked) {
321 unlock_err = got_ref_unlock(ref);
322 if (unlock_err && err == NULL)
323 err = unlock_err;
325 got_ref_close(ref);
327 free(remote_refname);
328 return err;
331 const struct got_error*
332 got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
333 struct got_pathlist_head *tag_names,
334 struct got_pathlist_head *delete_branches,
335 int verbosity, int overwrite_refs, int sendfd,
336 struct got_repository *repo, got_send_progress_cb progress_cb,
337 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
339 int imsg_sendfds[2] = { -1, -1 };
340 int npackfd = -1, nsendfd = -1;
341 int sendstatus, done = 0;
342 const struct got_error *err;
343 struct imsgbuf sendibuf;
344 pid_t sendpid = -1;
345 struct got_pathlist_head have_refs;
346 struct got_pathlist_head their_refs;
347 struct got_pathlist_entry *pe;
348 struct got_object_id **our_ids = NULL;
349 struct got_object_id **their_ids = NULL;
350 int nours = 0, ntheirs = 0;
351 size_t nalloc_ours = 0, nalloc_theirs = 0;
352 int refs_to_send = 0, refs_to_delete = 0;
353 off_t bytes_sent = 0, bytes_sent_cur = 0;
354 struct pack_progress_arg ppa;
355 struct got_object_id packhash;
356 int packfd = -1;
357 FILE *delta_cache = NULL;
358 char *s = NULL;
360 TAILQ_INIT(&have_refs);
361 TAILQ_INIT(&their_refs);
363 if (got_repo_get_object_format(repo) != GOT_HASH_SHA1)
364 return got_error_fmt(GOT_ERR_NOT_IMPL,
365 "sha256 object IDs unsupported in network protocol");
367 TAILQ_FOREACH(pe, branch_names, entry) {
368 const char *branchname = pe->path;
369 const char *targetname = pe->data;
371 if (targetname == NULL)
372 targetname = branchname;
374 if (strncmp(targetname, "refs/heads/", 11) != 0) {
375 if (asprintf(&s, "refs/heads/%s", targetname) == -1) {
376 err = got_error_from_errno("asprintf");
377 goto done;
379 } else {
380 if ((s = strdup(targetname)) == NULL) {
381 err = got_error_from_errno("strdup");
382 goto done;
385 err = insert_sendable_ref(&have_refs, branchname, s, repo);
386 if (err)
387 goto done;
388 s = NULL;
391 TAILQ_FOREACH(pe, delete_branches, entry) {
392 const char *branchname = pe->path;
393 struct got_pathlist_entry *ref;
394 if (strncmp(branchname, "refs/heads/", 11) != 0) {
395 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
396 branchname);
397 goto done;
399 ref = find_ref(&have_refs, branchname);
400 if (ref) {
401 err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
402 "changes on %s will be sent to server",
403 branchname);
404 goto done;
408 TAILQ_FOREACH(pe, tag_names, entry) {
409 const char *tagname = pe->path;
410 if (strncmp(tagname, "refs/tags/", 10) != 0) {
411 if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
412 err = got_error_from_errno("asprintf");
413 goto done;
415 } else {
416 if ((s = strdup(pe->path)) == NULL) {
417 err = got_error_from_errno("strdup");
418 goto done;
421 err = insert_sendable_ref(&have_refs, s, s, repo);
422 if (err)
423 goto done;
424 s = NULL;
427 if (TAILQ_EMPTY(&have_refs) && TAILQ_EMPTY(delete_branches)) {
428 err = got_error(GOT_ERR_SEND_EMPTY);
429 goto done;
432 packfd = got_opentempfd();
433 if (packfd == -1) {
434 err = got_error_from_errno("got_opentempfd");
435 goto done;
438 delta_cache = got_opentemp();
439 if (delta_cache == NULL) {
440 err = got_error_from_errno("got_opentemp");
441 goto done;
444 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
445 err = got_error_from_errno("socketpair");
446 goto done;
449 sendpid = fork();
450 if (sendpid == -1) {
451 err = got_error_from_errno("fork");
452 goto done;
453 } else if (sendpid == 0) {
454 got_privsep_exec_child(imsg_sendfds,
455 GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
458 if (close(imsg_sendfds[1]) == -1) {
459 err = got_error_from_errno("close");
460 goto done;
462 imsg_sendfds[1] = -1;
463 imsg_init(&sendibuf, imsg_sendfds[0]);
464 nsendfd = dup(sendfd);
465 if (nsendfd == -1) {
466 err = got_error_from_errno("dup");
467 goto done;
471 * Prepare the array of our object IDs which
472 * will be needed for generating a pack file.
474 TAILQ_FOREACH(pe, &have_refs, entry) {
475 struct got_object_id *id = pe->data;
477 err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
478 if (err)
479 goto done;
480 our_ids[nours] = id;
481 nours++;
484 err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
485 delete_branches, verbosity);
486 if (err)
487 goto done;
488 nsendfd = -1;
490 err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
491 if (err)
492 goto done;
494 * Process references reported by the server.
495 * Push appropriate object IDs onto the "their IDs" array.
496 * This array will be used to exclude objects which already
497 * exist on the server from our pack file.
499 TAILQ_FOREACH(pe, &their_refs, entry) {
500 const char *refname = pe->path;
501 struct got_object_id *their_id = pe->data;
502 int have_their_id;
503 struct got_object *obj;
504 struct got_pathlist_entry *my_ref = NULL;
505 int is_tag = 0;
507 /* Don't blindly trust the server to send us valid names. */
508 if (!got_ref_name_is_valid(refname))
509 continue;
511 if (strncmp(refname, "refs/tags/", 10) == 0)
512 is_tag = 1;
514 * Find out whether this is a reference we want to upload.
515 * Otherwise we can still use this reference as a hint to
516 * avoid uploading any objects the server already has.
518 my_ref = find_ref(&have_refs, refname);
519 if (my_ref) {
520 struct got_object_id *my_id = my_ref->data;
521 if (got_object_id_cmp(my_id, their_id) != 0) {
522 if (!overwrite_refs && is_tag) {
523 err = got_error_fmt(
524 GOT_ERR_SEND_TAG_EXISTS,
525 "%s", refname);
526 goto done;
528 refs_to_send++;
532 /* Check if their object exists locally. */
533 err = got_object_open(&obj, repo, their_id);
534 if (err) {
535 if (err->code != GOT_ERR_NO_OBJ)
536 goto done;
537 if (!overwrite_refs && my_ref != NULL) {
538 err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
539 "%s", refname);
540 goto done;
542 have_their_id = 0;
543 } else {
544 got_object_close(obj);
545 have_their_id = 1;
548 err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
549 if (err)
550 goto done;
552 if (have_their_id) {
553 /* Enforce linear ancestry if required. */
554 if (!overwrite_refs && my_ref && !is_tag) {
555 struct got_object_id *my_id = my_ref->data;
556 err = check_common_ancestry(refname, my_id,
557 their_id, repo, cancel_cb, cancel_arg);
558 if (err)
559 goto done;
561 /* Exclude any objects reachable via their ID. */
562 their_ids[ntheirs] = their_id;
563 ntheirs++;
564 } else if (!is_tag) {
565 char *remote_refname;
566 struct got_reference *ref;
568 * Exclude any objects which exist on the server
569 * according to a locally cached remote reference.
571 err = get_remote_refname(&remote_refname,
572 remote_name, refname);
573 if (err)
574 goto done;
575 err = got_ref_open(&ref, repo, remote_refname, 0);
576 free(remote_refname);
577 if (err) {
578 if (err->code != GOT_ERR_NOT_REF)
579 goto done;
580 } else {
581 err = got_ref_resolve(&their_ids[ntheirs],
582 repo, ref);
583 got_ref_close(ref);
584 if (err)
585 goto done;
586 ntheirs++;
591 /* Account for any new references we are going to upload. */
592 TAILQ_FOREACH(pe, &have_refs, entry) {
593 const char *refname = pe->path;
594 if (find_ref(&their_refs, refname) == NULL)
595 refs_to_send++;
598 /* Account for any existing references we are going to delete. */
599 TAILQ_FOREACH(pe, delete_branches, entry) {
600 const char *branchname = pe->path;
601 if (find_ref(&their_refs, branchname))
602 refs_to_delete++;
605 if (refs_to_send == 0 && refs_to_delete == 0) {
606 got_privsep_send_stop(imsg_sendfds[0]);
607 goto done;
610 if (refs_to_send > 0) {
611 struct got_ratelimit rl;
612 got_ratelimit_init(&rl, 0, 500);
613 memset(&ppa, 0, sizeof(ppa));
614 ppa.progress_cb = progress_cb;
615 ppa.progress_arg = progress_arg;
616 ppa.sendfd = sendfd;
617 err = got_pack_create(&packhash, packfd, delta_cache,
618 their_ids, ntheirs, our_ids, nours, repo, 0, 1, 0,
619 pack_progress, &ppa, &rl, cancel_cb, cancel_arg);
620 if (err)
621 goto done;
623 npackfd = dup(packfd);
624 if (npackfd == -1) {
625 err = got_error_from_errno("dup");
626 goto done;
628 err = got_privsep_send_packfd(&sendibuf, npackfd);
629 if (err != NULL)
630 goto done;
631 npackfd = -1;
632 } else {
633 err = got_privsep_send_packfd(&sendibuf, -1);
634 if (err != NULL)
635 goto done;
638 while (!done) {
639 int success = 0;
640 char *refname = NULL;
641 char *errmsg = NULL;
643 if (cancel_cb) {
644 err = (*cancel_cb)(cancel_arg);
645 if (err)
646 goto done;
648 err = got_privsep_recv_send_progress(&done, &bytes_sent,
649 &success, &refname, &errmsg, &sendibuf);
650 if (err)
651 goto done;
652 if (refname && got_ref_name_is_valid(refname) && success &&
653 strncmp(refname, "refs/tags/", 10) != 0) {
654 struct got_pathlist_entry *my_ref;
656 * The server has accepted our changes.
657 * Update our reference in refs/remotes/ accordingly.
659 my_ref = find_ref(&have_refs, refname);
660 if (my_ref) {
661 err = update_remote_ref(my_ref, remote_name,
662 repo);
663 if (err)
664 goto done;
667 if (refname != NULL ||
668 bytes_sent_cur != bytes_sent) {
669 err = progress_cb(progress_arg, ppa.ncolored,
670 ppa.nfound, ppa.ntrees, ppa.packfile_size,
671 ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
672 ppa.nobj_written, bytes_sent,
673 refname, errmsg, success);
674 if (err) {
675 free(refname);
676 free(errmsg);
677 goto done;
679 bytes_sent_cur = bytes_sent;
681 free(refname);
682 free(errmsg);
684 done:
685 if (sendpid != -1) {
686 if (err)
687 got_privsep_send_stop(imsg_sendfds[0]);
688 if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
689 err = got_error_from_errno("waitpid");
691 if (imsg_sendfds[0] != -1 && close(imsg_sendfds[0]) == -1 && err == NULL)
692 err = got_error_from_errno("close");
693 if (imsg_sendfds[1] != -1 && close(imsg_sendfds[1]) == -1 && err == NULL)
694 err = got_error_from_errno("close");
695 if (packfd != -1 && close(packfd) == -1 && err == NULL)
696 err = got_error_from_errno("close");
697 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
698 err = got_error_from_errno("fclose");
699 if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
700 err = got_error_from_errno("close");
701 if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
702 err = got_error_from_errno("close");
704 got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
705 got_pathlist_free(&their_refs, GOT_PATHLIST_FREE_ALL);
707 * Object ids are owned by have_refs/their_refs and are already freed;
708 * Only the arrays must be freed.
710 free(our_ids);
711 free(their_ids);
712 free(s);
713 return err;