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 5822e79e 2023-02-23 op #include <sha2.h>
34 13b2bc37 2022-10-23 stsp #include <unistd.h>
35 13b2bc37 2022-10-23 stsp #include <zlib.h>
37 13b2bc37 2022-10-23 stsp #include "buf.h"
39 13b2bc37 2022-10-23 stsp #include "got_error.h"
40 13b2bc37 2022-10-23 stsp #include "got_repository.h"
41 13b2bc37 2022-10-23 stsp #include "got_object.h"
42 13b2bc37 2022-10-23 stsp #include "got_reference.h"
43 13b2bc37 2022-10-23 stsp #include "got_path.h"
45 13b2bc37 2022-10-23 stsp #include "got_lib_delta.h"
46 13b2bc37 2022-10-23 stsp #include "got_lib_delta_cache.h"
47 13b2bc37 2022-10-23 stsp #include "got_lib_object.h"
48 13b2bc37 2022-10-23 stsp #include "got_lib_object_cache.h"
49 13b2bc37 2022-10-23 stsp #include "got_lib_ratelimit.h"
50 13b2bc37 2022-10-23 stsp #include "got_lib_pack.h"
51 13b2bc37 2022-10-23 stsp #include "got_lib_pack_index.h"
52 13b2bc37 2022-10-23 stsp #include "got_lib_repository.h"
53 13b2bc37 2022-10-23 stsp #include "got_lib_poll.h"
55 53bf0b54 2023-02-23 op #include "got_lib_hash.h" /* XXX temp include for debugging */
57 13b2bc37 2022-10-23 stsp #include "log.h"
58 13b2bc37 2022-10-23 stsp #include "gotd.h"
59 13b2bc37 2022-10-23 stsp #include "repo_write.h"
61 13b2bc37 2022-10-23 stsp #ifndef nitems
62 13b2bc37 2022-10-23 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 13b2bc37 2022-10-23 stsp static struct repo_write {
67 13b2bc37 2022-10-23 stsp const char *title;
68 13b2bc37 2022-10-23 stsp struct got_repository *repo;
69 13b2bc37 2022-10-23 stsp int *pack_fds;
70 13b2bc37 2022-10-23 stsp int *temp_fds;
71 ae7c1b78 2023-01-10 stsp int session_fd;
72 ae7c1b78 2023-01-10 stsp struct gotd_imsgev session_iev;
73 13b2bc37 2022-10-23 stsp } repo_write;
75 13b2bc37 2022-10-23 stsp struct gotd_ref_update {
76 13b2bc37 2022-10-23 stsp STAILQ_ENTRY(gotd_ref_update) entry;
77 13b2bc37 2022-10-23 stsp struct got_reference *ref;
78 13b2bc37 2022-10-23 stsp int ref_is_new;
79 9a8e357c 2023-01-28 op int delete_ref;
80 13b2bc37 2022-10-23 stsp struct got_object_id old_id;
81 13b2bc37 2022-10-23 stsp struct got_object_id new_id;
83 13b2bc37 2022-10-23 stsp STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
85 1a52c9bf 2022-12-30 stsp static struct repo_write_client {
86 13b2bc37 2022-10-23 stsp uint32_t id;
88 7fec5f4a 2022-10-28 stsp int pack_pipe;
89 13b2bc37 2022-10-23 stsp struct got_pack pack;
90 13b2bc37 2022-10-23 stsp uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
91 13b2bc37 2022-10-23 stsp int packidx_fd;
92 13b2bc37 2022-10-23 stsp struct gotd_ref_updates ref_updates;
93 13b2bc37 2022-10-23 stsp int nref_updates;
95 0ff2c315 2023-01-18 stsp int nref_new;
96 1a52c9bf 2022-12-30 stsp } repo_write_client;
98 13b2bc37 2022-10-23 stsp static volatile sig_atomic_t sigint_received;
99 13b2bc37 2022-10-23 stsp static volatile sig_atomic_t sigterm_received;
101 13b2bc37 2022-10-23 stsp static void
102 13b2bc37 2022-10-23 stsp catch_sigint(int signo)
104 13b2bc37 2022-10-23 stsp sigint_received = 1;
107 13b2bc37 2022-10-23 stsp static void
108 13b2bc37 2022-10-23 stsp catch_sigterm(int signo)
110 13b2bc37 2022-10-23 stsp sigterm_received = 1;
113 13b2bc37 2022-10-23 stsp static const struct got_error *
114 13b2bc37 2022-10-23 stsp check_cancelled(void *arg)
116 13b2bc37 2022-10-23 stsp if (sigint_received || sigterm_received)
117 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_CANCELLED);
119 13b2bc37 2022-10-23 stsp return NULL;
122 13b2bc37 2022-10-23 stsp static const struct got_error *
123 13b2bc37 2022-10-23 stsp send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
124 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf)
126 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
127 13b2bc37 2022-10-23 stsp struct got_tag_object *tag;
128 13b2bc37 2022-10-23 stsp size_t namelen, len;
129 13b2bc37 2022-10-23 stsp char *peeled_refname = NULL;
130 13b2bc37 2022-10-23 stsp struct got_object_id *id;
131 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
133 13b2bc37 2022-10-23 stsp err = got_object_tag_open(&tag, repo_write.repo, obj);
135 13b2bc37 2022-10-23 stsp return err;
137 13b2bc37 2022-10-23 stsp if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
138 13b2bc37 2022-10-23 stsp err = got_error_from_errno("asprintf");
142 13b2bc37 2022-10-23 stsp id = got_object_tag_get_object_id(tag);
143 13b2bc37 2022-10-23 stsp namelen = strlen(peeled_refname);
145 13b2bc37 2022-10-23 stsp len = sizeof(struct gotd_imsg_ref) + namelen;
146 13b2bc37 2022-10-23 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
147 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_NO_SPACE);
151 13b2bc37 2022-10-23 stsp wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
152 13b2bc37 2022-10-23 stsp repo_write.pid, len);
153 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
154 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create REF");
158 13b2bc37 2022-10-23 stsp /* Keep in sync with struct gotd_imsg_ref definition. */
159 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -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, &namelen, sizeof(namelen)) == -1) {
164 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add REF");
167 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
168 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add REF");
172 13b2bc37 2022-10-23 stsp wbuf->fd = -1;
173 13b2bc37 2022-10-23 stsp imsg_close(ibuf, wbuf);
175 13b2bc37 2022-10-23 stsp got_object_tag_close(tag);
176 13b2bc37 2022-10-23 stsp return err;
179 13b2bc37 2022-10-23 stsp static const struct got_error *
180 13b2bc37 2022-10-23 stsp send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
182 13b2bc37 2022-10-23 stsp const struct got_error *err;
183 13b2bc37 2022-10-23 stsp const char *refname = got_ref_get_name(ref);
184 13b2bc37 2022-10-23 stsp size_t namelen;
185 13b2bc37 2022-10-23 stsp struct got_object_id *id = NULL;
186 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
187 13b2bc37 2022-10-23 stsp size_t len;
188 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
190 13b2bc37 2022-10-23 stsp namelen = strlen(refname);
192 13b2bc37 2022-10-23 stsp len = sizeof(struct gotd_imsg_ref) + namelen;
193 13b2bc37 2022-10-23 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
194 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
196 13b2bc37 2022-10-23 stsp err = got_ref_resolve(&id, repo_write.repo, ref);
198 13b2bc37 2022-10-23 stsp return err;
200 13b2bc37 2022-10-23 stsp wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
201 13b2bc37 2022-10-23 stsp repo_write.pid, len);
202 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
203 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create REF");
207 13b2bc37 2022-10-23 stsp /* Keep in sync with struct gotd_imsg_ref definition. */
208 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
209 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF");
210 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
211 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF");
212 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, refname, namelen) == -1)
213 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF");
215 13b2bc37 2022-10-23 stsp wbuf->fd = -1;
216 13b2bc37 2022-10-23 stsp imsg_close(ibuf, wbuf);
218 13b2bc37 2022-10-23 stsp err = got_object_open(&obj, repo_write.repo, id);
221 13b2bc37 2022-10-23 stsp if (obj->type == GOT_OBJ_TYPE_TAG)
222 13b2bc37 2022-10-23 stsp err = send_peeled_tag_ref(ref, obj, ibuf);
225 13b2bc37 2022-10-23 stsp got_object_close(obj);
227 13b2bc37 2022-10-23 stsp return err;
230 13b2bc37 2022-10-23 stsp static const struct got_error *
231 1a52c9bf 2022-12-30 stsp list_refs(struct imsg *imsg)
233 13b2bc37 2022-10-23 stsp const struct got_error *err;
234 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
235 13b2bc37 2022-10-23 stsp struct got_reflist_head refs;
236 13b2bc37 2022-10-23 stsp struct got_reflist_entry *re;
237 13b2bc37 2022-10-23 stsp struct gotd_imsg_list_refs_internal ireq;
238 13b2bc37 2022-10-23 stsp size_t datalen;
239 13b2bc37 2022-10-23 stsp struct gotd_imsg_reflist irefs;
240 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
241 13b2bc37 2022-10-23 stsp int client_fd = imsg->fd;
243 13b2bc37 2022-10-23 stsp TAILQ_INIT(&refs);
245 13b2bc37 2022-10-23 stsp if (client_fd == -1)
246 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
248 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
249 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
250 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
251 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
253 1a52c9bf 2022-12-30 stsp if (ireq.client_id == 0)
254 1a52c9bf 2022-12-30 stsp return got_error(GOT_ERR_CLIENT_ID);
255 1a52c9bf 2022-12-30 stsp if (client->id != 0) {
256 1a52c9bf 2022-12-30 stsp return got_error_msg(GOT_ERR_CLIENT_ID,
257 1a52c9bf 2022-12-30 stsp "duplicate list-refs request");
259 1a52c9bf 2022-12-30 stsp client->id = ireq.client_id;
260 1a52c9bf 2022-12-30 stsp client->fd = client_fd;
261 1a52c9bf 2022-12-30 stsp client->nref_updates = 0;
262 9a8e357c 2023-01-28 op client->nref_del = 0;
263 0ff2c315 2023-01-18 stsp client->nref_new = 0;
265 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, client_fd);
267 13b2bc37 2022-10-23 stsp err = got_ref_list(&refs, repo_write.repo, "",
268 13b2bc37 2022-10-23 stsp got_ref_cmp_by_name, NULL);
270 13b2bc37 2022-10-23 stsp return err;
272 13b2bc37 2022-10-23 stsp memset(&irefs, 0, sizeof(irefs));
273 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(re, &refs, entry) {
274 13b2bc37 2022-10-23 stsp struct got_object_id *id;
275 13b2bc37 2022-10-23 stsp int obj_type;
277 13b2bc37 2022-10-23 stsp if (got_ref_is_symbolic(re->ref))
280 13b2bc37 2022-10-23 stsp irefs.nrefs++;
282 13b2bc37 2022-10-23 stsp /* Account for a peeled tag refs. */
283 13b2bc37 2022-10-23 stsp err = got_ref_resolve(&id, repo_write.repo, re->ref);
286 e26970cc 2023-01-10 op err = got_object_get_type(&obj_type, repo_write.repo, id);
290 13b2bc37 2022-10-23 stsp if (obj_type == GOT_OBJ_TYPE_TAG)
291 13b2bc37 2022-10-23 stsp irefs.nrefs++;
294 13b2bc37 2022-10-23 stsp if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
295 13b2bc37 2022-10-23 stsp repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
296 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_compose REFLIST");
300 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(re, &refs, entry) {
301 13b2bc37 2022-10-23 stsp if (got_ref_is_symbolic(re->ref))
303 13b2bc37 2022-10-23 stsp err = send_ref(re->ref, &ibuf);
308 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(&ibuf);
310 13b2bc37 2022-10-23 stsp got_ref_list_free(&refs);
311 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
312 13b2bc37 2022-10-23 stsp return err;
315 13b2bc37 2022-10-23 stsp static const struct got_error *
316 13b2bc37 2022-10-23 stsp protect_ref_namespace(struct got_reference *ref, const char *namespace)
318 13b2bc37 2022-10-23 stsp size_t len = strlen(namespace);
320 13b2bc37 2022-10-23 stsp if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
321 13b2bc37 2022-10-23 stsp namespace[len -1] != '/') {
322 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_REF_NAME,
323 13b2bc37 2022-10-23 stsp "reference namespace '%s'", namespace);
326 13b2bc37 2022-10-23 stsp if (strncmp(namespace, got_ref_get_name(ref), len) == 0)
327 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
329 13b2bc37 2022-10-23 stsp return NULL;
332 13b2bc37 2022-10-23 stsp static const struct got_error *
333 1a52c9bf 2022-12-30 stsp recv_ref_update(struct imsg *imsg)
335 9a8e357c 2023-01-28 op static const char zero_id[SHA1_DIGEST_LENGTH];
336 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
337 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
338 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update iref;
339 13b2bc37 2022-10-23 stsp size_t datalen;
340 13b2bc37 2022-10-23 stsp char *refname = NULL;
341 13b2bc37 2022-10-23 stsp struct got_reference *ref = NULL;
342 13b2bc37 2022-10-23 stsp struct got_object_id *id = NULL;
343 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
344 13b2bc37 2022-10-23 stsp struct gotd_ref_update *ref_update = NULL;
346 13b2bc37 2022-10-23 stsp log_debug("ref-update received");
348 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
349 13b2bc37 2022-10-23 stsp if (datalen < sizeof(iref))
350 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
351 13b2bc37 2022-10-23 stsp memcpy(&iref, imsg->data, sizeof(iref));
352 13b2bc37 2022-10-23 stsp if (datalen != sizeof(iref) + iref.name_len)
353 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
355 1a52c9bf 2022-12-30 stsp imsg_init(&ibuf, client->fd);
357 00b3e9ae 2023-01-11 op refname = strndup(imsg->data + sizeof(iref), iref.name_len);
358 13b2bc37 2022-10-23 stsp if (refname == NULL)
359 00b3e9ae 2023-01-11 op return got_error_from_errno("strndup");
361 13b2bc37 2022-10-23 stsp ref_update = calloc(1, sizeof(*ref_update));
362 13b2bc37 2022-10-23 stsp if (ref_update == NULL) {
363 13b2bc37 2022-10-23 stsp err = got_error_from_errno("malloc");
367 13b2bc37 2022-10-23 stsp memcpy(ref_update->old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
368 13b2bc37 2022-10-23 stsp memcpy(ref_update->new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
370 13b2bc37 2022-10-23 stsp err = got_ref_open(&ref, repo_write.repo, refname, 0);
372 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_NOT_REF)
374 13b2bc37 2022-10-23 stsp err = got_ref_alloc(&ref, refname, &ref_update->new_id);
377 13b2bc37 2022-10-23 stsp ref_update->ref_is_new = 1;
378 0ff2c315 2023-01-18 stsp client->nref_new++;
380 13b2bc37 2022-10-23 stsp if (got_ref_is_symbolic(ref)) {
381 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
382 13b2bc37 2022-10-23 stsp "'%s' is a symbolic reference and cannot "
383 13b2bc37 2022-10-23 stsp "be updated", got_ref_get_name(ref));
386 13b2bc37 2022-10-23 stsp if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
387 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
388 13b2bc37 2022-10-23 stsp "%s: does not begin with 'refs/'",
389 13b2bc37 2022-10-23 stsp got_ref_get_name(ref));
393 13b2bc37 2022-10-23 stsp err = protect_ref_namespace(ref, "refs/got/");
396 13b2bc37 2022-10-23 stsp err = protect_ref_namespace(ref, "refs/remotes/");
400 13b2bc37 2022-10-23 stsp if (!ref_update->ref_is_new) {
402 13b2bc37 2022-10-23 stsp * Ensure the client's idea of this update is still valid.
403 13b2bc37 2022-10-23 stsp * At this point we can only return an error, to prevent
404 13b2bc37 2022-10-23 stsp * the client from uploading a pack file which will likely
405 13b2bc37 2022-10-23 stsp * have to be discarded.
407 13b2bc37 2022-10-23 stsp err = got_ref_resolve(&id, repo_write.repo, ref);
411 13b2bc37 2022-10-23 stsp if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
412 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
413 13b2bc37 2022-10-23 stsp "%s has been modified by someone else "
414 13b2bc37 2022-10-23 stsp "while transaction was in progress",
415 13b2bc37 2022-10-23 stsp got_ref_get_name(ref));
420 13b2bc37 2022-10-23 stsp gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
421 13b2bc37 2022-10-23 stsp repo_write.pid);
423 13b2bc37 2022-10-23 stsp ref_update->ref = ref;
424 9a8e357c 2023-01-28 op if (memcmp(ref_update->new_id.sha1, zero_id, sizeof(zero_id)) == 0) {
425 9a8e357c 2023-01-28 op ref_update->delete_ref = 1;
426 9a8e357c 2023-01-28 op client->nref_del++;
428 1a52c9bf 2022-12-30 stsp STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
429 1a52c9bf 2022-12-30 stsp client->nref_updates++;
430 13b2bc37 2022-10-23 stsp ref = NULL;
431 13b2bc37 2022-10-23 stsp ref_update = NULL;
434 13b2bc37 2022-10-23 stsp got_ref_close(ref);
435 13b2bc37 2022-10-23 stsp free(ref_update);
436 13b2bc37 2022-10-23 stsp free(refname);
438 13b2bc37 2022-10-23 stsp return err;
441 13b2bc37 2022-10-23 stsp static const struct got_error *
442 13b2bc37 2022-10-23 stsp pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
443 13b2bc37 2022-10-23 stsp uint32_t nobj_loose, uint32_t nobj_resolved)
445 13b2bc37 2022-10-23 stsp int p_indexed = 0, p_resolved = 0;
446 13b2bc37 2022-10-23 stsp int nobj_delta = nobj_total - nobj_loose;
448 13b2bc37 2022-10-23 stsp if (nobj_total > 0)
449 13b2bc37 2022-10-23 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
451 13b2bc37 2022-10-23 stsp if (nobj_delta > 0)
452 13b2bc37 2022-10-23 stsp p_resolved = (nobj_resolved * 100) / nobj_delta;
454 13b2bc37 2022-10-23 stsp if (p_resolved > 0) {
455 13b2bc37 2022-10-23 stsp log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
456 13b2bc37 2022-10-23 stsp nobj_total, p_indexed, nobj_delta, p_resolved);
458 13b2bc37 2022-10-23 stsp log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
460 13b2bc37 2022-10-23 stsp return NULL;
463 13b2bc37 2022-10-23 stsp static const struct got_error *
464 13b2bc37 2022-10-23 stsp read_more_pack_stream(int infd, BUF *buf, size_t minsize)
466 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
467 13b2bc37 2022-10-23 stsp uint8_t readahead[65536];
468 13b2bc37 2022-10-23 stsp size_t have, newlen;
470 13b2bc37 2022-10-23 stsp err = got_poll_read_full(infd, &have,
471 13b2bc37 2022-10-23 stsp readahead, sizeof(readahead), minsize);
473 13b2bc37 2022-10-23 stsp return err;
475 13b2bc37 2022-10-23 stsp err = buf_append(&newlen, buf, readahead, have);
477 13b2bc37 2022-10-23 stsp return err;
481 13b2bc37 2022-10-23 stsp static const struct got_error *
482 13b2bc37 2022-10-23 stsp copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
483 13b2bc37 2022-10-23 stsp off_t *outsize, BUF *buf, size_t *buf_pos, SHA1_CTX *ctx)
485 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
486 13b2bc37 2022-10-23 stsp uint8_t t = 0;
487 13b2bc37 2022-10-23 stsp uint64_t s = 0;
488 13b2bc37 2022-10-23 stsp uint8_t sizebuf[8];
489 13b2bc37 2022-10-23 stsp size_t i = 0;
490 13b2bc37 2022-10-23 stsp off_t obj_offset = *outsize;
493 13b2bc37 2022-10-23 stsp /* We do not support size values which don't fit in 64 bit. */
495 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
496 22ec3416 2022-10-25 op "packfile offset %lld", (long long)obj_offset);
498 13b2bc37 2022-10-23 stsp if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
499 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
500 13b2bc37 2022-10-23 stsp sizeof(sizebuf[0]));
502 13b2bc37 2022-10-23 stsp return err;
505 13b2bc37 2022-10-23 stsp sizebuf[i] = buf_getc(buf, *buf_pos);
506 13b2bc37 2022-10-23 stsp *buf_pos += sizeof(sizebuf[i]);
508 13b2bc37 2022-10-23 stsp if (i == 0) {
509 13b2bc37 2022-10-23 stsp t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
510 13b2bc37 2022-10-23 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
511 13b2bc37 2022-10-23 stsp s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
513 13b2bc37 2022-10-23 stsp size_t shift = 4 + 7 * (i - 1);
514 13b2bc37 2022-10-23 stsp s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
518 13b2bc37 2022-10-23 stsp } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
520 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, sizebuf, i, ctx);
522 13b2bc37 2022-10-23 stsp return err;
523 13b2bc37 2022-10-23 stsp *outsize += i;
527 13b2bc37 2022-10-23 stsp return NULL;
530 13b2bc37 2022-10-23 stsp static const struct got_error *
531 13b2bc37 2022-10-23 stsp copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
532 13b2bc37 2022-10-23 stsp SHA1_CTX *ctx)
534 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
535 13b2bc37 2022-10-23 stsp size_t remain = buf_len(buf) - *buf_pos;
537 13b2bc37 2022-10-23 stsp if (remain < SHA1_DIGEST_LENGTH) {
538 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
539 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH - remain);
541 13b2bc37 2022-10-23 stsp return err;
544 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
545 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH, ctx);
547 13b2bc37 2022-10-23 stsp return err;
549 13b2bc37 2022-10-23 stsp *buf_pos += SHA1_DIGEST_LENGTH;
550 13b2bc37 2022-10-23 stsp return NULL;
553 13b2bc37 2022-10-23 stsp static const struct got_error *
554 13b2bc37 2022-10-23 stsp copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
555 13b2bc37 2022-10-23 stsp SHA1_CTX *ctx)
557 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
558 13b2bc37 2022-10-23 stsp uint64_t o = 0;
559 13b2bc37 2022-10-23 stsp uint8_t offbuf[8];
560 13b2bc37 2022-10-23 stsp size_t i = 0;
561 13b2bc37 2022-10-23 stsp off_t obj_offset = *outsize;
564 13b2bc37 2022-10-23 stsp /* We do not support offset values which don't fit in 64 bit. */
566 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
567 22ec3416 2022-10-25 op "packfile offset %lld", (long long)obj_offset);
569 13b2bc37 2022-10-23 stsp if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
570 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
571 13b2bc37 2022-10-23 stsp sizeof(offbuf[0]));
573 13b2bc37 2022-10-23 stsp return err;
576 13b2bc37 2022-10-23 stsp offbuf[i] = buf_getc(buf, *buf_pos);
577 13b2bc37 2022-10-23 stsp *buf_pos += sizeof(offbuf[i]);
579 13b2bc37 2022-10-23 stsp if (i == 0)
580 13b2bc37 2022-10-23 stsp o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
584 13b2bc37 2022-10-23 stsp o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
587 13b2bc37 2022-10-23 stsp } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
589 13b2bc37 2022-10-23 stsp if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
590 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PACK_OFFSET);
592 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, offbuf, i, ctx);
594 13b2bc37 2022-10-23 stsp return err;
596 13b2bc37 2022-10-23 stsp *outsize += i;
597 13b2bc37 2022-10-23 stsp return NULL;
600 13b2bc37 2022-10-23 stsp static const struct got_error *
601 13b2bc37 2022-10-23 stsp copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
602 13b2bc37 2022-10-23 stsp SHA1_CTX *ctx)
604 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
605 13b2bc37 2022-10-23 stsp z_stream z;
607 13b2bc37 2022-10-23 stsp char voidbuf[1024];
608 13b2bc37 2022-10-23 stsp size_t consumed_total = 0;
609 13b2bc37 2022-10-23 stsp off_t zstream_offset = *outsize;
611 13b2bc37 2022-10-23 stsp memset(&z, 0, sizeof(z));
613 13b2bc37 2022-10-23 stsp z.zalloc = Z_NULL;
614 13b2bc37 2022-10-23 stsp z.zfree = Z_NULL;
615 13b2bc37 2022-10-23 stsp zret = inflateInit(&z);
616 13b2bc37 2022-10-23 stsp if (zret != Z_OK) {
617 13b2bc37 2022-10-23 stsp if (zret == Z_ERRNO)
618 13b2bc37 2022-10-23 stsp return got_error_from_errno("inflateInit");
619 13b2bc37 2022-10-23 stsp if (zret == Z_MEM_ERROR) {
620 13b2bc37 2022-10-23 stsp errno = ENOMEM;
621 13b2bc37 2022-10-23 stsp return got_error_from_errno("inflateInit");
623 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_DECOMPRESSION,
624 13b2bc37 2022-10-23 stsp "inflateInit failed");
627 13b2bc37 2022-10-23 stsp while (zret != Z_STREAM_END) {
628 13b2bc37 2022-10-23 stsp size_t last_total_in, consumed;
631 13b2bc37 2022-10-23 stsp * Decompress into the void. Object data will be parsed
632 13b2bc37 2022-10-23 stsp * later, when the pack file is indexed. For now, we just
633 13b2bc37 2022-10-23 stsp * want to locate the end of the compressed stream.
635 13b2bc37 2022-10-23 stsp while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
636 13b2bc37 2022-10-23 stsp last_total_in = z.total_in;
637 13b2bc37 2022-10-23 stsp z.next_in = buf_get(buf) + *buf_pos;
638 13b2bc37 2022-10-23 stsp z.avail_in = buf_len(buf) - *buf_pos;
639 13b2bc37 2022-10-23 stsp z.next_out = voidbuf;
640 13b2bc37 2022-10-23 stsp z.avail_out = sizeof(voidbuf);
642 13b2bc37 2022-10-23 stsp zret = inflate(&z, Z_SYNC_FLUSH);
643 13b2bc37 2022-10-23 stsp if (zret != Z_OK && zret != Z_BUF_ERROR &&
644 13b2bc37 2022-10-23 stsp zret != Z_STREAM_END) {
645 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_DECOMPRESSION,
646 22ec3416 2022-10-25 op "packfile offset %lld",
647 22ec3416 2022-10-25 op (long long)zstream_offset);
650 13b2bc37 2022-10-23 stsp consumed = z.total_in - last_total_in;
652 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
653 13b2bc37 2022-10-23 stsp consumed, ctx);
657 13b2bc37 2022-10-23 stsp err = buf_discard(buf, *buf_pos + consumed);
660 13b2bc37 2022-10-23 stsp *buf_pos = 0;
662 13b2bc37 2022-10-23 stsp consumed_total += consumed;
665 13b2bc37 2022-10-23 stsp if (zret != Z_STREAM_END) {
666 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf, 1);
672 13b2bc37 2022-10-23 stsp if (err == NULL)
673 13b2bc37 2022-10-23 stsp *outsize += consumed_total;
675 13b2bc37 2022-10-23 stsp inflateEnd(&z);
676 13b2bc37 2022-10-23 stsp return err;
679 13b2bc37 2022-10-23 stsp static const struct got_error *
680 13b2bc37 2022-10-23 stsp validate_object_type(int obj_type)
682 13b2bc37 2022-10-23 stsp switch (obj_type) {
683 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_BLOB:
684 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_COMMIT:
685 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_TREE:
686 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_TAG:
687 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
688 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
689 13b2bc37 2022-10-23 stsp return NULL;
694 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_OBJ_TYPE);
697 13b2bc37 2022-10-23 stsp static const struct got_error *
698 0ff2c315 2023-01-18 stsp recv_packdata(off_t *outsize, uint32_t *nobj, uint8_t *sha1,
699 0ff2c315 2023-01-18 stsp int infd, int outfd)
701 13b2bc37 2022-10-23 stsp const struct got_error *err;
702 0ff2c315 2023-01-18 stsp struct repo_write_client *client = &repo_write_client;
703 13b2bc37 2022-10-23 stsp struct got_packfile_hdr hdr;
704 13b2bc37 2022-10-23 stsp size_t have;
705 0ff2c315 2023-01-18 stsp uint32_t nhave = 0;
706 13b2bc37 2022-10-23 stsp SHA1_CTX ctx;
707 13b2bc37 2022-10-23 stsp uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
708 13b2bc37 2022-10-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
709 13b2bc37 2022-10-23 stsp BUF *buf = NULL;
710 13b2bc37 2022-10-23 stsp size_t buf_pos = 0, remain;
713 13b2bc37 2022-10-23 stsp *outsize = 0;
716 9a8e357c 2023-01-28 op /* if only deleting references there's nothing to read */
717 9a8e357c 2023-01-28 op if (client->nref_updates == client->nref_del)
720 13b2bc37 2022-10-23 stsp SHA1Init(&ctx);
722 13b2bc37 2022-10-23 stsp err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
724 13b2bc37 2022-10-23 stsp return err;
725 13b2bc37 2022-10-23 stsp if (have != sizeof(hdr))
726 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
727 13b2bc37 2022-10-23 stsp *outsize += have;
729 13b2bc37 2022-10-23 stsp if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
730 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
731 13b2bc37 2022-10-23 stsp "bad packfile signature");
732 13b2bc37 2022-10-23 stsp if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
733 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
734 13b2bc37 2022-10-23 stsp "bad packfile version");
736 0ff2c315 2023-01-18 stsp *nobj = be32toh(hdr.nobjects);
737 0ff2c315 2023-01-18 stsp if (*nobj == 0) {
739 0ff2c315 2023-01-18 stsp * Clients which are creating new references only
740 0ff2c315 2023-01-18 stsp * will send us an empty pack file.
742 0ff2c315 2023-01-18 stsp if (client->nref_updates > 0 &&
743 0ff2c315 2023-01-18 stsp client->nref_updates == client->nref_new)
744 0ff2c315 2023-01-18 stsp return NULL;
746 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
747 13b2bc37 2022-10-23 stsp "bad packfile with zero objects");
750 0ff2c315 2023-01-18 stsp log_debug("expecting %d objects", *nobj);
752 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
754 13b2bc37 2022-10-23 stsp return err;
756 13b2bc37 2022-10-23 stsp err = buf_alloc(&buf, 65536);
758 13b2bc37 2022-10-23 stsp return err;
760 0ff2c315 2023-01-18 stsp while (nhave != *nobj) {
761 13b2bc37 2022-10-23 stsp uint8_t obj_type;
762 13b2bc37 2022-10-23 stsp uint64_t obj_size;
764 13b2bc37 2022-10-23 stsp err = copy_object_type_and_size(&obj_type, &obj_size,
765 13b2bc37 2022-10-23 stsp infd, outfd, outsize, buf, &buf_pos, &ctx);
769 13b2bc37 2022-10-23 stsp err = validate_object_type(obj_type);
773 13b2bc37 2022-10-23 stsp if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
774 13b2bc37 2022-10-23 stsp err = copy_ref_delta(infd, outfd, outsize,
775 13b2bc37 2022-10-23 stsp buf, &buf_pos, &ctx);
778 13b2bc37 2022-10-23 stsp } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
779 13b2bc37 2022-10-23 stsp err = copy_offset_delta(infd, outfd, outsize,
780 13b2bc37 2022-10-23 stsp buf, &buf_pos, &ctx);
785 13b2bc37 2022-10-23 stsp err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
792 0ff2c315 2023-01-18 stsp log_debug("received %u objects", *nobj);
794 13b2bc37 2022-10-23 stsp SHA1Final(expected_sha1, &ctx);
796 13b2bc37 2022-10-23 stsp remain = buf_len(buf) - buf_pos;
797 13b2bc37 2022-10-23 stsp if (remain < SHA1_DIGEST_LENGTH) {
798 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
799 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH - remain);
801 13b2bc37 2022-10-23 stsp return err;
804 13b2bc37 2022-10-23 stsp got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
805 13b2bc37 2022-10-23 stsp log_debug("expect SHA1: %s", hex);
806 13b2bc37 2022-10-23 stsp got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
807 13b2bc37 2022-10-23 stsp log_debug("actual SHA1: %s", hex);
809 13b2bc37 2022-10-23 stsp if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
810 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH) != 0) {
811 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PACKFILE_CSUM);
815 13b2bc37 2022-10-23 stsp memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
817 13b2bc37 2022-10-23 stsp w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
818 13b2bc37 2022-10-23 stsp if (w == -1) {
819 13b2bc37 2022-10-23 stsp err = got_error_from_errno("write");
822 13b2bc37 2022-10-23 stsp if (w != SHA1_DIGEST_LENGTH) {
823 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_IO);
827 13b2bc37 2022-10-23 stsp *outsize += SHA1_DIGEST_LENGTH;
829 13b2bc37 2022-10-23 stsp if (fsync(outfd) == -1) {
830 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fsync");
833 13b2bc37 2022-10-23 stsp if (lseek(outfd, 0L, SEEK_SET) == -1) {
834 13b2bc37 2022-10-23 stsp err = got_error_from_errno("lseek");
838 13b2bc37 2022-10-23 stsp buf_free(buf);
839 13b2bc37 2022-10-23 stsp return err;
842 13b2bc37 2022-10-23 stsp static const struct got_error *
843 1a52c9bf 2022-12-30 stsp report_pack_status(const struct got_error *unpack_err)
845 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
846 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
847 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_status istatus;
848 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
849 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
850 13b2bc37 2022-10-23 stsp const char *unpack_ok = "unpack ok\n";
851 13b2bc37 2022-10-23 stsp size_t len;
853 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, client->fd);
855 13b2bc37 2022-10-23 stsp if (unpack_err)
856 13b2bc37 2022-10-23 stsp istatus.reason_len = strlen(unpack_err->msg);
858 13b2bc37 2022-10-23 stsp istatus.reason_len = strlen(unpack_ok);
860 13b2bc37 2022-10-23 stsp len = sizeof(istatus) + istatus.reason_len;
861 13b2bc37 2022-10-23 stsp wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
862 13b2bc37 2022-10-23 stsp repo_write.pid, len);
863 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
864 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create PACKFILE_STATUS");
868 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
869 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add PACKFILE_STATUS");
873 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, err ? err->msg : unpack_ok,
874 13b2bc37 2022-10-23 stsp istatus.reason_len) == -1) {
875 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add PACKFILE_STATUS");
879 13b2bc37 2022-10-23 stsp wbuf->fd = -1;
880 13b2bc37 2022-10-23 stsp imsg_close(&ibuf, wbuf);
882 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(&ibuf);
884 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
885 13b2bc37 2022-10-23 stsp return err;
888 13b2bc37 2022-10-23 stsp static const struct got_error *
889 0ff2c315 2023-01-18 stsp recv_packfile(int *have_packfile, struct imsg *imsg)
891 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL, *unpack_err;
892 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
893 13b2bc37 2022-10-23 stsp struct gotd_imsg_recv_packfile ireq;
894 13b2bc37 2022-10-23 stsp FILE *tempfiles[3] = { NULL, NULL, NULL };
895 13b2bc37 2022-10-23 stsp struct repo_tempfile {
898 13b2bc37 2022-10-23 stsp } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
900 13b2bc37 2022-10-23 stsp size_t datalen;
901 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
902 13b2bc37 2022-10-23 stsp struct got_ratelimit rl;
903 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
904 13b2bc37 2022-10-23 stsp off_t pack_filesize = 0;
905 0ff2c315 2023-01-18 stsp uint32_t nobj = 0;
907 13b2bc37 2022-10-23 stsp log_debug("packfile request received");
909 0ff2c315 2023-01-18 stsp *have_packfile = 0;
910 13b2bc37 2022-10-23 stsp got_ratelimit_init(&rl, 2, 0);
912 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
913 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
914 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
915 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
917 1a52c9bf 2022-12-30 stsp if (client->pack_pipe == -1 || client->packidx_fd == -1)
918 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
920 1a52c9bf 2022-12-30 stsp imsg_init(&ibuf, client->fd);
922 13b2bc37 2022-10-23 stsp if (imsg->fd == -1)
923 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
925 1a52c9bf 2022-12-30 stsp pack = &client->pack;
926 13b2bc37 2022-10-23 stsp memset(pack, 0, sizeof(*pack));
927 13b2bc37 2022-10-23 stsp pack->fd = imsg->fd;
928 13b2bc37 2022-10-23 stsp err = got_delta_cache_alloc(&pack->delta_cache);
930 13b2bc37 2022-10-23 stsp return err;
932 13b2bc37 2022-10-23 stsp for (i = 0; i < nitems(repo_tempfiles); i++) {
933 13b2bc37 2022-10-23 stsp struct repo_tempfile *t = &repo_tempfiles[i];
934 13b2bc37 2022-10-23 stsp err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
939 13b2bc37 2022-10-23 stsp for (i = 0; i < nitems(tempfiles); i++) {
943 e294dc4e 2023-02-03 mark fd = dup(repo_tempfiles[i].fd);
944 13b2bc37 2022-10-23 stsp if (fd == -1) {
945 13b2bc37 2022-10-23 stsp err = got_error_from_errno("dup");
948 13b2bc37 2022-10-23 stsp f = fdopen(fd, "w+");
949 13b2bc37 2022-10-23 stsp if (f == NULL) {
950 e294dc4e 2023-02-03 mark err = got_error_from_errno("fdopen");
954 13b2bc37 2022-10-23 stsp tempfiles[i] = f;
957 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(&ibuf);
961 13b2bc37 2022-10-23 stsp log_debug("receiving pack data");
962 0ff2c315 2023-01-18 stsp unpack_err = recv_packdata(&pack_filesize, &nobj,
963 0ff2c315 2023-01-18 stsp client->pack_sha1, client->pack_pipe, pack->fd);
964 13b2bc37 2022-10-23 stsp if (ireq.report_status) {
965 1a52c9bf 2022-12-30 stsp err = report_pack_status(unpack_err);
967 13b2bc37 2022-10-23 stsp /* Git clients hang up after sending the pack file. */
968 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_EOF)
969 13b2bc37 2022-10-23 stsp err = NULL;
972 13b2bc37 2022-10-23 stsp if (unpack_err)
973 13b2bc37 2022-10-23 stsp err = unpack_err;
977 13b2bc37 2022-10-23 stsp log_debug("pack data received");
980 0ff2c315 2023-01-18 stsp * Clients which are creating new references only will
981 0ff2c315 2023-01-18 stsp * send us an empty pack file.
983 0ff2c315 2023-01-18 stsp if (nobj == 0 &&
984 0ff2c315 2023-01-18 stsp pack_filesize == sizeof(struct got_packfile_hdr) &&
985 0ff2c315 2023-01-18 stsp client->nref_updates > 0 &&
986 0ff2c315 2023-01-18 stsp client->nref_updates == client->nref_new)
990 9a8e357c 2023-01-28 op * Clients which are deleting references only will send
991 9a8e357c 2023-01-28 op * no pack file.
993 9a8e357c 2023-01-28 op if (nobj == 0 &&
994 9a8e357c 2023-01-28 op client->nref_del > 0 &&
995 9a8e357c 2023-01-28 op client->nref_updates == client->nref_del)
998 13b2bc37 2022-10-23 stsp pack->filesize = pack_filesize;
999 0ff2c315 2023-01-18 stsp *have_packfile = 1;
1001 ad4cc361 2022-10-27 op log_debug("begin indexing pack (%lld bytes in size)",
1002 ad4cc361 2022-10-27 op (long long)pack->filesize);
1003 1a52c9bf 2022-12-30 stsp err = got_pack_index(pack, client->packidx_fd,
1004 1a52c9bf 2022-12-30 stsp tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
1005 13b2bc37 2022-10-23 stsp pack_index_progress, NULL, &rl);
1007 13b2bc37 2022-10-23 stsp goto done;
1008 13b2bc37 2022-10-23 stsp log_debug("done indexing pack");
1010 1a52c9bf 2022-12-30 stsp if (fsync(client->packidx_fd) == -1) {
1011 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fsync");
1012 13b2bc37 2022-10-23 stsp goto done;
1014 1a52c9bf 2022-12-30 stsp if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1015 13b2bc37 2022-10-23 stsp err = got_error_from_errno("lseek");
1017 1a52c9bf 2022-12-30 stsp if (close(client->pack_pipe) == -1 && err == NULL)
1018 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
1019 1a52c9bf 2022-12-30 stsp client->pack_pipe = -1;
1020 13b2bc37 2022-10-23 stsp for (i = 0; i < nitems(repo_tempfiles); i++) {
1021 13b2bc37 2022-10-23 stsp struct repo_tempfile *t = &repo_tempfiles[i];
1022 13b2bc37 2022-10-23 stsp if (t->idx != -1)
1023 13b2bc37 2022-10-23 stsp got_repo_temp_fds_put(t->idx, repo_write.repo);
1025 13b2bc37 2022-10-23 stsp for (i = 0; i < nitems(tempfiles); i++) {
1026 13b2bc37 2022-10-23 stsp if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1027 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fclose");
1030 13b2bc37 2022-10-23 stsp got_pack_close(pack);
1031 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
1032 13b2bc37 2022-10-23 stsp return err;
1035 13b2bc37 2022-10-23 stsp static const struct got_error *
1036 1a52c9bf 2022-12-30 stsp verify_packfile(void)
1038 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL, *close_err;
1039 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1040 13b2bc37 2022-10-23 stsp struct gotd_ref_update *ref_update;
1041 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
1042 13b2bc37 2022-10-23 stsp struct stat sb;
1043 13b2bc37 2022-10-23 stsp char *id_str = NULL;
1044 13b2bc37 2022-10-23 stsp int idx = -1;
1046 13b2bc37 2022-10-23 stsp if (STAILQ_EMPTY(&client->ref_updates)) {
1047 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1048 13b2bc37 2022-10-23 stsp "cannot verify pack file without any ref-updates");
1051 13b2bc37 2022-10-23 stsp if (client->pack.fd == -1) {
1052 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1053 13b2bc37 2022-10-23 stsp "invalid pack file handle during pack verification");
1055 13b2bc37 2022-10-23 stsp if (client->packidx_fd == -1) {
1056 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1057 13b2bc37 2022-10-23 stsp "invalid pack index handle during pack verification");
1060 13b2bc37 2022-10-23 stsp if (fstat(client->packidx_fd, &sb) == -1)
1061 13b2bc37 2022-10-23 stsp return got_error_from_errno("pack index fstat");
1063 13b2bc37 2022-10-23 stsp packidx = malloc(sizeof(*packidx));
1064 13b2bc37 2022-10-23 stsp memset(packidx, 0, sizeof(*packidx));
1065 13b2bc37 2022-10-23 stsp packidx->fd = client->packidx_fd;
1066 13b2bc37 2022-10-23 stsp client->packidx_fd = -1;
1067 13b2bc37 2022-10-23 stsp packidx->len = sb.st_size;
1069 13b2bc37 2022-10-23 stsp err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1071 13b2bc37 2022-10-23 stsp return err;
1073 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1074 9a8e357c 2023-01-28 op if (ref_update->delete_ref)
1077 13b2bc37 2022-10-23 stsp err = got_object_id_str(&id_str, &ref_update->new_id);
1079 13b2bc37 2022-10-23 stsp goto done;
1081 13b2bc37 2022-10-23 stsp idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1082 13b2bc37 2022-10-23 stsp if (idx == -1) {
1083 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1084 13b2bc37 2022-10-23 stsp "advertised object %s is missing from pack file",
1086 13b2bc37 2022-10-23 stsp goto done;
1091 13b2bc37 2022-10-23 stsp close_err = got_packidx_close(packidx);
1092 13b2bc37 2022-10-23 stsp if (close_err && err == NULL)
1093 13b2bc37 2022-10-23 stsp err = close_err;
1094 13b2bc37 2022-10-23 stsp free(id_str);
1095 13b2bc37 2022-10-23 stsp return err;
1098 13b2bc37 2022-10-23 stsp static const struct got_error *
1099 1a52c9bf 2022-12-30 stsp install_packfile(struct gotd_imsgev *iev)
1101 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1102 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_install inst;
1105 13b2bc37 2022-10-23 stsp memset(&inst, 0, sizeof(inst));
1106 13b2bc37 2022-10-23 stsp inst.client_id = client->id;
1107 13b2bc37 2022-10-23 stsp memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1109 13b2bc37 2022-10-23 stsp ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1110 13b2bc37 2022-10-23 stsp PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1111 13b2bc37 2022-10-23 stsp if (ret == -1)
1112 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1114 13b2bc37 2022-10-23 stsp return NULL;
1117 13b2bc37 2022-10-23 stsp static const struct got_error *
1118 1a52c9bf 2022-12-30 stsp send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1120 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1121 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_updates_start istart;
1124 13b2bc37 2022-10-23 stsp memset(&istart, 0, sizeof(istart));
1125 13b2bc37 2022-10-23 stsp istart.nref_updates = nref_updates;
1126 13b2bc37 2022-10-23 stsp istart.client_id = client->id;
1128 13b2bc37 2022-10-23 stsp ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1129 13b2bc37 2022-10-23 stsp PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1130 13b2bc37 2022-10-23 stsp if (ret == -1)
1131 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_compose REF_UPDATES_START");
1133 13b2bc37 2022-10-23 stsp return NULL;
1137 13b2bc37 2022-10-23 stsp static const struct got_error *
1138 1a52c9bf 2022-12-30 stsp send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1140 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1141 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update iref;
1142 13b2bc37 2022-10-23 stsp const char *refname = got_ref_get_name(ref_update->ref);
1143 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
1144 13b2bc37 2022-10-23 stsp size_t len;
1146 13b2bc37 2022-10-23 stsp memset(&iref, 0, sizeof(iref));
1147 13b2bc37 2022-10-23 stsp memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1148 13b2bc37 2022-10-23 stsp memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1149 13b2bc37 2022-10-23 stsp iref.ref_is_new = ref_update->ref_is_new;
1150 9a8e357c 2023-01-28 op iref.delete_ref = ref_update->delete_ref;
1151 13b2bc37 2022-10-23 stsp iref.client_id = client->id;
1152 13b2bc37 2022-10-23 stsp iref.name_len = strlen(refname);
1154 13b2bc37 2022-10-23 stsp len = sizeof(iref) + iref.name_len;
1155 13b2bc37 2022-10-23 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1156 13b2bc37 2022-10-23 stsp repo_write.pid, len);
1157 13b2bc37 2022-10-23 stsp if (wbuf == NULL)
1158 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_create REF_UPDATE");
1160 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1161 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE");
1162 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, refname, iref.name_len) == -1)
1163 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE");
1165 13b2bc37 2022-10-23 stsp wbuf->fd = -1;
1166 13b2bc37 2022-10-23 stsp imsg_close(&iev->ibuf, wbuf);
1168 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
1169 13b2bc37 2022-10-23 stsp return NULL;
1172 13b2bc37 2022-10-23 stsp static const struct got_error *
1173 1a52c9bf 2022-12-30 stsp update_refs(struct gotd_imsgev *iev)
1175 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1176 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1177 13b2bc37 2022-10-23 stsp struct gotd_ref_update *ref_update;
1179 1a52c9bf 2022-12-30 stsp err = send_ref_updates_start(client->nref_updates, iev);
1181 13b2bc37 2022-10-23 stsp return err;
1183 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1184 1a52c9bf 2022-12-30 stsp err = send_ref_update(ref_update, iev);
1186 13b2bc37 2022-10-23 stsp goto done;
1189 13b2bc37 2022-10-23 stsp return err;
1192 13b2bc37 2022-10-23 stsp static const struct got_error *
1193 1a52c9bf 2022-12-30 stsp receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1195 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1196 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_pipe ireq;
1197 13b2bc37 2022-10-23 stsp size_t datalen;
1199 13b2bc37 2022-10-23 stsp log_debug("receving pack pipe descriptor");
1201 13b2bc37 2022-10-23 stsp if (imsg->fd == -1)
1202 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1204 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1205 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
1206 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1207 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
1209 1a52c9bf 2022-12-30 stsp if (client->pack_pipe != -1)
1210 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1212 1a52c9bf 2022-12-30 stsp client->pack_pipe = imsg->fd;
1213 13b2bc37 2022-10-23 stsp return NULL;
1216 13b2bc37 2022-10-23 stsp static const struct got_error *
1217 1a52c9bf 2022-12-30 stsp receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1219 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1220 13b2bc37 2022-10-23 stsp struct gotd_imsg_packidx_file ireq;
1221 13b2bc37 2022-10-23 stsp size_t datalen;
1223 13b2bc37 2022-10-23 stsp log_debug("receving pack index output file");
1225 13b2bc37 2022-10-23 stsp if (imsg->fd == -1)
1226 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1228 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1229 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
1230 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1231 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
1233 1a52c9bf 2022-12-30 stsp if (client->packidx_fd != -1)
1234 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1236 1a52c9bf 2022-12-30 stsp client->packidx_fd = imsg->fd;
1237 13b2bc37 2022-10-23 stsp return NULL;
1240 13b2bc37 2022-10-23 stsp static void
1241 ae7c1b78 2023-01-10 stsp repo_write_dispatch_session(int fd, short event, void *arg)
1243 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1244 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
1245 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
1246 13b2bc37 2022-10-23 stsp struct imsg imsg;
1247 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1248 13b2bc37 2022-10-23 stsp ssize_t n;
1249 0ff2c315 2023-01-18 stsp int shut = 0, have_packfile = 0;
1251 13b2bc37 2022-10-23 stsp if (event & EV_READ) {
1252 13b2bc37 2022-10-23 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1253 13b2bc37 2022-10-23 stsp fatal("imsg_read error");
1254 13b2bc37 2022-10-23 stsp if (n == 0) /* Connection closed. */
1258 13b2bc37 2022-10-23 stsp if (event & EV_WRITE) {
1259 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
1260 13b2bc37 2022-10-23 stsp if (n == -1 && errno != EAGAIN)
1261 13b2bc37 2022-10-23 stsp fatal("msgbuf_write");
1262 13b2bc37 2022-10-23 stsp if (n == 0) /* Connection closed. */
1266 13b2bc37 2022-10-23 stsp for (;;) {
1267 13b2bc37 2022-10-23 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1268 13b2bc37 2022-10-23 stsp fatal("%s: imsg_get error", __func__);
1269 13b2bc37 2022-10-23 stsp if (n == 0) /* No more messages. */
1272 1a52c9bf 2022-12-30 stsp if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1273 1a52c9bf 2022-12-30 stsp client->id == 0) {
1274 1a52c9bf 2022-12-30 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1278 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
1279 13b2bc37 2022-10-23 stsp case GOTD_IMSG_LIST_REFS_INTERNAL:
1280 1a52c9bf 2022-12-30 stsp err = list_refs(&imsg);
1282 13b2bc37 2022-10-23 stsp log_warnx("%s: ls-refs: %s", repo_write.title,
1283 13b2bc37 2022-10-23 stsp err->msg);
1285 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE:
1286 1a52c9bf 2022-12-30 stsp err = recv_ref_update(&imsg);
1288 13b2bc37 2022-10-23 stsp log_warnx("%s: ref-update: %s",
1289 13b2bc37 2022-10-23 stsp repo_write.title, err->msg);
1291 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_PIPE:
1292 1a52c9bf 2022-12-30 stsp err = receive_pack_pipe(&imsg, iev);
1293 13b2bc37 2022-10-23 stsp if (err) {
1294 13b2bc37 2022-10-23 stsp log_warnx("%s: receiving pack pipe: %s",
1295 13b2bc37 2022-10-23 stsp repo_write.title, err->msg);
1299 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKIDX_FILE:
1300 1a52c9bf 2022-12-30 stsp err = receive_pack_idx(&imsg, iev);
1301 13b2bc37 2022-10-23 stsp if (err) {
1302 13b2bc37 2022-10-23 stsp log_warnx("%s: receiving pack index: %s",
1303 13b2bc37 2022-10-23 stsp repo_write.title, err->msg);
1307 13b2bc37 2022-10-23 stsp case GOTD_IMSG_RECV_PACKFILE:
1308 0ff2c315 2023-01-18 stsp err = recv_packfile(&have_packfile, &imsg);
1309 13b2bc37 2022-10-23 stsp if (err) {
1310 13b2bc37 2022-10-23 stsp log_warnx("%s: receive packfile: %s",
1311 13b2bc37 2022-10-23 stsp repo_write.title, err->msg);
1314 0ff2c315 2023-01-18 stsp if (have_packfile) {
1315 0ff2c315 2023-01-18 stsp err = verify_packfile();
1316 0ff2c315 2023-01-18 stsp if (err) {
1317 0ff2c315 2023-01-18 stsp log_warnx("%s: verify packfile: %s",
1318 0ff2c315 2023-01-18 stsp repo_write.title, err->msg);
1321 0ff2c315 2023-01-18 stsp err = install_packfile(iev);
1322 0ff2c315 2023-01-18 stsp if (err) {
1323 0ff2c315 2023-01-18 stsp log_warnx("%s: install packfile: %s",
1324 0ff2c315 2023-01-18 stsp repo_write.title, err->msg);
1328 1a52c9bf 2022-12-30 stsp err = update_refs(iev);
1329 13b2bc37 2022-10-23 stsp if (err) {
1330 13b2bc37 2022-10-23 stsp log_warnx("%s: update refs: %s",
1331 13b2bc37 2022-10-23 stsp repo_write.title, err->msg);
1335 ae7c1b78 2023-01-10 stsp log_debug("%s: unexpected imsg %d", repo_write.title,
1336 ae7c1b78 2023-01-10 stsp imsg.hdr.type);
1340 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
1343 ae7c1b78 2023-01-10 stsp if (!shut && check_cancelled(NULL) == NULL) {
1344 ae7c1b78 2023-01-10 stsp if (err &&
1345 ae7c1b78 2023-01-10 stsp gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1346 ae7c1b78 2023-01-10 stsp client->id, err) == -1) {
1347 ae7c1b78 2023-01-10 stsp log_warnx("could not send error to parent: %s",
1348 ae7c1b78 2023-01-10 stsp err->msg);
1350 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
1352 ae7c1b78 2023-01-10 stsp /* This pipe is dead. Remove its event handler */
1353 ae7c1b78 2023-01-10 stsp event_del(&iev->ev);
1354 ae7c1b78 2023-01-10 stsp event_loopexit(NULL);
1358 ae7c1b78 2023-01-10 stsp static const struct got_error *
1359 ae7c1b78 2023-01-10 stsp recv_connect(struct imsg *imsg)
1361 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = &repo_write.session_iev;
1362 ae7c1b78 2023-01-10 stsp size_t datalen;
1364 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1365 ae7c1b78 2023-01-10 stsp if (datalen != 0)
1366 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1367 ae7c1b78 2023-01-10 stsp if (imsg->fd == -1)
1368 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1370 ae7c1b78 2023-01-10 stsp if (repo_write.session_fd != -1)
1371 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1373 ae7c1b78 2023-01-10 stsp repo_write.session_fd = imsg->fd;
1375 ae7c1b78 2023-01-10 stsp imsg_init(&iev->ibuf, repo_write.session_fd);
1376 ae7c1b78 2023-01-10 stsp iev->handler = repo_write_dispatch_session;
1377 ae7c1b78 2023-01-10 stsp iev->events = EV_READ;
1378 ae7c1b78 2023-01-10 stsp iev->handler_arg = NULL;
1379 ae7c1b78 2023-01-10 stsp event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1380 ae7c1b78 2023-01-10 stsp repo_write_dispatch_session, iev);
1381 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
1383 ae7c1b78 2023-01-10 stsp return NULL;
1386 ae7c1b78 2023-01-10 stsp static void
1387 ae7c1b78 2023-01-10 stsp repo_write_dispatch(int fd, short event, void *arg)
1389 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
1390 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = arg;
1391 ae7c1b78 2023-01-10 stsp struct imsgbuf *ibuf = &iev->ibuf;
1392 ae7c1b78 2023-01-10 stsp struct imsg imsg;
1393 ae7c1b78 2023-01-10 stsp ssize_t n;
1394 ae7c1b78 2023-01-10 stsp int shut = 0;
1395 ae7c1b78 2023-01-10 stsp struct repo_write_client *client = &repo_write_client;
1397 ae7c1b78 2023-01-10 stsp if (event & EV_READ) {
1398 ae7c1b78 2023-01-10 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1399 ae7c1b78 2023-01-10 stsp fatal("imsg_read error");
1400 ae7c1b78 2023-01-10 stsp if (n == 0) /* Connection closed. */
1404 ae7c1b78 2023-01-10 stsp if (event & EV_WRITE) {
1405 ae7c1b78 2023-01-10 stsp n = msgbuf_write(&ibuf->w);
1406 ae7c1b78 2023-01-10 stsp if (n == -1 && errno != EAGAIN)
1407 ae7c1b78 2023-01-10 stsp fatal("msgbuf_write");
1408 ae7c1b78 2023-01-10 stsp if (n == 0) /* Connection closed. */
1412 ae7c1b78 2023-01-10 stsp while (err == NULL && check_cancelled(NULL) == NULL) {
1413 ae7c1b78 2023-01-10 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1414 ae7c1b78 2023-01-10 stsp fatal("%s: imsg_get", __func__);
1415 ae7c1b78 2023-01-10 stsp if (n == 0) /* No more messages. */
1418 ae7c1b78 2023-01-10 stsp switch (imsg.hdr.type) {
1419 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CONNECT_REPO_CHILD:
1420 ae7c1b78 2023-01-10 stsp err = recv_connect(&imsg);
1423 13b2bc37 2022-10-23 stsp log_debug("%s: unexpected imsg %d", repo_write.title,
1424 13b2bc37 2022-10-23 stsp imsg.hdr.type);
1428 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1431 13b2bc37 2022-10-23 stsp if (!shut && check_cancelled(NULL) == NULL) {
1432 13b2bc37 2022-10-23 stsp if (err &&
1433 13b2bc37 2022-10-23 stsp gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1434 1a52c9bf 2022-12-30 stsp client->id, err) == -1) {
1435 13b2bc37 2022-10-23 stsp log_warnx("could not send error to parent: %s",
1436 13b2bc37 2022-10-23 stsp err->msg);
1438 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
1440 13b2bc37 2022-10-23 stsp /* This pipe is dead. Remove its event handler */
1441 13b2bc37 2022-10-23 stsp event_del(&iev->ev);
1442 13b2bc37 2022-10-23 stsp event_loopexit(NULL);
1447 eec68231 2022-12-14 stsp repo_write_main(const char *title, const char *repo_path,
1448 eec68231 2022-12-14 stsp int *pack_fds, int *temp_fds)
1450 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1451 363c6230 2023-02-09 stsp struct repo_write_client *client = &repo_write_client;
1452 13b2bc37 2022-10-23 stsp struct gotd_imsgev iev;
1454 363c6230 2023-02-09 stsp client->fd = -1;
1455 363c6230 2023-02-09 stsp client->pack_pipe = -1;
1456 363c6230 2023-02-09 stsp client->packidx_fd = -1;
1457 363c6230 2023-02-09 stsp client->pack.fd = -1;
1459 13b2bc37 2022-10-23 stsp repo_write.title = title;
1460 13b2bc37 2022-10-23 stsp repo_write.pid = getpid();
1461 13b2bc37 2022-10-23 stsp repo_write.pack_fds = pack_fds;
1462 13b2bc37 2022-10-23 stsp repo_write.temp_fds = temp_fds;
1463 ae7c1b78 2023-01-10 stsp repo_write.session_fd = -1;
1464 ae7c1b78 2023-01-10 stsp repo_write.session_iev.ibuf.fd = -1;
1466 1a52c9bf 2022-12-30 stsp STAILQ_INIT(&repo_write_client.ref_updates);
1468 eec68231 2022-12-14 stsp err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1470 13b2bc37 2022-10-23 stsp goto done;
1471 13b2bc37 2022-10-23 stsp if (!got_repo_is_bare(repo_write.repo)) {
1472 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1473 13b2bc37 2022-10-23 stsp "bare git repository required");
1474 13b2bc37 2022-10-23 stsp goto done;
1477 13b2bc37 2022-10-23 stsp got_repo_temp_fds_set(repo_write.repo, temp_fds);
1479 13b2bc37 2022-10-23 stsp signal(SIGINT, catch_sigint);
1480 13b2bc37 2022-10-23 stsp signal(SIGTERM, catch_sigterm);
1481 13b2bc37 2022-10-23 stsp signal(SIGPIPE, SIG_IGN);
1482 13b2bc37 2022-10-23 stsp signal(SIGHUP, SIG_IGN);
1484 8c6fc146 2022-11-17 stsp imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1485 13b2bc37 2022-10-23 stsp iev.handler = repo_write_dispatch;
1486 13b2bc37 2022-10-23 stsp iev.events = EV_READ;
1487 13b2bc37 2022-10-23 stsp iev.handler_arg = NULL;
1488 13b2bc37 2022-10-23 stsp event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1489 b50a2b46 2022-12-29 stsp if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1490 b50a2b46 2022-12-29 stsp PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1491 b50a2b46 2022-12-29 stsp err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1492 13b2bc37 2022-10-23 stsp goto done;
1495 13b2bc37 2022-10-23 stsp event_dispatch();
1498 13b2bc37 2022-10-23 stsp log_warnx("%s: %s", title, err->msg);
1499 13b2bc37 2022-10-23 stsp repo_write_shutdown();
1503 13b2bc37 2022-10-23 stsp repo_write_shutdown(void)
1505 363c6230 2023-02-09 stsp struct repo_write_client *client = &repo_write_client;
1506 363c6230 2023-02-09 stsp struct gotd_ref_update *ref_update;
1508 13b2bc37 2022-10-23 stsp log_debug("%s: shutting down", repo_write.title);
1510 363c6230 2023-02-09 stsp while (!STAILQ_EMPTY(&client->ref_updates)) {
1511 363c6230 2023-02-09 stsp ref_update = STAILQ_FIRST(&client->ref_updates);
1512 363c6230 2023-02-09 stsp STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1513 363c6230 2023-02-09 stsp got_ref_close(ref_update->ref);
1514 363c6230 2023-02-09 stsp free(ref_update);
1517 363c6230 2023-02-09 stsp got_pack_close(&client->pack);
1518 363c6230 2023-02-09 stsp if (client->fd != -1)
1519 363c6230 2023-02-09 stsp close(client->fd);
1520 363c6230 2023-02-09 stsp if (client->pack_pipe != -1)
1521 363c6230 2023-02-09 stsp close(client->pack_pipe);
1522 363c6230 2023-02-09 stsp if (client->packidx_fd != -1)
1523 363c6230 2023-02-09 stsp close(client->packidx_fd);
1525 13b2bc37 2022-10-23 stsp if (repo_write.repo)
1526 13b2bc37 2022-10-23 stsp got_repo_close(repo_write.repo);
1527 13b2bc37 2022-10-23 stsp got_repo_pack_fds_close(repo_write.pack_fds);
1528 8193d041 2022-10-30 stsp got_repo_temp_fds_close(repo_write.temp_fds);
1529 ae7c1b78 2023-01-10 stsp if (repo_write.session_fd != -1)
1530 ae7c1b78 2023-01-10 stsp close(repo_write.session_fd);