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