2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 3efd8e31 2022-10-23 thomas * Permission to use, copy, modify, and distribute this software for any
5 3efd8e31 2022-10-23 thomas * purpose with or without fee is hereby granted, provided that the above
6 3efd8e31 2022-10-23 thomas * copyright notice and this permission notice appear in all copies.
8 3efd8e31 2022-10-23 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3efd8e31 2022-10-23 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3efd8e31 2022-10-23 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3efd8e31 2022-10-23 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3efd8e31 2022-10-23 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3efd8e31 2022-10-23 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3efd8e31 2022-10-23 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 4efc8dcb 2023-08-29 thomas #include "got_compat.h"
19 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
20 3efd8e31 2022-10-23 thomas #include <sys/types.h>
22 3efd8e31 2022-10-23 thomas #include <event.h>
23 3efd8e31 2022-10-23 thomas #include <errno.h>
24 3efd8e31 2022-10-23 thomas #include <imsg.h>
25 3efd8e31 2022-10-23 thomas #include <signal.h>
26 3efd8e31 2022-10-23 thomas #include <stdlib.h>
27 3efd8e31 2022-10-23 thomas #include <limits.h>
28 3efd8e31 2022-10-23 thomas #include <poll.h>
29 3efd8e31 2022-10-23 thomas #include <stdio.h>
30 3efd8e31 2022-10-23 thomas #include <string.h>
31 3efd8e31 2022-10-23 thomas #include <unistd.h>
33 3efd8e31 2022-10-23 thomas #include "got_error.h"
34 3efd8e31 2022-10-23 thomas #include "got_cancel.h"
35 3efd8e31 2022-10-23 thomas #include "got_object.h"
36 3efd8e31 2022-10-23 thomas #include "got_repository.h"
37 3efd8e31 2022-10-23 thomas #include "got_reference.h"
38 3efd8e31 2022-10-23 thomas #include "got_repository_admin.h"
39 6d7eb4f7 2023-04-04 thomas #include "got_path.h"
41 3efd8e31 2022-10-23 thomas #include "got_lib_delta.h"
42 3efd8e31 2022-10-23 thomas #include "got_lib_object.h"
43 3efd8e31 2022-10-23 thomas #include "got_lib_object_idset.h"
44 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
45 3efd8e31 2022-10-23 thomas #include "got_lib_pack.h"
46 3efd8e31 2022-10-23 thomas #include "got_lib_ratelimit.h"
47 3efd8e31 2022-10-23 thomas #include "got_lib_pack_create.h"
48 3efd8e31 2022-10-23 thomas #include "got_lib_poll.h"
50 3efd8e31 2022-10-23 thomas #include "log.h"
51 3efd8e31 2022-10-23 thomas #include "gotd.h"
52 3efd8e31 2022-10-23 thomas #include "repo_read.h"
54 3efd8e31 2022-10-23 thomas #ifndef nitems
55 3efd8e31 2022-10-23 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 3efd8e31 2022-10-23 thomas static struct repo_read {
59 3efd8e31 2022-10-23 thomas pid_t pid;
60 3efd8e31 2022-10-23 thomas const char *title;
61 3efd8e31 2022-10-23 thomas struct got_repository *repo;
62 3efd8e31 2022-10-23 thomas int *pack_fds;
63 3efd8e31 2022-10-23 thomas int *temp_fds;
64 62ee7d94 2023-01-10 thomas int session_fd;
65 62ee7d94 2023-01-10 thomas struct gotd_imsgev session_iev;
66 b92adf10 2024-03-30 thomas int refs_listed;
67 3efd8e31 2022-10-23 thomas } repo_read;
69 9148c8a7 2023-01-02 thomas static struct repo_read_client {
70 3efd8e31 2022-10-23 thomas uint32_t id;
72 3efd8e31 2022-10-23 thomas int delta_cache_fd;
73 3efd8e31 2022-10-23 thomas int report_progress;
74 cc4cc677 2022-10-28 thomas int pack_pipe;
75 19aad72f 2023-03-01 thomas struct got_object_idset *want_ids;
76 19aad72f 2023-03-01 thomas struct got_object_idset *have_ids;
77 9148c8a7 2023-01-02 thomas } repo_read_client;
79 3efd8e31 2022-10-23 thomas static volatile sig_atomic_t sigint_received;
80 3efd8e31 2022-10-23 thomas static volatile sig_atomic_t sigterm_received;
82 3efd8e31 2022-10-23 thomas static void
83 3efd8e31 2022-10-23 thomas catch_sigint(int signo)
85 3efd8e31 2022-10-23 thomas sigint_received = 1;
88 3efd8e31 2022-10-23 thomas static void
89 3efd8e31 2022-10-23 thomas catch_sigterm(int signo)
91 3efd8e31 2022-10-23 thomas sigterm_received = 1;
94 3efd8e31 2022-10-23 thomas static const struct got_error *
95 3efd8e31 2022-10-23 thomas check_cancelled(void *arg)
97 3efd8e31 2022-10-23 thomas if (sigint_received || sigterm_received)
98 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_CANCELLED);
100 3efd8e31 2022-10-23 thomas return NULL;
103 3efd8e31 2022-10-23 thomas static const struct got_error *
104 a7e9cbc6 2022-11-08 thomas send_symref(struct got_reference *symref, struct got_object_id *target_id,
105 a7e9cbc6 2022-11-08 thomas struct imsgbuf *ibuf)
107 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
108 3efd8e31 2022-10-23 thomas struct gotd_imsg_symref isymref;
109 3efd8e31 2022-10-23 thomas const char *refname = got_ref_get_name(symref);
110 3efd8e31 2022-10-23 thomas const char *target = got_ref_get_symref_target(symref);
111 3efd8e31 2022-10-23 thomas size_t len;
112 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
114 3efd8e31 2022-10-23 thomas memset(&isymref, 0, sizeof(isymref));
115 3efd8e31 2022-10-23 thomas isymref.name_len = strlen(refname);
116 3efd8e31 2022-10-23 thomas isymref.target_len = strlen(target);
117 3efd8e31 2022-10-23 thomas memcpy(isymref.target_id, target_id->sha1, sizeof(isymref.target_id));
119 3efd8e31 2022-10-23 thomas len = sizeof(isymref) + isymref.name_len + isymref.target_len;
120 3efd8e31 2022-10-23 thomas if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
121 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_NO_SPACE);
122 3efd8e31 2022-10-23 thomas goto done;
125 3efd8e31 2022-10-23 thomas wbuf = imsg_create(ibuf, GOTD_IMSG_SYMREF, 0, 0, len);
126 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
127 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create SYMREF");
128 3efd8e31 2022-10-23 thomas goto done;
131 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &isymref, sizeof(isymref)) == -1) {
132 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add SYMREF");
133 3efd8e31 2022-10-23 thomas goto done;
135 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, refname, isymref.name_len) == -1) {
136 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add SYMREF");
137 3efd8e31 2022-10-23 thomas goto done;
139 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, target, isymref.target_len) == -1) {
140 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add SYMREF");
141 3efd8e31 2022-10-23 thomas goto done;
144 3efd8e31 2022-10-23 thomas imsg_close(ibuf, wbuf);
146 3efd8e31 2022-10-23 thomas free(target_id);
147 3efd8e31 2022-10-23 thomas return err;
150 3efd8e31 2022-10-23 thomas static const struct got_error *
151 3efd8e31 2022-10-23 thomas send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
152 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf)
154 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
155 3efd8e31 2022-10-23 thomas struct got_tag_object *tag;
156 3efd8e31 2022-10-23 thomas size_t namelen, len;
157 3efd8e31 2022-10-23 thomas char *peeled_refname = NULL;
158 3efd8e31 2022-10-23 thomas struct got_object_id *id;
159 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
161 3efd8e31 2022-10-23 thomas err = got_object_tag_open(&tag, repo_read.repo, obj);
163 3efd8e31 2022-10-23 thomas return err;
165 3efd8e31 2022-10-23 thomas if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
166 3efd8e31 2022-10-23 thomas err = got_error_from_errno("asprintf");
167 3efd8e31 2022-10-23 thomas goto done;
170 3efd8e31 2022-10-23 thomas id = got_object_tag_get_object_id(tag);
171 3efd8e31 2022-10-23 thomas namelen = strlen(peeled_refname);
173 3efd8e31 2022-10-23 thomas len = sizeof(struct gotd_imsg_ref) + namelen;
174 3efd8e31 2022-10-23 thomas if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
175 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_NO_SPACE);
176 3efd8e31 2022-10-23 thomas goto done;
179 3efd8e31 2022-10-23 thomas wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
180 3efd8e31 2022-10-23 thomas repo_read.pid, len);
181 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
182 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create MREF");
183 3efd8e31 2022-10-23 thomas goto done;
186 3efd8e31 2022-10-23 thomas /* Keep in sync with struct gotd_imsg_ref definition. */
187 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
188 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add REF");
189 3efd8e31 2022-10-23 thomas goto done;
191 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
192 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add REF");
193 3efd8e31 2022-10-23 thomas goto done;
195 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
196 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add REF");
197 3efd8e31 2022-10-23 thomas goto done;
200 3efd8e31 2022-10-23 thomas imsg_close(ibuf, wbuf);
202 3efd8e31 2022-10-23 thomas got_object_tag_close(tag);
203 3efd8e31 2022-10-23 thomas return err;
206 3efd8e31 2022-10-23 thomas static const struct got_error *
207 3efd8e31 2022-10-23 thomas send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
209 3efd8e31 2022-10-23 thomas const struct got_error *err;
210 3efd8e31 2022-10-23 thomas const char *refname = got_ref_get_name(ref);
211 3efd8e31 2022-10-23 thomas size_t namelen;
212 3efd8e31 2022-10-23 thomas struct got_object_id *id = NULL;
213 3efd8e31 2022-10-23 thomas struct got_object *obj = NULL;
214 3efd8e31 2022-10-23 thomas size_t len;
215 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
217 3efd8e31 2022-10-23 thomas namelen = strlen(refname);
219 3efd8e31 2022-10-23 thomas len = sizeof(struct gotd_imsg_ref) + namelen;
220 3efd8e31 2022-10-23 thomas if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
221 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
223 3efd8e31 2022-10-23 thomas err = got_ref_resolve(&id, repo_read.repo, ref);
225 3efd8e31 2022-10-23 thomas return err;
227 3efd8e31 2022-10-23 thomas wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
228 3efd8e31 2022-10-23 thomas repo_read.pid, len);
229 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
230 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create REF");
231 3efd8e31 2022-10-23 thomas goto done;
234 3efd8e31 2022-10-23 thomas /* Keep in sync with struct gotd_imsg_ref definition. */
235 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
236 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF");
237 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
238 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF");
239 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, refname, namelen) == -1)
240 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF");
242 3efd8e31 2022-10-23 thomas imsg_close(ibuf, wbuf);
244 3efd8e31 2022-10-23 thomas err = got_object_open(&obj, repo_read.repo, id);
246 3efd8e31 2022-10-23 thomas goto done;
247 3efd8e31 2022-10-23 thomas if (obj->type == GOT_OBJ_TYPE_TAG)
248 3efd8e31 2022-10-23 thomas err = send_peeled_tag_ref(ref, obj, ibuf);
251 3efd8e31 2022-10-23 thomas got_object_close(obj);
252 3efd8e31 2022-10-23 thomas free(id);
253 3efd8e31 2022-10-23 thomas return err;
256 3efd8e31 2022-10-23 thomas static const struct got_error *
257 9148c8a7 2023-01-02 thomas list_refs(struct imsg *imsg)
259 3efd8e31 2022-10-23 thomas const struct got_error *err;
260 9148c8a7 2023-01-02 thomas struct repo_read_client *client = &repo_read_client;
261 3efd8e31 2022-10-23 thomas struct got_reflist_head refs;
262 3efd8e31 2022-10-23 thomas struct got_reflist_entry *re;
263 3efd8e31 2022-10-23 thomas size_t datalen;
264 3efd8e31 2022-10-23 thomas struct gotd_imsg_reflist irefs;
265 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
266 3d97effa 2024-01-31 thomas int client_fd;
267 a7e9cbc6 2022-11-08 thomas struct got_object_id *head_target_id = NULL;
269 3efd8e31 2022-10-23 thomas TAILQ_INIT(&refs);
271 3d97effa 2024-01-31 thomas client_fd = imsg_get_fd(imsg);
272 3efd8e31 2022-10-23 thomas if (client_fd == -1)
273 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
275 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
276 b92adf10 2024-03-30 thomas if (datalen != 0)
277 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
279 b92adf10 2024-03-30 thomas if (repo_read.refs_listed) {
280 9148c8a7 2023-01-02 thomas return got_error_msg(GOT_ERR_CLIENT_ID,
281 9148c8a7 2023-01-02 thomas "duplicate list-refs request");
283 b92adf10 2024-03-30 thomas repo_read.refs_listed = 1;
285 9148c8a7 2023-01-02 thomas client->fd = client_fd;
287 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, client_fd);
289 3efd8e31 2022-10-23 thomas err = got_ref_list(&refs, repo_read.repo, "",
290 3efd8e31 2022-10-23 thomas got_ref_cmp_by_name, NULL);
292 3efd8e31 2022-10-23 thomas return err;
294 3efd8e31 2022-10-23 thomas memset(&irefs, 0, sizeof(irefs));
295 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(re, &refs, entry) {
296 3efd8e31 2022-10-23 thomas struct got_object_id *id;
297 3efd8e31 2022-10-23 thomas int obj_type;
299 3efd8e31 2022-10-23 thomas if (got_ref_is_symbolic(re->ref)) {
300 3efd8e31 2022-10-23 thomas const char *refname = got_ref_get_name(re->ref);
301 a7e9cbc6 2022-11-08 thomas if (strcmp(refname, GOT_REF_HEAD) != 0)
302 a7e9cbc6 2022-11-08 thomas continue;
303 a7e9cbc6 2022-11-08 thomas err = got_ref_resolve(&head_target_id, repo_read.repo,
304 a7e9cbc6 2022-11-08 thomas re->ref);
305 a7e9cbc6 2022-11-08 thomas if (err) {
306 a7e9cbc6 2022-11-08 thomas if (err->code != GOT_ERR_NOT_REF)
307 a7e9cbc6 2022-11-08 thomas return err;
309 a7e9cbc6 2022-11-08 thomas * HEAD points to a non-existent branch.
310 a7e9cbc6 2022-11-08 thomas * Do not advertise it.
311 a7e9cbc6 2022-11-08 thomas * Matches git-daemon's behaviour.
313 a7e9cbc6 2022-11-08 thomas head_target_id = NULL;
314 a7e9cbc6 2022-11-08 thomas err = NULL;
316 3efd8e31 2022-10-23 thomas irefs.nrefs++;
317 3efd8e31 2022-10-23 thomas continue;
320 3efd8e31 2022-10-23 thomas irefs.nrefs++;
322 3efd8e31 2022-10-23 thomas /* Account for a peeled tag refs. */
323 3efd8e31 2022-10-23 thomas err = got_ref_resolve(&id, repo_read.repo, re->ref);
325 3efd8e31 2022-10-23 thomas goto done;
326 8652e561 2023-01-14 thomas err = got_object_get_type(&obj_type, repo_read.repo, id);
327 3efd8e31 2022-10-23 thomas free(id);
329 3efd8e31 2022-10-23 thomas goto done;
330 3efd8e31 2022-10-23 thomas if (obj_type == GOT_OBJ_TYPE_TAG)
331 3efd8e31 2022-10-23 thomas irefs.nrefs++;
334 3efd8e31 2022-10-23 thomas if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
335 3efd8e31 2022-10-23 thomas repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
336 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_compose REFLIST");
337 3efd8e31 2022-10-23 thomas goto done;
341 3efd8e31 2022-10-23 thomas * Send the HEAD symref first. In Git-protocol versions < 2
342 3efd8e31 2022-10-23 thomas * the HEAD symref must be announced on the initial line of
343 3efd8e31 2022-10-23 thomas * the server's ref advertisement.
344 3efd8e31 2022-10-23 thomas * For now, we do not advertise symrefs other than HEAD.
346 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(re, &refs, entry) {
347 3efd8e31 2022-10-23 thomas if (!got_ref_is_symbolic(re->ref) ||
348 a7e9cbc6 2022-11-08 thomas strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0 ||
349 a7e9cbc6 2022-11-08 thomas head_target_id == NULL)
350 3efd8e31 2022-10-23 thomas continue;
351 a7e9cbc6 2022-11-08 thomas err = send_symref(re->ref, head_target_id, &ibuf);
353 3efd8e31 2022-10-23 thomas goto done;
356 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(re, &refs, entry) {
357 3efd8e31 2022-10-23 thomas if (got_ref_is_symbolic(re->ref))
358 3efd8e31 2022-10-23 thomas continue;
359 3efd8e31 2022-10-23 thomas err = send_ref(re->ref, &ibuf);
361 3efd8e31 2022-10-23 thomas goto done;
364 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(&ibuf);
366 3efd8e31 2022-10-23 thomas got_ref_list_free(&refs);
367 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
368 3efd8e31 2022-10-23 thomas return err;
371 3efd8e31 2022-10-23 thomas static const struct got_error *
372 19aad72f 2023-03-01 thomas append_object_id(struct got_object_id *id, void *data, void *arg)
374 19aad72f 2023-03-01 thomas struct gotd_object_id_array *array = arg;
375 3efd8e31 2022-10-23 thomas const size_t alloc_chunksz = 256;
377 3efd8e31 2022-10-23 thomas if (array->ids == NULL) {
378 3efd8e31 2022-10-23 thomas array->ids = reallocarray(NULL, alloc_chunksz,
379 3efd8e31 2022-10-23 thomas sizeof(*array->ids));
380 3efd8e31 2022-10-23 thomas if (array->ids == NULL)
381 3efd8e31 2022-10-23 thomas return got_error_from_errno("reallocarray");
382 3efd8e31 2022-10-23 thomas array->nalloc = alloc_chunksz;
383 3efd8e31 2022-10-23 thomas array->nids = 0;
384 3efd8e31 2022-10-23 thomas } else if (array->nalloc <= array->nids) {
385 3efd8e31 2022-10-23 thomas struct got_object_id **new;
386 3efd8e31 2022-10-23 thomas new = recallocarray(array->ids, array->nalloc,
387 3efd8e31 2022-10-23 thomas array->nalloc + alloc_chunksz, sizeof(*new));
388 3efd8e31 2022-10-23 thomas if (new == NULL)
389 3efd8e31 2022-10-23 thomas return got_error_from_errno("recallocarray");
390 3efd8e31 2022-10-23 thomas array->ids = new;
391 3efd8e31 2022-10-23 thomas array->nalloc += alloc_chunksz;
394 19aad72f 2023-03-01 thomas array->ids[array->nids] = id;
395 3efd8e31 2022-10-23 thomas array->nids++;
396 3efd8e31 2022-10-23 thomas return NULL;
399 3efd8e31 2022-10-23 thomas static const struct got_error *
400 9148c8a7 2023-01-02 thomas recv_want(struct imsg *imsg)
402 3efd8e31 2022-10-23 thomas const struct got_error *err;
403 9148c8a7 2023-01-02 thomas struct repo_read_client *client = &repo_read_client;
404 3efd8e31 2022-10-23 thomas struct gotd_imsg_want iwant;
405 3efd8e31 2022-10-23 thomas size_t datalen;
406 3efd8e31 2022-10-23 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
407 3efd8e31 2022-10-23 thomas struct got_object_id id;
408 3efd8e31 2022-10-23 thomas int obj_type;
409 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
411 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
412 3efd8e31 2022-10-23 thomas if (datalen != sizeof(iwant))
413 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
414 3efd8e31 2022-10-23 thomas memcpy(&iwant, imsg->data, sizeof(iwant));
416 3efd8e31 2022-10-23 thomas memset(&id, 0, sizeof(id));
417 3efd8e31 2022-10-23 thomas memcpy(id.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
419 3efd8e31 2022-10-23 thomas if (log_getverbose() > 0 &&
420 3efd8e31 2022-10-23 thomas got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
421 3efd8e31 2022-10-23 thomas log_debug("client wants %s", hex);
423 9148c8a7 2023-01-02 thomas imsg_init(&ibuf, client->fd);
425 3efd8e31 2022-10-23 thomas err = got_object_get_type(&obj_type, repo_read.repo, &id);
427 3efd8e31 2022-10-23 thomas return err;
429 3efd8e31 2022-10-23 thomas if (obj_type != GOT_OBJ_TYPE_COMMIT &&
430 3efd8e31 2022-10-23 thomas obj_type != GOT_OBJ_TYPE_TAG)
431 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_OBJ_TYPE);
433 19aad72f 2023-03-01 thomas if (!got_object_idset_contains(client->want_ids, &id)) {
434 19aad72f 2023-03-01 thomas err = got_object_idset_add(client->want_ids, &id, NULL);
436 19aad72f 2023-03-01 thomas return err;
439 3efd8e31 2022-10-23 thomas gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
440 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
441 3efd8e31 2022-10-23 thomas return err;
444 3efd8e31 2022-10-23 thomas static const struct got_error *
445 9148c8a7 2023-01-02 thomas recv_have(struct imsg *imsg)
447 3efd8e31 2022-10-23 thomas const struct got_error *err;
448 9148c8a7 2023-01-02 thomas struct repo_read_client *client = &repo_read_client;
449 3efd8e31 2022-10-23 thomas struct gotd_imsg_have ihave;
450 3efd8e31 2022-10-23 thomas size_t datalen;
451 3efd8e31 2022-10-23 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
452 3efd8e31 2022-10-23 thomas struct got_object_id id;
453 3efd8e31 2022-10-23 thomas int obj_type;
454 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
456 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
457 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ihave))
458 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
459 3efd8e31 2022-10-23 thomas memcpy(&ihave, imsg->data, sizeof(ihave));
461 3efd8e31 2022-10-23 thomas memset(&id, 0, sizeof(id));
462 3efd8e31 2022-10-23 thomas memcpy(id.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
464 3efd8e31 2022-10-23 thomas if (log_getverbose() > 0 &&
465 3efd8e31 2022-10-23 thomas got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
466 3efd8e31 2022-10-23 thomas log_debug("client has %s", hex);
468 9148c8a7 2023-01-02 thomas imsg_init(&ibuf, client->fd);
470 3efd8e31 2022-10-23 thomas err = got_object_get_type(&obj_type, repo_read.repo, &id);
471 3efd8e31 2022-10-23 thomas if (err) {
472 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_NO_OBJ) {
473 3efd8e31 2022-10-23 thomas gotd_imsg_send_nak(&id, &ibuf,
474 3efd8e31 2022-10-23 thomas PROC_REPO_READ, repo_read.pid);
475 3efd8e31 2022-10-23 thomas err = NULL;
477 3efd8e31 2022-10-23 thomas goto done;
480 3efd8e31 2022-10-23 thomas if (obj_type != GOT_OBJ_TYPE_COMMIT &&
481 3efd8e31 2022-10-23 thomas obj_type != GOT_OBJ_TYPE_TAG) {
482 3efd8e31 2022-10-23 thomas gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
483 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_OBJ_TYPE);
484 3efd8e31 2022-10-23 thomas goto done;
487 19aad72f 2023-03-01 thomas if (!got_object_idset_contains(client->have_ids, &id)) {
488 19aad72f 2023-03-01 thomas err = got_object_idset_add(client->have_ids, &id, NULL);
490 19aad72f 2023-03-01 thomas goto done;
493 3efd8e31 2022-10-23 thomas gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
495 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
496 3efd8e31 2022-10-23 thomas return err;
499 3efd8e31 2022-10-23 thomas struct repo_read_pack_progress_arg {
500 3efd8e31 2022-10-23 thomas int report_progress;
501 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf;
502 3efd8e31 2022-10-23 thomas int sent_ready;
505 3efd8e31 2022-10-23 thomas static const struct got_error *
506 3efd8e31 2022-10-23 thomas pack_progress(void *arg, int ncolored, int nfound, int ntrees,
507 3efd8e31 2022-10-23 thomas off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
508 3efd8e31 2022-10-23 thomas int nobj_written)
510 3efd8e31 2022-10-23 thomas struct repo_read_pack_progress_arg *a = arg;
511 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_progress iprog;
514 3efd8e31 2022-10-23 thomas if (!a->report_progress)
515 3efd8e31 2022-10-23 thomas return NULL;
516 3efd8e31 2022-10-23 thomas if (packfile_size > 0 && a->sent_ready)
517 3efd8e31 2022-10-23 thomas return NULL;
519 3efd8e31 2022-10-23 thomas memset(&iprog, 0, sizeof(iprog));
520 3efd8e31 2022-10-23 thomas iprog.ncolored = ncolored;
521 3efd8e31 2022-10-23 thomas iprog.nfound = nfound;
522 3efd8e31 2022-10-23 thomas iprog.ntrees = ntrees;
523 3efd8e31 2022-10-23 thomas iprog.packfile_size = packfile_size;
524 3efd8e31 2022-10-23 thomas iprog.ncommits = ncommits;
525 3efd8e31 2022-10-23 thomas iprog.nobj_total = nobj_total;
526 3efd8e31 2022-10-23 thomas iprog.nobj_deltify = nobj_deltify;
527 3efd8e31 2022-10-23 thomas iprog.nobj_written = nobj_written;
529 3efd8e31 2022-10-23 thomas /* Using synchronous writes since we are blocking the event loop. */
530 3efd8e31 2022-10-23 thomas if (packfile_size == 0) {
531 3efd8e31 2022-10-23 thomas ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
532 3efd8e31 2022-10-23 thomas PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
533 3efd8e31 2022-10-23 thomas if (ret == -1) {
534 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg compose "
535 3efd8e31 2022-10-23 thomas "PACKFILE_PROGRESS");
538 3efd8e31 2022-10-23 thomas a->sent_ready = 1;
539 3efd8e31 2022-10-23 thomas ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
540 3efd8e31 2022-10-23 thomas PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
541 3efd8e31 2022-10-23 thomas if (ret == -1) {
542 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg compose "
543 3efd8e31 2022-10-23 thomas "PACKFILE_READY");
547 3efd8e31 2022-10-23 thomas return gotd_imsg_flush(a->ibuf);
550 3efd8e31 2022-10-23 thomas static const struct got_error *
551 9148c8a7 2023-01-02 thomas receive_delta_cache_fd(struct imsg *imsg,
552 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev)
554 9148c8a7 2023-01-02 thomas struct repo_read_client *client = &repo_read_client;
555 3efd8e31 2022-10-23 thomas struct gotd_imsg_send_packfile ireq;
556 3efd8e31 2022-10-23 thomas size_t datalen;
558 ff553ea7 2023-04-22 thomas log_debug("receiving delta cache file");
560 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
561 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
562 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
563 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
565 9148c8a7 2023-01-02 thomas if (client->delta_cache_fd != -1)
566 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
568 3d97effa 2024-01-31 thomas client->delta_cache_fd = imsg_get_fd(imsg);
569 3d97effa 2024-01-31 thomas if (client->delta_cache_fd == -1)
570 3d97effa 2024-01-31 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
572 9148c8a7 2023-01-02 thomas client->report_progress = ireq.report_progress;
573 3efd8e31 2022-10-23 thomas return NULL;
576 3efd8e31 2022-10-23 thomas static const struct got_error *
577 9148c8a7 2023-01-02 thomas receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
579 9148c8a7 2023-01-02 thomas struct repo_read_client *client = &repo_read_client;
580 3efd8e31 2022-10-23 thomas size_t datalen;
582 ff553ea7 2023-04-22 thomas log_debug("receiving pack pipe descriptor");
584 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
585 b92adf10 2024-03-30 thomas if (datalen != 0)
586 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
588 9148c8a7 2023-01-02 thomas if (client->pack_pipe != -1)
589 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
591 3d97effa 2024-01-31 thomas client->pack_pipe = imsg_get_fd(imsg);
592 3d97effa 2024-01-31 thomas if (client->pack_pipe == -1)
593 3d97effa 2024-01-31 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
595 3efd8e31 2022-10-23 thomas return NULL;
598 3efd8e31 2022-10-23 thomas static const struct got_error *
599 9148c8a7 2023-01-02 thomas send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
601 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
602 9148c8a7 2023-01-02 thomas struct repo_read_client *client = &repo_read_client;
603 3efd8e31 2022-10-23 thomas uint8_t packsha1[SHA1_DIGEST_LENGTH];
604 3efd8e31 2022-10-23 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
605 3efd8e31 2022-10-23 thomas FILE *delta_cache = NULL;
606 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
607 3efd8e31 2022-10-23 thomas struct repo_read_pack_progress_arg pa;
608 3efd8e31 2022-10-23 thomas struct got_ratelimit rl;
609 19aad72f 2023-03-01 thomas struct gotd_object_id_array want_ids;
610 19aad72f 2023-03-01 thomas struct gotd_object_id_array have_ids;
612 3efd8e31 2022-10-23 thomas log_debug("packfile request received");
614 19aad72f 2023-03-01 thomas memset(&want_ids, 0, sizeof(want_ids));
615 19aad72f 2023-03-01 thomas memset(&have_ids, 0, sizeof(have_ids));
617 3efd8e31 2022-10-23 thomas got_ratelimit_init(&rl, 2, 0);
619 cc4cc677 2022-10-28 thomas if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
620 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
622 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, client->fd);
624 3efd8e31 2022-10-23 thomas delta_cache = fdopen(client->delta_cache_fd, "w+");
625 3efd8e31 2022-10-23 thomas if (delta_cache == NULL) {
626 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fdopen");
627 3efd8e31 2022-10-23 thomas goto done;
629 3efd8e31 2022-10-23 thomas client->delta_cache_fd = -1;
631 3efd8e31 2022-10-23 thomas memset(&pa, 0, sizeof(pa));
632 3efd8e31 2022-10-23 thomas pa.ibuf = &ibuf;
633 3efd8e31 2022-10-23 thomas pa.report_progress = client->report_progress;
635 19aad72f 2023-03-01 thomas err = got_object_idset_for_each(client->want_ids,
636 19aad72f 2023-03-01 thomas append_object_id, &want_ids);
638 19aad72f 2023-03-01 thomas goto done;
639 19aad72f 2023-03-01 thomas err = got_object_idset_for_each(client->have_ids,
640 19aad72f 2023-03-01 thomas append_object_id, &have_ids);
642 19aad72f 2023-03-01 thomas goto done;
644 cc4cc677 2022-10-28 thomas err = got_pack_create(packsha1, client->pack_pipe, delta_cache,
645 19aad72f 2023-03-01 thomas have_ids.ids, have_ids.nids, want_ids.ids, want_ids.nids,
646 91b1d456 2023-02-17 thomas repo_read.repo, 0, 1, 0, pack_progress, &pa, &rl,
647 3efd8e31 2022-10-23 thomas check_cancelled, NULL);
649 3efd8e31 2022-10-23 thomas goto done;
651 3efd8e31 2022-10-23 thomas if (log_getverbose() > 0 &&
652 3efd8e31 2022-10-23 thomas got_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
653 3efd8e31 2022-10-23 thomas log_debug("sent pack-%s.pack", hex);
655 b92adf10 2024-03-30 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
656 b92adf10 2024-03-30 thomas PROC_REPO_READ, -1, NULL, 0) == -1)
657 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg compose PACKFILE_DONE");
659 815470d4 2024-01-31 thomas if (client->delta_cache_fd != -1 &&
660 815470d4 2024-01-31 thomas close(client->delta_cache_fd) == -1 && err == NULL)
661 815470d4 2024-01-31 thomas err = got_error_from_errno("close");
662 815470d4 2024-01-31 thomas client->delta_cache_fd = -1;
663 3efd8e31 2022-10-23 thomas if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
664 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fclose");
665 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
666 19aad72f 2023-03-01 thomas free(want_ids.ids);
667 19aad72f 2023-03-01 thomas free(have_ids.ids);
668 3efd8e31 2022-10-23 thomas return err;
671 3efd8e31 2022-10-23 thomas static void
672 62ee7d94 2023-01-10 thomas repo_read_dispatch_session(int fd, short event, void *arg)
674 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
675 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev = arg;
676 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf = &iev->ibuf;
677 3efd8e31 2022-10-23 thomas struct imsg imsg;
678 3efd8e31 2022-10-23 thomas ssize_t n;
679 3efd8e31 2022-10-23 thomas int shut = 0;
680 9148c8a7 2023-01-02 thomas struct repo_read_client *client = &repo_read_client;
682 3efd8e31 2022-10-23 thomas if (event & EV_READ) {
683 3efd8e31 2022-10-23 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
684 3efd8e31 2022-10-23 thomas fatal("imsg_read error");
685 3efd8e31 2022-10-23 thomas if (n == 0) /* Connection closed. */
686 3efd8e31 2022-10-23 thomas shut = 1;
689 3efd8e31 2022-10-23 thomas if (event & EV_WRITE) {
690 3efd8e31 2022-10-23 thomas n = msgbuf_write(&ibuf->w);
691 3efd8e31 2022-10-23 thomas if (n == -1 && errno != EAGAIN)
692 3efd8e31 2022-10-23 thomas fatal("msgbuf_write");
693 3efd8e31 2022-10-23 thomas if (n == 0) /* Connection closed. */
694 3efd8e31 2022-10-23 thomas shut = 1;
697 3efd8e31 2022-10-23 thomas while (err == NULL && check_cancelled(NULL) == NULL) {
698 3efd8e31 2022-10-23 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
699 3efd8e31 2022-10-23 thomas fatal("%s: imsg_get", __func__);
700 3efd8e31 2022-10-23 thomas if (n == 0) /* No more messages. */
703 9148c8a7 2023-01-02 thomas if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
704 b92adf10 2024-03-30 thomas !repo_read.refs_listed) {
705 9148c8a7 2023-01-02 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
709 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
710 3efd8e31 2022-10-23 thomas case GOTD_IMSG_LIST_REFS_INTERNAL:
711 9148c8a7 2023-01-02 thomas err = list_refs(&imsg);
713 42f290d4 2023-03-05 thomas log_warnx("ls-refs: %s", err->msg);
715 3efd8e31 2022-10-23 thomas case GOTD_IMSG_WANT:
716 9148c8a7 2023-01-02 thomas err = recv_want(&imsg);
718 42f290d4 2023-03-05 thomas log_warnx("want-line: %s", err->msg);
720 3efd8e31 2022-10-23 thomas case GOTD_IMSG_HAVE:
721 9148c8a7 2023-01-02 thomas err = recv_have(&imsg);
723 42f290d4 2023-03-05 thomas log_warnx("have-line: %s", err->msg);
725 3efd8e31 2022-10-23 thomas case GOTD_IMSG_SEND_PACKFILE:
726 9148c8a7 2023-01-02 thomas err = receive_delta_cache_fd(&imsg, iev);
728 42f290d4 2023-03-05 thomas log_warnx("receiving delta cache: %s",
729 42f290d4 2023-03-05 thomas err->msg);
731 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_PIPE:
732 9148c8a7 2023-01-02 thomas err = receive_pack_pipe(&imsg, iev);
733 3efd8e31 2022-10-23 thomas if (err) {
734 42f290d4 2023-03-05 thomas log_warnx("receiving pack pipe: %s", err->msg);
737 9148c8a7 2023-01-02 thomas err = send_packfile(&imsg, iev);
739 42f290d4 2023-03-05 thomas log_warnx("sending packfile: %s", err->msg);
742 42f290d4 2023-03-05 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
746 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
749 62ee7d94 2023-01-10 thomas if (!shut && check_cancelled(NULL) == NULL) {
750 62ee7d94 2023-01-10 thomas if (err &&
751 62ee7d94 2023-01-10 thomas gotd_imsg_send_error_event(iev, PROC_REPO_READ,
752 62ee7d94 2023-01-10 thomas client->id, err) == -1) {
753 62ee7d94 2023-01-10 thomas log_warnx("could not send error to parent: %s",
754 62ee7d94 2023-01-10 thomas err->msg);
756 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
758 62ee7d94 2023-01-10 thomas /* This pipe is dead. Remove its event handler */
759 62ee7d94 2023-01-10 thomas event_del(&iev->ev);
760 62ee7d94 2023-01-10 thomas event_loopexit(NULL);
764 62ee7d94 2023-01-10 thomas static const struct got_error *
765 62ee7d94 2023-01-10 thomas recv_connect(struct imsg *imsg)
767 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = &repo_read.session_iev;
768 62ee7d94 2023-01-10 thomas size_t datalen;
770 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
771 62ee7d94 2023-01-10 thomas if (datalen != 0)
772 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
774 62ee7d94 2023-01-10 thomas if (repo_read.session_fd != -1)
775 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
777 3d97effa 2024-01-31 thomas repo_read.session_fd = imsg_get_fd(imsg);
778 3d97effa 2024-01-31 thomas if (repo_read.session_fd == -1)
779 3d97effa 2024-01-31 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
781 62ee7d94 2023-01-10 thomas imsg_init(&iev->ibuf, repo_read.session_fd);
782 62ee7d94 2023-01-10 thomas iev->handler = repo_read_dispatch_session;
783 62ee7d94 2023-01-10 thomas iev->events = EV_READ;
784 62ee7d94 2023-01-10 thomas iev->handler_arg = NULL;
785 62ee7d94 2023-01-10 thomas event_set(&iev->ev, iev->ibuf.fd, EV_READ,
786 62ee7d94 2023-01-10 thomas repo_read_dispatch_session, iev);
787 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
789 62ee7d94 2023-01-10 thomas return NULL;
792 62ee7d94 2023-01-10 thomas static void
793 62ee7d94 2023-01-10 thomas repo_read_dispatch(int fd, short event, void *arg)
795 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
796 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
797 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
798 62ee7d94 2023-01-10 thomas struct imsg imsg;
799 62ee7d94 2023-01-10 thomas ssize_t n;
800 62ee7d94 2023-01-10 thomas int shut = 0;
801 62ee7d94 2023-01-10 thomas struct repo_read_client *client = &repo_read_client;
803 62ee7d94 2023-01-10 thomas if (event & EV_READ) {
804 62ee7d94 2023-01-10 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
805 62ee7d94 2023-01-10 thomas fatal("imsg_read error");
806 62ee7d94 2023-01-10 thomas if (n == 0) /* Connection closed. */
807 9148c8a7 2023-01-02 thomas shut = 1;
810 62ee7d94 2023-01-10 thomas if (event & EV_WRITE) {
811 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
812 62ee7d94 2023-01-10 thomas if (n == -1 && errno != EAGAIN)
813 62ee7d94 2023-01-10 thomas fatal("msgbuf_write");
814 62ee7d94 2023-01-10 thomas if (n == 0) /* Connection closed. */
815 62ee7d94 2023-01-10 thomas shut = 1;
818 62ee7d94 2023-01-10 thomas while (err == NULL && check_cancelled(NULL) == NULL) {
819 62ee7d94 2023-01-10 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
820 62ee7d94 2023-01-10 thomas fatal("%s: imsg_get", __func__);
821 62ee7d94 2023-01-10 thomas if (n == 0) /* No more messages. */
824 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
825 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CONNECT_REPO_CHILD:
826 62ee7d94 2023-01-10 thomas err = recv_connect(&imsg);
829 42f290d4 2023-03-05 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
833 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
836 3efd8e31 2022-10-23 thomas if (!shut && check_cancelled(NULL) == NULL) {
837 3efd8e31 2022-10-23 thomas if (err &&
838 3efd8e31 2022-10-23 thomas gotd_imsg_send_error_event(iev, PROC_REPO_READ,
839 9148c8a7 2023-01-02 thomas client->id, err) == -1) {
840 3efd8e31 2022-10-23 thomas log_warnx("could not send error to parent: %s",
841 3efd8e31 2022-10-23 thomas err->msg);
843 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(iev);
845 3efd8e31 2022-10-23 thomas /* This pipe is dead. Remove its event handler */
846 3efd8e31 2022-10-23 thomas event_del(&iev->ev);
847 3efd8e31 2022-10-23 thomas event_loopexit(NULL);
852 414e37cb 2022-12-30 thomas repo_read_main(const char *title, const char *repo_path,
853 414e37cb 2022-12-30 thomas int *pack_fds, int *temp_fds)
855 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
856 92db09ff 2023-02-17 thomas struct repo_read_client *client = &repo_read_client;
857 3efd8e31 2022-10-23 thomas struct gotd_imsgev iev;
859 92db09ff 2023-02-17 thomas client->fd = -1;
860 92db09ff 2023-02-17 thomas client->delta_cache_fd = -1;
861 92db09ff 2023-02-17 thomas client->pack_pipe = -1;
862 19aad72f 2023-03-01 thomas client->have_ids = got_object_idset_alloc();
863 19aad72f 2023-03-01 thomas if (client->have_ids == NULL) {
864 19aad72f 2023-03-01 thomas err = got_error_from_errno("got_object_idset_alloc");
865 19aad72f 2023-03-01 thomas goto done;
867 19aad72f 2023-03-01 thomas client->want_ids = got_object_idset_alloc();
868 19aad72f 2023-03-01 thomas if (client->want_ids == NULL) {
869 19aad72f 2023-03-01 thomas err = got_error_from_errno("got_object_idset_alloc");
870 19aad72f 2023-03-01 thomas goto done;
873 3efd8e31 2022-10-23 thomas repo_read.title = title;
874 3efd8e31 2022-10-23 thomas repo_read.pid = getpid();
875 3efd8e31 2022-10-23 thomas repo_read.pack_fds = pack_fds;
876 3efd8e31 2022-10-23 thomas repo_read.temp_fds = temp_fds;
877 62ee7d94 2023-01-10 thomas repo_read.session_fd = -1;
878 62ee7d94 2023-01-10 thomas repo_read.session_iev.ibuf.fd = -1;
880 414e37cb 2022-12-30 thomas err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
882 3efd8e31 2022-10-23 thomas goto done;
883 3efd8e31 2022-10-23 thomas if (!got_repo_is_bare(repo_read.repo)) {
884 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
885 3efd8e31 2022-10-23 thomas "bare git repository required");
886 3efd8e31 2022-10-23 thomas goto done;
889 3efd8e31 2022-10-23 thomas got_repo_temp_fds_set(repo_read.repo, temp_fds);
891 3efd8e31 2022-10-23 thomas signal(SIGINT, catch_sigint);
892 3efd8e31 2022-10-23 thomas signal(SIGTERM, catch_sigterm);
893 3efd8e31 2022-10-23 thomas signal(SIGPIPE, SIG_IGN);
894 3efd8e31 2022-10-23 thomas signal(SIGHUP, SIG_IGN);
896 bb3a6ce9 2022-11-17 thomas imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
897 3efd8e31 2022-10-23 thomas iev.handler = repo_read_dispatch;
898 3efd8e31 2022-10-23 thomas iev.events = EV_READ;
899 3efd8e31 2022-10-23 thomas iev.handler_arg = NULL;
900 3efd8e31 2022-10-23 thomas event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
902 85b37c72 2022-12-30 thomas if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
903 85b37c72 2022-12-30 thomas PROC_REPO_READ, -1, NULL, 0) == -1) {
904 85b37c72 2022-12-30 thomas err = got_error_from_errno("imsg compose REPO_CHILD_READY");
905 3efd8e31 2022-10-23 thomas goto done;
908 3efd8e31 2022-10-23 thomas event_dispatch();
911 3efd8e31 2022-10-23 thomas log_warnx("%s: %s", title, err->msg);
912 3efd8e31 2022-10-23 thomas repo_read_shutdown();
916 3efd8e31 2022-10-23 thomas repo_read_shutdown(void)
918 92db09ff 2023-02-17 thomas struct repo_read_client *client = &repo_read_client;
920 b1a47061 2024-03-30 thomas log_debug("%s: shutting down", repo_read.title);
922 19aad72f 2023-03-01 thomas if (client->have_ids)
923 19aad72f 2023-03-01 thomas got_object_idset_free(client->have_ids);
924 19aad72f 2023-03-01 thomas if (client->want_ids)
925 19aad72f 2023-03-01 thomas got_object_idset_free(client->want_ids);
926 92db09ff 2023-02-17 thomas if (client->fd != -1)
927 92db09ff 2023-02-17 thomas close(client->fd);
928 92db09ff 2023-02-17 thomas if (client->delta_cache_fd != -1)
929 92db09ff 2023-02-17 thomas close(client->delta_cache_fd);
930 92db09ff 2023-02-17 thomas if (client->pack_pipe != -1)
931 92db09ff 2023-02-17 thomas close(client->pack_pipe);
933 3efd8e31 2022-10-23 thomas if (repo_read.repo)
934 3efd8e31 2022-10-23 thomas got_repo_close(repo_read.repo);
935 3efd8e31 2022-10-23 thomas got_repo_pack_fds_close(repo_read.pack_fds);
936 3efd8e31 2022-10-23 thomas got_repo_temp_fds_close(repo_read.temp_fds);
937 62ee7d94 2023-01-10 thomas if (repo_read.session_fd != -1)
938 62ee7d94 2023-01-10 thomas close(repo_read.session_fd);