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;
69 static struct repo_read_client {
75 struct got_object_idset *want_ids;
76 struct got_object_idset *have_ids;
79 static volatile sig_atomic_t sigint_received;
80 static volatile sig_atomic_t sigterm_received;
83 catch_sigint(int signo)
89 catch_sigterm(int signo)
94 static const struct got_error *
95 check_cancelled(void *arg)
97 if (sigint_received || sigterm_received)
98 return got_error(GOT_ERR_CANCELLED);
103 static const struct got_error *
104 send_symref(struct got_reference *symref, struct got_object_id *target_id,
105 struct imsgbuf *ibuf)
107 const struct got_error *err = NULL;
108 struct gotd_imsg_symref isymref;
109 const char *refname = got_ref_get_name(symref);
110 const char *target = got_ref_get_symref_target(symref);
114 memset(&isymref, 0, sizeof(isymref));
115 isymref.name_len = strlen(refname);
116 isymref.target_len = strlen(target);
117 memcpy(isymref.target_id, target_id->sha1, sizeof(isymref.target_id));
119 len = sizeof(isymref) + isymref.name_len + isymref.target_len;
120 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
121 err = got_error(GOT_ERR_NO_SPACE);
125 wbuf = imsg_create(ibuf, GOTD_IMSG_SYMREF, 0, 0, len);
127 err = got_error_from_errno("imsg_create SYMREF");
131 if (imsg_add(wbuf, &isymref, sizeof(isymref)) == -1) {
132 err = got_error_from_errno("imsg_add SYMREF");
135 if (imsg_add(wbuf, refname, isymref.name_len) == -1) {
136 err = got_error_from_errno("imsg_add SYMREF");
139 if (imsg_add(wbuf, target, isymref.target_len) == -1) {
140 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");
202 imsg_close(ibuf, wbuf);
204 got_object_tag_close(tag);
208 static const struct got_error *
209 send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
211 const struct got_error *err;
212 const char *refname = got_ref_get_name(ref);
214 struct got_object_id *id = NULL;
215 struct got_object *obj = NULL;
219 namelen = strlen(refname);
221 len = sizeof(struct gotd_imsg_ref) + namelen;
222 if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
223 return got_error(GOT_ERR_NO_SPACE);
225 err = got_ref_resolve(&id, repo_read.repo, ref);
229 wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
232 err = got_error_from_errno("imsg_create REF");
236 /* Keep in sync with struct gotd_imsg_ref definition. */
237 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
238 return got_error_from_errno("imsg_add REF");
239 if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
240 return got_error_from_errno("imsg_add REF");
241 if (imsg_add(wbuf, refname, namelen) == -1)
242 return got_error_from_errno("imsg_add REF");
245 imsg_close(ibuf, wbuf);
247 err = got_object_open(&obj, repo_read.repo, id);
250 if (obj->type == GOT_OBJ_TYPE_TAG)
251 err = send_peeled_tag_ref(ref, obj, ibuf);
254 got_object_close(obj);
259 static const struct got_error *
260 list_refs(struct imsg *imsg)
262 const struct got_error *err;
263 struct repo_read_client *client = &repo_read_client;
264 struct got_reflist_head refs;
265 struct got_reflist_entry *re;
266 struct gotd_imsg_list_refs_internal ireq;
268 struct gotd_imsg_reflist irefs;
271 struct got_object_id *head_target_id = NULL;
275 client_fd = imsg_get_fd(imsg);
277 return got_error(GOT_ERR_PRIVSEP_NO_FD);
279 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
280 if (datalen != sizeof(ireq))
281 return got_error(GOT_ERR_PRIVSEP_LEN);
282 memcpy(&ireq, imsg->data, sizeof(ireq));
284 if (ireq.client_id == 0)
285 return got_error(GOT_ERR_CLIENT_ID);
286 if (client->id != 0) {
287 return got_error_msg(GOT_ERR_CLIENT_ID,
288 "duplicate list-refs request");
290 client->id = ireq.client_id;
291 client->fd = client_fd;
293 imsg_init(&ibuf, client_fd);
295 err = got_ref_list(&refs, repo_read.repo, "",
296 got_ref_cmp_by_name, NULL);
300 memset(&irefs, 0, sizeof(irefs));
301 TAILQ_FOREACH(re, &refs, entry) {
302 struct got_object_id *id;
305 if (got_ref_is_symbolic(re->ref)) {
306 const char *refname = got_ref_get_name(re->ref);
307 if (strcmp(refname, GOT_REF_HEAD) != 0)
309 err = got_ref_resolve(&head_target_id, repo_read.repo,
312 if (err->code != GOT_ERR_NOT_REF)
315 * HEAD points to a non-existent branch.
316 * Do not advertise it.
317 * Matches git-daemon's behaviour.
319 head_target_id = NULL;
328 /* Account for a peeled tag refs. */
329 err = got_ref_resolve(&id, repo_read.repo, re->ref);
332 err = got_object_get_type(&obj_type, repo_read.repo, id);
336 if (obj_type == GOT_OBJ_TYPE_TAG)
340 if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
341 repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
342 err = got_error_from_errno("imsg_compose REFLIST");
347 * Send the HEAD symref first. In Git-protocol versions < 2
348 * the HEAD symref must be announced on the initial line of
349 * the server's ref advertisement.
350 * For now, we do not advertise symrefs other than HEAD.
352 TAILQ_FOREACH(re, &refs, entry) {
353 if (!got_ref_is_symbolic(re->ref) ||
354 strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0 ||
355 head_target_id == NULL)
357 err = send_symref(re->ref, head_target_id, &ibuf);
362 TAILQ_FOREACH(re, &refs, entry) {
363 if (got_ref_is_symbolic(re->ref))
365 err = send_ref(re->ref, &ibuf);
370 err = gotd_imsg_flush(&ibuf);
372 got_ref_list_free(&refs);
377 static const struct got_error *
378 append_object_id(struct got_object_id *id, void *data, void *arg)
380 struct gotd_object_id_array *array = arg;
381 const size_t alloc_chunksz = 256;
383 if (array->ids == NULL) {
384 array->ids = reallocarray(NULL, alloc_chunksz,
385 sizeof(*array->ids));
386 if (array->ids == NULL)
387 return got_error_from_errno("reallocarray");
388 array->nalloc = alloc_chunksz;
390 } else if (array->nalloc <= array->nids) {
391 struct got_object_id **new;
392 new = recallocarray(array->ids, array->nalloc,
393 array->nalloc + alloc_chunksz, sizeof(*new));
395 return got_error_from_errno("recallocarray");
397 array->nalloc += alloc_chunksz;
400 array->ids[array->nids] = id;
405 static const struct got_error *
406 recv_want(struct imsg *imsg)
408 const struct got_error *err;
409 struct repo_read_client *client = &repo_read_client;
410 struct gotd_imsg_want iwant;
412 char hex[SHA1_DIGEST_STRING_LENGTH];
413 struct got_object_id id;
417 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
418 if (datalen != sizeof(iwant))
419 return got_error(GOT_ERR_PRIVSEP_LEN);
420 memcpy(&iwant, imsg->data, sizeof(iwant));
422 memset(&id, 0, sizeof(id));
423 memcpy(id.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
425 if (log_getverbose() > 0 &&
426 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
427 log_debug("client wants %s", hex);
429 imsg_init(&ibuf, client->fd);
431 err = got_object_get_type(&obj_type, repo_read.repo, &id);
435 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
436 obj_type != GOT_OBJ_TYPE_TAG)
437 return got_error(GOT_ERR_OBJ_TYPE);
439 if (!got_object_idset_contains(client->want_ids, &id)) {
440 err = got_object_idset_add(client->want_ids, &id, NULL);
445 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
450 static const struct got_error *
451 recv_have(struct imsg *imsg)
453 const struct got_error *err;
454 struct repo_read_client *client = &repo_read_client;
455 struct gotd_imsg_have ihave;
457 char hex[SHA1_DIGEST_STRING_LENGTH];
458 struct got_object_id id;
462 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
463 if (datalen != sizeof(ihave))
464 return got_error(GOT_ERR_PRIVSEP_LEN);
465 memcpy(&ihave, imsg->data, sizeof(ihave));
467 memset(&id, 0, sizeof(id));
468 memcpy(id.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
470 if (log_getverbose() > 0 &&
471 got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
472 log_debug("client has %s", hex);
474 imsg_init(&ibuf, client->fd);
476 err = got_object_get_type(&obj_type, repo_read.repo, &id);
478 if (err->code == GOT_ERR_NO_OBJ) {
479 gotd_imsg_send_nak(&id, &ibuf,
480 PROC_REPO_READ, repo_read.pid);
486 if (obj_type != GOT_OBJ_TYPE_COMMIT &&
487 obj_type != GOT_OBJ_TYPE_TAG) {
488 gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
489 err = got_error(GOT_ERR_OBJ_TYPE);
493 if (!got_object_idset_contains(client->have_ids, &id)) {
494 err = got_object_idset_add(client->have_ids, &id, NULL);
499 gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
505 struct repo_read_pack_progress_arg {
507 struct imsgbuf *ibuf;
511 static const struct got_error *
512 pack_progress(void *arg, int ncolored, int nfound, int ntrees,
513 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
516 struct repo_read_pack_progress_arg *a = arg;
517 struct gotd_imsg_packfile_progress iprog;
520 if (!a->report_progress)
522 if (packfile_size > 0 && a->sent_ready)
525 memset(&iprog, 0, sizeof(iprog));
526 iprog.ncolored = ncolored;
527 iprog.nfound = nfound;
528 iprog.ntrees = ntrees;
529 iprog.packfile_size = packfile_size;
530 iprog.ncommits = ncommits;
531 iprog.nobj_total = nobj_total;
532 iprog.nobj_deltify = nobj_deltify;
533 iprog.nobj_written = nobj_written;
535 /* Using synchronous writes since we are blocking the event loop. */
536 if (packfile_size == 0) {
537 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
538 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
540 return got_error_from_errno("imsg compose "
541 "PACKFILE_PROGRESS");
545 ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
546 PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
548 return got_error_from_errno("imsg compose "
553 return gotd_imsg_flush(a->ibuf);
556 static const struct got_error *
557 receive_delta_cache_fd(struct imsg *imsg,
558 struct gotd_imsgev *iev)
560 struct repo_read_client *client = &repo_read_client;
561 struct gotd_imsg_send_packfile ireq;
564 log_debug("receiving delta cache file");
566 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
567 if (datalen != sizeof(ireq))
568 return got_error(GOT_ERR_PRIVSEP_LEN);
569 memcpy(&ireq, imsg->data, sizeof(ireq));
571 if (client->delta_cache_fd != -1)
572 return got_error(GOT_ERR_PRIVSEP_MSG);
574 client->delta_cache_fd = imsg_get_fd(imsg);
575 if (client->delta_cache_fd == -1)
576 return got_error(GOT_ERR_PRIVSEP_NO_FD);
578 client->report_progress = ireq.report_progress;
582 static const struct got_error *
583 receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
585 struct repo_read_client *client = &repo_read_client;
586 struct gotd_imsg_packfile_pipe ireq;
589 log_debug("receiving pack pipe descriptor");
591 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
592 if (datalen != sizeof(ireq))
593 return got_error(GOT_ERR_PRIVSEP_LEN);
594 memcpy(&ireq, imsg->data, sizeof(ireq));
596 if (client->pack_pipe != -1)
597 return got_error(GOT_ERR_PRIVSEP_MSG);
599 client->pack_pipe = imsg_get_fd(imsg);
600 if (client->pack_pipe == -1)
601 return got_error(GOT_ERR_PRIVSEP_NO_FD);
606 static const struct got_error *
607 send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
609 const struct got_error *err = NULL;
610 struct repo_read_client *client = &repo_read_client;
611 struct gotd_imsg_packfile_done idone;
612 uint8_t packsha1[SHA1_DIGEST_LENGTH];
613 char hex[SHA1_DIGEST_STRING_LENGTH];
614 FILE *delta_cache = NULL;
616 struct repo_read_pack_progress_arg pa;
617 struct got_ratelimit rl;
618 struct gotd_object_id_array want_ids;
619 struct gotd_object_id_array have_ids;
621 log_debug("packfile request received");
623 memset(&want_ids, 0, sizeof(want_ids));
624 memset(&have_ids, 0, sizeof(have_ids));
626 got_ratelimit_init(&rl, 2, 0);
628 if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
629 return got_error(GOT_ERR_PRIVSEP_NO_FD);
631 imsg_init(&ibuf, client->fd);
633 delta_cache = fdopen(client->delta_cache_fd, "w+");
634 if (delta_cache == NULL) {
635 err = got_error_from_errno("fdopen");
638 client->delta_cache_fd = -1;
640 memset(&pa, 0, sizeof(pa));
642 pa.report_progress = client->report_progress;
644 err = got_object_idset_for_each(client->want_ids,
645 append_object_id, &want_ids);
648 err = got_object_idset_for_each(client->have_ids,
649 append_object_id, &have_ids);
653 err = got_pack_create(packsha1, client->pack_pipe, delta_cache,
654 have_ids.ids, have_ids.nids, want_ids.ids, want_ids.nids,
655 repo_read.repo, 0, 1, 0, pack_progress, &pa, &rl,
656 check_cancelled, NULL);
660 if (log_getverbose() > 0 &&
661 got_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
662 log_debug("sent pack-%s.pack", hex);
664 memset(&idone, 0, sizeof(idone));
665 idone.client_id = client->id;
666 if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
667 PROC_REPO_READ, -1, &idone, sizeof(idone)) == -1)
668 err = got_error_from_errno("imsg compose PACKFILE_DONE");
670 if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
671 err = got_error_from_errno("fclose");
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;
687 struct repo_read_client *client = &repo_read_client;
689 if (event & EV_READ) {
690 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
691 fatal("imsg_read error");
692 if (n == 0) /* Connection closed. */
696 if (event & EV_WRITE) {
697 n = msgbuf_write(&ibuf->w);
698 if (n == -1 && errno != EAGAIN)
699 fatal("msgbuf_write");
700 if (n == 0) /* Connection closed. */
704 while (err == NULL && check_cancelled(NULL) == NULL) {
705 if ((n = imsg_get(ibuf, &imsg)) == -1)
706 fatal("%s: imsg_get", __func__);
707 if (n == 0) /* No more messages. */
710 if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
712 err = got_error(GOT_ERR_PRIVSEP_MSG);
716 switch (imsg.hdr.type) {
717 case GOTD_IMSG_LIST_REFS_INTERNAL:
718 err = list_refs(&imsg);
720 log_warnx("ls-refs: %s", err->msg);
723 err = recv_want(&imsg);
725 log_warnx("want-line: %s", err->msg);
728 err = recv_have(&imsg);
730 log_warnx("have-line: %s", err->msg);
732 case GOTD_IMSG_SEND_PACKFILE:
733 err = receive_delta_cache_fd(&imsg, iev);
735 log_warnx("receiving delta cache: %s",
738 case GOTD_IMSG_PACKFILE_PIPE:
739 err = receive_pack_pipe(&imsg, iev);
741 log_warnx("receiving pack pipe: %s", err->msg);
744 err = send_packfile(&imsg, iev);
746 log_warnx("sending packfile: %s", err->msg);
749 log_debug("unexpected imsg %d", imsg.hdr.type);
756 if (!shut && check_cancelled(NULL) == NULL) {
758 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
759 client->id, err) == -1) {
760 log_warnx("could not send error to parent: %s",
763 gotd_imsg_event_add(iev);
765 /* This pipe is dead. Remove its event handler */
767 event_loopexit(NULL);
771 static const struct got_error *
772 recv_connect(struct imsg *imsg)
774 struct gotd_imsgev *iev = &repo_read.session_iev;
777 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
779 return got_error(GOT_ERR_PRIVSEP_LEN);
781 if (repo_read.session_fd != -1)
782 return got_error(GOT_ERR_PRIVSEP_MSG);
784 repo_read.session_fd = imsg_get_fd(imsg);
785 if (repo_read.session_fd == -1)
786 return got_error(GOT_ERR_PRIVSEP_NO_FD);
788 imsg_init(&iev->ibuf, repo_read.session_fd);
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);
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;
808 struct repo_read_client *client = &repo_read_client;
810 if (event & EV_READ) {
811 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
812 fatal("imsg_read error");
813 if (n == 0) /* Connection closed. */
817 if (event & EV_WRITE) {
818 n = msgbuf_write(&ibuf->w);
819 if (n == -1 && errno != EAGAIN)
820 fatal("msgbuf_write");
821 if (n == 0) /* Connection closed. */
825 while (err == NULL && check_cancelled(NULL) == NULL) {
826 if ((n = imsg_get(ibuf, &imsg)) == -1)
827 fatal("%s: imsg_get", __func__);
828 if (n == 0) /* No more messages. */
831 switch (imsg.hdr.type) {
832 case GOTD_IMSG_CONNECT_REPO_CHILD:
833 err = recv_connect(&imsg);
836 log_debug("unexpected imsg %d", imsg.hdr.type);
843 if (!shut && check_cancelled(NULL) == NULL) {
845 gotd_imsg_send_error_event(iev, PROC_REPO_READ,
846 client->id, err) == -1) {
847 log_warnx("could not send error to parent: %s",
850 gotd_imsg_event_add(iev);
852 /* This pipe is dead. Remove its event handler */
854 event_loopexit(NULL);
859 repo_read_main(const char *title, const char *repo_path,
860 int *pack_fds, int *temp_fds)
862 const struct got_error *err = NULL;
863 struct repo_read_client *client = &repo_read_client;
864 struct gotd_imsgev iev;
867 client->delta_cache_fd = -1;
868 client->pack_pipe = -1;
869 client->have_ids = got_object_idset_alloc();
870 if (client->have_ids == NULL) {
871 err = got_error_from_errno("got_object_idset_alloc");
874 client->want_ids = got_object_idset_alloc();
875 if (client->want_ids == NULL) {
876 err = got_error_from_errno("got_object_idset_alloc");
880 repo_read.title = title;
881 repo_read.pid = getpid();
882 repo_read.pack_fds = pack_fds;
883 repo_read.temp_fds = temp_fds;
884 repo_read.session_fd = -1;
885 repo_read.session_iev.ibuf.fd = -1;
887 err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
890 if (!got_repo_is_bare(repo_read.repo)) {
891 err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
892 "bare git repository required");
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("shutting down");
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);