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