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 <sys/queue.h>
18 #include <sys/types.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"
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"
53 #include "repo_read.h"
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 static struct repo_read {
62 struct got_repository *repo;
66 struct gotd_imsgev session_iev;
70 static struct repo_read_client {
76 struct got_object_idset *want_ids;
77 struct got_object_idset *have_ids;
80 static volatile sig_atomic_t sigint_received;
81 static volatile sig_atomic_t sigterm_received;
84 catch_sigint(int signo)
90 catch_sigterm(int signo)
95 static const struct got_error *
96 check_cancelled(void *arg)
98 if (sigint_received || sigterm_received)
99 return got_error(GOT_ERR_CANCELLED);
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);
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->sha1, 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);
126 wbuf = imsg_create(ibuf, GOTD_IMSG_SYMREF, 0, 0, len);
128 err = got_error_from_errno("imsg_create SYMREF");
132 if (imsg_add(wbuf, &isymref, sizeof(isymref)) == -1) {
133 err = got_error_from_errno("imsg_add SYMREF");
136 if (imsg_add(wbuf, refname, isymref.name_len) == -1) {
137 err = got_error_from_errno("imsg_add SYMREF");
140 if (imsg_add(wbuf, target, isymref.target_len) == -1) {
141 err = got_error_from_errno("imsg_add SYMREF");
145 imsg_close(ibuf, wbuf);
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;
158 char *peeled_refname = NULL;
159 struct got_object_id *id;
162 err = got_object_tag_open(&tag, repo_read.repo, obj);
166 if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
167 err = got_error_from_errno("asprintf");
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);
180 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
183 err = got_error_from_errno("imsg_create MREF");
187 /* Keep in sync with struct gotd_imsg_ref definition. */
188 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
189 err = got_error_from_errno("imsg_add REF");
192 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
193 err = got_error_from_errno("imsg_add REF");
196 if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
197 err = got_error_from_errno("imsg_add REF");
201 imsg_close(ibuf, wbuf);
203 got_object_tag_close(tag);
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);
213 struct got_object_id *id = NULL;
214 struct got_object *obj = NULL;
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);
228 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
231 err = got_error_from_errno("imsg_create REF");
235 /* Keep in sync with struct gotd_imsg_ref definition. */
236 if (imsg_add(wbuf, id->sha1, 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);
248 if (obj->type == GOT_OBJ_TYPE_TAG)
249 err = send_peeled_tag_ref(ref, obj, ibuf);
252 got_object_close(obj);
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;
265 struct gotd_imsg_reflist irefs;
268 struct got_object_id *head_target_id = NULL;
272 client_fd = imsg_get_fd(imsg);
274 return got_error(GOT_ERR_PRIVSEP_NO_FD);
276 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
278 return got_error(GOT_ERR_PRIVSEP_LEN);
280 if (repo_read.refs_listed) {
281 return got_error_msg(GOT_ERR_CLIENT_ID,
282 "duplicate list-refs request");
284 repo_read.refs_listed = 1;
286 client->fd = client_fd;
288 imsg_init(&ibuf, client_fd);
290 err = got_ref_list(&refs, repo_read.repo, "",
291 got_ref_cmp_by_name, NULL);
295 memset(&irefs, 0, sizeof(irefs));
296 TAILQ_FOREACH(re, &refs, entry) {
297 struct got_object_id *id;
300 if (got_ref_is_symbolic(re->ref)) {
301 const char *refname = got_ref_get_name(re->ref);
302 if (strcmp(refname, GOT_REF_HEAD) != 0)
304 err = got_ref_resolve(&head_target_id, repo_read.repo,
307 if (err->code != GOT_ERR_NOT_REF)
310 * HEAD points to a non-existent branch.
311 * Do not advertise it.
312 * Matches git-daemon's behaviour.
314 head_target_id = NULL;
323 /* Account for a peeled tag refs. */
324 err = got_ref_resolve(&id, repo_read.repo, re->ref);
327 err = got_object_get_type(&obj_type, repo_read.repo, id);
331 if (obj_type == GOT_OBJ_TYPE_TAG)
335 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
336 repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
337 err = got_error_from_errno("imsg_compose REFLIST");
342 * Send the HEAD symref first. In Git-protocol versions < 2
343 * the HEAD symref must be announced on the initial line of
344 * the server's ref advertisement.
345 * For now, we do not advertise symrefs other than HEAD.
347 TAILQ_FOREACH(re, &refs, entry) {
348 if (!got_ref_is_symbolic(re->ref) ||
349 strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0 ||
350 head_target_id == NULL)
352 err = send_symref(re->ref, head_target_id, &ibuf);
357 TAILQ_FOREACH(re, &refs, entry) {
358 if (got_ref_is_symbolic(re->ref))
360 err = send_ref(re->ref, &ibuf);
365 err = gotd_imsg_flush(&ibuf);
367 got_ref_list_free(&refs);
372 static const struct got_error *
373 append_object_id(struct got_object_id *id, void *data, void *arg)
375 struct gotd_object_id_array *array = arg;
376 const size_t alloc_chunksz = 256;
378 if (array->ids == NULL) {
379 array->ids = reallocarray(NULL, alloc_chunksz,
380 sizeof(*array->ids));
381 if (array->ids == NULL)
382 return got_error_from_errno("reallocarray");
383 array->nalloc = alloc_chunksz;
385 } else if (array->nalloc <= array->nids) {
386 struct got_object_id **new;
387 new = recallocarray(array->ids, array->nalloc,
388 array->nalloc + alloc_chunksz, sizeof(*new));
390 return got_error_from_errno("recallocarray");
392 array->nalloc += alloc_chunksz;
395 array->ids[array->nids] = id;
400 static const struct got_error *
401 recv_want(struct imsg *imsg)
403 const struct got_error *err;
404 struct repo_read_client *client = &repo_read_client;
405 struct gotd_imsg_want iwant;
407 char hex[SHA1_DIGEST_STRING_LENGTH];
408 struct got_object_id id;
412 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
413 if (datalen != sizeof(iwant))
414 return got_error(GOT_ERR_PRIVSEP_LEN);
415 memcpy(&iwant, imsg->data, sizeof(iwant));
417 memset(&id, 0, sizeof(id));
418 memcpy(id.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
420 if (log_getverbose() > 0 &&
421 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
422 log_debug("client wants %s", hex);
424 imsg_init(&ibuf, client->fd);
426 err = got_object_get_type(&obj_type, repo_read.repo, &id);
430 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
431 obj_type != GOT_OBJ_TYPE_TAG)
432 return got_error(GOT_ERR_OBJ_TYPE);
434 if (!got_object_idset_contains(client->want_ids, &id)) {
435 err = got_object_idset_add(client->want_ids, &id, NULL);
440 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
445 static const struct got_error *
446 recv_have(struct imsg *imsg)
448 const struct got_error *err;
449 struct repo_read_client *client = &repo_read_client;
450 struct gotd_imsg_have ihave;
452 char hex[SHA1_DIGEST_STRING_LENGTH];
453 struct got_object_id id;
457 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
458 if (datalen != sizeof(ihave))
459 return got_error(GOT_ERR_PRIVSEP_LEN);
460 memcpy(&ihave, imsg->data, sizeof(ihave));
462 memset(&id, 0, sizeof(id));
463 memcpy(id.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
465 if (log_getverbose() > 0 &&
466 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
467 log_debug("client has %s", hex);
469 imsg_init(&ibuf, client->fd);
471 err = got_object_get_type(&obj_type, repo_read.repo, &id);
473 if (err->code == GOT_ERR_NO_OBJ) {
474 gotd_imsg_send_nak(&id, &ibuf,
475 PROC_REPO_READ, repo_read.pid);
481 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
482 obj_type != GOT_OBJ_TYPE_TAG) {
483 gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
484 err = got_error(GOT_ERR_OBJ_TYPE);
488 if (!got_object_idset_contains(client->have_ids, &id)) {
489 err = got_object_idset_add(client->have_ids, &id, NULL);
494 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
500 struct repo_read_pack_progress_arg {
502 struct imsgbuf *ibuf;
506 static const struct got_error *
507 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
508 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
511 struct repo_read_pack_progress_arg *a = arg;
512 struct gotd_imsg_packfile_progress iprog;
515 if (!a->report_progress)
517 if (packfile_size > 0 && a->sent_ready)
520 memset(&iprog, 0, sizeof(iprog));
521 iprog.ncolored = ncolored;
522 iprog.nfound = nfound;
523 iprog.ntrees = ntrees;
524 iprog.packfile_size = packfile_size;
525 iprog.ncommits = ncommits;
526 iprog.nobj_total = nobj_total;
527 iprog.nobj_deltify = nobj_deltify;
528 iprog.nobj_written = nobj_written;
530 /* Using synchronous writes since we are blocking the event loop. */
531 if (packfile_size == 0) {
532 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
533 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
535 return got_error_from_errno("imsg compose "
536 "PACKFILE_PROGRESS");
540 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
541 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
543 return got_error_from_errno("imsg compose "
548 return gotd_imsg_flush(a->ibuf);
551 static const struct got_error *
552 receive_delta_cache_fd(struct imsg *imsg,
553 struct gotd_imsgev *iev)
555 struct repo_read_client *client = &repo_read_client;
556 struct gotd_imsg_send_packfile ireq;
559 log_debug("receiving delta cache file");
561 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
562 if (datalen != sizeof(ireq))
563 return got_error(GOT_ERR_PRIVSEP_LEN);
564 memcpy(&ireq, imsg->data, sizeof(ireq));
566 if (client->delta_cache_fd != -1)
567 return got_error(GOT_ERR_PRIVSEP_MSG);
569 client->delta_cache_fd = imsg_get_fd(imsg);
570 if (client->delta_cache_fd == -1)
571 return got_error(GOT_ERR_PRIVSEP_NO_FD);
573 client->report_progress = ireq.report_progress;
577 static const struct got_error *
578 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
580 struct repo_read_client *client = &repo_read_client;
583 log_debug("receiving pack pipe descriptor");
585 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
587 return got_error(GOT_ERR_PRIVSEP_LEN);
589 if (client->pack_pipe != -1)
590 return got_error(GOT_ERR_PRIVSEP_MSG);
592 client->pack_pipe = imsg_get_fd(imsg);
593 if (client->pack_pipe == -1)
594 return got_error(GOT_ERR_PRIVSEP_NO_FD);
599 static const struct got_error *
600 send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
602 const struct got_error *err = NULL;
603 struct repo_read_client *client = &repo_read_client;
604 uint8_t packsha1[SHA1_DIGEST_LENGTH];
605 char hex[SHA1_DIGEST_STRING_LENGTH];
606 FILE *delta_cache = NULL;
608 struct repo_read_pack_progress_arg pa;
609 struct got_ratelimit rl;
610 struct gotd_object_id_array want_ids;
611 struct gotd_object_id_array have_ids;
613 log_debug("packfile request received");
615 memset(&want_ids, 0, sizeof(want_ids));
616 memset(&have_ids, 0, sizeof(have_ids));
618 got_ratelimit_init(&rl, 2, 0);
620 if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
621 return got_error(GOT_ERR_PRIVSEP_NO_FD);
623 imsg_init(&ibuf, client->fd);
625 delta_cache = fdopen(client->delta_cache_fd, "w+");
626 if (delta_cache == NULL) {
627 err = got_error_from_errno("fdopen");
630 client->delta_cache_fd = -1;
632 memset(&pa, 0, sizeof(pa));
634 pa.report_progress = client->report_progress;
636 err = got_object_idset_for_each(client->want_ids,
637 append_object_id, &want_ids);
640 err = got_object_idset_for_each(client->have_ids,
641 append_object_id, &have_ids);
645 err = got_pack_create(packsha1, client->pack_pipe, delta_cache,
646 have_ids.ids, have_ids.nids, want_ids.ids, want_ids.nids,
647 repo_read.repo, 0, 1, 0, pack_progress, &pa, &rl,
648 check_cancelled, NULL);
652 if (log_getverbose() > 0 &&
653 got_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
654 log_debug("sent pack-%s.pack", hex);
656 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
657 PROC_REPO_READ, -1, NULL, 0) == -1)
658 err = got_error_from_errno("imsg compose PACKFILE_DONE");
660 if (client->delta_cache_fd != -1 &&
661 close(client->delta_cache_fd) == -1 && err == NULL)
662 err = got_error_from_errno("close");
663 client->delta_cache_fd = -1;
664 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
665 err = got_error_from_errno("fclose");
673 repo_read_dispatch_session(int fd, short event, void *arg)
675 const struct got_error *err = NULL;
676 struct gotd_imsgev *iev = arg;
677 struct imsgbuf *ibuf = &iev->ibuf;
681 struct repo_read_client *client = &repo_read_client;
683 if (event & EV_READ) {
684 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
685 fatal("imsg_read error");
686 if (n == 0) /* Connection closed. */
690 if (event & EV_WRITE) {
691 n = msgbuf_write(&ibuf->w);
692 if (n == -1 && errno != EAGAIN)
693 fatal("msgbuf_write");
694 if (n == 0) /* Connection closed. */
698 while (err == NULL && check_cancelled(NULL) == NULL) {
699 if ((n = imsg_get(ibuf, &imsg)) == -1)
700 fatal("%s: imsg_get", __func__);
701 if (n == 0) /* No more messages. */
704 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
705 !repo_read.refs_listed) {
706 err = got_error(GOT_ERR_PRIVSEP_MSG);
710 switch (imsg.hdr.type) {
711 case GOTD_IMSG_LIST_REFS_INTERNAL:
712 err = list_refs(&imsg);
714 log_warnx("ls-refs: %s", err->msg);
717 err = recv_want(&imsg);
719 log_warnx("want-line: %s", err->msg);
722 err = recv_have(&imsg);
724 log_warnx("have-line: %s", err->msg);
726 case GOTD_IMSG_SEND_PACKFILE:
727 err = receive_delta_cache_fd(&imsg, iev);
729 log_warnx("receiving delta cache: %s",
732 case GOTD_IMSG_PACKFILE_PIPE:
733 err = receive_pack_pipe(&imsg, iev);
735 log_warnx("receiving pack pipe: %s", err->msg);
738 err = send_packfile(&imsg, iev);
740 log_warnx("sending packfile: %s", err->msg);
743 log_debug("unexpected imsg %d", imsg.hdr.type);
750 if (!shut && check_cancelled(NULL) == NULL) {
752 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
753 client->id, err) == -1) {
754 log_warnx("could not send error to parent: %s",
757 gotd_imsg_event_add(iev);
759 /* This pipe is dead. Remove its event handler */
761 event_loopexit(NULL);
765 static const struct got_error *
766 recv_connect(struct imsg *imsg)
768 struct gotd_imsgev *iev = &repo_read.session_iev;
771 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
773 return got_error(GOT_ERR_PRIVSEP_LEN);
775 if (repo_read.session_fd != -1)
776 return got_error(GOT_ERR_PRIVSEP_MSG);
778 repo_read.session_fd = imsg_get_fd(imsg);
779 if (repo_read.session_fd == -1)
780 return got_error(GOT_ERR_PRIVSEP_NO_FD);
782 imsg_init(&iev->ibuf, repo_read.session_fd);
783 iev->handler = repo_read_dispatch_session;
784 iev->events = EV_READ;
785 iev->handler_arg = NULL;
786 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
787 repo_read_dispatch_session, iev);
788 gotd_imsg_event_add(iev);
794 repo_read_dispatch(int fd, short event, void *arg)
796 const struct got_error *err = NULL;
797 struct gotd_imsgev *iev = arg;
798 struct imsgbuf *ibuf = &iev->ibuf;
802 struct repo_read_client *client = &repo_read_client;
804 if (event & EV_READ) {
805 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
806 fatal("imsg_read error");
807 if (n == 0) /* Connection closed. */
811 if (event & EV_WRITE) {
812 n = msgbuf_write(&ibuf->w);
813 if (n == -1 && errno != EAGAIN)
814 fatal("msgbuf_write");
815 if (n == 0) /* Connection closed. */
819 while (err == NULL && check_cancelled(NULL) == NULL) {
820 if ((n = imsg_get(ibuf, &imsg)) == -1)
821 fatal("%s: imsg_get", __func__);
822 if (n == 0) /* No more messages. */
825 switch (imsg.hdr.type) {
826 case GOTD_IMSG_CONNECT_REPO_CHILD:
827 err = recv_connect(&imsg);
830 log_debug("unexpected imsg %d", imsg.hdr.type);
837 if (!shut && check_cancelled(NULL) == NULL) {
839 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
840 client->id, err) == -1) {
841 log_warnx("could not send error to parent: %s",
844 gotd_imsg_event_add(iev);
846 /* This pipe is dead. Remove its event handler */
848 event_loopexit(NULL);
853 repo_read_main(const char *title, const char *repo_path,
854 int *pack_fds, int *temp_fds)
856 const struct got_error *err = NULL;
857 struct repo_read_client *client = &repo_read_client;
858 struct gotd_imsgev iev;
861 client->delta_cache_fd = -1;
862 client->pack_pipe = -1;
863 client->have_ids = got_object_idset_alloc();
864 if (client->have_ids == NULL) {
865 err = got_error_from_errno("got_object_idset_alloc");
868 client->want_ids = got_object_idset_alloc();
869 if (client->want_ids == NULL) {
870 err = got_error_from_errno("got_object_idset_alloc");
874 repo_read.title = title;
875 repo_read.pid = getpid();
876 repo_read.pack_fds = pack_fds;
877 repo_read.temp_fds = temp_fds;
878 repo_read.session_fd = -1;
879 repo_read.session_iev.ibuf.fd = -1;
881 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
884 if (!got_repo_is_bare(repo_read.repo)) {
885 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
886 "bare git repository required");
890 got_repo_temp_fds_set(repo_read.repo, temp_fds);
892 signal(SIGINT, catch_sigint);
893 signal(SIGTERM, catch_sigterm);
894 signal(SIGPIPE, SIG_IGN);
895 signal(SIGHUP, SIG_IGN);
897 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
898 iev.handler = repo_read_dispatch;
899 iev.events = EV_READ;
900 iev.handler_arg = NULL;
901 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
903 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
904 PROC_REPO_READ, -1, NULL, 0) == -1) {
905 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
912 log_warnx("%s: %s", title, err->msg);
913 repo_read_shutdown();
917 repo_read_shutdown(void)
919 struct repo_read_client *client = &repo_read_client;
921 log_debug("%s: shutting down", repo_read.title);
923 if (client->have_ids)
924 got_object_idset_free(client->have_ids);
925 if (client->want_ids)
926 got_object_idset_free(client->want_ids);
927 if (client->fd != -1)
929 if (client->delta_cache_fd != -1)
930 close(client->delta_cache_fd);
931 if (client->pack_pipe != -1)
932 close(client->pack_pipe);
935 got_repo_close(repo_read.repo);
936 got_repo_pack_fds_close(repo_read.pack_fds);
937 got_repo_temp_fds_close(repo_read.temp_fds);
938 if (repo_read.session_fd != -1)
939 close(repo_read.session_fd);