Blame


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