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