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>
21 #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
;
267 struct got_object_id
*head_target_id
= NULL
;
271 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
273 return got_error(GOT_ERR_PRIVSEP_LEN
);
275 if (repo_read
.refs_listed
) {
276 return got_error_msg(GOT_ERR_CLIENT_ID
,
277 "duplicate list-refs request");
279 repo_read
.refs_listed
= 1;
281 client
->fd
= imsg_get_fd(imsg
);
282 if (client
->fd
== -1)
283 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
285 if (imsgbuf_init(&ibuf
, client
->fd
) == -1)
286 return got_error_from_errno("imsgbuf_init");
287 imsgbuf_allow_fdpass(&ibuf
);
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
);
367 imsgbuf_clear(&ibuf
);
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
.hash
, 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 if (imsgbuf_init(&ibuf
, client
->fd
) == -1)
424 return got_error_from_errno("imsgbuf_init");
425 imsgbuf_allow_fdpass(&ibuf
);
427 err
= got_object_get_type(&obj_type
, repo_read
.repo
, &id
);
431 if (obj_type
!= GOT_OBJ_TYPE_COMMIT
&&
432 obj_type
!= GOT_OBJ_TYPE_TAG
)
433 return got_error(GOT_ERR_OBJ_TYPE
);
435 if (!got_object_idset_contains(client
->want_ids
, &id
)) {
436 err
= got_object_idset_add(client
->want_ids
, &id
, NULL
);
441 gotd_imsg_send_ack(&id
, &ibuf
, PROC_REPO_READ
, repo_read
.pid
);
442 imsgbuf_clear(&ibuf
);
446 static const struct got_error
*
447 recv_have(struct imsg
*imsg
)
449 const struct got_error
*err
;
450 struct repo_read_client
*client
= &repo_read_client
;
451 struct gotd_imsg_have ihave
;
453 char hex
[SHA1_DIGEST_STRING_LENGTH
];
454 struct got_object_id id
;
458 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
459 if (datalen
!= sizeof(ihave
))
460 return got_error(GOT_ERR_PRIVSEP_LEN
);
461 memcpy(&ihave
, imsg
->data
, sizeof(ihave
));
463 memset(&id
, 0, sizeof(id
));
464 memcpy(id
.hash
, ihave
.object_id
, SHA1_DIGEST_LENGTH
);
466 if (log_getverbose() > 0 &&
467 got_object_id_hex(&id
, hex
, sizeof(hex
)))
468 log_debug("client has %s", hex
);
470 if (imsgbuf_init(&ibuf
, client
->fd
) == -1)
471 return got_error_from_errno("imsgbuf_init");
472 imsgbuf_allow_fdpass(&ibuf
);
474 err
= got_object_get_type(&obj_type
, repo_read
.repo
, &id
);
476 if (err
->code
== GOT_ERR_NO_OBJ
) {
477 gotd_imsg_send_nak(&id
, &ibuf
,
478 PROC_REPO_READ
, repo_read
.pid
);
484 if (obj_type
!= GOT_OBJ_TYPE_COMMIT
&&
485 obj_type
!= GOT_OBJ_TYPE_TAG
) {
486 gotd_imsg_send_nak(&id
, &ibuf
, PROC_REPO_READ
, repo_read
.pid
);
487 err
= got_error(GOT_ERR_OBJ_TYPE
);
491 if (!got_object_idset_contains(client
->have_ids
, &id
)) {
492 err
= got_object_idset_add(client
->have_ids
, &id
, NULL
);
497 gotd_imsg_send_ack(&id
, &ibuf
, PROC_REPO_READ
, repo_read
.pid
);
499 imsgbuf_clear(&ibuf
);
503 struct repo_read_pack_progress_arg
{
505 struct imsgbuf
*ibuf
;
509 static const struct got_error
*
510 pack_progress(void *arg
, int ncolored
, int nfound
, int ntrees
,
511 off_t packfile_size
, int ncommits
, int nobj_total
, int nobj_deltify
,
514 struct repo_read_pack_progress_arg
*a
= arg
;
515 struct gotd_imsg_packfile_progress iprog
;
518 if (!a
->report_progress
)
520 if (packfile_size
> 0 && a
->sent_ready
)
523 memset(&iprog
, 0, sizeof(iprog
));
524 iprog
.ncolored
= ncolored
;
525 iprog
.nfound
= nfound
;
526 iprog
.ntrees
= ntrees
;
527 iprog
.packfile_size
= packfile_size
;
528 iprog
.ncommits
= ncommits
;
529 iprog
.nobj_total
= nobj_total
;
530 iprog
.nobj_deltify
= nobj_deltify
;
531 iprog
.nobj_written
= nobj_written
;
533 /* Using synchronous writes since we are blocking the event loop. */
534 if (packfile_size
== 0) {
535 ret
= imsg_compose(a
->ibuf
, GOTD_IMSG_PACKFILE_PROGRESS
,
536 PROC_REPO_READ
, repo_read
.pid
, -1, &iprog
, sizeof(iprog
));
538 return got_error_from_errno("imsg compose "
539 "PACKFILE_PROGRESS");
543 ret
= imsg_compose(a
->ibuf
, GOTD_IMSG_PACKFILE_READY
,
544 PROC_REPO_READ
, repo_read
.pid
, -1, &iprog
, sizeof(iprog
));
546 return got_error_from_errno("imsg compose "
551 return gotd_imsg_flush(a
->ibuf
);
554 static const struct got_error
*
555 receive_delta_cache_fd(struct imsg
*imsg
,
556 struct gotd_imsgev
*iev
)
558 struct repo_read_client
*client
= &repo_read_client
;
559 struct gotd_imsg_send_packfile ireq
;
562 log_debug("receiving delta cache file");
564 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
565 if (datalen
!= sizeof(ireq
))
566 return got_error(GOT_ERR_PRIVSEP_LEN
);
567 memcpy(&ireq
, imsg
->data
, sizeof(ireq
));
569 if (client
->delta_cache_fd
!= -1)
570 return got_error(GOT_ERR_PRIVSEP_MSG
);
572 client
->delta_cache_fd
= imsg_get_fd(imsg
);
573 if (client
->delta_cache_fd
== -1)
574 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
576 client
->report_progress
= ireq
.report_progress
;
580 static const struct got_error
*
581 receive_pack_pipe(struct imsg
*imsg
, struct gotd_imsgev
*iev
)
583 struct repo_read_client
*client
= &repo_read_client
;
586 log_debug("receiving pack pipe descriptor");
588 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
590 return got_error(GOT_ERR_PRIVSEP_LEN
);
592 if (client
->pack_pipe
!= -1)
593 return got_error(GOT_ERR_PRIVSEP_MSG
);
595 client
->pack_pipe
= imsg_get_fd(imsg
);
596 if (client
->pack_pipe
== -1)
597 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
602 static const struct got_error
*
603 send_packfile(struct imsg
*imsg
, struct gotd_imsgev
*iev
)
605 const struct got_error
*err
= NULL
;
606 struct repo_read_client
*client
= &repo_read_client
;
607 struct got_object_id packhash
;
608 char hex
[GOT_HASH_DIGEST_STRING_MAXLEN
];
609 FILE *delta_cache
= NULL
;
611 struct repo_read_pack_progress_arg pa
;
612 struct got_ratelimit rl
;
613 struct gotd_object_id_array want_ids
;
614 struct gotd_object_id_array have_ids
;
616 log_debug("packfile request received");
618 memset(&want_ids
, 0, sizeof(want_ids
));
619 memset(&have_ids
, 0, sizeof(have_ids
));
621 got_ratelimit_init(&rl
, 2, 0);
623 if (client
->delta_cache_fd
== -1 || client
->pack_pipe
== -1)
624 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
626 if (imsgbuf_init(&ibuf
, client
->fd
) == -1)
627 return got_error_from_errno("imsgbuf_init");
628 imsgbuf_allow_fdpass(&ibuf
);
630 delta_cache
= fdopen(client
->delta_cache_fd
, "w+");
631 if (delta_cache
== NULL
) {
632 err
= got_error_from_errno("fdopen");
635 client
->delta_cache_fd
= -1;
637 memset(&pa
, 0, sizeof(pa
));
639 pa
.report_progress
= client
->report_progress
;
641 err
= got_object_idset_for_each(client
->want_ids
,
642 append_object_id
, &want_ids
);
645 err
= got_object_idset_for_each(client
->have_ids
,
646 append_object_id
, &have_ids
);
650 err
= got_pack_create(&packhash
, client
->pack_pipe
, delta_cache
,
651 have_ids
.ids
, have_ids
.nids
, want_ids
.ids
, want_ids
.nids
,
652 repo_read
.repo
, 0, 1, 0, pack_progress
, &pa
, &rl
,
653 check_cancelled
, NULL
);
657 if (log_getverbose() > 0 &&
658 got_hash_digest_to_str(packhash
.hash
, hex
, sizeof(hex
),
660 log_debug("sent pack-%s.pack", hex
);
662 if (gotd_imsg_compose_event(iev
, GOTD_IMSG_PACKFILE_DONE
,
663 PROC_REPO_READ
, -1, NULL
, 0) == -1)
664 err
= got_error_from_errno("imsg compose PACKFILE_DONE");
666 if (client
->delta_cache_fd
!= -1 &&
667 close(client
->delta_cache_fd
) == -1 && err
== NULL
)
668 err
= got_error_from_errno("close");
669 client
->delta_cache_fd
= -1;
670 if (delta_cache
!= NULL
&& fclose(delta_cache
) == EOF
&& err
== NULL
)
671 err
= got_error_from_errno("fclose");
672 imsgbuf_clear(&ibuf
);
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
= imsgbuf_read(ibuf
)) == -1)
691 fatal("imsgbuf_read error");
692 if (n
== 0) /* Connection closed. */
696 if (event
& EV_WRITE
) {
697 err
= gotd_imsg_flush(ibuf
);
699 fatalx("%s", err
->msg
);
702 while (err
== NULL
&& check_cancelled(NULL
) == NULL
) {
703 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
704 fatal("%s: imsg_get", __func__
);
705 if (n
== 0) /* No more messages. */
708 if (imsg
.hdr
.type
!= GOTD_IMSG_LIST_REFS_INTERNAL
&&
709 !repo_read
.refs_listed
) {
710 err
= got_error(GOT_ERR_PRIVSEP_MSG
);
714 switch (imsg
.hdr
.type
) {
715 case GOTD_IMSG_LIST_REFS_INTERNAL
:
716 err
= list_refs(&imsg
);
718 log_warnx("ls-refs: %s", err
->msg
);
721 err
= recv_want(&imsg
);
723 log_warnx("want-line: %s", err
->msg
);
726 err
= recv_have(&imsg
);
728 log_warnx("have-line: %s", err
->msg
);
730 case GOTD_IMSG_SEND_PACKFILE
:
731 err
= receive_delta_cache_fd(&imsg
, iev
);
733 log_warnx("receiving delta cache: %s",
736 case GOTD_IMSG_PACKFILE_PIPE
:
737 err
= receive_pack_pipe(&imsg
, iev
);
739 log_warnx("receiving pack pipe: %s", err
->msg
);
742 err
= send_packfile(&imsg
, iev
);
744 log_warnx("sending packfile: %s", err
->msg
);
747 log_debug("unexpected imsg %d", imsg
.hdr
.type
);
754 if (!shut
&& check_cancelled(NULL
) == NULL
) {
756 gotd_imsg_send_error_event(iev
, PROC_REPO_READ
,
757 client
->id
, err
) == -1) {
758 log_warnx("could not send error to parent: %s",
761 gotd_imsg_event_add(iev
);
763 /* This pipe is dead. Remove its event handler */
765 event_loopexit(NULL
);
769 static const struct got_error
*
770 recv_connect(struct imsg
*imsg
)
772 struct gotd_imsgev
*iev
= &repo_read
.session_iev
;
775 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
777 return got_error(GOT_ERR_PRIVSEP_LEN
);
779 if (repo_read
.session_fd
!= -1)
780 return got_error(GOT_ERR_PRIVSEP_MSG
);
782 repo_read
.session_fd
= imsg_get_fd(imsg
);
783 if (repo_read
.session_fd
== -1)
784 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
786 if (imsgbuf_init(&iev
->ibuf
, repo_read
.session_fd
) == -1)
787 return got_error_from_errno("imsgbuf_init");
788 imsgbuf_allow_fdpass(&iev
->ibuf
);
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
= imsgbuf_read(ibuf
)) == -1)
812 fatal("imsgbuf_read error");
813 if (n
== 0) /* Connection closed. */
817 if (event
& EV_WRITE
) {
818 err
= gotd_imsg_flush(ibuf
);
820 fatalx("%s", err
->msg
);
823 while (err
== NULL
&& check_cancelled(NULL
) == NULL
) {
824 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
825 fatal("%s: imsg_get", __func__
);
826 if (n
== 0) /* No more messages. */
829 switch (imsg
.hdr
.type
) {
830 case GOTD_IMSG_CONNECT_REPO_CHILD
:
831 err
= recv_connect(&imsg
);
834 log_debug("unexpected imsg %d", imsg
.hdr
.type
);
841 if (!shut
&& check_cancelled(NULL
) == NULL
) {
843 gotd_imsg_send_error_event(iev
, PROC_REPO_READ
,
844 client
->id
, err
) == -1) {
845 log_warnx("could not send error to parent: %s",
848 gotd_imsg_event_add(iev
);
850 /* This pipe is dead. Remove its event handler */
852 event_loopexit(NULL
);
857 repo_read_main(const char *title
, const char *repo_path
,
858 int *pack_fds
, int *temp_fds
)
860 const struct got_error
*err
= NULL
;
861 struct repo_read_client
*client
= &repo_read_client
;
862 struct gotd_imsgev iev
;
865 client
->delta_cache_fd
= -1;
866 client
->pack_pipe
= -1;
867 client
->have_ids
= got_object_idset_alloc();
868 if (client
->have_ids
== NULL
) {
869 err
= got_error_from_errno("got_object_idset_alloc");
872 client
->want_ids
= got_object_idset_alloc();
873 if (client
->want_ids
== NULL
) {
874 err
= got_error_from_errno("got_object_idset_alloc");
878 repo_read
.title
= title
;
879 repo_read
.pid
= getpid();
880 repo_read
.pack_fds
= pack_fds
;
881 repo_read
.temp_fds
= temp_fds
;
882 repo_read
.session_fd
= -1;
883 repo_read
.session_iev
.ibuf
.fd
= -1;
885 err
= got_repo_open(&repo_read
.repo
, repo_path
, NULL
, pack_fds
);
888 if (!got_repo_is_bare(repo_read
.repo
)) {
889 err
= got_error_msg(GOT_ERR_NOT_GIT_REPO
,
890 "bare git repository required");
893 if (got_repo_get_object_format(repo_read
.repo
) != GOT_HASH_SHA1
) {
894 err
= got_error_msg(GOT_ERR_NOT_IMPL
,
895 "sha256 object IDs unsupported in network protocol");
899 got_repo_temp_fds_set(repo_read
.repo
, temp_fds
);
901 signal(SIGINT
, catch_sigint
);
902 signal(SIGTERM
, catch_sigterm
);
903 signal(SIGPIPE
, SIG_IGN
);
904 signal(SIGHUP
, SIG_IGN
);
906 if (imsgbuf_init(&iev
.ibuf
, GOTD_FILENO_MSG_PIPE
) == -1) {
907 err
= got_error_from_errno("imsgbuf_init");
910 imsgbuf_allow_fdpass(&iev
.ibuf
);
911 iev
.handler
= repo_read_dispatch
;
912 iev
.events
= EV_READ
;
913 iev
.handler_arg
= NULL
;
914 event_set(&iev
.ev
, iev
.ibuf
.fd
, EV_READ
, repo_read_dispatch
, &iev
);
916 if (gotd_imsg_compose_event(&iev
, GOTD_IMSG_REPO_CHILD_READY
,
917 PROC_REPO_READ
, -1, NULL
, 0) == -1) {
918 err
= got_error_from_errno("imsg compose REPO_CHILD_READY");
925 log_warnx("%s: %s", title
, err
->msg
);
926 repo_read_shutdown();
930 repo_read_shutdown(void)
932 struct repo_read_client
*client
= &repo_read_client
;
934 log_debug("%s: shutting down", repo_read
.title
);
936 if (client
->have_ids
)
937 got_object_idset_free(client
->have_ids
);
938 if (client
->want_ids
)
939 got_object_idset_free(client
->want_ids
);
940 if (client
->fd
!= -1)
942 if (client
->delta_cache_fd
!= -1)
943 close(client
->delta_cache_fd
);
944 if (client
->pack_pipe
!= -1)
945 close(client
->pack_pipe
);
948 got_repo_close(repo_read
.repo
);
949 got_repo_pack_fds_close(repo_read
.pack_fds
);
950 got_repo_temp_fds_close(repo_read
.temp_fds
);
951 if (repo_read
.session_fd
!= -1)
952 close(repo_read
.session_fd
);