make got-read-object clear its imsgbuf before exit in an error case
[got-portable.git] / gotd / repo_read.c
blob9032e4b7b4f67a85f287c2112f03fd001b7c4a30
1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "got_compat.h"
19 #include <sys/queue.h>
20 #include <sys/tree.h>
21 #include <sys/types.h>
23 #include <event.h>
24 #include <errno.h>
25 #include <imsg.h>
26 #include <signal.h>
27 #include <stdlib.h>
28 #include <limits.h>
29 #include <poll.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include "got_error.h"
35 #include "got_cancel.h"
36 #include "got_object.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_repository_admin.h"
40 #include "got_path.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_object.h"
44 #include "got_lib_object_idset.h"
45 #include "got_lib_hash.h"
46 #include "got_lib_pack.h"
47 #include "got_lib_ratelimit.h"
48 #include "got_lib_pack_create.h"
49 #include "got_lib_poll.h"
51 #include "log.h"
52 #include "gotd.h"
53 #include "repo_read.h"
55 #ifndef nitems
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 #endif
59 static struct repo_read {
60 pid_t pid;
61 const char *title;
62 struct got_repository *repo;
63 int *pack_fds;
64 int *temp_fds;
65 int session_fd;
66 struct gotd_imsgev session_iev;
67 int refs_listed;
68 } repo_read;
70 static struct repo_read_client {
71 uint32_t id;
72 int fd;
73 int delta_cache_fd;
74 int report_progress;
75 int pack_pipe;
76 struct got_object_idset *want_ids;
77 struct got_object_idset *have_ids;
78 } repo_read_client;
80 static volatile sig_atomic_t sigint_received;
81 static volatile sig_atomic_t sigterm_received;
83 static void
84 catch_sigint(int signo)
86 sigint_received = 1;
89 static void
90 catch_sigterm(int signo)
92 sigterm_received = 1;
95 static const struct got_error *
96 check_cancelled(void *arg)
98 if (sigint_received || sigterm_received)
99 return got_error(GOT_ERR_CANCELLED);
101 return NULL;
104 static const struct got_error *
105 send_symref(struct got_reference *symref, struct got_object_id *target_id,
106 struct imsgbuf *ibuf)
108 const struct got_error *err = NULL;
109 struct gotd_imsg_symref isymref;
110 const char *refname = got_ref_get_name(symref);
111 const char *target = got_ref_get_symref_target(symref);
112 size_t len;
113 struct ibuf *wbuf;
115 memset(&isymref, 0, sizeof(isymref));
116 isymref.name_len = strlen(refname);
117 isymref.target_len = strlen(target);
118 memcpy(isymref.target_id, target_id->hash, sizeof(isymref.target_id));
120 len = sizeof(isymref) + isymref.name_len + isymref.target_len;
121 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
122 err = got_error(GOT_ERR_NO_SPACE);
123 goto done;
126 wbuf = imsg_create(ibuf, GOTD_IMSG_SYMREF, 0, 0, len);
127 if (wbuf == NULL) {
128 err = got_error_from_errno("imsg_create SYMREF");
129 goto done;
132 if (imsg_add(wbuf, &isymref, sizeof(isymref)) == -1) {
133 err = got_error_from_errno("imsg_add SYMREF");
134 goto done;
136 if (imsg_add(wbuf, refname, isymref.name_len) == -1) {
137 err = got_error_from_errno("imsg_add SYMREF");
138 goto done;
140 if (imsg_add(wbuf, target, isymref.target_len) == -1) {
141 err = got_error_from_errno("imsg_add SYMREF");
142 goto done;
145 imsg_close(ibuf, wbuf);
146 done:
147 free(target_id);
148 return err;
151 static const struct got_error *
152 send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
153 struct imsgbuf *ibuf)
155 const struct got_error *err = NULL;
156 struct got_tag_object *tag;
157 size_t namelen, len;
158 char *peeled_refname = NULL;
159 struct got_object_id *id;
160 struct ibuf *wbuf;
162 err = got_object_tag_open(&tag, repo_read.repo, obj);
163 if (err)
164 return err;
166 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
167 err = got_error_from_errno("asprintf");
168 goto done;
171 id = got_object_tag_get_object_id(tag);
172 namelen = strlen(peeled_refname);
174 len = sizeof(struct gotd_imsg_ref) + namelen;
175 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
176 err = got_error(GOT_ERR_NO_SPACE);
177 goto done;
180 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
181 repo_read.pid, len);
182 if (wbuf == NULL) {
183 err = got_error_from_errno("imsg_create MREF");
184 goto done;
187 /* Keep in sync with struct gotd_imsg_ref definition. */
188 if (imsg_add(wbuf, id->hash, SHA1_DIGEST_LENGTH) == -1) {
189 err = got_error_from_errno("imsg_add REF");
190 goto done;
192 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
193 err = got_error_from_errno("imsg_add REF");
194 goto done;
196 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
197 err = got_error_from_errno("imsg_add REF");
198 goto done;
201 imsg_close(ibuf, wbuf);
202 done:
203 got_object_tag_close(tag);
204 return err;
207 static const struct got_error *
208 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
210 const struct got_error *err;
211 const char *refname = got_ref_get_name(ref);
212 size_t namelen;
213 struct got_object_id *id = NULL;
214 struct got_object *obj = NULL;
215 size_t len;
216 struct ibuf *wbuf;
218 namelen = strlen(refname);
220 len = sizeof(struct gotd_imsg_ref) + namelen;
221 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
222 return got_error(GOT_ERR_NO_SPACE);
224 err = got_ref_resolve(&id, repo_read.repo, ref);
225 if (err)
226 return err;
228 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
229 repo_read.pid, len);
230 if (wbuf == NULL) {
231 err = got_error_from_errno("imsg_create REF");
232 goto done;
235 /* Keep in sync with struct gotd_imsg_ref definition. */
236 if (imsg_add(wbuf, id->hash, SHA1_DIGEST_LENGTH) == -1)
237 return got_error_from_errno("imsg_add REF");
238 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
239 return got_error_from_errno("imsg_add REF");
240 if (imsg_add(wbuf, refname, namelen) == -1)
241 return got_error_from_errno("imsg_add REF");
243 imsg_close(ibuf, wbuf);
245 err = got_object_open(&obj, repo_read.repo, id);
246 if (err)
247 goto done;
248 if (obj->type == GOT_OBJ_TYPE_TAG)
249 err = send_peeled_tag_ref(ref, obj, ibuf);
250 done:
251 if (obj)
252 got_object_close(obj);
253 free(id);
254 return err;
257 static const struct got_error *
258 list_refs(struct imsg *imsg)
260 const struct got_error *err;
261 struct repo_read_client *client = &repo_read_client;
262 struct got_reflist_head refs;
263 struct got_reflist_entry *re;
264 size_t datalen;
265 struct gotd_imsg_reflist irefs;
266 struct imsgbuf ibuf;
267 struct got_object_id *head_target_id = NULL;
269 TAILQ_INIT(&refs);
271 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
272 if (datalen != 0)
273 return got_error(GOT_ERR_PRIVSEP_LEN);
275 if (repo_read.refs_listed) {
276 return got_error_msg(GOT_ERR_CLIENT_ID,
277 "duplicate list-refs request");
279 repo_read.refs_listed = 1;
281 client->fd = imsg_get_fd(imsg);
282 if (client->fd == -1)
283 return got_error(GOT_ERR_PRIVSEP_NO_FD);
285 if (imsgbuf_init(&ibuf, client->fd) == -1)
286 return got_error_from_errno("imsgbuf_init");
287 imsgbuf_allow_fdpass(&ibuf);
289 err = got_ref_list(&refs, repo_read.repo, "",
290 got_ref_cmp_by_name, NULL);
291 if (err)
292 return err;
294 memset(&irefs, 0, sizeof(irefs));
295 TAILQ_FOREACH(re, &refs, entry) {
296 struct got_object_id *id;
297 int obj_type;
299 if (got_ref_is_symbolic(re->ref)) {
300 const char *refname = got_ref_get_name(re->ref);
301 if (strcmp(refname, GOT_REF_HEAD) != 0)
302 continue;
303 err = got_ref_resolve(&head_target_id, repo_read.repo,
304 re->ref);
305 if (err) {
306 if (err->code != GOT_ERR_NOT_REF)
307 return err;
309 * HEAD points to a non-existent branch.
310 * Do not advertise it.
311 * Matches git-daemon's behaviour.
313 head_target_id = NULL;
314 err = NULL;
315 } else
316 irefs.nrefs++;
317 continue;
320 irefs.nrefs++;
322 /* Account for a peeled tag refs. */
323 err = got_ref_resolve(&id, repo_read.repo, re->ref);
324 if (err)
325 goto done;
326 err = got_object_get_type(&obj_type, repo_read.repo, id);
327 free(id);
328 if (err)
329 goto done;
330 if (obj_type == GOT_OBJ_TYPE_TAG)
331 irefs.nrefs++;
334 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
335 repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
336 err = got_error_from_errno("imsg_compose REFLIST");
337 goto done;
341 * Send the HEAD symref first. In Git-protocol versions < 2
342 * the HEAD symref must be announced on the initial line of
343 * the server's ref advertisement.
344 * For now, we do not advertise symrefs other than HEAD.
346 TAILQ_FOREACH(re, &refs, entry) {
347 if (!got_ref_is_symbolic(re->ref) ||
348 strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0 ||
349 head_target_id == NULL)
350 continue;
351 err = send_symref(re->ref, head_target_id, &ibuf);
352 if (err)
353 goto done;
354 break;
356 TAILQ_FOREACH(re, &refs, entry) {
357 if (got_ref_is_symbolic(re->ref))
358 continue;
359 err = send_ref(re->ref, &ibuf);
360 if (err)
361 goto done;
364 err = gotd_imsg_flush(&ibuf);
365 done:
366 got_ref_list_free(&refs);
367 imsgbuf_clear(&ibuf);
368 return err;
371 static const struct got_error *
372 append_object_id(struct got_object_id *id, void *data, void *arg)
374 struct gotd_object_id_array *array = arg;
375 const size_t alloc_chunksz = 256;
377 if (array->ids == NULL) {
378 array->ids = reallocarray(NULL, alloc_chunksz,
379 sizeof(*array->ids));
380 if (array->ids == NULL)
381 return got_error_from_errno("reallocarray");
382 array->nalloc = alloc_chunksz;
383 array->nids = 0;
384 } else if (array->nalloc <= array->nids) {
385 struct got_object_id **new;
386 new = recallocarray(array->ids, array->nalloc,
387 array->nalloc + alloc_chunksz, sizeof(*new));
388 if (new == NULL)
389 return got_error_from_errno("recallocarray");
390 array->ids = new;
391 array->nalloc += alloc_chunksz;
394 array->ids[array->nids] = id;
395 array->nids++;
396 return NULL;
399 static const struct got_error *
400 recv_want(struct imsg *imsg)
402 const struct got_error *err;
403 struct repo_read_client *client = &repo_read_client;
404 struct gotd_imsg_want iwant;
405 size_t datalen;
406 char hex[SHA1_DIGEST_STRING_LENGTH];
407 struct got_object_id id;
408 int obj_type;
409 struct imsgbuf ibuf;
411 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
412 if (datalen != sizeof(iwant))
413 return got_error(GOT_ERR_PRIVSEP_LEN);
414 memcpy(&iwant, imsg->data, sizeof(iwant));
416 memset(&id, 0, sizeof(id));
417 memcpy(id.hash, iwant.object_id, SHA1_DIGEST_LENGTH);
419 if (log_getverbose() > 0 &&
420 got_object_id_hex(&id, hex, sizeof(hex)))
421 log_debug("client wants %s", hex);
423 if (imsgbuf_init(&ibuf, client->fd) == -1)
424 return got_error_from_errno("imsgbuf_init");
425 imsgbuf_allow_fdpass(&ibuf);
427 err = got_object_get_type(&obj_type, repo_read.repo, &id);
428 if (err)
429 return err;
431 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
432 obj_type != GOT_OBJ_TYPE_TAG)
433 return got_error(GOT_ERR_OBJ_TYPE);
435 if (!got_object_idset_contains(client->want_ids, &id)) {
436 err = got_object_idset_add(client->want_ids, &id, NULL);
437 if (err)
438 return err;
441 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
442 imsgbuf_clear(&ibuf);
443 return err;
446 static const struct got_error *
447 recv_have(struct imsg *imsg)
449 const struct got_error *err;
450 struct repo_read_client *client = &repo_read_client;
451 struct gotd_imsg_have ihave;
452 size_t datalen;
453 char hex[SHA1_DIGEST_STRING_LENGTH];
454 struct got_object_id id;
455 int obj_type;
456 struct imsgbuf ibuf;
458 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
459 if (datalen != sizeof(ihave))
460 return got_error(GOT_ERR_PRIVSEP_LEN);
461 memcpy(&ihave, imsg->data, sizeof(ihave));
463 memset(&id, 0, sizeof(id));
464 memcpy(id.hash, ihave.object_id, SHA1_DIGEST_LENGTH);
466 if (log_getverbose() > 0 &&
467 got_object_id_hex(&id, hex, sizeof(hex)))
468 log_debug("client has %s", hex);
470 if (imsgbuf_init(&ibuf, client->fd) == -1)
471 return got_error_from_errno("imsgbuf_init");
472 imsgbuf_allow_fdpass(&ibuf);
474 err = got_object_get_type(&obj_type, repo_read.repo, &id);
475 if (err) {
476 if (err->code == GOT_ERR_NO_OBJ) {
477 gotd_imsg_send_nak(&id, &ibuf,
478 PROC_REPO_READ, repo_read.pid);
479 err = NULL;
481 goto done;
484 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
485 obj_type != GOT_OBJ_TYPE_TAG) {
486 gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
487 err = got_error(GOT_ERR_OBJ_TYPE);
488 goto done;
491 if (!got_object_idset_contains(client->have_ids, &id)) {
492 err = got_object_idset_add(client->have_ids, &id, NULL);
493 if (err)
494 goto done;
497 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
498 done:
499 imsgbuf_clear(&ibuf);
500 return err;
503 struct repo_read_pack_progress_arg {
504 int report_progress;
505 struct imsgbuf *ibuf;
506 int sent_ready;
509 static const struct got_error *
510 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
511 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
512 int nobj_written)
514 struct repo_read_pack_progress_arg *a = arg;
515 struct gotd_imsg_packfile_progress iprog;
516 int ret;
518 if (!a->report_progress)
519 return NULL;
520 if (packfile_size > 0 && a->sent_ready)
521 return NULL;
523 memset(&iprog, 0, sizeof(iprog));
524 iprog.ncolored = ncolored;
525 iprog.nfound = nfound;
526 iprog.ntrees = ntrees;
527 iprog.packfile_size = packfile_size;
528 iprog.ncommits = ncommits;
529 iprog.nobj_total = nobj_total;
530 iprog.nobj_deltify = nobj_deltify;
531 iprog.nobj_written = nobj_written;
533 /* Using synchronous writes since we are blocking the event loop. */
534 if (packfile_size == 0) {
535 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
536 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
537 if (ret == -1) {
538 return got_error_from_errno("imsg compose "
539 "PACKFILE_PROGRESS");
541 } else {
542 a->sent_ready = 1;
543 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
544 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
545 if (ret == -1) {
546 return got_error_from_errno("imsg compose "
547 "PACKFILE_READY");
551 return gotd_imsg_flush(a->ibuf);
554 static const struct got_error *
555 receive_delta_cache_fd(struct imsg *imsg,
556 struct gotd_imsgev *iev)
558 struct repo_read_client *client = &repo_read_client;
559 struct gotd_imsg_send_packfile ireq;
560 size_t datalen;
562 log_debug("receiving delta cache file");
564 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
565 if (datalen != sizeof(ireq))
566 return got_error(GOT_ERR_PRIVSEP_LEN);
567 memcpy(&ireq, imsg->data, sizeof(ireq));
569 if (client->delta_cache_fd != -1)
570 return got_error(GOT_ERR_PRIVSEP_MSG);
572 client->delta_cache_fd = imsg_get_fd(imsg);
573 if (client->delta_cache_fd == -1)
574 return got_error(GOT_ERR_PRIVSEP_NO_FD);
576 client->report_progress = ireq.report_progress;
577 return NULL;
580 static const struct got_error *
581 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
583 struct repo_read_client *client = &repo_read_client;
584 size_t datalen;
586 log_debug("receiving pack pipe descriptor");
588 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
589 if (datalen != 0)
590 return got_error(GOT_ERR_PRIVSEP_LEN);
592 if (client->pack_pipe != -1)
593 return got_error(GOT_ERR_PRIVSEP_MSG);
595 client->pack_pipe = imsg_get_fd(imsg);
596 if (client->pack_pipe == -1)
597 return got_error(GOT_ERR_PRIVSEP_NO_FD);
599 return NULL;
602 static const struct got_error *
603 send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
605 const struct got_error *err = NULL;
606 struct repo_read_client *client = &repo_read_client;
607 struct got_object_id packhash;
608 char hex[GOT_HASH_DIGEST_STRING_MAXLEN];
609 FILE *delta_cache = NULL;
610 struct imsgbuf ibuf;
611 struct repo_read_pack_progress_arg pa;
612 struct got_ratelimit rl;
613 struct gotd_object_id_array want_ids;
614 struct gotd_object_id_array have_ids;
616 log_debug("packfile request received");
618 memset(&want_ids, 0, sizeof(want_ids));
619 memset(&have_ids, 0, sizeof(have_ids));
621 got_ratelimit_init(&rl, 2, 0);
623 if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
624 return got_error(GOT_ERR_PRIVSEP_NO_FD);
626 if (imsgbuf_init(&ibuf, client->fd) == -1)
627 return got_error_from_errno("imsgbuf_init");
628 imsgbuf_allow_fdpass(&ibuf);
630 delta_cache = fdopen(client->delta_cache_fd, "w+");
631 if (delta_cache == NULL) {
632 err = got_error_from_errno("fdopen");
633 goto done;
635 client->delta_cache_fd = -1;
637 memset(&pa, 0, sizeof(pa));
638 pa.ibuf = &ibuf;
639 pa.report_progress = client->report_progress;
641 err = got_object_idset_for_each(client->want_ids,
642 append_object_id, &want_ids);
643 if (err)
644 goto done;
645 err = got_object_idset_for_each(client->have_ids,
646 append_object_id, &have_ids);
647 if (err)
648 goto done;
650 err = got_pack_create(&packhash, client->pack_pipe, delta_cache,
651 have_ids.ids, have_ids.nids, want_ids.ids, want_ids.nids,
652 repo_read.repo, 0, 1, 0, pack_progress, &pa, &rl,
653 check_cancelled, NULL);
654 if (err)
655 goto done;
657 if (log_getverbose() > 0 &&
658 got_hash_digest_to_str(packhash.hash, hex, sizeof(hex),
659 packhash.algo))
660 log_debug("sent pack-%s.pack", hex);
662 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
663 PROC_REPO_READ, -1, NULL, 0) == -1)
664 err = got_error_from_errno("imsg compose PACKFILE_DONE");
665 done:
666 if (client->delta_cache_fd != -1 &&
667 close(client->delta_cache_fd) == -1 && err == NULL)
668 err = got_error_from_errno("close");
669 client->delta_cache_fd = -1;
670 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
671 err = got_error_from_errno("fclose");
672 imsgbuf_clear(&ibuf);
673 free(want_ids.ids);
674 free(have_ids.ids);
675 return err;
678 static void
679 repo_read_dispatch_session(int fd, short event, void *arg)
681 const struct got_error *err = NULL;
682 struct gotd_imsgev *iev = arg;
683 struct imsgbuf *ibuf = &iev->ibuf;
684 struct imsg imsg;
685 ssize_t n;
686 int shut = 0;
687 struct repo_read_client *client = &repo_read_client;
689 if (event & EV_READ) {
690 if ((n = imsgbuf_read(ibuf)) == -1)
691 fatal("imsgbuf_read error");
692 if (n == 0) /* Connection closed. */
693 shut = 1;
696 if (event & EV_WRITE) {
697 err = gotd_imsg_flush(ibuf);
698 if (err)
699 fatalx("%s", err->msg);
702 while (err == NULL && check_cancelled(NULL) == NULL) {
703 if ((n = imsg_get(ibuf, &imsg)) == -1)
704 fatal("%s: imsg_get", __func__);
705 if (n == 0) /* No more messages. */
706 break;
708 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
709 !repo_read.refs_listed) {
710 err = got_error(GOT_ERR_PRIVSEP_MSG);
711 break;
714 switch (imsg.hdr.type) {
715 case GOTD_IMSG_LIST_REFS_INTERNAL:
716 err = list_refs(&imsg);
717 if (err)
718 log_warnx("ls-refs: %s", err->msg);
719 break;
720 case GOTD_IMSG_WANT:
721 err = recv_want(&imsg);
722 if (err)
723 log_warnx("want-line: %s", err->msg);
724 break;
725 case GOTD_IMSG_HAVE:
726 err = recv_have(&imsg);
727 if (err)
728 log_warnx("have-line: %s", err->msg);
729 break;
730 case GOTD_IMSG_SEND_PACKFILE:
731 err = receive_delta_cache_fd(&imsg, iev);
732 if (err)
733 log_warnx("receiving delta cache: %s",
734 err->msg);
735 break;
736 case GOTD_IMSG_PACKFILE_PIPE:
737 err = receive_pack_pipe(&imsg, iev);
738 if (err) {
739 log_warnx("receiving pack pipe: %s", err->msg);
740 break;
742 err = send_packfile(&imsg, iev);
743 if (err)
744 log_warnx("sending packfile: %s", err->msg);
745 break;
746 default:
747 log_debug("unexpected imsg %d", imsg.hdr.type);
748 break;
751 imsg_free(&imsg);
754 if (!shut && check_cancelled(NULL) == NULL) {
755 if (err &&
756 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
757 client->id, err) == -1) {
758 log_warnx("could not send error to parent: %s",
759 err->msg);
761 gotd_imsg_event_add(iev);
762 } else {
763 /* This pipe is dead. Remove its event handler */
764 event_del(&iev->ev);
765 event_loopexit(NULL);
769 static const struct got_error *
770 recv_connect(struct imsg *imsg)
772 struct gotd_imsgev *iev = &repo_read.session_iev;
773 size_t datalen;
775 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
776 if (datalen != 0)
777 return got_error(GOT_ERR_PRIVSEP_LEN);
779 if (repo_read.session_fd != -1)
780 return got_error(GOT_ERR_PRIVSEP_MSG);
782 repo_read.session_fd = imsg_get_fd(imsg);
783 if (repo_read.session_fd == -1)
784 return got_error(GOT_ERR_PRIVSEP_NO_FD);
786 if (imsgbuf_init(&iev->ibuf, repo_read.session_fd) == -1)
787 return got_error_from_errno("imsgbuf_init");
788 imsgbuf_allow_fdpass(&iev->ibuf);
789 iev->handler = repo_read_dispatch_session;
790 iev->events = EV_READ;
791 iev->handler_arg = NULL;
792 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
793 repo_read_dispatch_session, iev);
794 gotd_imsg_event_add(iev);
796 return NULL;
799 static void
800 repo_read_dispatch(int fd, short event, void *arg)
802 const struct got_error *err = NULL;
803 struct gotd_imsgev *iev = arg;
804 struct imsgbuf *ibuf = &iev->ibuf;
805 struct imsg imsg;
806 ssize_t n;
807 int shut = 0;
808 struct repo_read_client *client = &repo_read_client;
810 if (event & EV_READ) {
811 if ((n = imsgbuf_read(ibuf)) == -1)
812 fatal("imsgbuf_read error");
813 if (n == 0) /* Connection closed. */
814 shut = 1;
817 if (event & EV_WRITE) {
818 err = gotd_imsg_flush(ibuf);
819 if (err)
820 fatalx("%s", err->msg);
823 while (err == NULL && check_cancelled(NULL) == NULL) {
824 if ((n = imsg_get(ibuf, &imsg)) == -1)
825 fatal("%s: imsg_get", __func__);
826 if (n == 0) /* No more messages. */
827 break;
829 switch (imsg.hdr.type) {
830 case GOTD_IMSG_CONNECT_REPO_CHILD:
831 err = recv_connect(&imsg);
832 break;
833 default:
834 log_debug("unexpected imsg %d", imsg.hdr.type);
835 break;
838 imsg_free(&imsg);
841 if (!shut && check_cancelled(NULL) == NULL) {
842 if (err &&
843 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
844 client->id, err) == -1) {
845 log_warnx("could not send error to parent: %s",
846 err->msg);
848 gotd_imsg_event_add(iev);
849 } else {
850 /* This pipe is dead. Remove its event handler */
851 event_del(&iev->ev);
852 event_loopexit(NULL);
856 void
857 repo_read_main(const char *title, const char *repo_path,
858 int *pack_fds, int *temp_fds)
860 const struct got_error *err = NULL;
861 struct repo_read_client *client = &repo_read_client;
862 struct gotd_imsgev iev;
864 client->fd = -1;
865 client->delta_cache_fd = -1;
866 client->pack_pipe = -1;
867 client->have_ids = got_object_idset_alloc();
868 if (client->have_ids == NULL) {
869 err = got_error_from_errno("got_object_idset_alloc");
870 goto done;
872 client->want_ids = got_object_idset_alloc();
873 if (client->want_ids == NULL) {
874 err = got_error_from_errno("got_object_idset_alloc");
875 goto done;
878 repo_read.title = title;
879 repo_read.pid = getpid();
880 repo_read.pack_fds = pack_fds;
881 repo_read.temp_fds = temp_fds;
882 repo_read.session_fd = -1;
883 repo_read.session_iev.ibuf.fd = -1;
885 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
886 if (err)
887 goto done;
888 if (!got_repo_is_bare(repo_read.repo)) {
889 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
890 "bare git repository required");
891 goto done;
893 if (got_repo_get_object_format(repo_read.repo) != GOT_HASH_SHA1) {
894 err = got_error_msg(GOT_ERR_NOT_IMPL,
895 "sha256 object IDs unsupported in network protocol");
896 goto done;
899 got_repo_temp_fds_set(repo_read.repo, temp_fds);
901 signal(SIGINT, catch_sigint);
902 signal(SIGTERM, catch_sigterm);
903 signal(SIGPIPE, SIG_IGN);
904 signal(SIGHUP, SIG_IGN);
906 if (imsgbuf_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE) == -1) {
907 err = got_error_from_errno("imsgbuf_init");
908 goto done;
910 imsgbuf_allow_fdpass(&iev.ibuf);
911 iev.handler = repo_read_dispatch;
912 iev.events = EV_READ;
913 iev.handler_arg = NULL;
914 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
916 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
917 PROC_REPO_READ, -1, NULL, 0) == -1) {
918 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
919 goto done;
922 event_dispatch();
923 done:
924 if (err)
925 log_warnx("%s: %s", title, err->msg);
926 repo_read_shutdown();
929 void
930 repo_read_shutdown(void)
932 struct repo_read_client *client = &repo_read_client;
934 log_debug("%s: shutting down", repo_read.title);
936 if (client->have_ids)
937 got_object_idset_free(client->have_ids);
938 if (client->want_ids)
939 got_object_idset_free(client->want_ids);
940 if (client->fd != -1)
941 close(client->fd);
942 if (client->delta_cache_fd != -1)
943 close(client->delta_cache_fd);
944 if (client->pack_pipe != -1)
945 close(client->pack_pipe);
947 if (repo_read.repo)
948 got_repo_close(repo_read.repo);
949 got_repo_pack_fds_close(repo_read.pack_fds);
950 got_repo_temp_fds_close(repo_read.temp_fds);
951 if (repo_read.session_fd != -1)
952 close(repo_read.session_fd);
953 exit(0);