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 13b2bc37 2022-10-23 stsp #include <siphash.h>
29 13b2bc37 2022-10-23 stsp #include <stdio.h>
30 13b2bc37 2022-10-23 stsp #include <string.h>
31 13b2bc37 2022-10-23 stsp #include <unistd.h>
33 13b2bc37 2022-10-23 stsp #include "got_error.h"
34 13b2bc37 2022-10-23 stsp #include "got_cancel.h"
35 13b2bc37 2022-10-23 stsp #include "got_object.h"
36 13b2bc37 2022-10-23 stsp #include "got_repository.h"
37 13b2bc37 2022-10-23 stsp #include "got_reference.h"
38 13b2bc37 2022-10-23 stsp #include "got_repository_admin.h"
40 13b2bc37 2022-10-23 stsp #include "got_lib_delta.h"
41 13b2bc37 2022-10-23 stsp #include "got_lib_object.h"
42 13b2bc37 2022-10-23 stsp #include "got_lib_object_idset.h"
43 13b2bc37 2022-10-23 stsp #include "got_lib_sha1.h"
44 13b2bc37 2022-10-23 stsp #include "got_lib_pack.h"
45 13b2bc37 2022-10-23 stsp #include "got_lib_ratelimit.h"
46 13b2bc37 2022-10-23 stsp #include "got_lib_pack_create.h"
47 13b2bc37 2022-10-23 stsp #include "got_lib_poll.h"
49 13b2bc37 2022-10-23 stsp #include "log.h"
50 13b2bc37 2022-10-23 stsp #include "gotd.h"
51 13b2bc37 2022-10-23 stsp #include "repo_read.h"
53 13b2bc37 2022-10-23 stsp #ifndef nitems
54 13b2bc37 2022-10-23 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 13b2bc37 2022-10-23 stsp static struct repo_read {
59 13b2bc37 2022-10-23 stsp const char *title;
60 13b2bc37 2022-10-23 stsp struct got_repository *repo;
61 13b2bc37 2022-10-23 stsp int *pack_fds;
62 13b2bc37 2022-10-23 stsp int *temp_fds;
63 ae7c1b78 2023-01-10 stsp int session_fd;
64 ae7c1b78 2023-01-10 stsp struct gotd_imsgev session_iev;
65 13b2bc37 2022-10-23 stsp } repo_read;
67 1a52c9bf 2022-12-30 stsp static struct repo_read_client {
68 13b2bc37 2022-10-23 stsp uint32_t id;
70 13b2bc37 2022-10-23 stsp int delta_cache_fd;
71 13b2bc37 2022-10-23 stsp int report_progress;
72 86769de8 2022-10-28 stsp int pack_pipe;
73 13b2bc37 2022-10-23 stsp struct gotd_object_id_array want_ids;
74 13b2bc37 2022-10-23 stsp struct gotd_object_id_array have_ids;
75 1a52c9bf 2022-12-30 stsp } repo_read_client;
77 13b2bc37 2022-10-23 stsp static volatile sig_atomic_t sigint_received;
78 13b2bc37 2022-10-23 stsp static volatile sig_atomic_t sigterm_received;
81 13b2bc37 2022-10-23 stsp catch_sigint(int signo)
83 13b2bc37 2022-10-23 stsp sigint_received = 1;
87 13b2bc37 2022-10-23 stsp catch_sigterm(int signo)
89 13b2bc37 2022-10-23 stsp sigterm_received = 1;
92 13b2bc37 2022-10-23 stsp static const struct got_error *
93 13b2bc37 2022-10-23 stsp check_cancelled(void *arg)
95 13b2bc37 2022-10-23 stsp if (sigint_received || sigterm_received)
96 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_CANCELLED);
98 13b2bc37 2022-10-23 stsp return NULL;
101 13b2bc37 2022-10-23 stsp static const struct got_error *
102 ca7cfae0 2022-11-07 stsp send_symref(struct got_reference *symref, struct got_object_id *target_id,
103 ca7cfae0 2022-11-07 stsp struct imsgbuf *ibuf)
105 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
106 13b2bc37 2022-10-23 stsp struct gotd_imsg_symref isymref;
107 13b2bc37 2022-10-23 stsp const char *refname = got_ref_get_name(symref);
108 13b2bc37 2022-10-23 stsp const char *target = got_ref_get_symref_target(symref);
109 13b2bc37 2022-10-23 stsp size_t len;
110 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
112 13b2bc37 2022-10-23 stsp memset(&isymref, 0, sizeof(isymref));
113 13b2bc37 2022-10-23 stsp isymref.name_len = strlen(refname);
114 13b2bc37 2022-10-23 stsp isymref.target_len = strlen(target);
115 13b2bc37 2022-10-23 stsp memcpy(isymref.target_id, target_id->sha1, sizeof(isymref.target_id));
117 13b2bc37 2022-10-23 stsp len = sizeof(isymref) + isymref.name_len + isymref.target_len;
118 13b2bc37 2022-10-23 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
119 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_NO_SPACE);
123 13b2bc37 2022-10-23 stsp wbuf = imsg_create(ibuf, GOTD_IMSG_SYMREF, 0, 0, len);
124 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
125 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create SYMREF");
129 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &isymref, sizeof(isymref)) == -1) {
130 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add SYMREF");
133 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, refname, isymref.name_len) == -1) {
134 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add SYMREF");
137 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, target, isymref.target_len) == -1) {
138 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add SYMREF");
142 13b2bc37 2022-10-23 stsp wbuf->fd = -1;
143 13b2bc37 2022-10-23 stsp imsg_close(ibuf, wbuf);
145 13b2bc37 2022-10-23 stsp free(target_id);
146 13b2bc37 2022-10-23 stsp return err;
149 13b2bc37 2022-10-23 stsp static const struct got_error *
150 13b2bc37 2022-10-23 stsp send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
151 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf)
153 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
154 13b2bc37 2022-10-23 stsp struct got_tag_object *tag;
155 13b2bc37 2022-10-23 stsp size_t namelen, len;
156 13b2bc37 2022-10-23 stsp char *peeled_refname = NULL;
157 13b2bc37 2022-10-23 stsp struct got_object_id *id;
158 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
160 13b2bc37 2022-10-23 stsp err = got_object_tag_open(&tag, repo_read.repo, obj);
162 13b2bc37 2022-10-23 stsp return err;
164 13b2bc37 2022-10-23 stsp if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
165 13b2bc37 2022-10-23 stsp err = got_error_from_errno("asprintf");
169 13b2bc37 2022-10-23 stsp id = got_object_tag_get_object_id(tag);
170 13b2bc37 2022-10-23 stsp namelen = strlen(peeled_refname);
172 13b2bc37 2022-10-23 stsp len = sizeof(struct gotd_imsg_ref) + namelen;
173 13b2bc37 2022-10-23 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
174 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_NO_SPACE);
178 13b2bc37 2022-10-23 stsp wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
179 13b2bc37 2022-10-23 stsp repo_read.pid, len);
180 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
181 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create MREF");
185 13b2bc37 2022-10-23 stsp /* Keep in sync with struct gotd_imsg_ref definition. */
186 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
187 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add REF");
190 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
191 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add REF");
194 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
195 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add REF");
199 13b2bc37 2022-10-23 stsp wbuf->fd = -1;
200 13b2bc37 2022-10-23 stsp imsg_close(ibuf, wbuf);
202 13b2bc37 2022-10-23 stsp got_object_tag_close(tag);
203 13b2bc37 2022-10-23 stsp return err;
206 13b2bc37 2022-10-23 stsp static const struct got_error *
207 13b2bc37 2022-10-23 stsp send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
209 13b2bc37 2022-10-23 stsp const struct got_error *err;
210 13b2bc37 2022-10-23 stsp const char *refname = got_ref_get_name(ref);
211 13b2bc37 2022-10-23 stsp size_t namelen;
212 13b2bc37 2022-10-23 stsp struct got_object_id *id = NULL;
213 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
214 13b2bc37 2022-10-23 stsp size_t len;
215 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
217 13b2bc37 2022-10-23 stsp namelen = strlen(refname);
219 13b2bc37 2022-10-23 stsp len = sizeof(struct gotd_imsg_ref) + namelen;
220 13b2bc37 2022-10-23 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
221 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
223 13b2bc37 2022-10-23 stsp err = got_ref_resolve(&id, repo_read.repo, ref);
225 13b2bc37 2022-10-23 stsp return err;
227 13b2bc37 2022-10-23 stsp wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
228 13b2bc37 2022-10-23 stsp repo_read.pid, len);
229 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
230 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create REF");
234 13b2bc37 2022-10-23 stsp /* Keep in sync with struct gotd_imsg_ref definition. */
235 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
236 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF");
237 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
238 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF");
239 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, refname, namelen) == -1)
240 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF");
242 13b2bc37 2022-10-23 stsp wbuf->fd = -1;
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 struct gotd_imsg_list_refs_internal ireq;
265 13b2bc37 2022-10-23 stsp size_t datalen;
266 13b2bc37 2022-10-23 stsp struct gotd_imsg_reflist irefs;
267 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
268 13b2bc37 2022-10-23 stsp int client_fd = imsg->fd;
269 ca7cfae0 2022-11-07 stsp struct got_object_id *head_target_id = NULL;
271 13b2bc37 2022-10-23 stsp TAILQ_INIT(&refs);
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 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
278 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
279 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
281 1a52c9bf 2022-12-30 stsp if (ireq.client_id == 0)
282 1a52c9bf 2022-12-30 stsp return got_error(GOT_ERR_CLIENT_ID);
283 1a52c9bf 2022-12-30 stsp if (client->id != 0) {
284 1a52c9bf 2022-12-30 stsp return got_error_msg(GOT_ERR_CLIENT_ID,
285 1a52c9bf 2022-12-30 stsp "duplicate list-refs request");
287 1a52c9bf 2022-12-30 stsp client->id = ireq.client_id;
288 1a52c9bf 2022-12-30 stsp client->fd = client_fd;
290 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, client_fd);
292 13b2bc37 2022-10-23 stsp err = got_ref_list(&refs, repo_read.repo, "",
293 13b2bc37 2022-10-23 stsp got_ref_cmp_by_name, NULL);
295 13b2bc37 2022-10-23 stsp return err;
297 13b2bc37 2022-10-23 stsp memset(&irefs, 0, sizeof(irefs));
298 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(re, &refs, entry) {
299 13b2bc37 2022-10-23 stsp struct got_object_id *id;
300 13b2bc37 2022-10-23 stsp int obj_type;
302 13b2bc37 2022-10-23 stsp if (got_ref_is_symbolic(re->ref)) {
303 13b2bc37 2022-10-23 stsp const char *refname = got_ref_get_name(re->ref);
304 ca7cfae0 2022-11-07 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
306 ca7cfae0 2022-11-07 stsp err = got_ref_resolve(&head_target_id, repo_read.repo,
309 ca7cfae0 2022-11-07 stsp if (err->code != GOT_ERR_NOT_REF)
310 ca7cfae0 2022-11-07 stsp return err;
312 ca7cfae0 2022-11-07 stsp * HEAD points to a non-existent branch.
313 ca7cfae0 2022-11-07 stsp * Do not advertise it.
314 ca7cfae0 2022-11-07 stsp * Matches git-daemon's behaviour.
316 ca7cfae0 2022-11-07 stsp head_target_id = NULL;
317 ca7cfae0 2022-11-07 stsp err = NULL;
319 13b2bc37 2022-10-23 stsp irefs.nrefs++;
323 13b2bc37 2022-10-23 stsp irefs.nrefs++;
325 13b2bc37 2022-10-23 stsp /* Account for a peeled tag refs. */
326 13b2bc37 2022-10-23 stsp err = got_ref_resolve(&id, repo_read.repo, re->ref);
329 e26970cc 2023-01-10 op err = got_object_get_type(&obj_type, repo_read.repo, id);
333 13b2bc37 2022-10-23 stsp if (obj_type == GOT_OBJ_TYPE_TAG)
334 13b2bc37 2022-10-23 stsp irefs.nrefs++;
337 13b2bc37 2022-10-23 stsp if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
338 13b2bc37 2022-10-23 stsp repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
339 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_compose REFLIST");
344 13b2bc37 2022-10-23 stsp * Send the HEAD symref first. In Git-protocol versions < 2
345 13b2bc37 2022-10-23 stsp * the HEAD symref must be announced on the initial line of
346 13b2bc37 2022-10-23 stsp * the server's ref advertisement.
347 13b2bc37 2022-10-23 stsp * For now, we do not advertise symrefs other than HEAD.
349 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(re, &refs, entry) {
350 13b2bc37 2022-10-23 stsp if (!got_ref_is_symbolic(re->ref) ||
351 ca7cfae0 2022-11-07 stsp strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0 ||
352 ca7cfae0 2022-11-07 stsp head_target_id == NULL)
354 ca7cfae0 2022-11-07 stsp err = send_symref(re->ref, head_target_id, &ibuf);
359 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(re, &refs, entry) {
360 13b2bc37 2022-10-23 stsp if (got_ref_is_symbolic(re->ref))
362 13b2bc37 2022-10-23 stsp err = send_ref(re->ref, &ibuf);
367 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(&ibuf);
369 13b2bc37 2022-10-23 stsp got_ref_list_free(&refs);
370 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
371 13b2bc37 2022-10-23 stsp return err;
374 13b2bc37 2022-10-23 stsp static const struct got_error *
375 13b2bc37 2022-10-23 stsp record_object_id(struct gotd_object_id_array *array, struct got_object_id *id)
377 13b2bc37 2022-10-23 stsp const size_t alloc_chunksz = 256;
379 13b2bc37 2022-10-23 stsp if (array->ids == NULL) {
380 13b2bc37 2022-10-23 stsp array->ids = reallocarray(NULL, alloc_chunksz,
381 13b2bc37 2022-10-23 stsp sizeof(*array->ids));
382 13b2bc37 2022-10-23 stsp if (array->ids == NULL)
383 13b2bc37 2022-10-23 stsp return got_error_from_errno("reallocarray");
384 13b2bc37 2022-10-23 stsp array->nalloc = alloc_chunksz;
385 13b2bc37 2022-10-23 stsp array->nids = 0;
386 13b2bc37 2022-10-23 stsp } else if (array->nalloc <= array->nids) {
387 13b2bc37 2022-10-23 stsp struct got_object_id **new;
388 13b2bc37 2022-10-23 stsp new = recallocarray(array->ids, array->nalloc,
389 13b2bc37 2022-10-23 stsp array->nalloc + alloc_chunksz, sizeof(*new));
390 13b2bc37 2022-10-23 stsp if (new == NULL)
391 13b2bc37 2022-10-23 stsp return got_error_from_errno("recallocarray");
392 13b2bc37 2022-10-23 stsp array->ids = new;
393 13b2bc37 2022-10-23 stsp array->nalloc += alloc_chunksz;
396 13b2bc37 2022-10-23 stsp array->ids[array->nids] = got_object_id_dup(id);
397 13b2bc37 2022-10-23 stsp if (array->ids[array->nids] == NULL)
398 13b2bc37 2022-10-23 stsp return got_error_from_errno("got_object_id_dup");
399 13b2bc37 2022-10-23 stsp array->nids++;
400 13b2bc37 2022-10-23 stsp return NULL;
403 13b2bc37 2022-10-23 stsp static void
404 13b2bc37 2022-10-23 stsp free_object_ids(struct gotd_object_id_array *array)
408 13b2bc37 2022-10-23 stsp for (i = 0; i < array->nids; i++)
409 13b2bc37 2022-10-23 stsp free(array->ids[i]);
410 13b2bc37 2022-10-23 stsp free(array->ids);
412 13b2bc37 2022-10-23 stsp array->ids = NULL;
413 13b2bc37 2022-10-23 stsp array->nalloc = 0;
414 13b2bc37 2022-10-23 stsp array->nids = 0;
417 13b2bc37 2022-10-23 stsp static const struct got_error *
418 1a52c9bf 2022-12-30 stsp recv_want(struct imsg *imsg)
420 13b2bc37 2022-10-23 stsp const struct got_error *err;
421 1a52c9bf 2022-12-30 stsp struct repo_read_client *client = &repo_read_client;
422 13b2bc37 2022-10-23 stsp struct gotd_imsg_want iwant;
423 13b2bc37 2022-10-23 stsp size_t datalen;
424 13b2bc37 2022-10-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
425 13b2bc37 2022-10-23 stsp struct got_object_id id;
426 13b2bc37 2022-10-23 stsp int obj_type;
427 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
429 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
430 13b2bc37 2022-10-23 stsp if (datalen != sizeof(iwant))
431 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
432 13b2bc37 2022-10-23 stsp memcpy(&iwant, imsg->data, sizeof(iwant));
434 13b2bc37 2022-10-23 stsp memset(&id, 0, sizeof(id));
435 13b2bc37 2022-10-23 stsp memcpy(id.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
437 13b2bc37 2022-10-23 stsp if (log_getverbose() > 0 &&
438 13b2bc37 2022-10-23 stsp got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
439 13b2bc37 2022-10-23 stsp log_debug("client wants %s", hex);
441 1a52c9bf 2022-12-30 stsp imsg_init(&ibuf, client->fd);
443 13b2bc37 2022-10-23 stsp err = got_object_get_type(&obj_type, repo_read.repo, &id);
445 13b2bc37 2022-10-23 stsp return err;
447 13b2bc37 2022-10-23 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT &&
448 13b2bc37 2022-10-23 stsp obj_type != GOT_OBJ_TYPE_TAG)
449 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_OBJ_TYPE);
451 1a52c9bf 2022-12-30 stsp err = record_object_id(&client->want_ids, &id);
453 13b2bc37 2022-10-23 stsp return err;
455 13b2bc37 2022-10-23 stsp gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
456 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
457 13b2bc37 2022-10-23 stsp return err;
460 13b2bc37 2022-10-23 stsp static const struct got_error *
461 1a52c9bf 2022-12-30 stsp recv_have(struct imsg *imsg)
463 13b2bc37 2022-10-23 stsp const struct got_error *err;
464 1a52c9bf 2022-12-30 stsp struct repo_read_client *client = &repo_read_client;
465 13b2bc37 2022-10-23 stsp struct gotd_imsg_have ihave;
466 13b2bc37 2022-10-23 stsp size_t datalen;
467 13b2bc37 2022-10-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
468 13b2bc37 2022-10-23 stsp struct got_object_id id;
469 13b2bc37 2022-10-23 stsp int obj_type;
470 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
472 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
473 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ihave))
474 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
475 13b2bc37 2022-10-23 stsp memcpy(&ihave, imsg->data, sizeof(ihave));
477 13b2bc37 2022-10-23 stsp memset(&id, 0, sizeof(id));
478 13b2bc37 2022-10-23 stsp memcpy(id.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
480 13b2bc37 2022-10-23 stsp if (log_getverbose() > 0 &&
481 13b2bc37 2022-10-23 stsp got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
482 13b2bc37 2022-10-23 stsp log_debug("client has %s", hex);
484 1a52c9bf 2022-12-30 stsp imsg_init(&ibuf, client->fd);
486 13b2bc37 2022-10-23 stsp err = got_object_get_type(&obj_type, repo_read.repo, &id);
488 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_NO_OBJ) {
489 13b2bc37 2022-10-23 stsp gotd_imsg_send_nak(&id, &ibuf,
490 13b2bc37 2022-10-23 stsp PROC_REPO_READ, repo_read.pid);
491 13b2bc37 2022-10-23 stsp err = NULL;
496 13b2bc37 2022-10-23 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT &&
497 13b2bc37 2022-10-23 stsp obj_type != GOT_OBJ_TYPE_TAG) {
498 13b2bc37 2022-10-23 stsp gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
499 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
503 1a52c9bf 2022-12-30 stsp err = record_object_id(&client->have_ids, &id);
505 13b2bc37 2022-10-23 stsp return err;
507 13b2bc37 2022-10-23 stsp gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
509 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
510 13b2bc37 2022-10-23 stsp return err;
513 13b2bc37 2022-10-23 stsp struct repo_read_pack_progress_arg {
514 13b2bc37 2022-10-23 stsp int report_progress;
515 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf;
516 13b2bc37 2022-10-23 stsp int sent_ready;
519 13b2bc37 2022-10-23 stsp static const struct got_error *
520 13b2bc37 2022-10-23 stsp pack_progress(void *arg, int ncolored, int nfound, int ntrees,
521 13b2bc37 2022-10-23 stsp off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
522 13b2bc37 2022-10-23 stsp int nobj_written)
524 13b2bc37 2022-10-23 stsp struct repo_read_pack_progress_arg *a = arg;
525 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_progress iprog;
528 13b2bc37 2022-10-23 stsp if (!a->report_progress)
529 13b2bc37 2022-10-23 stsp return NULL;
530 13b2bc37 2022-10-23 stsp if (packfile_size > 0 && a->sent_ready)
531 13b2bc37 2022-10-23 stsp return NULL;
533 13b2bc37 2022-10-23 stsp memset(&iprog, 0, sizeof(iprog));
534 13b2bc37 2022-10-23 stsp iprog.ncolored = ncolored;
535 13b2bc37 2022-10-23 stsp iprog.nfound = nfound;
536 13b2bc37 2022-10-23 stsp iprog.ntrees = ntrees;
537 13b2bc37 2022-10-23 stsp iprog.packfile_size = packfile_size;
538 13b2bc37 2022-10-23 stsp iprog.ncommits = ncommits;
539 13b2bc37 2022-10-23 stsp iprog.nobj_total = nobj_total;
540 13b2bc37 2022-10-23 stsp iprog.nobj_deltify = nobj_deltify;
541 13b2bc37 2022-10-23 stsp iprog.nobj_written = nobj_written;
543 13b2bc37 2022-10-23 stsp /* Using synchronous writes since we are blocking the event loop. */
544 13b2bc37 2022-10-23 stsp if (packfile_size == 0) {
545 13b2bc37 2022-10-23 stsp ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
546 13b2bc37 2022-10-23 stsp PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
547 13b2bc37 2022-10-23 stsp if (ret == -1) {
548 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg compose "
549 13b2bc37 2022-10-23 stsp "PACKFILE_PROGRESS");
552 13b2bc37 2022-10-23 stsp a->sent_ready = 1;
553 13b2bc37 2022-10-23 stsp ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
554 13b2bc37 2022-10-23 stsp PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
555 13b2bc37 2022-10-23 stsp if (ret == -1) {
556 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg compose "
557 13b2bc37 2022-10-23 stsp "PACKFILE_READY");
561 13b2bc37 2022-10-23 stsp return gotd_imsg_flush(a->ibuf);
564 13b2bc37 2022-10-23 stsp static const struct got_error *
565 1a52c9bf 2022-12-30 stsp receive_delta_cache_fd(struct imsg *imsg,
566 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev)
568 1a52c9bf 2022-12-30 stsp struct repo_read_client *client = &repo_read_client;
569 13b2bc37 2022-10-23 stsp struct gotd_imsg_send_packfile ireq;
570 13b2bc37 2022-10-23 stsp size_t datalen;
572 13b2bc37 2022-10-23 stsp log_debug("receving delta cache file");
574 13b2bc37 2022-10-23 stsp if (imsg->fd == -1)
575 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
577 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
578 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
579 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
580 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
582 1a52c9bf 2022-12-30 stsp if (client->delta_cache_fd != -1)
583 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
585 1a52c9bf 2022-12-30 stsp client->delta_cache_fd = imsg->fd;
586 1a52c9bf 2022-12-30 stsp client->report_progress = ireq.report_progress;
587 13b2bc37 2022-10-23 stsp return NULL;
590 13b2bc37 2022-10-23 stsp static const struct got_error *
591 1a52c9bf 2022-12-30 stsp receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
593 1a52c9bf 2022-12-30 stsp struct repo_read_client *client = &repo_read_client;
594 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_pipe ireq;
595 13b2bc37 2022-10-23 stsp size_t datalen;
597 13b2bc37 2022-10-23 stsp log_debug("receving pack pipe descriptor");
599 13b2bc37 2022-10-23 stsp if (imsg->fd == -1)
600 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
602 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
603 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
604 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
605 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
607 1a52c9bf 2022-12-30 stsp if (client->pack_pipe != -1)
608 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
610 1a52c9bf 2022-12-30 stsp client->pack_pipe = imsg->fd;
611 13b2bc37 2022-10-23 stsp return NULL;
614 13b2bc37 2022-10-23 stsp static const struct got_error *
615 1a52c9bf 2022-12-30 stsp send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
617 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
618 1a52c9bf 2022-12-30 stsp struct repo_read_client *client = &repo_read_client;
619 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_done idone;
620 13b2bc37 2022-10-23 stsp uint8_t packsha1[SHA1_DIGEST_LENGTH];
621 13b2bc37 2022-10-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
622 13b2bc37 2022-10-23 stsp FILE *delta_cache = NULL;
623 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
624 13b2bc37 2022-10-23 stsp struct repo_read_pack_progress_arg pa;
625 13b2bc37 2022-10-23 stsp struct got_ratelimit rl;
627 13b2bc37 2022-10-23 stsp log_debug("packfile request received");
629 13b2bc37 2022-10-23 stsp got_ratelimit_init(&rl, 2, 0);
631 86769de8 2022-10-28 stsp if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
632 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
634 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, client->fd);
636 13b2bc37 2022-10-23 stsp delta_cache = fdopen(client->delta_cache_fd, "w+");
637 13b2bc37 2022-10-23 stsp if (delta_cache == NULL) {
638 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fdopen");
641 13b2bc37 2022-10-23 stsp client->delta_cache_fd = -1;
643 13b2bc37 2022-10-23 stsp memset(&pa, 0, sizeof(pa));
644 13b2bc37 2022-10-23 stsp pa.ibuf = &ibuf;
645 13b2bc37 2022-10-23 stsp pa.report_progress = client->report_progress;
647 86769de8 2022-10-28 stsp err = got_pack_create(packsha1, client->pack_pipe, delta_cache,
648 13b2bc37 2022-10-23 stsp client->have_ids.ids, client->have_ids.nids,
649 13b2bc37 2022-10-23 stsp client->want_ids.ids, client->want_ids.nids,
650 98350b20 2023-02-17 op repo_read.repo, 0, 1, 0, pack_progress, &pa, &rl,
651 13b2bc37 2022-10-23 stsp check_cancelled, NULL);
655 13b2bc37 2022-10-23 stsp if (log_getverbose() > 0 &&
656 13b2bc37 2022-10-23 stsp got_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
657 13b2bc37 2022-10-23 stsp log_debug("sent pack-%s.pack", hex);
659 13b2bc37 2022-10-23 stsp memset(&idone, 0, sizeof(idone));
660 13b2bc37 2022-10-23 stsp idone.client_id = client->id;
661 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
662 13b2bc37 2022-10-23 stsp PROC_REPO_READ, -1, &idone, sizeof(idone)) == -1)
663 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg compose PACKFILE_DONE");
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 13b2bc37 2022-10-23 stsp return err;
671 13b2bc37 2022-10-23 stsp static void
672 ae7c1b78 2023-01-10 stsp repo_read_dispatch_session(int fd, short event, void *arg)
674 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
675 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
676 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
677 13b2bc37 2022-10-23 stsp struct imsg imsg;
679 13b2bc37 2022-10-23 stsp int shut = 0;
680 1a52c9bf 2022-12-30 stsp struct repo_read_client *client = &repo_read_client;
682 13b2bc37 2022-10-23 stsp if (event & EV_READ) {
683 13b2bc37 2022-10-23 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
684 13b2bc37 2022-10-23 stsp fatal("imsg_read error");
685 13b2bc37 2022-10-23 stsp if (n == 0) /* Connection closed. */
689 13b2bc37 2022-10-23 stsp if (event & EV_WRITE) {
690 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
691 13b2bc37 2022-10-23 stsp if (n == -1 && errno != EAGAIN)
692 13b2bc37 2022-10-23 stsp fatal("msgbuf_write");
693 13b2bc37 2022-10-23 stsp if (n == 0) /* Connection closed. */
697 13b2bc37 2022-10-23 stsp while (err == NULL && check_cancelled(NULL) == NULL) {
698 13b2bc37 2022-10-23 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
699 13b2bc37 2022-10-23 stsp fatal("%s: imsg_get", __func__);
700 13b2bc37 2022-10-23 stsp if (n == 0) /* No more messages. */
703 1a52c9bf 2022-12-30 stsp if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
704 1a52c9bf 2022-12-30 stsp client->id == 0) {
705 1a52c9bf 2022-12-30 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
709 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
710 13b2bc37 2022-10-23 stsp case GOTD_IMSG_LIST_REFS_INTERNAL:
711 1a52c9bf 2022-12-30 stsp err = list_refs(&imsg);
713 13b2bc37 2022-10-23 stsp log_warnx("%s: ls-refs: %s", repo_read.title,
716 13b2bc37 2022-10-23 stsp case GOTD_IMSG_WANT:
717 1a52c9bf 2022-12-30 stsp err = recv_want(&imsg);
719 13b2bc37 2022-10-23 stsp log_warnx("%s: want-line: %s", repo_read.title,
722 13b2bc37 2022-10-23 stsp case GOTD_IMSG_HAVE:
723 1a52c9bf 2022-12-30 stsp err = recv_have(&imsg);
725 13b2bc37 2022-10-23 stsp log_warnx("%s: have-line: %s", repo_read.title,
728 13b2bc37 2022-10-23 stsp case GOTD_IMSG_SEND_PACKFILE:
729 1a52c9bf 2022-12-30 stsp err = receive_delta_cache_fd(&imsg, iev);
731 13b2bc37 2022-10-23 stsp log_warnx("%s: receiving delta cache: %s",
732 13b2bc37 2022-10-23 stsp repo_read.title, err->msg);
734 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_PIPE:
735 1a52c9bf 2022-12-30 stsp err = receive_pack_pipe(&imsg, iev);
737 13b2bc37 2022-10-23 stsp log_warnx("%s: receiving pack pipe: %s",
738 13b2bc37 2022-10-23 stsp repo_read.title, err->msg);
741 1a52c9bf 2022-12-30 stsp err = send_packfile(&imsg, iev);
743 13b2bc37 2022-10-23 stsp log_warnx("%s: sending packfile: %s",
744 13b2bc37 2022-10-23 stsp repo_read.title, err->msg);
747 ae7c1b78 2023-01-10 stsp log_debug("%s: unexpected imsg %d", repo_read.title,
748 ae7c1b78 2023-01-10 stsp imsg.hdr.type);
752 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
755 ae7c1b78 2023-01-10 stsp if (!shut && check_cancelled(NULL) == NULL) {
757 ae7c1b78 2023-01-10 stsp gotd_imsg_send_error_event(iev, PROC_REPO_READ,
758 ae7c1b78 2023-01-10 stsp client->id, err) == -1) {
759 ae7c1b78 2023-01-10 stsp log_warnx("could not send error to parent: %s",
762 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
764 ae7c1b78 2023-01-10 stsp /* This pipe is dead. Remove its event handler */
765 ae7c1b78 2023-01-10 stsp event_del(&iev->ev);
766 ae7c1b78 2023-01-10 stsp event_loopexit(NULL);
770 ae7c1b78 2023-01-10 stsp static const struct got_error *
771 ae7c1b78 2023-01-10 stsp recv_connect(struct imsg *imsg)
773 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = &repo_read.session_iev;
774 ae7c1b78 2023-01-10 stsp size_t datalen;
776 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
777 ae7c1b78 2023-01-10 stsp if (datalen != 0)
778 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
779 ae7c1b78 2023-01-10 stsp if (imsg->fd == -1)
780 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
782 ae7c1b78 2023-01-10 stsp if (repo_read.session_fd != -1)
783 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
785 ae7c1b78 2023-01-10 stsp repo_read.session_fd = imsg->fd;
787 ae7c1b78 2023-01-10 stsp imsg_init(&iev->ibuf, repo_read.session_fd);
788 ae7c1b78 2023-01-10 stsp iev->handler = repo_read_dispatch_session;
789 ae7c1b78 2023-01-10 stsp iev->events = EV_READ;
790 ae7c1b78 2023-01-10 stsp iev->handler_arg = NULL;
791 ae7c1b78 2023-01-10 stsp event_set(&iev->ev, iev->ibuf.fd, EV_READ,
792 ae7c1b78 2023-01-10 stsp repo_read_dispatch_session, iev);
793 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
795 ae7c1b78 2023-01-10 stsp return NULL;
798 ae7c1b78 2023-01-10 stsp static void
799 ae7c1b78 2023-01-10 stsp repo_read_dispatch(int fd, short event, void *arg)
801 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
802 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = arg;
803 ae7c1b78 2023-01-10 stsp struct imsgbuf *ibuf = &iev->ibuf;
804 ae7c1b78 2023-01-10 stsp struct imsg imsg;
806 ae7c1b78 2023-01-10 stsp int shut = 0;
807 ae7c1b78 2023-01-10 stsp struct repo_read_client *client = &repo_read_client;
809 ae7c1b78 2023-01-10 stsp if (event & EV_READ) {
810 ae7c1b78 2023-01-10 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
811 ae7c1b78 2023-01-10 stsp fatal("imsg_read error");
812 ae7c1b78 2023-01-10 stsp if (n == 0) /* Connection closed. */
816 ae7c1b78 2023-01-10 stsp if (event & EV_WRITE) {
817 ae7c1b78 2023-01-10 stsp n = msgbuf_write(&ibuf->w);
818 ae7c1b78 2023-01-10 stsp if (n == -1 && errno != EAGAIN)
819 ae7c1b78 2023-01-10 stsp fatal("msgbuf_write");
820 ae7c1b78 2023-01-10 stsp if (n == 0) /* Connection closed. */
824 ae7c1b78 2023-01-10 stsp while (err == NULL && check_cancelled(NULL) == NULL) {
825 ae7c1b78 2023-01-10 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
826 ae7c1b78 2023-01-10 stsp fatal("%s: imsg_get", __func__);
827 ae7c1b78 2023-01-10 stsp if (n == 0) /* No more messages. */
830 ae7c1b78 2023-01-10 stsp switch (imsg.hdr.type) {
831 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CONNECT_REPO_CHILD:
832 ae7c1b78 2023-01-10 stsp err = recv_connect(&imsg);
835 13b2bc37 2022-10-23 stsp log_debug("%s: unexpected imsg %d", repo_read.title,
836 13b2bc37 2022-10-23 stsp imsg.hdr.type);
840 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
843 13b2bc37 2022-10-23 stsp if (!shut && check_cancelled(NULL) == NULL) {
845 13b2bc37 2022-10-23 stsp gotd_imsg_send_error_event(iev, PROC_REPO_READ,
846 1a52c9bf 2022-12-30 stsp client->id, err) == -1) {
847 13b2bc37 2022-10-23 stsp log_warnx("could not send error to parent: %s",
850 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
852 13b2bc37 2022-10-23 stsp /* This pipe is dead. Remove its event handler */
853 13b2bc37 2022-10-23 stsp event_del(&iev->ev);
854 13b2bc37 2022-10-23 stsp event_loopexit(NULL);
859 eec68231 2022-12-14 stsp repo_read_main(const char *title, const char *repo_path,
860 eec68231 2022-12-14 stsp int *pack_fds, int *temp_fds)
862 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
863 363c6230 2023-02-09 stsp struct repo_read_client *client = &repo_read_client;
864 13b2bc37 2022-10-23 stsp struct gotd_imsgev iev;
866 363c6230 2023-02-09 stsp client->fd = -1;
867 363c6230 2023-02-09 stsp client->delta_cache_fd = -1;
868 363c6230 2023-02-09 stsp client->pack_pipe = -1;
870 13b2bc37 2022-10-23 stsp repo_read.title = title;
871 13b2bc37 2022-10-23 stsp repo_read.pid = getpid();
872 13b2bc37 2022-10-23 stsp repo_read.pack_fds = pack_fds;
873 13b2bc37 2022-10-23 stsp repo_read.temp_fds = temp_fds;
874 ae7c1b78 2023-01-10 stsp repo_read.session_fd = -1;
875 ae7c1b78 2023-01-10 stsp repo_read.session_iev.ibuf.fd = -1;
877 eec68231 2022-12-14 stsp err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
880 13b2bc37 2022-10-23 stsp if (!got_repo_is_bare(repo_read.repo)) {
881 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
882 13b2bc37 2022-10-23 stsp "bare git repository required");
886 13b2bc37 2022-10-23 stsp got_repo_temp_fds_set(repo_read.repo, temp_fds);
888 13b2bc37 2022-10-23 stsp signal(SIGINT, catch_sigint);
889 13b2bc37 2022-10-23 stsp signal(SIGTERM, catch_sigterm);
890 13b2bc37 2022-10-23 stsp signal(SIGPIPE, SIG_IGN);
891 13b2bc37 2022-10-23 stsp signal(SIGHUP, SIG_IGN);
893 8c6fc146 2022-11-17 stsp imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
894 13b2bc37 2022-10-23 stsp iev.handler = repo_read_dispatch;
895 13b2bc37 2022-10-23 stsp iev.events = EV_READ;
896 13b2bc37 2022-10-23 stsp iev.handler_arg = NULL;
897 13b2bc37 2022-10-23 stsp event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
899 b50a2b46 2022-12-29 stsp if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
900 b50a2b46 2022-12-29 stsp PROC_REPO_READ, -1, NULL, 0) == -1) {
901 b50a2b46 2022-12-29 stsp err = got_error_from_errno("imsg compose REPO_CHILD_READY");
905 13b2bc37 2022-10-23 stsp event_dispatch();
908 13b2bc37 2022-10-23 stsp log_warnx("%s: %s", title, err->msg);
909 13b2bc37 2022-10-23 stsp repo_read_shutdown();
913 13b2bc37 2022-10-23 stsp repo_read_shutdown(void)
915 363c6230 2023-02-09 stsp struct repo_read_client *client = &repo_read_client;
917 13b2bc37 2022-10-23 stsp log_debug("%s: shutting down", repo_read.title);
919 363c6230 2023-02-09 stsp free_object_ids(&client->have_ids);
920 363c6230 2023-02-09 stsp free_object_ids(&client->want_ids);
921 363c6230 2023-02-09 stsp if (client->fd != -1)
922 363c6230 2023-02-09 stsp close(client->fd);
923 363c6230 2023-02-09 stsp if (client->delta_cache_fd != -1)
924 363c6230 2023-02-09 stsp close(client->delta_cache_fd);
925 363c6230 2023-02-09 stsp if (client->pack_pipe != -1)
926 363c6230 2023-02-09 stsp close(client->pack_pipe);
928 13b2bc37 2022-10-23 stsp if (repo_read.repo)
929 13b2bc37 2022-10-23 stsp got_repo_close(repo_read.repo);
930 13b2bc37 2022-10-23 stsp got_repo_pack_fds_close(repo_read.pack_fds);
931 13b2bc37 2022-10-23 stsp got_repo_temp_fds_close(repo_read.temp_fds);
932 ae7c1b78 2023-01-10 stsp if (repo_read.session_fd != -1)
933 ae7c1b78 2023-01-10 stsp close(repo_read.session_fd);