2 13b2bc37 2022-10-23 stsp * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 13b2bc37 2022-10-23 stsp * Permission to use, copy, modify, and distribute this software for any
5 13b2bc37 2022-10-23 stsp * purpose with or without fee is hereby granted, provided that the above
6 13b2bc37 2022-10-23 stsp * copyright notice and this permission notice appear in all copies.
8 13b2bc37 2022-10-23 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 13b2bc37 2022-10-23 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 13b2bc37 2022-10-23 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 13b2bc37 2022-10-23 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 13b2bc37 2022-10-23 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 13b2bc37 2022-10-23 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 13b2bc37 2022-10-23 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 13b2bc37 2022-10-23 stsp #include <sys/queue.h>
18 13b2bc37 2022-10-23 stsp #include <sys/types.h>
20 13b2bc37 2022-10-23 stsp #include <event.h>
21 13b2bc37 2022-10-23 stsp #include <errno.h>
22 13b2bc37 2022-10-23 stsp #include <imsg.h>
23 13b2bc37 2022-10-23 stsp #include <signal.h>
24 13b2bc37 2022-10-23 stsp #include <stdlib.h>
25 13b2bc37 2022-10-23 stsp #include <limits.h>
26 13b2bc37 2022-10-23 stsp #include <poll.h>
27 13b2bc37 2022-10-23 stsp #include <sha1.h>
28 5822e79e 2023-02-23 op #include <sha2.h>
29 13b2bc37 2022-10-23 stsp #include <siphash.h>
30 13b2bc37 2022-10-23 stsp #include <stdio.h>
31 13b2bc37 2022-10-23 stsp #include <string.h>
32 13b2bc37 2022-10-23 stsp #include <unistd.h>
34 13b2bc37 2022-10-23 stsp #include "got_error.h"
35 13b2bc37 2022-10-23 stsp #include "got_cancel.h"
36 13b2bc37 2022-10-23 stsp #include "got_object.h"
37 13b2bc37 2022-10-23 stsp #include "got_repository.h"
38 13b2bc37 2022-10-23 stsp #include "got_reference.h"
39 13b2bc37 2022-10-23 stsp #include "got_repository_admin.h"
40 9afa3de2 2023-04-04 stsp #include "got_path.h"
42 13b2bc37 2022-10-23 stsp #include "got_lib_delta.h"
43 13b2bc37 2022-10-23 stsp #include "got_lib_object.h"
44 13b2bc37 2022-10-23 stsp #include "got_lib_object_idset.h"
45 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
46 13b2bc37 2022-10-23 stsp #include "got_lib_pack.h"
47 13b2bc37 2022-10-23 stsp #include "got_lib_ratelimit.h"
48 13b2bc37 2022-10-23 stsp #include "got_lib_pack_create.h"
49 13b2bc37 2022-10-23 stsp #include "got_lib_poll.h"
51 13b2bc37 2022-10-23 stsp #include "log.h"
52 13b2bc37 2022-10-23 stsp #include "gotd.h"
53 13b2bc37 2022-10-23 stsp #include "repo_read.h"
55 13b2bc37 2022-10-23 stsp #ifndef nitems
56 13b2bc37 2022-10-23 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 13b2bc37 2022-10-23 stsp static struct repo_read {
61 13b2bc37 2022-10-23 stsp const char *title;
62 13b2bc37 2022-10-23 stsp struct got_repository *repo;
63 13b2bc37 2022-10-23 stsp int *pack_fds;
64 13b2bc37 2022-10-23 stsp int *temp_fds;
65 ae7c1b78 2023-01-10 stsp int session_fd;
66 ae7c1b78 2023-01-10 stsp struct gotd_imsgev session_iev;
67 1cb49b67 2024-03-21 stsp int refs_listed;
68 13b2bc37 2022-10-23 stsp } repo_read;
70 1a52c9bf 2022-12-30 stsp static struct repo_read_client {
71 13b2bc37 2022-10-23 stsp uint32_t id;
73 13b2bc37 2022-10-23 stsp int delta_cache_fd;
74 13b2bc37 2022-10-23 stsp int report_progress;
75 86769de8 2022-10-28 stsp int pack_pipe;
76 4fb2bb7d 2023-02-27 stsp struct got_object_idset *want_ids;
77 4fb2bb7d 2023-02-27 stsp struct got_object_idset *have_ids;
78 1a52c9bf 2022-12-30 stsp } repo_read_client;
80 13b2bc37 2022-10-23 stsp static volatile sig_atomic_t sigint_received;
81 13b2bc37 2022-10-23 stsp static volatile sig_atomic_t sigterm_received;
84 13b2bc37 2022-10-23 stsp catch_sigint(int signo)
86 13b2bc37 2022-10-23 stsp sigint_received = 1;
90 13b2bc37 2022-10-23 stsp catch_sigterm(int signo)
92 13b2bc37 2022-10-23 stsp sigterm_received = 1;
95 13b2bc37 2022-10-23 stsp static const struct got_error *
96 13b2bc37 2022-10-23 stsp check_cancelled(void *arg)
98 13b2bc37 2022-10-23 stsp if (sigint_received || sigterm_received)
99 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_CANCELLED);
101 13b2bc37 2022-10-23 stsp return NULL;
104 13b2bc37 2022-10-23 stsp static const struct got_error *
105 ca7cfae0 2022-11-07 stsp send_symref(struct got_reference *symref, struct got_object_id *target_id,
106 ca7cfae0 2022-11-07 stsp struct imsgbuf *ibuf)
108 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
109 13b2bc37 2022-10-23 stsp struct gotd_imsg_symref isymref;
110 13b2bc37 2022-10-23 stsp const char *refname = got_ref_get_name(symref);
111 13b2bc37 2022-10-23 stsp const char *target = got_ref_get_symref_target(symref);
112 13b2bc37 2022-10-23 stsp size_t len;
113 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
115 13b2bc37 2022-10-23 stsp memset(&isymref, 0, sizeof(isymref));
116 13b2bc37 2022-10-23 stsp isymref.name_len = strlen(refname);
117 13b2bc37 2022-10-23 stsp isymref.target_len = strlen(target);
118 aaa34732 2024-07-13 op memcpy(isymref.target_id, target_id->hash, sizeof(isymref.target_id));
120 13b2bc37 2022-10-23 stsp len = sizeof(isymref) + isymref.name_len + isymref.target_len;
121 13b2bc37 2022-10-23 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
122 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_NO_SPACE);
126 13b2bc37 2022-10-23 stsp wbuf = imsg_create(ibuf, GOTD_IMSG_SYMREF, 0, 0, len);
127 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
128 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create SYMREF");
132 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &isymref, sizeof(isymref)) == -1) {
133 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add SYMREF");
136 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, refname, isymref.name_len) == -1) {
137 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add SYMREF");
140 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, target, isymref.target_len) == -1) {
141 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add SYMREF");
145 13b2bc37 2022-10-23 stsp imsg_close(ibuf, wbuf);
147 13b2bc37 2022-10-23 stsp free(target_id);
148 13b2bc37 2022-10-23 stsp return err;
151 13b2bc37 2022-10-23 stsp static const struct got_error *
152 13b2bc37 2022-10-23 stsp send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
153 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf)
155 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
156 13b2bc37 2022-10-23 stsp struct got_tag_object *tag;
157 13b2bc37 2022-10-23 stsp size_t namelen, len;
158 13b2bc37 2022-10-23 stsp char *peeled_refname = NULL;
159 13b2bc37 2022-10-23 stsp struct got_object_id *id;
160 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
162 13b2bc37 2022-10-23 stsp err = got_object_tag_open(&tag, repo_read.repo, obj);
164 13b2bc37 2022-10-23 stsp return err;
166 13b2bc37 2022-10-23 stsp if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
167 13b2bc37 2022-10-23 stsp err = got_error_from_errno("asprintf");
171 13b2bc37 2022-10-23 stsp id = got_object_tag_get_object_id(tag);
172 13b2bc37 2022-10-23 stsp namelen = strlen(peeled_refname);
174 13b2bc37 2022-10-23 stsp len = sizeof(struct gotd_imsg_ref) + namelen;
175 13b2bc37 2022-10-23 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
176 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_NO_SPACE);
180 13b2bc37 2022-10-23 stsp wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
181 13b2bc37 2022-10-23 stsp repo_read.pid, len);
182 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
183 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create MREF");
187 13b2bc37 2022-10-23 stsp /* Keep in sync with struct gotd_imsg_ref definition. */
188 aaa34732 2024-07-13 op if (imsg_add(wbuf, id->hash, SHA1_DIGEST_LENGTH) == -1) {
189 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add REF");
192 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
193 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add REF");
196 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
197 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add REF");
201 13b2bc37 2022-10-23 stsp imsg_close(ibuf, wbuf);
203 13b2bc37 2022-10-23 stsp got_object_tag_close(tag);
204 13b2bc37 2022-10-23 stsp return err;
207 13b2bc37 2022-10-23 stsp static const struct got_error *
208 13b2bc37 2022-10-23 stsp send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
210 13b2bc37 2022-10-23 stsp const struct got_error *err;
211 13b2bc37 2022-10-23 stsp const char *refname = got_ref_get_name(ref);
212 13b2bc37 2022-10-23 stsp size_t namelen;
213 13b2bc37 2022-10-23 stsp struct got_object_id *id = NULL;
214 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
215 13b2bc37 2022-10-23 stsp size_t len;
216 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
218 13b2bc37 2022-10-23 stsp namelen = strlen(refname);
220 13b2bc37 2022-10-23 stsp len = sizeof(struct gotd_imsg_ref) + namelen;
221 13b2bc37 2022-10-23 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
222 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
224 13b2bc37 2022-10-23 stsp err = got_ref_resolve(&id, repo_read.repo, ref);
226 13b2bc37 2022-10-23 stsp return err;
228 13b2bc37 2022-10-23 stsp wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
229 13b2bc37 2022-10-23 stsp repo_read.pid, len);
230 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
231 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create REF");
235 13b2bc37 2022-10-23 stsp /* Keep in sync with struct gotd_imsg_ref definition. */
236 aaa34732 2024-07-13 op if (imsg_add(wbuf, id->hash, SHA1_DIGEST_LENGTH) == -1)
237 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF");
238 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
239 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF");
240 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, refname, namelen) == -1)
241 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF");
243 13b2bc37 2022-10-23 stsp imsg_close(ibuf, wbuf);
245 13b2bc37 2022-10-23 stsp err = got_object_open(&obj, repo_read.repo, id);
248 13b2bc37 2022-10-23 stsp if (obj->type == GOT_OBJ_TYPE_TAG)
249 13b2bc37 2022-10-23 stsp err = send_peeled_tag_ref(ref, obj, ibuf);
252 13b2bc37 2022-10-23 stsp got_object_close(obj);
254 13b2bc37 2022-10-23 stsp return err;
257 13b2bc37 2022-10-23 stsp static const struct got_error *
258 1a52c9bf 2022-12-30 stsp list_refs(struct imsg *imsg)
260 13b2bc37 2022-10-23 stsp const struct got_error *err;
261 1a52c9bf 2022-12-30 stsp struct repo_read_client *client = &repo_read_client;
262 13b2bc37 2022-10-23 stsp struct got_reflist_head refs;
263 13b2bc37 2022-10-23 stsp struct got_reflist_entry *re;
264 13b2bc37 2022-10-23 stsp size_t datalen;
265 13b2bc37 2022-10-23 stsp struct gotd_imsg_reflist irefs;
266 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
267 2c52c623 2024-01-30 op int client_fd;
268 ca7cfae0 2022-11-07 stsp struct got_object_id *head_target_id = NULL;
270 13b2bc37 2022-10-23 stsp TAILQ_INIT(&refs);
272 2c52c623 2024-01-30 op client_fd = imsg_get_fd(imsg);
273 13b2bc37 2022-10-23 stsp if (client_fd == -1)
274 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
276 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
277 1cb49b67 2024-03-21 stsp if (datalen != 0)
278 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
280 1cb49b67 2024-03-21 stsp if (repo_read.refs_listed) {
281 1a52c9bf 2022-12-30 stsp return got_error_msg(GOT_ERR_CLIENT_ID,
282 1a52c9bf 2022-12-30 stsp "duplicate list-refs request");
284 1cb49b67 2024-03-21 stsp repo_read.refs_listed = 1;
286 1a52c9bf 2022-12-30 stsp client->fd = client_fd;
288 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, client_fd);
290 13b2bc37 2022-10-23 stsp err = got_ref_list(&refs, repo_read.repo, "",
291 13b2bc37 2022-10-23 stsp got_ref_cmp_by_name, NULL);
293 13b2bc37 2022-10-23 stsp return err;
295 13b2bc37 2022-10-23 stsp memset(&irefs, 0, sizeof(irefs));
296 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(re, &refs, entry) {
297 13b2bc37 2022-10-23 stsp struct got_object_id *id;
298 13b2bc37 2022-10-23 stsp int obj_type;
300 13b2bc37 2022-10-23 stsp if (got_ref_is_symbolic(re->ref)) {
301 13b2bc37 2022-10-23 stsp const char *refname = got_ref_get_name(re->ref);
302 ca7cfae0 2022-11-07 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
304 ca7cfae0 2022-11-07 stsp err = got_ref_resolve(&head_target_id, repo_read.repo,
307 ca7cfae0 2022-11-07 stsp if (err->code != GOT_ERR_NOT_REF)
308 ca7cfae0 2022-11-07 stsp return err;
310 ca7cfae0 2022-11-07 stsp * HEAD points to a non-existent branch.
311 ca7cfae0 2022-11-07 stsp * Do not advertise it.
312 ca7cfae0 2022-11-07 stsp * Matches git-daemon's behaviour.
314 ca7cfae0 2022-11-07 stsp head_target_id = NULL;
315 ca7cfae0 2022-11-07 stsp err = NULL;
317 13b2bc37 2022-10-23 stsp irefs.nrefs++;
321 13b2bc37 2022-10-23 stsp irefs.nrefs++;
323 13b2bc37 2022-10-23 stsp /* Account for a peeled tag refs. */
324 13b2bc37 2022-10-23 stsp err = got_ref_resolve(&id, repo_read.repo, re->ref);
327 e26970cc 2023-01-10 op err = got_object_get_type(&obj_type, repo_read.repo, id);
331 13b2bc37 2022-10-23 stsp if (obj_type == GOT_OBJ_TYPE_TAG)
332 13b2bc37 2022-10-23 stsp irefs.nrefs++;
335 13b2bc37 2022-10-23 stsp if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
336 13b2bc37 2022-10-23 stsp repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
337 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_compose REFLIST");
342 13b2bc37 2022-10-23 stsp * Send the HEAD symref first. In Git-protocol versions < 2
343 13b2bc37 2022-10-23 stsp * the HEAD symref must be announced on the initial line of
344 13b2bc37 2022-10-23 stsp * the server's ref advertisement.
345 13b2bc37 2022-10-23 stsp * For now, we do not advertise symrefs other than HEAD.
347 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(re, &refs, entry) {
348 13b2bc37 2022-10-23 stsp if (!got_ref_is_symbolic(re->ref) ||
349 ca7cfae0 2022-11-07 stsp strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0 ||
350 ca7cfae0 2022-11-07 stsp head_target_id == NULL)
352 ca7cfae0 2022-11-07 stsp err = send_symref(re->ref, head_target_id, &ibuf);
357 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(re, &refs, entry) {
358 13b2bc37 2022-10-23 stsp if (got_ref_is_symbolic(re->ref))
360 13b2bc37 2022-10-23 stsp err = send_ref(re->ref, &ibuf);
365 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(&ibuf);
367 13b2bc37 2022-10-23 stsp got_ref_list_free(&refs);
368 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
369 13b2bc37 2022-10-23 stsp return err;
372 13b2bc37 2022-10-23 stsp static const struct got_error *
373 4fb2bb7d 2023-02-27 stsp append_object_id(struct got_object_id *id, void *data, void *arg)
375 4fb2bb7d 2023-02-27 stsp struct gotd_object_id_array *array = arg;
376 13b2bc37 2022-10-23 stsp const size_t alloc_chunksz = 256;
378 13b2bc37 2022-10-23 stsp if (array->ids == NULL) {
379 13b2bc37 2022-10-23 stsp array->ids = reallocarray(NULL, alloc_chunksz,
380 13b2bc37 2022-10-23 stsp sizeof(*array->ids));
381 13b2bc37 2022-10-23 stsp if (array->ids == NULL)
382 13b2bc37 2022-10-23 stsp return got_error_from_errno("reallocarray");
383 13b2bc37 2022-10-23 stsp array->nalloc = alloc_chunksz;
384 13b2bc37 2022-10-23 stsp array->nids = 0;
385 13b2bc37 2022-10-23 stsp } else if (array->nalloc <= array->nids) {
386 13b2bc37 2022-10-23 stsp struct got_object_id **new;
387 13b2bc37 2022-10-23 stsp new = recallocarray(array->ids, array->nalloc,
388 13b2bc37 2022-10-23 stsp array->nalloc + alloc_chunksz, sizeof(*new));
389 13b2bc37 2022-10-23 stsp if (new == NULL)
390 13b2bc37 2022-10-23 stsp return got_error_from_errno("recallocarray");
391 13b2bc37 2022-10-23 stsp array->ids = new;
392 13b2bc37 2022-10-23 stsp array->nalloc += alloc_chunksz;
395 4fb2bb7d 2023-02-27 stsp array->ids[array->nids] = id;
396 13b2bc37 2022-10-23 stsp array->nids++;
397 13b2bc37 2022-10-23 stsp return NULL;
400 13b2bc37 2022-10-23 stsp static const struct got_error *
401 1a52c9bf 2022-12-30 stsp recv_want(struct imsg *imsg)
403 13b2bc37 2022-10-23 stsp const struct got_error *err;
404 1a52c9bf 2022-12-30 stsp struct repo_read_client *client = &repo_read_client;
405 13b2bc37 2022-10-23 stsp struct gotd_imsg_want iwant;
406 13b2bc37 2022-10-23 stsp size_t datalen;
407 13b2bc37 2022-10-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
408 13b2bc37 2022-10-23 stsp struct got_object_id id;
409 13b2bc37 2022-10-23 stsp int obj_type;
410 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
412 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
413 13b2bc37 2022-10-23 stsp if (datalen != sizeof(iwant))
414 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
415 13b2bc37 2022-10-23 stsp memcpy(&iwant, imsg->data, sizeof(iwant));
417 13b2bc37 2022-10-23 stsp memset(&id, 0, sizeof(id));
418 aaa34732 2024-07-13 op memcpy(id.hash, iwant.object_id, SHA1_DIGEST_LENGTH);
420 13b2bc37 2022-10-23 stsp if (log_getverbose() > 0 &&
421 310a8ea5 2024-07-10 op got_object_id_hex(&id, hex, sizeof(hex)))
422 13b2bc37 2022-10-23 stsp log_debug("client wants %s", hex);
424 1a52c9bf 2022-12-30 stsp imsg_init(&ibuf, client->fd);
426 13b2bc37 2022-10-23 stsp err = got_object_get_type(&obj_type, repo_read.repo, &id);
428 13b2bc37 2022-10-23 stsp return err;
430 13b2bc37 2022-10-23 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT &&
431 13b2bc37 2022-10-23 stsp obj_type != GOT_OBJ_TYPE_TAG)
432 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_OBJ_TYPE);
434 4fb2bb7d 2023-02-27 stsp if (!got_object_idset_contains(client->want_ids, &id)) {
435 4fb2bb7d 2023-02-27 stsp err = got_object_idset_add(client->want_ids, &id, NULL);
437 4fb2bb7d 2023-02-27 stsp return err;
440 13b2bc37 2022-10-23 stsp gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
441 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
442 13b2bc37 2022-10-23 stsp return err;
445 13b2bc37 2022-10-23 stsp static const struct got_error *
446 1a52c9bf 2022-12-30 stsp recv_have(struct imsg *imsg)
448 13b2bc37 2022-10-23 stsp const struct got_error *err;
449 1a52c9bf 2022-12-30 stsp struct repo_read_client *client = &repo_read_client;
450 13b2bc37 2022-10-23 stsp struct gotd_imsg_have ihave;
451 13b2bc37 2022-10-23 stsp size_t datalen;
452 13b2bc37 2022-10-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
453 13b2bc37 2022-10-23 stsp struct got_object_id id;
454 13b2bc37 2022-10-23 stsp int obj_type;
455 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
457 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
458 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ihave))
459 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
460 13b2bc37 2022-10-23 stsp memcpy(&ihave, imsg->data, sizeof(ihave));
462 13b2bc37 2022-10-23 stsp memset(&id, 0, sizeof(id));
463 aaa34732 2024-07-13 op memcpy(id.hash, ihave.object_id, SHA1_DIGEST_LENGTH);
465 13b2bc37 2022-10-23 stsp if (log_getverbose() > 0 &&
466 310a8ea5 2024-07-10 op got_object_id_hex(&id, hex, sizeof(hex)))
467 13b2bc37 2022-10-23 stsp log_debug("client has %s", hex);
469 1a52c9bf 2022-12-30 stsp imsg_init(&ibuf, client->fd);
471 13b2bc37 2022-10-23 stsp err = got_object_get_type(&obj_type, repo_read.repo, &id);
473 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_NO_OBJ) {
474 13b2bc37 2022-10-23 stsp gotd_imsg_send_nak(&id, &ibuf,
475 13b2bc37 2022-10-23 stsp PROC_REPO_READ, repo_read.pid);
476 13b2bc37 2022-10-23 stsp err = NULL;
481 13b2bc37 2022-10-23 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT &&
482 13b2bc37 2022-10-23 stsp obj_type != GOT_OBJ_TYPE_TAG) {
483 13b2bc37 2022-10-23 stsp gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
484 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
488 4fb2bb7d 2023-02-27 stsp if (!got_object_idset_contains(client->have_ids, &id)) {
489 4fb2bb7d 2023-02-27 stsp err = got_object_idset_add(client->have_ids, &id, NULL);
494 13b2bc37 2022-10-23 stsp gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
496 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
497 13b2bc37 2022-10-23 stsp return err;
500 13b2bc37 2022-10-23 stsp struct repo_read_pack_progress_arg {
501 13b2bc37 2022-10-23 stsp int report_progress;
502 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf;
503 13b2bc37 2022-10-23 stsp int sent_ready;
506 13b2bc37 2022-10-23 stsp static const struct got_error *
507 13b2bc37 2022-10-23 stsp pack_progress(void *arg, int ncolored, int nfound, int ntrees,
508 13b2bc37 2022-10-23 stsp off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
509 13b2bc37 2022-10-23 stsp int nobj_written)
511 13b2bc37 2022-10-23 stsp struct repo_read_pack_progress_arg *a = arg;
512 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_progress iprog;
515 13b2bc37 2022-10-23 stsp if (!a->report_progress)
516 13b2bc37 2022-10-23 stsp return NULL;
517 13b2bc37 2022-10-23 stsp if (packfile_size > 0 && a->sent_ready)
518 13b2bc37 2022-10-23 stsp return NULL;
520 13b2bc37 2022-10-23 stsp memset(&iprog, 0, sizeof(iprog));
521 13b2bc37 2022-10-23 stsp iprog.ncolored = ncolored;
522 13b2bc37 2022-10-23 stsp iprog.nfound = nfound;
523 13b2bc37 2022-10-23 stsp iprog.ntrees = ntrees;
524 13b2bc37 2022-10-23 stsp iprog.packfile_size = packfile_size;
525 13b2bc37 2022-10-23 stsp iprog.ncommits = ncommits;
526 13b2bc37 2022-10-23 stsp iprog.nobj_total = nobj_total;
527 13b2bc37 2022-10-23 stsp iprog.nobj_deltify = nobj_deltify;
528 13b2bc37 2022-10-23 stsp iprog.nobj_written = nobj_written;
530 13b2bc37 2022-10-23 stsp /* Using synchronous writes since we are blocking the event loop. */
531 13b2bc37 2022-10-23 stsp if (packfile_size == 0) {
532 13b2bc37 2022-10-23 stsp ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
533 13b2bc37 2022-10-23 stsp PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
534 13b2bc37 2022-10-23 stsp if (ret == -1) {
535 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg compose "
536 13b2bc37 2022-10-23 stsp "PACKFILE_PROGRESS");
539 13b2bc37 2022-10-23 stsp a->sent_ready = 1;
540 13b2bc37 2022-10-23 stsp ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
541 13b2bc37 2022-10-23 stsp PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
542 13b2bc37 2022-10-23 stsp if (ret == -1) {
543 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg compose "
544 13b2bc37 2022-10-23 stsp "PACKFILE_READY");
548 13b2bc37 2022-10-23 stsp return gotd_imsg_flush(a->ibuf);
551 13b2bc37 2022-10-23 stsp static const struct got_error *
552 1a52c9bf 2022-12-30 stsp receive_delta_cache_fd(struct imsg *imsg,
553 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev)
555 1a52c9bf 2022-12-30 stsp struct repo_read_client *client = &repo_read_client;
556 13b2bc37 2022-10-23 stsp struct gotd_imsg_send_packfile ireq;
557 13b2bc37 2022-10-23 stsp size_t datalen;
559 9cbac887 2023-04-20 stsp log_debug("receiving delta cache file");
561 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
562 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
563 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
564 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
566 1a52c9bf 2022-12-30 stsp if (client->delta_cache_fd != -1)
567 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
569 2c52c623 2024-01-30 op client->delta_cache_fd = imsg_get_fd(imsg);
570 2c52c623 2024-01-30 op if (client->delta_cache_fd == -1)
571 2c52c623 2024-01-30 op return got_error(GOT_ERR_PRIVSEP_NO_FD);
573 1a52c9bf 2022-12-30 stsp client->report_progress = ireq.report_progress;
574 13b2bc37 2022-10-23 stsp return NULL;
577 13b2bc37 2022-10-23 stsp static const struct got_error *
578 1a52c9bf 2022-12-30 stsp receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
580 1a52c9bf 2022-12-30 stsp struct repo_read_client *client = &repo_read_client;
581 13b2bc37 2022-10-23 stsp size_t datalen;
583 9cbac887 2023-04-20 stsp log_debug("receiving pack pipe descriptor");
585 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
586 1cb49b67 2024-03-21 stsp if (datalen != 0)
587 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
589 1a52c9bf 2022-12-30 stsp if (client->pack_pipe != -1)
590 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
592 2c52c623 2024-01-30 op client->pack_pipe = imsg_get_fd(imsg);
593 2c52c623 2024-01-30 op if (client->pack_pipe == -1)
594 2c52c623 2024-01-30 op return got_error(GOT_ERR_PRIVSEP_NO_FD);
596 13b2bc37 2022-10-23 stsp return NULL;
599 13b2bc37 2022-10-23 stsp static const struct got_error *
600 1a52c9bf 2022-12-30 stsp send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
602 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
603 1a52c9bf 2022-12-30 stsp struct repo_read_client *client = &repo_read_client;
604 394b0db7 2024-07-16 op struct got_object_id packhash;
605 394b0db7 2024-07-16 op char hex[GOT_HASH_DIGEST_STRING_MAXLEN];
606 13b2bc37 2022-10-23 stsp FILE *delta_cache = NULL;
607 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
608 13b2bc37 2022-10-23 stsp struct repo_read_pack_progress_arg pa;
609 13b2bc37 2022-10-23 stsp struct got_ratelimit rl;
610 4fb2bb7d 2023-02-27 stsp struct gotd_object_id_array want_ids;
611 4fb2bb7d 2023-02-27 stsp struct gotd_object_id_array have_ids;
613 13b2bc37 2022-10-23 stsp log_debug("packfile request received");
615 4fb2bb7d 2023-02-27 stsp memset(&want_ids, 0, sizeof(want_ids));
616 4fb2bb7d 2023-02-27 stsp memset(&have_ids, 0, sizeof(have_ids));
618 13b2bc37 2022-10-23 stsp got_ratelimit_init(&rl, 2, 0);
620 86769de8 2022-10-28 stsp if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
621 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
623 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, client->fd);
625 13b2bc37 2022-10-23 stsp delta_cache = fdopen(client->delta_cache_fd, "w+");
626 13b2bc37 2022-10-23 stsp if (delta_cache == NULL) {
627 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fdopen");
630 13b2bc37 2022-10-23 stsp client->delta_cache_fd = -1;
632 13b2bc37 2022-10-23 stsp memset(&pa, 0, sizeof(pa));
633 13b2bc37 2022-10-23 stsp pa.ibuf = &ibuf;
634 13b2bc37 2022-10-23 stsp pa.report_progress = client->report_progress;
636 4fb2bb7d 2023-02-27 stsp err = got_object_idset_for_each(client->want_ids,
637 4fb2bb7d 2023-02-27 stsp append_object_id, &want_ids);
640 4fb2bb7d 2023-02-27 stsp err = got_object_idset_for_each(client->have_ids,
641 4fb2bb7d 2023-02-27 stsp append_object_id, &have_ids);
645 394b0db7 2024-07-16 op err = got_pack_create(&packhash, client->pack_pipe, delta_cache,
646 4fb2bb7d 2023-02-27 stsp have_ids.ids, have_ids.nids, want_ids.ids, want_ids.nids,
647 98350b20 2023-02-17 op repo_read.repo, 0, 1, 0, pack_progress, &pa, &rl,
648 13b2bc37 2022-10-23 stsp check_cancelled, NULL);
652 13b2bc37 2022-10-23 stsp if (log_getverbose() > 0 &&
653 394b0db7 2024-07-16 op got_hash_digest_to_str(packhash.hash, hex, sizeof(hex),
654 394b0db7 2024-07-16 op packhash.algo))
655 13b2bc37 2022-10-23 stsp log_debug("sent pack-%s.pack", hex);
657 1cb49b67 2024-03-21 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
658 1cb49b67 2024-03-21 stsp PROC_REPO_READ, -1, NULL, 0) == -1)
659 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg compose PACKFILE_DONE");
661 fc9b745f 2024-01-30 op if (client->delta_cache_fd != -1 &&
662 fc9b745f 2024-01-30 op close(client->delta_cache_fd) == -1 && err == NULL)
663 fc9b745f 2024-01-30 op err = got_error_from_errno("close");
664 fc9b745f 2024-01-30 op client->delta_cache_fd = -1;
665 13b2bc37 2022-10-23 stsp if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
666 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fclose");
667 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
668 4fb2bb7d 2023-02-27 stsp free(want_ids.ids);
669 4fb2bb7d 2023-02-27 stsp free(have_ids.ids);
670 13b2bc37 2022-10-23 stsp return err;
673 13b2bc37 2022-10-23 stsp static void
674 ae7c1b78 2023-01-10 stsp repo_read_dispatch_session(int fd, short event, void *arg)
676 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
677 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
678 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
679 13b2bc37 2022-10-23 stsp struct imsg imsg;
681 13b2bc37 2022-10-23 stsp int shut = 0;
682 1a52c9bf 2022-12-30 stsp struct repo_read_client *client = &repo_read_client;
684 13b2bc37 2022-10-23 stsp if (event & EV_READ) {
685 13b2bc37 2022-10-23 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
686 13b2bc37 2022-10-23 stsp fatal("imsg_read error");
687 13b2bc37 2022-10-23 stsp if (n == 0) /* Connection closed. */
691 13b2bc37 2022-10-23 stsp if (event & EV_WRITE) {
692 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
693 13b2bc37 2022-10-23 stsp if (n == -1 && errno != EAGAIN)
694 13b2bc37 2022-10-23 stsp fatal("msgbuf_write");
695 13b2bc37 2022-10-23 stsp if (n == 0) /* Connection closed. */
699 13b2bc37 2022-10-23 stsp while (err == NULL && check_cancelled(NULL) == NULL) {
700 13b2bc37 2022-10-23 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
701 13b2bc37 2022-10-23 stsp fatal("%s: imsg_get", __func__);
702 13b2bc37 2022-10-23 stsp if (n == 0) /* No more messages. */
705 1a52c9bf 2022-12-30 stsp if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
706 1cb49b67 2024-03-21 stsp !repo_read.refs_listed) {
707 1a52c9bf 2022-12-30 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
711 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
712 13b2bc37 2022-10-23 stsp case GOTD_IMSG_LIST_REFS_INTERNAL:
713 1a52c9bf 2022-12-30 stsp err = list_refs(&imsg);
715 88f6dccd 2023-03-04 op log_warnx("ls-refs: %s", err->msg);
717 13b2bc37 2022-10-23 stsp case GOTD_IMSG_WANT:
718 1a52c9bf 2022-12-30 stsp err = recv_want(&imsg);
720 88f6dccd 2023-03-04 op log_warnx("want-line: %s", err->msg);
722 13b2bc37 2022-10-23 stsp case GOTD_IMSG_HAVE:
723 1a52c9bf 2022-12-30 stsp err = recv_have(&imsg);
725 88f6dccd 2023-03-04 op log_warnx("have-line: %s", err->msg);
727 13b2bc37 2022-10-23 stsp case GOTD_IMSG_SEND_PACKFILE:
728 1a52c9bf 2022-12-30 stsp err = receive_delta_cache_fd(&imsg, iev);
730 88f6dccd 2023-03-04 op log_warnx("receiving delta cache: %s",
733 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_PIPE:
734 1a52c9bf 2022-12-30 stsp err = receive_pack_pipe(&imsg, iev);
736 88f6dccd 2023-03-04 op log_warnx("receiving pack pipe: %s", err->msg);
739 1a52c9bf 2022-12-30 stsp err = send_packfile(&imsg, iev);
741 88f6dccd 2023-03-04 op log_warnx("sending packfile: %s", err->msg);
744 88f6dccd 2023-03-04 op log_debug("unexpected imsg %d", imsg.hdr.type);
748 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
751 ae7c1b78 2023-01-10 stsp if (!shut && check_cancelled(NULL) == NULL) {
753 ae7c1b78 2023-01-10 stsp gotd_imsg_send_error_event(iev, PROC_REPO_READ,
754 ae7c1b78 2023-01-10 stsp client->id, err) == -1) {
755 ae7c1b78 2023-01-10 stsp log_warnx("could not send error to parent: %s",
758 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
760 ae7c1b78 2023-01-10 stsp /* This pipe is dead. Remove its event handler */
761 ae7c1b78 2023-01-10 stsp event_del(&iev->ev);
762 ae7c1b78 2023-01-10 stsp event_loopexit(NULL);
766 ae7c1b78 2023-01-10 stsp static const struct got_error *
767 ae7c1b78 2023-01-10 stsp recv_connect(struct imsg *imsg)
769 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = &repo_read.session_iev;
770 ae7c1b78 2023-01-10 stsp size_t datalen;
772 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
773 ae7c1b78 2023-01-10 stsp if (datalen != 0)
774 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
776 ae7c1b78 2023-01-10 stsp if (repo_read.session_fd != -1)
777 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
779 2c52c623 2024-01-30 op repo_read.session_fd = imsg_get_fd(imsg);
780 2c52c623 2024-01-30 op if (repo_read.session_fd == -1)
781 2c52c623 2024-01-30 op return got_error(GOT_ERR_PRIVSEP_NO_FD);
783 ae7c1b78 2023-01-10 stsp imsg_init(&iev->ibuf, repo_read.session_fd);
784 ae7c1b78 2023-01-10 stsp iev->handler = repo_read_dispatch_session;
785 ae7c1b78 2023-01-10 stsp iev->events = EV_READ;
786 ae7c1b78 2023-01-10 stsp iev->handler_arg = NULL;
787 ae7c1b78 2023-01-10 stsp event_set(&iev->ev, iev->ibuf.fd, EV_READ,
788 ae7c1b78 2023-01-10 stsp repo_read_dispatch_session, iev);
789 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
791 ae7c1b78 2023-01-10 stsp return NULL;
794 ae7c1b78 2023-01-10 stsp static void
795 ae7c1b78 2023-01-10 stsp repo_read_dispatch(int fd, short event, void *arg)
797 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
798 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = arg;
799 ae7c1b78 2023-01-10 stsp struct imsgbuf *ibuf = &iev->ibuf;
800 ae7c1b78 2023-01-10 stsp struct imsg imsg;
802 ae7c1b78 2023-01-10 stsp int shut = 0;
803 ae7c1b78 2023-01-10 stsp struct repo_read_client *client = &repo_read_client;
805 ae7c1b78 2023-01-10 stsp if (event & EV_READ) {
806 ae7c1b78 2023-01-10 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
807 ae7c1b78 2023-01-10 stsp fatal("imsg_read error");
808 ae7c1b78 2023-01-10 stsp if (n == 0) /* Connection closed. */
812 ae7c1b78 2023-01-10 stsp if (event & EV_WRITE) {
813 ae7c1b78 2023-01-10 stsp n = msgbuf_write(&ibuf->w);
814 ae7c1b78 2023-01-10 stsp if (n == -1 && errno != EAGAIN)
815 ae7c1b78 2023-01-10 stsp fatal("msgbuf_write");
816 ae7c1b78 2023-01-10 stsp if (n == 0) /* Connection closed. */
820 ae7c1b78 2023-01-10 stsp while (err == NULL && check_cancelled(NULL) == NULL) {
821 ae7c1b78 2023-01-10 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
822 ae7c1b78 2023-01-10 stsp fatal("%s: imsg_get", __func__);
823 ae7c1b78 2023-01-10 stsp if (n == 0) /* No more messages. */
826 ae7c1b78 2023-01-10 stsp switch (imsg.hdr.type) {
827 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CONNECT_REPO_CHILD:
828 ae7c1b78 2023-01-10 stsp err = recv_connect(&imsg);
831 88f6dccd 2023-03-04 op log_debug("unexpected imsg %d", imsg.hdr.type);
835 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
838 13b2bc37 2022-10-23 stsp if (!shut && check_cancelled(NULL) == NULL) {
840 13b2bc37 2022-10-23 stsp gotd_imsg_send_error_event(iev, PROC_REPO_READ,
841 1a52c9bf 2022-12-30 stsp client->id, err) == -1) {
842 13b2bc37 2022-10-23 stsp log_warnx("could not send error to parent: %s",
845 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
847 13b2bc37 2022-10-23 stsp /* This pipe is dead. Remove its event handler */
848 13b2bc37 2022-10-23 stsp event_del(&iev->ev);
849 13b2bc37 2022-10-23 stsp event_loopexit(NULL);
854 eec68231 2022-12-14 stsp repo_read_main(const char *title, const char *repo_path,
855 eec68231 2022-12-14 stsp int *pack_fds, int *temp_fds)
857 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
858 363c6230 2023-02-09 stsp struct repo_read_client *client = &repo_read_client;
859 13b2bc37 2022-10-23 stsp struct gotd_imsgev iev;
861 363c6230 2023-02-09 stsp client->fd = -1;
862 363c6230 2023-02-09 stsp client->delta_cache_fd = -1;
863 363c6230 2023-02-09 stsp client->pack_pipe = -1;
864 4fb2bb7d 2023-02-27 stsp client->have_ids = got_object_idset_alloc();
865 4fb2bb7d 2023-02-27 stsp if (client->have_ids == NULL) {
866 4fb2bb7d 2023-02-27 stsp err = got_error_from_errno("got_object_idset_alloc");
869 4fb2bb7d 2023-02-27 stsp client->want_ids = got_object_idset_alloc();
870 4fb2bb7d 2023-02-27 stsp if (client->want_ids == NULL) {
871 4fb2bb7d 2023-02-27 stsp err = got_error_from_errno("got_object_idset_alloc");
875 13b2bc37 2022-10-23 stsp repo_read.title = title;
876 13b2bc37 2022-10-23 stsp repo_read.pid = getpid();
877 13b2bc37 2022-10-23 stsp repo_read.pack_fds = pack_fds;
878 13b2bc37 2022-10-23 stsp repo_read.temp_fds = temp_fds;
879 ae7c1b78 2023-01-10 stsp repo_read.session_fd = -1;
880 ae7c1b78 2023-01-10 stsp repo_read.session_iev.ibuf.fd = -1;
882 eec68231 2022-12-14 stsp err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
885 13b2bc37 2022-10-23 stsp if (!got_repo_is_bare(repo_read.repo)) {
886 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
887 13b2bc37 2022-10-23 stsp "bare git repository required");
890 c214ca4f 2024-08-01 op if (got_repo_get_object_format(repo_read.repo) != GOT_HASH_SHA1) {
891 c214ca4f 2024-08-01 op err = got_error_msg(GOT_ERR_NOT_IMPL,
892 c214ca4f 2024-08-01 op "sha256 object IDs unsupported in network protocol");
896 13b2bc37 2022-10-23 stsp got_repo_temp_fds_set(repo_read.repo, temp_fds);
898 13b2bc37 2022-10-23 stsp signal(SIGINT, catch_sigint);
899 13b2bc37 2022-10-23 stsp signal(SIGTERM, catch_sigterm);
900 13b2bc37 2022-10-23 stsp signal(SIGPIPE, SIG_IGN);
901 13b2bc37 2022-10-23 stsp signal(SIGHUP, SIG_IGN);
903 8c6fc146 2022-11-17 stsp imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
904 13b2bc37 2022-10-23 stsp iev.handler = repo_read_dispatch;
905 13b2bc37 2022-10-23 stsp iev.events = EV_READ;
906 13b2bc37 2022-10-23 stsp iev.handler_arg = NULL;
907 13b2bc37 2022-10-23 stsp event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
909 b50a2b46 2022-12-29 stsp if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
910 b50a2b46 2022-12-29 stsp PROC_REPO_READ, -1, NULL, 0) == -1) {
911 b50a2b46 2022-12-29 stsp err = got_error_from_errno("imsg compose REPO_CHILD_READY");
915 13b2bc37 2022-10-23 stsp event_dispatch();
918 13b2bc37 2022-10-23 stsp log_warnx("%s: %s", title, err->msg);
919 13b2bc37 2022-10-23 stsp repo_read_shutdown();
923 13b2bc37 2022-10-23 stsp repo_read_shutdown(void)
925 363c6230 2023-02-09 stsp struct repo_read_client *client = &repo_read_client;
927 e8d451cc 2024-03-22 stsp log_debug("%s: shutting down", repo_read.title);
929 4fb2bb7d 2023-02-27 stsp if (client->have_ids)
930 4fb2bb7d 2023-02-27 stsp got_object_idset_free(client->have_ids);
931 4fb2bb7d 2023-02-27 stsp if (client->want_ids)
932 4fb2bb7d 2023-02-27 stsp got_object_idset_free(client->want_ids);
933 363c6230 2023-02-09 stsp if (client->fd != -1)
934 363c6230 2023-02-09 stsp close(client->fd);
935 363c6230 2023-02-09 stsp if (client->delta_cache_fd != -1)
936 363c6230 2023-02-09 stsp close(client->delta_cache_fd);
937 363c6230 2023-02-09 stsp if (client->pack_pipe != -1)
938 363c6230 2023-02-09 stsp close(client->pack_pipe);
940 13b2bc37 2022-10-23 stsp if (repo_read.repo)
941 13b2bc37 2022-10-23 stsp got_repo_close(repo_read.repo);
942 13b2bc37 2022-10-23 stsp got_repo_pack_fds_close(repo_read.pack_fds);
943 13b2bc37 2022-10-23 stsp got_repo_temp_fds_close(repo_read.temp_fds);
944 ae7c1b78 2023-01-10 stsp if (repo_read.session_fd != -1)
945 ae7c1b78 2023-01-10 stsp close(repo_read.session_fd);