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.
17 63915ee5 2022-06-23 thomas #include <sys/stat.h>
18 876c234b 2018-09-10 stsp #include <sys/types.h>
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
20 876c234b 2018-09-10 stsp #include <sys/uio.h>
21 876c234b 2018-09-10 stsp #include <sys/time.h>
22 876c234b 2018-09-10 stsp #include <sys/mman.h>
24 60b94e7d 2022-07-19 thomas #include <inttypes.h>
25 876c234b 2018-09-10 stsp #include <limits.h>
26 99437157 2018-11-11 stsp #include <signal.h>
27 876c234b 2018-09-10 stsp #include <stdint.h>
28 876c234b 2018-09-10 stsp #include <stdio.h>
29 876c234b 2018-09-10 stsp #include <stdlib.h>
30 876c234b 2018-09-10 stsp #include <string.h>
31 81a12da5 2020-09-09 naddy #include <unistd.h>
32 876c234b 2018-09-10 stsp #include <zlib.h>
34 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
36 876c234b 2018-09-10 stsp #include "got_error.h"
37 876c234b 2018-09-10 stsp #include "got_object.h"
38 3022d272 2019-11-14 stsp #include "got_path.h"
40 876c234b 2018-09-10 stsp #include "got_lib_delta.h"
41 ab2f42e7 2019-11-10 stsp #include "got_lib_delta_cache.h"
42 876c234b 2018-09-10 stsp #include "got_lib_object.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 106807b4 2018-09-15 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
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 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
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 c77e00b3 2022-10-18 thomas open_tree(uint8_t **buf, struct got_parsed_tree_entry **entries, size_t *nentries,
187 c77e00b3 2022-10-18 thomas size_t *nentries_alloc, struct got_pack *pack, struct got_packidx *packidx,
188 c77e00b3 2022-10-18 thomas int obj_idx, 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 ca6e02ac 2020-01-07 stsp size_t len;
194 ca6e02ac 2020-01-07 stsp *buf = NULL;
195 ca6e02ac 2020-01-07 stsp *nentries = 0;
197 ca6e02ac 2020-01-07 stsp obj = got_object_cache_get(objcache, id);
199 ca6e02ac 2020-01-07 stsp obj->refcnt++;
201 ca6e02ac 2020-01-07 stsp err = open_object(&obj, pack, packidx, obj_idx, id,
204 ca6e02ac 2020-01-07 stsp return err;
207 ca6e02ac 2020-01-07 stsp err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
211 ca6e02ac 2020-01-07 stsp obj->size = len;
213 c77e00b3 2022-10-18 thomas err = got_object_parse_tree(entries, nentries, nentries_alloc,
214 c77e00b3 2022-10-18 thomas *buf, len);
216 ca6e02ac 2020-01-07 stsp got_object_close(obj);
218 ca6e02ac 2020-01-07 stsp free(*buf);
219 ca6e02ac 2020-01-07 stsp *buf = NULL;
221 ca6e02ac 2020-01-07 stsp return err;
224 7762fe12 2018-11-05 stsp static const struct got_error *
225 876c234b 2018-09-10 stsp tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
226 c77e00b3 2022-10-18 thomas struct got_packidx *packidx, struct got_object_cache *objcache,
227 c77e00b3 2022-10-18 thomas struct got_parsed_tree_entry **entries, size_t *nentries,
228 c77e00b3 2022-10-18 thomas size_t *nentries_alloc)
230 e7885405 2018-09-10 stsp const struct got_error *err = NULL;
231 13c729f7 2018-12-24 stsp struct got_imsg_packed_object iobj;
232 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
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 13c729f7 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
242 c77e00b3 2022-10-18 thomas err = open_tree(&buf, entries, nentries, nentries_alloc,
243 c77e00b3 2022-10-18 thomas pack, packidx, iobj.idx, &id, objcache);
245 ca6e02ac 2020-01-07 stsp return err;
247 c77e00b3 2022-10-18 thomas err = got_privsep_send_tree(ibuf, *entries, *nentries);
250 e7885405 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
251 e7885405 2018-09-10 stsp err = NULL;
253 e7885405 2018-09-10 stsp got_privsep_send_error(ibuf, err);
256 e7885405 2018-09-10 stsp return err;
259 876c234b 2018-09-10 stsp static const struct got_error *
260 01bb5a15 2021-09-25 thomas.ad receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
262 3840f4c9 2018-09-12 stsp const struct got_error *err;
263 3840f4c9 2018-09-12 stsp struct imsg imsg;
264 55da3778 2018-09-10 stsp size_t datalen;
266 3840f4c9 2018-09-12 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
268 55da3778 2018-09-10 stsp return err;
270 3840f4c9 2018-09-12 stsp if (imsg.hdr.type != imsg_code) {
271 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
275 3840f4c9 2018-09-12 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
276 55da3778 2018-09-10 stsp if (datalen != 0) {
277 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
280 3840f4c9 2018-09-12 stsp if (imsg.fd == -1) {
281 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
285 3840f4c9 2018-09-12 stsp *f = fdopen(imsg.fd, "w+");
286 3840f4c9 2018-09-12 stsp if (*f == NULL) {
287 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
288 3a6ce05a 2019-02-11 stsp close(imsg.fd);
292 3840f4c9 2018-09-12 stsp imsg_free(&imsg);
293 3840f4c9 2018-09-12 stsp return err;
296 bc1f382f 2022-01-05 thomas static const struct got_error *
297 f9c2e8e5 2022-02-13 thomas receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
298 bc1f382f 2022-01-05 thomas struct imsgbuf *ibuf)
300 bc1f382f 2022-01-05 thomas size_t datalen;
302 bc1f382f 2022-01-05 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
303 bc1f382f 2022-01-05 thomas if (datalen != 0)
304 bc1f382f 2022-01-05 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
306 bc1f382f 2022-01-05 thomas if (imsg->fd == -1)
307 bc1f382f 2022-01-05 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
309 f9c2e8e5 2022-02-13 thomas *f = fdopen(imsg->fd, mode);
310 bc1f382f 2022-01-05 thomas if (*f == NULL)
311 bc1f382f 2022-01-05 thomas return got_error_from_errno("fdopen");
312 bc1f382f 2022-01-05 thomas imsg->fd = -1;
314 bc1f382f 2022-01-05 thomas return NULL;
317 3840f4c9 2018-09-12 stsp static const struct got_error *
318 3840f4c9 2018-09-12 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
319 bc1f382f 2022-01-05 thomas struct got_packidx *packidx, struct got_object_cache *objcache,
320 bc1f382f 2022-01-05 thomas FILE *basefile, FILE *accumfile)
322 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
323 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj;
324 3840f4c9 2018-09-12 stsp struct got_object *obj = NULL;
325 bc1f382f 2022-01-05 thomas FILE *outfile = NULL;
326 ebc55e2d 2018-12-24 stsp struct got_object_id id;
327 ebc55e2d 2018-12-24 stsp size_t datalen;
328 ac544f8c 2019-01-13 stsp uint64_t blob_size;
329 ac544f8c 2019-01-13 stsp uint8_t *buf = NULL;
331 ebc55e2d 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
332 ebc55e2d 2018-12-24 stsp if (datalen != sizeof(iobj))
333 ebc55e2d 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
334 ebc55e2d 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
335 ebc55e2d 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
337 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
339 704b89c4 2019-05-23 stsp obj->refcnt++;
341 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
344 704b89c4 2019-05-23 stsp return err;
347 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
351 ac544f8c 2019-01-13 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
352 42c69117 2019-11-10 stsp err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
356 ac544f8c 2019-01-13 stsp blob_size = obj->size;
358 ac544f8c 2019-01-13 stsp if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
359 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
360 ac544f8c 2019-01-13 stsp obj, pack);
362 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
363 ac544f8c 2019-01-13 stsp accumfile);
367 ac544f8c 2019-01-13 stsp err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
370 56b63ca4 2021-01-22 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
371 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
372 cb5e38fd 2019-05-23 stsp got_object_close(obj);
373 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
374 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
376 55da3778 2018-09-10 stsp return err;
379 876c234b 2018-09-10 stsp static const struct got_error *
380 f4a881ce 2018-11-17 stsp tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
381 f4a881ce 2018-11-17 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
383 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
384 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj;
385 f4a881ce 2018-11-17 stsp struct got_object *obj = NULL;
386 f4a881ce 2018-11-17 stsp struct got_tag_object *tag = NULL;
387 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
388 f4a881ce 2018-11-17 stsp size_t len;
389 268f7291 2018-12-24 stsp struct got_object_id id;
390 268f7291 2018-12-24 stsp size_t datalen;
392 268f7291 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
393 268f7291 2018-12-24 stsp if (datalen != sizeof(iobj))
394 268f7291 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
395 268f7291 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
396 268f7291 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
398 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
400 704b89c4 2019-05-23 stsp obj->refcnt++;
402 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
405 704b89c4 2019-05-23 stsp return err;
408 f4a881ce 2018-11-17 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
412 f4a881ce 2018-11-17 stsp obj->size = len;
413 f4a881ce 2018-11-17 stsp err = got_object_parse_tag(&tag, buf, len);
417 f4a881ce 2018-11-17 stsp err = got_privsep_send_tag(ibuf, tag);
420 cb5e38fd 2019-05-23 stsp got_object_close(obj);
422 cb5e38fd 2019-05-23 stsp got_object_tag_close(tag);
424 ca6e02ac 2020-01-07 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
425 ca6e02ac 2020-01-07 stsp err = NULL;
427 ca6e02ac 2020-01-07 stsp got_privsep_send_error(ibuf, err);
430 ca6e02ac 2020-01-07 stsp return err;
433 ca6e02ac 2020-01-07 stsp static struct got_parsed_tree_entry *
434 78e7b7b8 2022-05-19 thomas find_entry_by_name(struct got_parsed_tree_entry *entries, int nentries,
435 ca6e02ac 2020-01-07 stsp const char *name, size_t len)
437 78e7b7b8 2022-05-19 thomas struct got_parsed_tree_entry *pte;
438 78e7b7b8 2022-05-19 thomas int cmp, i;
440 ca6e02ac 2020-01-07 stsp /* Note that tree entries are sorted in strncmp() order. */
441 78e7b7b8 2022-05-19 thomas for (i = 0; i < nentries; i++) {
442 78e7b7b8 2022-05-19 thomas pte = &entries[i];
443 78e7b7b8 2022-05-19 thomas cmp = strncmp(pte->name, name, len);
444 ca6e02ac 2020-01-07 stsp if (cmp < 0)
446 ca6e02ac 2020-01-07 stsp if (cmp > 0)
448 78e7b7b8 2022-05-19 thomas if (pte->name[len] == '\0')
449 78e7b7b8 2022-05-19 thomas return pte;
451 ca6e02ac 2020-01-07 stsp return NULL;
454 8f1c06eb 2021-09-25 thomas.ad static const struct got_error *
455 ca6e02ac 2020-01-07 stsp tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
456 c77e00b3 2022-10-18 thomas struct got_parsed_tree_entry **entries1, size_t *nentries1,
457 c77e00b3 2022-10-18 thomas size_t *nentries_alloc1,
458 c77e00b3 2022-10-18 thomas struct got_parsed_tree_entry **entries2, size_t *nentries2,
459 c77e00b3 2022-10-18 thomas size_t *nentries_alloc2,
460 ca6e02ac 2020-01-07 stsp const char *path, struct got_pack *pack, struct got_packidx *packidx,
461 ca6e02ac 2020-01-07 stsp struct imsgbuf *ibuf, struct got_object_cache *objcache)
463 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
464 ca6e02ac 2020-01-07 stsp struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
465 ca6e02ac 2020-01-07 stsp const char *seg, *s;
466 ca6e02ac 2020-01-07 stsp size_t seglen;
468 ca6e02ac 2020-01-07 stsp *changed = 0;
470 ca6e02ac 2020-01-07 stsp /* We not do support comparing the root path. */
471 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
472 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
475 61a7d79f 2020-02-29 stsp while (*s == '/')
478 ca6e02ac 2020-01-07 stsp seglen = 0;
479 ca6e02ac 2020-01-07 stsp while (*s) {
480 ca6e02ac 2020-01-07 stsp if (*s != '/') {
487 78e7b7b8 2022-05-19 thomas pte1 = find_entry_by_name(*entries1, *nentries1, seg, seglen);
488 ca6e02ac 2020-01-07 stsp if (pte1 == NULL) {
489 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_NO_OBJ);
493 78e7b7b8 2022-05-19 thomas pte2 = find_entry_by_name(*entries2, *nentries2, seg, seglen);
494 ca6e02ac 2020-01-07 stsp if (pte2 == NULL) {
495 ca6e02ac 2020-01-07 stsp *changed = 1;
499 ca6e02ac 2020-01-07 stsp if (pte1->mode != pte2->mode) {
500 ca6e02ac 2020-01-07 stsp *changed = 1;
504 ca6e02ac 2020-01-07 stsp if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
505 ca6e02ac 2020-01-07 stsp *changed = 0;
509 ca6e02ac 2020-01-07 stsp if (*s == '\0') { /* final path element */
510 ca6e02ac 2020-01-07 stsp *changed = 1;
514 ca6e02ac 2020-01-07 stsp seg = s + 1;
516 ca6e02ac 2020-01-07 stsp seglen = 0;
518 ca6e02ac 2020-01-07 stsp struct got_object_id id1, id2;
521 ded8fbb8 2020-04-19 stsp memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
522 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id1);
523 ca6e02ac 2020-01-07 stsp if (idx == -1) {
524 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id1);
527 ca6e02ac 2020-01-07 stsp *nentries1 = 0;
528 ca6e02ac 2020-01-07 stsp free(*buf1);
529 ca6e02ac 2020-01-07 stsp *buf1 = NULL;
530 c77e00b3 2022-10-18 thomas err = open_tree(buf1, entries1, nentries1,
531 c77e00b3 2022-10-18 thomas nentries_alloc1, pack, packidx, idx, &id1,
532 c77e00b3 2022-10-18 thomas objcache);
533 ca6e02ac 2020-01-07 stsp pte1 = NULL;
537 ded8fbb8 2020-04-19 stsp memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
538 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id2);
539 ca6e02ac 2020-01-07 stsp if (idx == -1) {
540 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id2);
543 ca6e02ac 2020-01-07 stsp *nentries2 = 0;
544 ca6e02ac 2020-01-07 stsp free(*buf2);
545 ca6e02ac 2020-01-07 stsp *buf2 = NULL;
546 c77e00b3 2022-10-18 thomas err = open_tree(buf2, entries2, nentries2,
547 c77e00b3 2022-10-18 thomas nentries_alloc2, pack, packidx, idx, &id2, objcache);
548 ca6e02ac 2020-01-07 stsp pte2 = NULL;
554 ca6e02ac 2020-01-07 stsp return err;
557 ca6e02ac 2020-01-07 stsp static const struct got_error *
558 e70bf110 2020-03-22 stsp send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
559 e70bf110 2020-03-22 stsp struct imsgbuf *ibuf)
561 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
562 01bb5a15 2021-09-25 thomas.ad size_t i;
564 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
565 e70bf110 2020-03-22 stsp sizeof(struct got_imsg_traversed_commits) +
566 e70bf110 2020-03-22 stsp ncommits * SHA1_DIGEST_LENGTH);
567 e70bf110 2020-03-22 stsp if (wbuf == NULL)
568 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
570 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
571 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
573 e70bf110 2020-03-22 stsp for (i = 0; i < ncommits; i++) {
574 e70bf110 2020-03-22 stsp struct got_object_id *id = &commit_ids[i];
575 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
576 e9f1a409 2022-05-19 thomas return got_error_from_errno(
577 e70bf110 2020-03-22 stsp "imsg_add TRAVERSED_COMMITS");
581 e70bf110 2020-03-22 stsp wbuf->fd = -1;
582 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
584 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
587 e70bf110 2020-03-22 stsp static const struct got_error *
588 e70bf110 2020-03-22 stsp send_commit_traversal_done(struct imsgbuf *ibuf)
590 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
591 e70bf110 2020-03-22 stsp NULL, 0) == -1)
592 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
594 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
597 e70bf110 2020-03-22 stsp static const struct got_error *
598 ca6e02ac 2020-01-07 stsp commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
599 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx,
600 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
602 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
603 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
604 ca6e02ac 2020-01-07 stsp struct got_object_qid *pid;
605 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL, *pcommit = NULL;
606 78e7b7b8 2022-05-19 thomas struct got_parsed_tree_entry *entries = NULL, *pentries = NULL;
607 c77e00b3 2022-10-18 thomas size_t nentries = 0, nentries_alloc = 0;
608 c77e00b3 2022-10-18 thomas size_t pnentries = 0, pnentries_alloc = 0;
609 ca6e02ac 2020-01-07 stsp struct got_object_id id;
610 ca6e02ac 2020-01-07 stsp size_t datalen, path_len;
611 ca6e02ac 2020-01-07 stsp char *path = NULL;
612 ca6e02ac 2020-01-07 stsp const int min_alloc = 64;
613 ca6e02ac 2020-01-07 stsp int changed = 0, ncommits = 0, nallocated = 0;
614 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_ids = NULL;
616 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
617 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(iobj))
618 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
619 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
620 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
622 ca6e02ac 2020-01-07 stsp path_len = datalen - sizeof(iobj) - 1;
623 ca6e02ac 2020-01-07 stsp if (path_len < 0)
624 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
625 ca6e02ac 2020-01-07 stsp if (path_len > 0) {
626 ca6e02ac 2020-01-07 stsp path = imsg->data + sizeof(iobj);
627 ca6e02ac 2020-01-07 stsp if (path[path_len] != '\0')
628 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
631 ca6e02ac 2020-01-07 stsp nallocated = min_alloc;
632 ca6e02ac 2020-01-07 stsp commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
633 ca6e02ac 2020-01-07 stsp if (commit_ids == NULL)
634 ca6e02ac 2020-01-07 stsp return got_error_from_errno("reallocarray");
637 ca6e02ac 2020-01-07 stsp const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
640 ca6e02ac 2020-01-07 stsp if (sigint_received) {
641 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_CANCELLED);
645 ca6e02ac 2020-01-07 stsp if (commit == NULL) {
646 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx, &id);
647 ca6e02ac 2020-01-07 stsp if (idx == -1)
649 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx,
650 ca6e02ac 2020-01-07 stsp idx, &id, objcache);
652 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
654 ca6e02ac 2020-01-07 stsp err = NULL;
659 ca6e02ac 2020-01-07 stsp if (sizeof(struct got_imsg_traversed_commits) +
660 ca6e02ac 2020-01-07 stsp ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
661 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits,
665 ca6e02ac 2020-01-07 stsp ncommits = 0;
667 ca6e02ac 2020-01-07 stsp ncommits++;
668 ca6e02ac 2020-01-07 stsp if (ncommits > nallocated) {
669 ca6e02ac 2020-01-07 stsp struct got_object_id *new;
670 ca6e02ac 2020-01-07 stsp nallocated += min_alloc;
671 ca6e02ac 2020-01-07 stsp new = reallocarray(commit_ids, nallocated,
672 ca6e02ac 2020-01-07 stsp sizeof(*commit_ids));
673 ca6e02ac 2020-01-07 stsp if (new == NULL) {
674 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("reallocarray");
677 ca6e02ac 2020-01-07 stsp commit_ids = new;
679 ca6e02ac 2020-01-07 stsp memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
680 ca6e02ac 2020-01-07 stsp SHA1_DIGEST_LENGTH);
682 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(&commit->parent_ids);
683 ca6e02ac 2020-01-07 stsp if (pid == NULL)
686 ec242592 2022-04-22 thomas idx = got_packidx_get_object_idx(packidx, &pid->id);
687 ca6e02ac 2020-01-07 stsp if (idx == -1)
690 ec242592 2022-04-22 thomas err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
693 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
695 ca6e02ac 2020-01-07 stsp err = NULL;
699 ca6e02ac 2020-01-07 stsp if (path[0] == '/' && path[1] == '\0') {
700 ca6e02ac 2020-01-07 stsp if (got_object_id_cmp(pcommit->tree_id,
701 ca6e02ac 2020-01-07 stsp commit->tree_id) != 0) {
702 ca6e02ac 2020-01-07 stsp changed = 1;
707 ca6e02ac 2020-01-07 stsp uint8_t *buf = NULL, *pbuf = NULL;
709 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx,
710 ca6e02ac 2020-01-07 stsp commit->tree_id);
711 ca6e02ac 2020-01-07 stsp if (idx == -1)
713 ca6e02ac 2020-01-07 stsp pidx = got_packidx_get_object_idx(packidx,
714 ca6e02ac 2020-01-07 stsp pcommit->tree_id);
715 ca6e02ac 2020-01-07 stsp if (pidx == -1)
718 c77e00b3 2022-10-18 thomas err = open_tree(&buf, &entries, &nentries,
719 c77e00b3 2022-10-18 thomas &nentries_alloc, pack, packidx, idx,
720 c77e00b3 2022-10-18 thomas commit->tree_id, objcache);
723 c77e00b3 2022-10-18 thomas err = open_tree(&pbuf, &pentries, &pnentries,
724 c77e00b3 2022-10-18 thomas &pnentries_alloc, pack, packidx, pidx,
725 c77e00b3 2022-10-18 thomas pcommit->tree_id, objcache);
731 ca6e02ac 2020-01-07 stsp err = tree_path_changed(&changed, &buf, &pbuf,
732 c77e00b3 2022-10-18 thomas &entries, &nentries, &nentries_alloc,
733 c77e00b3 2022-10-18 thomas &pentries, &pnentries, &pnentries_alloc,
734 c77e00b3 2022-10-18 thomas path, pack, packidx, ibuf, objcache);
736 ca6e02ac 2020-01-07 stsp nentries = 0;
738 ca6e02ac 2020-01-07 stsp pnentries = 0;
739 ca6e02ac 2020-01-07 stsp free(pbuf);
741 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
743 ca6e02ac 2020-01-07 stsp err = NULL;
748 ca6e02ac 2020-01-07 stsp if (!changed) {
749 ec242592 2022-04-22 thomas memcpy(id.sha1, pid->id.sha1, SHA1_DIGEST_LENGTH);
750 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
751 ca6e02ac 2020-01-07 stsp commit = pcommit;
752 ca6e02ac 2020-01-07 stsp pcommit = NULL;
754 ca6e02ac 2020-01-07 stsp } while (!changed);
756 ca6e02ac 2020-01-07 stsp if (ncommits > 0) {
757 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits, ibuf);
761 ca6e02ac 2020-01-07 stsp if (changed) {
762 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit(ibuf, commit);
767 e70bf110 2020-03-22 stsp err = send_commit_traversal_done(ibuf);
769 ca6e02ac 2020-01-07 stsp free(commit_ids);
770 ca6e02ac 2020-01-07 stsp if (commit)
771 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
772 ca6e02ac 2020-01-07 stsp if (pcommit)
773 ca6e02ac 2020-01-07 stsp got_object_commit_close(pcommit);
774 78e7b7b8 2022-05-19 thomas free(entries);
775 78e7b7b8 2022-05-19 thomas free(pentries);
777 f4a881ce 2018-11-17 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
778 f4a881ce 2018-11-17 stsp err = NULL;
780 f4a881ce 2018-11-17 stsp got_privsep_send_error(ibuf, err);
783 f4a881ce 2018-11-17 stsp return err;
786 f4a881ce 2018-11-17 stsp static const struct got_error *
787 48b4f239 2021-12-31 thomas raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
788 48b4f239 2021-12-31 thomas struct got_pack *pack, struct got_packidx *packidx,
789 bc1f382f 2022-01-05 thomas struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
791 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
792 59d1e4a0 2021-03-10 stsp uint8_t *buf = NULL;
793 59d1e4a0 2021-03-10 stsp uint64_t size = 0;
794 bc1f382f 2022-01-05 thomas FILE *outfile = NULL;
795 59d1e4a0 2021-03-10 stsp struct got_imsg_packed_object iobj;
796 59d1e4a0 2021-03-10 stsp struct got_object *obj;
797 59d1e4a0 2021-03-10 stsp struct got_object_id id;
798 59d1e4a0 2021-03-10 stsp size_t datalen;
800 59d1e4a0 2021-03-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
801 59d1e4a0 2021-03-10 stsp if (datalen != sizeof(iobj))
802 59d1e4a0 2021-03-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
803 59d1e4a0 2021-03-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
804 59d1e4a0 2021-03-10 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
806 59d1e4a0 2021-03-10 stsp obj = got_object_cache_get(objcache, &id);
808 59d1e4a0 2021-03-10 stsp obj->refcnt++;
810 59d1e4a0 2021-03-10 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
813 59d1e4a0 2021-03-10 stsp return err;
816 59d1e4a0 2021-03-10 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
818 59d1e4a0 2021-03-10 stsp return err;
820 59d1e4a0 2021-03-10 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
821 59d1e4a0 2021-03-10 stsp err = got_pack_get_max_delta_object_size(&size, obj, pack);
825 59d1e4a0 2021-03-10 stsp size = obj->size;
827 59d1e4a0 2021-03-10 stsp if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
828 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
829 59d1e4a0 2021-03-10 stsp obj, pack);
831 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
832 59d1e4a0 2021-03-10 stsp accumfile);
836 40e3cb72 2021-06-22 stsp err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
839 59d1e4a0 2021-03-10 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
840 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fclose");
841 59d1e4a0 2021-03-10 stsp got_object_close(obj);
842 59d1e4a0 2021-03-10 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
843 59d1e4a0 2021-03-10 stsp got_privsep_send_error(ibuf, err);
845 59d1e4a0 2021-03-10 stsp return err;
848 f9c2e8e5 2022-02-13 thomas static const struct got_error *
849 f9c2e8e5 2022-02-13 thomas get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
850 f9c2e8e5 2022-02-13 thomas off_t base_offset)
852 f9c2e8e5 2022-02-13 thomas const struct got_error *err;
855 b6b86fd1 2022-08-30 thomas err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
857 f9c2e8e5 2022-02-13 thomas return err;
858 f9c2e8e5 2022-02-13 thomas if (idx == -1)
859 f9c2e8e5 2022-02-13 thomas return got_error(GOT_ERR_BAD_PACKIDX);
861 f9c2e8e5 2022-02-13 thomas return got_packidx_get_object_id(base_id, packidx, idx);
864 59d1e4a0 2021-03-10 stsp static const struct got_error *
865 f9c2e8e5 2022-02-13 thomas raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
866 f9c2e8e5 2022-02-13 thomas FILE *delta_outfile, struct got_pack *pack,
867 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx)
869 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
870 f9c2e8e5 2022-02-13 thomas struct got_imsg_raw_delta_request req;
871 9249e7e3 2022-05-12 thomas size_t datalen, delta_size, delta_compressed_size;
872 c44c7d6e 2022-12-04 thomas off_t delta_offset, delta_data_offset;
873 f9c2e8e5 2022-02-13 thomas uint8_t *delta_buf = NULL;
874 f9c2e8e5 2022-02-13 thomas struct got_object_id id, base_id;
875 f9c2e8e5 2022-02-13 thomas off_t base_offset, delta_out_offset = 0;
876 f9c2e8e5 2022-02-13 thomas uint64_t base_size = 0, result_size = 0;
877 f9c2e8e5 2022-02-13 thomas size_t w;
879 f9c2e8e5 2022-02-13 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
880 f9c2e8e5 2022-02-13 thomas if (datalen != sizeof(req))
881 f9c2e8e5 2022-02-13 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
882 f9c2e8e5 2022-02-13 thomas memcpy(&req, imsg->data, sizeof(req));
883 f9c2e8e5 2022-02-13 thomas memcpy(id.sha1, req.id, SHA1_DIGEST_LENGTH);
885 f9c2e8e5 2022-02-13 thomas imsg->fd = -1;
887 f9c2e8e5 2022-02-13 thomas err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
888 c44c7d6e 2022-12-04 thomas &delta_compressed_size, &delta_offset, &delta_data_offset,
889 c44c7d6e 2022-12-04 thomas &base_offset, &base_id, &base_size, &result_size,
890 c44c7d6e 2022-12-04 thomas pack, packidx, req.idx);
892 f9c2e8e5 2022-02-13 thomas goto done;
895 f9c2e8e5 2022-02-13 thomas * If this is an offset delta we must determine the base
896 f9c2e8e5 2022-02-13 thomas * object ID ourselves.
898 f9c2e8e5 2022-02-13 thomas if (base_offset != 0) {
899 f9c2e8e5 2022-02-13 thomas err = get_base_object_id(&base_id, packidx, base_offset);
901 f9c2e8e5 2022-02-13 thomas goto done;
904 f9c2e8e5 2022-02-13 thomas delta_out_offset = ftello(delta_outfile);
905 9249e7e3 2022-05-12 thomas w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
906 9249e7e3 2022-05-12 thomas if (w != delta_compressed_size) {
907 f9c2e8e5 2022-02-13 thomas err = got_ferror(delta_outfile, GOT_ERR_IO);
908 f9c2e8e5 2022-02-13 thomas goto done;
910 f9c2e8e5 2022-02-13 thomas if (fflush(delta_outfile) == -1) {
911 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fflush");
912 f9c2e8e5 2022-02-13 thomas goto done;
915 f9c2e8e5 2022-02-13 thomas err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
916 9249e7e3 2022-05-12 thomas delta_size, delta_compressed_size, delta_offset, delta_out_offset,
917 9249e7e3 2022-05-12 thomas &base_id);
919 f9c2e8e5 2022-02-13 thomas free(delta_buf);
920 f9c2e8e5 2022-02-13 thomas return err;
923 7d0d4920 2022-05-12 thomas struct search_deltas_arg {
924 7d0d4920 2022-05-12 thomas struct imsgbuf *ibuf;
925 7d0d4920 2022-05-12 thomas struct got_packidx *packidx;
926 7d0d4920 2022-05-12 thomas struct got_pack *pack;
927 7d0d4920 2022-05-12 thomas struct got_object_idset *idset;
928 7d0d4920 2022-05-12 thomas struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
929 7d0d4920 2022-05-12 thomas size_t ndeltas;
932 f9c2e8e5 2022-02-13 thomas static const struct got_error *
933 7d0d4920 2022-05-12 thomas search_delta_for_object(struct got_object_id *id, void *data, void *arg)
935 7d0d4920 2022-05-12 thomas const struct got_error *err;
936 7d0d4920 2022-05-12 thomas struct search_deltas_arg *a = arg;
937 7d0d4920 2022-05-12 thomas int obj_idx;
938 7d0d4920 2022-05-12 thomas uint8_t *delta_buf = NULL;
939 7d0d4920 2022-05-12 thomas uint64_t base_size, result_size;
940 7d0d4920 2022-05-12 thomas size_t delta_size, delta_compressed_size;
941 c44c7d6e 2022-12-04 thomas off_t delta_offset, delta_data_offset, base_offset;
942 7d0d4920 2022-05-12 thomas struct got_object_id base_id;
944 7d0d4920 2022-05-12 thomas if (sigint_received)
945 7d0d4920 2022-05-12 thomas return got_error(GOT_ERR_CANCELLED);
947 7d0d4920 2022-05-12 thomas obj_idx = got_packidx_get_object_idx(a->packidx, id);
948 7d0d4920 2022-05-12 thomas if (obj_idx == -1)
949 7d0d4920 2022-05-12 thomas return NULL; /* object not present in our pack file */
951 7d0d4920 2022-05-12 thomas err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
952 c44c7d6e 2022-12-04 thomas &delta_compressed_size, &delta_offset, &delta_data_offset,
953 c44c7d6e 2022-12-04 thomas &base_offset, &base_id, &base_size, &result_size,
954 c44c7d6e 2022-12-04 thomas a->pack, a->packidx, obj_idx);
955 7d0d4920 2022-05-12 thomas if (err) {
956 7d0d4920 2022-05-12 thomas if (err->code == GOT_ERR_OBJ_TYPE)
957 7d0d4920 2022-05-12 thomas return NULL; /* object not stored as a delta */
958 7d0d4920 2022-05-12 thomas return err;
962 7d0d4920 2022-05-12 thomas * If this is an offset delta we must determine the base
963 7d0d4920 2022-05-12 thomas * object ID ourselves.
965 7d0d4920 2022-05-12 thomas if (base_offset != 0) {
966 7d0d4920 2022-05-12 thomas err = get_base_object_id(&base_id, a->packidx, base_offset);
968 7d0d4920 2022-05-12 thomas goto done;
971 7d0d4920 2022-05-12 thomas if (got_object_idset_contains(a->idset, &base_id)) {
972 7d0d4920 2022-05-12 thomas struct got_imsg_reused_delta *delta;
974 7d0d4920 2022-05-12 thomas delta = &a->deltas[a->ndeltas++];
975 7d0d4920 2022-05-12 thomas memcpy(&delta->id, id, sizeof(delta->id));
976 7d0d4920 2022-05-12 thomas memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
977 7d0d4920 2022-05-12 thomas delta->base_size = base_size;
978 7d0d4920 2022-05-12 thomas delta->result_size = result_size;
979 7d0d4920 2022-05-12 thomas delta->delta_size = delta_size;
980 7d0d4920 2022-05-12 thomas delta->delta_compressed_size = delta_compressed_size;
981 c44c7d6e 2022-12-04 thomas delta->delta_offset = delta_data_offset;
983 7d0d4920 2022-05-12 thomas if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
984 7d0d4920 2022-05-12 thomas err = got_privsep_send_reused_deltas(a->ibuf,
985 7d0d4920 2022-05-12 thomas a->deltas, a->ndeltas);
987 7d0d4920 2022-05-12 thomas goto done;
988 7d0d4920 2022-05-12 thomas a->ndeltas = 0;
992 7d0d4920 2022-05-12 thomas free(delta_buf);
993 7d0d4920 2022-05-12 thomas return err;
996 7d0d4920 2022-05-12 thomas static const struct got_error *
997 7d0d4920 2022-05-12 thomas recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
999 7d0d4920 2022-05-12 thomas const struct got_error *err = NULL;
1000 7d0d4920 2022-05-12 thomas int done = 0;
1001 7d0d4920 2022-05-12 thomas struct got_object_id *ids;
1002 7d0d4920 2022-05-12 thomas size_t nids, i;
1004 7d0d4920 2022-05-12 thomas for (;;) {
1005 7d0d4920 2022-05-12 thomas err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1006 7d0d4920 2022-05-12 thomas if (err || done)
1008 7d0d4920 2022-05-12 thomas for (i = 0; i < nids; i++) {
1009 7d0d4920 2022-05-12 thomas err = got_object_idset_add(idset, &ids[i], NULL);
1010 7d0d4920 2022-05-12 thomas if (err) {
1011 7d0d4920 2022-05-12 thomas free(ids);
1012 7d0d4920 2022-05-12 thomas return err;
1015 7d0d4920 2022-05-12 thomas free(ids);
1018 63915ee5 2022-06-23 thomas return err;
1021 63915ee5 2022-06-23 thomas static const struct got_error *
1022 63915ee5 2022-06-23 thomas recv_object_id_queue(struct got_object_id_queue *queue, struct imsgbuf *ibuf)
1024 63915ee5 2022-06-23 thomas const struct got_error *err = NULL;
1025 63915ee5 2022-06-23 thomas int done = 0;
1026 63915ee5 2022-06-23 thomas struct got_object_qid *qid;
1027 63915ee5 2022-06-23 thomas struct got_object_id *ids;
1028 63915ee5 2022-06-23 thomas size_t nids, i;
1030 63915ee5 2022-06-23 thomas for (;;) {
1031 63915ee5 2022-06-23 thomas err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1032 63915ee5 2022-06-23 thomas if (err || done)
1034 63915ee5 2022-06-23 thomas for (i = 0; i < nids; i++) {
1035 63915ee5 2022-06-23 thomas err = got_object_qid_alloc_partial(&qid);
1036 63915ee5 2022-06-23 thomas if (err)
1037 63915ee5 2022-06-23 thomas return err;
1038 63915ee5 2022-06-23 thomas memcpy(&qid->id, &ids[i], sizeof(qid->id));
1039 63915ee5 2022-06-23 thomas STAILQ_INSERT_TAIL(queue, qid, entry);
1043 13280750 2022-06-13 thomas return err;
1046 13280750 2022-06-13 thomas static const struct got_error *
1047 7d0d4920 2022-05-12 thomas delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1048 c44c7d6e 2022-12-04 thomas struct got_pack *pack, struct got_packidx *packidx)
1050 7d0d4920 2022-05-12 thomas const struct got_error *err = NULL;
1051 7d0d4920 2022-05-12 thomas struct got_object_idset *idset;
1052 7d0d4920 2022-05-12 thomas struct search_deltas_arg sda;
1054 7d0d4920 2022-05-12 thomas idset = got_object_idset_alloc();
1055 7d0d4920 2022-05-12 thomas if (idset == NULL)
1056 7d0d4920 2022-05-12 thomas return got_error_from_errno("got_object_idset_alloc");
1058 7d0d4920 2022-05-12 thomas err = recv_object_ids(idset, ibuf);
1059 7d0d4920 2022-05-12 thomas if (err)
1060 7d0d4920 2022-05-12 thomas return err;
1062 7d0d4920 2022-05-12 thomas memset(&sda, 0, sizeof(sda));
1063 7d0d4920 2022-05-12 thomas sda.ibuf = ibuf;
1064 7d0d4920 2022-05-12 thomas sda.idset = idset;
1065 7d0d4920 2022-05-12 thomas sda.pack = pack;
1066 7d0d4920 2022-05-12 thomas sda.packidx = packidx;
1067 7d0d4920 2022-05-12 thomas err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1068 7d0d4920 2022-05-12 thomas if (err)
1069 7d0d4920 2022-05-12 thomas goto done;
1071 7d0d4920 2022-05-12 thomas if (sda.ndeltas > 0) {
1072 7d0d4920 2022-05-12 thomas err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1073 7d0d4920 2022-05-12 thomas sda.ndeltas);
1074 7d0d4920 2022-05-12 thomas if (err)
1075 7d0d4920 2022-05-12 thomas goto done;
1078 7d0d4920 2022-05-12 thomas err = got_privsep_send_reused_deltas_done(ibuf);
1080 7d0d4920 2022-05-12 thomas got_object_idset_free(idset);
1081 7d0d4920 2022-05-12 thomas return err;
1084 7d0d4920 2022-05-12 thomas static const struct got_error *
1085 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1087 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1088 876c234b 2018-09-10 stsp struct imsg imsg;
1089 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1090 876c234b 2018-09-10 stsp size_t datalen;
1091 876c234b 2018-09-10 stsp struct got_packidx *p;
1093 876c234b 2018-09-10 stsp *packidx = NULL;
1095 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1097 876c234b 2018-09-10 stsp return err;
1099 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
1100 876c234b 2018-09-10 stsp if (p == NULL) {
1101 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1102 876c234b 2018-09-10 stsp goto done;
1105 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1106 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1107 876c234b 2018-09-10 stsp goto done;
1110 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
1111 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1112 876c234b 2018-09-10 stsp goto done;
1115 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1116 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
1117 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1118 876c234b 2018-09-10 stsp goto done;
1120 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1122 876c234b 2018-09-10 stsp p->len = ipackidx.len;
1123 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
1124 876c234b 2018-09-10 stsp if (p->fd == -1) {
1125 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
1126 56bef47a 2018-09-15 stsp goto done;
1128 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
1129 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1130 876c234b 2018-09-10 stsp goto done;
1133 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
1134 aa75acde 2022-10-25 thomas if (p->len > 0 && p->len <= SIZE_MAX) {
1135 aa75acde 2022-10-25 thomas p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1136 aa75acde 2022-10-25 thomas if (p->map == MAP_FAILED)
1137 aa75acde 2022-10-25 thomas p->map = NULL; /* fall back to read(2) */
1140 c3564dfa 2021-07-15 stsp err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1142 876c234b 2018-09-10 stsp if (err) {
1143 876c234b 2018-09-10 stsp if (imsg.fd != -1)
1144 876c234b 2018-09-10 stsp close(imsg.fd);
1145 876c234b 2018-09-10 stsp got_packidx_close(p);
1147 876c234b 2018-09-10 stsp *packidx = p;
1148 876c234b 2018-09-10 stsp imsg_free(&imsg);
1149 63915ee5 2022-06-23 thomas return err;
1152 63915ee5 2022-06-23 thomas static const struct got_error *
1153 63915ee5 2022-06-23 thomas send_tree_enumeration_done(struct imsgbuf *ibuf)
1155 63915ee5 2022-06-23 thomas if (imsg_compose(ibuf, GOT_IMSG_TREE_ENUMERATION_DONE, 0, 0, -1,
1156 63915ee5 2022-06-23 thomas NULL, 0) == -1)
1157 63915ee5 2022-06-23 thomas return got_error_from_errno("imsg_compose TREE_ENUMERATION_DONE");
1159 63915ee5 2022-06-23 thomas return got_privsep_flush_imsg(ibuf);
1162 63915ee5 2022-06-23 thomas struct enumerated_tree {
1163 63915ee5 2022-06-23 thomas struct got_object_id id;
1164 63915ee5 2022-06-23 thomas char *path;
1165 63915ee5 2022-06-23 thomas uint8_t *buf;
1166 63915ee5 2022-06-23 thomas struct got_parsed_tree_entry *entries;
1167 63915ee5 2022-06-23 thomas int nentries;
1170 63915ee5 2022-06-23 thomas static const struct got_error *
1171 63915ee5 2022-06-23 thomas enumerate_tree(int *have_all_entries, struct imsgbuf *ibuf, size_t *totlen,
1172 63915ee5 2022-06-23 thomas struct got_object_id *tree_id,
1173 63915ee5 2022-06-23 thomas const char *path, struct got_pack *pack, struct got_packidx *packidx,
1174 63915ee5 2022-06-23 thomas struct got_object_cache *objcache, struct got_object_idset *idset,
1175 63915ee5 2022-06-23 thomas struct enumerated_tree **trees, size_t *nalloc, size_t *ntrees)
1177 63915ee5 2022-06-23 thomas const struct got_error *err = NULL;
1178 63915ee5 2022-06-23 thomas struct got_object_id_queue ids;
1179 63915ee5 2022-06-23 thomas struct got_object_qid *qid;
1180 63915ee5 2022-06-23 thomas uint8_t *buf = NULL;
1181 63915ee5 2022-06-23 thomas struct got_parsed_tree_entry *entries = NULL;
1182 c77e00b3 2022-10-18 thomas size_t nentries = 0, nentries_alloc = 0, i;
1183 63915ee5 2022-06-23 thomas struct enumerated_tree *tree;
1185 63915ee5 2022-06-23 thomas *ntrees = 0;
1186 63915ee5 2022-06-23 thomas *have_all_entries = 1;
1187 63915ee5 2022-06-23 thomas STAILQ_INIT(&ids);
1189 63915ee5 2022-06-23 thomas err = got_object_qid_alloc_partial(&qid);
1190 63915ee5 2022-06-23 thomas if (err)
1191 63915ee5 2022-06-23 thomas return err;
1192 63915ee5 2022-06-23 thomas memcpy(&qid->id.sha1, tree_id, SHA1_DIGEST_LENGTH);
1193 63915ee5 2022-06-23 thomas qid->data = strdup(path);
1194 63915ee5 2022-06-23 thomas if (qid->data == NULL) {
1195 63915ee5 2022-06-23 thomas err = got_error_from_errno("strdup");
1196 63915ee5 2022-06-23 thomas goto done;
1198 63915ee5 2022-06-23 thomas STAILQ_INSERT_TAIL(&ids, qid, entry);
1199 63915ee5 2022-06-23 thomas qid = NULL;
1201 63915ee5 2022-06-23 thomas /* Traverse the tree hierarchy, gather tree object IDs and paths. */
1203 63915ee5 2022-06-23 thomas const char *path;
1204 63915ee5 2022-06-23 thomas int idx, i;
1206 63915ee5 2022-06-23 thomas if (sigint_received) {
1207 63915ee5 2022-06-23 thomas err = got_error(GOT_ERR_CANCELLED);
1208 63915ee5 2022-06-23 thomas goto done;
1211 63915ee5 2022-06-23 thomas qid = STAILQ_FIRST(&ids);
1212 63915ee5 2022-06-23 thomas STAILQ_REMOVE_HEAD(&ids, entry);
1213 63915ee5 2022-06-23 thomas path = qid->data;
1215 63915ee5 2022-06-23 thomas idx = got_packidx_get_object_idx(packidx, &qid->id);
1216 63915ee5 2022-06-23 thomas if (idx == -1) {
1217 63915ee5 2022-06-23 thomas *have_all_entries = 0;
1221 c77e00b3 2022-10-18 thomas err = open_tree(&buf, &entries, &nentries, &nentries_alloc,
1222 63915ee5 2022-06-23 thomas pack, packidx, idx, &qid->id, objcache);
1223 63915ee5 2022-06-23 thomas if (err) {
1224 63915ee5 2022-06-23 thomas if (err->code != GOT_ERR_NO_OBJ)
1225 63915ee5 2022-06-23 thomas goto done;
1228 63915ee5 2022-06-23 thomas err = got_object_idset_add(idset, &qid->id, NULL);
1229 63915ee5 2022-06-23 thomas if (err)
1230 63915ee5 2022-06-23 thomas goto done;
1232 63915ee5 2022-06-23 thomas for (i = 0; i < nentries; i++) {
1233 63915ee5 2022-06-23 thomas struct got_object_qid *eqid = NULL;
1234 63915ee5 2022-06-23 thomas struct got_parsed_tree_entry *pte = &entries[i];
1235 63915ee5 2022-06-23 thomas char *p;
1237 63915ee5 2022-06-23 thomas if (!S_ISDIR(pte->mode))
1238 63915ee5 2022-06-23 thomas continue;
1240 63915ee5 2022-06-23 thomas err = got_object_qid_alloc_partial(&eqid);
1241 63915ee5 2022-06-23 thomas if (err)
1242 63915ee5 2022-06-23 thomas goto done;
1243 63915ee5 2022-06-23 thomas memcpy(eqid->id.sha1, pte->id, sizeof(eqid->id.sha1));
1245 63915ee5 2022-06-23 thomas if (got_object_idset_contains(idset, &eqid->id)) {
1246 63915ee5 2022-06-23 thomas got_object_qid_free(eqid);
1247 63915ee5 2022-06-23 thomas continue;
1250 63915ee5 2022-06-23 thomas if (asprintf(&p, "%s%s%s", path,
1251 63915ee5 2022-06-23 thomas got_path_is_root_dir(path) ? "" : "/",
1252 63915ee5 2022-06-23 thomas pte->name) == -1) {
1253 63915ee5 2022-06-23 thomas err = got_error_from_errno("asprintf");
1254 63915ee5 2022-06-23 thomas got_object_qid_free(eqid);
1255 63915ee5 2022-06-23 thomas goto done;
1257 63915ee5 2022-06-23 thomas eqid->data = p;
1258 63915ee5 2022-06-23 thomas STAILQ_INSERT_TAIL(&ids, eqid, entry);
1261 63915ee5 2022-06-23 thomas if (*ntrees >= *nalloc) {
1262 63915ee5 2022-06-23 thomas struct enumerated_tree *new;
1263 63915ee5 2022-06-23 thomas new = recallocarray(*trees, *nalloc, *nalloc + 16,
1264 63915ee5 2022-06-23 thomas sizeof(*new));
1265 63915ee5 2022-06-23 thomas if (new == NULL) {
1266 63915ee5 2022-06-23 thomas err = got_error_from_errno("malloc");
1267 63915ee5 2022-06-23 thomas goto done;
1269 63915ee5 2022-06-23 thomas *trees = new;
1270 63915ee5 2022-06-23 thomas *nalloc += 16;
1272 63915ee5 2022-06-23 thomas tree = &(*trees)[*ntrees];
1273 63915ee5 2022-06-23 thomas (*ntrees)++;
1274 63915ee5 2022-06-23 thomas memcpy(&tree->id, &qid->id, sizeof(tree->id));
1275 63915ee5 2022-06-23 thomas tree->path = qid->data;
1276 63915ee5 2022-06-23 thomas tree->buf = buf;
1277 63915ee5 2022-06-23 thomas buf = NULL;
1278 63915ee5 2022-06-23 thomas tree->entries = entries;
1279 63915ee5 2022-06-23 thomas entries = NULL;
1280 c77e00b3 2022-10-18 thomas nentries_alloc = 0;
1281 63915ee5 2022-06-23 thomas tree->nentries = nentries;
1282 c77e00b3 2022-10-18 thomas nentries = 0;
1284 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1285 63915ee5 2022-06-23 thomas qid = NULL;
1286 63915ee5 2022-06-23 thomas } while (!STAILQ_EMPTY(&ids));
1288 63915ee5 2022-06-23 thomas if (*have_all_entries) {
1291 63915ee5 2022-06-23 thomas * We have managed to traverse all entries in the hierarchy.
1292 63915ee5 2022-06-23 thomas * Tell the main process what we have found.
1294 63915ee5 2022-06-23 thomas for (i = 0; i < *ntrees; i++) {
1295 63915ee5 2022-06-23 thomas tree = &(*trees)[i];
1296 63915ee5 2022-06-23 thomas err = got_privsep_send_enumerated_tree(totlen,
1297 63915ee5 2022-06-23 thomas ibuf, &tree->id, tree->path, tree->entries,
1298 63915ee5 2022-06-23 thomas tree->nentries);
1299 63915ee5 2022-06-23 thomas if (err)
1300 63915ee5 2022-06-23 thomas goto done;
1301 63915ee5 2022-06-23 thomas free(tree->buf);
1302 63915ee5 2022-06-23 thomas tree->buf = NULL;
1303 63915ee5 2022-06-23 thomas free(tree->path);
1304 63915ee5 2022-06-23 thomas tree->path = NULL;
1305 63915ee5 2022-06-23 thomas free(tree->entries);
1306 63915ee5 2022-06-23 thomas tree->entries = NULL;
1308 63915ee5 2022-06-23 thomas *ntrees = 0; /* don't loop again below to free memory */
1310 63915ee5 2022-06-23 thomas err = send_tree_enumeration_done(ibuf);
1311 63915ee5 2022-06-23 thomas } else {
1313 63915ee5 2022-06-23 thomas * We can only load fully packed tree hierarchies on
1314 63915ee5 2022-06-23 thomas * behalf of the main process, otherwise the main process
1315 63915ee5 2022-06-23 thomas * gets a wrong idea about which tree objects have
1316 63915ee5 2022-06-23 thomas * already been traversed.
1317 63915ee5 2022-06-23 thomas * Indicate a missing entry for the root of this tree.
1318 63915ee5 2022-06-23 thomas * The main process should continue by loading this
1319 63915ee5 2022-06-23 thomas * entire tree the slow way.
1321 63915ee5 2022-06-23 thomas err = got_privsep_send_enumerated_tree(totlen, ibuf,
1322 63915ee5 2022-06-23 thomas tree_id, "/", NULL, -1);
1323 63915ee5 2022-06-23 thomas if (err)
1324 63915ee5 2022-06-23 thomas goto done;
1327 63915ee5 2022-06-23 thomas free(buf);
1328 63915ee5 2022-06-23 thomas free(entries);
1329 63915ee5 2022-06-23 thomas for (i = 0; i < *ntrees; i++) {
1330 63915ee5 2022-06-23 thomas tree = &(*trees)[i];
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 if (qid)
1339 63915ee5 2022-06-23 thomas free(qid->data);
1340 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1341 63915ee5 2022-06-23 thomas got_object_id_queue_free(&ids);
1342 63915ee5 2022-06-23 thomas if (err) {
1343 63915ee5 2022-06-23 thomas if (err->code == GOT_ERR_PRIVSEP_PIPE)
1344 63915ee5 2022-06-23 thomas err = NULL;
1346 63915ee5 2022-06-23 thomas got_privsep_send_error(ibuf, err);
1349 13280750 2022-06-13 thomas return err;
1352 13280750 2022-06-13 thomas static const struct got_error *
1353 63915ee5 2022-06-23 thomas enumeration_request(struct imsg *imsg, struct imsgbuf *ibuf,
1354 63915ee5 2022-06-23 thomas struct got_pack *pack, struct got_packidx *packidx,
1355 63915ee5 2022-06-23 thomas struct got_object_cache *objcache)
1357 63915ee5 2022-06-23 thomas const struct got_error *err = NULL;
1358 63915ee5 2022-06-23 thomas struct got_object_id_queue commit_ids;
1359 63915ee5 2022-06-23 thomas const struct got_object_id_queue *parents = NULL;
1360 63915ee5 2022-06-23 thomas struct got_object_qid *qid = NULL;
1361 63915ee5 2022-06-23 thomas struct got_object *obj = NULL;
1362 63915ee5 2022-06-23 thomas struct got_commit_object *commit = NULL;
1363 63915ee5 2022-06-23 thomas struct got_object_id *tree_id = NULL;
1364 63915ee5 2022-06-23 thomas size_t totlen = 0;
1365 63915ee5 2022-06-23 thomas struct got_object_idset *idset;
1366 63915ee5 2022-06-23 thomas int i, idx, have_all_entries = 1;
1367 63915ee5 2022-06-23 thomas struct enumerated_tree *trees = NULL;
1368 63915ee5 2022-06-23 thomas size_t ntrees = 0, nalloc = 16;
1370 63915ee5 2022-06-23 thomas STAILQ_INIT(&commit_ids);
1372 93088cca 2022-06-23 thomas trees = calloc(nalloc, sizeof(*trees));
1373 63915ee5 2022-06-23 thomas if (trees == NULL)
1374 63915ee5 2022-06-23 thomas return got_error_from_errno("calloc");
1376 63915ee5 2022-06-23 thomas idset = got_object_idset_alloc();
1377 63915ee5 2022-06-23 thomas if (idset == NULL) {
1378 63915ee5 2022-06-23 thomas err = got_error_from_errno("got_object_idset_alloc");
1379 63915ee5 2022-06-23 thomas goto done;
1382 63915ee5 2022-06-23 thomas err = recv_object_id_queue(&commit_ids, ibuf);
1383 63915ee5 2022-06-23 thomas if (err)
1384 63915ee5 2022-06-23 thomas goto done;
1386 ef53e23c 2022-06-23 thomas if (STAILQ_EMPTY(&commit_ids)) {
1387 ef53e23c 2022-06-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1388 ef53e23c 2022-06-23 thomas goto done;
1391 63915ee5 2022-06-23 thomas err = recv_object_ids(idset, ibuf);
1392 63915ee5 2022-06-23 thomas if (err)
1393 63915ee5 2022-06-23 thomas goto done;
1395 63915ee5 2022-06-23 thomas while (!STAILQ_EMPTY(&commit_ids)) {
1396 63915ee5 2022-06-23 thomas if (sigint_received) {
1397 63915ee5 2022-06-23 thomas err = got_error(GOT_ERR_CANCELLED);
1398 63915ee5 2022-06-23 thomas goto done;
1401 63915ee5 2022-06-23 thomas qid = STAILQ_FIRST(&commit_ids);
1402 63915ee5 2022-06-23 thomas STAILQ_REMOVE_HEAD(&commit_ids, entry);
1404 63915ee5 2022-06-23 thomas if (got_object_idset_contains(idset, &qid->id)) {
1405 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1406 63915ee5 2022-06-23 thomas qid = NULL;
1407 63915ee5 2022-06-23 thomas continue;
1410 63915ee5 2022-06-23 thomas idx = got_packidx_get_object_idx(packidx, &qid->id);
1411 e71f1e62 2022-06-23 thomas if (idx == -1) {
1412 e71f1e62 2022-06-23 thomas have_all_entries = 0;
1416 63915ee5 2022-06-23 thomas err = open_object(&obj, pack, packidx, idx, &qid->id,
1417 63915ee5 2022-06-23 thomas objcache);
1418 63915ee5 2022-06-23 thomas if (err)
1419 63915ee5 2022-06-23 thomas goto done;
1420 63915ee5 2022-06-23 thomas if (obj->type == GOT_OBJ_TYPE_TAG) {
1421 63915ee5 2022-06-23 thomas struct got_tag_object *tag;
1422 63915ee5 2022-06-23 thomas uint8_t *buf;
1423 63915ee5 2022-06-23 thomas size_t len;
1424 63915ee5 2022-06-23 thomas err = got_packfile_extract_object_to_mem(&buf,
1425 63915ee5 2022-06-23 thomas &len, obj, pack);
1426 63915ee5 2022-06-23 thomas if (err)
1427 63915ee5 2022-06-23 thomas goto done;
1428 63915ee5 2022-06-23 thomas obj->size = len;
1429 63915ee5 2022-06-23 thomas err = got_object_parse_tag(&tag, buf, len);
1430 63915ee5 2022-06-23 thomas if (err) {
1431 63915ee5 2022-06-23 thomas free(buf);
1432 63915ee5 2022-06-23 thomas goto done;
1434 63915ee5 2022-06-23 thomas idx = got_packidx_get_object_idx(packidx, &tag->id);
1435 e71f1e62 2022-06-23 thomas if (idx == -1) {
1436 e71f1e62 2022-06-23 thomas have_all_entries = 0;
1439 63915ee5 2022-06-23 thomas err = open_commit(&commit, pack, packidx, idx,
1440 63915ee5 2022-06-23 thomas &tag->id, objcache);
1441 63915ee5 2022-06-23 thomas got_object_tag_close(tag);
1442 63915ee5 2022-06-23 thomas free(buf);
1443 63915ee5 2022-06-23 thomas if (err)
1444 63915ee5 2022-06-23 thomas goto done;
1445 63915ee5 2022-06-23 thomas } else if (obj->type == GOT_OBJ_TYPE_COMMIT) {
1446 63915ee5 2022-06-23 thomas err = open_commit(&commit, pack, packidx, idx,
1447 63915ee5 2022-06-23 thomas &qid->id, objcache);
1448 63915ee5 2022-06-23 thomas if (err)
1449 63915ee5 2022-06-23 thomas goto done;
1450 63915ee5 2022-06-23 thomas } else {
1451 63915ee5 2022-06-23 thomas err = got_error(GOT_ERR_OBJ_TYPE);
1452 63915ee5 2022-06-23 thomas goto done;
1454 63915ee5 2022-06-23 thomas got_object_close(obj);
1455 63915ee5 2022-06-23 thomas obj = NULL;
1457 63915ee5 2022-06-23 thomas err = got_privsep_send_enumerated_commit(ibuf, &qid->id,
1458 63915ee5 2022-06-23 thomas got_object_commit_get_committer_time(commit));
1459 63915ee5 2022-06-23 thomas if (err)
1460 63915ee5 2022-06-23 thomas goto done;
1462 63915ee5 2022-06-23 thomas tree_id = got_object_commit_get_tree_id(commit);
1463 63915ee5 2022-06-23 thomas idx = got_packidx_get_object_idx(packidx, tree_id);
1464 63915ee5 2022-06-23 thomas if (idx == -1) {
1465 e71f1e62 2022-06-23 thomas have_all_entries = 0;
1466 63915ee5 2022-06-23 thomas err = got_privsep_send_enumerated_tree(&totlen, ibuf,
1467 63915ee5 2022-06-23 thomas tree_id, "/", NULL, -1);
1468 63915ee5 2022-06-23 thomas if (err)
1469 63915ee5 2022-06-23 thomas goto done;
1473 63915ee5 2022-06-23 thomas if (got_object_idset_contains(idset, tree_id)) {
1474 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1475 63915ee5 2022-06-23 thomas qid = NULL;
1476 63915ee5 2022-06-23 thomas continue;
1479 0f4feb58 2022-06-23 thomas err = enumerate_tree(&have_all_entries, ibuf, &totlen,
1480 0f4feb58 2022-06-23 thomas tree_id, "/", pack, packidx, objcache, idset,
1481 0f4feb58 2022-06-23 thomas &trees, &nalloc, &ntrees);
1482 63915ee5 2022-06-23 thomas if (err)
1483 63915ee5 2022-06-23 thomas goto done;
1485 63915ee5 2022-06-23 thomas if (!have_all_entries)
1488 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1489 63915ee5 2022-06-23 thomas qid = NULL;
1491 63915ee5 2022-06-23 thomas parents = got_object_commit_get_parent_ids(commit);
1492 63915ee5 2022-06-23 thomas if (parents) {
1493 63915ee5 2022-06-23 thomas struct got_object_qid *pid;
1494 63915ee5 2022-06-23 thomas STAILQ_FOREACH(pid, parents, entry) {
1495 63915ee5 2022-06-23 thomas if (got_object_idset_contains(idset, &pid->id))
1496 63915ee5 2022-06-23 thomas continue;
1497 63915ee5 2022-06-23 thomas err = got_object_qid_alloc_partial(&qid);
1498 63915ee5 2022-06-23 thomas if (err)
1499 63915ee5 2022-06-23 thomas goto done;
1500 63915ee5 2022-06-23 thomas memcpy(&qid->id, &pid->id, sizeof(qid->id));
1501 63915ee5 2022-06-23 thomas STAILQ_INSERT_TAIL(&commit_ids, qid, entry);
1502 63915ee5 2022-06-23 thomas qid = NULL;
1506 63915ee5 2022-06-23 thomas got_object_commit_close(commit);
1507 63915ee5 2022-06-23 thomas commit = NULL;
1510 63915ee5 2022-06-23 thomas if (have_all_entries) {
1511 63915ee5 2022-06-23 thomas err = got_privsep_send_object_enumeration_done(ibuf);
1512 e71f1e62 2022-06-23 thomas if (err)
1513 e71f1e62 2022-06-23 thomas goto done;
1514 e71f1e62 2022-06-23 thomas } else {
1515 e71f1e62 2022-06-23 thomas err = got_privsep_send_object_enumeration_incomplete(ibuf);
1516 63915ee5 2022-06-23 thomas if (err)
1517 63915ee5 2022-06-23 thomas goto done;
1520 63915ee5 2022-06-23 thomas if (obj)
1521 63915ee5 2022-06-23 thomas got_object_close(obj);
1522 63915ee5 2022-06-23 thomas if (commit)
1523 63915ee5 2022-06-23 thomas got_object_commit_close(commit);
1524 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1525 63915ee5 2022-06-23 thomas got_object_id_queue_free(&commit_ids);
1526 63915ee5 2022-06-23 thomas if (idset)
1527 63915ee5 2022-06-23 thomas got_object_idset_free(idset);
1528 63915ee5 2022-06-23 thomas for (i = 0; i < ntrees; i++) {
1529 63915ee5 2022-06-23 thomas struct enumerated_tree *tree = &trees[i];
1530 63915ee5 2022-06-23 thomas free(tree->buf);
1531 63915ee5 2022-06-23 thomas free(tree->path);
1532 63915ee5 2022-06-23 thomas free(tree->entries);
1534 63915ee5 2022-06-23 thomas free(trees);
1535 63915ee5 2022-06-23 thomas return err;
1538 ec2b23c5 2022-07-01 thomas enum findtwixt_color {
1539 ec2b23c5 2022-07-01 thomas COLOR_KEEP = 0,
1540 ec2b23c5 2022-07-01 thomas COLOR_DROP,
1541 ec2b23c5 2022-07-01 thomas COLOR_SKIP,
1542 ec2b23c5 2022-07-01 thomas COLOR_MAX,
1545 63915ee5 2022-06-23 thomas static const struct got_error *
1546 ec2b23c5 2022-07-01 thomas paint_commit(struct got_object_qid *qid, intptr_t color)
1548 ec2b23c5 2022-07-01 thomas if (color < 0 || color >= COLOR_MAX)
1549 ec2b23c5 2022-07-01 thomas return got_error(GOT_ERR_RANGE);
1551 ec2b23c5 2022-07-01 thomas qid->data = (void *)color;
1552 ec2b23c5 2022-07-01 thomas return NULL;
1555 ec2b23c5 2022-07-01 thomas static const struct got_error *
1556 ec2b23c5 2022-07-01 thomas queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1557 ec2b23c5 2022-07-01 thomas intptr_t color)
1559 ec2b23c5 2022-07-01 thomas const struct got_error *err;
1560 ec2b23c5 2022-07-01 thomas struct got_object_qid *qid;
1562 ec2b23c5 2022-07-01 thomas err = got_object_qid_alloc_partial(&qid);
1563 ec2b23c5 2022-07-01 thomas if (err)
1564 ec2b23c5 2022-07-01 thomas return err;
1566 ec2b23c5 2022-07-01 thomas memcpy(&qid->id, id, sizeof(qid->id));
1567 ec2b23c5 2022-07-01 thomas STAILQ_INSERT_TAIL(ids, qid, entry);
1568 ec2b23c5 2022-07-01 thomas return paint_commit(qid, color);
1571 ec2b23c5 2022-07-01 thomas static const struct got_error *
1572 ec2b23c5 2022-07-01 thomas paint_commits(struct got_object_id_queue *ids, int *nids,
1573 ec2b23c5 2022-07-01 thomas struct got_object_idset *keep, struct got_object_idset *drop,
1574 ec2b23c5 2022-07-01 thomas struct got_object_idset *skip, struct got_pack *pack,
1575 ec2b23c5 2022-07-01 thomas struct got_packidx *packidx, struct imsgbuf *ibuf,
1576 ec2b23c5 2022-07-01 thomas struct got_object_cache *objcache)
1578 ec2b23c5 2022-07-01 thomas const struct got_error *err = NULL;
1579 ec2b23c5 2022-07-01 thomas struct got_commit_object *commit = NULL;
1580 ec2b23c5 2022-07-01 thomas struct got_object_id_queue painted;
1581 ec2b23c5 2022-07-01 thomas const struct got_object_id_queue *parents;
1582 ec2b23c5 2022-07-01 thomas struct got_object_qid *qid = NULL;
1583 ec2b23c5 2022-07-01 thomas int nqueued = *nids, nskip = 0, npainted = 0;
1585 ec2b23c5 2022-07-01 thomas STAILQ_INIT(&painted);
1587 ec2b23c5 2022-07-01 thomas while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1588 ec2b23c5 2022-07-01 thomas int idx;
1589 ec2b23c5 2022-07-01 thomas intptr_t color;
1591 ec2b23c5 2022-07-01 thomas if (sigint_received) {
1592 ec2b23c5 2022-07-01 thomas err = got_error(GOT_ERR_CANCELLED);
1593 ec2b23c5 2022-07-01 thomas goto done;
1596 ec2b23c5 2022-07-01 thomas qid = STAILQ_FIRST(ids);
1597 ec2b23c5 2022-07-01 thomas idx = got_packidx_get_object_idx(packidx, &qid->id);
1598 ec2b23c5 2022-07-01 thomas if (idx == -1) {
1599 ec2b23c5 2022-07-01 thomas qid = NULL;
1603 ec2b23c5 2022-07-01 thomas STAILQ_REMOVE_HEAD(ids, entry);
1604 ec2b23c5 2022-07-01 thomas nqueued--;
1605 ec2b23c5 2022-07-01 thomas color = (intptr_t)qid->data;
1606 ec2b23c5 2022-07-01 thomas if (color == COLOR_SKIP)
1607 ec2b23c5 2022-07-01 thomas nskip--;
1609 ec2b23c5 2022-07-01 thomas if (got_object_idset_contains(skip, &qid->id)) {
1610 ec2b23c5 2022-07-01 thomas got_object_qid_free(qid);
1611 ec2b23c5 2022-07-01 thomas qid = NULL;
1612 ec2b23c5 2022-07-01 thomas continue;
1615 ec2b23c5 2022-07-01 thomas switch (color) {
1616 ec2b23c5 2022-07-01 thomas case COLOR_KEEP:
1617 ec2b23c5 2022-07-01 thomas if (got_object_idset_contains(keep, &qid->id)) {
1618 ec2b23c5 2022-07-01 thomas got_object_qid_free(qid);
1619 ec2b23c5 2022-07-01 thomas qid = NULL;
1620 ec2b23c5 2022-07-01 thomas continue;
1622 ec2b23c5 2022-07-01 thomas if (got_object_idset_contains(drop, &qid->id)) {
1623 ec2b23c5 2022-07-01 thomas err = paint_commit(qid, COLOR_SKIP);
1624 ec2b23c5 2022-07-01 thomas if (err)
1625 ec2b23c5 2022-07-01 thomas goto done;
1627 ec2b23c5 2022-07-01 thomas err = got_object_idset_add(keep, &qid->id, NULL);
1628 ec2b23c5 2022-07-01 thomas if (err)
1629 ec2b23c5 2022-07-01 thomas goto done;
1631 ec2b23c5 2022-07-01 thomas case COLOR_DROP:
1632 ec2b23c5 2022-07-01 thomas if (got_object_idset_contains(drop, &qid->id)) {
1633 ec2b23c5 2022-07-01 thomas got_object_qid_free(qid);
1634 ec2b23c5 2022-07-01 thomas qid = NULL;
1635 ec2b23c5 2022-07-01 thomas continue;
1637 ec2b23c5 2022-07-01 thomas if (got_object_idset_contains(keep, &qid->id)) {
1638 ec2b23c5 2022-07-01 thomas err = paint_commit(qid, COLOR_SKIP);
1639 ec2b23c5 2022-07-01 thomas if (err)
1640 ec2b23c5 2022-07-01 thomas goto done;
1642 ec2b23c5 2022-07-01 thomas err = got_object_idset_add(drop, &qid->id, NULL);
1643 ec2b23c5 2022-07-01 thomas if (err)
1644 ec2b23c5 2022-07-01 thomas goto done;
1646 ec2b23c5 2022-07-01 thomas case COLOR_SKIP:
1647 ec2b23c5 2022-07-01 thomas if (!got_object_idset_contains(skip, &qid->id)) {
1648 ec2b23c5 2022-07-01 thomas err = got_object_idset_add(skip, &qid->id,
1650 ec2b23c5 2022-07-01 thomas if (err)
1651 ec2b23c5 2022-07-01 thomas goto done;
1654 ec2b23c5 2022-07-01 thomas default:
1655 ec2b23c5 2022-07-01 thomas /* should not happen */
1656 ec2b23c5 2022-07-01 thomas err = got_error_fmt(GOT_ERR_NOT_IMPL,
1657 60b94e7d 2022-07-19 thomas "%s invalid commit color %"PRIdPTR, __func__,
1659 ec2b23c5 2022-07-01 thomas goto done;
1662 ec2b23c5 2022-07-01 thomas err = open_commit(&commit, pack, packidx, idx, &qid->id,
1663 ec2b23c5 2022-07-01 thomas objcache);
1664 ec2b23c5 2022-07-01 thomas if (err)
1665 ec2b23c5 2022-07-01 thomas goto done;
1667 ec2b23c5 2022-07-01 thomas parents = got_object_commit_get_parent_ids(commit);
1668 ec2b23c5 2022-07-01 thomas if (parents) {
1669 ec2b23c5 2022-07-01 thomas struct got_object_qid *pid;
1670 ec2b23c5 2022-07-01 thomas color = (intptr_t)qid->data;
1671 ec2b23c5 2022-07-01 thomas STAILQ_FOREACH(pid, parents, entry) {
1672 ec2b23c5 2022-07-01 thomas err = queue_commit_id(ids, &pid->id, color);
1673 ec2b23c5 2022-07-01 thomas if (err)
1674 ec2b23c5 2022-07-01 thomas goto done;
1675 ec2b23c5 2022-07-01 thomas nqueued++;
1676 ec2b23c5 2022-07-01 thomas if (color == COLOR_SKIP)
1677 ec2b23c5 2022-07-01 thomas nskip++;
1681 ec2b23c5 2022-07-01 thomas got_object_commit_close(commit);
1682 ec2b23c5 2022-07-01 thomas commit = NULL;
1684 ec2b23c5 2022-07-01 thomas STAILQ_INSERT_TAIL(&painted, qid, entry);
1685 ec2b23c5 2022-07-01 thomas qid = NULL;
1686 ec2b23c5 2022-07-01 thomas npainted++;
1688 ec2b23c5 2022-07-01 thomas err = got_privsep_send_painted_commits(ibuf, &painted,
1689 ec2b23c5 2022-07-01 thomas &npainted, 1, 0);
1690 ec2b23c5 2022-07-01 thomas if (err)
1691 ec2b23c5 2022-07-01 thomas goto done;
1694 ec2b23c5 2022-07-01 thomas err = got_privsep_send_painted_commits(ibuf, &painted, &npainted, 1, 1);
1695 ec2b23c5 2022-07-01 thomas if (err)
1696 ec2b23c5 2022-07-01 thomas goto done;
1698 ec2b23c5 2022-07-01 thomas *nids = nqueued;
1700 ec2b23c5 2022-07-01 thomas if (commit)
1701 ec2b23c5 2022-07-01 thomas got_object_commit_close(commit);
1702 ec2b23c5 2022-07-01 thomas got_object_qid_free(qid);
1703 ec2b23c5 2022-07-01 thomas return err;
1706 ec2b23c5 2022-07-01 thomas static void
1707 ec2b23c5 2022-07-01 thomas commit_painting_free(struct got_object_idset **keep,
1708 ec2b23c5 2022-07-01 thomas struct got_object_idset **drop,
1709 ec2b23c5 2022-07-01 thomas struct got_object_idset **skip)
1711 ec2b23c5 2022-07-01 thomas if (*keep) {
1712 ec2b23c5 2022-07-01 thomas got_object_idset_free(*keep);
1713 ec2b23c5 2022-07-01 thomas *keep = NULL;
1715 ec2b23c5 2022-07-01 thomas if (*drop) {
1716 ec2b23c5 2022-07-01 thomas got_object_idset_free(*drop);
1717 ec2b23c5 2022-07-01 thomas *drop = NULL;
1719 ec2b23c5 2022-07-01 thomas if (*skip) {
1720 b6b86fd1 2022-08-30 thomas got_object_idset_free(*skip);
1721 ec2b23c5 2022-07-01 thomas *skip = NULL;
1725 ec2b23c5 2022-07-01 thomas static const struct got_error *
1726 ec2b23c5 2022-07-01 thomas commit_painting_init(struct imsgbuf *ibuf, struct got_object_idset **keep,
1727 ec2b23c5 2022-07-01 thomas struct got_object_idset **drop, struct got_object_idset **skip)
1729 ec2b23c5 2022-07-01 thomas const struct got_error *err = NULL;
1731 ec2b23c5 2022-07-01 thomas *keep = got_object_idset_alloc();
1732 ec2b23c5 2022-07-01 thomas if (*keep == NULL) {
1733 ec2b23c5 2022-07-01 thomas err = got_error_from_errno("got_object_idset_alloc");
1734 ec2b23c5 2022-07-01 thomas goto done;
1736 ec2b23c5 2022-07-01 thomas *drop = got_object_idset_alloc();
1737 ec2b23c5 2022-07-01 thomas if (*drop == NULL) {
1738 ec2b23c5 2022-07-01 thomas err = got_error_from_errno("got_object_idset_alloc");
1739 ec2b23c5 2022-07-01 thomas goto done;
1741 ec2b23c5 2022-07-01 thomas *skip = got_object_idset_alloc();
1742 ec2b23c5 2022-07-01 thomas if (*skip == NULL) {
1743 ec2b23c5 2022-07-01 thomas err = got_error_from_errno("got_object_idset_alloc");
1744 ec2b23c5 2022-07-01 thomas goto done;
1747 ec2b23c5 2022-07-01 thomas err = recv_object_ids(*keep, ibuf);
1748 ec2b23c5 2022-07-01 thomas if (err)
1749 ec2b23c5 2022-07-01 thomas goto done;
1750 ec2b23c5 2022-07-01 thomas err = recv_object_ids(*drop, ibuf);
1751 ec2b23c5 2022-07-01 thomas if (err)
1752 ec2b23c5 2022-07-01 thomas goto done;
1753 ec2b23c5 2022-07-01 thomas err = recv_object_ids(*skip, ibuf);
1754 ec2b23c5 2022-07-01 thomas if (err)
1755 ec2b23c5 2022-07-01 thomas goto done;
1758 ec2b23c5 2022-07-01 thomas if (err)
1759 ec2b23c5 2022-07-01 thomas commit_painting_free(keep, drop, skip);
1761 ec2b23c5 2022-07-01 thomas return err;
1764 ec2b23c5 2022-07-01 thomas static const struct got_error *
1765 ec2b23c5 2022-07-01 thomas commit_painting_request(struct imsg *imsg, struct imsgbuf *ibuf,
1766 ec2b23c5 2022-07-01 thomas struct got_pack *pack, struct got_packidx *packidx,
1767 ec2b23c5 2022-07-01 thomas struct got_object_cache *objcache, struct got_object_idset *keep,
1768 ec2b23c5 2022-07-01 thomas struct got_object_idset *drop, struct got_object_idset *skip)
1770 ec2b23c5 2022-07-01 thomas const struct got_error *err = NULL;
1771 ec2b23c5 2022-07-01 thomas struct got_imsg_commit_painting_request ireq;
1772 ec2b23c5 2022-07-01 thomas struct got_object_id id;
1773 ec2b23c5 2022-07-01 thomas size_t datalen;
1774 ec2b23c5 2022-07-01 thomas struct got_object_id_queue ids;
1775 ec2b23c5 2022-07-01 thomas int nids = 0;
1777 ec2b23c5 2022-07-01 thomas STAILQ_INIT(&ids);
1779 ec2b23c5 2022-07-01 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1780 ec2b23c5 2022-07-01 thomas if (datalen != sizeof(ireq))
1781 ec2b23c5 2022-07-01 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1782 ec2b23c5 2022-07-01 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
1783 ec2b23c5 2022-07-01 thomas memcpy(id.sha1, ireq.id, SHA1_DIGEST_LENGTH);
1785 ec2b23c5 2022-07-01 thomas err = queue_commit_id(&ids, &id, ireq.color);
1786 ec2b23c5 2022-07-01 thomas if (err)
1787 ec2b23c5 2022-07-01 thomas return err;
1788 ec2b23c5 2022-07-01 thomas nids = 1;
1790 ec2b23c5 2022-07-01 thomas err = paint_commits(&ids, &nids, keep, drop, skip,
1791 ec2b23c5 2022-07-01 thomas pack, packidx, ibuf, objcache);
1792 ec2b23c5 2022-07-01 thomas if (err)
1793 ec2b23c5 2022-07-01 thomas goto done;
1795 ec2b23c5 2022-07-01 thomas err = got_privsep_send_painted_commits(ibuf, &ids, &nids, 0, 1);
1796 ec2b23c5 2022-07-01 thomas if (err)
1797 ec2b23c5 2022-07-01 thomas goto done;
1799 ec2b23c5 2022-07-01 thomas err = got_privsep_send_painting_commits_done(ibuf);
1801 ec2b23c5 2022-07-01 thomas got_object_id_queue_free(&ids);
1802 ec2b23c5 2022-07-01 thomas return err;
1805 ec2b23c5 2022-07-01 thomas static const struct got_error *
1806 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1808 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1809 876c234b 2018-09-10 stsp struct imsg imsg;
1810 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1811 876c234b 2018-09-10 stsp size_t datalen;
1812 876c234b 2018-09-10 stsp struct got_pack *pack;
1814 876c234b 2018-09-10 stsp *packp = NULL;
1816 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1818 876c234b 2018-09-10 stsp return err;
1820 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
1821 876c234b 2018-09-10 stsp if (pack == NULL) {
1822 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1823 876c234b 2018-09-10 stsp goto done;
1826 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
1827 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1828 876c234b 2018-09-10 stsp goto done;
1831 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
1832 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1833 876c234b 2018-09-10 stsp goto done;
1836 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1837 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
1838 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1839 876c234b 2018-09-10 stsp goto done;
1841 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
1843 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
1844 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
1845 876c234b 2018-09-10 stsp if (pack->fd == -1) {
1846 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
1847 876c234b 2018-09-10 stsp goto done;
1849 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1850 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1851 56bef47a 2018-09-15 stsp goto done;
1853 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
1854 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
1855 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1856 ab2f42e7 2019-11-10 stsp goto done;
1859 a5061f77 2022-06-13 thomas err = got_delta_cache_alloc(&pack->delta_cache);
1860 a5061f77 2022-06-13 thomas if (err)
1861 876c234b 2018-09-10 stsp goto done;
1863 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
1864 aa75acde 2022-10-25 thomas if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1865 aa75acde 2022-10-25 thomas pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1866 aa75acde 2022-10-25 thomas pack->fd, 0);
1867 aa75acde 2022-10-25 thomas if (pack->map == MAP_FAILED)
1868 aa75acde 2022-10-25 thomas pack->map = NULL; /* fall back to read(2) */
1872 876c234b 2018-09-10 stsp if (err) {
1873 876c234b 2018-09-10 stsp if (imsg.fd != -1)
1874 876c234b 2018-09-10 stsp close(imsg.fd);
1875 876c234b 2018-09-10 stsp free(pack);
1877 876c234b 2018-09-10 stsp *packp = pack;
1878 876c234b 2018-09-10 stsp imsg_free(&imsg);
1879 876c234b 2018-09-10 stsp return err;
1883 876c234b 2018-09-10 stsp main(int argc, char *argv[])
1885 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1886 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
1887 876c234b 2018-09-10 stsp struct imsg imsg;
1888 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
1889 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
1890 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
1891 f9c2e8e5 2022-02-13 thomas FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1892 ec2b23c5 2022-07-01 thomas struct got_object_idset *keep = NULL, *drop = NULL, *skip = NULL;
1893 c77e00b3 2022-10-18 thomas struct got_parsed_tree_entry *entries = NULL;
1894 c77e00b3 2022-10-18 thomas size_t nentries = 0, nentries_alloc = 0;
1896 876c234b 2018-09-10 stsp //static int attached;
1897 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
1899 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
1901 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1903 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1904 c59b3346 2018-09-11 stsp if (err) {
1905 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_cache_init");
1906 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
1910 2ff12563 2018-09-15 stsp #ifndef PROFILE
1911 876c234b 2018-09-10 stsp /* revoke access to most system calls */
1912 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
1913 638f9024 2019-05-13 stsp err = got_error_from_errno("pledge");
1914 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1918 97799ccd 2022-02-06 thomas /* revoke fs access */
1919 97799ccd 2022-02-06 thomas if (landlock_no_fs() == -1) {
1920 97799ccd 2022-02-06 thomas err = got_error_from_errno("landlock_no_fs");
1921 97799ccd 2022-02-06 thomas got_privsep_send_error(&ibuf, err);
1922 97799ccd 2022-02-06 thomas return 1;
1924 5d120ea8 2022-06-23 op if (cap_enter() == -1) {
1925 5d120ea8 2022-06-23 op err = got_error_from_errno("cap_enter");
1926 5d120ea8 2022-06-23 op got_privsep_send_error(&ibuf, err);
1931 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
1932 876c234b 2018-09-10 stsp if (err) {
1933 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1937 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
1938 876c234b 2018-09-10 stsp if (err) {
1939 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1944 876c234b 2018-09-10 stsp imsg.fd = -1;
1946 99437157 2018-11-11 stsp if (sigint_received) {
1947 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
1951 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1952 876c234b 2018-09-10 stsp if (err) {
1953 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1954 876c234b 2018-09-10 stsp err = NULL;
1958 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1961 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
1962 bc1f382f 2022-01-05 thomas case GOT_IMSG_TMPFD:
1963 f9c2e8e5 2022-02-13 thomas if (basefile == NULL) {
1964 f9c2e8e5 2022-02-13 thomas err = receive_tempfile(&basefile, "w+",
1965 f9c2e8e5 2022-02-13 thomas &imsg, &ibuf);
1966 f9c2e8e5 2022-02-13 thomas } else if (accumfile == NULL) {
1967 f9c2e8e5 2022-02-13 thomas err = receive_tempfile(&accumfile, "w+",
1968 f9c2e8e5 2022-02-13 thomas &imsg, &ibuf);
1970 f9c2e8e5 2022-02-13 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1972 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
1973 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
1974 c59b3346 2018-09-11 stsp &objcache);
1976 59d1e4a0 2021-03-10 stsp case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1977 bc1f382f 2022-01-05 thomas if (basefile == NULL || accumfile == NULL) {
1978 bc1f382f 2022-01-05 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1981 59d1e4a0 2021-03-10 stsp err = raw_object_request(&imsg, &ibuf, pack, packidx,
1982 bc1f382f 2022-01-05 thomas &objcache, basefile, accumfile);
1984 f9c2e8e5 2022-02-13 thomas case GOT_IMSG_RAW_DELTA_OUTFD:
1985 f9c2e8e5 2022-02-13 thomas if (delta_outfile != NULL) {
1986 f9c2e8e5 2022-02-13 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1989 f9c2e8e5 2022-02-13 thomas err = receive_tempfile(&delta_outfile, "w",
1990 f9c2e8e5 2022-02-13 thomas &imsg, &ibuf);
1992 f9c2e8e5 2022-02-13 thomas case GOT_IMSG_RAW_DELTA_REQUEST:
1993 f9c2e8e5 2022-02-13 thomas if (delta_outfile == NULL) {
1994 f9c2e8e5 2022-02-13 thomas err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1997 f9c2e8e5 2022-02-13 thomas err = raw_delta_request(&imsg, &ibuf, delta_outfile,
1998 f9c2e8e5 2022-02-13 thomas pack, packidx);
2000 7d0d4920 2022-05-12 thomas case GOT_IMSG_DELTA_REUSE_REQUEST:
2001 c44c7d6e 2022-12-04 thomas err = delta_reuse_request(&imsg, &ibuf, pack, packidx);
2003 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
2004 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
2005 7762fe12 2018-11-05 stsp &objcache);
2007 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
2008 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
2009 c77e00b3 2022-10-18 thomas &objcache, &entries, &nentries, &nentries_alloc);
2011 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
2012 bc1f382f 2022-01-05 thomas if (basefile == NULL || accumfile == NULL) {
2013 bc1f382f 2022-01-05 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
2016 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
2017 bc1f382f 2022-01-05 thomas &objcache, basefile, accumfile);
2019 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG_REQUEST:
2020 f4a881ce 2018-11-17 stsp err = tag_request(&imsg, &ibuf, pack, packidx,
2021 62d463ca 2020-10-20 naddy &objcache);
2023 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
2024 ca6e02ac 2020-01-07 stsp err = commit_traversal_request(&imsg, &ibuf, pack,
2025 ca6e02ac 2020-01-07 stsp packidx, &objcache);
2027 63915ee5 2022-06-23 thomas case GOT_IMSG_OBJECT_ENUMERATION_REQUEST:
2028 63915ee5 2022-06-23 thomas err = enumeration_request(&imsg, &ibuf, pack,
2029 63915ee5 2022-06-23 thomas packidx, &objcache);
2031 ec2b23c5 2022-07-01 thomas case GOT_IMSG_COMMIT_PAINTING_INIT:
2032 ec2b23c5 2022-07-01 thomas commit_painting_free(&keep, &drop, &skip);
2033 ec2b23c5 2022-07-01 thomas err = commit_painting_init(&ibuf, &keep, &drop, &skip);
2035 ec2b23c5 2022-07-01 thomas case GOT_IMSG_COMMIT_PAINTING_REQUEST:
2036 ec2b23c5 2022-07-01 thomas if (keep == NULL || drop == NULL || skip == NULL) {
2037 ec2b23c5 2022-07-01 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
2040 ec2b23c5 2022-07-01 thomas err = commit_painting_request(&imsg, &ibuf, pack,
2041 ec2b23c5 2022-07-01 thomas packidx, &objcache, keep, drop, skip);
2043 ec2b23c5 2022-07-01 thomas case GOT_IMSG_COMMIT_PAINTING_DONE:
2044 ec2b23c5 2022-07-01 thomas commit_painting_free(&keep, &drop, &skip);
2047 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2051 08578a35 2021-01-22 stsp if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
2052 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
2053 876c234b 2018-09-10 stsp imsg_free(&imsg);
2058 c77e00b3 2022-10-18 thomas free(entries);
2059 ec2b23c5 2022-07-01 thomas commit_painting_free(&keep, &drop, &skip);
2060 c59b3346 2018-09-11 stsp if (packidx)
2061 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
2063 c59b3346 2018-09-11 stsp got_pack_close(pack);
2064 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
2065 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
2066 bc1f382f 2022-01-05 thomas if (basefile && fclose(basefile) == EOF && err == NULL)
2067 bc1f382f 2022-01-05 thomas err = got_error_from_errno("fclose");
2068 bc1f382f 2022-01-05 thomas if (accumfile && fclose(accumfile) == EOF && err == NULL)
2069 bc1f382f 2022-01-05 thomas err = got_error_from_errno("fclose");
2070 f9c2e8e5 2022-02-13 thomas if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
2071 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fclose");
2072 99437157 2018-11-11 stsp if (err) {
2073 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
2074 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
2075 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
2078 08578a35 2021-01-22 stsp if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
2079 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
2080 876c234b 2018-09-10 stsp return err ? 1 : 0;