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