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 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, id->sha1, 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 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, id->sha1, 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 9afa3de2 2023-04-04 stsp got_sha1_digest_to_str(id->sha1, 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 9afa3de2 2023-04-04 stsp got_sha1_digest_to_str(id->sha1, 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 9afa3de2 2023-04-04 stsp got_sha1_digest_to_str(expected_yca_id->sha1, 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 9afa3de2 2023-04-04 stsp got_sha1_digest_to_str(qid->id.sha1,
496 9afa3de2 2023-04-04 stsp hex, sizeof(hex));
497 9afa3de2 2023-04-04 stsp err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
498 9afa3de2 2023-04-04 stsp "object %s is missing from pack file", hex);
499 9afa3de2 2023-04-04 stsp goto done;
500 9afa3de2 2023-04-04 stsp }
501 9afa3de2 2023-04-04 stsp
502 9afa3de2 2023-04-04 stsp err = got_object_open_from_packfile(&obj, &qid->id,
503 9afa3de2 2023-04-04 stsp pack, packidx, idx, repo_write.repo);
504 9afa3de2 2023-04-04 stsp if (err)
505 9afa3de2 2023-04-04 stsp goto done;
506 9afa3de2 2023-04-04 stsp
507 9afa3de2 2023-04-04 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
508 9afa3de2 2023-04-04 stsp got_sha1_digest_to_str(qid->id.sha1,
509 9afa3de2 2023-04-04 stsp hex, sizeof(hex));
510 9afa3de2 2023-04-04 stsp err = got_error_fmt(GOT_ERR_OBJ_TYPE,
511 9afa3de2 2023-04-04 stsp "%s is not pointing at a commit object",
512 9afa3de2 2023-04-04 stsp hex);
513 9afa3de2 2023-04-04 stsp goto done;
514 9afa3de2 2023-04-04 stsp }
515 9afa3de2 2023-04-04 stsp
516 9afa3de2 2023-04-04 stsp err = got_packfile_extract_object_to_mem(&buf, &len,
517 9afa3de2 2023-04-04 stsp obj, pack);
518 9afa3de2 2023-04-04 stsp if (err)
519 9afa3de2 2023-04-04 stsp goto done;
520 9afa3de2 2023-04-04 stsp
521 9afa3de2 2023-04-04 stsp err = got_object_parse_commit(&commit, buf, len);
522 9afa3de2 2023-04-04 stsp if (err)
523 9afa3de2 2023-04-04 stsp goto done;
524 9afa3de2 2023-04-04 stsp
525 9afa3de2 2023-04-04 stsp free(buf);
526 9afa3de2 2023-04-04 stsp buf = NULL;
527 9afa3de2 2023-04-04 stsp }
528 9afa3de2 2023-04-04 stsp
529 9afa3de2 2023-04-04 stsp got_object_close(obj);
530 9afa3de2 2023-04-04 stsp obj = NULL;
531 9afa3de2 2023-04-04 stsp
532 9afa3de2 2023-04-04 stsp STAILQ_REMOVE_HEAD(&ids, entry);
533 9afa3de2 2023-04-04 stsp got_object_qid_free(qid);
534 9afa3de2 2023-04-04 stsp qid = NULL;
535 9afa3de2 2023-04-04 stsp
536 9afa3de2 2023-04-04 stsp if (got_object_commit_get_nparents(commit) == 0)
537 9afa3de2 2023-04-04 stsp break;
538 9afa3de2 2023-04-04 stsp
539 9afa3de2 2023-04-04 stsp parent_ids = got_object_commit_get_parent_ids(commit);
540 9afa3de2 2023-04-04 stsp STAILQ_FOREACH(pid, parent_ids, entry) {
541 9afa3de2 2023-04-04 stsp err = check_cancelled(NULL);
542 9afa3de2 2023-04-04 stsp if (err)
543 9afa3de2 2023-04-04 stsp goto done;
544 9afa3de2 2023-04-04 stsp err = got_object_qid_alloc(&qid, &pid->id);
545 9afa3de2 2023-04-04 stsp if (err)
546 9afa3de2 2023-04-04 stsp goto done;
547 9afa3de2 2023-04-04 stsp STAILQ_INSERT_TAIL(&ids, qid, entry);
548 9afa3de2 2023-04-04 stsp qid = NULL;
549 9afa3de2 2023-04-04 stsp }
550 9afa3de2 2023-04-04 stsp got_object_commit_close(commit);
551 9afa3de2 2023-04-04 stsp commit = NULL;
552 9afa3de2 2023-04-04 stsp }
553 9afa3de2 2023-04-04 stsp
554 9afa3de2 2023-04-04 stsp if (!found_yca) {
555 9afa3de2 2023-04-04 stsp err = got_error_fmt(GOT_ERR_REF_PROTECTED, "%s",
556 9afa3de2 2023-04-04 stsp got_ref_get_name(ref));
557 9afa3de2 2023-04-04 stsp }
558 9afa3de2 2023-04-04 stsp done:
559 9afa3de2 2023-04-04 stsp got_object_idset_free(traversed_set);
560 9afa3de2 2023-04-04 stsp got_object_id_queue_free(&ids);
561 9afa3de2 2023-04-04 stsp free(buf);
562 9afa3de2 2023-04-04 stsp if (obj)
563 9afa3de2 2023-04-04 stsp got_object_close(obj);
564 9afa3de2 2023-04-04 stsp if (commit)
565 9afa3de2 2023-04-04 stsp got_object_commit_close(commit);
566 9afa3de2 2023-04-04 stsp free(expected_yca_id);
567 9afa3de2 2023-04-04 stsp return err;
568 9afa3de2 2023-04-04 stsp }
569 9afa3de2 2023-04-04 stsp
570 9afa3de2 2023-04-04 stsp static const struct got_error *
571 9afa3de2 2023-04-04 stsp protect_branch_namespace(const char *namespace, struct got_pack *pack,
572 9afa3de2 2023-04-04 stsp struct got_packidx *packidx, struct gotd_ref_update *ref_update)
573 9afa3de2 2023-04-04 stsp {
574 9afa3de2 2023-04-04 stsp const struct got_error *err;
575 9afa3de2 2023-04-04 stsp
576 9afa3de2 2023-04-04 stsp err = validate_namespace(namespace);
577 9afa3de2 2023-04-04 stsp if (err)
578 9afa3de2 2023-04-04 stsp return err;
579 9afa3de2 2023-04-04 stsp
580 9afa3de2 2023-04-04 stsp if (strncmp(namespace, got_ref_get_name(ref_update->ref),
581 9afa3de2 2023-04-04 stsp strlen(namespace)) != 0)
582 9afa3de2 2023-04-04 stsp return NULL;
583 9afa3de2 2023-04-04 stsp
584 9afa3de2 2023-04-04 stsp if (ref_update->ref_is_new) {
585 9afa3de2 2023-04-04 stsp return verify_object_type(&ref_update->new_id,
586 9afa3de2 2023-04-04 stsp GOT_OBJ_TYPE_COMMIT, pack, packidx);
587 9afa3de2 2023-04-04 stsp }
588 9afa3de2 2023-04-04 stsp
589 9afa3de2 2023-04-04 stsp return protect_require_yca(&ref_update->new_id,
590 9afa3de2 2023-04-04 stsp be32toh(packidx->hdr.fanout_table[0xff]), pack, packidx,
591 9afa3de2 2023-04-04 stsp ref_update->ref);
592 9afa3de2 2023-04-04 stsp }
593 9afa3de2 2023-04-04 stsp
594 9afa3de2 2023-04-04 stsp static const struct got_error *
595 9afa3de2 2023-04-04 stsp protect_branch(const char *refname, struct got_pack *pack,
596 9afa3de2 2023-04-04 stsp struct got_packidx *packidx, struct gotd_ref_update *ref_update)
597 9afa3de2 2023-04-04 stsp {
598 9afa3de2 2023-04-04 stsp if (strcmp(refname, got_ref_get_name(ref_update->ref)) != 0)
599 9afa3de2 2023-04-04 stsp return NULL;
600 9afa3de2 2023-04-04 stsp
601 9afa3de2 2023-04-04 stsp /* Always allow new branches to be created. */
602 9afa3de2 2023-04-04 stsp if (ref_update->ref_is_new) {
603 9afa3de2 2023-04-04 stsp return verify_object_type(&ref_update->new_id,
604 9afa3de2 2023-04-04 stsp GOT_OBJ_TYPE_COMMIT, pack, packidx);
605 9afa3de2 2023-04-04 stsp }
606 9afa3de2 2023-04-04 stsp
607 9afa3de2 2023-04-04 stsp return protect_require_yca(&ref_update->new_id,
608 9afa3de2 2023-04-04 stsp be32toh(packidx->hdr.fanout_table[0xff]), pack, packidx,
609 9afa3de2 2023-04-04 stsp ref_update->ref);
610 9afa3de2 2023-04-04 stsp }
611 9afa3de2 2023-04-04 stsp
612 9afa3de2 2023-04-04 stsp static const struct got_error *
613 1a52c9bf 2022-12-30 stsp recv_ref_update(struct imsg *imsg)
614 13b2bc37 2022-10-23 stsp {
615 9a8e357c 2023-01-28 op static const char zero_id[SHA1_DIGEST_LENGTH];
616 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
617 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
618 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update iref;
619 13b2bc37 2022-10-23 stsp size_t datalen;
620 13b2bc37 2022-10-23 stsp char *refname = NULL;
621 13b2bc37 2022-10-23 stsp struct got_reference *ref = NULL;
622 13b2bc37 2022-10-23 stsp struct got_object_id *id = NULL;
623 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
624 13b2bc37 2022-10-23 stsp struct gotd_ref_update *ref_update = NULL;
625 13b2bc37 2022-10-23 stsp
626 13b2bc37 2022-10-23 stsp log_debug("ref-update received");
627 13b2bc37 2022-10-23 stsp
628 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
629 13b2bc37 2022-10-23 stsp if (datalen < sizeof(iref))
630 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
631 13b2bc37 2022-10-23 stsp memcpy(&iref, imsg->data, sizeof(iref));
632 13b2bc37 2022-10-23 stsp if (datalen != sizeof(iref) + iref.name_len)
633 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
634 13b2bc37 2022-10-23 stsp
635 1a52c9bf 2022-12-30 stsp imsg_init(&ibuf, client->fd);
636 13b2bc37 2022-10-23 stsp
637 00b3e9ae 2023-01-11 op refname = strndup(imsg->data + sizeof(iref), iref.name_len);
638 13b2bc37 2022-10-23 stsp if (refname == NULL)
639 00b3e9ae 2023-01-11 op return got_error_from_errno("strndup");
640 13b2bc37 2022-10-23 stsp
641 13b2bc37 2022-10-23 stsp ref_update = calloc(1, sizeof(*ref_update));
642 13b2bc37 2022-10-23 stsp if (ref_update == NULL) {
643 13b2bc37 2022-10-23 stsp err = got_error_from_errno("malloc");
644 13b2bc37 2022-10-23 stsp goto done;
645 13b2bc37 2022-10-23 stsp }
646 13b2bc37 2022-10-23 stsp
647 13b2bc37 2022-10-23 stsp memcpy(ref_update->old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
648 13b2bc37 2022-10-23 stsp memcpy(ref_update->new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
649 13b2bc37 2022-10-23 stsp
650 13b2bc37 2022-10-23 stsp err = got_ref_open(&ref, repo_write.repo, refname, 0);
651 13b2bc37 2022-10-23 stsp if (err) {
652 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_NOT_REF)
653 13b2bc37 2022-10-23 stsp goto done;
654 9afa3de2 2023-04-04 stsp if (memcmp(ref_update->new_id.sha1,
655 9afa3de2 2023-04-04 stsp zero_id, sizeof(zero_id)) == 0) {
656 9afa3de2 2023-04-04 stsp err = got_error_fmt(GOT_ERR_BAD_OBJ_ID,
657 9afa3de2 2023-04-04 stsp "%s", refname);
658 9afa3de2 2023-04-04 stsp goto done;
659 9afa3de2 2023-04-04 stsp }
660 13b2bc37 2022-10-23 stsp err = got_ref_alloc(&ref, refname, &ref_update->new_id);
661 13b2bc37 2022-10-23 stsp if (err)
662 13b2bc37 2022-10-23 stsp goto done;
663 13b2bc37 2022-10-23 stsp ref_update->ref_is_new = 1;
664 0ff2c315 2023-01-18 stsp client->nref_new++;
665 13b2bc37 2022-10-23 stsp }
666 13b2bc37 2022-10-23 stsp if (got_ref_is_symbolic(ref)) {
667 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
668 13b2bc37 2022-10-23 stsp "'%s' is a symbolic reference and cannot "
669 13b2bc37 2022-10-23 stsp "be updated", got_ref_get_name(ref));
670 13b2bc37 2022-10-23 stsp goto done;
671 13b2bc37 2022-10-23 stsp }
672 13b2bc37 2022-10-23 stsp if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
673 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
674 13b2bc37 2022-10-23 stsp "%s: does not begin with 'refs/'",
675 13b2bc37 2022-10-23 stsp got_ref_get_name(ref));
676 13b2bc37 2022-10-23 stsp goto done;
677 13b2bc37 2022-10-23 stsp }
678 13b2bc37 2022-10-23 stsp
679 9afa3de2 2023-04-04 stsp err = protect_ref_namespace(got_ref_get_name(ref), "refs/got/");
680 13b2bc37 2022-10-23 stsp if (err)
681 13b2bc37 2022-10-23 stsp goto done;
682 9afa3de2 2023-04-04 stsp err = protect_ref_namespace(got_ref_get_name(ref), "refs/remotes/");
683 13b2bc37 2022-10-23 stsp if (err)
684 13b2bc37 2022-10-23 stsp goto done;
685 13b2bc37 2022-10-23 stsp
686 13b2bc37 2022-10-23 stsp if (!ref_update->ref_is_new) {
687 13b2bc37 2022-10-23 stsp /*
688 13b2bc37 2022-10-23 stsp * Ensure the client's idea of this update is still valid.
689 13b2bc37 2022-10-23 stsp * At this point we can only return an error, to prevent
690 13b2bc37 2022-10-23 stsp * the client from uploading a pack file which will likely
691 13b2bc37 2022-10-23 stsp * have to be discarded.
692 13b2bc37 2022-10-23 stsp */
693 13b2bc37 2022-10-23 stsp err = got_ref_resolve(&id, repo_write.repo, ref);
694 13b2bc37 2022-10-23 stsp if (err)
695 13b2bc37 2022-10-23 stsp goto done;
696 13b2bc37 2022-10-23 stsp
697 13b2bc37 2022-10-23 stsp if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
698 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
699 13b2bc37 2022-10-23 stsp "%s has been modified by someone else "
700 13b2bc37 2022-10-23 stsp "while transaction was in progress",
701 13b2bc37 2022-10-23 stsp got_ref_get_name(ref));
702 13b2bc37 2022-10-23 stsp goto done;
703 13b2bc37 2022-10-23 stsp }
704 13b2bc37 2022-10-23 stsp }
705 13b2bc37 2022-10-23 stsp
706 13b2bc37 2022-10-23 stsp gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
707 13b2bc37 2022-10-23 stsp repo_write.pid);
708 13b2bc37 2022-10-23 stsp
709 13b2bc37 2022-10-23 stsp ref_update->ref = ref;
710 9a8e357c 2023-01-28 op if (memcmp(ref_update->new_id.sha1, zero_id, sizeof(zero_id)) == 0) {
711 9a8e357c 2023-01-28 op ref_update->delete_ref = 1;
712 9a8e357c 2023-01-28 op client->nref_del++;
713 9a8e357c 2023-01-28 op }
714 1a52c9bf 2022-12-30 stsp STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
715 1a52c9bf 2022-12-30 stsp client->nref_updates++;
716 13b2bc37 2022-10-23 stsp ref = NULL;
717 13b2bc37 2022-10-23 stsp ref_update = NULL;
718 13b2bc37 2022-10-23 stsp done:
719 13b2bc37 2022-10-23 stsp if (ref)
720 13b2bc37 2022-10-23 stsp got_ref_close(ref);
721 13b2bc37 2022-10-23 stsp free(ref_update);
722 13b2bc37 2022-10-23 stsp free(refname);
723 13b2bc37 2022-10-23 stsp free(id);
724 13b2bc37 2022-10-23 stsp return err;
725 13b2bc37 2022-10-23 stsp }
726 13b2bc37 2022-10-23 stsp
727 13b2bc37 2022-10-23 stsp static const struct got_error *
728 13b2bc37 2022-10-23 stsp pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
729 13b2bc37 2022-10-23 stsp uint32_t nobj_loose, uint32_t nobj_resolved)
730 13b2bc37 2022-10-23 stsp {
731 13b2bc37 2022-10-23 stsp int p_indexed = 0, p_resolved = 0;
732 13b2bc37 2022-10-23 stsp int nobj_delta = nobj_total - nobj_loose;
733 13b2bc37 2022-10-23 stsp
734 13b2bc37 2022-10-23 stsp if (nobj_total > 0)
735 13b2bc37 2022-10-23 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
736 13b2bc37 2022-10-23 stsp
737 13b2bc37 2022-10-23 stsp if (nobj_delta > 0)
738 13b2bc37 2022-10-23 stsp p_resolved = (nobj_resolved * 100) / nobj_delta;
739 13b2bc37 2022-10-23 stsp
740 13b2bc37 2022-10-23 stsp if (p_resolved > 0) {
741 13b2bc37 2022-10-23 stsp log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
742 13b2bc37 2022-10-23 stsp nobj_total, p_indexed, nobj_delta, p_resolved);
743 13b2bc37 2022-10-23 stsp } else
744 13b2bc37 2022-10-23 stsp log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
745 13b2bc37 2022-10-23 stsp
746 13b2bc37 2022-10-23 stsp return NULL;
747 13b2bc37 2022-10-23 stsp }
748 13b2bc37 2022-10-23 stsp
749 13b2bc37 2022-10-23 stsp static const struct got_error *
750 13b2bc37 2022-10-23 stsp read_more_pack_stream(int infd, BUF *buf, size_t minsize)
751 13b2bc37 2022-10-23 stsp {
752 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
753 13b2bc37 2022-10-23 stsp uint8_t readahead[65536];
754 13b2bc37 2022-10-23 stsp size_t have, newlen;
755 13b2bc37 2022-10-23 stsp
756 13b2bc37 2022-10-23 stsp err = got_poll_read_full(infd, &have,
757 13b2bc37 2022-10-23 stsp readahead, sizeof(readahead), minsize);
758 13b2bc37 2022-10-23 stsp if (err)
759 13b2bc37 2022-10-23 stsp return err;
760 13b2bc37 2022-10-23 stsp
761 13b2bc37 2022-10-23 stsp err = buf_append(&newlen, buf, readahead, have);
762 13b2bc37 2022-10-23 stsp if (err)
763 13b2bc37 2022-10-23 stsp return err;
764 e26970cc 2023-01-10 op return NULL;
765 13b2bc37 2022-10-23 stsp }
766 13b2bc37 2022-10-23 stsp
767 13b2bc37 2022-10-23 stsp static const struct got_error *
768 13b2bc37 2022-10-23 stsp copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
769 ae25a666 2023-02-23 op off_t *outsize, BUF *buf, size_t *buf_pos, struct got_hash *ctx)
770 13b2bc37 2022-10-23 stsp {
771 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
772 13b2bc37 2022-10-23 stsp uint8_t t = 0;
773 13b2bc37 2022-10-23 stsp uint64_t s = 0;
774 13b2bc37 2022-10-23 stsp uint8_t sizebuf[8];
775 13b2bc37 2022-10-23 stsp size_t i = 0;
776 13b2bc37 2022-10-23 stsp off_t obj_offset = *outsize;
777 13b2bc37 2022-10-23 stsp
778 13b2bc37 2022-10-23 stsp do {
779 13b2bc37 2022-10-23 stsp /* We do not support size values which don't fit in 64 bit. */
780 13b2bc37 2022-10-23 stsp if (i > 9)
781 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
782 22ec3416 2022-10-25 op "packfile offset %lld", (long long)obj_offset);
783 13b2bc37 2022-10-23 stsp
784 13b2bc37 2022-10-23 stsp if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
785 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
786 13b2bc37 2022-10-23 stsp sizeof(sizebuf[0]));
787 13b2bc37 2022-10-23 stsp if (err)
788 13b2bc37 2022-10-23 stsp return err;
789 13b2bc37 2022-10-23 stsp }
790 13b2bc37 2022-10-23 stsp
791 13b2bc37 2022-10-23 stsp sizebuf[i] = buf_getc(buf, *buf_pos);
792 13b2bc37 2022-10-23 stsp *buf_pos += sizeof(sizebuf[i]);
793 13b2bc37 2022-10-23 stsp
794 13b2bc37 2022-10-23 stsp if (i == 0) {
795 13b2bc37 2022-10-23 stsp t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
796 13b2bc37 2022-10-23 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
797 13b2bc37 2022-10-23 stsp s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
798 13b2bc37 2022-10-23 stsp } else {
799 13b2bc37 2022-10-23 stsp size_t shift = 4 + 7 * (i - 1);
800 13b2bc37 2022-10-23 stsp s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
801 13b2bc37 2022-10-23 stsp shift);
802 13b2bc37 2022-10-23 stsp }
803 13b2bc37 2022-10-23 stsp i++;
804 13b2bc37 2022-10-23 stsp } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
805 13b2bc37 2022-10-23 stsp
806 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, sizebuf, i, ctx);
807 13b2bc37 2022-10-23 stsp if (err)
808 13b2bc37 2022-10-23 stsp return err;
809 13b2bc37 2022-10-23 stsp *outsize += i;
810 13b2bc37 2022-10-23 stsp
811 13b2bc37 2022-10-23 stsp *type = t;
812 13b2bc37 2022-10-23 stsp *size = s;
813 13b2bc37 2022-10-23 stsp return NULL;
814 13b2bc37 2022-10-23 stsp }
815 13b2bc37 2022-10-23 stsp
816 13b2bc37 2022-10-23 stsp static const struct got_error *
817 13b2bc37 2022-10-23 stsp copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
818 ae25a666 2023-02-23 op struct got_hash *ctx)
819 13b2bc37 2022-10-23 stsp {
820 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
821 13b2bc37 2022-10-23 stsp size_t remain = buf_len(buf) - *buf_pos;
822 13b2bc37 2022-10-23 stsp
823 13b2bc37 2022-10-23 stsp if (remain < SHA1_DIGEST_LENGTH) {
824 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
825 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH - remain);
826 13b2bc37 2022-10-23 stsp if (err)
827 13b2bc37 2022-10-23 stsp return err;
828 13b2bc37 2022-10-23 stsp }
829 13b2bc37 2022-10-23 stsp
830 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
831 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH, ctx);
832 13b2bc37 2022-10-23 stsp if (err)
833 13b2bc37 2022-10-23 stsp return err;
834 13b2bc37 2022-10-23 stsp
835 13b2bc37 2022-10-23 stsp *buf_pos += SHA1_DIGEST_LENGTH;
836 13b2bc37 2022-10-23 stsp return NULL;
837 13b2bc37 2022-10-23 stsp }
838 13b2bc37 2022-10-23 stsp
839 13b2bc37 2022-10-23 stsp static const struct got_error *
840 13b2bc37 2022-10-23 stsp copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
841 ae25a666 2023-02-23 op struct got_hash *ctx)
842 13b2bc37 2022-10-23 stsp {
843 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
844 13b2bc37 2022-10-23 stsp uint64_t o = 0;
845 13b2bc37 2022-10-23 stsp uint8_t offbuf[8];
846 13b2bc37 2022-10-23 stsp size_t i = 0;
847 13b2bc37 2022-10-23 stsp off_t obj_offset = *outsize;
848 13b2bc37 2022-10-23 stsp
849 13b2bc37 2022-10-23 stsp do {
850 13b2bc37 2022-10-23 stsp /* We do not support offset values which don't fit in 64 bit. */
851 13b2bc37 2022-10-23 stsp if (i > 8)
852 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
853 22ec3416 2022-10-25 op "packfile offset %lld", (long long)obj_offset);
854 13b2bc37 2022-10-23 stsp
855 13b2bc37 2022-10-23 stsp if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
856 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
857 13b2bc37 2022-10-23 stsp sizeof(offbuf[0]));
858 13b2bc37 2022-10-23 stsp if (err)
859 13b2bc37 2022-10-23 stsp return err;
860 13b2bc37 2022-10-23 stsp }
861 13b2bc37 2022-10-23 stsp
862 13b2bc37 2022-10-23 stsp offbuf[i] = buf_getc(buf, *buf_pos);
863 13b2bc37 2022-10-23 stsp *buf_pos += sizeof(offbuf[i]);
864 13b2bc37 2022-10-23 stsp
865 13b2bc37 2022-10-23 stsp if (i == 0)
866 13b2bc37 2022-10-23 stsp o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
867 13b2bc37 2022-10-23 stsp else {
868 13b2bc37 2022-10-23 stsp o++;
869 13b2bc37 2022-10-23 stsp o <<= 7;
870 13b2bc37 2022-10-23 stsp o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
871 13b2bc37 2022-10-23 stsp }
872 13b2bc37 2022-10-23 stsp i++;
873 13b2bc37 2022-10-23 stsp } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
874 13b2bc37 2022-10-23 stsp
875 13b2bc37 2022-10-23 stsp if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
876 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PACK_OFFSET);
877 13b2bc37 2022-10-23 stsp
878 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, offbuf, i, ctx);
879 13b2bc37 2022-10-23 stsp if (err)
880 13b2bc37 2022-10-23 stsp return err;
881 13b2bc37 2022-10-23 stsp
882 13b2bc37 2022-10-23 stsp *outsize += i;
883 13b2bc37 2022-10-23 stsp return NULL;
884 13b2bc37 2022-10-23 stsp }
885 13b2bc37 2022-10-23 stsp
886 13b2bc37 2022-10-23 stsp static const struct got_error *
887 13b2bc37 2022-10-23 stsp copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
888 ae25a666 2023-02-23 op struct got_hash *ctx)
889 13b2bc37 2022-10-23 stsp {
890 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
891 13b2bc37 2022-10-23 stsp z_stream z;
892 13b2bc37 2022-10-23 stsp int zret;
893 13b2bc37 2022-10-23 stsp char voidbuf[1024];
894 13b2bc37 2022-10-23 stsp size_t consumed_total = 0;
895 13b2bc37 2022-10-23 stsp off_t zstream_offset = *outsize;
896 13b2bc37 2022-10-23 stsp
897 13b2bc37 2022-10-23 stsp memset(&z, 0, sizeof(z));
898 13b2bc37 2022-10-23 stsp
899 13b2bc37 2022-10-23 stsp z.zalloc = Z_NULL;
900 13b2bc37 2022-10-23 stsp z.zfree = Z_NULL;
901 13b2bc37 2022-10-23 stsp zret = inflateInit(&z);
902 13b2bc37 2022-10-23 stsp if (zret != Z_OK) {
903 13b2bc37 2022-10-23 stsp if (zret == Z_ERRNO)
904 13b2bc37 2022-10-23 stsp return got_error_from_errno("inflateInit");
905 13b2bc37 2022-10-23 stsp if (zret == Z_MEM_ERROR) {
906 13b2bc37 2022-10-23 stsp errno = ENOMEM;
907 13b2bc37 2022-10-23 stsp return got_error_from_errno("inflateInit");
908 13b2bc37 2022-10-23 stsp }
909 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_DECOMPRESSION,
910 13b2bc37 2022-10-23 stsp "inflateInit failed");
911 13b2bc37 2022-10-23 stsp }
912 13b2bc37 2022-10-23 stsp
913 13b2bc37 2022-10-23 stsp while (zret != Z_STREAM_END) {
914 13b2bc37 2022-10-23 stsp size_t last_total_in, consumed;
915 13b2bc37 2022-10-23 stsp
916 13b2bc37 2022-10-23 stsp /*
917 13b2bc37 2022-10-23 stsp * Decompress into the void. Object data will be parsed
918 13b2bc37 2022-10-23 stsp * later, when the pack file is indexed. For now, we just
919 13b2bc37 2022-10-23 stsp * want to locate the end of the compressed stream.
920 13b2bc37 2022-10-23 stsp */
921 13b2bc37 2022-10-23 stsp while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
922 13b2bc37 2022-10-23 stsp last_total_in = z.total_in;
923 13b2bc37 2022-10-23 stsp z.next_in = buf_get(buf) + *buf_pos;
924 13b2bc37 2022-10-23 stsp z.avail_in = buf_len(buf) - *buf_pos;
925 13b2bc37 2022-10-23 stsp z.next_out = voidbuf;
926 13b2bc37 2022-10-23 stsp z.avail_out = sizeof(voidbuf);
927 13b2bc37 2022-10-23 stsp
928 13b2bc37 2022-10-23 stsp zret = inflate(&z, Z_SYNC_FLUSH);
929 13b2bc37 2022-10-23 stsp if (zret != Z_OK && zret != Z_BUF_ERROR &&
930 13b2bc37 2022-10-23 stsp zret != Z_STREAM_END) {
931 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_DECOMPRESSION,
932 22ec3416 2022-10-25 op "packfile offset %lld",
933 22ec3416 2022-10-25 op (long long)zstream_offset);
934 13b2bc37 2022-10-23 stsp goto done;
935 13b2bc37 2022-10-23 stsp }
936 13b2bc37 2022-10-23 stsp consumed = z.total_in - last_total_in;
937 13b2bc37 2022-10-23 stsp
938 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
939 13b2bc37 2022-10-23 stsp consumed, ctx);
940 13b2bc37 2022-10-23 stsp if (err)
941 13b2bc37 2022-10-23 stsp goto done;
942 13b2bc37 2022-10-23 stsp
943 13b2bc37 2022-10-23 stsp err = buf_discard(buf, *buf_pos + consumed);
944 13b2bc37 2022-10-23 stsp if (err)
945 13b2bc37 2022-10-23 stsp goto done;
946 13b2bc37 2022-10-23 stsp *buf_pos = 0;
947 13b2bc37 2022-10-23 stsp
948 13b2bc37 2022-10-23 stsp consumed_total += consumed;
949 13b2bc37 2022-10-23 stsp }
950 13b2bc37 2022-10-23 stsp
951 13b2bc37 2022-10-23 stsp if (zret != Z_STREAM_END) {
952 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf, 1);
953 13b2bc37 2022-10-23 stsp if (err)
954 13b2bc37 2022-10-23 stsp goto done;
955 13b2bc37 2022-10-23 stsp }
956 13b2bc37 2022-10-23 stsp }
957 13b2bc37 2022-10-23 stsp
958 13b2bc37 2022-10-23 stsp if (err == NULL)
959 13b2bc37 2022-10-23 stsp *outsize += consumed_total;
960 13b2bc37 2022-10-23 stsp done:
961 13b2bc37 2022-10-23 stsp inflateEnd(&z);
962 13b2bc37 2022-10-23 stsp return err;
963 13b2bc37 2022-10-23 stsp }
964 13b2bc37 2022-10-23 stsp
965 13b2bc37 2022-10-23 stsp static const struct got_error *
966 13b2bc37 2022-10-23 stsp validate_object_type(int obj_type)
967 13b2bc37 2022-10-23 stsp {
968 13b2bc37 2022-10-23 stsp switch (obj_type) {
969 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_BLOB:
970 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_COMMIT:
971 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_TREE:
972 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_TAG:
973 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
974 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
975 13b2bc37 2022-10-23 stsp return NULL;
976 13b2bc37 2022-10-23 stsp default:
977 13b2bc37 2022-10-23 stsp break;
978 13b2bc37 2022-10-23 stsp }
979 13b2bc37 2022-10-23 stsp
980 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_OBJ_TYPE);
981 13b2bc37 2022-10-23 stsp }
982 13b2bc37 2022-10-23 stsp
983 13b2bc37 2022-10-23 stsp static const struct got_error *
984 cc88020e 2023-04-11 stsp ensure_all_objects_exist_locally(struct gotd_ref_updates *ref_updates)
985 cc88020e 2023-04-11 stsp {
986 cc88020e 2023-04-11 stsp const struct got_error *err = NULL;
987 cc88020e 2023-04-11 stsp struct gotd_ref_update *ref_update;
988 cc88020e 2023-04-11 stsp struct got_object *obj;
989 cc88020e 2023-04-11 stsp
990 cc88020e 2023-04-11 stsp STAILQ_FOREACH(ref_update, ref_updates, entry) {
991 cc88020e 2023-04-11 stsp err = got_object_open(&obj, repo_write.repo,
992 cc88020e 2023-04-11 stsp &ref_update->new_id);
993 cc88020e 2023-04-11 stsp if (err)
994 cc88020e 2023-04-11 stsp return err;
995 cc88020e 2023-04-11 stsp got_object_close(obj);
996 cc88020e 2023-04-11 stsp }
997 cc88020e 2023-04-11 stsp
998 cc88020e 2023-04-11 stsp return NULL;
999 cc88020e 2023-04-11 stsp }
1000 cc88020e 2023-04-11 stsp
1001 cc88020e 2023-04-11 stsp static const struct got_error *
1002 0ff2c315 2023-01-18 stsp recv_packdata(off_t *outsize, uint32_t *nobj, uint8_t *sha1,
1003 0ff2c315 2023-01-18 stsp int infd, int outfd)
1004 13b2bc37 2022-10-23 stsp {
1005 13b2bc37 2022-10-23 stsp const struct got_error *err;
1006 0ff2c315 2023-01-18 stsp struct repo_write_client *client = &repo_write_client;
1007 13b2bc37 2022-10-23 stsp struct got_packfile_hdr hdr;
1008 13b2bc37 2022-10-23 stsp size_t have;
1009 0ff2c315 2023-01-18 stsp uint32_t nhave = 0;
1010 ae25a666 2023-02-23 op struct got_hash ctx;
1011 13b2bc37 2022-10-23 stsp uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
1012 13b2bc37 2022-10-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
1013 13b2bc37 2022-10-23 stsp BUF *buf = NULL;
1014 13b2bc37 2022-10-23 stsp size_t buf_pos = 0, remain;
1015 13b2bc37 2022-10-23 stsp ssize_t w;
1016 13b2bc37 2022-10-23 stsp
1017 13b2bc37 2022-10-23 stsp *outsize = 0;
1018 0ff2c315 2023-01-18 stsp *nobj = 0;
1019 9a8e357c 2023-01-28 op
1020 9a8e357c 2023-01-28 op /* if only deleting references there's nothing to read */
1021 9a8e357c 2023-01-28 op if (client->nref_updates == client->nref_del)
1022 9a8e357c 2023-01-28 op return NULL;
1023 9a8e357c 2023-01-28 op
1024 ae25a666 2023-02-23 op got_hash_init(&ctx, GOT_HASH_SHA1);
1025 13b2bc37 2022-10-23 stsp
1026 13b2bc37 2022-10-23 stsp err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
1027 13b2bc37 2022-10-23 stsp if (err)
1028 13b2bc37 2022-10-23 stsp return err;
1029 13b2bc37 2022-10-23 stsp if (have != sizeof(hdr))
1030 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
1031 13b2bc37 2022-10-23 stsp *outsize += have;
1032 13b2bc37 2022-10-23 stsp
1033 13b2bc37 2022-10-23 stsp if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
1034 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
1035 13b2bc37 2022-10-23 stsp "bad packfile signature");
1036 13b2bc37 2022-10-23 stsp if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
1037 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
1038 13b2bc37 2022-10-23 stsp "bad packfile version");
1039 13b2bc37 2022-10-23 stsp
1040 0ff2c315 2023-01-18 stsp *nobj = be32toh(hdr.nobjects);
1041 0ff2c315 2023-01-18 stsp if (*nobj == 0) {
1042 0ff2c315 2023-01-18 stsp /*
1043 0ff2c315 2023-01-18 stsp * Clients which are creating new references only
1044 0ff2c315 2023-01-18 stsp * will send us an empty pack file.
1045 0ff2c315 2023-01-18 stsp */
1046 0ff2c315 2023-01-18 stsp if (client->nref_updates > 0 &&
1047 0ff2c315 2023-01-18 stsp client->nref_updates == client->nref_new)
1048 0ff2c315 2023-01-18 stsp return NULL;
1049 0ff2c315 2023-01-18 stsp
1050 cc88020e 2023-04-11 stsp /*
1051 cc88020e 2023-04-11 stsp * Clients which only move existing refs will send us an empty
1052 cc88020e 2023-04-11 stsp * pack file. All referenced objects must exist locally.
1053 cc88020e 2023-04-11 stsp */
1054 cc88020e 2023-04-11 stsp err = ensure_all_objects_exist_locally(&client->ref_updates);
1055 cc88020e 2023-04-11 stsp if (err) {
1056 cc88020e 2023-04-11 stsp if (err->code != GOT_ERR_NO_OBJ)
1057 cc88020e 2023-04-11 stsp return err;
1058 cc88020e 2023-04-11 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
1059 cc88020e 2023-04-11 stsp "bad packfile with zero objects");
1060 cc88020e 2023-04-11 stsp }
1061 cc88020e 2023-04-11 stsp
1062 cc88020e 2023-04-11 stsp client->nref_move = client->nref_updates;
1063 cc88020e 2023-04-11 stsp return NULL;
1064 0ff2c315 2023-01-18 stsp }
1065 13b2bc37 2022-10-23 stsp
1066 0ff2c315 2023-01-18 stsp log_debug("expecting %d objects", *nobj);
1067 13b2bc37 2022-10-23 stsp
1068 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
1069 13b2bc37 2022-10-23 stsp if (err)
1070 13b2bc37 2022-10-23 stsp return err;
1071 13b2bc37 2022-10-23 stsp
1072 13b2bc37 2022-10-23 stsp err = buf_alloc(&buf, 65536);
1073 13b2bc37 2022-10-23 stsp if (err)
1074 13b2bc37 2022-10-23 stsp return err;
1075 13b2bc37 2022-10-23 stsp
1076 0ff2c315 2023-01-18 stsp while (nhave != *nobj) {
1077 13b2bc37 2022-10-23 stsp uint8_t obj_type;
1078 13b2bc37 2022-10-23 stsp uint64_t obj_size;
1079 13b2bc37 2022-10-23 stsp
1080 13b2bc37 2022-10-23 stsp err = copy_object_type_and_size(&obj_type, &obj_size,
1081 13b2bc37 2022-10-23 stsp infd, outfd, outsize, buf, &buf_pos, &ctx);
1082 13b2bc37 2022-10-23 stsp if (err)
1083 13b2bc37 2022-10-23 stsp goto done;
1084 13b2bc37 2022-10-23 stsp
1085 13b2bc37 2022-10-23 stsp err = validate_object_type(obj_type);
1086 13b2bc37 2022-10-23 stsp if (err)
1087 13b2bc37 2022-10-23 stsp goto done;
1088 13b2bc37 2022-10-23 stsp
1089 13b2bc37 2022-10-23 stsp if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
1090 13b2bc37 2022-10-23 stsp err = copy_ref_delta(infd, outfd, outsize,
1091 13b2bc37 2022-10-23 stsp buf, &buf_pos, &ctx);
1092 13b2bc37 2022-10-23 stsp if (err)
1093 13b2bc37 2022-10-23 stsp goto done;
1094 13b2bc37 2022-10-23 stsp } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
1095 13b2bc37 2022-10-23 stsp err = copy_offset_delta(infd, outfd, outsize,
1096 13b2bc37 2022-10-23 stsp buf, &buf_pos, &ctx);
1097 13b2bc37 2022-10-23 stsp if (err)
1098 13b2bc37 2022-10-23 stsp goto done;
1099 13b2bc37 2022-10-23 stsp }
1100 13b2bc37 2022-10-23 stsp
1101 13b2bc37 2022-10-23 stsp err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
1102 13b2bc37 2022-10-23 stsp if (err)
1103 13b2bc37 2022-10-23 stsp goto done;
1104 13b2bc37 2022-10-23 stsp
1105 13b2bc37 2022-10-23 stsp nhave++;
1106 13b2bc37 2022-10-23 stsp }
1107 13b2bc37 2022-10-23 stsp
1108 0ff2c315 2023-01-18 stsp log_debug("received %u objects", *nobj);
1109 13b2bc37 2022-10-23 stsp
1110 ae25a666 2023-02-23 op got_hash_final(&ctx, expected_sha1);
1111 13b2bc37 2022-10-23 stsp
1112 13b2bc37 2022-10-23 stsp remain = buf_len(buf) - buf_pos;
1113 13b2bc37 2022-10-23 stsp if (remain < SHA1_DIGEST_LENGTH) {
1114 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
1115 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH - remain);
1116 13b2bc37 2022-10-23 stsp if (err)
1117 13b2bc37 2022-10-23 stsp return err;
1118 13b2bc37 2022-10-23 stsp }
1119 13b2bc37 2022-10-23 stsp
1120 13b2bc37 2022-10-23 stsp got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
1121 13b2bc37 2022-10-23 stsp log_debug("expect SHA1: %s", hex);
1122 13b2bc37 2022-10-23 stsp got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
1123 13b2bc37 2022-10-23 stsp log_debug("actual SHA1: %s", hex);
1124 13b2bc37 2022-10-23 stsp
1125 13b2bc37 2022-10-23 stsp if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
1126 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH) != 0) {
1127 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PACKFILE_CSUM);
1128 13b2bc37 2022-10-23 stsp goto done;
1129 13b2bc37 2022-10-23 stsp }
1130 13b2bc37 2022-10-23 stsp
1131 13b2bc37 2022-10-23 stsp memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
1132 13b2bc37 2022-10-23 stsp
1133 13b2bc37 2022-10-23 stsp w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
1134 13b2bc37 2022-10-23 stsp if (w == -1) {
1135 13b2bc37 2022-10-23 stsp err = got_error_from_errno("write");
1136 13b2bc37 2022-10-23 stsp goto done;
1137 13b2bc37 2022-10-23 stsp }
1138 13b2bc37 2022-10-23 stsp if (w != SHA1_DIGEST_LENGTH) {
1139 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_IO);
1140 13b2bc37 2022-10-23 stsp goto done;
1141 13b2bc37 2022-10-23 stsp }
1142 13b2bc37 2022-10-23 stsp
1143 13b2bc37 2022-10-23 stsp *outsize += SHA1_DIGEST_LENGTH;
1144 13b2bc37 2022-10-23 stsp
1145 13b2bc37 2022-10-23 stsp if (fsync(outfd) == -1) {
1146 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fsync");
1147 13b2bc37 2022-10-23 stsp goto done;
1148 13b2bc37 2022-10-23 stsp }
1149 13b2bc37 2022-10-23 stsp if (lseek(outfd, 0L, SEEK_SET) == -1) {
1150 13b2bc37 2022-10-23 stsp err = got_error_from_errno("lseek");
1151 13b2bc37 2022-10-23 stsp goto done;
1152 13b2bc37 2022-10-23 stsp }
1153 13b2bc37 2022-10-23 stsp done:
1154 13b2bc37 2022-10-23 stsp buf_free(buf);
1155 13b2bc37 2022-10-23 stsp return err;
1156 13b2bc37 2022-10-23 stsp }
1157 13b2bc37 2022-10-23 stsp
1158 13b2bc37 2022-10-23 stsp static const struct got_error *
1159 1a52c9bf 2022-12-30 stsp report_pack_status(const struct got_error *unpack_err)
1160 13b2bc37 2022-10-23 stsp {
1161 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1162 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1163 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_status istatus;
1164 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
1165 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
1166 13b2bc37 2022-10-23 stsp const char *unpack_ok = "unpack ok\n";
1167 13b2bc37 2022-10-23 stsp size_t len;
1168 e26970cc 2023-01-10 op
1169 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, client->fd);
1170 13b2bc37 2022-10-23 stsp
1171 13b2bc37 2022-10-23 stsp if (unpack_err)
1172 13b2bc37 2022-10-23 stsp istatus.reason_len = strlen(unpack_err->msg);
1173 13b2bc37 2022-10-23 stsp else
1174 13b2bc37 2022-10-23 stsp istatus.reason_len = strlen(unpack_ok);
1175 13b2bc37 2022-10-23 stsp
1176 13b2bc37 2022-10-23 stsp len = sizeof(istatus) + istatus.reason_len;
1177 13b2bc37 2022-10-23 stsp wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
1178 13b2bc37 2022-10-23 stsp repo_write.pid, len);
1179 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
1180 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create PACKFILE_STATUS");
1181 13b2bc37 2022-10-23 stsp goto done;
1182 13b2bc37 2022-10-23 stsp }
1183 13b2bc37 2022-10-23 stsp
1184 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
1185 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add PACKFILE_STATUS");
1186 13b2bc37 2022-10-23 stsp goto done;
1187 13b2bc37 2022-10-23 stsp }
1188 13b2bc37 2022-10-23 stsp
1189 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, err ? err->msg : unpack_ok,
1190 13b2bc37 2022-10-23 stsp istatus.reason_len) == -1) {
1191 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add PACKFILE_STATUS");
1192 13b2bc37 2022-10-23 stsp goto done;
1193 13b2bc37 2022-10-23 stsp }
1194 13b2bc37 2022-10-23 stsp
1195 13b2bc37 2022-10-23 stsp imsg_close(&ibuf, wbuf);
1196 13b2bc37 2022-10-23 stsp
1197 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(&ibuf);
1198 13b2bc37 2022-10-23 stsp done:
1199 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
1200 13b2bc37 2022-10-23 stsp return err;
1201 13b2bc37 2022-10-23 stsp }
1202 13b2bc37 2022-10-23 stsp
1203 13b2bc37 2022-10-23 stsp static const struct got_error *
1204 0ff2c315 2023-01-18 stsp recv_packfile(int *have_packfile, struct imsg *imsg)
1205 13b2bc37 2022-10-23 stsp {
1206 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL, *unpack_err;
1207 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1208 13b2bc37 2022-10-23 stsp struct gotd_imsg_recv_packfile ireq;
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 ad4cc361 2022-10-27 op log_debug("begin indexing pack (%lld bytes in size)",
1327 ad4cc361 2022-10-27 op (long long)pack->filesize);
1328 1a52c9bf 2022-12-30 stsp err = got_pack_index(pack, client->packidx_fd,
1329 1a52c9bf 2022-12-30 stsp tempfiles[0], tempfiles[1], tempfiles[2], client->pack_sha1,
1330 13b2bc37 2022-10-23 stsp pack_index_progress, NULL, &rl);
1331 13b2bc37 2022-10-23 stsp if (err)
1332 13b2bc37 2022-10-23 stsp goto done;
1333 13b2bc37 2022-10-23 stsp log_debug("done indexing pack");
1334 13b2bc37 2022-10-23 stsp
1335 1a52c9bf 2022-12-30 stsp if (fsync(client->packidx_fd) == -1) {
1336 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fsync");
1337 13b2bc37 2022-10-23 stsp goto done;
1338 13b2bc37 2022-10-23 stsp }
1339 1a52c9bf 2022-12-30 stsp if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1340 13b2bc37 2022-10-23 stsp err = got_error_from_errno("lseek");
1341 13b2bc37 2022-10-23 stsp done:
1342 1a52c9bf 2022-12-30 stsp if (close(client->pack_pipe) == -1 && err == NULL)
1343 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
1344 1a52c9bf 2022-12-30 stsp client->pack_pipe = -1;
1345 13b2bc37 2022-10-23 stsp for (i = 0; i < nitems(repo_tempfiles); i++) {
1346 13b2bc37 2022-10-23 stsp struct repo_tempfile *t = &repo_tempfiles[i];
1347 13b2bc37 2022-10-23 stsp if (t->idx != -1)
1348 13b2bc37 2022-10-23 stsp got_repo_temp_fds_put(t->idx, repo_write.repo);
1349 13b2bc37 2022-10-23 stsp }
1350 13b2bc37 2022-10-23 stsp for (i = 0; i < nitems(tempfiles); i++) {
1351 13b2bc37 2022-10-23 stsp if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1352 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fclose");
1353 13b2bc37 2022-10-23 stsp }
1354 13b2bc37 2022-10-23 stsp if (err)
1355 13b2bc37 2022-10-23 stsp got_pack_close(pack);
1356 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
1357 13b2bc37 2022-10-23 stsp return err;
1358 13b2bc37 2022-10-23 stsp }
1359 13b2bc37 2022-10-23 stsp
1360 13b2bc37 2022-10-23 stsp static const struct got_error *
1361 1a52c9bf 2022-12-30 stsp verify_packfile(void)
1362 13b2bc37 2022-10-23 stsp {
1363 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL, *close_err;
1364 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1365 13b2bc37 2022-10-23 stsp struct gotd_ref_update *ref_update;
1366 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
1367 13b2bc37 2022-10-23 stsp struct stat sb;
1368 13b2bc37 2022-10-23 stsp char *id_str = NULL;
1369 9afa3de2 2023-04-04 stsp struct got_object *obj = NULL;
1370 9afa3de2 2023-04-04 stsp struct got_pathlist_entry *pe;
1371 9afa3de2 2023-04-04 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
1372 13b2bc37 2022-10-23 stsp
1373 13b2bc37 2022-10-23 stsp if (STAILQ_EMPTY(&client->ref_updates)) {
1374 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1375 13b2bc37 2022-10-23 stsp "cannot verify pack file without any ref-updates");
1376 13b2bc37 2022-10-23 stsp }
1377 13b2bc37 2022-10-23 stsp
1378 13b2bc37 2022-10-23 stsp if (client->pack.fd == -1) {
1379 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1380 13b2bc37 2022-10-23 stsp "invalid pack file handle during pack verification");
1381 13b2bc37 2022-10-23 stsp }
1382 13b2bc37 2022-10-23 stsp if (client->packidx_fd == -1) {
1383 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1384 13b2bc37 2022-10-23 stsp "invalid pack index handle during pack verification");
1385 13b2bc37 2022-10-23 stsp }
1386 13b2bc37 2022-10-23 stsp
1387 13b2bc37 2022-10-23 stsp if (fstat(client->packidx_fd, &sb) == -1)
1388 13b2bc37 2022-10-23 stsp return got_error_from_errno("pack index fstat");
1389 13b2bc37 2022-10-23 stsp
1390 13b2bc37 2022-10-23 stsp packidx = malloc(sizeof(*packidx));
1391 13b2bc37 2022-10-23 stsp memset(packidx, 0, sizeof(*packidx));
1392 13b2bc37 2022-10-23 stsp packidx->fd = client->packidx_fd;
1393 13b2bc37 2022-10-23 stsp client->packidx_fd = -1;
1394 13b2bc37 2022-10-23 stsp packidx->len = sb.st_size;
1395 e26970cc 2023-01-10 op
1396 13b2bc37 2022-10-23 stsp err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1397 13b2bc37 2022-10-23 stsp if (err)
1398 13b2bc37 2022-10-23 stsp return err;
1399 13b2bc37 2022-10-23 stsp
1400 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1401 9a8e357c 2023-01-28 op if (ref_update->delete_ref)
1402 9a8e357c 2023-01-28 op continue;
1403 9a8e357c 2023-01-28 op
1404 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(pe, repo_write.protected_tag_namespaces, entry) {
1405 9afa3de2 2023-04-04 stsp err = protect_tag_namespace(pe->path, &client->pack,
1406 9afa3de2 2023-04-04 stsp packidx, ref_update);
1407 9afa3de2 2023-04-04 stsp if (err)
1408 9afa3de2 2023-04-04 stsp goto done;
1409 9afa3de2 2023-04-04 stsp }
1410 13b2bc37 2022-10-23 stsp
1411 9afa3de2 2023-04-04 stsp /*
1412 9afa3de2 2023-04-04 stsp * Objects which already exist in our repository need
1413 9afa3de2 2023-04-04 stsp * not be present in the pack file.
1414 9afa3de2 2023-04-04 stsp */
1415 9afa3de2 2023-04-04 stsp err = got_object_open(&obj, repo_write.repo,
1416 9afa3de2 2023-04-04 stsp &ref_update->new_id);
1417 9afa3de2 2023-04-04 stsp if (err && err->code != GOT_ERR_NO_OBJ)
1418 13b2bc37 2022-10-23 stsp goto done;
1419 9afa3de2 2023-04-04 stsp err = NULL;
1420 9afa3de2 2023-04-04 stsp if (obj) {
1421 9afa3de2 2023-04-04 stsp got_object_close(obj);
1422 9afa3de2 2023-04-04 stsp obj = NULL;
1423 9afa3de2 2023-04-04 stsp } else {
1424 9afa3de2 2023-04-04 stsp int idx = got_packidx_get_object_idx(packidx,
1425 9afa3de2 2023-04-04 stsp &ref_update->new_id);
1426 9afa3de2 2023-04-04 stsp if (idx == -1) {
1427 9afa3de2 2023-04-04 stsp got_sha1_digest_to_str(ref_update->new_id.sha1,
1428 9afa3de2 2023-04-04 stsp hex, sizeof(hex));
1429 9afa3de2 2023-04-04 stsp err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1430 9afa3de2 2023-04-04 stsp "object %s is missing from pack file",
1431 9afa3de2 2023-04-04 stsp hex);
1432 9afa3de2 2023-04-04 stsp goto done;
1433 9afa3de2 2023-04-04 stsp }
1434 13b2bc37 2022-10-23 stsp }
1435 9afa3de2 2023-04-04 stsp
1436 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(pe, repo_write.protected_branch_namespaces,
1437 9afa3de2 2023-04-04 stsp entry) {
1438 9afa3de2 2023-04-04 stsp err = protect_branch_namespace(pe->path,
1439 9afa3de2 2023-04-04 stsp &client->pack, packidx, ref_update);
1440 9afa3de2 2023-04-04 stsp if (err)
1441 9afa3de2 2023-04-04 stsp goto done;
1442 9afa3de2 2023-04-04 stsp }
1443 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(pe, repo_write.protected_branches, entry) {
1444 9afa3de2 2023-04-04 stsp err = protect_branch(pe->path, &client->pack,
1445 9afa3de2 2023-04-04 stsp packidx, ref_update);
1446 9afa3de2 2023-04-04 stsp if (err)
1447 9afa3de2 2023-04-04 stsp goto done;
1448 9afa3de2 2023-04-04 stsp }
1449 13b2bc37 2022-10-23 stsp }
1450 13b2bc37 2022-10-23 stsp
1451 e26970cc 2023-01-10 op done:
1452 13b2bc37 2022-10-23 stsp close_err = got_packidx_close(packidx);
1453 13b2bc37 2022-10-23 stsp if (close_err && err == NULL)
1454 13b2bc37 2022-10-23 stsp err = close_err;
1455 13b2bc37 2022-10-23 stsp free(id_str);
1456 9afa3de2 2023-04-04 stsp if (obj)
1457 9afa3de2 2023-04-04 stsp got_object_close(obj);
1458 13b2bc37 2022-10-23 stsp return err;
1459 13b2bc37 2022-10-23 stsp }
1460 13b2bc37 2022-10-23 stsp
1461 13b2bc37 2022-10-23 stsp static const struct got_error *
1462 9afa3de2 2023-04-04 stsp protect_refs_from_deletion(void)
1463 9afa3de2 2023-04-04 stsp {
1464 9afa3de2 2023-04-04 stsp const struct got_error *err = NULL;
1465 9afa3de2 2023-04-04 stsp struct repo_write_client *client = &repo_write_client;
1466 9afa3de2 2023-04-04 stsp struct gotd_ref_update *ref_update;
1467 9afa3de2 2023-04-04 stsp struct got_pathlist_entry *pe;
1468 9afa3de2 2023-04-04 stsp const char *refname;
1469 9afa3de2 2023-04-04 stsp
1470 9afa3de2 2023-04-04 stsp STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1471 9afa3de2 2023-04-04 stsp if (!ref_update->delete_ref)
1472 9afa3de2 2023-04-04 stsp continue;
1473 9afa3de2 2023-04-04 stsp
1474 9afa3de2 2023-04-04 stsp refname = got_ref_get_name(ref_update->ref);
1475 9afa3de2 2023-04-04 stsp
1476 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(pe, repo_write.protected_tag_namespaces, entry) {
1477 9afa3de2 2023-04-04 stsp err = protect_ref_namespace(refname, pe->path);
1478 9afa3de2 2023-04-04 stsp if (err)
1479 9afa3de2 2023-04-04 stsp return err;
1480 9afa3de2 2023-04-04 stsp }
1481 9afa3de2 2023-04-04 stsp
1482 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(pe, repo_write.protected_branch_namespaces,
1483 9afa3de2 2023-04-04 stsp entry) {
1484 9afa3de2 2023-04-04 stsp err = protect_ref_namespace(refname, pe->path);
1485 9afa3de2 2023-04-04 stsp if (err)
1486 9afa3de2 2023-04-04 stsp return err;
1487 9afa3de2 2023-04-04 stsp }
1488 9afa3de2 2023-04-04 stsp
1489 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(pe, repo_write.protected_branches, entry) {
1490 9afa3de2 2023-04-04 stsp if (strcmp(refname, pe->path) == 0) {
1491 9afa3de2 2023-04-04 stsp return got_error_fmt(GOT_ERR_REF_PROTECTED,
1492 9afa3de2 2023-04-04 stsp "%s", refname);
1493 9afa3de2 2023-04-04 stsp }
1494 9afa3de2 2023-04-04 stsp }
1495 9afa3de2 2023-04-04 stsp }
1496 9afa3de2 2023-04-04 stsp
1497 9afa3de2 2023-04-04 stsp return NULL;
1498 9afa3de2 2023-04-04 stsp }
1499 9afa3de2 2023-04-04 stsp
1500 9afa3de2 2023-04-04 stsp static const struct got_error *
1501 1a52c9bf 2022-12-30 stsp install_packfile(struct gotd_imsgev *iev)
1502 13b2bc37 2022-10-23 stsp {
1503 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1504 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_install inst;
1505 13b2bc37 2022-10-23 stsp int ret;
1506 13b2bc37 2022-10-23 stsp
1507 13b2bc37 2022-10-23 stsp memset(&inst, 0, sizeof(inst));
1508 13b2bc37 2022-10-23 stsp memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1509 13b2bc37 2022-10-23 stsp
1510 13b2bc37 2022-10-23 stsp ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1511 13b2bc37 2022-10-23 stsp PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1512 13b2bc37 2022-10-23 stsp if (ret == -1)
1513 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1514 13b2bc37 2022-10-23 stsp
1515 13b2bc37 2022-10-23 stsp return NULL;
1516 13b2bc37 2022-10-23 stsp }
1517 13b2bc37 2022-10-23 stsp
1518 13b2bc37 2022-10-23 stsp static const struct got_error *
1519 1a52c9bf 2022-12-30 stsp send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1520 13b2bc37 2022-10-23 stsp {
1521 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_updates_start istart;
1522 13b2bc37 2022-10-23 stsp int ret;
1523 13b2bc37 2022-10-23 stsp
1524 13b2bc37 2022-10-23 stsp memset(&istart, 0, sizeof(istart));
1525 13b2bc37 2022-10-23 stsp istart.nref_updates = nref_updates;
1526 13b2bc37 2022-10-23 stsp
1527 13b2bc37 2022-10-23 stsp ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1528 13b2bc37 2022-10-23 stsp PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1529 13b2bc37 2022-10-23 stsp if (ret == -1)
1530 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_compose REF_UPDATES_START");
1531 13b2bc37 2022-10-23 stsp
1532 13b2bc37 2022-10-23 stsp return NULL;
1533 13b2bc37 2022-10-23 stsp }
1534 13b2bc37 2022-10-23 stsp
1535 13b2bc37 2022-10-23 stsp
1536 13b2bc37 2022-10-23 stsp static const struct got_error *
1537 1a52c9bf 2022-12-30 stsp send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1538 13b2bc37 2022-10-23 stsp {
1539 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update iref;
1540 13b2bc37 2022-10-23 stsp const char *refname = got_ref_get_name(ref_update->ref);
1541 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
1542 13b2bc37 2022-10-23 stsp size_t len;
1543 13b2bc37 2022-10-23 stsp
1544 13b2bc37 2022-10-23 stsp memset(&iref, 0, sizeof(iref));
1545 13b2bc37 2022-10-23 stsp memcpy(iref.old_id, ref_update->old_id.sha1, SHA1_DIGEST_LENGTH);
1546 13b2bc37 2022-10-23 stsp memcpy(iref.new_id, ref_update->new_id.sha1, SHA1_DIGEST_LENGTH);
1547 13b2bc37 2022-10-23 stsp iref.ref_is_new = ref_update->ref_is_new;
1548 9a8e357c 2023-01-28 op iref.delete_ref = ref_update->delete_ref;
1549 13b2bc37 2022-10-23 stsp iref.name_len = strlen(refname);
1550 13b2bc37 2022-10-23 stsp
1551 13b2bc37 2022-10-23 stsp len = sizeof(iref) + iref.name_len;
1552 13b2bc37 2022-10-23 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1553 13b2bc37 2022-10-23 stsp repo_write.pid, len);
1554 13b2bc37 2022-10-23 stsp if (wbuf == NULL)
1555 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_create REF_UPDATE");
1556 13b2bc37 2022-10-23 stsp
1557 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1558 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE");
1559 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, refname, iref.name_len) == -1)
1560 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE");
1561 13b2bc37 2022-10-23 stsp
1562 13b2bc37 2022-10-23 stsp imsg_close(&iev->ibuf, wbuf);
1563 13b2bc37 2022-10-23 stsp
1564 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
1565 13b2bc37 2022-10-23 stsp return NULL;
1566 13b2bc37 2022-10-23 stsp }
1567 13b2bc37 2022-10-23 stsp
1568 13b2bc37 2022-10-23 stsp static const struct got_error *
1569 1a52c9bf 2022-12-30 stsp update_refs(struct gotd_imsgev *iev)
1570 13b2bc37 2022-10-23 stsp {
1571 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1572 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1573 13b2bc37 2022-10-23 stsp struct gotd_ref_update *ref_update;
1574 13b2bc37 2022-10-23 stsp
1575 1a52c9bf 2022-12-30 stsp err = send_ref_updates_start(client->nref_updates, iev);
1576 13b2bc37 2022-10-23 stsp if (err)
1577 13b2bc37 2022-10-23 stsp return err;
1578 13b2bc37 2022-10-23 stsp
1579 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1580 1a52c9bf 2022-12-30 stsp err = send_ref_update(ref_update, iev);
1581 13b2bc37 2022-10-23 stsp if (err)
1582 13b2bc37 2022-10-23 stsp goto done;
1583 13b2bc37 2022-10-23 stsp }
1584 13b2bc37 2022-10-23 stsp done:
1585 13b2bc37 2022-10-23 stsp return err;
1586 13b2bc37 2022-10-23 stsp }
1587 13b2bc37 2022-10-23 stsp
1588 13b2bc37 2022-10-23 stsp static const struct got_error *
1589 1a52c9bf 2022-12-30 stsp receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1590 13b2bc37 2022-10-23 stsp {
1591 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1592 13b2bc37 2022-10-23 stsp size_t datalen;
1593 13b2bc37 2022-10-23 stsp
1594 9cbac887 2023-04-20 stsp log_debug("receiving pack pipe descriptor");
1595 13b2bc37 2022-10-23 stsp
1596 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1597 1cb49b67 2024-03-21 stsp if (datalen != 0)
1598 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1599 13b2bc37 2022-10-23 stsp
1600 1a52c9bf 2022-12-30 stsp if (client->pack_pipe != -1)
1601 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1602 13b2bc37 2022-10-23 stsp
1603 2c52c623 2024-01-30 op client->pack_pipe = imsg_get_fd(imsg);
1604 2c52c623 2024-01-30 op if (client->pack_pipe == -1)
1605 2c52c623 2024-01-30 op return got_error(GOT_ERR_PRIVSEP_NO_FD);
1606 2c52c623 2024-01-30 op
1607 13b2bc37 2022-10-23 stsp return NULL;
1608 13b2bc37 2022-10-23 stsp }
1609 13b2bc37 2022-10-23 stsp
1610 13b2bc37 2022-10-23 stsp static const struct got_error *
1611 1a52c9bf 2022-12-30 stsp receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1612 13b2bc37 2022-10-23 stsp {
1613 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1614 13b2bc37 2022-10-23 stsp size_t datalen;
1615 13b2bc37 2022-10-23 stsp
1616 9cbac887 2023-04-20 stsp log_debug("receiving pack index output file");
1617 13b2bc37 2022-10-23 stsp
1618 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1619 1cb49b67 2024-03-21 stsp if (datalen != 0)
1620 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1621 13b2bc37 2022-10-23 stsp
1622 1a52c9bf 2022-12-30 stsp if (client->packidx_fd != -1)
1623 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1624 13b2bc37 2022-10-23 stsp
1625 2c52c623 2024-01-30 op client->packidx_fd = imsg_get_fd(imsg);
1626 2c52c623 2024-01-30 op if (client->packidx_fd == -1)
1627 2c52c623 2024-01-30 op return got_error(GOT_ERR_PRIVSEP_NO_FD);
1628 2c52c623 2024-01-30 op
1629 13b2bc37 2022-10-23 stsp return NULL;
1630 ba97b2d7 2024-03-20 stsp }
1631 ba97b2d7 2024-03-20 stsp
1632 ba97b2d7 2024-03-20 stsp static char *
1633 ba97b2d7 2024-03-20 stsp get_datestr(time_t *time, char *datebuf)
1634 ba97b2d7 2024-03-20 stsp {
1635 ba97b2d7 2024-03-20 stsp struct tm mytm, *tm;
1636 ba97b2d7 2024-03-20 stsp char *p, *s;
1637 ba97b2d7 2024-03-20 stsp
1638 ba97b2d7 2024-03-20 stsp tm = gmtime_r(time, &mytm);
1639 ba97b2d7 2024-03-20 stsp if (tm == NULL)
1640 ba97b2d7 2024-03-20 stsp return NULL;
1641 ba97b2d7 2024-03-20 stsp s = asctime_r(tm, datebuf);
1642 ba97b2d7 2024-03-20 stsp if (s == NULL)
1643 ba97b2d7 2024-03-20 stsp return NULL;
1644 ba97b2d7 2024-03-20 stsp p = strchr(s, '\n');
1645 ba97b2d7 2024-03-20 stsp if (p)
1646 ba97b2d7 2024-03-20 stsp *p = '\0';
1647 ba97b2d7 2024-03-20 stsp return s;
1648 ba97b2d7 2024-03-20 stsp }
1649 ba97b2d7 2024-03-20 stsp
1650 ba97b2d7 2024-03-20 stsp static const struct got_error *
1651 ba97b2d7 2024-03-20 stsp notify_removed_ref(const char *refname, uint8_t *sha1,
1652 ba97b2d7 2024-03-20 stsp struct gotd_imsgev *iev, int fd)
1653 ba97b2d7 2024-03-20 stsp {
1654 ba97b2d7 2024-03-20 stsp const struct got_error *err;
1655 ba97b2d7 2024-03-20 stsp struct got_object_id id;
1656 ba97b2d7 2024-03-20 stsp char *id_str;
1657 ba97b2d7 2024-03-20 stsp
1658 ba97b2d7 2024-03-20 stsp memset(&id, 0, sizeof(id));
1659 ba97b2d7 2024-03-20 stsp memcpy(id.sha1, sha1, sizeof(id.sha1));
1660 ba97b2d7 2024-03-20 stsp
1661 ba97b2d7 2024-03-20 stsp err = got_object_id_str(&id_str, &id);
1662 ba97b2d7 2024-03-20 stsp if (err)
1663 ba97b2d7 2024-03-20 stsp return err;
1664 ba97b2d7 2024-03-20 stsp
1665 ba97b2d7 2024-03-20 stsp dprintf(fd, "Removed %s: %s\n", refname, id_str);
1666 ba97b2d7 2024-03-20 stsp free(id_str);
1667 ba97b2d7 2024-03-20 stsp return err;
1668 13b2bc37 2022-10-23 stsp }
1669 13b2bc37 2022-10-23 stsp
1670 ba97b2d7 2024-03-20 stsp static const char *
1671 ba97b2d7 2024-03-20 stsp format_author(char *author)
1672 ba97b2d7 2024-03-20 stsp {
1673 ba97b2d7 2024-03-20 stsp char *smallerthan;
1674 ba97b2d7 2024-03-20 stsp
1675 ba97b2d7 2024-03-20 stsp smallerthan = strchr(author, '<');
1676 ba97b2d7 2024-03-20 stsp if (smallerthan && smallerthan[1] != '\0')
1677 ba97b2d7 2024-03-20 stsp author = smallerthan + 1;
1678 ba97b2d7 2024-03-20 stsp author[strcspn(author, "@>")] = '\0';
1679 ba97b2d7 2024-03-20 stsp
1680 ba97b2d7 2024-03-20 stsp return author;
1681 ba97b2d7 2024-03-20 stsp }
1682 ba97b2d7 2024-03-20 stsp
1683 ba97b2d7 2024-03-20 stsp static const struct got_error *
1684 ba97b2d7 2024-03-20 stsp print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
1685 ba97b2d7 2024-03-20 stsp struct got_repository *repo, int fd)
1686 ba97b2d7 2024-03-20 stsp {
1687 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
1688 ba97b2d7 2024-03-20 stsp char *id_str = NULL, *logmsg0 = NULL;
1689 ba97b2d7 2024-03-20 stsp char *s, *nl;
1690 ba97b2d7 2024-03-20 stsp char *committer = NULL, *author = NULL;
1691 ba97b2d7 2024-03-20 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1692 ba97b2d7 2024-03-20 stsp struct tm tm;
1693 ba97b2d7 2024-03-20 stsp time_t committer_time;
1694 ba97b2d7 2024-03-20 stsp
1695 ba97b2d7 2024-03-20 stsp err = got_object_id_str(&id_str, id);
1696 ba97b2d7 2024-03-20 stsp if (err)
1697 ba97b2d7 2024-03-20 stsp return err;
1698 ba97b2d7 2024-03-20 stsp
1699 ba97b2d7 2024-03-20 stsp committer_time = got_object_commit_get_committer_time(commit);
1700 ba97b2d7 2024-03-20 stsp if (gmtime_r(&committer_time, &tm) == NULL) {
1701 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("gmtime_r");
1702 ba97b2d7 2024-03-20 stsp goto done;
1703 ba97b2d7 2024-03-20 stsp }
1704 ba97b2d7 2024-03-20 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
1705 ba97b2d7 2024-03-20 stsp err = got_error(GOT_ERR_NO_SPACE);
1706 ba97b2d7 2024-03-20 stsp goto done;
1707 ba97b2d7 2024-03-20 stsp }
1708 ba97b2d7 2024-03-20 stsp
1709 ba97b2d7 2024-03-20 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1710 ba97b2d7 2024-03-20 stsp if (err)
1711 ba97b2d7 2024-03-20 stsp goto done;
1712 ba97b2d7 2024-03-20 stsp
1713 ba97b2d7 2024-03-20 stsp s = logmsg0;
1714 ba97b2d7 2024-03-20 stsp while (isspace((unsigned char)s[0]))
1715 ba97b2d7 2024-03-20 stsp s++;
1716 ba97b2d7 2024-03-20 stsp
1717 ba97b2d7 2024-03-20 stsp nl = strchr(s, '\n');
1718 ba97b2d7 2024-03-20 stsp if (nl) {
1719 ba97b2d7 2024-03-20 stsp *nl = '\0';
1720 ba97b2d7 2024-03-20 stsp }
1721 ba97b2d7 2024-03-20 stsp
1722 ba97b2d7 2024-03-20 stsp if (strcmp(got_object_commit_get_author(commit),
1723 ba97b2d7 2024-03-20 stsp got_object_commit_get_committer(commit)) != 0) {
1724 ba97b2d7 2024-03-20 stsp author = strdup(got_object_commit_get_author(commit));
1725 ba97b2d7 2024-03-20 stsp if (author == NULL) {
1726 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strdup");
1727 ba97b2d7 2024-03-20 stsp goto done;
1728 ba97b2d7 2024-03-20 stsp }
1729 ba97b2d7 2024-03-20 stsp dprintf(fd, "%s%.7s %.8s %s\n", datebuf, id_str,
1730 ba97b2d7 2024-03-20 stsp format_author(author), s);
1731 ba97b2d7 2024-03-20 stsp } else {
1732 ba97b2d7 2024-03-20 stsp committer = strdup(got_object_commit_get_committer(commit));
1733 ba97b2d7 2024-03-20 stsp if (committer == NULL) {
1734 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strdup");
1735 ba97b2d7 2024-03-20 stsp goto done;
1736 ba97b2d7 2024-03-20 stsp }
1737 ba97b2d7 2024-03-20 stsp dprintf(fd, "%s%.7s %.8s %s\n", datebuf, id_str,
1738 ba97b2d7 2024-03-20 stsp format_author(committer), s);
1739 ba97b2d7 2024-03-20 stsp }
1740 ba97b2d7 2024-03-20 stsp
1741 ba97b2d7 2024-03-20 stsp if (fsync(fd) == -1 && err == NULL)
1742 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("fsync");
1743 ba97b2d7 2024-03-20 stsp done:
1744 ba97b2d7 2024-03-20 stsp free(id_str);
1745 ba97b2d7 2024-03-20 stsp free(logmsg0);
1746 ba97b2d7 2024-03-20 stsp free(committer);
1747 ba97b2d7 2024-03-20 stsp free(author);
1748 ba97b2d7 2024-03-20 stsp return err;
1749 ba97b2d7 2024-03-20 stsp }
1750 ba97b2d7 2024-03-20 stsp
1751 ba97b2d7 2024-03-20 stsp static const struct got_error *
1752 ba97b2d7 2024-03-20 stsp print_diffstat(struct got_diffstat_cb_arg *dsa, int fd)
1753 ba97b2d7 2024-03-20 stsp {
1754 ba97b2d7 2024-03-20 stsp struct got_pathlist_entry *pe;
1755 ba97b2d7 2024-03-20 stsp
1756 ba97b2d7 2024-03-20 stsp TAILQ_FOREACH(pe, dsa->paths, entry) {
1757 ba97b2d7 2024-03-20 stsp struct got_diff_changed_path *cp = pe->data;
1758 ba97b2d7 2024-03-20 stsp int pad = dsa->max_path_len - pe->path_len + 1;
1759 ba97b2d7 2024-03-20 stsp
1760 ba97b2d7 2024-03-20 stsp dprintf(fd, " %c %s%*c | %*d+ %*d-\n", cp->status,
1761 ba97b2d7 2024-03-20 stsp pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
1762 ba97b2d7 2024-03-20 stsp dsa->rm_cols + 1, cp->rm);
1763 ba97b2d7 2024-03-20 stsp }
1764 ba97b2d7 2024-03-20 stsp dprintf(fd,
1765 ba97b2d7 2024-03-20 stsp "\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
1766 ba97b2d7 2024-03-20 stsp dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
1767 ba97b2d7 2024-03-20 stsp dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
1768 ba97b2d7 2024-03-20 stsp
1769 ba97b2d7 2024-03-20 stsp return NULL;
1770 ba97b2d7 2024-03-20 stsp }
1771 ba97b2d7 2024-03-20 stsp
1772 ba97b2d7 2024-03-20 stsp static const struct got_error *
1773 ba97b2d7 2024-03-20 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1774 ba97b2d7 2024-03-20 stsp struct got_repository *repo, struct got_pathlist_head *changed_paths,
1775 ba97b2d7 2024-03-20 stsp struct got_diffstat_cb_arg *diffstat, int fd)
1776 ba97b2d7 2024-03-20 stsp {
1777 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
1778 ba97b2d7 2024-03-20 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1779 ba97b2d7 2024-03-20 stsp char datebuf[26];
1780 ba97b2d7 2024-03-20 stsp time_t committer_time;
1781 ba97b2d7 2024-03-20 stsp const char *author, *committer;
1782 ba97b2d7 2024-03-20 stsp
1783 ba97b2d7 2024-03-20 stsp err = got_object_id_str(&id_str, id);
1784 ba97b2d7 2024-03-20 stsp if (err)
1785 ba97b2d7 2024-03-20 stsp return err;
1786 ba97b2d7 2024-03-20 stsp
1787 ba97b2d7 2024-03-20 stsp dprintf(fd, "commit %s\n", id_str);
1788 ba97b2d7 2024-03-20 stsp free(id_str);
1789 ba97b2d7 2024-03-20 stsp id_str = NULL;
1790 ba97b2d7 2024-03-20 stsp dprintf(fd, "from: %s\n", got_object_commit_get_author(commit));
1791 ba97b2d7 2024-03-20 stsp author = got_object_commit_get_author(commit);
1792 ba97b2d7 2024-03-20 stsp committer = got_object_commit_get_committer(commit);
1793 ba97b2d7 2024-03-20 stsp if (strcmp(author, committer) != 0)
1794 ba97b2d7 2024-03-20 stsp dprintf(fd, "via: %s\n", committer);
1795 ba97b2d7 2024-03-20 stsp committer_time = got_object_commit_get_committer_time(commit);
1796 ba97b2d7 2024-03-20 stsp datestr = get_datestr(&committer_time, datebuf);
1797 ba97b2d7 2024-03-20 stsp if (datestr)
1798 ba97b2d7 2024-03-20 stsp dprintf(fd, "date: %s UTC\n", datestr);
1799 ba97b2d7 2024-03-20 stsp if (got_object_commit_get_nparents(commit) > 1) {
1800 ba97b2d7 2024-03-20 stsp const struct got_object_id_queue *parent_ids;
1801 ba97b2d7 2024-03-20 stsp struct got_object_qid *qid;
1802 ba97b2d7 2024-03-20 stsp int n = 1;
1803 ba97b2d7 2024-03-20 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1804 ba97b2d7 2024-03-20 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
1805 ba97b2d7 2024-03-20 stsp err = got_object_id_str(&id_str, &qid->id);
1806 ba97b2d7 2024-03-20 stsp if (err)
1807 ba97b2d7 2024-03-20 stsp goto done;
1808 ba97b2d7 2024-03-20 stsp dprintf(fd, "parent %d: %s\n", n++, id_str);
1809 ba97b2d7 2024-03-20 stsp free(id_str);
1810 ba97b2d7 2024-03-20 stsp id_str = NULL;
1811 ba97b2d7 2024-03-20 stsp }
1812 ba97b2d7 2024-03-20 stsp }
1813 ba97b2d7 2024-03-20 stsp
1814 ba97b2d7 2024-03-20 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1815 ba97b2d7 2024-03-20 stsp if (err)
1816 ba97b2d7 2024-03-20 stsp goto done;
1817 fb5636be 2024-03-27 op
1818 fb5636be 2024-03-27 op dprintf(fd, "messagelen: %zu\n", strlen(logmsg0));
1819 ba97b2d7 2024-03-20 stsp
1820 ba97b2d7 2024-03-20 stsp logmsg = logmsg0;
1821 ba97b2d7 2024-03-20 stsp do {
1822 ba97b2d7 2024-03-20 stsp line = strsep(&logmsg, "\n");
1823 ba97b2d7 2024-03-20 stsp if (line)
1824 ba97b2d7 2024-03-20 stsp dprintf(fd, " %s\n", line);
1825 ba97b2d7 2024-03-20 stsp } while (line);
1826 ba97b2d7 2024-03-20 stsp free(logmsg0);
1827 ba97b2d7 2024-03-20 stsp
1828 ba97b2d7 2024-03-20 stsp err = print_diffstat(diffstat, fd);
1829 ba97b2d7 2024-03-20 stsp if (err)
1830 ba97b2d7 2024-03-20 stsp goto done;
1831 ba97b2d7 2024-03-20 stsp
1832 ba97b2d7 2024-03-20 stsp if (fsync(fd) == -1 && err == NULL)
1833 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("fsync");
1834 ba97b2d7 2024-03-20 stsp done:
1835 ba97b2d7 2024-03-20 stsp free(id_str);
1836 ba97b2d7 2024-03-20 stsp return err;
1837 ba97b2d7 2024-03-20 stsp }
1838 ba97b2d7 2024-03-20 stsp
1839 ba97b2d7 2024-03-20 stsp static const struct got_error *
1840 ba97b2d7 2024-03-20 stsp get_changed_paths(struct got_pathlist_head *paths,
1841 ba97b2d7 2024-03-20 stsp struct got_commit_object *commit, struct got_repository *repo,
1842 ba97b2d7 2024-03-20 stsp struct got_diffstat_cb_arg *dsa)
1843 ba97b2d7 2024-03-20 stsp {
1844 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
1845 ba97b2d7 2024-03-20 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1846 ba97b2d7 2024-03-20 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1847 ba97b2d7 2024-03-20 stsp struct got_object_qid *qid;
1848 ba97b2d7 2024-03-20 stsp got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
1849 ba97b2d7 2024-03-20 stsp FILE *f1 = repo_write.diff.f1, *f2 = repo_write.diff.f2;
1850 ba97b2d7 2024-03-20 stsp int fd1 = repo_write.diff.fd1, fd2 = repo_write.diff.fd2;
1851 ba97b2d7 2024-03-20 stsp
1852 ba97b2d7 2024-03-20 stsp if (dsa)
1853 ba97b2d7 2024-03-20 stsp cb = got_diff_tree_compute_diffstat;
1854 ba97b2d7 2024-03-20 stsp
1855 ba97b2d7 2024-03-20 stsp err = got_opentemp_truncate(f1);
1856 ba97b2d7 2024-03-20 stsp if (err)
1857 ba97b2d7 2024-03-20 stsp return err;
1858 ba97b2d7 2024-03-20 stsp err = got_opentemp_truncate(f2);
1859 ba97b2d7 2024-03-20 stsp if (err)
1860 ba97b2d7 2024-03-20 stsp return err;
1861 ba97b2d7 2024-03-20 stsp err = got_opentemp_truncatefd(fd1);
1862 ba97b2d7 2024-03-20 stsp if (err)
1863 ba97b2d7 2024-03-20 stsp return err;
1864 ba97b2d7 2024-03-20 stsp err = got_opentemp_truncatefd(fd2);
1865 ba97b2d7 2024-03-20 stsp if (err)
1866 ba97b2d7 2024-03-20 stsp return err;
1867 ba97b2d7 2024-03-20 stsp
1868 ba97b2d7 2024-03-20 stsp qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1869 ba97b2d7 2024-03-20 stsp if (qid != NULL) {
1870 ba97b2d7 2024-03-20 stsp struct got_commit_object *pcommit;
1871 ba97b2d7 2024-03-20 stsp err = got_object_open_as_commit(&pcommit, repo,
1872 ba97b2d7 2024-03-20 stsp &qid->id);
1873 ba97b2d7 2024-03-20 stsp if (err)
1874 ba97b2d7 2024-03-20 stsp return err;
1875 ba97b2d7 2024-03-20 stsp
1876 ba97b2d7 2024-03-20 stsp tree_id1 = got_object_id_dup(
1877 ba97b2d7 2024-03-20 stsp got_object_commit_get_tree_id(pcommit));
1878 ba97b2d7 2024-03-20 stsp if (tree_id1 == NULL) {
1879 ba97b2d7 2024-03-20 stsp got_object_commit_close(pcommit);
1880 ba97b2d7 2024-03-20 stsp return got_error_from_errno("got_object_id_dup");
1881 ba97b2d7 2024-03-20 stsp }
1882 ba97b2d7 2024-03-20 stsp got_object_commit_close(pcommit);
1883 ba97b2d7 2024-03-20 stsp
1884 ba97b2d7 2024-03-20 stsp }
1885 ba97b2d7 2024-03-20 stsp
1886 ba97b2d7 2024-03-20 stsp if (tree_id1) {
1887 ba97b2d7 2024-03-20 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1888 ba97b2d7 2024-03-20 stsp if (err)
1889 ba97b2d7 2024-03-20 stsp goto done;
1890 ba97b2d7 2024-03-20 stsp }
1891 ba97b2d7 2024-03-20 stsp
1892 ba97b2d7 2024-03-20 stsp tree_id2 = got_object_commit_get_tree_id(commit);
1893 ba97b2d7 2024-03-20 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1894 ba97b2d7 2024-03-20 stsp if (err)
1895 ba97b2d7 2024-03-20 stsp goto done;
1896 ba97b2d7 2024-03-20 stsp
1897 ba97b2d7 2024-03-20 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
1898 ba97b2d7 2024-03-20 stsp cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
1899 ba97b2d7 2024-03-20 stsp done:
1900 ba97b2d7 2024-03-20 stsp if (tree1)
1901 ba97b2d7 2024-03-20 stsp got_object_tree_close(tree1);
1902 ba97b2d7 2024-03-20 stsp if (tree2)
1903 ba97b2d7 2024-03-20 stsp got_object_tree_close(tree2);
1904 ba97b2d7 2024-03-20 stsp free(tree_id1);
1905 ba97b2d7 2024-03-20 stsp return err;
1906 ba97b2d7 2024-03-20 stsp }
1907 ba97b2d7 2024-03-20 stsp
1908 ba97b2d7 2024-03-20 stsp static const struct got_error *
1909 ba97b2d7 2024-03-20 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
1910 ba97b2d7 2024-03-20 stsp struct got_repository *repo, int fd)
1911 ba97b2d7 2024-03-20 stsp {
1912 ba97b2d7 2024-03-20 stsp const struct got_error *err;
1913 ba97b2d7 2024-03-20 stsp struct got_commit_graph *graph;
1914 ba97b2d7 2024-03-20 stsp struct got_object_id_queue reversed_commits;
1915 ba97b2d7 2024-03-20 stsp struct got_object_qid *qid;
1916 ba97b2d7 2024-03-20 stsp struct got_commit_object *commit = NULL;
1917 ba97b2d7 2024-03-20 stsp struct got_pathlist_head changed_paths;
1918 ba97b2d7 2024-03-20 stsp int ncommits = 0;
1919 ba97b2d7 2024-03-20 stsp const int shortlog_threshold = 50;
1920 ba97b2d7 2024-03-20 stsp
1921 ba97b2d7 2024-03-20 stsp STAILQ_INIT(&reversed_commits);
1922 ba97b2d7 2024-03-20 stsp TAILQ_INIT(&changed_paths);
1923 ba97b2d7 2024-03-20 stsp
1924 ba97b2d7 2024-03-20 stsp /* XXX first-parent only for now */
1925 ba97b2d7 2024-03-20 stsp err = got_commit_graph_open(&graph, "/", 1);
1926 ba97b2d7 2024-03-20 stsp if (err)
1927 ba97b2d7 2024-03-20 stsp return err;
1928 98297eed 2024-03-27 stsp err = got_commit_graph_bfsort(graph, root_id, repo,
1929 ba97b2d7 2024-03-20 stsp check_cancelled, NULL);
1930 ba97b2d7 2024-03-20 stsp if (err)
1931 ba97b2d7 2024-03-20 stsp goto done;
1932 ba97b2d7 2024-03-20 stsp for (;;) {
1933 ba97b2d7 2024-03-20 stsp struct got_object_id id;
1934 ba97b2d7 2024-03-20 stsp
1935 ba97b2d7 2024-03-20 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1936 ba97b2d7 2024-03-20 stsp check_cancelled, NULL);
1937 ba97b2d7 2024-03-20 stsp if (err) {
1938 ba97b2d7 2024-03-20 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1939 ba97b2d7 2024-03-20 stsp err = NULL;
1940 ba97b2d7 2024-03-20 stsp break;
1941 ba97b2d7 2024-03-20 stsp }
1942 ba97b2d7 2024-03-20 stsp
1943 ba97b2d7 2024-03-20 stsp err = got_object_open_as_commit(&commit, repo, &id);
1944 ba97b2d7 2024-03-20 stsp if (err)
1945 ba97b2d7 2024-03-20 stsp break;
1946 ba97b2d7 2024-03-20 stsp
1947 ba97b2d7 2024-03-20 stsp if (end_id && got_object_id_cmp(&id, end_id) == 0)
1948 ba97b2d7 2024-03-20 stsp break;
1949 ba97b2d7 2024-03-20 stsp
1950 ba97b2d7 2024-03-20 stsp err = got_object_qid_alloc(&qid, &id);
1951 ba97b2d7 2024-03-20 stsp if (err)
1952 ba97b2d7 2024-03-20 stsp break;
1953 ba97b2d7 2024-03-20 stsp
1954 ba97b2d7 2024-03-20 stsp STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
1955 ba97b2d7 2024-03-20 stsp ncommits++;
1956 ba97b2d7 2024-03-20 stsp got_object_commit_close(commit);
1957 ba97b2d7 2024-03-20 stsp
1958 ba97b2d7 2024-03-20 stsp if (end_id == NULL)
1959 ba97b2d7 2024-03-20 stsp break;
1960 ba97b2d7 2024-03-20 stsp }
1961 ba97b2d7 2024-03-20 stsp
1962 ba97b2d7 2024-03-20 stsp STAILQ_FOREACH(qid, &reversed_commits, entry) {
1963 ba97b2d7 2024-03-20 stsp struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
1964 ba97b2d7 2024-03-20 stsp &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
1965 ba97b2d7 2024-03-20 stsp
1966 ba97b2d7 2024-03-20 stsp err = got_object_open_as_commit(&commit, repo, &qid->id);
1967 ba97b2d7 2024-03-20 stsp if (err)
1968 ba97b2d7 2024-03-20 stsp break;
1969 7a095277 2024-03-28 op
1970 ba97b2d7 2024-03-20 stsp if (ncommits > shortlog_threshold) {
1971 ba97b2d7 2024-03-20 stsp err = print_commit_oneline(commit, &qid->id,
1972 ba97b2d7 2024-03-20 stsp repo, fd);
1973 ba97b2d7 2024-03-20 stsp if (err)
1974 ba97b2d7 2024-03-20 stsp break;
1975 ba97b2d7 2024-03-20 stsp } else {
1976 ba97b2d7 2024-03-20 stsp err = get_changed_paths(&changed_paths, commit,
1977 ba97b2d7 2024-03-20 stsp repo, &dsa);
1978 ba97b2d7 2024-03-20 stsp if (err)
1979 ba97b2d7 2024-03-20 stsp break;
1980 ba97b2d7 2024-03-20 stsp err = print_commit(commit, &qid->id, repo,
1981 ba97b2d7 2024-03-20 stsp &changed_paths, &dsa, fd);
1982 ba97b2d7 2024-03-20 stsp }
1983 ba97b2d7 2024-03-20 stsp got_object_commit_close(commit);
1984 ba97b2d7 2024-03-20 stsp commit = NULL;
1985 ba97b2d7 2024-03-20 stsp got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
1986 ba97b2d7 2024-03-20 stsp }
1987 ba97b2d7 2024-03-20 stsp done:
1988 ba97b2d7 2024-03-20 stsp if (commit)
1989 ba97b2d7 2024-03-20 stsp got_object_commit_close(commit);
1990 ba97b2d7 2024-03-20 stsp while (!STAILQ_EMPTY(&reversed_commits)) {
1991 ba97b2d7 2024-03-20 stsp qid = STAILQ_FIRST(&reversed_commits);
1992 ba97b2d7 2024-03-20 stsp STAILQ_REMOVE_HEAD(&reversed_commits, entry);
1993 ba97b2d7 2024-03-20 stsp got_object_qid_free(qid);
1994 ba97b2d7 2024-03-20 stsp }
1995 ba97b2d7 2024-03-20 stsp got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
1996 ba97b2d7 2024-03-20 stsp got_commit_graph_close(graph);
1997 ba97b2d7 2024-03-20 stsp return err;
1998 ba97b2d7 2024-03-20 stsp }
1999 ba97b2d7 2024-03-20 stsp
2000 ba97b2d7 2024-03-20 stsp static const struct got_error *
2001 7a095277 2024-03-28 op print_tag(struct got_object_id *id,
2002 ba97b2d7 2024-03-20 stsp const char *refname, struct got_repository *repo, int fd)
2003 ba97b2d7 2024-03-20 stsp {
2004 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
2005 ba97b2d7 2024-03-20 stsp struct got_tag_object *tag = NULL;
2006 ba97b2d7 2024-03-20 stsp const char *tagger = NULL;
2007 ba97b2d7 2024-03-20 stsp char *id_str = NULL, *tagmsg0 = NULL, *tagmsg, *line, *datestr;
2008 ba97b2d7 2024-03-20 stsp char datebuf[26];
2009 ba97b2d7 2024-03-20 stsp time_t tagger_time;
2010 ba97b2d7 2024-03-20 stsp
2011 ba97b2d7 2024-03-20 stsp err = got_object_open_as_tag(&tag, repo, id);
2012 ba97b2d7 2024-03-20 stsp if (err)
2013 ba97b2d7 2024-03-20 stsp return err;
2014 ba97b2d7 2024-03-20 stsp
2015 ba97b2d7 2024-03-20 stsp tagger = got_object_tag_get_tagger(tag);
2016 ba97b2d7 2024-03-20 stsp tagger_time = got_object_tag_get_tagger_time(tag);
2017 ba97b2d7 2024-03-20 stsp err = got_object_id_str(&id_str,
2018 ba97b2d7 2024-03-20 stsp got_object_tag_get_object_id(tag));
2019 ba97b2d7 2024-03-20 stsp if (err)
2020 ba97b2d7 2024-03-20 stsp goto done;
2021 ba97b2d7 2024-03-20 stsp
2022 ba97b2d7 2024-03-20 stsp dprintf(fd, "tag %s\n", refname);
2023 ba97b2d7 2024-03-20 stsp dprintf(fd, "from: %s\n", tagger);
2024 ba97b2d7 2024-03-20 stsp datestr = get_datestr(&tagger_time, datebuf);
2025 ba97b2d7 2024-03-20 stsp if (datestr)
2026 ba97b2d7 2024-03-20 stsp dprintf(fd, "date: %s UTC\n", datestr);
2027 ba97b2d7 2024-03-20 stsp
2028 ba97b2d7 2024-03-20 stsp switch (got_object_tag_get_object_type(tag)) {
2029 ba97b2d7 2024-03-20 stsp case GOT_OBJ_TYPE_BLOB:
2030 ba97b2d7 2024-03-20 stsp dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
2031 ba97b2d7 2024-03-20 stsp break;
2032 ba97b2d7 2024-03-20 stsp case GOT_OBJ_TYPE_TREE:
2033 ba97b2d7 2024-03-20 stsp dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
2034 ba97b2d7 2024-03-20 stsp break;
2035 ba97b2d7 2024-03-20 stsp case GOT_OBJ_TYPE_COMMIT:
2036 ba97b2d7 2024-03-20 stsp dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
2037 ba97b2d7 2024-03-20 stsp break;
2038 ba97b2d7 2024-03-20 stsp case GOT_OBJ_TYPE_TAG:
2039 ba97b2d7 2024-03-20 stsp dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
2040 ba97b2d7 2024-03-20 stsp break;
2041 ba97b2d7 2024-03-20 stsp default:
2042 ba97b2d7 2024-03-20 stsp break;
2043 ba97b2d7 2024-03-20 stsp }
2044 ba97b2d7 2024-03-20 stsp
2045 ba97b2d7 2024-03-20 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
2046 ba97b2d7 2024-03-20 stsp if (tagmsg0 == NULL) {
2047 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strdup");
2048 ba97b2d7 2024-03-20 stsp goto done;
2049 ba97b2d7 2024-03-20 stsp }
2050 fb5636be 2024-03-27 op
2051 fb5636be 2024-03-27 op dprintf(fd, "messagelen: %zu\n", strlen(tagmsg0));
2052 fb5636be 2024-03-27 op
2053 ba97b2d7 2024-03-20 stsp tagmsg = tagmsg0;
2054 ba97b2d7 2024-03-20 stsp do {
2055 ba97b2d7 2024-03-20 stsp line = strsep(&tagmsg, "\n");
2056 ba97b2d7 2024-03-20 stsp if (line)
2057 ba97b2d7 2024-03-20 stsp dprintf(fd, " %s\n", line);
2058 ba97b2d7 2024-03-20 stsp } while (line);
2059 ba97b2d7 2024-03-20 stsp free(tagmsg0);
2060 ba97b2d7 2024-03-20 stsp done:
2061 ba97b2d7 2024-03-20 stsp if (tag)
2062 ba97b2d7 2024-03-20 stsp got_object_tag_close(tag);
2063 ba97b2d7 2024-03-20 stsp free(id_str);
2064 ba97b2d7 2024-03-20 stsp return err;
2065 ba97b2d7 2024-03-20 stsp }
2066 ba97b2d7 2024-03-20 stsp
2067 ba97b2d7 2024-03-20 stsp static const struct got_error *
2068 ba97b2d7 2024-03-20 stsp notify_changed_ref(const char *refname, uint8_t *old_sha1,
2069 ba97b2d7 2024-03-20 stsp uint8_t *new_sha1, struct gotd_imsgev *iev, int fd)
2070 ba97b2d7 2024-03-20 stsp {
2071 ba97b2d7 2024-03-20 stsp const struct got_error *err;
2072 ba97b2d7 2024-03-20 stsp struct got_object_id old_id, new_id;
2073 ba97b2d7 2024-03-20 stsp int old_obj_type, new_obj_type;
2074 ba97b2d7 2024-03-20 stsp const char *label;
2075 ba97b2d7 2024-03-20 stsp char *new_id_str = NULL;
2076 ba97b2d7 2024-03-20 stsp
2077 ba97b2d7 2024-03-20 stsp memset(&old_id, 0, sizeof(old_id));
2078 ba97b2d7 2024-03-20 stsp memcpy(old_id.sha1, old_sha1, sizeof(old_id.sha1));
2079 ba97b2d7 2024-03-20 stsp memset(&new_id, 0, sizeof(new_id));
2080 ba97b2d7 2024-03-20 stsp memcpy(new_id.sha1, new_sha1, sizeof(new_id.sha1));
2081 ba97b2d7 2024-03-20 stsp
2082 ba97b2d7 2024-03-20 stsp err = got_object_get_type(&old_obj_type, repo_write.repo, &old_id);
2083 ba97b2d7 2024-03-20 stsp if (err)
2084 ba97b2d7 2024-03-20 stsp return err;
2085 ba97b2d7 2024-03-20 stsp
2086 ba97b2d7 2024-03-20 stsp err = got_object_get_type(&new_obj_type, repo_write.repo, &new_id);
2087 ba97b2d7 2024-03-20 stsp if (err)
2088 ba97b2d7 2024-03-20 stsp return err;
2089 ba97b2d7 2024-03-20 stsp
2090 ba97b2d7 2024-03-20 stsp switch (new_obj_type) {
2091 ba97b2d7 2024-03-20 stsp case GOT_OBJ_TYPE_COMMIT:
2092 ba97b2d7 2024-03-20 stsp err = print_commits(&new_id,
2093 ba97b2d7 2024-03-20 stsp old_obj_type == GOT_OBJ_TYPE_COMMIT ? &old_id : NULL,
2094 ba97b2d7 2024-03-20 stsp repo_write.repo, fd);
2095 ba97b2d7 2024-03-20 stsp break;
2096 ba97b2d7 2024-03-20 stsp case GOT_OBJ_TYPE_TAG:
2097 ba97b2d7 2024-03-20 stsp err = print_tag(&new_id, refname, repo_write.repo, fd);
2098 ba97b2d7 2024-03-20 stsp break;
2099 ba97b2d7 2024-03-20 stsp default:
2100 ba97b2d7 2024-03-20 stsp err = got_object_type_label(&label, new_obj_type);
2101 ba97b2d7 2024-03-20 stsp if (err)
2102 ba97b2d7 2024-03-20 stsp goto done;
2103 ba97b2d7 2024-03-20 stsp err = got_object_id_str(&new_id_str, &new_id);
2104 ba97b2d7 2024-03-20 stsp if (err)
2105 ba97b2d7 2024-03-20 stsp goto done;
2106 ba97b2d7 2024-03-20 stsp dprintf(fd, "%s: %s object %s\n", refname, label, new_id_str);
2107 ba97b2d7 2024-03-20 stsp break;
2108 ba97b2d7 2024-03-20 stsp }
2109 ba97b2d7 2024-03-20 stsp done:
2110 ba97b2d7 2024-03-20 stsp free(new_id_str);
2111 ba97b2d7 2024-03-20 stsp return err;
2112 ba97b2d7 2024-03-20 stsp }
2113 ba97b2d7 2024-03-20 stsp
2114 ba97b2d7 2024-03-20 stsp static const struct got_error *
2115 ba97b2d7 2024-03-20 stsp notify_created_ref(const char *refname, uint8_t *sha1,
2116 ba97b2d7 2024-03-20 stsp struct gotd_imsgev *iev, int fd)
2117 ba97b2d7 2024-03-20 stsp {
2118 ba97b2d7 2024-03-20 stsp const struct got_error *err;
2119 ba97b2d7 2024-03-20 stsp struct got_object_id id;
2120 ba97b2d7 2024-03-20 stsp int obj_type;
2121 ba97b2d7 2024-03-20 stsp
2122 ba97b2d7 2024-03-20 stsp memset(&id, 0, sizeof(id));
2123 ba97b2d7 2024-03-20 stsp memcpy(id.sha1, sha1, sizeof(id.sha1));
2124 ba97b2d7 2024-03-20 stsp
2125 ba97b2d7 2024-03-20 stsp err = got_object_get_type(&obj_type, repo_write.repo, &id);
2126 ba97b2d7 2024-03-20 stsp if (err)
2127 ba97b2d7 2024-03-20 stsp return err;
2128 ba97b2d7 2024-03-20 stsp
2129 ba97b2d7 2024-03-20 stsp if (obj_type == GOT_OBJ_TYPE_TAG)
2130 ba97b2d7 2024-03-20 stsp return print_tag(&id, refname, repo_write.repo, fd);
2131 ba97b2d7 2024-03-20 stsp
2132 ba97b2d7 2024-03-20 stsp return print_commits(&id, NULL, repo_write.repo, fd);
2133 ba97b2d7 2024-03-20 stsp }
2134 ba97b2d7 2024-03-20 stsp
2135 ba97b2d7 2024-03-20 stsp static const struct got_error *
2136 ba97b2d7 2024-03-20 stsp render_notification(struct imsg *imsg, struct gotd_imsgev *iev)
2137 ba97b2d7 2024-03-20 stsp {
2138 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
2139 ba97b2d7 2024-03-20 stsp struct gotd_imsg_notification_content ireq;
2140 ba97b2d7 2024-03-20 stsp size_t datalen, len;
2141 c237d891 2024-03-26 op char *refname = NULL;
2142 ba97b2d7 2024-03-20 stsp struct ibuf *wbuf;
2143 c237d891 2024-03-26 op int fd = -1;
2144 ba97b2d7 2024-03-20 stsp
2145 ba97b2d7 2024-03-20 stsp fd = imsg_get_fd(imsg);
2146 ba97b2d7 2024-03-20 stsp if (fd == -1)
2147 ba97b2d7 2024-03-20 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
2148 ba97b2d7 2024-03-20 stsp
2149 ba97b2d7 2024-03-20 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
2150 c237d891 2024-03-26 op if (datalen < sizeof(ireq)) {
2151 c237d891 2024-03-26 op err = got_error(GOT_ERR_PRIVSEP_LEN);
2152 c237d891 2024-03-26 op goto done;
2153 c237d891 2024-03-26 op }
2154 ba97b2d7 2024-03-20 stsp
2155 ba97b2d7 2024-03-20 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
2156 ba97b2d7 2024-03-20 stsp
2157 c237d891 2024-03-26 op if (datalen != sizeof(ireq) + ireq.refname_len) {
2158 c237d891 2024-03-26 op err = got_error(GOT_ERR_PRIVSEP_LEN);
2159 c237d891 2024-03-26 op goto done;
2160 c237d891 2024-03-26 op }
2161 ba97b2d7 2024-03-20 stsp
2162 ba97b2d7 2024-03-20 stsp refname = strndup(imsg->data + sizeof(ireq), ireq.refname_len);
2163 c237d891 2024-03-26 op if (refname == NULL) {
2164 c237d891 2024-03-26 op err = got_error_from_errno("strndup");
2165 c237d891 2024-03-26 op goto done;
2166 c237d891 2024-03-26 op }
2167 ba97b2d7 2024-03-20 stsp
2168 ba97b2d7 2024-03-20 stsp switch (ireq.action) {
2169 ba97b2d7 2024-03-20 stsp case GOTD_NOTIF_ACTION_CREATED:
2170 ba97b2d7 2024-03-20 stsp err = notify_created_ref(refname, ireq.new_id, iev, fd);
2171 ba97b2d7 2024-03-20 stsp break;
2172 ba97b2d7 2024-03-20 stsp case GOTD_NOTIF_ACTION_REMOVED:
2173 ba97b2d7 2024-03-20 stsp err = notify_removed_ref(refname, ireq.old_id, iev, fd);
2174 ba97b2d7 2024-03-20 stsp break;
2175 ba97b2d7 2024-03-20 stsp case GOTD_NOTIF_ACTION_CHANGED:
2176 ba97b2d7 2024-03-20 stsp err = notify_changed_ref(refname, ireq.old_id, ireq.new_id,
2177 ba97b2d7 2024-03-20 stsp iev, fd);
2178 ba97b2d7 2024-03-20 stsp break;
2179 ba97b2d7 2024-03-20 stsp }
2180 cef7eb79 2024-03-26 op if (err != NULL)
2181 cef7eb79 2024-03-26 op goto done;
2182 ba97b2d7 2024-03-20 stsp
2183 ba97b2d7 2024-03-20 stsp if (fsync(fd) == -1) {
2184 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("fsync");
2185 ba97b2d7 2024-03-20 stsp goto done;
2186 ba97b2d7 2024-03-20 stsp }
2187 ba97b2d7 2024-03-20 stsp
2188 ba97b2d7 2024-03-20 stsp len = sizeof(ireq) + ireq.refname_len;
2189 ba97b2d7 2024-03-20 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_NOTIFY, PROC_REPO_WRITE,
2190 ba97b2d7 2024-03-20 stsp repo_write.pid, len);
2191 ba97b2d7 2024-03-20 stsp if (wbuf == NULL) {
2192 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("imsg_create REF");
2193 ba97b2d7 2024-03-20 stsp goto done;
2194 ba97b2d7 2024-03-20 stsp }
2195 ba97b2d7 2024-03-20 stsp if (imsg_add(wbuf, &ireq, sizeof(ireq)) == -1) {
2196 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("imsg_add NOTIFY");
2197 ba97b2d7 2024-03-20 stsp goto done;
2198 ba97b2d7 2024-03-20 stsp }
2199 ba97b2d7 2024-03-20 stsp if (imsg_add(wbuf, refname, ireq.refname_len) == -1) {
2200 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("imsg_add NOTIFY");
2201 ba97b2d7 2024-03-20 stsp goto done;
2202 ba97b2d7 2024-03-20 stsp }
2203 ba97b2d7 2024-03-20 stsp
2204 ba97b2d7 2024-03-20 stsp imsg_close(&iev->ibuf, wbuf);
2205 ba97b2d7 2024-03-20 stsp gotd_imsg_event_add(iev);
2206 ba97b2d7 2024-03-20 stsp done:
2207 ba97b2d7 2024-03-20 stsp free(refname);
2208 c237d891 2024-03-26 op if (fd != -1 && close(fd) == -1 && err == NULL)
2209 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("close");
2210 ba97b2d7 2024-03-20 stsp return err;
2211 ba97b2d7 2024-03-20 stsp }
2212 ba97b2d7 2024-03-20 stsp
2213 13b2bc37 2022-10-23 stsp static void
2214 ae7c1b78 2023-01-10 stsp repo_write_dispatch_session(int fd, short event, void *arg)
2215 13b2bc37 2022-10-23 stsp {
2216 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
2217 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
2218 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
2219 13b2bc37 2022-10-23 stsp struct imsg imsg;
2220 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
2221 13b2bc37 2022-10-23 stsp ssize_t n;
2222 0ff2c315 2023-01-18 stsp int shut = 0, have_packfile = 0;
2223 13b2bc37 2022-10-23 stsp
2224 13b2bc37 2022-10-23 stsp if (event & EV_READ) {
2225 13b2bc37 2022-10-23 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
2226 13b2bc37 2022-10-23 stsp fatal("imsg_read error");
2227 13b2bc37 2022-10-23 stsp if (n == 0) /* Connection closed. */
2228 13b2bc37 2022-10-23 stsp shut = 1;
2229 13b2bc37 2022-10-23 stsp }
2230 13b2bc37 2022-10-23 stsp
2231 13b2bc37 2022-10-23 stsp if (event & EV_WRITE) {
2232 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
2233 13b2bc37 2022-10-23 stsp if (n == -1 && errno != EAGAIN)
2234 13b2bc37 2022-10-23 stsp fatal("msgbuf_write");
2235 13b2bc37 2022-10-23 stsp if (n == 0) /* Connection closed. */
2236 13b2bc37 2022-10-23 stsp shut = 1;
2237 13b2bc37 2022-10-23 stsp }
2238 13b2bc37 2022-10-23 stsp
2239 13b2bc37 2022-10-23 stsp for (;;) {
2240 13b2bc37 2022-10-23 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
2241 13b2bc37 2022-10-23 stsp fatal("%s: imsg_get error", __func__);
2242 13b2bc37 2022-10-23 stsp if (n == 0) /* No more messages. */
2243 13b2bc37 2022-10-23 stsp break;
2244 13b2bc37 2022-10-23 stsp
2245 1a52c9bf 2022-12-30 stsp if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
2246 1cb49b67 2024-03-21 stsp !repo_write.refs_listed) {
2247 1a52c9bf 2022-12-30 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2248 1a52c9bf 2022-12-30 stsp break;
2249 1a52c9bf 2022-12-30 stsp }
2250 1a52c9bf 2022-12-30 stsp
2251 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
2252 13b2bc37 2022-10-23 stsp case GOTD_IMSG_LIST_REFS_INTERNAL:
2253 1a52c9bf 2022-12-30 stsp err = list_refs(&imsg);
2254 13b2bc37 2022-10-23 stsp if (err)
2255 88f6dccd 2023-03-04 op log_warnx("ls-refs: %s", err->msg);
2256 13b2bc37 2022-10-23 stsp break;
2257 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE:
2258 1a52c9bf 2022-12-30 stsp err = recv_ref_update(&imsg);
2259 13b2bc37 2022-10-23 stsp if (err)
2260 88f6dccd 2023-03-04 op log_warnx("ref-update: %s", err->msg);
2261 13b2bc37 2022-10-23 stsp break;
2262 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_PIPE:
2263 1a52c9bf 2022-12-30 stsp err = receive_pack_pipe(&imsg, iev);
2264 13b2bc37 2022-10-23 stsp if (err) {
2265 88f6dccd 2023-03-04 op log_warnx("receiving pack pipe: %s", err->msg);
2266 13b2bc37 2022-10-23 stsp break;
2267 13b2bc37 2022-10-23 stsp }
2268 13b2bc37 2022-10-23 stsp break;
2269 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKIDX_FILE:
2270 1a52c9bf 2022-12-30 stsp err = receive_pack_idx(&imsg, iev);
2271 13b2bc37 2022-10-23 stsp if (err) {
2272 88f6dccd 2023-03-04 op log_warnx("receiving pack index: %s",
2273 88f6dccd 2023-03-04 op err->msg);
2274 13b2bc37 2022-10-23 stsp break;
2275 13b2bc37 2022-10-23 stsp }
2276 13b2bc37 2022-10-23 stsp break;
2277 13b2bc37 2022-10-23 stsp case GOTD_IMSG_RECV_PACKFILE:
2278 9afa3de2 2023-04-04 stsp err = protect_refs_from_deletion();
2279 9afa3de2 2023-04-04 stsp if (err)
2280 9afa3de2 2023-04-04 stsp break;
2281 0ff2c315 2023-01-18 stsp err = recv_packfile(&have_packfile, &imsg);
2282 13b2bc37 2022-10-23 stsp if (err) {
2283 88f6dccd 2023-03-04 op log_warnx("receive packfile: %s", err->msg);
2284 13b2bc37 2022-10-23 stsp break;
2285 13b2bc37 2022-10-23 stsp }
2286 0ff2c315 2023-01-18 stsp if (have_packfile) {
2287 0ff2c315 2023-01-18 stsp err = verify_packfile();
2288 0ff2c315 2023-01-18 stsp if (err) {
2289 88f6dccd 2023-03-04 op log_warnx("verify packfile: %s",
2290 88f6dccd 2023-03-04 op err->msg);
2291 0ff2c315 2023-01-18 stsp break;
2292 0ff2c315 2023-01-18 stsp }
2293 0ff2c315 2023-01-18 stsp err = install_packfile(iev);
2294 0ff2c315 2023-01-18 stsp if (err) {
2295 88f6dccd 2023-03-04 op log_warnx("install packfile: %s",
2296 88f6dccd 2023-03-04 op err->msg);
2297 0ff2c315 2023-01-18 stsp break;
2298 0ff2c315 2023-01-18 stsp }
2299 30a624fb 2024-03-19 stsp /*
2300 30a624fb 2024-03-19 stsp * Ensure we re-read the pack index list
2301 30a624fb 2024-03-19 stsp * upon next access.
2302 30a624fb 2024-03-19 stsp */
2303 30a624fb 2024-03-19 stsp repo_write.repo->pack_path_mtime.tv_sec = 0;
2304 30a624fb 2024-03-19 stsp repo_write.repo->pack_path_mtime.tv_nsec = 0;
2305 13b2bc37 2022-10-23 stsp }
2306 1a52c9bf 2022-12-30 stsp err = update_refs(iev);
2307 13b2bc37 2022-10-23 stsp if (err) {
2308 88f6dccd 2023-03-04 op log_warnx("update refs: %s", err->msg);
2309 ba97b2d7 2024-03-20 stsp }
2310 ba97b2d7 2024-03-20 stsp break;
2311 ba97b2d7 2024-03-20 stsp case GOTD_IMSG_NOTIFY:
2312 ba97b2d7 2024-03-20 stsp err = render_notification(&imsg, iev);
2313 ba97b2d7 2024-03-20 stsp if (err) {
2314 ba97b2d7 2024-03-20 stsp log_warnx("render notification: %s", err->msg);
2315 ba97b2d7 2024-03-20 stsp shut = 1;
2316 13b2bc37 2022-10-23 stsp }
2317 13b2bc37 2022-10-23 stsp break;
2318 ae7c1b78 2023-01-10 stsp default:
2319 88f6dccd 2023-03-04 op log_debug("unexpected imsg %d", imsg.hdr.type);
2320 ae7c1b78 2023-01-10 stsp break;
2321 ae7c1b78 2023-01-10 stsp }
2322 ae7c1b78 2023-01-10 stsp
2323 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
2324 ae7c1b78 2023-01-10 stsp }
2325 ae7c1b78 2023-01-10 stsp
2326 ae7c1b78 2023-01-10 stsp if (!shut && check_cancelled(NULL) == NULL) {
2327 ae7c1b78 2023-01-10 stsp if (err &&
2328 ae7c1b78 2023-01-10 stsp gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
2329 ae7c1b78 2023-01-10 stsp client->id, err) == -1) {
2330 ae7c1b78 2023-01-10 stsp log_warnx("could not send error to parent: %s",
2331 ae7c1b78 2023-01-10 stsp err->msg);
2332 ae7c1b78 2023-01-10 stsp }
2333 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
2334 ae7c1b78 2023-01-10 stsp } else {
2335 ae7c1b78 2023-01-10 stsp /* This pipe is dead. Remove its event handler */
2336 ae7c1b78 2023-01-10 stsp event_del(&iev->ev);
2337 ae7c1b78 2023-01-10 stsp event_loopexit(NULL);
2338 ae7c1b78 2023-01-10 stsp }
2339 ae7c1b78 2023-01-10 stsp }
2340 ae7c1b78 2023-01-10 stsp
2341 ae7c1b78 2023-01-10 stsp static const struct got_error *
2342 ae7c1b78 2023-01-10 stsp recv_connect(struct imsg *imsg)
2343 ae7c1b78 2023-01-10 stsp {
2344 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = &repo_write.session_iev;
2345 ae7c1b78 2023-01-10 stsp size_t datalen;
2346 ae7c1b78 2023-01-10 stsp
2347 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
2348 ae7c1b78 2023-01-10 stsp if (datalen != 0)
2349 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
2350 ae7c1b78 2023-01-10 stsp
2351 ae7c1b78 2023-01-10 stsp if (repo_write.session_fd != -1)
2352 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
2353 ae7c1b78 2023-01-10 stsp
2354 2c52c623 2024-01-30 op repo_write.session_fd = imsg_get_fd(imsg);
2355 2c52c623 2024-01-30 op if (repo_write.session_fd == -1)
2356 2c52c623 2024-01-30 op return got_error(GOT_ERR_PRIVSEP_NO_FD);
2357 ae7c1b78 2023-01-10 stsp
2358 ae7c1b78 2023-01-10 stsp imsg_init(&iev->ibuf, repo_write.session_fd);
2359 ae7c1b78 2023-01-10 stsp iev->handler = repo_write_dispatch_session;
2360 ae7c1b78 2023-01-10 stsp iev->events = EV_READ;
2361 ae7c1b78 2023-01-10 stsp iev->handler_arg = NULL;
2362 ae7c1b78 2023-01-10 stsp event_set(&iev->ev, iev->ibuf.fd, EV_READ,
2363 ae7c1b78 2023-01-10 stsp repo_write_dispatch_session, iev);
2364 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
2365 ae7c1b78 2023-01-10 stsp
2366 ae7c1b78 2023-01-10 stsp return NULL;
2367 ae7c1b78 2023-01-10 stsp }
2368 ae7c1b78 2023-01-10 stsp
2369 ae7c1b78 2023-01-10 stsp static void
2370 ae7c1b78 2023-01-10 stsp repo_write_dispatch(int fd, short event, void *arg)
2371 ae7c1b78 2023-01-10 stsp {
2372 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
2373 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = arg;
2374 ae7c1b78 2023-01-10 stsp struct imsgbuf *ibuf = &iev->ibuf;
2375 ae7c1b78 2023-01-10 stsp struct imsg imsg;
2376 ae7c1b78 2023-01-10 stsp ssize_t n;
2377 ae7c1b78 2023-01-10 stsp int shut = 0;
2378 ae7c1b78 2023-01-10 stsp struct repo_write_client *client = &repo_write_client;
2379 ae7c1b78 2023-01-10 stsp
2380 ae7c1b78 2023-01-10 stsp if (event & EV_READ) {
2381 ae7c1b78 2023-01-10 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
2382 ae7c1b78 2023-01-10 stsp fatal("imsg_read error");
2383 ae7c1b78 2023-01-10 stsp if (n == 0) /* Connection closed. */
2384 1a52c9bf 2022-12-30 stsp shut = 1;
2385 ae7c1b78 2023-01-10 stsp }
2386 ae7c1b78 2023-01-10 stsp
2387 ae7c1b78 2023-01-10 stsp if (event & EV_WRITE) {
2388 ae7c1b78 2023-01-10 stsp n = msgbuf_write(&ibuf->w);
2389 ae7c1b78 2023-01-10 stsp if (n == -1 && errno != EAGAIN)
2390 ae7c1b78 2023-01-10 stsp fatal("msgbuf_write");
2391 ae7c1b78 2023-01-10 stsp if (n == 0) /* Connection closed. */
2392 ae7c1b78 2023-01-10 stsp shut = 1;
2393 ae7c1b78 2023-01-10 stsp }
2394 ae7c1b78 2023-01-10 stsp
2395 ae7c1b78 2023-01-10 stsp while (err == NULL && check_cancelled(NULL) == NULL) {
2396 ae7c1b78 2023-01-10 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
2397 ae7c1b78 2023-01-10 stsp fatal("%s: imsg_get", __func__);
2398 ae7c1b78 2023-01-10 stsp if (n == 0) /* No more messages. */
2399 13b2bc37 2022-10-23 stsp break;
2400 ae7c1b78 2023-01-10 stsp
2401 ae7c1b78 2023-01-10 stsp switch (imsg.hdr.type) {
2402 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CONNECT_REPO_CHILD:
2403 ae7c1b78 2023-01-10 stsp err = recv_connect(&imsg);
2404 ae7c1b78 2023-01-10 stsp break;
2405 13b2bc37 2022-10-23 stsp default:
2406 88f6dccd 2023-03-04 op log_debug("unexpected imsg %d", imsg.hdr.type);
2407 13b2bc37 2022-10-23 stsp break;
2408 13b2bc37 2022-10-23 stsp }
2409 13b2bc37 2022-10-23 stsp
2410 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
2411 13b2bc37 2022-10-23 stsp }
2412 13b2bc37 2022-10-23 stsp
2413 13b2bc37 2022-10-23 stsp if (!shut && check_cancelled(NULL) == NULL) {
2414 13b2bc37 2022-10-23 stsp if (err &&
2415 13b2bc37 2022-10-23 stsp gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
2416 1a52c9bf 2022-12-30 stsp client->id, err) == -1) {
2417 13b2bc37 2022-10-23 stsp log_warnx("could not send error to parent: %s",
2418 13b2bc37 2022-10-23 stsp err->msg);
2419 13b2bc37 2022-10-23 stsp }
2420 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
2421 13b2bc37 2022-10-23 stsp } else {
2422 13b2bc37 2022-10-23 stsp /* This pipe is dead. Remove its event handler */
2423 13b2bc37 2022-10-23 stsp event_del(&iev->ev);
2424 13b2bc37 2022-10-23 stsp event_loopexit(NULL);
2425 13b2bc37 2022-10-23 stsp }
2426 13b2bc37 2022-10-23 stsp }
2427 13b2bc37 2022-10-23 stsp
2428 13b2bc37 2022-10-23 stsp void
2429 eec68231 2022-12-14 stsp repo_write_main(const char *title, const char *repo_path,
2430 9afa3de2 2023-04-04 stsp int *pack_fds, int *temp_fds,
2431 ba97b2d7 2024-03-20 stsp FILE *diff_f1, FILE *diff_f2, int diff_fd1, int diff_fd2,
2432 9afa3de2 2023-04-04 stsp struct got_pathlist_head *protected_tag_namespaces,
2433 9afa3de2 2023-04-04 stsp struct got_pathlist_head *protected_branch_namespaces,
2434 9afa3de2 2023-04-04 stsp struct got_pathlist_head *protected_branches)
2435 13b2bc37 2022-10-23 stsp {
2436 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
2437 363c6230 2023-02-09 stsp struct repo_write_client *client = &repo_write_client;
2438 13b2bc37 2022-10-23 stsp struct gotd_imsgev iev;
2439 13b2bc37 2022-10-23 stsp
2440 363c6230 2023-02-09 stsp client->fd = -1;
2441 363c6230 2023-02-09 stsp client->pack_pipe = -1;
2442 363c6230 2023-02-09 stsp client->packidx_fd = -1;
2443 363c6230 2023-02-09 stsp client->pack.fd = -1;
2444 363c6230 2023-02-09 stsp
2445 13b2bc37 2022-10-23 stsp repo_write.title = title;
2446 13b2bc37 2022-10-23 stsp repo_write.pid = getpid();
2447 13b2bc37 2022-10-23 stsp repo_write.pack_fds = pack_fds;
2448 13b2bc37 2022-10-23 stsp repo_write.temp_fds = temp_fds;
2449 ae7c1b78 2023-01-10 stsp repo_write.session_fd = -1;
2450 ae7c1b78 2023-01-10 stsp repo_write.session_iev.ibuf.fd = -1;
2451 9afa3de2 2023-04-04 stsp repo_write.protected_tag_namespaces = protected_tag_namespaces;
2452 9afa3de2 2023-04-04 stsp repo_write.protected_branch_namespaces = protected_branch_namespaces;
2453 9afa3de2 2023-04-04 stsp repo_write.protected_branches = protected_branches;
2454 ba97b2d7 2024-03-20 stsp repo_write.diff.f1 = diff_f1;
2455 ba97b2d7 2024-03-20 stsp repo_write.diff.f2 = diff_f2;
2456 ba97b2d7 2024-03-20 stsp repo_write.diff.fd1 = diff_fd1;
2457 ba97b2d7 2024-03-20 stsp repo_write.diff.fd2 = diff_fd2;
2458 13b2bc37 2022-10-23 stsp
2459 1a52c9bf 2022-12-30 stsp STAILQ_INIT(&repo_write_client.ref_updates);
2460 13b2bc37 2022-10-23 stsp
2461 eec68231 2022-12-14 stsp err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
2462 13b2bc37 2022-10-23 stsp if (err)
2463 13b2bc37 2022-10-23 stsp goto done;
2464 13b2bc37 2022-10-23 stsp if (!got_repo_is_bare(repo_write.repo)) {
2465 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
2466 13b2bc37 2022-10-23 stsp "bare git repository required");
2467 13b2bc37 2022-10-23 stsp goto done;
2468 13b2bc37 2022-10-23 stsp }
2469 13b2bc37 2022-10-23 stsp
2470 13b2bc37 2022-10-23 stsp got_repo_temp_fds_set(repo_write.repo, temp_fds);
2471 13b2bc37 2022-10-23 stsp
2472 13b2bc37 2022-10-23 stsp signal(SIGINT, catch_sigint);
2473 13b2bc37 2022-10-23 stsp signal(SIGTERM, catch_sigterm);
2474 13b2bc37 2022-10-23 stsp signal(SIGPIPE, SIG_IGN);
2475 13b2bc37 2022-10-23 stsp signal(SIGHUP, SIG_IGN);
2476 13b2bc37 2022-10-23 stsp
2477 8c6fc146 2022-11-17 stsp imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
2478 13b2bc37 2022-10-23 stsp iev.handler = repo_write_dispatch;
2479 13b2bc37 2022-10-23 stsp iev.events = EV_READ;
2480 13b2bc37 2022-10-23 stsp iev.handler_arg = NULL;
2481 13b2bc37 2022-10-23 stsp event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
2482 b50a2b46 2022-12-29 stsp if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
2483 b50a2b46 2022-12-29 stsp PROC_REPO_WRITE, -1, NULL, 0) == -1) {
2484 b50a2b46 2022-12-29 stsp err = got_error_from_errno("imsg compose REPO_CHILD_READY");
2485 13b2bc37 2022-10-23 stsp goto done;
2486 13b2bc37 2022-10-23 stsp }
2487 13b2bc37 2022-10-23 stsp
2488 13b2bc37 2022-10-23 stsp event_dispatch();
2489 13b2bc37 2022-10-23 stsp done:
2490 ba97b2d7 2024-03-20 stsp if (fclose(diff_f1) == EOF && err == NULL)
2491 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("fclose");
2492 ba97b2d7 2024-03-20 stsp if (fclose(diff_f2) == EOF && err == NULL)
2493 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("fclose");
2494 ba97b2d7 2024-03-20 stsp if (close(diff_fd1) == -1 && err == NULL)
2495 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("close");
2496 ba97b2d7 2024-03-20 stsp if (close(diff_fd2) == -1 && err == NULL)
2497 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("close");
2498 13b2bc37 2022-10-23 stsp if (err)
2499 13b2bc37 2022-10-23 stsp log_warnx("%s: %s", title, err->msg);
2500 13b2bc37 2022-10-23 stsp repo_write_shutdown();
2501 13b2bc37 2022-10-23 stsp }
2502 13b2bc37 2022-10-23 stsp
2503 13b2bc37 2022-10-23 stsp void
2504 13b2bc37 2022-10-23 stsp repo_write_shutdown(void)
2505 13b2bc37 2022-10-23 stsp {
2506 363c6230 2023-02-09 stsp struct repo_write_client *client = &repo_write_client;
2507 363c6230 2023-02-09 stsp struct gotd_ref_update *ref_update;
2508 363c6230 2023-02-09 stsp
2509 e8d451cc 2024-03-22 stsp log_debug("%s: shutting down", repo_write.title);
2510 363c6230 2023-02-09 stsp
2511 363c6230 2023-02-09 stsp while (!STAILQ_EMPTY(&client->ref_updates)) {
2512 363c6230 2023-02-09 stsp ref_update = STAILQ_FIRST(&client->ref_updates);
2513 363c6230 2023-02-09 stsp STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
2514 363c6230 2023-02-09 stsp got_ref_close(ref_update->ref);
2515 363c6230 2023-02-09 stsp free(ref_update);
2516 363c6230 2023-02-09 stsp }
2517 363c6230 2023-02-09 stsp
2518 363c6230 2023-02-09 stsp got_pack_close(&client->pack);
2519 363c6230 2023-02-09 stsp if (client->fd != -1)
2520 363c6230 2023-02-09 stsp close(client->fd);
2521 363c6230 2023-02-09 stsp if (client->pack_pipe != -1)
2522 363c6230 2023-02-09 stsp close(client->pack_pipe);
2523 363c6230 2023-02-09 stsp if (client->packidx_fd != -1)
2524 363c6230 2023-02-09 stsp close(client->packidx_fd);
2525 363c6230 2023-02-09 stsp
2526 13b2bc37 2022-10-23 stsp if (repo_write.repo)
2527 13b2bc37 2022-10-23 stsp got_repo_close(repo_write.repo);
2528 13b2bc37 2022-10-23 stsp got_repo_pack_fds_close(repo_write.pack_fds);
2529 8193d041 2022-10-30 stsp got_repo_temp_fds_close(repo_write.temp_fds);
2530 ae7c1b78 2023-01-10 stsp if (repo_write.session_fd != -1)
2531 ae7c1b78 2023-01-10 stsp close(repo_write.session_fd);
2532 13b2bc37 2022-10-23 stsp exit(0);
2533 13b2bc37 2022-10-23 stsp }