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 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
18 3efd8e31 2022-10-23 thomas #include <sys/types.h>
20 3efd8e31 2022-10-23 thomas #include <event.h>
21 3efd8e31 2022-10-23 thomas #include <errno.h>
22 3efd8e31 2022-10-23 thomas #include <imsg.h>
23 3efd8e31 2022-10-23 thomas #include <signal.h>
24 3efd8e31 2022-10-23 thomas #include <stdlib.h>
25 3efd8e31 2022-10-23 thomas #include <limits.h>
26 3efd8e31 2022-10-23 thomas #include <poll.h>
27 3efd8e31 2022-10-23 thomas #include <sha1.h>
28 3efd8e31 2022-10-23 thomas #include <siphash.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"
40 3efd8e31 2022-10-23 thomas #include "got_lib_delta.h"
41 3efd8e31 2022-10-23 thomas #include "got_lib_object.h"
42 3efd8e31 2022-10-23 thomas #include "got_lib_object_idset.h"
43 3efd8e31 2022-10-23 thomas #include "got_lib_sha1.h"
44 3efd8e31 2022-10-23 thomas #include "got_lib_pack.h"
45 3efd8e31 2022-10-23 thomas #include "got_lib_ratelimit.h"
46 3efd8e31 2022-10-23 thomas #include "got_lib_pack_create.h"
47 3efd8e31 2022-10-23 thomas #include "got_lib_poll.h"
49 3efd8e31 2022-10-23 thomas #include "log.h"
50 3efd8e31 2022-10-23 thomas #include "gotd.h"
51 3efd8e31 2022-10-23 thomas #include "repo_read.h"
53 3efd8e31 2022-10-23 thomas #ifndef nitems
54 3efd8e31 2022-10-23 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 3efd8e31 2022-10-23 thomas static struct repo_read {
58 3efd8e31 2022-10-23 thomas pid_t pid;
59 3efd8e31 2022-10-23 thomas const char *title;
60 3efd8e31 2022-10-23 thomas struct got_repository *repo;
61 3efd8e31 2022-10-23 thomas int *pack_fds;
62 3efd8e31 2022-10-23 thomas int *temp_fds;
63 62ee7d94 2023-01-10 thomas int session_fd;
64 62ee7d94 2023-01-10 thomas struct gotd_imsgev session_iev;
65 3efd8e31 2022-10-23 thomas } repo_read;
67 9148c8a7 2023-01-02 thomas static struct repo_read_client {
68 3efd8e31 2022-10-23 thomas uint32_t id;
70 3efd8e31 2022-10-23 thomas int delta_cache_fd;
71 3efd8e31 2022-10-23 thomas int report_progress;
72 cc4cc677 2022-10-28 thomas int pack_pipe;
73 3efd8e31 2022-10-23 thomas struct gotd_object_id_array want_ids;
74 3efd8e31 2022-10-23 thomas struct gotd_object_id_array have_ids;
75 9148c8a7 2023-01-02 thomas } repo_read_client;
77 3efd8e31 2022-10-23 thomas static volatile sig_atomic_t sigint_received;
78 3efd8e31 2022-10-23 thomas static volatile sig_atomic_t sigterm_received;
80 3efd8e31 2022-10-23 thomas static void
81 3efd8e31 2022-10-23 thomas catch_sigint(int signo)
83 3efd8e31 2022-10-23 thomas sigint_received = 1;
86 3efd8e31 2022-10-23 thomas static void
87 3efd8e31 2022-10-23 thomas catch_sigterm(int signo)
89 3efd8e31 2022-10-23 thomas sigterm_received = 1;
92 3efd8e31 2022-10-23 thomas static const struct got_error *
93 3efd8e31 2022-10-23 thomas check_cancelled(void *arg)
95 3efd8e31 2022-10-23 thomas if (sigint_received || sigterm_received)
96 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_CANCELLED);
98 3efd8e31 2022-10-23 thomas return NULL;
101 3efd8e31 2022-10-23 thomas static const struct got_error *
102 a7e9cbc6 2022-11-08 thomas send_symref(struct got_reference *symref, struct got_object_id *target_id,
103 a7e9cbc6 2022-11-08 thomas struct imsgbuf *ibuf)
105 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
106 3efd8e31 2022-10-23 thomas struct gotd_imsg_symref isymref;
107 3efd8e31 2022-10-23 thomas const char *refname = got_ref_get_name(symref);
108 3efd8e31 2022-10-23 thomas const char *target = got_ref_get_symref_target(symref);
109 3efd8e31 2022-10-23 thomas size_t len;
110 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
112 3efd8e31 2022-10-23 thomas memset(&isymref, 0, sizeof(isymref));
113 3efd8e31 2022-10-23 thomas isymref.name_len = strlen(refname);
114 3efd8e31 2022-10-23 thomas isymref.target_len = strlen(target);
115 3efd8e31 2022-10-23 thomas memcpy(isymref.target_id, target_id->sha1, sizeof(isymref.target_id));
117 3efd8e31 2022-10-23 thomas len = sizeof(isymref) + isymref.name_len + isymref.target_len;
118 3efd8e31 2022-10-23 thomas if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
119 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_NO_SPACE);
120 3efd8e31 2022-10-23 thomas goto done;
123 3efd8e31 2022-10-23 thomas wbuf = imsg_create(ibuf, GOTD_IMSG_SYMREF, 0, 0, len);
124 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
125 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create SYMREF");
126 3efd8e31 2022-10-23 thomas goto done;
129 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &isymref, sizeof(isymref)) == -1) {
130 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add SYMREF");
131 3efd8e31 2022-10-23 thomas goto done;
133 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, refname, isymref.name_len) == -1) {
134 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add SYMREF");
135 3efd8e31 2022-10-23 thomas goto done;
137 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, target, isymref.target_len) == -1) {
138 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add SYMREF");
139 3efd8e31 2022-10-23 thomas goto done;
142 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
143 3efd8e31 2022-10-23 thomas imsg_close(ibuf, wbuf);
145 3efd8e31 2022-10-23 thomas free(target_id);
146 3efd8e31 2022-10-23 thomas return err;
149 3efd8e31 2022-10-23 thomas static const struct got_error *
150 3efd8e31 2022-10-23 thomas send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
151 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf)
153 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
154 3efd8e31 2022-10-23 thomas struct got_tag_object *tag;
155 3efd8e31 2022-10-23 thomas size_t namelen, len;
156 3efd8e31 2022-10-23 thomas char *peeled_refname = NULL;
157 3efd8e31 2022-10-23 thomas struct got_object_id *id;
158 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
160 3efd8e31 2022-10-23 thomas err = got_object_tag_open(&tag, repo_read.repo, obj);
162 3efd8e31 2022-10-23 thomas return err;
164 3efd8e31 2022-10-23 thomas if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
165 3efd8e31 2022-10-23 thomas err = got_error_from_errno("asprintf");
166 3efd8e31 2022-10-23 thomas goto done;
169 3efd8e31 2022-10-23 thomas id = got_object_tag_get_object_id(tag);
170 3efd8e31 2022-10-23 thomas namelen = strlen(peeled_refname);
172 3efd8e31 2022-10-23 thomas len = sizeof(struct gotd_imsg_ref) + namelen;
173 3efd8e31 2022-10-23 thomas if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
174 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_NO_SPACE);
175 3efd8e31 2022-10-23 thomas goto done;
178 3efd8e31 2022-10-23 thomas wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
179 3efd8e31 2022-10-23 thomas repo_read.pid, len);
180 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
181 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create MREF");
182 3efd8e31 2022-10-23 thomas goto done;
185 3efd8e31 2022-10-23 thomas /* Keep in sync with struct gotd_imsg_ref definition. */
186 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
187 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add REF");
188 3efd8e31 2022-10-23 thomas goto done;
190 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
191 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add REF");
192 3efd8e31 2022-10-23 thomas goto done;
194 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
195 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add REF");
196 3efd8e31 2022-10-23 thomas goto done;
199 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
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 wbuf->fd = -1;
243 3efd8e31 2022-10-23 thomas imsg_close(ibuf, wbuf);
245 3efd8e31 2022-10-23 thomas err = got_object_open(&obj, repo_read.repo, id);
247 3efd8e31 2022-10-23 thomas goto done;
248 3efd8e31 2022-10-23 thomas if (obj->type == GOT_OBJ_TYPE_TAG)
249 3efd8e31 2022-10-23 thomas err = send_peeled_tag_ref(ref, obj, ibuf);
252 3efd8e31 2022-10-23 thomas got_object_close(obj);
253 3efd8e31 2022-10-23 thomas free(id);
254 3efd8e31 2022-10-23 thomas return err;
257 3efd8e31 2022-10-23 thomas static const struct got_error *
258 9148c8a7 2023-01-02 thomas list_refs(struct imsg *imsg)
260 3efd8e31 2022-10-23 thomas const struct got_error *err;
261 9148c8a7 2023-01-02 thomas struct repo_read_client *client = &repo_read_client;
262 3efd8e31 2022-10-23 thomas struct got_reflist_head refs;
263 3efd8e31 2022-10-23 thomas struct got_reflist_entry *re;
264 3efd8e31 2022-10-23 thomas struct gotd_imsg_list_refs_internal ireq;
265 3efd8e31 2022-10-23 thomas size_t datalen;
266 3efd8e31 2022-10-23 thomas struct gotd_imsg_reflist irefs;
267 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
268 3efd8e31 2022-10-23 thomas int client_fd = imsg->fd;
269 a7e9cbc6 2022-11-08 thomas struct got_object_id *head_target_id = NULL;
271 3efd8e31 2022-10-23 thomas TAILQ_INIT(&refs);
273 3efd8e31 2022-10-23 thomas if (client_fd == -1)
274 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
276 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
277 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
278 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
279 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
281 9148c8a7 2023-01-02 thomas if (ireq.client_id == 0)
282 9148c8a7 2023-01-02 thomas return got_error(GOT_ERR_CLIENT_ID);
283 9148c8a7 2023-01-02 thomas if (client->id != 0) {
284 9148c8a7 2023-01-02 thomas return got_error_msg(GOT_ERR_CLIENT_ID,
285 9148c8a7 2023-01-02 thomas "duplicate list-refs request");
287 9148c8a7 2023-01-02 thomas client->id = ireq.client_id;
288 9148c8a7 2023-01-02 thomas client->fd = client_fd;
289 9148c8a7 2023-01-02 thomas client->delta_cache_fd = -1;
290 9148c8a7 2023-01-02 thomas client->pack_pipe = -1;
292 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, client_fd);
294 3efd8e31 2022-10-23 thomas err = got_ref_list(&refs, repo_read.repo, "",
295 3efd8e31 2022-10-23 thomas got_ref_cmp_by_name, NULL);
297 3efd8e31 2022-10-23 thomas return err;
299 3efd8e31 2022-10-23 thomas memset(&irefs, 0, sizeof(irefs));
300 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(re, &refs, entry) {
301 3efd8e31 2022-10-23 thomas struct got_object_id *id;
302 3efd8e31 2022-10-23 thomas int obj_type;
304 3efd8e31 2022-10-23 thomas if (got_ref_is_symbolic(re->ref)) {
305 3efd8e31 2022-10-23 thomas const char *refname = got_ref_get_name(re->ref);
306 a7e9cbc6 2022-11-08 thomas if (strcmp(refname, GOT_REF_HEAD) != 0)
307 a7e9cbc6 2022-11-08 thomas continue;
308 a7e9cbc6 2022-11-08 thomas err = got_ref_resolve(&head_target_id, repo_read.repo,
309 a7e9cbc6 2022-11-08 thomas re->ref);
310 a7e9cbc6 2022-11-08 thomas if (err) {
311 a7e9cbc6 2022-11-08 thomas if (err->code != GOT_ERR_NOT_REF)
312 a7e9cbc6 2022-11-08 thomas return err;
314 a7e9cbc6 2022-11-08 thomas * HEAD points to a non-existent branch.
315 a7e9cbc6 2022-11-08 thomas * Do not advertise it.
316 a7e9cbc6 2022-11-08 thomas * Matches git-daemon's behaviour.
318 a7e9cbc6 2022-11-08 thomas head_target_id = NULL;
319 a7e9cbc6 2022-11-08 thomas err = NULL;
321 3efd8e31 2022-10-23 thomas irefs.nrefs++;
322 3efd8e31 2022-10-23 thomas continue;
325 3efd8e31 2022-10-23 thomas irefs.nrefs++;
327 3efd8e31 2022-10-23 thomas /* Account for a peeled tag refs. */
328 3efd8e31 2022-10-23 thomas err = got_ref_resolve(&id, repo_read.repo, re->ref);
330 3efd8e31 2022-10-23 thomas goto done;
331 8652e561 2023-01-14 thomas err = got_object_get_type(&obj_type, repo_read.repo, id);
332 3efd8e31 2022-10-23 thomas free(id);
334 3efd8e31 2022-10-23 thomas goto done;
335 3efd8e31 2022-10-23 thomas if (obj_type == GOT_OBJ_TYPE_TAG)
336 3efd8e31 2022-10-23 thomas irefs.nrefs++;
339 3efd8e31 2022-10-23 thomas if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
340 3efd8e31 2022-10-23 thomas repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
341 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_compose REFLIST");
342 3efd8e31 2022-10-23 thomas goto done;
346 3efd8e31 2022-10-23 thomas * Send the HEAD symref first. In Git-protocol versions < 2
347 3efd8e31 2022-10-23 thomas * the HEAD symref must be announced on the initial line of
348 3efd8e31 2022-10-23 thomas * the server's ref advertisement.
349 3efd8e31 2022-10-23 thomas * For now, we do not advertise symrefs other than HEAD.
351 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(re, &refs, entry) {
352 3efd8e31 2022-10-23 thomas if (!got_ref_is_symbolic(re->ref) ||
353 a7e9cbc6 2022-11-08 thomas strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0 ||
354 a7e9cbc6 2022-11-08 thomas head_target_id == NULL)
355 3efd8e31 2022-10-23 thomas continue;
356 a7e9cbc6 2022-11-08 thomas err = send_symref(re->ref, head_target_id, &ibuf);
358 3efd8e31 2022-10-23 thomas goto done;
361 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(re, &refs, entry) {
362 3efd8e31 2022-10-23 thomas if (got_ref_is_symbolic(re->ref))
363 3efd8e31 2022-10-23 thomas continue;
364 3efd8e31 2022-10-23 thomas err = send_ref(re->ref, &ibuf);
366 3efd8e31 2022-10-23 thomas goto done;
369 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(&ibuf);
371 3efd8e31 2022-10-23 thomas got_ref_list_free(&refs);
372 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
373 3efd8e31 2022-10-23 thomas return err;
376 3efd8e31 2022-10-23 thomas static const struct got_error *
377 3efd8e31 2022-10-23 thomas record_object_id(struct gotd_object_id_array *array, struct got_object_id *id)
379 3efd8e31 2022-10-23 thomas const size_t alloc_chunksz = 256;
381 3efd8e31 2022-10-23 thomas if (array->ids == NULL) {
382 3efd8e31 2022-10-23 thomas array->ids = reallocarray(NULL, alloc_chunksz,
383 3efd8e31 2022-10-23 thomas sizeof(*array->ids));
384 3efd8e31 2022-10-23 thomas if (array->ids == NULL)
385 3efd8e31 2022-10-23 thomas return got_error_from_errno("reallocarray");
386 3efd8e31 2022-10-23 thomas array->nalloc = alloc_chunksz;
387 3efd8e31 2022-10-23 thomas array->nids = 0;
388 3efd8e31 2022-10-23 thomas } else if (array->nalloc <= array->nids) {
389 3efd8e31 2022-10-23 thomas struct got_object_id **new;
390 3efd8e31 2022-10-23 thomas new = recallocarray(array->ids, array->nalloc,
391 3efd8e31 2022-10-23 thomas array->nalloc + alloc_chunksz, sizeof(*new));
392 3efd8e31 2022-10-23 thomas if (new == NULL)
393 3efd8e31 2022-10-23 thomas return got_error_from_errno("recallocarray");
394 3efd8e31 2022-10-23 thomas array->ids = new;
395 3efd8e31 2022-10-23 thomas array->nalloc += alloc_chunksz;
398 3efd8e31 2022-10-23 thomas array->ids[array->nids] = got_object_id_dup(id);
399 3efd8e31 2022-10-23 thomas if (array->ids[array->nids] == NULL)
400 3efd8e31 2022-10-23 thomas return got_error_from_errno("got_object_id_dup");
401 3efd8e31 2022-10-23 thomas array->nids++;
402 3efd8e31 2022-10-23 thomas return NULL;
405 3efd8e31 2022-10-23 thomas static void
406 3efd8e31 2022-10-23 thomas free_object_ids(struct gotd_object_id_array *array)
408 3efd8e31 2022-10-23 thomas size_t i;
410 3efd8e31 2022-10-23 thomas for (i = 0; i < array->nids; i++)
411 3efd8e31 2022-10-23 thomas free(array->ids[i]);
412 3efd8e31 2022-10-23 thomas free(array->ids);
414 3efd8e31 2022-10-23 thomas array->ids = NULL;
415 3efd8e31 2022-10-23 thomas array->nalloc = 0;
416 3efd8e31 2022-10-23 thomas array->nids = 0;
419 3efd8e31 2022-10-23 thomas static const struct got_error *
420 9148c8a7 2023-01-02 thomas recv_want(struct imsg *imsg)
422 3efd8e31 2022-10-23 thomas const struct got_error *err;
423 9148c8a7 2023-01-02 thomas struct repo_read_client *client = &repo_read_client;
424 3efd8e31 2022-10-23 thomas struct gotd_imsg_want iwant;
425 3efd8e31 2022-10-23 thomas size_t datalen;
426 3efd8e31 2022-10-23 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
427 3efd8e31 2022-10-23 thomas struct got_object_id id;
428 3efd8e31 2022-10-23 thomas int obj_type;
429 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
431 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
432 3efd8e31 2022-10-23 thomas if (datalen != sizeof(iwant))
433 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
434 3efd8e31 2022-10-23 thomas memcpy(&iwant, imsg->data, sizeof(iwant));
436 3efd8e31 2022-10-23 thomas memset(&id, 0, sizeof(id));
437 3efd8e31 2022-10-23 thomas memcpy(id.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
439 3efd8e31 2022-10-23 thomas if (log_getverbose() > 0 &&
440 3efd8e31 2022-10-23 thomas got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
441 3efd8e31 2022-10-23 thomas log_debug("client wants %s", hex);
443 9148c8a7 2023-01-02 thomas imsg_init(&ibuf, client->fd);
445 3efd8e31 2022-10-23 thomas err = got_object_get_type(&obj_type, repo_read.repo, &id);
447 3efd8e31 2022-10-23 thomas return err;
449 3efd8e31 2022-10-23 thomas if (obj_type != GOT_OBJ_TYPE_COMMIT &&
450 3efd8e31 2022-10-23 thomas obj_type != GOT_OBJ_TYPE_TAG)
451 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_OBJ_TYPE);
453 9148c8a7 2023-01-02 thomas err = record_object_id(&client->want_ids, &id);
455 3efd8e31 2022-10-23 thomas return err;
457 3efd8e31 2022-10-23 thomas gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
458 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
459 3efd8e31 2022-10-23 thomas return err;
462 3efd8e31 2022-10-23 thomas static const struct got_error *
463 9148c8a7 2023-01-02 thomas recv_have(struct imsg *imsg)
465 3efd8e31 2022-10-23 thomas const struct got_error *err;
466 9148c8a7 2023-01-02 thomas struct repo_read_client *client = &repo_read_client;
467 3efd8e31 2022-10-23 thomas struct gotd_imsg_have ihave;
468 3efd8e31 2022-10-23 thomas size_t datalen;
469 3efd8e31 2022-10-23 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
470 3efd8e31 2022-10-23 thomas struct got_object_id id;
471 3efd8e31 2022-10-23 thomas int obj_type;
472 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
474 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
475 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ihave))
476 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
477 3efd8e31 2022-10-23 thomas memcpy(&ihave, imsg->data, sizeof(ihave));
479 3efd8e31 2022-10-23 thomas memset(&id, 0, sizeof(id));
480 3efd8e31 2022-10-23 thomas memcpy(id.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
482 3efd8e31 2022-10-23 thomas if (log_getverbose() > 0 &&
483 3efd8e31 2022-10-23 thomas got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
484 3efd8e31 2022-10-23 thomas log_debug("client has %s", hex);
486 9148c8a7 2023-01-02 thomas imsg_init(&ibuf, client->fd);
488 3efd8e31 2022-10-23 thomas err = got_object_get_type(&obj_type, repo_read.repo, &id);
489 3efd8e31 2022-10-23 thomas if (err) {
490 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_NO_OBJ) {
491 3efd8e31 2022-10-23 thomas gotd_imsg_send_nak(&id, &ibuf,
492 3efd8e31 2022-10-23 thomas PROC_REPO_READ, repo_read.pid);
493 3efd8e31 2022-10-23 thomas err = NULL;
495 3efd8e31 2022-10-23 thomas goto done;
498 3efd8e31 2022-10-23 thomas if (obj_type != GOT_OBJ_TYPE_COMMIT &&
499 3efd8e31 2022-10-23 thomas obj_type != GOT_OBJ_TYPE_TAG) {
500 3efd8e31 2022-10-23 thomas gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
501 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_OBJ_TYPE);
502 3efd8e31 2022-10-23 thomas goto done;
505 9148c8a7 2023-01-02 thomas err = record_object_id(&client->have_ids, &id);
507 3efd8e31 2022-10-23 thomas return err;
509 3efd8e31 2022-10-23 thomas gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
511 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
512 3efd8e31 2022-10-23 thomas return err;
515 3efd8e31 2022-10-23 thomas struct repo_read_pack_progress_arg {
516 3efd8e31 2022-10-23 thomas int report_progress;
517 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf;
518 3efd8e31 2022-10-23 thomas int sent_ready;
521 3efd8e31 2022-10-23 thomas static const struct got_error *
522 3efd8e31 2022-10-23 thomas pack_progress(void *arg, int ncolored, int nfound, int ntrees,
523 3efd8e31 2022-10-23 thomas off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
524 3efd8e31 2022-10-23 thomas int nobj_written)
526 3efd8e31 2022-10-23 thomas struct repo_read_pack_progress_arg *a = arg;
527 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_progress iprog;
530 3efd8e31 2022-10-23 thomas if (!a->report_progress)
531 3efd8e31 2022-10-23 thomas return NULL;
532 3efd8e31 2022-10-23 thomas if (packfile_size > 0 && a->sent_ready)
533 3efd8e31 2022-10-23 thomas return NULL;
535 3efd8e31 2022-10-23 thomas memset(&iprog, 0, sizeof(iprog));
536 3efd8e31 2022-10-23 thomas iprog.ncolored = ncolored;
537 3efd8e31 2022-10-23 thomas iprog.nfound = nfound;
538 3efd8e31 2022-10-23 thomas iprog.ntrees = ntrees;
539 3efd8e31 2022-10-23 thomas iprog.packfile_size = packfile_size;
540 3efd8e31 2022-10-23 thomas iprog.ncommits = ncommits;
541 3efd8e31 2022-10-23 thomas iprog.nobj_total = nobj_total;
542 3efd8e31 2022-10-23 thomas iprog.nobj_deltify = nobj_deltify;
543 3efd8e31 2022-10-23 thomas iprog.nobj_written = nobj_written;
545 3efd8e31 2022-10-23 thomas /* Using synchronous writes since we are blocking the event loop. */
546 3efd8e31 2022-10-23 thomas if (packfile_size == 0) {
547 3efd8e31 2022-10-23 thomas ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
548 3efd8e31 2022-10-23 thomas PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
549 3efd8e31 2022-10-23 thomas if (ret == -1) {
550 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg compose "
551 3efd8e31 2022-10-23 thomas "PACKFILE_PROGRESS");
554 3efd8e31 2022-10-23 thomas a->sent_ready = 1;
555 3efd8e31 2022-10-23 thomas ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
556 3efd8e31 2022-10-23 thomas PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
557 3efd8e31 2022-10-23 thomas if (ret == -1) {
558 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg compose "
559 3efd8e31 2022-10-23 thomas "PACKFILE_READY");
563 3efd8e31 2022-10-23 thomas return gotd_imsg_flush(a->ibuf);
566 3efd8e31 2022-10-23 thomas static const struct got_error *
567 9148c8a7 2023-01-02 thomas receive_delta_cache_fd(struct imsg *imsg,
568 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev)
570 9148c8a7 2023-01-02 thomas struct repo_read_client *client = &repo_read_client;
571 3efd8e31 2022-10-23 thomas struct gotd_imsg_send_packfile ireq;
572 3efd8e31 2022-10-23 thomas size_t datalen;
574 3efd8e31 2022-10-23 thomas log_debug("receving delta cache file");
576 3efd8e31 2022-10-23 thomas if (imsg->fd == -1)
577 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
579 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
580 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
581 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
582 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
584 9148c8a7 2023-01-02 thomas if (client->delta_cache_fd != -1)
585 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
587 9148c8a7 2023-01-02 thomas client->delta_cache_fd = imsg->fd;
588 9148c8a7 2023-01-02 thomas client->report_progress = ireq.report_progress;
589 3efd8e31 2022-10-23 thomas return NULL;
592 3efd8e31 2022-10-23 thomas static const struct got_error *
593 9148c8a7 2023-01-02 thomas receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
595 9148c8a7 2023-01-02 thomas struct repo_read_client *client = &repo_read_client;
596 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_pipe ireq;
597 3efd8e31 2022-10-23 thomas size_t datalen;
599 3efd8e31 2022-10-23 thomas log_debug("receving pack pipe descriptor");
601 3efd8e31 2022-10-23 thomas if (imsg->fd == -1)
602 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
604 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
605 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
606 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
607 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
609 9148c8a7 2023-01-02 thomas if (client->pack_pipe != -1)
610 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
612 9148c8a7 2023-01-02 thomas client->pack_pipe = imsg->fd;
613 3efd8e31 2022-10-23 thomas return NULL;
616 3efd8e31 2022-10-23 thomas static const struct got_error *
617 9148c8a7 2023-01-02 thomas send_packfile(struct imsg *imsg, struct gotd_imsgev *iev)
619 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
620 9148c8a7 2023-01-02 thomas struct repo_read_client *client = &repo_read_client;
621 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_done idone;
622 3efd8e31 2022-10-23 thomas uint8_t packsha1[SHA1_DIGEST_LENGTH];
623 3efd8e31 2022-10-23 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
624 3efd8e31 2022-10-23 thomas FILE *delta_cache = NULL;
625 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
626 3efd8e31 2022-10-23 thomas struct repo_read_pack_progress_arg pa;
627 3efd8e31 2022-10-23 thomas struct got_ratelimit rl;
629 3efd8e31 2022-10-23 thomas log_debug("packfile request received");
631 3efd8e31 2022-10-23 thomas got_ratelimit_init(&rl, 2, 0);
633 cc4cc677 2022-10-28 thomas if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
634 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
636 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, client->fd);
638 3efd8e31 2022-10-23 thomas delta_cache = fdopen(client->delta_cache_fd, "w+");
639 3efd8e31 2022-10-23 thomas if (delta_cache == NULL) {
640 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fdopen");
641 3efd8e31 2022-10-23 thomas goto done;
643 3efd8e31 2022-10-23 thomas client->delta_cache_fd = -1;
645 3efd8e31 2022-10-23 thomas memset(&pa, 0, sizeof(pa));
646 3efd8e31 2022-10-23 thomas pa.ibuf = &ibuf;
647 3efd8e31 2022-10-23 thomas pa.report_progress = client->report_progress;
649 cc4cc677 2022-10-28 thomas err = got_pack_create(packsha1, client->pack_pipe, delta_cache,
650 3efd8e31 2022-10-23 thomas client->have_ids.ids, client->have_ids.nids,
651 3efd8e31 2022-10-23 thomas client->want_ids.ids, client->want_ids.nids,
652 3efd8e31 2022-10-23 thomas repo_read.repo, 0, 1, pack_progress, &pa, &rl,
653 3efd8e31 2022-10-23 thomas check_cancelled, NULL);
655 3efd8e31 2022-10-23 thomas goto done;
657 3efd8e31 2022-10-23 thomas if (log_getverbose() > 0 &&
658 3efd8e31 2022-10-23 thomas got_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
659 3efd8e31 2022-10-23 thomas log_debug("sent pack-%s.pack", hex);
661 3efd8e31 2022-10-23 thomas memset(&idone, 0, sizeof(idone));
662 3efd8e31 2022-10-23 thomas idone.client_id = client->id;
663 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
664 3efd8e31 2022-10-23 thomas PROC_REPO_READ, -1, &idone, sizeof(idone)) == -1)
665 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg compose PACKFILE_DONE");
667 3efd8e31 2022-10-23 thomas if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
668 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fclose");
669 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
670 3efd8e31 2022-10-23 thomas return err;
673 3efd8e31 2022-10-23 thomas static const struct got_error *
674 3efd8e31 2022-10-23 thomas recv_disconnect(struct imsg *imsg)
676 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
677 3efd8e31 2022-10-23 thomas struct gotd_imsg_disconnect idisconnect;
678 3efd8e31 2022-10-23 thomas size_t datalen;
679 9148c8a7 2023-01-02 thomas int delta_cache_fd, pack_pipe;
680 9148c8a7 2023-01-02 thomas struct repo_read_client *client = &repo_read_client;
682 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
683 3efd8e31 2022-10-23 thomas if (datalen != sizeof(idisconnect))
684 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
685 3efd8e31 2022-10-23 thomas memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
687 3efd8e31 2022-10-23 thomas log_debug("client disconnecting");
689 3efd8e31 2022-10-23 thomas free_object_ids(&client->have_ids);
690 3efd8e31 2022-10-23 thomas free_object_ids(&client->want_ids);
691 9148c8a7 2023-01-02 thomas if (close(client->fd) == -1)
692 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
693 9148c8a7 2023-01-02 thomas delta_cache_fd = client->delta_cache_fd;
694 3efd8e31 2022-10-23 thomas if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
695 3efd8e31 2022-10-23 thomas return got_error_from_errno("close");
696 9148c8a7 2023-01-02 thomas pack_pipe = client->pack_pipe;
697 cc4cc677 2022-10-28 thomas if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
698 3efd8e31 2022-10-23 thomas return got_error_from_errno("close");
699 3efd8e31 2022-10-23 thomas return err;
702 3efd8e31 2022-10-23 thomas static void
703 62ee7d94 2023-01-10 thomas repo_read_dispatch_session(int fd, short event, void *arg)
705 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
706 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev = arg;
707 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf = &iev->ibuf;
708 3efd8e31 2022-10-23 thomas struct imsg imsg;
709 3efd8e31 2022-10-23 thomas ssize_t n;
710 3efd8e31 2022-10-23 thomas int shut = 0;
711 9148c8a7 2023-01-02 thomas struct repo_read_client *client = &repo_read_client;
713 3efd8e31 2022-10-23 thomas if (event & EV_READ) {
714 3efd8e31 2022-10-23 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
715 3efd8e31 2022-10-23 thomas fatal("imsg_read error");
716 3efd8e31 2022-10-23 thomas if (n == 0) /* Connection closed. */
717 3efd8e31 2022-10-23 thomas shut = 1;
720 3efd8e31 2022-10-23 thomas if (event & EV_WRITE) {
721 3efd8e31 2022-10-23 thomas n = msgbuf_write(&ibuf->w);
722 3efd8e31 2022-10-23 thomas if (n == -1 && errno != EAGAIN)
723 3efd8e31 2022-10-23 thomas fatal("msgbuf_write");
724 3efd8e31 2022-10-23 thomas if (n == 0) /* Connection closed. */
725 3efd8e31 2022-10-23 thomas shut = 1;
728 3efd8e31 2022-10-23 thomas while (err == NULL && check_cancelled(NULL) == NULL) {
729 3efd8e31 2022-10-23 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
730 3efd8e31 2022-10-23 thomas fatal("%s: imsg_get", __func__);
731 3efd8e31 2022-10-23 thomas if (n == 0) /* No more messages. */
734 9148c8a7 2023-01-02 thomas if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
735 9148c8a7 2023-01-02 thomas client->id == 0) {
736 9148c8a7 2023-01-02 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
740 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
741 3efd8e31 2022-10-23 thomas case GOTD_IMSG_LIST_REFS_INTERNAL:
742 9148c8a7 2023-01-02 thomas err = list_refs(&imsg);
744 3efd8e31 2022-10-23 thomas log_warnx("%s: ls-refs: %s", repo_read.title,
745 3efd8e31 2022-10-23 thomas err->msg);
747 3efd8e31 2022-10-23 thomas case GOTD_IMSG_WANT:
748 9148c8a7 2023-01-02 thomas err = recv_want(&imsg);
750 3efd8e31 2022-10-23 thomas log_warnx("%s: want-line: %s", repo_read.title,
751 3efd8e31 2022-10-23 thomas err->msg);
753 3efd8e31 2022-10-23 thomas case GOTD_IMSG_HAVE:
754 9148c8a7 2023-01-02 thomas err = recv_have(&imsg);
756 3efd8e31 2022-10-23 thomas log_warnx("%s: have-line: %s", repo_read.title,
757 3efd8e31 2022-10-23 thomas err->msg);
759 3efd8e31 2022-10-23 thomas case GOTD_IMSG_SEND_PACKFILE:
760 9148c8a7 2023-01-02 thomas err = receive_delta_cache_fd(&imsg, iev);
762 3efd8e31 2022-10-23 thomas log_warnx("%s: receiving delta cache: %s",
763 3efd8e31 2022-10-23 thomas repo_read.title, err->msg);
765 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_PIPE:
766 9148c8a7 2023-01-02 thomas err = receive_pack_pipe(&imsg, iev);
767 3efd8e31 2022-10-23 thomas if (err) {
768 3efd8e31 2022-10-23 thomas log_warnx("%s: receiving pack pipe: %s",
769 3efd8e31 2022-10-23 thomas repo_read.title, err->msg);
772 9148c8a7 2023-01-02 thomas err = send_packfile(&imsg, iev);
774 3efd8e31 2022-10-23 thomas log_warnx("%s: sending packfile: %s",
775 3efd8e31 2022-10-23 thomas repo_read.title, err->msg);
777 3efd8e31 2022-10-23 thomas case GOTD_IMSG_DISCONNECT:
778 3efd8e31 2022-10-23 thomas err = recv_disconnect(&imsg);
780 3efd8e31 2022-10-23 thomas log_warnx("%s: disconnect: %s",
781 3efd8e31 2022-10-23 thomas repo_read.title, err->msg);
782 62ee7d94 2023-01-10 thomas shut = 1;
785 62ee7d94 2023-01-10 thomas log_debug("%s: unexpected imsg %d", repo_read.title,
786 62ee7d94 2023-01-10 thomas imsg.hdr.type);
790 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
793 62ee7d94 2023-01-10 thomas if (!shut && check_cancelled(NULL) == NULL) {
794 62ee7d94 2023-01-10 thomas if (err &&
795 62ee7d94 2023-01-10 thomas gotd_imsg_send_error_event(iev, PROC_REPO_READ,
796 62ee7d94 2023-01-10 thomas client->id, err) == -1) {
797 62ee7d94 2023-01-10 thomas log_warnx("could not send error to parent: %s",
798 62ee7d94 2023-01-10 thomas err->msg);
800 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
802 62ee7d94 2023-01-10 thomas /* This pipe is dead. Remove its event handler */
803 62ee7d94 2023-01-10 thomas event_del(&iev->ev);
804 62ee7d94 2023-01-10 thomas event_loopexit(NULL);
808 62ee7d94 2023-01-10 thomas static const struct got_error *
809 62ee7d94 2023-01-10 thomas recv_connect(struct imsg *imsg)
811 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = &repo_read.session_iev;
812 62ee7d94 2023-01-10 thomas size_t datalen;
814 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
815 62ee7d94 2023-01-10 thomas if (datalen != 0)
816 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
817 62ee7d94 2023-01-10 thomas if (imsg->fd == -1)
818 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
820 62ee7d94 2023-01-10 thomas if (repo_read.session_fd != -1)
821 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
823 62ee7d94 2023-01-10 thomas repo_read.session_fd = imsg->fd;
825 62ee7d94 2023-01-10 thomas imsg_init(&iev->ibuf, repo_read.session_fd);
826 62ee7d94 2023-01-10 thomas iev->handler = repo_read_dispatch_session;
827 62ee7d94 2023-01-10 thomas iev->events = EV_READ;
828 62ee7d94 2023-01-10 thomas iev->handler_arg = NULL;
829 62ee7d94 2023-01-10 thomas event_set(&iev->ev, iev->ibuf.fd, EV_READ,
830 62ee7d94 2023-01-10 thomas repo_read_dispatch_session, iev);
831 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
833 62ee7d94 2023-01-10 thomas return NULL;
836 62ee7d94 2023-01-10 thomas static void
837 62ee7d94 2023-01-10 thomas repo_read_dispatch(int fd, short event, void *arg)
839 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
840 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
841 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
842 62ee7d94 2023-01-10 thomas struct imsg imsg;
843 62ee7d94 2023-01-10 thomas ssize_t n;
844 62ee7d94 2023-01-10 thomas int shut = 0;
845 62ee7d94 2023-01-10 thomas struct repo_read_client *client = &repo_read_client;
847 62ee7d94 2023-01-10 thomas if (event & EV_READ) {
848 62ee7d94 2023-01-10 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
849 62ee7d94 2023-01-10 thomas fatal("imsg_read error");
850 62ee7d94 2023-01-10 thomas if (n == 0) /* Connection closed. */
851 9148c8a7 2023-01-02 thomas shut = 1;
854 62ee7d94 2023-01-10 thomas if (event & EV_WRITE) {
855 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
856 62ee7d94 2023-01-10 thomas if (n == -1 && errno != EAGAIN)
857 62ee7d94 2023-01-10 thomas fatal("msgbuf_write");
858 62ee7d94 2023-01-10 thomas if (n == 0) /* Connection closed. */
859 62ee7d94 2023-01-10 thomas shut = 1;
862 62ee7d94 2023-01-10 thomas while (err == NULL && check_cancelled(NULL) == NULL) {
863 62ee7d94 2023-01-10 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
864 62ee7d94 2023-01-10 thomas fatal("%s: imsg_get", __func__);
865 62ee7d94 2023-01-10 thomas if (n == 0) /* No more messages. */
868 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
869 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CONNECT_REPO_CHILD:
870 62ee7d94 2023-01-10 thomas err = recv_connect(&imsg);
873 3efd8e31 2022-10-23 thomas log_debug("%s: unexpected imsg %d", repo_read.title,
874 3efd8e31 2022-10-23 thomas imsg.hdr.type);
878 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
881 3efd8e31 2022-10-23 thomas if (!shut && check_cancelled(NULL) == NULL) {
882 3efd8e31 2022-10-23 thomas if (err &&
883 3efd8e31 2022-10-23 thomas gotd_imsg_send_error_event(iev, PROC_REPO_READ,
884 9148c8a7 2023-01-02 thomas client->id, err) == -1) {
885 3efd8e31 2022-10-23 thomas log_warnx("could not send error to parent: %s",
886 3efd8e31 2022-10-23 thomas err->msg);
888 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(iev);
890 3efd8e31 2022-10-23 thomas /* This pipe is dead. Remove its event handler */
891 3efd8e31 2022-10-23 thomas event_del(&iev->ev);
892 3efd8e31 2022-10-23 thomas event_loopexit(NULL);
897 414e37cb 2022-12-30 thomas repo_read_main(const char *title, const char *repo_path,
898 414e37cb 2022-12-30 thomas int *pack_fds, int *temp_fds)
900 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
901 3efd8e31 2022-10-23 thomas struct gotd_imsgev iev;
903 3efd8e31 2022-10-23 thomas repo_read.title = title;
904 3efd8e31 2022-10-23 thomas repo_read.pid = getpid();
905 3efd8e31 2022-10-23 thomas repo_read.pack_fds = pack_fds;
906 3efd8e31 2022-10-23 thomas repo_read.temp_fds = temp_fds;
907 62ee7d94 2023-01-10 thomas repo_read.session_fd = -1;
908 62ee7d94 2023-01-10 thomas repo_read.session_iev.ibuf.fd = -1;
910 414e37cb 2022-12-30 thomas err = got_repo_open(&repo_read.repo, repo_path, NULL, pack_fds);
912 3efd8e31 2022-10-23 thomas goto done;
913 3efd8e31 2022-10-23 thomas if (!got_repo_is_bare(repo_read.repo)) {
914 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
915 3efd8e31 2022-10-23 thomas "bare git repository required");
916 3efd8e31 2022-10-23 thomas goto done;
919 3efd8e31 2022-10-23 thomas got_repo_temp_fds_set(repo_read.repo, temp_fds);
921 3efd8e31 2022-10-23 thomas signal(SIGINT, catch_sigint);
922 3efd8e31 2022-10-23 thomas signal(SIGTERM, catch_sigterm);
923 3efd8e31 2022-10-23 thomas signal(SIGPIPE, SIG_IGN);
924 3efd8e31 2022-10-23 thomas signal(SIGHUP, SIG_IGN);
926 bb3a6ce9 2022-11-17 thomas imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
927 3efd8e31 2022-10-23 thomas iev.handler = repo_read_dispatch;
928 3efd8e31 2022-10-23 thomas iev.events = EV_READ;
929 3efd8e31 2022-10-23 thomas iev.handler_arg = NULL;
930 3efd8e31 2022-10-23 thomas event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
932 85b37c72 2022-12-30 thomas if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
933 85b37c72 2022-12-30 thomas PROC_REPO_READ, -1, NULL, 0) == -1) {
934 85b37c72 2022-12-30 thomas err = got_error_from_errno("imsg compose REPO_CHILD_READY");
935 3efd8e31 2022-10-23 thomas goto done;
938 3efd8e31 2022-10-23 thomas event_dispatch();
941 3efd8e31 2022-10-23 thomas log_warnx("%s: %s", title, err->msg);
942 3efd8e31 2022-10-23 thomas repo_read_shutdown();
946 3efd8e31 2022-10-23 thomas repo_read_shutdown(void)
948 3efd8e31 2022-10-23 thomas log_debug("%s: shutting down", repo_read.title);
949 3efd8e31 2022-10-23 thomas if (repo_read.repo)
950 3efd8e31 2022-10-23 thomas got_repo_close(repo_read.repo);
951 3efd8e31 2022-10-23 thomas got_repo_pack_fds_close(repo_read.pack_fds);
952 3efd8e31 2022-10-23 thomas got_repo_temp_fds_close(repo_read.temp_fds);
953 62ee7d94 2023-01-10 thomas if (repo_read.session_fd != -1)
954 62ee7d94 2023-01-10 thomas close(repo_read.session_fd);