Blame


1 3efd8e31 2022-10-23 thomas /*
2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 3efd8e31 2022-10-23 thomas *
4 3efd8e31 2022-10-23 thomas * Permission to use, copy, modify, and distribute this software for any
5 3efd8e31 2022-10-23 thomas * purpose with or without fee is hereby granted, provided that the above
6 3efd8e31 2022-10-23 thomas * copyright notice and this permission notice appear in all copies.
7 3efd8e31 2022-10-23 thomas *
8 3efd8e31 2022-10-23 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3efd8e31 2022-10-23 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3efd8e31 2022-10-23 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3efd8e31 2022-10-23 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3efd8e31 2022-10-23 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3efd8e31 2022-10-23 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3efd8e31 2022-10-23 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 3efd8e31 2022-10-23 thomas */
16 3efd8e31 2022-10-23 thomas
17 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
18 3efd8e31 2022-10-23 thomas #include <sys/stat.h>
19 3efd8e31 2022-10-23 thomas #include <sys/tree.h>
20 3efd8e31 2022-10-23 thomas #include <sys/types.h>
21 3efd8e31 2022-10-23 thomas
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>
35 3efd8e31 2022-10-23 thomas
36 3efd8e31 2022-10-23 thomas #include "buf.h"
37 3efd8e31 2022-10-23 thomas
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"
43 3efd8e31 2022-10-23 thomas
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"
53 3efd8e31 2022-10-23 thomas
54 3efd8e31 2022-10-23 thomas #include "got_lib_sha1.h" /* XXX temp include for debugging */
55 3efd8e31 2022-10-23 thomas
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"
59 3efd8e31 2022-10-23 thomas
60 3efd8e31 2022-10-23 thomas #ifndef nitems
61 3efd8e31 2022-10-23 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 3efd8e31 2022-10-23 thomas #endif
63 3efd8e31 2022-10-23 thomas
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 3efd8e31 2022-10-23 thomas } repo_write;
71 3efd8e31 2022-10-23 thomas
72 3efd8e31 2022-10-23 thomas struct gotd_ref_update {
73 3efd8e31 2022-10-23 thomas STAILQ_ENTRY(gotd_ref_update) entry;
74 3efd8e31 2022-10-23 thomas struct got_reference *ref;
75 3efd8e31 2022-10-23 thomas int ref_is_new;
76 3efd8e31 2022-10-23 thomas struct got_object_id old_id;
77 3efd8e31 2022-10-23 thomas struct got_object_id new_id;
78 3efd8e31 2022-10-23 thomas };
79 3efd8e31 2022-10-23 thomas STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
80 3efd8e31 2022-10-23 thomas
81 3efd8e31 2022-10-23 thomas struct repo_write_client {
82 3efd8e31 2022-10-23 thomas STAILQ_ENTRY(repo_write_client) entry;
83 3efd8e31 2022-10-23 thomas uint32_t id;
84 3efd8e31 2022-10-23 thomas int fd;
85 3efd8e31 2022-10-23 thomas int pack_pipe[2];
86 3efd8e31 2022-10-23 thomas struct got_pack pack;
87 3efd8e31 2022-10-23 thomas uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
88 3efd8e31 2022-10-23 thomas int packidx_fd;
89 3efd8e31 2022-10-23 thomas struct gotd_ref_updates ref_updates;
90 3efd8e31 2022-10-23 thomas int nref_updates;
91 3efd8e31 2022-10-23 thomas };
92 3efd8e31 2022-10-23 thomas STAILQ_HEAD(repo_write_clients, repo_write_client);
93 3efd8e31 2022-10-23 thomas
94 3efd8e31 2022-10-23 thomas static struct repo_write_clients repo_write_clients[GOTD_CLIENT_TABLE_SIZE];
95 3efd8e31 2022-10-23 thomas static SIPHASH_KEY clients_hash_key;
96 3efd8e31 2022-10-23 thomas
97 3efd8e31 2022-10-23 thomas static uint64_t
98 3efd8e31 2022-10-23 thomas client_hash(uint32_t client_id)
99 3efd8e31 2022-10-23 thomas {
100 3efd8e31 2022-10-23 thomas return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
101 3efd8e31 2022-10-23 thomas }
102 3efd8e31 2022-10-23 thomas
103 3efd8e31 2022-10-23 thomas static void
104 3efd8e31 2022-10-23 thomas add_client(struct repo_write_client *client, uint32_t client_id, int fd)
105 3efd8e31 2022-10-23 thomas {
106 3efd8e31 2022-10-23 thomas uint64_t slot;
107 3efd8e31 2022-10-23 thomas
108 3efd8e31 2022-10-23 thomas client->id = client_id;
109 3efd8e31 2022-10-23 thomas client->fd = fd;
110 3efd8e31 2022-10-23 thomas client->pack_pipe[0] = -1;
111 3efd8e31 2022-10-23 thomas client->pack_pipe[1] = -1;
112 3efd8e31 2022-10-23 thomas client->packidx_fd = -1;
113 3efd8e31 2022-10-23 thomas STAILQ_INIT(&client->ref_updates);
114 3efd8e31 2022-10-23 thomas client->nref_updates = 0;
115 3efd8e31 2022-10-23 thomas slot = client_hash(client->id) % nitems(repo_write_clients);
116 3efd8e31 2022-10-23 thomas STAILQ_INSERT_HEAD(&repo_write_clients[slot], client, entry);
117 3efd8e31 2022-10-23 thomas }
118 3efd8e31 2022-10-23 thomas
119 3efd8e31 2022-10-23 thomas static struct repo_write_client *
120 3efd8e31 2022-10-23 thomas find_client(uint32_t client_id)
121 3efd8e31 2022-10-23 thomas {
122 3efd8e31 2022-10-23 thomas uint64_t slot;
123 3efd8e31 2022-10-23 thomas struct repo_write_client *c;
124 3efd8e31 2022-10-23 thomas
125 3efd8e31 2022-10-23 thomas slot = client_hash(client_id) % nitems(repo_write_clients);
126 3efd8e31 2022-10-23 thomas STAILQ_FOREACH(c, &repo_write_clients[slot], entry) {
127 3efd8e31 2022-10-23 thomas if (c->id == client_id)
128 3efd8e31 2022-10-23 thomas return c;
129 3efd8e31 2022-10-23 thomas }
130 3efd8e31 2022-10-23 thomas
131 3efd8e31 2022-10-23 thomas return NULL;
132 3efd8e31 2022-10-23 thomas }
133 3efd8e31 2022-10-23 thomas
134 3efd8e31 2022-10-23 thomas static volatile sig_atomic_t sigint_received;
135 3efd8e31 2022-10-23 thomas static volatile sig_atomic_t sigterm_received;
136 3efd8e31 2022-10-23 thomas
137 3efd8e31 2022-10-23 thomas static void
138 3efd8e31 2022-10-23 thomas catch_sigint(int signo)
139 3efd8e31 2022-10-23 thomas {
140 3efd8e31 2022-10-23 thomas sigint_received = 1;
141 3efd8e31 2022-10-23 thomas }
142 3efd8e31 2022-10-23 thomas
143 3efd8e31 2022-10-23 thomas static void
144 3efd8e31 2022-10-23 thomas catch_sigterm(int signo)
145 3efd8e31 2022-10-23 thomas {
146 3efd8e31 2022-10-23 thomas sigterm_received = 1;
147 3efd8e31 2022-10-23 thomas }
148 3efd8e31 2022-10-23 thomas
149 3efd8e31 2022-10-23 thomas static const struct got_error *
150 3efd8e31 2022-10-23 thomas check_cancelled(void *arg)
151 3efd8e31 2022-10-23 thomas {
152 3efd8e31 2022-10-23 thomas if (sigint_received || sigterm_received)
153 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_CANCELLED);
154 3efd8e31 2022-10-23 thomas
155 3efd8e31 2022-10-23 thomas return NULL;
156 3efd8e31 2022-10-23 thomas }
157 3efd8e31 2022-10-23 thomas
158 3efd8e31 2022-10-23 thomas static const struct got_error *
159 3efd8e31 2022-10-23 thomas send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
160 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf)
161 3efd8e31 2022-10-23 thomas {
162 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
163 3efd8e31 2022-10-23 thomas struct got_tag_object *tag;
164 3efd8e31 2022-10-23 thomas size_t namelen, len;
165 3efd8e31 2022-10-23 thomas char *peeled_refname = NULL;
166 3efd8e31 2022-10-23 thomas struct got_object_id *id;
167 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
168 3efd8e31 2022-10-23 thomas
169 3efd8e31 2022-10-23 thomas err = got_object_tag_open(&tag, repo_write.repo, obj);
170 3efd8e31 2022-10-23 thomas if (err)
171 3efd8e31 2022-10-23 thomas return err;
172 3efd8e31 2022-10-23 thomas
173 3efd8e31 2022-10-23 thomas if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
174 3efd8e31 2022-10-23 thomas err = got_error_from_errno("asprintf");
175 3efd8e31 2022-10-23 thomas goto done;
176 3efd8e31 2022-10-23 thomas }
177 3efd8e31 2022-10-23 thomas
178 3efd8e31 2022-10-23 thomas id = got_object_tag_get_object_id(tag);
179 3efd8e31 2022-10-23 thomas namelen = strlen(peeled_refname);
180 3efd8e31 2022-10-23 thomas
181 3efd8e31 2022-10-23 thomas len = sizeof(struct gotd_imsg_ref) + namelen;
182 3efd8e31 2022-10-23 thomas if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
183 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_NO_SPACE);
184 3efd8e31 2022-10-23 thomas goto done;
185 3efd8e31 2022-10-23 thomas }
186 3efd8e31 2022-10-23 thomas
187 3efd8e31 2022-10-23 thomas wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
188 3efd8e31 2022-10-23 thomas repo_write.pid, len);
189 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
190 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create REF");
191 3efd8e31 2022-10-23 thomas goto done;
192 3efd8e31 2022-10-23 thomas }
193 3efd8e31 2022-10-23 thomas
194 3efd8e31 2022-10-23 thomas /* Keep in sync with struct gotd_imsg_ref definition. */
195 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
196 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add REF");
197 3efd8e31 2022-10-23 thomas goto done;
198 3efd8e31 2022-10-23 thomas }
199 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
200 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add REF");
201 3efd8e31 2022-10-23 thomas goto done;
202 3efd8e31 2022-10-23 thomas }
203 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
204 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add REF");
205 3efd8e31 2022-10-23 thomas goto done;
206 3efd8e31 2022-10-23 thomas }
207 3efd8e31 2022-10-23 thomas
208 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
209 3efd8e31 2022-10-23 thomas imsg_close(ibuf, wbuf);
210 3efd8e31 2022-10-23 thomas done:
211 3efd8e31 2022-10-23 thomas got_object_tag_close(tag);
212 3efd8e31 2022-10-23 thomas return err;
213 3efd8e31 2022-10-23 thomas }
214 3efd8e31 2022-10-23 thomas
215 3efd8e31 2022-10-23 thomas static const struct got_error *
216 3efd8e31 2022-10-23 thomas send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
217 3efd8e31 2022-10-23 thomas {
218 3efd8e31 2022-10-23 thomas const struct got_error *err;
219 3efd8e31 2022-10-23 thomas const char *refname = got_ref_get_name(ref);
220 3efd8e31 2022-10-23 thomas size_t namelen;
221 3efd8e31 2022-10-23 thomas struct got_object_id *id = NULL;
222 3efd8e31 2022-10-23 thomas struct got_object *obj = NULL;
223 3efd8e31 2022-10-23 thomas size_t len;
224 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
225 3efd8e31 2022-10-23 thomas
226 3efd8e31 2022-10-23 thomas namelen = strlen(refname);
227 3efd8e31 2022-10-23 thomas
228 3efd8e31 2022-10-23 thomas len = sizeof(struct gotd_imsg_ref) + namelen;
229 3efd8e31 2022-10-23 thomas if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
230 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
231 3efd8e31 2022-10-23 thomas
232 3efd8e31 2022-10-23 thomas err = got_ref_resolve(&id, repo_write.repo, ref);
233 3efd8e31 2022-10-23 thomas if (err)
234 3efd8e31 2022-10-23 thomas return err;
235 3efd8e31 2022-10-23 thomas
236 3efd8e31 2022-10-23 thomas wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
237 3efd8e31 2022-10-23 thomas repo_write.pid, len);
238 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
239 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create REF");
240 3efd8e31 2022-10-23 thomas goto done;
241 3efd8e31 2022-10-23 thomas }
242 3efd8e31 2022-10-23 thomas
243 3efd8e31 2022-10-23 thomas /* Keep in sync with struct gotd_imsg_ref definition. */
244 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1)
245 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF");
246 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
247 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF");
248 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, refname, namelen) == -1)
249 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF");
250 3efd8e31 2022-10-23 thomas
251 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
252 3efd8e31 2022-10-23 thomas imsg_close(ibuf, wbuf);
253 3efd8e31 2022-10-23 thomas
254 3efd8e31 2022-10-23 thomas err = got_object_open(&obj, repo_write.repo, id);
255 3efd8e31 2022-10-23 thomas if (err)
256 3efd8e31 2022-10-23 thomas goto done;
257 3efd8e31 2022-10-23 thomas if (obj->type == GOT_OBJ_TYPE_TAG)
258 3efd8e31 2022-10-23 thomas err = send_peeled_tag_ref(ref, obj, ibuf);
259 3efd8e31 2022-10-23 thomas done:
260 3efd8e31 2022-10-23 thomas if (obj)
261 3efd8e31 2022-10-23 thomas got_object_close(obj);
262 3efd8e31 2022-10-23 thomas free(id);
263 3efd8e31 2022-10-23 thomas return err;
264 3efd8e31 2022-10-23 thomas }
265 3efd8e31 2022-10-23 thomas
266 3efd8e31 2022-10-23 thomas static const struct got_error *
267 3efd8e31 2022-10-23 thomas list_refs(struct repo_write_client **client, struct imsg *imsg)
268 3efd8e31 2022-10-23 thomas {
269 3efd8e31 2022-10-23 thomas const struct got_error *err;
270 3efd8e31 2022-10-23 thomas struct got_reflist_head refs;
271 3efd8e31 2022-10-23 thomas struct got_reflist_entry *re;
272 3efd8e31 2022-10-23 thomas struct gotd_imsg_list_refs_internal ireq;
273 3efd8e31 2022-10-23 thomas size_t datalen;
274 3efd8e31 2022-10-23 thomas struct gotd_imsg_reflist irefs;
275 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
276 3efd8e31 2022-10-23 thomas int client_fd = imsg->fd;
277 3efd8e31 2022-10-23 thomas
278 3efd8e31 2022-10-23 thomas TAILQ_INIT(&refs);
279 3efd8e31 2022-10-23 thomas
280 3efd8e31 2022-10-23 thomas if (client_fd == -1)
281 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
282 3efd8e31 2022-10-23 thomas
283 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
284 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
285 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
286 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
287 3efd8e31 2022-10-23 thomas
288 3efd8e31 2022-10-23 thomas *client = find_client(ireq.client_id);
289 3efd8e31 2022-10-23 thomas if (*client)
290 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
291 3efd8e31 2022-10-23 thomas
292 3efd8e31 2022-10-23 thomas *client = calloc(1, sizeof(**client));
293 3efd8e31 2022-10-23 thomas if (*client == NULL)
294 3efd8e31 2022-10-23 thomas return got_error_from_errno("calloc");
295 3efd8e31 2022-10-23 thomas add_client(*client, ireq.client_id, client_fd);
296 3efd8e31 2022-10-23 thomas
297 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, client_fd);
298 3efd8e31 2022-10-23 thomas
299 3efd8e31 2022-10-23 thomas err = got_ref_list(&refs, repo_write.repo, "",
300 3efd8e31 2022-10-23 thomas got_ref_cmp_by_name, NULL);
301 3efd8e31 2022-10-23 thomas if (err)
302 3efd8e31 2022-10-23 thomas return err;
303 3efd8e31 2022-10-23 thomas
304 3efd8e31 2022-10-23 thomas memset(&irefs, 0, sizeof(irefs));
305 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(re, &refs, entry) {
306 3efd8e31 2022-10-23 thomas struct got_object_id *id;
307 3efd8e31 2022-10-23 thomas int obj_type;
308 3efd8e31 2022-10-23 thomas
309 3efd8e31 2022-10-23 thomas if (got_ref_is_symbolic(re->ref))
310 3efd8e31 2022-10-23 thomas continue;
311 3efd8e31 2022-10-23 thomas
312 3efd8e31 2022-10-23 thomas irefs.nrefs++;
313 3efd8e31 2022-10-23 thomas
314 3efd8e31 2022-10-23 thomas /* Account for a peeled tag refs. */
315 3efd8e31 2022-10-23 thomas err = got_ref_resolve(&id, repo_write.repo, re->ref);
316 3efd8e31 2022-10-23 thomas if (err)
317 3efd8e31 2022-10-23 thomas goto done;
318 3efd8e31 2022-10-23 thomas err = got_object_get_type(&obj_type, repo_write.repo, id);
319 3efd8e31 2022-10-23 thomas free(id);
320 3efd8e31 2022-10-23 thomas if (err)
321 3efd8e31 2022-10-23 thomas goto done;
322 3efd8e31 2022-10-23 thomas if (obj_type == GOT_OBJ_TYPE_TAG)
323 3efd8e31 2022-10-23 thomas irefs.nrefs++;
324 3efd8e31 2022-10-23 thomas }
325 3efd8e31 2022-10-23 thomas
326 3efd8e31 2022-10-23 thomas if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
327 3efd8e31 2022-10-23 thomas repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
328 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_compose REFLIST");
329 3efd8e31 2022-10-23 thomas goto done;
330 3efd8e31 2022-10-23 thomas }
331 3efd8e31 2022-10-23 thomas
332 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(re, &refs, entry) {
333 3efd8e31 2022-10-23 thomas if (got_ref_is_symbolic(re->ref))
334 3efd8e31 2022-10-23 thomas continue;
335 3efd8e31 2022-10-23 thomas err = send_ref(re->ref, &ibuf);
336 3efd8e31 2022-10-23 thomas if (err)
337 3efd8e31 2022-10-23 thomas goto done;
338 3efd8e31 2022-10-23 thomas }
339 3efd8e31 2022-10-23 thomas
340 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(&ibuf);
341 3efd8e31 2022-10-23 thomas done:
342 3efd8e31 2022-10-23 thomas got_ref_list_free(&refs);
343 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
344 3efd8e31 2022-10-23 thomas return err;
345 3efd8e31 2022-10-23 thomas }
346 3efd8e31 2022-10-23 thomas
347 3efd8e31 2022-10-23 thomas static const struct got_error *
348 3efd8e31 2022-10-23 thomas protect_ref_namespace(struct got_reference *ref, const char *namespace)
349 3efd8e31 2022-10-23 thomas {
350 3efd8e31 2022-10-23 thomas size_t len = strlen(namespace);
351 3efd8e31 2022-10-23 thomas
352 3efd8e31 2022-10-23 thomas if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
353 3efd8e31 2022-10-23 thomas namespace[len -1] != '/') {
354 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_BAD_REF_NAME,
355 3efd8e31 2022-10-23 thomas "reference namespace '%s'", namespace);
356 3efd8e31 2022-10-23 thomas }
357 3efd8e31 2022-10-23 thomas
358 3efd8e31 2022-10-23 thomas if (strncmp(namespace, got_ref_get_name(ref), len) == 0)
359 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
360 3efd8e31 2022-10-23 thomas
361 3efd8e31 2022-10-23 thomas return NULL;
362 3efd8e31 2022-10-23 thomas }
363 3efd8e31 2022-10-23 thomas
364 3efd8e31 2022-10-23 thomas static const struct got_error *
365 3efd8e31 2022-10-23 thomas recv_ref_update(struct repo_write_client **client, struct imsg *imsg)
366 3efd8e31 2022-10-23 thomas {
367 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
368 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update iref;
369 3efd8e31 2022-10-23 thomas size_t datalen;
370 3efd8e31 2022-10-23 thomas char *refname = NULL;
371 3efd8e31 2022-10-23 thomas struct got_reference *ref = NULL;
372 3efd8e31 2022-10-23 thomas struct got_object_id *id = NULL;
373 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
374 3efd8e31 2022-10-23 thomas struct gotd_ref_update *ref_update = NULL;
375 3efd8e31 2022-10-23 thomas
376 3efd8e31 2022-10-23 thomas log_debug("ref-update received");
377 3efd8e31 2022-10-23 thomas
378 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
379 3efd8e31 2022-10-23 thomas if (datalen < sizeof(iref))
380 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
381 3efd8e31 2022-10-23 thomas memcpy(&iref, imsg->data, sizeof(iref));
382 3efd8e31 2022-10-23 thomas if (datalen != sizeof(iref) + iref.name_len)
383 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
384 3efd8e31 2022-10-23 thomas
385 3efd8e31 2022-10-23 thomas *client = find_client(iref.client_id);
386 3efd8e31 2022-10-23 thomas if (*client == NULL)
387 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_CLIENT_ID);
388 3efd8e31 2022-10-23 thomas
389 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, (*client)->fd);
390 3efd8e31 2022-10-23 thomas
391 3efd8e31 2022-10-23 thomas refname = malloc(iref.name_len + 1);
392 3efd8e31 2022-10-23 thomas if (refname == NULL)
393 3efd8e31 2022-10-23 thomas return got_error_from_errno("malloc");
394 3efd8e31 2022-10-23 thomas memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
395 3efd8e31 2022-10-23 thomas refname[iref.name_len] = '\0';
396 3efd8e31 2022-10-23 thomas
397 3efd8e31 2022-10-23 thomas ref_update = calloc(1, sizeof(*ref_update));
398 3efd8e31 2022-10-23 thomas if (ref_update == NULL) {
399 3efd8e31 2022-10-23 thomas err = got_error_from_errno("malloc");
400 3efd8e31 2022-10-23 thomas goto done;
401 3efd8e31 2022-10-23 thomas }
402 3efd8e31 2022-10-23 thomas
403 3efd8e31 2022-10-23 thomas memcpy(ref_update->old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
404 3efd8e31 2022-10-23 thomas memcpy(ref_update->new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
405 3efd8e31 2022-10-23 thomas
406 3efd8e31 2022-10-23 thomas err = got_ref_open(&ref, repo_write.repo, refname, 0);
407 3efd8e31 2022-10-23 thomas if (err) {
408 3efd8e31 2022-10-23 thomas if (err->code != GOT_ERR_NOT_REF)
409 3efd8e31 2022-10-23 thomas goto done;
410 3efd8e31 2022-10-23 thomas err = got_ref_alloc(&ref, refname, &ref_update->new_id);
411 3efd8e31 2022-10-23 thomas if (err)
412 3efd8e31 2022-10-23 thomas goto done;
413 3efd8e31 2022-10-23 thomas ref_update->ref_is_new = 1;
414 3efd8e31 2022-10-23 thomas }
415 3efd8e31 2022-10-23 thomas if (got_ref_is_symbolic(ref)) {
416 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
417 3efd8e31 2022-10-23 thomas "'%s' is a symbolic reference and cannot "
418 3efd8e31 2022-10-23 thomas "be updated", got_ref_get_name(ref));
419 3efd8e31 2022-10-23 thomas goto done;
420 3efd8e31 2022-10-23 thomas }
421 3efd8e31 2022-10-23 thomas if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
422 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
423 3efd8e31 2022-10-23 thomas "%s: does not begin with 'refs/'",
424 3efd8e31 2022-10-23 thomas got_ref_get_name(ref));
425 3efd8e31 2022-10-23 thomas goto done;
426 3efd8e31 2022-10-23 thomas }
427 3efd8e31 2022-10-23 thomas
428 3efd8e31 2022-10-23 thomas err = protect_ref_namespace(ref, "refs/got/");
429 3efd8e31 2022-10-23 thomas if (err)
430 3efd8e31 2022-10-23 thomas goto done;
431 3efd8e31 2022-10-23 thomas err = protect_ref_namespace(ref, "refs/remotes/");
432 3efd8e31 2022-10-23 thomas if (err)
433 3efd8e31 2022-10-23 thomas goto done;
434 3efd8e31 2022-10-23 thomas
435 3efd8e31 2022-10-23 thomas if (!ref_update->ref_is_new) {
436 3efd8e31 2022-10-23 thomas /*
437 3efd8e31 2022-10-23 thomas * Ensure the client's idea of this update is still valid.
438 3efd8e31 2022-10-23 thomas * At this point we can only return an error, to prevent
439 3efd8e31 2022-10-23 thomas * the client from uploading a pack file which will likely
440 3efd8e31 2022-10-23 thomas * have to be discarded.
441 3efd8e31 2022-10-23 thomas */
442 3efd8e31 2022-10-23 thomas err = got_ref_resolve(&id, repo_write.repo, ref);
443 3efd8e31 2022-10-23 thomas if (err)
444 3efd8e31 2022-10-23 thomas goto done;
445 3efd8e31 2022-10-23 thomas
446 3efd8e31 2022-10-23 thomas if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
447 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
448 3efd8e31 2022-10-23 thomas "%s has been modified by someone else "
449 3efd8e31 2022-10-23 thomas "while transaction was in progress",
450 3efd8e31 2022-10-23 thomas got_ref_get_name(ref));
451 3efd8e31 2022-10-23 thomas goto done;
452 3efd8e31 2022-10-23 thomas }
453 3efd8e31 2022-10-23 thomas }
454 3efd8e31 2022-10-23 thomas
455 3efd8e31 2022-10-23 thomas gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
456 3efd8e31 2022-10-23 thomas repo_write.pid);
457 3efd8e31 2022-10-23 thomas
458 3efd8e31 2022-10-23 thomas ref_update->ref = ref;
459 3efd8e31 2022-10-23 thomas STAILQ_INSERT_HEAD(&(*client)->ref_updates, ref_update, entry);
460 3efd8e31 2022-10-23 thomas (*client)->nref_updates++;
461 3efd8e31 2022-10-23 thomas ref = NULL;
462 3efd8e31 2022-10-23 thomas ref_update = NULL;
463 3efd8e31 2022-10-23 thomas done:
464 3efd8e31 2022-10-23 thomas if (ref)
465 3efd8e31 2022-10-23 thomas got_ref_close(ref);
466 3efd8e31 2022-10-23 thomas free(ref_update);
467 3efd8e31 2022-10-23 thomas free(refname);
468 3efd8e31 2022-10-23 thomas free(id);
469 3efd8e31 2022-10-23 thomas return err;
470 3efd8e31 2022-10-23 thomas }
471 3efd8e31 2022-10-23 thomas
472 3efd8e31 2022-10-23 thomas static const struct got_error *
473 3efd8e31 2022-10-23 thomas pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
474 3efd8e31 2022-10-23 thomas uint32_t nobj_loose, uint32_t nobj_resolved)
475 3efd8e31 2022-10-23 thomas {
476 3efd8e31 2022-10-23 thomas int p_indexed = 0, p_resolved = 0;
477 3efd8e31 2022-10-23 thomas int nobj_delta = nobj_total - nobj_loose;
478 3efd8e31 2022-10-23 thomas
479 3efd8e31 2022-10-23 thomas if (nobj_total > 0)
480 3efd8e31 2022-10-23 thomas p_indexed = (nobj_indexed * 100) / nobj_total;
481 3efd8e31 2022-10-23 thomas
482 3efd8e31 2022-10-23 thomas if (nobj_delta > 0)
483 3efd8e31 2022-10-23 thomas p_resolved = (nobj_resolved * 100) / nobj_delta;
484 3efd8e31 2022-10-23 thomas
485 3efd8e31 2022-10-23 thomas if (p_resolved > 0) {
486 3efd8e31 2022-10-23 thomas log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
487 3efd8e31 2022-10-23 thomas nobj_total, p_indexed, nobj_delta, p_resolved);
488 3efd8e31 2022-10-23 thomas } else
489 3efd8e31 2022-10-23 thomas log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
490 3efd8e31 2022-10-23 thomas
491 3efd8e31 2022-10-23 thomas return NULL;
492 3efd8e31 2022-10-23 thomas }
493 3efd8e31 2022-10-23 thomas
494 3efd8e31 2022-10-23 thomas static const struct got_error *
495 3efd8e31 2022-10-23 thomas read_more_pack_stream(int infd, BUF *buf, size_t minsize)
496 3efd8e31 2022-10-23 thomas {
497 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
498 3efd8e31 2022-10-23 thomas uint8_t readahead[65536];
499 3efd8e31 2022-10-23 thomas size_t have, newlen;
500 3efd8e31 2022-10-23 thomas
501 3efd8e31 2022-10-23 thomas err = got_poll_read_full(infd, &have,
502 3efd8e31 2022-10-23 thomas readahead, sizeof(readahead), minsize);
503 3efd8e31 2022-10-23 thomas if (err)
504 3efd8e31 2022-10-23 thomas return err;
505 3efd8e31 2022-10-23 thomas
506 3efd8e31 2022-10-23 thomas err = buf_append(&newlen, buf, readahead, have);
507 3efd8e31 2022-10-23 thomas if (err)
508 3efd8e31 2022-10-23 thomas return err;
509 3efd8e31 2022-10-23 thomas return NULL;
510 3efd8e31 2022-10-23 thomas }
511 3efd8e31 2022-10-23 thomas
512 3efd8e31 2022-10-23 thomas static const struct got_error *
513 3efd8e31 2022-10-23 thomas copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
514 3efd8e31 2022-10-23 thomas off_t *outsize, BUF *buf, size_t *buf_pos, SHA1_CTX *ctx)
515 3efd8e31 2022-10-23 thomas {
516 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
517 3efd8e31 2022-10-23 thomas uint8_t t = 0;
518 3efd8e31 2022-10-23 thomas uint64_t s = 0;
519 3efd8e31 2022-10-23 thomas uint8_t sizebuf[8];
520 3efd8e31 2022-10-23 thomas size_t i = 0;
521 3efd8e31 2022-10-23 thomas off_t obj_offset = *outsize;
522 3efd8e31 2022-10-23 thomas
523 3efd8e31 2022-10-23 thomas do {
524 3efd8e31 2022-10-23 thomas /* We do not support size values which don't fit in 64 bit. */
525 3efd8e31 2022-10-23 thomas if (i > 9)
526 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
527 fe3b5495 2022-10-25 thomas "packfile offset %lld", (long long)obj_offset);
528 3efd8e31 2022-10-23 thomas
529 3efd8e31 2022-10-23 thomas if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
530 3efd8e31 2022-10-23 thomas err = read_more_pack_stream(infd, buf,
531 3efd8e31 2022-10-23 thomas sizeof(sizebuf[0]));
532 3efd8e31 2022-10-23 thomas if (err)
533 3efd8e31 2022-10-23 thomas return err;
534 3efd8e31 2022-10-23 thomas }
535 3efd8e31 2022-10-23 thomas
536 3efd8e31 2022-10-23 thomas sizebuf[i] = buf_getc(buf, *buf_pos);
537 3efd8e31 2022-10-23 thomas *buf_pos += sizeof(sizebuf[i]);
538 3efd8e31 2022-10-23 thomas
539 3efd8e31 2022-10-23 thomas if (i == 0) {
540 3efd8e31 2022-10-23 thomas t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
541 3efd8e31 2022-10-23 thomas GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
542 3efd8e31 2022-10-23 thomas s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
543 3efd8e31 2022-10-23 thomas } else {
544 3efd8e31 2022-10-23 thomas size_t shift = 4 + 7 * (i - 1);
545 3efd8e31 2022-10-23 thomas s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
546 3efd8e31 2022-10-23 thomas shift);
547 3efd8e31 2022-10-23 thomas }
548 3efd8e31 2022-10-23 thomas i++;
549 3efd8e31 2022-10-23 thomas } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
550 3efd8e31 2022-10-23 thomas
551 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(outfd, sizebuf, i, ctx);
552 3efd8e31 2022-10-23 thomas if (err)
553 3efd8e31 2022-10-23 thomas return err;
554 3efd8e31 2022-10-23 thomas *outsize += i;
555 3efd8e31 2022-10-23 thomas
556 3efd8e31 2022-10-23 thomas *type = t;
557 3efd8e31 2022-10-23 thomas *size = s;
558 3efd8e31 2022-10-23 thomas return NULL;
559 3efd8e31 2022-10-23 thomas }
560 3efd8e31 2022-10-23 thomas
561 3efd8e31 2022-10-23 thomas static const struct got_error *
562 3efd8e31 2022-10-23 thomas copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
563 3efd8e31 2022-10-23 thomas SHA1_CTX *ctx)
564 3efd8e31 2022-10-23 thomas {
565 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
566 3efd8e31 2022-10-23 thomas size_t remain = buf_len(buf) - *buf_pos;
567 3efd8e31 2022-10-23 thomas
568 3efd8e31 2022-10-23 thomas if (remain < SHA1_DIGEST_LENGTH) {
569 3efd8e31 2022-10-23 thomas err = read_more_pack_stream(infd, buf,
570 3efd8e31 2022-10-23 thomas SHA1_DIGEST_LENGTH - remain);
571 3efd8e31 2022-10-23 thomas if (err)
572 3efd8e31 2022-10-23 thomas return err;
573 3efd8e31 2022-10-23 thomas }
574 3efd8e31 2022-10-23 thomas
575 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
576 3efd8e31 2022-10-23 thomas SHA1_DIGEST_LENGTH, ctx);
577 3efd8e31 2022-10-23 thomas if (err)
578 3efd8e31 2022-10-23 thomas return err;
579 3efd8e31 2022-10-23 thomas
580 3efd8e31 2022-10-23 thomas *buf_pos += SHA1_DIGEST_LENGTH;
581 3efd8e31 2022-10-23 thomas return NULL;
582 3efd8e31 2022-10-23 thomas }
583 3efd8e31 2022-10-23 thomas
584 3efd8e31 2022-10-23 thomas static const struct got_error *
585 3efd8e31 2022-10-23 thomas copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
586 3efd8e31 2022-10-23 thomas SHA1_CTX *ctx)
587 3efd8e31 2022-10-23 thomas {
588 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
589 3efd8e31 2022-10-23 thomas uint64_t o = 0;
590 3efd8e31 2022-10-23 thomas uint8_t offbuf[8];
591 3efd8e31 2022-10-23 thomas size_t i = 0;
592 3efd8e31 2022-10-23 thomas off_t obj_offset = *outsize;
593 3efd8e31 2022-10-23 thomas
594 3efd8e31 2022-10-23 thomas do {
595 3efd8e31 2022-10-23 thomas /* We do not support offset values which don't fit in 64 bit. */
596 3efd8e31 2022-10-23 thomas if (i > 8)
597 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
598 fe3b5495 2022-10-25 thomas "packfile offset %lld", (long long)obj_offset);
599 3efd8e31 2022-10-23 thomas
600 3efd8e31 2022-10-23 thomas if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
601 3efd8e31 2022-10-23 thomas err = read_more_pack_stream(infd, buf,
602 3efd8e31 2022-10-23 thomas sizeof(offbuf[0]));
603 3efd8e31 2022-10-23 thomas if (err)
604 3efd8e31 2022-10-23 thomas return err;
605 3efd8e31 2022-10-23 thomas }
606 3efd8e31 2022-10-23 thomas
607 3efd8e31 2022-10-23 thomas offbuf[i] = buf_getc(buf, *buf_pos);
608 3efd8e31 2022-10-23 thomas *buf_pos += sizeof(offbuf[i]);
609 3efd8e31 2022-10-23 thomas
610 3efd8e31 2022-10-23 thomas if (i == 0)
611 3efd8e31 2022-10-23 thomas o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
612 3efd8e31 2022-10-23 thomas else {
613 3efd8e31 2022-10-23 thomas o++;
614 3efd8e31 2022-10-23 thomas o <<= 7;
615 3efd8e31 2022-10-23 thomas o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
616 3efd8e31 2022-10-23 thomas }
617 3efd8e31 2022-10-23 thomas i++;
618 3efd8e31 2022-10-23 thomas } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
619 3efd8e31 2022-10-23 thomas
620 3efd8e31 2022-10-23 thomas if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
621 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PACK_OFFSET);
622 3efd8e31 2022-10-23 thomas
623 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(outfd, offbuf, i, ctx);
624 3efd8e31 2022-10-23 thomas if (err)
625 3efd8e31 2022-10-23 thomas return err;
626 3efd8e31 2022-10-23 thomas
627 3efd8e31 2022-10-23 thomas *outsize += i;
628 3efd8e31 2022-10-23 thomas return NULL;
629 3efd8e31 2022-10-23 thomas }
630 3efd8e31 2022-10-23 thomas
631 3efd8e31 2022-10-23 thomas static const struct got_error *
632 3efd8e31 2022-10-23 thomas copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
633 3efd8e31 2022-10-23 thomas SHA1_CTX *ctx)
634 3efd8e31 2022-10-23 thomas {
635 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
636 3efd8e31 2022-10-23 thomas z_stream z;
637 3efd8e31 2022-10-23 thomas int zret;
638 3efd8e31 2022-10-23 thomas char voidbuf[1024];
639 3efd8e31 2022-10-23 thomas size_t consumed_total = 0;
640 3efd8e31 2022-10-23 thomas off_t zstream_offset = *outsize;
641 3efd8e31 2022-10-23 thomas
642 3efd8e31 2022-10-23 thomas memset(&z, 0, sizeof(z));
643 3efd8e31 2022-10-23 thomas
644 3efd8e31 2022-10-23 thomas z.zalloc = Z_NULL;
645 3efd8e31 2022-10-23 thomas z.zfree = Z_NULL;
646 3efd8e31 2022-10-23 thomas zret = inflateInit(&z);
647 3efd8e31 2022-10-23 thomas if (zret != Z_OK) {
648 3efd8e31 2022-10-23 thomas if (zret == Z_ERRNO)
649 3efd8e31 2022-10-23 thomas return got_error_from_errno("inflateInit");
650 3efd8e31 2022-10-23 thomas if (zret == Z_MEM_ERROR) {
651 3efd8e31 2022-10-23 thomas errno = ENOMEM;
652 3efd8e31 2022-10-23 thomas return got_error_from_errno("inflateInit");
653 3efd8e31 2022-10-23 thomas }
654 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_DECOMPRESSION,
655 3efd8e31 2022-10-23 thomas "inflateInit failed");
656 3efd8e31 2022-10-23 thomas }
657 3efd8e31 2022-10-23 thomas
658 3efd8e31 2022-10-23 thomas while (zret != Z_STREAM_END) {
659 3efd8e31 2022-10-23 thomas size_t last_total_in, consumed;
660 3efd8e31 2022-10-23 thomas
661 3efd8e31 2022-10-23 thomas /*
662 3efd8e31 2022-10-23 thomas * Decompress into the void. Object data will be parsed
663 3efd8e31 2022-10-23 thomas * later, when the pack file is indexed. For now, we just
664 3efd8e31 2022-10-23 thomas * want to locate the end of the compressed stream.
665 3efd8e31 2022-10-23 thomas */
666 3efd8e31 2022-10-23 thomas while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
667 3efd8e31 2022-10-23 thomas last_total_in = z.total_in;
668 3efd8e31 2022-10-23 thomas z.next_in = buf_get(buf) + *buf_pos;
669 3efd8e31 2022-10-23 thomas z.avail_in = buf_len(buf) - *buf_pos;
670 3efd8e31 2022-10-23 thomas z.next_out = voidbuf;
671 3efd8e31 2022-10-23 thomas z.avail_out = sizeof(voidbuf);
672 3efd8e31 2022-10-23 thomas
673 3efd8e31 2022-10-23 thomas zret = inflate(&z, Z_SYNC_FLUSH);
674 3efd8e31 2022-10-23 thomas if (zret != Z_OK && zret != Z_BUF_ERROR &&
675 3efd8e31 2022-10-23 thomas zret != Z_STREAM_END) {
676 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_DECOMPRESSION,
677 fe3b5495 2022-10-25 thomas "packfile offset %lld",
678 fe3b5495 2022-10-25 thomas (long long)zstream_offset);
679 3efd8e31 2022-10-23 thomas goto done;
680 3efd8e31 2022-10-23 thomas }
681 3efd8e31 2022-10-23 thomas consumed = z.total_in - last_total_in;
682 3efd8e31 2022-10-23 thomas
683 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
684 3efd8e31 2022-10-23 thomas consumed, ctx);
685 3efd8e31 2022-10-23 thomas if (err)
686 3efd8e31 2022-10-23 thomas goto done;
687 3efd8e31 2022-10-23 thomas
688 3efd8e31 2022-10-23 thomas err = buf_discard(buf, *buf_pos + consumed);
689 3efd8e31 2022-10-23 thomas if (err)
690 3efd8e31 2022-10-23 thomas goto done;
691 3efd8e31 2022-10-23 thomas *buf_pos = 0;
692 3efd8e31 2022-10-23 thomas
693 3efd8e31 2022-10-23 thomas consumed_total += consumed;
694 3efd8e31 2022-10-23 thomas }
695 3efd8e31 2022-10-23 thomas
696 3efd8e31 2022-10-23 thomas if (zret != Z_STREAM_END) {
697 3efd8e31 2022-10-23 thomas err = read_more_pack_stream(infd, buf, 1);
698 3efd8e31 2022-10-23 thomas if (err)
699 3efd8e31 2022-10-23 thomas goto done;
700 3efd8e31 2022-10-23 thomas }
701 3efd8e31 2022-10-23 thomas }
702 3efd8e31 2022-10-23 thomas
703 3efd8e31 2022-10-23 thomas if (err == NULL)
704 3efd8e31 2022-10-23 thomas *outsize += consumed_total;
705 3efd8e31 2022-10-23 thomas done:
706 3efd8e31 2022-10-23 thomas inflateEnd(&z);
707 3efd8e31 2022-10-23 thomas return err;
708 3efd8e31 2022-10-23 thomas }
709 3efd8e31 2022-10-23 thomas
710 3efd8e31 2022-10-23 thomas static const struct got_error *
711 3efd8e31 2022-10-23 thomas validate_object_type(int obj_type)
712 3efd8e31 2022-10-23 thomas {
713 3efd8e31 2022-10-23 thomas switch (obj_type) {
714 3efd8e31 2022-10-23 thomas case GOT_OBJ_TYPE_BLOB:
715 3efd8e31 2022-10-23 thomas case GOT_OBJ_TYPE_COMMIT:
716 3efd8e31 2022-10-23 thomas case GOT_OBJ_TYPE_TREE:
717 3efd8e31 2022-10-23 thomas case GOT_OBJ_TYPE_TAG:
718 3efd8e31 2022-10-23 thomas case GOT_OBJ_TYPE_REF_DELTA:
719 3efd8e31 2022-10-23 thomas case GOT_OBJ_TYPE_OFFSET_DELTA:
720 3efd8e31 2022-10-23 thomas return NULL;
721 3efd8e31 2022-10-23 thomas default:
722 3efd8e31 2022-10-23 thomas break;
723 3efd8e31 2022-10-23 thomas }
724 3efd8e31 2022-10-23 thomas
725 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_OBJ_TYPE);
726 3efd8e31 2022-10-23 thomas }
727 3efd8e31 2022-10-23 thomas
728 3efd8e31 2022-10-23 thomas static const struct got_error *
729 3efd8e31 2022-10-23 thomas recv_packdata(off_t *outsize, uint8_t *sha1, int infd, int outfd)
730 3efd8e31 2022-10-23 thomas {
731 3efd8e31 2022-10-23 thomas const struct got_error *err;
732 3efd8e31 2022-10-23 thomas struct got_packfile_hdr hdr;
733 3efd8e31 2022-10-23 thomas size_t have;
734 3efd8e31 2022-10-23 thomas uint32_t nobj, nhave = 0;
735 3efd8e31 2022-10-23 thomas SHA1_CTX ctx;
736 3efd8e31 2022-10-23 thomas uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
737 3efd8e31 2022-10-23 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
738 3efd8e31 2022-10-23 thomas BUF *buf = NULL;
739 3efd8e31 2022-10-23 thomas size_t buf_pos = 0, remain;
740 3efd8e31 2022-10-23 thomas ssize_t w;
741 3efd8e31 2022-10-23 thomas
742 3efd8e31 2022-10-23 thomas *outsize = 0;
743 3efd8e31 2022-10-23 thomas SHA1Init(&ctx);
744 3efd8e31 2022-10-23 thomas
745 3efd8e31 2022-10-23 thomas err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
746 3efd8e31 2022-10-23 thomas if (err)
747 3efd8e31 2022-10-23 thomas return err;
748 3efd8e31 2022-10-23 thomas if (have != sizeof(hdr))
749 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
750 3efd8e31 2022-10-23 thomas *outsize += have;
751 3efd8e31 2022-10-23 thomas
752 3efd8e31 2022-10-23 thomas if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
753 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE,
754 3efd8e31 2022-10-23 thomas "bad packfile signature");
755 3efd8e31 2022-10-23 thomas if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
756 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE,
757 3efd8e31 2022-10-23 thomas "bad packfile version");
758 3efd8e31 2022-10-23 thomas
759 3efd8e31 2022-10-23 thomas nobj = be32toh(hdr.nobjects);
760 3efd8e31 2022-10-23 thomas if (nobj == 0)
761 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE,
762 3efd8e31 2022-10-23 thomas "bad packfile with zero objects");
763 3efd8e31 2022-10-23 thomas
764 3efd8e31 2022-10-23 thomas log_debug("expecting %d objects", nobj);
765 3efd8e31 2022-10-23 thomas
766 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
767 3efd8e31 2022-10-23 thomas if (err)
768 3efd8e31 2022-10-23 thomas return err;
769 3efd8e31 2022-10-23 thomas
770 3efd8e31 2022-10-23 thomas err = buf_alloc(&buf, 65536);
771 3efd8e31 2022-10-23 thomas if (err)
772 3efd8e31 2022-10-23 thomas return err;
773 3efd8e31 2022-10-23 thomas
774 3efd8e31 2022-10-23 thomas while (nhave != nobj) {
775 3efd8e31 2022-10-23 thomas uint8_t obj_type;
776 3efd8e31 2022-10-23 thomas uint64_t obj_size;
777 3efd8e31 2022-10-23 thomas
778 3efd8e31 2022-10-23 thomas err = copy_object_type_and_size(&obj_type, &obj_size,
779 3efd8e31 2022-10-23 thomas infd, outfd, outsize, buf, &buf_pos, &ctx);
780 3efd8e31 2022-10-23 thomas if (err)
781 3efd8e31 2022-10-23 thomas goto done;
782 3efd8e31 2022-10-23 thomas
783 3efd8e31 2022-10-23 thomas err = validate_object_type(obj_type);
784 3efd8e31 2022-10-23 thomas if (err)
785 3efd8e31 2022-10-23 thomas goto done;
786 3efd8e31 2022-10-23 thomas
787 3efd8e31 2022-10-23 thomas if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
788 3efd8e31 2022-10-23 thomas err = copy_ref_delta(infd, outfd, outsize,
789 3efd8e31 2022-10-23 thomas buf, &buf_pos, &ctx);
790 3efd8e31 2022-10-23 thomas if (err)
791 3efd8e31 2022-10-23 thomas goto done;
792 3efd8e31 2022-10-23 thomas } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
793 3efd8e31 2022-10-23 thomas err = copy_offset_delta(infd, outfd, outsize,
794 3efd8e31 2022-10-23 thomas buf, &buf_pos, &ctx);
795 3efd8e31 2022-10-23 thomas if (err)
796 3efd8e31 2022-10-23 thomas goto done;
797 3efd8e31 2022-10-23 thomas }
798 3efd8e31 2022-10-23 thomas
799 3efd8e31 2022-10-23 thomas err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
800 3efd8e31 2022-10-23 thomas if (err)
801 3efd8e31 2022-10-23 thomas goto done;
802 3efd8e31 2022-10-23 thomas
803 3efd8e31 2022-10-23 thomas nhave++;
804 3efd8e31 2022-10-23 thomas }
805 3efd8e31 2022-10-23 thomas
806 3efd8e31 2022-10-23 thomas log_debug("received %u objects", nobj);
807 3efd8e31 2022-10-23 thomas
808 3efd8e31 2022-10-23 thomas SHA1Final(expected_sha1, &ctx);
809 3efd8e31 2022-10-23 thomas
810 3efd8e31 2022-10-23 thomas remain = buf_len(buf) - buf_pos;
811 3efd8e31 2022-10-23 thomas if (remain < SHA1_DIGEST_LENGTH) {
812 3efd8e31 2022-10-23 thomas err = read_more_pack_stream(infd, buf,
813 3efd8e31 2022-10-23 thomas SHA1_DIGEST_LENGTH - remain);
814 3efd8e31 2022-10-23 thomas if (err)
815 3efd8e31 2022-10-23 thomas return err;
816 3efd8e31 2022-10-23 thomas }
817 3efd8e31 2022-10-23 thomas
818 3efd8e31 2022-10-23 thomas got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
819 3efd8e31 2022-10-23 thomas log_debug("expect SHA1: %s", hex);
820 3efd8e31 2022-10-23 thomas got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
821 3efd8e31 2022-10-23 thomas log_debug("actual SHA1: %s", hex);
822 3efd8e31 2022-10-23 thomas
823 3efd8e31 2022-10-23 thomas if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
824 3efd8e31 2022-10-23 thomas SHA1_DIGEST_LENGTH) != 0) {
825 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PACKFILE_CSUM);
826 3efd8e31 2022-10-23 thomas goto done;
827 3efd8e31 2022-10-23 thomas }
828 3efd8e31 2022-10-23 thomas
829 3efd8e31 2022-10-23 thomas memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
830 3efd8e31 2022-10-23 thomas
831 3efd8e31 2022-10-23 thomas w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
832 3efd8e31 2022-10-23 thomas if (w == -1) {
833 3efd8e31 2022-10-23 thomas err = got_error_from_errno("write");
834 3efd8e31 2022-10-23 thomas goto done;
835 3efd8e31 2022-10-23 thomas }
836 3efd8e31 2022-10-23 thomas if (w != SHA1_DIGEST_LENGTH) {
837 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_IO);
838 3efd8e31 2022-10-23 thomas goto done;
839 3efd8e31 2022-10-23 thomas }
840 3efd8e31 2022-10-23 thomas
841 3efd8e31 2022-10-23 thomas *outsize += SHA1_DIGEST_LENGTH;
842 3efd8e31 2022-10-23 thomas
843 3efd8e31 2022-10-23 thomas if (fsync(outfd) == -1) {
844 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fsync");
845 3efd8e31 2022-10-23 thomas goto done;
846 3efd8e31 2022-10-23 thomas }
847 3efd8e31 2022-10-23 thomas if (lseek(outfd, 0L, SEEK_SET) == -1) {
848 3efd8e31 2022-10-23 thomas err = got_error_from_errno("lseek");
849 3efd8e31 2022-10-23 thomas goto done;
850 3efd8e31 2022-10-23 thomas }
851 3efd8e31 2022-10-23 thomas done:
852 3efd8e31 2022-10-23 thomas buf_free(buf);
853 3efd8e31 2022-10-23 thomas return err;
854 3efd8e31 2022-10-23 thomas }
855 3efd8e31 2022-10-23 thomas
856 3efd8e31 2022-10-23 thomas static const struct got_error *
857 3efd8e31 2022-10-23 thomas report_pack_status(struct repo_write_client *client,
858 3efd8e31 2022-10-23 thomas const struct got_error *unpack_err)
859 3efd8e31 2022-10-23 thomas {
860 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
861 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_status istatus;
862 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
863 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
864 3efd8e31 2022-10-23 thomas const char *unpack_ok = "unpack ok\n";
865 3efd8e31 2022-10-23 thomas size_t len;
866 3efd8e31 2022-10-23 thomas
867 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, client->fd);
868 3efd8e31 2022-10-23 thomas
869 3efd8e31 2022-10-23 thomas if (unpack_err)
870 3efd8e31 2022-10-23 thomas istatus.reason_len = strlen(unpack_err->msg);
871 3efd8e31 2022-10-23 thomas else
872 3efd8e31 2022-10-23 thomas istatus.reason_len = strlen(unpack_ok);
873 3efd8e31 2022-10-23 thomas
874 3efd8e31 2022-10-23 thomas len = sizeof(istatus) + istatus.reason_len;
875 3efd8e31 2022-10-23 thomas wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
876 3efd8e31 2022-10-23 thomas repo_write.pid, len);
877 3efd8e31 2022-10-23 thomas if (wbuf == NULL) {
878 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_create PACKFILE_STATUS");
879 3efd8e31 2022-10-23 thomas goto done;
880 3efd8e31 2022-10-23 thomas }
881 3efd8e31 2022-10-23 thomas
882 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
883 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add PACKFILE_STATUS");
884 3efd8e31 2022-10-23 thomas goto done;
885 3efd8e31 2022-10-23 thomas }
886 3efd8e31 2022-10-23 thomas
887 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, err ? err->msg : unpack_ok,
888 3efd8e31 2022-10-23 thomas istatus.reason_len) == -1) {
889 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_add PACKFILE_STATUS");
890 3efd8e31 2022-10-23 thomas goto done;
891 3efd8e31 2022-10-23 thomas }
892 3efd8e31 2022-10-23 thomas
893 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
894 3efd8e31 2022-10-23 thomas imsg_close(&ibuf, wbuf);
895 3efd8e31 2022-10-23 thomas
896 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(&ibuf);
897 3efd8e31 2022-10-23 thomas done:
898 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
899 3efd8e31 2022-10-23 thomas return err;
900 3efd8e31 2022-10-23 thomas }
901 3efd8e31 2022-10-23 thomas
902 3efd8e31 2022-10-23 thomas static const struct got_error *
903 3efd8e31 2022-10-23 thomas recv_packfile(struct repo_write_client **client, struct imsg *imsg)
904 3efd8e31 2022-10-23 thomas {
905 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL, *unpack_err;
906 3efd8e31 2022-10-23 thomas struct gotd_imsg_recv_packfile ireq;
907 3efd8e31 2022-10-23 thomas FILE *tempfiles[3] = { NULL, NULL, NULL };
908 3efd8e31 2022-10-23 thomas struct repo_tempfile {
909 3efd8e31 2022-10-23 thomas int fd;
910 3efd8e31 2022-10-23 thomas int idx;
911 3efd8e31 2022-10-23 thomas } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
912 3efd8e31 2022-10-23 thomas int i;
913 3efd8e31 2022-10-23 thomas size_t datalen;
914 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
915 3efd8e31 2022-10-23 thomas struct got_ratelimit rl;
916 3efd8e31 2022-10-23 thomas struct got_pack *pack = NULL;
917 3efd8e31 2022-10-23 thomas off_t pack_filesize = 0;
918 3efd8e31 2022-10-23 thomas
919 3efd8e31 2022-10-23 thomas log_debug("packfile request received");
920 3efd8e31 2022-10-23 thomas
921 3efd8e31 2022-10-23 thomas got_ratelimit_init(&rl, 2, 0);
922 3efd8e31 2022-10-23 thomas
923 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
924 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
925 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
926 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
927 3efd8e31 2022-10-23 thomas
928 3efd8e31 2022-10-23 thomas *client = find_client(ireq.client_id);
929 3efd8e31 2022-10-23 thomas if (*client == NULL || STAILQ_EMPTY(&(*client)->ref_updates))
930 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_CLIENT_ID);
931 3efd8e31 2022-10-23 thomas
932 3efd8e31 2022-10-23 thomas if ((*client)->pack_pipe[0] == -1 ||
933 3efd8e31 2022-10-23 thomas (*client)->pack_pipe[1] == -1 ||
934 3efd8e31 2022-10-23 thomas (*client)->packidx_fd == -1)
935 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
936 3efd8e31 2022-10-23 thomas
937 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, (*client)->fd);
938 3efd8e31 2022-10-23 thomas
939 3efd8e31 2022-10-23 thomas if (imsg->fd == -1)
940 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
941 3efd8e31 2022-10-23 thomas
942 3efd8e31 2022-10-23 thomas pack = &(*client)->pack;
943 3efd8e31 2022-10-23 thomas memset(pack, 0, sizeof(*pack));
944 3efd8e31 2022-10-23 thomas pack->fd = imsg->fd;
945 3efd8e31 2022-10-23 thomas err = got_delta_cache_alloc(&pack->delta_cache);
946 3efd8e31 2022-10-23 thomas if (err)
947 3efd8e31 2022-10-23 thomas return err;
948 3efd8e31 2022-10-23 thomas
949 3efd8e31 2022-10-23 thomas for (i = 0; i < nitems(repo_tempfiles); i++) {
950 3efd8e31 2022-10-23 thomas struct repo_tempfile *t = &repo_tempfiles[i];
951 3efd8e31 2022-10-23 thomas err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
952 3efd8e31 2022-10-23 thomas if (err)
953 3efd8e31 2022-10-23 thomas goto done;
954 3efd8e31 2022-10-23 thomas }
955 3efd8e31 2022-10-23 thomas
956 3efd8e31 2022-10-23 thomas for (i = 0; i < nitems(tempfiles); i++) {
957 3efd8e31 2022-10-23 thomas int fd = dup(repo_tempfiles[i].fd);
958 3efd8e31 2022-10-23 thomas FILE *f;
959 3efd8e31 2022-10-23 thomas if (fd == -1) {
960 3efd8e31 2022-10-23 thomas err = got_error_from_errno("dup");
961 3efd8e31 2022-10-23 thomas goto done;
962 3efd8e31 2022-10-23 thomas }
963 3efd8e31 2022-10-23 thomas f = fdopen(fd, "w+");
964 3efd8e31 2022-10-23 thomas if (f == NULL) {
965 3efd8e31 2022-10-23 thomas err = got_error_from_errno("dup");
966 3efd8e31 2022-10-23 thomas close(fd);
967 3efd8e31 2022-10-23 thomas goto done;
968 3efd8e31 2022-10-23 thomas }
969 3efd8e31 2022-10-23 thomas tempfiles[i] = f;
970 3efd8e31 2022-10-23 thomas }
971 3efd8e31 2022-10-23 thomas
972 3efd8e31 2022-10-23 thomas /* Send pack file pipe to gotsh(1). */
973 3efd8e31 2022-10-23 thomas if (imsg_compose(&ibuf, GOTD_IMSG_RECV_PACKFILE, PROC_REPO_WRITE,
974 3efd8e31 2022-10-23 thomas repo_write.pid, (*client)->pack_pipe[1], NULL, 0) == -1) {
975 3efd8e31 2022-10-23 thomas (*client)->pack_pipe[1] = -1;
976 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_compose ACK");
977 3efd8e31 2022-10-23 thomas if (err)
978 3efd8e31 2022-10-23 thomas goto done;
979 3efd8e31 2022-10-23 thomas }
980 3efd8e31 2022-10-23 thomas (*client)->pack_pipe[1] = -1;
981 3efd8e31 2022-10-23 thomas err = gotd_imsg_flush(&ibuf);
982 3efd8e31 2022-10-23 thomas if (err)
983 3efd8e31 2022-10-23 thomas goto done;
984 3efd8e31 2022-10-23 thomas
985 3efd8e31 2022-10-23 thomas log_debug("receiving pack data");
986 3efd8e31 2022-10-23 thomas unpack_err = recv_packdata(&pack_filesize, (*client)->pack_sha1,
987 3efd8e31 2022-10-23 thomas (*client)->pack_pipe[0], pack->fd);
988 3efd8e31 2022-10-23 thomas if (ireq.report_status) {
989 3efd8e31 2022-10-23 thomas err = report_pack_status(*client, unpack_err);
990 3efd8e31 2022-10-23 thomas if (err) {
991 3efd8e31 2022-10-23 thomas /* Git clients hang up after sending the pack file. */
992 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_EOF)
993 3efd8e31 2022-10-23 thomas err = NULL;
994 3efd8e31 2022-10-23 thomas }
995 3efd8e31 2022-10-23 thomas }
996 3efd8e31 2022-10-23 thomas if (unpack_err)
997 3efd8e31 2022-10-23 thomas err = unpack_err;
998 3efd8e31 2022-10-23 thomas if (err)
999 3efd8e31 2022-10-23 thomas goto done;
1000 3efd8e31 2022-10-23 thomas
1001 3efd8e31 2022-10-23 thomas log_debug("pack data received");
1002 3efd8e31 2022-10-23 thomas
1003 3efd8e31 2022-10-23 thomas pack->filesize = pack_filesize;
1004 3efd8e31 2022-10-23 thomas
1005 66e6097f 2022-10-27 thomas log_debug("begin indexing pack (%lld bytes in size)",
1006 66e6097f 2022-10-27 thomas (long long)pack->filesize);
1007 3efd8e31 2022-10-23 thomas err = got_pack_index(pack, (*client)->packidx_fd,
1008 3efd8e31 2022-10-23 thomas tempfiles[0], tempfiles[1], tempfiles[2], (*client)->pack_sha1,
1009 3efd8e31 2022-10-23 thomas pack_index_progress, NULL, &rl);
1010 3efd8e31 2022-10-23 thomas if (err)
1011 3efd8e31 2022-10-23 thomas goto done;
1012 3efd8e31 2022-10-23 thomas log_debug("done indexing pack");
1013 3efd8e31 2022-10-23 thomas
1014 3efd8e31 2022-10-23 thomas if (fsync((*client)->packidx_fd) == -1) {
1015 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fsync");
1016 3efd8e31 2022-10-23 thomas goto done;
1017 3efd8e31 2022-10-23 thomas }
1018 3efd8e31 2022-10-23 thomas if (lseek((*client)->packidx_fd, 0L, SEEK_SET) == -1)
1019 3efd8e31 2022-10-23 thomas err = got_error_from_errno("lseek");
1020 3efd8e31 2022-10-23 thomas done:
1021 3efd8e31 2022-10-23 thomas if (close((*client)->pack_pipe[0]) == -1 && err == NULL)
1022 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
1023 3efd8e31 2022-10-23 thomas (*client)->pack_pipe[0] = -1;
1024 3efd8e31 2022-10-23 thomas for (i = 0; i < nitems(repo_tempfiles); i++) {
1025 3efd8e31 2022-10-23 thomas struct repo_tempfile *t = &repo_tempfiles[i];
1026 3efd8e31 2022-10-23 thomas if (t->idx != -1)
1027 3efd8e31 2022-10-23 thomas got_repo_temp_fds_put(t->idx, repo_write.repo);
1028 3efd8e31 2022-10-23 thomas }
1029 3efd8e31 2022-10-23 thomas for (i = 0; i < nitems(tempfiles); i++) {
1030 3efd8e31 2022-10-23 thomas if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1031 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fclose");
1032 3efd8e31 2022-10-23 thomas }
1033 3efd8e31 2022-10-23 thomas if (err)
1034 3efd8e31 2022-10-23 thomas got_pack_close(pack);
1035 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
1036 3efd8e31 2022-10-23 thomas return err;
1037 3efd8e31 2022-10-23 thomas }
1038 3efd8e31 2022-10-23 thomas
1039 3efd8e31 2022-10-23 thomas static const struct got_error *
1040 3efd8e31 2022-10-23 thomas verify_packfile(struct repo_write_client *client)
1041 3efd8e31 2022-10-23 thomas {
1042 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL, *close_err;
1043 3efd8e31 2022-10-23 thomas struct gotd_ref_update *ref_update;
1044 3efd8e31 2022-10-23 thomas struct got_packidx *packidx = NULL;
1045 3efd8e31 2022-10-23 thomas struct stat sb;
1046 3efd8e31 2022-10-23 thomas char *id_str = NULL;
1047 3efd8e31 2022-10-23 thomas int idx = -1;
1048 3efd8e31 2022-10-23 thomas
1049 3efd8e31 2022-10-23 thomas if (STAILQ_EMPTY(&client->ref_updates)) {
1050 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
1051 3efd8e31 2022-10-23 thomas "cannot verify pack file without any ref-updates");
1052 3efd8e31 2022-10-23 thomas }
1053 3efd8e31 2022-10-23 thomas
1054 3efd8e31 2022-10-23 thomas if (client->pack.fd == -1) {
1055 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
1056 3efd8e31 2022-10-23 thomas "invalid pack file handle during pack verification");
1057 3efd8e31 2022-10-23 thomas }
1058 3efd8e31 2022-10-23 thomas if (client->packidx_fd == -1) {
1059 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
1060 3efd8e31 2022-10-23 thomas "invalid pack index handle during pack verification");
1061 3efd8e31 2022-10-23 thomas }
1062 3efd8e31 2022-10-23 thomas
1063 3efd8e31 2022-10-23 thomas if (fstat(client->packidx_fd, &sb) == -1)
1064 3efd8e31 2022-10-23 thomas return got_error_from_errno("pack index fstat");
1065 3efd8e31 2022-10-23 thomas
1066 3efd8e31 2022-10-23 thomas packidx = malloc(sizeof(*packidx));
1067 3efd8e31 2022-10-23 thomas memset(packidx, 0, sizeof(*packidx));
1068 3efd8e31 2022-10-23 thomas packidx->fd = client->packidx_fd;
1069 3efd8e31 2022-10-23 thomas client->packidx_fd = -1;
1070 3efd8e31 2022-10-23 thomas packidx->len = sb.st_size;
1071 3efd8e31 2022-10-23 thomas
1072 3efd8e31 2022-10-23 thomas err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1073 3efd8e31 2022-10-23 thomas if (err)
1074 3efd8e31 2022-10-23 thomas return err;
1075 3efd8e31 2022-10-23 thomas
1076 3efd8e31 2022-10-23 thomas STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1077 3efd8e31 2022-10-23 thomas err = got_object_id_str(&id_str, &ref_update->new_id);
1078 3efd8e31 2022-10-23 thomas if (err)
1079 3efd8e31 2022-10-23 thomas goto done;
1080 3efd8e31 2022-10-23 thomas
1081 3efd8e31 2022-10-23 thomas idx = got_packidx_get_object_idx(packidx, &ref_update->new_id);
1082 3efd8e31 2022-10-23 thomas if (idx == -1) {
1083 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1084 3efd8e31 2022-10-23 thomas "advertised object %s is missing from pack file",
1085 3efd8e31 2022-10-23 thomas id_str);
1086 3efd8e31 2022-10-23 thomas goto done;
1087 3efd8e31 2022-10-23 thomas }
1088 3efd8e31 2022-10-23 thomas }
1089 3efd8e31 2022-10-23 thomas
1090 3efd8e31 2022-10-23 thomas done:
1091 3efd8e31 2022-10-23 thomas close_err = got_packidx_close(packidx);
1092 3efd8e31 2022-10-23 thomas if (close_err && err == NULL)
1093 3efd8e31 2022-10-23 thomas err = close_err;
1094 3efd8e31 2022-10-23 thomas free(id_str);
1095 3efd8e31 2022-10-23 thomas return err;
1096 3efd8e31 2022-10-23 thomas }
1097 3efd8e31 2022-10-23 thomas
1098 3efd8e31 2022-10-23 thomas static const struct got_error *
1099 3efd8e31 2022-10-23 thomas install_packfile(struct repo_write_client *client, struct gotd_imsgev *iev)
1100 3efd8e31 2022-10-23 thomas {
1101 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_install inst;
1102 3efd8e31 2022-10-23 thomas int ret;
1103 3efd8e31 2022-10-23 thomas
1104 3efd8e31 2022-10-23 thomas memset(&inst, 0, sizeof(inst));
1105 3efd8e31 2022-10-23 thomas inst.client_id = client->id;
1106 3efd8e31 2022-10-23 thomas memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1107 3efd8e31 2022-10-23 thomas
1108 3efd8e31 2022-10-23 thomas ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1109 3efd8e31 2022-10-23 thomas PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1110 3efd8e31 2022-10-23 thomas if (ret == -1)
1111 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1112 3efd8e31 2022-10-23 thomas
1113 3efd8e31 2022-10-23 thomas return NULL;
1114 3efd8e31 2022-10-23 thomas }
1115 3efd8e31 2022-10-23 thomas
1116 3efd8e31 2022-10-23 thomas static const struct got_error *
1117 3efd8e31 2022-10-23 thomas send_ref_updates_start(struct repo_write_client *client, int nref_updates,
1118 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev)
1119 3efd8e31 2022-10-23 thomas {
1120 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_updates_start istart;
1121 3efd8e31 2022-10-23 thomas int ret;
1122 3efd8e31 2022-10-23 thomas
1123 3efd8e31 2022-10-23 thomas memset(&istart, 0, sizeof(istart));
1124 3efd8e31 2022-10-23 thomas istart.nref_updates = nref_updates;
1125 3efd8e31 2022-10-23 thomas istart.client_id = client->id;
1126 3efd8e31 2022-10-23 thomas
1127 3efd8e31 2022-10-23 thomas ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1128 3efd8e31 2022-10-23 thomas PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1129 3efd8e31 2022-10-23 thomas if (ret == -1)
1130 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_compose REF_UPDATES_START");
1131 3efd8e31 2022-10-23 thomas
1132 3efd8e31 2022-10-23 thomas return NULL;
1133 3efd8e31 2022-10-23 thomas }
1134 3efd8e31 2022-10-23 thomas
1135 3efd8e31 2022-10-23 thomas
1136 3efd8e31 2022-10-23 thomas static const struct got_error *
1137 3efd8e31 2022-10-23 thomas send_ref_update(struct repo_write_client *client,
1138 3efd8e31 2022-10-23 thomas struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1139 3efd8e31 2022-10-23 thomas {
1140 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update iref;
1141 3efd8e31 2022-10-23 thomas const char *refname = got_ref_get_name(ref_update->ref);
1142 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
1143 3efd8e31 2022-10-23 thomas size_t len;
1144 3efd8e31 2022-10-23 thomas
1145 3efd8e31 2022-10-23 thomas memset(&iref, 0, sizeof(iref));
1146 3efd8e31 2022-10-23 thomas memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1147 3efd8e31 2022-10-23 thomas memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1148 3efd8e31 2022-10-23 thomas iref.ref_is_new = ref_update->ref_is_new;
1149 3efd8e31 2022-10-23 thomas iref.client_id = client->id;
1150 3efd8e31 2022-10-23 thomas iref.name_len = strlen(refname);
1151 3efd8e31 2022-10-23 thomas
1152 3efd8e31 2022-10-23 thomas len = sizeof(iref) + iref.name_len;
1153 3efd8e31 2022-10-23 thomas wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1154 3efd8e31 2022-10-23 thomas repo_write.pid, len);
1155 3efd8e31 2022-10-23 thomas if (wbuf == NULL)
1156 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_create REF_UPDATE");
1157 3efd8e31 2022-10-23 thomas
1158 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1159 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE");
1160 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, refname, iref.name_len) == -1)
1161 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE");
1162 3efd8e31 2022-10-23 thomas
1163 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
1164 3efd8e31 2022-10-23 thomas imsg_close(&iev->ibuf, wbuf);
1165 3efd8e31 2022-10-23 thomas
1166 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(iev);
1167 3efd8e31 2022-10-23 thomas return NULL;
1168 3efd8e31 2022-10-23 thomas }
1169 3efd8e31 2022-10-23 thomas
1170 3efd8e31 2022-10-23 thomas static const struct got_error *
1171 3efd8e31 2022-10-23 thomas update_refs(struct repo_write_client *client, struct gotd_imsgev *iev)
1172 3efd8e31 2022-10-23 thomas {
1173 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1174 3efd8e31 2022-10-23 thomas struct gotd_ref_update *ref_update;
1175 3efd8e31 2022-10-23 thomas
1176 3efd8e31 2022-10-23 thomas err = send_ref_updates_start(client, client->nref_updates, iev);
1177 3efd8e31 2022-10-23 thomas if (err)
1178 3efd8e31 2022-10-23 thomas return err;
1179 3efd8e31 2022-10-23 thomas
1180 3efd8e31 2022-10-23 thomas STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1181 3efd8e31 2022-10-23 thomas err = send_ref_update(client, ref_update, iev);
1182 3efd8e31 2022-10-23 thomas if (err)
1183 3efd8e31 2022-10-23 thomas goto done;
1184 3efd8e31 2022-10-23 thomas }
1185 3efd8e31 2022-10-23 thomas done:
1186 3efd8e31 2022-10-23 thomas return err;
1187 3efd8e31 2022-10-23 thomas }
1188 3efd8e31 2022-10-23 thomas
1189 3efd8e31 2022-10-23 thomas static const struct got_error *
1190 3efd8e31 2022-10-23 thomas recv_disconnect(struct imsg *imsg)
1191 3efd8e31 2022-10-23 thomas {
1192 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1193 3efd8e31 2022-10-23 thomas struct gotd_imsg_disconnect idisconnect;
1194 3efd8e31 2022-10-23 thomas size_t datalen;
1195 3efd8e31 2022-10-23 thomas int client_fd = -1, pipe0 = -1, pipe1 = - 1, idxfd = -1;
1196 3efd8e31 2022-10-23 thomas struct repo_write_client *client = NULL;
1197 3efd8e31 2022-10-23 thomas uint64_t slot;
1198 3efd8e31 2022-10-23 thomas
1199 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1200 3efd8e31 2022-10-23 thomas if (datalen != sizeof(idisconnect))
1201 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1202 3efd8e31 2022-10-23 thomas memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
1203 3efd8e31 2022-10-23 thomas
1204 3efd8e31 2022-10-23 thomas log_debug("client disconnecting");
1205 3efd8e31 2022-10-23 thomas
1206 3efd8e31 2022-10-23 thomas client = find_client(idisconnect.client_id);
1207 3efd8e31 2022-10-23 thomas if (client == NULL)
1208 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_CLIENT_ID);
1209 3efd8e31 2022-10-23 thomas
1210 3efd8e31 2022-10-23 thomas slot = client_hash(client->id) % nitems(repo_write_clients);
1211 3efd8e31 2022-10-23 thomas STAILQ_REMOVE(&repo_write_clients[slot], client, repo_write_client,
1212 3efd8e31 2022-10-23 thomas entry);
1213 3efd8e31 2022-10-23 thomas while (!STAILQ_EMPTY(&client->ref_updates)) {
1214 3efd8e31 2022-10-23 thomas struct gotd_ref_update *ref_update;
1215 3efd8e31 2022-10-23 thomas ref_update = STAILQ_FIRST(&client->ref_updates);
1216 3efd8e31 2022-10-23 thomas STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
1217 3efd8e31 2022-10-23 thomas got_ref_close(ref_update->ref);
1218 3efd8e31 2022-10-23 thomas free(ref_update);
1219 3efd8e31 2022-10-23 thomas }
1220 3efd8e31 2022-10-23 thomas err = got_pack_close(&client->pack);
1221 3efd8e31 2022-10-23 thomas client_fd = client->fd;
1222 3efd8e31 2022-10-23 thomas pipe0 = client->pack_pipe[0];
1223 3efd8e31 2022-10-23 thomas pipe1 = client->pack_pipe[1];
1224 3efd8e31 2022-10-23 thomas idxfd = client->packidx_fd;
1225 3efd8e31 2022-10-23 thomas free(client);
1226 3efd8e31 2022-10-23 thomas if (client_fd != -1 && close(client_fd) == -1)
1227 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
1228 3efd8e31 2022-10-23 thomas if (pipe0 != -1 && close(pipe0) == -1 && err == NULL)
1229 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
1230 3efd8e31 2022-10-23 thomas if (pipe1 != -1 && close(pipe1) == -1 && err == NULL)
1231 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
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;
1235 3efd8e31 2022-10-23 thomas }
1236 3efd8e31 2022-10-23 thomas
1237 3efd8e31 2022-10-23 thomas static const struct got_error *
1238 3efd8e31 2022-10-23 thomas receive_pack_pipe(struct repo_write_client **client, struct imsg *imsg,
1239 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev)
1240 3efd8e31 2022-10-23 thomas {
1241 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_pipe ireq;
1242 3efd8e31 2022-10-23 thomas size_t datalen;
1243 3efd8e31 2022-10-23 thomas
1244 3efd8e31 2022-10-23 thomas log_debug("receving pack pipe descriptor");
1245 3efd8e31 2022-10-23 thomas
1246 3efd8e31 2022-10-23 thomas if (imsg->fd == -1)
1247 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1248 3efd8e31 2022-10-23 thomas
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));
1253 3efd8e31 2022-10-23 thomas
1254 3efd8e31 2022-10-23 thomas *client = find_client(ireq.client_id);
1255 3efd8e31 2022-10-23 thomas if (*client == NULL)
1256 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_CLIENT_ID);
1257 3efd8e31 2022-10-23 thomas if ((*client)->pack_pipe[1] != -1)
1258 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1259 3efd8e31 2022-10-23 thomas
1260 3efd8e31 2022-10-23 thomas if ((*client)->pack_pipe[0] == -1)
1261 3efd8e31 2022-10-23 thomas (*client)->pack_pipe[0] = imsg->fd;
1262 3efd8e31 2022-10-23 thomas else
1263 3efd8e31 2022-10-23 thomas (*client)->pack_pipe[1] = imsg->fd;
1264 3efd8e31 2022-10-23 thomas
1265 3efd8e31 2022-10-23 thomas return NULL;
1266 3efd8e31 2022-10-23 thomas }
1267 3efd8e31 2022-10-23 thomas
1268 3efd8e31 2022-10-23 thomas static const struct got_error *
1269 3efd8e31 2022-10-23 thomas receive_pack_idx(struct repo_write_client **client, struct imsg *imsg,
1270 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev)
1271 3efd8e31 2022-10-23 thomas {
1272 3efd8e31 2022-10-23 thomas struct gotd_imsg_packidx_file ireq;
1273 3efd8e31 2022-10-23 thomas size_t datalen;
1274 3efd8e31 2022-10-23 thomas
1275 3efd8e31 2022-10-23 thomas log_debug("receving pack index output file");
1276 3efd8e31 2022-10-23 thomas
1277 3efd8e31 2022-10-23 thomas if (imsg->fd == -1)
1278 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1279 3efd8e31 2022-10-23 thomas
1280 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1281 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
1282 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1283 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
1284 3efd8e31 2022-10-23 thomas
1285 3efd8e31 2022-10-23 thomas *client = find_client(ireq.client_id);
1286 3efd8e31 2022-10-23 thomas if (*client == NULL)
1287 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_CLIENT_ID);
1288 3efd8e31 2022-10-23 thomas if ((*client)->packidx_fd != -1)
1289 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1290 3efd8e31 2022-10-23 thomas
1291 3efd8e31 2022-10-23 thomas (*client)->packidx_fd = imsg->fd;
1292 3efd8e31 2022-10-23 thomas return NULL;
1293 3efd8e31 2022-10-23 thomas }
1294 3efd8e31 2022-10-23 thomas
1295 3efd8e31 2022-10-23 thomas static void
1296 3efd8e31 2022-10-23 thomas repo_write_dispatch(int fd, short event, void *arg)
1297 3efd8e31 2022-10-23 thomas {
1298 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1299 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev = arg;
1300 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf = &iev->ibuf;
1301 3efd8e31 2022-10-23 thomas struct imsg imsg;
1302 3efd8e31 2022-10-23 thomas struct repo_write_client *client = NULL;
1303 3efd8e31 2022-10-23 thomas ssize_t n;
1304 3efd8e31 2022-10-23 thomas int shut = 0;
1305 3efd8e31 2022-10-23 thomas
1306 3efd8e31 2022-10-23 thomas if (event & EV_READ) {
1307 3efd8e31 2022-10-23 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1308 3efd8e31 2022-10-23 thomas fatal("imsg_read error");
1309 3efd8e31 2022-10-23 thomas if (n == 0) /* Connection closed. */
1310 3efd8e31 2022-10-23 thomas shut = 1;
1311 3efd8e31 2022-10-23 thomas }
1312 3efd8e31 2022-10-23 thomas
1313 3efd8e31 2022-10-23 thomas if (event & EV_WRITE) {
1314 3efd8e31 2022-10-23 thomas n = msgbuf_write(&ibuf->w);
1315 3efd8e31 2022-10-23 thomas if (n == -1 && errno != EAGAIN)
1316 3efd8e31 2022-10-23 thomas fatal("msgbuf_write");
1317 3efd8e31 2022-10-23 thomas if (n == 0) /* Connection closed. */
1318 3efd8e31 2022-10-23 thomas shut = 1;
1319 3efd8e31 2022-10-23 thomas }
1320 3efd8e31 2022-10-23 thomas
1321 3efd8e31 2022-10-23 thomas for (;;) {
1322 3efd8e31 2022-10-23 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1323 3efd8e31 2022-10-23 thomas fatal("%s: imsg_get error", __func__);
1324 3efd8e31 2022-10-23 thomas if (n == 0) /* No more messages. */
1325 3efd8e31 2022-10-23 thomas break;
1326 3efd8e31 2022-10-23 thomas
1327 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
1328 3efd8e31 2022-10-23 thomas case GOTD_IMSG_LIST_REFS_INTERNAL:
1329 3efd8e31 2022-10-23 thomas err = list_refs(&client, &imsg);
1330 3efd8e31 2022-10-23 thomas if (err)
1331 3efd8e31 2022-10-23 thomas log_warnx("%s: ls-refs: %s", repo_write.title,
1332 3efd8e31 2022-10-23 thomas err->msg);
1333 3efd8e31 2022-10-23 thomas break;
1334 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF_UPDATE:
1335 3efd8e31 2022-10-23 thomas err = recv_ref_update(&client, &imsg);
1336 3efd8e31 2022-10-23 thomas if (err)
1337 3efd8e31 2022-10-23 thomas log_warnx("%s: ref-update: %s",
1338 3efd8e31 2022-10-23 thomas repo_write.title, err->msg);
1339 3efd8e31 2022-10-23 thomas break;
1340 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_PIPE:
1341 3efd8e31 2022-10-23 thomas err = receive_pack_pipe(&client, &imsg, iev);
1342 3efd8e31 2022-10-23 thomas if (err) {
1343 3efd8e31 2022-10-23 thomas log_warnx("%s: receiving pack pipe: %s",
1344 3efd8e31 2022-10-23 thomas repo_write.title, err->msg);
1345 3efd8e31 2022-10-23 thomas break;
1346 3efd8e31 2022-10-23 thomas }
1347 3efd8e31 2022-10-23 thomas break;
1348 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKIDX_FILE:
1349 3efd8e31 2022-10-23 thomas err = receive_pack_idx(&client, &imsg, iev);
1350 3efd8e31 2022-10-23 thomas if (err) {
1351 3efd8e31 2022-10-23 thomas log_warnx("%s: receiving pack index: %s",
1352 3efd8e31 2022-10-23 thomas repo_write.title, err->msg);
1353 3efd8e31 2022-10-23 thomas break;
1354 3efd8e31 2022-10-23 thomas }
1355 3efd8e31 2022-10-23 thomas break;
1356 3efd8e31 2022-10-23 thomas case GOTD_IMSG_RECV_PACKFILE:
1357 3efd8e31 2022-10-23 thomas err = recv_packfile(&client, &imsg);
1358 3efd8e31 2022-10-23 thomas if (err) {
1359 3efd8e31 2022-10-23 thomas log_warnx("%s: receive packfile: %s",
1360 3efd8e31 2022-10-23 thomas repo_write.title, err->msg);
1361 3efd8e31 2022-10-23 thomas break;
1362 3efd8e31 2022-10-23 thomas }
1363 3efd8e31 2022-10-23 thomas err = verify_packfile(client);
1364 3efd8e31 2022-10-23 thomas if (err) {
1365 3efd8e31 2022-10-23 thomas log_warnx("%s: verify packfile: %s",
1366 3efd8e31 2022-10-23 thomas repo_write.title, err->msg);
1367 3efd8e31 2022-10-23 thomas break;
1368 3efd8e31 2022-10-23 thomas }
1369 3efd8e31 2022-10-23 thomas err = install_packfile(client, iev);
1370 3efd8e31 2022-10-23 thomas if (err) {
1371 3efd8e31 2022-10-23 thomas log_warnx("%s: install packfile: %s",
1372 3efd8e31 2022-10-23 thomas repo_write.title, err->msg);
1373 3efd8e31 2022-10-23 thomas break;
1374 3efd8e31 2022-10-23 thomas }
1375 3efd8e31 2022-10-23 thomas err = update_refs(client, iev);
1376 3efd8e31 2022-10-23 thomas if (err) {
1377 3efd8e31 2022-10-23 thomas log_warnx("%s: update refs: %s",
1378 3efd8e31 2022-10-23 thomas repo_write.title, err->msg);
1379 3efd8e31 2022-10-23 thomas }
1380 3efd8e31 2022-10-23 thomas break;
1381 3efd8e31 2022-10-23 thomas case GOTD_IMSG_DISCONNECT:
1382 3efd8e31 2022-10-23 thomas err = recv_disconnect(&imsg);
1383 3efd8e31 2022-10-23 thomas if (err)
1384 3efd8e31 2022-10-23 thomas log_warnx("%s: disconnect: %s",
1385 3efd8e31 2022-10-23 thomas repo_write.title, err->msg);
1386 3efd8e31 2022-10-23 thomas break;
1387 3efd8e31 2022-10-23 thomas default:
1388 3efd8e31 2022-10-23 thomas log_debug("%s: unexpected imsg %d", repo_write.title,
1389 3efd8e31 2022-10-23 thomas imsg.hdr.type);
1390 3efd8e31 2022-10-23 thomas break;
1391 3efd8e31 2022-10-23 thomas }
1392 3efd8e31 2022-10-23 thomas
1393 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
1394 3efd8e31 2022-10-23 thomas }
1395 3efd8e31 2022-10-23 thomas
1396 3efd8e31 2022-10-23 thomas if (!shut && check_cancelled(NULL) == NULL) {
1397 3efd8e31 2022-10-23 thomas if (err &&
1398 3efd8e31 2022-10-23 thomas gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
1399 3efd8e31 2022-10-23 thomas client ? client->id : 0, err) == -1) {
1400 3efd8e31 2022-10-23 thomas log_warnx("could not send error to parent: %s",
1401 3efd8e31 2022-10-23 thomas err->msg);
1402 3efd8e31 2022-10-23 thomas }
1403 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(iev);
1404 3efd8e31 2022-10-23 thomas } else {
1405 3efd8e31 2022-10-23 thomas /* This pipe is dead. Remove its event handler */
1406 3efd8e31 2022-10-23 thomas event_del(&iev->ev);
1407 3efd8e31 2022-10-23 thomas event_loopexit(NULL);
1408 3efd8e31 2022-10-23 thomas }
1409 3efd8e31 2022-10-23 thomas }
1410 3efd8e31 2022-10-23 thomas
1411 3efd8e31 2022-10-23 thomas void
1412 3efd8e31 2022-10-23 thomas repo_write_main(const char *title, int *pack_fds, int *temp_fds)
1413 3efd8e31 2022-10-23 thomas {
1414 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1415 3efd8e31 2022-10-23 thomas struct gotd_imsgev iev;
1416 3efd8e31 2022-10-23 thomas
1417 3efd8e31 2022-10-23 thomas repo_write.title = title;
1418 3efd8e31 2022-10-23 thomas repo_write.pid = getpid();
1419 3efd8e31 2022-10-23 thomas repo_write.pack_fds = pack_fds;
1420 3efd8e31 2022-10-23 thomas repo_write.temp_fds = temp_fds;
1421 3efd8e31 2022-10-23 thomas
1422 3efd8e31 2022-10-23 thomas arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1423 3efd8e31 2022-10-23 thomas
1424 3efd8e31 2022-10-23 thomas /*
1425 3efd8e31 2022-10-23 thomas * Open a repository in the root directory.
1426 3efd8e31 2022-10-23 thomas * We are already in chroot at this point.
1427 3efd8e31 2022-10-23 thomas */
1428 3efd8e31 2022-10-23 thomas err = got_repo_open(&repo_write.repo, "/", NULL, pack_fds);
1429 3efd8e31 2022-10-23 thomas if (err)
1430 3efd8e31 2022-10-23 thomas goto done;
1431 3efd8e31 2022-10-23 thomas if (!got_repo_is_bare(repo_write.repo)) {
1432 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1433 3efd8e31 2022-10-23 thomas "bare git repository required");
1434 3efd8e31 2022-10-23 thomas goto done;
1435 3efd8e31 2022-10-23 thomas }
1436 3efd8e31 2022-10-23 thomas
1437 3efd8e31 2022-10-23 thomas got_repo_temp_fds_set(repo_write.repo, temp_fds);
1438 3efd8e31 2022-10-23 thomas
1439 3efd8e31 2022-10-23 thomas signal(SIGINT, catch_sigint);
1440 3efd8e31 2022-10-23 thomas signal(SIGTERM, catch_sigterm);
1441 3efd8e31 2022-10-23 thomas signal(SIGPIPE, SIG_IGN);
1442 3efd8e31 2022-10-23 thomas signal(SIGHUP, SIG_IGN);
1443 3efd8e31 2022-10-23 thomas
1444 3efd8e31 2022-10-23 thomas imsg_init(&iev.ibuf, GOTD_SOCK_FILENO);
1445 3efd8e31 2022-10-23 thomas iev.handler = repo_write_dispatch;
1446 3efd8e31 2022-10-23 thomas iev.events = EV_READ;
1447 3efd8e31 2022-10-23 thomas iev.handler_arg = NULL;
1448 3efd8e31 2022-10-23 thomas event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
1449 3efd8e31 2022-10-23 thomas if (event_add(&iev.ev, NULL) == -1) {
1450 3efd8e31 2022-10-23 thomas err = got_error_from_errno("event_add");
1451 3efd8e31 2022-10-23 thomas goto done;
1452 3efd8e31 2022-10-23 thomas }
1453 3efd8e31 2022-10-23 thomas
1454 3efd8e31 2022-10-23 thomas event_dispatch();
1455 3efd8e31 2022-10-23 thomas done:
1456 3efd8e31 2022-10-23 thomas if (err)
1457 3efd8e31 2022-10-23 thomas log_warnx("%s: %s", title, err->msg);
1458 3efd8e31 2022-10-23 thomas repo_write_shutdown();
1459 3efd8e31 2022-10-23 thomas }
1460 3efd8e31 2022-10-23 thomas
1461 3efd8e31 2022-10-23 thomas void
1462 3efd8e31 2022-10-23 thomas repo_write_shutdown(void)
1463 3efd8e31 2022-10-23 thomas {
1464 3efd8e31 2022-10-23 thomas log_debug("%s: shutting down", repo_write.title);
1465 3efd8e31 2022-10-23 thomas if (repo_write.repo)
1466 3efd8e31 2022-10-23 thomas got_repo_close(repo_write.repo);
1467 3efd8e31 2022-10-23 thomas got_repo_pack_fds_close(repo_write.pack_fds);
1468 80536967 2022-10-30 thomas got_repo_temp_fds_close(repo_write.temp_fds);
1469 3efd8e31 2022-10-23 thomas exit(0);
1470 3efd8e31 2022-10-23 thomas }