Blame


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