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>
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
;
270 int client_fd
= imsg
->fd
;
271 struct got_object_id
*head_target_id
= NULL
;
276 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
278 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
279 if (datalen
!= sizeof(ireq
))
280 return got_error(GOT_ERR_PRIVSEP_LEN
);
281 memcpy(&ireq
, imsg
->data
, sizeof(ireq
));
283 if (ireq
.client_id
== 0)
284 return got_error(GOT_ERR_CLIENT_ID
);
285 if (client
->id
!= 0) {
286 return got_error_msg(GOT_ERR_CLIENT_ID
,
287 "duplicate list-refs request");
289 client
->id
= ireq
.client_id
;
290 client
->fd
= client_fd
;
292 imsg_init(&ibuf
, client_fd
);
294 err
= got_ref_list(&refs
, repo_read
.repo
, "",
295 got_ref_cmp_by_name
, NULL
);
299 memset(&irefs
, 0, sizeof(irefs
));
300 TAILQ_FOREACH(re
, &refs
, entry
) {
301 struct got_object_id
*id
;
304 if (got_ref_is_symbolic(re
->ref
)) {
305 const char *refname
= got_ref_get_name(re
->ref
);
306 if (strcmp(refname
, GOT_REF_HEAD
) != 0)
308 err
= got_ref_resolve(&head_target_id
, repo_read
.repo
,
311 if (err
->code
!= GOT_ERR_NOT_REF
)
314 * HEAD points to a non-existent branch.
315 * Do not advertise it.
316 * Matches git-daemon's behaviour.
318 head_target_id
= NULL
;
327 /* Account for a peeled tag refs. */
328 err
= got_ref_resolve(&id
, repo_read
.repo
, re
->ref
);
331 err
= got_object_get_type(&obj_type
, repo_read
.repo
, id
);
335 if (obj_type
== GOT_OBJ_TYPE_TAG
)
339 if (imsg_compose(&ibuf
, GOTD_IMSG_REFLIST
, PROC_REPO_READ
,
340 repo_read
.pid
, -1, &irefs
, sizeof(irefs
)) == -1) {
341 err
= got_error_from_errno("imsg_compose REFLIST");
346 * Send the HEAD symref first. In Git-protocol versions < 2
347 * the HEAD symref must be announced on the initial line of
348 * the server's ref advertisement.
349 * For now, we do not advertise symrefs other than HEAD.
351 TAILQ_FOREACH(re
, &refs
, entry
) {
352 if (!got_ref_is_symbolic(re
->ref
) ||
353 strcmp(got_ref_get_name(re
->ref
), GOT_REF_HEAD
) != 0 ||
354 head_target_id
== NULL
)
356 err
= send_symref(re
->ref
, head_target_id
, &ibuf
);
361 TAILQ_FOREACH(re
, &refs
, entry
) {
362 if (got_ref_is_symbolic(re
->ref
))
364 err
= send_ref(re
->ref
, &ibuf
);
369 err
= gotd_imsg_flush(&ibuf
);
371 got_ref_list_free(&refs
);
376 static const struct got_error
*
377 append_object_id(struct got_object_id
*id
, void *data
, void *arg
)
379 struct gotd_object_id_array
*array
= arg
;
380 const size_t alloc_chunksz
= 256;
382 if (array
->ids
== NULL
) {
383 array
->ids
= reallocarray(NULL
, alloc_chunksz
,
384 sizeof(*array
->ids
));
385 if (array
->ids
== NULL
)
386 return got_error_from_errno("reallocarray");
387 array
->nalloc
= alloc_chunksz
;
389 } else if (array
->nalloc
<= array
->nids
) {
390 struct got_object_id
**new;
391 new = recallocarray(array
->ids
, array
->nalloc
,
392 array
->nalloc
+ alloc_chunksz
, sizeof(*new));
394 return got_error_from_errno("recallocarray");
396 array
->nalloc
+= alloc_chunksz
;
399 array
->ids
[array
->nids
] = id
;
404 static const struct got_error
*
405 recv_want(struct imsg
*imsg
)
407 const struct got_error
*err
;
408 struct repo_read_client
*client
= &repo_read_client
;
409 struct gotd_imsg_want iwant
;
411 char hex
[SHA1_DIGEST_STRING_LENGTH
];
412 struct got_object_id id
;
416 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
417 if (datalen
!= sizeof(iwant
))
418 return got_error(GOT_ERR_PRIVSEP_LEN
);
419 memcpy(&iwant
, imsg
->data
, sizeof(iwant
));
421 memset(&id
, 0, sizeof(id
));
422 memcpy(id
.sha1
, iwant
.object_id
, SHA1_DIGEST_LENGTH
);
424 if (log_getverbose() > 0 &&
425 got_sha1_digest_to_str(id
.sha1
, hex
, sizeof(hex
)))
426 log_debug("client wants %s", hex
);
428 imsg_init(&ibuf
, client
->fd
);
430 err
= got_object_get_type(&obj_type
, repo_read
.repo
, &id
);
434 if (obj_type
!= GOT_OBJ_TYPE_COMMIT
&&
435 obj_type
!= GOT_OBJ_TYPE_TAG
)
436 return got_error(GOT_ERR_OBJ_TYPE
);
438 if (!got_object_idset_contains(client
->want_ids
, &id
)) {
439 err
= got_object_idset_add(client
->want_ids
, &id
, NULL
);
444 gotd_imsg_send_ack(&id
, &ibuf
, PROC_REPO_READ
, repo_read
.pid
);
449 static const struct got_error
*
450 recv_have(struct imsg
*imsg
)
452 const struct got_error
*err
;
453 struct repo_read_client
*client
= &repo_read_client
;
454 struct gotd_imsg_have ihave
;
456 char hex
[SHA1_DIGEST_STRING_LENGTH
];
457 struct got_object_id id
;
461 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
462 if (datalen
!= sizeof(ihave
))
463 return got_error(GOT_ERR_PRIVSEP_LEN
);
464 memcpy(&ihave
, imsg
->data
, sizeof(ihave
));
466 memset(&id
, 0, sizeof(id
));
467 memcpy(id
.sha1
, ihave
.object_id
, SHA1_DIGEST_LENGTH
);
469 if (log_getverbose() > 0 &&
470 got_sha1_digest_to_str(id
.sha1
, hex
, sizeof(hex
)))
471 log_debug("client has %s", hex
);
473 imsg_init(&ibuf
, client
->fd
);
475 err
= got_object_get_type(&obj_type
, repo_read
.repo
, &id
);
477 if (err
->code
== GOT_ERR_NO_OBJ
) {
478 gotd_imsg_send_nak(&id
, &ibuf
,
479 PROC_REPO_READ
, repo_read
.pid
);
485 if (obj_type
!= GOT_OBJ_TYPE_COMMIT
&&
486 obj_type
!= GOT_OBJ_TYPE_TAG
) {
487 gotd_imsg_send_nak(&id
, &ibuf
, PROC_REPO_READ
, repo_read
.pid
);
488 err
= got_error(GOT_ERR_OBJ_TYPE
);
492 if (!got_object_idset_contains(client
->have_ids
, &id
)) {
493 err
= got_object_idset_add(client
->have_ids
, &id
, NULL
);
498 gotd_imsg_send_ack(&id
, &ibuf
, PROC_REPO_READ
, repo_read
.pid
);
504 struct repo_read_pack_progress_arg
{
506 struct imsgbuf
*ibuf
;
510 static const struct got_error
*
511 pack_progress(void *arg
, int ncolored
, int nfound
, int ntrees
,
512 off_t packfile_size
, int ncommits
, int nobj_total
, int nobj_deltify
,
515 struct repo_read_pack_progress_arg
*a
= arg
;
516 struct gotd_imsg_packfile_progress iprog
;
519 if (!a
->report_progress
)
521 if (packfile_size
> 0 && a
->sent_ready
)
524 memset(&iprog
, 0, sizeof(iprog
));
525 iprog
.ncolored
= ncolored
;
526 iprog
.nfound
= nfound
;
527 iprog
.ntrees
= ntrees
;
528 iprog
.packfile_size
= packfile_size
;
529 iprog
.ncommits
= ncommits
;
530 iprog
.nobj_total
= nobj_total
;
531 iprog
.nobj_deltify
= nobj_deltify
;
532 iprog
.nobj_written
= nobj_written
;
534 /* Using synchronous writes since we are blocking the event loop. */
535 if (packfile_size
== 0) {
536 ret
= imsg_compose(a
->ibuf
, GOTD_IMSG_PACKFILE_PROGRESS
,
537 PROC_REPO_READ
, repo_read
.pid
, -1, &iprog
, sizeof(iprog
));
539 return got_error_from_errno("imsg compose "
540 "PACKFILE_PROGRESS");
544 ret
= imsg_compose(a
->ibuf
, GOTD_IMSG_PACKFILE_READY
,
545 PROC_REPO_READ
, repo_read
.pid
, -1, &iprog
, sizeof(iprog
));
547 return got_error_from_errno("imsg compose "
552 return gotd_imsg_flush(a
->ibuf
);
555 static const struct got_error
*
556 receive_delta_cache_fd(struct imsg
*imsg
,
557 struct gotd_imsgev
*iev
)
559 struct repo_read_client
*client
= &repo_read_client
;
560 struct gotd_imsg_send_packfile ireq
;
563 log_debug("receiving delta cache file");
566 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
568 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
569 if (datalen
!= sizeof(ireq
))
570 return got_error(GOT_ERR_PRIVSEP_LEN
);
571 memcpy(&ireq
, imsg
->data
, sizeof(ireq
));
573 if (client
->delta_cache_fd
!= -1)
574 return got_error(GOT_ERR_PRIVSEP_MSG
);
576 client
->delta_cache_fd
= imsg
->fd
;
577 client
->report_progress
= ireq
.report_progress
;
581 static const struct got_error
*
582 receive_pack_pipe(struct imsg
*imsg
, struct gotd_imsgev
*iev
)
584 struct repo_read_client
*client
= &repo_read_client
;
585 struct gotd_imsg_packfile_pipe ireq
;
588 log_debug("receiving pack pipe descriptor");
591 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
593 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
594 if (datalen
!= sizeof(ireq
))
595 return got_error(GOT_ERR_PRIVSEP_LEN
);
596 memcpy(&ireq
, imsg
->data
, sizeof(ireq
));
598 if (client
->pack_pipe
!= -1)
599 return got_error(GOT_ERR_PRIVSEP_MSG
);
601 client
->pack_pipe
= imsg
->fd
;
605 static const struct got_error
*
606 send_packfile(struct imsg
*imsg
, struct gotd_imsgev
*iev
)
608 const struct got_error
*err
= NULL
;
609 struct repo_read_client
*client
= &repo_read_client
;
610 struct gotd_imsg_packfile_done idone
;
611 uint8_t packsha1
[SHA1_DIGEST_LENGTH
];
612 char hex
[SHA1_DIGEST_STRING_LENGTH
];
613 FILE *delta_cache
= NULL
;
615 struct repo_read_pack_progress_arg pa
;
616 struct got_ratelimit rl
;
617 struct gotd_object_id_array want_ids
;
618 struct gotd_object_id_array have_ids
;
620 log_debug("packfile request received");
622 memset(&want_ids
, 0, sizeof(want_ids
));
623 memset(&have_ids
, 0, sizeof(have_ids
));
625 got_ratelimit_init(&rl
, 2, 0);
627 if (client
->delta_cache_fd
== -1 || client
->pack_pipe
== -1)
628 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
630 imsg_init(&ibuf
, client
->fd
);
632 delta_cache
= fdopen(client
->delta_cache_fd
, "w+");
633 if (delta_cache
== NULL
) {
634 err
= got_error_from_errno("fdopen");
637 client
->delta_cache_fd
= -1;
639 memset(&pa
, 0, sizeof(pa
));
641 pa
.report_progress
= client
->report_progress
;
643 err
= got_object_idset_for_each(client
->want_ids
,
644 append_object_id
, &want_ids
);
647 err
= got_object_idset_for_each(client
->have_ids
,
648 append_object_id
, &have_ids
);
652 err
= got_pack_create(packsha1
, client
->pack_pipe
, delta_cache
,
653 have_ids
.ids
, have_ids
.nids
, want_ids
.ids
, want_ids
.nids
,
654 repo_read
.repo
, 0, 1, 0, pack_progress
, &pa
, &rl
,
655 check_cancelled
, NULL
);
659 if (log_getverbose() > 0 &&
660 got_sha1_digest_to_str(packsha1
, hex
, sizeof(hex
)))
661 log_debug("sent pack-%s.pack", hex
);
663 memset(&idone
, 0, sizeof(idone
));
664 idone
.client_id
= client
->id
;
665 if (gotd_imsg_compose_event(iev
, GOTD_IMSG_PACKFILE_DONE
,
666 PROC_REPO_READ
, -1, &idone
, sizeof(idone
)) == -1)
667 err
= got_error_from_errno("imsg compose PACKFILE_DONE");
669 if (delta_cache
!= NULL
&& fclose(delta_cache
) == EOF
&& err
== NULL
)
670 err
= got_error_from_errno("fclose");
678 repo_read_dispatch_session(int fd
, short event
, void *arg
)
680 const struct got_error
*err
= NULL
;
681 struct gotd_imsgev
*iev
= arg
;
682 struct imsgbuf
*ibuf
= &iev
->ibuf
;
686 struct repo_read_client
*client
= &repo_read_client
;
688 if (event
& EV_READ
) {
689 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
690 fatal("imsg_read error");
691 if (n
== 0) /* Connection closed. */
695 if (event
& EV_WRITE
) {
696 n
= msgbuf_write(&ibuf
->w
);
697 if (n
== -1 && errno
!= EAGAIN
)
698 fatal("msgbuf_write");
699 if (n
== 0) /* Connection closed. */
703 while (err
== NULL
&& check_cancelled(NULL
) == NULL
) {
704 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
705 fatal("%s: imsg_get", __func__
);
706 if (n
== 0) /* No more messages. */
709 if (imsg
.hdr
.type
!= GOTD_IMSG_LIST_REFS_INTERNAL
&&
711 err
= got_error(GOT_ERR_PRIVSEP_MSG
);
715 switch (imsg
.hdr
.type
) {
716 case GOTD_IMSG_LIST_REFS_INTERNAL
:
717 err
= list_refs(&imsg
);
719 log_warnx("ls-refs: %s", err
->msg
);
722 err
= recv_want(&imsg
);
724 log_warnx("want-line: %s", err
->msg
);
727 err
= recv_have(&imsg
);
729 log_warnx("have-line: %s", err
->msg
);
731 case GOTD_IMSG_SEND_PACKFILE
:
732 err
= receive_delta_cache_fd(&imsg
, iev
);
734 log_warnx("receiving delta cache: %s",
737 case GOTD_IMSG_PACKFILE_PIPE
:
738 err
= receive_pack_pipe(&imsg
, iev
);
740 log_warnx("receiving pack pipe: %s", err
->msg
);
743 err
= send_packfile(&imsg
, iev
);
745 log_warnx("sending packfile: %s", err
->msg
);
748 log_debug("unexpected imsg %d", imsg
.hdr
.type
);
755 if (!shut
&& check_cancelled(NULL
) == NULL
) {
757 gotd_imsg_send_error_event(iev
, PROC_REPO_READ
,
758 client
->id
, err
) == -1) {
759 log_warnx("could not send error to parent: %s",
762 gotd_imsg_event_add(iev
);
764 /* This pipe is dead. Remove its event handler */
766 event_loopexit(NULL
);
770 static const struct got_error
*
771 recv_connect(struct imsg
*imsg
)
773 struct gotd_imsgev
*iev
= &repo_read
.session_iev
;
776 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
778 return got_error(GOT_ERR_PRIVSEP_LEN
);
780 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
782 if (repo_read
.session_fd
!= -1)
783 return got_error(GOT_ERR_PRIVSEP_MSG
);
785 repo_read
.session_fd
= imsg
->fd
;
787 imsg_init(&iev
->ibuf
, repo_read
.session_fd
);
788 iev
->handler
= repo_read_dispatch_session
;
789 iev
->events
= EV_READ
;
790 iev
->handler_arg
= NULL
;
791 event_set(&iev
->ev
, iev
->ibuf
.fd
, EV_READ
,
792 repo_read_dispatch_session
, iev
);
793 gotd_imsg_event_add(iev
);
799 repo_read_dispatch(int fd
, short event
, void *arg
)
801 const struct got_error
*err
= NULL
;
802 struct gotd_imsgev
*iev
= arg
;
803 struct imsgbuf
*ibuf
= &iev
->ibuf
;
807 struct repo_read_client
*client
= &repo_read_client
;
809 if (event
& EV_READ
) {
810 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
811 fatal("imsg_read error");
812 if (n
== 0) /* Connection closed. */
816 if (event
& EV_WRITE
) {
817 n
= msgbuf_write(&ibuf
->w
);
818 if (n
== -1 && errno
!= EAGAIN
)
819 fatal("msgbuf_write");
820 if (n
== 0) /* Connection closed. */
824 while (err
== NULL
&& check_cancelled(NULL
) == NULL
) {
825 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
826 fatal("%s: imsg_get", __func__
);
827 if (n
== 0) /* No more messages. */
830 switch (imsg
.hdr
.type
) {
831 case GOTD_IMSG_CONNECT_REPO_CHILD
:
832 err
= recv_connect(&imsg
);
835 log_debug("unexpected imsg %d", imsg
.hdr
.type
);
842 if (!shut
&& check_cancelled(NULL
) == NULL
) {
844 gotd_imsg_send_error_event(iev
, PROC_REPO_READ
,
845 client
->id
, err
) == -1) {
846 log_warnx("could not send error to parent: %s",
849 gotd_imsg_event_add(iev
);
851 /* This pipe is dead. Remove its event handler */
853 event_loopexit(NULL
);
858 repo_read_main(const char *title
, const char *repo_path
,
859 int *pack_fds
, int *temp_fds
)
861 const struct got_error
*err
= NULL
;
862 struct repo_read_client
*client
= &repo_read_client
;
863 struct gotd_imsgev iev
;
866 client
->delta_cache_fd
= -1;
867 client
->pack_pipe
= -1;
868 client
->have_ids
= got_object_idset_alloc();
869 if (client
->have_ids
== NULL
) {
870 err
= got_error_from_errno("got_object_idset_alloc");
873 client
->want_ids
= got_object_idset_alloc();
874 if (client
->want_ids
== NULL
) {
875 err
= got_error_from_errno("got_object_idset_alloc");
879 repo_read
.title
= title
;
880 repo_read
.pid
= getpid();
881 repo_read
.pack_fds
= pack_fds
;
882 repo_read
.temp_fds
= temp_fds
;
883 repo_read
.session_fd
= -1;
884 repo_read
.session_iev
.ibuf
.fd
= -1;
886 err
= got_repo_open(&repo_read
.repo
, repo_path
, NULL
, pack_fds
);
889 if (!got_repo_is_bare(repo_read
.repo
)) {
890 err
= got_error_msg(GOT_ERR_NOT_GIT_REPO
,
891 "bare git repository required");
895 got_repo_temp_fds_set(repo_read
.repo
, temp_fds
);
897 signal(SIGINT
, catch_sigint
);
898 signal(SIGTERM
, catch_sigterm
);
899 signal(SIGPIPE
, SIG_IGN
);
900 signal(SIGHUP
, SIG_IGN
);
902 imsg_init(&iev
.ibuf
, GOTD_FILENO_MSG_PIPE
);
903 iev
.handler
= repo_read_dispatch
;
904 iev
.events
= EV_READ
;
905 iev
.handler_arg
= NULL
;
906 event_set(&iev
.ev
, iev
.ibuf
.fd
, EV_READ
, repo_read_dispatch
, &iev
);
908 if (gotd_imsg_compose_event(&iev
, GOTD_IMSG_REPO_CHILD_READY
,
909 PROC_REPO_READ
, -1, NULL
, 0) == -1) {
910 err
= got_error_from_errno("imsg compose REPO_CHILD_READY");
917 log_warnx("%s: %s", title
, err
->msg
);
918 repo_read_shutdown();
922 repo_read_shutdown(void)
924 struct repo_read_client
*client
= &repo_read_client
;
926 log_debug("shutting down");
928 if (client
->have_ids
)
929 got_object_idset_free(client
->have_ids
);
930 if (client
->want_ids
)
931 got_object_idset_free(client
->want_ids
);
932 if (client
->fd
!= -1)
934 if (client
->delta_cache_fd
!= -1)
935 close(client
->delta_cache_fd
);
936 if (client
->pack_pipe
!= -1)
937 close(client
->pack_pipe
);
940 got_repo_close(repo_read
.repo
);
941 got_repo_pack_fds_close(repo_read
.pack_fds
);
942 got_repo_temp_fds_close(repo_read
.temp_fds
);
943 if (repo_read
.session_fd
!= -1)
944 close(repo_read
.session_fd
);