2 13b2bc37 2022-10-23 stsp * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 13b2bc37 2022-10-23 stsp #include <sys/queue.h>
18 13b2bc37 2022-10-23 stsp #include <sys/stat.h>
19 13b2bc37 2022-10-23 stsp #include <sys/tree.h>
20 13b2bc37 2022-10-23 stsp #include <sys/types.h>
22 13b2bc37 2022-10-23 stsp #include <event.h>
23 13b2bc37 2022-10-23 stsp #include <errno.h>
24 13b2bc37 2022-10-23 stsp #include <imsg.h>
25 13b2bc37 2022-10-23 stsp #include <signal.h>
26 13b2bc37 2022-10-23 stsp #include <siphash.h>
27 13b2bc37 2022-10-23 stsp #include <stdio.h>
28 13b2bc37 2022-10-23 stsp #include <stdlib.h>
29 13b2bc37 2022-10-23 stsp #include <string.h>
30 13b2bc37 2022-10-23 stsp #include <limits.h>
31 13b2bc37 2022-10-23 stsp #include <poll.h>
32 13b2bc37 2022-10-23 stsp #include <sha1.h>
33 13b2bc37 2022-10-23 stsp #include <unistd.h>
34 13b2bc37 2022-10-23 stsp #include <zlib.h>
36 13b2bc37 2022-10-23 stsp #include "buf.h"
38 13b2bc37 2022-10-23 stsp #include "got_error.h"
39 13b2bc37 2022-10-23 stsp #include "got_repository.h"
40 13b2bc37 2022-10-23 stsp #include "got_object.h"
41 13b2bc37 2022-10-23 stsp #include "got_reference.h"
42 13b2bc37 2022-10-23 stsp #include "got_path.h"
44 13b2bc37 2022-10-23 stsp #include "got_lib_delta.h"
45 13b2bc37 2022-10-23 stsp #include "got_lib_delta_cache.h"
46 13b2bc37 2022-10-23 stsp #include "got_lib_object.h"
47 13b2bc37 2022-10-23 stsp #include "got_lib_object_cache.h"
48 13b2bc37 2022-10-23 stsp #include "got_lib_ratelimit.h"
49 13b2bc37 2022-10-23 stsp #include "got_lib_pack.h"
50 13b2bc37 2022-10-23 stsp #include "got_lib_pack_index.h"
51 13b2bc37 2022-10-23 stsp #include "got_lib_repository.h"
52 13b2bc37 2022-10-23 stsp #include "got_lib_poll.h"
54 13b2bc37 2022-10-23 stsp #include "got_lib_sha1.h" /* XXX temp include for debugging */
56 13b2bc37 2022-10-23 stsp #include "log.h"
57 13b2bc37 2022-10-23 stsp #include "gotd.h"
58 13b2bc37 2022-10-23 stsp #include "repo_write.h"
60 13b2bc37 2022-10-23 stsp #ifndef nitems
61 13b2bc37 2022-10-23 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 13b2bc37 2022-10-23 stsp static struct repo_write {
66 13b2bc37 2022-10-23 stsp const char *title;
67 13b2bc37 2022-10-23 stsp struct got_repository *repo;
68 13b2bc37 2022-10-23 stsp int *pack_fds;
69 13b2bc37 2022-10-23 stsp int *temp_fds;
70 ae7c1b78 2023-01-10 stsp int session_fd;
71 ae7c1b78 2023-01-10 stsp struct gotd_imsgev session_iev;
72 13b2bc37 2022-10-23 stsp } repo_write;
74 13b2bc37 2022-10-23 stsp struct gotd_ref_update {
75 13b2bc37 2022-10-23 stsp STAILQ_ENTRY(gotd_ref_update) entry;
76 13b2bc37 2022-10-23 stsp struct got_reference *ref;
77 13b2bc37 2022-10-23 stsp int ref_is_new;
78 13b2bc37 2022-10-23 stsp struct got_object_id old_id;
79 13b2bc37 2022-10-23 stsp struct got_object_id new_id;
81 13b2bc37 2022-10-23 stsp STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
83 1a52c9bf 2022-12-30 stsp static struct repo_write_client {
84 13b2bc37 2022-10-23 stsp uint32_t id;
86 7fec5f4a 2022-10-28 stsp int pack_pipe;
87 13b2bc37 2022-10-23 stsp struct got_pack pack;
88 13b2bc37 2022-10-23 stsp uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
89 13b2bc37 2022-10-23 stsp int packidx_fd;
90 13b2bc37 2022-10-23 stsp struct gotd_ref_updates ref_updates;
91 13b2bc37 2022-10-23 stsp int nref_updates;
92 1a52c9bf 2022-12-30 stsp } repo_write_client;
94 13b2bc37 2022-10-23 stsp static volatile sig_atomic_t sigint_received;
95 13b2bc37 2022-10-23 stsp static volatile sig_atomic_t sigterm_received;
98 13b2bc37 2022-10-23 stsp catch_sigint(int signo)
100 13b2bc37 2022-10-23 stsp sigint_received = 1;
103 13b2bc37 2022-10-23 stsp static void
104 13b2bc37 2022-10-23 stsp catch_sigterm(int signo)
106 13b2bc37 2022-10-23 stsp sigterm_received = 1;
109 13b2bc37 2022-10-23 stsp static const struct got_error *
110 13b2bc37 2022-10-23 stsp check_cancelled(void *arg)
112 13b2bc37 2022-10-23 stsp if (sigint_received || sigterm_received)
113 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_CANCELLED);
115 13b2bc37 2022-10-23 stsp return NULL;
118 13b2bc37 2022-10-23 stsp static const struct got_error *
119 13b2bc37 2022-10-23 stsp send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
120 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf)
122 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
123 13b2bc37 2022-10-23 stsp struct got_tag_object *tag;
124 13b2bc37 2022-10-23 stsp size_t namelen, len;
125 13b2bc37 2022-10-23 stsp char *peeled_refname = NULL;
126 13b2bc37 2022-10-23 stsp struct got_object_id *id;
127 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
129 13b2bc37 2022-10-23 stsp err = got_object_tag_open(&tag, repo_write.repo, obj);
131 13b2bc37 2022-10-23 stsp return err;
133 13b2bc37 2022-10-23 stsp if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
134 13b2bc37 2022-10-23 stsp err = got_error_from_errno("asprintf");
138 13b2bc37 2022-10-23 stsp id = got_object_tag_get_object_id(tag);
139 13b2bc37 2022-10-23 stsp namelen = strlen(peeled_refname);
141 13b2bc37 2022-10-23 stsp len = sizeof(struct gotd_imsg_ref) + namelen;
142 13b2bc37 2022-10-23 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
143 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_NO_SPACE);
147 13b2bc37 2022-10-23 stsp wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
148 13b2bc37 2022-10-23 stsp repo_write.pid, len);
149 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
150 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create REF");
154 13b2bc37 2022-10-23 stsp /* Keep in sync with struct gotd_imsg_ref definition. */
155 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
156 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add REF");
159 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
160 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add REF");
163 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
164 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add REF");
168 13b2bc37 2022-10-23 stsp wbuf->fd = -1;
169 13b2bc37 2022-10-23 stsp imsg_close(ibuf, wbuf);
171 13b2bc37 2022-10-23 stsp got_object_tag_close(tag);
172 13b2bc37 2022-10-23 stsp return err;
175 13b2bc37 2022-10-23 stsp static const struct got_error *
176 13b2bc37 2022-10-23 stsp send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
178 13b2bc37 2022-10-23 stsp const struct got_error *err;
179 13b2bc37 2022-10-23 stsp const char *refname = got_ref_get_name(ref);
180 13b2bc37 2022-10-23 stsp size_t namelen;
181 13b2bc37 2022-10-23 stsp struct got_object_id *id = NULL;
182 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
183 13b2bc37 2022-10-23 stsp size_t len;
184 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
186 13b2bc37 2022-10-23 stsp namelen = strlen(refname);
188 13b2bc37 2022-10-23 stsp len = sizeof(struct gotd_imsg_ref) + namelen;
189 13b2bc37 2022-10-23 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
190 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
192 13b2bc37 2022-10-23 stsp err = got_ref_resolve(&id, repo_write.repo, ref);
194 13b2bc37 2022-10-23 stsp return err;
196 13b2bc37 2022-10-23 stsp wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
197 13b2bc37 2022-10-23 stsp repo_write.pid, len);
198 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
199 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create REF");
203 13b2bc37 2022-10-23 stsp /* Keep in sync with struct gotd_imsg_ref definition. */
204 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
205 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF");
206 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
207 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF");
208 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, refname, namelen) == -1)
209 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF");
211 13b2bc37 2022-10-23 stsp wbuf->fd = -1;
212 13b2bc37 2022-10-23 stsp imsg_close(ibuf, wbuf);
214 13b2bc37 2022-10-23 stsp err = got_object_open(&obj, repo_write.repo, id);
217 13b2bc37 2022-10-23 stsp if (obj->type == GOT_OBJ_TYPE_TAG)
218 13b2bc37 2022-10-23 stsp err = send_peeled_tag_ref(ref, obj, ibuf);
221 13b2bc37 2022-10-23 stsp got_object_close(obj);
223 13b2bc37 2022-10-23 stsp return err;
226 13b2bc37 2022-10-23 stsp static const struct got_error *
227 1a52c9bf 2022-12-30 stsp list_refs(struct imsg *imsg)
229 13b2bc37 2022-10-23 stsp const struct got_error *err;
230 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
231 13b2bc37 2022-10-23 stsp struct got_reflist_head refs;
232 13b2bc37 2022-10-23 stsp struct got_reflist_entry *re;
233 13b2bc37 2022-10-23 stsp struct gotd_imsg_list_refs_internal ireq;
234 13b2bc37 2022-10-23 stsp size_t datalen;
235 13b2bc37 2022-10-23 stsp struct gotd_imsg_reflist irefs;
236 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
237 13b2bc37 2022-10-23 stsp int client_fd = imsg->fd;
239 13b2bc37 2022-10-23 stsp TAILQ_INIT(&refs);
241 13b2bc37 2022-10-23 stsp if (client_fd == -1)
242 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
244 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
245 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
246 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
247 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
249 1a52c9bf 2022-12-30 stsp if (ireq.client_id == 0)
250 1a52c9bf 2022-12-30 stsp return got_error(GOT_ERR_CLIENT_ID);
251 1a52c9bf 2022-12-30 stsp if (client->id != 0) {
252 1a52c9bf 2022-12-30 stsp return got_error_msg(GOT_ERR_CLIENT_ID,
253 1a52c9bf 2022-12-30 stsp "duplicate list-refs request");
255 1a52c9bf 2022-12-30 stsp client->id = ireq.client_id;
256 1a52c9bf 2022-12-30 stsp client->fd = client_fd;
257 1a52c9bf 2022-12-30 stsp client->pack_pipe = -1;
258 1a52c9bf 2022-12-30 stsp client->packidx_fd = -1;
259 1a52c9bf 2022-12-30 stsp client->nref_updates = 0;
261 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, client_fd);
263 13b2bc37 2022-10-23 stsp err = got_ref_list(&refs, repo_write.repo, "",
264 13b2bc37 2022-10-23 stsp got_ref_cmp_by_name, NULL);
266 13b2bc37 2022-10-23 stsp return err;
268 13b2bc37 2022-10-23 stsp memset(&irefs, 0, sizeof(irefs));
269 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(re, &refs, entry) {
270 13b2bc37 2022-10-23 stsp struct got_object_id *id;
271 13b2bc37 2022-10-23 stsp int obj_type;
273 13b2bc37 2022-10-23 stsp if (got_ref_is_symbolic(re->ref))
276 13b2bc37 2022-10-23 stsp irefs.nrefs++;
278 13b2bc37 2022-10-23 stsp /* Account for a peeled tag refs. */
279 13b2bc37 2022-10-23 stsp err = got_ref_resolve(&id, repo_write.repo, re->ref);
282 13b2bc37 2022-10-23 stsp err = got_object_get_type(&obj_type, repo_write.repo, id);
286 13b2bc37 2022-10-23 stsp if (obj_type == GOT_OBJ_TYPE_TAG)
287 13b2bc37 2022-10-23 stsp irefs.nrefs++;
290 13b2bc37 2022-10-23 stsp if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
291 13b2bc37 2022-10-23 stsp repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
292 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_compose REFLIST");
296 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(re, &refs, entry) {
297 13b2bc37 2022-10-23 stsp if (got_ref_is_symbolic(re->ref))
299 13b2bc37 2022-10-23 stsp err = send_ref(re->ref, &ibuf);
304 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(&ibuf);
306 13b2bc37 2022-10-23 stsp got_ref_list_free(&refs);
307 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
308 13b2bc37 2022-10-23 stsp return err;
311 13b2bc37 2022-10-23 stsp static const struct got_error *
312 13b2bc37 2022-10-23 stsp protect_ref_namespace(struct got_reference *ref, const char *namespace)
314 13b2bc37 2022-10-23 stsp size_t len = strlen(namespace);
316 13b2bc37 2022-10-23 stsp if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
317 13b2bc37 2022-10-23 stsp namespace[len -1] != '/') {
318 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_REF_NAME,
319 13b2bc37 2022-10-23 stsp "reference namespace '%s'", namespace);
322 13b2bc37 2022-10-23 stsp if (strncmp(namespace, got_ref_get_name(ref), len) == 0)
323 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
325 13b2bc37 2022-10-23 stsp return NULL;
328 13b2bc37 2022-10-23 stsp static const struct got_error *
329 1a52c9bf 2022-12-30 stsp recv_ref_update(struct imsg *imsg)
331 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
332 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
333 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update iref;
334 13b2bc37 2022-10-23 stsp size_t datalen;
335 13b2bc37 2022-10-23 stsp char *refname = NULL;
336 13b2bc37 2022-10-23 stsp struct got_reference *ref = NULL;
337 13b2bc37 2022-10-23 stsp struct got_object_id *id = NULL;
338 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
339 13b2bc37 2022-10-23 stsp struct gotd_ref_update *ref_update = NULL;
341 13b2bc37 2022-10-23 stsp log_debug("ref-update received");
343 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
344 13b2bc37 2022-10-23 stsp if (datalen < sizeof(iref))
345 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
346 13b2bc37 2022-10-23 stsp memcpy(&iref, imsg->data, sizeof(iref));
347 13b2bc37 2022-10-23 stsp if (datalen != sizeof(iref) + iref.name_len)
348 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
350 1a52c9bf 2022-12-30 stsp imsg_init(&ibuf, client->fd);
352 13b2bc37 2022-10-23 stsp refname = malloc(iref.name_len + 1);
353 13b2bc37 2022-10-23 stsp if (refname == NULL)
354 13b2bc37 2022-10-23 stsp return got_error_from_errno("malloc");
355 13b2bc37 2022-10-23 stsp memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
356 13b2bc37 2022-10-23 stsp refname[iref.name_len] = '\0';
358 13b2bc37 2022-10-23 stsp ref_update = calloc(1, sizeof(*ref_update));
359 13b2bc37 2022-10-23 stsp if (ref_update == NULL) {
360 13b2bc37 2022-10-23 stsp err = got_error_from_errno("malloc");
364 13b2bc37 2022-10-23 stsp memcpy(ref_update->old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
365 13b2bc37 2022-10-23 stsp memcpy(ref_update->new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
367 13b2bc37 2022-10-23 stsp err = got_ref_open(&ref, repo_write.repo, refname, 0);
369 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_NOT_REF)
371 13b2bc37 2022-10-23 stsp err = got_ref_alloc(&ref, refname, &ref_update->new_id);
374 13b2bc37 2022-10-23 stsp ref_update->ref_is_new = 1;
376 13b2bc37 2022-10-23 stsp if (got_ref_is_symbolic(ref)) {
377 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
378 13b2bc37 2022-10-23 stsp "'%s' is a symbolic reference and cannot "
379 13b2bc37 2022-10-23 stsp "be updated", got_ref_get_name(ref));
382 13b2bc37 2022-10-23 stsp if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
383 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
384 13b2bc37 2022-10-23 stsp "%s: does not begin with 'refs/'",
385 13b2bc37 2022-10-23 stsp got_ref_get_name(ref));
389 13b2bc37 2022-10-23 stsp err = protect_ref_namespace(ref, "refs/got/");
392 13b2bc37 2022-10-23 stsp err = protect_ref_namespace(ref, "refs/remotes/");
396 13b2bc37 2022-10-23 stsp if (!ref_update->ref_is_new) {
398 13b2bc37 2022-10-23 stsp * Ensure the client's idea of this update is still valid.
399 13b2bc37 2022-10-23 stsp * At this point we can only return an error, to prevent
400 13b2bc37 2022-10-23 stsp * the client from uploading a pack file which will likely
401 13b2bc37 2022-10-23 stsp * have to be discarded.
403 13b2bc37 2022-10-23 stsp err = got_ref_resolve(&id, repo_write.repo, ref);
407 13b2bc37 2022-10-23 stsp if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
408 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
409 13b2bc37 2022-10-23 stsp "%s has been modified by someone else "
410 13b2bc37 2022-10-23 stsp "while transaction was in progress",
411 13b2bc37 2022-10-23 stsp got_ref_get_name(ref));
416 13b2bc37 2022-10-23 stsp gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
417 13b2bc37 2022-10-23 stsp repo_write.pid);
419 13b2bc37 2022-10-23 stsp ref_update->ref = ref;
420 1a52c9bf 2022-12-30 stsp STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
421 1a52c9bf 2022-12-30 stsp client->nref_updates++;
422 13b2bc37 2022-10-23 stsp ref = NULL;
423 13b2bc37 2022-10-23 stsp ref_update = NULL;
426 13b2bc37 2022-10-23 stsp got_ref_close(ref);
427 13b2bc37 2022-10-23 stsp free(ref_update);
428 13b2bc37 2022-10-23 stsp free(refname);
430 13b2bc37 2022-10-23 stsp return err;
433 13b2bc37 2022-10-23 stsp static const struct got_error *
434 13b2bc37 2022-10-23 stsp pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
435 13b2bc37 2022-10-23 stsp uint32_t nobj_loose, uint32_t nobj_resolved)
437 13b2bc37 2022-10-23 stsp int p_indexed = 0, p_resolved = 0;
438 13b2bc37 2022-10-23 stsp int nobj_delta = nobj_total - nobj_loose;
440 13b2bc37 2022-10-23 stsp if (nobj_total > 0)
441 13b2bc37 2022-10-23 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
443 13b2bc37 2022-10-23 stsp if (nobj_delta > 0)
444 13b2bc37 2022-10-23 stsp p_resolved = (nobj_resolved * 100) / nobj_delta;
446 13b2bc37 2022-10-23 stsp if (p_resolved > 0) {
447 13b2bc37 2022-10-23 stsp log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
448 13b2bc37 2022-10-23 stsp nobj_total, p_indexed, nobj_delta, p_resolved);
450 13b2bc37 2022-10-23 stsp log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
452 13b2bc37 2022-10-23 stsp return NULL;
455 13b2bc37 2022-10-23 stsp static const struct got_error *
456 13b2bc37 2022-10-23 stsp read_more_pack_stream(int infd, BUF *buf, size_t minsize)
458 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
459 13b2bc37 2022-10-23 stsp uint8_t readahead[65536];
460 13b2bc37 2022-10-23 stsp size_t have, newlen;
462 13b2bc37 2022-10-23 stsp err = got_poll_read_full(infd, &have,
463 13b2bc37 2022-10-23 stsp readahead, sizeof(readahead), minsize);
465 13b2bc37 2022-10-23 stsp return err;
467 13b2bc37 2022-10-23 stsp err = buf_append(&newlen, buf, readahead, have);
469 13b2bc37 2022-10-23 stsp return err;
470 13b2bc37 2022-10-23 stsp return NULL;
473 13b2bc37 2022-10-23 stsp static const struct got_error *
474 13b2bc37 2022-10-23 stsp copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
475 13b2bc37 2022-10-23 stsp off_t *outsize, BUF *buf, size_t *buf_pos, SHA1_CTX *ctx)
477 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
478 13b2bc37 2022-10-23 stsp uint8_t t = 0;
479 13b2bc37 2022-10-23 stsp uint64_t s = 0;
480 13b2bc37 2022-10-23 stsp uint8_t sizebuf[8];
481 13b2bc37 2022-10-23 stsp size_t i = 0;
482 13b2bc37 2022-10-23 stsp off_t obj_offset = *outsize;
485 13b2bc37 2022-10-23 stsp /* We do not support size values which don't fit in 64 bit. */
487 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
488 22ec3416 2022-10-25 op "packfile offset %lld", (long long)obj_offset);
490 13b2bc37 2022-10-23 stsp if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
491 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
492 13b2bc37 2022-10-23 stsp sizeof(sizebuf[0]));
494 13b2bc37 2022-10-23 stsp return err;
497 13b2bc37 2022-10-23 stsp sizebuf[i] = buf_getc(buf, *buf_pos);
498 13b2bc37 2022-10-23 stsp *buf_pos += sizeof(sizebuf[i]);
500 13b2bc37 2022-10-23 stsp if (i == 0) {
501 13b2bc37 2022-10-23 stsp t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
502 13b2bc37 2022-10-23 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
503 13b2bc37 2022-10-23 stsp s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
505 13b2bc37 2022-10-23 stsp size_t shift = 4 + 7 * (i - 1);
506 13b2bc37 2022-10-23 stsp s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
510 13b2bc37 2022-10-23 stsp } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
512 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, sizebuf, i, ctx);
514 13b2bc37 2022-10-23 stsp return err;
515 13b2bc37 2022-10-23 stsp *outsize += i;
519 13b2bc37 2022-10-23 stsp return NULL;
522 13b2bc37 2022-10-23 stsp static const struct got_error *
523 13b2bc37 2022-10-23 stsp copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
524 13b2bc37 2022-10-23 stsp SHA1_CTX *ctx)
526 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
527 13b2bc37 2022-10-23 stsp size_t remain = buf_len(buf) - *buf_pos;
529 13b2bc37 2022-10-23 stsp if (remain < SHA1_DIGEST_LENGTH) {
530 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
531 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH - remain);
533 13b2bc37 2022-10-23 stsp return err;
536 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
537 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH, ctx);
539 13b2bc37 2022-10-23 stsp return err;
541 13b2bc37 2022-10-23 stsp *buf_pos += SHA1_DIGEST_LENGTH;
542 13b2bc37 2022-10-23 stsp return NULL;
545 13b2bc37 2022-10-23 stsp static const struct got_error *
546 13b2bc37 2022-10-23 stsp copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
547 13b2bc37 2022-10-23 stsp SHA1_CTX *ctx)
549 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
550 13b2bc37 2022-10-23 stsp uint64_t o = 0;
551 13b2bc37 2022-10-23 stsp uint8_t offbuf[8];
552 13b2bc37 2022-10-23 stsp size_t i = 0;
553 13b2bc37 2022-10-23 stsp off_t obj_offset = *outsize;
556 13b2bc37 2022-10-23 stsp /* We do not support offset values which don't fit in 64 bit. */
558 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
559 22ec3416 2022-10-25 op "packfile offset %lld", (long long)obj_offset);
561 13b2bc37 2022-10-23 stsp if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
562 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
563 13b2bc37 2022-10-23 stsp sizeof(offbuf[0]));
565 13b2bc37 2022-10-23 stsp return err;
568 13b2bc37 2022-10-23 stsp offbuf[i] = buf_getc(buf, *buf_pos);
569 13b2bc37 2022-10-23 stsp *buf_pos += sizeof(offbuf[i]);
571 13b2bc37 2022-10-23 stsp if (i == 0)
572 13b2bc37 2022-10-23 stsp o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
576 13b2bc37 2022-10-23 stsp o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
579 13b2bc37 2022-10-23 stsp } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
581 13b2bc37 2022-10-23 stsp if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
582 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PACK_OFFSET);
584 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, offbuf, i, ctx);
586 13b2bc37 2022-10-23 stsp return err;
588 13b2bc37 2022-10-23 stsp *outsize += i;
589 13b2bc37 2022-10-23 stsp return NULL;
592 13b2bc37 2022-10-23 stsp static const struct got_error *
593 13b2bc37 2022-10-23 stsp copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
594 13b2bc37 2022-10-23 stsp SHA1_CTX *ctx)
596 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
597 13b2bc37 2022-10-23 stsp z_stream z;
599 13b2bc37 2022-10-23 stsp char voidbuf[1024];
600 13b2bc37 2022-10-23 stsp size_t consumed_total = 0;
601 13b2bc37 2022-10-23 stsp off_t zstream_offset = *outsize;
603 13b2bc37 2022-10-23 stsp memset(&z, 0, sizeof(z));
605 13b2bc37 2022-10-23 stsp z.zalloc = Z_NULL;
606 13b2bc37 2022-10-23 stsp z.zfree = Z_NULL;
607 13b2bc37 2022-10-23 stsp zret = inflateInit(&z);
608 13b2bc37 2022-10-23 stsp if (zret != Z_OK) {
609 13b2bc37 2022-10-23 stsp if (zret == Z_ERRNO)
610 13b2bc37 2022-10-23 stsp return got_error_from_errno("inflateInit");
611 13b2bc37 2022-10-23 stsp if (zret == Z_MEM_ERROR) {
612 13b2bc37 2022-10-23 stsp errno = ENOMEM;
613 13b2bc37 2022-10-23 stsp return got_error_from_errno("inflateInit");
615 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_DECOMPRESSION,
616 13b2bc37 2022-10-23 stsp "inflateInit failed");
619 13b2bc37 2022-10-23 stsp while (zret != Z_STREAM_END) {
620 13b2bc37 2022-10-23 stsp size_t last_total_in, consumed;
623 13b2bc37 2022-10-23 stsp * Decompress into the void. Object data will be parsed
624 13b2bc37 2022-10-23 stsp * later, when the pack file is indexed. For now, we just
625 13b2bc37 2022-10-23 stsp * want to locate the end of the compressed stream.
627 13b2bc37 2022-10-23 stsp while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
628 13b2bc37 2022-10-23 stsp last_total_in = z.total_in;
629 13b2bc37 2022-10-23 stsp z.next_in = buf_get(buf) + *buf_pos;
630 13b2bc37 2022-10-23 stsp z.avail_in = buf_len(buf) - *buf_pos;
631 13b2bc37 2022-10-23 stsp z.next_out = voidbuf;
632 13b2bc37 2022-10-23 stsp z.avail_out = sizeof(voidbuf);
634 13b2bc37 2022-10-23 stsp zret = inflate(&z, Z_SYNC_FLUSH);
635 13b2bc37 2022-10-23 stsp if (zret != Z_OK && zret != Z_BUF_ERROR &&
636 13b2bc37 2022-10-23 stsp zret != Z_STREAM_END) {
637 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_DECOMPRESSION,
638 22ec3416 2022-10-25 op "packfile offset %lld",
639 22ec3416 2022-10-25 op (long long)zstream_offset);
642 13b2bc37 2022-10-23 stsp consumed = z.total_in - last_total_in;
644 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
645 13b2bc37 2022-10-23 stsp consumed, ctx);
649 13b2bc37 2022-10-23 stsp err = buf_discard(buf, *buf_pos + consumed);
652 13b2bc37 2022-10-23 stsp *buf_pos = 0;
654 13b2bc37 2022-10-23 stsp consumed_total += consumed;
657 13b2bc37 2022-10-23 stsp if (zret != Z_STREAM_END) {
658 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf, 1);
664 13b2bc37 2022-10-23 stsp if (err == NULL)
665 13b2bc37 2022-10-23 stsp *outsize += consumed_total;
667 13b2bc37 2022-10-23 stsp inflateEnd(&z);
668 13b2bc37 2022-10-23 stsp return err;
671 13b2bc37 2022-10-23 stsp static const struct got_error *
672 13b2bc37 2022-10-23 stsp validate_object_type(int obj_type)
674 13b2bc37 2022-10-23 stsp switch (obj_type) {
675 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_BLOB:
676 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_COMMIT:
677 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_TREE:
678 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_TAG:
679 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
680 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
681 13b2bc37 2022-10-23 stsp return NULL;
686 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_OBJ_TYPE);
689 13b2bc37 2022-10-23 stsp static const struct got_error *
690 13b2bc37 2022-10-23 stsp recv_packdata(off_t *outsize, uint8_t *sha1, int infd, int outfd)
692 13b2bc37 2022-10-23 stsp const struct got_error *err;
693 13b2bc37 2022-10-23 stsp struct got_packfile_hdr hdr;
694 13b2bc37 2022-10-23 stsp size_t have;
695 13b2bc37 2022-10-23 stsp uint32_t nobj, nhave = 0;
696 13b2bc37 2022-10-23 stsp SHA1_CTX ctx;
697 13b2bc37 2022-10-23 stsp uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
698 13b2bc37 2022-10-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
699 13b2bc37 2022-10-23 stsp BUF *buf = NULL;
700 13b2bc37 2022-10-23 stsp size_t buf_pos = 0, remain;
703 13b2bc37 2022-10-23 stsp *outsize = 0;
704 13b2bc37 2022-10-23 stsp SHA1Init(&ctx);
706 13b2bc37 2022-10-23 stsp err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
708 13b2bc37 2022-10-23 stsp return err;
709 13b2bc37 2022-10-23 stsp if (have != sizeof(hdr))
710 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
711 13b2bc37 2022-10-23 stsp *outsize += have;
713 13b2bc37 2022-10-23 stsp if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
714 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
715 13b2bc37 2022-10-23 stsp "bad packfile signature");
716 13b2bc37 2022-10-23 stsp if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
717 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
718 13b2bc37 2022-10-23 stsp "bad packfile version");
720 13b2bc37 2022-10-23 stsp nobj = be32toh(hdr.nobjects);
721 13b2bc37 2022-10-23 stsp if (nobj == 0)
722 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
723 13b2bc37 2022-10-23 stsp "bad packfile with zero objects");
725 13b2bc37 2022-10-23 stsp log_debug("expecting %d objects", nobj);
727 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
729 13b2bc37 2022-10-23 stsp return err;
731 13b2bc37 2022-10-23 stsp err = buf_alloc(&buf, 65536);
733 13b2bc37 2022-10-23 stsp return err;
735 13b2bc37 2022-10-23 stsp while (nhave != nobj) {
736 13b2bc37 2022-10-23 stsp uint8_t obj_type;
737 13b2bc37 2022-10-23 stsp uint64_t obj_size;
739 13b2bc37 2022-10-23 stsp err = copy_object_type_and_size(&obj_type, &obj_size,
740 13b2bc37 2022-10-23 stsp infd, outfd, outsize, buf, &buf_pos, &ctx);
744 13b2bc37 2022-10-23 stsp err = validate_object_type(obj_type);
748 13b2bc37 2022-10-23 stsp if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
749 13b2bc37 2022-10-23 stsp err = copy_ref_delta(infd, outfd, outsize,
750 13b2bc37 2022-10-23 stsp buf, &buf_pos, &ctx);
753 13b2bc37 2022-10-23 stsp } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
754 13b2bc37 2022-10-23 stsp err = copy_offset_delta(infd, outfd, outsize,
755 13b2bc37 2022-10-23 stsp buf, &buf_pos, &ctx);
760 13b2bc37 2022-10-23 stsp err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
767 13b2bc37 2022-10-23 stsp log_debug("received %u objects", nobj);
769 13b2bc37 2022-10-23 stsp SHA1Final(expected_sha1, &ctx);
771 13b2bc37 2022-10-23 stsp remain = buf_len(buf) - buf_pos;
772 13b2bc37 2022-10-23 stsp if (remain < SHA1_DIGEST_LENGTH) {
773 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
774 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH - remain);
776 13b2bc37 2022-10-23 stsp return err;
779 13b2bc37 2022-10-23 stsp got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
780 13b2bc37 2022-10-23 stsp log_debug("expect SHA1: %s", hex);
781 13b2bc37 2022-10-23 stsp got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
782 13b2bc37 2022-10-23 stsp log_debug("actual SHA1: %s", hex);
784 13b2bc37 2022-10-23 stsp if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
785 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH) != 0) {
786 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PACKFILE_CSUM);
790 13b2bc37 2022-10-23 stsp memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
792 13b2bc37 2022-10-23 stsp w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
793 13b2bc37 2022-10-23 stsp if (w == -1) {
794 13b2bc37 2022-10-23 stsp err = got_error_from_errno("write");
797 13b2bc37 2022-10-23 stsp if (w != SHA1_DIGEST_LENGTH) {
798 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_IO);
802 13b2bc37 2022-10-23 stsp *outsize += SHA1_DIGEST_LENGTH;
804 13b2bc37 2022-10-23 stsp if (fsync(outfd) == -1) {
805 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fsync");
808 13b2bc37 2022-10-23 stsp if (lseek(outfd, 0L, SEEK_SET) == -1) {
809 13b2bc37 2022-10-23 stsp err = got_error_from_errno("lseek");
813 13b2bc37 2022-10-23 stsp buf_free(buf);
814 13b2bc37 2022-10-23 stsp return err;
817 13b2bc37 2022-10-23 stsp static const struct got_error *
818 1a52c9bf 2022-12-30 stsp report_pack_status(const struct got_error *unpack_err)
820 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
821 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
822 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_status istatus;
823 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
824 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
825 13b2bc37 2022-10-23 stsp const char *unpack_ok = "unpack ok\n";
826 13b2bc37 2022-10-23 stsp size_t len;
828 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, client->fd);
830 13b2bc37 2022-10-23 stsp if (unpack_err)
831 13b2bc37 2022-10-23 stsp istatus.reason_len = strlen(unpack_err->msg);
833 13b2bc37 2022-10-23 stsp istatus.reason_len = strlen(unpack_ok);
835 13b2bc37 2022-10-23 stsp len = sizeof(istatus) + istatus.reason_len;
836 13b2bc37 2022-10-23 stsp wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
837 13b2bc37 2022-10-23 stsp repo_write.pid, len);
838 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
839 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create PACKFILE_STATUS");
843 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
844 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add PACKFILE_STATUS");
848 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, err ? err->msg : unpack_ok,
849 13b2bc37 2022-10-23 stsp istatus.reason_len) == -1) {
850 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add PACKFILE_STATUS");
854 13b2bc37 2022-10-23 stsp wbuf->fd = -1;
855 13b2bc37 2022-10-23 stsp imsg_close(&ibuf, wbuf);
857 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(&ibuf);
859 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
860 13b2bc37 2022-10-23 stsp return err;
863 13b2bc37 2022-10-23 stsp static const struct got_error *
864 1a52c9bf 2022-12-30 stsp recv_packfile(struct imsg *imsg)
866 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL, *unpack_err;
867 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
868 13b2bc37 2022-10-23 stsp struct gotd_imsg_recv_packfile ireq;
869 13b2bc37 2022-10-23 stsp FILE *tempfiles[3] = { NULL, NULL, NULL };
870 13b2bc37 2022-10-23 stsp struct repo_tempfile {
873 13b2bc37 2022-10-23 stsp } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
875 13b2bc37 2022-10-23 stsp size_t datalen;
876 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
877 13b2bc37 2022-10-23 stsp struct got_ratelimit rl;
878 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
879 13b2bc37 2022-10-23 stsp off_t pack_filesize = 0;
881 13b2bc37 2022-10-23 stsp log_debug("packfile request received");
883 13b2bc37 2022-10-23 stsp got_ratelimit_init(&rl, 2, 0);
885 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
886 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
887 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
888 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
890 1a52c9bf 2022-12-30 stsp if (client->pack_pipe == -1 || client->packidx_fd == -1)
891 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
893 1a52c9bf 2022-12-30 stsp imsg_init(&ibuf, client->fd);
895 13b2bc37 2022-10-23 stsp if (imsg->fd == -1)
896 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
898 1a52c9bf 2022-12-30 stsp pack = &client->pack;
899 13b2bc37 2022-10-23 stsp memset(pack, 0, sizeof(*pack));
900 13b2bc37 2022-10-23 stsp pack->fd = imsg->fd;
901 13b2bc37 2022-10-23 stsp err = got_delta_cache_alloc(&pack->delta_cache);
903 13b2bc37 2022-10-23 stsp return err;
905 13b2bc37 2022-10-23 stsp for (i = 0; i < nitems(repo_tempfiles); i++) {
906 13b2bc37 2022-10-23 stsp struct repo_tempfile *t = &repo_tempfiles[i];
907 13b2bc37 2022-10-23 stsp err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
912 13b2bc37 2022-10-23 stsp for (i = 0; i < nitems(tempfiles); i++) {
913 13b2bc37 2022-10-23 stsp int fd = dup(repo_tempfiles[i].fd);
915 13b2bc37 2022-10-23 stsp if (fd == -1) {
916 13b2bc37 2022-10-23 stsp err = got_error_from_errno("dup");
919 13b2bc37 2022-10-23 stsp f = fdopen(fd, "w+");
920 13b2bc37 2022-10-23 stsp if (f == NULL) {
921 13b2bc37 2022-10-23 stsp err = got_error_from_errno("dup");
925 13b2bc37 2022-10-23 stsp tempfiles[i] = f;
928 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(&ibuf);
932 13b2bc37 2022-10-23 stsp log_debug("receiving pack data");
933 1a52c9bf 2022-12-30 stsp unpack_err = recv_packdata(&pack_filesize, client->pack_sha1,
934 1a52c9bf 2022-12-30 stsp client->pack_pipe, pack->fd);
935 13b2bc37 2022-10-23 stsp if (ireq.report_status) {
936 1a52c9bf 2022-12-30 stsp err = report_pack_status(unpack_err);
938 13b2bc37 2022-10-23 stsp /* Git clients hang up after sending the pack file. */
939 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_EOF)
940 13b2bc37 2022-10-23 stsp err = NULL;
943 13b2bc37 2022-10-23 stsp if (unpack_err)
944 13b2bc37 2022-10-23 stsp err = unpack_err;
948 13b2bc37 2022-10-23 stsp log_debug("pack data received");
950 13b2bc37 2022-10-23 stsp pack->filesize = pack_filesize;
952 ad4cc361 2022-10-27 op log_debug("begin indexing pack (%lld bytes in size)",
953 ad4cc361 2022-10-27 op (long long)pack->filesize);
954 1a52c9bf 2022-12-30 stsp err = got_pack_index(pack, client->packidx_fd,
955 1a52c9bf 2022-12-30 stsp tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
956 13b2bc37 2022-10-23 stsp pack_index_progress, NULL, &rl);
959 13b2bc37 2022-10-23 stsp log_debug("done indexing pack");
961 1a52c9bf 2022-12-30 stsp if (fsync(client->packidx_fd) == -1) {
962 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fsync");
965 1a52c9bf 2022-12-30 stsp if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
966 13b2bc37 2022-10-23 stsp err = got_error_from_errno("lseek");
968 1a52c9bf 2022-12-30 stsp if (close(client->pack_pipe) == -1 && err == NULL)
969 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
970 1a52c9bf 2022-12-30 stsp client->pack_pipe = -1;
971 13b2bc37 2022-10-23 stsp for (i = 0; i < nitems(repo_tempfiles); i++) {
972 13b2bc37 2022-10-23 stsp struct repo_tempfile *t = &repo_tempfiles[i];
973 13b2bc37 2022-10-23 stsp if (t->idx != -1)
974 13b2bc37 2022-10-23 stsp got_repo_temp_fds_put(t->idx, repo_write.repo);
976 13b2bc37 2022-10-23 stsp for (i = 0; i < nitems(tempfiles); i++) {
977 13b2bc37 2022-10-23 stsp if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
978 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fclose");
981 13b2bc37 2022-10-23 stsp got_pack_close(pack);
982 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
983 13b2bc37 2022-10-23 stsp return err;
986 13b2bc37 2022-10-23 stsp static const struct got_error *
987 1a52c9bf 2022-12-30 stsp verify_packfile(void)
989 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL, *close_err;
990 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
991 13b2bc37 2022-10-23 stsp struct gotd_ref_update *ref_update;
992 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
993 13b2bc37 2022-10-23 stsp struct stat sb;
994 13b2bc37 2022-10-23 stsp char *id_str = NULL;
995 13b2bc37 2022-10-23 stsp int idx = -1;
997 13b2bc37 2022-10-23 stsp if (STAILQ_EMPTY(&client->ref_updates)) {
998 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
999 13b2bc37 2022-10-23 stsp "cannot verify pack file without any ref-updates");
1002 13b2bc37 2022-10-23 stsp if (client->pack.fd == -1) {
1003 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1004 13b2bc37 2022-10-23 stsp "invalid pack file handle during pack verification");
1006 13b2bc37 2022-10-23 stsp if (client->packidx_fd == -1) {
1007 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1008 13b2bc37 2022-10-23 stsp "invalid pack index handle during pack verification");
1011 13b2bc37 2022-10-23 stsp if (fstat(client->packidx_fd, &sb) == -1)
1012 13b2bc37 2022-10-23 stsp return got_error_from_errno("pack index fstat");
1014 13b2bc37 2022-10-23 stsp packidx = malloc(sizeof(*packidx));
1015 13b2bc37 2022-10-23 stsp memset(packidx, 0, sizeof(*packidx));
1016 13b2bc37 2022-10-23 stsp packidx->fd = client->packidx_fd;
1017 13b2bc37 2022-10-23 stsp client->packidx_fd = -1;
1018 13b2bc37 2022-10-23 stsp packidx->len = sb.st_size;
1020 13b2bc37 2022-10-23 stsp err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1022 13b2bc37 2022-10-23 stsp return err;
1024 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1025 13b2bc37 2022-10-23 stsp err = got_object_id_str(&id_str, &ref_update->new_id);
1027 13b2bc37 2022-10-23 stsp goto done;
1029 13b2bc37 2022-10-23 stsp idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1030 13b2bc37 2022-10-23 stsp if (idx == -1) {
1031 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1032 13b2bc37 2022-10-23 stsp "advertised object %s is missing from pack file",
1034 13b2bc37 2022-10-23 stsp goto done;
1039 13b2bc37 2022-10-23 stsp close_err = got_packidx_close(packidx);
1040 13b2bc37 2022-10-23 stsp if (close_err && err == NULL)
1041 13b2bc37 2022-10-23 stsp err = close_err;
1042 13b2bc37 2022-10-23 stsp free(id_str);
1043 13b2bc37 2022-10-23 stsp return err;
1046 13b2bc37 2022-10-23 stsp static const struct got_error *
1047 1a52c9bf 2022-12-30 stsp install_packfile(struct gotd_imsgev *iev)
1049 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1050 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_install inst;
1053 13b2bc37 2022-10-23 stsp memset(&inst, 0, sizeof(inst));
1054 13b2bc37 2022-10-23 stsp inst.client_id = client->id;
1055 13b2bc37 2022-10-23 stsp memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1057 13b2bc37 2022-10-23 stsp ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1058 13b2bc37 2022-10-23 stsp PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1059 13b2bc37 2022-10-23 stsp if (ret == -1)
1060 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1062 13b2bc37 2022-10-23 stsp return NULL;
1065 13b2bc37 2022-10-23 stsp static const struct got_error *
1066 1a52c9bf 2022-12-30 stsp send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1068 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1069 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_updates_start istart;
1072 13b2bc37 2022-10-23 stsp memset(&istart, 0, sizeof(istart));
1073 13b2bc37 2022-10-23 stsp istart.nref_updates = nref_updates;
1074 13b2bc37 2022-10-23 stsp istart.client_id = client->id;
1076 13b2bc37 2022-10-23 stsp ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1077 13b2bc37 2022-10-23 stsp PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1078 13b2bc37 2022-10-23 stsp if (ret == -1)
1079 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_compose REF_UPDATES_START");
1081 13b2bc37 2022-10-23 stsp return NULL;
1085 13b2bc37 2022-10-23 stsp static const struct got_error *
1086 1a52c9bf 2022-12-30 stsp send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1088 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1089 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update iref;
1090 13b2bc37 2022-10-23 stsp const char *refname = got_ref_get_name(ref_update->ref);
1091 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
1092 13b2bc37 2022-10-23 stsp size_t len;
1094 13b2bc37 2022-10-23 stsp memset(&iref, 0, sizeof(iref));
1095 13b2bc37 2022-10-23 stsp memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1096 13b2bc37 2022-10-23 stsp memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1097 13b2bc37 2022-10-23 stsp iref.ref_is_new = ref_update->ref_is_new;
1098 13b2bc37 2022-10-23 stsp iref.client_id = client->id;
1099 13b2bc37 2022-10-23 stsp iref.name_len = strlen(refname);
1101 13b2bc37 2022-10-23 stsp len = sizeof(iref) + iref.name_len;
1102 13b2bc37 2022-10-23 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1103 13b2bc37 2022-10-23 stsp repo_write.pid, len);
1104 13b2bc37 2022-10-23 stsp if (wbuf == NULL)
1105 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_create REF_UPDATE");
1107 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1108 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE");
1109 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, refname, iref.name_len) == -1)
1110 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE");
1112 13b2bc37 2022-10-23 stsp wbuf->fd = -1;
1113 13b2bc37 2022-10-23 stsp imsg_close(&iev->ibuf, wbuf);
1115 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
1116 13b2bc37 2022-10-23 stsp return NULL;
1119 13b2bc37 2022-10-23 stsp static const struct got_error *
1120 1a52c9bf 2022-12-30 stsp update_refs(struct gotd_imsgev *iev)
1122 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1123 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1124 13b2bc37 2022-10-23 stsp struct gotd_ref_update *ref_update;
1126 1a52c9bf 2022-12-30 stsp err = send_ref_updates_start(client->nref_updates, iev);
1128 13b2bc37 2022-10-23 stsp return err;
1130 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1131 1a52c9bf 2022-12-30 stsp err = send_ref_update(ref_update, iev);
1133 13b2bc37 2022-10-23 stsp goto done;
1136 13b2bc37 2022-10-23 stsp return err;
1139 13b2bc37 2022-10-23 stsp static const struct got_error *
1140 13b2bc37 2022-10-23 stsp recv_disconnect(struct imsg *imsg)
1142 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1143 13b2bc37 2022-10-23 stsp struct gotd_imsg_disconnect idisconnect;
1144 13b2bc37 2022-10-23 stsp size_t datalen;
1145 1a52c9bf 2022-12-30 stsp int pack_pipe = -1, idxfd = -1;
1146 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1148 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1149 13b2bc37 2022-10-23 stsp if (datalen != sizeof(idisconnect))
1150 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1151 13b2bc37 2022-10-23 stsp memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
1153 13b2bc37 2022-10-23 stsp log_debug("client disconnecting");
1155 13b2bc37 2022-10-23 stsp while (!STAILQ_EMPTY(&client->ref_updates)) {
1156 13b2bc37 2022-10-23 stsp struct gotd_ref_update *ref_update;
1157 13b2bc37 2022-10-23 stsp ref_update = STAILQ_FIRST(&client->ref_updates);
1158 13b2bc37 2022-10-23 stsp STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1159 13b2bc37 2022-10-23 stsp got_ref_close(ref_update->ref);
1160 13b2bc37 2022-10-23 stsp free(ref_update);
1162 13b2bc37 2022-10-23 stsp err = got_pack_close(&client->pack);
1163 1a52c9bf 2022-12-30 stsp if (client->fd != -1 && close(client->fd) == -1)
1164 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
1165 1a52c9bf 2022-12-30 stsp pack_pipe = client->pack_pipe;
1166 7fec5f4a 2022-10-28 stsp if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
1167 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
1168 1a52c9bf 2022-12-30 stsp idxfd = client->packidx_fd;
1169 13b2bc37 2022-10-23 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1170 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
1171 13b2bc37 2022-10-23 stsp return err;
1174 13b2bc37 2022-10-23 stsp static const struct got_error *
1175 1a52c9bf 2022-12-30 stsp receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1177 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1178 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_pipe ireq;
1179 13b2bc37 2022-10-23 stsp size_t datalen;
1181 13b2bc37 2022-10-23 stsp log_debug("receving pack pipe descriptor");
1183 13b2bc37 2022-10-23 stsp if (imsg->fd == -1)
1184 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1186 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1187 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
1188 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1189 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
1191 1a52c9bf 2022-12-30 stsp if (client->pack_pipe != -1)
1192 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1194 1a52c9bf 2022-12-30 stsp client->pack_pipe = imsg->fd;
1195 13b2bc37 2022-10-23 stsp return NULL;
1198 13b2bc37 2022-10-23 stsp static const struct got_error *
1199 1a52c9bf 2022-12-30 stsp receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1201 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1202 13b2bc37 2022-10-23 stsp struct gotd_imsg_packidx_file ireq;
1203 13b2bc37 2022-10-23 stsp size_t datalen;
1205 13b2bc37 2022-10-23 stsp log_debug("receving pack index output file");
1207 13b2bc37 2022-10-23 stsp if (imsg->fd == -1)
1208 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1210 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1211 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
1212 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1213 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
1215 1a52c9bf 2022-12-30 stsp if (client->packidx_fd != -1)
1216 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1218 1a52c9bf 2022-12-30 stsp client->packidx_fd = imsg->fd;
1219 13b2bc37 2022-10-23 stsp return NULL;
1222 13b2bc37 2022-10-23 stsp static void
1223 ae7c1b78 2023-01-10 stsp repo_write_dispatch_session(int fd, short event, void *arg)
1225 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1226 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
1227 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
1228 13b2bc37 2022-10-23 stsp struct imsg imsg;
1229 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1230 13b2bc37 2022-10-23 stsp ssize_t n;
1231 13b2bc37 2022-10-23 stsp int shut = 0;
1233 13b2bc37 2022-10-23 stsp if (event & EV_READ) {
1234 13b2bc37 2022-10-23 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1235 13b2bc37 2022-10-23 stsp fatal("imsg_read error");
1236 13b2bc37 2022-10-23 stsp if (n == 0) /* Connection closed. */
1240 13b2bc37 2022-10-23 stsp if (event & EV_WRITE) {
1241 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
1242 13b2bc37 2022-10-23 stsp if (n == -1 && errno != EAGAIN)
1243 13b2bc37 2022-10-23 stsp fatal("msgbuf_write");
1244 13b2bc37 2022-10-23 stsp if (n == 0) /* Connection closed. */
1248 13b2bc37 2022-10-23 stsp for (;;) {
1249 13b2bc37 2022-10-23 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1250 13b2bc37 2022-10-23 stsp fatal("%s: imsg_get error", __func__);
1251 13b2bc37 2022-10-23 stsp if (n == 0) /* No more messages. */
1254 1a52c9bf 2022-12-30 stsp if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1255 1a52c9bf 2022-12-30 stsp client->id == 0) {
1256 1a52c9bf 2022-12-30 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1260 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
1261 13b2bc37 2022-10-23 stsp case GOTD_IMSG_LIST_REFS_INTERNAL:
1262 1a52c9bf 2022-12-30 stsp err = list_refs(&imsg);
1264 13b2bc37 2022-10-23 stsp log_warnx("%s: ls-refs: %s", repo_write.title,
1265 13b2bc37 2022-10-23 stsp err->msg);
1267 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE:
1268 1a52c9bf 2022-12-30 stsp err = recv_ref_update(&imsg);
1270 13b2bc37 2022-10-23 stsp log_warnx("%s: ref-update: %s",
1271 13b2bc37 2022-10-23 stsp repo_write.title, err->msg);
1273 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_PIPE:
1274 1a52c9bf 2022-12-30 stsp err = receive_pack_pipe(&imsg, iev);
1275 13b2bc37 2022-10-23 stsp if (err) {
1276 13b2bc37 2022-10-23 stsp log_warnx("%s: receiving pack pipe: %s",
1277 13b2bc37 2022-10-23 stsp repo_write.title, err->msg);
1281 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKIDX_FILE:
1282 1a52c9bf 2022-12-30 stsp err = receive_pack_idx(&imsg, iev);
1283 13b2bc37 2022-10-23 stsp if (err) {
1284 13b2bc37 2022-10-23 stsp log_warnx("%s: receiving pack index: %s",
1285 13b2bc37 2022-10-23 stsp repo_write.title, err->msg);
1289 13b2bc37 2022-10-23 stsp case GOTD_IMSG_RECV_PACKFILE:
1290 1a52c9bf 2022-12-30 stsp err = recv_packfile(&imsg);
1291 13b2bc37 2022-10-23 stsp if (err) {
1292 13b2bc37 2022-10-23 stsp log_warnx("%s: receive packfile: %s",
1293 13b2bc37 2022-10-23 stsp repo_write.title, err->msg);
1296 1a52c9bf 2022-12-30 stsp err = verify_packfile();
1297 13b2bc37 2022-10-23 stsp if (err) {
1298 13b2bc37 2022-10-23 stsp log_warnx("%s: verify packfile: %s",
1299 13b2bc37 2022-10-23 stsp repo_write.title, err->msg);
1302 1a52c9bf 2022-12-30 stsp err = install_packfile(iev);
1303 13b2bc37 2022-10-23 stsp if (err) {
1304 13b2bc37 2022-10-23 stsp log_warnx("%s: install packfile: %s",
1305 13b2bc37 2022-10-23 stsp repo_write.title, err->msg);
1308 1a52c9bf 2022-12-30 stsp err = update_refs(iev);
1309 13b2bc37 2022-10-23 stsp if (err) {
1310 13b2bc37 2022-10-23 stsp log_warnx("%s: update refs: %s",
1311 13b2bc37 2022-10-23 stsp repo_write.title, err->msg);
1314 13b2bc37 2022-10-23 stsp case GOTD_IMSG_DISCONNECT:
1315 13b2bc37 2022-10-23 stsp err = recv_disconnect(&imsg);
1317 13b2bc37 2022-10-23 stsp log_warnx("%s: disconnect: %s",
1318 13b2bc37 2022-10-23 stsp repo_write.title, err->msg);
1322 ae7c1b78 2023-01-10 stsp log_debug("%s: unexpected imsg %d", repo_write.title,
1323 ae7c1b78 2023-01-10 stsp imsg.hdr.type);
1327 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
1330 ae7c1b78 2023-01-10 stsp if (!shut && check_cancelled(NULL) == NULL) {
1331 ae7c1b78 2023-01-10 stsp if (err &&
1332 ae7c1b78 2023-01-10 stsp gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1333 ae7c1b78 2023-01-10 stsp client->id, err) == -1) {
1334 ae7c1b78 2023-01-10 stsp log_warnx("could not send error to parent: %s",
1335 ae7c1b78 2023-01-10 stsp err->msg);
1337 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
1339 ae7c1b78 2023-01-10 stsp /* This pipe is dead. Remove its event handler */
1340 ae7c1b78 2023-01-10 stsp event_del(&iev->ev);
1341 ae7c1b78 2023-01-10 stsp event_loopexit(NULL);
1345 ae7c1b78 2023-01-10 stsp static const struct got_error *
1346 ae7c1b78 2023-01-10 stsp recv_connect(struct imsg *imsg)
1348 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = &repo_write.session_iev;
1349 ae7c1b78 2023-01-10 stsp size_t datalen;
1351 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1352 ae7c1b78 2023-01-10 stsp if (datalen != 0)
1353 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1354 ae7c1b78 2023-01-10 stsp if (imsg->fd == -1)
1355 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1357 ae7c1b78 2023-01-10 stsp if (repo_write.session_fd != -1)
1358 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1360 ae7c1b78 2023-01-10 stsp repo_write.session_fd = imsg->fd;
1362 ae7c1b78 2023-01-10 stsp imsg_init(&iev->ibuf, repo_write.session_fd);
1363 ae7c1b78 2023-01-10 stsp iev->handler = repo_write_dispatch_session;
1364 ae7c1b78 2023-01-10 stsp iev->events = EV_READ;
1365 ae7c1b78 2023-01-10 stsp iev->handler_arg = NULL;
1366 ae7c1b78 2023-01-10 stsp event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1367 ae7c1b78 2023-01-10 stsp repo_write_dispatch_session, iev);
1368 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
1370 ae7c1b78 2023-01-10 stsp return NULL;
1373 ae7c1b78 2023-01-10 stsp static void
1374 ae7c1b78 2023-01-10 stsp repo_write_dispatch(int fd, short event, void *arg)
1376 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
1377 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = arg;
1378 ae7c1b78 2023-01-10 stsp struct imsgbuf *ibuf = &iev->ibuf;
1379 ae7c1b78 2023-01-10 stsp struct imsg imsg;
1380 ae7c1b78 2023-01-10 stsp ssize_t n;
1381 ae7c1b78 2023-01-10 stsp int shut = 0;
1382 ae7c1b78 2023-01-10 stsp struct repo_write_client *client = &repo_write_client;
1384 ae7c1b78 2023-01-10 stsp if (event & EV_READ) {
1385 ae7c1b78 2023-01-10 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1386 ae7c1b78 2023-01-10 stsp fatal("imsg_read error");
1387 ae7c1b78 2023-01-10 stsp if (n == 0) /* Connection closed. */
1391 ae7c1b78 2023-01-10 stsp if (event & EV_WRITE) {
1392 ae7c1b78 2023-01-10 stsp n = msgbuf_write(&ibuf->w);
1393 ae7c1b78 2023-01-10 stsp if (n == -1 && errno != EAGAIN)
1394 ae7c1b78 2023-01-10 stsp fatal("msgbuf_write");
1395 ae7c1b78 2023-01-10 stsp if (n == 0) /* Connection closed. */
1399 ae7c1b78 2023-01-10 stsp while (err == NULL && check_cancelled(NULL) == NULL) {
1400 ae7c1b78 2023-01-10 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1401 ae7c1b78 2023-01-10 stsp fatal("%s: imsg_get", __func__);
1402 ae7c1b78 2023-01-10 stsp if (n == 0) /* No more messages. */
1405 ae7c1b78 2023-01-10 stsp switch (imsg.hdr.type) {
1406 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CONNECT_REPO_CHILD:
1407 ae7c1b78 2023-01-10 stsp err = recv_connect(&imsg);
1410 13b2bc37 2022-10-23 stsp log_debug("%s: unexpected imsg %d", repo_write.title,
1411 13b2bc37 2022-10-23 stsp imsg.hdr.type);
1415 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1418 13b2bc37 2022-10-23 stsp if (!shut && check_cancelled(NULL) == NULL) {
1419 13b2bc37 2022-10-23 stsp if (err &&
1420 13b2bc37 2022-10-23 stsp gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1421 1a52c9bf 2022-12-30 stsp client->id, err) == -1) {
1422 13b2bc37 2022-10-23 stsp log_warnx("could not send error to parent: %s",
1423 13b2bc37 2022-10-23 stsp err->msg);
1425 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
1427 13b2bc37 2022-10-23 stsp /* This pipe is dead. Remove its event handler */
1428 13b2bc37 2022-10-23 stsp event_del(&iev->ev);
1429 13b2bc37 2022-10-23 stsp event_loopexit(NULL);
1434 eec68231 2022-12-14 stsp repo_write_main(const char *title, const char *repo_path,
1435 eec68231 2022-12-14 stsp int *pack_fds, int *temp_fds)
1437 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1438 13b2bc37 2022-10-23 stsp struct gotd_imsgev iev;
1440 13b2bc37 2022-10-23 stsp repo_write.title = title;
1441 13b2bc37 2022-10-23 stsp repo_write.pid = getpid();
1442 13b2bc37 2022-10-23 stsp repo_write.pack_fds = pack_fds;
1443 13b2bc37 2022-10-23 stsp repo_write.temp_fds = temp_fds;
1444 ae7c1b78 2023-01-10 stsp repo_write.session_fd = -1;
1445 ae7c1b78 2023-01-10 stsp repo_write.session_iev.ibuf.fd = -1;
1447 1a52c9bf 2022-12-30 stsp STAILQ_INIT(&repo_write_client.ref_updates);
1449 eec68231 2022-12-14 stsp err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1451 13b2bc37 2022-10-23 stsp goto done;
1452 13b2bc37 2022-10-23 stsp if (!got_repo_is_bare(repo_write.repo)) {
1453 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1454 13b2bc37 2022-10-23 stsp "bare git repository required");
1455 13b2bc37 2022-10-23 stsp goto done;
1458 13b2bc37 2022-10-23 stsp got_repo_temp_fds_set(repo_write.repo, temp_fds);
1460 13b2bc37 2022-10-23 stsp signal(SIGINT, catch_sigint);
1461 13b2bc37 2022-10-23 stsp signal(SIGTERM, catch_sigterm);
1462 13b2bc37 2022-10-23 stsp signal(SIGPIPE, SIG_IGN);
1463 13b2bc37 2022-10-23 stsp signal(SIGHUP, SIG_IGN);
1465 8c6fc146 2022-11-17 stsp imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1466 13b2bc37 2022-10-23 stsp iev.handler = repo_write_dispatch;
1467 13b2bc37 2022-10-23 stsp iev.events = EV_READ;
1468 13b2bc37 2022-10-23 stsp iev.handler_arg = NULL;
1469 13b2bc37 2022-10-23 stsp event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1470 b50a2b46 2022-12-29 stsp if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1471 b50a2b46 2022-12-29 stsp PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1472 b50a2b46 2022-12-29 stsp err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1473 13b2bc37 2022-10-23 stsp goto done;
1476 13b2bc37 2022-10-23 stsp event_dispatch();
1479 13b2bc37 2022-10-23 stsp log_warnx("%s: %s", title, err->msg);
1480 13b2bc37 2022-10-23 stsp repo_write_shutdown();
1484 13b2bc37 2022-10-23 stsp repo_write_shutdown(void)
1486 13b2bc37 2022-10-23 stsp log_debug("%s: shutting down", repo_write.title);
1487 13b2bc37 2022-10-23 stsp if (repo_write.repo)
1488 13b2bc37 2022-10-23 stsp got_repo_close(repo_write.repo);
1489 13b2bc37 2022-10-23 stsp got_repo_pack_fds_close(repo_write.pack_fds);
1490 8193d041 2022-10-30 stsp got_repo_temp_fds_close(repo_write.temp_fds);
1491 ae7c1b78 2023-01-10 stsp if (repo_write.session_fd != -1)
1492 ae7c1b78 2023-01-10 stsp close(repo_write.session_fd);