2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 3efd8e31 2022-10-23 thomas * Permission to use, copy, modify, and distribute this software for any
5 3efd8e31 2022-10-23 thomas * purpose with or without fee is hereby granted, provided that the above
6 3efd8e31 2022-10-23 thomas * copyright notice and this permission notice appear in all copies.
8 3efd8e31 2022-10-23 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3efd8e31 2022-10-23 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3efd8e31 2022-10-23 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3efd8e31 2022-10-23 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3efd8e31 2022-10-23 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3efd8e31 2022-10-23 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3efd8e31 2022-10-23 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
18 3efd8e31 2022-10-23 thomas #include <sys/stat.h>
19 3efd8e31 2022-10-23 thomas #include <sys/tree.h>
20 3efd8e31 2022-10-23 thomas #include <sys/types.h>
22 3efd8e31 2022-10-23 thomas #include <event.h>
23 3efd8e31 2022-10-23 thomas #include <errno.h>
24 3efd8e31 2022-10-23 thomas #include <imsg.h>
25 3efd8e31 2022-10-23 thomas #include <signal.h>
26 3efd8e31 2022-10-23 thomas #include <siphash.h>
27 3efd8e31 2022-10-23 thomas #include <stdio.h>
28 3efd8e31 2022-10-23 thomas #include <stdlib.h>
29 3efd8e31 2022-10-23 thomas #include <string.h>
30 3efd8e31 2022-10-23 thomas #include <limits.h>
31 3efd8e31 2022-10-23 thomas #include <poll.h>
32 3efd8e31 2022-10-23 thomas #include <sha1.h>
33 3efd8e31 2022-10-23 thomas #include <unistd.h>
34 3efd8e31 2022-10-23 thomas #include <zlib.h>
36 3efd8e31 2022-10-23 thomas #include "buf.h"
38 3efd8e31 2022-10-23 thomas #include "got_error.h"
39 3efd8e31 2022-10-23 thomas #include "got_repository.h"
40 3efd8e31 2022-10-23 thomas #include "got_object.h"
41 3efd8e31 2022-10-23 thomas #include "got_reference.h"
42 3efd8e31 2022-10-23 thomas #include "got_path.h"
44 3efd8e31 2022-10-23 thomas #include "got_lib_delta.h"
45 3efd8e31 2022-10-23 thomas #include "got_lib_delta_cache.h"
46 3efd8e31 2022-10-23 thomas #include "got_lib_object.h"
47 3efd8e31 2022-10-23 thomas #include "got_lib_object_cache.h"
48 3efd8e31 2022-10-23 thomas #include "got_lib_ratelimit.h"
49 3efd8e31 2022-10-23 thomas #include "got_lib_pack.h"
50 3efd8e31 2022-10-23 thomas #include "got_lib_pack_index.h"
51 3efd8e31 2022-10-23 thomas #include "got_lib_repository.h"
52 3efd8e31 2022-10-23 thomas #include "got_lib_poll.h"
54 3efd8e31 2022-10-23 thomas #include "got_lib_sha1.h" /* XXX temp include for debugging */
56 3efd8e31 2022-10-23 thomas #include "log.h"
57 3efd8e31 2022-10-23 thomas #include "gotd.h"
58 3efd8e31 2022-10-23 thomas #include "repo_write.h"
60 3efd8e31 2022-10-23 thomas #ifndef nitems
61 3efd8e31 2022-10-23 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 3efd8e31 2022-10-23 thomas static struct repo_write {
65 3efd8e31 2022-10-23 thomas pid_t pid;
66 3efd8e31 2022-10-23 thomas const char *title;
67 3efd8e31 2022-10-23 thomas struct got_repository *repo;
68 3efd8e31 2022-10-23 thomas int *pack_fds;
69 3efd8e31 2022-10-23 thomas int *temp_fds;
70 62ee7d94 2023-01-10 thomas int session_fd;
71 62ee7d94 2023-01-10 thomas struct gotd_imsgev session_iev;
72 3efd8e31 2022-10-23 thomas } repo_write;
74 3efd8e31 2022-10-23 thomas struct gotd_ref_update {
75 3efd8e31 2022-10-23 thomas STAILQ_ENTRY(gotd_ref_update) entry;
76 3efd8e31 2022-10-23 thomas struct got_reference *ref;
77 3efd8e31 2022-10-23 thomas int ref_is_new;
78 49563dfb 2023-01-28 thomas int delete_ref;
79 3efd8e31 2022-10-23 thomas struct got_object_id old_id;
80 3efd8e31 2022-10-23 thomas struct got_object_id new_id;
82 3efd8e31 2022-10-23 thomas STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
84 9148c8a7 2023-01-02 thomas static struct repo_write_client {
85 3efd8e31 2022-10-23 thomas uint32_t id;
87 3efd8e31 2022-10-23 thomas int pack_pipe[2];
88 3efd8e31 2022-10-23 thomas struct got_pack pack;
89 3efd8e31 2022-10-23 thomas uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
90 3efd8e31 2022-10-23 thomas int packidx_fd;
91 3efd8e31 2022-10-23 thomas struct gotd_ref_updates ref_updates;
92 3efd8e31 2022-10-23 thomas int nref_updates;
93 49563dfb 2023-01-28 thomas int nref_del;
94 d98779cd 2023-01-19 thomas int nref_new;
95 9148c8a7 2023-01-02 thomas } repo_write_client;
97 3efd8e31 2022-10-23 thomas static volatile sig_atomic_t sigint_received;
98 3efd8e31 2022-10-23 thomas static volatile sig_atomic_t sigterm_received;
100 3efd8e31 2022-10-23 thomas static void
101 3efd8e31 2022-10-23 thomas catch_sigint(int signo)
103 3efd8e31 2022-10-23 thomas sigint_received = 1;
106 3efd8e31 2022-10-23 thomas static void
107 3efd8e31 2022-10-23 thomas catch_sigterm(int signo)
109 3efd8e31 2022-10-23 thomas sigterm_received = 1;
112 3efd8e31 2022-10-23 thomas static const struct got_error *
113 3efd8e31 2022-10-23 thomas check_cancelled(void *arg)
115 3efd8e31 2022-10-23 thomas if (sigint_received || sigterm_received)
116 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_CANCELLED);
118 3efd8e31 2022-10-23 thomas return NULL;
121 3efd8e31 2022-10-23 thomas static const struct got_error *
122 3efd8e31 2022-10-23 thomas send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
123 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf)
125 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
126 3efd8e31 2022-10-23 thomas struct got_tag_object *tag;
127 3efd8e31 2022-10-23 thomas size_t namelen, len;
128 3efd8e31 2022-10-23 thomas char *peeled_refname = NULL;
129 3efd8e31 2022-10-23 thomas struct got_object_id *id;
130 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
132 3efd8e31 2022-10-23 thomas err = got_object_tag_open(&tag, repo_write.repo, obj);
134 3efd8e31 2022-10-23 thomas return err;
136 3efd8e31 2022-10-23 thomas if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
137 3efd8e31 2022-10-23 thomas err = got_error_from_errno("asprintf");
138 3efd8e31 2022-10-23 thomas goto done;
141 3efd8e31 2022-10-23 thomas id = got_object_tag_get_object_id(tag);
142 3efd8e31 2022-10-23 thomas namelen = strlen(peeled_refname);
144 3efd8e31 2022-10-23 thomas len = sizeof(struct gotd_imsg_ref) + namelen;
145 3efd8e31 2022-10-23 thomas if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
146 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_NO_SPACE);
147 3efd8e31 2022-10-23 thomas goto done;
150 3efd8e31 2022-10-23 thomas wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
151 3efd8e31 2022-10-23 thomas repo_write.pid, len);
152 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
153 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create REF");
154 3efd8e31 2022-10-23 thomas goto done;
157 3efd8e31 2022-10-23 thomas /* Keep in sync with struct gotd_imsg_ref definition. */
158 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
159 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add REF");
160 3efd8e31 2022-10-23 thomas goto done;
162 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
163 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add REF");
164 3efd8e31 2022-10-23 thomas goto done;
166 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
167 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add REF");
168 3efd8e31 2022-10-23 thomas goto done;
171 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
172 3efd8e31 2022-10-23 thomas imsg_close(ibuf, wbuf);
174 3efd8e31 2022-10-23 thomas got_object_tag_close(tag);
175 3efd8e31 2022-10-23 thomas return err;
178 3efd8e31 2022-10-23 thomas static const struct got_error *
179 3efd8e31 2022-10-23 thomas send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
181 3efd8e31 2022-10-23 thomas const struct got_error *err;
182 3efd8e31 2022-10-23 thomas const char *refname = got_ref_get_name(ref);
183 3efd8e31 2022-10-23 thomas size_t namelen;
184 3efd8e31 2022-10-23 thomas struct got_object_id *id = NULL;
185 3efd8e31 2022-10-23 thomas struct got_object *obj = NULL;
186 3efd8e31 2022-10-23 thomas size_t len;
187 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
189 3efd8e31 2022-10-23 thomas namelen = strlen(refname);
191 3efd8e31 2022-10-23 thomas len = sizeof(struct gotd_imsg_ref) + namelen;
192 3efd8e31 2022-10-23 thomas if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
193 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
195 3efd8e31 2022-10-23 thomas err = got_ref_resolve(&id, repo_write.repo, ref);
197 3efd8e31 2022-10-23 thomas return err;
199 3efd8e31 2022-10-23 thomas wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
200 3efd8e31 2022-10-23 thomas repo_write.pid, len);
201 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
202 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create REF");
203 3efd8e31 2022-10-23 thomas goto done;
206 3efd8e31 2022-10-23 thomas /* Keep in sync with struct gotd_imsg_ref definition. */
207 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
208 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF");
209 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
210 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF");
211 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, refname, namelen) == -1)
212 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF");
214 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
215 3efd8e31 2022-10-23 thomas imsg_close(ibuf, wbuf);
217 3efd8e31 2022-10-23 thomas err = got_object_open(&obj, repo_write.repo, id);
219 3efd8e31 2022-10-23 thomas goto done;
220 3efd8e31 2022-10-23 thomas if (obj->type == GOT_OBJ_TYPE_TAG)
221 3efd8e31 2022-10-23 thomas err = send_peeled_tag_ref(ref, obj, ibuf);
224 3efd8e31 2022-10-23 thomas got_object_close(obj);
225 3efd8e31 2022-10-23 thomas free(id);
226 3efd8e31 2022-10-23 thomas return err;
229 3efd8e31 2022-10-23 thomas static const struct got_error *
230 9148c8a7 2023-01-02 thomas list_refs(struct imsg *imsg)
232 3efd8e31 2022-10-23 thomas const struct got_error *err;
233 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
234 3efd8e31 2022-10-23 thomas struct got_reflist_head refs;
235 3efd8e31 2022-10-23 thomas struct got_reflist_entry *re;
236 3efd8e31 2022-10-23 thomas struct gotd_imsg_list_refs_internal ireq;
237 3efd8e31 2022-10-23 thomas size_t datalen;
238 3efd8e31 2022-10-23 thomas struct gotd_imsg_reflist irefs;
239 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
240 3efd8e31 2022-10-23 thomas int client_fd = imsg->fd;
242 3efd8e31 2022-10-23 thomas TAILQ_INIT(&refs);
244 3efd8e31 2022-10-23 thomas if (client_fd == -1)
245 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
247 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
248 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
249 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
250 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
252 9148c8a7 2023-01-02 thomas if (ireq.client_id == 0)
253 9148c8a7 2023-01-02 thomas return got_error(GOT_ERR_CLIENT_ID);
254 9148c8a7 2023-01-02 thomas if (client->id != 0) {
255 9148c8a7 2023-01-02 thomas return got_error_msg(GOT_ERR_CLIENT_ID,
256 9148c8a7 2023-01-02 thomas "duplicate list-refs request");
258 9148c8a7 2023-01-02 thomas client->id = ireq.client_id;
259 9148c8a7 2023-01-02 thomas client->fd = client_fd;
260 9148c8a7 2023-01-02 thomas client->pack_pipe = -1;
261 9148c8a7 2023-01-02 thomas client->packidx_fd = -1;
262 9148c8a7 2023-01-02 thomas client->nref_updates = 0;
263 49563dfb 2023-01-28 thomas client->nref_del = 0;
264 d98779cd 2023-01-19 thomas client->nref_new = 0;
266 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, client_fd);
268 3efd8e31 2022-10-23 thomas err = got_ref_list(&refs, repo_write.repo, "",
269 3efd8e31 2022-10-23 thomas got_ref_cmp_by_name, NULL);
271 3efd8e31 2022-10-23 thomas return err;
273 3efd8e31 2022-10-23 thomas memset(&irefs, 0, sizeof(irefs));
274 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(re, &refs, entry) {
275 3efd8e31 2022-10-23 thomas struct got_object_id *id;
276 3efd8e31 2022-10-23 thomas int obj_type;
278 3efd8e31 2022-10-23 thomas if (got_ref_is_symbolic(re->ref))
279 3efd8e31 2022-10-23 thomas continue;
281 3efd8e31 2022-10-23 thomas irefs.nrefs++;
283 3efd8e31 2022-10-23 thomas /* Account for a peeled tag refs. */
284 3efd8e31 2022-10-23 thomas err = got_ref_resolve(&id, repo_write.repo, re->ref);
286 3efd8e31 2022-10-23 thomas goto done;
287 8652e561 2023-01-14 thomas err = got_object_get_type(&obj_type, repo_write.repo, id);
288 3efd8e31 2022-10-23 thomas free(id);
290 3efd8e31 2022-10-23 thomas goto done;
291 3efd8e31 2022-10-23 thomas if (obj_type == GOT_OBJ_TYPE_TAG)
292 3efd8e31 2022-10-23 thomas irefs.nrefs++;
295 3efd8e31 2022-10-23 thomas if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
296 3efd8e31 2022-10-23 thomas repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
297 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_compose REFLIST");
298 3efd8e31 2022-10-23 thomas goto done;
301 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(re, &refs, entry) {
302 3efd8e31 2022-10-23 thomas if (got_ref_is_symbolic(re->ref))
303 3efd8e31 2022-10-23 thomas continue;
304 3efd8e31 2022-10-23 thomas err = send_ref(re->ref, &ibuf);
306 3efd8e31 2022-10-23 thomas goto done;
309 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(&ibuf);
311 3efd8e31 2022-10-23 thomas got_ref_list_free(&refs);
312 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
313 3efd8e31 2022-10-23 thomas return err;
316 3efd8e31 2022-10-23 thomas static const struct got_error *
317 3efd8e31 2022-10-23 thomas protect_ref_namespace(struct got_reference *ref, const char *namespace)
319 3efd8e31 2022-10-23 thomas size_t len = strlen(namespace);
321 3efd8e31 2022-10-23 thomas if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
322 3efd8e31 2022-10-23 thomas namespace[len -1] != '/') {
323 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_BAD_REF_NAME,
324 3efd8e31 2022-10-23 thomas "reference namespace '%s'", namespace);
327 3efd8e31 2022-10-23 thomas if (strncmp(namespace, got_ref_get_name(ref), len) == 0)
328 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
330 3efd8e31 2022-10-23 thomas return NULL;
333 3efd8e31 2022-10-23 thomas static const struct got_error *
334 9148c8a7 2023-01-02 thomas recv_ref_update(struct imsg *imsg)
336 49563dfb 2023-01-28 thomas static const char zero_id[SHA1_DIGEST_LENGTH];
337 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
338 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
339 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update iref;
340 3efd8e31 2022-10-23 thomas size_t datalen;
341 3efd8e31 2022-10-23 thomas char *refname = NULL;
342 3efd8e31 2022-10-23 thomas struct got_reference *ref = NULL;
343 3efd8e31 2022-10-23 thomas struct got_object_id *id = NULL;
344 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
345 3efd8e31 2022-10-23 thomas struct gotd_ref_update *ref_update = NULL;
347 3efd8e31 2022-10-23 thomas log_debug("ref-update received");
349 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
350 3efd8e31 2022-10-23 thomas if (datalen < sizeof(iref))
351 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
352 3efd8e31 2022-10-23 thomas memcpy(&iref, imsg->data, sizeof(iref));
353 3efd8e31 2022-10-23 thomas if (datalen != sizeof(iref) + iref.name_len)
354 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
356 9148c8a7 2023-01-02 thomas imsg_init(&ibuf, client->fd);
358 fcbb06bf 2023-01-14 thomas refname = strndup(imsg->data + sizeof(iref), iref.name_len);
359 3efd8e31 2022-10-23 thomas if (refname == NULL)
360 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
362 3efd8e31 2022-10-23 thomas ref_update = calloc(1, sizeof(*ref_update));
363 3efd8e31 2022-10-23 thomas if (ref_update == NULL) {
364 3efd8e31 2022-10-23 thomas err = got_error_from_errno("malloc");
365 3efd8e31 2022-10-23 thomas goto done;
368 3efd8e31 2022-10-23 thomas memcpy(ref_update->old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
369 3efd8e31 2022-10-23 thomas memcpy(ref_update->new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
371 3efd8e31 2022-10-23 thomas err = got_ref_open(&ref, repo_write.repo, refname, 0);
372 3efd8e31 2022-10-23 thomas if (err) {
373 3efd8e31 2022-10-23 thomas if (err->code != GOT_ERR_NOT_REF)
374 3efd8e31 2022-10-23 thomas goto done;
375 3efd8e31 2022-10-23 thomas err = got_ref_alloc(&ref, refname, &ref_update->new_id);
377 3efd8e31 2022-10-23 thomas goto done;
378 3efd8e31 2022-10-23 thomas ref_update->ref_is_new = 1;
379 d98779cd 2023-01-19 thomas client->nref_new++;
381 3efd8e31 2022-10-23 thomas if (got_ref_is_symbolic(ref)) {
382 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
383 3efd8e31 2022-10-23 thomas "'%s' is a symbolic reference and cannot "
384 3efd8e31 2022-10-23 thomas "be updated", got_ref_get_name(ref));
385 3efd8e31 2022-10-23 thomas goto done;
387 3efd8e31 2022-10-23 thomas if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
388 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
389 3efd8e31 2022-10-23 thomas "%s: does not begin with 'refs/'",
390 3efd8e31 2022-10-23 thomas got_ref_get_name(ref));
391 3efd8e31 2022-10-23 thomas goto done;
394 3efd8e31 2022-10-23 thomas err = protect_ref_namespace(ref, "refs/got/");
396 3efd8e31 2022-10-23 thomas goto done;
397 3efd8e31 2022-10-23 thomas err = protect_ref_namespace(ref, "refs/remotes/");
399 3efd8e31 2022-10-23 thomas goto done;
401 3efd8e31 2022-10-23 thomas if (!ref_update->ref_is_new) {
403 3efd8e31 2022-10-23 thomas * Ensure the client's idea of this update is still valid.
404 3efd8e31 2022-10-23 thomas * At this point we can only return an error, to prevent
405 3efd8e31 2022-10-23 thomas * the client from uploading a pack file which will likely
406 3efd8e31 2022-10-23 thomas * have to be discarded.
408 3efd8e31 2022-10-23 thomas err = got_ref_resolve(&id, repo_write.repo, ref);
410 3efd8e31 2022-10-23 thomas goto done;
412 3efd8e31 2022-10-23 thomas if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
413 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
414 3efd8e31 2022-10-23 thomas "%s has been modified by someone else "
415 3efd8e31 2022-10-23 thomas "while transaction was in progress",
416 3efd8e31 2022-10-23 thomas got_ref_get_name(ref));
417 3efd8e31 2022-10-23 thomas goto done;
421 3efd8e31 2022-10-23 thomas gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
422 3efd8e31 2022-10-23 thomas repo_write.pid);
424 3efd8e31 2022-10-23 thomas ref_update->ref = ref;
425 49563dfb 2023-01-28 thomas if (memcmp(ref_update->new_id.sha1, zero_id, sizeof(zero_id)) == 0) {
426 49563dfb 2023-01-28 thomas ref_update->delete_ref = 1;
427 49563dfb 2023-01-28 thomas client->nref_del++;
429 9148c8a7 2023-01-02 thomas STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
430 9148c8a7 2023-01-02 thomas client->nref_updates++;
431 3efd8e31 2022-10-23 thomas ref = NULL;
432 3efd8e31 2022-10-23 thomas ref_update = NULL;
435 3efd8e31 2022-10-23 thomas got_ref_close(ref);
436 3efd8e31 2022-10-23 thomas free(ref_update);
437 3efd8e31 2022-10-23 thomas free(refname);
438 3efd8e31 2022-10-23 thomas free(id);
439 3efd8e31 2022-10-23 thomas return err;
442 3efd8e31 2022-10-23 thomas static const struct got_error *
443 3efd8e31 2022-10-23 thomas pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
444 3efd8e31 2022-10-23 thomas uint32_t nobj_loose, uint32_t nobj_resolved)
446 3efd8e31 2022-10-23 thomas int p_indexed = 0, p_resolved = 0;
447 3efd8e31 2022-10-23 thomas int nobj_delta = nobj_total - nobj_loose;
449 3efd8e31 2022-10-23 thomas if (nobj_total > 0)
450 3efd8e31 2022-10-23 thomas p_indexed = (nobj_indexed * 100) / nobj_total;
452 3efd8e31 2022-10-23 thomas if (nobj_delta > 0)
453 3efd8e31 2022-10-23 thomas p_resolved = (nobj_resolved * 100) / nobj_delta;
455 3efd8e31 2022-10-23 thomas if (p_resolved > 0) {
456 3efd8e31 2022-10-23 thomas log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
457 3efd8e31 2022-10-23 thomas nobj_total, p_indexed, nobj_delta, p_resolved);
459 3efd8e31 2022-10-23 thomas log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
461 3efd8e31 2022-10-23 thomas return NULL;
464 3efd8e31 2022-10-23 thomas static const struct got_error *
465 3efd8e31 2022-10-23 thomas read_more_pack_stream(int infd, BUF *buf, size_t minsize)
467 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
468 3efd8e31 2022-10-23 thomas uint8_t readahead[65536];
469 3efd8e31 2022-10-23 thomas size_t have, newlen;
471 3efd8e31 2022-10-23 thomas err = got_poll_read_full(infd, &have,
472 3efd8e31 2022-10-23 thomas readahead, sizeof(readahead), minsize);
474 3efd8e31 2022-10-23 thomas return err;
476 3efd8e31 2022-10-23 thomas err = buf_append(&newlen, buf, readahead, have);
478 3efd8e31 2022-10-23 thomas return err;
479 8652e561 2023-01-14 thomas return NULL;
482 3efd8e31 2022-10-23 thomas static const struct got_error *
483 3efd8e31 2022-10-23 thomas copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
484 3efd8e31 2022-10-23 thomas off_t *outsize, BUF *buf, size_t *buf_pos, SHA1_CTX *ctx)
486 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
487 3efd8e31 2022-10-23 thomas uint8_t t = 0;
488 3efd8e31 2022-10-23 thomas uint64_t s = 0;
489 3efd8e31 2022-10-23 thomas uint8_t sizebuf[8];
490 3efd8e31 2022-10-23 thomas size_t i = 0;
491 3efd8e31 2022-10-23 thomas off_t obj_offset = *outsize;
494 3efd8e31 2022-10-23 thomas /* We do not support size values which don't fit in 64 bit. */
495 3efd8e31 2022-10-23 thomas if (i > 9)
496 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
497 fe3b5495 2022-10-25 thomas "packfile offset %lld", (long long)obj_offset);
499 3efd8e31 2022-10-23 thomas if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
500 3efd8e31 2022-10-23 thomas err = read_more_pack_stream(infd, buf,
501 3efd8e31 2022-10-23 thomas sizeof(sizebuf[0]));
503 3efd8e31 2022-10-23 thomas return err;
506 3efd8e31 2022-10-23 thomas sizebuf[i] = buf_getc(buf, *buf_pos);
507 3efd8e31 2022-10-23 thomas *buf_pos += sizeof(sizebuf[i]);
509 3efd8e31 2022-10-23 thomas if (i == 0) {
510 3efd8e31 2022-10-23 thomas t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
511 3efd8e31 2022-10-23 thomas GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
512 3efd8e31 2022-10-23 thomas s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
514 3efd8e31 2022-10-23 thomas size_t shift = 4 + 7 * (i - 1);
515 3efd8e31 2022-10-23 thomas s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
519 3efd8e31 2022-10-23 thomas } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
521 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(outfd, sizebuf, i, ctx);
523 3efd8e31 2022-10-23 thomas return err;
524 3efd8e31 2022-10-23 thomas *outsize += i;
526 3efd8e31 2022-10-23 thomas *type = t;
527 3efd8e31 2022-10-23 thomas *size = s;
528 3efd8e31 2022-10-23 thomas return NULL;
531 3efd8e31 2022-10-23 thomas static const struct got_error *
532 3efd8e31 2022-10-23 thomas copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
533 3efd8e31 2022-10-23 thomas SHA1_CTX *ctx)
535 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
536 3efd8e31 2022-10-23 thomas size_t remain = buf_len(buf) - *buf_pos;
538 3efd8e31 2022-10-23 thomas if (remain < SHA1_DIGEST_LENGTH) {
539 3efd8e31 2022-10-23 thomas err = read_more_pack_stream(infd, buf,
540 3efd8e31 2022-10-23 thomas SHA1_DIGEST_LENGTH - remain);
542 3efd8e31 2022-10-23 thomas return err;
545 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
546 3efd8e31 2022-10-23 thomas SHA1_DIGEST_LENGTH, ctx);
548 3efd8e31 2022-10-23 thomas return err;
550 3efd8e31 2022-10-23 thomas *buf_pos += SHA1_DIGEST_LENGTH;
551 3efd8e31 2022-10-23 thomas return NULL;
554 3efd8e31 2022-10-23 thomas static const struct got_error *
555 3efd8e31 2022-10-23 thomas copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
556 3efd8e31 2022-10-23 thomas SHA1_CTX *ctx)
558 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
559 3efd8e31 2022-10-23 thomas uint64_t o = 0;
560 3efd8e31 2022-10-23 thomas uint8_t offbuf[8];
561 3efd8e31 2022-10-23 thomas size_t i = 0;
562 3efd8e31 2022-10-23 thomas off_t obj_offset = *outsize;
565 3efd8e31 2022-10-23 thomas /* We do not support offset values which don't fit in 64 bit. */
566 3efd8e31 2022-10-23 thomas if (i > 8)
567 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
568 fe3b5495 2022-10-25 thomas "packfile offset %lld", (long long)obj_offset);
570 3efd8e31 2022-10-23 thomas if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
571 3efd8e31 2022-10-23 thomas err = read_more_pack_stream(infd, buf,
572 3efd8e31 2022-10-23 thomas sizeof(offbuf[0]));
574 3efd8e31 2022-10-23 thomas return err;
577 3efd8e31 2022-10-23 thomas offbuf[i] = buf_getc(buf, *buf_pos);
578 3efd8e31 2022-10-23 thomas *buf_pos += sizeof(offbuf[i]);
580 3efd8e31 2022-10-23 thomas if (i == 0)
581 3efd8e31 2022-10-23 thomas o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
585 3efd8e31 2022-10-23 thomas o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
588 3efd8e31 2022-10-23 thomas } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
590 3efd8e31 2022-10-23 thomas if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
591 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PACK_OFFSET);
593 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(outfd, offbuf, i, ctx);
595 3efd8e31 2022-10-23 thomas return err;
597 3efd8e31 2022-10-23 thomas *outsize += i;
598 3efd8e31 2022-10-23 thomas return NULL;
601 3efd8e31 2022-10-23 thomas static const struct got_error *
602 3efd8e31 2022-10-23 thomas copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
603 3efd8e31 2022-10-23 thomas SHA1_CTX *ctx)
605 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
606 3efd8e31 2022-10-23 thomas z_stream z;
607 3efd8e31 2022-10-23 thomas int zret;
608 3efd8e31 2022-10-23 thomas char voidbuf[1024];
609 3efd8e31 2022-10-23 thomas size_t consumed_total = 0;
610 3efd8e31 2022-10-23 thomas off_t zstream_offset = *outsize;
612 3efd8e31 2022-10-23 thomas memset(&z, 0, sizeof(z));
614 3efd8e31 2022-10-23 thomas z.zalloc = Z_NULL;
615 3efd8e31 2022-10-23 thomas z.zfree = Z_NULL;
616 3efd8e31 2022-10-23 thomas zret = inflateInit(&z);
617 3efd8e31 2022-10-23 thomas if (zret != Z_OK) {
618 3efd8e31 2022-10-23 thomas if (zret == Z_ERRNO)
619 3efd8e31 2022-10-23 thomas return got_error_from_errno("inflateInit");
620 3efd8e31 2022-10-23 thomas if (zret == Z_MEM_ERROR) {
621 3efd8e31 2022-10-23 thomas errno = ENOMEM;
622 3efd8e31 2022-10-23 thomas return got_error_from_errno("inflateInit");
624 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_DECOMPRESSION,
625 3efd8e31 2022-10-23 thomas "inflateInit failed");
628 3efd8e31 2022-10-23 thomas while (zret != Z_STREAM_END) {
629 3efd8e31 2022-10-23 thomas size_t last_total_in, consumed;
632 3efd8e31 2022-10-23 thomas * Decompress into the void. Object data will be parsed
633 3efd8e31 2022-10-23 thomas * later, when the pack file is indexed. For now, we just
634 3efd8e31 2022-10-23 thomas * want to locate the end of the compressed stream.
636 3efd8e31 2022-10-23 thomas while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
637 3efd8e31 2022-10-23 thomas last_total_in = z.total_in;
638 3efd8e31 2022-10-23 thomas z.next_in = buf_get(buf) + *buf_pos;
639 3efd8e31 2022-10-23 thomas z.avail_in = buf_len(buf) - *buf_pos;
640 3efd8e31 2022-10-23 thomas z.next_out = voidbuf;
641 3efd8e31 2022-10-23 thomas z.avail_out = sizeof(voidbuf);
643 3efd8e31 2022-10-23 thomas zret = inflate(&z, Z_SYNC_FLUSH);
644 3efd8e31 2022-10-23 thomas if (zret != Z_OK && zret != Z_BUF_ERROR &&
645 3efd8e31 2022-10-23 thomas zret != Z_STREAM_END) {
646 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_DECOMPRESSION,
647 fe3b5495 2022-10-25 thomas "packfile offset %lld",
648 fe3b5495 2022-10-25 thomas (long long)zstream_offset);
649 3efd8e31 2022-10-23 thomas goto done;
651 3efd8e31 2022-10-23 thomas consumed = z.total_in - last_total_in;
653 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
654 3efd8e31 2022-10-23 thomas consumed, ctx);
656 3efd8e31 2022-10-23 thomas goto done;
658 3efd8e31 2022-10-23 thomas err = buf_discard(buf, *buf_pos + consumed);
660 3efd8e31 2022-10-23 thomas goto done;
661 3efd8e31 2022-10-23 thomas *buf_pos = 0;
663 3efd8e31 2022-10-23 thomas consumed_total += consumed;
666 3efd8e31 2022-10-23 thomas if (zret != Z_STREAM_END) {
667 3efd8e31 2022-10-23 thomas err = read_more_pack_stream(infd, buf, 1);
669 3efd8e31 2022-10-23 thomas goto done;
673 3efd8e31 2022-10-23 thomas if (err == NULL)
674 3efd8e31 2022-10-23 thomas *outsize += consumed_total;
676 3efd8e31 2022-10-23 thomas inflateEnd(&z);
677 3efd8e31 2022-10-23 thomas return err;
680 3efd8e31 2022-10-23 thomas static const struct got_error *
681 3efd8e31 2022-10-23 thomas validate_object_type(int obj_type)
683 3efd8e31 2022-10-23 thomas switch (obj_type) {
684 3efd8e31 2022-10-23 thomas case GOT_OBJ_TYPE_BLOB:
685 3efd8e31 2022-10-23 thomas case GOT_OBJ_TYPE_COMMIT:
686 3efd8e31 2022-10-23 thomas case GOT_OBJ_TYPE_TREE:
687 3efd8e31 2022-10-23 thomas case GOT_OBJ_TYPE_TAG:
688 3efd8e31 2022-10-23 thomas case GOT_OBJ_TYPE_REF_DELTA:
689 3efd8e31 2022-10-23 thomas case GOT_OBJ_TYPE_OFFSET_DELTA:
690 3efd8e31 2022-10-23 thomas return NULL;
695 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_OBJ_TYPE);
698 3efd8e31 2022-10-23 thomas static const struct got_error *
699 d98779cd 2023-01-19 thomas recv_packdata(off_t *outsize, uint32_t *nobj, uint8_t *sha1,
700 d98779cd 2023-01-19 thomas int infd, int outfd)
702 3efd8e31 2022-10-23 thomas const struct got_error *err;
703 d98779cd 2023-01-19 thomas struct repo_write_client *client = &repo_write_client;
704 3efd8e31 2022-10-23 thomas struct got_packfile_hdr hdr;
705 3efd8e31 2022-10-23 thomas size_t have;
706 d98779cd 2023-01-19 thomas uint32_t nhave = 0;
707 3efd8e31 2022-10-23 thomas SHA1_CTX ctx;
708 3efd8e31 2022-10-23 thomas uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
709 3efd8e31 2022-10-23 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
710 3efd8e31 2022-10-23 thomas BUF *buf = NULL;
711 3efd8e31 2022-10-23 thomas size_t buf_pos = 0, remain;
712 3efd8e31 2022-10-23 thomas ssize_t w;
714 3efd8e31 2022-10-23 thomas *outsize = 0;
715 d98779cd 2023-01-19 thomas *nobj = 0;
717 49563dfb 2023-01-28 thomas /* if only deleting references there's nothing to read */
718 49563dfb 2023-01-28 thomas if (client->nref_updates == client->nref_del)
719 49563dfb 2023-01-28 thomas return NULL;
721 3efd8e31 2022-10-23 thomas SHA1Init(&ctx);
723 3efd8e31 2022-10-23 thomas err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
725 3efd8e31 2022-10-23 thomas return err;
726 3efd8e31 2022-10-23 thomas if (have != sizeof(hdr))
727 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
728 3efd8e31 2022-10-23 thomas *outsize += have;
730 3efd8e31 2022-10-23 thomas if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
731 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE,
732 3efd8e31 2022-10-23 thomas "bad packfile signature");
733 3efd8e31 2022-10-23 thomas if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
734 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE,
735 3efd8e31 2022-10-23 thomas "bad packfile version");
737 d98779cd 2023-01-19 thomas *nobj = be32toh(hdr.nobjects);
738 d98779cd 2023-01-19 thomas if (*nobj == 0) {
740 d98779cd 2023-01-19 thomas * Clients which are creating new references only
741 d98779cd 2023-01-19 thomas * will send us an empty pack file.
743 d98779cd 2023-01-19 thomas if (client->nref_updates > 0 &&
744 d98779cd 2023-01-19 thomas client->nref_updates == client->nref_new)
745 d98779cd 2023-01-19 thomas return NULL;
747 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE,
748 3efd8e31 2022-10-23 thomas "bad packfile with zero objects");
751 d98779cd 2023-01-19 thomas log_debug("expecting %d objects", *nobj);
753 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
755 3efd8e31 2022-10-23 thomas return err;
757 3efd8e31 2022-10-23 thomas err = buf_alloc(&buf, 65536);
759 3efd8e31 2022-10-23 thomas return err;
761 d98779cd 2023-01-19 thomas while (nhave != *nobj) {
762 3efd8e31 2022-10-23 thomas uint8_t obj_type;
763 3efd8e31 2022-10-23 thomas uint64_t obj_size;
765 3efd8e31 2022-10-23 thomas err = copy_object_type_and_size(&obj_type, &obj_size,
766 3efd8e31 2022-10-23 thomas infd, outfd, outsize, buf, &buf_pos, &ctx);
768 3efd8e31 2022-10-23 thomas goto done;
770 3efd8e31 2022-10-23 thomas err = validate_object_type(obj_type);
772 3efd8e31 2022-10-23 thomas goto done;
774 3efd8e31 2022-10-23 thomas if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
775 3efd8e31 2022-10-23 thomas err = copy_ref_delta(infd, outfd, outsize,
776 3efd8e31 2022-10-23 thomas buf, &buf_pos, &ctx);
778 3efd8e31 2022-10-23 thomas goto done;
779 3efd8e31 2022-10-23 thomas } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
780 3efd8e31 2022-10-23 thomas err = copy_offset_delta(infd, outfd, outsize,
781 3efd8e31 2022-10-23 thomas buf, &buf_pos, &ctx);
783 3efd8e31 2022-10-23 thomas goto done;
786 3efd8e31 2022-10-23 thomas err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
788 3efd8e31 2022-10-23 thomas goto done;
793 d98779cd 2023-01-19 thomas log_debug("received %u objects", *nobj);
795 3efd8e31 2022-10-23 thomas SHA1Final(expected_sha1, &ctx);
797 3efd8e31 2022-10-23 thomas remain = buf_len(buf) - buf_pos;
798 3efd8e31 2022-10-23 thomas if (remain < SHA1_DIGEST_LENGTH) {
799 3efd8e31 2022-10-23 thomas err = read_more_pack_stream(infd, buf,
800 3efd8e31 2022-10-23 thomas SHA1_DIGEST_LENGTH - remain);
802 3efd8e31 2022-10-23 thomas return err;
805 3efd8e31 2022-10-23 thomas got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
806 3efd8e31 2022-10-23 thomas log_debug("expect SHA1: %s", hex);
807 3efd8e31 2022-10-23 thomas got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
808 3efd8e31 2022-10-23 thomas log_debug("actual SHA1: %s", hex);
810 3efd8e31 2022-10-23 thomas if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
811 3efd8e31 2022-10-23 thomas SHA1_DIGEST_LENGTH) != 0) {
812 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PACKFILE_CSUM);
813 3efd8e31 2022-10-23 thomas goto done;
816 3efd8e31 2022-10-23 thomas memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
818 3efd8e31 2022-10-23 thomas w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
819 3efd8e31 2022-10-23 thomas if (w == -1) {
820 3efd8e31 2022-10-23 thomas err = got_error_from_errno("write");
821 3efd8e31 2022-10-23 thomas goto done;
823 3efd8e31 2022-10-23 thomas if (w != SHA1_DIGEST_LENGTH) {
824 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_IO);
825 3efd8e31 2022-10-23 thomas goto done;
828 3efd8e31 2022-10-23 thomas *outsize += SHA1_DIGEST_LENGTH;
830 3efd8e31 2022-10-23 thomas if (fsync(outfd) == -1) {
831 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fsync");
832 3efd8e31 2022-10-23 thomas goto done;
834 3efd8e31 2022-10-23 thomas if (lseek(outfd, 0L, SEEK_SET) == -1) {
835 3efd8e31 2022-10-23 thomas err = got_error_from_errno("lseek");
836 3efd8e31 2022-10-23 thomas goto done;
839 3efd8e31 2022-10-23 thomas buf_free(buf);
840 3efd8e31 2022-10-23 thomas return err;
843 3efd8e31 2022-10-23 thomas static const struct got_error *
844 9148c8a7 2023-01-02 thomas report_pack_status(const struct got_error *unpack_err)
846 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
847 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
848 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_status istatus;
849 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
850 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
851 3efd8e31 2022-10-23 thomas const char *unpack_ok = "unpack ok\n";
852 3efd8e31 2022-10-23 thomas size_t len;
854 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, client->fd);
856 3efd8e31 2022-10-23 thomas if (unpack_err)
857 3efd8e31 2022-10-23 thomas istatus.reason_len = strlen(unpack_err->msg);
859 3efd8e31 2022-10-23 thomas istatus.reason_len = strlen(unpack_ok);
861 3efd8e31 2022-10-23 thomas len = sizeof(istatus) + istatus.reason_len;
862 3efd8e31 2022-10-23 thomas wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
863 3efd8e31 2022-10-23 thomas repo_write.pid, len);
864 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
865 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create PACKFILE_STATUS");
866 3efd8e31 2022-10-23 thomas goto done;
869 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
870 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add PACKFILE_STATUS");
871 3efd8e31 2022-10-23 thomas goto done;
874 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, err ? err->msg : unpack_ok,
875 3efd8e31 2022-10-23 thomas istatus.reason_len) == -1) {
876 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add PACKFILE_STATUS");
877 3efd8e31 2022-10-23 thomas goto done;
880 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
881 3efd8e31 2022-10-23 thomas imsg_close(&ibuf, wbuf);
883 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(&ibuf);
885 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
886 3efd8e31 2022-10-23 thomas return err;
889 3efd8e31 2022-10-23 thomas static const struct got_error *
890 d98779cd 2023-01-19 thomas recv_packfile(int *have_packfile, struct imsg *imsg)
892 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL, *unpack_err;
893 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
894 3efd8e31 2022-10-23 thomas struct gotd_imsg_recv_packfile ireq;
895 3efd8e31 2022-10-23 thomas FILE *tempfiles[3] = { NULL, NULL, NULL };
896 3efd8e31 2022-10-23 thomas struct repo_tempfile {
899 3efd8e31 2022-10-23 thomas } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
901 3efd8e31 2022-10-23 thomas size_t datalen;
902 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
903 3efd8e31 2022-10-23 thomas struct got_ratelimit rl;
904 3efd8e31 2022-10-23 thomas struct got_pack *pack = NULL;
905 3efd8e31 2022-10-23 thomas off_t pack_filesize = 0;
906 d98779cd 2023-01-19 thomas uint32_t nobj = 0;
908 3efd8e31 2022-10-23 thomas log_debug("packfile request received");
910 d98779cd 2023-01-19 thomas *have_packfile = 0;
911 3efd8e31 2022-10-23 thomas got_ratelimit_init(&rl, 2, 0);
913 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
914 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
915 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
916 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
918 9148c8a7 2023-01-02 thomas if (client->pack_pipe == -1 || client->packidx_fd == -1)
919 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
921 9148c8a7 2023-01-02 thomas imsg_init(&ibuf, client->fd);
923 3efd8e31 2022-10-23 thomas if (imsg->fd == -1)
924 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
926 9148c8a7 2023-01-02 thomas pack = &client->pack;
927 3efd8e31 2022-10-23 thomas memset(pack, 0, sizeof(*pack));
928 3efd8e31 2022-10-23 thomas pack->fd = imsg->fd;
929 3efd8e31 2022-10-23 thomas err = got_delta_cache_alloc(&pack->delta_cache);
931 3efd8e31 2022-10-23 thomas return err;
933 3efd8e31 2022-10-23 thomas for (i = 0; i < nitems(repo_tempfiles); i++) {
934 3efd8e31 2022-10-23 thomas struct repo_tempfile *t = &repo_tempfiles[i];
935 3efd8e31 2022-10-23 thomas err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
937 3efd8e31 2022-10-23 thomas goto done;
940 3efd8e31 2022-10-23 thomas for (i = 0; i < nitems(tempfiles); i++) {
944 da2c57e4 2023-02-03 thomas fd = dup(repo_tempfiles[i].fd);
945 3efd8e31 2022-10-23 thomas if (fd == -1) {
946 3efd8e31 2022-10-23 thomas err = got_error_from_errno("dup");
947 3efd8e31 2022-10-23 thomas goto done;
949 3efd8e31 2022-10-23 thomas f = fdopen(fd, "w+");
950 3efd8e31 2022-10-23 thomas if (f == NULL) {
951 da2c57e4 2023-02-03 thomas err = got_error_from_errno("fdopen");
952 3efd8e31 2022-10-23 thomas close(fd);
953 3efd8e31 2022-10-23 thomas goto done;
955 3efd8e31 2022-10-23 thomas tempfiles[i] = f;
958 3efd8e31 2022-10-23 thomas /* Send pack file pipe to gotsh(1). */
959 3efd8e31 2022-10-23 thomas if (imsg_compose(&ibuf, GOTD_IMSG_RECV_PACKFILE, PROC_REPO_WRITE,
960 3efd8e31 2022-10-23 thomas repo_write.pid, (*client)->pack_pipe[1], NULL, 0) == -1) {
961 3efd8e31 2022-10-23 thomas (*client)->pack_pipe[1] = -1;
962 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_compose ACK");
964 3efd8e31 2022-10-23 thomas goto done;
966 3efd8e31 2022-10-23 thomas (*client)->pack_pipe[1] = -1;
967 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(&ibuf);
969 3efd8e31 2022-10-23 thomas goto done;
971 3efd8e31 2022-10-23 thomas log_debug("receiving pack data");
972 d98779cd 2023-01-19 thomas unpack_err = recv_packdata(&pack_filesize, &nobj,
973 d98779cd 2023-01-19 thomas client->pack_sha1, client->pack_pipe, pack->fd);
974 3efd8e31 2022-10-23 thomas if (ireq.report_status) {
975 9148c8a7 2023-01-02 thomas err = report_pack_status(unpack_err);
976 3efd8e31 2022-10-23 thomas if (err) {
977 3efd8e31 2022-10-23 thomas /* Git clients hang up after sending the pack file. */
978 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_EOF)
979 3efd8e31 2022-10-23 thomas err = NULL;
982 3efd8e31 2022-10-23 thomas if (unpack_err)
983 3efd8e31 2022-10-23 thomas err = unpack_err;
985 3efd8e31 2022-10-23 thomas goto done;
987 3efd8e31 2022-10-23 thomas log_debug("pack data received");
990 d98779cd 2023-01-19 thomas * Clients which are creating new references only will
991 d98779cd 2023-01-19 thomas * send us an empty pack file.
993 d98779cd 2023-01-19 thomas if (nobj == 0 &&
994 d98779cd 2023-01-19 thomas pack_filesize == sizeof(struct got_packfile_hdr) &&
995 d98779cd 2023-01-19 thomas client->nref_updates > 0 &&
996 d98779cd 2023-01-19 thomas client->nref_updates == client->nref_new)
997 d98779cd 2023-01-19 thomas goto done;
1000 49563dfb 2023-01-28 thomas * Clients which are deleting references only will send
1001 49563dfb 2023-01-28 thomas * no pack file.
1003 49563dfb 2023-01-28 thomas if (nobj == 0 &&
1004 49563dfb 2023-01-28 thomas client->nref_del > 0 &&
1005 49563dfb 2023-01-28 thomas client->nref_updates == client->nref_del)
1006 49563dfb 2023-01-28 thomas goto done;
1008 3efd8e31 2022-10-23 thomas pack->filesize = pack_filesize;
1009 d98779cd 2023-01-19 thomas *have_packfile = 1;
1011 66e6097f 2022-10-27 thomas log_debug("begin indexing pack (%lld bytes in size)",
1012 66e6097f 2022-10-27 thomas (long long)pack->filesize);
1013 9148c8a7 2023-01-02 thomas err = got_pack_index(pack, client->packidx_fd,
1014 9148c8a7 2023-01-02 thomas tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
1015 3efd8e31 2022-10-23 thomas pack_index_progress, NULL, &rl);
1016 3efd8e31 2022-10-23 thomas if (err)
1017 3efd8e31 2022-10-23 thomas goto done;
1018 3efd8e31 2022-10-23 thomas log_debug("done indexing pack");
1020 9148c8a7 2023-01-02 thomas if (fsync(client->packidx_fd) == -1) {
1021 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fsync");
1022 3efd8e31 2022-10-23 thomas goto done;
1024 9148c8a7 2023-01-02 thomas if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1025 3efd8e31 2022-10-23 thomas err = got_error_from_errno("lseek");
1027 9148c8a7 2023-01-02 thomas if (close(client->pack_pipe) == -1 && err == NULL)
1028 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
1029 9148c8a7 2023-01-02 thomas client->pack_pipe = -1;
1030 3efd8e31 2022-10-23 thomas for (i = 0; i < nitems(repo_tempfiles); i++) {
1031 3efd8e31 2022-10-23 thomas struct repo_tempfile *t = &repo_tempfiles[i];
1032 3efd8e31 2022-10-23 thomas if (t->idx != -1)
1033 3efd8e31 2022-10-23 thomas got_repo_temp_fds_put(t->idx, repo_write.repo);
1035 3efd8e31 2022-10-23 thomas for (i = 0; i < nitems(tempfiles); i++) {
1036 3efd8e31 2022-10-23 thomas if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1037 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fclose");
1039 3efd8e31 2022-10-23 thomas if (err)
1040 3efd8e31 2022-10-23 thomas got_pack_close(pack);
1041 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
1042 3efd8e31 2022-10-23 thomas return err;
1045 3efd8e31 2022-10-23 thomas static const struct got_error *
1046 9148c8a7 2023-01-02 thomas verify_packfile(void)
1048 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL, *close_err;
1049 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
1050 3efd8e31 2022-10-23 thomas struct gotd_ref_update *ref_update;
1051 3efd8e31 2022-10-23 thomas struct got_packidx *packidx = NULL;
1052 3efd8e31 2022-10-23 thomas struct stat sb;
1053 3efd8e31 2022-10-23 thomas char *id_str = NULL;
1054 3efd8e31 2022-10-23 thomas int idx = -1;
1056 3efd8e31 2022-10-23 thomas if (STAILQ_EMPTY(&client->ref_updates)) {
1057 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
1058 3efd8e31 2022-10-23 thomas "cannot verify pack file without any ref-updates");
1061 3efd8e31 2022-10-23 thomas if (client->pack.fd == -1) {
1062 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
1063 3efd8e31 2022-10-23 thomas "invalid pack file handle during pack verification");
1065 3efd8e31 2022-10-23 thomas if (client->packidx_fd == -1) {
1066 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
1067 3efd8e31 2022-10-23 thomas "invalid pack index handle during pack verification");
1070 3efd8e31 2022-10-23 thomas if (fstat(client->packidx_fd, &sb) == -1)
1071 3efd8e31 2022-10-23 thomas return got_error_from_errno("pack index fstat");
1073 3efd8e31 2022-10-23 thomas packidx = malloc(sizeof(*packidx));
1074 3efd8e31 2022-10-23 thomas memset(packidx, 0, sizeof(*packidx));
1075 3efd8e31 2022-10-23 thomas packidx->fd = client->packidx_fd;
1076 3efd8e31 2022-10-23 thomas client->packidx_fd = -1;
1077 3efd8e31 2022-10-23 thomas packidx->len = sb.st_size;
1079 3efd8e31 2022-10-23 thomas err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1080 3efd8e31 2022-10-23 thomas if (err)
1081 3efd8e31 2022-10-23 thomas return err;
1083 3efd8e31 2022-10-23 thomas STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1084 49563dfb 2023-01-28 thomas if (ref_update->delete_ref)
1085 49563dfb 2023-01-28 thomas continue;
1087 3efd8e31 2022-10-23 thomas err = got_object_id_str(&id_str, &ref_update->new_id);
1088 3efd8e31 2022-10-23 thomas if (err)
1089 3efd8e31 2022-10-23 thomas goto done;
1091 3efd8e31 2022-10-23 thomas idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1092 3efd8e31 2022-10-23 thomas if (idx == -1) {
1093 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1094 3efd8e31 2022-10-23 thomas "advertised object %s is missing from pack file",
1095 3efd8e31 2022-10-23 thomas id_str);
1096 3efd8e31 2022-10-23 thomas goto done;
1101 3efd8e31 2022-10-23 thomas close_err = got_packidx_close(packidx);
1102 3efd8e31 2022-10-23 thomas if (close_err && err == NULL)
1103 3efd8e31 2022-10-23 thomas err = close_err;
1104 3efd8e31 2022-10-23 thomas free(id_str);
1105 3efd8e31 2022-10-23 thomas return err;
1108 3efd8e31 2022-10-23 thomas static const struct got_error *
1109 9148c8a7 2023-01-02 thomas install_packfile(struct gotd_imsgev *iev)
1111 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
1112 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_install inst;
1113 3efd8e31 2022-10-23 thomas int ret;
1115 3efd8e31 2022-10-23 thomas memset(&inst, 0, sizeof(inst));
1116 3efd8e31 2022-10-23 thomas inst.client_id = client->id;
1117 3efd8e31 2022-10-23 thomas memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1119 3efd8e31 2022-10-23 thomas ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1120 3efd8e31 2022-10-23 thomas PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1121 3efd8e31 2022-10-23 thomas if (ret == -1)
1122 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1124 3efd8e31 2022-10-23 thomas return NULL;
1127 3efd8e31 2022-10-23 thomas static const struct got_error *
1128 9148c8a7 2023-01-02 thomas send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1130 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
1131 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_updates_start istart;
1132 3efd8e31 2022-10-23 thomas int ret;
1134 3efd8e31 2022-10-23 thomas memset(&istart, 0, sizeof(istart));
1135 3efd8e31 2022-10-23 thomas istart.nref_updates = nref_updates;
1136 3efd8e31 2022-10-23 thomas istart.client_id = client->id;
1138 3efd8e31 2022-10-23 thomas ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1139 3efd8e31 2022-10-23 thomas PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1140 3efd8e31 2022-10-23 thomas if (ret == -1)
1141 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_compose REF_UPDATES_START");
1143 3efd8e31 2022-10-23 thomas return NULL;
1147 3efd8e31 2022-10-23 thomas static const struct got_error *
1148 9148c8a7 2023-01-02 thomas send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1150 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
1151 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update iref;
1152 3efd8e31 2022-10-23 thomas const char *refname = got_ref_get_name(ref_update->ref);
1153 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
1154 3efd8e31 2022-10-23 thomas size_t len;
1156 3efd8e31 2022-10-23 thomas memset(&iref, 0, sizeof(iref));
1157 3efd8e31 2022-10-23 thomas memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1158 3efd8e31 2022-10-23 thomas memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1159 3efd8e31 2022-10-23 thomas iref.ref_is_new = ref_update->ref_is_new;
1160 49563dfb 2023-01-28 thomas iref.delete_ref = ref_update->delete_ref;
1161 3efd8e31 2022-10-23 thomas iref.client_id = client->id;
1162 3efd8e31 2022-10-23 thomas iref.name_len = strlen(refname);
1164 3efd8e31 2022-10-23 thomas len = sizeof(iref) + iref.name_len;
1165 3efd8e31 2022-10-23 thomas wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1166 3efd8e31 2022-10-23 thomas repo_write.pid, len);
1167 3efd8e31 2022-10-23 thomas if (wbuf == NULL)
1168 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_create REF_UPDATE");
1170 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1171 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE");
1172 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, refname, iref.name_len) == -1)
1173 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE");
1175 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
1176 3efd8e31 2022-10-23 thomas imsg_close(&iev->ibuf, wbuf);
1178 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(iev);
1179 3efd8e31 2022-10-23 thomas return NULL;
1182 3efd8e31 2022-10-23 thomas static const struct got_error *
1183 9148c8a7 2023-01-02 thomas update_refs(struct gotd_imsgev *iev)
1185 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1186 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
1187 3efd8e31 2022-10-23 thomas struct gotd_ref_update *ref_update;
1189 9148c8a7 2023-01-02 thomas err = send_ref_updates_start(client->nref_updates, iev);
1190 3efd8e31 2022-10-23 thomas if (err)
1191 3efd8e31 2022-10-23 thomas return err;
1193 3efd8e31 2022-10-23 thomas STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1194 9148c8a7 2023-01-02 thomas err = send_ref_update(ref_update, iev);
1195 3efd8e31 2022-10-23 thomas if (err)
1196 3efd8e31 2022-10-23 thomas goto done;
1199 3efd8e31 2022-10-23 thomas return err;
1202 3efd8e31 2022-10-23 thomas static const struct got_error *
1203 3efd8e31 2022-10-23 thomas recv_disconnect(struct imsg *imsg)
1205 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1206 3efd8e31 2022-10-23 thomas struct gotd_imsg_disconnect idisconnect;
1207 3efd8e31 2022-10-23 thomas size_t datalen;
1208 9148c8a7 2023-01-02 thomas int pack_pipe = -1, idxfd = -1;
1209 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
1211 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1212 3efd8e31 2022-10-23 thomas if (datalen != sizeof(idisconnect))
1213 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1214 3efd8e31 2022-10-23 thomas memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
1216 3efd8e31 2022-10-23 thomas log_debug("client disconnecting");
1218 3efd8e31 2022-10-23 thomas while (!STAILQ_EMPTY(&client->ref_updates)) {
1219 3efd8e31 2022-10-23 thomas struct gotd_ref_update *ref_update;
1220 3efd8e31 2022-10-23 thomas ref_update = STAILQ_FIRST(&client->ref_updates);
1221 3efd8e31 2022-10-23 thomas STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1222 3efd8e31 2022-10-23 thomas got_ref_close(ref_update->ref);
1223 3efd8e31 2022-10-23 thomas free(ref_update);
1225 3efd8e31 2022-10-23 thomas err = got_pack_close(&client->pack);
1226 9148c8a7 2023-01-02 thomas if (client->fd != -1 && close(client->fd) == -1)
1227 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
1228 9148c8a7 2023-01-02 thomas pack_pipe = client->pack_pipe;
1229 9148c8a7 2023-01-02 thomas if (pack_pipe != -1 && close(pack_pipe) == -1 && err == NULL)
1230 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
1231 9148c8a7 2023-01-02 thomas idxfd = client->packidx_fd;
1232 3efd8e31 2022-10-23 thomas if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1233 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
1234 3efd8e31 2022-10-23 thomas return err;
1237 3efd8e31 2022-10-23 thomas static const struct got_error *
1238 9148c8a7 2023-01-02 thomas receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1240 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
1241 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_pipe ireq;
1242 3efd8e31 2022-10-23 thomas size_t datalen;
1244 3efd8e31 2022-10-23 thomas log_debug("receving pack pipe descriptor");
1246 3efd8e31 2022-10-23 thomas if (imsg->fd == -1)
1247 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1249 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1250 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
1251 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1252 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
1254 9148c8a7 2023-01-02 thomas if (client->pack_pipe != -1)
1255 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1257 9148c8a7 2023-01-02 thomas client->pack_pipe = imsg->fd;
1258 3efd8e31 2022-10-23 thomas return NULL;
1261 3efd8e31 2022-10-23 thomas static const struct got_error *
1262 9148c8a7 2023-01-02 thomas receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1264 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
1265 3efd8e31 2022-10-23 thomas struct gotd_imsg_packidx_file ireq;
1266 3efd8e31 2022-10-23 thomas size_t datalen;
1268 3efd8e31 2022-10-23 thomas log_debug("receving pack index output file");
1270 3efd8e31 2022-10-23 thomas if (imsg->fd == -1)
1271 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1273 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1274 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
1275 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1276 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
1278 9148c8a7 2023-01-02 thomas if (client->packidx_fd != -1)
1279 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1281 9148c8a7 2023-01-02 thomas client->packidx_fd = imsg->fd;
1282 3efd8e31 2022-10-23 thomas return NULL;
1285 3efd8e31 2022-10-23 thomas static void
1286 62ee7d94 2023-01-10 thomas repo_write_dispatch_session(int fd, short event, void *arg)
1288 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1289 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev = arg;
1290 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf = &iev->ibuf;
1291 3efd8e31 2022-10-23 thomas struct imsg imsg;
1292 9148c8a7 2023-01-02 thomas struct repo_write_client *client = &repo_write_client;
1293 3efd8e31 2022-10-23 thomas ssize_t n;
1294 d98779cd 2023-01-19 thomas int shut = 0, have_packfile = 0;
1296 3efd8e31 2022-10-23 thomas if (event & EV_READ) {
1297 3efd8e31 2022-10-23 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1298 3efd8e31 2022-10-23 thomas fatal("imsg_read error");
1299 3efd8e31 2022-10-23 thomas if (n == 0) /* Connection closed. */
1300 3efd8e31 2022-10-23 thomas shut = 1;
1303 3efd8e31 2022-10-23 thomas if (event & EV_WRITE) {
1304 3efd8e31 2022-10-23 thomas n = msgbuf_write(&ibuf->w);
1305 3efd8e31 2022-10-23 thomas if (n == -1 && errno != EAGAIN)
1306 3efd8e31 2022-10-23 thomas fatal("msgbuf_write");
1307 3efd8e31 2022-10-23 thomas if (n == 0) /* Connection closed. */
1308 3efd8e31 2022-10-23 thomas shut = 1;
1311 3efd8e31 2022-10-23 thomas for (;;) {
1312 3efd8e31 2022-10-23 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1313 3efd8e31 2022-10-23 thomas fatal("%s: imsg_get error", __func__);
1314 3efd8e31 2022-10-23 thomas if (n == 0) /* No more messages. */
1317 9148c8a7 2023-01-02 thomas if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
1318 9148c8a7 2023-01-02 thomas client->id == 0) {
1319 9148c8a7 2023-01-02 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1323 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
1324 3efd8e31 2022-10-23 thomas case GOTD_IMSG_LIST_REFS_INTERNAL:
1325 9148c8a7 2023-01-02 thomas err = list_refs(&imsg);
1326 3efd8e31 2022-10-23 thomas if (err)
1327 3efd8e31 2022-10-23 thomas log_warnx("%s: ls-refs: %s", repo_write.title,
1328 3efd8e31 2022-10-23 thomas err->msg);
1330 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF_UPDATE:
1331 9148c8a7 2023-01-02 thomas err = recv_ref_update(&imsg);
1332 3efd8e31 2022-10-23 thomas if (err)
1333 3efd8e31 2022-10-23 thomas log_warnx("%s: ref-update: %s",
1334 3efd8e31 2022-10-23 thomas repo_write.title, err->msg);
1336 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_PIPE:
1337 9148c8a7 2023-01-02 thomas err = receive_pack_pipe(&imsg, iev);
1338 3efd8e31 2022-10-23 thomas if (err) {
1339 3efd8e31 2022-10-23 thomas log_warnx("%s: receiving pack pipe: %s",
1340 3efd8e31 2022-10-23 thomas repo_write.title, err->msg);
1344 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKIDX_FILE:
1345 9148c8a7 2023-01-02 thomas err = receive_pack_idx(&imsg, iev);
1346 3efd8e31 2022-10-23 thomas if (err) {
1347 3efd8e31 2022-10-23 thomas log_warnx("%s: receiving pack index: %s",
1348 3efd8e31 2022-10-23 thomas repo_write.title, err->msg);
1352 3efd8e31 2022-10-23 thomas case GOTD_IMSG_RECV_PACKFILE:
1353 d98779cd 2023-01-19 thomas err = recv_packfile(&have_packfile, &imsg);
1354 3efd8e31 2022-10-23 thomas if (err) {
1355 3efd8e31 2022-10-23 thomas log_warnx("%s: receive packfile: %s",
1356 3efd8e31 2022-10-23 thomas repo_write.title, err->msg);
1359 d98779cd 2023-01-19 thomas if (have_packfile) {
1360 d98779cd 2023-01-19 thomas err = verify_packfile();
1361 d98779cd 2023-01-19 thomas if (err) {
1362 d98779cd 2023-01-19 thomas log_warnx("%s: verify packfile: %s",
1363 d98779cd 2023-01-19 thomas repo_write.title, err->msg);
1366 d98779cd 2023-01-19 thomas err = install_packfile(iev);
1367 d98779cd 2023-01-19 thomas if (err) {
1368 d98779cd 2023-01-19 thomas log_warnx("%s: install packfile: %s",
1369 d98779cd 2023-01-19 thomas repo_write.title, err->msg);
1373 9148c8a7 2023-01-02 thomas err = update_refs(iev);
1374 3efd8e31 2022-10-23 thomas if (err) {
1375 3efd8e31 2022-10-23 thomas log_warnx("%s: update refs: %s",
1376 3efd8e31 2022-10-23 thomas repo_write.title, err->msg);
1379 3efd8e31 2022-10-23 thomas case GOTD_IMSG_DISCONNECT:
1380 3efd8e31 2022-10-23 thomas err = recv_disconnect(&imsg);
1381 3efd8e31 2022-10-23 thomas if (err)
1382 3efd8e31 2022-10-23 thomas log_warnx("%s: disconnect: %s",
1383 3efd8e31 2022-10-23 thomas repo_write.title, err->msg);
1384 62ee7d94 2023-01-10 thomas shut = 1;
1386 62ee7d94 2023-01-10 thomas default:
1387 62ee7d94 2023-01-10 thomas log_debug("%s: unexpected imsg %d", repo_write.title,
1388 62ee7d94 2023-01-10 thomas imsg.hdr.type);
1392 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
1395 62ee7d94 2023-01-10 thomas if (!shut && check_cancelled(NULL) == NULL) {
1396 62ee7d94 2023-01-10 thomas if (err &&
1397 62ee7d94 2023-01-10 thomas gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1398 62ee7d94 2023-01-10 thomas client->id, err) == -1) {
1399 62ee7d94 2023-01-10 thomas log_warnx("could not send error to parent: %s",
1400 62ee7d94 2023-01-10 thomas err->msg);
1402 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
1403 62ee7d94 2023-01-10 thomas } else {
1404 62ee7d94 2023-01-10 thomas /* This pipe is dead. Remove its event handler */
1405 62ee7d94 2023-01-10 thomas event_del(&iev->ev);
1406 62ee7d94 2023-01-10 thomas event_loopexit(NULL);
1410 62ee7d94 2023-01-10 thomas static const struct got_error *
1411 62ee7d94 2023-01-10 thomas recv_connect(struct imsg *imsg)
1413 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = &repo_write.session_iev;
1414 62ee7d94 2023-01-10 thomas size_t datalen;
1416 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1417 62ee7d94 2023-01-10 thomas if (datalen != 0)
1418 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1419 62ee7d94 2023-01-10 thomas if (imsg->fd == -1)
1420 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1422 62ee7d94 2023-01-10 thomas if (repo_write.session_fd != -1)
1423 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1425 62ee7d94 2023-01-10 thomas repo_write.session_fd = imsg->fd;
1427 62ee7d94 2023-01-10 thomas imsg_init(&iev->ibuf, repo_write.session_fd);
1428 62ee7d94 2023-01-10 thomas iev->handler = repo_write_dispatch_session;
1429 62ee7d94 2023-01-10 thomas iev->events = EV_READ;
1430 62ee7d94 2023-01-10 thomas iev->handler_arg = NULL;
1431 62ee7d94 2023-01-10 thomas event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1432 62ee7d94 2023-01-10 thomas repo_write_dispatch_session, iev);
1433 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
1435 62ee7d94 2023-01-10 thomas return NULL;
1438 62ee7d94 2023-01-10 thomas static void
1439 62ee7d94 2023-01-10 thomas repo_write_dispatch(int fd, short event, void *arg)
1441 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1442 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
1443 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
1444 62ee7d94 2023-01-10 thomas struct imsg imsg;
1445 62ee7d94 2023-01-10 thomas ssize_t n;
1446 62ee7d94 2023-01-10 thomas int shut = 0;
1447 62ee7d94 2023-01-10 thomas struct repo_write_client *client = &repo_write_client;
1449 62ee7d94 2023-01-10 thomas if (event & EV_READ) {
1450 62ee7d94 2023-01-10 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1451 62ee7d94 2023-01-10 thomas fatal("imsg_read error");
1452 62ee7d94 2023-01-10 thomas if (n == 0) /* Connection closed. */
1453 9148c8a7 2023-01-02 thomas shut = 1;
1456 62ee7d94 2023-01-10 thomas if (event & EV_WRITE) {
1457 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
1458 62ee7d94 2023-01-10 thomas if (n == -1 && errno != EAGAIN)
1459 62ee7d94 2023-01-10 thomas fatal("msgbuf_write");
1460 62ee7d94 2023-01-10 thomas if (n == 0) /* Connection closed. */
1461 62ee7d94 2023-01-10 thomas shut = 1;
1464 62ee7d94 2023-01-10 thomas while (err == NULL && check_cancelled(NULL) == NULL) {
1465 62ee7d94 2023-01-10 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1466 62ee7d94 2023-01-10 thomas fatal("%s: imsg_get", __func__);
1467 62ee7d94 2023-01-10 thomas if (n == 0) /* No more messages. */
1470 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
1471 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CONNECT_REPO_CHILD:
1472 62ee7d94 2023-01-10 thomas err = recv_connect(&imsg);
1474 3efd8e31 2022-10-23 thomas default:
1475 3efd8e31 2022-10-23 thomas log_debug("%s: unexpected imsg %d", repo_write.title,
1476 3efd8e31 2022-10-23 thomas imsg.hdr.type);
1480 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
1483 3efd8e31 2022-10-23 thomas if (!shut && check_cancelled(NULL) == NULL) {
1484 3efd8e31 2022-10-23 thomas if (err &&
1485 3efd8e31 2022-10-23 thomas gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1486 9148c8a7 2023-01-02 thomas client->id, err) == -1) {
1487 3efd8e31 2022-10-23 thomas log_warnx("could not send error to parent: %s",
1488 3efd8e31 2022-10-23 thomas err->msg);
1490 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(iev);
1491 3efd8e31 2022-10-23 thomas } else {
1492 3efd8e31 2022-10-23 thomas /* This pipe is dead. Remove its event handler */
1493 3efd8e31 2022-10-23 thomas event_del(&iev->ev);
1494 3efd8e31 2022-10-23 thomas event_loopexit(NULL);
1499 414e37cb 2022-12-30 thomas repo_write_main(const char *title, const char *repo_path,
1500 414e37cb 2022-12-30 thomas int *pack_fds, int *temp_fds)
1502 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1503 3efd8e31 2022-10-23 thomas struct gotd_imsgev iev;
1505 3efd8e31 2022-10-23 thomas repo_write.title = title;
1506 3efd8e31 2022-10-23 thomas repo_write.pid = getpid();
1507 3efd8e31 2022-10-23 thomas repo_write.pack_fds = pack_fds;
1508 3efd8e31 2022-10-23 thomas repo_write.temp_fds = temp_fds;
1509 62ee7d94 2023-01-10 thomas repo_write.session_fd = -1;
1510 62ee7d94 2023-01-10 thomas repo_write.session_iev.ibuf.fd = -1;
1512 9148c8a7 2023-01-02 thomas STAILQ_INIT(&repo_write_client.ref_updates);
1514 414e37cb 2022-12-30 thomas err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
1515 3efd8e31 2022-10-23 thomas if (err)
1516 3efd8e31 2022-10-23 thomas goto done;
1517 3efd8e31 2022-10-23 thomas if (!got_repo_is_bare(repo_write.repo)) {
1518 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1519 3efd8e31 2022-10-23 thomas "bare git repository required");
1520 3efd8e31 2022-10-23 thomas goto done;
1523 3efd8e31 2022-10-23 thomas got_repo_temp_fds_set(repo_write.repo, temp_fds);
1525 3efd8e31 2022-10-23 thomas signal(SIGINT, catch_sigint);
1526 3efd8e31 2022-10-23 thomas signal(SIGTERM, catch_sigterm);
1527 3efd8e31 2022-10-23 thomas signal(SIGPIPE, SIG_IGN);
1528 3efd8e31 2022-10-23 thomas signal(SIGHUP, SIG_IGN);
1530 bb3a6ce9 2022-11-17 thomas imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
1531 3efd8e31 2022-10-23 thomas iev.handler = repo_write_dispatch;
1532 3efd8e31 2022-10-23 thomas iev.events = EV_READ;
1533 3efd8e31 2022-10-23 thomas iev.handler_arg = NULL;
1534 3efd8e31 2022-10-23 thomas event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1535 85b37c72 2022-12-30 thomas if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
1536 85b37c72 2022-12-30 thomas PROC_REPO_WRITE, -1, NULL, 0) == -1) {
1537 85b37c72 2022-12-30 thomas err = got_error_from_errno("imsg compose REPO_CHILD_READY");
1538 3efd8e31 2022-10-23 thomas goto done;
1541 3efd8e31 2022-10-23 thomas event_dispatch();
1543 3efd8e31 2022-10-23 thomas if (err)
1544 3efd8e31 2022-10-23 thomas log_warnx("%s: %s", title, err->msg);
1545 3efd8e31 2022-10-23 thomas repo_write_shutdown();
1549 3efd8e31 2022-10-23 thomas repo_write_shutdown(void)
1551 3efd8e31 2022-10-23 thomas log_debug("%s: shutting down", repo_write.title);
1552 3efd8e31 2022-10-23 thomas if (repo_write.repo)
1553 3efd8e31 2022-10-23 thomas got_repo_close(repo_write.repo);
1554 3efd8e31 2022-10-23 thomas got_repo_pack_fds_close(repo_write.pack_fds);
1555 80536967 2022-10-30 thomas got_repo_temp_fds_close(repo_write.temp_fds);
1556 62ee7d94 2023-01-10 thomas if (repo_write.session_fd != -1)
1557 62ee7d94 2023-01-10 thomas close(repo_write.session_fd);
1558 3efd8e31 2022-10-23 thomas exit(0);