2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 876c234b 2018-09-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 876c234b 2018-09-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 876c234b 2018-09-10 stsp * copyright notice and this permission notice appear in all copies.
8 876c234b 2018-09-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 876c234b 2018-09-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 876c234b 2018-09-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 876c234b 2018-09-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 876c234b 2018-09-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 876c234b 2018-09-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 876c234b 2018-09-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
18 63915ee5 2022-06-23 thomas #include <sys/stat.h>
19 876c234b 2018-09-10 stsp #include <sys/types.h>
20 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
21 876c234b 2018-09-10 stsp #include <sys/uio.h>
22 876c234b 2018-09-10 stsp #include <sys/time.h>
23 876c234b 2018-09-10 stsp #include <sys/mman.h>
25 60b94e7d 2022-07-19 thomas #include <inttypes.h>
26 876c234b 2018-09-10 stsp #include <limits.h>
27 99437157 2018-11-11 stsp #include <signal.h>
28 876c234b 2018-09-10 stsp #include <stdint.h>
29 876c234b 2018-09-10 stsp #include <stdio.h>
30 876c234b 2018-09-10 stsp #include <stdlib.h>
31 876c234b 2018-09-10 stsp #include <string.h>
32 81a12da5 2020-09-09 naddy #include <unistd.h>
33 876c234b 2018-09-10 stsp #include <zlib.h>
35 876c234b 2018-09-10 stsp #include "got_error.h"
36 876c234b 2018-09-10 stsp #include "got_object.h"
37 3022d272 2019-11-14 stsp #include "got_path.h"
39 876c234b 2018-09-10 stsp #include "got_lib_delta.h"
40 ab2f42e7 2019-11-10 stsp #include "got_lib_delta_cache.h"
41 876c234b 2018-09-10 stsp #include "got_lib_object.h"
42 232c0ac1 2023-04-22 thomas #include "got_lib_object_qid.h"
43 c59b3346 2018-09-11 stsp #include "got_lib_object_cache.h"
44 876c234b 2018-09-10 stsp #include "got_lib_object_parse.h"
45 7d0d4920 2022-05-12 thomas #include "got_lib_object_idset.h"
46 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
47 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
49 ec2b23c5 2022-07-01 thomas #ifndef nitems
50 ec2b23c5 2022-07-01 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
56 99437157 2018-11-11 stsp catch_sigint(int signo)
58 99437157 2018-11-11 stsp sigint_received = 1;
61 876c234b 2018-09-10 stsp static const struct got_error *
62 704b89c4 2019-05-23 stsp open_object(struct got_object **obj, struct got_pack *pack,
63 704b89c4 2019-05-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id,
64 704b89c4 2019-05-23 stsp struct got_object_cache *objcache)
66 704b89c4 2019-05-23 stsp const struct got_error *err;
68 704b89c4 2019-05-23 stsp err = got_packfile_open_object(obj, pack, packidx, idx, id);
71 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
73 704b89c4 2019-05-23 stsp err = got_object_cache_add(objcache, id, *obj);
75 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
76 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
80 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
81 704b89c4 2019-05-23 stsp return NULL;
84 704b89c4 2019-05-23 stsp static const struct got_error *
85 876c234b 2018-09-10 stsp object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
86 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
88 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
89 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
90 876c234b 2018-09-10 stsp struct got_object *obj;
91 106807b4 2018-09-15 stsp struct got_object_id id;
92 876c234b 2018-09-10 stsp size_t datalen;
94 876c234b 2018-09-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
95 876c234b 2018-09-10 stsp if (datalen != sizeof(iobj))
96 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
97 876c234b 2018-09-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
98 b6f67730 2023-02-03 thomas memcpy(&id, &iobj.id, sizeof(id));
100 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
102 704b89c4 2019-05-23 stsp obj->refcnt++;
104 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
110 876c234b 2018-09-10 stsp err = got_privsep_send_obj(ibuf, obj);
112 876c234b 2018-09-10 stsp got_object_close(obj);
113 876c234b 2018-09-10 stsp return err;
116 8f1c06eb 2021-09-25 thomas.ad static const struct got_error *
117 ca6e02ac 2020-01-07 stsp open_commit(struct got_commit_object **commit, struct got_pack *pack,
118 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
119 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
121 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
122 cb5e38fd 2019-05-23 stsp struct got_object *obj = NULL;
123 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
124 cfd633c2 2018-09-10 stsp size_t len;
126 ca6e02ac 2020-01-07 stsp *commit = NULL;
128 ca6e02ac 2020-01-07 stsp obj = got_object_cache_get(objcache, id);
130 704b89c4 2019-05-23 stsp obj->refcnt++;
132 ca6e02ac 2020-01-07 stsp err = open_object(&obj, pack, packidx, obj_idx, id,
135 704b89c4 2019-05-23 stsp return err;
138 cfd633c2 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
142 cfd633c2 2018-09-10 stsp obj->size = len;
144 ca6e02ac 2020-01-07 stsp err = got_object_parse_commit(commit, buf, len);
146 ca6e02ac 2020-01-07 stsp got_object_close(obj);
148 ca6e02ac 2020-01-07 stsp return err;
151 ca6e02ac 2020-01-07 stsp static const struct got_error *
152 ca6e02ac 2020-01-07 stsp commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
153 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
155 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
156 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
157 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL;
158 ca6e02ac 2020-01-07 stsp struct got_object_id id;
159 ca6e02ac 2020-01-07 stsp size_t datalen;
161 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
162 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(iobj))
163 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
164 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
165 b6f67730 2023-02-03 thomas memcpy(&id, &iobj.id, sizeof(id));
167 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
171 cfd633c2 2018-09-10 stsp err = got_privsep_send_commit(ibuf, commit);
173 cb5e38fd 2019-05-23 stsp if (commit)
174 cb5e38fd 2019-05-23 stsp got_object_commit_close(commit);
176 7762fe12 2018-11-05 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
177 7762fe12 2018-11-05 stsp err = NULL;
179 7762fe12 2018-11-05 stsp got_privsep_send_error(ibuf, err);
182 7762fe12 2018-11-05 stsp return err;
185 8f1c06eb 2021-09-25 thomas.ad static const struct got_error *
186 2d28509d 2023-04-28 thomas open_tree(uint8_t **buf, size_t *len,
187 2d28509d 2023-04-28 thomas struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
188 2d28509d 2023-04-28 thomas struct got_object_id *id, struct got_object_cache *objcache)
190 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
191 ca6e02ac 2020-01-07 stsp struct got_object *obj = NULL;
192 2d28509d 2023-04-28 thomas int cached = 0;
194 ca6e02ac 2020-01-07 stsp *buf = NULL;
195 2d28509d 2023-04-28 thomas *len = 0;
197 ca6e02ac 2020-01-07 stsp obj = got_object_cache_get(objcache, id);
199 ca6e02ac 2020-01-07 stsp obj->refcnt++;
200 2d28509d 2023-04-28 thomas cached = 1;
202 ca6e02ac 2020-01-07 stsp err = open_object(&obj, pack, packidx, obj_idx, id,
205 ca6e02ac 2020-01-07 stsp return err;
208 2d28509d 2023-04-28 thomas err = got_packfile_extract_object_to_mem(buf, len, obj, pack);
212 2d28509d 2023-04-28 thomas if (!cached)
213 2d28509d 2023-04-28 thomas obj->size = *len;
215 ca6e02ac 2020-01-07 stsp got_object_close(obj);
217 ca6e02ac 2020-01-07 stsp free(*buf);
218 ca6e02ac 2020-01-07 stsp *buf = NULL;
220 ca6e02ac 2020-01-07 stsp return err;
223 7762fe12 2018-11-05 stsp static const struct got_error *
224 876c234b 2018-09-10 stsp tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
225 c77e00b3 2022-10-18 thomas struct got_packidx *packidx, struct got_object_cache *objcache,
226 c77e00b3 2022-10-18 thomas struct got_parsed_tree_entry **entries, size_t *nentries,
227 c77e00b3 2022-10-18 thomas size_t *nentries_alloc)
229 e7885405 2018-09-10 stsp const struct got_error *err = NULL;
230 13c729f7 2018-12-24 stsp struct got_imsg_packed_object iobj;
231 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
232 2d28509d 2023-04-28 thomas size_t len = 0;
233 13c729f7 2018-12-24 stsp struct got_object_id id;
234 13c729f7 2018-12-24 stsp size_t datalen;
236 13c729f7 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
237 13c729f7 2018-12-24 stsp if (datalen != sizeof(iobj))
238 13c729f7 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
239 13c729f7 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
240 b6f67730 2023-02-03 thomas memcpy(&id, &iobj.id, sizeof(id));
242 2d28509d 2023-04-28 thomas err = open_tree(&buf, &len, pack, packidx, iobj.idx, &id, objcache);
244 ca6e02ac 2020-01-07 stsp return err;
246 2d28509d 2023-04-28 thomas err = got_object_parse_tree(entries, nentries, nentries_alloc,
247 2d28509d 2023-04-28 thomas buf, len);
249 2d28509d 2023-04-28 thomas goto done;
251 c77e00b3 2022-10-18 thomas err = got_privsep_send_tree(ibuf, *entries, *nentries);
253 e7885405 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
254 e7885405 2018-09-10 stsp err = NULL;
256 e7885405 2018-09-10 stsp got_privsep_send_error(ibuf, err);
259 2d28509d 2023-04-28 thomas free(buf);
260 e7885405 2018-09-10 stsp return err;
263 876c234b 2018-09-10 stsp static const struct got_error *
264 01bb5a15 2021-09-25 thomas.ad receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
266 3840f4c9 2018-09-12 stsp const struct got_error *err;
267 3840f4c9 2018-09-12 stsp struct imsg imsg;
268 55da3778 2018-09-10 stsp size_t datalen;
271 3840f4c9 2018-09-12 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
273 55da3778 2018-09-10 stsp return err;
275 3840f4c9 2018-09-12 stsp if (imsg.hdr.type != imsg_code) {
276 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
280 3840f4c9 2018-09-12 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
281 55da3778 2018-09-10 stsp if (datalen != 0) {
282 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
285 3d97effa 2024-01-31 thomas fd = imsg_get_fd(&imsg);
286 3d97effa 2024-01-31 thomas if (fd == -1) {
287 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
291 3d97effa 2024-01-31 thomas *f = fdopen(fd, "w+");
292 3840f4c9 2018-09-12 stsp if (*f == NULL) {
293 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
294 3d97effa 2024-01-31 thomas close(fd);
298 3840f4c9 2018-09-12 stsp imsg_free(&imsg);
299 3840f4c9 2018-09-12 stsp return err;
302 bc1f382f 2022-01-05 thomas static const struct got_error *
303 f9c2e8e5 2022-02-13 thomas receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
304 bc1f382f 2022-01-05 thomas struct imsgbuf *ibuf)
306 3d97effa 2024-01-31 thomas const struct got_error *err;
307 bc1f382f 2022-01-05 thomas size_t datalen;
310 bc1f382f 2022-01-05 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
311 bc1f382f 2022-01-05 thomas if (datalen != 0)
312 bc1f382f 2022-01-05 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
314 3d97effa 2024-01-31 thomas fd = imsg_get_fd(imsg);
315 3d97effa 2024-01-31 thomas if (fd == -1)
316 bc1f382f 2022-01-05 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
318 3d97effa 2024-01-31 thomas *f = fdopen(fd, mode);
319 3d97effa 2024-01-31 thomas if (*f == NULL) {
320 3d97effa 2024-01-31 thomas err = got_error_from_errno("fdopen");
321 3d97effa 2024-01-31 thomas close(fd);
322 3d97effa 2024-01-31 thomas return err;
325 bc1f382f 2022-01-05 thomas return NULL;
328 3840f4c9 2018-09-12 stsp static const struct got_error *
329 3840f4c9 2018-09-12 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
330 bc1f382f 2022-01-05 thomas struct got_packidx *packidx, struct got_object_cache *objcache,
331 bc1f382f 2022-01-05 thomas FILE *basefile, FILE *accumfile)
333 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
334 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj;
335 3840f4c9 2018-09-12 stsp struct got_object *obj = NULL;
336 bc1f382f 2022-01-05 thomas FILE *outfile = NULL;
337 ebc55e2d 2018-12-24 stsp struct got_object_id id;
338 ebc55e2d 2018-12-24 stsp size_t datalen;
339 ac544f8c 2019-01-13 stsp uint64_t blob_size;
340 ac544f8c 2019-01-13 stsp uint8_t *buf = NULL;
342 ebc55e2d 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
343 ebc55e2d 2018-12-24 stsp if (datalen != sizeof(iobj))
344 ebc55e2d 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
345 ebc55e2d 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
346 b6f67730 2023-02-03 thomas memcpy(&id, &iobj.id, sizeof(id));
348 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
350 704b89c4 2019-05-23 stsp obj->refcnt++;
352 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
355 704b89c4 2019-05-23 stsp return err;
358 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
362 ac544f8c 2019-01-13 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
363 42c69117 2019-11-10 stsp err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
367 ac544f8c 2019-01-13 stsp blob_size = obj->size;
369 ac544f8c 2019-01-13 stsp if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
370 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
371 ac544f8c 2019-01-13 stsp obj, pack);
373 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
374 ac544f8c 2019-01-13 stsp accumfile);
378 ac544f8c 2019-01-13 stsp err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
381 56b63ca4 2021-01-22 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
382 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
383 cb5e38fd 2019-05-23 stsp got_object_close(obj);
384 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
385 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
387 55da3778 2018-09-10 stsp return err;
390 876c234b 2018-09-10 stsp static const struct got_error *
391 f4a881ce 2018-11-17 stsp tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
392 f4a881ce 2018-11-17 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
394 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
395 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj;
396 f4a881ce 2018-11-17 stsp struct got_object *obj = NULL;
397 f4a881ce 2018-11-17 stsp struct got_tag_object *tag = NULL;
398 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
399 f4a881ce 2018-11-17 stsp size_t len;
400 268f7291 2018-12-24 stsp struct got_object_id id;
401 268f7291 2018-12-24 stsp size_t datalen;
403 268f7291 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
404 268f7291 2018-12-24 stsp if (datalen != sizeof(iobj))
405 268f7291 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
406 268f7291 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
407 b6f67730 2023-02-03 thomas memcpy(&id, &iobj.id, sizeof(id));
409 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
411 704b89c4 2019-05-23 stsp obj->refcnt++;
413 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
416 704b89c4 2019-05-23 stsp return err;
419 f4a881ce 2018-11-17 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
423 f4a881ce 2018-11-17 stsp obj->size = len;
424 f4a881ce 2018-11-17 stsp err = got_object_parse_tag(&tag, buf, len);
428 f4a881ce 2018-11-17 stsp err = got_privsep_send_tag(ibuf, tag);
431 cb5e38fd 2019-05-23 stsp got_object_close(obj);
433 cb5e38fd 2019-05-23 stsp got_object_tag_close(tag);
435 ca6e02ac 2020-01-07 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
436 ca6e02ac 2020-01-07 stsp err = NULL;
438 ca6e02ac 2020-01-07 stsp got_privsep_send_error(ibuf, err);
441 ca6e02ac 2020-01-07 stsp return err;
444 8f1c06eb 2021-09-25 thomas.ad static const struct got_error *
445 2d28509d 2023-04-28 thomas tree_path_changed(int *changed, uint8_t **buf1, size_t *len1,
446 2d28509d 2023-04-28 thomas uint8_t **buf2, size_t *len2, const char *path,
447 2d28509d 2023-04-28 thomas struct got_pack *pack, struct got_packidx *packidx,
448 ca6e02ac 2020-01-07 stsp struct imsgbuf *ibuf, struct got_object_cache *objcache)
450 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
451 2d28509d 2023-04-28 thomas struct got_parsed_tree_entry pte1, pte2;
452 ca6e02ac 2020-01-07 stsp const char *seg, *s;
453 ca6e02ac 2020-01-07 stsp size_t seglen;
454 2d28509d 2023-04-28 thomas size_t remain1 = *len1, remain2 = *len2, elen;
455 2d28509d 2023-04-28 thomas uint8_t *next_entry1 = *buf1;
456 2d28509d 2023-04-28 thomas uint8_t *next_entry2 = *buf2;
458 2d28509d 2023-04-28 thomas memset(&pte1, 0, sizeof(pte1));
459 2d28509d 2023-04-28 thomas memset(&pte2, 0, sizeof(pte2));
461 ca6e02ac 2020-01-07 stsp *changed = 0;
463 ca6e02ac 2020-01-07 stsp /* We not do support comparing the root path. */
464 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
465 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
468 61a7d79f 2020-02-29 stsp while (*s == '/')
471 ca6e02ac 2020-01-07 stsp seglen = 0;
472 ca6e02ac 2020-01-07 stsp while (*s) {
473 ca6e02ac 2020-01-07 stsp if (*s != '/') {
481 2d28509d 2023-04-28 thomas * As an optimization we compare entries in on-disk order
482 2d28509d 2023-04-28 thomas * rather than in got_path_cmp() order. We only need to
483 2d28509d 2023-04-28 thomas * find out if any entries differ. Parsing all entries and
484 2d28509d 2023-04-28 thomas * sorting them slows us down significantly when tree objects
485 2d28509d 2023-04-28 thomas * have thousands of entries. We can assume that on-disk entry
486 2d28509d 2023-04-28 thomas * ordering is stable, as per got_object_tree_create() and
487 2d28509d 2023-04-28 thomas * sort_tree_entries_the_way_git_likes_it(). Other orderings
488 2d28509d 2023-04-28 thomas * are incompatible with Git and would yield false positives
489 2d28509d 2023-04-28 thomas * here, too.
491 2d28509d 2023-04-28 thomas while (remain1 > 0) {
492 2d28509d 2023-04-28 thomas err = got_object_parse_tree_entry(&pte1, &elen,
493 2d28509d 2023-04-28 thomas next_entry1, remain1);
495 2d28509d 2023-04-28 thomas return err;
496 2d28509d 2023-04-28 thomas next_entry1 += elen;
497 2d28509d 2023-04-28 thomas remain1 -= elen;
498 2d28509d 2023-04-28 thomas if (strncmp(pte1.name, seg, seglen) != 0 ||
499 2d28509d 2023-04-28 thomas pte1.name[seglen] != '\0') {
500 2d28509d 2023-04-28 thomas memset(&pte1, 0, sizeof(pte1));
501 2d28509d 2023-04-28 thomas continue;
505 2d28509d 2023-04-28 thomas if (pte1.name == NULL) {
506 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_NO_OBJ);
510 2d28509d 2023-04-28 thomas if (remain2 == 0) {
511 ca6e02ac 2020-01-07 stsp *changed = 1;
515 2d28509d 2023-04-28 thomas while (remain2 > 0) {
516 2d28509d 2023-04-28 thomas err = got_object_parse_tree_entry(&pte2, &elen,
517 2d28509d 2023-04-28 thomas next_entry2, remain2);
519 2d28509d 2023-04-28 thomas return err;
520 2d28509d 2023-04-28 thomas next_entry2 += elen;
521 2d28509d 2023-04-28 thomas remain2 -= elen;
522 2d28509d 2023-04-28 thomas if (strncmp(pte2.name, seg, seglen) != 0 ||
523 2d28509d 2023-04-28 thomas pte2.name[seglen] != '\0') {
524 2d28509d 2023-04-28 thomas memset(&pte2, 0, sizeof(pte2));
525 2d28509d 2023-04-28 thomas continue;
530 2d28509d 2023-04-28 thomas if (pte2.name == NULL) {
531 ca6e02ac 2020-01-07 stsp *changed = 1;
535 2d28509d 2023-04-28 thomas if (pte1.mode != pte2.mode) {
536 2d28509d 2023-04-28 thomas *changed = 1;
540 2d28509d 2023-04-28 thomas if (memcmp(pte1.id, pte2.id, SHA1_DIGEST_LENGTH) == 0) {
541 ca6e02ac 2020-01-07 stsp *changed = 0;
545 ca6e02ac 2020-01-07 stsp if (*s == '\0') { /* final path element */
546 ca6e02ac 2020-01-07 stsp *changed = 1;
550 ca6e02ac 2020-01-07 stsp seg = s + 1;
552 ca6e02ac 2020-01-07 stsp seglen = 0;
554 ca6e02ac 2020-01-07 stsp struct got_object_id id1, id2;
557 2d28509d 2023-04-28 thomas memcpy(id1.sha1, pte1.id, SHA1_DIGEST_LENGTH);
558 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id1);
559 ca6e02ac 2020-01-07 stsp if (idx == -1) {
560 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id1);
563 ca6e02ac 2020-01-07 stsp free(*buf1);
564 ca6e02ac 2020-01-07 stsp *buf1 = NULL;
565 2d28509d 2023-04-28 thomas err = open_tree(buf1, len1, pack, packidx, idx, &id1,
566 c77e00b3 2022-10-18 thomas objcache);
567 2d28509d 2023-04-28 thomas memset(&pte1, 0, sizeof(pte1));
570 2d28509d 2023-04-28 thomas next_entry1 = *buf1;
571 2d28509d 2023-04-28 thomas remain1 = *len1;
573 2d28509d 2023-04-28 thomas memcpy(id2.sha1, pte2.id, SHA1_DIGEST_LENGTH);
574 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id2);
575 ca6e02ac 2020-01-07 stsp if (idx == -1) {
576 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id2);
579 ca6e02ac 2020-01-07 stsp free(*buf2);
580 ca6e02ac 2020-01-07 stsp *buf2 = NULL;
581 2d28509d 2023-04-28 thomas err = open_tree(buf2, len2, pack, packidx, idx, &id2,
582 83592549 2023-02-17 thomas objcache);
583 2d28509d 2023-04-28 thomas memset(&pte2, 0, sizeof(pte2));
586 2d28509d 2023-04-28 thomas next_entry2 = *buf2;
587 2d28509d 2023-04-28 thomas remain2 = *len2;
591 ca6e02ac 2020-01-07 stsp return err;
594 ca6e02ac 2020-01-07 stsp static const struct got_error *
595 e70bf110 2020-03-22 stsp send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
596 e70bf110 2020-03-22 stsp struct imsgbuf *ibuf)
598 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
599 01bb5a15 2021-09-25 thomas.ad size_t i;
601 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
602 e70bf110 2020-03-22 stsp sizeof(struct got_imsg_traversed_commits) +
603 35cdfa2c 2023-02-20 thomas ncommits * sizeof(commit_ids[0]));
604 e70bf110 2020-03-22 stsp if (wbuf == NULL)
605 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
607 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
608 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
610 e70bf110 2020-03-22 stsp for (i = 0; i < ncommits; i++) {
611 e70bf110 2020-03-22 stsp struct got_object_id *id = &commit_ids[i];
612 35cdfa2c 2023-02-20 thomas if (imsg_add(wbuf, id, sizeof(*id)) == -1) {
613 e9f1a409 2022-05-19 thomas return got_error_from_errno(
614 e70bf110 2020-03-22 stsp "imsg_add TRAVERSED_COMMITS");
618 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
620 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
623 e70bf110 2020-03-22 stsp static const struct got_error *
624 e70bf110 2020-03-22 stsp send_commit_traversal_done(struct imsgbuf *ibuf)
626 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
627 e70bf110 2020-03-22 stsp NULL, 0) == -1)
628 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
630 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
633 e70bf110 2020-03-22 stsp static const struct got_error *
634 ca6e02ac 2020-01-07 stsp commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
635 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx,
636 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
638 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
639 a578d6ec 2023-03-01 thomas struct got_imsg_commit_traversal_request ctreq;
640 ca6e02ac 2020-01-07 stsp struct got_object_qid *pid;
641 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL, *pcommit = NULL;
642 ca6e02ac 2020-01-07 stsp struct got_object_id id;
643 a578d6ec 2023-03-01 thomas size_t datalen;
644 ca6e02ac 2020-01-07 stsp char *path = NULL;
645 ca6e02ac 2020-01-07 stsp const int min_alloc = 64;
646 ca6e02ac 2020-01-07 stsp int changed = 0, ncommits = 0, nallocated = 0;
647 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_ids = NULL;
649 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
650 a578d6ec 2023-03-01 thomas if (datalen < sizeof(ctreq))
651 a578d6ec 2023-03-01 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
652 a578d6ec 2023-03-01 thomas memcpy(&ctreq, imsg->data, sizeof(ctreq));
653 a578d6ec 2023-03-01 thomas memcpy(&id, &ctreq.iobj.id, sizeof(id));
655 a578d6ec 2023-03-01 thomas if (datalen != sizeof(ctreq) + ctreq.path_len)
656 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
657 a578d6ec 2023-03-01 thomas if (ctreq.path_len == 0)
658 a578d6ec 2023-03-01 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
660 a578d6ec 2023-03-01 thomas path = strndup(imsg->data + sizeof(ctreq), ctreq.path_len);
661 a578d6ec 2023-03-01 thomas if (path == NULL)
662 a578d6ec 2023-03-01 thomas return got_error_from_errno("strndup");
664 ca6e02ac 2020-01-07 stsp nallocated = min_alloc;
665 ca6e02ac 2020-01-07 stsp commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
666 ca6e02ac 2020-01-07 stsp if (commit_ids == NULL)
667 ca6e02ac 2020-01-07 stsp return got_error_from_errno("reallocarray");
670 ca6e02ac 2020-01-07 stsp const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
673 ca6e02ac 2020-01-07 stsp if (sigint_received) {
674 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_CANCELLED);
678 ca6e02ac 2020-01-07 stsp if (commit == NULL) {
679 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx, &id);
680 ca6e02ac 2020-01-07 stsp if (idx == -1)
682 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx,
683 ca6e02ac 2020-01-07 stsp idx, &id, objcache);
685 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
687 ca6e02ac 2020-01-07 stsp err = NULL;
692 ca6e02ac 2020-01-07 stsp if (sizeof(struct got_imsg_traversed_commits) +
693 35cdfa2c 2023-02-20 thomas ncommits * sizeof(commit_ids[0]) >= max_datalen) {
694 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits,
698 ca6e02ac 2020-01-07 stsp ncommits = 0;
700 ca6e02ac 2020-01-07 stsp ncommits++;
701 ca6e02ac 2020-01-07 stsp if (ncommits > nallocated) {
702 ca6e02ac 2020-01-07 stsp struct got_object_id *new;
703 ca6e02ac 2020-01-07 stsp nallocated += min_alloc;
704 ca6e02ac 2020-01-07 stsp new = reallocarray(commit_ids, nallocated,
705 ca6e02ac 2020-01-07 stsp sizeof(*commit_ids));
706 ca6e02ac 2020-01-07 stsp if (new == NULL) {
707 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("reallocarray");
710 ca6e02ac 2020-01-07 stsp commit_ids = new;
712 c5b519a9 2023-02-20 thomas memcpy(&commit_ids[ncommits - 1], &id, sizeof(id));
714 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(&commit->parent_ids);
715 ca6e02ac 2020-01-07 stsp if (pid == NULL)
718 ec242592 2022-04-22 thomas idx = got_packidx_get_object_idx(packidx, &pid->id);
719 ca6e02ac 2020-01-07 stsp if (idx == -1)
722 ec242592 2022-04-22 thomas err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
725 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
727 ca6e02ac 2020-01-07 stsp err = NULL;
731 ca6e02ac 2020-01-07 stsp if (path[0] == '/' && path[1] == '\0') {
732 ca6e02ac 2020-01-07 stsp if (got_object_id_cmp(pcommit->tree_id,
733 ca6e02ac 2020-01-07 stsp commit->tree_id) != 0) {
734 ca6e02ac 2020-01-07 stsp changed = 1;
739 ca6e02ac 2020-01-07 stsp uint8_t *buf = NULL, *pbuf = NULL;
740 2d28509d 2023-04-28 thomas size_t len = 0, plen = 0;
742 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx,
743 ca6e02ac 2020-01-07 stsp commit->tree_id);
744 ca6e02ac 2020-01-07 stsp if (idx == -1)
746 ca6e02ac 2020-01-07 stsp pidx = got_packidx_get_object_idx(packidx,
747 ca6e02ac 2020-01-07 stsp pcommit->tree_id);
748 ca6e02ac 2020-01-07 stsp if (pidx == -1)
751 2d28509d 2023-04-28 thomas err = open_tree(&buf, &len, pack, packidx, idx,
752 c77e00b3 2022-10-18 thomas commit->tree_id, objcache);
756 2d28509d 2023-04-28 thomas err = open_tree(&pbuf, &plen, pack, packidx, pidx,
757 c77e00b3 2022-10-18 thomas pcommit->tree_id, objcache);
763 2d28509d 2023-04-28 thomas err = tree_path_changed(&changed, &buf, &len,
764 2d28509d 2023-04-28 thomas &pbuf, &plen, path, pack, packidx, ibuf,
765 2d28509d 2023-04-28 thomas objcache);
768 ca6e02ac 2020-01-07 stsp free(pbuf);
770 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
772 ca6e02ac 2020-01-07 stsp err = NULL;
777 ca6e02ac 2020-01-07 stsp if (!changed) {
778 c5b519a9 2023-02-20 thomas memcpy(&id, &pid->id, sizeof(id));
779 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
780 ca6e02ac 2020-01-07 stsp commit = pcommit;
781 ca6e02ac 2020-01-07 stsp pcommit = NULL;
783 ca6e02ac 2020-01-07 stsp } while (!changed);
785 ca6e02ac 2020-01-07 stsp if (ncommits > 0) {
786 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits, ibuf);
790 ca6e02ac 2020-01-07 stsp if (changed) {
791 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit(ibuf, commit);
796 e70bf110 2020-03-22 stsp err = send_commit_traversal_done(ibuf);
798 a578d6ec 2023-03-01 thomas free(path);
799 ca6e02ac 2020-01-07 stsp free(commit_ids);
800 ca6e02ac 2020-01-07 stsp if (commit)
801 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
802 ca6e02ac 2020-01-07 stsp if (pcommit)
803 ca6e02ac 2020-01-07 stsp got_object_commit_close(pcommit);
805 f4a881ce 2018-11-17 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
806 f4a881ce 2018-11-17 stsp err = NULL;
808 f4a881ce 2018-11-17 stsp got_privsep_send_error(ibuf, err);
811 f4a881ce 2018-11-17 stsp return err;
814 f4a881ce 2018-11-17 stsp static const struct got_error *
815 48b4f239 2021-12-31 thomas raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
816 48b4f239 2021-12-31 thomas struct got_pack *pack, struct got_packidx *packidx,
817 bc1f382f 2022-01-05 thomas struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
819 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
820 59d1e4a0 2021-03-10 stsp uint8_t *buf = NULL;
821 59d1e4a0 2021-03-10 stsp uint64_t size = 0;
822 bc1f382f 2022-01-05 thomas FILE *outfile = NULL;
823 59d1e4a0 2021-03-10 stsp struct got_imsg_packed_object iobj;
824 59d1e4a0 2021-03-10 stsp struct got_object *obj;
825 59d1e4a0 2021-03-10 stsp struct got_object_id id;
826 59d1e4a0 2021-03-10 stsp size_t datalen;
828 59d1e4a0 2021-03-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
829 59d1e4a0 2021-03-10 stsp if (datalen != sizeof(iobj))
830 59d1e4a0 2021-03-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
831 59d1e4a0 2021-03-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
832 b6f67730 2023-02-03 thomas memcpy(&id, &iobj.id, sizeof(id));
834 59d1e4a0 2021-03-10 stsp obj = got_object_cache_get(objcache, &id);
836 59d1e4a0 2021-03-10 stsp obj->refcnt++;
838 59d1e4a0 2021-03-10 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
841 59d1e4a0 2021-03-10 stsp return err;
844 59d1e4a0 2021-03-10 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
846 59d1e4a0 2021-03-10 stsp return err;
848 59d1e4a0 2021-03-10 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
849 59d1e4a0 2021-03-10 stsp err = got_pack_get_max_delta_object_size(&size, obj, pack);
853 59d1e4a0 2021-03-10 stsp size = obj->size;
855 59d1e4a0 2021-03-10 stsp if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
856 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
857 59d1e4a0 2021-03-10 stsp obj, pack);
859 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
860 59d1e4a0 2021-03-10 stsp accumfile);
864 40e3cb72 2021-06-22 stsp err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
867 59d1e4a0 2021-03-10 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
868 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fclose");
869 59d1e4a0 2021-03-10 stsp got_object_close(obj);
870 59d1e4a0 2021-03-10 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
871 59d1e4a0 2021-03-10 stsp got_privsep_send_error(ibuf, err);
873 59d1e4a0 2021-03-10 stsp return err;
876 f9c2e8e5 2022-02-13 thomas static const struct got_error *
877 f9c2e8e5 2022-02-13 thomas get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
878 f9c2e8e5 2022-02-13 thomas off_t base_offset)
880 f9c2e8e5 2022-02-13 thomas const struct got_error *err;
883 b6b86fd1 2022-08-30 thomas err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
885 f9c2e8e5 2022-02-13 thomas return err;
886 f9c2e8e5 2022-02-13 thomas if (idx == -1)
887 f9c2e8e5 2022-02-13 thomas return got_error(GOT_ERR_BAD_PACKIDX);
889 f9c2e8e5 2022-02-13 thomas return got_packidx_get_object_id(base_id, packidx, idx);
892 59d1e4a0 2021-03-10 stsp static const struct got_error *
893 f9c2e8e5 2022-02-13 thomas raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
894 f9c2e8e5 2022-02-13 thomas FILE *delta_outfile, struct got_pack *pack,
895 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx)
897 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
898 f9c2e8e5 2022-02-13 thomas struct got_imsg_raw_delta_request req;
899 9249e7e3 2022-05-12 thomas size_t datalen, delta_size, delta_compressed_size;
900 c44c7d6e 2022-12-04 thomas off_t delta_offset, delta_data_offset;
901 f9c2e8e5 2022-02-13 thomas uint8_t *delta_buf = NULL;
902 f9c2e8e5 2022-02-13 thomas struct got_object_id id, base_id;
903 f9c2e8e5 2022-02-13 thomas off_t base_offset, delta_out_offset = 0;
904 f9c2e8e5 2022-02-13 thomas uint64_t base_size = 0, result_size = 0;
905 f9c2e8e5 2022-02-13 thomas size_t w;
907 f9c2e8e5 2022-02-13 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
908 f9c2e8e5 2022-02-13 thomas if (datalen != sizeof(req))
909 f9c2e8e5 2022-02-13 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
910 f9c2e8e5 2022-02-13 thomas memcpy(&req, imsg->data, sizeof(req));
911 d77295e3 2023-02-03 thomas memcpy(&id, &req.id, sizeof(id));
913 f9c2e8e5 2022-02-13 thomas err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
914 c44c7d6e 2022-12-04 thomas &delta_compressed_size, &delta_offset, &delta_data_offset,
915 c44c7d6e 2022-12-04 thomas &base_offset, &base_id, &base_size, &result_size,
916 c44c7d6e 2022-12-04 thomas pack, packidx, req.idx);
918 f9c2e8e5 2022-02-13 thomas goto done;
921 f9c2e8e5 2022-02-13 thomas * If this is an offset delta we must determine the base
922 f9c2e8e5 2022-02-13 thomas * object ID ourselves.
924 f9c2e8e5 2022-02-13 thomas if (base_offset != 0) {
925 f9c2e8e5 2022-02-13 thomas err = get_base_object_id(&base_id, packidx, base_offset);
927 f9c2e8e5 2022-02-13 thomas goto done;
930 f9c2e8e5 2022-02-13 thomas delta_out_offset = ftello(delta_outfile);
931 9249e7e3 2022-05-12 thomas w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
932 9249e7e3 2022-05-12 thomas if (w != delta_compressed_size) {
933 f9c2e8e5 2022-02-13 thomas err = got_ferror(delta_outfile, GOT_ERR_IO);
934 f9c2e8e5 2022-02-13 thomas goto done;
936 f9c2e8e5 2022-02-13 thomas if (fflush(delta_outfile) == -1) {
937 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fflush");
938 f9c2e8e5 2022-02-13 thomas goto done;
941 f9c2e8e5 2022-02-13 thomas err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
942 9249e7e3 2022-05-12 thomas delta_size, delta_compressed_size, delta_offset, delta_out_offset,
943 9249e7e3 2022-05-12 thomas &base_id);
945 f9c2e8e5 2022-02-13 thomas free(delta_buf);
946 f9c2e8e5 2022-02-13 thomas return err;
949 7d0d4920 2022-05-12 thomas struct search_deltas_arg {
950 7d0d4920 2022-05-12 thomas struct imsgbuf *ibuf;
951 7d0d4920 2022-05-12 thomas struct got_packidx *packidx;
952 7d0d4920 2022-05-12 thomas struct got_pack *pack;
953 7d0d4920 2022-05-12 thomas struct got_object_idset *idset;
954 7d0d4920 2022-05-12 thomas struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
955 7d0d4920 2022-05-12 thomas size_t ndeltas;
958 f9c2e8e5 2022-02-13 thomas static const struct got_error *
959 7d0d4920 2022-05-12 thomas search_delta_for_object(struct got_object_id *id, void *data, void *arg)
961 7d0d4920 2022-05-12 thomas const struct got_error *err;
962 7d0d4920 2022-05-12 thomas struct search_deltas_arg *a = arg;
963 7d0d4920 2022-05-12 thomas int obj_idx;
964 7d0d4920 2022-05-12 thomas uint8_t *delta_buf = NULL;
965 7d0d4920 2022-05-12 thomas uint64_t base_size, result_size;
966 7d0d4920 2022-05-12 thomas size_t delta_size, delta_compressed_size;
967 c44c7d6e 2022-12-04 thomas off_t delta_offset, delta_data_offset, base_offset;
968 7d0d4920 2022-05-12 thomas struct got_object_id base_id;
970 7d0d4920 2022-05-12 thomas if (sigint_received)
971 7d0d4920 2022-05-12 thomas return got_error(GOT_ERR_CANCELLED);
973 7d0d4920 2022-05-12 thomas obj_idx = got_packidx_get_object_idx(a->packidx, id);
974 7d0d4920 2022-05-12 thomas if (obj_idx == -1)
975 7d0d4920 2022-05-12 thomas return NULL; /* object not present in our pack file */
977 7d0d4920 2022-05-12 thomas err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
978 c44c7d6e 2022-12-04 thomas &delta_compressed_size, &delta_offset, &delta_data_offset,
979 c44c7d6e 2022-12-04 thomas &base_offset, &base_id, &base_size, &result_size,
980 c44c7d6e 2022-12-04 thomas a->pack, a->packidx, obj_idx);
981 7d0d4920 2022-05-12 thomas if (err) {
982 7d0d4920 2022-05-12 thomas if (err->code == GOT_ERR_OBJ_TYPE)
983 7d0d4920 2022-05-12 thomas return NULL; /* object not stored as a delta */
984 7d0d4920 2022-05-12 thomas return err;
988 7d0d4920 2022-05-12 thomas * If this is an offset delta we must determine the base
989 7d0d4920 2022-05-12 thomas * object ID ourselves.
991 7d0d4920 2022-05-12 thomas if (base_offset != 0) {
992 7d0d4920 2022-05-12 thomas err = get_base_object_id(&base_id, a->packidx, base_offset);
994 7d0d4920 2022-05-12 thomas goto done;
997 7d0d4920 2022-05-12 thomas if (got_object_idset_contains(a->idset, &base_id)) {
998 7d0d4920 2022-05-12 thomas struct got_imsg_reused_delta *delta;
1000 7d0d4920 2022-05-12 thomas delta = &a->deltas[a->ndeltas++];
1001 7d0d4920 2022-05-12 thomas memcpy(&delta->id, id, sizeof(delta->id));
1002 7d0d4920 2022-05-12 thomas memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
1003 7d0d4920 2022-05-12 thomas delta->base_size = base_size;
1004 7d0d4920 2022-05-12 thomas delta->result_size = result_size;
1005 7d0d4920 2022-05-12 thomas delta->delta_size = delta_size;
1006 7d0d4920 2022-05-12 thomas delta->delta_compressed_size = delta_compressed_size;
1007 c44c7d6e 2022-12-04 thomas delta->delta_offset = delta_data_offset;
1009 7d0d4920 2022-05-12 thomas if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
1010 7d0d4920 2022-05-12 thomas err = got_privsep_send_reused_deltas(a->ibuf,
1011 7d0d4920 2022-05-12 thomas a->deltas, a->ndeltas);
1012 7d0d4920 2022-05-12 thomas if (err)
1013 7d0d4920 2022-05-12 thomas goto done;
1014 7d0d4920 2022-05-12 thomas a->ndeltas = 0;
1018 7d0d4920 2022-05-12 thomas free(delta_buf);
1019 7d0d4920 2022-05-12 thomas return err;
1022 7d0d4920 2022-05-12 thomas static const struct got_error *
1023 7d0d4920 2022-05-12 thomas recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
1025 7d0d4920 2022-05-12 thomas const struct got_error *err = NULL;
1026 7d0d4920 2022-05-12 thomas int done = 0;
1027 7d0d4920 2022-05-12 thomas struct got_object_id *ids;
1028 7d0d4920 2022-05-12 thomas size_t nids, i;
1030 7d0d4920 2022-05-12 thomas for (;;) {
1031 7d0d4920 2022-05-12 thomas err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1032 7d0d4920 2022-05-12 thomas if (err || done)
1034 7d0d4920 2022-05-12 thomas for (i = 0; i < nids; i++) {
1035 7d0d4920 2022-05-12 thomas err = got_object_idset_add(idset, &ids[i], NULL);
1036 7d0d4920 2022-05-12 thomas if (err) {
1037 7d0d4920 2022-05-12 thomas free(ids);
1038 7d0d4920 2022-05-12 thomas return err;
1041 7d0d4920 2022-05-12 thomas free(ids);
1044 63915ee5 2022-06-23 thomas return err;
1047 63915ee5 2022-06-23 thomas static const struct got_error *
1048 47f02830 2023-01-27 thomas recv_object_id_queue(struct got_object_id_queue *queue,
1049 47f02830 2023-01-27 thomas struct got_object_idset *queued_ids, struct imsgbuf *ibuf)
1051 63915ee5 2022-06-23 thomas const struct got_error *err = NULL;
1052 63915ee5 2022-06-23 thomas int done = 0;
1053 63915ee5 2022-06-23 thomas struct got_object_qid *qid;
1054 63915ee5 2022-06-23 thomas struct got_object_id *ids;
1055 63915ee5 2022-06-23 thomas size_t nids, i;
1057 63915ee5 2022-06-23 thomas for (;;) {
1058 63915ee5 2022-06-23 thomas err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1059 63915ee5 2022-06-23 thomas if (err || done)
1061 63915ee5 2022-06-23 thomas for (i = 0; i < nids; i++) {
1062 63915ee5 2022-06-23 thomas err = got_object_qid_alloc_partial(&qid);
1063 63915ee5 2022-06-23 thomas if (err)
1064 63915ee5 2022-06-23 thomas return err;
1065 63915ee5 2022-06-23 thomas memcpy(&qid->id, &ids[i], sizeof(qid->id));
1066 63915ee5 2022-06-23 thomas STAILQ_INSERT_TAIL(queue, qid, entry);
1067 47f02830 2023-01-27 thomas err = got_object_idset_add(queued_ids, &qid->id, NULL);
1068 47f02830 2023-01-27 thomas if (err)
1069 47f02830 2023-01-27 thomas return err;
1073 13280750 2022-06-13 thomas return err;
1076 13280750 2022-06-13 thomas static const struct got_error *
1077 7d0d4920 2022-05-12 thomas delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1078 c44c7d6e 2022-12-04 thomas struct got_pack *pack, struct got_packidx *packidx)
1080 7d0d4920 2022-05-12 thomas const struct got_error *err = NULL;
1081 7d0d4920 2022-05-12 thomas struct got_object_idset *idset;
1082 7d0d4920 2022-05-12 thomas struct search_deltas_arg sda;
1084 7d0d4920 2022-05-12 thomas idset = got_object_idset_alloc();
1085 7d0d4920 2022-05-12 thomas if (idset == NULL)
1086 7d0d4920 2022-05-12 thomas return got_error_from_errno("got_object_idset_alloc");
1088 7d0d4920 2022-05-12 thomas err = recv_object_ids(idset, ibuf);
1089 7d0d4920 2022-05-12 thomas if (err)
1090 7d0d4920 2022-05-12 thomas return err;
1092 7d0d4920 2022-05-12 thomas memset(&sda, 0, sizeof(sda));
1093 7d0d4920 2022-05-12 thomas sda.ibuf = ibuf;
1094 7d0d4920 2022-05-12 thomas sda.idset = idset;
1095 7d0d4920 2022-05-12 thomas sda.pack = pack;
1096 7d0d4920 2022-05-12 thomas sda.packidx = packidx;
1097 7d0d4920 2022-05-12 thomas err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1098 7d0d4920 2022-05-12 thomas if (err)
1099 7d0d4920 2022-05-12 thomas goto done;
1101 7d0d4920 2022-05-12 thomas if (sda.ndeltas > 0) {
1102 7d0d4920 2022-05-12 thomas err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1103 7d0d4920 2022-05-12 thomas sda.ndeltas);
1104 7d0d4920 2022-05-12 thomas if (err)
1105 7d0d4920 2022-05-12 thomas goto done;
1108 7d0d4920 2022-05-12 thomas err = got_privsep_send_reused_deltas_done(ibuf);
1110 7d0d4920 2022-05-12 thomas got_object_idset_free(idset);
1111 7d0d4920 2022-05-12 thomas return err;
1114 7d0d4920 2022-05-12 thomas static const struct got_error *
1115 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1117 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1118 876c234b 2018-09-10 stsp struct imsg imsg;
1119 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1120 876c234b 2018-09-10 stsp size_t datalen;
1121 876c234b 2018-09-10 stsp struct got_packidx *p;
1123 876c234b 2018-09-10 stsp *packidx = NULL;
1125 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1127 876c234b 2018-09-10 stsp return err;
1129 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
1130 876c234b 2018-09-10 stsp if (p == NULL) {
1131 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1132 876c234b 2018-09-10 stsp goto done;
1135 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1136 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1137 876c234b 2018-09-10 stsp goto done;
1140 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1141 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
1142 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1143 876c234b 2018-09-10 stsp goto done;
1145 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1147 3d97effa 2024-01-31 thomas p->fd = imsg_get_fd(&imsg);
1148 876c234b 2018-09-10 stsp p->len = ipackidx.len;
1149 876c234b 2018-09-10 stsp if (p->fd == -1) {
1150 3d97effa 2024-01-31 thomas err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1151 56bef47a 2018-09-15 stsp goto done;
1153 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
1154 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1155 876c234b 2018-09-10 stsp goto done;
1158 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
1159 aa75acde 2022-10-25 thomas if (p->len > 0 && p->len <= SIZE_MAX) {
1160 aa75acde 2022-10-25 thomas p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1161 aa75acde 2022-10-25 thomas if (p->map == MAP_FAILED)
1162 aa75acde 2022-10-25 thomas p->map = NULL; /* fall back to read(2) */
1165 c3564dfa 2021-07-15 stsp err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1167 876c234b 2018-09-10 stsp if (err) {
1168 3d97effa 2024-01-31 thomas if (p != NULL)
1169 3d97effa 2024-01-31 thomas got_packidx_close(p);
1171 876c234b 2018-09-10 stsp *packidx = p;
1172 876c234b 2018-09-10 stsp imsg_free(&imsg);
1173 63915ee5 2022-06-23 thomas return err;
1176 63915ee5 2022-06-23 thomas static const struct got_error *
1177 63915ee5 2022-06-23 thomas send_tree_enumeration_done(struct imsgbuf *ibuf)
1179 63915ee5 2022-06-23 thomas if (imsg_compose(ibuf, GOT_IMSG_TREE_ENUMERATION_DONE, 0, 0, -1,
1180 63915ee5 2022-06-23 thomas NULL, 0) == -1)
1181 63915ee5 2022-06-23 thomas return got_error_from_errno("imsg_compose TREE_ENUMERATION_DONE");
1183 63915ee5 2022-06-23 thomas return got_privsep_flush_imsg(ibuf);
1186 63915ee5 2022-06-23 thomas struct enumerated_tree {
1187 63915ee5 2022-06-23 thomas struct got_object_id id;
1188 63915ee5 2022-06-23 thomas char *path;
1189 63915ee5 2022-06-23 thomas uint8_t *buf;
1190 63915ee5 2022-06-23 thomas struct got_parsed_tree_entry *entries;
1191 63915ee5 2022-06-23 thomas int nentries;
1194 63915ee5 2022-06-23 thomas static const struct got_error *
1195 63915ee5 2022-06-23 thomas enumerate_tree(int *have_all_entries, struct imsgbuf *ibuf, size_t *totlen,
1196 63915ee5 2022-06-23 thomas struct got_object_id *tree_id,
1197 63915ee5 2022-06-23 thomas const char *path, struct got_pack *pack, struct got_packidx *packidx,
1198 63915ee5 2022-06-23 thomas struct got_object_cache *objcache, struct got_object_idset *idset,
1199 63915ee5 2022-06-23 thomas struct enumerated_tree **trees, size_t *nalloc, size_t *ntrees)
1201 63915ee5 2022-06-23 thomas const struct got_error *err = NULL;
1202 63915ee5 2022-06-23 thomas struct got_object_id_queue ids;
1203 63915ee5 2022-06-23 thomas struct got_object_qid *qid;
1204 63915ee5 2022-06-23 thomas uint8_t *buf = NULL;
1205 2d28509d 2023-04-28 thomas size_t len = 0;
1206 63915ee5 2022-06-23 thomas struct got_parsed_tree_entry *entries = NULL;
1207 c77e00b3 2022-10-18 thomas size_t nentries = 0, nentries_alloc = 0, i;
1208 63915ee5 2022-06-23 thomas struct enumerated_tree *tree;
1210 63915ee5 2022-06-23 thomas *ntrees = 0;
1211 63915ee5 2022-06-23 thomas *have_all_entries = 1;
1212 63915ee5 2022-06-23 thomas STAILQ_INIT(&ids);
1214 63915ee5 2022-06-23 thomas err = got_object_qid_alloc_partial(&qid);
1215 63915ee5 2022-06-23 thomas if (err)
1216 63915ee5 2022-06-23 thomas return err;
1217 35cdfa2c 2023-02-20 thomas memcpy(&qid->id, tree_id, sizeof(*tree_id));
1218 63915ee5 2022-06-23 thomas qid->data = strdup(path);
1219 63915ee5 2022-06-23 thomas if (qid->data == NULL) {
1220 63915ee5 2022-06-23 thomas err = got_error_from_errno("strdup");
1221 63915ee5 2022-06-23 thomas goto done;
1223 63915ee5 2022-06-23 thomas STAILQ_INSERT_TAIL(&ids, qid, entry);
1224 63915ee5 2022-06-23 thomas qid = NULL;
1226 63915ee5 2022-06-23 thomas /* Traverse the tree hierarchy, gather tree object IDs and paths. */
1228 63915ee5 2022-06-23 thomas const char *path;
1229 63915ee5 2022-06-23 thomas int idx, i;
1231 63915ee5 2022-06-23 thomas if (sigint_received) {
1232 63915ee5 2022-06-23 thomas err = got_error(GOT_ERR_CANCELLED);
1233 63915ee5 2022-06-23 thomas goto done;
1236 63915ee5 2022-06-23 thomas qid = STAILQ_FIRST(&ids);
1237 63915ee5 2022-06-23 thomas STAILQ_REMOVE_HEAD(&ids, entry);
1238 63915ee5 2022-06-23 thomas path = qid->data;
1240 63915ee5 2022-06-23 thomas idx = got_packidx_get_object_idx(packidx, &qid->id);
1241 63915ee5 2022-06-23 thomas if (idx == -1) {
1242 63915ee5 2022-06-23 thomas *have_all_entries = 0;
1246 2d28509d 2023-04-28 thomas err = open_tree(&buf, &len, pack, packidx, idx, &qid->id,
1247 2d28509d 2023-04-28 thomas objcache);
1248 63915ee5 2022-06-23 thomas if (err) {
1249 63915ee5 2022-06-23 thomas if (err->code != GOT_ERR_NO_OBJ)
1250 63915ee5 2022-06-23 thomas goto done;
1253 2d28509d 2023-04-28 thomas err = got_object_parse_tree(&entries, &nentries,
1254 2d28509d 2023-04-28 thomas &nentries_alloc, buf, len);
1255 2d28509d 2023-04-28 thomas if (err)
1256 2d28509d 2023-04-28 thomas goto done;
1258 63915ee5 2022-06-23 thomas err = got_object_idset_add(idset, &qid->id, NULL);
1259 63915ee5 2022-06-23 thomas if (err)
1260 63915ee5 2022-06-23 thomas goto done;
1262 63915ee5 2022-06-23 thomas for (i = 0; i < nentries; i++) {
1263 63915ee5 2022-06-23 thomas struct got_object_qid *eqid = NULL;
1264 63915ee5 2022-06-23 thomas struct got_parsed_tree_entry *pte = &entries[i];
1265 63915ee5 2022-06-23 thomas char *p;
1267 63915ee5 2022-06-23 thomas if (!S_ISDIR(pte->mode))
1268 63915ee5 2022-06-23 thomas continue;
1270 63915ee5 2022-06-23 thomas err = got_object_qid_alloc_partial(&eqid);
1271 63915ee5 2022-06-23 thomas if (err)
1272 63915ee5 2022-06-23 thomas goto done;
1273 63915ee5 2022-06-23 thomas memcpy(eqid->id.sha1, pte->id, sizeof(eqid->id.sha1));
1275 63915ee5 2022-06-23 thomas if (got_object_idset_contains(idset, &eqid->id)) {
1276 63915ee5 2022-06-23 thomas got_object_qid_free(eqid);
1277 63915ee5 2022-06-23 thomas continue;
1280 63915ee5 2022-06-23 thomas if (asprintf(&p, "%s%s%s", path,
1281 63915ee5 2022-06-23 thomas got_path_is_root_dir(path) ? "" : "/",
1282 63915ee5 2022-06-23 thomas pte->name) == -1) {
1283 63915ee5 2022-06-23 thomas err = got_error_from_errno("asprintf");
1284 63915ee5 2022-06-23 thomas got_object_qid_free(eqid);
1285 63915ee5 2022-06-23 thomas goto done;
1287 63915ee5 2022-06-23 thomas eqid->data = p;
1288 63915ee5 2022-06-23 thomas STAILQ_INSERT_TAIL(&ids, eqid, entry);
1291 63915ee5 2022-06-23 thomas if (*ntrees >= *nalloc) {
1292 63915ee5 2022-06-23 thomas struct enumerated_tree *new;
1293 63915ee5 2022-06-23 thomas new = recallocarray(*trees, *nalloc, *nalloc + 16,
1294 63915ee5 2022-06-23 thomas sizeof(*new));
1295 63915ee5 2022-06-23 thomas if (new == NULL) {
1296 63915ee5 2022-06-23 thomas err = got_error_from_errno("malloc");
1297 63915ee5 2022-06-23 thomas goto done;
1299 63915ee5 2022-06-23 thomas *trees = new;
1300 63915ee5 2022-06-23 thomas *nalloc += 16;
1302 63915ee5 2022-06-23 thomas tree = &(*trees)[*ntrees];
1303 63915ee5 2022-06-23 thomas (*ntrees)++;
1304 63915ee5 2022-06-23 thomas memcpy(&tree->id, &qid->id, sizeof(tree->id));
1305 63915ee5 2022-06-23 thomas tree->path = qid->data;
1306 63915ee5 2022-06-23 thomas tree->buf = buf;
1307 63915ee5 2022-06-23 thomas buf = NULL;
1308 63915ee5 2022-06-23 thomas tree->entries = entries;
1309 63915ee5 2022-06-23 thomas entries = NULL;
1310 c77e00b3 2022-10-18 thomas nentries_alloc = 0;
1311 63915ee5 2022-06-23 thomas tree->nentries = nentries;
1312 c77e00b3 2022-10-18 thomas nentries = 0;
1314 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1315 63915ee5 2022-06-23 thomas qid = NULL;
1316 63915ee5 2022-06-23 thomas } while (!STAILQ_EMPTY(&ids));
1318 63915ee5 2022-06-23 thomas if (*have_all_entries) {
1321 63915ee5 2022-06-23 thomas * We have managed to traverse all entries in the hierarchy.
1322 63915ee5 2022-06-23 thomas * Tell the main process what we have found.
1324 63915ee5 2022-06-23 thomas for (i = 0; i < *ntrees; i++) {
1325 63915ee5 2022-06-23 thomas tree = &(*trees)[i];
1326 63915ee5 2022-06-23 thomas err = got_privsep_send_enumerated_tree(totlen,
1327 63915ee5 2022-06-23 thomas ibuf, &tree->id, tree->path, tree->entries,
1328 63915ee5 2022-06-23 thomas tree->nentries);
1329 63915ee5 2022-06-23 thomas if (err)
1330 63915ee5 2022-06-23 thomas goto done;
1331 63915ee5 2022-06-23 thomas free(tree->buf);
1332 63915ee5 2022-06-23 thomas tree->buf = NULL;
1333 63915ee5 2022-06-23 thomas free(tree->path);
1334 63915ee5 2022-06-23 thomas tree->path = NULL;
1335 63915ee5 2022-06-23 thomas free(tree->entries);
1336 63915ee5 2022-06-23 thomas tree->entries = NULL;
1338 63915ee5 2022-06-23 thomas *ntrees = 0; /* don't loop again below to free memory */
1340 63915ee5 2022-06-23 thomas err = send_tree_enumeration_done(ibuf);
1341 63915ee5 2022-06-23 thomas } else {
1343 63915ee5 2022-06-23 thomas * We can only load fully packed tree hierarchies on
1344 63915ee5 2022-06-23 thomas * behalf of the main process, otherwise the main process
1345 63915ee5 2022-06-23 thomas * gets a wrong idea about which tree objects have
1346 63915ee5 2022-06-23 thomas * already been traversed.
1347 63915ee5 2022-06-23 thomas * Indicate a missing entry for the root of this tree.
1348 63915ee5 2022-06-23 thomas * The main process should continue by loading this
1349 63915ee5 2022-06-23 thomas * entire tree the slow way.
1351 63915ee5 2022-06-23 thomas err = got_privsep_send_enumerated_tree(totlen, ibuf,
1352 63915ee5 2022-06-23 thomas tree_id, "/", NULL, -1);
1353 63915ee5 2022-06-23 thomas if (err)
1354 63915ee5 2022-06-23 thomas goto done;
1357 63915ee5 2022-06-23 thomas free(buf);
1358 63915ee5 2022-06-23 thomas free(entries);
1359 63915ee5 2022-06-23 thomas for (i = 0; i < *ntrees; i++) {
1360 63915ee5 2022-06-23 thomas tree = &(*trees)[i];
1361 63915ee5 2022-06-23 thomas free(tree->buf);
1362 63915ee5 2022-06-23 thomas tree->buf = NULL;
1363 63915ee5 2022-06-23 thomas free(tree->path);
1364 63915ee5 2022-06-23 thomas tree->path = NULL;
1365 63915ee5 2022-06-23 thomas free(tree->entries);
1366 63915ee5 2022-06-23 thomas tree->entries = NULL;
1368 63915ee5 2022-06-23 thomas if (qid)
1369 63915ee5 2022-06-23 thomas free(qid->data);
1370 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1371 63915ee5 2022-06-23 thomas got_object_id_queue_free(&ids);
1372 63915ee5 2022-06-23 thomas if (err) {
1373 63915ee5 2022-06-23 thomas if (err->code == GOT_ERR_PRIVSEP_PIPE)
1374 63915ee5 2022-06-23 thomas err = NULL;
1376 63915ee5 2022-06-23 thomas got_privsep_send_error(ibuf, err);
1379 13280750 2022-06-13 thomas return err;
1382 13280750 2022-06-13 thomas static const struct got_error *
1383 63915ee5 2022-06-23 thomas enumeration_request(struct imsg *imsg, struct imsgbuf *ibuf,
1384 63915ee5 2022-06-23 thomas struct got_pack *pack, struct got_packidx *packidx,
1385 63915ee5 2022-06-23 thomas struct got_object_cache *objcache)
1387 63915ee5 2022-06-23 thomas const struct got_error *err = NULL;
1388 63915ee5 2022-06-23 thomas struct got_object_id_queue commit_ids;
1389 63915ee5 2022-06-23 thomas const struct got_object_id_queue *parents = NULL;
1390 63915ee5 2022-06-23 thomas struct got_object_qid *qid = NULL;
1391 63915ee5 2022-06-23 thomas struct got_object *obj = NULL;
1392 63915ee5 2022-06-23 thomas struct got_commit_object *commit = NULL;
1393 63915ee5 2022-06-23 thomas struct got_object_id *tree_id = NULL;
1394 63915ee5 2022-06-23 thomas size_t totlen = 0;
1395 47f02830 2023-01-27 thomas struct got_object_idset *idset, *queued_ids = NULL;
1396 63915ee5 2022-06-23 thomas int i, idx, have_all_entries = 1;
1397 63915ee5 2022-06-23 thomas struct enumerated_tree *trees = NULL;
1398 63915ee5 2022-06-23 thomas size_t ntrees = 0, nalloc = 16;
1400 63915ee5 2022-06-23 thomas STAILQ_INIT(&commit_ids);
1402 93088cca 2022-06-23 thomas trees = calloc(nalloc, sizeof(*trees));
1403 63915ee5 2022-06-23 thomas if (trees == NULL)
1404 63915ee5 2022-06-23 thomas return got_error_from_errno("calloc");
1406 63915ee5 2022-06-23 thomas idset = got_object_idset_alloc();
1407 63915ee5 2022-06-23 thomas if (idset == NULL) {
1408 63915ee5 2022-06-23 thomas err = got_error_from_errno("got_object_idset_alloc");
1409 63915ee5 2022-06-23 thomas goto done;
1412 47f02830 2023-01-27 thomas queued_ids = got_object_idset_alloc();
1413 47f02830 2023-01-27 thomas if (queued_ids == NULL) {
1414 47f02830 2023-01-27 thomas err = got_error_from_errno("got_object_idset_alloc");
1415 47f02830 2023-01-27 thomas goto done;
1418 47f02830 2023-01-27 thomas err = recv_object_id_queue(&commit_ids, queued_ids, ibuf);
1419 63915ee5 2022-06-23 thomas if (err)
1420 63915ee5 2022-06-23 thomas goto done;
1422 ef53e23c 2022-06-23 thomas if (STAILQ_EMPTY(&commit_ids)) {
1423 ef53e23c 2022-06-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1424 ef53e23c 2022-06-23 thomas goto done;
1427 63915ee5 2022-06-23 thomas err = recv_object_ids(idset, ibuf);
1428 63915ee5 2022-06-23 thomas if (err)
1429 63915ee5 2022-06-23 thomas goto done;
1431 63915ee5 2022-06-23 thomas while (!STAILQ_EMPTY(&commit_ids)) {
1432 63915ee5 2022-06-23 thomas if (sigint_received) {
1433 63915ee5 2022-06-23 thomas err = got_error(GOT_ERR_CANCELLED);
1434 63915ee5 2022-06-23 thomas goto done;
1437 63915ee5 2022-06-23 thomas qid = STAILQ_FIRST(&commit_ids);
1438 63915ee5 2022-06-23 thomas STAILQ_REMOVE_HEAD(&commit_ids, entry);
1440 63915ee5 2022-06-23 thomas if (got_object_idset_contains(idset, &qid->id)) {
1441 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1442 63915ee5 2022-06-23 thomas qid = NULL;
1443 63915ee5 2022-06-23 thomas continue;
1446 63915ee5 2022-06-23 thomas idx = got_packidx_get_object_idx(packidx, &qid->id);
1447 e71f1e62 2022-06-23 thomas if (idx == -1) {
1448 e71f1e62 2022-06-23 thomas have_all_entries = 0;
1452 63915ee5 2022-06-23 thomas err = open_object(&obj, pack, packidx, idx, &qid->id,
1453 63915ee5 2022-06-23 thomas objcache);
1454 63915ee5 2022-06-23 thomas if (err)
1455 63915ee5 2022-06-23 thomas goto done;
1456 63915ee5 2022-06-23 thomas if (obj->type == GOT_OBJ_TYPE_TAG) {
1457 63915ee5 2022-06-23 thomas struct got_tag_object *tag;
1458 63915ee5 2022-06-23 thomas uint8_t *buf;
1459 63915ee5 2022-06-23 thomas size_t len;
1460 63915ee5 2022-06-23 thomas err = got_packfile_extract_object_to_mem(&buf,
1461 63915ee5 2022-06-23 thomas &len, obj, pack);
1462 63915ee5 2022-06-23 thomas if (err)
1463 63915ee5 2022-06-23 thomas goto done;
1464 63915ee5 2022-06-23 thomas obj->size = len;
1465 63915ee5 2022-06-23 thomas err = got_object_parse_tag(&tag, buf, len);
1466 63915ee5 2022-06-23 thomas if (err) {
1467 63915ee5 2022-06-23 thomas free(buf);
1468 63915ee5 2022-06-23 thomas goto done;
1470 63915ee5 2022-06-23 thomas idx = got_packidx_get_object_idx(packidx, &tag->id);
1471 e71f1e62 2022-06-23 thomas if (idx == -1) {
1472 e71f1e62 2022-06-23 thomas have_all_entries = 0;
1475 63915ee5 2022-06-23 thomas err = open_commit(&commit, pack, packidx, idx,
1476 63915ee5 2022-06-23 thomas &tag->id, objcache);
1477 63915ee5 2022-06-23 thomas got_object_tag_close(tag);
1478 63915ee5 2022-06-23 thomas free(buf);
1479 63915ee5 2022-06-23 thomas if (err)
1480 63915ee5 2022-06-23 thomas goto done;
1481 63915ee5 2022-06-23 thomas } else if (obj->type == GOT_OBJ_TYPE_COMMIT) {
1482 63915ee5 2022-06-23 thomas err = open_commit(&commit, pack, packidx, idx,
1483 63915ee5 2022-06-23 thomas &qid->id, objcache);
1484 63915ee5 2022-06-23 thomas if (err)
1485 63915ee5 2022-06-23 thomas goto done;
1486 63915ee5 2022-06-23 thomas } else {
1487 63915ee5 2022-06-23 thomas err = got_error(GOT_ERR_OBJ_TYPE);
1488 63915ee5 2022-06-23 thomas goto done;
1490 63915ee5 2022-06-23 thomas got_object_close(obj);
1491 63915ee5 2022-06-23 thomas obj = NULL;
1493 63915ee5 2022-06-23 thomas err = got_privsep_send_enumerated_commit(ibuf, &qid->id,
1494 63915ee5 2022-06-23 thomas got_object_commit_get_committer_time(commit));
1495 63915ee5 2022-06-23 thomas if (err)
1496 63915ee5 2022-06-23 thomas goto done;
1498 63915ee5 2022-06-23 thomas tree_id = got_object_commit_get_tree_id(commit);
1499 63915ee5 2022-06-23 thomas idx = got_packidx_get_object_idx(packidx, tree_id);
1500 63915ee5 2022-06-23 thomas if (idx == -1) {
1501 e71f1e62 2022-06-23 thomas have_all_entries = 0;
1502 63915ee5 2022-06-23 thomas err = got_privsep_send_enumerated_tree(&totlen, ibuf,
1503 63915ee5 2022-06-23 thomas tree_id, "/", NULL, -1);
1504 63915ee5 2022-06-23 thomas if (err)
1505 63915ee5 2022-06-23 thomas goto done;
1509 63915ee5 2022-06-23 thomas if (got_object_idset_contains(idset, tree_id)) {
1510 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1511 63915ee5 2022-06-23 thomas qid = NULL;
1512 4088ab23 2023-01-27 thomas err = send_tree_enumeration_done(ibuf);
1513 4088ab23 2023-01-27 thomas if (err)
1514 4088ab23 2023-01-27 thomas goto done;
1515 63915ee5 2022-06-23 thomas continue;
1518 0f4feb58 2022-06-23 thomas err = enumerate_tree(&have_all_entries, ibuf, &totlen,
1519 0f4feb58 2022-06-23 thomas tree_id, "/", pack, packidx, objcache, idset,
1520 0f4feb58 2022-06-23 thomas &trees, &nalloc, &ntrees);
1521 63915ee5 2022-06-23 thomas if (err)
1522 63915ee5 2022-06-23 thomas goto done;
1524 63915ee5 2022-06-23 thomas if (!have_all_entries)
1527 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1528 63915ee5 2022-06-23 thomas qid = NULL;
1530 63915ee5 2022-06-23 thomas parents = got_object_commit_get_parent_ids(commit);
1531 63915ee5 2022-06-23 thomas if (parents) {
1532 63915ee5 2022-06-23 thomas struct got_object_qid *pid;
1533 63915ee5 2022-06-23 thomas STAILQ_FOREACH(pid, parents, entry) {
1534 63915ee5 2022-06-23 thomas if (got_object_idset_contains(idset, &pid->id))
1535 63915ee5 2022-06-23 thomas continue;
1536 47f02830 2023-01-27 thomas if (got_object_idset_contains(queued_ids, &pid->id))
1537 47f02830 2023-01-27 thomas continue;
1538 63915ee5 2022-06-23 thomas err = got_object_qid_alloc_partial(&qid);
1539 63915ee5 2022-06-23 thomas if (err)
1540 63915ee5 2022-06-23 thomas goto done;
1541 63915ee5 2022-06-23 thomas memcpy(&qid->id, &pid->id, sizeof(qid->id));
1542 63915ee5 2022-06-23 thomas STAILQ_INSERT_TAIL(&commit_ids, qid, entry);
1543 63915ee5 2022-06-23 thomas qid = NULL;
1547 63915ee5 2022-06-23 thomas got_object_commit_close(commit);
1548 63915ee5 2022-06-23 thomas commit = NULL;
1551 63915ee5 2022-06-23 thomas if (have_all_entries) {
1552 63915ee5 2022-06-23 thomas err = got_privsep_send_object_enumeration_done(ibuf);
1553 e71f1e62 2022-06-23 thomas if (err)
1554 e71f1e62 2022-06-23 thomas goto done;
1555 e71f1e62 2022-06-23 thomas } else {
1556 e71f1e62 2022-06-23 thomas err = got_privsep_send_object_enumeration_incomplete(ibuf);
1557 63915ee5 2022-06-23 thomas if (err)
1558 63915ee5 2022-06-23 thomas goto done;
1561 63915ee5 2022-06-23 thomas if (obj)
1562 63915ee5 2022-06-23 thomas got_object_close(obj);
1563 63915ee5 2022-06-23 thomas if (commit)
1564 63915ee5 2022-06-23 thomas got_object_commit_close(commit);
1565 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1566 63915ee5 2022-06-23 thomas got_object_id_queue_free(&commit_ids);
1567 63915ee5 2022-06-23 thomas if (idset)
1568 63915ee5 2022-06-23 thomas got_object_idset_free(idset);
1569 47f02830 2023-01-27 thomas if (queued_ids)
1570 47f02830 2023-01-27 thomas got_object_idset_free(queued_ids);
1571 63915ee5 2022-06-23 thomas for (i = 0; i < ntrees; i++) {
1572 63915ee5 2022-06-23 thomas struct enumerated_tree *tree = &trees[i];
1573 63915ee5 2022-06-23 thomas free(tree->buf);
1574 63915ee5 2022-06-23 thomas free(tree->path);
1575 63915ee5 2022-06-23 thomas free(tree->entries);
1577 63915ee5 2022-06-23 thomas free(trees);
1578 63915ee5 2022-06-23 thomas return err;
1581 ec2b23c5 2022-07-01 thomas enum findtwixt_color {
1582 ec2b23c5 2022-07-01 thomas COLOR_KEEP = 0,
1583 ec2b23c5 2022-07-01 thomas COLOR_DROP,
1584 ec2b23c5 2022-07-01 thomas COLOR_SKIP,
1585 ec2b23c5 2022-07-01 thomas COLOR_MAX,
1588 63915ee5 2022-06-23 thomas static const struct got_error *
1589 ec2b23c5 2022-07-01 thomas paint_commit(struct got_object_qid *qid, intptr_t color)
1591 ec2b23c5 2022-07-01 thomas if (color < 0 || color >= COLOR_MAX)
1592 ec2b23c5 2022-07-01 thomas return got_error(GOT_ERR_RANGE);
1594 ec2b23c5 2022-07-01 thomas qid->data = (void *)color;
1595 ec2b23c5 2022-07-01 thomas return NULL;
1598 ec2b23c5 2022-07-01 thomas static const struct got_error *
1599 ec2b23c5 2022-07-01 thomas queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1600 ec2b23c5 2022-07-01 thomas intptr_t color)
1602 ec2b23c5 2022-07-01 thomas const struct got_error *err;
1603 ec2b23c5 2022-07-01 thomas struct got_object_qid *qid;
1605 ec2b23c5 2022-07-01 thomas err = got_object_qid_alloc_partial(&qid);
1606 ec2b23c5 2022-07-01 thomas if (err)
1607 ec2b23c5 2022-07-01 thomas return err;
1609 ec2b23c5 2022-07-01 thomas memcpy(&qid->id, id, sizeof(qid->id));
1610 ec2b23c5 2022-07-01 thomas STAILQ_INSERT_TAIL(ids, qid, entry);
1611 ec2b23c5 2022-07-01 thomas return paint_commit(qid, color);
1614 ec2b23c5 2022-07-01 thomas static const struct got_error *
1615 ec2b23c5 2022-07-01 thomas paint_commits(struct got_object_id_queue *ids, int *nids,
1616 ec2b23c5 2022-07-01 thomas struct got_object_idset *keep, struct got_object_idset *drop,
1617 ec2b23c5 2022-07-01 thomas struct got_object_idset *skip, struct got_pack *pack,
1618 ec2b23c5 2022-07-01 thomas struct got_packidx *packidx, struct imsgbuf *ibuf,
1619 ec2b23c5 2022-07-01 thomas struct got_object_cache *objcache)
1621 ec2b23c5 2022-07-01 thomas const struct got_error *err = NULL;
1622 ec2b23c5 2022-07-01 thomas struct got_commit_object *commit = NULL;
1623 ec2b23c5 2022-07-01 thomas struct got_object_id_queue painted;
1624 ec2b23c5 2022-07-01 thomas const struct got_object_id_queue *parents;
1625 ec2b23c5 2022-07-01 thomas struct got_object_qid *qid = NULL;
1626 ec2b23c5 2022-07-01 thomas int nqueued = *nids, nskip = 0, npainted = 0;
1628 ec2b23c5 2022-07-01 thomas STAILQ_INIT(&painted);
1630 ec2b23c5 2022-07-01 thomas while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1631 ec2b23c5 2022-07-01 thomas int idx;
1632 ec2b23c5 2022-07-01 thomas intptr_t color;
1634 ec2b23c5 2022-07-01 thomas if (sigint_received) {
1635 ec2b23c5 2022-07-01 thomas err = got_error(GOT_ERR_CANCELLED);
1636 ec2b23c5 2022-07-01 thomas goto done;
1639 ec2b23c5 2022-07-01 thomas qid = STAILQ_FIRST(ids);
1640 ec2b23c5 2022-07-01 thomas idx = got_packidx_get_object_idx(packidx, &qid->id);
1641 ec2b23c5 2022-07-01 thomas if (idx == -1) {
1642 ec2b23c5 2022-07-01 thomas qid = NULL;
1646 ec2b23c5 2022-07-01 thomas STAILQ_REMOVE_HEAD(ids, entry);
1647 ec2b23c5 2022-07-01 thomas nqueued--;
1648 ec2b23c5 2022-07-01 thomas color = (intptr_t)qid->data;
1649 ec2b23c5 2022-07-01 thomas if (color == COLOR_SKIP)
1650 ec2b23c5 2022-07-01 thomas nskip--;
1652 ec2b23c5 2022-07-01 thomas if (got_object_idset_contains(skip, &qid->id)) {
1653 ec2b23c5 2022-07-01 thomas got_object_qid_free(qid);
1654 ec2b23c5 2022-07-01 thomas qid = NULL;
1655 ec2b23c5 2022-07-01 thomas continue;
1658 ec2b23c5 2022-07-01 thomas switch (color) {
1659 ec2b23c5 2022-07-01 thomas case COLOR_KEEP:
1660 ec2b23c5 2022-07-01 thomas if (got_object_idset_contains(keep, &qid->id)) {
1661 ec2b23c5 2022-07-01 thomas got_object_qid_free(qid);
1662 ec2b23c5 2022-07-01 thomas qid = NULL;
1663 ec2b23c5 2022-07-01 thomas continue;
1665 ec2b23c5 2022-07-01 thomas if (got_object_idset_contains(drop, &qid->id)) {
1666 ec2b23c5 2022-07-01 thomas err = paint_commit(qid, COLOR_SKIP);
1667 ec2b23c5 2022-07-01 thomas if (err)
1668 ec2b23c5 2022-07-01 thomas goto done;
1670 ec2b23c5 2022-07-01 thomas err = got_object_idset_add(keep, &qid->id, NULL);
1671 ec2b23c5 2022-07-01 thomas if (err)
1672 ec2b23c5 2022-07-01 thomas goto done;
1674 ec2b23c5 2022-07-01 thomas case COLOR_DROP:
1675 ec2b23c5 2022-07-01 thomas if (got_object_idset_contains(drop, &qid->id)) {
1676 ec2b23c5 2022-07-01 thomas got_object_qid_free(qid);
1677 ec2b23c5 2022-07-01 thomas qid = NULL;
1678 ec2b23c5 2022-07-01 thomas continue;
1680 ec2b23c5 2022-07-01 thomas if (got_object_idset_contains(keep, &qid->id)) {
1681 ec2b23c5 2022-07-01 thomas err = paint_commit(qid, COLOR_SKIP);
1682 ec2b23c5 2022-07-01 thomas if (err)
1683 ec2b23c5 2022-07-01 thomas goto done;
1685 ec2b23c5 2022-07-01 thomas err = got_object_idset_add(drop, &qid->id, NULL);
1686 ec2b23c5 2022-07-01 thomas if (err)
1687 ec2b23c5 2022-07-01 thomas goto done;
1689 ec2b23c5 2022-07-01 thomas case COLOR_SKIP:
1690 ec2b23c5 2022-07-01 thomas if (!got_object_idset_contains(skip, &qid->id)) {
1691 ec2b23c5 2022-07-01 thomas err = got_object_idset_add(skip, &qid->id,
1693 ec2b23c5 2022-07-01 thomas if (err)
1694 ec2b23c5 2022-07-01 thomas goto done;
1697 ec2b23c5 2022-07-01 thomas default:
1698 ec2b23c5 2022-07-01 thomas /* should not happen */
1699 ec2b23c5 2022-07-01 thomas err = got_error_fmt(GOT_ERR_NOT_IMPL,
1700 60b94e7d 2022-07-19 thomas "%s invalid commit color %"PRIdPTR, __func__,
1702 ec2b23c5 2022-07-01 thomas goto done;
1705 ec2b23c5 2022-07-01 thomas err = open_commit(&commit, pack, packidx, idx, &qid->id,
1706 ec2b23c5 2022-07-01 thomas objcache);
1707 ec2b23c5 2022-07-01 thomas if (err)
1708 ec2b23c5 2022-07-01 thomas goto done;
1710 ec2b23c5 2022-07-01 thomas parents = got_object_commit_get_parent_ids(commit);
1711 ec2b23c5 2022-07-01 thomas if (parents) {
1712 ec2b23c5 2022-07-01 thomas struct got_object_qid *pid;
1713 ec2b23c5 2022-07-01 thomas color = (intptr_t)qid->data;
1714 ec2b23c5 2022-07-01 thomas STAILQ_FOREACH(pid, parents, entry) {
1715 ec2b23c5 2022-07-01 thomas err = queue_commit_id(ids, &pid->id, color);
1716 ec2b23c5 2022-07-01 thomas if (err)
1717 ec2b23c5 2022-07-01 thomas goto done;
1718 ec2b23c5 2022-07-01 thomas nqueued++;
1719 ec2b23c5 2022-07-01 thomas if (color == COLOR_SKIP)
1720 ec2b23c5 2022-07-01 thomas nskip++;
1724 ec2b23c5 2022-07-01 thomas got_object_commit_close(commit);
1725 ec2b23c5 2022-07-01 thomas commit = NULL;
1727 ec2b23c5 2022-07-01 thomas STAILQ_INSERT_TAIL(&painted, qid, entry);
1728 ec2b23c5 2022-07-01 thomas qid = NULL;
1729 ec2b23c5 2022-07-01 thomas npainted++;
1731 ec2b23c5 2022-07-01 thomas err = got_privsep_send_painted_commits(ibuf, &painted,
1732 ec2b23c5 2022-07-01 thomas &npainted, 1, 0);
1733 ec2b23c5 2022-07-01 thomas if (err)
1734 ec2b23c5 2022-07-01 thomas goto done;
1737 ec2b23c5 2022-07-01 thomas err = got_privsep_send_painted_commits(ibuf, &painted, &npainted, 1, 1);
1738 ec2b23c5 2022-07-01 thomas if (err)
1739 ec2b23c5 2022-07-01 thomas goto done;
1741 ec2b23c5 2022-07-01 thomas *nids = nqueued;
1743 ec2b23c5 2022-07-01 thomas if (commit)
1744 ec2b23c5 2022-07-01 thomas got_object_commit_close(commit);
1745 ec2b23c5 2022-07-01 thomas got_object_qid_free(qid);
1746 ec2b23c5 2022-07-01 thomas return err;
1749 ec2b23c5 2022-07-01 thomas static void
1750 ec2b23c5 2022-07-01 thomas commit_painting_free(struct got_object_idset **keep,
1751 ec2b23c5 2022-07-01 thomas struct got_object_idset **drop,
1752 ec2b23c5 2022-07-01 thomas struct got_object_idset **skip)
1754 ec2b23c5 2022-07-01 thomas if (*keep) {
1755 ec2b23c5 2022-07-01 thomas got_object_idset_free(*keep);
1756 ec2b23c5 2022-07-01 thomas *keep = NULL;
1758 ec2b23c5 2022-07-01 thomas if (*drop) {
1759 ec2b23c5 2022-07-01 thomas got_object_idset_free(*drop);
1760 ec2b23c5 2022-07-01 thomas *drop = NULL;
1762 ec2b23c5 2022-07-01 thomas if (*skip) {
1763 b6b86fd1 2022-08-30 thomas got_object_idset_free(*skip);
1764 ec2b23c5 2022-07-01 thomas *skip = NULL;
1768 ec2b23c5 2022-07-01 thomas static const struct got_error *
1769 ec2b23c5 2022-07-01 thomas commit_painting_init(struct imsgbuf *ibuf, struct got_object_idset **keep,
1770 ec2b23c5 2022-07-01 thomas struct got_object_idset **drop, struct got_object_idset **skip)
1772 ec2b23c5 2022-07-01 thomas const struct got_error *err = NULL;
1774 ec2b23c5 2022-07-01 thomas *keep = got_object_idset_alloc();
1775 ec2b23c5 2022-07-01 thomas if (*keep == NULL) {
1776 ec2b23c5 2022-07-01 thomas err = got_error_from_errno("got_object_idset_alloc");
1777 ec2b23c5 2022-07-01 thomas goto done;
1779 ec2b23c5 2022-07-01 thomas *drop = got_object_idset_alloc();
1780 ec2b23c5 2022-07-01 thomas if (*drop == NULL) {
1781 ec2b23c5 2022-07-01 thomas err = got_error_from_errno("got_object_idset_alloc");
1782 ec2b23c5 2022-07-01 thomas goto done;
1784 ec2b23c5 2022-07-01 thomas *skip = got_object_idset_alloc();
1785 ec2b23c5 2022-07-01 thomas if (*skip == NULL) {
1786 ec2b23c5 2022-07-01 thomas err = got_error_from_errno("got_object_idset_alloc");
1787 ec2b23c5 2022-07-01 thomas goto done;
1790 ec2b23c5 2022-07-01 thomas err = recv_object_ids(*keep, ibuf);
1791 ec2b23c5 2022-07-01 thomas if (err)
1792 ec2b23c5 2022-07-01 thomas goto done;
1793 ec2b23c5 2022-07-01 thomas err = recv_object_ids(*drop, ibuf);
1794 ec2b23c5 2022-07-01 thomas if (err)
1795 ec2b23c5 2022-07-01 thomas goto done;
1796 ec2b23c5 2022-07-01 thomas err = recv_object_ids(*skip, ibuf);
1797 ec2b23c5 2022-07-01 thomas if (err)
1798 ec2b23c5 2022-07-01 thomas goto done;
1801 ec2b23c5 2022-07-01 thomas if (err)
1802 ec2b23c5 2022-07-01 thomas commit_painting_free(keep, drop, skip);
1804 ec2b23c5 2022-07-01 thomas return err;
1807 ec2b23c5 2022-07-01 thomas static const struct got_error *
1808 ec2b23c5 2022-07-01 thomas commit_painting_request(struct imsg *imsg, struct imsgbuf *ibuf,
1809 ec2b23c5 2022-07-01 thomas struct got_pack *pack, struct got_packidx *packidx,
1810 ec2b23c5 2022-07-01 thomas struct got_object_cache *objcache, struct got_object_idset *keep,
1811 ec2b23c5 2022-07-01 thomas struct got_object_idset *drop, struct got_object_idset *skip)
1813 ec2b23c5 2022-07-01 thomas const struct got_error *err = NULL;
1814 ec2b23c5 2022-07-01 thomas struct got_imsg_commit_painting_request ireq;
1815 ec2b23c5 2022-07-01 thomas struct got_object_id id;
1816 ec2b23c5 2022-07-01 thomas size_t datalen;
1817 ec2b23c5 2022-07-01 thomas struct got_object_id_queue ids;
1818 ec2b23c5 2022-07-01 thomas int nids = 0;
1820 ec2b23c5 2022-07-01 thomas STAILQ_INIT(&ids);
1822 ec2b23c5 2022-07-01 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1823 ec2b23c5 2022-07-01 thomas if (datalen != sizeof(ireq))
1824 ec2b23c5 2022-07-01 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1825 ec2b23c5 2022-07-01 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
1826 35cdfa2c 2023-02-20 thomas memcpy(&id, &ireq.id, sizeof(id));
1828 ec2b23c5 2022-07-01 thomas err = queue_commit_id(&ids, &id, ireq.color);
1829 ec2b23c5 2022-07-01 thomas if (err)
1830 ec2b23c5 2022-07-01 thomas return err;
1831 ec2b23c5 2022-07-01 thomas nids = 1;
1833 ec2b23c5 2022-07-01 thomas err = paint_commits(&ids, &nids, keep, drop, skip,
1834 ec2b23c5 2022-07-01 thomas pack, packidx, ibuf, objcache);
1835 ec2b23c5 2022-07-01 thomas if (err)
1836 ec2b23c5 2022-07-01 thomas goto done;
1838 ec2b23c5 2022-07-01 thomas err = got_privsep_send_painted_commits(ibuf, &ids, &nids, 0, 1);
1839 ec2b23c5 2022-07-01 thomas if (err)
1840 ec2b23c5 2022-07-01 thomas goto done;
1842 ec2b23c5 2022-07-01 thomas err = got_privsep_send_painting_commits_done(ibuf);
1844 ec2b23c5 2022-07-01 thomas got_object_id_queue_free(&ids);
1845 ec2b23c5 2022-07-01 thomas return err;
1848 ec2b23c5 2022-07-01 thomas static const struct got_error *
1849 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1851 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1852 876c234b 2018-09-10 stsp struct imsg imsg;
1853 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1854 876c234b 2018-09-10 stsp size_t datalen;
1855 876c234b 2018-09-10 stsp struct got_pack *pack;
1857 876c234b 2018-09-10 stsp *packp = NULL;
1859 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1861 876c234b 2018-09-10 stsp return err;
1863 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
1864 876c234b 2018-09-10 stsp if (pack == NULL) {
1865 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1866 876c234b 2018-09-10 stsp goto done;
1869 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
1870 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1871 876c234b 2018-09-10 stsp goto done;
1874 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1875 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
1876 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1877 876c234b 2018-09-10 stsp goto done;
1879 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
1881 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
1882 3d97effa 2024-01-31 thomas pack->fd = imsg_get_fd(&imsg);
1883 876c234b 2018-09-10 stsp if (pack->fd == -1) {
1884 3d97effa 2024-01-31 thomas err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1885 876c234b 2018-09-10 stsp goto done;
1887 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1888 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1889 56bef47a 2018-09-15 stsp goto done;
1891 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
1892 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
1893 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1894 ab2f42e7 2019-11-10 stsp goto done;
1897 a5061f77 2022-06-13 thomas err = got_delta_cache_alloc(&pack->delta_cache);
1898 a5061f77 2022-06-13 thomas if (err)
1899 876c234b 2018-09-10 stsp goto done;
1901 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
1902 aa75acde 2022-10-25 thomas if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1903 aa75acde 2022-10-25 thomas pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1904 aa75acde 2022-10-25 thomas pack->fd, 0);
1905 aa75acde 2022-10-25 thomas if (pack->map == MAP_FAILED)
1906 aa75acde 2022-10-25 thomas pack->map = NULL; /* fall back to read(2) */
1910 876c234b 2018-09-10 stsp if (err) {
1911 3d97effa 2024-01-31 thomas if (pack != NULL)
1912 3d97effa 2024-01-31 thomas got_pack_close(pack);
1914 876c234b 2018-09-10 stsp *packp = pack;
1915 876c234b 2018-09-10 stsp imsg_free(&imsg);
1916 876c234b 2018-09-10 stsp return err;
1920 876c234b 2018-09-10 stsp main(int argc, char *argv[])
1922 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1923 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
1924 876c234b 2018-09-10 stsp struct imsg imsg;
1925 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
1926 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
1927 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
1928 f9c2e8e5 2022-02-13 thomas FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1929 ec2b23c5 2022-07-01 thomas struct got_object_idset *keep = NULL, *drop = NULL, *skip = NULL;
1930 c77e00b3 2022-10-18 thomas struct got_parsed_tree_entry *entries = NULL;
1931 c77e00b3 2022-10-18 thomas size_t nentries = 0, nentries_alloc = 0;
1933 876c234b 2018-09-10 stsp //static int attached;
1934 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
1936 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
1938 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1940 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1941 c59b3346 2018-09-11 stsp if (err) {
1942 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_cache_init");
1943 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
1947 2ff12563 2018-09-15 stsp #ifndef PROFILE
1948 876c234b 2018-09-10 stsp /* revoke access to most system calls */
1949 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
1950 638f9024 2019-05-13 stsp err = got_error_from_errno("pledge");
1951 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1955 97799ccd 2022-02-06 thomas /* revoke fs access */
1956 97799ccd 2022-02-06 thomas if (landlock_no_fs() == -1) {
1957 97799ccd 2022-02-06 thomas err = got_error_from_errno("landlock_no_fs");
1958 97799ccd 2022-02-06 thomas got_privsep_send_error(&ibuf, err);
1959 97799ccd 2022-02-06 thomas return 1;
1961 5d120ea8 2022-06-23 op if (cap_enter() == -1) {
1962 5d120ea8 2022-06-23 op err = got_error_from_errno("cap_enter");
1963 5d120ea8 2022-06-23 op got_privsep_send_error(&ibuf, err);
1968 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
1969 876c234b 2018-09-10 stsp if (err) {
1970 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1974 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
1975 876c234b 2018-09-10 stsp if (err) {
1976 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1981 99437157 2018-11-11 stsp if (sigint_received) {
1982 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
1986 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1987 876c234b 2018-09-10 stsp if (err) {
1988 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1989 876c234b 2018-09-10 stsp err = NULL;
1993 3d97effa 2024-01-31 thomas if (imsg.hdr.type == GOT_IMSG_STOP) {
1994 3d97effa 2024-01-31 thomas imsg_free(&imsg);
1998 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
1999 bc1f382f 2022-01-05 thomas case GOT_IMSG_TMPFD:
2000 f9c2e8e5 2022-02-13 thomas if (basefile == NULL) {
2001 f9c2e8e5 2022-02-13 thomas err = receive_tempfile(&basefile, "w+",
2002 f9c2e8e5 2022-02-13 thomas &imsg, &ibuf);
2003 f9c2e8e5 2022-02-13 thomas } else if (accumfile == NULL) {
2004 f9c2e8e5 2022-02-13 thomas err = receive_tempfile(&accumfile, "w+",
2005 f9c2e8e5 2022-02-13 thomas &imsg, &ibuf);
2007 f9c2e8e5 2022-02-13 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
2009 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
2010 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
2011 c59b3346 2018-09-11 stsp &objcache);
2013 59d1e4a0 2021-03-10 stsp case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
2014 bc1f382f 2022-01-05 thomas if (basefile == NULL || accumfile == NULL) {
2015 bc1f382f 2022-01-05 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
2018 59d1e4a0 2021-03-10 stsp err = raw_object_request(&imsg, &ibuf, pack, packidx,
2019 bc1f382f 2022-01-05 thomas &objcache, basefile, accumfile);
2021 f9c2e8e5 2022-02-13 thomas case GOT_IMSG_RAW_DELTA_OUTFD:
2022 f9c2e8e5 2022-02-13 thomas if (delta_outfile != NULL) {
2023 f9c2e8e5 2022-02-13 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
2026 f9c2e8e5 2022-02-13 thomas err = receive_tempfile(&delta_outfile, "w",
2027 f9c2e8e5 2022-02-13 thomas &imsg, &ibuf);
2029 f9c2e8e5 2022-02-13 thomas case GOT_IMSG_RAW_DELTA_REQUEST:
2030 f9c2e8e5 2022-02-13 thomas if (delta_outfile == NULL) {
2031 f9c2e8e5 2022-02-13 thomas err = got_error(GOT_ERR_PRIVSEP_NO_FD);
2034 f9c2e8e5 2022-02-13 thomas err = raw_delta_request(&imsg, &ibuf, delta_outfile,
2035 f9c2e8e5 2022-02-13 thomas pack, packidx);
2037 7d0d4920 2022-05-12 thomas case GOT_IMSG_DELTA_REUSE_REQUEST:
2038 c44c7d6e 2022-12-04 thomas err = delta_reuse_request(&imsg, &ibuf, pack, packidx);
2040 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
2041 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
2042 7762fe12 2018-11-05 stsp &objcache);
2044 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
2045 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
2046 c77e00b3 2022-10-18 thomas &objcache, &entries, &nentries, &nentries_alloc);
2048 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
2049 bc1f382f 2022-01-05 thomas if (basefile == NULL || accumfile == NULL) {
2050 bc1f382f 2022-01-05 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
2053 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
2054 bc1f382f 2022-01-05 thomas &objcache, basefile, accumfile);
2056 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG_REQUEST:
2057 f4a881ce 2018-11-17 stsp err = tag_request(&imsg, &ibuf, pack, packidx,
2058 62d463ca 2020-10-20 naddy &objcache);
2060 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
2061 ca6e02ac 2020-01-07 stsp err = commit_traversal_request(&imsg, &ibuf, pack,
2062 ca6e02ac 2020-01-07 stsp packidx, &objcache);
2064 63915ee5 2022-06-23 thomas case GOT_IMSG_OBJECT_ENUMERATION_REQUEST:
2065 63915ee5 2022-06-23 thomas err = enumeration_request(&imsg, &ibuf, pack,
2066 63915ee5 2022-06-23 thomas packidx, &objcache);
2068 ec2b23c5 2022-07-01 thomas case GOT_IMSG_COMMIT_PAINTING_INIT:
2069 ec2b23c5 2022-07-01 thomas commit_painting_free(&keep, &drop, &skip);
2070 ec2b23c5 2022-07-01 thomas err = commit_painting_init(&ibuf, &keep, &drop, &skip);
2072 ec2b23c5 2022-07-01 thomas case GOT_IMSG_COMMIT_PAINTING_REQUEST:
2073 ec2b23c5 2022-07-01 thomas if (keep == NULL || drop == NULL || skip == NULL) {
2074 ec2b23c5 2022-07-01 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
2077 ec2b23c5 2022-07-01 thomas err = commit_painting_request(&imsg, &ibuf, pack,
2078 ec2b23c5 2022-07-01 thomas packidx, &objcache, keep, drop, skip);
2080 ec2b23c5 2022-07-01 thomas case GOT_IMSG_COMMIT_PAINTING_DONE:
2081 ec2b23c5 2022-07-01 thomas commit_painting_free(&keep, &drop, &skip);
2084 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2088 876c234b 2018-09-10 stsp imsg_free(&imsg);
2093 c77e00b3 2022-10-18 thomas free(entries);
2094 ec2b23c5 2022-07-01 thomas commit_painting_free(&keep, &drop, &skip);
2095 c59b3346 2018-09-11 stsp if (packidx)
2096 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
2098 c59b3346 2018-09-11 stsp got_pack_close(pack);
2099 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
2100 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
2101 bc1f382f 2022-01-05 thomas if (basefile && fclose(basefile) == EOF && err == NULL)
2102 bc1f382f 2022-01-05 thomas err = got_error_from_errno("fclose");
2103 bc1f382f 2022-01-05 thomas if (accumfile && fclose(accumfile) == EOF && err == NULL)
2104 bc1f382f 2022-01-05 thomas err = got_error_from_errno("fclose");
2105 f9c2e8e5 2022-02-13 thomas if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
2106 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fclose");
2107 99437157 2018-11-11 stsp if (err) {
2108 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
2109 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
2110 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
2113 08578a35 2021-01-22 stsp if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
2114 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
2115 876c234b 2018-09-10 stsp return err ? 1 : 0;