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