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
->hash
, 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
->hash
, 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
->hash
, 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
;
266 struct got_object_id
*head_target_id
= NULL
;
270 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
272 return got_error(GOT_ERR_PRIVSEP_LEN
);
274 if (repo_read
.refs_listed
) {
275 return got_error_msg(GOT_ERR_CLIENT_ID
,
276 "duplicate list-refs request");
278 repo_read
.refs_listed
= 1;
280 client
->fd
= imsg_get_fd(imsg
);
281 if (client
->fd
== -1)
282 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
284 imsg_init(&ibuf
, client
->fd
);
286 err
= got_ref_list(&refs
, repo_read
.repo
, "",
287 got_ref_cmp_by_name
, NULL
);
291 memset(&irefs
, 0, sizeof(irefs
));
292 TAILQ_FOREACH(re
, &refs
, entry
) {
293 struct got_object_id
*id
;
296 if (got_ref_is_symbolic(re
->ref
)) {
297 const char *refname
= got_ref_get_name(re
->ref
);
298 if (strcmp(refname
, GOT_REF_HEAD
) != 0)
300 err
= got_ref_resolve(&head_target_id
, repo_read
.repo
,
303 if (err
->code
!= GOT_ERR_NOT_REF
)
306 * HEAD points to a non-existent branch.
307 * Do not advertise it.
308 * Matches git-daemon's behaviour.
310 head_target_id
= NULL
;
319 /* Account for a peeled tag refs. */
320 err
= got_ref_resolve(&id
, repo_read
.repo
, re
->ref
);
323 err
= got_object_get_type(&obj_type
, repo_read
.repo
, id
);
327 if (obj_type
== GOT_OBJ_TYPE_TAG
)
331 if (imsg_compose(&ibuf
, GOTD_IMSG_REFLIST
, PROC_REPO_READ
,
332 repo_read
.pid
, -1, &irefs
, sizeof(irefs
)) == -1) {
333 err
= got_error_from_errno("imsg_compose REFLIST");
338 * Send the HEAD symref first. In Git-protocol versions < 2
339 * the HEAD symref must be announced on the initial line of
340 * the server's ref advertisement.
341 * For now, we do not advertise symrefs other than HEAD.
343 TAILQ_FOREACH(re
, &refs
, entry
) {
344 if (!got_ref_is_symbolic(re
->ref
) ||
345 strcmp(got_ref_get_name(re
->ref
), GOT_REF_HEAD
) != 0 ||
346 head_target_id
== NULL
)
348 err
= send_symref(re
->ref
, head_target_id
, &ibuf
);
353 TAILQ_FOREACH(re
, &refs
, entry
) {
354 if (got_ref_is_symbolic(re
->ref
))
356 err
= send_ref(re
->ref
, &ibuf
);
361 err
= gotd_imsg_flush(&ibuf
);
363 got_ref_list_free(&refs
);
368 static const struct got_error
*
369 append_object_id(struct got_object_id
*id
, void *data
, void *arg
)
371 struct gotd_object_id_array
*array
= arg
;
372 const size_t alloc_chunksz
= 256;
374 if (array
->ids
== NULL
) {
375 array
->ids
= reallocarray(NULL
, alloc_chunksz
,
376 sizeof(*array
->ids
));
377 if (array
->ids
== NULL
)
378 return got_error_from_errno("reallocarray");
379 array
->nalloc
= alloc_chunksz
;
381 } else if (array
->nalloc
<= array
->nids
) {
382 struct got_object_id
**new;
383 new = recallocarray(array
->ids
, array
->nalloc
,
384 array
->nalloc
+ alloc_chunksz
, sizeof(*new));
386 return got_error_from_errno("recallocarray");
388 array
->nalloc
+= alloc_chunksz
;
391 array
->ids
[array
->nids
] = id
;
396 static const struct got_error
*
397 recv_want(struct imsg
*imsg
)
399 const struct got_error
*err
;
400 struct repo_read_client
*client
= &repo_read_client
;
401 struct gotd_imsg_want iwant
;
403 char hex
[SHA1_DIGEST_STRING_LENGTH
];
404 struct got_object_id id
;
408 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
409 if (datalen
!= sizeof(iwant
))
410 return got_error(GOT_ERR_PRIVSEP_LEN
);
411 memcpy(&iwant
, imsg
->data
, sizeof(iwant
));
413 memset(&id
, 0, sizeof(id
));
414 memcpy(id
.hash
, iwant
.object_id
, SHA1_DIGEST_LENGTH
);
416 if (log_getverbose() > 0 &&
417 got_object_id_hex(&id
, hex
, sizeof(hex
)))
418 log_debug("client wants %s", hex
);
420 imsg_init(&ibuf
, client
->fd
);
422 err
= got_object_get_type(&obj_type
, repo_read
.repo
, &id
);
426 if (obj_type
!= GOT_OBJ_TYPE_COMMIT
&&
427 obj_type
!= GOT_OBJ_TYPE_TAG
)
428 return got_error(GOT_ERR_OBJ_TYPE
);
430 if (!got_object_idset_contains(client
->want_ids
, &id
)) {
431 err
= got_object_idset_add(client
->want_ids
, &id
, NULL
);
436 gotd_imsg_send_ack(&id
, &ibuf
, PROC_REPO_READ
, repo_read
.pid
);
441 static const struct got_error
*
442 recv_have(struct imsg
*imsg
)
444 const struct got_error
*err
;
445 struct repo_read_client
*client
= &repo_read_client
;
446 struct gotd_imsg_have ihave
;
448 char hex
[SHA1_DIGEST_STRING_LENGTH
];
449 struct got_object_id id
;
453 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
454 if (datalen
!= sizeof(ihave
))
455 return got_error(GOT_ERR_PRIVSEP_LEN
);
456 memcpy(&ihave
, imsg
->data
, sizeof(ihave
));
458 memset(&id
, 0, sizeof(id
));
459 memcpy(id
.hash
, ihave
.object_id
, SHA1_DIGEST_LENGTH
);
461 if (log_getverbose() > 0 &&
462 got_object_id_hex(&id
, hex
, sizeof(hex
)))
463 log_debug("client has %s", hex
);
465 imsg_init(&ibuf
, client
->fd
);
467 err
= got_object_get_type(&obj_type
, repo_read
.repo
, &id
);
469 if (err
->code
== GOT_ERR_NO_OBJ
) {
470 gotd_imsg_send_nak(&id
, &ibuf
,
471 PROC_REPO_READ
, repo_read
.pid
);
477 if (obj_type
!= GOT_OBJ_TYPE_COMMIT
&&
478 obj_type
!= GOT_OBJ_TYPE_TAG
) {
479 gotd_imsg_send_nak(&id
, &ibuf
, PROC_REPO_READ
, repo_read
.pid
);
480 err
= got_error(GOT_ERR_OBJ_TYPE
);
484 if (!got_object_idset_contains(client
->have_ids
, &id
)) {
485 err
= got_object_idset_add(client
->have_ids
, &id
, NULL
);
490 gotd_imsg_send_ack(&id
, &ibuf
, PROC_REPO_READ
, repo_read
.pid
);
496 struct repo_read_pack_progress_arg
{
498 struct imsgbuf
*ibuf
;
502 static const struct got_error
*
503 pack_progress(void *arg
, int ncolored
, int nfound
, int ntrees
,
504 off_t packfile_size
, int ncommits
, int nobj_total
, int nobj_deltify
,
507 struct repo_read_pack_progress_arg
*a
= arg
;
508 struct gotd_imsg_packfile_progress iprog
;
511 if (!a
->report_progress
)
513 if (packfile_size
> 0 && a
->sent_ready
)
516 memset(&iprog
, 0, sizeof(iprog
));
517 iprog
.ncolored
= ncolored
;
518 iprog
.nfound
= nfound
;
519 iprog
.ntrees
= ntrees
;
520 iprog
.packfile_size
= packfile_size
;
521 iprog
.ncommits
= ncommits
;
522 iprog
.nobj_total
= nobj_total
;
523 iprog
.nobj_deltify
= nobj_deltify
;
524 iprog
.nobj_written
= nobj_written
;
526 /* Using synchronous writes since we are blocking the event loop. */
527 if (packfile_size
== 0) {
528 ret
= imsg_compose(a
->ibuf
, GOTD_IMSG_PACKFILE_PROGRESS
,
529 PROC_REPO_READ
, repo_read
.pid
, -1, &iprog
, sizeof(iprog
));
531 return got_error_from_errno("imsg compose "
532 "PACKFILE_PROGRESS");
536 ret
= imsg_compose(a
->ibuf
, GOTD_IMSG_PACKFILE_READY
,
537 PROC_REPO_READ
, repo_read
.pid
, -1, &iprog
, sizeof(iprog
));
539 return got_error_from_errno("imsg compose "
544 return gotd_imsg_flush(a
->ibuf
);
547 static const struct got_error
*
548 receive_delta_cache_fd(struct imsg
*imsg
,
549 struct gotd_imsgev
*iev
)
551 struct repo_read_client
*client
= &repo_read_client
;
552 struct gotd_imsg_send_packfile ireq
;
555 log_debug("receiving delta cache file");
557 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
558 if (datalen
!= sizeof(ireq
))
559 return got_error(GOT_ERR_PRIVSEP_LEN
);
560 memcpy(&ireq
, imsg
->data
, sizeof(ireq
));
562 if (client
->delta_cache_fd
!= -1)
563 return got_error(GOT_ERR_PRIVSEP_MSG
);
565 client
->delta_cache_fd
= imsg_get_fd(imsg
);
566 if (client
->delta_cache_fd
== -1)
567 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
569 client
->report_progress
= ireq
.report_progress
;
573 static const struct got_error
*
574 receive_pack_pipe(struct imsg
*imsg
, struct gotd_imsgev
*iev
)
576 struct repo_read_client
*client
= &repo_read_client
;
579 log_debug("receiving pack pipe descriptor");
581 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
583 return got_error(GOT_ERR_PRIVSEP_LEN
);
585 if (client
->pack_pipe
!= -1)
586 return got_error(GOT_ERR_PRIVSEP_MSG
);
588 client
->pack_pipe
= imsg_get_fd(imsg
);
589 if (client
->pack_pipe
== -1)
590 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
595 static const struct got_error
*
596 send_packfile(struct imsg
*imsg
, struct gotd_imsgev
*iev
)
598 const struct got_error
*err
= NULL
;
599 struct repo_read_client
*client
= &repo_read_client
;
600 struct got_object_id packhash
;
601 char hex
[GOT_HASH_DIGEST_STRING_MAXLEN
];
602 FILE *delta_cache
= NULL
;
604 struct repo_read_pack_progress_arg pa
;
605 struct got_ratelimit rl
;
606 struct gotd_object_id_array want_ids
;
607 struct gotd_object_id_array have_ids
;
609 log_debug("packfile request received");
611 memset(&want_ids
, 0, sizeof(want_ids
));
612 memset(&have_ids
, 0, sizeof(have_ids
));
614 got_ratelimit_init(&rl
, 2, 0);
616 if (client
->delta_cache_fd
== -1 || client
->pack_pipe
== -1)
617 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
619 imsg_init(&ibuf
, client
->fd
);
621 delta_cache
= fdopen(client
->delta_cache_fd
, "w+");
622 if (delta_cache
== NULL
) {
623 err
= got_error_from_errno("fdopen");
626 client
->delta_cache_fd
= -1;
628 memset(&pa
, 0, sizeof(pa
));
630 pa
.report_progress
= client
->report_progress
;
632 err
= got_object_idset_for_each(client
->want_ids
,
633 append_object_id
, &want_ids
);
636 err
= got_object_idset_for_each(client
->have_ids
,
637 append_object_id
, &have_ids
);
641 err
= got_pack_create(&packhash
, client
->pack_pipe
, delta_cache
,
642 have_ids
.ids
, have_ids
.nids
, want_ids
.ids
, want_ids
.nids
,
643 repo_read
.repo
, 0, 1, 0, pack_progress
, &pa
, &rl
,
644 check_cancelled
, NULL
);
648 if (log_getverbose() > 0 &&
649 got_hash_digest_to_str(packhash
.hash
, hex
, sizeof(hex
),
651 log_debug("sent pack-%s.pack", hex
);
653 if (gotd_imsg_compose_event(iev
, GOTD_IMSG_PACKFILE_DONE
,
654 PROC_REPO_READ
, -1, NULL
, 0) == -1)
655 err
= got_error_from_errno("imsg compose PACKFILE_DONE");
657 if (client
->delta_cache_fd
!= -1 &&
658 close(client
->delta_cache_fd
) == -1 && err
== NULL
)
659 err
= got_error_from_errno("close");
660 client
->delta_cache_fd
= -1;
661 if (delta_cache
!= NULL
&& fclose(delta_cache
) == EOF
&& err
== NULL
)
662 err
= got_error_from_errno("fclose");
670 repo_read_dispatch_session(int fd
, short event
, void *arg
)
672 const struct got_error
*err
= NULL
;
673 struct gotd_imsgev
*iev
= arg
;
674 struct imsgbuf
*ibuf
= &iev
->ibuf
;
678 struct repo_read_client
*client
= &repo_read_client
;
680 if (event
& EV_READ
) {
681 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
682 fatal("imsg_read error");
683 if (n
== 0) /* Connection closed. */
687 if (event
& EV_WRITE
) {
688 n
= msgbuf_write(&ibuf
->w
);
689 if (n
== -1 && errno
!= EAGAIN
)
690 fatal("msgbuf_write");
691 if (n
== 0) /* Connection closed. */
695 while (err
== NULL
&& check_cancelled(NULL
) == NULL
) {
696 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
697 fatal("%s: imsg_get", __func__
);
698 if (n
== 0) /* No more messages. */
701 if (imsg
.hdr
.type
!= GOTD_IMSG_LIST_REFS_INTERNAL
&&
702 !repo_read
.refs_listed
) {
703 err
= got_error(GOT_ERR_PRIVSEP_MSG
);
707 switch (imsg
.hdr
.type
) {
708 case GOTD_IMSG_LIST_REFS_INTERNAL
:
709 err
= list_refs(&imsg
);
711 log_warnx("ls-refs: %s", err
->msg
);
714 err
= recv_want(&imsg
);
716 log_warnx("want-line: %s", err
->msg
);
719 err
= recv_have(&imsg
);
721 log_warnx("have-line: %s", err
->msg
);
723 case GOTD_IMSG_SEND_PACKFILE
:
724 err
= receive_delta_cache_fd(&imsg
, iev
);
726 log_warnx("receiving delta cache: %s",
729 case GOTD_IMSG_PACKFILE_PIPE
:
730 err
= receive_pack_pipe(&imsg
, iev
);
732 log_warnx("receiving pack pipe: %s", err
->msg
);
735 err
= send_packfile(&imsg
, iev
);
737 log_warnx("sending packfile: %s", err
->msg
);
740 log_debug("unexpected imsg %d", imsg
.hdr
.type
);
747 if (!shut
&& check_cancelled(NULL
) == NULL
) {
749 gotd_imsg_send_error_event(iev
, PROC_REPO_READ
,
750 client
->id
, err
) == -1) {
751 log_warnx("could not send error to parent: %s",
754 gotd_imsg_event_add(iev
);
756 /* This pipe is dead. Remove its event handler */
758 event_loopexit(NULL
);
762 static const struct got_error
*
763 recv_connect(struct imsg
*imsg
)
765 struct gotd_imsgev
*iev
= &repo_read
.session_iev
;
768 datalen
= imsg
->hdr
.len
- IMSG_HEADER_SIZE
;
770 return got_error(GOT_ERR_PRIVSEP_LEN
);
772 if (repo_read
.session_fd
!= -1)
773 return got_error(GOT_ERR_PRIVSEP_MSG
);
775 repo_read
.session_fd
= imsg_get_fd(imsg
);
776 if (repo_read
.session_fd
== -1)
777 return got_error(GOT_ERR_PRIVSEP_NO_FD
);
779 imsg_init(&iev
->ibuf
, repo_read
.session_fd
);
780 iev
->handler
= repo_read_dispatch_session
;
781 iev
->events
= EV_READ
;
782 iev
->handler_arg
= NULL
;
783 event_set(&iev
->ev
, iev
->ibuf
.fd
, EV_READ
,
784 repo_read_dispatch_session
, iev
);
785 gotd_imsg_event_add(iev
);
791 repo_read_dispatch(int fd
, short event
, void *arg
)
793 const struct got_error
*err
= NULL
;
794 struct gotd_imsgev
*iev
= arg
;
795 struct imsgbuf
*ibuf
= &iev
->ibuf
;
799 struct repo_read_client
*client
= &repo_read_client
;
801 if (event
& EV_READ
) {
802 if ((n
= imsg_read(ibuf
)) == -1 && errno
!= EAGAIN
)
803 fatal("imsg_read error");
804 if (n
== 0) /* Connection closed. */
808 if (event
& EV_WRITE
) {
809 n
= msgbuf_write(&ibuf
->w
);
810 if (n
== -1 && errno
!= EAGAIN
)
811 fatal("msgbuf_write");
812 if (n
== 0) /* Connection closed. */
816 while (err
== NULL
&& check_cancelled(NULL
) == NULL
) {
817 if ((n
= imsg_get(ibuf
, &imsg
)) == -1)
818 fatal("%s: imsg_get", __func__
);
819 if (n
== 0) /* No more messages. */
822 switch (imsg
.hdr
.type
) {
823 case GOTD_IMSG_CONNECT_REPO_CHILD
:
824 err
= recv_connect(&imsg
);
827 log_debug("unexpected imsg %d", imsg
.hdr
.type
);
834 if (!shut
&& check_cancelled(NULL
) == NULL
) {
836 gotd_imsg_send_error_event(iev
, PROC_REPO_READ
,
837 client
->id
, err
) == -1) {
838 log_warnx("could not send error to parent: %s",
841 gotd_imsg_event_add(iev
);
843 /* This pipe is dead. Remove its event handler */
845 event_loopexit(NULL
);
850 repo_read_main(const char *title
, const char *repo_path
,
851 int *pack_fds
, int *temp_fds
)
853 const struct got_error
*err
= NULL
;
854 struct repo_read_client
*client
= &repo_read_client
;
855 struct gotd_imsgev iev
;
858 client
->delta_cache_fd
= -1;
859 client
->pack_pipe
= -1;
860 client
->have_ids
= got_object_idset_alloc();
861 if (client
->have_ids
== NULL
) {
862 err
= got_error_from_errno("got_object_idset_alloc");
865 client
->want_ids
= got_object_idset_alloc();
866 if (client
->want_ids
== NULL
) {
867 err
= got_error_from_errno("got_object_idset_alloc");
871 repo_read
.title
= title
;
872 repo_read
.pid
= getpid();
873 repo_read
.pack_fds
= pack_fds
;
874 repo_read
.temp_fds
= temp_fds
;
875 repo_read
.session_fd
= -1;
876 repo_read
.session_iev
.ibuf
.fd
= -1;
878 err
= got_repo_open(&repo_read
.repo
, repo_path
, NULL
, pack_fds
);
881 if (!got_repo_is_bare(repo_read
.repo
)) {
882 err
= got_error_msg(GOT_ERR_NOT_GIT_REPO
,
883 "bare git repository required");
886 if (got_repo_get_object_format(repo_read
.repo
) != GOT_HASH_SHA1
) {
887 err
= got_error_msg(GOT_ERR_NOT_IMPL
,
888 "sha256 object IDs unsupported in network protocol");
892 got_repo_temp_fds_set(repo_read
.repo
, temp_fds
);
894 signal(SIGINT
, catch_sigint
);
895 signal(SIGTERM
, catch_sigterm
);
896 signal(SIGPIPE
, SIG_IGN
);
897 signal(SIGHUP
, SIG_IGN
);
899 imsg_init(&iev
.ibuf
, GOTD_FILENO_MSG_PIPE
);
900 iev
.handler
= repo_read_dispatch
;
901 iev
.events
= EV_READ
;
902 iev
.handler_arg
= NULL
;
903 event_set(&iev
.ev
, iev
.ibuf
.fd
, EV_READ
, repo_read_dispatch
, &iev
);
905 if (gotd_imsg_compose_event(&iev
, GOTD_IMSG_REPO_CHILD_READY
,
906 PROC_REPO_READ
, -1, NULL
, 0) == -1) {
907 err
= got_error_from_errno("imsg compose REPO_CHILD_READY");
914 log_warnx("%s: %s", title
, err
->msg
);
915 repo_read_shutdown();
919 repo_read_shutdown(void)
921 struct repo_read_client
*client
= &repo_read_client
;
923 log_debug("%s: shutting down", repo_read
.title
);
925 if (client
->have_ids
)
926 got_object_idset_free(client
->have_ids
);
927 if (client
->want_ids
)
928 got_object_idset_free(client
->want_ids
);
929 if (client
->fd
!= -1)
931 if (client
->delta_cache_fd
!= -1)
932 close(client
->delta_cache_fd
);
933 if (client
->pack_pipe
!= -1)
934 close(client
->pack_pipe
);
937 got_repo_close(repo_read
.repo
);
938 got_repo_pack_fds_close(repo_read
.pack_fds
);
939 got_repo_temp_fds_close(repo_read
.temp_fds
);
940 if (repo_read
.session_fd
!= -1)
941 close(repo_read
.session_fd
);