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->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);
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->hash, 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->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);
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.hash, iwant.object_id, SHA1_DIGEST_LENGTH);
420 if (log_getverbose() > 0 &&
421 got_object_id_hex(&id, 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.hash, ihave.object_id, SHA1_DIGEST_LENGTH);
465 if (log_getverbose() > 0 &&
466 got_object_id_hex(&id, 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 struct got_object_id packhash;
605 char hex[GOT_HASH_DIGEST_STRING_MAXLEN];
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(&packhash, 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_hash_digest_to_str(packhash.hash, hex, sizeof(hex),
655 log_debug("sent pack-%s.pack", hex);
657 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
658 PROC_REPO_READ, -1, NULL, 0) == -1)
659 err = got_error_from_errno("imsg compose PACKFILE_DONE");
661 if (client->delta_cache_fd != -1 &&
662 close(client->delta_cache_fd) == -1 && err == NULL)
663 err = got_error_from_errno("close");
664 client->delta_cache_fd = -1;
665 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
666 err = got_error_from_errno("fclose");
674 repo_read_dispatch_session(int fd, short event, void *arg)
676 const struct got_error *err = NULL;
677 struct gotd_imsgev *iev = arg;
678 struct imsgbuf *ibuf = &iev->ibuf;
682 struct repo_read_client *client = &repo_read_client;
684 if (event & EV_READ) {
685 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
686 fatal("imsg_read error");
687 if (n == 0) /* Connection closed. */
691 if (event & EV_WRITE) {
692 n = msgbuf_write(&ibuf->w);
693 if (n == -1 && errno != EAGAIN)
694 fatal("msgbuf_write");
695 if (n == 0) /* Connection closed. */
699 while (err == NULL && check_cancelled(NULL) == NULL) {
700 if ((n = imsg_get(ibuf, &imsg)) == -1)
701 fatal("%s: imsg_get", __func__);
702 if (n == 0) /* No more messages. */
705 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
706 !repo_read.refs_listed) {
707 err = got_error(GOT_ERR_PRIVSEP_MSG);
711 switch (imsg.hdr.type) {
712 case GOTD_IMSG_LIST_REFS_INTERNAL:
713 err = list_refs(&imsg);
715 log_warnx("ls-refs: %s", err->msg);
718 err = recv_want(&imsg);
720 log_warnx("want-line: %s", err->msg);
723 err = recv_have(&imsg);
725 log_warnx("have-line: %s", err->msg);
727 case GOTD_IMSG_SEND_PACKFILE:
728 err = receive_delta_cache_fd(&imsg, iev);
730 log_warnx("receiving delta cache: %s",
733 case GOTD_IMSG_PACKFILE_PIPE:
734 err = receive_pack_pipe(&imsg, iev);
736 log_warnx("receiving pack pipe: %s", err->msg);
739 err = send_packfile(&imsg, iev);
741 log_warnx("sending packfile: %s", err->msg);
744 log_debug("unexpected imsg %d", imsg.hdr.type);
751 if (!shut && check_cancelled(NULL) == NULL) {
753 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
754 client->id, err) == -1) {
755 log_warnx("could not send error to parent: %s",
758 gotd_imsg_event_add(iev);
760 /* This pipe is dead. Remove its event handler */
762 event_loopexit(NULL);
766 static const struct got_error *
767 recv_connect(struct imsg *imsg)
769 struct gotd_imsgev *iev = &repo_read.session_iev;
772 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
774 return got_error(GOT_ERR_PRIVSEP_LEN);
776 if (repo_read.session_fd != -1)
777 return got_error(GOT_ERR_PRIVSEP_MSG);
779 repo_read.session_fd = imsg_get_fd(imsg);
780 if (repo_read.session_fd == -1)
781 return got_error(GOT_ERR_PRIVSEP_NO_FD);
783 imsg_init(&iev->ibuf, repo_read.session_fd);
784 iev->handler = repo_read_dispatch_session;
785 iev->events = EV_READ;
786 iev->handler_arg = NULL;
787 event_set(&iev->ev, iev->ibuf.fd, EV_READ,
788 repo_read_dispatch_session, iev);
789 gotd_imsg_event_add(iev);
795 repo_read_dispatch(int fd, short event, void *arg)
797 const struct got_error *err = NULL;
798 struct gotd_imsgev *iev = arg;
799 struct imsgbuf *ibuf = &iev->ibuf;
803 struct repo_read_client *client = &repo_read_client;
805 if (event & EV_READ) {
806 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
807 fatal("imsg_read error");
808 if (n == 0) /* Connection closed. */
812 if (event & EV_WRITE) {
813 n = msgbuf_write(&ibuf->w);
814 if (n == -1 && errno != EAGAIN)
815 fatal("msgbuf_write");
816 if (n == 0) /* Connection closed. */
820 while (err == NULL && check_cancelled(NULL) == NULL) {
821 if ((n = imsg_get(ibuf, &imsg)) == -1)
822 fatal("%s: imsg_get", __func__);
823 if (n == 0) /* No more messages. */
826 switch (imsg.hdr.type) {
827 case GOTD_IMSG_CONNECT_REPO_CHILD:
828 err = recv_connect(&imsg);
831 log_debug("unexpected imsg %d", imsg.hdr.type);
838 if (!shut && check_cancelled(NULL) == NULL) {
840 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
841 client->id, err) == -1) {
842 log_warnx("could not send error to parent: %s",
845 gotd_imsg_event_add(iev);
847 /* This pipe is dead. Remove its event handler */
849 event_loopexit(NULL);
854 repo_read_main(const char *title, const char *repo_path,
855 int *pack_fds, int *temp_fds)
857 const struct got_error *err = NULL;
858 struct repo_read_client *client = &repo_read_client;
859 struct gotd_imsgev iev;
862 client->delta_cache_fd = -1;
863 client->pack_pipe = -1;
864 client->have_ids = got_object_idset_alloc();
865 if (client->have_ids == NULL) {
866 err = got_error_from_errno("got_object_idset_alloc");
869 client->want_ids = got_object_idset_alloc();
870 if (client->want_ids == NULL) {
871 err = got_error_from_errno("got_object_idset_alloc");
875 repo_read.title = title;
876 repo_read.pid = getpid();
877 repo_read.pack_fds = pack_fds;
878 repo_read.temp_fds = temp_fds;
879 repo_read.session_fd = -1;
880 repo_read.session_iev.ibuf.fd = -1;
882 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
885 if (!got_repo_is_bare(repo_read.repo)) {
886 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
887 "bare git repository required");
890 if (got_repo_get_object_format(repo_read.repo) != GOT_HASH_SHA1) {
891 err = got_error_msg(GOT_ERR_NOT_IMPL,
892 "sha256 object IDs unsupported in network protocol");
896 got_repo_temp_fds_set(repo_read.repo, temp_fds);
898 signal(SIGINT, catch_sigint);
899 signal(SIGTERM, catch_sigterm);
900 signal(SIGPIPE, SIG_IGN);
901 signal(SIGHUP, SIG_IGN);
903 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
904 iev.handler = repo_read_dispatch;
905 iev.events = EV_READ;
906 iev.handler_arg = NULL;
907 event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
909 if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
910 PROC_REPO_READ, -1, NULL, 0) == -1) {
911 err = got_error_from_errno("imsg compose REPO_CHILD_READY");
918 log_warnx("%s: %s", title, err->msg);
919 repo_read_shutdown();
923 repo_read_shutdown(void)
925 struct repo_read_client *client = &repo_read_client;
927 log_debug("%s: shutting down", repo_read.title);
929 if (client->have_ids)
930 got_object_idset_free(client->have_ids);
931 if (client->want_ids)
932 got_object_idset_free(client->want_ids);
933 if (client->fd != -1)
935 if (client->delta_cache_fd != -1)
936 close(client->delta_cache_fd);
937 if (client->pack_pipe != -1)
938 close(client->pack_pipe);
941 got_repo_close(repo_read.repo);
942 got_repo_pack_fds_close(repo_read.pack_fds);
943 got_repo_temp_fds_close(repo_read.temp_fds);
944 if (repo_read.session_fd != -1)
945 close(repo_read.session_fd);