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