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/types.h>
33 #include "got_error.h"
34 #include "got_cancel.h"
35 #include "got_object.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_repository_admin.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_object.h"
43 #include "got_lib_object_idset.h"
44 #include "got_lib_hash.h"
45 #include "got_lib_pack.h"
46 #include "got_lib_ratelimit.h"
47 #include "got_lib_pack_create.h"
48 #include "got_lib_poll.h"
52 #include "repo_read.h"
55 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 static struct repo_read
{
61 struct got_repository
*repo
;
65 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");
144 imsg_close(ibuf
, wbuf
);
150 static const struct got_error
*
151 send_peeled_tag_ref(struct got_reference
*ref
, struct got_object
*obj
,
152 struct imsgbuf
*ibuf
)
154 const struct got_error
*err
= NULL
;
155 struct got_tag_object
*tag
;
157 char *peeled_refname
= NULL
;
158 struct got_object_id
*id
;
161 err
= got_object_tag_open(&tag
, repo_read
.repo
, obj
);
165 if (asprintf(&peeled_refname
, "%s^{}", got_ref_get_name(ref
)) == -1) {
166 err
= got_error_from_errno("asprintf");
170 id
= got_object_tag_get_object_id(tag
);
171 namelen
= strlen(peeled_refname
);
173 len
= sizeof(struct gotd_imsg_ref
) + namelen
;
174 if (len
> MAX_IMSGSIZE
- IMSG_HEADER_SIZE
) {
175 err
= got_error(GOT_ERR_NO_SPACE
);
179 wbuf
= imsg_create(ibuf
, GOTD_IMSG_REF
, PROC_REPO_READ
,
182 err
= got_error_from_errno("imsg_create MREF");
186 /* Keep in sync with struct gotd_imsg_ref definition. */
187 if (imsg_add(wbuf
, id
->sha1
, SHA1_DIGEST_LENGTH
) == -1) {
188 err
= got_error_from_errno("imsg_add REF");
191 if (imsg_add(wbuf
, &namelen
, sizeof(namelen
)) == -1) {
192 err
= got_error_from_errno("imsg_add REF");
195 if (imsg_add(wbuf
, peeled_refname
, namelen
) == -1) {
196 err
= got_error_from_errno("imsg_add REF");
200 imsg_close(ibuf
, wbuf
);
202 got_object_tag_close(tag
);
206 static const struct got_error
*
207 send_ref(struct got_reference
*ref
, struct imsgbuf
*ibuf
)
209 const struct got_error
*err
;
210 const char *refname
= got_ref_get_name(ref
);
212 struct got_object_id
*id
= NULL
;
213 struct got_object
*obj
= NULL
;
217 namelen
= strlen(refname
);
219 len
= sizeof(struct gotd_imsg_ref
) + namelen
;
220 if (len
> MAX_IMSGSIZE
- IMSG_HEADER_SIZE
)
221 return got_error(GOT_ERR_NO_SPACE
);
223 err
= got_ref_resolve(&id
, repo_read
.repo
, ref
);
227 wbuf
= imsg_create(ibuf
, GOTD_IMSG_REF
, PROC_REPO_READ
,
230 err
= got_error_from_errno("imsg_create REF");
234 /* Keep in sync with struct gotd_imsg_ref definition. */
235 if (imsg_add(wbuf
, id
->sha1
, SHA1_DIGEST_LENGTH
) == -1)
236 return got_error_from_errno("imsg_add REF");
237 if (imsg_add(wbuf
, &namelen
, sizeof(namelen
)) == -1)
238 return got_error_from_errno("imsg_add REF");
239 if (imsg_add(wbuf
, refname
, namelen
) == -1)
240 return got_error_from_errno("imsg_add REF");
242 imsg_close(ibuf
, wbuf
);
244 err
= got_object_open(&obj
, repo_read
.repo
, id
);
247 if (obj
->type
== GOT_OBJ_TYPE_TAG
)
248 err
= send_peeled_tag_ref(ref
, obj
, ibuf
);
251 got_object_close(obj
);
256 static const struct got_error
*
257 list_refs(struct imsg
*imsg
)
259 const struct got_error
*err
;
260 struct repo_read_client
*client
= &repo_read_client
;
261 struct got_reflist_head refs
;
262 struct got_reflist_entry
*re
;
264 struct gotd_imsg_reflist irefs
;
267 struct got_object_id
*head_target_id
= NULL
;
271 client_fd
= imsg_get_fd(imsg
);
273 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
275 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
277 return got_error(GOT_ERR_PRIVSEP_LEN
);
279 if (repo_read
.refs_listed
) {
280 return got_error_msg(GOT_ERR_CLIENT_ID
,
281 "duplicate list-refs request");
283 repo_read
.refs_listed
= 1;
285 client
->fd
= client_fd
;
287 imsg_init(&ibuf
, client_fd
);
289 err
= got_ref_list(&refs
, repo_read
.repo
, "",
290 got_ref_cmp_by_name
, NULL
);
294 memset(&irefs
, 0, sizeof(irefs
));
295 TAILQ_FOREACH(re
, &refs
, entry
) {
296 struct got_object_id
*id
;
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)
303 err
= got_ref_resolve(&head_target_id
, repo_read
.repo
,
306 if (err
->code
!= GOT_ERR_NOT_REF
)
309 * HEAD points to a non-existent branch.
310 * Do not advertise it.
311 * Matches git-daemon's behaviour.
313 head_target_id
= NULL
;
322 /* Account for a peeled tag refs. */
323 err
= got_ref_resolve(&id
, repo_read
.repo
, re
->ref
);
326 err
= got_object_get_type(&obj_type
, repo_read
.repo
, id
);
330 if (obj_type
== GOT_OBJ_TYPE_TAG
)
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");
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
)
351 err
= send_symref(re
->ref
, head_target_id
, &ibuf
);
356 TAILQ_FOREACH(re
, &refs
, entry
) {
357 if (got_ref_is_symbolic(re
->ref
))
359 err
= send_ref(re
->ref
, &ibuf
);
364 err
= gotd_imsg_flush(&ibuf
);
366 got_ref_list_free(&refs
);
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
;
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));
389 return got_error_from_errno("recallocarray");
391 array
->nalloc
+= alloc_chunksz
;
394 array
->ids
[array
->nids
] = id
;
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
;
406 char hex
[SHA1_DIGEST_STRING_LENGTH
];
407 struct got_object_id id
;
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
.sha1
, 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 imsg_init(&ibuf
, client
->fd
);
425 err
= got_object_get_type(&obj_type
, repo_read
.repo
, &id
);
429 if (obj_type
!= GOT_OBJ_TYPE_COMMIT
&&
430 obj_type
!= GOT_OBJ_TYPE_TAG
)
431 return got_error(GOT_ERR_OBJ_TYPE
);
433 if (!got_object_idset_contains(client
->want_ids
, &id
)) {
434 err
= got_object_idset_add(client
->want_ids
, &id
, NULL
);
439 gotd_imsg_send_ack(&id
, &ibuf
, PROC_REPO_READ
, repo_read
.pid
);
444 static const struct got_error
*
445 recv_have(struct imsg
*imsg
)
447 const struct got_error
*err
;
448 struct repo_read_client
*client
= &repo_read_client
;
449 struct gotd_imsg_have ihave
;
451 char hex
[SHA1_DIGEST_STRING_LENGTH
];
452 struct got_object_id id
;
456 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
457 if (datalen
!= sizeof(ihave
))
458 return got_error(GOT_ERR_PRIVSEP_LEN
);
459 memcpy(&ihave
, imsg
->data
, sizeof(ihave
));
461 memset(&id
, 0, sizeof(id
));
462 memcpy(id
.sha1
, ihave
.object_id
, SHA1_DIGEST_LENGTH
);
464 if (log_getverbose() > 0 &&
465 got_object_id_hex(&id
, hex
, sizeof(hex
)))
466 log_debug("client has %s", hex
);
468 imsg_init(&ibuf
, client
->fd
);
470 err
= got_object_get_type(&obj_type
, repo_read
.repo
, &id
);
472 if (err
->code
== GOT_ERR_NO_OBJ
) {
473 gotd_imsg_send_nak(&id
, &ibuf
,
474 PROC_REPO_READ
, repo_read
.pid
);
480 if (obj_type
!= GOT_OBJ_TYPE_COMMIT
&&
481 obj_type
!= GOT_OBJ_TYPE_TAG
) {
482 gotd_imsg_send_nak(&id
, &ibuf
, PROC_REPO_READ
, repo_read
.pid
);
483 err
= got_error(GOT_ERR_OBJ_TYPE
);
487 if (!got_object_idset_contains(client
->have_ids
, &id
)) {
488 err
= got_object_idset_add(client
->have_ids
, &id
, NULL
);
493 gotd_imsg_send_ack(&id
, &ibuf
, PROC_REPO_READ
, repo_read
.pid
);
499 struct repo_read_pack_progress_arg
{
501 struct imsgbuf
*ibuf
;
505 static const struct got_error
*
506 pack_progress(void *arg
, int ncolored
, int nfound
, int ntrees
,
507 off_t packfile_size
, int ncommits
, int nobj_total
, int nobj_deltify
,
510 struct repo_read_pack_progress_arg
*a
= arg
;
511 struct gotd_imsg_packfile_progress iprog
;
514 if (!a
->report_progress
)
516 if (packfile_size
> 0 && a
->sent_ready
)
519 memset(&iprog
, 0, sizeof(iprog
));
520 iprog
.ncolored
= ncolored
;
521 iprog
.nfound
= nfound
;
522 iprog
.ntrees
= ntrees
;
523 iprog
.packfile_size
= packfile_size
;
524 iprog
.ncommits
= ncommits
;
525 iprog
.nobj_total
= nobj_total
;
526 iprog
.nobj_deltify
= nobj_deltify
;
527 iprog
.nobj_written
= nobj_written
;
529 /* Using synchronous writes since we are blocking the event loop. */
530 if (packfile_size
== 0) {
531 ret
= imsg_compose(a
->ibuf
, GOTD_IMSG_PACKFILE_PROGRESS
,
532 PROC_REPO_READ
, repo_read
.pid
, -1, &iprog
, sizeof(iprog
));
534 return got_error_from_errno("imsg compose "
535 "PACKFILE_PROGRESS");
539 ret
= imsg_compose(a
->ibuf
, GOTD_IMSG_PACKFILE_READY
,
540 PROC_REPO_READ
, repo_read
.pid
, -1, &iprog
, sizeof(iprog
));
542 return got_error_from_errno("imsg compose "
547 return gotd_imsg_flush(a
->ibuf
);
550 static const struct got_error
*
551 receive_delta_cache_fd(struct imsg
*imsg
,
552 struct gotd_imsgev
*iev
)
554 struct repo_read_client
*client
= &repo_read_client
;
555 struct gotd_imsg_send_packfile ireq
;
558 log_debug("receiving delta cache file");
560 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
561 if (datalen
!= sizeof(ireq
))
562 return got_error(GOT_ERR_PRIVSEP_LEN
);
563 memcpy(&ireq
, imsg
->data
, sizeof(ireq
));
565 if (client
->delta_cache_fd
!= -1)
566 return got_error(GOT_ERR_PRIVSEP_MSG
);
568 client
->delta_cache_fd
= imsg_get_fd(imsg
);
569 if (client
->delta_cache_fd
== -1)
570 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
572 client
->report_progress
= ireq
.report_progress
;
576 static const struct got_error
*
577 receive_pack_pipe(struct imsg
*imsg
, struct gotd_imsgev
*iev
)
579 struct repo_read_client
*client
= &repo_read_client
;
582 log_debug("receiving pack pipe descriptor");
584 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
586 return got_error(GOT_ERR_PRIVSEP_LEN
);
588 if (client
->pack_pipe
!= -1)
589 return got_error(GOT_ERR_PRIVSEP_MSG
);
591 client
->pack_pipe
= imsg_get_fd(imsg
);
592 if (client
->pack_pipe
== -1)
593 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
598 static const struct got_error
*
599 send_packfile(struct imsg
*imsg
, struct gotd_imsgev
*iev
)
601 const struct got_error
*err
= NULL
;
602 struct repo_read_client
*client
= &repo_read_client
;
603 uint8_t packsha1
[SHA1_DIGEST_LENGTH
];
604 char hex
[SHA1_DIGEST_STRING_LENGTH
];
605 FILE *delta_cache
= NULL
;
607 struct repo_read_pack_progress_arg pa
;
608 struct got_ratelimit rl
;
609 struct gotd_object_id_array want_ids
;
610 struct gotd_object_id_array have_ids
;
612 log_debug("packfile request received");
614 memset(&want_ids
, 0, sizeof(want_ids
));
615 memset(&have_ids
, 0, sizeof(have_ids
));
617 got_ratelimit_init(&rl
, 2, 0);
619 if (client
->delta_cache_fd
== -1 || client
->pack_pipe
== -1)
620 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
622 imsg_init(&ibuf
, client
->fd
);
624 delta_cache
= fdopen(client
->delta_cache_fd
, "w+");
625 if (delta_cache
== NULL
) {
626 err
= got_error_from_errno("fdopen");
629 client
->delta_cache_fd
= -1;
631 memset(&pa
, 0, sizeof(pa
));
633 pa
.report_progress
= client
->report_progress
;
635 err
= got_object_idset_for_each(client
->want_ids
,
636 append_object_id
, &want_ids
);
639 err
= got_object_idset_for_each(client
->have_ids
,
640 append_object_id
, &have_ids
);
644 err
= got_pack_create(packsha1
, client
->pack_pipe
, delta_cache
,
645 have_ids
.ids
, have_ids
.nids
, want_ids
.ids
, want_ids
.nids
,
646 repo_read
.repo
, 0, 1, 0, pack_progress
, &pa
, &rl
,
647 check_cancelled
, NULL
);
651 if (log_getverbose() > 0 &&
652 got_sha1_digest_to_str(packsha1
, hex
, sizeof(hex
)))
653 log_debug("sent pack-%s.pack", hex
);
655 if (gotd_imsg_compose_event(iev
, GOTD_IMSG_PACKFILE_DONE
,
656 PROC_REPO_READ
, -1, NULL
, 0) == -1)
657 err
= got_error_from_errno("imsg compose PACKFILE_DONE");
659 if (client
->delta_cache_fd
!= -1 &&
660 close(client
->delta_cache_fd
) == -1 && err
== NULL
)
661 err
= got_error_from_errno("close");
662 client
->delta_cache_fd
= -1;
663 if (delta_cache
!= NULL
&& fclose(delta_cache
) == EOF
&& err
== NULL
)
664 err
= got_error_from_errno("fclose");
672 repo_read_dispatch_session(int fd
, short event
, void *arg
)
674 const struct got_error
*err
= NULL
;
675 struct gotd_imsgev
*iev
= arg
;
676 struct imsgbuf
*ibuf
= &iev
->ibuf
;
680 struct repo_read_client
*client
= &repo_read_client
;
682 if (event
& EV_READ
) {
683 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
684 fatal("imsg_read error");
685 if (n
== 0) /* Connection closed. */
689 if (event
& EV_WRITE
) {
690 n
= msgbuf_write(&ibuf
->w
);
691 if (n
== -1 && errno
!= EAGAIN
)
692 fatal("msgbuf_write");
693 if (n
== 0) /* Connection closed. */
697 while (err
== NULL
&& check_cancelled(NULL
) == NULL
) {
698 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
699 fatal("%s: imsg_get", __func__
);
700 if (n
== 0) /* No more messages. */
703 if (imsg
.hdr
.type
!= GOTD_IMSG_LIST_REFS_INTERNAL
&&
704 !repo_read
.refs_listed
) {
705 err
= got_error(GOT_ERR_PRIVSEP_MSG
);
709 switch (imsg
.hdr
.type
) {
710 case GOTD_IMSG_LIST_REFS_INTERNAL
:
711 err
= list_refs(&imsg
);
713 log_warnx("ls-refs: %s", err
->msg
);
716 err
= recv_want(&imsg
);
718 log_warnx("want-line: %s", err
->msg
);
721 err
= recv_have(&imsg
);
723 log_warnx("have-line: %s", err
->msg
);
725 case GOTD_IMSG_SEND_PACKFILE
:
726 err
= receive_delta_cache_fd(&imsg
, iev
);
728 log_warnx("receiving delta cache: %s",
731 case GOTD_IMSG_PACKFILE_PIPE
:
732 err
= receive_pack_pipe(&imsg
, iev
);
734 log_warnx("receiving pack pipe: %s", err
->msg
);
737 err
= send_packfile(&imsg
, iev
);
739 log_warnx("sending packfile: %s", err
->msg
);
742 log_debug("unexpected imsg %d", imsg
.hdr
.type
);
749 if (!shut
&& check_cancelled(NULL
) == NULL
) {
751 gotd_imsg_send_error_event(iev
, PROC_REPO_READ
,
752 client
->id
, err
) == -1) {
753 log_warnx("could not send error to parent: %s",
756 gotd_imsg_event_add(iev
);
758 /* This pipe is dead. Remove its event handler */
760 event_loopexit(NULL
);
764 static const struct got_error
*
765 recv_connect(struct imsg
*imsg
)
767 struct gotd_imsgev
*iev
= &repo_read
.session_iev
;
770 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
772 return got_error(GOT_ERR_PRIVSEP_LEN
);
774 if (repo_read
.session_fd
!= -1)
775 return got_error(GOT_ERR_PRIVSEP_MSG
);
777 repo_read
.session_fd
= imsg_get_fd(imsg
);
778 if (repo_read
.session_fd
== -1)
779 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
781 imsg_init(&iev
->ibuf
, repo_read
.session_fd
);
782 iev
->handler
= repo_read_dispatch_session
;
783 iev
->events
= EV_READ
;
784 iev
->handler_arg
= NULL
;
785 event_set(&iev
->ev
, iev
->ibuf
.fd
, EV_READ
,
786 repo_read_dispatch_session
, iev
);
787 gotd_imsg_event_add(iev
);
793 repo_read_dispatch(int fd
, short event
, void *arg
)
795 const struct got_error
*err
= NULL
;
796 struct gotd_imsgev
*iev
= arg
;
797 struct imsgbuf
*ibuf
= &iev
->ibuf
;
801 struct repo_read_client
*client
= &repo_read_client
;
803 if (event
& EV_READ
) {
804 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
805 fatal("imsg_read error");
806 if (n
== 0) /* Connection closed. */
810 if (event
& EV_WRITE
) {
811 n
= msgbuf_write(&ibuf
->w
);
812 if (n
== -1 && errno
!= EAGAIN
)
813 fatal("msgbuf_write");
814 if (n
== 0) /* Connection closed. */
818 while (err
== NULL
&& check_cancelled(NULL
) == NULL
) {
819 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
820 fatal("%s: imsg_get", __func__
);
821 if (n
== 0) /* No more messages. */
824 switch (imsg
.hdr
.type
) {
825 case GOTD_IMSG_CONNECT_REPO_CHILD
:
826 err
= recv_connect(&imsg
);
829 log_debug("unexpected imsg %d", imsg
.hdr
.type
);
836 if (!shut
&& check_cancelled(NULL
) == NULL
) {
838 gotd_imsg_send_error_event(iev
, PROC_REPO_READ
,
839 client
->id
, err
) == -1) {
840 log_warnx("could not send error to parent: %s",
843 gotd_imsg_event_add(iev
);
845 /* This pipe is dead. Remove its event handler */
847 event_loopexit(NULL
);
852 repo_read_main(const char *title
, const char *repo_path
,
853 int *pack_fds
, int *temp_fds
)
855 const struct got_error
*err
= NULL
;
856 struct repo_read_client
*client
= &repo_read_client
;
857 struct gotd_imsgev iev
;
860 client
->delta_cache_fd
= -1;
861 client
->pack_pipe
= -1;
862 client
->have_ids
= got_object_idset_alloc();
863 if (client
->have_ids
== NULL
) {
864 err
= got_error_from_errno("got_object_idset_alloc");
867 client
->want_ids
= got_object_idset_alloc();
868 if (client
->want_ids
== NULL
) {
869 err
= got_error_from_errno("got_object_idset_alloc");
873 repo_read
.title
= title
;
874 repo_read
.pid
= getpid();
875 repo_read
.pack_fds
= pack_fds
;
876 repo_read
.temp_fds
= temp_fds
;
877 repo_read
.session_fd
= -1;
878 repo_read
.session_iev
.ibuf
.fd
= -1;
880 err
= got_repo_open(&repo_read
.repo
, repo_path
, NULL
, pack_fds
);
883 if (!got_repo_is_bare(repo_read
.repo
)) {
884 err
= got_error_msg(GOT_ERR_NOT_GIT_REPO
,
885 "bare git repository required");
889 got_repo_temp_fds_set(repo_read
.repo
, temp_fds
);
891 signal(SIGINT
, catch_sigint
);
892 signal(SIGTERM
, catch_sigterm
);
893 signal(SIGPIPE
, SIG_IGN
);
894 signal(SIGHUP
, SIG_IGN
);
896 imsg_init(&iev
.ibuf
, GOTD_FILENO_MSG_PIPE
);
897 iev
.handler
= repo_read_dispatch
;
898 iev
.events
= EV_READ
;
899 iev
.handler_arg
= NULL
;
900 event_set(&iev
.ev
, iev
.ibuf
.fd
, EV_READ
, repo_read_dispatch
, &iev
);
902 if (gotd_imsg_compose_event(&iev
, GOTD_IMSG_REPO_CHILD_READY
,
903 PROC_REPO_READ
, -1, NULL
, 0) == -1) {
904 err
= got_error_from_errno("imsg compose REPO_CHILD_READY");
911 log_warnx("%s: %s", title
, err
->msg
);
912 repo_read_shutdown();
916 repo_read_shutdown(void)
918 struct repo_read_client
*client
= &repo_read_client
;
920 log_debug("%s: shutting down", repo_read
.title
);
922 if (client
->have_ids
)
923 got_object_idset_free(client
->have_ids
);
924 if (client
->want_ids
)
925 got_object_idset_free(client
->want_ids
);
926 if (client
->fd
!= -1)
928 if (client
->delta_cache_fd
!= -1)
929 close(client
->delta_cache_fd
);
930 if (client
->pack_pipe
!= -1)
931 close(client
->pack_pipe
);
934 got_repo_close(repo_read
.repo
);
935 got_repo_pack_fds_close(repo_read
.pack_fds
);
936 got_repo_temp_fds_close(repo_read
.temp_fds
);
937 if (repo_read
.session_fd
!= -1)
938 close(repo_read
.session_fd
);