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 876c234b 2018-09-10 stsp #include <sys/uio.h>
20 876c234b 2018-09-10 stsp #include <sys/time.h>
21 876c234b 2018-09-10 stsp #include <sys/mman.h>
23 876c234b 2018-09-10 stsp #include <limits.h>
24 99437157 2018-11-11 stsp #include <signal.h>
25 876c234b 2018-09-10 stsp #include <stdint.h>
26 876c234b 2018-09-10 stsp #include <stdio.h>
27 876c234b 2018-09-10 stsp #include <stdlib.h>
28 876c234b 2018-09-10 stsp #include <string.h>
29 81a12da5 2020-09-09 naddy #include <unistd.h>
30 876c234b 2018-09-10 stsp #include <zlib.h>
32 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
34 876c234b 2018-09-10 stsp #include "got_error.h"
35 876c234b 2018-09-10 stsp #include "got_object.h"
36 3022d272 2019-11-14 stsp #include "got_path.h"
38 876c234b 2018-09-10 stsp #include "got_lib_delta.h"
39 ab2f42e7 2019-11-10 stsp #include "got_lib_delta_cache.h"
40 876c234b 2018-09-10 stsp #include "got_lib_object.h"
41 c59b3346 2018-09-11 stsp #include "got_lib_object_cache.h"
42 876c234b 2018-09-10 stsp #include "got_lib_object_parse.h"
43 7d0d4920 2022-05-12 thomas #include "got_lib_object_idset.h"
44 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
45 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
47 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
50 99437157 2018-11-11 stsp catch_sigint(int signo)
52 99437157 2018-11-11 stsp sigint_received = 1;
55 876c234b 2018-09-10 stsp static const struct got_error *
56 704b89c4 2019-05-23 stsp open_object(struct got_object **obj, struct got_pack *pack,
57 704b89c4 2019-05-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id,
58 704b89c4 2019-05-23 stsp struct got_object_cache *objcache)
60 704b89c4 2019-05-23 stsp const struct got_error *err;
62 704b89c4 2019-05-23 stsp err = got_packfile_open_object(obj, pack, packidx, idx, id);
65 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
67 704b89c4 2019-05-23 stsp err = got_object_cache_add(objcache, id, *obj);
69 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
70 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
74 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
75 704b89c4 2019-05-23 stsp return NULL;
78 704b89c4 2019-05-23 stsp static const struct got_error *
79 876c234b 2018-09-10 stsp object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
80 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
82 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
83 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
84 876c234b 2018-09-10 stsp struct got_object *obj;
85 106807b4 2018-09-15 stsp struct got_object_id id;
86 876c234b 2018-09-10 stsp size_t datalen;
88 876c234b 2018-09-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
89 876c234b 2018-09-10 stsp if (datalen != sizeof(iobj))
90 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
91 876c234b 2018-09-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
92 106807b4 2018-09-15 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
94 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
96 704b89c4 2019-05-23 stsp obj->refcnt++;
98 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
104 876c234b 2018-09-10 stsp err = got_privsep_send_obj(ibuf, obj);
106 876c234b 2018-09-10 stsp got_object_close(obj);
107 876c234b 2018-09-10 stsp return err;
110 8f1c06eb 2021-09-25 thomas.ad static const struct got_error *
111 ca6e02ac 2020-01-07 stsp open_commit(struct got_commit_object **commit, struct got_pack *pack,
112 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
113 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
115 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
116 cb5e38fd 2019-05-23 stsp struct got_object *obj = NULL;
117 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
118 cfd633c2 2018-09-10 stsp size_t len;
120 ca6e02ac 2020-01-07 stsp *commit = NULL;
122 ca6e02ac 2020-01-07 stsp obj = got_object_cache_get(objcache, id);
124 704b89c4 2019-05-23 stsp obj->refcnt++;
126 ca6e02ac 2020-01-07 stsp err = open_object(&obj, pack, packidx, obj_idx, id,
129 704b89c4 2019-05-23 stsp return err;
132 cfd633c2 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
136 cfd633c2 2018-09-10 stsp obj->size = len;
138 ca6e02ac 2020-01-07 stsp err = got_object_parse_commit(commit, buf, len);
140 ca6e02ac 2020-01-07 stsp got_object_close(obj);
142 ca6e02ac 2020-01-07 stsp return err;
145 ca6e02ac 2020-01-07 stsp static const struct got_error *
146 ca6e02ac 2020-01-07 stsp commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
147 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
149 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
150 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
151 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL;
152 ca6e02ac 2020-01-07 stsp struct got_object_id id;
153 ca6e02ac 2020-01-07 stsp size_t datalen;
155 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
156 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(iobj))
157 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
158 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
159 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
161 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
165 cfd633c2 2018-09-10 stsp err = got_privsep_send_commit(ibuf, commit);
167 cb5e38fd 2019-05-23 stsp if (commit)
168 cb5e38fd 2019-05-23 stsp got_object_commit_close(commit);
170 7762fe12 2018-11-05 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
171 7762fe12 2018-11-05 stsp err = NULL;
173 7762fe12 2018-11-05 stsp got_privsep_send_error(ibuf, err);
176 7762fe12 2018-11-05 stsp return err;
179 8f1c06eb 2021-09-25 thomas.ad static const struct got_error *
180 78e7b7b8 2022-05-19 thomas open_tree(uint8_t **buf, struct got_parsed_tree_entry **entries, int *nentries,
181 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
182 ca6e02ac 2020-01-07 stsp struct got_object_id *id, struct got_object_cache *objcache)
184 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
185 ca6e02ac 2020-01-07 stsp struct got_object *obj = NULL;
186 ca6e02ac 2020-01-07 stsp size_t len;
188 ca6e02ac 2020-01-07 stsp *buf = NULL;
189 ca6e02ac 2020-01-07 stsp *nentries = 0;
191 ca6e02ac 2020-01-07 stsp obj = got_object_cache_get(objcache, id);
193 ca6e02ac 2020-01-07 stsp obj->refcnt++;
195 ca6e02ac 2020-01-07 stsp err = open_object(&obj, pack, packidx, obj_idx, id,
198 ca6e02ac 2020-01-07 stsp return err;
201 ca6e02ac 2020-01-07 stsp err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
205 ca6e02ac 2020-01-07 stsp obj->size = len;
207 ca6e02ac 2020-01-07 stsp err = got_object_parse_tree(entries, nentries, *buf, len);
209 ca6e02ac 2020-01-07 stsp got_object_close(obj);
211 ca6e02ac 2020-01-07 stsp free(*buf);
212 ca6e02ac 2020-01-07 stsp *buf = NULL;
214 ca6e02ac 2020-01-07 stsp return err;
217 7762fe12 2018-11-05 stsp static const struct got_error *
218 876c234b 2018-09-10 stsp tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
219 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
221 e7885405 2018-09-10 stsp const struct got_error *err = NULL;
222 13c729f7 2018-12-24 stsp struct got_imsg_packed_object iobj;
223 78e7b7b8 2022-05-19 thomas struct got_parsed_tree_entry *entries = NULL;
224 3022d272 2019-11-14 stsp int nentries = 0;
225 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
226 13c729f7 2018-12-24 stsp struct got_object_id id;
227 13c729f7 2018-12-24 stsp size_t datalen;
229 13c729f7 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
230 13c729f7 2018-12-24 stsp if (datalen != sizeof(iobj))
231 13c729f7 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
232 13c729f7 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
233 13c729f7 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
235 ca6e02ac 2020-01-07 stsp err = open_tree(&buf, &entries, &nentries, pack, packidx, iobj.idx,
236 62d463ca 2020-10-20 naddy &id, objcache);
238 ca6e02ac 2020-01-07 stsp return err;
240 78e7b7b8 2022-05-19 thomas err = got_privsep_send_tree(ibuf, entries, nentries);
241 78e7b7b8 2022-05-19 thomas free(entries);
244 e7885405 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
245 e7885405 2018-09-10 stsp err = NULL;
247 e7885405 2018-09-10 stsp got_privsep_send_error(ibuf, err);
250 e7885405 2018-09-10 stsp return err;
253 876c234b 2018-09-10 stsp static const struct got_error *
254 01bb5a15 2021-09-25 thomas.ad receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
256 3840f4c9 2018-09-12 stsp const struct got_error *err;
257 3840f4c9 2018-09-12 stsp struct imsg imsg;
258 55da3778 2018-09-10 stsp size_t datalen;
260 3840f4c9 2018-09-12 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
262 55da3778 2018-09-10 stsp return err;
264 3840f4c9 2018-09-12 stsp if (imsg.hdr.type != imsg_code) {
265 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
269 3840f4c9 2018-09-12 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
270 55da3778 2018-09-10 stsp if (datalen != 0) {
271 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
274 3840f4c9 2018-09-12 stsp if (imsg.fd == -1) {
275 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
279 3840f4c9 2018-09-12 stsp *f = fdopen(imsg.fd, "w+");
280 3840f4c9 2018-09-12 stsp if (*f == NULL) {
281 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
282 3a6ce05a 2019-02-11 stsp close(imsg.fd);
286 3840f4c9 2018-09-12 stsp imsg_free(&imsg);
287 3840f4c9 2018-09-12 stsp return err;
290 bc1f382f 2022-01-05 thomas static const struct got_error *
291 f9c2e8e5 2022-02-13 thomas receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
292 bc1f382f 2022-01-05 thomas struct imsgbuf *ibuf)
294 bc1f382f 2022-01-05 thomas size_t datalen;
296 bc1f382f 2022-01-05 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
297 bc1f382f 2022-01-05 thomas if (datalen != 0)
298 bc1f382f 2022-01-05 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
300 bc1f382f 2022-01-05 thomas if (imsg->fd == -1)
301 bc1f382f 2022-01-05 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
303 f9c2e8e5 2022-02-13 thomas *f = fdopen(imsg->fd, mode);
304 bc1f382f 2022-01-05 thomas if (*f == NULL)
305 bc1f382f 2022-01-05 thomas return got_error_from_errno("fdopen");
306 bc1f382f 2022-01-05 thomas imsg->fd = -1;
308 bc1f382f 2022-01-05 thomas return NULL;
311 3840f4c9 2018-09-12 stsp static const struct got_error *
312 3840f4c9 2018-09-12 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
313 bc1f382f 2022-01-05 thomas struct got_packidx *packidx, struct got_object_cache *objcache,
314 bc1f382f 2022-01-05 thomas FILE *basefile, FILE *accumfile)
316 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
317 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj;
318 3840f4c9 2018-09-12 stsp struct got_object *obj = NULL;
319 bc1f382f 2022-01-05 thomas FILE *outfile = NULL;
320 ebc55e2d 2018-12-24 stsp struct got_object_id id;
321 ebc55e2d 2018-12-24 stsp size_t datalen;
322 ac544f8c 2019-01-13 stsp uint64_t blob_size;
323 ac544f8c 2019-01-13 stsp uint8_t *buf = NULL;
325 ebc55e2d 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
326 ebc55e2d 2018-12-24 stsp if (datalen != sizeof(iobj))
327 ebc55e2d 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
328 ebc55e2d 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
329 ebc55e2d 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
331 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
333 704b89c4 2019-05-23 stsp obj->refcnt++;
335 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
338 704b89c4 2019-05-23 stsp return err;
341 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
345 ac544f8c 2019-01-13 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
346 42c69117 2019-11-10 stsp err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
350 ac544f8c 2019-01-13 stsp blob_size = obj->size;
352 ac544f8c 2019-01-13 stsp if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
353 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
354 ac544f8c 2019-01-13 stsp obj, pack);
356 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
357 ac544f8c 2019-01-13 stsp accumfile);
361 ac544f8c 2019-01-13 stsp err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
364 56b63ca4 2021-01-22 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
365 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
366 cb5e38fd 2019-05-23 stsp got_object_close(obj);
367 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
368 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
370 55da3778 2018-09-10 stsp return err;
373 876c234b 2018-09-10 stsp static const struct got_error *
374 f4a881ce 2018-11-17 stsp tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
375 f4a881ce 2018-11-17 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
377 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
378 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj;
379 f4a881ce 2018-11-17 stsp struct got_object *obj = NULL;
380 f4a881ce 2018-11-17 stsp struct got_tag_object *tag = NULL;
381 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
382 f4a881ce 2018-11-17 stsp size_t len;
383 268f7291 2018-12-24 stsp struct got_object_id id;
384 268f7291 2018-12-24 stsp size_t datalen;
386 268f7291 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
387 268f7291 2018-12-24 stsp if (datalen != sizeof(iobj))
388 268f7291 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
389 268f7291 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
390 268f7291 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
392 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
394 704b89c4 2019-05-23 stsp obj->refcnt++;
396 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
399 704b89c4 2019-05-23 stsp return err;
402 f4a881ce 2018-11-17 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
406 f4a881ce 2018-11-17 stsp obj->size = len;
407 f4a881ce 2018-11-17 stsp err = got_object_parse_tag(&tag, buf, len);
411 f4a881ce 2018-11-17 stsp err = got_privsep_send_tag(ibuf, tag);
414 cb5e38fd 2019-05-23 stsp got_object_close(obj);
416 cb5e38fd 2019-05-23 stsp got_object_tag_close(tag);
418 ca6e02ac 2020-01-07 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
419 ca6e02ac 2020-01-07 stsp err = NULL;
421 ca6e02ac 2020-01-07 stsp got_privsep_send_error(ibuf, err);
424 ca6e02ac 2020-01-07 stsp return err;
427 ca6e02ac 2020-01-07 stsp static struct got_parsed_tree_entry *
428 78e7b7b8 2022-05-19 thomas find_entry_by_name(struct got_parsed_tree_entry *entries, int nentries,
429 ca6e02ac 2020-01-07 stsp const char *name, size_t len)
431 78e7b7b8 2022-05-19 thomas struct got_parsed_tree_entry *pte;
432 78e7b7b8 2022-05-19 thomas int cmp, i;
434 ca6e02ac 2020-01-07 stsp /* Note that tree entries are sorted in strncmp() order. */
435 78e7b7b8 2022-05-19 thomas for (i = 0; i < nentries; i++) {
436 78e7b7b8 2022-05-19 thomas pte = &entries[i];
437 78e7b7b8 2022-05-19 thomas cmp = strncmp(pte->name, name, len);
438 ca6e02ac 2020-01-07 stsp if (cmp < 0)
440 ca6e02ac 2020-01-07 stsp if (cmp > 0)
442 78e7b7b8 2022-05-19 thomas if (pte->name[len] == '\0')
443 78e7b7b8 2022-05-19 thomas return pte;
445 ca6e02ac 2020-01-07 stsp return NULL;
448 8f1c06eb 2021-09-25 thomas.ad static const struct got_error *
449 ca6e02ac 2020-01-07 stsp tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
450 78e7b7b8 2022-05-19 thomas struct got_parsed_tree_entry **entries1, int *nentries1,
451 78e7b7b8 2022-05-19 thomas struct got_parsed_tree_entry **entries2, int *nentries2,
452 ca6e02ac 2020-01-07 stsp const char *path, struct got_pack *pack, struct got_packidx *packidx,
453 ca6e02ac 2020-01-07 stsp struct imsgbuf *ibuf, struct got_object_cache *objcache)
455 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
456 ca6e02ac 2020-01-07 stsp struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
457 ca6e02ac 2020-01-07 stsp const char *seg, *s;
458 ca6e02ac 2020-01-07 stsp size_t seglen;
460 ca6e02ac 2020-01-07 stsp *changed = 0;
462 ca6e02ac 2020-01-07 stsp /* We not do support comparing the root path. */
463 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
464 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
467 61a7d79f 2020-02-29 stsp while (*s == '/')
470 ca6e02ac 2020-01-07 stsp seglen = 0;
471 ca6e02ac 2020-01-07 stsp while (*s) {
472 ca6e02ac 2020-01-07 stsp if (*s != '/') {
479 78e7b7b8 2022-05-19 thomas pte1 = find_entry_by_name(*entries1, *nentries1, seg, seglen);
480 ca6e02ac 2020-01-07 stsp if (pte1 == NULL) {
481 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_NO_OBJ);
485 78e7b7b8 2022-05-19 thomas pte2 = find_entry_by_name(*entries2, *nentries2, seg, seglen);
486 ca6e02ac 2020-01-07 stsp if (pte2 == NULL) {
487 ca6e02ac 2020-01-07 stsp *changed = 1;
491 ca6e02ac 2020-01-07 stsp if (pte1->mode != pte2->mode) {
492 ca6e02ac 2020-01-07 stsp *changed = 1;
496 ca6e02ac 2020-01-07 stsp if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
497 ca6e02ac 2020-01-07 stsp *changed = 0;
501 ca6e02ac 2020-01-07 stsp if (*s == '\0') { /* final path element */
502 ca6e02ac 2020-01-07 stsp *changed = 1;
506 ca6e02ac 2020-01-07 stsp seg = s + 1;
508 ca6e02ac 2020-01-07 stsp seglen = 0;
510 ca6e02ac 2020-01-07 stsp struct got_object_id id1, id2;
513 ded8fbb8 2020-04-19 stsp memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
514 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id1);
515 ca6e02ac 2020-01-07 stsp if (idx == -1) {
516 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id1);
519 78e7b7b8 2022-05-19 thomas free(*entries1);
520 ca6e02ac 2020-01-07 stsp *nentries1 = 0;
521 ca6e02ac 2020-01-07 stsp free(*buf1);
522 ca6e02ac 2020-01-07 stsp *buf1 = NULL;
523 ca6e02ac 2020-01-07 stsp err = open_tree(buf1, entries1, nentries1, pack,
524 ca6e02ac 2020-01-07 stsp packidx, idx, &id1, objcache);
525 ca6e02ac 2020-01-07 stsp pte1 = NULL;
529 ded8fbb8 2020-04-19 stsp memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
530 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id2);
531 ca6e02ac 2020-01-07 stsp if (idx == -1) {
532 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id2);
535 78e7b7b8 2022-05-19 thomas free(*entries2);
536 ca6e02ac 2020-01-07 stsp *nentries2 = 0;
537 ca6e02ac 2020-01-07 stsp free(*buf2);
538 ca6e02ac 2020-01-07 stsp *buf2 = NULL;
539 ca6e02ac 2020-01-07 stsp err = open_tree(buf2, entries2, nentries2, pack,
540 ca6e02ac 2020-01-07 stsp packidx, idx, &id2, objcache);
541 ca6e02ac 2020-01-07 stsp pte2 = NULL;
547 ca6e02ac 2020-01-07 stsp return err;
550 ca6e02ac 2020-01-07 stsp static const struct got_error *
551 e70bf110 2020-03-22 stsp send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
552 e70bf110 2020-03-22 stsp struct imsgbuf *ibuf)
554 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
555 01bb5a15 2021-09-25 thomas.ad size_t i;
557 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
558 e70bf110 2020-03-22 stsp sizeof(struct got_imsg_traversed_commits) +
559 e70bf110 2020-03-22 stsp ncommits * SHA1_DIGEST_LENGTH);
560 e70bf110 2020-03-22 stsp if (wbuf == NULL)
561 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
563 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
564 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
566 e70bf110 2020-03-22 stsp for (i = 0; i < ncommits; i++) {
567 e70bf110 2020-03-22 stsp struct got_object_id *id = &commit_ids[i];
568 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
569 e9f1a409 2022-05-19 thomas return got_error_from_errno(
570 e70bf110 2020-03-22 stsp "imsg_add TRAVERSED_COMMITS");
574 e70bf110 2020-03-22 stsp wbuf->fd = -1;
575 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
577 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
580 e70bf110 2020-03-22 stsp static const struct got_error *
581 e70bf110 2020-03-22 stsp send_commit_traversal_done(struct imsgbuf *ibuf)
583 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
584 e70bf110 2020-03-22 stsp NULL, 0) == -1)
585 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
587 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
590 e70bf110 2020-03-22 stsp static const struct got_error *
591 ca6e02ac 2020-01-07 stsp commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
592 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx,
593 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
595 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
596 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
597 ca6e02ac 2020-01-07 stsp struct got_object_qid *pid;
598 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL, *pcommit = NULL;
599 78e7b7b8 2022-05-19 thomas struct got_parsed_tree_entry *entries = NULL, *pentries = NULL;
600 ca6e02ac 2020-01-07 stsp int nentries = 0, pnentries = 0;
601 ca6e02ac 2020-01-07 stsp struct got_object_id id;
602 ca6e02ac 2020-01-07 stsp size_t datalen, path_len;
603 ca6e02ac 2020-01-07 stsp char *path = NULL;
604 ca6e02ac 2020-01-07 stsp const int min_alloc = 64;
605 ca6e02ac 2020-01-07 stsp int changed = 0, ncommits = 0, nallocated = 0;
606 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_ids = NULL;
608 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
609 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(iobj))
610 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
611 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
612 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
614 ca6e02ac 2020-01-07 stsp path_len = datalen - sizeof(iobj) - 1;
615 ca6e02ac 2020-01-07 stsp if (path_len < 0)
616 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
617 ca6e02ac 2020-01-07 stsp if (path_len > 0) {
618 ca6e02ac 2020-01-07 stsp path = imsg->data + sizeof(iobj);
619 ca6e02ac 2020-01-07 stsp if (path[path_len] != '\0')
620 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
623 ca6e02ac 2020-01-07 stsp nallocated = min_alloc;
624 ca6e02ac 2020-01-07 stsp commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
625 ca6e02ac 2020-01-07 stsp if (commit_ids == NULL)
626 ca6e02ac 2020-01-07 stsp return got_error_from_errno("reallocarray");
629 ca6e02ac 2020-01-07 stsp const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
632 ca6e02ac 2020-01-07 stsp if (sigint_received) {
633 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_CANCELLED);
637 ca6e02ac 2020-01-07 stsp if (commit == NULL) {
638 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx, &id);
639 ca6e02ac 2020-01-07 stsp if (idx == -1)
641 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx,
642 ca6e02ac 2020-01-07 stsp idx, &id, objcache);
644 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
646 ca6e02ac 2020-01-07 stsp err = NULL;
651 ca6e02ac 2020-01-07 stsp if (sizeof(struct got_imsg_traversed_commits) +
652 ca6e02ac 2020-01-07 stsp ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
653 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits,
657 ca6e02ac 2020-01-07 stsp ncommits = 0;
659 ca6e02ac 2020-01-07 stsp ncommits++;
660 ca6e02ac 2020-01-07 stsp if (ncommits > nallocated) {
661 ca6e02ac 2020-01-07 stsp struct got_object_id *new;
662 ca6e02ac 2020-01-07 stsp nallocated += min_alloc;
663 ca6e02ac 2020-01-07 stsp new = reallocarray(commit_ids, nallocated,
664 ca6e02ac 2020-01-07 stsp sizeof(*commit_ids));
665 ca6e02ac 2020-01-07 stsp if (new == NULL) {
666 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("reallocarray");
669 ca6e02ac 2020-01-07 stsp commit_ids = new;
671 ca6e02ac 2020-01-07 stsp memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
672 ca6e02ac 2020-01-07 stsp SHA1_DIGEST_LENGTH);
674 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(&commit->parent_ids);
675 ca6e02ac 2020-01-07 stsp if (pid == NULL)
678 ec242592 2022-04-22 thomas idx = got_packidx_get_object_idx(packidx, &pid->id);
679 ca6e02ac 2020-01-07 stsp if (idx == -1)
682 ec242592 2022-04-22 thomas err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
685 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
687 ca6e02ac 2020-01-07 stsp err = NULL;
691 ca6e02ac 2020-01-07 stsp if (path[0] == '/' && path[1] == '\0') {
692 ca6e02ac 2020-01-07 stsp if (got_object_id_cmp(pcommit->tree_id,
693 ca6e02ac 2020-01-07 stsp commit->tree_id) != 0) {
694 ca6e02ac 2020-01-07 stsp changed = 1;
699 ca6e02ac 2020-01-07 stsp uint8_t *buf = NULL, *pbuf = NULL;
701 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx,
702 ca6e02ac 2020-01-07 stsp commit->tree_id);
703 ca6e02ac 2020-01-07 stsp if (idx == -1)
705 ca6e02ac 2020-01-07 stsp pidx = got_packidx_get_object_idx(packidx,
706 ca6e02ac 2020-01-07 stsp pcommit->tree_id);
707 ca6e02ac 2020-01-07 stsp if (pidx == -1)
710 ca6e02ac 2020-01-07 stsp err = open_tree(&buf, &entries, &nentries, pack,
711 ca6e02ac 2020-01-07 stsp packidx, idx, commit->tree_id, objcache);
714 ca6e02ac 2020-01-07 stsp err = open_tree(&pbuf, &pentries, &pnentries, pack,
715 ca6e02ac 2020-01-07 stsp packidx, pidx, pcommit->tree_id, objcache);
721 ca6e02ac 2020-01-07 stsp err = tree_path_changed(&changed, &buf, &pbuf,
722 ca6e02ac 2020-01-07 stsp &entries, &nentries, &pentries, &pnentries, path,
723 ca6e02ac 2020-01-07 stsp pack, packidx, ibuf, objcache);
725 78e7b7b8 2022-05-19 thomas free(entries);
726 78e7b7b8 2022-05-19 thomas entries = NULL;
727 ca6e02ac 2020-01-07 stsp nentries = 0;
729 78e7b7b8 2022-05-19 thomas free(pentries);
730 78e7b7b8 2022-05-19 thomas pentries = NULL;
731 ca6e02ac 2020-01-07 stsp pnentries = 0;
732 ca6e02ac 2020-01-07 stsp free(pbuf);
734 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
736 ca6e02ac 2020-01-07 stsp err = NULL;
741 ca6e02ac 2020-01-07 stsp if (!changed) {
742 ec242592 2022-04-22 thomas memcpy(id.sha1, pid->id.sha1, SHA1_DIGEST_LENGTH);
743 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
744 ca6e02ac 2020-01-07 stsp commit = pcommit;
745 ca6e02ac 2020-01-07 stsp pcommit = NULL;
747 ca6e02ac 2020-01-07 stsp } while (!changed);
749 ca6e02ac 2020-01-07 stsp if (ncommits > 0) {
750 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits, ibuf);
754 ca6e02ac 2020-01-07 stsp if (changed) {
755 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit(ibuf, commit);
760 e70bf110 2020-03-22 stsp err = send_commit_traversal_done(ibuf);
762 ca6e02ac 2020-01-07 stsp free(commit_ids);
763 ca6e02ac 2020-01-07 stsp if (commit)
764 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
765 ca6e02ac 2020-01-07 stsp if (pcommit)
766 ca6e02ac 2020-01-07 stsp got_object_commit_close(pcommit);
767 78e7b7b8 2022-05-19 thomas free(entries);
768 78e7b7b8 2022-05-19 thomas free(pentries);
770 f4a881ce 2018-11-17 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
771 f4a881ce 2018-11-17 stsp err = NULL;
773 f4a881ce 2018-11-17 stsp got_privsep_send_error(ibuf, err);
776 f4a881ce 2018-11-17 stsp return err;
779 f4a881ce 2018-11-17 stsp static const struct got_error *
780 48b4f239 2021-12-31 thomas raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
781 48b4f239 2021-12-31 thomas struct got_pack *pack, struct got_packidx *packidx,
782 bc1f382f 2022-01-05 thomas struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
784 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
785 59d1e4a0 2021-03-10 stsp uint8_t *buf = NULL;
786 59d1e4a0 2021-03-10 stsp uint64_t size = 0;
787 bc1f382f 2022-01-05 thomas FILE *outfile = NULL;
788 59d1e4a0 2021-03-10 stsp struct got_imsg_packed_object iobj;
789 59d1e4a0 2021-03-10 stsp struct got_object *obj;
790 59d1e4a0 2021-03-10 stsp struct got_object_id id;
791 59d1e4a0 2021-03-10 stsp size_t datalen;
793 59d1e4a0 2021-03-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
794 59d1e4a0 2021-03-10 stsp if (datalen != sizeof(iobj))
795 59d1e4a0 2021-03-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
796 59d1e4a0 2021-03-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
797 59d1e4a0 2021-03-10 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
799 59d1e4a0 2021-03-10 stsp obj = got_object_cache_get(objcache, &id);
801 59d1e4a0 2021-03-10 stsp obj->refcnt++;
803 59d1e4a0 2021-03-10 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
806 59d1e4a0 2021-03-10 stsp return err;
809 59d1e4a0 2021-03-10 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
811 59d1e4a0 2021-03-10 stsp return err;
813 59d1e4a0 2021-03-10 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
814 59d1e4a0 2021-03-10 stsp err = got_pack_get_max_delta_object_size(&size, obj, pack);
818 59d1e4a0 2021-03-10 stsp size = obj->size;
820 59d1e4a0 2021-03-10 stsp if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
821 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
822 59d1e4a0 2021-03-10 stsp obj, pack);
824 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
825 59d1e4a0 2021-03-10 stsp accumfile);
829 40e3cb72 2021-06-22 stsp err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
832 59d1e4a0 2021-03-10 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
833 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fclose");
834 59d1e4a0 2021-03-10 stsp got_object_close(obj);
835 59d1e4a0 2021-03-10 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
836 59d1e4a0 2021-03-10 stsp got_privsep_send_error(ibuf, err);
838 59d1e4a0 2021-03-10 stsp return err;
841 f9c2e8e5 2022-02-13 thomas static const struct got_error *
842 f9c2e8e5 2022-02-13 thomas get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
843 f9c2e8e5 2022-02-13 thomas off_t base_offset)
845 f9c2e8e5 2022-02-13 thomas const struct got_error *err;
848 f9c2e8e5 2022-02-13 thomas err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
850 f9c2e8e5 2022-02-13 thomas return err;
851 f9c2e8e5 2022-02-13 thomas if (idx == -1)
852 f9c2e8e5 2022-02-13 thomas return got_error(GOT_ERR_BAD_PACKIDX);
854 f9c2e8e5 2022-02-13 thomas return got_packidx_get_object_id(base_id, packidx, idx);
857 59d1e4a0 2021-03-10 stsp static const struct got_error *
858 f9c2e8e5 2022-02-13 thomas raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
859 f9c2e8e5 2022-02-13 thomas FILE *delta_outfile, struct got_pack *pack,
860 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx)
862 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
863 f9c2e8e5 2022-02-13 thomas struct got_imsg_raw_delta_request req;
864 9249e7e3 2022-05-12 thomas size_t datalen, delta_size, delta_compressed_size;
865 f9c2e8e5 2022-02-13 thomas off_t delta_offset;
866 f9c2e8e5 2022-02-13 thomas uint8_t *delta_buf = NULL;
867 f9c2e8e5 2022-02-13 thomas struct got_object_id id, base_id;
868 f9c2e8e5 2022-02-13 thomas off_t base_offset, delta_out_offset = 0;
869 f9c2e8e5 2022-02-13 thomas uint64_t base_size = 0, result_size = 0;
870 f9c2e8e5 2022-02-13 thomas size_t w;
872 f9c2e8e5 2022-02-13 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
873 f9c2e8e5 2022-02-13 thomas if (datalen != sizeof(req))
874 f9c2e8e5 2022-02-13 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
875 f9c2e8e5 2022-02-13 thomas memcpy(&req, imsg->data, sizeof(req));
876 f9c2e8e5 2022-02-13 thomas memcpy(id.sha1, req.id, SHA1_DIGEST_LENGTH);
878 f9c2e8e5 2022-02-13 thomas imsg->fd = -1;
880 f9c2e8e5 2022-02-13 thomas err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
881 9249e7e3 2022-05-12 thomas &delta_compressed_size, &delta_offset, &base_offset, &base_id,
882 9249e7e3 2022-05-12 thomas &base_size, &result_size, pack, packidx, req.idx);
884 f9c2e8e5 2022-02-13 thomas goto done;
887 f9c2e8e5 2022-02-13 thomas * If this is an offset delta we must determine the base
888 f9c2e8e5 2022-02-13 thomas * object ID ourselves.
890 f9c2e8e5 2022-02-13 thomas if (base_offset != 0) {
891 f9c2e8e5 2022-02-13 thomas err = get_base_object_id(&base_id, packidx, base_offset);
893 f9c2e8e5 2022-02-13 thomas goto done;
896 f9c2e8e5 2022-02-13 thomas delta_out_offset = ftello(delta_outfile);
897 9249e7e3 2022-05-12 thomas w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
898 9249e7e3 2022-05-12 thomas if (w != delta_compressed_size) {
899 f9c2e8e5 2022-02-13 thomas err = got_ferror(delta_outfile, GOT_ERR_IO);
900 f9c2e8e5 2022-02-13 thomas goto done;
902 f9c2e8e5 2022-02-13 thomas if (fflush(delta_outfile) == -1) {
903 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fflush");
904 f9c2e8e5 2022-02-13 thomas goto done;
907 f9c2e8e5 2022-02-13 thomas err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
908 9249e7e3 2022-05-12 thomas delta_size, delta_compressed_size, delta_offset, delta_out_offset,
909 9249e7e3 2022-05-12 thomas &base_id);
911 f9c2e8e5 2022-02-13 thomas free(delta_buf);
912 f9c2e8e5 2022-02-13 thomas return err;
915 7d0d4920 2022-05-12 thomas struct search_deltas_arg {
916 7d0d4920 2022-05-12 thomas struct imsgbuf *ibuf;
917 7d0d4920 2022-05-12 thomas struct got_packidx *packidx;
918 7d0d4920 2022-05-12 thomas struct got_pack *pack;
919 7d0d4920 2022-05-12 thomas struct got_object_idset *idset;
920 7d0d4920 2022-05-12 thomas FILE *delta_outfile;
921 7d0d4920 2022-05-12 thomas struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
922 7d0d4920 2022-05-12 thomas size_t ndeltas;
925 f9c2e8e5 2022-02-13 thomas static const struct got_error *
926 7d0d4920 2022-05-12 thomas search_delta_for_object(struct got_object_id *id, void *data, void *arg)
928 7d0d4920 2022-05-12 thomas const struct got_error *err;
929 7d0d4920 2022-05-12 thomas struct search_deltas_arg *a = arg;
930 7d0d4920 2022-05-12 thomas int obj_idx;
931 7d0d4920 2022-05-12 thomas uint8_t *delta_buf = NULL;
932 7d0d4920 2022-05-12 thomas uint64_t base_size, result_size;
933 7d0d4920 2022-05-12 thomas size_t delta_size, delta_compressed_size;
934 7d0d4920 2022-05-12 thomas off_t delta_offset, base_offset;
935 7d0d4920 2022-05-12 thomas struct got_object_id base_id;
937 7d0d4920 2022-05-12 thomas if (sigint_received)
938 7d0d4920 2022-05-12 thomas return got_error(GOT_ERR_CANCELLED);
940 7d0d4920 2022-05-12 thomas obj_idx = got_packidx_get_object_idx(a->packidx, id);
941 7d0d4920 2022-05-12 thomas if (obj_idx == -1)
942 7d0d4920 2022-05-12 thomas return NULL; /* object not present in our pack file */
944 7d0d4920 2022-05-12 thomas err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
945 7d0d4920 2022-05-12 thomas &delta_compressed_size, &delta_offset, &base_offset, &base_id,
946 7d0d4920 2022-05-12 thomas &base_size, &result_size, a->pack, a->packidx, obj_idx);
947 7d0d4920 2022-05-12 thomas if (err) {
948 7d0d4920 2022-05-12 thomas if (err->code == GOT_ERR_OBJ_TYPE)
949 7d0d4920 2022-05-12 thomas return NULL; /* object not stored as a delta */
950 7d0d4920 2022-05-12 thomas return err;
954 7d0d4920 2022-05-12 thomas * If this is an offset delta we must determine the base
955 7d0d4920 2022-05-12 thomas * object ID ourselves.
957 7d0d4920 2022-05-12 thomas if (base_offset != 0) {
958 7d0d4920 2022-05-12 thomas err = get_base_object_id(&base_id, a->packidx, base_offset);
960 7d0d4920 2022-05-12 thomas goto done;
963 7d0d4920 2022-05-12 thomas if (got_object_idset_contains(a->idset, &base_id)) {
964 7d0d4920 2022-05-12 thomas struct got_imsg_reused_delta *delta;
965 7d0d4920 2022-05-12 thomas off_t delta_out_offset = ftello(a->delta_outfile);
966 7d0d4920 2022-05-12 thomas size_t w;
968 7d0d4920 2022-05-12 thomas w = fwrite(delta_buf, 1, delta_compressed_size,
969 7d0d4920 2022-05-12 thomas a->delta_outfile);
970 7d0d4920 2022-05-12 thomas if (w != delta_compressed_size) {
971 7d0d4920 2022-05-12 thomas err = got_ferror(a->delta_outfile, GOT_ERR_IO);
972 7d0d4920 2022-05-12 thomas goto done;
975 7d0d4920 2022-05-12 thomas delta = &a->deltas[a->ndeltas++];
976 7d0d4920 2022-05-12 thomas memcpy(&delta->id, id, sizeof(delta->id));
977 7d0d4920 2022-05-12 thomas memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
978 7d0d4920 2022-05-12 thomas delta->base_size = base_size;
979 7d0d4920 2022-05-12 thomas delta->result_size = result_size;
980 7d0d4920 2022-05-12 thomas delta->delta_size = delta_size;
981 7d0d4920 2022-05-12 thomas delta->delta_compressed_size = delta_compressed_size;
982 7d0d4920 2022-05-12 thomas delta->delta_offset = delta_offset;
983 7d0d4920 2022-05-12 thomas delta->delta_out_offset = delta_out_offset;
985 7d0d4920 2022-05-12 thomas if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
986 7d0d4920 2022-05-12 thomas err = got_privsep_send_reused_deltas(a->ibuf,
987 7d0d4920 2022-05-12 thomas a->deltas, a->ndeltas);
989 7d0d4920 2022-05-12 thomas goto done;
990 7d0d4920 2022-05-12 thomas a->ndeltas = 0;
994 7d0d4920 2022-05-12 thomas free(delta_buf);
995 7d0d4920 2022-05-12 thomas return err;
998 7d0d4920 2022-05-12 thomas static const struct got_error *
999 7d0d4920 2022-05-12 thomas recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
1001 7d0d4920 2022-05-12 thomas const struct got_error *err = NULL;
1002 7d0d4920 2022-05-12 thomas int done = 0;
1003 7d0d4920 2022-05-12 thomas struct got_object_id *ids;
1004 7d0d4920 2022-05-12 thomas size_t nids, i;
1006 7d0d4920 2022-05-12 thomas for (;;) {
1007 7d0d4920 2022-05-12 thomas err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1008 7d0d4920 2022-05-12 thomas if (err || done)
1010 7d0d4920 2022-05-12 thomas for (i = 0; i < nids; i++) {
1011 7d0d4920 2022-05-12 thomas err = got_object_idset_add(idset, &ids[i], NULL);
1012 7d0d4920 2022-05-12 thomas if (err) {
1013 7d0d4920 2022-05-12 thomas free(ids);
1014 7d0d4920 2022-05-12 thomas return err;
1017 7d0d4920 2022-05-12 thomas free(ids);
1020 63915ee5 2022-06-23 thomas return err;
1023 63915ee5 2022-06-23 thomas static const struct got_error *
1024 63915ee5 2022-06-23 thomas recv_object_id_queue(struct got_object_id_queue *queue, struct imsgbuf *ibuf)
1026 63915ee5 2022-06-23 thomas const struct got_error *err = NULL;
1027 63915ee5 2022-06-23 thomas int done = 0;
1028 63915ee5 2022-06-23 thomas struct got_object_qid *qid;
1029 63915ee5 2022-06-23 thomas struct got_object_id *ids;
1030 63915ee5 2022-06-23 thomas size_t nids, i;
1032 63915ee5 2022-06-23 thomas for (;;) {
1033 63915ee5 2022-06-23 thomas err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1034 63915ee5 2022-06-23 thomas if (err || done)
1036 63915ee5 2022-06-23 thomas for (i = 0; i < nids; i++) {
1037 63915ee5 2022-06-23 thomas err = got_object_qid_alloc_partial(&qid);
1038 63915ee5 2022-06-23 thomas if (err)
1039 63915ee5 2022-06-23 thomas return err;
1040 63915ee5 2022-06-23 thomas memcpy(&qid->id, &ids[i], sizeof(qid->id));
1041 63915ee5 2022-06-23 thomas STAILQ_INSERT_TAIL(queue, qid, entry);
1045 13280750 2022-06-13 thomas return err;
1048 13280750 2022-06-13 thomas static const struct got_error *
1049 7d0d4920 2022-05-12 thomas delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1050 7d0d4920 2022-05-12 thomas FILE *delta_outfile, struct got_pack *pack, struct got_packidx *packidx)
1052 7d0d4920 2022-05-12 thomas const struct got_error *err = NULL;
1053 7d0d4920 2022-05-12 thomas struct got_object_idset *idset;
1054 7d0d4920 2022-05-12 thomas struct search_deltas_arg sda;
1056 7d0d4920 2022-05-12 thomas idset = got_object_idset_alloc();
1057 7d0d4920 2022-05-12 thomas if (idset == NULL)
1058 7d0d4920 2022-05-12 thomas return got_error_from_errno("got_object_idset_alloc");
1060 7d0d4920 2022-05-12 thomas err = recv_object_ids(idset, ibuf);
1061 7d0d4920 2022-05-12 thomas if (err)
1062 7d0d4920 2022-05-12 thomas return err;
1064 7d0d4920 2022-05-12 thomas memset(&sda, 0, sizeof(sda));
1065 7d0d4920 2022-05-12 thomas sda.ibuf = ibuf;
1066 7d0d4920 2022-05-12 thomas sda.idset = idset;
1067 7d0d4920 2022-05-12 thomas sda.pack = pack;
1068 7d0d4920 2022-05-12 thomas sda.packidx = packidx;
1069 7d0d4920 2022-05-12 thomas sda.delta_outfile = delta_outfile;
1070 7d0d4920 2022-05-12 thomas err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1071 7d0d4920 2022-05-12 thomas if (err)
1072 7d0d4920 2022-05-12 thomas goto done;
1074 7d0d4920 2022-05-12 thomas if (sda.ndeltas > 0) {
1075 7d0d4920 2022-05-12 thomas err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1076 7d0d4920 2022-05-12 thomas sda.ndeltas);
1077 7d0d4920 2022-05-12 thomas if (err)
1078 7d0d4920 2022-05-12 thomas goto done;
1081 7d0d4920 2022-05-12 thomas if (fflush(delta_outfile) == -1) {
1082 7d0d4920 2022-05-12 thomas err = got_error_from_errno("fflush");
1083 7d0d4920 2022-05-12 thomas goto done;
1086 7d0d4920 2022-05-12 thomas err = got_privsep_send_reused_deltas_done(ibuf);
1088 7d0d4920 2022-05-12 thomas got_object_idset_free(idset);
1089 7d0d4920 2022-05-12 thomas return err;
1092 7d0d4920 2022-05-12 thomas static const struct got_error *
1093 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1095 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1096 876c234b 2018-09-10 stsp struct imsg imsg;
1097 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1098 876c234b 2018-09-10 stsp size_t datalen;
1099 876c234b 2018-09-10 stsp struct got_packidx *p;
1101 876c234b 2018-09-10 stsp *packidx = NULL;
1103 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1105 876c234b 2018-09-10 stsp return err;
1107 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
1108 876c234b 2018-09-10 stsp if (p == NULL) {
1109 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1110 876c234b 2018-09-10 stsp goto done;
1113 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1114 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1115 876c234b 2018-09-10 stsp goto done;
1118 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
1119 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1120 876c234b 2018-09-10 stsp goto done;
1123 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1124 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
1125 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1126 876c234b 2018-09-10 stsp goto done;
1128 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1130 876c234b 2018-09-10 stsp p->len = ipackidx.len;
1131 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
1132 876c234b 2018-09-10 stsp if (p->fd == -1) {
1133 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
1134 56bef47a 2018-09-15 stsp goto done;
1136 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
1137 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1138 876c234b 2018-09-10 stsp goto done;
1141 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
1142 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1143 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
1144 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
1146 c3564dfa 2021-07-15 stsp err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1148 876c234b 2018-09-10 stsp if (err) {
1149 876c234b 2018-09-10 stsp if (imsg.fd != -1)
1150 876c234b 2018-09-10 stsp close(imsg.fd);
1151 876c234b 2018-09-10 stsp got_packidx_close(p);
1153 876c234b 2018-09-10 stsp *packidx = p;
1154 876c234b 2018-09-10 stsp imsg_free(&imsg);
1155 63915ee5 2022-06-23 thomas return err;
1158 63915ee5 2022-06-23 thomas static const struct got_error *
1159 63915ee5 2022-06-23 thomas send_tree_enumeration_done(struct imsgbuf *ibuf)
1161 63915ee5 2022-06-23 thomas if (imsg_compose(ibuf, GOT_IMSG_TREE_ENUMERATION_DONE, 0, 0, -1,
1162 63915ee5 2022-06-23 thomas NULL, 0) == -1)
1163 63915ee5 2022-06-23 thomas return got_error_from_errno("imsg_compose TREE_ENUMERATION_DONE");
1165 63915ee5 2022-06-23 thomas return got_privsep_flush_imsg(ibuf);
1168 63915ee5 2022-06-23 thomas struct enumerated_tree {
1169 63915ee5 2022-06-23 thomas struct got_object_id id;
1170 63915ee5 2022-06-23 thomas char *path;
1171 63915ee5 2022-06-23 thomas uint8_t *buf;
1172 63915ee5 2022-06-23 thomas struct got_parsed_tree_entry *entries;
1173 63915ee5 2022-06-23 thomas int nentries;
1176 63915ee5 2022-06-23 thomas static const struct got_error *
1177 63915ee5 2022-06-23 thomas enumerate_tree(int *have_all_entries, struct imsgbuf *ibuf, size_t *totlen,
1178 63915ee5 2022-06-23 thomas struct got_object_id *tree_id,
1179 63915ee5 2022-06-23 thomas const char *path, struct got_pack *pack, struct got_packidx *packidx,
1180 63915ee5 2022-06-23 thomas struct got_object_cache *objcache, struct got_object_idset *idset,
1181 63915ee5 2022-06-23 thomas struct enumerated_tree **trees, size_t *nalloc, size_t *ntrees)
1183 63915ee5 2022-06-23 thomas const struct got_error *err = NULL;
1184 63915ee5 2022-06-23 thomas struct got_object_id_queue ids;
1185 63915ee5 2022-06-23 thomas struct got_object_qid *qid;
1186 63915ee5 2022-06-23 thomas uint8_t *buf = NULL;
1187 63915ee5 2022-06-23 thomas struct got_parsed_tree_entry *entries = NULL;
1188 63915ee5 2022-06-23 thomas int nentries = 0, i;
1189 63915ee5 2022-06-23 thomas struct enumerated_tree *tree;
1191 63915ee5 2022-06-23 thomas *ntrees = 0;
1192 63915ee5 2022-06-23 thomas *have_all_entries = 1;
1193 63915ee5 2022-06-23 thomas STAILQ_INIT(&ids);
1195 63915ee5 2022-06-23 thomas err = got_object_qid_alloc_partial(&qid);
1196 63915ee5 2022-06-23 thomas if (err)
1197 63915ee5 2022-06-23 thomas return err;
1198 63915ee5 2022-06-23 thomas memcpy(&qid->id.sha1, tree_id, SHA1_DIGEST_LENGTH);
1199 63915ee5 2022-06-23 thomas qid->data = strdup(path);
1200 63915ee5 2022-06-23 thomas if (qid->data == NULL) {
1201 63915ee5 2022-06-23 thomas err = got_error_from_errno("strdup");
1202 63915ee5 2022-06-23 thomas goto done;
1204 63915ee5 2022-06-23 thomas STAILQ_INSERT_TAIL(&ids, qid, entry);
1205 63915ee5 2022-06-23 thomas qid = NULL;
1207 63915ee5 2022-06-23 thomas /* Traverse the tree hierarchy, gather tree object IDs and paths. */
1209 63915ee5 2022-06-23 thomas const char *path;
1210 63915ee5 2022-06-23 thomas int idx, i;
1212 63915ee5 2022-06-23 thomas if (sigint_received) {
1213 63915ee5 2022-06-23 thomas err = got_error(GOT_ERR_CANCELLED);
1214 63915ee5 2022-06-23 thomas goto done;
1217 63915ee5 2022-06-23 thomas qid = STAILQ_FIRST(&ids);
1218 63915ee5 2022-06-23 thomas STAILQ_REMOVE_HEAD(&ids, entry);
1219 63915ee5 2022-06-23 thomas path = qid->data;
1221 63915ee5 2022-06-23 thomas idx = got_packidx_get_object_idx(packidx, &qid->id);
1222 63915ee5 2022-06-23 thomas if (idx == -1) {
1223 63915ee5 2022-06-23 thomas *have_all_entries = 0;
1227 63915ee5 2022-06-23 thomas err = open_tree(&buf, &entries, &nentries,
1228 63915ee5 2022-06-23 thomas pack, packidx, idx, &qid->id, objcache);
1229 63915ee5 2022-06-23 thomas if (err) {
1230 63915ee5 2022-06-23 thomas if (err->code != GOT_ERR_NO_OBJ)
1231 63915ee5 2022-06-23 thomas goto done;
1234 63915ee5 2022-06-23 thomas err = got_object_idset_add(idset, &qid->id, NULL);
1235 63915ee5 2022-06-23 thomas if (err)
1236 63915ee5 2022-06-23 thomas goto done;
1238 63915ee5 2022-06-23 thomas for (i = 0; i < nentries; i++) {
1239 63915ee5 2022-06-23 thomas struct got_object_qid *eqid = NULL;
1240 63915ee5 2022-06-23 thomas struct got_parsed_tree_entry *pte = &entries[i];
1241 63915ee5 2022-06-23 thomas char *p;
1243 63915ee5 2022-06-23 thomas if (!S_ISDIR(pte->mode))
1244 63915ee5 2022-06-23 thomas continue;
1246 63915ee5 2022-06-23 thomas err = got_object_qid_alloc_partial(&eqid);
1247 63915ee5 2022-06-23 thomas if (err)
1248 63915ee5 2022-06-23 thomas goto done;
1249 63915ee5 2022-06-23 thomas memcpy(eqid->id.sha1, pte->id, sizeof(eqid->id.sha1));
1251 63915ee5 2022-06-23 thomas if (got_object_idset_contains(idset, &eqid->id)) {
1252 63915ee5 2022-06-23 thomas got_object_qid_free(eqid);
1253 63915ee5 2022-06-23 thomas continue;
1256 63915ee5 2022-06-23 thomas if (asprintf(&p, "%s%s%s", path,
1257 63915ee5 2022-06-23 thomas got_path_is_root_dir(path) ? "" : "/",
1258 63915ee5 2022-06-23 thomas pte->name) == -1) {
1259 63915ee5 2022-06-23 thomas err = got_error_from_errno("asprintf");
1260 63915ee5 2022-06-23 thomas got_object_qid_free(eqid);
1261 63915ee5 2022-06-23 thomas goto done;
1263 63915ee5 2022-06-23 thomas eqid->data = p;
1264 63915ee5 2022-06-23 thomas STAILQ_INSERT_TAIL(&ids, eqid, entry);
1267 63915ee5 2022-06-23 thomas if (*ntrees >= *nalloc) {
1268 63915ee5 2022-06-23 thomas struct enumerated_tree *new;
1269 63915ee5 2022-06-23 thomas new = recallocarray(*trees, *nalloc, *nalloc + 16,
1270 63915ee5 2022-06-23 thomas sizeof(*new));
1271 63915ee5 2022-06-23 thomas if (new == NULL) {
1272 63915ee5 2022-06-23 thomas err = got_error_from_errno("malloc");
1273 63915ee5 2022-06-23 thomas goto done;
1275 63915ee5 2022-06-23 thomas *trees = new;
1276 63915ee5 2022-06-23 thomas *nalloc += 16;
1278 63915ee5 2022-06-23 thomas tree = &(*trees)[*ntrees];
1279 63915ee5 2022-06-23 thomas (*ntrees)++;
1280 63915ee5 2022-06-23 thomas memcpy(&tree->id, &qid->id, sizeof(tree->id));
1281 63915ee5 2022-06-23 thomas tree->path = qid->data;
1282 63915ee5 2022-06-23 thomas tree->buf = buf;
1283 63915ee5 2022-06-23 thomas buf = NULL;
1284 63915ee5 2022-06-23 thomas tree->entries = entries;
1285 63915ee5 2022-06-23 thomas entries = NULL;
1286 63915ee5 2022-06-23 thomas tree->nentries = nentries;
1288 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1289 63915ee5 2022-06-23 thomas qid = NULL;
1290 63915ee5 2022-06-23 thomas } while (!STAILQ_EMPTY(&ids));
1292 63915ee5 2022-06-23 thomas if (*have_all_entries) {
1295 63915ee5 2022-06-23 thomas * We have managed to traverse all entries in the hierarchy.
1296 63915ee5 2022-06-23 thomas * Tell the main process what we have found.
1298 63915ee5 2022-06-23 thomas for (i = 0; i < *ntrees; i++) {
1299 63915ee5 2022-06-23 thomas tree = &(*trees)[i];
1300 63915ee5 2022-06-23 thomas err = got_privsep_send_enumerated_tree(totlen,
1301 63915ee5 2022-06-23 thomas ibuf, &tree->id, tree->path, tree->entries,
1302 63915ee5 2022-06-23 thomas tree->nentries);
1303 63915ee5 2022-06-23 thomas if (err)
1304 63915ee5 2022-06-23 thomas goto done;
1305 63915ee5 2022-06-23 thomas free(tree->buf);
1306 63915ee5 2022-06-23 thomas tree->buf = NULL;
1307 63915ee5 2022-06-23 thomas free(tree->path);
1308 63915ee5 2022-06-23 thomas tree->path = NULL;
1309 63915ee5 2022-06-23 thomas free(tree->entries);
1310 63915ee5 2022-06-23 thomas tree->entries = NULL;
1312 63915ee5 2022-06-23 thomas *ntrees = 0; /* don't loop again below to free memory */
1314 63915ee5 2022-06-23 thomas err = send_tree_enumeration_done(ibuf);
1315 63915ee5 2022-06-23 thomas } else {
1317 63915ee5 2022-06-23 thomas * We can only load fully packed tree hierarchies on
1318 63915ee5 2022-06-23 thomas * behalf of the main process, otherwise the main process
1319 63915ee5 2022-06-23 thomas * gets a wrong idea about which tree objects have
1320 63915ee5 2022-06-23 thomas * already been traversed.
1321 63915ee5 2022-06-23 thomas * Indicate a missing entry for the root of this tree.
1322 63915ee5 2022-06-23 thomas * The main process should continue by loading this
1323 63915ee5 2022-06-23 thomas * entire tree the slow way.
1325 63915ee5 2022-06-23 thomas err = got_privsep_send_enumerated_tree(totlen, ibuf,
1326 63915ee5 2022-06-23 thomas tree_id, "/", NULL, -1);
1327 63915ee5 2022-06-23 thomas if (err)
1328 63915ee5 2022-06-23 thomas goto done;
1331 63915ee5 2022-06-23 thomas free(buf);
1332 63915ee5 2022-06-23 thomas free(entries);
1333 63915ee5 2022-06-23 thomas for (i = 0; i < *ntrees; i++) {
1334 63915ee5 2022-06-23 thomas tree = &(*trees)[i];
1335 63915ee5 2022-06-23 thomas free(tree->buf);
1336 63915ee5 2022-06-23 thomas tree->buf = NULL;
1337 63915ee5 2022-06-23 thomas free(tree->path);
1338 63915ee5 2022-06-23 thomas tree->path = NULL;
1339 63915ee5 2022-06-23 thomas free(tree->entries);
1340 63915ee5 2022-06-23 thomas tree->entries = NULL;
1342 63915ee5 2022-06-23 thomas if (qid)
1343 63915ee5 2022-06-23 thomas free(qid->data);
1344 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1345 63915ee5 2022-06-23 thomas got_object_id_queue_free(&ids);
1346 63915ee5 2022-06-23 thomas if (err) {
1347 63915ee5 2022-06-23 thomas if (err->code == GOT_ERR_PRIVSEP_PIPE)
1348 63915ee5 2022-06-23 thomas err = NULL;
1350 63915ee5 2022-06-23 thomas got_privsep_send_error(ibuf, err);
1353 13280750 2022-06-13 thomas return err;
1356 13280750 2022-06-13 thomas static const struct got_error *
1357 63915ee5 2022-06-23 thomas enumeration_request(struct imsg *imsg, struct imsgbuf *ibuf,
1358 63915ee5 2022-06-23 thomas struct got_pack *pack, struct got_packidx *packidx,
1359 63915ee5 2022-06-23 thomas struct got_object_cache *objcache)
1361 63915ee5 2022-06-23 thomas const struct got_error *err = NULL;
1362 63915ee5 2022-06-23 thomas struct got_object_id_queue commit_ids;
1363 63915ee5 2022-06-23 thomas const struct got_object_id_queue *parents = NULL;
1364 63915ee5 2022-06-23 thomas struct got_object_qid *qid = NULL;
1365 63915ee5 2022-06-23 thomas struct got_object *obj = NULL;
1366 63915ee5 2022-06-23 thomas struct got_commit_object *commit = NULL;
1367 63915ee5 2022-06-23 thomas struct got_object_id *tree_id = NULL;
1368 63915ee5 2022-06-23 thomas size_t totlen = 0;
1369 63915ee5 2022-06-23 thomas struct got_object_idset *idset;
1370 63915ee5 2022-06-23 thomas int i, idx, have_all_entries = 1;
1371 63915ee5 2022-06-23 thomas struct enumerated_tree *trees = NULL;
1372 63915ee5 2022-06-23 thomas size_t ntrees = 0, nalloc = 16;
1374 63915ee5 2022-06-23 thomas STAILQ_INIT(&commit_ids);
1376 63915ee5 2022-06-23 thomas trees = calloc(1, nalloc);
1377 63915ee5 2022-06-23 thomas if (trees == NULL)
1378 63915ee5 2022-06-23 thomas return got_error_from_errno("calloc");
1380 63915ee5 2022-06-23 thomas idset = got_object_idset_alloc();
1381 63915ee5 2022-06-23 thomas if (idset == NULL) {
1382 63915ee5 2022-06-23 thomas err = got_error_from_errno("got_object_idset_alloc");
1383 63915ee5 2022-06-23 thomas goto done;
1386 63915ee5 2022-06-23 thomas err = recv_object_id_queue(&commit_ids, ibuf);
1387 63915ee5 2022-06-23 thomas if (err)
1388 63915ee5 2022-06-23 thomas goto done;
1390 63915ee5 2022-06-23 thomas err = recv_object_ids(idset, ibuf);
1391 63915ee5 2022-06-23 thomas if (err)
1392 63915ee5 2022-06-23 thomas goto done;
1394 63915ee5 2022-06-23 thomas while (!STAILQ_EMPTY(&commit_ids)) {
1395 63915ee5 2022-06-23 thomas if (sigint_received) {
1396 63915ee5 2022-06-23 thomas err = got_error(GOT_ERR_CANCELLED);
1397 63915ee5 2022-06-23 thomas goto done;
1400 63915ee5 2022-06-23 thomas qid = STAILQ_FIRST(&commit_ids);
1401 63915ee5 2022-06-23 thomas STAILQ_REMOVE_HEAD(&commit_ids, entry);
1403 63915ee5 2022-06-23 thomas if (got_object_idset_contains(idset, &qid->id)) {
1404 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1405 63915ee5 2022-06-23 thomas qid = NULL;
1406 63915ee5 2022-06-23 thomas continue;
1409 63915ee5 2022-06-23 thomas idx = got_packidx_get_object_idx(packidx, &qid->id);
1410 63915ee5 2022-06-23 thomas if (idx == -1)
1413 63915ee5 2022-06-23 thomas err = open_object(&obj, pack, packidx, idx, &qid->id,
1414 63915ee5 2022-06-23 thomas objcache);
1415 63915ee5 2022-06-23 thomas if (err)
1416 63915ee5 2022-06-23 thomas goto done;
1417 63915ee5 2022-06-23 thomas if (obj->type == GOT_OBJ_TYPE_TAG) {
1418 63915ee5 2022-06-23 thomas struct got_tag_object *tag;
1419 63915ee5 2022-06-23 thomas uint8_t *buf;
1420 63915ee5 2022-06-23 thomas size_t len;
1421 63915ee5 2022-06-23 thomas err = got_packfile_extract_object_to_mem(&buf,
1422 63915ee5 2022-06-23 thomas &len, obj, pack);
1423 63915ee5 2022-06-23 thomas if (err)
1424 63915ee5 2022-06-23 thomas goto done;
1425 63915ee5 2022-06-23 thomas obj->size = len;
1426 63915ee5 2022-06-23 thomas err = got_object_parse_tag(&tag, buf, len);
1427 63915ee5 2022-06-23 thomas if (err) {
1428 63915ee5 2022-06-23 thomas free(buf);
1429 63915ee5 2022-06-23 thomas goto done;
1431 63915ee5 2022-06-23 thomas idx = got_packidx_get_object_idx(packidx, &tag->id);
1432 63915ee5 2022-06-23 thomas if (idx == -1)
1434 63915ee5 2022-06-23 thomas err = open_commit(&commit, pack, packidx, idx,
1435 63915ee5 2022-06-23 thomas &tag->id, objcache);
1436 63915ee5 2022-06-23 thomas got_object_tag_close(tag);
1437 63915ee5 2022-06-23 thomas free(buf);
1438 63915ee5 2022-06-23 thomas if (err)
1439 63915ee5 2022-06-23 thomas goto done;
1440 63915ee5 2022-06-23 thomas } else if (obj->type == GOT_OBJ_TYPE_COMMIT) {
1441 63915ee5 2022-06-23 thomas err = open_commit(&commit, pack, packidx, idx,
1442 63915ee5 2022-06-23 thomas &qid->id, objcache);
1443 63915ee5 2022-06-23 thomas if (err)
1444 63915ee5 2022-06-23 thomas goto done;
1445 63915ee5 2022-06-23 thomas } else {
1446 63915ee5 2022-06-23 thomas err = got_error(GOT_ERR_OBJ_TYPE);
1447 63915ee5 2022-06-23 thomas goto done;
1449 63915ee5 2022-06-23 thomas got_object_close(obj);
1450 63915ee5 2022-06-23 thomas obj = NULL;
1452 63915ee5 2022-06-23 thomas err = got_privsep_send_enumerated_commit(ibuf, &qid->id,
1453 63915ee5 2022-06-23 thomas got_object_commit_get_committer_time(commit));
1454 63915ee5 2022-06-23 thomas if (err)
1455 63915ee5 2022-06-23 thomas goto done;
1457 63915ee5 2022-06-23 thomas tree_id = got_object_commit_get_tree_id(commit);
1458 63915ee5 2022-06-23 thomas idx = got_packidx_get_object_idx(packidx, tree_id);
1459 63915ee5 2022-06-23 thomas if (idx == -1) {
1460 63915ee5 2022-06-23 thomas err = got_privsep_send_enumerated_tree(&totlen, ibuf,
1461 63915ee5 2022-06-23 thomas tree_id, "/", NULL, -1);
1462 63915ee5 2022-06-23 thomas if (err)
1463 63915ee5 2022-06-23 thomas goto done;
1467 63915ee5 2022-06-23 thomas if (got_object_idset_contains(idset, tree_id)) {
1468 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1469 63915ee5 2022-06-23 thomas qid = NULL;
1470 63915ee5 2022-06-23 thomas continue;
1473 63915ee5 2022-06-23 thomas err = enumerate_tree(&have_all_entries, ibuf, &totlen, tree_id, "/",
1474 63915ee5 2022-06-23 thomas pack, packidx, objcache, idset, &trees, &nalloc, &ntrees);
1475 63915ee5 2022-06-23 thomas if (err)
1476 63915ee5 2022-06-23 thomas goto done;
1478 63915ee5 2022-06-23 thomas if (!have_all_entries)
1481 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1482 63915ee5 2022-06-23 thomas qid = NULL;
1484 63915ee5 2022-06-23 thomas parents = got_object_commit_get_parent_ids(commit);
1485 63915ee5 2022-06-23 thomas if (parents) {
1486 63915ee5 2022-06-23 thomas struct got_object_qid *pid;
1487 63915ee5 2022-06-23 thomas STAILQ_FOREACH(pid, parents, entry) {
1488 63915ee5 2022-06-23 thomas if (got_object_idset_contains(idset, &pid->id))
1489 63915ee5 2022-06-23 thomas continue;
1490 63915ee5 2022-06-23 thomas err = got_object_qid_alloc_partial(&qid);
1491 63915ee5 2022-06-23 thomas if (err)
1492 63915ee5 2022-06-23 thomas goto done;
1493 63915ee5 2022-06-23 thomas memcpy(&qid->id, &pid->id, sizeof(qid->id));
1494 63915ee5 2022-06-23 thomas STAILQ_INSERT_TAIL(&commit_ids, qid, entry);
1495 63915ee5 2022-06-23 thomas qid = NULL;
1499 63915ee5 2022-06-23 thomas got_object_commit_close(commit);
1500 63915ee5 2022-06-23 thomas commit = NULL;
1503 63915ee5 2022-06-23 thomas if (have_all_entries) {
1504 63915ee5 2022-06-23 thomas err = got_privsep_send_object_enumeration_done(ibuf);
1505 63915ee5 2022-06-23 thomas if (err)
1506 63915ee5 2022-06-23 thomas goto done;
1509 63915ee5 2022-06-23 thomas if (obj)
1510 63915ee5 2022-06-23 thomas got_object_close(obj);
1511 63915ee5 2022-06-23 thomas if (commit)
1512 63915ee5 2022-06-23 thomas got_object_commit_close(commit);
1513 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1514 63915ee5 2022-06-23 thomas got_object_id_queue_free(&commit_ids);
1515 63915ee5 2022-06-23 thomas if (idset)
1516 63915ee5 2022-06-23 thomas got_object_idset_free(idset);
1517 63915ee5 2022-06-23 thomas for (i = 0; i < ntrees; i++) {
1518 63915ee5 2022-06-23 thomas struct enumerated_tree *tree = &trees[i];
1519 63915ee5 2022-06-23 thomas free(tree->buf);
1520 63915ee5 2022-06-23 thomas free(tree->path);
1521 63915ee5 2022-06-23 thomas free(tree->entries);
1523 63915ee5 2022-06-23 thomas free(trees);
1524 63915ee5 2022-06-23 thomas return err;
1527 63915ee5 2022-06-23 thomas static const struct got_error *
1528 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1530 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1531 876c234b 2018-09-10 stsp struct imsg imsg;
1532 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1533 876c234b 2018-09-10 stsp size_t datalen;
1534 876c234b 2018-09-10 stsp struct got_pack *pack;
1536 876c234b 2018-09-10 stsp *packp = NULL;
1538 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1540 876c234b 2018-09-10 stsp return err;
1542 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
1543 876c234b 2018-09-10 stsp if (pack == NULL) {
1544 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1545 876c234b 2018-09-10 stsp goto done;
1548 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
1549 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1550 876c234b 2018-09-10 stsp goto done;
1553 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
1554 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1555 876c234b 2018-09-10 stsp goto done;
1558 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1559 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
1560 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1561 876c234b 2018-09-10 stsp goto done;
1563 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
1565 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
1566 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
1567 876c234b 2018-09-10 stsp if (pack->fd == -1) {
1568 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
1569 876c234b 2018-09-10 stsp goto done;
1571 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1572 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1573 56bef47a 2018-09-15 stsp goto done;
1575 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
1576 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
1577 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1578 ab2f42e7 2019-11-10 stsp goto done;
1581 a5061f77 2022-06-13 thomas err = got_delta_cache_alloc(&pack->delta_cache);
1582 a5061f77 2022-06-13 thomas if (err)
1583 876c234b 2018-09-10 stsp goto done;
1585 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
1586 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1587 876c234b 2018-09-10 stsp pack->fd, 0);
1588 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
1589 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
1592 876c234b 2018-09-10 stsp if (err) {
1593 876c234b 2018-09-10 stsp if (imsg.fd != -1)
1594 876c234b 2018-09-10 stsp close(imsg.fd);
1595 876c234b 2018-09-10 stsp free(pack);
1597 876c234b 2018-09-10 stsp *packp = pack;
1598 876c234b 2018-09-10 stsp imsg_free(&imsg);
1599 876c234b 2018-09-10 stsp return err;
1603 876c234b 2018-09-10 stsp main(int argc, char *argv[])
1605 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1606 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
1607 876c234b 2018-09-10 stsp struct imsg imsg;
1608 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
1609 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
1610 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
1611 f9c2e8e5 2022-02-13 thomas FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1613 876c234b 2018-09-10 stsp //static int attached;
1614 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
1616 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
1618 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1620 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1621 c59b3346 2018-09-11 stsp if (err) {
1622 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_cache_init");
1623 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
1627 2ff12563 2018-09-15 stsp #ifndef PROFILE
1628 876c234b 2018-09-10 stsp /* revoke access to most system calls */
1629 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
1630 638f9024 2019-05-13 stsp err = got_error_from_errno("pledge");
1631 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1635 97799ccd 2022-02-06 thomas /* revoke fs access */
1636 97799ccd 2022-02-06 thomas if (landlock_no_fs() == -1) {
1637 97799ccd 2022-02-06 thomas err = got_error_from_errno("landlock_no_fs");
1638 97799ccd 2022-02-06 thomas got_privsep_send_error(&ibuf, err);
1639 97799ccd 2022-02-06 thomas return 1;
1643 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
1644 876c234b 2018-09-10 stsp if (err) {
1645 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1649 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
1650 876c234b 2018-09-10 stsp if (err) {
1651 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1656 876c234b 2018-09-10 stsp imsg.fd = -1;
1658 99437157 2018-11-11 stsp if (sigint_received) {
1659 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
1663 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1664 876c234b 2018-09-10 stsp if (err) {
1665 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1666 876c234b 2018-09-10 stsp err = NULL;
1670 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1673 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
1674 bc1f382f 2022-01-05 thomas case GOT_IMSG_TMPFD:
1675 f9c2e8e5 2022-02-13 thomas if (basefile == NULL) {
1676 f9c2e8e5 2022-02-13 thomas err = receive_tempfile(&basefile, "w+",
1677 f9c2e8e5 2022-02-13 thomas &imsg, &ibuf);
1678 f9c2e8e5 2022-02-13 thomas } else if (accumfile == NULL) {
1679 f9c2e8e5 2022-02-13 thomas err = receive_tempfile(&accumfile, "w+",
1680 f9c2e8e5 2022-02-13 thomas &imsg, &ibuf);
1682 f9c2e8e5 2022-02-13 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1684 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
1685 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
1686 c59b3346 2018-09-11 stsp &objcache);
1688 59d1e4a0 2021-03-10 stsp case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1689 bc1f382f 2022-01-05 thomas if (basefile == NULL || accumfile == NULL) {
1690 bc1f382f 2022-01-05 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1693 59d1e4a0 2021-03-10 stsp err = raw_object_request(&imsg, &ibuf, pack, packidx,
1694 bc1f382f 2022-01-05 thomas &objcache, basefile, accumfile);
1696 f9c2e8e5 2022-02-13 thomas case GOT_IMSG_RAW_DELTA_OUTFD:
1697 f9c2e8e5 2022-02-13 thomas if (delta_outfile != NULL) {
1698 f9c2e8e5 2022-02-13 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1701 f9c2e8e5 2022-02-13 thomas err = receive_tempfile(&delta_outfile, "w",
1702 f9c2e8e5 2022-02-13 thomas &imsg, &ibuf);
1704 f9c2e8e5 2022-02-13 thomas case GOT_IMSG_RAW_DELTA_REQUEST:
1705 f9c2e8e5 2022-02-13 thomas if (delta_outfile == NULL) {
1706 f9c2e8e5 2022-02-13 thomas err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1709 f9c2e8e5 2022-02-13 thomas err = raw_delta_request(&imsg, &ibuf, delta_outfile,
1710 f9c2e8e5 2022-02-13 thomas pack, packidx);
1712 7d0d4920 2022-05-12 thomas case GOT_IMSG_DELTA_REUSE_REQUEST:
1713 7d0d4920 2022-05-12 thomas if (delta_outfile == NULL) {
1714 7d0d4920 2022-05-12 thomas err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1717 7d0d4920 2022-05-12 thomas err = delta_reuse_request(&imsg, &ibuf,
1718 7d0d4920 2022-05-12 thomas delta_outfile, pack, packidx);
1720 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
1721 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
1722 7762fe12 2018-11-05 stsp &objcache);
1724 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
1725 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
1726 62d463ca 2020-10-20 naddy &objcache);
1728 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
1729 bc1f382f 2022-01-05 thomas if (basefile == NULL || accumfile == NULL) {
1730 bc1f382f 2022-01-05 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1733 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
1734 bc1f382f 2022-01-05 thomas &objcache, basefile, accumfile);
1736 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG_REQUEST:
1737 f4a881ce 2018-11-17 stsp err = tag_request(&imsg, &ibuf, pack, packidx,
1738 62d463ca 2020-10-20 naddy &objcache);
1740 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1741 ca6e02ac 2020-01-07 stsp err = commit_traversal_request(&imsg, &ibuf, pack,
1742 ca6e02ac 2020-01-07 stsp packidx, &objcache);
1744 63915ee5 2022-06-23 thomas case GOT_IMSG_OBJECT_ENUMERATION_REQUEST:
1745 63915ee5 2022-06-23 thomas err = enumeration_request(&imsg, &ibuf, pack,
1746 63915ee5 2022-06-23 thomas packidx, &objcache);
1749 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1753 08578a35 2021-01-22 stsp if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
1754 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1755 876c234b 2018-09-10 stsp imsg_free(&imsg);
1760 c59b3346 2018-09-11 stsp if (packidx)
1761 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
1763 c59b3346 2018-09-11 stsp got_pack_close(pack);
1764 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
1765 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
1766 bc1f382f 2022-01-05 thomas if (basefile && fclose(basefile) == EOF && err == NULL)
1767 bc1f382f 2022-01-05 thomas err = got_error_from_errno("fclose");
1768 bc1f382f 2022-01-05 thomas if (accumfile && fclose(accumfile) == EOF && err == NULL)
1769 bc1f382f 2022-01-05 thomas err = got_error_from_errno("fclose");
1770 f9c2e8e5 2022-02-13 thomas if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
1771 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fclose");
1772 99437157 2018-11-11 stsp if (err) {
1773 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1774 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1775 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
1778 08578a35 2021-01-22 stsp if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
1779 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1780 876c234b 2018-09-10 stsp return err ? 1 : 0;