2 13b2bc37 2022-10-23 stsp * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
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>
38 13b2bc37 2022-10-23 stsp #include "buf.h"
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"
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"
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"
67 13b2bc37 2022-10-23 stsp #ifndef nitems
68 13b2bc37 2022-10-23 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 13b2bc37 2022-10-23 stsp static struct repo_write {
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;
88 1cb49b67 2024-03-21 stsp int refs_listed;
89 13b2bc37 2022-10-23 stsp } repo_write;
91 13b2bc37 2022-10-23 stsp struct gotd_ref_update {
92 13b2bc37 2022-10-23 stsp STAILQ_ENTRY(gotd_ref_update) entry;
93 13b2bc37 2022-10-23 stsp struct got_reference *ref;
94 13b2bc37 2022-10-23 stsp int ref_is_new;
95 9a8e357c 2023-01-28 op int delete_ref;
96 13b2bc37 2022-10-23 stsp struct got_object_id old_id;
97 13b2bc37 2022-10-23 stsp struct got_object_id new_id;
99 13b2bc37 2022-10-23 stsp STAILQ_HEAD(gotd_ref_updates, gotd_ref_update);
101 1a52c9bf 2022-12-30 stsp static struct repo_write_client {
102 13b2bc37 2022-10-23 stsp uint32_t id;
104 7fec5f4a 2022-10-28 stsp int pack_pipe;
105 13b2bc37 2022-10-23 stsp struct got_pack pack;
106 13b2bc37 2022-10-23 stsp uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
107 13b2bc37 2022-10-23 stsp int packidx_fd;
108 13b2bc37 2022-10-23 stsp struct gotd_ref_updates ref_updates;
109 13b2bc37 2022-10-23 stsp int nref_updates;
110 9a8e357c 2023-01-28 op int nref_del;
111 0ff2c315 2023-01-18 stsp int nref_new;
112 cc88020e 2023-04-11 stsp int nref_move;
113 1a52c9bf 2022-12-30 stsp } repo_write_client;
115 13b2bc37 2022-10-23 stsp static volatile sig_atomic_t sigint_received;
116 13b2bc37 2022-10-23 stsp static volatile sig_atomic_t sigterm_received;
118 13b2bc37 2022-10-23 stsp static void
119 13b2bc37 2022-10-23 stsp catch_sigint(int signo)
121 13b2bc37 2022-10-23 stsp sigint_received = 1;
124 13b2bc37 2022-10-23 stsp static void
125 13b2bc37 2022-10-23 stsp catch_sigterm(int signo)
127 13b2bc37 2022-10-23 stsp sigterm_received = 1;
130 13b2bc37 2022-10-23 stsp static const struct got_error *
131 13b2bc37 2022-10-23 stsp check_cancelled(void *arg)
133 13b2bc37 2022-10-23 stsp if (sigint_received || sigterm_received)
134 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_CANCELLED);
136 13b2bc37 2022-10-23 stsp return NULL;
139 13b2bc37 2022-10-23 stsp static const struct got_error *
140 13b2bc37 2022-10-23 stsp send_peeled_tag_ref(struct got_reference *ref, struct got_object *obj,
141 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf)
143 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
144 13b2bc37 2022-10-23 stsp struct got_tag_object *tag;
145 13b2bc37 2022-10-23 stsp size_t namelen, len;
146 13b2bc37 2022-10-23 stsp char *peeled_refname = NULL;
147 13b2bc37 2022-10-23 stsp struct got_object_id *id;
148 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
150 13b2bc37 2022-10-23 stsp err = got_object_tag_open(&tag, repo_write.repo, obj);
152 13b2bc37 2022-10-23 stsp return err;
154 13b2bc37 2022-10-23 stsp if (asprintf(&peeled_refname, "%s^{}", got_ref_get_name(ref)) == -1) {
155 13b2bc37 2022-10-23 stsp err = got_error_from_errno("asprintf");
159 13b2bc37 2022-10-23 stsp id = got_object_tag_get_object_id(tag);
160 13b2bc37 2022-10-23 stsp namelen = strlen(peeled_refname);
162 13b2bc37 2022-10-23 stsp len = sizeof(struct gotd_imsg_ref) + namelen;
163 13b2bc37 2022-10-23 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
164 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_NO_SPACE);
168 13b2bc37 2022-10-23 stsp wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
169 13b2bc37 2022-10-23 stsp repo_write.pid, len);
170 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
171 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create REF");
175 13b2bc37 2022-10-23 stsp /* Keep in sync with struct gotd_imsg_ref definition. */
176 aaa34732 2024-07-13 op if (imsg_add(wbuf, id->hash, SHA1_DIGEST_LENGTH) == -1) {
177 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add REF");
180 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1) {
181 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add REF");
184 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, peeled_refname, namelen) == -1) {
185 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add REF");
189 13b2bc37 2022-10-23 stsp imsg_close(ibuf, wbuf);
191 13b2bc37 2022-10-23 stsp got_object_tag_close(tag);
192 13b2bc37 2022-10-23 stsp return err;
195 13b2bc37 2022-10-23 stsp static const struct got_error *
196 13b2bc37 2022-10-23 stsp send_ref(struct got_reference *ref, struct imsgbuf *ibuf)
198 13b2bc37 2022-10-23 stsp const struct got_error *err;
199 13b2bc37 2022-10-23 stsp const char *refname = got_ref_get_name(ref);
200 13b2bc37 2022-10-23 stsp size_t namelen;
201 13b2bc37 2022-10-23 stsp struct got_object_id *id = NULL;
202 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
203 13b2bc37 2022-10-23 stsp size_t len;
204 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
206 13b2bc37 2022-10-23 stsp namelen = strlen(refname);
208 13b2bc37 2022-10-23 stsp len = sizeof(struct gotd_imsg_ref) + namelen;
209 13b2bc37 2022-10-23 stsp if (len > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
210 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
212 13b2bc37 2022-10-23 stsp err = got_ref_resolve(&id, repo_write.repo, ref);
214 13b2bc37 2022-10-23 stsp return err;
216 13b2bc37 2022-10-23 stsp wbuf = imsg_create(ibuf, GOTD_IMSG_REF, PROC_REPO_WRITE,
217 13b2bc37 2022-10-23 stsp repo_write.pid, len);
218 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
219 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create REF");
223 13b2bc37 2022-10-23 stsp /* Keep in sync with struct gotd_imsg_ref definition. */
224 aaa34732 2024-07-13 op if (imsg_add(wbuf, id->hash, SHA1_DIGEST_LENGTH) == -1)
225 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF");
226 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &namelen, sizeof(namelen)) == -1)
227 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF");
228 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, refname, namelen) == -1)
229 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF");
231 13b2bc37 2022-10-23 stsp imsg_close(ibuf, wbuf);
233 13b2bc37 2022-10-23 stsp err = got_object_open(&obj, repo_write.repo, id);
236 13b2bc37 2022-10-23 stsp if (obj->type == GOT_OBJ_TYPE_TAG)
237 13b2bc37 2022-10-23 stsp err = send_peeled_tag_ref(ref, obj, ibuf);
240 13b2bc37 2022-10-23 stsp got_object_close(obj);
242 13b2bc37 2022-10-23 stsp return err;
245 13b2bc37 2022-10-23 stsp static const struct got_error *
246 1a52c9bf 2022-12-30 stsp list_refs(struct imsg *imsg)
248 13b2bc37 2022-10-23 stsp const struct got_error *err;
249 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
250 13b2bc37 2022-10-23 stsp struct got_reflist_head refs;
251 13b2bc37 2022-10-23 stsp struct got_reflist_entry *re;
252 13b2bc37 2022-10-23 stsp size_t datalen;
253 13b2bc37 2022-10-23 stsp struct gotd_imsg_reflist irefs;
254 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
255 2c52c623 2024-01-30 op int client_fd;
257 13b2bc37 2022-10-23 stsp TAILQ_INIT(&refs);
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);
263 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
264 1cb49b67 2024-03-21 stsp if (datalen != 0)
265 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
267 1cb49b67 2024-03-21 stsp if (repo_write.refs_listed) {
268 1a52c9bf 2022-12-30 stsp return got_error_msg(GOT_ERR_CLIENT_ID,
269 1a52c9bf 2022-12-30 stsp "duplicate list-refs request");
271 1cb49b67 2024-03-21 stsp repo_write.refs_listed = 1;
273 1a52c9bf 2022-12-30 stsp client->fd = client_fd;
274 1a52c9bf 2022-12-30 stsp client->nref_updates = 0;
275 9a8e357c 2023-01-28 op client->nref_del = 0;
276 0ff2c315 2023-01-18 stsp client->nref_new = 0;
277 cc88020e 2023-04-11 stsp client->nref_move = 0;
279 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, client_fd);
281 13b2bc37 2022-10-23 stsp err = got_ref_list(&refs, repo_write.repo, "",
282 13b2bc37 2022-10-23 stsp got_ref_cmp_by_name, NULL);
284 13b2bc37 2022-10-23 stsp return err;
286 13b2bc37 2022-10-23 stsp memset(&irefs, 0, sizeof(irefs));
287 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(re, &refs, entry) {
288 13b2bc37 2022-10-23 stsp struct got_object_id *id;
289 13b2bc37 2022-10-23 stsp int obj_type;
291 13b2bc37 2022-10-23 stsp if (got_ref_is_symbolic(re->ref))
294 13b2bc37 2022-10-23 stsp irefs.nrefs++;
296 13b2bc37 2022-10-23 stsp /* Account for a peeled tag refs. */
297 13b2bc37 2022-10-23 stsp err = got_ref_resolve(&id, repo_write.repo, re->ref);
300 e26970cc 2023-01-10 op err = got_object_get_type(&obj_type, repo_write.repo, id);
304 13b2bc37 2022-10-23 stsp if (obj_type == GOT_OBJ_TYPE_TAG)
305 13b2bc37 2022-10-23 stsp irefs.nrefs++;
308 13b2bc37 2022-10-23 stsp if (imsg_compose(&ibuf, GOTD_IMSG_REFLIST, PROC_REPO_WRITE,
309 13b2bc37 2022-10-23 stsp repo_write.pid, -1, &irefs, sizeof(irefs)) == -1) {
310 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_compose REFLIST");
314 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(re, &refs, entry) {
315 13b2bc37 2022-10-23 stsp if (got_ref_is_symbolic(re->ref))
317 13b2bc37 2022-10-23 stsp err = send_ref(re->ref, &ibuf);
322 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(&ibuf);
324 13b2bc37 2022-10-23 stsp got_ref_list_free(&refs);
325 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
326 13b2bc37 2022-10-23 stsp return err;
329 13b2bc37 2022-10-23 stsp static const struct got_error *
330 9afa3de2 2023-04-04 stsp validate_namespace(const char *namespace)
332 13b2bc37 2022-10-23 stsp size_t len = strlen(namespace);
334 13b2bc37 2022-10-23 stsp if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
335 13b2bc37 2022-10-23 stsp namespace[len -1] != '/') {
336 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_REF_NAME,
337 13b2bc37 2022-10-23 stsp "reference namespace '%s'", namespace);
340 9afa3de2 2023-04-04 stsp return NULL;
343 9afa3de2 2023-04-04 stsp static const struct got_error *
344 9afa3de2 2023-04-04 stsp protect_ref_namespace(const char *refname, const char *namespace)
346 9afa3de2 2023-04-04 stsp const struct got_error *err;
348 9afa3de2 2023-04-04 stsp err = validate_namespace(namespace);
350 9afa3de2 2023-04-04 stsp return err;
352 9afa3de2 2023-04-04 stsp if (strncmp(namespace, refname, strlen(namespace)) == 0)
353 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
355 13b2bc37 2022-10-23 stsp return NULL;
358 13b2bc37 2022-10-23 stsp static const struct got_error *
359 9afa3de2 2023-04-04 stsp verify_object_type(struct got_object_id *id, int expected_obj_type,
360 9afa3de2 2023-04-04 stsp struct got_pack *pack, struct got_packidx *packidx)
362 9afa3de2 2023-04-04 stsp const struct got_error *err;
363 9afa3de2 2023-04-04 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
364 9afa3de2 2023-04-04 stsp struct got_object *obj;
366 9afa3de2 2023-04-04 stsp const char *typestr;
368 9afa3de2 2023-04-04 stsp idx = got_packidx_get_object_idx(packidx, id);
369 9afa3de2 2023-04-04 stsp if (idx == -1) {
370 310a8ea5 2024-07-10 op got_object_id_hex(id, hex, sizeof(hex));
371 9afa3de2 2023-04-04 stsp return got_error_fmt(GOT_ERR_BAD_PACKFILE,
372 9afa3de2 2023-04-04 stsp "object %s is missing from pack file", hex);
375 9afa3de2 2023-04-04 stsp err = got_object_open_from_packfile(&obj, id, pack, packidx,
376 9afa3de2 2023-04-04 stsp idx, repo_write.repo);
378 9afa3de2 2023-04-04 stsp return err;
380 9afa3de2 2023-04-04 stsp if (obj->type != expected_obj_type) {
381 310a8ea5 2024-07-10 op got_object_id_hex(id, hex, sizeof(hex));
382 9afa3de2 2023-04-04 stsp got_object_type_label(&typestr, expected_obj_type);
383 9afa3de2 2023-04-04 stsp err = got_error_fmt(GOT_ERR_OBJ_TYPE,
384 9afa3de2 2023-04-04 stsp "%s is not pointing at a %s object", hex, typestr);
386 9afa3de2 2023-04-04 stsp got_object_close(obj);
387 9afa3de2 2023-04-04 stsp return err;
390 9afa3de2 2023-04-04 stsp static const struct got_error *
391 9afa3de2 2023-04-04 stsp protect_tag_namespace(const char *namespace, struct got_pack *pack,
392 9afa3de2 2023-04-04 stsp struct got_packidx *packidx, struct gotd_ref_update *ref_update)
394 9afa3de2 2023-04-04 stsp const struct got_error *err;
396 9afa3de2 2023-04-04 stsp err = validate_namespace(namespace);
398 9afa3de2 2023-04-04 stsp return err;
400 9afa3de2 2023-04-04 stsp if (strncmp(namespace, got_ref_get_name(ref_update->ref),
401 9afa3de2 2023-04-04 stsp strlen(namespace)) != 0)
402 9afa3de2 2023-04-04 stsp return NULL;
404 9afa3de2 2023-04-04 stsp if (!ref_update->ref_is_new)
405 9afa3de2 2023-04-04 stsp return got_error_fmt(GOT_ERR_REFS_PROTECTED, "%s", namespace);
407 9afa3de2 2023-04-04 stsp return verify_object_type(&ref_update->new_id, GOT_OBJ_TYPE_TAG,
408 9afa3de2 2023-04-04 stsp pack, packidx);
411 9afa3de2 2023-04-04 stsp static const struct got_error *
412 9afa3de2 2023-04-04 stsp protect_require_yca(struct got_object_id *tip_id,
413 9afa3de2 2023-04-04 stsp size_t max_commits_to_traverse, struct got_pack *pack,
414 9afa3de2 2023-04-04 stsp struct got_packidx *packidx, struct got_reference *ref)
416 9afa3de2 2023-04-04 stsp const struct got_error *err;
417 9afa3de2 2023-04-04 stsp uint8_t *buf = NULL;
418 9afa3de2 2023-04-04 stsp size_t len;
419 9afa3de2 2023-04-04 stsp struct got_object_id *expected_yca_id = NULL;
420 9afa3de2 2023-04-04 stsp struct got_object *obj = NULL;
421 9afa3de2 2023-04-04 stsp struct got_commit_object *commit = NULL;
422 9afa3de2 2023-04-04 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
423 9afa3de2 2023-04-04 stsp const struct got_object_id_queue *parent_ids;
424 9afa3de2 2023-04-04 stsp struct got_object_id_queue ids;
425 9afa3de2 2023-04-04 stsp struct got_object_qid *pid, *qid;
426 9afa3de2 2023-04-04 stsp struct got_object_idset *traversed_set = NULL;
427 9afa3de2 2023-04-04 stsp int found_yca = 0, obj_type;
429 9afa3de2 2023-04-04 stsp STAILQ_INIT(&ids);
431 9afa3de2 2023-04-04 stsp err = got_ref_resolve(&expected_yca_id, repo_write.repo, ref);
433 9afa3de2 2023-04-04 stsp return err;
435 9afa3de2 2023-04-04 stsp err = got_object_get_type(&obj_type, repo_write.repo, expected_yca_id);
439 9afa3de2 2023-04-04 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
440 310a8ea5 2024-07-10 op got_object_id_hex(expected_yca_id, hex, sizeof(hex));
441 9afa3de2 2023-04-04 stsp err = got_error_fmt(GOT_ERR_OBJ_TYPE,
442 9afa3de2 2023-04-04 stsp "%s is not pointing at a commit object", hex);
446 9afa3de2 2023-04-04 stsp traversed_set = got_object_idset_alloc();
447 9afa3de2 2023-04-04 stsp if (traversed_set == NULL) {
448 9afa3de2 2023-04-04 stsp err = got_error_from_errno("got_object_idset_alloc");
452 9afa3de2 2023-04-04 stsp err = got_object_qid_alloc(&qid, tip_id);
455 9afa3de2 2023-04-04 stsp STAILQ_INSERT_TAIL(&ids, qid, entry);
456 9afa3de2 2023-04-04 stsp while (!STAILQ_EMPTY(&ids)) {
457 9afa3de2 2023-04-04 stsp err = check_cancelled(NULL);
461 9afa3de2 2023-04-04 stsp qid = STAILQ_FIRST(&ids);
462 9afa3de2 2023-04-04 stsp if (got_object_id_cmp(&qid->id, expected_yca_id) == 0) {
463 9afa3de2 2023-04-04 stsp found_yca = 1;
467 9afa3de2 2023-04-04 stsp if (got_object_idset_num_elements(traversed_set) >=
468 9afa3de2 2023-04-04 stsp max_commits_to_traverse)
471 9afa3de2 2023-04-04 stsp if (got_object_idset_contains(traversed_set, &qid->id)) {
472 9afa3de2 2023-04-04 stsp STAILQ_REMOVE_HEAD(&ids, entry);
473 9afa3de2 2023-04-04 stsp got_object_qid_free(qid);
474 9afa3de2 2023-04-04 stsp qid = NULL;
477 9afa3de2 2023-04-04 stsp err = got_object_idset_add(traversed_set, &qid->id, NULL);
481 9afa3de2 2023-04-04 stsp err = got_object_open(&obj, repo_write.repo, &qid->id);
482 9afa3de2 2023-04-04 stsp if (err && err->code != GOT_ERR_NO_OBJ)
484 9afa3de2 2023-04-04 stsp err = NULL;
486 9afa3de2 2023-04-04 stsp err = got_object_commit_open(&commit, repo_write.repo,
493 9afa3de2 2023-04-04 stsp idx = got_packidx_get_object_idx(packidx, &qid->id);
494 9afa3de2 2023-04-04 stsp if (idx == -1) {
495 310a8ea5 2024-07-10 op got_object_id_hex(&qid->id, hex, sizeof(hex));
496 9afa3de2 2023-04-04 stsp err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
497 9afa3de2 2023-04-04 stsp "object %s is missing from pack file", hex);
501 9afa3de2 2023-04-04 stsp err = got_object_open_from_packfile(&obj, &qid->id,
502 9afa3de2 2023-04-04 stsp pack, packidx, idx, repo_write.repo);
506 9afa3de2 2023-04-04 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
507 310a8ea5 2024-07-10 op got_object_id_hex(&qid->id, hex, sizeof(hex));
508 9afa3de2 2023-04-04 stsp err = got_error_fmt(GOT_ERR_OBJ_TYPE,
509 9afa3de2 2023-04-04 stsp "%s is not pointing at a commit object",
514 9afa3de2 2023-04-04 stsp err = got_packfile_extract_object_to_mem(&buf, &len,
515 9afa3de2 2023-04-04 stsp obj, pack);
519 8fdd8178 2024-07-12 op err = got_object_parse_commit(&commit, buf, len,
520 8fdd8178 2024-07-12 op GOT_HASH_SHA1);
525 9afa3de2 2023-04-04 stsp buf = NULL;
528 9afa3de2 2023-04-04 stsp got_object_close(obj);
529 9afa3de2 2023-04-04 stsp obj = NULL;
531 9afa3de2 2023-04-04 stsp STAILQ_REMOVE_HEAD(&ids, entry);
532 9afa3de2 2023-04-04 stsp got_object_qid_free(qid);
533 9afa3de2 2023-04-04 stsp qid = NULL;
535 9afa3de2 2023-04-04 stsp if (got_object_commit_get_nparents(commit) == 0)
538 9afa3de2 2023-04-04 stsp parent_ids = got_object_commit_get_parent_ids(commit);
539 9afa3de2 2023-04-04 stsp STAILQ_FOREACH(pid, parent_ids, entry) {
540 9afa3de2 2023-04-04 stsp err = check_cancelled(NULL);
543 9afa3de2 2023-04-04 stsp err = got_object_qid_alloc(&qid, &pid->id);
546 9afa3de2 2023-04-04 stsp STAILQ_INSERT_TAIL(&ids, qid, entry);
547 9afa3de2 2023-04-04 stsp qid = NULL;
549 9afa3de2 2023-04-04 stsp got_object_commit_close(commit);
550 9afa3de2 2023-04-04 stsp commit = NULL;
553 9afa3de2 2023-04-04 stsp if (!found_yca) {
554 9afa3de2 2023-04-04 stsp err = got_error_fmt(GOT_ERR_REF_PROTECTED, "%s",
555 9afa3de2 2023-04-04 stsp got_ref_get_name(ref));
558 9afa3de2 2023-04-04 stsp got_object_idset_free(traversed_set);
559 9afa3de2 2023-04-04 stsp got_object_id_queue_free(&ids);
562 9afa3de2 2023-04-04 stsp got_object_close(obj);
563 9afa3de2 2023-04-04 stsp if (commit)
564 9afa3de2 2023-04-04 stsp got_object_commit_close(commit);
565 9afa3de2 2023-04-04 stsp free(expected_yca_id);
566 9afa3de2 2023-04-04 stsp return err;
569 9afa3de2 2023-04-04 stsp static const struct got_error *
570 9afa3de2 2023-04-04 stsp protect_branch_namespace(const char *namespace, struct got_pack *pack,
571 9afa3de2 2023-04-04 stsp struct got_packidx *packidx, struct gotd_ref_update *ref_update)
573 9afa3de2 2023-04-04 stsp const struct got_error *err;
575 9afa3de2 2023-04-04 stsp err = validate_namespace(namespace);
577 9afa3de2 2023-04-04 stsp return err;
579 9afa3de2 2023-04-04 stsp if (strncmp(namespace, got_ref_get_name(ref_update->ref),
580 9afa3de2 2023-04-04 stsp strlen(namespace)) != 0)
581 9afa3de2 2023-04-04 stsp return NULL;
583 9afa3de2 2023-04-04 stsp if (ref_update->ref_is_new) {
584 9afa3de2 2023-04-04 stsp return verify_object_type(&ref_update->new_id,
585 9afa3de2 2023-04-04 stsp GOT_OBJ_TYPE_COMMIT, pack, packidx);
588 9afa3de2 2023-04-04 stsp return protect_require_yca(&ref_update->new_id,
589 9afa3de2 2023-04-04 stsp be32toh(packidx->hdr.fanout_table[0xff]), pack, packidx,
590 9afa3de2 2023-04-04 stsp ref_update->ref);
593 9afa3de2 2023-04-04 stsp static const struct got_error *
594 9afa3de2 2023-04-04 stsp protect_branch(const char *refname, struct got_pack *pack,
595 9afa3de2 2023-04-04 stsp struct got_packidx *packidx, struct gotd_ref_update *ref_update)
597 9afa3de2 2023-04-04 stsp if (strcmp(refname, got_ref_get_name(ref_update->ref)) != 0)
598 9afa3de2 2023-04-04 stsp return NULL;
600 9afa3de2 2023-04-04 stsp /* Always allow new branches to be created. */
601 9afa3de2 2023-04-04 stsp if (ref_update->ref_is_new) {
602 9afa3de2 2023-04-04 stsp return verify_object_type(&ref_update->new_id,
603 9afa3de2 2023-04-04 stsp GOT_OBJ_TYPE_COMMIT, pack, packidx);
606 9afa3de2 2023-04-04 stsp return protect_require_yca(&ref_update->new_id,
607 9afa3de2 2023-04-04 stsp be32toh(packidx->hdr.fanout_table[0xff]), pack, packidx,
608 9afa3de2 2023-04-04 stsp ref_update->ref);
611 9afa3de2 2023-04-04 stsp static const struct got_error *
612 1a52c9bf 2022-12-30 stsp recv_ref_update(struct imsg *imsg)
614 9a8e357c 2023-01-28 op static const char zero_id[SHA1_DIGEST_LENGTH];
615 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
616 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
617 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update iref;
618 13b2bc37 2022-10-23 stsp size_t datalen;
619 13b2bc37 2022-10-23 stsp char *refname = NULL;
620 13b2bc37 2022-10-23 stsp struct got_reference *ref = NULL;
621 13b2bc37 2022-10-23 stsp struct got_object_id *id = NULL;
622 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
623 13b2bc37 2022-10-23 stsp struct gotd_ref_update *ref_update = NULL;
625 13b2bc37 2022-10-23 stsp log_debug("ref-update received");
627 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
628 13b2bc37 2022-10-23 stsp if (datalen < sizeof(iref))
629 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
630 13b2bc37 2022-10-23 stsp memcpy(&iref, imsg->data, sizeof(iref));
631 13b2bc37 2022-10-23 stsp if (datalen != sizeof(iref) + iref.name_len)
632 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
634 1a52c9bf 2022-12-30 stsp imsg_init(&ibuf, client->fd);
636 00b3e9ae 2023-01-11 op refname = strndup(imsg->data + sizeof(iref), iref.name_len);
637 13b2bc37 2022-10-23 stsp if (refname == NULL)
638 00b3e9ae 2023-01-11 op return got_error_from_errno("strndup");
640 13b2bc37 2022-10-23 stsp ref_update = calloc(1, sizeof(*ref_update));
641 13b2bc37 2022-10-23 stsp if (ref_update == NULL) {
642 13b2bc37 2022-10-23 stsp err = got_error_from_errno("malloc");
646 aaa34732 2024-07-13 op memcpy(ref_update->old_id.hash, iref.old_id, SHA1_DIGEST_LENGTH);
647 aaa34732 2024-07-13 op memcpy(ref_update->new_id.hash, iref.new_id, SHA1_DIGEST_LENGTH);
649 13b2bc37 2022-10-23 stsp err = got_ref_open(&ref, repo_write.repo, refname, 0);
651 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_NOT_REF)
653 aaa34732 2024-07-13 op if (memcmp(ref_update->new_id.hash,
654 9afa3de2 2023-04-04 stsp zero_id, sizeof(zero_id)) == 0) {
655 9afa3de2 2023-04-04 stsp err = got_error_fmt(GOT_ERR_BAD_OBJ_ID,
656 9afa3de2 2023-04-04 stsp "%s", refname);
659 13b2bc37 2022-10-23 stsp err = got_ref_alloc(&ref, refname, &ref_update->new_id);
662 13b2bc37 2022-10-23 stsp ref_update->ref_is_new = 1;
663 0ff2c315 2023-01-18 stsp client->nref_new++;
665 13b2bc37 2022-10-23 stsp if (got_ref_is_symbolic(ref)) {
666 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
667 13b2bc37 2022-10-23 stsp "'%s' is a symbolic reference and cannot "
668 13b2bc37 2022-10-23 stsp "be updated", got_ref_get_name(ref));
671 13b2bc37 2022-10-23 stsp if (strncmp("refs/", got_ref_get_name(ref), 5) != 0) {
672 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_BAD_REF_NAME,
673 13b2bc37 2022-10-23 stsp "%s: does not begin with 'refs/'",
674 13b2bc37 2022-10-23 stsp got_ref_get_name(ref));
678 9afa3de2 2023-04-04 stsp err = protect_ref_namespace(got_ref_get_name(ref), "refs/got/");
681 9afa3de2 2023-04-04 stsp err = protect_ref_namespace(got_ref_get_name(ref), "refs/remotes/");
685 13b2bc37 2022-10-23 stsp if (!ref_update->ref_is_new) {
687 13b2bc37 2022-10-23 stsp * Ensure the client's idea of this update is still valid.
688 13b2bc37 2022-10-23 stsp * At this point we can only return an error, to prevent
689 13b2bc37 2022-10-23 stsp * the client from uploading a pack file which will likely
690 13b2bc37 2022-10-23 stsp * have to be discarded.
692 13b2bc37 2022-10-23 stsp err = got_ref_resolve(&id, repo_write.repo, ref);
696 13b2bc37 2022-10-23 stsp if (got_object_id_cmp(id, &ref_update->old_id) != 0) {
697 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
698 13b2bc37 2022-10-23 stsp "%s has been modified by someone else "
699 13b2bc37 2022-10-23 stsp "while transaction was in progress",
700 13b2bc37 2022-10-23 stsp got_ref_get_name(ref));
705 13b2bc37 2022-10-23 stsp gotd_imsg_send_ack(&ref_update->new_id, &ibuf, PROC_REPO_WRITE,
706 13b2bc37 2022-10-23 stsp repo_write.pid);
708 13b2bc37 2022-10-23 stsp ref_update->ref = ref;
709 aaa34732 2024-07-13 op if (memcmp(ref_update->new_id.hash, zero_id, sizeof(zero_id)) == 0) {
710 9a8e357c 2023-01-28 op ref_update->delete_ref = 1;
711 9a8e357c 2023-01-28 op client->nref_del++;
713 1a52c9bf 2022-12-30 stsp STAILQ_INSERT_HEAD(&client->ref_updates, ref_update, entry);
714 1a52c9bf 2022-12-30 stsp client->nref_updates++;
715 13b2bc37 2022-10-23 stsp ref = NULL;
716 13b2bc37 2022-10-23 stsp ref_update = NULL;
719 13b2bc37 2022-10-23 stsp got_ref_close(ref);
720 13b2bc37 2022-10-23 stsp free(ref_update);
721 13b2bc37 2022-10-23 stsp free(refname);
723 13b2bc37 2022-10-23 stsp return err;
726 13b2bc37 2022-10-23 stsp static const struct got_error *
727 13b2bc37 2022-10-23 stsp pack_index_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
728 13b2bc37 2022-10-23 stsp uint32_t nobj_loose, uint32_t nobj_resolved)
730 13b2bc37 2022-10-23 stsp int p_indexed = 0, p_resolved = 0;
731 13b2bc37 2022-10-23 stsp int nobj_delta = nobj_total - nobj_loose;
733 13b2bc37 2022-10-23 stsp if (nobj_total > 0)
734 13b2bc37 2022-10-23 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
736 13b2bc37 2022-10-23 stsp if (nobj_delta > 0)
737 13b2bc37 2022-10-23 stsp p_resolved = (nobj_resolved * 100) / nobj_delta;
739 13b2bc37 2022-10-23 stsp if (p_resolved > 0) {
740 13b2bc37 2022-10-23 stsp log_debug("indexing %d objects %d%%; resolving %d deltas %d%%",
741 13b2bc37 2022-10-23 stsp nobj_total, p_indexed, nobj_delta, p_resolved);
743 13b2bc37 2022-10-23 stsp log_debug("indexing %d objects %d%%", nobj_total, p_indexed);
745 13b2bc37 2022-10-23 stsp return NULL;
748 13b2bc37 2022-10-23 stsp static const struct got_error *
749 13b2bc37 2022-10-23 stsp read_more_pack_stream(int infd, BUF *buf, size_t minsize)
751 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
752 13b2bc37 2022-10-23 stsp uint8_t readahead[65536];
753 13b2bc37 2022-10-23 stsp size_t have, newlen;
755 13b2bc37 2022-10-23 stsp err = got_poll_read_full(infd, &have,
756 13b2bc37 2022-10-23 stsp readahead, sizeof(readahead), minsize);
758 13b2bc37 2022-10-23 stsp return err;
760 13b2bc37 2022-10-23 stsp err = buf_append(&newlen, buf, readahead, have);
762 13b2bc37 2022-10-23 stsp return err;
766 13b2bc37 2022-10-23 stsp static const struct got_error *
767 13b2bc37 2022-10-23 stsp copy_object_type_and_size(uint8_t *type, uint64_t *size, int infd, int outfd,
768 ae25a666 2023-02-23 op off_t *outsize, BUF *buf, size_t *buf_pos, struct got_hash *ctx)
770 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
771 13b2bc37 2022-10-23 stsp uint8_t t = 0;
772 13b2bc37 2022-10-23 stsp uint64_t s = 0;
773 13b2bc37 2022-10-23 stsp uint8_t sizebuf[8];
774 13b2bc37 2022-10-23 stsp size_t i = 0;
775 13b2bc37 2022-10-23 stsp off_t obj_offset = *outsize;
778 13b2bc37 2022-10-23 stsp /* We do not support size values which don't fit in 64 bit. */
780 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
781 22ec3416 2022-10-25 op "packfile offset %lld", (long long)obj_offset);
783 13b2bc37 2022-10-23 stsp if (buf_len(buf) - *buf_pos < sizeof(sizebuf[0])) {
784 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
785 13b2bc37 2022-10-23 stsp sizeof(sizebuf[0]));
787 13b2bc37 2022-10-23 stsp return err;
790 13b2bc37 2022-10-23 stsp sizebuf[i] = buf_getc(buf, *buf_pos);
791 13b2bc37 2022-10-23 stsp *buf_pos += sizeof(sizebuf[i]);
793 13b2bc37 2022-10-23 stsp if (i == 0) {
794 13b2bc37 2022-10-23 stsp t = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
795 13b2bc37 2022-10-23 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
796 13b2bc37 2022-10-23 stsp s = (sizebuf[i] & GOT_PACK_OBJ_SIZE0_VAL_MASK);
798 13b2bc37 2022-10-23 stsp size_t shift = 4 + 7 * (i - 1);
799 13b2bc37 2022-10-23 stsp s |= ((sizebuf[i] & GOT_PACK_OBJ_SIZE_VAL_MASK) <<
803 13b2bc37 2022-10-23 stsp } while (sizebuf[i - 1] & GOT_PACK_OBJ_SIZE_MORE);
805 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, sizebuf, i, ctx);
807 13b2bc37 2022-10-23 stsp return err;
808 13b2bc37 2022-10-23 stsp *outsize += i;
812 13b2bc37 2022-10-23 stsp return NULL;
815 13b2bc37 2022-10-23 stsp static const struct got_error *
816 13b2bc37 2022-10-23 stsp copy_ref_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
817 ae25a666 2023-02-23 op struct got_hash *ctx)
819 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
820 13b2bc37 2022-10-23 stsp size_t remain = buf_len(buf) - *buf_pos;
822 13b2bc37 2022-10-23 stsp if (remain < SHA1_DIGEST_LENGTH) {
823 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
824 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH - remain);
826 13b2bc37 2022-10-23 stsp return err;
829 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
830 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH, ctx);
832 13b2bc37 2022-10-23 stsp return err;
834 13b2bc37 2022-10-23 stsp *buf_pos += SHA1_DIGEST_LENGTH;
835 13b2bc37 2022-10-23 stsp return NULL;
838 13b2bc37 2022-10-23 stsp static const struct got_error *
839 13b2bc37 2022-10-23 stsp copy_offset_delta(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
840 ae25a666 2023-02-23 op struct got_hash *ctx)
842 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
843 13b2bc37 2022-10-23 stsp uint64_t o = 0;
844 13b2bc37 2022-10-23 stsp uint8_t offbuf[8];
845 13b2bc37 2022-10-23 stsp size_t i = 0;
846 13b2bc37 2022-10-23 stsp off_t obj_offset = *outsize;
849 13b2bc37 2022-10-23 stsp /* We do not support offset values which don't fit in 64 bit. */
851 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
852 22ec3416 2022-10-25 op "packfile offset %lld", (long long)obj_offset);
854 13b2bc37 2022-10-23 stsp if (buf_len(buf) - *buf_pos < sizeof(offbuf[0])) {
855 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
856 13b2bc37 2022-10-23 stsp sizeof(offbuf[0]));
858 13b2bc37 2022-10-23 stsp return err;
861 13b2bc37 2022-10-23 stsp offbuf[i] = buf_getc(buf, *buf_pos);
862 13b2bc37 2022-10-23 stsp *buf_pos += sizeof(offbuf[i]);
864 13b2bc37 2022-10-23 stsp if (i == 0)
865 13b2bc37 2022-10-23 stsp o = (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
869 13b2bc37 2022-10-23 stsp o += (offbuf[i] & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
872 13b2bc37 2022-10-23 stsp } while (offbuf[i - 1] & GOT_PACK_OBJ_DELTA_OFF_MORE);
874 13b2bc37 2022-10-23 stsp if (o < sizeof(struct got_packfile_hdr) || o > *outsize)
875 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PACK_OFFSET);
877 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, offbuf, i, ctx);
879 13b2bc37 2022-10-23 stsp return err;
881 13b2bc37 2022-10-23 stsp *outsize += i;
882 13b2bc37 2022-10-23 stsp return NULL;
885 13b2bc37 2022-10-23 stsp static const struct got_error *
886 13b2bc37 2022-10-23 stsp copy_zstream(int infd, int outfd, off_t *outsize, BUF *buf, size_t *buf_pos,
887 ae25a666 2023-02-23 op struct got_hash *ctx)
889 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
890 13b2bc37 2022-10-23 stsp z_stream z;
892 13b2bc37 2022-10-23 stsp char voidbuf[1024];
893 13b2bc37 2022-10-23 stsp size_t consumed_total = 0;
894 13b2bc37 2022-10-23 stsp off_t zstream_offset = *outsize;
896 13b2bc37 2022-10-23 stsp memset(&z, 0, sizeof(z));
898 13b2bc37 2022-10-23 stsp z.zalloc = Z_NULL;
899 13b2bc37 2022-10-23 stsp z.zfree = Z_NULL;
900 13b2bc37 2022-10-23 stsp zret = inflateInit(&z);
901 13b2bc37 2022-10-23 stsp if (zret != Z_OK) {
902 13b2bc37 2022-10-23 stsp if (zret == Z_ERRNO)
903 13b2bc37 2022-10-23 stsp return got_error_from_errno("inflateInit");
904 13b2bc37 2022-10-23 stsp if (zret == Z_MEM_ERROR) {
905 13b2bc37 2022-10-23 stsp errno = ENOMEM;
906 13b2bc37 2022-10-23 stsp return got_error_from_errno("inflateInit");
908 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_DECOMPRESSION,
909 13b2bc37 2022-10-23 stsp "inflateInit failed");
912 13b2bc37 2022-10-23 stsp while (zret != Z_STREAM_END) {
913 13b2bc37 2022-10-23 stsp size_t last_total_in, consumed;
916 13b2bc37 2022-10-23 stsp * Decompress into the void. Object data will be parsed
917 13b2bc37 2022-10-23 stsp * later, when the pack file is indexed. For now, we just
918 13b2bc37 2022-10-23 stsp * want to locate the end of the compressed stream.
920 13b2bc37 2022-10-23 stsp while (zret != Z_STREAM_END && buf_len(buf) - *buf_pos > 0) {
921 13b2bc37 2022-10-23 stsp last_total_in = z.total_in;
922 13b2bc37 2022-10-23 stsp z.next_in = buf_get(buf) + *buf_pos;
923 13b2bc37 2022-10-23 stsp z.avail_in = buf_len(buf) - *buf_pos;
924 13b2bc37 2022-10-23 stsp z.next_out = voidbuf;
925 13b2bc37 2022-10-23 stsp z.avail_out = sizeof(voidbuf);
927 13b2bc37 2022-10-23 stsp zret = inflate(&z, Z_SYNC_FLUSH);
928 13b2bc37 2022-10-23 stsp if (zret != Z_OK && zret != Z_BUF_ERROR &&
929 13b2bc37 2022-10-23 stsp zret != Z_STREAM_END) {
930 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_DECOMPRESSION,
931 22ec3416 2022-10-25 op "packfile offset %lld",
932 22ec3416 2022-10-25 op (long long)zstream_offset);
935 13b2bc37 2022-10-23 stsp consumed = z.total_in - last_total_in;
937 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, buf_get(buf) + *buf_pos,
938 13b2bc37 2022-10-23 stsp consumed, ctx);
942 13b2bc37 2022-10-23 stsp err = buf_discard(buf, *buf_pos + consumed);
945 13b2bc37 2022-10-23 stsp *buf_pos = 0;
947 13b2bc37 2022-10-23 stsp consumed_total += consumed;
950 13b2bc37 2022-10-23 stsp if (zret != Z_STREAM_END) {
951 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf, 1);
957 13b2bc37 2022-10-23 stsp if (err == NULL)
958 13b2bc37 2022-10-23 stsp *outsize += consumed_total;
960 13b2bc37 2022-10-23 stsp inflateEnd(&z);
961 13b2bc37 2022-10-23 stsp return err;
964 13b2bc37 2022-10-23 stsp static const struct got_error *
965 13b2bc37 2022-10-23 stsp validate_object_type(int obj_type)
967 13b2bc37 2022-10-23 stsp switch (obj_type) {
968 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_BLOB:
969 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_COMMIT:
970 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_TREE:
971 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_TAG:
972 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
973 13b2bc37 2022-10-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
974 13b2bc37 2022-10-23 stsp return NULL;
979 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_OBJ_TYPE);
982 13b2bc37 2022-10-23 stsp static const struct got_error *
983 cc88020e 2023-04-11 stsp ensure_all_objects_exist_locally(struct gotd_ref_updates *ref_updates)
985 cc88020e 2023-04-11 stsp const struct got_error *err = NULL;
986 cc88020e 2023-04-11 stsp struct gotd_ref_update *ref_update;
987 cc88020e 2023-04-11 stsp struct got_object *obj;
989 cc88020e 2023-04-11 stsp STAILQ_FOREACH(ref_update, ref_updates, entry) {
990 cc88020e 2023-04-11 stsp err = got_object_open(&obj, repo_write.repo,
991 cc88020e 2023-04-11 stsp &ref_update->new_id);
993 cc88020e 2023-04-11 stsp return err;
994 cc88020e 2023-04-11 stsp got_object_close(obj);
997 cc88020e 2023-04-11 stsp return NULL;
1000 cc88020e 2023-04-11 stsp static const struct got_error *
1001 0ff2c315 2023-01-18 stsp recv_packdata(off_t *outsize, uint32_t *nobj, uint8_t *sha1,
1002 0ff2c315 2023-01-18 stsp int infd, int outfd)
1004 13b2bc37 2022-10-23 stsp const struct got_error *err;
1005 0ff2c315 2023-01-18 stsp struct repo_write_client *client = &repo_write_client;
1006 13b2bc37 2022-10-23 stsp struct got_packfile_hdr hdr;
1007 13b2bc37 2022-10-23 stsp size_t have;
1008 0ff2c315 2023-01-18 stsp uint32_t nhave = 0;
1009 ae25a666 2023-02-23 op struct got_hash ctx;
1010 13b2bc37 2022-10-23 stsp uint8_t expected_sha1[SHA1_DIGEST_LENGTH];
1011 13b2bc37 2022-10-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
1012 13b2bc37 2022-10-23 stsp BUF *buf = NULL;
1013 13b2bc37 2022-10-23 stsp size_t buf_pos = 0, remain;
1014 13b2bc37 2022-10-23 stsp ssize_t w;
1016 13b2bc37 2022-10-23 stsp *outsize = 0;
1017 0ff2c315 2023-01-18 stsp *nobj = 0;
1019 9a8e357c 2023-01-28 op /* if only deleting references there's nothing to read */
1020 9a8e357c 2023-01-28 op if (client->nref_updates == client->nref_del)
1021 9a8e357c 2023-01-28 op return NULL;
1023 ae25a666 2023-02-23 op got_hash_init(&ctx, GOT_HASH_SHA1);
1025 13b2bc37 2022-10-23 stsp err = got_poll_read_full(infd, &have, &hdr, sizeof(hdr), sizeof(hdr));
1027 13b2bc37 2022-10-23 stsp return err;
1028 13b2bc37 2022-10-23 stsp if (have != sizeof(hdr))
1029 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE, "short pack file");
1030 13b2bc37 2022-10-23 stsp *outsize += have;
1032 13b2bc37 2022-10-23 stsp if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
1033 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
1034 13b2bc37 2022-10-23 stsp "bad packfile signature");
1035 13b2bc37 2022-10-23 stsp if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
1036 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
1037 13b2bc37 2022-10-23 stsp "bad packfile version");
1039 0ff2c315 2023-01-18 stsp *nobj = be32toh(hdr.nobjects);
1040 0ff2c315 2023-01-18 stsp if (*nobj == 0) {
1042 0ff2c315 2023-01-18 stsp * Clients which are creating new references only
1043 0ff2c315 2023-01-18 stsp * will send us an empty pack file.
1045 0ff2c315 2023-01-18 stsp if (client->nref_updates > 0 &&
1046 0ff2c315 2023-01-18 stsp client->nref_updates == client->nref_new)
1047 0ff2c315 2023-01-18 stsp return NULL;
1050 cc88020e 2023-04-11 stsp * Clients which only move existing refs will send us an empty
1051 cc88020e 2023-04-11 stsp * pack file. All referenced objects must exist locally.
1053 cc88020e 2023-04-11 stsp err = ensure_all_objects_exist_locally(&client->ref_updates);
1054 cc88020e 2023-04-11 stsp if (err) {
1055 cc88020e 2023-04-11 stsp if (err->code != GOT_ERR_NO_OBJ)
1056 cc88020e 2023-04-11 stsp return err;
1057 cc88020e 2023-04-11 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
1058 cc88020e 2023-04-11 stsp "bad packfile with zero objects");
1061 cc88020e 2023-04-11 stsp client->nref_move = client->nref_updates;
1062 cc88020e 2023-04-11 stsp return NULL;
1065 0ff2c315 2023-01-18 stsp log_debug("expecting %d objects", *nobj);
1067 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(outfd, &hdr, sizeof(hdr), &ctx);
1069 13b2bc37 2022-10-23 stsp return err;
1071 13b2bc37 2022-10-23 stsp err = buf_alloc(&buf, 65536);
1073 13b2bc37 2022-10-23 stsp return err;
1075 0ff2c315 2023-01-18 stsp while (nhave != *nobj) {
1076 13b2bc37 2022-10-23 stsp uint8_t obj_type;
1077 13b2bc37 2022-10-23 stsp uint64_t obj_size;
1079 13b2bc37 2022-10-23 stsp err = copy_object_type_and_size(&obj_type, &obj_size,
1080 13b2bc37 2022-10-23 stsp infd, outfd, outsize, buf, &buf_pos, &ctx);
1082 13b2bc37 2022-10-23 stsp goto done;
1084 13b2bc37 2022-10-23 stsp err = validate_object_type(obj_type);
1086 13b2bc37 2022-10-23 stsp goto done;
1088 13b2bc37 2022-10-23 stsp if (obj_type == GOT_OBJ_TYPE_REF_DELTA) {
1089 13b2bc37 2022-10-23 stsp err = copy_ref_delta(infd, outfd, outsize,
1090 13b2bc37 2022-10-23 stsp buf, &buf_pos, &ctx);
1092 13b2bc37 2022-10-23 stsp goto done;
1093 13b2bc37 2022-10-23 stsp } else if (obj_type == GOT_OBJ_TYPE_OFFSET_DELTA) {
1094 13b2bc37 2022-10-23 stsp err = copy_offset_delta(infd, outfd, outsize,
1095 13b2bc37 2022-10-23 stsp buf, &buf_pos, &ctx);
1097 13b2bc37 2022-10-23 stsp goto done;
1100 13b2bc37 2022-10-23 stsp err = copy_zstream(infd, outfd, outsize, buf, &buf_pos, &ctx);
1102 13b2bc37 2022-10-23 stsp goto done;
1107 0ff2c315 2023-01-18 stsp log_debug("received %u objects", *nobj);
1109 ae25a666 2023-02-23 op got_hash_final(&ctx, expected_sha1);
1111 13b2bc37 2022-10-23 stsp remain = buf_len(buf) - buf_pos;
1112 13b2bc37 2022-10-23 stsp if (remain < SHA1_DIGEST_LENGTH) {
1113 13b2bc37 2022-10-23 stsp err = read_more_pack_stream(infd, buf,
1114 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH - remain);
1116 13b2bc37 2022-10-23 stsp return err;
1119 13b2bc37 2022-10-23 stsp got_sha1_digest_to_str(expected_sha1, hex, sizeof(hex));
1120 13b2bc37 2022-10-23 stsp log_debug("expect SHA1: %s", hex);
1121 13b2bc37 2022-10-23 stsp got_sha1_digest_to_str(buf_get(buf) + buf_pos, hex, sizeof(hex));
1122 13b2bc37 2022-10-23 stsp log_debug("actual SHA1: %s", hex);
1124 13b2bc37 2022-10-23 stsp if (memcmp(buf_get(buf) + buf_pos, expected_sha1,
1125 13b2bc37 2022-10-23 stsp SHA1_DIGEST_LENGTH) != 0) {
1126 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PACKFILE_CSUM);
1127 13b2bc37 2022-10-23 stsp goto done;
1130 13b2bc37 2022-10-23 stsp memcpy(sha1, expected_sha1, SHA1_DIGEST_LENGTH);
1132 13b2bc37 2022-10-23 stsp w = write(outfd, expected_sha1, SHA1_DIGEST_LENGTH);
1133 13b2bc37 2022-10-23 stsp if (w == -1) {
1134 13b2bc37 2022-10-23 stsp err = got_error_from_errno("write");
1135 13b2bc37 2022-10-23 stsp goto done;
1137 13b2bc37 2022-10-23 stsp if (w != SHA1_DIGEST_LENGTH) {
1138 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_IO);
1139 13b2bc37 2022-10-23 stsp goto done;
1142 13b2bc37 2022-10-23 stsp *outsize += SHA1_DIGEST_LENGTH;
1144 13b2bc37 2022-10-23 stsp if (fsync(outfd) == -1) {
1145 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fsync");
1146 13b2bc37 2022-10-23 stsp goto done;
1148 13b2bc37 2022-10-23 stsp if (lseek(outfd, 0L, SEEK_SET) == -1) {
1149 13b2bc37 2022-10-23 stsp err = got_error_from_errno("lseek");
1150 13b2bc37 2022-10-23 stsp goto done;
1153 13b2bc37 2022-10-23 stsp buf_free(buf);
1154 13b2bc37 2022-10-23 stsp return err;
1157 13b2bc37 2022-10-23 stsp static const struct got_error *
1158 1a52c9bf 2022-12-30 stsp report_pack_status(const struct got_error *unpack_err)
1160 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1161 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1162 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_status istatus;
1163 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
1164 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
1165 13b2bc37 2022-10-23 stsp const char *unpack_ok = "unpack ok\n";
1166 13b2bc37 2022-10-23 stsp size_t len;
1168 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, client->fd);
1170 13b2bc37 2022-10-23 stsp if (unpack_err)
1171 13b2bc37 2022-10-23 stsp istatus.reason_len = strlen(unpack_err->msg);
1173 13b2bc37 2022-10-23 stsp istatus.reason_len = strlen(unpack_ok);
1175 13b2bc37 2022-10-23 stsp len = sizeof(istatus) + istatus.reason_len;
1176 13b2bc37 2022-10-23 stsp wbuf = imsg_create(&ibuf, GOTD_IMSG_PACKFILE_STATUS, PROC_REPO_WRITE,
1177 13b2bc37 2022-10-23 stsp repo_write.pid, len);
1178 13b2bc37 2022-10-23 stsp if (wbuf == NULL) {
1179 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_create PACKFILE_STATUS");
1180 13b2bc37 2022-10-23 stsp goto done;
1183 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &istatus, sizeof(istatus)) == -1) {
1184 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add PACKFILE_STATUS");
1185 13b2bc37 2022-10-23 stsp goto done;
1188 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, err ? err->msg : unpack_ok,
1189 13b2bc37 2022-10-23 stsp istatus.reason_len) == -1) {
1190 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_add PACKFILE_STATUS");
1191 13b2bc37 2022-10-23 stsp goto done;
1194 13b2bc37 2022-10-23 stsp imsg_close(&ibuf, wbuf);
1196 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(&ibuf);
1198 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
1199 13b2bc37 2022-10-23 stsp return err;
1202 13b2bc37 2022-10-23 stsp static const struct got_error *
1203 0ff2c315 2023-01-18 stsp recv_packfile(int *have_packfile, struct imsg *imsg)
1205 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL, *unpack_err;
1206 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1207 13b2bc37 2022-10-23 stsp struct gotd_imsg_recv_packfile ireq;
1208 394b0db7 2024-07-16 op struct got_object_id id;
1209 13b2bc37 2022-10-23 stsp FILE *tempfiles[3] = { NULL, NULL, NULL };
1210 13b2bc37 2022-10-23 stsp struct repo_tempfile {
1213 13b2bc37 2022-10-23 stsp } repo_tempfiles[3] = { { - 1, - 1 }, { - 1, - 1 }, { - 1, - 1 }, };
1215 13b2bc37 2022-10-23 stsp size_t datalen;
1216 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
1217 13b2bc37 2022-10-23 stsp struct got_ratelimit rl;
1218 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
1219 13b2bc37 2022-10-23 stsp off_t pack_filesize = 0;
1220 0ff2c315 2023-01-18 stsp uint32_t nobj = 0;
1222 13b2bc37 2022-10-23 stsp log_debug("packfile request received");
1224 0ff2c315 2023-01-18 stsp *have_packfile = 0;
1225 13b2bc37 2022-10-23 stsp got_ratelimit_init(&rl, 2, 0);
1227 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1228 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
1229 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1230 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
1232 1a52c9bf 2022-12-30 stsp if (client->pack_pipe == -1 || client->packidx_fd == -1)
1233 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1235 1a52c9bf 2022-12-30 stsp imsg_init(&ibuf, client->fd);
1237 1a52c9bf 2022-12-30 stsp pack = &client->pack;
1238 13b2bc37 2022-10-23 stsp memset(pack, 0, sizeof(*pack));
1239 2c52c623 2024-01-30 op pack->fd = imsg_get_fd(imsg);
1240 2c52c623 2024-01-30 op if (pack->fd == -1)
1241 2c52c623 2024-01-30 op return got_error(GOT_ERR_PRIVSEP_NO_FD);
1243 13b2bc37 2022-10-23 stsp err = got_delta_cache_alloc(&pack->delta_cache);
1245 13b2bc37 2022-10-23 stsp return err;
1247 13b2bc37 2022-10-23 stsp for (i = 0; i < nitems(repo_tempfiles); i++) {
1248 13b2bc37 2022-10-23 stsp struct repo_tempfile *t = &repo_tempfiles[i];
1249 13b2bc37 2022-10-23 stsp err = got_repo_temp_fds_get(&t->fd, &t->idx, repo_write.repo);
1251 13b2bc37 2022-10-23 stsp goto done;
1254 13b2bc37 2022-10-23 stsp for (i = 0; i < nitems(tempfiles); i++) {
1258 e294dc4e 2023-02-03 mark fd = dup(repo_tempfiles[i].fd);
1259 13b2bc37 2022-10-23 stsp if (fd == -1) {
1260 13b2bc37 2022-10-23 stsp err = got_error_from_errno("dup");
1261 13b2bc37 2022-10-23 stsp goto done;
1263 13b2bc37 2022-10-23 stsp f = fdopen(fd, "w+");
1264 13b2bc37 2022-10-23 stsp if (f == NULL) {
1265 e294dc4e 2023-02-03 mark err = got_error_from_errno("fdopen");
1266 13b2bc37 2022-10-23 stsp close(fd);
1267 13b2bc37 2022-10-23 stsp goto done;
1269 13b2bc37 2022-10-23 stsp tempfiles[i] = f;
1272 13b2bc37 2022-10-23 stsp err = gotd_imsg_flush(&ibuf);
1274 13b2bc37 2022-10-23 stsp goto done;
1276 13b2bc37 2022-10-23 stsp log_debug("receiving pack data");
1277 0ff2c315 2023-01-18 stsp unpack_err = recv_packdata(&pack_filesize, &nobj,
1278 0ff2c315 2023-01-18 stsp client->pack_sha1, client->pack_pipe, pack->fd);
1279 13b2bc37 2022-10-23 stsp if (ireq.report_status) {
1280 1a52c9bf 2022-12-30 stsp err = report_pack_status(unpack_err);
1281 13b2bc37 2022-10-23 stsp if (err) {
1282 13b2bc37 2022-10-23 stsp /* Git clients hang up after sending the pack file. */
1283 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_EOF)
1284 13b2bc37 2022-10-23 stsp err = NULL;
1287 13b2bc37 2022-10-23 stsp if (unpack_err)
1288 13b2bc37 2022-10-23 stsp err = unpack_err;
1290 13b2bc37 2022-10-23 stsp goto done;
1292 13b2bc37 2022-10-23 stsp log_debug("pack data received");
1295 0ff2c315 2023-01-18 stsp * Clients which are creating new references only will
1296 0ff2c315 2023-01-18 stsp * send us an empty pack file.
1298 0ff2c315 2023-01-18 stsp if (nobj == 0 &&
1299 0ff2c315 2023-01-18 stsp pack_filesize == sizeof(struct got_packfile_hdr) &&
1300 0ff2c315 2023-01-18 stsp client->nref_updates > 0 &&
1301 0ff2c315 2023-01-18 stsp client->nref_updates == client->nref_new)
1302 0ff2c315 2023-01-18 stsp goto done;
1305 9a8e357c 2023-01-28 op * Clients which are deleting references only will send
1306 9a8e357c 2023-01-28 op * no pack file.
1308 9a8e357c 2023-01-28 op if (nobj == 0 &&
1309 9a8e357c 2023-01-28 op client->nref_del > 0 &&
1310 9a8e357c 2023-01-28 op client->nref_updates == client->nref_del)
1311 cc88020e 2023-04-11 stsp goto done;
1314 cc88020e 2023-04-11 stsp * Clients which only move existing refs will send us an empty
1315 cc88020e 2023-04-11 stsp * pack file. All referenced objects must exist locally.
1317 cc88020e 2023-04-11 stsp if (nobj == 0 &&
1318 cc88020e 2023-04-11 stsp pack_filesize == sizeof(struct got_packfile_hdr) &&
1319 cc88020e 2023-04-11 stsp client->nref_move > 0 &&
1320 cc88020e 2023-04-11 stsp client->nref_updates == client->nref_move)
1323 13b2bc37 2022-10-23 stsp pack->filesize = pack_filesize;
1324 0ff2c315 2023-01-18 stsp *have_packfile = 1;
1326 394b0db7 2024-07-16 op memset(&id, 0, sizeof(id));
1327 394b0db7 2024-07-16 op memcpy(&id.hash, client->pack_sha1, SHA1_DIGEST_LENGTH);
1328 394b0db7 2024-07-16 op id.algo = GOT_HASH_SHA1;
1330 ad4cc361 2022-10-27 op log_debug("begin indexing pack (%lld bytes in size)",
1331 ad4cc361 2022-10-27 op (long long)pack->filesize);
1332 1a52c9bf 2022-12-30 stsp err = got_pack_index(pack, client->packidx_fd,
1333 394b0db7 2024-07-16 op tempfiles[0], tempfiles[1], tempfiles[2], &id,
1334 13b2bc37 2022-10-23 stsp pack_index_progress, NULL, &rl);
1336 13b2bc37 2022-10-23 stsp goto done;
1337 13b2bc37 2022-10-23 stsp log_debug("done indexing pack");
1339 1a52c9bf 2022-12-30 stsp if (fsync(client->packidx_fd) == -1) {
1340 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fsync");
1341 13b2bc37 2022-10-23 stsp goto done;
1343 1a52c9bf 2022-12-30 stsp if (lseek(client->packidx_fd, 0L, SEEK_SET) == -1)
1344 13b2bc37 2022-10-23 stsp err = got_error_from_errno("lseek");
1346 1a52c9bf 2022-12-30 stsp if (close(client->pack_pipe) == -1 && err == NULL)
1347 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
1348 1a52c9bf 2022-12-30 stsp client->pack_pipe = -1;
1349 13b2bc37 2022-10-23 stsp for (i = 0; i < nitems(repo_tempfiles); i++) {
1350 13b2bc37 2022-10-23 stsp struct repo_tempfile *t = &repo_tempfiles[i];
1351 13b2bc37 2022-10-23 stsp if (t->idx != -1)
1352 13b2bc37 2022-10-23 stsp got_repo_temp_fds_put(t->idx, repo_write.repo);
1354 13b2bc37 2022-10-23 stsp for (i = 0; i < nitems(tempfiles); i++) {
1355 13b2bc37 2022-10-23 stsp if (tempfiles[i] && fclose(tempfiles[i]) == EOF && err == NULL)
1356 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fclose");
1359 13b2bc37 2022-10-23 stsp got_pack_close(pack);
1360 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
1361 13b2bc37 2022-10-23 stsp return err;
1364 13b2bc37 2022-10-23 stsp static const struct got_error *
1365 1a52c9bf 2022-12-30 stsp verify_packfile(void)
1367 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL, *close_err;
1368 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1369 13b2bc37 2022-10-23 stsp struct gotd_ref_update *ref_update;
1370 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
1371 13b2bc37 2022-10-23 stsp struct stat sb;
1372 13b2bc37 2022-10-23 stsp char *id_str = NULL;
1373 9afa3de2 2023-04-04 stsp struct got_object *obj = NULL;
1374 9afa3de2 2023-04-04 stsp struct got_pathlist_entry *pe;
1375 9afa3de2 2023-04-04 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
1377 13b2bc37 2022-10-23 stsp if (STAILQ_EMPTY(&client->ref_updates)) {
1378 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1379 13b2bc37 2022-10-23 stsp "cannot verify pack file without any ref-updates");
1382 13b2bc37 2022-10-23 stsp if (client->pack.fd == -1) {
1383 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1384 13b2bc37 2022-10-23 stsp "invalid pack file handle during pack verification");
1386 13b2bc37 2022-10-23 stsp if (client->packidx_fd == -1) {
1387 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1388 13b2bc37 2022-10-23 stsp "invalid pack index handle during pack verification");
1391 13b2bc37 2022-10-23 stsp if (fstat(client->packidx_fd, &sb) == -1)
1392 13b2bc37 2022-10-23 stsp return got_error_from_errno("pack index fstat");
1394 13b2bc37 2022-10-23 stsp packidx = malloc(sizeof(*packidx));
1395 13b2bc37 2022-10-23 stsp memset(packidx, 0, sizeof(*packidx));
1396 13b2bc37 2022-10-23 stsp packidx->fd = client->packidx_fd;
1397 13b2bc37 2022-10-23 stsp client->packidx_fd = -1;
1398 13b2bc37 2022-10-23 stsp packidx->len = sb.st_size;
1400 13b2bc37 2022-10-23 stsp err = got_packidx_init_hdr(packidx, 1, client->pack.filesize);
1402 13b2bc37 2022-10-23 stsp return err;
1404 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1405 9a8e357c 2023-01-28 op if (ref_update->delete_ref)
1408 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(pe, repo_write.protected_tag_namespaces, entry) {
1409 9afa3de2 2023-04-04 stsp err = protect_tag_namespace(pe->path, &client->pack,
1410 9afa3de2 2023-04-04 stsp packidx, ref_update);
1412 9afa3de2 2023-04-04 stsp goto done;
1416 9afa3de2 2023-04-04 stsp * Objects which already exist in our repository need
1417 9afa3de2 2023-04-04 stsp * not be present in the pack file.
1419 9afa3de2 2023-04-04 stsp err = got_object_open(&obj, repo_write.repo,
1420 9afa3de2 2023-04-04 stsp &ref_update->new_id);
1421 9afa3de2 2023-04-04 stsp if (err && err->code != GOT_ERR_NO_OBJ)
1422 13b2bc37 2022-10-23 stsp goto done;
1423 9afa3de2 2023-04-04 stsp err = NULL;
1424 9afa3de2 2023-04-04 stsp if (obj) {
1425 9afa3de2 2023-04-04 stsp got_object_close(obj);
1426 9afa3de2 2023-04-04 stsp obj = NULL;
1428 9afa3de2 2023-04-04 stsp int idx = got_packidx_get_object_idx(packidx,
1429 9afa3de2 2023-04-04 stsp &ref_update->new_id);
1430 9afa3de2 2023-04-04 stsp if (idx == -1) {
1431 310a8ea5 2024-07-10 op got_object_id_hex(&ref_update->new_id,
1432 9afa3de2 2023-04-04 stsp hex, sizeof(hex));
1433 9afa3de2 2023-04-04 stsp err = got_error_fmt(GOT_ERR_BAD_PACKFILE,
1434 9afa3de2 2023-04-04 stsp "object %s is missing from pack file",
1436 9afa3de2 2023-04-04 stsp goto done;
1440 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(pe, repo_write.protected_branch_namespaces,
1442 9afa3de2 2023-04-04 stsp err = protect_branch_namespace(pe->path,
1443 9afa3de2 2023-04-04 stsp &client->pack, packidx, ref_update);
1445 9afa3de2 2023-04-04 stsp goto done;
1447 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(pe, repo_write.protected_branches, entry) {
1448 9afa3de2 2023-04-04 stsp err = protect_branch(pe->path, &client->pack,
1449 9afa3de2 2023-04-04 stsp packidx, ref_update);
1451 9afa3de2 2023-04-04 stsp goto done;
1456 13b2bc37 2022-10-23 stsp close_err = got_packidx_close(packidx);
1457 13b2bc37 2022-10-23 stsp if (close_err && err == NULL)
1458 13b2bc37 2022-10-23 stsp err = close_err;
1459 13b2bc37 2022-10-23 stsp free(id_str);
1461 9afa3de2 2023-04-04 stsp got_object_close(obj);
1462 13b2bc37 2022-10-23 stsp return err;
1465 13b2bc37 2022-10-23 stsp static const struct got_error *
1466 9afa3de2 2023-04-04 stsp protect_refs_from_deletion(void)
1468 9afa3de2 2023-04-04 stsp const struct got_error *err = NULL;
1469 9afa3de2 2023-04-04 stsp struct repo_write_client *client = &repo_write_client;
1470 9afa3de2 2023-04-04 stsp struct gotd_ref_update *ref_update;
1471 9afa3de2 2023-04-04 stsp struct got_pathlist_entry *pe;
1472 9afa3de2 2023-04-04 stsp const char *refname;
1474 9afa3de2 2023-04-04 stsp STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1475 9afa3de2 2023-04-04 stsp if (!ref_update->delete_ref)
1478 9afa3de2 2023-04-04 stsp refname = got_ref_get_name(ref_update->ref);
1480 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(pe, repo_write.protected_tag_namespaces, entry) {
1481 9afa3de2 2023-04-04 stsp err = protect_ref_namespace(refname, pe->path);
1483 9afa3de2 2023-04-04 stsp return err;
1486 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(pe, repo_write.protected_branch_namespaces,
1488 9afa3de2 2023-04-04 stsp err = protect_ref_namespace(refname, pe->path);
1490 9afa3de2 2023-04-04 stsp return err;
1493 9afa3de2 2023-04-04 stsp TAILQ_FOREACH(pe, repo_write.protected_branches, entry) {
1494 9afa3de2 2023-04-04 stsp if (strcmp(refname, pe->path) == 0) {
1495 9afa3de2 2023-04-04 stsp return got_error_fmt(GOT_ERR_REF_PROTECTED,
1496 9afa3de2 2023-04-04 stsp "%s", refname);
1501 9afa3de2 2023-04-04 stsp return NULL;
1504 9afa3de2 2023-04-04 stsp static const struct got_error *
1505 1a52c9bf 2022-12-30 stsp install_packfile(struct gotd_imsgev *iev)
1507 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1508 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_install inst;
1511 13b2bc37 2022-10-23 stsp memset(&inst, 0, sizeof(inst));
1512 13b2bc37 2022-10-23 stsp memcpy(inst.pack_sha1, client->pack_sha1, SHA1_DIGEST_LENGTH);
1514 13b2bc37 2022-10-23 stsp ret = gotd_imsg_compose_event(iev, GOTD_IMSG_PACKFILE_INSTALL,
1515 13b2bc37 2022-10-23 stsp PROC_REPO_WRITE, -1, &inst, sizeof(inst));
1516 13b2bc37 2022-10-23 stsp if (ret == -1)
1517 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_compose PACKFILE_INSTALL");
1519 13b2bc37 2022-10-23 stsp return NULL;
1522 13b2bc37 2022-10-23 stsp static const struct got_error *
1523 1a52c9bf 2022-12-30 stsp send_ref_updates_start(int nref_updates, struct gotd_imsgev *iev)
1525 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_updates_start istart;
1528 13b2bc37 2022-10-23 stsp memset(&istart, 0, sizeof(istart));
1529 13b2bc37 2022-10-23 stsp istart.nref_updates = nref_updates;
1531 13b2bc37 2022-10-23 stsp ret = gotd_imsg_compose_event(iev, GOTD_IMSG_REF_UPDATES_START,
1532 13b2bc37 2022-10-23 stsp PROC_REPO_WRITE, -1, &istart, sizeof(istart));
1533 13b2bc37 2022-10-23 stsp if (ret == -1)
1534 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_compose REF_UPDATES_START");
1536 13b2bc37 2022-10-23 stsp return NULL;
1540 13b2bc37 2022-10-23 stsp static const struct got_error *
1541 1a52c9bf 2022-12-30 stsp send_ref_update(struct gotd_ref_update *ref_update, struct gotd_imsgev *iev)
1543 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update iref;
1544 13b2bc37 2022-10-23 stsp const char *refname = got_ref_get_name(ref_update->ref);
1545 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
1546 13b2bc37 2022-10-23 stsp size_t len;
1548 13b2bc37 2022-10-23 stsp memset(&iref, 0, sizeof(iref));
1549 aaa34732 2024-07-13 op memcpy(iref.old_id, ref_update->old_id.hash, SHA1_DIGEST_LENGTH);
1550 aaa34732 2024-07-13 op memcpy(iref.new_id, ref_update->new_id.hash, SHA1_DIGEST_LENGTH);
1551 13b2bc37 2022-10-23 stsp iref.ref_is_new = ref_update->ref_is_new;
1552 9a8e357c 2023-01-28 op iref.delete_ref = ref_update->delete_ref;
1553 13b2bc37 2022-10-23 stsp iref.name_len = strlen(refname);
1555 13b2bc37 2022-10-23 stsp len = sizeof(iref) + iref.name_len;
1556 13b2bc37 2022-10-23 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE, PROC_REPO_WRITE,
1557 13b2bc37 2022-10-23 stsp repo_write.pid, len);
1558 13b2bc37 2022-10-23 stsp if (wbuf == NULL)
1559 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_create REF_UPDATE");
1561 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &iref, sizeof(iref)) == -1)
1562 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE");
1563 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, refname, iref.name_len) == -1)
1564 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE");
1566 13b2bc37 2022-10-23 stsp imsg_close(&iev->ibuf, wbuf);
1568 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
1569 13b2bc37 2022-10-23 stsp return NULL;
1572 13b2bc37 2022-10-23 stsp static const struct got_error *
1573 1a52c9bf 2022-12-30 stsp update_refs(struct gotd_imsgev *iev)
1575 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1576 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1577 13b2bc37 2022-10-23 stsp struct gotd_ref_update *ref_update;
1579 1a52c9bf 2022-12-30 stsp err = send_ref_updates_start(client->nref_updates, iev);
1581 13b2bc37 2022-10-23 stsp return err;
1583 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(ref_update, &client->ref_updates, entry) {
1584 1a52c9bf 2022-12-30 stsp err = send_ref_update(ref_update, iev);
1586 13b2bc37 2022-10-23 stsp goto done;
1589 13b2bc37 2022-10-23 stsp return err;
1592 13b2bc37 2022-10-23 stsp static const struct got_error *
1593 1a52c9bf 2022-12-30 stsp receive_pack_pipe(struct imsg *imsg, struct gotd_imsgev *iev)
1595 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1596 13b2bc37 2022-10-23 stsp size_t datalen;
1598 9cbac887 2023-04-20 stsp log_debug("receiving pack pipe descriptor");
1600 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1601 1cb49b67 2024-03-21 stsp if (datalen != 0)
1602 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1604 1a52c9bf 2022-12-30 stsp if (client->pack_pipe != -1)
1605 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1607 2c52c623 2024-01-30 op client->pack_pipe = imsg_get_fd(imsg);
1608 2c52c623 2024-01-30 op if (client->pack_pipe == -1)
1609 2c52c623 2024-01-30 op return got_error(GOT_ERR_PRIVSEP_NO_FD);
1611 13b2bc37 2022-10-23 stsp return NULL;
1614 13b2bc37 2022-10-23 stsp static const struct got_error *
1615 1a52c9bf 2022-12-30 stsp receive_pack_idx(struct imsg *imsg, struct gotd_imsgev *iev)
1617 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
1618 13b2bc37 2022-10-23 stsp size_t datalen;
1620 9cbac887 2023-04-20 stsp log_debug("receiving pack index output file");
1622 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1623 1cb49b67 2024-03-21 stsp if (datalen != 0)
1624 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1626 1a52c9bf 2022-12-30 stsp if (client->packidx_fd != -1)
1627 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1629 2c52c623 2024-01-30 op client->packidx_fd = imsg_get_fd(imsg);
1630 2c52c623 2024-01-30 op if (client->packidx_fd == -1)
1631 2c52c623 2024-01-30 op return got_error(GOT_ERR_PRIVSEP_NO_FD);
1633 13b2bc37 2022-10-23 stsp return NULL;
1636 ba97b2d7 2024-03-20 stsp static const struct got_error *
1637 1b1a386d 2024-07-09 op notify_removed_ref(const char *refname, struct got_object_id *id,
1638 ba97b2d7 2024-03-20 stsp struct gotd_imsgev *iev, int fd)
1640 ba97b2d7 2024-03-20 stsp const struct got_error *err;
1641 ba97b2d7 2024-03-20 stsp char *id_str;
1643 1b1a386d 2024-07-09 op err = got_object_id_str(&id_str, id);
1645 ba97b2d7 2024-03-20 stsp return err;
1647 ba97b2d7 2024-03-20 stsp dprintf(fd, "Removed %s: %s\n", refname, id_str);
1648 ba97b2d7 2024-03-20 stsp free(id_str);
1649 ba97b2d7 2024-03-20 stsp return err;
1652 ba97b2d7 2024-03-20 stsp static const char *
1653 ba97b2d7 2024-03-20 stsp format_author(char *author)
1655 ba97b2d7 2024-03-20 stsp char *smallerthan;
1657 ba97b2d7 2024-03-20 stsp smallerthan = strchr(author, '<');
1658 ba97b2d7 2024-03-20 stsp if (smallerthan && smallerthan[1] != '\0')
1659 ba97b2d7 2024-03-20 stsp author = smallerthan + 1;
1660 ba97b2d7 2024-03-20 stsp author[strcspn(author, "@>")] = '\0';
1662 ba97b2d7 2024-03-20 stsp return author;
1665 ba97b2d7 2024-03-20 stsp static const struct got_error *
1666 ba97b2d7 2024-03-20 stsp print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
1667 ba97b2d7 2024-03-20 stsp struct got_repository *repo, int fd)
1669 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
1670 ba97b2d7 2024-03-20 stsp char *id_str = NULL, *logmsg0 = NULL;
1671 ba97b2d7 2024-03-20 stsp char *s, *nl;
1672 ba97b2d7 2024-03-20 stsp char *committer = NULL, *author = NULL;
1673 ba97b2d7 2024-03-20 stsp time_t committer_time;
1675 ba97b2d7 2024-03-20 stsp err = got_object_id_str(&id_str, id);
1677 ba97b2d7 2024-03-20 stsp return err;
1679 ba97b2d7 2024-03-20 stsp committer_time = got_object_commit_get_committer_time(commit);
1681 ba97b2d7 2024-03-20 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1683 ba97b2d7 2024-03-20 stsp goto done;
1685 ba97b2d7 2024-03-20 stsp s = logmsg0;
1686 ba97b2d7 2024-03-20 stsp while (isspace((unsigned char)s[0]))
1689 ba97b2d7 2024-03-20 stsp nl = strchr(s, '\n');
1691 ba97b2d7 2024-03-20 stsp *nl = '\0';
1694 ba97b2d7 2024-03-20 stsp if (strcmp(got_object_commit_get_author(commit),
1695 ba97b2d7 2024-03-20 stsp got_object_commit_get_committer(commit)) != 0) {
1696 ba97b2d7 2024-03-20 stsp author = strdup(got_object_commit_get_author(commit));
1697 ba97b2d7 2024-03-20 stsp if (author == NULL) {
1698 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strdup");
1699 ba97b2d7 2024-03-20 stsp goto done;
1701 939d3016 2024-04-23 op dprintf(fd, "%lld %.7s %.8s %s\n", (long long)committer_time,
1702 939d3016 2024-04-23 op id_str, format_author(author), s);
1704 ba97b2d7 2024-03-20 stsp committer = strdup(got_object_commit_get_committer(commit));
1705 939d3016 2024-04-23 op dprintf(fd, "%lld %.7s %.8s %s\n", (long long)committer_time,
1706 939d3016 2024-04-23 op id_str, format_author(committer), s);
1709 ba97b2d7 2024-03-20 stsp if (fsync(fd) == -1 && err == NULL)
1710 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("fsync");
1712 ba97b2d7 2024-03-20 stsp free(id_str);
1713 ba97b2d7 2024-03-20 stsp free(logmsg0);
1714 ba97b2d7 2024-03-20 stsp free(committer);
1715 ba97b2d7 2024-03-20 stsp free(author);
1716 ba97b2d7 2024-03-20 stsp return err;
1719 ba97b2d7 2024-03-20 stsp static const struct got_error *
1720 ba97b2d7 2024-03-20 stsp print_diffstat(struct got_diffstat_cb_arg *dsa, int fd)
1722 ba97b2d7 2024-03-20 stsp struct got_pathlist_entry *pe;
1724 ba97b2d7 2024-03-20 stsp TAILQ_FOREACH(pe, dsa->paths, entry) {
1725 ba97b2d7 2024-03-20 stsp struct got_diff_changed_path *cp = pe->data;
1726 ba97b2d7 2024-03-20 stsp int pad = dsa->max_path_len - pe->path_len + 1;
1728 ba97b2d7 2024-03-20 stsp dprintf(fd, " %c %s%*c | %*d+ %*d-\n", cp->status,
1729 ba97b2d7 2024-03-20 stsp pe->path, pad, ' ', dsa->add_cols + 1, cp->add,
1730 ba97b2d7 2024-03-20 stsp dsa->rm_cols + 1, cp->rm);
1732 ba97b2d7 2024-03-20 stsp dprintf(fd,
1733 ba97b2d7 2024-03-20 stsp "\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
1734 ba97b2d7 2024-03-20 stsp dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
1735 ba97b2d7 2024-03-20 stsp dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
1737 ba97b2d7 2024-03-20 stsp return NULL;
1740 ba97b2d7 2024-03-20 stsp static const struct got_error *
1741 ba97b2d7 2024-03-20 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1742 ba97b2d7 2024-03-20 stsp struct got_repository *repo, struct got_pathlist_head *changed_paths,
1743 ba97b2d7 2024-03-20 stsp struct got_diffstat_cb_arg *diffstat, int fd)
1745 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
1746 939d3016 2024-04-23 op char *id_str, *logmsg0, *logmsg, *line;
1747 ba97b2d7 2024-03-20 stsp time_t committer_time;
1748 ba97b2d7 2024-03-20 stsp const char *author, *committer;
1750 ba97b2d7 2024-03-20 stsp err = got_object_id_str(&id_str, id);
1752 ba97b2d7 2024-03-20 stsp return err;
1754 ba97b2d7 2024-03-20 stsp dprintf(fd, "commit %s\n", id_str);
1755 ba97b2d7 2024-03-20 stsp free(id_str);
1756 ba97b2d7 2024-03-20 stsp id_str = NULL;
1757 ba97b2d7 2024-03-20 stsp dprintf(fd, "from: %s\n", got_object_commit_get_author(commit));
1758 ba97b2d7 2024-03-20 stsp author = got_object_commit_get_author(commit);
1759 ba97b2d7 2024-03-20 stsp committer = got_object_commit_get_committer(commit);
1760 ba97b2d7 2024-03-20 stsp if (strcmp(author, committer) != 0)
1761 ba97b2d7 2024-03-20 stsp dprintf(fd, "via: %s\n", committer);
1762 ba97b2d7 2024-03-20 stsp committer_time = got_object_commit_get_committer_time(commit);
1763 939d3016 2024-04-23 op dprintf(fd, "date: %lld\n", (long long)committer_time);
1764 ba97b2d7 2024-03-20 stsp if (got_object_commit_get_nparents(commit) > 1) {
1765 ba97b2d7 2024-03-20 stsp const struct got_object_id_queue *parent_ids;
1766 ba97b2d7 2024-03-20 stsp struct got_object_qid *qid;
1767 ba97b2d7 2024-03-20 stsp int n = 1;
1768 ba97b2d7 2024-03-20 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1769 ba97b2d7 2024-03-20 stsp STAILQ_FOREACH(qid, parent_ids, entry) {
1770 ba97b2d7 2024-03-20 stsp err = got_object_id_str(&id_str, &qid->id);
1772 ba97b2d7 2024-03-20 stsp goto done;
1773 ba97b2d7 2024-03-20 stsp dprintf(fd, "parent %d: %s\n", n++, id_str);
1774 ba97b2d7 2024-03-20 stsp free(id_str);
1775 ba97b2d7 2024-03-20 stsp id_str = NULL;
1779 ba97b2d7 2024-03-20 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1781 ba97b2d7 2024-03-20 stsp goto done;
1783 fb5636be 2024-03-27 op dprintf(fd, "messagelen: %zu\n", strlen(logmsg0));
1785 ba97b2d7 2024-03-20 stsp logmsg = logmsg0;
1787 ba97b2d7 2024-03-20 stsp line = strsep(&logmsg, "\n");
1789 ba97b2d7 2024-03-20 stsp dprintf(fd, " %s\n", line);
1790 ba97b2d7 2024-03-20 stsp } while (line);
1791 ba97b2d7 2024-03-20 stsp free(logmsg0);
1793 ba97b2d7 2024-03-20 stsp err = print_diffstat(diffstat, fd);
1795 ba97b2d7 2024-03-20 stsp goto done;
1797 ba97b2d7 2024-03-20 stsp if (fsync(fd) == -1 && err == NULL)
1798 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("fsync");
1800 ba97b2d7 2024-03-20 stsp free(id_str);
1801 ba97b2d7 2024-03-20 stsp return err;
1804 ba97b2d7 2024-03-20 stsp static const struct got_error *
1805 ba97b2d7 2024-03-20 stsp get_changed_paths(struct got_pathlist_head *paths,
1806 ba97b2d7 2024-03-20 stsp struct got_commit_object *commit, struct got_repository *repo,
1807 ba97b2d7 2024-03-20 stsp struct got_diffstat_cb_arg *dsa)
1809 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
1810 ba97b2d7 2024-03-20 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1811 ba97b2d7 2024-03-20 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1812 ba97b2d7 2024-03-20 stsp struct got_object_qid *qid;
1813 ba97b2d7 2024-03-20 stsp got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
1814 ba97b2d7 2024-03-20 stsp FILE *f1 = repo_write.diff.f1, *f2 = repo_write.diff.f2;
1815 ba97b2d7 2024-03-20 stsp int fd1 = repo_write.diff.fd1, fd2 = repo_write.diff.fd2;
1818 ba97b2d7 2024-03-20 stsp cb = got_diff_tree_compute_diffstat;
1820 ba97b2d7 2024-03-20 stsp err = got_opentemp_truncate(f1);
1822 ba97b2d7 2024-03-20 stsp return err;
1823 ba97b2d7 2024-03-20 stsp err = got_opentemp_truncate(f2);
1825 ba97b2d7 2024-03-20 stsp return err;
1826 ba97b2d7 2024-03-20 stsp err = got_opentemp_truncatefd(fd1);
1828 ba97b2d7 2024-03-20 stsp return err;
1829 ba97b2d7 2024-03-20 stsp err = got_opentemp_truncatefd(fd2);
1831 ba97b2d7 2024-03-20 stsp return err;
1833 ba97b2d7 2024-03-20 stsp qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1834 ba97b2d7 2024-03-20 stsp if (qid != NULL) {
1835 ba97b2d7 2024-03-20 stsp struct got_commit_object *pcommit;
1836 ba97b2d7 2024-03-20 stsp err = got_object_open_as_commit(&pcommit, repo,
1837 ba97b2d7 2024-03-20 stsp &qid->id);
1839 ba97b2d7 2024-03-20 stsp return err;
1841 ba97b2d7 2024-03-20 stsp tree_id1 = got_object_id_dup(
1842 ba97b2d7 2024-03-20 stsp got_object_commit_get_tree_id(pcommit));
1843 ba97b2d7 2024-03-20 stsp if (tree_id1 == NULL) {
1844 ba97b2d7 2024-03-20 stsp got_object_commit_close(pcommit);
1845 ba97b2d7 2024-03-20 stsp return got_error_from_errno("got_object_id_dup");
1847 ba97b2d7 2024-03-20 stsp got_object_commit_close(pcommit);
1851 ba97b2d7 2024-03-20 stsp if (tree_id1) {
1852 ba97b2d7 2024-03-20 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1854 ba97b2d7 2024-03-20 stsp goto done;
1857 ba97b2d7 2024-03-20 stsp tree_id2 = got_object_commit_get_tree_id(commit);
1858 ba97b2d7 2024-03-20 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1860 ba97b2d7 2024-03-20 stsp goto done;
1862 ba97b2d7 2024-03-20 stsp err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
1863 ba97b2d7 2024-03-20 stsp cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
1865 ba97b2d7 2024-03-20 stsp if (tree1)
1866 ba97b2d7 2024-03-20 stsp got_object_tree_close(tree1);
1867 ba97b2d7 2024-03-20 stsp if (tree2)
1868 ba97b2d7 2024-03-20 stsp got_object_tree_close(tree2);
1869 ba97b2d7 2024-03-20 stsp free(tree_id1);
1870 ba97b2d7 2024-03-20 stsp return err;
1873 ba97b2d7 2024-03-20 stsp static const struct got_error *
1874 ba97b2d7 2024-03-20 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
1875 ba97b2d7 2024-03-20 stsp struct got_repository *repo, int fd)
1877 ba97b2d7 2024-03-20 stsp const struct got_error *err;
1878 ba97b2d7 2024-03-20 stsp struct got_commit_graph *graph;
1879 ba97b2d7 2024-03-20 stsp struct got_object_id_queue reversed_commits;
1880 ba97b2d7 2024-03-20 stsp struct got_object_qid *qid;
1881 ba97b2d7 2024-03-20 stsp struct got_commit_object *commit = NULL;
1882 ba97b2d7 2024-03-20 stsp struct got_pathlist_head changed_paths;
1883 ba97b2d7 2024-03-20 stsp int ncommits = 0;
1884 ba97b2d7 2024-03-20 stsp const int shortlog_threshold = 50;
1886 ba97b2d7 2024-03-20 stsp STAILQ_INIT(&reversed_commits);
1887 ba97b2d7 2024-03-20 stsp TAILQ_INIT(&changed_paths);
1889 ba97b2d7 2024-03-20 stsp /* XXX first-parent only for now */
1890 ba97b2d7 2024-03-20 stsp err = got_commit_graph_open(&graph, "/", 1);
1892 ba97b2d7 2024-03-20 stsp return err;
1893 98297eed 2024-03-27 stsp err = got_commit_graph_bfsort(graph, root_id, repo,
1894 ba97b2d7 2024-03-20 stsp check_cancelled, NULL);
1896 ba97b2d7 2024-03-20 stsp goto done;
1897 ba97b2d7 2024-03-20 stsp for (;;) {
1898 ba97b2d7 2024-03-20 stsp struct got_object_id id;
1900 ba97b2d7 2024-03-20 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1901 ba97b2d7 2024-03-20 stsp check_cancelled, NULL);
1902 ba97b2d7 2024-03-20 stsp if (err) {
1903 ba97b2d7 2024-03-20 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1904 ba97b2d7 2024-03-20 stsp err = NULL;
1908 ba97b2d7 2024-03-20 stsp err = got_object_open_as_commit(&commit, repo, &id);
1912 ba97b2d7 2024-03-20 stsp if (end_id && got_object_id_cmp(&id, end_id) == 0)
1915 ba97b2d7 2024-03-20 stsp err = got_object_qid_alloc(&qid, &id);
1919 ba97b2d7 2024-03-20 stsp STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
1920 ba97b2d7 2024-03-20 stsp ncommits++;
1921 ba97b2d7 2024-03-20 stsp got_object_commit_close(commit);
1923 ba97b2d7 2024-03-20 stsp if (end_id == NULL)
1927 ba97b2d7 2024-03-20 stsp STAILQ_FOREACH(qid, &reversed_commits, entry) {
1928 ba97b2d7 2024-03-20 stsp struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
1929 ba97b2d7 2024-03-20 stsp &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
1931 ba97b2d7 2024-03-20 stsp err = got_object_open_as_commit(&commit, repo, &qid->id);
1935 ba97b2d7 2024-03-20 stsp if (ncommits > shortlog_threshold) {
1936 ba97b2d7 2024-03-20 stsp err = print_commit_oneline(commit, &qid->id,
1937 ba97b2d7 2024-03-20 stsp repo, fd);
1941 ba97b2d7 2024-03-20 stsp err = get_changed_paths(&changed_paths, commit,
1942 ba97b2d7 2024-03-20 stsp repo, &dsa);
1945 ba97b2d7 2024-03-20 stsp err = print_commit(commit, &qid->id, repo,
1946 ba97b2d7 2024-03-20 stsp &changed_paths, &dsa, fd);
1948 ba97b2d7 2024-03-20 stsp got_object_commit_close(commit);
1949 ba97b2d7 2024-03-20 stsp commit = NULL;
1950 ba97b2d7 2024-03-20 stsp got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
1953 ba97b2d7 2024-03-20 stsp if (commit)
1954 ba97b2d7 2024-03-20 stsp got_object_commit_close(commit);
1955 ba97b2d7 2024-03-20 stsp while (!STAILQ_EMPTY(&reversed_commits)) {
1956 ba97b2d7 2024-03-20 stsp qid = STAILQ_FIRST(&reversed_commits);
1957 ba97b2d7 2024-03-20 stsp STAILQ_REMOVE_HEAD(&reversed_commits, entry);
1958 ba97b2d7 2024-03-20 stsp got_object_qid_free(qid);
1960 ba97b2d7 2024-03-20 stsp got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
1961 ba97b2d7 2024-03-20 stsp got_commit_graph_close(graph);
1962 ba97b2d7 2024-03-20 stsp return err;
1965 ba97b2d7 2024-03-20 stsp static const struct got_error *
1966 7a095277 2024-03-28 op print_tag(struct got_object_id *id,
1967 ba97b2d7 2024-03-20 stsp const char *refname, struct got_repository *repo, int fd)
1969 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
1970 ba97b2d7 2024-03-20 stsp struct got_tag_object *tag = NULL;
1971 ba97b2d7 2024-03-20 stsp const char *tagger = NULL;
1972 939d3016 2024-04-23 op char *id_str = NULL, *tagmsg0 = NULL, *tagmsg, *line;
1973 ba97b2d7 2024-03-20 stsp time_t tagger_time;
1975 ba97b2d7 2024-03-20 stsp err = got_object_open_as_tag(&tag, repo, id);
1977 ba97b2d7 2024-03-20 stsp return err;
1979 ba97b2d7 2024-03-20 stsp tagger = got_object_tag_get_tagger(tag);
1980 ba97b2d7 2024-03-20 stsp tagger_time = got_object_tag_get_tagger_time(tag);
1981 ba97b2d7 2024-03-20 stsp err = got_object_id_str(&id_str,
1982 ba97b2d7 2024-03-20 stsp got_object_tag_get_object_id(tag));
1984 ba97b2d7 2024-03-20 stsp goto done;
1986 ba97b2d7 2024-03-20 stsp dprintf(fd, "tag %s\n", refname);
1987 ba97b2d7 2024-03-20 stsp dprintf(fd, "from: %s\n", tagger);
1988 939d3016 2024-04-23 op dprintf(fd, "date: %lld\n", (long long)tagger_time);
1990 ba97b2d7 2024-03-20 stsp switch (got_object_tag_get_object_type(tag)) {
1991 ba97b2d7 2024-03-20 stsp case GOT_OBJ_TYPE_BLOB:
1992 ba97b2d7 2024-03-20 stsp dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
1994 ba97b2d7 2024-03-20 stsp case GOT_OBJ_TYPE_TREE:
1995 ba97b2d7 2024-03-20 stsp dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
1997 ba97b2d7 2024-03-20 stsp case GOT_OBJ_TYPE_COMMIT:
1998 ba97b2d7 2024-03-20 stsp dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
2000 ba97b2d7 2024-03-20 stsp case GOT_OBJ_TYPE_TAG:
2001 ba97b2d7 2024-03-20 stsp dprintf(fd, "object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
2007 ba97b2d7 2024-03-20 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
2008 ba97b2d7 2024-03-20 stsp if (tagmsg0 == NULL) {
2009 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("strdup");
2010 ba97b2d7 2024-03-20 stsp goto done;
2013 fb5636be 2024-03-27 op dprintf(fd, "messagelen: %zu\n", strlen(tagmsg0));
2015 ba97b2d7 2024-03-20 stsp tagmsg = tagmsg0;
2017 ba97b2d7 2024-03-20 stsp line = strsep(&tagmsg, "\n");
2019 ba97b2d7 2024-03-20 stsp dprintf(fd, " %s\n", line);
2020 ba97b2d7 2024-03-20 stsp } while (line);
2021 ba97b2d7 2024-03-20 stsp free(tagmsg0);
2024 ba97b2d7 2024-03-20 stsp got_object_tag_close(tag);
2025 ba97b2d7 2024-03-20 stsp free(id_str);
2026 ba97b2d7 2024-03-20 stsp return err;
2029 ba97b2d7 2024-03-20 stsp static const struct got_error *
2030 1b1a386d 2024-07-09 op notify_changed_ref(const char *refname, struct got_object_id *old_id,
2031 1b1a386d 2024-07-09 op struct got_object_id *new_id, struct gotd_imsgev *iev, int fd)
2033 ba97b2d7 2024-03-20 stsp const struct got_error *err;
2034 ba97b2d7 2024-03-20 stsp int old_obj_type, new_obj_type;
2035 ba97b2d7 2024-03-20 stsp const char *label;
2036 ba97b2d7 2024-03-20 stsp char *new_id_str = NULL;
2038 1b1a386d 2024-07-09 op err = got_object_get_type(&old_obj_type, repo_write.repo, old_id);
2040 ba97b2d7 2024-03-20 stsp return err;
2042 1b1a386d 2024-07-09 op err = got_object_get_type(&new_obj_type, repo_write.repo, new_id);
2044 ba97b2d7 2024-03-20 stsp return err;
2046 ba97b2d7 2024-03-20 stsp switch (new_obj_type) {
2047 ba97b2d7 2024-03-20 stsp case GOT_OBJ_TYPE_COMMIT:
2048 1b1a386d 2024-07-09 op err = print_commits(new_id,
2049 1b1a386d 2024-07-09 op old_obj_type == GOT_OBJ_TYPE_COMMIT ? old_id : NULL,
2050 ba97b2d7 2024-03-20 stsp repo_write.repo, fd);
2052 ba97b2d7 2024-03-20 stsp case GOT_OBJ_TYPE_TAG:
2053 1b1a386d 2024-07-09 op err = print_tag(new_id, refname, repo_write.repo, fd);
2056 ba97b2d7 2024-03-20 stsp err = got_object_type_label(&label, new_obj_type);
2058 ba97b2d7 2024-03-20 stsp goto done;
2059 1b1a386d 2024-07-09 op err = got_object_id_str(&new_id_str, new_id);
2061 ba97b2d7 2024-03-20 stsp goto done;
2062 ba97b2d7 2024-03-20 stsp dprintf(fd, "%s: %s object %s\n", refname, label, new_id_str);
2066 ba97b2d7 2024-03-20 stsp free(new_id_str);
2067 ba97b2d7 2024-03-20 stsp return err;
2070 ba97b2d7 2024-03-20 stsp static const struct got_error *
2071 1b1a386d 2024-07-09 op notify_created_ref(const char *refname, struct got_object_id *id,
2072 ba97b2d7 2024-03-20 stsp struct gotd_imsgev *iev, int fd)
2074 ba97b2d7 2024-03-20 stsp const struct got_error *err;
2075 ba97b2d7 2024-03-20 stsp int obj_type;
2077 1b1a386d 2024-07-09 op err = got_object_get_type(&obj_type, repo_write.repo, id);
2079 ba97b2d7 2024-03-20 stsp return err;
2081 ba97b2d7 2024-03-20 stsp if (obj_type == GOT_OBJ_TYPE_TAG)
2082 1b1a386d 2024-07-09 op return print_tag(id, refname, repo_write.repo, fd);
2084 1b1a386d 2024-07-09 op return print_commits(id, NULL, repo_write.repo, fd);
2087 ba97b2d7 2024-03-20 stsp static const struct got_error *
2088 ba97b2d7 2024-03-20 stsp render_notification(struct imsg *imsg, struct gotd_imsgev *iev)
2090 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
2091 ba97b2d7 2024-03-20 stsp struct gotd_imsg_notification_content ireq;
2092 ba97b2d7 2024-03-20 stsp size_t datalen, len;
2093 c237d891 2024-03-26 op char *refname = NULL;
2094 ba97b2d7 2024-03-20 stsp struct ibuf *wbuf;
2095 c237d891 2024-03-26 op int fd = -1;
2097 ba97b2d7 2024-03-20 stsp fd = imsg_get_fd(imsg);
2098 ba97b2d7 2024-03-20 stsp if (fd == -1)
2099 ba97b2d7 2024-03-20 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
2101 ba97b2d7 2024-03-20 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
2102 c237d891 2024-03-26 op if (datalen < sizeof(ireq)) {
2103 c237d891 2024-03-26 op err = got_error(GOT_ERR_PRIVSEP_LEN);
2107 ba97b2d7 2024-03-20 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
2109 c237d891 2024-03-26 op if (datalen != sizeof(ireq) + ireq.refname_len) {
2110 c237d891 2024-03-26 op err = got_error(GOT_ERR_PRIVSEP_LEN);
2114 ba97b2d7 2024-03-20 stsp refname = strndup(imsg->data + sizeof(ireq), ireq.refname_len);
2115 c237d891 2024-03-26 op if (refname == NULL) {
2116 c237d891 2024-03-26 op err = got_error_from_errno("strndup");
2120 ba97b2d7 2024-03-20 stsp switch (ireq.action) {
2121 ba97b2d7 2024-03-20 stsp case GOTD_NOTIF_ACTION_CREATED:
2122 1b1a386d 2024-07-09 op err = notify_created_ref(refname, &ireq.new_id, iev, fd);
2124 ba97b2d7 2024-03-20 stsp case GOTD_NOTIF_ACTION_REMOVED:
2125 1b1a386d 2024-07-09 op err = notify_removed_ref(refname, &ireq.old_id, iev, fd);
2127 ba97b2d7 2024-03-20 stsp case GOTD_NOTIF_ACTION_CHANGED:
2128 1b1a386d 2024-07-09 op err = notify_changed_ref(refname, &ireq.old_id, &ireq.new_id,
2132 cef7eb79 2024-03-26 op if (err != NULL)
2135 ba97b2d7 2024-03-20 stsp if (fsync(fd) == -1) {
2136 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("fsync");
2137 ba97b2d7 2024-03-20 stsp goto done;
2140 ba97b2d7 2024-03-20 stsp len = sizeof(ireq) + ireq.refname_len;
2141 ba97b2d7 2024-03-20 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_NOTIFY, PROC_REPO_WRITE,
2142 ba97b2d7 2024-03-20 stsp repo_write.pid, len);
2143 ba97b2d7 2024-03-20 stsp if (wbuf == NULL) {
2144 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("imsg_create REF");
2145 ba97b2d7 2024-03-20 stsp goto done;
2147 ba97b2d7 2024-03-20 stsp if (imsg_add(wbuf, &ireq, sizeof(ireq)) == -1) {
2148 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("imsg_add NOTIFY");
2149 ba97b2d7 2024-03-20 stsp goto done;
2151 ba97b2d7 2024-03-20 stsp if (imsg_add(wbuf, refname, ireq.refname_len) == -1) {
2152 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("imsg_add NOTIFY");
2153 ba97b2d7 2024-03-20 stsp goto done;
2156 ba97b2d7 2024-03-20 stsp imsg_close(&iev->ibuf, wbuf);
2157 ba97b2d7 2024-03-20 stsp gotd_imsg_event_add(iev);
2159 ba97b2d7 2024-03-20 stsp free(refname);
2160 c237d891 2024-03-26 op if (fd != -1 && close(fd) == -1 && err == NULL)
2161 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("close");
2162 ba97b2d7 2024-03-20 stsp return err;
2165 13b2bc37 2022-10-23 stsp static void
2166 ae7c1b78 2023-01-10 stsp repo_write_dispatch_session(int fd, short event, void *arg)
2168 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
2169 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
2170 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
2171 13b2bc37 2022-10-23 stsp struct imsg imsg;
2172 1a52c9bf 2022-12-30 stsp struct repo_write_client *client = &repo_write_client;
2173 13b2bc37 2022-10-23 stsp ssize_t n;
2174 0ff2c315 2023-01-18 stsp int shut = 0, have_packfile = 0;
2176 13b2bc37 2022-10-23 stsp if (event & EV_READ) {
2177 13b2bc37 2022-10-23 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
2178 13b2bc37 2022-10-23 stsp fatal("imsg_read error");
2179 13b2bc37 2022-10-23 stsp if (n == 0) /* Connection closed. */
2183 13b2bc37 2022-10-23 stsp if (event & EV_WRITE) {
2184 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
2185 13b2bc37 2022-10-23 stsp if (n == -1 && errno != EAGAIN)
2186 13b2bc37 2022-10-23 stsp fatal("msgbuf_write");
2187 13b2bc37 2022-10-23 stsp if (n == 0) /* Connection closed. */
2191 13b2bc37 2022-10-23 stsp for (;;) {
2192 13b2bc37 2022-10-23 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
2193 13b2bc37 2022-10-23 stsp fatal("%s: imsg_get error", __func__);
2194 13b2bc37 2022-10-23 stsp if (n == 0) /* No more messages. */
2197 1a52c9bf 2022-12-30 stsp if (imsg.hdr.type != GOTD_IMSG_LIST_REFS_INTERNAL &&
2198 1cb49b67 2024-03-21 stsp !repo_write.refs_listed) {
2199 1a52c9bf 2022-12-30 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2203 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
2204 13b2bc37 2022-10-23 stsp case GOTD_IMSG_LIST_REFS_INTERNAL:
2205 1a52c9bf 2022-12-30 stsp err = list_refs(&imsg);
2207 88f6dccd 2023-03-04 op log_warnx("ls-refs: %s", err->msg);
2209 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE:
2210 1a52c9bf 2022-12-30 stsp err = recv_ref_update(&imsg);
2212 88f6dccd 2023-03-04 op log_warnx("ref-update: %s", err->msg);
2214 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_PIPE:
2215 1a52c9bf 2022-12-30 stsp err = receive_pack_pipe(&imsg, iev);
2216 13b2bc37 2022-10-23 stsp if (err) {
2217 88f6dccd 2023-03-04 op log_warnx("receiving pack pipe: %s", err->msg);
2221 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKIDX_FILE:
2222 1a52c9bf 2022-12-30 stsp err = receive_pack_idx(&imsg, iev);
2223 13b2bc37 2022-10-23 stsp if (err) {
2224 88f6dccd 2023-03-04 op log_warnx("receiving pack index: %s",
2229 13b2bc37 2022-10-23 stsp case GOTD_IMSG_RECV_PACKFILE:
2230 9afa3de2 2023-04-04 stsp err = protect_refs_from_deletion();
2233 0ff2c315 2023-01-18 stsp err = recv_packfile(&have_packfile, &imsg);
2234 13b2bc37 2022-10-23 stsp if (err) {
2235 88f6dccd 2023-03-04 op log_warnx("receive packfile: %s", err->msg);
2238 0ff2c315 2023-01-18 stsp if (have_packfile) {
2239 0ff2c315 2023-01-18 stsp err = verify_packfile();
2240 0ff2c315 2023-01-18 stsp if (err) {
2241 88f6dccd 2023-03-04 op log_warnx("verify packfile: %s",
2245 0ff2c315 2023-01-18 stsp err = install_packfile(iev);
2246 0ff2c315 2023-01-18 stsp if (err) {
2247 88f6dccd 2023-03-04 op log_warnx("install packfile: %s",
2252 30a624fb 2024-03-19 stsp * Ensure we re-read the pack index list
2253 30a624fb 2024-03-19 stsp * upon next access.
2255 30a624fb 2024-03-19 stsp repo_write.repo->pack_path_mtime.tv_sec = 0;
2256 30a624fb 2024-03-19 stsp repo_write.repo->pack_path_mtime.tv_nsec = 0;
2258 1a52c9bf 2022-12-30 stsp err = update_refs(iev);
2259 13b2bc37 2022-10-23 stsp if (err) {
2260 88f6dccd 2023-03-04 op log_warnx("update refs: %s", err->msg);
2263 ba97b2d7 2024-03-20 stsp case GOTD_IMSG_NOTIFY:
2264 ba97b2d7 2024-03-20 stsp err = render_notification(&imsg, iev);
2265 ba97b2d7 2024-03-20 stsp if (err) {
2266 ba97b2d7 2024-03-20 stsp log_warnx("render notification: %s", err->msg);
2271 88f6dccd 2023-03-04 op log_debug("unexpected imsg %d", imsg.hdr.type);
2275 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
2278 ae7c1b78 2023-01-10 stsp if (!shut && check_cancelled(NULL) == NULL) {
2279 ae7c1b78 2023-01-10 stsp if (err &&
2280 ae7c1b78 2023-01-10 stsp gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
2281 ae7c1b78 2023-01-10 stsp client->id, err) == -1) {
2282 ae7c1b78 2023-01-10 stsp log_warnx("could not send error to parent: %s",
2283 ae7c1b78 2023-01-10 stsp err->msg);
2285 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
2287 ae7c1b78 2023-01-10 stsp /* This pipe is dead. Remove its event handler */
2288 ae7c1b78 2023-01-10 stsp event_del(&iev->ev);
2289 ae7c1b78 2023-01-10 stsp event_loopexit(NULL);
2293 ae7c1b78 2023-01-10 stsp static const struct got_error *
2294 ae7c1b78 2023-01-10 stsp recv_connect(struct imsg *imsg)
2296 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = &repo_write.session_iev;
2297 ae7c1b78 2023-01-10 stsp size_t datalen;
2299 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
2300 ae7c1b78 2023-01-10 stsp if (datalen != 0)
2301 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
2303 ae7c1b78 2023-01-10 stsp if (repo_write.session_fd != -1)
2304 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
2306 2c52c623 2024-01-30 op repo_write.session_fd = imsg_get_fd(imsg);
2307 2c52c623 2024-01-30 op if (repo_write.session_fd == -1)
2308 2c52c623 2024-01-30 op return got_error(GOT_ERR_PRIVSEP_NO_FD);
2310 ae7c1b78 2023-01-10 stsp imsg_init(&iev->ibuf, repo_write.session_fd);
2311 ae7c1b78 2023-01-10 stsp iev->handler = repo_write_dispatch_session;
2312 ae7c1b78 2023-01-10 stsp iev->events = EV_READ;
2313 ae7c1b78 2023-01-10 stsp iev->handler_arg = NULL;
2314 ae7c1b78 2023-01-10 stsp event_set(&iev->ev, iev->ibuf.fd, EV_READ,
2315 ae7c1b78 2023-01-10 stsp repo_write_dispatch_session, iev);
2316 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
2318 ae7c1b78 2023-01-10 stsp return NULL;
2321 ae7c1b78 2023-01-10 stsp static void
2322 ae7c1b78 2023-01-10 stsp repo_write_dispatch(int fd, short event, void *arg)
2324 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
2325 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = arg;
2326 ae7c1b78 2023-01-10 stsp struct imsgbuf *ibuf = &iev->ibuf;
2327 ae7c1b78 2023-01-10 stsp struct imsg imsg;
2328 ae7c1b78 2023-01-10 stsp ssize_t n;
2329 ae7c1b78 2023-01-10 stsp int shut = 0;
2330 ae7c1b78 2023-01-10 stsp struct repo_write_client *client = &repo_write_client;
2332 ae7c1b78 2023-01-10 stsp if (event & EV_READ) {
2333 ae7c1b78 2023-01-10 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
2334 ae7c1b78 2023-01-10 stsp fatal("imsg_read error");
2335 ae7c1b78 2023-01-10 stsp if (n == 0) /* Connection closed. */
2339 ae7c1b78 2023-01-10 stsp if (event & EV_WRITE) {
2340 ae7c1b78 2023-01-10 stsp n = msgbuf_write(&ibuf->w);
2341 ae7c1b78 2023-01-10 stsp if (n == -1 && errno != EAGAIN)
2342 ae7c1b78 2023-01-10 stsp fatal("msgbuf_write");
2343 ae7c1b78 2023-01-10 stsp if (n == 0) /* Connection closed. */
2347 ae7c1b78 2023-01-10 stsp while (err == NULL && check_cancelled(NULL) == NULL) {
2348 ae7c1b78 2023-01-10 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
2349 ae7c1b78 2023-01-10 stsp fatal("%s: imsg_get", __func__);
2350 ae7c1b78 2023-01-10 stsp if (n == 0) /* No more messages. */
2353 ae7c1b78 2023-01-10 stsp switch (imsg.hdr.type) {
2354 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CONNECT_REPO_CHILD:
2355 ae7c1b78 2023-01-10 stsp err = recv_connect(&imsg);
2358 88f6dccd 2023-03-04 op log_debug("unexpected imsg %d", imsg.hdr.type);
2362 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
2365 13b2bc37 2022-10-23 stsp if (!shut && check_cancelled(NULL) == NULL) {
2366 13b2bc37 2022-10-23 stsp if (err &&
2367 13b2bc37 2022-10-23 stsp gotd_imsg_send_error_event(iev, PROC_REPO_WRITE,
2368 1a52c9bf 2022-12-30 stsp client->id, err) == -1) {
2369 13b2bc37 2022-10-23 stsp log_warnx("could not send error to parent: %s",
2370 13b2bc37 2022-10-23 stsp err->msg);
2372 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
2374 13b2bc37 2022-10-23 stsp /* This pipe is dead. Remove its event handler */
2375 13b2bc37 2022-10-23 stsp event_del(&iev->ev);
2376 13b2bc37 2022-10-23 stsp event_loopexit(NULL);
2381 eec68231 2022-12-14 stsp repo_write_main(const char *title, const char *repo_path,
2382 9afa3de2 2023-04-04 stsp int *pack_fds, int *temp_fds,
2383 ba97b2d7 2024-03-20 stsp FILE *diff_f1, FILE *diff_f2, int diff_fd1, int diff_fd2,
2384 9afa3de2 2023-04-04 stsp struct got_pathlist_head *protected_tag_namespaces,
2385 9afa3de2 2023-04-04 stsp struct got_pathlist_head *protected_branch_namespaces,
2386 9afa3de2 2023-04-04 stsp struct got_pathlist_head *protected_branches)
2388 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
2389 363c6230 2023-02-09 stsp struct repo_write_client *client = &repo_write_client;
2390 13b2bc37 2022-10-23 stsp struct gotd_imsgev iev;
2392 363c6230 2023-02-09 stsp client->fd = -1;
2393 363c6230 2023-02-09 stsp client->pack_pipe = -1;
2394 363c6230 2023-02-09 stsp client->packidx_fd = -1;
2395 363c6230 2023-02-09 stsp client->pack.fd = -1;
2397 13b2bc37 2022-10-23 stsp repo_write.title = title;
2398 13b2bc37 2022-10-23 stsp repo_write.pid = getpid();
2399 13b2bc37 2022-10-23 stsp repo_write.pack_fds = pack_fds;
2400 13b2bc37 2022-10-23 stsp repo_write.temp_fds = temp_fds;
2401 ae7c1b78 2023-01-10 stsp repo_write.session_fd = -1;
2402 ae7c1b78 2023-01-10 stsp repo_write.session_iev.ibuf.fd = -1;
2403 9afa3de2 2023-04-04 stsp repo_write.protected_tag_namespaces = protected_tag_namespaces;
2404 9afa3de2 2023-04-04 stsp repo_write.protected_branch_namespaces = protected_branch_namespaces;
2405 9afa3de2 2023-04-04 stsp repo_write.protected_branches = protected_branches;
2406 ba97b2d7 2024-03-20 stsp repo_write.diff.f1 = diff_f1;
2407 ba97b2d7 2024-03-20 stsp repo_write.diff.f2 = diff_f2;
2408 ba97b2d7 2024-03-20 stsp repo_write.diff.fd1 = diff_fd1;
2409 ba97b2d7 2024-03-20 stsp repo_write.diff.fd2 = diff_fd2;
2411 1a52c9bf 2022-12-30 stsp STAILQ_INIT(&repo_write_client.ref_updates);
2413 eec68231 2022-12-14 stsp err = got_repo_open(&repo_write.repo, repo_path, NULL, pack_fds);
2415 13b2bc37 2022-10-23 stsp goto done;
2416 13b2bc37 2022-10-23 stsp if (!got_repo_is_bare(repo_write.repo)) {
2417 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
2418 13b2bc37 2022-10-23 stsp "bare git repository required");
2419 13b2bc37 2022-10-23 stsp goto done;
2421 c214ca4f 2024-08-01 op if (got_repo_get_object_format(repo_write.repo) != GOT_HASH_SHA1) {
2422 c214ca4f 2024-08-01 op err = got_error_msg(GOT_ERR_NOT_IMPL,
2423 c214ca4f 2024-08-01 op "sha256 object IDs unsupported in network protocol");
2427 13b2bc37 2022-10-23 stsp got_repo_temp_fds_set(repo_write.repo, temp_fds);
2429 13b2bc37 2022-10-23 stsp signal(SIGINT, catch_sigint);
2430 13b2bc37 2022-10-23 stsp signal(SIGTERM, catch_sigterm);
2431 13b2bc37 2022-10-23 stsp signal(SIGPIPE, SIG_IGN);
2432 13b2bc37 2022-10-23 stsp signal(SIGHUP, SIG_IGN);
2434 8c6fc146 2022-11-17 stsp imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
2435 13b2bc37 2022-10-23 stsp iev.handler = repo_write_dispatch;
2436 13b2bc37 2022-10-23 stsp iev.events = EV_READ;
2437 13b2bc37 2022-10-23 stsp iev.handler_arg = NULL;
2438 13b2bc37 2022-10-23 stsp event_set(&iev.ev, iev.ibuf.fd, EV_READ, repo_write_dispatch, &iev);
2439 b50a2b46 2022-12-29 stsp if (gotd_imsg_compose_event(&iev, GOTD_IMSG_REPO_CHILD_READY,
2440 b50a2b46 2022-12-29 stsp PROC_REPO_WRITE, -1, NULL, 0) == -1) {
2441 b50a2b46 2022-12-29 stsp err = got_error_from_errno("imsg compose REPO_CHILD_READY");
2442 13b2bc37 2022-10-23 stsp goto done;
2445 13b2bc37 2022-10-23 stsp event_dispatch();
2447 ba97b2d7 2024-03-20 stsp if (fclose(diff_f1) == EOF && err == NULL)
2448 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("fclose");
2449 ba97b2d7 2024-03-20 stsp if (fclose(diff_f2) == EOF && err == NULL)
2450 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("fclose");
2451 ba97b2d7 2024-03-20 stsp if (close(diff_fd1) == -1 && err == NULL)
2452 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("close");
2453 ba97b2d7 2024-03-20 stsp if (close(diff_fd2) == -1 && err == NULL)
2454 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("close");
2456 13b2bc37 2022-10-23 stsp log_warnx("%s: %s", title, err->msg);
2457 13b2bc37 2022-10-23 stsp repo_write_shutdown();
2461 13b2bc37 2022-10-23 stsp repo_write_shutdown(void)
2463 363c6230 2023-02-09 stsp struct repo_write_client *client = &repo_write_client;
2464 363c6230 2023-02-09 stsp struct gotd_ref_update *ref_update;
2466 e8d451cc 2024-03-22 stsp log_debug("%s: shutting down", repo_write.title);
2468 363c6230 2023-02-09 stsp while (!STAILQ_EMPTY(&client->ref_updates)) {
2469 363c6230 2023-02-09 stsp ref_update = STAILQ_FIRST(&client->ref_updates);
2470 363c6230 2023-02-09 stsp STAILQ_REMOVE_HEAD(&client->ref_updates, entry);
2471 363c6230 2023-02-09 stsp got_ref_close(ref_update->ref);
2472 363c6230 2023-02-09 stsp free(ref_update);
2475 363c6230 2023-02-09 stsp got_pack_close(&client->pack);
2476 363c6230 2023-02-09 stsp if (client->fd != -1)
2477 363c6230 2023-02-09 stsp close(client->fd);
2478 363c6230 2023-02-09 stsp if (client->pack_pipe != -1)
2479 363c6230 2023-02-09 stsp close(client->pack_pipe);
2480 363c6230 2023-02-09 stsp if (client->packidx_fd != -1)
2481 363c6230 2023-02-09 stsp close(client->packidx_fd);
2483 13b2bc37 2022-10-23 stsp if (repo_write.repo)
2484 13b2bc37 2022-10-23 stsp got_repo_close(repo_write.repo);
2485 13b2bc37 2022-10-23 stsp got_repo_pack_fds_close(repo_write.pack_fds);
2486 8193d041 2022-10-30 stsp got_repo_temp_fds_close(repo_write.temp_fds);
2487 ae7c1b78 2023-01-10 stsp if (repo_write.session_fd != -1)
2488 ae7c1b78 2023-01-10 stsp close(repo_write.session_fd);