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 ba97b2d7 2024-03-20 stsp #include <ctype.h>
23 13b2bc37 2022-10-23 stsp #include <event.h>
24 13b2bc37 2022-10-23 stsp #include <errno.h>
25 13b2bc37 2022-10-23 stsp #include <imsg.h>
26 13b2bc37 2022-10-23 stsp #include <signal.h>
27 13b2bc37 2022-10-23 stsp #include <siphash.h>
28 13b2bc37 2022-10-23 stsp #include <stdio.h>
29 13b2bc37 2022-10-23 stsp #include <stdlib.h>
30 13b2bc37 2022-10-23 stsp #include <string.h>
31 13b2bc37 2022-10-23 stsp #include <limits.h>
32 13b2bc37 2022-10-23 stsp #include <poll.h>
33 13b2bc37 2022-10-23 stsp #include <sha1.h>
34 5822e79e 2023-02-23 op #include <sha2.h>
35 13b2bc37 2022-10-23 stsp #include <unistd.h>
36 13b2bc37 2022-10-23 stsp #include <zlib.h>
37 13b2bc37 2022-10-23 stsp
38 13b2bc37 2022-10-23 stsp #include "buf.h"
39 13b2bc37 2022-10-23 stsp
40 13b2bc37 2022-10-23 stsp #include "got_error.h"
41 13b2bc37 2022-10-23 stsp #include "got_repository.h"
42 13b2bc37 2022-10-23 stsp #include "got_object.h"
43 13b2bc37 2022-10-23 stsp #include "got_reference.h"
44 13b2bc37 2022-10-23 stsp #include "got_path.h"
45 ba97b2d7 2024-03-20 stsp #include "got_diff.h"
46 ba97b2d7 2024-03-20 stsp #include "got_cancel.h"
47 ba97b2d7 2024-03-20 stsp #include "got_commit_graph.h"
48 ba97b2d7 2024-03-20 stsp #include "got_opentemp.h"
49 13b2bc37 2022-10-23 stsp
50 13b2bc37 2022-10-23 stsp #include "got_lib_delta.h"
51 13b2bc37 2022-10-23 stsp #include "got_lib_delta_cache.h"
52 ae25a666 2023-02-23 op #include "got_lib_hash.h"
53 13b2bc37 2022-10-23 stsp #include "got_lib_object.h"
54 13b2bc37 2022-10-23 stsp #include "got_lib_object_cache.h"
55 9afa3de2 2023-04-04 stsp #include "got_lib_object_idset.h"
56 9afa3de2 2023-04-04 stsp #include "got_lib_object_parse.h"
57 13b2bc37 2022-10-23 stsp #include "got_lib_ratelimit.h"
58 13b2bc37 2022-10-23 stsp #include "got_lib_pack.h"
59 13b2bc37 2022-10-23 stsp #include "got_lib_pack_index.h"
60 13b2bc37 2022-10-23 stsp #include "got_lib_repository.h"
61 13b2bc37 2022-10-23 stsp #include "got_lib_poll.h"
62 13b2bc37 2022-10-23 stsp
63 13b2bc37 2022-10-23 stsp #include "log.h"
64 13b2bc37 2022-10-23 stsp #include "gotd.h"
65 13b2bc37 2022-10-23 stsp #include "repo_write.h"
66 13b2bc37 2022-10-23 stsp
67 13b2bc37 2022-10-23 stsp #ifndef nitems
68 13b2bc37 2022-10-23 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 13b2bc37 2022-10-23 stsp #endif
70 13b2bc37 2022-10-23 stsp
71 13b2bc37 2022-10-23 stsp static struct repo_write {
72 13b2bc37 2022-10-23 stsp pid_t pid;
73 13b2bc37 2022-10-23 stsp const char *title;
74 13b2bc37 2022-10-23 stsp struct got_repository *repo;
75 13b2bc37 2022-10-23 stsp int *pack_fds;
76 13b2bc37 2022-10-23 stsp int *temp_fds;
77 ae7c1b78 2023-01-10 stsp int session_fd;
78 ae7c1b78 2023-01-10 stsp struct gotd_imsgev session_iev;
79 9afa3de2 2023-04-04 stsp struct got_pathlist_head *protected_tag_namespaces;
80 9afa3de2 2023-04-04 stsp struct got_pathlist_head *protected_branch_namespaces;
81 9afa3de2 2023-04-04 stsp struct got_pathlist_head *protected_branches;
82 ba97b2d7 2024-03-20 stsp struct {
83 ba97b2d7 2024-03-20 stsp FILE *f1;
84 ba97b2d7 2024-03-20 stsp FILE *f2;
85 ba97b2d7 2024-03-20 stsp int fd1;
86 ba97b2d7 2024-03-20 stsp int fd2;
87 ba97b2d7 2024-03-20 stsp } diff;
88 1cb49b67 2024-03-21 stsp int refs_listed;
89 13b2bc37 2022-10-23 stsp } repo_write;
90 13b2bc37 2022-10-23 stsp
91 13b2bc37 2022-10-23 stsp struct gotd_ref_update {
92 13b2bc37 2022-10-23 stsp STAILQ_ENTRY(gotd_ref_update) entry;
93 13b2bc37 2022-10-23 stsp struct got_reference *ref;
94 13b2bc37 2022-10-23 stsp int ref_is_new;
95 9a8e357c 2023-01-28 op int delete_ref;
96 13b2bc37 2022-10-23 stsp struct got_object_id old_id;
97 13b2bc37 2022-10-23 stsp struct got_object_id new_id;
98 13b2bc37 2022-10-23 stsp };
99 13b2bc37 2022-10-23 stsp STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
100 13b2bc37 2022-10-23 stsp
101 1a52c9bf 2022-12-30 stsp static struct repo_write_client {
102 13b2bc37 2022-10-23 stsp uint32_t id;
103 13b2bc37 2022-10-23 stsp int fd;
104 7fec5f4a 2022-10-28 stsp int pack_pipe;
105 13b2bc37 2022-10-23 stsp struct got_pack pack;
106 13b2bc37 2022-10-23 stsp uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
107 13b2bc37 2022-10-23 stsp int packidx_fd;
108 13b2bc37 2022-10-23 stsp struct gotd_ref_updates ref_updates;
109 13b2bc37 2022-10-23 stsp int nref_updates;
110 9a8e357c 2023-01-28 op int nref_del;
111 0ff2c315 2023-01-18 stsp int nref_new;
112 cc88020e 2023-04-11 stsp int nref_move;
113 1a52c9bf 2022-12-30 stsp } repo_write_client;
114 13b2bc37 2022-10-23 stsp
115 13b2bc37 2022-10-23 stsp static volatile sig_atomic_t sigint_received;
116 13b2bc37 2022-10-23 stsp static volatile sig_atomic_t sigterm_received;
117 13b2bc37 2022-10-23 stsp
118 13b2bc37 2022-10-23 stsp static void
119 13b2bc37 2022-10-23 stsp catch_sigint(int signo)
120 13b2bc37 2022-10-23 stsp {
121 13b2bc37 2022-10-23 stsp sigint_received = 1;
122 13b2bc37 2022-10-23 stsp }
123 13b2bc37 2022-10-23 stsp
124 13b2bc37 2022-10-23 stsp static void
125 13b2bc37 2022-10-23 stsp catch_sigterm(int signo)
126 13b2bc37 2022-10-23 stsp {
127 13b2bc37 2022-10-23 stsp sigterm_received = 1;
128 13b2bc37 2022-10-23 stsp }
129 13b2bc37 2022-10-23 stsp
130 13b2bc37 2022-10-23 stsp static const struct got_error *
131 13b2bc37 2022-10-23 stsp check_cancelled(void *arg)
132 13b2bc37 2022-10-23 stsp {
133 13b2bc37 2022-10-23 stsp if (sigint_received || sigterm_received)
134 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_CANCELLED);
135 13b2bc37 2022-10-23 stsp
136 13b2bc37 2022-10-23 stsp return NULL;
137 13b2bc37 2022-10-23 stsp }
138 13b2bc37 2022-10-23 stsp
139 13b2bc37 2022-10-23 stsp static const struct got_error *
140 13b2bc37 2022-10-23 stsp send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
141 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf)
142 13b2bc37 2022-10-23 stsp {
143 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
144 13b2bc37 2022-10-23 stsp struct got_tag_object *tag;
145 13b2bc37 2022-10-23 stsp size_t namelen, len;
146 13b2bc37 2022-10-23 stsp char *peeled_refname = NULL;
147 13b2bc37 2022-10-23 stsp struct got_object_id *id;
148 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
149 13b2bc37 2022-10-23 stsp
150 13b2bc37 2022-10-23 stsp err = got_object_tag_open(&tag, repo_write.repo, obj);
151 13b2bc37 2022-10-23 stsp if (err)
152 13b2bc37 2022-10-23 stsp return err;
153 13b2bc37 2022-10-23 stsp
154 13b2bc37 2022-10-23 stsp if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
155 13b2bc37 2022-10-23 stsp err = got_error_from_errno("asprintf");
156 13b2bc37 2022-10-23 stsp goto done;
157 13b2bc37 2022-10-23 stsp }
158 13b2bc37 2022-10-23 stsp
159 13b2bc37 2022-10-23 stsp id = got_object_tag_get_object_id(tag);
160 13b2bc37 2022-10-23 stsp namelen = strlen(peeled_refname);
161 13b2bc37 2022-10-23 stsp
162 13b2bc37 2022-10-23 stsp len = sizeof(struct gotd_imsg_ref) + namelen;
163 13b2bc37 2022-10-23 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
164 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_NO_SPACE);
165 13b2bc37 2022-10-23 stsp goto done;
166 13b2bc37 2022-10-23 stsp }
167 13b2bc37 2022-10-23 stsp
168 13b2bc37 2022-10-23 stsp wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
169 13b2bc37 2022-10-23 stsp repo_write.pid, len);
170 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
171 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create REF");
172 13b2bc37 2022-10-23 stsp goto done;
173 13b2bc37 2022-10-23 stsp }
174 13b2bc37 2022-10-23 stsp
175 13b2bc37 2022-10-23 stsp /* Keep in sync with struct gotd_imsg_ref definition. */
176 aaa34732 2024-07-13 op if (imsg_add(wbuf, id->hash, SHA1_DIGEST_LENGTH) == -1) {
177 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add REF");
178 13b2bc37 2022-10-23 stsp goto done;
179 13b2bc37 2022-10-23 stsp }
180 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
181 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add REF");
182 13b2bc37 2022-10-23 stsp goto done;
183 13b2bc37 2022-10-23 stsp }
184 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
185 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add REF");
186 13b2bc37 2022-10-23 stsp goto done;
187 13b2bc37 2022-10-23 stsp }
188 13b2bc37 2022-10-23 stsp
189 13b2bc37 2022-10-23 stsp imsg_close(ibuf, wbuf);
190 13b2bc37 2022-10-23 stsp done:
191 13b2bc37 2022-10-23 stsp got_object_tag_close(tag);
192 13b2bc37 2022-10-23 stsp return err;
193 13b2bc37 2022-10-23 stsp }
194 13b2bc37 2022-10-23 stsp
195 13b2bc37 2022-10-23 stsp static const struct got_error *
196 13b2bc37 2022-10-23 stsp send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
197 13b2bc37 2022-10-23 stsp {
198 13b2bc37 2022-10-23 stsp const struct got_error *err;
199 13b2bc37 2022-10-23 stsp const char *refname = got_ref_get_name(ref);
200 13b2bc37 2022-10-23 stsp size_t namelen;
201 13b2bc37 2022-10-23 stsp struct got_object_id *id = NULL;
202 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
203 13b2bc37 2022-10-23 stsp size_t len;
204 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
205 13b2bc37 2022-10-23 stsp
206 13b2bc37 2022-10-23 stsp namelen = strlen(refname);
207 13b2bc37 2022-10-23 stsp
208 13b2bc37 2022-10-23 stsp len = sizeof(struct gotd_imsg_ref) + namelen;
209 13b2bc37 2022-10-23 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
210 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
211 13b2bc37 2022-10-23 stsp
212 13b2bc37 2022-10-23 stsp err = got_ref_resolve(&id, repo_write.repo, ref);
213 13b2bc37 2022-10-23 stsp if (err)
214 13b2bc37 2022-10-23 stsp return err;
215 13b2bc37 2022-10-23 stsp
216 13b2bc37 2022-10-23 stsp wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
217 13b2bc37 2022-10-23 stsp repo_write.pid, len);
218 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
219 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create REF");
220 13b2bc37 2022-10-23 stsp goto done;
221 13b2bc37 2022-10-23 stsp }
222 13b2bc37 2022-10-23 stsp
223 13b2bc37 2022-10-23 stsp /* Keep in sync with struct gotd_imsg_ref definition. */
224 aaa34732 2024-07-13 op if (imsg_add(wbuf, id->hash, SHA1_DIGEST_LENGTH) == -1)
225 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF");
226 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
227 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF");
228 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, refname, namelen) == -1)
229 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF");
230 13b2bc37 2022-10-23 stsp
231 13b2bc37 2022-10-23 stsp imsg_close(ibuf, wbuf);
232 13b2bc37 2022-10-23 stsp
233 13b2bc37 2022-10-23 stsp err = got_object_open(&obj, repo_write.repo, id);
234 13b2bc37 2022-10-23 stsp if (err)
235 13b2bc37 2022-10-23 stsp goto done;
236 13b2bc37 2022-10-23 stsp if (obj->type == GOT_OBJ_TYPE_TAG)
237 13b2bc37 2022-10-23 stsp err = send_peeled_tag_ref(ref, obj, ibuf);
238 13b2bc37 2022-10-23 stsp done:
239 13b2bc37 2022-10-23 stsp if (obj)
240 13b2bc37 2022-10-23 stsp got_object_close(obj);
241 13b2bc37 2022-10-23 stsp free(id);
242 13b2bc37 2022-10-23 stsp return err;
243 13b2bc37 2022-10-23 stsp }
244 13b2bc37 2022-10-23 stsp
245 13b2bc37 2022-10-23 stsp static const struct got_error *
246 1a52c9bf 2022-12-30 stsp list_refs(struct imsg *imsg)
247 13b2bc37 2022-10-23 stsp {
248 13b2bc37 2022-10-23 stsp const struct got_error *err;
249 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
250 13b2bc37 2022-10-23 stsp struct got_reflist_head refs;
251 13b2bc37 2022-10-23 stsp struct got_reflist_entry *re;
252 13b2bc37 2022-10-23 stsp size_t datalen;
253 13b2bc37 2022-10-23 stsp struct gotd_imsg_reflist irefs;
254 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
255 2c52c623 2024-01-30 op int client_fd;
256 13b2bc37 2022-10-23 stsp
257 13b2bc37 2022-10-23 stsp TAILQ_INIT(&refs);
258 13b2bc37 2022-10-23 stsp
259 2c52c623 2024-01-30 op client_fd = imsg_get_fd(imsg);
260 13b2bc37 2022-10-23 stsp if (client_fd == -1)
261 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
262 13b2bc37 2022-10-23 stsp
263 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
264 1cb49b67 2024-03-21 stsp if (datalen != 0)
265 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
266 13b2bc37 2022-10-23 stsp
267 1cb49b67 2024-03-21 stsp if (repo_write.refs_listed) {
268 1a52c9bf 2022-12-30 stsp return got_error_msg(GOT_ERR_CLIENT_ID,
269 1a52c9bf 2022-12-30 stsp "duplicate list-refs request");
270 1a52c9bf 2022-12-30 stsp }
271 1cb49b67 2024-03-21 stsp repo_write.refs_listed = 1;
272 1cb49b67 2024-03-21 stsp
273 1a52c9bf 2022-12-30 stsp client->fd = client_fd;
274 1a52c9bf 2022-12-30 stsp client->nref_updates = 0;
275 9a8e357c 2023-01-28 op client->nref_del = 0;
276 0ff2c315 2023-01-18 stsp client->nref_new = 0;
277 cc88020e 2023-04-11 stsp client->nref_move = 0;
278 13b2bc37 2022-10-23 stsp
279 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, client_fd);
280 13b2bc37 2022-10-23 stsp
281 13b2bc37 2022-10-23 stsp err = got_ref_list(&refs, repo_write.repo, "",
282 13b2bc37 2022-10-23 stsp got_ref_cmp_by_name, NULL);
283 13b2bc37 2022-10-23 stsp if (err)
284 13b2bc37 2022-10-23 stsp return err;
285 13b2bc37 2022-10-23 stsp
286 13b2bc37 2022-10-23 stsp memset(&irefs, 0, sizeof(irefs));
287 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(re, &refs, entry) {
288 13b2bc37 2022-10-23 stsp struct got_object_id *id;
289 13b2bc37 2022-10-23 stsp int obj_type;
290 13b2bc37 2022-10-23 stsp
291 13b2bc37 2022-10-23 stsp if (got_ref_is_symbolic(re->ref))
292 13b2bc37 2022-10-23 stsp continue;
293 13b2bc37 2022-10-23 stsp
294 13b2bc37 2022-10-23 stsp irefs.nrefs++;
295 13b2bc37 2022-10-23 stsp
296 13b2bc37 2022-10-23 stsp /* Account for a peeled tag refs. */
297 13b2bc37 2022-10-23 stsp err = got_ref_resolve(&id, repo_write.repo, re->ref);
298 13b2bc37 2022-10-23 stsp if (err)
299 13b2bc37 2022-10-23 stsp goto done;
300 e26970cc 2023-01-10 op err = got_object_get_type(&obj_type, repo_write.repo, id);
301 13b2bc37 2022-10-23 stsp free(id);
302 13b2bc37 2022-10-23 stsp if (err)
303 13b2bc37 2022-10-23 stsp goto done;
304 13b2bc37 2022-10-23 stsp if (obj_type == GOT_OBJ_TYPE_TAG)
305 13b2bc37 2022-10-23 stsp irefs.nrefs++;
306 13b2bc37 2022-10-23 stsp }
307 13b2bc37 2022-10-23 stsp
308 13b2bc37 2022-10-23 stsp if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
309 13b2bc37 2022-10-23 stsp repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
310 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_compose REFLIST");
311 13b2bc37 2022-10-23 stsp goto done;
312 13b2bc37 2022-10-23 stsp }
313 13b2bc37 2022-10-23 stsp
314 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(re, &refs, entry) {
315 13b2bc37 2022-10-23 stsp if (got_ref_is_symbolic(re->ref))
316 13b2bc37 2022-10-23 stsp continue;
317 13b2bc37 2022-10-23 stsp err = send_ref(re->ref, &ibuf);
318 13b2bc37 2022-10-23 stsp if (err)
319 13b2bc37 2022-10-23 stsp goto done;
320 13b2bc37 2022-10-23 stsp }
321 13b2bc37 2022-10-23 stsp
322 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(&ibuf);
323 13b2bc37 2022-10-23 stsp done:
324 13b2bc37 2022-10-23 stsp got_ref_list_free(&refs);
325 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
326 13b2bc37 2022-10-23 stsp return err;
327 13b2bc37 2022-10-23 stsp }
328 13b2bc37 2022-10-23 stsp
329 13b2bc37 2022-10-23 stsp static const struct got_error *
330 9afa3de2 2023-04-04 stsp validate_namespace(const char *namespace)
331 13b2bc37 2022-10-23 stsp {
332 13b2bc37 2022-10-23 stsp size_t len = strlen(namespace);
333 13b2bc37 2022-10-23 stsp
334 13b2bc37 2022-10-23 stsp if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
335 13b2bc37 2022-10-23 stsp namespace[len -1] != '/') {
336 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_REF_NAME,
337 13b2bc37 2022-10-23 stsp "reference namespace '%s'", namespace);
338 13b2bc37 2022-10-23 stsp }
339 13b2bc37 2022-10-23 stsp
340 9afa3de2 2023-04-04 stsp return NULL;
341 9afa3de2 2023-04-04 stsp }
342 9afa3de2 2023-04-04 stsp
343 9afa3de2 2023-04-04 stsp static const struct got_error *
344 9afa3de2 2023-04-04 stsp protect_ref_namespace(const char *refname, const char *namespace)
345 9afa3de2 2023-04-04 stsp {
346 9afa3de2 2023-04-04 stsp const struct got_error *err;
347 9afa3de2 2023-04-04 stsp
348 9afa3de2 2023-04-04 stsp err = validate_namespace(namespace);
349 9afa3de2 2023-04-04 stsp if (err)
350 9afa3de2 2023-04-04 stsp return err;
351 9afa3de2 2023-04-04 stsp
352 9afa3de2 2023-04-04 stsp if (strncmp(namespace, refname, strlen(namespace)) == 0)
353 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
354 13b2bc37 2022-10-23 stsp
355 13b2bc37 2022-10-23 stsp return NULL;
356 13b2bc37 2022-10-23 stsp }
357 13b2bc37 2022-10-23 stsp
358 13b2bc37 2022-10-23 stsp static const struct got_error *
359 9afa3de2 2023-04-04 stsp verify_object_type(struct got_object_id *id, int expected_obj_type,
360 9afa3de2 2023-04-04 stsp struct got_pack *pack, struct got_packidx *packidx)
361 9afa3de2 2023-04-04 stsp {
362 9afa3de2 2023-04-04 stsp const struct got_error *err;
363 9afa3de2 2023-04-04 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
364 9afa3de2 2023-04-04 stsp struct got_object *obj;
365 9afa3de2 2023-04-04 stsp int idx;
366 9afa3de2 2023-04-04 stsp const char *typestr;
367 9afa3de2 2023-04-04 stsp
368 9afa3de2 2023-04-04 stsp idx = got_packidx_get_object_idx(packidx, id);
369 9afa3de2 2023-04-04 stsp if (idx == -1) {
370 310a8ea5 2024-07-10 op got_object_id_hex(id, hex, sizeof(hex));
371 9afa3de2 2023-04-04 stsp return got_error_fmt(GOT_ERR_BAD_PACKFILE,
372 9afa3de2 2023-04-04 stsp "object %s is missing from pack file", hex);
373 9afa3de2 2023-04-04 stsp }
374 9afa3de2 2023-04-04 stsp
375 9afa3de2 2023-04-04 stsp err = got_object_open_from_packfile(&obj, id, pack, packidx,
376 9afa3de2 2023-04-04 stsp idx, repo_write.repo);
377 9afa3de2 2023-04-04 stsp if (err)
378 9afa3de2 2023-04-04 stsp return err;
379 9afa3de2 2023-04-04 stsp
380 9afa3de2 2023-04-04 stsp if (obj->type != expected_obj_type) {
381 310a8ea5 2024-07-10 op got_object_id_hex(id, hex, sizeof(hex));
382 9afa3de2 2023-04-04 stsp got_object_type_label(&typestr, expected_obj_type);
383 9afa3de2 2023-04-04 stsp err = got_error_fmt(GOT_ERR_OBJ_TYPE,
384 9afa3de2 2023-04-04 stsp "%s is not pointing at a %s object", hex, typestr);
385 9afa3de2 2023-04-04 stsp }
386 9afa3de2 2023-04-04 stsp got_object_close(obj);
387 9afa3de2 2023-04-04 stsp return err;
388 9afa3de2 2023-04-04 stsp }
389 9afa3de2 2023-04-04 stsp
390 9afa3de2 2023-04-04 stsp static const struct got_error *
391 9afa3de2 2023-04-04 stsp protect_tag_namespace(const char *namespace, struct got_pack *pack,
392 9afa3de2 2023-04-04 stsp struct got_packidx *packidx, struct gotd_ref_update *ref_update)
393 9afa3de2 2023-04-04 stsp {
394 9afa3de2 2023-04-04 stsp const struct got_error *err;
395 9afa3de2 2023-04-04 stsp
396 9afa3de2 2023-04-04 stsp err = validate_namespace(namespace);
397 9afa3de2 2023-04-04 stsp if (err)
398 9afa3de2 2023-04-04 stsp return err;
399 9afa3de2 2023-04-04 stsp
400 9afa3de2 2023-04-04 stsp if (strncmp(namespace, got_ref_get_name(ref_update->ref),
401 9afa3de2 2023-04-04 stsp strlen(namespace)) != 0)
402 9afa3de2 2023-04-04 stsp return NULL;
403 9afa3de2 2023-04-04 stsp
404 9afa3de2 2023-04-04 stsp if (!ref_update->ref_is_new)
405 9afa3de2 2023-04-04 stsp return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
406 9afa3de2 2023-04-04 stsp
407 9afa3de2 2023-04-04 stsp return verify_object_type(&ref_update->new_id, GOT_OBJ_TYPE_TAG,
408 9afa3de2 2023-04-04 stsp pack, packidx);
409 9afa3de2 2023-04-04 stsp }
410 9afa3de2 2023-04-04 stsp
411 9afa3de2 2023-04-04 stsp static const struct got_error *
412 9afa3de2 2023-04-04 stsp protect_require_yca(struct got_object_id *tip_id,
413 9afa3de2 2023-04-04 stsp size_t max_commits_to_traverse, struct got_pack *pack,
414 9afa3de2 2023-04-04 stsp struct got_packidx *packidx, struct got_reference *ref)
415 9afa3de2 2023-04-04 stsp {
416 9afa3de2 2023-04-04 stsp const struct got_error *err;
417 9afa3de2 2023-04-04 stsp uint8_t *buf = NULL;
418 9afa3de2 2023-04-04 stsp size_t len;
419 9afa3de2 2023-04-04 stsp struct got_object_id *expected_yca_id = NULL;
420 9afa3de2 2023-04-04 stsp struct got_object *obj = NULL;
421 9afa3de2 2023-04-04 stsp struct got_commit_object *commit = NULL;
422 9afa3de2 2023-04-04 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
423 9afa3de2 2023-04-04 stsp const struct got_object_id_queue *parent_ids;
424 9afa3de2 2023-04-04 stsp struct got_object_id_queue ids;
425 9afa3de2 2023-04-04 stsp struct got_object_qid *pid, *qid;
426 9afa3de2 2023-04-04 stsp struct got_object_idset *traversed_set = NULL;
427 9afa3de2 2023-04-04 stsp int found_yca = 0, obj_type;
428 9afa3de2 2023-04-04 stsp
429 9afa3de2 2023-04-04 stsp STAILQ_INIT(&ids);
430 9afa3de2 2023-04-04 stsp
431 9afa3de2 2023-04-04 stsp err = got_ref_resolve(&expected_yca_id, repo_write.repo, ref);
432 9afa3de2 2023-04-04 stsp if (err)
433 9afa3de2 2023-04-04 stsp return err;
434 7a095277 2024-03-28 op
435 9afa3de2 2023-04-04 stsp err = got_object_get_type(&obj_type, repo_write.repo, expected_yca_id);
436 9afa3de2 2023-04-04 stsp if (err)
437 9afa3de2 2023-04-04 stsp goto done;
438 9afa3de2 2023-04-04 stsp
439 9afa3de2 2023-04-04 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
440 310a8ea5 2024-07-10 op got_object_id_hex(expected_yca_id, hex, sizeof(hex));
441 9afa3de2 2023-04-04 stsp err = got_error_fmt(GOT_ERR_OBJ_TYPE,
442 9afa3de2 2023-04-04 stsp "%s is not pointing at a commit object", hex);
443 9afa3de2 2023-04-04 stsp goto done;
444 9afa3de2 2023-04-04 stsp }
445 9afa3de2 2023-04-04 stsp
446 9afa3de2 2023-04-04 stsp traversed_set = got_object_idset_alloc();
447 9afa3de2 2023-04-04 stsp if (traversed_set == NULL) {
448 9afa3de2 2023-04-04 stsp err = got_error_from_errno("got_object_idset_alloc");
449 9afa3de2 2023-04-04 stsp goto done;
450 9afa3de2 2023-04-04 stsp }
451 9afa3de2 2023-04-04 stsp
452 9afa3de2 2023-04-04 stsp err = got_object_qid_alloc(&qid, tip_id);
453 9afa3de2 2023-04-04 stsp if (err)
454 9afa3de2 2023-04-04 stsp goto done;
455 9afa3de2 2023-04-04 stsp STAILQ_INSERT_TAIL(&ids, qid, entry);
456 9afa3de2 2023-04-04 stsp while (!STAILQ_EMPTY(&ids)) {
457 9afa3de2 2023-04-04 stsp err = check_cancelled(NULL);
458 9afa3de2 2023-04-04 stsp if (err)
459 9afa3de2 2023-04-04 stsp break;
460 9afa3de2 2023-04-04 stsp
461 9afa3de2 2023-04-04 stsp qid = STAILQ_FIRST(&ids);
462 9afa3de2 2023-04-04 stsp if (got_object_id_cmp(&qid->id, expected_yca_id) == 0) {
463 9afa3de2 2023-04-04 stsp found_yca = 1;
464 9afa3de2 2023-04-04 stsp break;
465 9afa3de2 2023-04-04 stsp }
466 9afa3de2 2023-04-04 stsp
467 9afa3de2 2023-04-04 stsp if (got_object_idset_num_elements(traversed_set) >=
468 9afa3de2 2023-04-04 stsp max_commits_to_traverse)
469 9afa3de2 2023-04-04 stsp break;
470 9afa3de2 2023-04-04 stsp
471 9afa3de2 2023-04-04 stsp if (got_object_idset_contains(traversed_set, &qid->id)) {
472 9afa3de2 2023-04-04 stsp STAILQ_REMOVE_HEAD(&ids, entry);
473 9afa3de2 2023-04-04 stsp got_object_qid_free(qid);
474 9afa3de2 2023-04-04 stsp qid = NULL;
475 9afa3de2 2023-04-04 stsp continue;
476 9afa3de2 2023-04-04 stsp }
477 9afa3de2 2023-04-04 stsp err = got_object_idset_add(traversed_set, &qid->id, NULL);
478 9afa3de2 2023-04-04 stsp if (err)
479 9afa3de2 2023-04-04 stsp goto done;
480 9afa3de2 2023-04-04 stsp
481 9afa3de2 2023-04-04 stsp err = got_object_open(&obj, repo_write.repo, &qid->id);
482 9afa3de2 2023-04-04 stsp if (err && err->code != GOT_ERR_NO_OBJ)
483 9afa3de2 2023-04-04 stsp goto done;
484 9afa3de2 2023-04-04 stsp err = NULL;
485 9afa3de2 2023-04-04 stsp if (obj) {
486 9afa3de2 2023-04-04 stsp err = got_object_commit_open(&commit, repo_write.repo,
487 9afa3de2 2023-04-04 stsp obj);
488 9afa3de2 2023-04-04 stsp if (err)
489 9afa3de2 2023-04-04 stsp goto done;
490 9afa3de2 2023-04-04 stsp } else {
491 9afa3de2 2023-04-04 stsp int idx;
492 9afa3de2 2023-04-04 stsp
493 9afa3de2 2023-04-04 stsp idx = got_packidx_get_object_idx(packidx, &qid->id);
494 9afa3de2 2023-04-04 stsp if (idx == -1) {
495 310a8ea5 2024-07-10 op got_object_id_hex(&qid->id, hex, sizeof(hex));
496 9afa3de2 2023-04-04 stsp err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
497 9afa3de2 2023-04-04 stsp "object %s is missing from pack file", hex);
498 9afa3de2 2023-04-04 stsp goto done;
499 9afa3de2 2023-04-04 stsp }
500 9afa3de2 2023-04-04 stsp
501 9afa3de2 2023-04-04 stsp err = got_object_open_from_packfile(&obj, &qid->id,
502 9afa3de2 2023-04-04 stsp pack, packidx, idx, repo_write.repo);
503 9afa3de2 2023-04-04 stsp if (err)
504 9afa3de2 2023-04-04 stsp goto done;
505 9afa3de2 2023-04-04 stsp
506 9afa3de2 2023-04-04 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
507 310a8ea5 2024-07-10 op got_object_id_hex(&qid->id, hex, sizeof(hex));
508 9afa3de2 2023-04-04 stsp err = got_error_fmt(GOT_ERR_OBJ_TYPE,
509 9afa3de2 2023-04-04 stsp "%s is not pointing at a commit object",
510 9afa3de2 2023-04-04 stsp hex);
511 9afa3de2 2023-04-04 stsp goto done;
512 9afa3de2 2023-04-04 stsp }
513 9afa3de2 2023-04-04 stsp
514 9afa3de2 2023-04-04 stsp err = got_packfile_extract_object_to_mem(&buf, &len,
515 9afa3de2 2023-04-04 stsp obj, pack);
516 9afa3de2 2023-04-04 stsp if (err)
517 9afa3de2 2023-04-04 stsp goto done;
518 9afa3de2 2023-04-04 stsp
519 8fdd8178 2024-07-12 op err = got_object_parse_commit(&commit, buf, len,
520 8fdd8178 2024-07-12 op GOT_HASH_SHA1);
521 9afa3de2 2023-04-04 stsp if (err)
522 9afa3de2 2023-04-04 stsp goto done;
523 9afa3de2 2023-04-04 stsp
524 9afa3de2 2023-04-04 stsp free(buf);
525 9afa3de2 2023-04-04 stsp buf = NULL;
526 9afa3de2 2023-04-04 stsp }
527 9afa3de2 2023-04-04 stsp
528 9afa3de2 2023-04-04 stsp got_object_close(obj);
529 9afa3de2 2023-04-04 stsp obj = NULL;
530 9afa3de2 2023-04-04 stsp
531 9afa3de2 2023-04-04 stsp STAILQ_REMOVE_HEAD(&ids, entry);
532 9afa3de2 2023-04-04 stsp got_object_qid_free(qid);
533 9afa3de2 2023-04-04 stsp qid = NULL;
534 9afa3de2 2023-04-04 stsp
535 9afa3de2 2023-04-04 stsp if (got_object_commit_get_nparents(commit) == 0)
536 9afa3de2 2023-04-04 stsp break;
537 9afa3de2 2023-04-04 stsp
538 9afa3de2 2023-04-04 stsp parent_ids = got_object_commit_get_parent_ids(commit);
539 9afa3de2 2023-04-04 stsp STAILQ_FOREACH(pid, parent_ids, entry) {
540 9afa3de2 2023-04-04 stsp err = check_cancelled(NULL);
541 9afa3de2 2023-04-04 stsp if (err)
542 9afa3de2 2023-04-04 stsp goto done;
543 9afa3de2 2023-04-04 stsp err = got_object_qid_alloc(&qid, &pid->id);
544 9afa3de2 2023-04-04 stsp if (err)
545 9afa3de2 2023-04-04 stsp goto done;
546 9afa3de2 2023-04-04 stsp STAILQ_INSERT_TAIL(&ids, qid, entry);
547 9afa3de2 2023-04-04 stsp qid = NULL;
548 9afa3de2 2023-04-04 stsp }
549 9afa3de2 2023-04-04 stsp got_object_commit_close(commit);
550 9afa3de2 2023-04-04 stsp commit = NULL;
551 9afa3de2 2023-04-04 stsp }
552 9afa3de2 2023-04-04 stsp
553 9afa3de2 2023-04-04 stsp if (!found_yca) {
554 9afa3de2 2023-04-04 stsp err = got_error_fmt(GOT_ERR_REF_PROTECTED, "%s",
555 9afa3de2 2023-04-04 stsp got_ref_get_name(ref));
556 9afa3de2 2023-04-04 stsp }
557 9afa3de2 2023-04-04 stsp done:
558 9afa3de2 2023-04-04 stsp got_object_idset_free(traversed_set);
559 9afa3de2 2023-04-04 stsp got_object_id_queue_free(&ids);
560 9afa3de2 2023-04-04 stsp free(buf);
561 9afa3de2 2023-04-04 stsp if (obj)
562 9afa3de2 2023-04-04 stsp got_object_close(obj);
563 9afa3de2 2023-04-04 stsp if (commit)
564 9afa3de2 2023-04-04 stsp got_object_commit_close(commit);
565 9afa3de2 2023-04-04 stsp free(expected_yca_id);
566 9afa3de2 2023-04-04 stsp return err;
567 9afa3de2 2023-04-04 stsp }
568 9afa3de2 2023-04-04 stsp
569 9afa3de2 2023-04-04 stsp static const struct got_error *
570 9afa3de2 2023-04-04 stsp protect_branch_namespace(const char *namespace, struct got_pack *pack,
571 9afa3de2 2023-04-04 stsp struct got_packidx *packidx, struct gotd_ref_update *ref_update)
572 9afa3de2 2023-04-04 stsp {
573 9afa3de2 2023-04-04 stsp const struct got_error *err;
574 9afa3de2 2023-04-04 stsp
575 9afa3de2 2023-04-04 stsp err = validate_namespace(namespace);
576 9afa3de2 2023-04-04 stsp if (err)
577 9afa3de2 2023-04-04 stsp return err;
578 9afa3de2 2023-04-04 stsp
579 9afa3de2 2023-04-04 stsp if (strncmp(namespace, got_ref_get_name(ref_update->ref),
580 9afa3de2 2023-04-04 stsp strlen(namespace)) != 0)
581 9afa3de2 2023-04-04 stsp return NULL;
582 9afa3de2 2023-04-04 stsp
583 9afa3de2 2023-04-04 stsp if (ref_update->ref_is_new) {
584 9afa3de2 2023-04-04 stsp return verify_object_type(&ref_update->new_id,
585 9afa3de2 2023-04-04 stsp GOT_OBJ_TYPE_COMMIT, pack, packidx);
586 9afa3de2 2023-04-04 stsp }
587 9afa3de2 2023-04-04 stsp
588 9afa3de2 2023-04-04 stsp return protect_require_yca(&ref_update->new_id,
589 9afa3de2 2023-04-04 stsp be32toh(packidx->hdr.fanout_table[0xff]), pack, packidx,
590 9afa3de2 2023-04-04 stsp ref_update->ref);
591 9afa3de2 2023-04-04 stsp }
592 9afa3de2 2023-04-04 stsp
593 9afa3de2 2023-04-04 stsp static const struct got_error *
594 9afa3de2 2023-04-04 stsp protect_branch(const char *refname, struct got_pack *pack,
595 9afa3de2 2023-04-04 stsp struct got_packidx *packidx, struct gotd_ref_update *ref_update)
596 9afa3de2 2023-04-04 stsp {
597 9afa3de2 2023-04-04 stsp if (strcmp(refname, got_ref_get_name(ref_update->ref)) != 0)
598 9afa3de2 2023-04-04 stsp return NULL;
599 9afa3de2 2023-04-04 stsp
600 9afa3de2 2023-04-04 stsp /* Always allow new branches to be created. */
601 9afa3de2 2023-04-04 stsp if (ref_update->ref_is_new) {
602 9afa3de2 2023-04-04 stsp return verify_object_type(&ref_update->new_id,
603 9afa3de2 2023-04-04 stsp GOT_OBJ_TYPE_COMMIT, pack, packidx);
604 9afa3de2 2023-04-04 stsp }
605 9afa3de2 2023-04-04 stsp
606 9afa3de2 2023-04-04 stsp return protect_require_yca(&ref_update->new_id,
607 9afa3de2 2023-04-04 stsp be32toh(packidx->hdr.fanout_table[0xff]), pack, packidx,
608 9afa3de2 2023-04-04 stsp ref_update->ref);
609 9afa3de2 2023-04-04 stsp }
610 9afa3de2 2023-04-04 stsp
611 9afa3de2 2023-04-04 stsp static const struct got_error *
612 1a52c9bf 2022-12-30 stsp recv_ref_update(struct imsg *imsg)
613 13b2bc37 2022-10-23 stsp {
614 9a8e357c 2023-01-28 op static const char zero_id[SHA1_DIGEST_LENGTH];
615 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
616 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
617 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update iref;
618 13b2bc37 2022-10-23 stsp size_t datalen;
619 13b2bc37 2022-10-23 stsp char *refname = NULL;
620 13b2bc37 2022-10-23 stsp struct got_reference *ref = NULL;
621 13b2bc37 2022-10-23 stsp struct got_object_id *id = NULL;
622 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
623 13b2bc37 2022-10-23 stsp struct gotd_ref_update *ref_update = NULL;
624 13b2bc37 2022-10-23 stsp
625 13b2bc37 2022-10-23 stsp log_debug("ref-update received");
626 13b2bc37 2022-10-23 stsp
627 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
628 13b2bc37 2022-10-23 stsp if (datalen < sizeof(iref))
629 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
630 13b2bc37 2022-10-23 stsp memcpy(&iref, imsg->data, sizeof(iref));
631 13b2bc37 2022-10-23 stsp if (datalen != sizeof(iref) + iref.name_len)
632 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
633 13b2bc37 2022-10-23 stsp
634 1a52c9bf 2022-12-30 stsp imsg_init(&ibuf, client->fd);
635 13b2bc37 2022-10-23 stsp
636 00b3e9ae 2023-01-11 op refname = strndup(imsg->data + sizeof(iref), iref.name_len);
637 13b2bc37 2022-10-23 stsp if (refname == NULL)
638 00b3e9ae 2023-01-11 op return got_error_from_errno("strndup");
639 13b2bc37 2022-10-23 stsp
640 13b2bc37 2022-10-23 stsp ref_update = calloc(1, sizeof(*ref_update));
641 13b2bc37 2022-10-23 stsp if (ref_update == NULL) {
642 13b2bc37 2022-10-23 stsp err = got_error_from_errno("malloc");
643 13b2bc37 2022-10-23 stsp goto done;
644 13b2bc37 2022-10-23 stsp }
645 13b2bc37 2022-10-23 stsp
646 aaa34732 2024-07-13 op memcpy(ref_update->old_id.hash, iref.old_id, SHA1_DIGEST_LENGTH);
647 aaa34732 2024-07-13 op memcpy(ref_update->new_id.hash, iref.new_id, SHA1_DIGEST_LENGTH);
648 13b2bc37 2022-10-23 stsp
649 13b2bc37 2022-10-23 stsp err = got_ref_open(&ref, repo_write.repo, refname, 0);
650 13b2bc37 2022-10-23 stsp if (err) {
651 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_NOT_REF)
652 13b2bc37 2022-10-23 stsp goto done;
653 aaa34732 2024-07-13 op if (memcmp(ref_update->new_id.hash,
654 9afa3de2 2023-04-04 stsp zero_id, sizeof(zero_id)) == 0) {
655 9afa3de2 2023-04-04 stsp err = got_error_fmt(GOT_ERR_BAD_OBJ_ID,
656 9afa3de2 2023-04-04 stsp "%s", refname);
657 9afa3de2 2023-04-04 stsp goto done;
658 9afa3de2 2023-04-04 stsp }
659 13b2bc37 2022-10-23 stsp err = got_ref_alloc(&ref, refname, &ref_update->new_id);
660 13b2bc37 2022-10-23 stsp if (err)
661 13b2bc37 2022-10-23 stsp goto done;
662 13b2bc37 2022-10-23 stsp ref_update->ref_is_new = 1;
663 0ff2c315 2023-01-18 stsp client->nref_new++;
664 13b2bc37 2022-10-23 stsp }
665 13b2bc37 2022-10-23 stsp if (got_ref_is_symbolic(ref)) {
666 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
667 13b2bc37 2022-10-23 stsp "'%s' is a symbolic reference and cannot "
668 13b2bc37 2022-10-23 stsp "be updated", 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 if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
672 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
673 13b2bc37 2022-10-23 stsp "%s: does not begin with 'refs/'",
674 13b2bc37 2022-10-23 stsp got_ref_get_name(ref));
675 13b2bc37 2022-10-23 stsp goto done;
676 13b2bc37 2022-10-23 stsp }
677 13b2bc37 2022-10-23 stsp
678 9afa3de2 2023-04-04 stsp err = protect_ref_namespace(got_ref_get_name(ref), "refs/got/");
679 13b2bc37 2022-10-23 stsp if (err)
680 13b2bc37 2022-10-23 stsp goto done;
681 9afa3de2 2023-04-04 stsp err = protect_ref_namespace(got_ref_get_name(ref), "refs/remotes/");
682 13b2bc37 2022-10-23 stsp if (err)
683 13b2bc37 2022-10-23 stsp goto done;
684 13b2bc37 2022-10-23 stsp
685 13b2bc37 2022-10-23 stsp if (!ref_update->ref_is_new) {
686 13b2bc37 2022-10-23 stsp /*
687 13b2bc37 2022-10-23 stsp * Ensure the client's idea of this update is still valid.
688 13b2bc37 2022-10-23 stsp * At this point we can only return an error, to prevent
689 13b2bc37 2022-10-23 stsp * the client from uploading a pack file which will likely
690 13b2bc37 2022-10-23 stsp * have to be discarded.
691 13b2bc37 2022-10-23 stsp */
692 13b2bc37 2022-10-23 stsp err = got_ref_resolve(&id, repo_write.repo, ref);
693 13b2bc37 2022-10-23 stsp if (err)
694 13b2bc37 2022-10-23 stsp goto done;
695 13b2bc37 2022-10-23 stsp
696 13b2bc37 2022-10-23 stsp if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
697 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
698 13b2bc37 2022-10-23 stsp "%s has been modified by someone else "
699 13b2bc37 2022-10-23 stsp "while transaction was in progress",
700 13b2bc37 2022-10-23 stsp got_ref_get_name(ref));
701 13b2bc37 2022-10-23 stsp goto done;
702 13b2bc37 2022-10-23 stsp }
703 13b2bc37 2022-10-23 stsp }
704 13b2bc37 2022-10-23 stsp
705 13b2bc37 2022-10-23 stsp gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
706 13b2bc37 2022-10-23 stsp repo_write.pid);
707 13b2bc37 2022-10-23 stsp
708 13b2bc37 2022-10-23 stsp ref_update->ref = ref;
709 aaa34732 2024-07-13 op if (memcmp(ref_update->new_id.hash, zero_id, sizeof(zero_id)) == 0) {
710 9a8e357c 2023-01-28 op ref_update->delete_ref = 1;
711 9a8e357c 2023-01-28 op client->nref_del++;
712 9a8e357c 2023-01-28 op }
713 1a52c9bf 2022-12-30 stsp STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
714 1a52c9bf 2022-12-30 stsp client->nref_updates++;
715 13b2bc37 2022-10-23 stsp ref = NULL;
716 13b2bc37 2022-10-23 stsp ref_update = NULL;
717 13b2bc37 2022-10-23 stsp done:
718 13b2bc37 2022-10-23 stsp if (ref)
719 13b2bc37 2022-10-23 stsp got_ref_close(ref);
720 13b2bc37 2022-10-23 stsp free(ref_update);
721 13b2bc37 2022-10-23 stsp free(refname);
722 13b2bc37 2022-10-23 stsp free(id);
723 13b2bc37 2022-10-23 stsp return err;
724 13b2bc37 2022-10-23 stsp }
725 13b2bc37 2022-10-23 stsp
726 13b2bc37 2022-10-23 stsp static const struct got_error *
727 13b2bc37 2022-10-23 stsp pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
728 13b2bc37 2022-10-23 stsp uint32_t nobj_loose, uint32_t nobj_resolved)
729 13b2bc37 2022-10-23 stsp {
730 13b2bc37 2022-10-23 stsp int p_indexed = 0, p_resolved = 0;
731 13b2bc37 2022-10-23 stsp int nobj_delta = nobj_total - nobj_loose;
732 13b2bc37 2022-10-23 stsp
733 13b2bc37 2022-10-23 stsp if (nobj_total > 0)
734 13b2bc37 2022-10-23 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
735 13b2bc37 2022-10-23 stsp
736 13b2bc37 2022-10-23 stsp if (nobj_delta > 0)
737 13b2bc37 2022-10-23 stsp p_resolved = (nobj_resolved * 100) / nobj_delta;
738 13b2bc37 2022-10-23 stsp
739 13b2bc37 2022-10-23 stsp if (p_resolved > 0) {
740 13b2bc37 2022-10-23 stsp log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
741 13b2bc37 2022-10-23 stsp nobj_total, p_indexed, nobj_delta, p_resolved);
742 13b2bc37 2022-10-23 stsp } else
743 13b2bc37 2022-10-23 stsp log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
744 13b2bc37 2022-10-23 stsp
745 13b2bc37 2022-10-23 stsp return NULL;
746 13b2bc37 2022-10-23 stsp }
747 13b2bc37 2022-10-23 stsp
748 13b2bc37 2022-10-23 stsp static const struct got_error *
749 13b2bc37 2022-10-23 stsp read_more_pack_stream(int infd, BUF *buf, size_t minsize)
750 13b2bc37 2022-10-23 stsp {
751 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
752 13b2bc37 2022-10-23 stsp uint8_t readahead[65536];
753 13b2bc37 2022-10-23 stsp size_t have, newlen;
754 13b2bc37 2022-10-23 stsp
755 13b2bc37 2022-10-23 stsp err = got_poll_read_full(infd, &have,
756 13b2bc37 2022-10-23 stsp readahead, sizeof(readahead), minsize);
757 13b2bc37 2022-10-23 stsp if (err)
758 13b2bc37 2022-10-23 stsp return err;
759 13b2bc37 2022-10-23 stsp
760 13b2bc37 2022-10-23 stsp err = buf_append(&newlen, buf, readahead, have);
761 13b2bc37 2022-10-23 stsp if (err)
762 13b2bc37 2022-10-23 stsp return err;
763 e26970cc 2023-01-10 op return NULL;
764 13b2bc37 2022-10-23 stsp }
765 13b2bc37 2022-10-23 stsp
766 13b2bc37 2022-10-23 stsp static const struct got_error *
767 13b2bc37 2022-10-23 stsp copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
768 ae25a666 2023-02-23 op off_t *outsize, BUF *buf, size_t *buf_pos, struct got_hash *ctx)
769 13b2bc37 2022-10-23 stsp {
770 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
771 13b2bc37 2022-10-23 stsp uint8_t t = 0;
772 13b2bc37 2022-10-23 stsp uint64_t s = 0;
773 13b2bc37 2022-10-23 stsp uint8_t sizebuf[8];
774 13b2bc37 2022-10-23 stsp size_t i = 0;
775 13b2bc37 2022-10-23 stsp off_t obj_offset = *outsize;
776 13b2bc37 2022-10-23 stsp
777 13b2bc37 2022-10-23 stsp do {
778 13b2bc37 2022-10-23 stsp /* We do not support size values which don't fit in 64 bit. */
779 13b2bc37 2022-10-23 stsp if (i > 9)
780 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
781 22ec3416 2022-10-25 op "packfile offset %lld", (long long)obj_offset);
782 13b2bc37 2022-10-23 stsp
783 13b2bc37 2022-10-23 stsp if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
784 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
785 13b2bc37 2022-10-23 stsp sizeof(sizebuf[0]));
786 13b2bc37 2022-10-23 stsp if (err)
787 13b2bc37 2022-10-23 stsp return err;
788 13b2bc37 2022-10-23 stsp }
789 13b2bc37 2022-10-23 stsp
790 13b2bc37 2022-10-23 stsp sizebuf[i] = buf_getc(buf, *buf_pos);
791 13b2bc37 2022-10-23 stsp *buf_pos += sizeof(sizebuf[i]);
792 13b2bc37 2022-10-23 stsp
793 13b2bc37 2022-10-23 stsp if (i == 0) {
794 13b2bc37 2022-10-23 stsp t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
795 13b2bc37 2022-10-23 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
796 13b2bc37 2022-10-23 stsp s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
797 13b2bc37 2022-10-23 stsp } else {
798 13b2bc37 2022-10-23 stsp size_t shift = 4 + 7 * (i - 1);
799 13b2bc37 2022-10-23 stsp s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
800 13b2bc37 2022-10-23 stsp shift);
801 13b2bc37 2022-10-23 stsp }
802 13b2bc37 2022-10-23 stsp i++;
803 13b2bc37 2022-10-23 stsp } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
804 13b2bc37 2022-10-23 stsp
805 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, sizebuf, i, ctx);
806 13b2bc37 2022-10-23 stsp if (err)
807 13b2bc37 2022-10-23 stsp return err;
808 13b2bc37 2022-10-23 stsp *outsize += i;
809 13b2bc37 2022-10-23 stsp
810 13b2bc37 2022-10-23 stsp *type = t;
811 13b2bc37 2022-10-23 stsp *size = s;
812 13b2bc37 2022-10-23 stsp return NULL;
813 13b2bc37 2022-10-23 stsp }
814 13b2bc37 2022-10-23 stsp
815 13b2bc37 2022-10-23 stsp static const struct got_error *
816 13b2bc37 2022-10-23 stsp copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
817 ae25a666 2023-02-23 op struct got_hash *ctx)
818 13b2bc37 2022-10-23 stsp {
819 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
820 13b2bc37 2022-10-23 stsp size_t remain = buf_len(buf) - *buf_pos;
821 13b2bc37 2022-10-23 stsp
822 13b2bc37 2022-10-23 stsp if (remain < SHA1_DIGEST_LENGTH) {
823 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
824 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH - remain);
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
829 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
830 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH, ctx);
831 13b2bc37 2022-10-23 stsp if (err)
832 13b2bc37 2022-10-23 stsp return err;
833 13b2bc37 2022-10-23 stsp
834 13b2bc37 2022-10-23 stsp *buf_pos += SHA1_DIGEST_LENGTH;
835 13b2bc37 2022-10-23 stsp return NULL;
836 13b2bc37 2022-10-23 stsp }
837 13b2bc37 2022-10-23 stsp
838 13b2bc37 2022-10-23 stsp static const struct got_error *
839 13b2bc37 2022-10-23 stsp copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
840 ae25a666 2023-02-23 op struct got_hash *ctx)
841 13b2bc37 2022-10-23 stsp {
842 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
843 13b2bc37 2022-10-23 stsp uint64_t o = 0;
844 13b2bc37 2022-10-23 stsp uint8_t offbuf[8];
845 13b2bc37 2022-10-23 stsp size_t i = 0;
846 13b2bc37 2022-10-23 stsp off_t obj_offset = *outsize;
847 13b2bc37 2022-10-23 stsp
848 13b2bc37 2022-10-23 stsp do {
849 13b2bc37 2022-10-23 stsp /* We do not support offset values which don't fit in 64 bit. */
850 13b2bc37 2022-10-23 stsp if (i > 8)
851 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
852 22ec3416 2022-10-25 op "packfile offset %lld", (long long)obj_offset);
853 13b2bc37 2022-10-23 stsp
854 13b2bc37 2022-10-23 stsp if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
855 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
856 13b2bc37 2022-10-23 stsp sizeof(offbuf[0]));
857 13b2bc37 2022-10-23 stsp if (err)
858 13b2bc37 2022-10-23 stsp return err;
859 13b2bc37 2022-10-23 stsp }
860 13b2bc37 2022-10-23 stsp
861 13b2bc37 2022-10-23 stsp offbuf[i] = buf_getc(buf, *buf_pos);
862 13b2bc37 2022-10-23 stsp *buf_pos += sizeof(offbuf[i]);
863 13b2bc37 2022-10-23 stsp
864 13b2bc37 2022-10-23 stsp if (i == 0)
865 13b2bc37 2022-10-23 stsp o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
866 13b2bc37 2022-10-23 stsp else {
867 13b2bc37 2022-10-23 stsp o++;
868 13b2bc37 2022-10-23 stsp o <<= 7;
869 13b2bc37 2022-10-23 stsp o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
870 13b2bc37 2022-10-23 stsp }
871 13b2bc37 2022-10-23 stsp i++;
872 13b2bc37 2022-10-23 stsp } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
873 13b2bc37 2022-10-23 stsp
874 13b2bc37 2022-10-23 stsp if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
875 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PACK_OFFSET);
876 13b2bc37 2022-10-23 stsp
877 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, offbuf, i, ctx);
878 13b2bc37 2022-10-23 stsp if (err)
879 13b2bc37 2022-10-23 stsp return err;
880 13b2bc37 2022-10-23 stsp
881 13b2bc37 2022-10-23 stsp *outsize += i;
882 13b2bc37 2022-10-23 stsp return NULL;
883 13b2bc37 2022-10-23 stsp }
884 13b2bc37 2022-10-23 stsp
885 13b2bc37 2022-10-23 stsp static const struct got_error *
886 13b2bc37 2022-10-23 stsp copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
887 ae25a666 2023-02-23 op struct got_hash *ctx)
888 13b2bc37 2022-10-23 stsp {
889 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
890 13b2bc37 2022-10-23 stsp z_stream z;
891 13b2bc37 2022-10-23 stsp int zret;
892 13b2bc37 2022-10-23 stsp char voidbuf[1024];
893 13b2bc37 2022-10-23 stsp size_t consumed_total = 0;
894 13b2bc37 2022-10-23 stsp off_t zstream_offset = *outsize;
895 13b2bc37 2022-10-23 stsp
896 13b2bc37 2022-10-23 stsp memset(&z, 0, sizeof(z));
897 13b2bc37 2022-10-23 stsp
898 13b2bc37 2022-10-23 stsp z.zalloc = Z_NULL;
899 13b2bc37 2022-10-23 stsp z.zfree = Z_NULL;
900 13b2bc37 2022-10-23 stsp zret = inflateInit(&z);
901 13b2bc37 2022-10-23 stsp if (zret != Z_OK) {
902 13b2bc37 2022-10-23 stsp if (zret == Z_ERRNO)
903 13b2bc37 2022-10-23 stsp return got_error_from_errno("inflateInit");
904 13b2bc37 2022-10-23 stsp if (zret == Z_MEM_ERROR) {
905 13b2bc37 2022-10-23 stsp errno = ENOMEM;
906 13b2bc37 2022-10-23 stsp return got_error_from_errno("inflateInit");
907 13b2bc37 2022-10-23 stsp }
908 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_DECOMPRESSION,
909 13b2bc37 2022-10-23 stsp "inflateInit failed");
910 13b2bc37 2022-10-23 stsp }
911 13b2bc37 2022-10-23 stsp
912 13b2bc37 2022-10-23 stsp while (zret != Z_STREAM_END) {
913 13b2bc37 2022-10-23 stsp size_t last_total_in, consumed;
914 13b2bc37 2022-10-23 stsp
915 13b2bc37 2022-10-23 stsp /*
916 13b2bc37 2022-10-23 stsp * Decompress into the void. Object data will be parsed
917 13b2bc37 2022-10-23 stsp * later, when the pack file is indexed. For now, we just
918 13b2bc37 2022-10-23 stsp * want to locate the end of the compressed stream.
919 13b2bc37 2022-10-23 stsp */
920 13b2bc37 2022-10-23 stsp while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
921 13b2bc37 2022-10-23 stsp last_total_in = z.total_in;
922 13b2bc37 2022-10-23 stsp z.next_in = buf_get(buf) + *buf_pos;
923 13b2bc37 2022-10-23 stsp z.avail_in = buf_len(buf) - *buf_pos;
924 13b2bc37 2022-10-23 stsp z.next_out = voidbuf;
925 13b2bc37 2022-10-23 stsp z.avail_out = sizeof(voidbuf);
926 13b2bc37 2022-10-23 stsp
927 13b2bc37 2022-10-23 stsp zret = inflate(&z, Z_SYNC_FLUSH);
928 13b2bc37 2022-10-23 stsp if (zret != Z_OK && zret != Z_BUF_ERROR &&
929 13b2bc37 2022-10-23 stsp zret != Z_STREAM_END) {
930 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_DECOMPRESSION,
931 22ec3416 2022-10-25 op "packfile offset %lld",
932 22ec3416 2022-10-25 op (long long)zstream_offset);
933 13b2bc37 2022-10-23 stsp goto done;
934 13b2bc37 2022-10-23 stsp }
935 13b2bc37 2022-10-23 stsp consumed = z.total_in - last_total_in;
936 13b2bc37 2022-10-23 stsp
937 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
938 13b2bc37 2022-10-23 stsp consumed, ctx);
939 13b2bc37 2022-10-23 stsp if (err)
940 13b2bc37 2022-10-23 stsp goto done;
941 13b2bc37 2022-10-23 stsp
942 13b2bc37 2022-10-23 stsp err = buf_discard(buf, *buf_pos + consumed);
943 13b2bc37 2022-10-23 stsp if (err)
944 13b2bc37 2022-10-23 stsp goto done;
945 13b2bc37 2022-10-23 stsp *buf_pos = 0;
946 13b2bc37 2022-10-23 stsp
947 13b2bc37 2022-10-23 stsp consumed_total += consumed;
948 13b2bc37 2022-10-23 stsp }
949 13b2bc37 2022-10-23 stsp
950 13b2bc37 2022-10-23 stsp if (zret != Z_STREAM_END) {
951 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf, 1);
952 13b2bc37 2022-10-23 stsp if (err)
953 13b2bc37 2022-10-23 stsp goto done;
954 13b2bc37 2022-10-23 stsp }
955 13b2bc37 2022-10-23 stsp }
956 13b2bc37 2022-10-23 stsp
957 13b2bc37 2022-10-23 stsp if (err == NULL)
958 13b2bc37 2022-10-23 stsp *outsize += consumed_total;
959 13b2bc37 2022-10-23 stsp done:
960 13b2bc37 2022-10-23 stsp inflateEnd(&z);
961 13b2bc37 2022-10-23 stsp return err;
962 13b2bc37 2022-10-23 stsp }
963 13b2bc37 2022-10-23 stsp
964 13b2bc37 2022-10-23 stsp static const struct got_error *
965 13b2bc37 2022-10-23 stsp validate_object_type(int obj_type)
966 13b2bc37 2022-10-23 stsp {
967 13b2bc37 2022-10-23 stsp switch (obj_type) {
968 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_BLOB:
969 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_COMMIT:
970 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_TREE:
971 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_TAG:
972 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
973 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
974 13b2bc37 2022-10-23 stsp return NULL;
975 13b2bc37 2022-10-23 stsp default:
976 13b2bc37 2022-10-23 stsp break;
977 13b2bc37 2022-10-23 stsp }
978 13b2bc37 2022-10-23 stsp
979 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_OBJ_TYPE);
980 13b2bc37 2022-10-23 stsp }
981 13b2bc37 2022-10-23 stsp
982 13b2bc37 2022-10-23 stsp static const struct got_error *
983 cc88020e 2023-04-11 stsp ensure_all_objects_exist_locally(struct gotd_ref_updates *ref_updates)
984 cc88020e 2023-04-11 stsp {
985 cc88020e 2023-04-11 stsp const struct got_error *err = NULL;
986 cc88020e 2023-04-11 stsp struct gotd_ref_update *ref_update;
987 cc88020e 2023-04-11 stsp struct got_object *obj;
988 cc88020e 2023-04-11 stsp
989 cc88020e 2023-04-11 stsp STAILQ_FOREACH(ref_update, ref_updates, entry) {
990 cc88020e 2023-04-11 stsp err = got_object_open(&obj, repo_write.repo,
991 cc88020e 2023-04-11 stsp &ref_update->new_id);
992 cc88020e 2023-04-11 stsp if (err)
993 cc88020e 2023-04-11 stsp return err;
994 cc88020e 2023-04-11 stsp got_object_close(obj);
995 cc88020e 2023-04-11 stsp }
996 cc88020e 2023-04-11 stsp
997 cc88020e 2023-04-11 stsp return NULL;
998 cc88020e 2023-04-11 stsp }
999 cc88020e 2023-04-11 stsp
1000 cc88020e 2023-04-11 stsp static const struct got_error *
1001 0ff2c315 2023-01-18 stsp recv_packdata(off_t *outsize, uint32_t *nobj, uint8_t *sha1,
1002 0ff2c315 2023-01-18 stsp int infd, int outfd)
1003 13b2bc37 2022-10-23 stsp {
1004 13b2bc37 2022-10-23 stsp const struct got_error *err;
1005 0ff2c315 2023-01-18 stsp struct repo_write_client *client = &repo_write_client;
1006 13b2bc37 2022-10-23 stsp struct got_packfile_hdr hdr;
1007 13b2bc37 2022-10-23 stsp size_t have;
1008 0ff2c315 2023-01-18 stsp uint32_t nhave = 0;
1009 ae25a666 2023-02-23 op struct got_hash ctx;
1010 13b2bc37 2022-10-23 stsp uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
1011 13b2bc37 2022-10-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
1012 13b2bc37 2022-10-23 stsp BUF *buf = NULL;
1013 13b2bc37 2022-10-23 stsp size_t buf_pos = 0, remain;
1014 13b2bc37 2022-10-23 stsp ssize_t w;
1015 13b2bc37 2022-10-23 stsp
1016 13b2bc37 2022-10-23 stsp *outsize = 0;
1017 0ff2c315 2023-01-18 stsp *nobj = 0;
1018 9a8e357c 2023-01-28 op
1019 9a8e357c 2023-01-28 op /* if only deleting references there's nothing to read */
1020 9a8e357c 2023-01-28 op if (client->nref_updates == client->nref_del)
1021 9a8e357c 2023-01-28 op return NULL;
1022 9a8e357c 2023-01-28 op
1023 ae25a666 2023-02-23 op got_hash_init(&ctx, GOT_HASH_SHA1);
1024 13b2bc37 2022-10-23 stsp
1025 13b2bc37 2022-10-23 stsp err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
1026 13b2bc37 2022-10-23 stsp if (err)
1027 13b2bc37 2022-10-23 stsp return err;
1028 13b2bc37 2022-10-23 stsp if (have != sizeof(hdr))
1029 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
1030 13b2bc37 2022-10-23 stsp *outsize += have;
1031 13b2bc37 2022-10-23 stsp
1032 13b2bc37 2022-10-23 stsp if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
1033 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
1034 13b2bc37 2022-10-23 stsp "bad packfile signature");
1035 13b2bc37 2022-10-23 stsp if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
1036 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
1037 13b2bc37 2022-10-23 stsp "bad packfile version");
1038 13b2bc37 2022-10-23 stsp
1039 0ff2c315 2023-01-18 stsp *nobj = be32toh(hdr.nobjects);
1040 0ff2c315 2023-01-18 stsp if (*nobj == 0) {
1041 0ff2c315 2023-01-18 stsp /*
1042 0ff2c315 2023-01-18 stsp * Clients which are creating new references only
1043 0ff2c315 2023-01-18 stsp * will send us an empty pack file.
1044 0ff2c315 2023-01-18 stsp */
1045 0ff2c315 2023-01-18 stsp if (client->nref_updates > 0 &&
1046 0ff2c315 2023-01-18 stsp client->nref_updates == client->nref_new)
1047 0ff2c315 2023-01-18 stsp return NULL;
1048 0ff2c315 2023-01-18 stsp
1049 cc88020e 2023-04-11 stsp /*
1050 cc88020e 2023-04-11 stsp * Clients which only move existing refs will send us an empty
1051 cc88020e 2023-04-11 stsp * pack file. All referenced objects must exist locally.
1052 cc88020e 2023-04-11 stsp */
1053 cc88020e 2023-04-11 stsp err = ensure_all_objects_exist_locally(&client->ref_updates);
1054 cc88020e 2023-04-11 stsp if (err) {
1055 cc88020e 2023-04-11 stsp if (err->code != GOT_ERR_NO_OBJ)
1056 cc88020e 2023-04-11 stsp return err;
1057 cc88020e 2023-04-11 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
1058 cc88020e 2023-04-11 stsp "bad packfile with zero objects");
1059 cc88020e 2023-04-11 stsp }
1060 cc88020e 2023-04-11 stsp
1061 cc88020e 2023-04-11 stsp client->nref_move = client->nref_updates;
1062 cc88020e 2023-04-11 stsp return NULL;
1063 0ff2c315 2023-01-18 stsp }
1064 13b2bc37 2022-10-23 stsp
1065 0ff2c315 2023-01-18 stsp log_debug("expecting %d objects", *nobj);
1066 13b2bc37 2022-10-23 stsp
1067 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
1068 13b2bc37 2022-10-23 stsp if (err)
1069 13b2bc37 2022-10-23 stsp return err;
1070 13b2bc37 2022-10-23 stsp
1071 13b2bc37 2022-10-23 stsp err = buf_alloc(&buf, 65536);
1072 13b2bc37 2022-10-23 stsp if (err)
1073 13b2bc37 2022-10-23 stsp return err;
1074 13b2bc37 2022-10-23 stsp
1075 0ff2c315 2023-01-18 stsp while (nhave != *nobj) {
1076 13b2bc37 2022-10-23 stsp uint8_t obj_type;
1077 13b2bc37 2022-10-23 stsp uint64_t obj_size;
1078 13b2bc37 2022-10-23 stsp
1079 13b2bc37 2022-10-23 stsp err = copy_object_type_and_size(&obj_type, &obj_size,
1080 13b2bc37 2022-10-23 stsp infd, outfd, outsize, buf, &buf_pos, &ctx);
1081 13b2bc37 2022-10-23 stsp if (err)
1082 13b2bc37 2022-10-23 stsp goto done;
1083 13b2bc37 2022-10-23 stsp
1084 13b2bc37 2022-10-23 stsp err = validate_object_type(obj_type);
1085 13b2bc37 2022-10-23 stsp if (err)
1086 13b2bc37 2022-10-23 stsp goto done;
1087 13b2bc37 2022-10-23 stsp
1088 13b2bc37 2022-10-23 stsp if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
1089 13b2bc37 2022-10-23 stsp err = copy_ref_delta(infd, outfd, outsize,
1090 13b2bc37 2022-10-23 stsp buf, &buf_pos, &ctx);
1091 13b2bc37 2022-10-23 stsp if (err)
1092 13b2bc37 2022-10-23 stsp goto done;
1093 13b2bc37 2022-10-23 stsp } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
1094 13b2bc37 2022-10-23 stsp err = copy_offset_delta(infd, outfd, outsize,
1095 13b2bc37 2022-10-23 stsp buf, &buf_pos, &ctx);
1096 13b2bc37 2022-10-23 stsp if (err)
1097 13b2bc37 2022-10-23 stsp goto done;
1098 13b2bc37 2022-10-23 stsp }
1099 13b2bc37 2022-10-23 stsp
1100 13b2bc37 2022-10-23 stsp err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
1101 13b2bc37 2022-10-23 stsp if (err)
1102 13b2bc37 2022-10-23 stsp goto done;
1103 13b2bc37 2022-10-23 stsp
1104 13b2bc37 2022-10-23 stsp nhave++;
1105 13b2bc37 2022-10-23 stsp }
1106 13b2bc37 2022-10-23 stsp
1107 0ff2c315 2023-01-18 stsp log_debug("received %u objects", *nobj);
1108 13b2bc37 2022-10-23 stsp
1109 ae25a666 2023-02-23 op got_hash_final(&ctx, expected_sha1);
1110 13b2bc37 2022-10-23 stsp
1111 13b2bc37 2022-10-23 stsp remain = buf_len(buf) - buf_pos;
1112 13b2bc37 2022-10-23 stsp if (remain < SHA1_DIGEST_LENGTH) {
1113 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
1114 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH - remain);
1115 13b2bc37 2022-10-23 stsp if (err)
1116 13b2bc37 2022-10-23 stsp return err;
1117 13b2bc37 2022-10-23 stsp }
1118 13b2bc37 2022-10-23 stsp
1119 13b2bc37 2022-10-23 stsp got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
1120 13b2bc37 2022-10-23 stsp log_debug("expect SHA1: %s", hex);
1121 13b2bc37 2022-10-23 stsp got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
1122 13b2bc37 2022-10-23 stsp log_debug("actual SHA1: %s", hex);
1123 13b2bc37 2022-10-23 stsp
1124 13b2bc37 2022-10-23 stsp if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
1125 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH) != 0) {
1126 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PACKFILE_CSUM);
1127 13b2bc37 2022-10-23 stsp goto done;
1128 13b2bc37 2022-10-23 stsp }
1129 13b2bc37 2022-10-23 stsp
1130 13b2bc37 2022-10-23 stsp memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
1131 13b2bc37 2022-10-23 stsp
1132 13b2bc37 2022-10-23 stsp w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
1133 13b2bc37 2022-10-23 stsp if (w == -1) {
1134 13b2bc37 2022-10-23 stsp err = got_error_from_errno("write");
1135 13b2bc37 2022-10-23 stsp goto done;
1136 13b2bc37 2022-10-23 stsp }
1137 13b2bc37 2022-10-23 stsp if (w != SHA1_DIGEST_LENGTH) {
1138 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_IO);
1139 13b2bc37 2022-10-23 stsp goto done;
1140 13b2bc37 2022-10-23 stsp }
1141 13b2bc37 2022-10-23 stsp
1142 13b2bc37 2022-10-23 stsp *outsize += SHA1_DIGEST_LENGTH;
1143 13b2bc37 2022-10-23 stsp
1144 13b2bc37 2022-10-23 stsp if (fsync(outfd) == -1) {
1145 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fsync");
1146 13b2bc37 2022-10-23 stsp goto done;
1147 13b2bc37 2022-10-23 stsp }
1148 13b2bc37 2022-10-23 stsp if (lseek(outfd, 0L, SEEK_SET) == -1) {
1149 13b2bc37 2022-10-23 stsp err = got_error_from_errno("lseek");
1150 13b2bc37 2022-10-23 stsp goto done;
1151 13b2bc37 2022-10-23 stsp }
1152 13b2bc37 2022-10-23 stsp done:
1153 13b2bc37 2022-10-23 stsp buf_free(buf);
1154 13b2bc37 2022-10-23 stsp return err;
1155 13b2bc37 2022-10-23 stsp }
1156 13b2bc37 2022-10-23 stsp
1157 13b2bc37 2022-10-23 stsp static const struct got_error *
1158 1a52c9bf 2022-12-30 stsp report_pack_status(const struct got_error *unpack_err)
1159 13b2bc37 2022-10-23 stsp {
1160 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1161 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1162 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_status istatus;
1163 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
1164 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
1165 13b2bc37 2022-10-23 stsp const char *unpack_ok = "unpack ok\n";
1166 13b2bc37 2022-10-23 stsp size_t len;
1167 e26970cc 2023-01-10 op
1168 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, client->fd);
1169 13b2bc37 2022-10-23 stsp
1170 13b2bc37 2022-10-23 stsp if (unpack_err)
1171 13b2bc37 2022-10-23 stsp istatus.reason_len = strlen(unpack_err->msg);
1172 13b2bc37 2022-10-23 stsp else
1173 13b2bc37 2022-10-23 stsp istatus.reason_len = strlen(unpack_ok);
1174 13b2bc37 2022-10-23 stsp
1175 13b2bc37 2022-10-23 stsp len = sizeof(istatus) + istatus.reason_len;
1176 13b2bc37 2022-10-23 stsp wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
1177 13b2bc37 2022-10-23 stsp repo_write.pid, len);
1178 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
1179 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create PACKFILE_STATUS");
1180 13b2bc37 2022-10-23 stsp goto done;
1181 13b2bc37 2022-10-23 stsp }
1182 13b2bc37 2022-10-23 stsp
1183 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -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 if (imsg_add(wbuf, err ? err->msg : unpack_ok,
1189 13b2bc37 2022-10-23 stsp istatus.reason_len) == -1) {
1190 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add PACKFILE_STATUS");
1191 13b2bc37 2022-10-23 stsp goto done;
1192 13b2bc37 2022-10-23 stsp }
1193 13b2bc37 2022-10-23 stsp
1194 13b2bc37 2022-10-23 stsp imsg_close(&ibuf, wbuf);
1195 13b2bc37 2022-10-23 stsp
1196 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(&ibuf);
1197 13b2bc37 2022-10-23 stsp done:
1198 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
1199 13b2bc37 2022-10-23 stsp return err;
1200 13b2bc37 2022-10-23 stsp }
1201 13b2bc37 2022-10-23 stsp
1202 13b2bc37 2022-10-23 stsp static const struct got_error *
1203 0ff2c315 2023-01-18 stsp recv_packfile(int *have_packfile, struct imsg *imsg)
1204 13b2bc37 2022-10-23 stsp {
1205 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL, *unpack_err;
1206 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1207 13b2bc37 2022-10-23 stsp struct gotd_imsg_recv_packfile ireq;
1208 394b0db7 2024-07-16 op struct got_object_id id;
1209 13b2bc37 2022-10-23 stsp FILE *tempfiles[3] = { NULL, NULL, NULL };
1210 13b2bc37 2022-10-23 stsp struct repo_tempfile {
1211 13b2bc37 2022-10-23 stsp int fd;
1212 13b2bc37 2022-10-23 stsp int idx;
1213 13b2bc37 2022-10-23 stsp } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
1214 13b2bc37 2022-10-23 stsp int i;
1215 13b2bc37 2022-10-23 stsp size_t datalen;
1216 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
1217 13b2bc37 2022-10-23 stsp struct got_ratelimit rl;
1218 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
1219 13b2bc37 2022-10-23 stsp off_t pack_filesize = 0;
1220 0ff2c315 2023-01-18 stsp uint32_t nobj = 0;
1221 13b2bc37 2022-10-23 stsp
1222 13b2bc37 2022-10-23 stsp log_debug("packfile request received");
1223 13b2bc37 2022-10-23 stsp
1224 0ff2c315 2023-01-18 stsp *have_packfile = 0;
1225 13b2bc37 2022-10-23 stsp got_ratelimit_init(&rl, 2, 0);
1226 13b2bc37 2022-10-23 stsp
1227 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1228 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
1229 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1230 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
1231 13b2bc37 2022-10-23 stsp
1232 1a52c9bf 2022-12-30 stsp if (client->pack_pipe == -1 || client->packidx_fd == -1)
1233 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1234 13b2bc37 2022-10-23 stsp
1235 1a52c9bf 2022-12-30 stsp imsg_init(&ibuf, client->fd);
1236 13b2bc37 2022-10-23 stsp
1237 1a52c9bf 2022-12-30 stsp pack = &client->pack;
1238 13b2bc37 2022-10-23 stsp memset(pack, 0, sizeof(*pack));
1239 2c52c623 2024-01-30 op pack->fd = imsg_get_fd(imsg);
1240 2c52c623 2024-01-30 op if (pack->fd == -1)
1241 2c52c623 2024-01-30 op return got_error(GOT_ERR_PRIVSEP_NO_FD);
1242 2c52c623 2024-01-30 op
1243 13b2bc37 2022-10-23 stsp err = got_delta_cache_alloc(&pack->delta_cache);
1244 13b2bc37 2022-10-23 stsp if (err)
1245 13b2bc37 2022-10-23 stsp return err;
1246 13b2bc37 2022-10-23 stsp
1247 13b2bc37 2022-10-23 stsp for (i = 0; i < nitems(repo_tempfiles); i++) {
1248 13b2bc37 2022-10-23 stsp struct repo_tempfile *t = &repo_tempfiles[i];
1249 13b2bc37 2022-10-23 stsp err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
1250 13b2bc37 2022-10-23 stsp if (err)
1251 13b2bc37 2022-10-23 stsp goto done;
1252 13b2bc37 2022-10-23 stsp }
1253 13b2bc37 2022-10-23 stsp
1254 13b2bc37 2022-10-23 stsp for (i = 0; i < nitems(tempfiles); i++) {
1255 e294dc4e 2023-02-03 mark int fd;
1256 13b2bc37 2022-10-23 stsp FILE *f;
1257 e294dc4e 2023-02-03 mark
1258 e294dc4e 2023-02-03 mark fd = dup(repo_tempfiles[i].fd);
1259 13b2bc37 2022-10-23 stsp if (fd == -1) {
1260 13b2bc37 2022-10-23 stsp err = got_error_from_errno("dup");
1261 13b2bc37 2022-10-23 stsp goto done;
1262 13b2bc37 2022-10-23 stsp }
1263 13b2bc37 2022-10-23 stsp f = fdopen(fd, "w+");
1264 13b2bc37 2022-10-23 stsp if (f == NULL) {
1265 e294dc4e 2023-02-03 mark err = got_error_from_errno("fdopen");
1266 13b2bc37 2022-10-23 stsp close(fd);
1267 13b2bc37 2022-10-23 stsp goto done;
1268 13b2bc37 2022-10-23 stsp }
1269 13b2bc37 2022-10-23 stsp tempfiles[i] = f;
1270 13b2bc37 2022-10-23 stsp }
1271 13b2bc37 2022-10-23 stsp
1272 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(&ibuf);
1273 13b2bc37 2022-10-23 stsp if (err)
1274 13b2bc37 2022-10-23 stsp goto done;
1275 13b2bc37 2022-10-23 stsp
1276 13b2bc37 2022-10-23 stsp log_debug("receiving pack data");
1277 0ff2c315 2023-01-18 stsp unpack_err = recv_packdata(&pack_filesize, &nobj,
1278 0ff2c315 2023-01-18 stsp client->pack_sha1, client->pack_pipe, pack->fd);
1279 13b2bc37 2022-10-23 stsp if (ireq.report_status) {
1280 1a52c9bf 2022-12-30 stsp err = report_pack_status(unpack_err);
1281 13b2bc37 2022-10-23 stsp if (err) {
1282 13b2bc37 2022-10-23 stsp /* Git clients hang up after sending the pack file. */
1283 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_EOF)
1284 13b2bc37 2022-10-23 stsp err = NULL;
1285 13b2bc37 2022-10-23 stsp }
1286 13b2bc37 2022-10-23 stsp }
1287 13b2bc37 2022-10-23 stsp if (unpack_err)
1288 13b2bc37 2022-10-23 stsp err = unpack_err;
1289 13b2bc37 2022-10-23 stsp if (err)
1290 13b2bc37 2022-10-23 stsp goto done;
1291 13b2bc37 2022-10-23 stsp
1292 13b2bc37 2022-10-23 stsp log_debug("pack data received");
1293 0ff2c315 2023-01-18 stsp
1294 0ff2c315 2023-01-18 stsp /*
1295 0ff2c315 2023-01-18 stsp * Clients which are creating new references only will
1296 0ff2c315 2023-01-18 stsp * send us an empty pack file.
1297 0ff2c315 2023-01-18 stsp */
1298 0ff2c315 2023-01-18 stsp if (nobj == 0 &&
1299 0ff2c315 2023-01-18 stsp pack_filesize == sizeof(struct got_packfile_hdr) &&
1300 0ff2c315 2023-01-18 stsp client->nref_updates > 0 &&
1301 0ff2c315 2023-01-18 stsp client->nref_updates == client->nref_new)
1302 0ff2c315 2023-01-18 stsp goto done;
1303 13b2bc37 2022-10-23 stsp
1304 9a8e357c 2023-01-28 op /*
1305 9a8e357c 2023-01-28 op * Clients which are deleting references only will send
1306 9a8e357c 2023-01-28 op * no pack file.
1307 9a8e357c 2023-01-28 op */
1308 9a8e357c 2023-01-28 op if (nobj == 0 &&
1309 9a8e357c 2023-01-28 op client->nref_del > 0 &&
1310 9a8e357c 2023-01-28 op client->nref_updates == client->nref_del)
1311 cc88020e 2023-04-11 stsp goto done;
1312 cc88020e 2023-04-11 stsp
1313 cc88020e 2023-04-11 stsp /*
1314 cc88020e 2023-04-11 stsp * Clients which only move existing refs will send us an empty
1315 cc88020e 2023-04-11 stsp * pack file. All referenced objects must exist locally.
1316 cc88020e 2023-04-11 stsp */
1317 cc88020e 2023-04-11 stsp if (nobj == 0 &&
1318 cc88020e 2023-04-11 stsp pack_filesize == sizeof(struct got_packfile_hdr) &&
1319 cc88020e 2023-04-11 stsp client->nref_move > 0 &&
1320 cc88020e 2023-04-11 stsp client->nref_updates == client->nref_move)
1321 9a8e357c 2023-01-28 op goto done;
1322 9a8e357c 2023-01-28 op
1323 13b2bc37 2022-10-23 stsp pack->filesize = pack_filesize;
1324 0ff2c315 2023-01-18 stsp *have_packfile = 1;
1325 13b2bc37 2022-10-23 stsp
1326 394b0db7 2024-07-16 op memset(&id, 0, sizeof(id));
1327 394b0db7 2024-07-16 op memcpy(&id.hash, client->pack_sha1, SHA1_DIGEST_LENGTH);
1328 394b0db7 2024-07-16 op id.algo = GOT_HASH_SHA1;
1329 394b0db7 2024-07-16 op
1330 ad4cc361 2022-10-27 op log_debug("begin indexing pack (%lld bytes in size)",
1331 ad4cc361 2022-10-27 op (long long)pack->filesize);
1332 1a52c9bf 2022-12-30 stsp err = got_pack_index(pack, client->packidx_fd,
1333 394b0db7 2024-07-16 op tempfiles[0], tempfiles[1], tempfiles[2], &id,
1334 13b2bc37 2022-10-23 stsp pack_index_progress, NULL, &rl);
1335 13b2bc37 2022-10-23 stsp if (err)
1336 13b2bc37 2022-10-23 stsp goto done;
1337 13b2bc37 2022-10-23 stsp log_debug("done indexing pack");
1338 13b2bc37 2022-10-23 stsp
1339 1a52c9bf 2022-12-30 stsp if (fsync(client->packidx_fd) == -1) {
1340 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fsync");
1341 13b2bc37 2022-10-23 stsp goto done;
1342 13b2bc37 2022-10-23 stsp }
1343 1a52c9bf 2022-12-30 stsp if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1344 13b2bc37 2022-10-23 stsp err = got_error_from_errno("lseek");
1345 13b2bc37 2022-10-23 stsp done:
1346 1a52c9bf 2022-12-30 stsp if (close(client->pack_pipe) == -1 && err == NULL)
1347 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
1348 1a52c9bf 2022-12-30 stsp client->pack_pipe = -1;
1349 13b2bc37 2022-10-23 stsp for (i = 0; i < nitems(repo_tempfiles); i++) {
1350 13b2bc37 2022-10-23 stsp struct repo_tempfile *t = &repo_tempfiles[i];
1351 13b2bc37 2022-10-23 stsp if (t->idx != -1)
1352 13b2bc37 2022-10-23 stsp got_repo_temp_fds_put(t->idx, repo_write.repo);
1353 13b2bc37 2022-10-23 stsp }
1354 13b2bc37 2022-10-23 stsp for (i = 0; i < nitems(tempfiles); i++) {
1355 13b2bc37 2022-10-23 stsp if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1356 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fclose");
1357 13b2bc37 2022-10-23 stsp }
1358 13b2bc37 2022-10-23 stsp if (err)
1359 13b2bc37 2022-10-23 stsp got_pack_close(pack);
1360 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
1361 13b2bc37 2022-10-23 stsp return err;
1362 13b2bc37 2022-10-23 stsp }
1363 13b2bc37 2022-10-23 stsp
1364 13b2bc37 2022-10-23 stsp static const struct got_error *
1365 1a52c9bf 2022-12-30 stsp verify_packfile(void)
1366 13b2bc37 2022-10-23 stsp {
1367 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL, *close_err;
1368 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1369 13b2bc37 2022-10-23 stsp struct gotd_ref_update *ref_update;
1370 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
1371 13b2bc37 2022-10-23 stsp struct stat sb;
1372 13b2bc37 2022-10-23 stsp char *id_str = NULL;
1373 9afa3de2 2023-04-04 stsp struct got_object *obj = NULL;
1374 9afa3de2 2023-04-04 stsp struct got_pathlist_entry *pe;
1375 9afa3de2 2023-04-04 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
1376 13b2bc37 2022-10-23 stsp
1377 13b2bc37 2022-10-23 stsp if (STAILQ_EMPTY(&client->ref_updates)) {
1378 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1379 13b2bc37 2022-10-23 stsp "cannot verify pack file without any ref-updates");
1380 13b2bc37 2022-10-23 stsp }
1381 13b2bc37 2022-10-23 stsp
1382 13b2bc37 2022-10-23 stsp if (client->pack.fd == -1) {
1383 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1384 13b2bc37 2022-10-23 stsp "invalid pack file handle during pack verification");
1385 13b2bc37 2022-10-23 stsp }
1386 13b2bc37 2022-10-23 stsp if (client->packidx_fd == -1) {
1387 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1388 13b2bc37 2022-10-23 stsp "invalid pack index handle during pack verification");
1389 13b2bc37 2022-10-23 stsp }
1390 13b2bc37 2022-10-23 stsp
1391 13b2bc37 2022-10-23 stsp if (fstat(client->packidx_fd, &sb) == -1)
1392 13b2bc37 2022-10-23 stsp return got_error_from_errno("pack index fstat");
1393 13b2bc37 2022-10-23 stsp
1394 13b2bc37 2022-10-23 stsp packidx = malloc(sizeof(*packidx));
1395 13b2bc37 2022-10-23 stsp memset(packidx, 0, sizeof(*packidx));
1396 13b2bc37 2022-10-23 stsp packidx->fd = client->packidx_fd;
1397 13b2bc37 2022-10-23 stsp client->packidx_fd = -1;
1398 13b2bc37 2022-10-23 stsp packidx->len = sb.st_size;
1399 e26970cc 2023-01-10 op
1400 13b2bc37 2022-10-23 stsp err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1401 13b2bc37 2022-10-23 stsp if (err)
1402 13b2bc37 2022-10-23 stsp return err;
1403 13b2bc37 2022-10-23 stsp
1404 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1405 9a8e357c 2023-01-28 op if (ref_update->delete_ref)
1406 9a8e357c 2023-01-28 op continue;
1407 9a8e357c 2023-01-28 op
1408 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(pe, repo_write.protected_tag_namespaces, entry) {
1409 9afa3de2 2023-04-04 stsp err = protect_tag_namespace(pe->path, &client->pack,
1410 9afa3de2 2023-04-04 stsp packidx, ref_update);
1411 9afa3de2 2023-04-04 stsp if (err)
1412 9afa3de2 2023-04-04 stsp goto done;
1413 9afa3de2 2023-04-04 stsp }
1414 13b2bc37 2022-10-23 stsp
1415 9afa3de2 2023-04-04 stsp /*
1416 9afa3de2 2023-04-04 stsp * Objects which already exist in our repository need
1417 9afa3de2 2023-04-04 stsp * not be present in the pack file.
1418 9afa3de2 2023-04-04 stsp */
1419 9afa3de2 2023-04-04 stsp err = got_object_open(&obj, repo_write.repo,
1420 9afa3de2 2023-04-04 stsp &ref_update->new_id);
1421 9afa3de2 2023-04-04 stsp if (err && err->code != GOT_ERR_NO_OBJ)
1422 13b2bc37 2022-10-23 stsp goto done;
1423 9afa3de2 2023-04-04 stsp err = NULL;
1424 9afa3de2 2023-04-04 stsp if (obj) {
1425 9afa3de2 2023-04-04 stsp got_object_close(obj);
1426 9afa3de2 2023-04-04 stsp obj = NULL;
1427 9afa3de2 2023-04-04 stsp } else {
1428 9afa3de2 2023-04-04 stsp int idx = got_packidx_get_object_idx(packidx,
1429 9afa3de2 2023-04-04 stsp &ref_update->new_id);
1430 9afa3de2 2023-04-04 stsp if (idx == -1) {
1431 310a8ea5 2024-07-10 op got_object_id_hex(&ref_update->new_id,
1432 9afa3de2 2023-04-04 stsp hex, sizeof(hex));
1433 9afa3de2 2023-04-04 stsp err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1434 9afa3de2 2023-04-04 stsp "object %s is missing from pack file",
1435 9afa3de2 2023-04-04 stsp hex);
1436 9afa3de2 2023-04-04 stsp goto done;
1437 9afa3de2 2023-04-04 stsp }
1438 13b2bc37 2022-10-23 stsp }
1439 9afa3de2 2023-04-04 stsp
1440 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(pe, repo_write.protected_branch_namespaces,
1441 9afa3de2 2023-04-04 stsp entry) {
1442 9afa3de2 2023-04-04 stsp err = protect_branch_namespace(pe->path,
1443 9afa3de2 2023-04-04 stsp &client->pack, packidx, ref_update);
1444 9afa3de2 2023-04-04 stsp if (err)
1445 9afa3de2 2023-04-04 stsp goto done;
1446 9afa3de2 2023-04-04 stsp }
1447 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(pe, repo_write.protected_branches, entry) {
1448 9afa3de2 2023-04-04 stsp err = protect_branch(pe->path, &client->pack,
1449 9afa3de2 2023-04-04 stsp packidx, ref_update);
1450 9afa3de2 2023-04-04 stsp if (err)
1451 9afa3de2 2023-04-04 stsp goto done;
1452 9afa3de2 2023-04-04 stsp }
1453 13b2bc37 2022-10-23 stsp }
1454 13b2bc37 2022-10-23 stsp
1455 e26970cc 2023-01-10 op done:
1456 13b2bc37 2022-10-23 stsp close_err = got_packidx_close(packidx);
1457 13b2bc37 2022-10-23 stsp if (close_err && err == NULL)
1458 13b2bc37 2022-10-23 stsp err = close_err;
1459 13b2bc37 2022-10-23 stsp free(id_str);
1460 9afa3de2 2023-04-04 stsp if (obj)
1461 9afa3de2 2023-04-04 stsp got_object_close(obj);
1462 13b2bc37 2022-10-23 stsp return err;
1463 13b2bc37 2022-10-23 stsp }
1464 13b2bc37 2022-10-23 stsp
1465 13b2bc37 2022-10-23 stsp static const struct got_error *
1466 9afa3de2 2023-04-04 stsp protect_refs_from_deletion(void)
1467 9afa3de2 2023-04-04 stsp {
1468 9afa3de2 2023-04-04 stsp const struct got_error *err = NULL;
1469 9afa3de2 2023-04-04 stsp struct repo_write_client *client = &repo_write_client;
1470 9afa3de2 2023-04-04 stsp struct gotd_ref_update *ref_update;
1471 9afa3de2 2023-04-04 stsp struct got_pathlist_entry *pe;
1472 9afa3de2 2023-04-04 stsp const char *refname;
1473 9afa3de2 2023-04-04 stsp
1474 9afa3de2 2023-04-04 stsp STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1475 9afa3de2 2023-04-04 stsp if (!ref_update->delete_ref)
1476 9afa3de2 2023-04-04 stsp continue;
1477 9afa3de2 2023-04-04 stsp
1478 9afa3de2 2023-04-04 stsp refname = got_ref_get_name(ref_update->ref);
1479 9afa3de2 2023-04-04 stsp
1480 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(pe, repo_write.protected_tag_namespaces, entry) {
1481 9afa3de2 2023-04-04 stsp err = protect_ref_namespace(refname, pe->path);
1482 9afa3de2 2023-04-04 stsp if (err)
1483 9afa3de2 2023-04-04 stsp return err;
1484 9afa3de2 2023-04-04 stsp }
1485 9afa3de2 2023-04-04 stsp
1486 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(pe, repo_write.protected_branch_namespaces,
1487 9afa3de2 2023-04-04 stsp entry) {
1488 9afa3de2 2023-04-04 stsp err = protect_ref_namespace(refname, pe->path);
1489 9afa3de2 2023-04-04 stsp if (err)
1490 9afa3de2 2023-04-04 stsp return err;
1491 9afa3de2 2023-04-04 stsp }
1492 9afa3de2 2023-04-04 stsp
1493 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(pe, repo_write.protected_branches, entry) {
1494 9afa3de2 2023-04-04 stsp if (strcmp(refname, pe->path) == 0) {
1495 9afa3de2 2023-04-04 stsp return got_error_fmt(GOT_ERR_REF_PROTECTED,
1496 9afa3de2 2023-04-04 stsp "%s", refname);
1497 9afa3de2 2023-04-04 stsp }
1498 9afa3de2 2023-04-04 stsp }
1499 9afa3de2 2023-04-04 stsp }
1500 9afa3de2 2023-04-04 stsp
1501 9afa3de2 2023-04-04 stsp return NULL;
1502 9afa3de2 2023-04-04 stsp }
1503 9afa3de2 2023-04-04 stsp
1504 9afa3de2 2023-04-04 stsp static const struct got_error *
1505 1a52c9bf 2022-12-30 stsp install_packfile(struct gotd_imsgev *iev)
1506 13b2bc37 2022-10-23 stsp {
1507 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1508 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_install inst;
1509 13b2bc37 2022-10-23 stsp int ret;
1510 13b2bc37 2022-10-23 stsp
1511 13b2bc37 2022-10-23 stsp memset(&inst, 0, sizeof(inst));
1512 13b2bc37 2022-10-23 stsp memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1513 13b2bc37 2022-10-23 stsp
1514 13b2bc37 2022-10-23 stsp ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1515 13b2bc37 2022-10-23 stsp PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1516 13b2bc37 2022-10-23 stsp if (ret == -1)
1517 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1518 13b2bc37 2022-10-23 stsp
1519 13b2bc37 2022-10-23 stsp return NULL;
1520 13b2bc37 2022-10-23 stsp }
1521 13b2bc37 2022-10-23 stsp
1522 13b2bc37 2022-10-23 stsp static const struct got_error *
1523 1a52c9bf 2022-12-30 stsp send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1524 13b2bc37 2022-10-23 stsp {
1525 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_updates_start istart;
1526 13b2bc37 2022-10-23 stsp int ret;
1527 13b2bc37 2022-10-23 stsp
1528 13b2bc37 2022-10-23 stsp memset(&istart, 0, sizeof(istart));
1529 13b2bc37 2022-10-23 stsp istart.nref_updates = nref_updates;
1530 13b2bc37 2022-10-23 stsp
1531 13b2bc37 2022-10-23 stsp ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1532 13b2bc37 2022-10-23 stsp PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1533 13b2bc37 2022-10-23 stsp if (ret == -1)
1534 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_compose REF_UPDATES_START");
1535 13b2bc37 2022-10-23 stsp
1536 13b2bc37 2022-10-23 stsp return NULL;
1537 13b2bc37 2022-10-23 stsp }
1538 13b2bc37 2022-10-23 stsp
1539 13b2bc37 2022-10-23 stsp
1540 13b2bc37 2022-10-23 stsp static const struct got_error *
1541 1a52c9bf 2022-12-30 stsp send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1542 13b2bc37 2022-10-23 stsp {
1543 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update iref;
1544 13b2bc37 2022-10-23 stsp const char *refname = got_ref_get_name(ref_update->ref);
1545 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
1546 13b2bc37 2022-10-23 stsp size_t len;
1547 13b2bc37 2022-10-23 stsp
1548 13b2bc37 2022-10-23 stsp memset(&iref, 0, sizeof(iref));
1549 aaa34732 2024-07-13 op memcpy(iref.old_id, ref_update->old_id.hash, SHA1_DIGEST_LENGTH);
1550 aaa34732 2024-07-13 op memcpy(iref.new_id, ref_update->new_id.hash, SHA1_DIGEST_LENGTH);
1551 13b2bc37 2022-10-23 stsp iref.ref_is_new = ref_update->ref_is_new;
1552 9a8e357c 2023-01-28 op iref.delete_ref = ref_update->delete_ref;
1553 13b2bc37 2022-10-23 stsp iref.name_len = strlen(refname);
1554 13b2bc37 2022-10-23 stsp
1555 13b2bc37 2022-10-23 stsp len = sizeof(iref) + iref.name_len;
1556 13b2bc37 2022-10-23 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1557 13b2bc37 2022-10-23 stsp repo_write.pid, len);
1558 13b2bc37 2022-10-23 stsp if (wbuf == NULL)
1559 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_create REF_UPDATE");
1560 13b2bc37 2022-10-23 stsp
1561 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1562 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE");
1563 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, refname, iref.name_len) == -1)
1564 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE");
1565 13b2bc37 2022-10-23 stsp
1566 13b2bc37 2022-10-23 stsp imsg_close(&iev->ibuf, wbuf);
1567 13b2bc37 2022-10-23 stsp
1568 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
1569 13b2bc37 2022-10-23 stsp return NULL;
1570 13b2bc37 2022-10-23 stsp }
1571 13b2bc37 2022-10-23 stsp
1572 13b2bc37 2022-10-23 stsp static const struct got_error *
1573 1a52c9bf 2022-12-30 stsp update_refs(struct gotd_imsgev *iev)
1574 13b2bc37 2022-10-23 stsp {
1575 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1576 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1577 13b2bc37 2022-10-23 stsp struct gotd_ref_update *ref_update;
1578 13b2bc37 2022-10-23 stsp
1579 1a52c9bf 2022-12-30 stsp err = send_ref_updates_start(client->nref_updates, iev);
1580 13b2bc37 2022-10-23 stsp if (err)
1581 13b2bc37 2022-10-23 stsp return err;
1582 13b2bc37 2022-10-23 stsp
1583 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1584 1a52c9bf 2022-12-30 stsp err = send_ref_update(ref_update, iev);
1585 13b2bc37 2022-10-23 stsp if (err)
1586 13b2bc37 2022-10-23 stsp goto done;
1587 13b2bc37 2022-10-23 stsp }
1588 13b2bc37 2022-10-23 stsp done:
1589 13b2bc37 2022-10-23 stsp return err;
1590 13b2bc37 2022-10-23 stsp }
1591 13b2bc37 2022-10-23 stsp
1592 13b2bc37 2022-10-23 stsp static const struct got_error *
1593 1a52c9bf 2022-12-30 stsp receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1594 13b2bc37 2022-10-23 stsp {
1595 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1596 13b2bc37 2022-10-23 stsp size_t datalen;
1597 13b2bc37 2022-10-23 stsp
1598 9cbac887 2023-04-20 stsp log_debug("receiving pack pipe descriptor");
1599 13b2bc37 2022-10-23 stsp
1600 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1601 1cb49b67 2024-03-21 stsp if (datalen != 0)
1602 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1603 13b2bc37 2022-10-23 stsp
1604 1a52c9bf 2022-12-30 stsp if (client->pack_pipe != -1)
1605 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1606 13b2bc37 2022-10-23 stsp
1607 2c52c623 2024-01-30 op client->pack_pipe = imsg_get_fd(imsg);
1608 2c52c623 2024-01-30 op if (client->pack_pipe == -1)
1609 2c52c623 2024-01-30 op return got_error(GOT_ERR_PRIVSEP_NO_FD);
1610 2c52c623 2024-01-30 op
1611 13b2bc37 2022-10-23 stsp return NULL;
1612 13b2bc37 2022-10-23 stsp }
1613 13b2bc37 2022-10-23 stsp
1614 13b2bc37 2022-10-23 stsp static const struct got_error *
1615 1a52c9bf 2022-12-30 stsp receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1616 13b2bc37 2022-10-23 stsp {
1617 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1618 13b2bc37 2022-10-23 stsp size_t datalen;
1619 13b2bc37 2022-10-23 stsp
1620 9cbac887 2023-04-20 stsp log_debug("receiving pack index output file");
1621 13b2bc37 2022-10-23 stsp
1622 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1623 1cb49b67 2024-03-21 stsp if (datalen != 0)
1624 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
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 ba97b2d7 2024-03-20 stsp }
1635 ba97b2d7 2024-03-20 stsp
1636 ba97b2d7 2024-03-20 stsp static const struct got_error *
1637 1b1a386d 2024-07-09 op notify_removed_ref(const char *refname, struct got_object_id *id,
1638 ba97b2d7 2024-03-20 stsp struct gotd_imsgev *iev, int fd)
1639 ba97b2d7 2024-03-20 stsp {
1640 ba97b2d7 2024-03-20 stsp const struct got_error *err;
1641 ba97b2d7 2024-03-20 stsp char *id_str;
1642 ba97b2d7 2024-03-20 stsp
1643 1b1a386d 2024-07-09 op err = got_object_id_str(&id_str, id);
1644 ba97b2d7 2024-03-20 stsp if (err)
1645 ba97b2d7 2024-03-20 stsp return err;
1646 ba97b2d7 2024-03-20 stsp
1647 ba97b2d7 2024-03-20 stsp dprintf(fd, "Removed %s: %s\n", refname, id_str);
1648 ba97b2d7 2024-03-20 stsp free(id_str);
1649 ba97b2d7 2024-03-20 stsp return err;
1650 13b2bc37 2022-10-23 stsp }
1651 13b2bc37 2022-10-23 stsp
1652 ba97b2d7 2024-03-20 stsp static const char *
1653 ba97b2d7 2024-03-20 stsp format_author(char *author)
1654 ba97b2d7 2024-03-20 stsp {
1655 ba97b2d7 2024-03-20 stsp char *smallerthan;
1656 ba97b2d7 2024-03-20 stsp
1657 ba97b2d7 2024-03-20 stsp smallerthan = strchr(author, '<');
1658 ba97b2d7 2024-03-20 stsp if (smallerthan && smallerthan[1] != '\0')
1659 ba97b2d7 2024-03-20 stsp author = smallerthan + 1;
1660 ba97b2d7 2024-03-20 stsp author[strcspn(author, "@>")] = '\0';
1661 ba97b2d7 2024-03-20 stsp
1662 ba97b2d7 2024-03-20 stsp return author;
1663 ba97b2d7 2024-03-20 stsp }
1664 ba97b2d7 2024-03-20 stsp
1665 ba97b2d7 2024-03-20 stsp static const struct got_error *
1666 ba97b2d7 2024-03-20 stsp print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
1667 ba97b2d7 2024-03-20 stsp struct got_repository *repo, int fd)
1668 ba97b2d7 2024-03-20 stsp {
1669 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
1670 ba97b2d7 2024-03-20 stsp char *id_str = NULL, *logmsg0 = NULL;
1671 ba97b2d7 2024-03-20 stsp char *s, *nl;
1672 ba97b2d7 2024-03-20 stsp char *committer = NULL, *author = NULL;
1673 ba97b2d7 2024-03-20 stsp time_t committer_time;
1674 ba97b2d7 2024-03-20 stsp
1675 ba97b2d7 2024-03-20 stsp err = got_object_id_str(&id_str, id);
1676 ba97b2d7 2024-03-20 stsp if (err)
1677 ba97b2d7 2024-03-20 stsp return err;
1678 ba97b2d7 2024-03-20 stsp
1679 ba97b2d7 2024-03-20 stsp committer_time = got_object_commit_get_committer_time(commit);
1680 ba97b2d7 2024-03-20 stsp
1681 ba97b2d7 2024-03-20 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1682 ba97b2d7 2024-03-20 stsp if (err)
1683 ba97b2d7 2024-03-20 stsp goto done;
1684 ba97b2d7 2024-03-20 stsp
1685 ba97b2d7 2024-03-20 stsp s = logmsg0;
1686 ba97b2d7 2024-03-20 stsp while (isspace((unsigned char)s[0]))
1687 ba97b2d7 2024-03-20 stsp s++;
1688 ba97b2d7 2024-03-20 stsp
1689 ba97b2d7 2024-03-20 stsp nl = strchr(s, '\n');
1690 ba97b2d7 2024-03-20 stsp if (nl) {
1691 ba97b2d7 2024-03-20 stsp *nl = '\0';
1692 ba97b2d7 2024-03-20 stsp }
1693 ba97b2d7 2024-03-20 stsp
1694 ba97b2d7 2024-03-20 stsp if (strcmp(got_object_commit_get_author(commit),
1695 ba97b2d7 2024-03-20 stsp got_object_commit_get_committer(commit)) != 0) {
1696 ba97b2d7 2024-03-20 stsp author = strdup(got_object_commit_get_author(commit));
1697 ba97b2d7 2024-03-20 stsp if (author == NULL) {
1698 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strdup");
1699 ba97b2d7 2024-03-20 stsp goto done;
1700 ba97b2d7 2024-03-20 stsp }
1701 939d3016 2024-04-23 op dprintf(fd, "%lld %.7s %.8s %s\n", (long long)committer_time,
1702 939d3016 2024-04-23 op id_str, format_author(author), s);
1703 ba97b2d7 2024-03-20 stsp } else {
1704 ba97b2d7 2024-03-20 stsp committer = strdup(got_object_commit_get_committer(commit));
1705 939d3016 2024-04-23 op dprintf(fd, "%lld %.7s %.8s %s\n", (long long)committer_time,
1706 939d3016 2024-04-23 op id_str, format_author(committer), s);
1707 ba97b2d7 2024-03-20 stsp }
1708 ba97b2d7 2024-03-20 stsp
1709 ba97b2d7 2024-03-20 stsp if (fsync(fd) == -1 && err == NULL)
1710 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("fsync");
1711 ba97b2d7 2024-03-20 stsp done:
1712 ba97b2d7 2024-03-20 stsp free(id_str);
1713 ba97b2d7 2024-03-20 stsp free(logmsg0);
1714 ba97b2d7 2024-03-20 stsp free(committer);
1715 ba97b2d7 2024-03-20 stsp free(author);
1716 ba97b2d7 2024-03-20 stsp return err;
1717 ba97b2d7 2024-03-20 stsp }
1718 ba97b2d7 2024-03-20 stsp
1719 ba97b2d7 2024-03-20 stsp static const struct got_error *
1720 ba97b2d7 2024-03-20 stsp print_diffstat(struct got_diffstat_cb_arg *dsa, int fd)
1721 ba97b2d7 2024-03-20 stsp {
1722 ba97b2d7 2024-03-20 stsp struct got_pathlist_entry *pe;
1723 ba97b2d7 2024-03-20 stsp
1724 ba97b2d7 2024-03-20 stsp TAILQ_FOREACH(pe, dsa->paths, entry) {
1725 ba97b2d7 2024-03-20 stsp struct got_diff_changed_path *cp = pe->data;
1726 ba97b2d7 2024-03-20 stsp int pad = dsa->max_path_len - pe->path_len + 1;
1727 ba97b2d7 2024-03-20 stsp
1728 ba97b2d7 2024-03-20 stsp dprintf(fd, " %c %s%*c | %*d+ %*d-\n", cp->status,
1729 ba97b2d7 2024-03-20 stsp pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
1730 ba97b2d7 2024-03-20 stsp dsa->rm_cols + 1, cp->rm);
1731 ba97b2d7 2024-03-20 stsp }
1732 ba97b2d7 2024-03-20 stsp dprintf(fd,
1733 ba97b2d7 2024-03-20 stsp "\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
1734 ba97b2d7 2024-03-20 stsp dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
1735 ba97b2d7 2024-03-20 stsp dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
1736 ba97b2d7 2024-03-20 stsp
1737 ba97b2d7 2024-03-20 stsp return NULL;
1738 ba97b2d7 2024-03-20 stsp }
1739 ba97b2d7 2024-03-20 stsp
1740 ba97b2d7 2024-03-20 stsp static const struct got_error *
1741 ba97b2d7 2024-03-20 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1742 ba97b2d7 2024-03-20 stsp struct got_repository *repo, struct got_pathlist_head *changed_paths,
1743 ba97b2d7 2024-03-20 stsp struct got_diffstat_cb_arg *diffstat, int fd)
1744 ba97b2d7 2024-03-20 stsp {
1745 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
1746 939d3016 2024-04-23 op char *id_str, *logmsg0, *logmsg, *line;
1747 ba97b2d7 2024-03-20 stsp time_t committer_time;
1748 ba97b2d7 2024-03-20 stsp const char *author, *committer;
1749 ba97b2d7 2024-03-20 stsp
1750 ba97b2d7 2024-03-20 stsp err = got_object_id_str(&id_str, id);
1751 ba97b2d7 2024-03-20 stsp if (err)
1752 ba97b2d7 2024-03-20 stsp return err;
1753 ba97b2d7 2024-03-20 stsp
1754 ba97b2d7 2024-03-20 stsp dprintf(fd, "commit %s\n", id_str);
1755 ba97b2d7 2024-03-20 stsp free(id_str);
1756 ba97b2d7 2024-03-20 stsp id_str = NULL;
1757 ba97b2d7 2024-03-20 stsp dprintf(fd, "from: %s\n", got_object_commit_get_author(commit));
1758 ba97b2d7 2024-03-20 stsp author = got_object_commit_get_author(commit);
1759 ba97b2d7 2024-03-20 stsp committer = got_object_commit_get_committer(commit);
1760 ba97b2d7 2024-03-20 stsp if (strcmp(author, committer) != 0)
1761 ba97b2d7 2024-03-20 stsp dprintf(fd, "via: %s\n", committer);
1762 ba97b2d7 2024-03-20 stsp committer_time = got_object_commit_get_committer_time(commit);
1763 939d3016 2024-04-23 op dprintf(fd, "date: %lld\n", (long long)committer_time);
1764 ba97b2d7 2024-03-20 stsp if (got_object_commit_get_nparents(commit) > 1) {
1765 ba97b2d7 2024-03-20 stsp const struct got_object_id_queue *parent_ids;
1766 ba97b2d7 2024-03-20 stsp struct got_object_qid *qid;
1767 ba97b2d7 2024-03-20 stsp int n = 1;
1768 ba97b2d7 2024-03-20 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1769 ba97b2d7 2024-03-20 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
1770 ba97b2d7 2024-03-20 stsp err = got_object_id_str(&id_str, &qid->id);
1771 ba97b2d7 2024-03-20 stsp if (err)
1772 ba97b2d7 2024-03-20 stsp goto done;
1773 ba97b2d7 2024-03-20 stsp dprintf(fd, "parent %d: %s\n", n++, id_str);
1774 ba97b2d7 2024-03-20 stsp free(id_str);
1775 ba97b2d7 2024-03-20 stsp id_str = NULL;
1776 ba97b2d7 2024-03-20 stsp }
1777 ba97b2d7 2024-03-20 stsp }
1778 ba97b2d7 2024-03-20 stsp
1779 ba97b2d7 2024-03-20 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1780 ba97b2d7 2024-03-20 stsp if (err)
1781 ba97b2d7 2024-03-20 stsp goto done;
1782 fb5636be 2024-03-27 op
1783 fb5636be 2024-03-27 op dprintf(fd, "messagelen: %zu\n", strlen(logmsg0));
1784 ba97b2d7 2024-03-20 stsp
1785 ba97b2d7 2024-03-20 stsp logmsg = logmsg0;
1786 ba97b2d7 2024-03-20 stsp do {
1787 ba97b2d7 2024-03-20 stsp line = strsep(&logmsg, "\n");
1788 ba97b2d7 2024-03-20 stsp if (line)
1789 ba97b2d7 2024-03-20 stsp dprintf(fd, " %s\n", line);
1790 ba97b2d7 2024-03-20 stsp } while (line);
1791 ba97b2d7 2024-03-20 stsp free(logmsg0);
1792 ba97b2d7 2024-03-20 stsp
1793 ba97b2d7 2024-03-20 stsp err = print_diffstat(diffstat, fd);
1794 ba97b2d7 2024-03-20 stsp if (err)
1795 ba97b2d7 2024-03-20 stsp goto done;
1796 ba97b2d7 2024-03-20 stsp
1797 ba97b2d7 2024-03-20 stsp if (fsync(fd) == -1 && err == NULL)
1798 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("fsync");
1799 ba97b2d7 2024-03-20 stsp done:
1800 ba97b2d7 2024-03-20 stsp free(id_str);
1801 ba97b2d7 2024-03-20 stsp return err;
1802 ba97b2d7 2024-03-20 stsp }
1803 ba97b2d7 2024-03-20 stsp
1804 ba97b2d7 2024-03-20 stsp static const struct got_error *
1805 ba97b2d7 2024-03-20 stsp get_changed_paths(struct got_pathlist_head *paths,
1806 ba97b2d7 2024-03-20 stsp struct got_commit_object *commit, struct got_repository *repo,
1807 ba97b2d7 2024-03-20 stsp struct got_diffstat_cb_arg *dsa)
1808 ba97b2d7 2024-03-20 stsp {
1809 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
1810 ba97b2d7 2024-03-20 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1811 ba97b2d7 2024-03-20 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1812 ba97b2d7 2024-03-20 stsp struct got_object_qid *qid;
1813 ba97b2d7 2024-03-20 stsp got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
1814 ba97b2d7 2024-03-20 stsp FILE *f1 = repo_write.diff.f1, *f2 = repo_write.diff.f2;
1815 ba97b2d7 2024-03-20 stsp int fd1 = repo_write.diff.fd1, fd2 = repo_write.diff.fd2;
1816 ba97b2d7 2024-03-20 stsp
1817 ba97b2d7 2024-03-20 stsp if (dsa)
1818 ba97b2d7 2024-03-20 stsp cb = got_diff_tree_compute_diffstat;
1819 ba97b2d7 2024-03-20 stsp
1820 ba97b2d7 2024-03-20 stsp err = got_opentemp_truncate(f1);
1821 ba97b2d7 2024-03-20 stsp if (err)
1822 ba97b2d7 2024-03-20 stsp return err;
1823 ba97b2d7 2024-03-20 stsp err = got_opentemp_truncate(f2);
1824 ba97b2d7 2024-03-20 stsp if (err)
1825 ba97b2d7 2024-03-20 stsp return err;
1826 ba97b2d7 2024-03-20 stsp err = got_opentemp_truncatefd(fd1);
1827 ba97b2d7 2024-03-20 stsp if (err)
1828 ba97b2d7 2024-03-20 stsp return err;
1829 ba97b2d7 2024-03-20 stsp err = got_opentemp_truncatefd(fd2);
1830 ba97b2d7 2024-03-20 stsp if (err)
1831 ba97b2d7 2024-03-20 stsp return err;
1832 ba97b2d7 2024-03-20 stsp
1833 ba97b2d7 2024-03-20 stsp qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1834 ba97b2d7 2024-03-20 stsp if (qid != NULL) {
1835 ba97b2d7 2024-03-20 stsp struct got_commit_object *pcommit;
1836 ba97b2d7 2024-03-20 stsp err = got_object_open_as_commit(&pcommit, repo,
1837 ba97b2d7 2024-03-20 stsp &qid->id);
1838 ba97b2d7 2024-03-20 stsp if (err)
1839 ba97b2d7 2024-03-20 stsp return err;
1840 ba97b2d7 2024-03-20 stsp
1841 ba97b2d7 2024-03-20 stsp tree_id1 = got_object_id_dup(
1842 ba97b2d7 2024-03-20 stsp got_object_commit_get_tree_id(pcommit));
1843 ba97b2d7 2024-03-20 stsp if (tree_id1 == NULL) {
1844 ba97b2d7 2024-03-20 stsp got_object_commit_close(pcommit);
1845 ba97b2d7 2024-03-20 stsp return got_error_from_errno("got_object_id_dup");
1846 ba97b2d7 2024-03-20 stsp }
1847 ba97b2d7 2024-03-20 stsp got_object_commit_close(pcommit);
1848 ba97b2d7 2024-03-20 stsp
1849 ba97b2d7 2024-03-20 stsp }
1850 ba97b2d7 2024-03-20 stsp
1851 ba97b2d7 2024-03-20 stsp if (tree_id1) {
1852 ba97b2d7 2024-03-20 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1853 ba97b2d7 2024-03-20 stsp if (err)
1854 ba97b2d7 2024-03-20 stsp goto done;
1855 ba97b2d7 2024-03-20 stsp }
1856 ba97b2d7 2024-03-20 stsp
1857 ba97b2d7 2024-03-20 stsp tree_id2 = got_object_commit_get_tree_id(commit);
1858 ba97b2d7 2024-03-20 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1859 ba97b2d7 2024-03-20 stsp if (err)
1860 ba97b2d7 2024-03-20 stsp goto done;
1861 ba97b2d7 2024-03-20 stsp
1862 ba97b2d7 2024-03-20 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
1863 ba97b2d7 2024-03-20 stsp cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
1864 ba97b2d7 2024-03-20 stsp done:
1865 ba97b2d7 2024-03-20 stsp if (tree1)
1866 ba97b2d7 2024-03-20 stsp got_object_tree_close(tree1);
1867 ba97b2d7 2024-03-20 stsp if (tree2)
1868 ba97b2d7 2024-03-20 stsp got_object_tree_close(tree2);
1869 ba97b2d7 2024-03-20 stsp free(tree_id1);
1870 ba97b2d7 2024-03-20 stsp return err;
1871 ba97b2d7 2024-03-20 stsp }
1872 ba97b2d7 2024-03-20 stsp
1873 ba97b2d7 2024-03-20 stsp static const struct got_error *
1874 ba97b2d7 2024-03-20 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
1875 ba97b2d7 2024-03-20 stsp struct got_repository *repo, int fd)
1876 ba97b2d7 2024-03-20 stsp {
1877 ba97b2d7 2024-03-20 stsp const struct got_error *err;
1878 ba97b2d7 2024-03-20 stsp struct got_commit_graph *graph;
1879 ba97b2d7 2024-03-20 stsp struct got_object_id_queue reversed_commits;
1880 ba97b2d7 2024-03-20 stsp struct got_object_qid *qid;
1881 ba97b2d7 2024-03-20 stsp struct got_commit_object *commit = NULL;
1882 ba97b2d7 2024-03-20 stsp struct got_pathlist_head changed_paths;
1883 ba97b2d7 2024-03-20 stsp int ncommits = 0;
1884 ba97b2d7 2024-03-20 stsp const int shortlog_threshold = 50;
1885 ba97b2d7 2024-03-20 stsp
1886 ba97b2d7 2024-03-20 stsp STAILQ_INIT(&reversed_commits);
1887 ba97b2d7 2024-03-20 stsp TAILQ_INIT(&changed_paths);
1888 ba97b2d7 2024-03-20 stsp
1889 ba97b2d7 2024-03-20 stsp /* XXX first-parent only for now */
1890 ba97b2d7 2024-03-20 stsp err = got_commit_graph_open(&graph, "/", 1);
1891 ba97b2d7 2024-03-20 stsp if (err)
1892 ba97b2d7 2024-03-20 stsp return err;
1893 98297eed 2024-03-27 stsp err = got_commit_graph_bfsort(graph, root_id, repo,
1894 ba97b2d7 2024-03-20 stsp check_cancelled, NULL);
1895 ba97b2d7 2024-03-20 stsp if (err)
1896 ba97b2d7 2024-03-20 stsp goto done;
1897 ba97b2d7 2024-03-20 stsp for (;;) {
1898 ba97b2d7 2024-03-20 stsp struct got_object_id id;
1899 ba97b2d7 2024-03-20 stsp
1900 ba97b2d7 2024-03-20 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1901 ba97b2d7 2024-03-20 stsp check_cancelled, NULL);
1902 ba97b2d7 2024-03-20 stsp if (err) {
1903 ba97b2d7 2024-03-20 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1904 ba97b2d7 2024-03-20 stsp err = NULL;
1905 ba97b2d7 2024-03-20 stsp break;
1906 ba97b2d7 2024-03-20 stsp }
1907 ba97b2d7 2024-03-20 stsp
1908 ba97b2d7 2024-03-20 stsp err = got_object_open_as_commit(&commit, repo, &id);
1909 ba97b2d7 2024-03-20 stsp if (err)
1910 ba97b2d7 2024-03-20 stsp break;
1911 ba97b2d7 2024-03-20 stsp
1912 ba97b2d7 2024-03-20 stsp if (end_id && got_object_id_cmp(&id, end_id) == 0)
1913 ba97b2d7 2024-03-20 stsp break;
1914 ba97b2d7 2024-03-20 stsp
1915 ba97b2d7 2024-03-20 stsp err = got_object_qid_alloc(&qid, &id);
1916 ba97b2d7 2024-03-20 stsp if (err)
1917 ba97b2d7 2024-03-20 stsp break;
1918 ba97b2d7 2024-03-20 stsp
1919 ba97b2d7 2024-03-20 stsp STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
1920 ba97b2d7 2024-03-20 stsp ncommits++;
1921 ba97b2d7 2024-03-20 stsp got_object_commit_close(commit);
1922 ba97b2d7 2024-03-20 stsp
1923 ba97b2d7 2024-03-20 stsp if (end_id == NULL)
1924 ba97b2d7 2024-03-20 stsp break;
1925 ba97b2d7 2024-03-20 stsp }
1926 ba97b2d7 2024-03-20 stsp
1927 ba97b2d7 2024-03-20 stsp STAILQ_FOREACH(qid, &reversed_commits, entry) {
1928 ba97b2d7 2024-03-20 stsp struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
1929 ba97b2d7 2024-03-20 stsp &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
1930 ba97b2d7 2024-03-20 stsp
1931 ba97b2d7 2024-03-20 stsp err = got_object_open_as_commit(&commit, repo, &qid->id);
1932 ba97b2d7 2024-03-20 stsp if (err)
1933 ba97b2d7 2024-03-20 stsp break;
1934 7a095277 2024-03-28 op
1935 ba97b2d7 2024-03-20 stsp if (ncommits > shortlog_threshold) {
1936 ba97b2d7 2024-03-20 stsp err = print_commit_oneline(commit, &qid->id,
1937 ba97b2d7 2024-03-20 stsp repo, fd);
1938 ba97b2d7 2024-03-20 stsp if (err)
1939 ba97b2d7 2024-03-20 stsp break;
1940 ba97b2d7 2024-03-20 stsp } else {
1941 ba97b2d7 2024-03-20 stsp err = get_changed_paths(&changed_paths, commit,
1942 ba97b2d7 2024-03-20 stsp repo, &dsa);
1943 ba97b2d7 2024-03-20 stsp if (err)
1944 ba97b2d7 2024-03-20 stsp break;
1945 ba97b2d7 2024-03-20 stsp err = print_commit(commit, &qid->id, repo,
1946 ba97b2d7 2024-03-20 stsp &changed_paths, &dsa, fd);
1947 ba97b2d7 2024-03-20 stsp }
1948 ba97b2d7 2024-03-20 stsp got_object_commit_close(commit);
1949 ba97b2d7 2024-03-20 stsp commit = NULL;
1950 ba97b2d7 2024-03-20 stsp got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
1951 ba97b2d7 2024-03-20 stsp }
1952 ba97b2d7 2024-03-20 stsp done:
1953 ba97b2d7 2024-03-20 stsp if (commit)
1954 ba97b2d7 2024-03-20 stsp got_object_commit_close(commit);
1955 ba97b2d7 2024-03-20 stsp while (!STAILQ_EMPTY(&reversed_commits)) {
1956 ba97b2d7 2024-03-20 stsp qid = STAILQ_FIRST(&reversed_commits);
1957 ba97b2d7 2024-03-20 stsp STAILQ_REMOVE_HEAD(&reversed_commits, entry);
1958 ba97b2d7 2024-03-20 stsp got_object_qid_free(qid);
1959 ba97b2d7 2024-03-20 stsp }
1960 ba97b2d7 2024-03-20 stsp got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
1961 ba97b2d7 2024-03-20 stsp got_commit_graph_close(graph);
1962 ba97b2d7 2024-03-20 stsp return err;
1963 ba97b2d7 2024-03-20 stsp }
1964 ba97b2d7 2024-03-20 stsp
1965 ba97b2d7 2024-03-20 stsp static const struct got_error *
1966 7a095277 2024-03-28 op print_tag(struct got_object_id *id,
1967 ba97b2d7 2024-03-20 stsp const char *refname, struct got_repository *repo, int fd)
1968 ba97b2d7 2024-03-20 stsp {
1969 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
1970 ba97b2d7 2024-03-20 stsp struct got_tag_object *tag = NULL;
1971 ba97b2d7 2024-03-20 stsp const char *tagger = NULL;
1972 939d3016 2024-04-23 op char *id_str = NULL, *tagmsg0 = NULL, *tagmsg, *line;
1973 ba97b2d7 2024-03-20 stsp time_t tagger_time;
1974 ba97b2d7 2024-03-20 stsp
1975 ba97b2d7 2024-03-20 stsp err = got_object_open_as_tag(&tag, repo, id);
1976 ba97b2d7 2024-03-20 stsp if (err)
1977 ba97b2d7 2024-03-20 stsp return err;
1978 ba97b2d7 2024-03-20 stsp
1979 ba97b2d7 2024-03-20 stsp tagger = got_object_tag_get_tagger(tag);
1980 ba97b2d7 2024-03-20 stsp tagger_time = got_object_tag_get_tagger_time(tag);
1981 ba97b2d7 2024-03-20 stsp err = got_object_id_str(&id_str,
1982 ba97b2d7 2024-03-20 stsp got_object_tag_get_object_id(tag));
1983 ba97b2d7 2024-03-20 stsp if (err)
1984 ba97b2d7 2024-03-20 stsp goto done;
1985 ba97b2d7 2024-03-20 stsp
1986 ba97b2d7 2024-03-20 stsp dprintf(fd, "tag %s\n", refname);
1987 ba97b2d7 2024-03-20 stsp dprintf(fd, "from: %s\n", tagger);
1988 939d3016 2024-04-23 op dprintf(fd, "date: %lld\n", (long long)tagger_time);
1989 ba97b2d7 2024-03-20 stsp
1990 ba97b2d7 2024-03-20 stsp switch (got_object_tag_get_object_type(tag)) {
1991 ba97b2d7 2024-03-20 stsp case GOT_OBJ_TYPE_BLOB:
1992 ba97b2d7 2024-03-20 stsp dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
1993 ba97b2d7 2024-03-20 stsp break;
1994 ba97b2d7 2024-03-20 stsp case GOT_OBJ_TYPE_TREE:
1995 ba97b2d7 2024-03-20 stsp dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
1996 ba97b2d7 2024-03-20 stsp break;
1997 ba97b2d7 2024-03-20 stsp case GOT_OBJ_TYPE_COMMIT:
1998 ba97b2d7 2024-03-20 stsp dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
1999 ba97b2d7 2024-03-20 stsp break;
2000 ba97b2d7 2024-03-20 stsp case GOT_OBJ_TYPE_TAG:
2001 ba97b2d7 2024-03-20 stsp dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
2002 ba97b2d7 2024-03-20 stsp break;
2003 ba97b2d7 2024-03-20 stsp default:
2004 ba97b2d7 2024-03-20 stsp break;
2005 ba97b2d7 2024-03-20 stsp }
2006 ba97b2d7 2024-03-20 stsp
2007 ba97b2d7 2024-03-20 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
2008 ba97b2d7 2024-03-20 stsp if (tagmsg0 == NULL) {
2009 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strdup");
2010 ba97b2d7 2024-03-20 stsp goto done;
2011 ba97b2d7 2024-03-20 stsp }
2012 fb5636be 2024-03-27 op
2013 fb5636be 2024-03-27 op dprintf(fd, "messagelen: %zu\n", strlen(tagmsg0));
2014 fb5636be 2024-03-27 op
2015 ba97b2d7 2024-03-20 stsp tagmsg = tagmsg0;
2016 ba97b2d7 2024-03-20 stsp do {
2017 ba97b2d7 2024-03-20 stsp line = strsep(&tagmsg, "\n");
2018 ba97b2d7 2024-03-20 stsp if (line)
2019 ba97b2d7 2024-03-20 stsp dprintf(fd, " %s\n", line);
2020 ba97b2d7 2024-03-20 stsp } while (line);
2021 ba97b2d7 2024-03-20 stsp free(tagmsg0);
2022 ba97b2d7 2024-03-20 stsp done:
2023 ba97b2d7 2024-03-20 stsp if (tag)
2024 ba97b2d7 2024-03-20 stsp got_object_tag_close(tag);
2025 ba97b2d7 2024-03-20 stsp free(id_str);
2026 ba97b2d7 2024-03-20 stsp return err;
2027 ba97b2d7 2024-03-20 stsp }
2028 ba97b2d7 2024-03-20 stsp
2029 ba97b2d7 2024-03-20 stsp static const struct got_error *
2030 1b1a386d 2024-07-09 op notify_changed_ref(const char *refname, struct got_object_id *old_id,
2031 1b1a386d 2024-07-09 op struct got_object_id *new_id, struct gotd_imsgev *iev, int fd)
2032 ba97b2d7 2024-03-20 stsp {
2033 ba97b2d7 2024-03-20 stsp const struct got_error *err;
2034 ba97b2d7 2024-03-20 stsp int old_obj_type, new_obj_type;
2035 ba97b2d7 2024-03-20 stsp const char *label;
2036 ba97b2d7 2024-03-20 stsp char *new_id_str = NULL;
2037 ba97b2d7 2024-03-20 stsp
2038 1b1a386d 2024-07-09 op err = got_object_get_type(&old_obj_type, repo_write.repo, old_id);
2039 ba97b2d7 2024-03-20 stsp if (err)
2040 ba97b2d7 2024-03-20 stsp return err;
2041 ba97b2d7 2024-03-20 stsp
2042 1b1a386d 2024-07-09 op err = got_object_get_type(&new_obj_type, repo_write.repo, new_id);
2043 ba97b2d7 2024-03-20 stsp if (err)
2044 ba97b2d7 2024-03-20 stsp return err;
2045 ba97b2d7 2024-03-20 stsp
2046 ba97b2d7 2024-03-20 stsp switch (new_obj_type) {
2047 ba97b2d7 2024-03-20 stsp case GOT_OBJ_TYPE_COMMIT:
2048 1b1a386d 2024-07-09 op err = print_commits(new_id,
2049 1b1a386d 2024-07-09 op old_obj_type == GOT_OBJ_TYPE_COMMIT ? old_id : NULL,
2050 ba97b2d7 2024-03-20 stsp repo_write.repo, fd);
2051 ba97b2d7 2024-03-20 stsp break;
2052 ba97b2d7 2024-03-20 stsp case GOT_OBJ_TYPE_TAG:
2053 1b1a386d 2024-07-09 op err = print_tag(new_id, refname, repo_write.repo, fd);
2054 ba97b2d7 2024-03-20 stsp break;
2055 ba97b2d7 2024-03-20 stsp default:
2056 ba97b2d7 2024-03-20 stsp err = got_object_type_label(&label, new_obj_type);
2057 ba97b2d7 2024-03-20 stsp if (err)
2058 ba97b2d7 2024-03-20 stsp goto done;
2059 1b1a386d 2024-07-09 op err = got_object_id_str(&new_id_str, new_id);
2060 ba97b2d7 2024-03-20 stsp if (err)
2061 ba97b2d7 2024-03-20 stsp goto done;
2062 ba97b2d7 2024-03-20 stsp dprintf(fd, "%s: %s object %s\n", refname, label, new_id_str);
2063 ba97b2d7 2024-03-20 stsp break;
2064 ba97b2d7 2024-03-20 stsp }
2065 ba97b2d7 2024-03-20 stsp done:
2066 ba97b2d7 2024-03-20 stsp free(new_id_str);
2067 ba97b2d7 2024-03-20 stsp return err;
2068 ba97b2d7 2024-03-20 stsp }
2069 ba97b2d7 2024-03-20 stsp
2070 ba97b2d7 2024-03-20 stsp static const struct got_error *
2071 1b1a386d 2024-07-09 op notify_created_ref(const char *refname, struct got_object_id *id,
2072 ba97b2d7 2024-03-20 stsp struct gotd_imsgev *iev, int fd)
2073 ba97b2d7 2024-03-20 stsp {
2074 ba97b2d7 2024-03-20 stsp const struct got_error *err;
2075 ba97b2d7 2024-03-20 stsp int obj_type;
2076 ba97b2d7 2024-03-20 stsp
2077 1b1a386d 2024-07-09 op err = got_object_get_type(&obj_type, repo_write.repo, id);
2078 ba97b2d7 2024-03-20 stsp if (err)
2079 ba97b2d7 2024-03-20 stsp return err;
2080 ba97b2d7 2024-03-20 stsp
2081 ba97b2d7 2024-03-20 stsp if (obj_type == GOT_OBJ_TYPE_TAG)
2082 1b1a386d 2024-07-09 op return print_tag(id, refname, repo_write.repo, fd);
2083 ba97b2d7 2024-03-20 stsp
2084 1b1a386d 2024-07-09 op return print_commits(id, NULL, repo_write.repo, fd);
2085 ba97b2d7 2024-03-20 stsp }
2086 ba97b2d7 2024-03-20 stsp
2087 ba97b2d7 2024-03-20 stsp static const struct got_error *
2088 ba97b2d7 2024-03-20 stsp render_notification(struct imsg *imsg, struct gotd_imsgev *iev)
2089 ba97b2d7 2024-03-20 stsp {
2090 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
2091 ba97b2d7 2024-03-20 stsp struct gotd_imsg_notification_content ireq;
2092 ba97b2d7 2024-03-20 stsp size_t datalen, len;
2093 c237d891 2024-03-26 op char *refname = NULL;
2094 ba97b2d7 2024-03-20 stsp struct ibuf *wbuf;
2095 c237d891 2024-03-26 op int fd = -1;
2096 ba97b2d7 2024-03-20 stsp
2097 ba97b2d7 2024-03-20 stsp fd = imsg_get_fd(imsg);
2098 ba97b2d7 2024-03-20 stsp if (fd == -1)
2099 ba97b2d7 2024-03-20 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
2100 ba97b2d7 2024-03-20 stsp
2101 ba97b2d7 2024-03-20 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
2102 c237d891 2024-03-26 op if (datalen < sizeof(ireq)) {
2103 c237d891 2024-03-26 op err = got_error(GOT_ERR_PRIVSEP_LEN);
2104 c237d891 2024-03-26 op goto done;
2105 c237d891 2024-03-26 op }
2106 ba97b2d7 2024-03-20 stsp
2107 ba97b2d7 2024-03-20 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
2108 ba97b2d7 2024-03-20 stsp
2109 c237d891 2024-03-26 op if (datalen != sizeof(ireq) + ireq.refname_len) {
2110 c237d891 2024-03-26 op err = got_error(GOT_ERR_PRIVSEP_LEN);
2111 c237d891 2024-03-26 op goto done;
2112 c237d891 2024-03-26 op }
2113 ba97b2d7 2024-03-20 stsp
2114 ba97b2d7 2024-03-20 stsp refname = strndup(imsg->data + sizeof(ireq), ireq.refname_len);
2115 c237d891 2024-03-26 op if (refname == NULL) {
2116 c237d891 2024-03-26 op err = got_error_from_errno("strndup");
2117 c237d891 2024-03-26 op goto done;
2118 c237d891 2024-03-26 op }
2119 ba97b2d7 2024-03-20 stsp
2120 ba97b2d7 2024-03-20 stsp switch (ireq.action) {
2121 ba97b2d7 2024-03-20 stsp case GOTD_NOTIF_ACTION_CREATED:
2122 1b1a386d 2024-07-09 op err = notify_created_ref(refname, &ireq.new_id, iev, fd);
2123 ba97b2d7 2024-03-20 stsp break;
2124 ba97b2d7 2024-03-20 stsp case GOTD_NOTIF_ACTION_REMOVED:
2125 1b1a386d 2024-07-09 op err = notify_removed_ref(refname, &ireq.old_id, iev, fd);
2126 ba97b2d7 2024-03-20 stsp break;
2127 ba97b2d7 2024-03-20 stsp case GOTD_NOTIF_ACTION_CHANGED:
2128 1b1a386d 2024-07-09 op err = notify_changed_ref(refname, &ireq.old_id, &ireq.new_id,
2129 ba97b2d7 2024-03-20 stsp iev, fd);
2130 ba97b2d7 2024-03-20 stsp break;
2131 ba97b2d7 2024-03-20 stsp }
2132 cef7eb79 2024-03-26 op if (err != NULL)
2133 cef7eb79 2024-03-26 op goto done;
2134 ba97b2d7 2024-03-20 stsp
2135 ba97b2d7 2024-03-20 stsp if (fsync(fd) == -1) {
2136 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("fsync");
2137 ba97b2d7 2024-03-20 stsp goto done;
2138 ba97b2d7 2024-03-20 stsp }
2139 ba97b2d7 2024-03-20 stsp
2140 ba97b2d7 2024-03-20 stsp len = sizeof(ireq) + ireq.refname_len;
2141 ba97b2d7 2024-03-20 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_NOTIFY, PROC_REPO_WRITE,
2142 ba97b2d7 2024-03-20 stsp repo_write.pid, len);
2143 ba97b2d7 2024-03-20 stsp if (wbuf == NULL) {
2144 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("imsg_create REF");
2145 ba97b2d7 2024-03-20 stsp goto done;
2146 ba97b2d7 2024-03-20 stsp }
2147 ba97b2d7 2024-03-20 stsp if (imsg_add(wbuf, &ireq, sizeof(ireq)) == -1) {
2148 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("imsg_add NOTIFY");
2149 ba97b2d7 2024-03-20 stsp goto done;
2150 ba97b2d7 2024-03-20 stsp }
2151 ba97b2d7 2024-03-20 stsp if (imsg_add(wbuf, refname, ireq.refname_len) == -1) {
2152 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("imsg_add NOTIFY");
2153 ba97b2d7 2024-03-20 stsp goto done;
2154 ba97b2d7 2024-03-20 stsp }
2155 ba97b2d7 2024-03-20 stsp
2156 ba97b2d7 2024-03-20 stsp imsg_close(&iev->ibuf, wbuf);
2157 ba97b2d7 2024-03-20 stsp gotd_imsg_event_add(iev);
2158 ba97b2d7 2024-03-20 stsp done:
2159 ba97b2d7 2024-03-20 stsp free(refname);
2160 c237d891 2024-03-26 op if (fd != -1 && close(fd) == -1 && err == NULL)
2161 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("close");
2162 ba97b2d7 2024-03-20 stsp return err;
2163 ba97b2d7 2024-03-20 stsp }
2164 ba97b2d7 2024-03-20 stsp
2165 13b2bc37 2022-10-23 stsp static void
2166 ae7c1b78 2023-01-10 stsp repo_write_dispatch_session(int fd, short event, void *arg)
2167 13b2bc37 2022-10-23 stsp {
2168 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
2169 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
2170 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
2171 13b2bc37 2022-10-23 stsp struct imsg imsg;
2172 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
2173 13b2bc37 2022-10-23 stsp ssize_t n;
2174 0ff2c315 2023-01-18 stsp int shut = 0, have_packfile = 0;
2175 13b2bc37 2022-10-23 stsp
2176 13b2bc37 2022-10-23 stsp if (event & EV_READ) {
2177 13b2bc37 2022-10-23 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
2178 13b2bc37 2022-10-23 stsp fatal("imsg_read error");
2179 13b2bc37 2022-10-23 stsp if (n == 0) /* Connection closed. */
2180 13b2bc37 2022-10-23 stsp shut = 1;
2181 13b2bc37 2022-10-23 stsp }
2182 13b2bc37 2022-10-23 stsp
2183 13b2bc37 2022-10-23 stsp if (event & EV_WRITE) {
2184 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
2185 13b2bc37 2022-10-23 stsp if (n == -1 && errno != EAGAIN)
2186 13b2bc37 2022-10-23 stsp fatal("msgbuf_write");
2187 13b2bc37 2022-10-23 stsp if (n == 0) /* Connection closed. */
2188 13b2bc37 2022-10-23 stsp shut = 1;
2189 13b2bc37 2022-10-23 stsp }
2190 13b2bc37 2022-10-23 stsp
2191 13b2bc37 2022-10-23 stsp for (;;) {
2192 13b2bc37 2022-10-23 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
2193 13b2bc37 2022-10-23 stsp fatal("%s: imsg_get error", __func__);
2194 13b2bc37 2022-10-23 stsp if (n == 0) /* No more messages. */
2195 13b2bc37 2022-10-23 stsp break;
2196 13b2bc37 2022-10-23 stsp
2197 1a52c9bf 2022-12-30 stsp if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
2198 1cb49b67 2024-03-21 stsp !repo_write.refs_listed) {
2199 1a52c9bf 2022-12-30 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2200 1a52c9bf 2022-12-30 stsp break;
2201 1a52c9bf 2022-12-30 stsp }
2202 1a52c9bf 2022-12-30 stsp
2203 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
2204 13b2bc37 2022-10-23 stsp case GOTD_IMSG_LIST_REFS_INTERNAL:
2205 1a52c9bf 2022-12-30 stsp err = list_refs(&imsg);
2206 13b2bc37 2022-10-23 stsp if (err)
2207 88f6dccd 2023-03-04 op log_warnx("ls-refs: %s", err->msg);
2208 13b2bc37 2022-10-23 stsp break;
2209 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE:
2210 1a52c9bf 2022-12-30 stsp err = recv_ref_update(&imsg);
2211 13b2bc37 2022-10-23 stsp if (err)
2212 88f6dccd 2023-03-04 op log_warnx("ref-update: %s", err->msg);
2213 13b2bc37 2022-10-23 stsp break;
2214 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_PIPE:
2215 1a52c9bf 2022-12-30 stsp err = receive_pack_pipe(&imsg, iev);
2216 13b2bc37 2022-10-23 stsp if (err) {
2217 88f6dccd 2023-03-04 op log_warnx("receiving pack pipe: %s", err->msg);
2218 13b2bc37 2022-10-23 stsp break;
2219 13b2bc37 2022-10-23 stsp }
2220 13b2bc37 2022-10-23 stsp break;
2221 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKIDX_FILE:
2222 1a52c9bf 2022-12-30 stsp err = receive_pack_idx(&imsg, iev);
2223 13b2bc37 2022-10-23 stsp if (err) {
2224 88f6dccd 2023-03-04 op log_warnx("receiving pack index: %s",
2225 88f6dccd 2023-03-04 op err->msg);
2226 13b2bc37 2022-10-23 stsp break;
2227 13b2bc37 2022-10-23 stsp }
2228 13b2bc37 2022-10-23 stsp break;
2229 13b2bc37 2022-10-23 stsp case GOTD_IMSG_RECV_PACKFILE:
2230 9afa3de2 2023-04-04 stsp err = protect_refs_from_deletion();
2231 9afa3de2 2023-04-04 stsp if (err)
2232 9afa3de2 2023-04-04 stsp break;
2233 0ff2c315 2023-01-18 stsp err = recv_packfile(&have_packfile, &imsg);
2234 13b2bc37 2022-10-23 stsp if (err) {
2235 88f6dccd 2023-03-04 op log_warnx("receive packfile: %s", err->msg);
2236 13b2bc37 2022-10-23 stsp break;
2237 13b2bc37 2022-10-23 stsp }
2238 0ff2c315 2023-01-18 stsp if (have_packfile) {
2239 0ff2c315 2023-01-18 stsp err = verify_packfile();
2240 0ff2c315 2023-01-18 stsp if (err) {
2241 88f6dccd 2023-03-04 op log_warnx("verify packfile: %s",
2242 88f6dccd 2023-03-04 op err->msg);
2243 0ff2c315 2023-01-18 stsp break;
2244 0ff2c315 2023-01-18 stsp }
2245 0ff2c315 2023-01-18 stsp err = install_packfile(iev);
2246 0ff2c315 2023-01-18 stsp if (err) {
2247 88f6dccd 2023-03-04 op log_warnx("install packfile: %s",
2248 88f6dccd 2023-03-04 op err->msg);
2249 0ff2c315 2023-01-18 stsp break;
2250 0ff2c315 2023-01-18 stsp }
2251 30a624fb 2024-03-19 stsp /*
2252 30a624fb 2024-03-19 stsp * Ensure we re-read the pack index list
2253 30a624fb 2024-03-19 stsp * upon next access.
2254 30a624fb 2024-03-19 stsp */
2255 30a624fb 2024-03-19 stsp repo_write.repo->pack_path_mtime.tv_sec = 0;
2256 30a624fb 2024-03-19 stsp repo_write.repo->pack_path_mtime.tv_nsec = 0;
2257 13b2bc37 2022-10-23 stsp }
2258 1a52c9bf 2022-12-30 stsp err = update_refs(iev);
2259 13b2bc37 2022-10-23 stsp if (err) {
2260 88f6dccd 2023-03-04 op log_warnx("update refs: %s", err->msg);
2261 ba97b2d7 2024-03-20 stsp }
2262 ba97b2d7 2024-03-20 stsp break;
2263 ba97b2d7 2024-03-20 stsp case GOTD_IMSG_NOTIFY:
2264 ba97b2d7 2024-03-20 stsp err = render_notification(&imsg, iev);
2265 ba97b2d7 2024-03-20 stsp if (err) {
2266 ba97b2d7 2024-03-20 stsp log_warnx("render notification: %s", err->msg);
2267 ba97b2d7 2024-03-20 stsp shut = 1;
2268 13b2bc37 2022-10-23 stsp }
2269 13b2bc37 2022-10-23 stsp break;
2270 ae7c1b78 2023-01-10 stsp default:
2271 88f6dccd 2023-03-04 op log_debug("unexpected imsg %d", imsg.hdr.type);
2272 ae7c1b78 2023-01-10 stsp break;
2273 ae7c1b78 2023-01-10 stsp }
2274 ae7c1b78 2023-01-10 stsp
2275 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
2276 ae7c1b78 2023-01-10 stsp }
2277 ae7c1b78 2023-01-10 stsp
2278 ae7c1b78 2023-01-10 stsp if (!shut && check_cancelled(NULL) == NULL) {
2279 ae7c1b78 2023-01-10 stsp if (err &&
2280 ae7c1b78 2023-01-10 stsp gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
2281 ae7c1b78 2023-01-10 stsp client->id, err) == -1) {
2282 ae7c1b78 2023-01-10 stsp log_warnx("could not send error to parent: %s",
2283 ae7c1b78 2023-01-10 stsp err->msg);
2284 ae7c1b78 2023-01-10 stsp }
2285 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
2286 ae7c1b78 2023-01-10 stsp } else {
2287 ae7c1b78 2023-01-10 stsp /* This pipe is dead. Remove its event handler */
2288 ae7c1b78 2023-01-10 stsp event_del(&iev->ev);
2289 ae7c1b78 2023-01-10 stsp event_loopexit(NULL);
2290 ae7c1b78 2023-01-10 stsp }
2291 ae7c1b78 2023-01-10 stsp }
2292 ae7c1b78 2023-01-10 stsp
2293 ae7c1b78 2023-01-10 stsp static const struct got_error *
2294 ae7c1b78 2023-01-10 stsp recv_connect(struct imsg *imsg)
2295 ae7c1b78 2023-01-10 stsp {
2296 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = &repo_write.session_iev;
2297 ae7c1b78 2023-01-10 stsp size_t datalen;
2298 ae7c1b78 2023-01-10 stsp
2299 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
2300 ae7c1b78 2023-01-10 stsp if (datalen != 0)
2301 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
2302 ae7c1b78 2023-01-10 stsp
2303 ae7c1b78 2023-01-10 stsp if (repo_write.session_fd != -1)
2304 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
2305 ae7c1b78 2023-01-10 stsp
2306 2c52c623 2024-01-30 op repo_write.session_fd = imsg_get_fd(imsg);
2307 2c52c623 2024-01-30 op if (repo_write.session_fd == -1)
2308 2c52c623 2024-01-30 op return got_error(GOT_ERR_PRIVSEP_NO_FD);
2309 ae7c1b78 2023-01-10 stsp
2310 ae7c1b78 2023-01-10 stsp imsg_init(&iev->ibuf, repo_write.session_fd);
2311 ae7c1b78 2023-01-10 stsp iev->handler = repo_write_dispatch_session;
2312 ae7c1b78 2023-01-10 stsp iev->events = EV_READ;
2313 ae7c1b78 2023-01-10 stsp iev->handler_arg = NULL;
2314 ae7c1b78 2023-01-10 stsp event_set(&iev->ev, iev->ibuf.fd, EV_READ,
2315 ae7c1b78 2023-01-10 stsp repo_write_dispatch_session, iev);
2316 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
2317 ae7c1b78 2023-01-10 stsp
2318 ae7c1b78 2023-01-10 stsp return NULL;
2319 ae7c1b78 2023-01-10 stsp }
2320 ae7c1b78 2023-01-10 stsp
2321 ae7c1b78 2023-01-10 stsp static void
2322 ae7c1b78 2023-01-10 stsp repo_write_dispatch(int fd, short event, void *arg)
2323 ae7c1b78 2023-01-10 stsp {
2324 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
2325 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = arg;
2326 ae7c1b78 2023-01-10 stsp struct imsgbuf *ibuf = &iev->ibuf;
2327 ae7c1b78 2023-01-10 stsp struct imsg imsg;
2328 ae7c1b78 2023-01-10 stsp ssize_t n;
2329 ae7c1b78 2023-01-10 stsp int shut = 0;
2330 ae7c1b78 2023-01-10 stsp struct repo_write_client *client = &repo_write_client;
2331 ae7c1b78 2023-01-10 stsp
2332 ae7c1b78 2023-01-10 stsp if (event & EV_READ) {
2333 ae7c1b78 2023-01-10 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
2334 ae7c1b78 2023-01-10 stsp fatal("imsg_read error");
2335 ae7c1b78 2023-01-10 stsp if (n == 0) /* Connection closed. */
2336 1a52c9bf 2022-12-30 stsp shut = 1;
2337 ae7c1b78 2023-01-10 stsp }
2338 ae7c1b78 2023-01-10 stsp
2339 ae7c1b78 2023-01-10 stsp if (event & EV_WRITE) {
2340 ae7c1b78 2023-01-10 stsp n = msgbuf_write(&ibuf->w);
2341 ae7c1b78 2023-01-10 stsp if (n == -1 && errno != EAGAIN)
2342 ae7c1b78 2023-01-10 stsp fatal("msgbuf_write");
2343 ae7c1b78 2023-01-10 stsp if (n == 0) /* Connection closed. */
2344 ae7c1b78 2023-01-10 stsp shut = 1;
2345 ae7c1b78 2023-01-10 stsp }
2346 ae7c1b78 2023-01-10 stsp
2347 ae7c1b78 2023-01-10 stsp while (err == NULL && check_cancelled(NULL) == NULL) {
2348 ae7c1b78 2023-01-10 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
2349 ae7c1b78 2023-01-10 stsp fatal("%s: imsg_get", __func__);
2350 ae7c1b78 2023-01-10 stsp if (n == 0) /* No more messages. */
2351 13b2bc37 2022-10-23 stsp break;
2352 ae7c1b78 2023-01-10 stsp
2353 ae7c1b78 2023-01-10 stsp switch (imsg.hdr.type) {
2354 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CONNECT_REPO_CHILD:
2355 ae7c1b78 2023-01-10 stsp err = recv_connect(&imsg);
2356 ae7c1b78 2023-01-10 stsp break;
2357 13b2bc37 2022-10-23 stsp default:
2358 88f6dccd 2023-03-04 op log_debug("unexpected imsg %d", imsg.hdr.type);
2359 13b2bc37 2022-10-23 stsp break;
2360 13b2bc37 2022-10-23 stsp }
2361 13b2bc37 2022-10-23 stsp
2362 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
2363 13b2bc37 2022-10-23 stsp }
2364 13b2bc37 2022-10-23 stsp
2365 13b2bc37 2022-10-23 stsp if (!shut && check_cancelled(NULL) == NULL) {
2366 13b2bc37 2022-10-23 stsp if (err &&
2367 13b2bc37 2022-10-23 stsp gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
2368 1a52c9bf 2022-12-30 stsp client->id, err) == -1) {
2369 13b2bc37 2022-10-23 stsp log_warnx("could not send error to parent: %s",
2370 13b2bc37 2022-10-23 stsp err->msg);
2371 13b2bc37 2022-10-23 stsp }
2372 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
2373 13b2bc37 2022-10-23 stsp } else {
2374 13b2bc37 2022-10-23 stsp /* This pipe is dead. Remove its event handler */
2375 13b2bc37 2022-10-23 stsp event_del(&iev->ev);
2376 13b2bc37 2022-10-23 stsp event_loopexit(NULL);
2377 13b2bc37 2022-10-23 stsp }
2378 13b2bc37 2022-10-23 stsp }
2379 13b2bc37 2022-10-23 stsp
2380 13b2bc37 2022-10-23 stsp void
2381 eec68231 2022-12-14 stsp repo_write_main(const char *title, const char *repo_path,
2382 9afa3de2 2023-04-04 stsp int *pack_fds, int *temp_fds,
2383 ba97b2d7 2024-03-20 stsp FILE *diff_f1, FILE *diff_f2, int diff_fd1, int diff_fd2,
2384 9afa3de2 2023-04-04 stsp struct got_pathlist_head *protected_tag_namespaces,
2385 9afa3de2 2023-04-04 stsp struct got_pathlist_head *protected_branch_namespaces,
2386 9afa3de2 2023-04-04 stsp struct got_pathlist_head *protected_branches)
2387 13b2bc37 2022-10-23 stsp {
2388 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
2389 363c6230 2023-02-09 stsp struct repo_write_client *client = &repo_write_client;
2390 13b2bc37 2022-10-23 stsp struct gotd_imsgev iev;
2391 13b2bc37 2022-10-23 stsp
2392 363c6230 2023-02-09 stsp client->fd = -1;
2393 363c6230 2023-02-09 stsp client->pack_pipe = -1;
2394 363c6230 2023-02-09 stsp client->packidx_fd = -1;
2395 363c6230 2023-02-09 stsp client->pack.fd = -1;
2396 363c6230 2023-02-09 stsp
2397 13b2bc37 2022-10-23 stsp repo_write.title = title;
2398 13b2bc37 2022-10-23 stsp repo_write.pid = getpid();
2399 13b2bc37 2022-10-23 stsp repo_write.pack_fds = pack_fds;
2400 13b2bc37 2022-10-23 stsp repo_write.temp_fds = temp_fds;
2401 ae7c1b78 2023-01-10 stsp repo_write.session_fd = -1;
2402 ae7c1b78 2023-01-10 stsp repo_write.session_iev.ibuf.fd = -1;
2403 9afa3de2 2023-04-04 stsp repo_write.protected_tag_namespaces = protected_tag_namespaces;
2404 9afa3de2 2023-04-04 stsp repo_write.protected_branch_namespaces = protected_branch_namespaces;
2405 9afa3de2 2023-04-04 stsp repo_write.protected_branches = protected_branches;
2406 ba97b2d7 2024-03-20 stsp repo_write.diff.f1 = diff_f1;
2407 ba97b2d7 2024-03-20 stsp repo_write.diff.f2 = diff_f2;
2408 ba97b2d7 2024-03-20 stsp repo_write.diff.fd1 = diff_fd1;
2409 ba97b2d7 2024-03-20 stsp repo_write.diff.fd2 = diff_fd2;
2410 13b2bc37 2022-10-23 stsp
2411 1a52c9bf 2022-12-30 stsp STAILQ_INIT(&repo_write_client.ref_updates);
2412 13b2bc37 2022-10-23 stsp
2413 eec68231 2022-12-14 stsp err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
2414 13b2bc37 2022-10-23 stsp if (err)
2415 13b2bc37 2022-10-23 stsp goto done;
2416 13b2bc37 2022-10-23 stsp if (!got_repo_is_bare(repo_write.repo)) {
2417 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
2418 13b2bc37 2022-10-23 stsp "bare git repository required");
2419 13b2bc37 2022-10-23 stsp goto done;
2420 13b2bc37 2022-10-23 stsp }
2421 c214ca4f 2024-08-01 op if (got_repo_get_object_format(repo_write.repo) != GOT_HASH_SHA1) {
2422 c214ca4f 2024-08-01 op err = got_error_msg(GOT_ERR_NOT_IMPL,
2423 c214ca4f 2024-08-01 op "sha256 object IDs unsupported in network protocol");
2424 c214ca4f 2024-08-01 op goto done;
2425 c214ca4f 2024-08-01 op }
2426 13b2bc37 2022-10-23 stsp
2427 13b2bc37 2022-10-23 stsp got_repo_temp_fds_set(repo_write.repo, temp_fds);
2428 13b2bc37 2022-10-23 stsp
2429 13b2bc37 2022-10-23 stsp signal(SIGINT, catch_sigint);
2430 13b2bc37 2022-10-23 stsp signal(SIGTERM, catch_sigterm);
2431 13b2bc37 2022-10-23 stsp signal(SIGPIPE, SIG_IGN);
2432 13b2bc37 2022-10-23 stsp signal(SIGHUP, SIG_IGN);
2433 13b2bc37 2022-10-23 stsp
2434 8c6fc146 2022-11-17 stsp imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
2435 13b2bc37 2022-10-23 stsp iev.handler = repo_write_dispatch;
2436 13b2bc37 2022-10-23 stsp iev.events = EV_READ;
2437 13b2bc37 2022-10-23 stsp iev.handler_arg = NULL;
2438 13b2bc37 2022-10-23 stsp event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
2439 b50a2b46 2022-12-29 stsp if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
2440 b50a2b46 2022-12-29 stsp PROC_REPO_WRITE, -1, NULL, 0) == -1) {
2441 b50a2b46 2022-12-29 stsp err = got_error_from_errno("imsg compose REPO_CHILD_READY");
2442 13b2bc37 2022-10-23 stsp goto done;
2443 13b2bc37 2022-10-23 stsp }
2444 13b2bc37 2022-10-23 stsp
2445 13b2bc37 2022-10-23 stsp event_dispatch();
2446 13b2bc37 2022-10-23 stsp done:
2447 ba97b2d7 2024-03-20 stsp if (fclose(diff_f1) == EOF && err == NULL)
2448 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("fclose");
2449 ba97b2d7 2024-03-20 stsp if (fclose(diff_f2) == EOF && err == NULL)
2450 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("fclose");
2451 ba97b2d7 2024-03-20 stsp if (close(diff_fd1) == -1 && err == NULL)
2452 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("close");
2453 ba97b2d7 2024-03-20 stsp if (close(diff_fd2) == -1 && err == NULL)
2454 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("close");
2455 13b2bc37 2022-10-23 stsp if (err)
2456 13b2bc37 2022-10-23 stsp log_warnx("%s: %s", title, err->msg);
2457 13b2bc37 2022-10-23 stsp repo_write_shutdown();
2458 13b2bc37 2022-10-23 stsp }
2459 13b2bc37 2022-10-23 stsp
2460 13b2bc37 2022-10-23 stsp void
2461 13b2bc37 2022-10-23 stsp repo_write_shutdown(void)
2462 13b2bc37 2022-10-23 stsp {
2463 363c6230 2023-02-09 stsp struct repo_write_client *client = &repo_write_client;
2464 363c6230 2023-02-09 stsp struct gotd_ref_update *ref_update;
2465 363c6230 2023-02-09 stsp
2466 e8d451cc 2024-03-22 stsp log_debug("%s: shutting down", repo_write.title);
2467 363c6230 2023-02-09 stsp
2468 363c6230 2023-02-09 stsp while (!STAILQ_EMPTY(&client->ref_updates)) {
2469 363c6230 2023-02-09 stsp ref_update = STAILQ_FIRST(&client->ref_updates);
2470 363c6230 2023-02-09 stsp STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
2471 363c6230 2023-02-09 stsp got_ref_close(ref_update->ref);
2472 363c6230 2023-02-09 stsp free(ref_update);
2473 363c6230 2023-02-09 stsp }
2474 363c6230 2023-02-09 stsp
2475 363c6230 2023-02-09 stsp got_pack_close(&client->pack);
2476 363c6230 2023-02-09 stsp if (client->fd != -1)
2477 363c6230 2023-02-09 stsp close(client->fd);
2478 363c6230 2023-02-09 stsp if (client->pack_pipe != -1)
2479 363c6230 2023-02-09 stsp close(client->pack_pipe);
2480 363c6230 2023-02-09 stsp if (client->packidx_fd != -1)
2481 363c6230 2023-02-09 stsp close(client->packidx_fd);
2482 363c6230 2023-02-09 stsp
2483 13b2bc37 2022-10-23 stsp if (repo_write.repo)
2484 13b2bc37 2022-10-23 stsp got_repo_close(repo_write.repo);
2485 13b2bc37 2022-10-23 stsp got_repo_pack_fds_close(repo_write.pack_fds);
2486 8193d041 2022-10-30 stsp got_repo_temp_fds_close(repo_write.temp_fds);
2487 ae7c1b78 2023-01-10 stsp if (repo_write.session_fd != -1)
2488 ae7c1b78 2023-01-10 stsp close(repo_write.session_fd);
2489 13b2bc37 2022-10-23 stsp exit(0);
2490 13b2bc37 2022-10-23 stsp }