Blame


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