Blame


1 3efd8e31 2022-10-23 thomas /*
2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 3efd8e31 2022-10-23 thomas *
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.
7 3efd8e31 2022-10-23 thomas *
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.
15 3efd8e31 2022-10-23 thomas */
16 3efd8e31 2022-10-23 thomas
17 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
18 3efd8e31 2022-10-23 thomas #include <sys/types.h>
19 3efd8e31 2022-10-23 thomas
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>
32 3efd8e31 2022-10-23 thomas
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 3efd8e31 2022-10-23 thomas
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"
48 3efd8e31 2022-10-23 thomas
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"
52 3efd8e31 2022-10-23 thomas
53 3efd8e31 2022-10-23 thomas #ifndef nitems
54 3efd8e31 2022-10-23 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 3efd8e31 2022-10-23 thomas #endif
56 3efd8e31 2022-10-23 thomas
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 3efd8e31 2022-10-23 thomas } repo_read;
64 3efd8e31 2022-10-23 thomas
65 3efd8e31 2022-10-23 thomas struct repo_read_client {
66 3efd8e31 2022-10-23 thomas STAILQ_ENTRY(repo_read_client) entry;
67 3efd8e31 2022-10-23 thomas uint32_t id;
68 3efd8e31 2022-10-23 thomas int fd;
69 3efd8e31 2022-10-23 thomas int delta_cache_fd;
70 3efd8e31 2022-10-23 thomas int report_progress;
71 cc4cc677 2022-10-28 thomas int pack_pipe;
72 3efd8e31 2022-10-23 thomas struct gotd_object_id_array want_ids;
73 3efd8e31 2022-10-23 thomas struct gotd_object_id_array have_ids;
74 3efd8e31 2022-10-23 thomas };
75 3efd8e31 2022-10-23 thomas STAILQ_HEAD(repo_read_clients, repo_read_client);
76 3efd8e31 2022-10-23 thomas
77 3efd8e31 2022-10-23 thomas static struct repo_read_clients repo_read_clients[GOTD_CLIENT_TABLE_SIZE];
78 3efd8e31 2022-10-23 thomas static SIPHASH_KEY clients_hash_key;
79 3efd8e31 2022-10-23 thomas
80 3efd8e31 2022-10-23 thomas static uint64_t
81 3efd8e31 2022-10-23 thomas client_hash(uint32_t client_id)
82 3efd8e31 2022-10-23 thomas {
83 3efd8e31 2022-10-23 thomas return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
84 3efd8e31 2022-10-23 thomas }
85 3efd8e31 2022-10-23 thomas
86 3efd8e31 2022-10-23 thomas static void
87 3efd8e31 2022-10-23 thomas add_client(struct repo_read_client *client, uint32_t client_id, int fd)
88 3efd8e31 2022-10-23 thomas {
89 3efd8e31 2022-10-23 thomas uint64_t slot;
90 3efd8e31 2022-10-23 thomas
91 3efd8e31 2022-10-23 thomas client->id = client_id;
92 3efd8e31 2022-10-23 thomas client->fd = fd;
93 3efd8e31 2022-10-23 thomas client->delta_cache_fd = -1;
94 cc4cc677 2022-10-28 thomas client->pack_pipe = -1;
95 3efd8e31 2022-10-23 thomas slot = client_hash(client->id) % nitems(repo_read_clients);
96 3efd8e31 2022-10-23 thomas STAILQ_INSERT_HEAD(&repo_read_clients[slot], client, entry);
97 3efd8e31 2022-10-23 thomas }
98 3efd8e31 2022-10-23 thomas
99 3efd8e31 2022-10-23 thomas static struct repo_read_client *
100 3efd8e31 2022-10-23 thomas find_client(uint32_t client_id)
101 3efd8e31 2022-10-23 thomas {
102 3efd8e31 2022-10-23 thomas uint64_t slot;
103 3efd8e31 2022-10-23 thomas struct repo_read_client *c;
104 3efd8e31 2022-10-23 thomas
105 3efd8e31 2022-10-23 thomas slot = client_hash(client_id) % nitems(repo_read_clients);
106 3efd8e31 2022-10-23 thomas STAILQ_FOREACH(c, &repo_read_clients[slot], entry) {
107 3efd8e31 2022-10-23 thomas if (c->id == client_id)
108 3efd8e31 2022-10-23 thomas return c;
109 3efd8e31 2022-10-23 thomas }
110 3efd8e31 2022-10-23 thomas
111 3efd8e31 2022-10-23 thomas return NULL;
112 3efd8e31 2022-10-23 thomas }
113 3efd8e31 2022-10-23 thomas
114 3efd8e31 2022-10-23 thomas static volatile sig_atomic_t sigint_received;
115 3efd8e31 2022-10-23 thomas static volatile sig_atomic_t sigterm_received;
116 3efd8e31 2022-10-23 thomas
117 3efd8e31 2022-10-23 thomas static void
118 3efd8e31 2022-10-23 thomas catch_sigint(int signo)
119 3efd8e31 2022-10-23 thomas {
120 3efd8e31 2022-10-23 thomas sigint_received = 1;
121 3efd8e31 2022-10-23 thomas }
122 3efd8e31 2022-10-23 thomas
123 3efd8e31 2022-10-23 thomas static void
124 3efd8e31 2022-10-23 thomas catch_sigterm(int signo)
125 3efd8e31 2022-10-23 thomas {
126 3efd8e31 2022-10-23 thomas sigterm_received = 1;
127 3efd8e31 2022-10-23 thomas }
128 3efd8e31 2022-10-23 thomas
129 3efd8e31 2022-10-23 thomas static const struct got_error *
130 3efd8e31 2022-10-23 thomas check_cancelled(void *arg)
131 3efd8e31 2022-10-23 thomas {
132 3efd8e31 2022-10-23 thomas if (sigint_received || sigterm_received)
133 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_CANCELLED);
134 3efd8e31 2022-10-23 thomas
135 3efd8e31 2022-10-23 thomas return NULL;
136 3efd8e31 2022-10-23 thomas }
137 3efd8e31 2022-10-23 thomas
138 3efd8e31 2022-10-23 thomas static const struct got_error *
139 3efd8e31 2022-10-23 thomas send_symref(struct got_reference *symref, struct imsgbuf *ibuf)
140 3efd8e31 2022-10-23 thomas {
141 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
142 3efd8e31 2022-10-23 thomas struct gotd_imsg_symref isymref;
143 3efd8e31 2022-10-23 thomas const char *refname = got_ref_get_name(symref);
144 3efd8e31 2022-10-23 thomas const char *target = got_ref_get_symref_target(symref);
145 3efd8e31 2022-10-23 thomas size_t len;
146 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
147 3efd8e31 2022-10-23 thomas struct got_object_id *target_id;
148 3efd8e31 2022-10-23 thomas
149 3efd8e31 2022-10-23 thomas err = got_ref_resolve(&target_id, repo_read.repo, symref);
150 3efd8e31 2022-10-23 thomas if (err)
151 3efd8e31 2022-10-23 thomas return err;
152 3efd8e31 2022-10-23 thomas
153 3efd8e31 2022-10-23 thomas memset(&isymref, 0, sizeof(isymref));
154 3efd8e31 2022-10-23 thomas isymref.name_len = strlen(refname);
155 3efd8e31 2022-10-23 thomas isymref.target_len = strlen(target);
156 3efd8e31 2022-10-23 thomas memcpy(isymref.target_id, target_id->sha1, sizeof(isymref.target_id));
157 3efd8e31 2022-10-23 thomas
158 3efd8e31 2022-10-23 thomas len = sizeof(isymref) + isymref.name_len + isymref.target_len;
159 3efd8e31 2022-10-23 thomas if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
160 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_NO_SPACE);
161 3efd8e31 2022-10-23 thomas goto done;
162 3efd8e31 2022-10-23 thomas }
163 3efd8e31 2022-10-23 thomas
164 3efd8e31 2022-10-23 thomas wbuf = imsg_create(ibuf, GOTD_IMSG_SYMREF, 0, 0, len);
165 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
166 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create SYMREF");
167 3efd8e31 2022-10-23 thomas goto done;
168 3efd8e31 2022-10-23 thomas }
169 3efd8e31 2022-10-23 thomas
170 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &isymref, sizeof(isymref)) == -1) {
171 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add SYMREF");
172 3efd8e31 2022-10-23 thomas goto done;
173 3efd8e31 2022-10-23 thomas }
174 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, refname, isymref.name_len) == -1) {
175 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add SYMREF");
176 3efd8e31 2022-10-23 thomas goto done;
177 3efd8e31 2022-10-23 thomas }
178 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, target, isymref.target_len) == -1) {
179 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add SYMREF");
180 3efd8e31 2022-10-23 thomas goto done;
181 3efd8e31 2022-10-23 thomas }
182 3efd8e31 2022-10-23 thomas
183 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
184 3efd8e31 2022-10-23 thomas imsg_close(ibuf, wbuf);
185 3efd8e31 2022-10-23 thomas done:
186 3efd8e31 2022-10-23 thomas free(target_id);
187 3efd8e31 2022-10-23 thomas return err;
188 3efd8e31 2022-10-23 thomas }
189 3efd8e31 2022-10-23 thomas
190 3efd8e31 2022-10-23 thomas static const struct got_error *
191 3efd8e31 2022-10-23 thomas send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
192 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf)
193 3efd8e31 2022-10-23 thomas {
194 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
195 3efd8e31 2022-10-23 thomas struct got_tag_object *tag;
196 3efd8e31 2022-10-23 thomas size_t namelen, len;
197 3efd8e31 2022-10-23 thomas char *peeled_refname = NULL;
198 3efd8e31 2022-10-23 thomas struct got_object_id *id;
199 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
200 3efd8e31 2022-10-23 thomas
201 3efd8e31 2022-10-23 thomas err = got_object_tag_open(&tag, repo_read.repo, obj);
202 3efd8e31 2022-10-23 thomas if (err)
203 3efd8e31 2022-10-23 thomas return err;
204 3efd8e31 2022-10-23 thomas
205 3efd8e31 2022-10-23 thomas if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
206 3efd8e31 2022-10-23 thomas err = got_error_from_errno("asprintf");
207 3efd8e31 2022-10-23 thomas goto done;
208 3efd8e31 2022-10-23 thomas }
209 3efd8e31 2022-10-23 thomas
210 3efd8e31 2022-10-23 thomas id = got_object_tag_get_object_id(tag);
211 3efd8e31 2022-10-23 thomas namelen = strlen(peeled_refname);
212 3efd8e31 2022-10-23 thomas
213 3efd8e31 2022-10-23 thomas len = sizeof(struct gotd_imsg_ref) + namelen;
214 3efd8e31 2022-10-23 thomas if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
215 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_NO_SPACE);
216 3efd8e31 2022-10-23 thomas goto done;
217 3efd8e31 2022-10-23 thomas }
218 3efd8e31 2022-10-23 thomas
219 3efd8e31 2022-10-23 thomas wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
220 3efd8e31 2022-10-23 thomas repo_read.pid, len);
221 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
222 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create MREF");
223 3efd8e31 2022-10-23 thomas goto done;
224 3efd8e31 2022-10-23 thomas }
225 3efd8e31 2022-10-23 thomas
226 3efd8e31 2022-10-23 thomas /* Keep in sync with struct gotd_imsg_ref definition. */
227 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
228 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add REF");
229 3efd8e31 2022-10-23 thomas goto done;
230 3efd8e31 2022-10-23 thomas }
231 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
232 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add REF");
233 3efd8e31 2022-10-23 thomas goto done;
234 3efd8e31 2022-10-23 thomas }
235 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
236 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add REF");
237 3efd8e31 2022-10-23 thomas goto done;
238 3efd8e31 2022-10-23 thomas }
239 3efd8e31 2022-10-23 thomas
240 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
241 3efd8e31 2022-10-23 thomas imsg_close(ibuf, wbuf);
242 3efd8e31 2022-10-23 thomas done:
243 3efd8e31 2022-10-23 thomas got_object_tag_close(tag);
244 3efd8e31 2022-10-23 thomas return err;
245 3efd8e31 2022-10-23 thomas }
246 3efd8e31 2022-10-23 thomas
247 3efd8e31 2022-10-23 thomas static const struct got_error *
248 3efd8e31 2022-10-23 thomas send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
249 3efd8e31 2022-10-23 thomas {
250 3efd8e31 2022-10-23 thomas const struct got_error *err;
251 3efd8e31 2022-10-23 thomas const char *refname = got_ref_get_name(ref);
252 3efd8e31 2022-10-23 thomas size_t namelen;
253 3efd8e31 2022-10-23 thomas struct got_object_id *id = NULL;
254 3efd8e31 2022-10-23 thomas struct got_object *obj = NULL;
255 3efd8e31 2022-10-23 thomas size_t len;
256 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
257 3efd8e31 2022-10-23 thomas
258 3efd8e31 2022-10-23 thomas namelen = strlen(refname);
259 3efd8e31 2022-10-23 thomas
260 3efd8e31 2022-10-23 thomas len = sizeof(struct gotd_imsg_ref) + namelen;
261 3efd8e31 2022-10-23 thomas if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
262 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
263 3efd8e31 2022-10-23 thomas
264 3efd8e31 2022-10-23 thomas err = got_ref_resolve(&id, repo_read.repo, ref);
265 3efd8e31 2022-10-23 thomas if (err)
266 3efd8e31 2022-10-23 thomas return err;
267 3efd8e31 2022-10-23 thomas
268 3efd8e31 2022-10-23 thomas wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_READ,
269 3efd8e31 2022-10-23 thomas repo_read.pid, len);
270 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
271 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create REF");
272 3efd8e31 2022-10-23 thomas goto done;
273 3efd8e31 2022-10-23 thomas }
274 3efd8e31 2022-10-23 thomas
275 3efd8e31 2022-10-23 thomas /* Keep in sync with struct gotd_imsg_ref definition. */
276 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
277 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF");
278 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
279 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF");
280 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, refname, namelen) == -1)
281 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF");
282 3efd8e31 2022-10-23 thomas
283 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
284 3efd8e31 2022-10-23 thomas imsg_close(ibuf, wbuf);
285 3efd8e31 2022-10-23 thomas
286 3efd8e31 2022-10-23 thomas err = got_object_open(&obj, repo_read.repo, id);
287 3efd8e31 2022-10-23 thomas if (err)
288 3efd8e31 2022-10-23 thomas goto done;
289 3efd8e31 2022-10-23 thomas if (obj->type == GOT_OBJ_TYPE_TAG)
290 3efd8e31 2022-10-23 thomas err = send_peeled_tag_ref(ref, obj, ibuf);
291 3efd8e31 2022-10-23 thomas done:
292 3efd8e31 2022-10-23 thomas if (obj)
293 3efd8e31 2022-10-23 thomas got_object_close(obj);
294 3efd8e31 2022-10-23 thomas free(id);
295 3efd8e31 2022-10-23 thomas return err;
296 3efd8e31 2022-10-23 thomas }
297 3efd8e31 2022-10-23 thomas
298 3efd8e31 2022-10-23 thomas static const struct got_error *
299 3efd8e31 2022-10-23 thomas list_refs(struct repo_read_client **client, struct imsg *imsg)
300 3efd8e31 2022-10-23 thomas {
301 3efd8e31 2022-10-23 thomas const struct got_error *err;
302 3efd8e31 2022-10-23 thomas struct got_reflist_head refs;
303 3efd8e31 2022-10-23 thomas struct got_reflist_entry *re;
304 3efd8e31 2022-10-23 thomas struct gotd_imsg_list_refs_internal ireq;
305 3efd8e31 2022-10-23 thomas size_t datalen;
306 3efd8e31 2022-10-23 thomas struct gotd_imsg_reflist irefs;
307 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
308 3efd8e31 2022-10-23 thomas int client_fd = imsg->fd;
309 3efd8e31 2022-10-23 thomas
310 3efd8e31 2022-10-23 thomas TAILQ_INIT(&refs);
311 3efd8e31 2022-10-23 thomas
312 3efd8e31 2022-10-23 thomas if (client_fd == -1)
313 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
314 3efd8e31 2022-10-23 thomas
315 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
316 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
317 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
318 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
319 3efd8e31 2022-10-23 thomas
320 3efd8e31 2022-10-23 thomas *client = find_client(ireq.client_id);
321 3efd8e31 2022-10-23 thomas if (*client)
322 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
323 3efd8e31 2022-10-23 thomas
324 3efd8e31 2022-10-23 thomas *client = calloc(1, sizeof(**client));
325 3efd8e31 2022-10-23 thomas if (*client == NULL)
326 3efd8e31 2022-10-23 thomas return got_error_from_errno("calloc");
327 3efd8e31 2022-10-23 thomas add_client(*client, ireq.client_id, client_fd);
328 3efd8e31 2022-10-23 thomas
329 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, client_fd);
330 3efd8e31 2022-10-23 thomas
331 3efd8e31 2022-10-23 thomas err = got_ref_list(&refs, repo_read.repo, "",
332 3efd8e31 2022-10-23 thomas got_ref_cmp_by_name, NULL);
333 3efd8e31 2022-10-23 thomas if (err)
334 3efd8e31 2022-10-23 thomas return err;
335 3efd8e31 2022-10-23 thomas
336 3efd8e31 2022-10-23 thomas memset(&irefs, 0, sizeof(irefs));
337 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(re, &refs, entry) {
338 3efd8e31 2022-10-23 thomas struct got_object_id *id;
339 3efd8e31 2022-10-23 thomas int obj_type;
340 3efd8e31 2022-10-23 thomas
341 3efd8e31 2022-10-23 thomas if (got_ref_is_symbolic(re->ref)) {
342 3efd8e31 2022-10-23 thomas const char *refname = got_ref_get_name(re->ref);
343 3efd8e31 2022-10-23 thomas if (strcmp(refname, GOT_REF_HEAD) == 0)
344 3efd8e31 2022-10-23 thomas irefs.nrefs++;
345 3efd8e31 2022-10-23 thomas continue;
346 3efd8e31 2022-10-23 thomas }
347 3efd8e31 2022-10-23 thomas
348 3efd8e31 2022-10-23 thomas irefs.nrefs++;
349 3efd8e31 2022-10-23 thomas
350 3efd8e31 2022-10-23 thomas /* Account for a peeled tag refs. */
351 3efd8e31 2022-10-23 thomas err = got_ref_resolve(&id, repo_read.repo, re->ref);
352 3efd8e31 2022-10-23 thomas if (err)
353 3efd8e31 2022-10-23 thomas goto done;
354 3efd8e31 2022-10-23 thomas err = got_object_get_type(&obj_type, repo_read.repo, id);
355 3efd8e31 2022-10-23 thomas free(id);
356 3efd8e31 2022-10-23 thomas if (err)
357 3efd8e31 2022-10-23 thomas goto done;
358 3efd8e31 2022-10-23 thomas if (obj_type == GOT_OBJ_TYPE_TAG)
359 3efd8e31 2022-10-23 thomas irefs.nrefs++;
360 3efd8e31 2022-10-23 thomas }
361 3efd8e31 2022-10-23 thomas
362 3efd8e31 2022-10-23 thomas if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_READ,
363 3efd8e31 2022-10-23 thomas repo_read.pid, -1, &irefs, sizeof(irefs)) == -1) {
364 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_compose REFLIST");
365 3efd8e31 2022-10-23 thomas goto done;
366 3efd8e31 2022-10-23 thomas }
367 3efd8e31 2022-10-23 thomas
368 3efd8e31 2022-10-23 thomas /*
369 3efd8e31 2022-10-23 thomas * Send the HEAD symref first. In Git-protocol versions < 2
370 3efd8e31 2022-10-23 thomas * the HEAD symref must be announced on the initial line of
371 3efd8e31 2022-10-23 thomas * the server's ref advertisement.
372 3efd8e31 2022-10-23 thomas * For now, we do not advertise symrefs other than HEAD.
373 3efd8e31 2022-10-23 thomas */
374 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(re, &refs, entry) {
375 3efd8e31 2022-10-23 thomas if (!got_ref_is_symbolic(re->ref) ||
376 3efd8e31 2022-10-23 thomas strcmp(got_ref_get_name(re->ref), GOT_REF_HEAD) != 0)
377 3efd8e31 2022-10-23 thomas continue;
378 3efd8e31 2022-10-23 thomas err = send_symref(re->ref, &ibuf);
379 3efd8e31 2022-10-23 thomas if (err)
380 3efd8e31 2022-10-23 thomas goto done;
381 3efd8e31 2022-10-23 thomas break;
382 3efd8e31 2022-10-23 thomas }
383 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(re, &refs, entry) {
384 3efd8e31 2022-10-23 thomas if (got_ref_is_symbolic(re->ref))
385 3efd8e31 2022-10-23 thomas continue;
386 3efd8e31 2022-10-23 thomas err = send_ref(re->ref, &ibuf);
387 3efd8e31 2022-10-23 thomas if (err)
388 3efd8e31 2022-10-23 thomas goto done;
389 3efd8e31 2022-10-23 thomas }
390 3efd8e31 2022-10-23 thomas
391 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(&ibuf);
392 3efd8e31 2022-10-23 thomas done:
393 3efd8e31 2022-10-23 thomas got_ref_list_free(&refs);
394 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
395 3efd8e31 2022-10-23 thomas return err;
396 3efd8e31 2022-10-23 thomas }
397 3efd8e31 2022-10-23 thomas
398 3efd8e31 2022-10-23 thomas static const struct got_error *
399 3efd8e31 2022-10-23 thomas record_object_id(struct gotd_object_id_array *array, struct got_object_id *id)
400 3efd8e31 2022-10-23 thomas {
401 3efd8e31 2022-10-23 thomas const size_t alloc_chunksz = 256;
402 3efd8e31 2022-10-23 thomas
403 3efd8e31 2022-10-23 thomas if (array->ids == NULL) {
404 3efd8e31 2022-10-23 thomas array->ids = reallocarray(NULL, alloc_chunksz,
405 3efd8e31 2022-10-23 thomas sizeof(*array->ids));
406 3efd8e31 2022-10-23 thomas if (array->ids == NULL)
407 3efd8e31 2022-10-23 thomas return got_error_from_errno("reallocarray");
408 3efd8e31 2022-10-23 thomas array->nalloc = alloc_chunksz;
409 3efd8e31 2022-10-23 thomas array->nids = 0;
410 3efd8e31 2022-10-23 thomas } else if (array->nalloc <= array->nids) {
411 3efd8e31 2022-10-23 thomas struct got_object_id **new;
412 3efd8e31 2022-10-23 thomas new = recallocarray(array->ids, array->nalloc,
413 3efd8e31 2022-10-23 thomas array->nalloc + alloc_chunksz, sizeof(*new));
414 3efd8e31 2022-10-23 thomas if (new == NULL)
415 3efd8e31 2022-10-23 thomas return got_error_from_errno("recallocarray");
416 3efd8e31 2022-10-23 thomas array->ids = new;
417 3efd8e31 2022-10-23 thomas array->nalloc += alloc_chunksz;
418 3efd8e31 2022-10-23 thomas }
419 3efd8e31 2022-10-23 thomas
420 3efd8e31 2022-10-23 thomas array->ids[array->nids] = got_object_id_dup(id);
421 3efd8e31 2022-10-23 thomas if (array->ids[array->nids] == NULL)
422 3efd8e31 2022-10-23 thomas return got_error_from_errno("got_object_id_dup");
423 3efd8e31 2022-10-23 thomas array->nids++;
424 3efd8e31 2022-10-23 thomas return NULL;
425 3efd8e31 2022-10-23 thomas }
426 3efd8e31 2022-10-23 thomas
427 3efd8e31 2022-10-23 thomas static void
428 3efd8e31 2022-10-23 thomas free_object_ids(struct gotd_object_id_array *array)
429 3efd8e31 2022-10-23 thomas {
430 3efd8e31 2022-10-23 thomas size_t i;
431 3efd8e31 2022-10-23 thomas
432 3efd8e31 2022-10-23 thomas for (i = 0; i < array->nids; i++)
433 3efd8e31 2022-10-23 thomas free(array->ids[i]);
434 3efd8e31 2022-10-23 thomas free(array->ids);
435 3efd8e31 2022-10-23 thomas
436 3efd8e31 2022-10-23 thomas array->ids = NULL;
437 3efd8e31 2022-10-23 thomas array->nalloc = 0;
438 3efd8e31 2022-10-23 thomas array->nids = 0;
439 3efd8e31 2022-10-23 thomas }
440 3efd8e31 2022-10-23 thomas
441 3efd8e31 2022-10-23 thomas static const struct got_error *
442 3efd8e31 2022-10-23 thomas recv_want(struct repo_read_client **client, struct imsg *imsg)
443 3efd8e31 2022-10-23 thomas {
444 3efd8e31 2022-10-23 thomas const struct got_error *err;
445 3efd8e31 2022-10-23 thomas struct gotd_imsg_want iwant;
446 3efd8e31 2022-10-23 thomas size_t datalen;
447 3efd8e31 2022-10-23 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
448 3efd8e31 2022-10-23 thomas struct got_object_id id;
449 3efd8e31 2022-10-23 thomas int obj_type;
450 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
451 3efd8e31 2022-10-23 thomas
452 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
453 3efd8e31 2022-10-23 thomas if (datalen != sizeof(iwant))
454 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
455 3efd8e31 2022-10-23 thomas memcpy(&iwant, imsg->data, sizeof(iwant));
456 3efd8e31 2022-10-23 thomas
457 3efd8e31 2022-10-23 thomas memset(&id, 0, sizeof(id));
458 3efd8e31 2022-10-23 thomas memcpy(id.sha1, iwant.object_id, SHA1_DIGEST_LENGTH);
459 3efd8e31 2022-10-23 thomas
460 3efd8e31 2022-10-23 thomas if (log_getverbose() > 0 &&
461 3efd8e31 2022-10-23 thomas got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
462 3efd8e31 2022-10-23 thomas log_debug("client wants %s", hex);
463 3efd8e31 2022-10-23 thomas
464 3efd8e31 2022-10-23 thomas *client = find_client(iwant.client_id);
465 3efd8e31 2022-10-23 thomas if (*client == NULL)
466 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_CLIENT_ID);
467 3efd8e31 2022-10-23 thomas
468 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, (*client)->fd);
469 3efd8e31 2022-10-23 thomas
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 return err;
473 3efd8e31 2022-10-23 thomas
474 3efd8e31 2022-10-23 thomas if (obj_type != GOT_OBJ_TYPE_COMMIT &&
475 3efd8e31 2022-10-23 thomas obj_type != GOT_OBJ_TYPE_TAG)
476 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_OBJ_TYPE);
477 3efd8e31 2022-10-23 thomas
478 3efd8e31 2022-10-23 thomas err = record_object_id(&(*client)->want_ids, &id);
479 3efd8e31 2022-10-23 thomas if (err)
480 3efd8e31 2022-10-23 thomas return err;
481 3efd8e31 2022-10-23 thomas
482 3efd8e31 2022-10-23 thomas gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
483 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
484 3efd8e31 2022-10-23 thomas return err;
485 3efd8e31 2022-10-23 thomas }
486 3efd8e31 2022-10-23 thomas
487 3efd8e31 2022-10-23 thomas static const struct got_error *
488 3efd8e31 2022-10-23 thomas recv_have(struct repo_read_client **client, struct imsg *imsg)
489 3efd8e31 2022-10-23 thomas {
490 3efd8e31 2022-10-23 thomas const struct got_error *err;
491 3efd8e31 2022-10-23 thomas struct gotd_imsg_have ihave;
492 3efd8e31 2022-10-23 thomas size_t datalen;
493 3efd8e31 2022-10-23 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
494 3efd8e31 2022-10-23 thomas struct got_object_id id;
495 3efd8e31 2022-10-23 thomas int obj_type;
496 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
497 3efd8e31 2022-10-23 thomas
498 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
499 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ihave))
500 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
501 3efd8e31 2022-10-23 thomas memcpy(&ihave, imsg->data, sizeof(ihave));
502 3efd8e31 2022-10-23 thomas
503 3efd8e31 2022-10-23 thomas memset(&id, 0, sizeof(id));
504 3efd8e31 2022-10-23 thomas memcpy(id.sha1, ihave.object_id, SHA1_DIGEST_LENGTH);
505 3efd8e31 2022-10-23 thomas
506 3efd8e31 2022-10-23 thomas if (log_getverbose() > 0 &&
507 3efd8e31 2022-10-23 thomas got_sha1_digest_to_str(id.sha1, hex, sizeof(hex)))
508 3efd8e31 2022-10-23 thomas log_debug("client has %s", hex);
509 3efd8e31 2022-10-23 thomas
510 3efd8e31 2022-10-23 thomas *client = find_client(ihave.client_id);
511 3efd8e31 2022-10-23 thomas if (*client == NULL)
512 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_CLIENT_ID);
513 3efd8e31 2022-10-23 thomas
514 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, (*client)->fd);
515 3efd8e31 2022-10-23 thomas
516 3efd8e31 2022-10-23 thomas err = got_object_get_type(&obj_type, repo_read.repo, &id);
517 3efd8e31 2022-10-23 thomas if (err) {
518 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_NO_OBJ) {
519 3efd8e31 2022-10-23 thomas gotd_imsg_send_nak(&id, &ibuf,
520 3efd8e31 2022-10-23 thomas PROC_REPO_READ, repo_read.pid);
521 3efd8e31 2022-10-23 thomas err = NULL;
522 3efd8e31 2022-10-23 thomas }
523 3efd8e31 2022-10-23 thomas goto done;
524 3efd8e31 2022-10-23 thomas }
525 3efd8e31 2022-10-23 thomas
526 3efd8e31 2022-10-23 thomas if (obj_type != GOT_OBJ_TYPE_COMMIT &&
527 3efd8e31 2022-10-23 thomas obj_type != GOT_OBJ_TYPE_TAG) {
528 3efd8e31 2022-10-23 thomas gotd_imsg_send_nak(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
529 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_OBJ_TYPE);
530 3efd8e31 2022-10-23 thomas goto done;
531 3efd8e31 2022-10-23 thomas }
532 3efd8e31 2022-10-23 thomas
533 3efd8e31 2022-10-23 thomas err = record_object_id(&(*client)->have_ids, &id);
534 3efd8e31 2022-10-23 thomas if (err)
535 3efd8e31 2022-10-23 thomas return err;
536 3efd8e31 2022-10-23 thomas
537 3efd8e31 2022-10-23 thomas gotd_imsg_send_ack(&id, &ibuf, PROC_REPO_READ, repo_read.pid);
538 3efd8e31 2022-10-23 thomas done:
539 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
540 3efd8e31 2022-10-23 thomas return err;
541 3efd8e31 2022-10-23 thomas }
542 3efd8e31 2022-10-23 thomas
543 3efd8e31 2022-10-23 thomas struct repo_read_pack_progress_arg {
544 3efd8e31 2022-10-23 thomas int report_progress;
545 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf;
546 3efd8e31 2022-10-23 thomas int sent_ready;
547 3efd8e31 2022-10-23 thomas };
548 3efd8e31 2022-10-23 thomas
549 3efd8e31 2022-10-23 thomas static const struct got_error *
550 3efd8e31 2022-10-23 thomas pack_progress(void *arg, int ncolored, int nfound, int ntrees,
551 3efd8e31 2022-10-23 thomas off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
552 3efd8e31 2022-10-23 thomas int nobj_written)
553 3efd8e31 2022-10-23 thomas {
554 3efd8e31 2022-10-23 thomas struct repo_read_pack_progress_arg *a = arg;
555 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_progress iprog;
556 3efd8e31 2022-10-23 thomas int ret;
557 3efd8e31 2022-10-23 thomas
558 3efd8e31 2022-10-23 thomas if (!a->report_progress)
559 3efd8e31 2022-10-23 thomas return NULL;
560 3efd8e31 2022-10-23 thomas if (packfile_size > 0 && a->sent_ready)
561 3efd8e31 2022-10-23 thomas return NULL;
562 3efd8e31 2022-10-23 thomas
563 3efd8e31 2022-10-23 thomas memset(&iprog, 0, sizeof(iprog));
564 3efd8e31 2022-10-23 thomas iprog.ncolored = ncolored;
565 3efd8e31 2022-10-23 thomas iprog.nfound = nfound;
566 3efd8e31 2022-10-23 thomas iprog.ntrees = ntrees;
567 3efd8e31 2022-10-23 thomas iprog.packfile_size = packfile_size;
568 3efd8e31 2022-10-23 thomas iprog.ncommits = ncommits;
569 3efd8e31 2022-10-23 thomas iprog.nobj_total = nobj_total;
570 3efd8e31 2022-10-23 thomas iprog.nobj_deltify = nobj_deltify;
571 3efd8e31 2022-10-23 thomas iprog.nobj_written = nobj_written;
572 3efd8e31 2022-10-23 thomas
573 3efd8e31 2022-10-23 thomas /* Using synchronous writes since we are blocking the event loop. */
574 3efd8e31 2022-10-23 thomas if (packfile_size == 0) {
575 3efd8e31 2022-10-23 thomas ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_PROGRESS,
576 3efd8e31 2022-10-23 thomas PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
577 3efd8e31 2022-10-23 thomas if (ret == -1) {
578 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg compose "
579 3efd8e31 2022-10-23 thomas "PACKFILE_PROGRESS");
580 3efd8e31 2022-10-23 thomas }
581 3efd8e31 2022-10-23 thomas } else {
582 3efd8e31 2022-10-23 thomas a->sent_ready = 1;
583 3efd8e31 2022-10-23 thomas ret = imsg_compose(a->ibuf, GOTD_IMSG_PACKFILE_READY,
584 3efd8e31 2022-10-23 thomas PROC_REPO_READ, repo_read.pid, -1, &iprog, sizeof(iprog));
585 3efd8e31 2022-10-23 thomas if (ret == -1) {
586 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg compose "
587 3efd8e31 2022-10-23 thomas "PACKFILE_READY");
588 3efd8e31 2022-10-23 thomas }
589 3efd8e31 2022-10-23 thomas }
590 3efd8e31 2022-10-23 thomas
591 3efd8e31 2022-10-23 thomas return gotd_imsg_flush(a->ibuf);
592 3efd8e31 2022-10-23 thomas }
593 3efd8e31 2022-10-23 thomas
594 3efd8e31 2022-10-23 thomas static const struct got_error *
595 3efd8e31 2022-10-23 thomas receive_delta_cache_fd(struct repo_read_client **client, struct imsg *imsg,
596 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev)
597 3efd8e31 2022-10-23 thomas {
598 3efd8e31 2022-10-23 thomas struct gotd_imsg_send_packfile ireq;
599 3efd8e31 2022-10-23 thomas size_t datalen;
600 3efd8e31 2022-10-23 thomas
601 3efd8e31 2022-10-23 thomas log_debug("receving delta cache file");
602 3efd8e31 2022-10-23 thomas
603 3efd8e31 2022-10-23 thomas if (imsg->fd == -1)
604 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
605 3efd8e31 2022-10-23 thomas
606 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
607 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
608 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
609 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
610 3efd8e31 2022-10-23 thomas
611 3efd8e31 2022-10-23 thomas *client = find_client(ireq.client_id);
612 3efd8e31 2022-10-23 thomas if (*client == NULL)
613 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_CLIENT_ID);
614 3efd8e31 2022-10-23 thomas
615 3efd8e31 2022-10-23 thomas if ((*client)->delta_cache_fd != -1)
616 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
617 3efd8e31 2022-10-23 thomas
618 3efd8e31 2022-10-23 thomas (*client)->delta_cache_fd = imsg->fd;
619 3efd8e31 2022-10-23 thomas (*client)->report_progress = ireq.report_progress;
620 3efd8e31 2022-10-23 thomas return NULL;
621 3efd8e31 2022-10-23 thomas }
622 3efd8e31 2022-10-23 thomas
623 3efd8e31 2022-10-23 thomas static const struct got_error *
624 3efd8e31 2022-10-23 thomas receive_pack_pipe(struct repo_read_client **client, struct imsg *imsg,
625 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev)
626 3efd8e31 2022-10-23 thomas {
627 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_pipe ireq;
628 3efd8e31 2022-10-23 thomas size_t datalen;
629 3efd8e31 2022-10-23 thomas
630 3efd8e31 2022-10-23 thomas log_debug("receving pack pipe descriptor");
631 3efd8e31 2022-10-23 thomas
632 3efd8e31 2022-10-23 thomas if (imsg->fd == -1)
633 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
634 3efd8e31 2022-10-23 thomas
635 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
636 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
637 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
638 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
639 3efd8e31 2022-10-23 thomas
640 3efd8e31 2022-10-23 thomas *client = find_client(ireq.client_id);
641 3efd8e31 2022-10-23 thomas if (*client == NULL)
642 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_CLIENT_ID);
643 cc4cc677 2022-10-28 thomas if ((*client)->pack_pipe != -1)
644 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
645 3efd8e31 2022-10-23 thomas
646 cc4cc677 2022-10-28 thomas (*client)->pack_pipe = imsg->fd;
647 3efd8e31 2022-10-23 thomas return NULL;
648 3efd8e31 2022-10-23 thomas }
649 3efd8e31 2022-10-23 thomas
650 3efd8e31 2022-10-23 thomas static const struct got_error *
651 3efd8e31 2022-10-23 thomas send_packfile(struct repo_read_client *client, struct imsg *imsg,
652 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev)
653 3efd8e31 2022-10-23 thomas {
654 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
655 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_done idone;
656 3efd8e31 2022-10-23 thomas uint8_t packsha1[SHA1_DIGEST_LENGTH];
657 3efd8e31 2022-10-23 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
658 3efd8e31 2022-10-23 thomas FILE *delta_cache = NULL;
659 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
660 3efd8e31 2022-10-23 thomas struct repo_read_pack_progress_arg pa;
661 3efd8e31 2022-10-23 thomas struct got_ratelimit rl;
662 3efd8e31 2022-10-23 thomas
663 3efd8e31 2022-10-23 thomas log_debug("packfile request received");
664 3efd8e31 2022-10-23 thomas
665 3efd8e31 2022-10-23 thomas got_ratelimit_init(&rl, 2, 0);
666 3efd8e31 2022-10-23 thomas
667 cc4cc677 2022-10-28 thomas if (client->delta_cache_fd == -1 || client->pack_pipe == -1)
668 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
669 3efd8e31 2022-10-23 thomas
670 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, client->fd);
671 3efd8e31 2022-10-23 thomas
672 3efd8e31 2022-10-23 thomas delta_cache = fdopen(client->delta_cache_fd, "w+");
673 3efd8e31 2022-10-23 thomas if (delta_cache == NULL) {
674 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fdopen");
675 3efd8e31 2022-10-23 thomas goto done;
676 3efd8e31 2022-10-23 thomas }
677 3efd8e31 2022-10-23 thomas client->delta_cache_fd = -1;
678 3efd8e31 2022-10-23 thomas
679 3efd8e31 2022-10-23 thomas memset(&pa, 0, sizeof(pa));
680 3efd8e31 2022-10-23 thomas pa.ibuf = &ibuf;
681 3efd8e31 2022-10-23 thomas pa.report_progress = client->report_progress;
682 3efd8e31 2022-10-23 thomas
683 cc4cc677 2022-10-28 thomas err = got_pack_create(packsha1, client->pack_pipe, delta_cache,
684 3efd8e31 2022-10-23 thomas client->have_ids.ids, client->have_ids.nids,
685 3efd8e31 2022-10-23 thomas client->want_ids.ids, client->want_ids.nids,
686 3efd8e31 2022-10-23 thomas repo_read.repo, 0, 1, pack_progress, &pa, &rl,
687 3efd8e31 2022-10-23 thomas check_cancelled, NULL);
688 3efd8e31 2022-10-23 thomas if (err)
689 3efd8e31 2022-10-23 thomas goto done;
690 3efd8e31 2022-10-23 thomas
691 3efd8e31 2022-10-23 thomas if (log_getverbose() > 0 &&
692 3efd8e31 2022-10-23 thomas got_sha1_digest_to_str(packsha1, hex, sizeof(hex)))
693 3efd8e31 2022-10-23 thomas log_debug("sent pack-%s.pack", hex);
694 3efd8e31 2022-10-23 thomas
695 3efd8e31 2022-10-23 thomas memset(&idone, 0, sizeof(idone));
696 3efd8e31 2022-10-23 thomas idone.client_id = client->id;
697 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_DONE,
698 3efd8e31 2022-10-23 thomas PROC_REPO_READ, -1, &idone, sizeof(idone)) == -1)
699 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg compose PACKFILE_DONE");
700 3efd8e31 2022-10-23 thomas done:
701 3efd8e31 2022-10-23 thomas if (delta_cache != NULL && fclose(delta_cache) == EOF && err == NULL)
702 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fclose");
703 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
704 3efd8e31 2022-10-23 thomas return err;
705 3efd8e31 2022-10-23 thomas }
706 3efd8e31 2022-10-23 thomas
707 3efd8e31 2022-10-23 thomas static const struct got_error *
708 3efd8e31 2022-10-23 thomas recv_disconnect(struct imsg *imsg)
709 3efd8e31 2022-10-23 thomas {
710 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
711 3efd8e31 2022-10-23 thomas struct gotd_imsg_disconnect idisconnect;
712 3efd8e31 2022-10-23 thomas size_t datalen;
713 cc4cc677 2022-10-28 thomas int client_fd, delta_cache_fd, pack_pipe;
714 3efd8e31 2022-10-23 thomas struct repo_read_client *client = NULL;
715 3efd8e31 2022-10-23 thomas uint64_t slot;
716 3efd8e31 2022-10-23 thomas
717 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
718 3efd8e31 2022-10-23 thomas if (datalen != sizeof(idisconnect))
719 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
720 3efd8e31 2022-10-23 thomas memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
721 3efd8e31 2022-10-23 thomas
722 3efd8e31 2022-10-23 thomas log_debug("client disconnecting");
723 3efd8e31 2022-10-23 thomas
724 3efd8e31 2022-10-23 thomas client = find_client(idisconnect.client_id);
725 3efd8e31 2022-10-23 thomas if (client == NULL)
726 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_CLIENT_ID);
727 3efd8e31 2022-10-23 thomas
728 3efd8e31 2022-10-23 thomas slot = client_hash(client->id) % nitems(repo_read_clients);
729 3efd8e31 2022-10-23 thomas STAILQ_REMOVE(&repo_read_clients[slot], client, repo_read_client,
730 3efd8e31 2022-10-23 thomas entry);
731 3efd8e31 2022-10-23 thomas free_object_ids(&client->have_ids);
732 3efd8e31 2022-10-23 thomas free_object_ids(&client->want_ids);
733 3efd8e31 2022-10-23 thomas client_fd = client->fd;
734 3efd8e31 2022-10-23 thomas delta_cache_fd = client->delta_cache_fd;
735 cc4cc677 2022-10-28 thomas pack_pipe = client->pack_pipe;
736 3efd8e31 2022-10-23 thomas free(client);
737 3efd8e31 2022-10-23 thomas if (close(client_fd) == -1)
738 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
739 3efd8e31 2022-10-23 thomas if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
740 3efd8e31 2022-10-23 thomas return got_error_from_errno("close");
741 cc4cc677 2022-10-28 thomas if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
742 3efd8e31 2022-10-23 thomas return got_error_from_errno("close");
743 3efd8e31 2022-10-23 thomas return err;
744 3efd8e31 2022-10-23 thomas }
745 3efd8e31 2022-10-23 thomas
746 3efd8e31 2022-10-23 thomas static void
747 3efd8e31 2022-10-23 thomas repo_read_dispatch(int fd, short event, void *arg)
748 3efd8e31 2022-10-23 thomas {
749 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
750 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev = arg;
751 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf = &iev->ibuf;
752 3efd8e31 2022-10-23 thomas struct imsg imsg;
753 3efd8e31 2022-10-23 thomas ssize_t n;
754 3efd8e31 2022-10-23 thomas int shut = 0;
755 3efd8e31 2022-10-23 thomas struct repo_read_client *client = NULL;
756 3efd8e31 2022-10-23 thomas
757 3efd8e31 2022-10-23 thomas if (event & EV_READ) {
758 3efd8e31 2022-10-23 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
759 3efd8e31 2022-10-23 thomas fatal("imsg_read error");
760 3efd8e31 2022-10-23 thomas if (n == 0) /* Connection closed. */
761 3efd8e31 2022-10-23 thomas shut = 1;
762 3efd8e31 2022-10-23 thomas }
763 3efd8e31 2022-10-23 thomas
764 3efd8e31 2022-10-23 thomas if (event & EV_WRITE) {
765 3efd8e31 2022-10-23 thomas n = msgbuf_write(&ibuf->w);
766 3efd8e31 2022-10-23 thomas if (n == -1 && errno != EAGAIN)
767 3efd8e31 2022-10-23 thomas fatal("msgbuf_write");
768 3efd8e31 2022-10-23 thomas if (n == 0) /* Connection closed. */
769 3efd8e31 2022-10-23 thomas shut = 1;
770 3efd8e31 2022-10-23 thomas }
771 3efd8e31 2022-10-23 thomas
772 3efd8e31 2022-10-23 thomas while (err == NULL && check_cancelled(NULL) == NULL) {
773 3efd8e31 2022-10-23 thomas client = NULL;
774 3efd8e31 2022-10-23 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
775 3efd8e31 2022-10-23 thomas fatal("%s: imsg_get", __func__);
776 3efd8e31 2022-10-23 thomas if (n == 0) /* No more messages. */
777 3efd8e31 2022-10-23 thomas break;
778 3efd8e31 2022-10-23 thomas
779 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
780 3efd8e31 2022-10-23 thomas case GOTD_IMSG_LIST_REFS_INTERNAL:
781 3efd8e31 2022-10-23 thomas err = list_refs(&client, &imsg);
782 3efd8e31 2022-10-23 thomas if (err)
783 3efd8e31 2022-10-23 thomas log_warnx("%s: ls-refs: %s", repo_read.title,
784 3efd8e31 2022-10-23 thomas err->msg);
785 3efd8e31 2022-10-23 thomas break;
786 3efd8e31 2022-10-23 thomas case GOTD_IMSG_WANT:
787 3efd8e31 2022-10-23 thomas err = recv_want(&client, &imsg);
788 3efd8e31 2022-10-23 thomas if (err)
789 3efd8e31 2022-10-23 thomas log_warnx("%s: want-line: %s", repo_read.title,
790 3efd8e31 2022-10-23 thomas err->msg);
791 3efd8e31 2022-10-23 thomas break;
792 3efd8e31 2022-10-23 thomas case GOTD_IMSG_HAVE:
793 3efd8e31 2022-10-23 thomas err = recv_have(&client, &imsg);
794 3efd8e31 2022-10-23 thomas if (err)
795 3efd8e31 2022-10-23 thomas log_warnx("%s: have-line: %s", repo_read.title,
796 3efd8e31 2022-10-23 thomas err->msg);
797 3efd8e31 2022-10-23 thomas break;
798 3efd8e31 2022-10-23 thomas case GOTD_IMSG_SEND_PACKFILE:
799 3efd8e31 2022-10-23 thomas err = receive_delta_cache_fd(&client, &imsg, iev);
800 3efd8e31 2022-10-23 thomas if (err)
801 3efd8e31 2022-10-23 thomas log_warnx("%s: receiving delta cache: %s",
802 3efd8e31 2022-10-23 thomas repo_read.title, err->msg);
803 3efd8e31 2022-10-23 thomas break;
804 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_PIPE:
805 3efd8e31 2022-10-23 thomas err = receive_pack_pipe(&client, &imsg, iev);
806 3efd8e31 2022-10-23 thomas if (err) {
807 3efd8e31 2022-10-23 thomas log_warnx("%s: receiving pack pipe: %s",
808 3efd8e31 2022-10-23 thomas repo_read.title, err->msg);
809 3efd8e31 2022-10-23 thomas break;
810 3efd8e31 2022-10-23 thomas }
811 cc4cc677 2022-10-28 thomas if (client->pack_pipe == -1)
812 3efd8e31 2022-10-23 thomas break;
813 3efd8e31 2022-10-23 thomas err = send_packfile(client, &imsg, iev);
814 3efd8e31 2022-10-23 thomas if (err)
815 3efd8e31 2022-10-23 thomas log_warnx("%s: sending packfile: %s",
816 3efd8e31 2022-10-23 thomas repo_read.title, err->msg);
817 3efd8e31 2022-10-23 thomas break;
818 3efd8e31 2022-10-23 thomas case GOTD_IMSG_DISCONNECT:
819 3efd8e31 2022-10-23 thomas err = recv_disconnect(&imsg);
820 3efd8e31 2022-10-23 thomas if (err)
821 3efd8e31 2022-10-23 thomas log_warnx("%s: disconnect: %s",
822 3efd8e31 2022-10-23 thomas repo_read.title, err->msg);
823 3efd8e31 2022-10-23 thomas break;
824 3efd8e31 2022-10-23 thomas default:
825 3efd8e31 2022-10-23 thomas log_debug("%s: unexpected imsg %d", repo_read.title,
826 3efd8e31 2022-10-23 thomas imsg.hdr.type);
827 3efd8e31 2022-10-23 thomas break;
828 3efd8e31 2022-10-23 thomas }
829 3efd8e31 2022-10-23 thomas
830 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
831 3efd8e31 2022-10-23 thomas }
832 3efd8e31 2022-10-23 thomas
833 3efd8e31 2022-10-23 thomas if (!shut && check_cancelled(NULL) == NULL) {
834 3efd8e31 2022-10-23 thomas if (err &&
835 3efd8e31 2022-10-23 thomas gotd_imsg_send_error_event(iev, PROC_REPO_READ,
836 3efd8e31 2022-10-23 thomas client ? client->id : 0, err) == -1) {
837 3efd8e31 2022-10-23 thomas log_warnx("could not send error to parent: %s",
838 3efd8e31 2022-10-23 thomas err->msg);
839 3efd8e31 2022-10-23 thomas }
840 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(iev);
841 3efd8e31 2022-10-23 thomas } else {
842 3efd8e31 2022-10-23 thomas /* This pipe is dead. Remove its event handler */
843 3efd8e31 2022-10-23 thomas event_del(&iev->ev);
844 3efd8e31 2022-10-23 thomas event_loopexit(NULL);
845 3efd8e31 2022-10-23 thomas }
846 3efd8e31 2022-10-23 thomas }
847 3efd8e31 2022-10-23 thomas
848 3efd8e31 2022-10-23 thomas void
849 3efd8e31 2022-10-23 thomas repo_read_main(const char *title, int *pack_fds, int *temp_fds)
850 3efd8e31 2022-10-23 thomas {
851 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
852 3efd8e31 2022-10-23 thomas struct gotd_imsgev iev;
853 3efd8e31 2022-10-23 thomas
854 3efd8e31 2022-10-23 thomas repo_read.title = title;
855 3efd8e31 2022-10-23 thomas repo_read.pid = getpid();
856 3efd8e31 2022-10-23 thomas repo_read.pack_fds = pack_fds;
857 3efd8e31 2022-10-23 thomas repo_read.temp_fds = temp_fds;
858 3efd8e31 2022-10-23 thomas
859 3efd8e31 2022-10-23 thomas arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
860 3efd8e31 2022-10-23 thomas
861 3efd8e31 2022-10-23 thomas /*
862 3efd8e31 2022-10-23 thomas * Open a repository in the root directory.
863 3efd8e31 2022-10-23 thomas * We are already in chroot at this point.
864 3efd8e31 2022-10-23 thomas */
865 3efd8e31 2022-10-23 thomas err = got_repo_open(&repo_read.repo, "/", NULL, pack_fds);
866 3efd8e31 2022-10-23 thomas if (err)
867 3efd8e31 2022-10-23 thomas goto done;
868 3efd8e31 2022-10-23 thomas if (!got_repo_is_bare(repo_read.repo)) {
869 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
870 3efd8e31 2022-10-23 thomas "bare git repository required");
871 3efd8e31 2022-10-23 thomas goto done;
872 3efd8e31 2022-10-23 thomas }
873 3efd8e31 2022-10-23 thomas
874 3efd8e31 2022-10-23 thomas got_repo_temp_fds_set(repo_read.repo, temp_fds);
875 3efd8e31 2022-10-23 thomas
876 3efd8e31 2022-10-23 thomas signal(SIGINT, catch_sigint);
877 3efd8e31 2022-10-23 thomas signal(SIGTERM, catch_sigterm);
878 3efd8e31 2022-10-23 thomas signal(SIGPIPE, SIG_IGN);
879 3efd8e31 2022-10-23 thomas signal(SIGHUP, SIG_IGN);
880 3efd8e31 2022-10-23 thomas
881 3efd8e31 2022-10-23 thomas imsg_init(&iev.ibuf, GOTD_SOCK_FILENO);
882 3efd8e31 2022-10-23 thomas iev.handler = repo_read_dispatch;
883 3efd8e31 2022-10-23 thomas iev.events = EV_READ;
884 3efd8e31 2022-10-23 thomas iev.handler_arg = NULL;
885 3efd8e31 2022-10-23 thomas event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_read_dispatch, &iev);
886 3efd8e31 2022-10-23 thomas if (event_add(&iev.ev, NULL) == -1) {
887 3efd8e31 2022-10-23 thomas err = got_error_from_errno("event_add");
888 3efd8e31 2022-10-23 thomas goto done;
889 3efd8e31 2022-10-23 thomas }
890 3efd8e31 2022-10-23 thomas
891 3efd8e31 2022-10-23 thomas event_dispatch();
892 3efd8e31 2022-10-23 thomas done:
893 3efd8e31 2022-10-23 thomas if (err)
894 3efd8e31 2022-10-23 thomas log_warnx("%s: %s", title, err->msg);
895 3efd8e31 2022-10-23 thomas repo_read_shutdown();
896 3efd8e31 2022-10-23 thomas }
897 3efd8e31 2022-10-23 thomas
898 3efd8e31 2022-10-23 thomas void
899 3efd8e31 2022-10-23 thomas repo_read_shutdown(void)
900 3efd8e31 2022-10-23 thomas {
901 3efd8e31 2022-10-23 thomas log_debug("%s: shutting down", repo_read.title);
902 3efd8e31 2022-10-23 thomas if (repo_read.repo)
903 3efd8e31 2022-10-23 thomas got_repo_close(repo_read.repo);
904 3efd8e31 2022-10-23 thomas got_repo_pack_fds_close(repo_read.pack_fds);
905 3efd8e31 2022-10-23 thomas got_repo_temp_fds_close(repo_read.temp_fds);
906 3efd8e31 2022-10-23 thomas exit(0);
907 3efd8e31 2022-10-23 thomas }