Blame


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