Blame


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