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