Blame


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