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