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 876c234b 2018-09-10 stsp #include <sys/types.h>
18 876c234b 2018-09-10 stsp #include <sys/queue.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 <imsg.h>
27 876c234b 2018-09-10 stsp #include <stdio.h>
28 876c234b 2018-09-10 stsp #include <stdlib.h>
29 876c234b 2018-09-10 stsp #include <string.h>
30 876c234b 2018-09-10 stsp #include <sha1.h>
31 81a12da5 2020-09-09 naddy #include <unistd.h>
32 876c234b 2018-09-10 stsp #include <zlib.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 fae7e038 2022-05-07 stsp #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 50127d69 2021-09-25 stsp 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 50127d69 2021-09-25 stsp static const struct got_error *
180 ca6e02ac 2020-01-07 stsp open_tree(uint8_t **buf, struct got_pathlist_head *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 3022d272 2019-11-14 stsp struct got_pathlist_head entries;
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 3022d272 2019-11-14 stsp TAILQ_INIT(&entries);
231 13c729f7 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
232 13c729f7 2018-12-24 stsp if (datalen != sizeof(iobj))
233 13c729f7 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
234 13c729f7 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
235 13c729f7 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
237 ca6e02ac 2020-01-07 stsp err = open_tree(&buf, &entries, &nentries, pack, packidx, iobj.idx,
238 62d463ca 2020-10-20 naddy &id, objcache);
240 ca6e02ac 2020-01-07 stsp return err;
242 3022d272 2019-11-14 stsp err = got_privsep_send_tree(ibuf, &entries, nentries);
243 b87b4170 2020-01-06 stsp got_object_parsed_tree_entries_free(&entries);
246 e7885405 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
247 e7885405 2018-09-10 stsp err = NULL;
249 e7885405 2018-09-10 stsp got_privsep_send_error(ibuf, err);
252 e7885405 2018-09-10 stsp return err;
255 876c234b 2018-09-10 stsp static const struct got_error *
256 030daac8 2021-09-25 stsp receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
258 3840f4c9 2018-09-12 stsp const struct got_error *err;
259 3840f4c9 2018-09-12 stsp struct imsg imsg;
260 55da3778 2018-09-10 stsp size_t datalen;
262 3840f4c9 2018-09-12 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
264 55da3778 2018-09-10 stsp return err;
266 3840f4c9 2018-09-12 stsp if (imsg.hdr.type != imsg_code) {
267 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
271 3840f4c9 2018-09-12 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
272 55da3778 2018-09-10 stsp if (datalen != 0) {
273 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
276 3840f4c9 2018-09-12 stsp if (imsg.fd == -1) {
277 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
281 3840f4c9 2018-09-12 stsp *f = fdopen(imsg.fd, "w+");
282 3840f4c9 2018-09-12 stsp if (*f == NULL) {
283 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
284 3a6ce05a 2019-02-11 stsp close(imsg.fd);
288 3840f4c9 2018-09-12 stsp imsg_free(&imsg);
289 3840f4c9 2018-09-12 stsp return err;
292 db696021 2022-01-04 stsp static const struct got_error *
293 67fd6849 2022-02-13 stsp receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
294 db696021 2022-01-04 stsp struct imsgbuf *ibuf)
296 db696021 2022-01-04 stsp size_t datalen;
298 db696021 2022-01-04 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
299 db696021 2022-01-04 stsp if (datalen != 0)
300 db696021 2022-01-04 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
302 db696021 2022-01-04 stsp if (imsg->fd == -1)
303 db696021 2022-01-04 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
305 67fd6849 2022-02-13 stsp *f = fdopen(imsg->fd, mode);
306 db696021 2022-01-04 stsp if (*f == NULL)
307 db696021 2022-01-04 stsp return got_error_from_errno("fdopen");
308 db696021 2022-01-04 stsp imsg->fd = -1;
310 db696021 2022-01-04 stsp return NULL;
313 3840f4c9 2018-09-12 stsp static const struct got_error *
314 3840f4c9 2018-09-12 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
315 db696021 2022-01-04 stsp struct got_packidx *packidx, struct got_object_cache *objcache,
316 db696021 2022-01-04 stsp FILE *basefile, FILE *accumfile)
318 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
319 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj;
320 3840f4c9 2018-09-12 stsp struct got_object *obj = NULL;
321 db696021 2022-01-04 stsp FILE *outfile = NULL;
322 ebc55e2d 2018-12-24 stsp struct got_object_id id;
323 ebc55e2d 2018-12-24 stsp size_t datalen;
324 ac544f8c 2019-01-13 stsp uint64_t blob_size;
325 ac544f8c 2019-01-13 stsp uint8_t *buf = NULL;
327 ebc55e2d 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
328 ebc55e2d 2018-12-24 stsp if (datalen != sizeof(iobj))
329 ebc55e2d 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
330 ebc55e2d 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
331 ebc55e2d 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
333 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
335 704b89c4 2019-05-23 stsp obj->refcnt++;
337 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
340 704b89c4 2019-05-23 stsp return err;
343 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
347 ac544f8c 2019-01-13 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
348 42c69117 2019-11-10 stsp err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
352 ac544f8c 2019-01-13 stsp blob_size = obj->size;
354 ac544f8c 2019-01-13 stsp if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
355 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
356 ac544f8c 2019-01-13 stsp obj, pack);
358 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
359 ac544f8c 2019-01-13 stsp accumfile);
363 ac544f8c 2019-01-13 stsp err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
366 56b63ca4 2021-01-22 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
367 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
368 cb5e38fd 2019-05-23 stsp got_object_close(obj);
369 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
370 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
372 55da3778 2018-09-10 stsp return err;
375 876c234b 2018-09-10 stsp static const struct got_error *
376 f4a881ce 2018-11-17 stsp tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
377 f4a881ce 2018-11-17 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
379 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
380 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj;
381 f4a881ce 2018-11-17 stsp struct got_object *obj = NULL;
382 f4a881ce 2018-11-17 stsp struct got_tag_object *tag = NULL;
383 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
384 f4a881ce 2018-11-17 stsp size_t len;
385 268f7291 2018-12-24 stsp struct got_object_id id;
386 268f7291 2018-12-24 stsp size_t datalen;
388 268f7291 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
389 268f7291 2018-12-24 stsp if (datalen != sizeof(iobj))
390 268f7291 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
391 268f7291 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
392 268f7291 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
394 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
396 704b89c4 2019-05-23 stsp obj->refcnt++;
398 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
401 704b89c4 2019-05-23 stsp return err;
404 f4a881ce 2018-11-17 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
408 f4a881ce 2018-11-17 stsp obj->size = len;
409 f4a881ce 2018-11-17 stsp err = got_object_parse_tag(&tag, buf, len);
413 f4a881ce 2018-11-17 stsp err = got_privsep_send_tag(ibuf, tag);
416 cb5e38fd 2019-05-23 stsp got_object_close(obj);
418 cb5e38fd 2019-05-23 stsp got_object_tag_close(tag);
420 ca6e02ac 2020-01-07 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
421 ca6e02ac 2020-01-07 stsp err = NULL;
423 ca6e02ac 2020-01-07 stsp got_privsep_send_error(ibuf, err);
426 ca6e02ac 2020-01-07 stsp return err;
429 ca6e02ac 2020-01-07 stsp static struct got_parsed_tree_entry *
430 ca6e02ac 2020-01-07 stsp find_entry_by_name(struct got_pathlist_head *entries, int nentries,
431 ca6e02ac 2020-01-07 stsp const char *name, size_t len)
433 ca6e02ac 2020-01-07 stsp struct got_pathlist_entry *pe;
435 ca6e02ac 2020-01-07 stsp /* Note that tree entries are sorted in strncmp() order. */
436 ca6e02ac 2020-01-07 stsp TAILQ_FOREACH(pe, entries, entry) {
437 ca6e02ac 2020-01-07 stsp int cmp = strncmp(pe->path, name, len);
438 ca6e02ac 2020-01-07 stsp if (cmp < 0)
440 ca6e02ac 2020-01-07 stsp if (cmp > 0)
442 ca6e02ac 2020-01-07 stsp if (pe->path[len] == '\0')
443 ca6e02ac 2020-01-07 stsp return (struct got_parsed_tree_entry *)pe->data;
445 ca6e02ac 2020-01-07 stsp return NULL;
448 50127d69 2021-09-25 stsp static const struct got_error *
449 ca6e02ac 2020-01-07 stsp tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
450 ca6e02ac 2020-01-07 stsp struct got_pathlist_head *entries1, int *nentries1,
451 ca6e02ac 2020-01-07 stsp struct got_pathlist_head *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 ca6e02ac 2020-01-07 stsp 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 ca6e02ac 2020-01-07 stsp 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 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_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 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_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 const struct got_error *err;
555 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
558 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
559 e70bf110 2020-03-22 stsp sizeof(struct got_imsg_traversed_commits) +
560 e70bf110 2020-03-22 stsp ncommits * SHA1_DIGEST_LENGTH);
561 e70bf110 2020-03-22 stsp if (wbuf == NULL)
562 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
564 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
565 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
566 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
567 e70bf110 2020-03-22 stsp return err;
569 e70bf110 2020-03-22 stsp for (i = 0; i < ncommits; i++) {
570 e70bf110 2020-03-22 stsp struct got_object_id *id = &commit_ids[i];
571 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
572 e70bf110 2020-03-22 stsp err = got_error_from_errno(
573 e70bf110 2020-03-22 stsp "imsg_add TRAVERSED_COMMITS");
574 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
575 e70bf110 2020-03-22 stsp return err;
579 e70bf110 2020-03-22 stsp wbuf->fd = -1;
580 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
582 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
585 e70bf110 2020-03-22 stsp static const struct got_error *
586 e70bf110 2020-03-22 stsp send_commit_traversal_done(struct imsgbuf *ibuf)
588 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
589 e70bf110 2020-03-22 stsp NULL, 0) == -1)
590 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
592 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
596 e70bf110 2020-03-22 stsp static const struct got_error *
597 ca6e02ac 2020-01-07 stsp commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
598 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx,
599 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
601 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
602 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
603 ca6e02ac 2020-01-07 stsp struct got_object_qid *pid;
604 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL, *pcommit = NULL;
605 ca6e02ac 2020-01-07 stsp struct got_pathlist_head entries, pentries;
606 ca6e02ac 2020-01-07 stsp int nentries = 0, pnentries = 0;
607 ca6e02ac 2020-01-07 stsp struct got_object_id id;
608 ca6e02ac 2020-01-07 stsp size_t datalen, path_len;
609 ca6e02ac 2020-01-07 stsp char *path = NULL;
610 ca6e02ac 2020-01-07 stsp const int min_alloc = 64;
611 ca6e02ac 2020-01-07 stsp int changed = 0, ncommits = 0, nallocated = 0;
612 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_ids = NULL;
614 ca6e02ac 2020-01-07 stsp TAILQ_INIT(&entries);
615 ca6e02ac 2020-01-07 stsp TAILQ_INIT(&pentries);
617 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
618 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(iobj))
619 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
620 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
621 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
623 ca6e02ac 2020-01-07 stsp path_len = datalen - sizeof(iobj) - 1;
624 ca6e02ac 2020-01-07 stsp if (path_len < 0)
625 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
626 ca6e02ac 2020-01-07 stsp if (path_len > 0) {
627 ca6e02ac 2020-01-07 stsp path = imsg->data + sizeof(iobj);
628 ca6e02ac 2020-01-07 stsp if (path[path_len] != '\0')
629 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
632 ca6e02ac 2020-01-07 stsp nallocated = min_alloc;
633 ca6e02ac 2020-01-07 stsp commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
634 ca6e02ac 2020-01-07 stsp if (commit_ids == NULL)
635 ca6e02ac 2020-01-07 stsp return got_error_from_errno("reallocarray");
638 ca6e02ac 2020-01-07 stsp const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
641 ca6e02ac 2020-01-07 stsp if (sigint_received) {
642 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_CANCELLED);
646 ca6e02ac 2020-01-07 stsp if (commit == NULL) {
647 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx, &id);
648 ca6e02ac 2020-01-07 stsp if (idx == -1)
650 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx,
651 ca6e02ac 2020-01-07 stsp idx, &id, objcache);
653 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
655 ca6e02ac 2020-01-07 stsp err = NULL;
660 ca6e02ac 2020-01-07 stsp if (sizeof(struct got_imsg_traversed_commits) +
661 ca6e02ac 2020-01-07 stsp ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
662 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits,
666 ca6e02ac 2020-01-07 stsp ncommits = 0;
668 ca6e02ac 2020-01-07 stsp ncommits++;
669 ca6e02ac 2020-01-07 stsp if (ncommits > nallocated) {
670 ca6e02ac 2020-01-07 stsp struct got_object_id *new;
671 ca6e02ac 2020-01-07 stsp nallocated += min_alloc;
672 ca6e02ac 2020-01-07 stsp new = reallocarray(commit_ids, nallocated,
673 ca6e02ac 2020-01-07 stsp sizeof(*commit_ids));
674 ca6e02ac 2020-01-07 stsp if (new == NULL) {
675 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("reallocarray");
678 ca6e02ac 2020-01-07 stsp commit_ids = new;
680 ca6e02ac 2020-01-07 stsp memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
681 ca6e02ac 2020-01-07 stsp SHA1_DIGEST_LENGTH);
683 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(&commit->parent_ids);
684 ca6e02ac 2020-01-07 stsp if (pid == NULL)
687 d7b5a0e8 2022-04-20 stsp idx = got_packidx_get_object_idx(packidx, &pid->id);
688 ca6e02ac 2020-01-07 stsp if (idx == -1)
691 d7b5a0e8 2022-04-20 stsp err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
694 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
696 ca6e02ac 2020-01-07 stsp err = NULL;
700 ca6e02ac 2020-01-07 stsp if (path[0] == '/' && path[1] == '\0') {
701 ca6e02ac 2020-01-07 stsp if (got_object_id_cmp(pcommit->tree_id,
702 ca6e02ac 2020-01-07 stsp commit->tree_id) != 0) {
703 ca6e02ac 2020-01-07 stsp changed = 1;
708 ca6e02ac 2020-01-07 stsp uint8_t *buf = NULL, *pbuf = NULL;
710 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx,
711 ca6e02ac 2020-01-07 stsp commit->tree_id);
712 ca6e02ac 2020-01-07 stsp if (idx == -1)
714 ca6e02ac 2020-01-07 stsp pidx = got_packidx_get_object_idx(packidx,
715 ca6e02ac 2020-01-07 stsp pcommit->tree_id);
716 ca6e02ac 2020-01-07 stsp if (pidx == -1)
719 ca6e02ac 2020-01-07 stsp err = open_tree(&buf, &entries, &nentries, pack,
720 ca6e02ac 2020-01-07 stsp packidx, idx, commit->tree_id, objcache);
723 ca6e02ac 2020-01-07 stsp err = open_tree(&pbuf, &pentries, &pnentries, pack,
724 ca6e02ac 2020-01-07 stsp packidx, pidx, pcommit->tree_id, objcache);
730 ca6e02ac 2020-01-07 stsp err = tree_path_changed(&changed, &buf, &pbuf,
731 ca6e02ac 2020-01-07 stsp &entries, &nentries, &pentries, &pnentries, path,
732 ca6e02ac 2020-01-07 stsp pack, packidx, ibuf, objcache);
734 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&entries);
735 ca6e02ac 2020-01-07 stsp nentries = 0;
737 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&pentries);
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 d7b5a0e8 2022-04-20 stsp 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 ca6e02ac 2020-01-07 stsp if (nentries != 0)
775 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&entries);
776 ca6e02ac 2020-01-07 stsp if (pnentries != 0)
777 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&pentries);
779 f4a881ce 2018-11-17 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
780 f4a881ce 2018-11-17 stsp err = NULL;
782 f4a881ce 2018-11-17 stsp got_privsep_send_error(ibuf, err);
785 f4a881ce 2018-11-17 stsp return err;
788 f4a881ce 2018-11-17 stsp static const struct got_error *
789 c0df5966 2021-12-31 stsp raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
790 c0df5966 2021-12-31 stsp struct got_pack *pack, struct got_packidx *packidx,
791 db696021 2022-01-04 stsp struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
793 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
794 59d1e4a0 2021-03-10 stsp uint8_t *buf = NULL;
795 59d1e4a0 2021-03-10 stsp uint64_t size = 0;
796 db696021 2022-01-04 stsp FILE *outfile = NULL;
797 59d1e4a0 2021-03-10 stsp struct got_imsg_packed_object iobj;
798 59d1e4a0 2021-03-10 stsp struct got_object *obj;
799 59d1e4a0 2021-03-10 stsp struct got_object_id id;
800 59d1e4a0 2021-03-10 stsp size_t datalen;
802 59d1e4a0 2021-03-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
803 59d1e4a0 2021-03-10 stsp if (datalen != sizeof(iobj))
804 59d1e4a0 2021-03-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
805 59d1e4a0 2021-03-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
806 59d1e4a0 2021-03-10 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
808 59d1e4a0 2021-03-10 stsp obj = got_object_cache_get(objcache, &id);
810 59d1e4a0 2021-03-10 stsp obj->refcnt++;
812 59d1e4a0 2021-03-10 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
815 59d1e4a0 2021-03-10 stsp return err;
818 59d1e4a0 2021-03-10 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
820 59d1e4a0 2021-03-10 stsp return err;
822 59d1e4a0 2021-03-10 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
823 59d1e4a0 2021-03-10 stsp err = got_pack_get_max_delta_object_size(&size, obj, pack);
827 59d1e4a0 2021-03-10 stsp size = obj->size;
829 59d1e4a0 2021-03-10 stsp if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
830 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
831 59d1e4a0 2021-03-10 stsp obj, pack);
833 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
834 59d1e4a0 2021-03-10 stsp accumfile);
838 40e3cb72 2021-06-22 stsp err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
841 59d1e4a0 2021-03-10 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
842 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fclose");
843 59d1e4a0 2021-03-10 stsp got_object_close(obj);
844 59d1e4a0 2021-03-10 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
845 59d1e4a0 2021-03-10 stsp got_privsep_send_error(ibuf, err);
847 59d1e4a0 2021-03-10 stsp return err;
850 67fd6849 2022-02-13 stsp static const struct got_error *
851 67fd6849 2022-02-13 stsp get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
852 67fd6849 2022-02-13 stsp off_t base_offset)
854 67fd6849 2022-02-13 stsp const struct got_error *err;
857 67fd6849 2022-02-13 stsp err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
859 67fd6849 2022-02-13 stsp return err;
860 67fd6849 2022-02-13 stsp if (idx == -1)
861 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_BAD_PACKIDX);
863 67fd6849 2022-02-13 stsp return got_packidx_get_object_id(base_id, packidx, idx);
866 59d1e4a0 2021-03-10 stsp static const struct got_error *
867 67fd6849 2022-02-13 stsp raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
868 67fd6849 2022-02-13 stsp FILE *delta_outfile, struct got_pack *pack,
869 67fd6849 2022-02-13 stsp struct got_packidx *packidx)
871 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
872 67fd6849 2022-02-13 stsp struct got_imsg_raw_delta_request req;
873 2d9e6abf 2022-05-04 stsp size_t datalen, delta_size, delta_compressed_size;
874 67fd6849 2022-02-13 stsp off_t delta_offset;
875 67fd6849 2022-02-13 stsp uint8_t *delta_buf = NULL;
876 67fd6849 2022-02-13 stsp struct got_object_id id, base_id;
877 67fd6849 2022-02-13 stsp off_t base_offset, delta_out_offset = 0;
878 67fd6849 2022-02-13 stsp uint64_t base_size = 0, result_size = 0;
881 67fd6849 2022-02-13 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
882 67fd6849 2022-02-13 stsp if (datalen != sizeof(req))
883 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
884 67fd6849 2022-02-13 stsp memcpy(&req, imsg->data, sizeof(req));
885 67fd6849 2022-02-13 stsp memcpy(id.sha1, req.id, SHA1_DIGEST_LENGTH);
887 67fd6849 2022-02-13 stsp imsg->fd = -1;
889 67fd6849 2022-02-13 stsp err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
890 2d9e6abf 2022-05-04 stsp &delta_compressed_size, &delta_offset, &base_offset, &base_id,
891 2d9e6abf 2022-05-04 stsp &base_size, &result_size, pack, packidx, req.idx);
896 67fd6849 2022-02-13 stsp * If this is an offset delta we must determine the base
897 67fd6849 2022-02-13 stsp * object ID ourselves.
899 67fd6849 2022-02-13 stsp if (base_offset != 0) {
900 67fd6849 2022-02-13 stsp err = get_base_object_id(&base_id, packidx, base_offset);
905 67fd6849 2022-02-13 stsp delta_out_offset = ftello(delta_outfile);
906 2d9e6abf 2022-05-04 stsp w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
907 2d9e6abf 2022-05-04 stsp if (w != delta_compressed_size) {
908 67fd6849 2022-02-13 stsp err = got_ferror(delta_outfile, GOT_ERR_IO);
911 67fd6849 2022-02-13 stsp if (fflush(delta_outfile) == -1) {
912 67fd6849 2022-02-13 stsp err = got_error_from_errno("fflush");
916 67fd6849 2022-02-13 stsp err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
917 2d9e6abf 2022-05-04 stsp delta_size, delta_compressed_size, delta_offset, delta_out_offset,
920 67fd6849 2022-02-13 stsp free(delta_buf);
921 67fd6849 2022-02-13 stsp return err;
924 fae7e038 2022-05-07 stsp struct search_deltas_arg {
925 fae7e038 2022-05-07 stsp struct imsgbuf *ibuf;
926 fae7e038 2022-05-07 stsp struct got_packidx *packidx;
927 fae7e038 2022-05-07 stsp struct got_pack *pack;
928 fae7e038 2022-05-07 stsp struct got_object_idset *idset;
929 fae7e038 2022-05-07 stsp FILE *delta_outfile;
930 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
931 fae7e038 2022-05-07 stsp size_t ndeltas;
934 67fd6849 2022-02-13 stsp static const struct got_error *
935 fae7e038 2022-05-07 stsp search_delta_for_object(struct got_object_id *id, void *data, void *arg)
937 fae7e038 2022-05-07 stsp const struct got_error *err;
938 fae7e038 2022-05-07 stsp struct search_deltas_arg *a = arg;
939 fae7e038 2022-05-07 stsp int obj_idx;
940 fae7e038 2022-05-07 stsp uint8_t *delta_buf = NULL;
941 fae7e038 2022-05-07 stsp uint64_t base_size, result_size;
942 fae7e038 2022-05-07 stsp size_t delta_size, delta_compressed_size;
943 fae7e038 2022-05-07 stsp off_t delta_offset, base_offset;
944 fae7e038 2022-05-07 stsp struct got_object_id base_id;
946 fae7e038 2022-05-07 stsp if (sigint_received)
947 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_CANCELLED);
949 fae7e038 2022-05-07 stsp obj_idx = got_packidx_get_object_idx(a->packidx, id);
950 fae7e038 2022-05-07 stsp if (obj_idx == -1)
951 fae7e038 2022-05-07 stsp return NULL; /* object not present in our pack file */
953 fae7e038 2022-05-07 stsp err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
954 fae7e038 2022-05-07 stsp &delta_compressed_size, &delta_offset, &base_offset, &base_id,
955 fae7e038 2022-05-07 stsp &base_size, &result_size, a->pack, a->packidx, obj_idx);
957 fae7e038 2022-05-07 stsp if (err->code == GOT_ERR_OBJ_TYPE)
958 fae7e038 2022-05-07 stsp return NULL; /* object not stored as a delta */
959 fae7e038 2022-05-07 stsp return err;
963 fae7e038 2022-05-07 stsp * If this is an offset delta we must determine the base
964 fae7e038 2022-05-07 stsp * object ID ourselves.
966 fae7e038 2022-05-07 stsp if (base_offset != 0) {
967 fae7e038 2022-05-07 stsp err = get_base_object_id(&base_id, a->packidx, base_offset);
972 fae7e038 2022-05-07 stsp if (got_object_idset_contains(a->idset, &base_id)) {
973 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta *delta;
974 fae7e038 2022-05-07 stsp off_t delta_out_offset = ftello(a->delta_outfile);
977 fae7e038 2022-05-07 stsp w = fwrite(delta_buf, 1, delta_compressed_size,
978 fae7e038 2022-05-07 stsp a->delta_outfile);
979 fae7e038 2022-05-07 stsp if (w != delta_compressed_size) {
980 fae7e038 2022-05-07 stsp err = got_ferror(a->delta_outfile, GOT_ERR_IO);
984 fae7e038 2022-05-07 stsp delta = &a->deltas[a->ndeltas++];
985 fae7e038 2022-05-07 stsp memcpy(&delta->id, id, sizeof(delta->id));
986 fae7e038 2022-05-07 stsp memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
987 fae7e038 2022-05-07 stsp delta->base_size = base_size;
988 fae7e038 2022-05-07 stsp delta->result_size = result_size;
989 fae7e038 2022-05-07 stsp delta->delta_size = delta_size;
990 fae7e038 2022-05-07 stsp delta->delta_compressed_size = delta_compressed_size;
991 fae7e038 2022-05-07 stsp delta->delta_offset = delta_offset;
992 fae7e038 2022-05-07 stsp delta->delta_out_offset = delta_out_offset;
994 fae7e038 2022-05-07 stsp if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
995 fae7e038 2022-05-07 stsp err = got_privsep_send_reused_deltas(a->ibuf,
996 fae7e038 2022-05-07 stsp a->deltas, a->ndeltas);
999 fae7e038 2022-05-07 stsp a->ndeltas = 0;
1003 fae7e038 2022-05-07 stsp free(delta_buf);
1004 fae7e038 2022-05-07 stsp return err;
1007 fae7e038 2022-05-07 stsp static const struct got_error *
1008 fae7e038 2022-05-07 stsp recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
1010 fae7e038 2022-05-07 stsp const struct got_error *err = NULL;
1011 fae7e038 2022-05-07 stsp int done = 0;
1012 fae7e038 2022-05-07 stsp struct got_object_id *ids;
1013 fae7e038 2022-05-07 stsp size_t nids, i;
1015 fae7e038 2022-05-07 stsp for (;;) {
1016 fae7e038 2022-05-07 stsp err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1017 fae7e038 2022-05-07 stsp if (err || done)
1019 fae7e038 2022-05-07 stsp for (i = 0; i < nids; i++) {
1020 fae7e038 2022-05-07 stsp err = got_object_idset_add(idset, &ids[i], NULL);
1021 fae7e038 2022-05-07 stsp if (err) {
1022 fae7e038 2022-05-07 stsp free(ids);
1023 fae7e038 2022-05-07 stsp return err;
1026 fae7e038 2022-05-07 stsp free(ids);
1029 fae7e038 2022-05-07 stsp return err;
1032 fae7e038 2022-05-07 stsp static const struct got_error *
1033 fae7e038 2022-05-07 stsp delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1034 fae7e038 2022-05-07 stsp FILE *delta_outfile, struct got_pack *pack, struct got_packidx *packidx)
1036 fae7e038 2022-05-07 stsp const struct got_error *err = NULL;
1037 fae7e038 2022-05-07 stsp struct got_object_idset *idset;
1038 fae7e038 2022-05-07 stsp struct search_deltas_arg sda;
1040 fae7e038 2022-05-07 stsp idset = got_object_idset_alloc();
1041 fae7e038 2022-05-07 stsp if (idset == NULL)
1042 fae7e038 2022-05-07 stsp return got_error_from_errno("got_object_idset_alloc");
1044 fae7e038 2022-05-07 stsp err = recv_object_ids(idset, ibuf);
1046 fae7e038 2022-05-07 stsp return err;
1048 fae7e038 2022-05-07 stsp memset(&sda, 0, sizeof(sda));
1049 fae7e038 2022-05-07 stsp sda.ibuf = ibuf;
1050 fae7e038 2022-05-07 stsp sda.idset = idset;
1051 fae7e038 2022-05-07 stsp sda.pack = pack;
1052 fae7e038 2022-05-07 stsp sda.packidx = packidx;
1053 fae7e038 2022-05-07 stsp sda.delta_outfile = delta_outfile;
1054 fae7e038 2022-05-07 stsp err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1056 fae7e038 2022-05-07 stsp goto done;
1058 fae7e038 2022-05-07 stsp if (sda.ndeltas > 0) {
1059 fae7e038 2022-05-07 stsp err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1060 fae7e038 2022-05-07 stsp sda.ndeltas);
1062 fae7e038 2022-05-07 stsp goto done;
1065 fae7e038 2022-05-07 stsp if (fflush(delta_outfile) == -1) {
1066 fae7e038 2022-05-07 stsp err = got_error_from_errno("fflush");
1067 fae7e038 2022-05-07 stsp goto done;
1070 fae7e038 2022-05-07 stsp err = got_privsep_send_reused_deltas_done(ibuf);
1072 fae7e038 2022-05-07 stsp got_object_idset_free(idset);
1073 fae7e038 2022-05-07 stsp return err;
1076 fae7e038 2022-05-07 stsp static const struct got_error *
1077 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1079 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1080 876c234b 2018-09-10 stsp struct imsg imsg;
1081 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1082 876c234b 2018-09-10 stsp size_t datalen;
1083 876c234b 2018-09-10 stsp struct got_packidx *p;
1085 876c234b 2018-09-10 stsp *packidx = NULL;
1087 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1089 876c234b 2018-09-10 stsp return err;
1091 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
1092 876c234b 2018-09-10 stsp if (p == NULL) {
1093 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1094 876c234b 2018-09-10 stsp goto done;
1097 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1098 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1099 876c234b 2018-09-10 stsp goto done;
1102 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
1103 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1104 876c234b 2018-09-10 stsp goto done;
1107 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1108 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
1109 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1110 876c234b 2018-09-10 stsp goto done;
1112 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1114 876c234b 2018-09-10 stsp p->len = ipackidx.len;
1115 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
1116 876c234b 2018-09-10 stsp if (p->fd == -1) {
1117 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
1118 56bef47a 2018-09-15 stsp goto done;
1120 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
1121 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1122 876c234b 2018-09-10 stsp goto done;
1125 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
1126 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1127 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
1128 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
1130 c3564dfa 2021-07-15 stsp err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1132 876c234b 2018-09-10 stsp if (err) {
1133 876c234b 2018-09-10 stsp if (imsg.fd != -1)
1134 876c234b 2018-09-10 stsp close(imsg.fd);
1135 876c234b 2018-09-10 stsp got_packidx_close(p);
1137 876c234b 2018-09-10 stsp *packidx = p;
1138 876c234b 2018-09-10 stsp imsg_free(&imsg);
1139 876c234b 2018-09-10 stsp return err;
1142 876c234b 2018-09-10 stsp static const struct got_error *
1143 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1145 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1146 876c234b 2018-09-10 stsp struct imsg imsg;
1147 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1148 876c234b 2018-09-10 stsp size_t datalen;
1149 876c234b 2018-09-10 stsp struct got_pack *pack;
1151 876c234b 2018-09-10 stsp *packp = NULL;
1153 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1155 876c234b 2018-09-10 stsp return err;
1157 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
1158 876c234b 2018-09-10 stsp if (pack == NULL) {
1159 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1160 876c234b 2018-09-10 stsp goto done;
1163 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
1164 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1165 876c234b 2018-09-10 stsp goto done;
1168 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
1169 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1170 876c234b 2018-09-10 stsp goto done;
1173 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1174 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
1175 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1176 876c234b 2018-09-10 stsp goto done;
1178 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
1180 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
1181 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
1182 876c234b 2018-09-10 stsp if (pack->fd == -1) {
1183 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
1184 876c234b 2018-09-10 stsp goto done;
1186 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1187 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1188 56bef47a 2018-09-15 stsp goto done;
1190 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
1191 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
1192 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1193 ab2f42e7 2019-11-10 stsp goto done;
1196 ab2f42e7 2019-11-10 stsp pack->delta_cache = got_delta_cache_alloc(100,
1197 ab2f42e7 2019-11-10 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX);
1198 ab2f42e7 2019-11-10 stsp if (pack->delta_cache == NULL) {
1199 ab2f42e7 2019-11-10 stsp err = got_error_from_errno("got_delta_cache_alloc");
1200 876c234b 2018-09-10 stsp goto done;
1203 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
1204 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1205 876c234b 2018-09-10 stsp pack->fd, 0);
1206 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
1207 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
1210 876c234b 2018-09-10 stsp if (err) {
1211 876c234b 2018-09-10 stsp if (imsg.fd != -1)
1212 876c234b 2018-09-10 stsp close(imsg.fd);
1213 876c234b 2018-09-10 stsp free(pack);
1215 876c234b 2018-09-10 stsp *packp = pack;
1216 876c234b 2018-09-10 stsp imsg_free(&imsg);
1217 876c234b 2018-09-10 stsp return err;
1221 876c234b 2018-09-10 stsp main(int argc, char *argv[])
1223 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1224 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
1225 876c234b 2018-09-10 stsp struct imsg imsg;
1226 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
1227 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
1228 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
1229 67fd6849 2022-02-13 stsp FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1231 876c234b 2018-09-10 stsp //static int attached;
1232 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
1234 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
1236 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1238 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1239 c59b3346 2018-09-11 stsp if (err) {
1240 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_cache_init");
1241 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
1245 2ff12563 2018-09-15 stsp #ifndef PROFILE
1246 876c234b 2018-09-10 stsp /* revoke access to most system calls */
1247 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
1248 638f9024 2019-05-13 stsp err = got_error_from_errno("pledge");
1249 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1254 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
1255 876c234b 2018-09-10 stsp if (err) {
1256 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1260 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
1261 876c234b 2018-09-10 stsp if (err) {
1262 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1267 876c234b 2018-09-10 stsp imsg.fd = -1;
1269 99437157 2018-11-11 stsp if (sigint_received) {
1270 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
1274 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1275 876c234b 2018-09-10 stsp if (err) {
1276 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1277 876c234b 2018-09-10 stsp err = NULL;
1281 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1284 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
1285 db696021 2022-01-04 stsp case GOT_IMSG_TMPFD:
1286 67fd6849 2022-02-13 stsp if (basefile == NULL) {
1287 67fd6849 2022-02-13 stsp err = receive_tempfile(&basefile, "w+",
1288 67fd6849 2022-02-13 stsp &imsg, &ibuf);
1289 67fd6849 2022-02-13 stsp } else if (accumfile == NULL) {
1290 67fd6849 2022-02-13 stsp err = receive_tempfile(&accumfile, "w+",
1291 67fd6849 2022-02-13 stsp &imsg, &ibuf);
1293 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1295 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
1296 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
1297 c59b3346 2018-09-11 stsp &objcache);
1299 59d1e4a0 2021-03-10 stsp case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1300 db696021 2022-01-04 stsp if (basefile == NULL || accumfile == NULL) {
1301 db696021 2022-01-04 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1304 59d1e4a0 2021-03-10 stsp err = raw_object_request(&imsg, &ibuf, pack, packidx,
1305 db696021 2022-01-04 stsp &objcache, basefile, accumfile);
1307 67fd6849 2022-02-13 stsp case GOT_IMSG_RAW_DELTA_OUTFD:
1308 67fd6849 2022-02-13 stsp if (delta_outfile != NULL) {
1309 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1312 67fd6849 2022-02-13 stsp err = receive_tempfile(&delta_outfile, "w",
1313 67fd6849 2022-02-13 stsp &imsg, &ibuf);
1315 67fd6849 2022-02-13 stsp case GOT_IMSG_RAW_DELTA_REQUEST:
1316 67fd6849 2022-02-13 stsp if (delta_outfile == NULL) {
1317 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1320 67fd6849 2022-02-13 stsp err = raw_delta_request(&imsg, &ibuf, delta_outfile,
1321 67fd6849 2022-02-13 stsp pack, packidx);
1323 fae7e038 2022-05-07 stsp case GOT_IMSG_DELTA_REUSE_REQUEST:
1324 fae7e038 2022-05-07 stsp if (delta_outfile == NULL) {
1325 fae7e038 2022-05-07 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1328 fae7e038 2022-05-07 stsp err = delta_reuse_request(&imsg, &ibuf,
1329 fae7e038 2022-05-07 stsp delta_outfile, pack, packidx);
1331 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
1332 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
1333 7762fe12 2018-11-05 stsp &objcache);
1335 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
1336 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
1337 62d463ca 2020-10-20 naddy &objcache);
1339 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
1340 db696021 2022-01-04 stsp if (basefile == NULL || accumfile == NULL) {
1341 db696021 2022-01-04 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1344 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
1345 db696021 2022-01-04 stsp &objcache, basefile, accumfile);
1347 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG_REQUEST:
1348 f4a881ce 2018-11-17 stsp err = tag_request(&imsg, &ibuf, pack, packidx,
1349 62d463ca 2020-10-20 naddy &objcache);
1351 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1352 ca6e02ac 2020-01-07 stsp err = commit_traversal_request(&imsg, &ibuf, pack,
1353 ca6e02ac 2020-01-07 stsp packidx, &objcache);
1356 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1360 08578a35 2021-01-22 stsp if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
1361 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1362 876c234b 2018-09-10 stsp imsg_free(&imsg);
1367 c59b3346 2018-09-11 stsp if (packidx)
1368 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
1370 c59b3346 2018-09-11 stsp got_pack_close(pack);
1371 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
1372 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
1373 db696021 2022-01-04 stsp if (basefile && fclose(basefile) == EOF && err == NULL)
1374 db696021 2022-01-04 stsp err = got_error_from_errno("fclose");
1375 db696021 2022-01-04 stsp if (accumfile && fclose(accumfile) == EOF && err == NULL)
1376 db696021 2022-01-04 stsp err = got_error_from_errno("fclose");
1377 67fd6849 2022-02-13 stsp if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
1378 67fd6849 2022-02-13 stsp err = got_error_from_errno("fclose");
1379 99437157 2018-11-11 stsp if (err) {
1380 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1381 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1382 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
1385 08578a35 2021-01-22 stsp if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
1386 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1387 876c234b 2018-09-10 stsp return err ? 1 : 0;