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/uio.h>
19 876c234b 2018-09-10 stsp #include <sys/time.h>
20 876c234b 2018-09-10 stsp #include <sys/mman.h>
22 876c234b 2018-09-10 stsp #include <limits.h>
23 99437157 2018-11-11 stsp #include <signal.h>
24 876c234b 2018-09-10 stsp #include <stdint.h>
25 876c234b 2018-09-10 stsp #include <stdio.h>
26 876c234b 2018-09-10 stsp #include <stdlib.h>
27 876c234b 2018-09-10 stsp #include <string.h>
28 81a12da5 2020-09-09 naddy #include <unistd.h>
29 876c234b 2018-09-10 stsp #include <zlib.h>
31 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
33 876c234b 2018-09-10 stsp #include "got_error.h"
34 876c234b 2018-09-10 stsp #include "got_object.h"
35 3022d272 2019-11-14 stsp #include "got_path.h"
37 876c234b 2018-09-10 stsp #include "got_lib_delta.h"
38 ab2f42e7 2019-11-10 stsp #include "got_lib_delta_cache.h"
39 876c234b 2018-09-10 stsp #include "got_lib_object.h"
40 c59b3346 2018-09-11 stsp #include "got_lib_object_cache.h"
41 876c234b 2018-09-10 stsp #include "got_lib_object_parse.h"
42 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
43 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
45 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
48 99437157 2018-11-11 stsp catch_sigint(int signo)
50 99437157 2018-11-11 stsp sigint_received = 1;
53 876c234b 2018-09-10 stsp static const struct got_error *
54 704b89c4 2019-05-23 stsp open_object(struct got_object **obj, struct got_pack *pack,
55 704b89c4 2019-05-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id,
56 704b89c4 2019-05-23 stsp struct got_object_cache *objcache)
58 704b89c4 2019-05-23 stsp const struct got_error *err;
60 704b89c4 2019-05-23 stsp err = got_packfile_open_object(obj, pack, packidx, idx, id);
63 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
65 704b89c4 2019-05-23 stsp err = got_object_cache_add(objcache, id, *obj);
67 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
68 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
72 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
73 704b89c4 2019-05-23 stsp return NULL;
76 704b89c4 2019-05-23 stsp static const struct got_error *
77 876c234b 2018-09-10 stsp object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
78 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
80 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
81 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
82 876c234b 2018-09-10 stsp struct got_object *obj;
83 106807b4 2018-09-15 stsp struct got_object_id id;
84 876c234b 2018-09-10 stsp size_t datalen;
86 876c234b 2018-09-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
87 876c234b 2018-09-10 stsp if (datalen != sizeof(iobj))
88 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
89 876c234b 2018-09-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
90 106807b4 2018-09-15 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
92 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
94 704b89c4 2019-05-23 stsp obj->refcnt++;
96 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
102 876c234b 2018-09-10 stsp err = got_privsep_send_obj(ibuf, obj);
104 876c234b 2018-09-10 stsp got_object_close(obj);
105 876c234b 2018-09-10 stsp return err;
108 8f1c06eb 2021-09-25 thomas.ad static const struct got_error *
109 ca6e02ac 2020-01-07 stsp open_commit(struct got_commit_object **commit, struct got_pack *pack,
110 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
111 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
113 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
114 cb5e38fd 2019-05-23 stsp struct got_object *obj = NULL;
115 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
116 cfd633c2 2018-09-10 stsp size_t len;
118 ca6e02ac 2020-01-07 stsp *commit = NULL;
120 ca6e02ac 2020-01-07 stsp obj = got_object_cache_get(objcache, id);
122 704b89c4 2019-05-23 stsp obj->refcnt++;
124 ca6e02ac 2020-01-07 stsp err = open_object(&obj, pack, packidx, obj_idx, id,
127 704b89c4 2019-05-23 stsp return err;
130 cfd633c2 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
134 cfd633c2 2018-09-10 stsp obj->size = len;
136 ca6e02ac 2020-01-07 stsp err = got_object_parse_commit(commit, buf, len);
138 ca6e02ac 2020-01-07 stsp got_object_close(obj);
140 ca6e02ac 2020-01-07 stsp return err;
143 ca6e02ac 2020-01-07 stsp static const struct got_error *
144 ca6e02ac 2020-01-07 stsp commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
145 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
147 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
148 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
149 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL;
150 ca6e02ac 2020-01-07 stsp struct got_object_id id;
151 ca6e02ac 2020-01-07 stsp size_t datalen;
153 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
154 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(iobj))
155 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
156 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
157 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
159 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
163 cfd633c2 2018-09-10 stsp err = got_privsep_send_commit(ibuf, commit);
165 cb5e38fd 2019-05-23 stsp if (commit)
166 cb5e38fd 2019-05-23 stsp got_object_commit_close(commit);
168 7762fe12 2018-11-05 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
169 7762fe12 2018-11-05 stsp err = NULL;
171 7762fe12 2018-11-05 stsp got_privsep_send_error(ibuf, err);
174 7762fe12 2018-11-05 stsp return err;
177 8f1c06eb 2021-09-25 thomas.ad static const struct got_error *
178 ca6e02ac 2020-01-07 stsp open_tree(uint8_t **buf, struct got_pathlist_head *entries, int *nentries,
179 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
180 ca6e02ac 2020-01-07 stsp struct got_object_id *id, struct got_object_cache *objcache)
182 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
183 ca6e02ac 2020-01-07 stsp struct got_object *obj = NULL;
184 ca6e02ac 2020-01-07 stsp size_t len;
186 ca6e02ac 2020-01-07 stsp *buf = NULL;
187 ca6e02ac 2020-01-07 stsp *nentries = 0;
189 ca6e02ac 2020-01-07 stsp obj = got_object_cache_get(objcache, id);
191 ca6e02ac 2020-01-07 stsp obj->refcnt++;
193 ca6e02ac 2020-01-07 stsp err = open_object(&obj, pack, packidx, obj_idx, id,
196 ca6e02ac 2020-01-07 stsp return err;
199 ca6e02ac 2020-01-07 stsp err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
203 ca6e02ac 2020-01-07 stsp obj->size = len;
205 ca6e02ac 2020-01-07 stsp err = got_object_parse_tree(entries, nentries, *buf, len);
207 ca6e02ac 2020-01-07 stsp got_object_close(obj);
209 ca6e02ac 2020-01-07 stsp free(*buf);
210 ca6e02ac 2020-01-07 stsp *buf = NULL;
212 ca6e02ac 2020-01-07 stsp return err;
215 7762fe12 2018-11-05 stsp static const struct got_error *
216 876c234b 2018-09-10 stsp tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
217 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
219 e7885405 2018-09-10 stsp const struct got_error *err = NULL;
220 13c729f7 2018-12-24 stsp struct got_imsg_packed_object iobj;
221 3022d272 2019-11-14 stsp struct got_pathlist_head entries;
222 3022d272 2019-11-14 stsp int nentries = 0;
223 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
224 13c729f7 2018-12-24 stsp struct got_object_id id;
225 13c729f7 2018-12-24 stsp size_t datalen;
227 3022d272 2019-11-14 stsp TAILQ_INIT(&entries);
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 3022d272 2019-11-14 stsp err = got_privsep_send_tree(ibuf, &entries, nentries);
241 b87b4170 2020-01-06 stsp got_object_parsed_tree_entries_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 3840f4c9 2018-09-12 stsp receive_file(FILE **f, struct imsgbuf *ibuf, int 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 3840f4c9 2018-09-12 stsp static const struct got_error *
291 3840f4c9 2018-09-12 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
292 3840f4c9 2018-09-12 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
294 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
295 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj;
296 3840f4c9 2018-09-12 stsp struct got_object *obj = NULL;
297 3840f4c9 2018-09-12 stsp FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
298 ebc55e2d 2018-12-24 stsp struct got_object_id id;
299 ebc55e2d 2018-12-24 stsp size_t datalen;
300 ac544f8c 2019-01-13 stsp uint64_t blob_size;
301 ac544f8c 2019-01-13 stsp uint8_t *buf = NULL;
303 ebc55e2d 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
304 ebc55e2d 2018-12-24 stsp if (datalen != sizeof(iobj))
305 ebc55e2d 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
306 ebc55e2d 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
307 ebc55e2d 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
309 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
311 704b89c4 2019-05-23 stsp obj->refcnt++;
313 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
316 704b89c4 2019-05-23 stsp return err;
319 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
322 3840f4c9 2018-09-12 stsp err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
325 3840f4c9 2018-09-12 stsp err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
329 ac544f8c 2019-01-13 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
330 42c69117 2019-11-10 stsp err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
334 ac544f8c 2019-01-13 stsp blob_size = obj->size;
336 ac544f8c 2019-01-13 stsp if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
337 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
338 ac544f8c 2019-01-13 stsp obj, pack);
340 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
341 ac544f8c 2019-01-13 stsp accumfile);
345 ac544f8c 2019-01-13 stsp err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
348 56b63ca4 2021-01-22 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
349 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
350 56b63ca4 2021-01-22 stsp if (basefile && fclose(basefile) == EOF && err == NULL)
351 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
352 56b63ca4 2021-01-22 stsp if (accumfile && fclose(accumfile) == EOF && err == NULL)
353 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
354 cb5e38fd 2019-05-23 stsp got_object_close(obj);
355 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
356 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
358 55da3778 2018-09-10 stsp return err;
361 876c234b 2018-09-10 stsp static const struct got_error *
362 f4a881ce 2018-11-17 stsp tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
363 f4a881ce 2018-11-17 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
365 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
366 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj;
367 f4a881ce 2018-11-17 stsp struct got_object *obj = NULL;
368 f4a881ce 2018-11-17 stsp struct got_tag_object *tag = NULL;
369 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
370 f4a881ce 2018-11-17 stsp size_t len;
371 268f7291 2018-12-24 stsp struct got_object_id id;
372 268f7291 2018-12-24 stsp size_t datalen;
374 268f7291 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
375 268f7291 2018-12-24 stsp if (datalen != sizeof(iobj))
376 268f7291 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
377 268f7291 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
378 268f7291 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
380 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
382 704b89c4 2019-05-23 stsp obj->refcnt++;
384 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
387 704b89c4 2019-05-23 stsp return err;
390 f4a881ce 2018-11-17 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
394 f4a881ce 2018-11-17 stsp obj->size = len;
395 f4a881ce 2018-11-17 stsp err = got_object_parse_tag(&tag, buf, len);
399 f4a881ce 2018-11-17 stsp err = got_privsep_send_tag(ibuf, tag);
402 cb5e38fd 2019-05-23 stsp got_object_close(obj);
404 cb5e38fd 2019-05-23 stsp got_object_tag_close(tag);
406 ca6e02ac 2020-01-07 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
407 ca6e02ac 2020-01-07 stsp err = NULL;
409 ca6e02ac 2020-01-07 stsp got_privsep_send_error(ibuf, err);
412 ca6e02ac 2020-01-07 stsp return err;
415 ca6e02ac 2020-01-07 stsp static struct got_parsed_tree_entry *
416 ca6e02ac 2020-01-07 stsp find_entry_by_name(struct got_pathlist_head *entries, int nentries,
417 ca6e02ac 2020-01-07 stsp const char *name, size_t len)
419 ca6e02ac 2020-01-07 stsp struct got_pathlist_entry *pe;
421 ca6e02ac 2020-01-07 stsp /* Note that tree entries are sorted in strncmp() order. */
422 ca6e02ac 2020-01-07 stsp TAILQ_FOREACH(pe, entries, entry) {
423 ca6e02ac 2020-01-07 stsp int cmp = strncmp(pe->path, name, len);
424 ca6e02ac 2020-01-07 stsp if (cmp < 0)
426 ca6e02ac 2020-01-07 stsp if (cmp > 0)
428 ca6e02ac 2020-01-07 stsp if (pe->path[len] == '\0')
429 ca6e02ac 2020-01-07 stsp return (struct got_parsed_tree_entry *)pe->data;
431 ca6e02ac 2020-01-07 stsp return NULL;
434 8f1c06eb 2021-09-25 thomas.ad static const struct got_error *
435 ca6e02ac 2020-01-07 stsp tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
436 ca6e02ac 2020-01-07 stsp struct got_pathlist_head *entries1, int *nentries1,
437 ca6e02ac 2020-01-07 stsp struct got_pathlist_head *entries2, int *nentries2,
438 ca6e02ac 2020-01-07 stsp const char *path, struct got_pack *pack, struct got_packidx *packidx,
439 ca6e02ac 2020-01-07 stsp struct imsgbuf *ibuf, struct got_object_cache *objcache)
441 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
442 ca6e02ac 2020-01-07 stsp struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
443 ca6e02ac 2020-01-07 stsp const char *seg, *s;
444 ca6e02ac 2020-01-07 stsp size_t seglen;
446 ca6e02ac 2020-01-07 stsp *changed = 0;
448 ca6e02ac 2020-01-07 stsp /* We not do support comparing the root path. */
449 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
450 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
453 61a7d79f 2020-02-29 stsp while (*s == '/')
456 ca6e02ac 2020-01-07 stsp seglen = 0;
457 ca6e02ac 2020-01-07 stsp while (*s) {
458 ca6e02ac 2020-01-07 stsp if (*s != '/') {
465 ca6e02ac 2020-01-07 stsp pte1 = find_entry_by_name(entries1, *nentries1, seg, seglen);
466 ca6e02ac 2020-01-07 stsp if (pte1 == NULL) {
467 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_NO_OBJ);
471 ca6e02ac 2020-01-07 stsp pte2 = find_entry_by_name(entries2, *nentries2, seg, seglen);
472 ca6e02ac 2020-01-07 stsp if (pte2 == NULL) {
473 ca6e02ac 2020-01-07 stsp *changed = 1;
477 ca6e02ac 2020-01-07 stsp if (pte1->mode != pte2->mode) {
478 ca6e02ac 2020-01-07 stsp *changed = 1;
482 ca6e02ac 2020-01-07 stsp if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
483 ca6e02ac 2020-01-07 stsp *changed = 0;
487 ca6e02ac 2020-01-07 stsp if (*s == '\0') { /* final path element */
488 ca6e02ac 2020-01-07 stsp *changed = 1;
492 ca6e02ac 2020-01-07 stsp seg = s + 1;
494 ca6e02ac 2020-01-07 stsp seglen = 0;
496 ca6e02ac 2020-01-07 stsp struct got_object_id id1, id2;
499 ded8fbb8 2020-04-19 stsp memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
500 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id1);
501 ca6e02ac 2020-01-07 stsp if (idx == -1) {
502 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id1);
505 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(entries1);
506 ca6e02ac 2020-01-07 stsp *nentries1 = 0;
507 ca6e02ac 2020-01-07 stsp free(*buf1);
508 ca6e02ac 2020-01-07 stsp *buf1 = NULL;
509 ca6e02ac 2020-01-07 stsp err = open_tree(buf1, entries1, nentries1, pack,
510 ca6e02ac 2020-01-07 stsp packidx, idx, &id1, objcache);
511 ca6e02ac 2020-01-07 stsp pte1 = NULL;
515 ded8fbb8 2020-04-19 stsp memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
516 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id2);
517 ca6e02ac 2020-01-07 stsp if (idx == -1) {
518 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id2);
521 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(entries2);
522 ca6e02ac 2020-01-07 stsp *nentries2 = 0;
523 ca6e02ac 2020-01-07 stsp free(*buf2);
524 ca6e02ac 2020-01-07 stsp *buf2 = NULL;
525 ca6e02ac 2020-01-07 stsp err = open_tree(buf2, entries2, nentries2, pack,
526 ca6e02ac 2020-01-07 stsp packidx, idx, &id2, objcache);
527 ca6e02ac 2020-01-07 stsp pte2 = NULL;
533 ca6e02ac 2020-01-07 stsp return err;
536 ca6e02ac 2020-01-07 stsp static const struct got_error *
537 e70bf110 2020-03-22 stsp send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
538 e70bf110 2020-03-22 stsp struct imsgbuf *ibuf)
540 e70bf110 2020-03-22 stsp const struct got_error *err;
541 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
544 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
545 e70bf110 2020-03-22 stsp sizeof(struct got_imsg_traversed_commits) +
546 e70bf110 2020-03-22 stsp ncommits * SHA1_DIGEST_LENGTH);
547 e70bf110 2020-03-22 stsp if (wbuf == NULL)
548 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
550 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
551 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
552 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
553 e70bf110 2020-03-22 stsp return err;
555 e70bf110 2020-03-22 stsp for (i = 0; i < ncommits; i++) {
556 e70bf110 2020-03-22 stsp struct got_object_id *id = &commit_ids[i];
557 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
558 e70bf110 2020-03-22 stsp err = got_error_from_errno(
559 e70bf110 2020-03-22 stsp "imsg_add TRAVERSED_COMMITS");
560 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
561 e70bf110 2020-03-22 stsp return err;
565 e70bf110 2020-03-22 stsp wbuf->fd = -1;
566 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
568 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
571 e70bf110 2020-03-22 stsp static const struct got_error *
572 e70bf110 2020-03-22 stsp send_commit_traversal_done(struct imsgbuf *ibuf)
574 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
575 e70bf110 2020-03-22 stsp NULL, 0) == -1)
576 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
578 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
582 e70bf110 2020-03-22 stsp static const struct got_error *
583 ca6e02ac 2020-01-07 stsp commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
584 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx,
585 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
587 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
588 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
589 ca6e02ac 2020-01-07 stsp struct got_object_qid *pid;
590 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL, *pcommit = NULL;
591 ca6e02ac 2020-01-07 stsp struct got_pathlist_head entries, pentries;
592 ca6e02ac 2020-01-07 stsp int nentries = 0, pnentries = 0;
593 ca6e02ac 2020-01-07 stsp struct got_object_id id;
594 ca6e02ac 2020-01-07 stsp size_t datalen, path_len;
595 ca6e02ac 2020-01-07 stsp char *path = NULL;
596 ca6e02ac 2020-01-07 stsp const int min_alloc = 64;
597 ca6e02ac 2020-01-07 stsp int changed = 0, ncommits = 0, nallocated = 0;
598 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_ids = NULL;
600 ca6e02ac 2020-01-07 stsp TAILQ_INIT(&entries);
601 ca6e02ac 2020-01-07 stsp TAILQ_INIT(&pentries);
603 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
604 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(iobj))
605 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
606 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
607 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
609 ca6e02ac 2020-01-07 stsp path_len = datalen - sizeof(iobj) - 1;
610 ca6e02ac 2020-01-07 stsp if (path_len < 0)
611 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
612 ca6e02ac 2020-01-07 stsp if (path_len > 0) {
613 ca6e02ac 2020-01-07 stsp path = imsg->data + sizeof(iobj);
614 ca6e02ac 2020-01-07 stsp if (path[path_len] != '\0')
615 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
618 ca6e02ac 2020-01-07 stsp nallocated = min_alloc;
619 ca6e02ac 2020-01-07 stsp commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
620 ca6e02ac 2020-01-07 stsp if (commit_ids == NULL)
621 ca6e02ac 2020-01-07 stsp return got_error_from_errno("reallocarray");
624 ca6e02ac 2020-01-07 stsp const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
627 ca6e02ac 2020-01-07 stsp if (sigint_received) {
628 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_CANCELLED);
632 ca6e02ac 2020-01-07 stsp if (commit == NULL) {
633 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx, &id);
634 ca6e02ac 2020-01-07 stsp if (idx == -1)
636 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx,
637 ca6e02ac 2020-01-07 stsp idx, &id, objcache);
639 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
641 ca6e02ac 2020-01-07 stsp err = NULL;
646 ca6e02ac 2020-01-07 stsp if (sizeof(struct got_imsg_traversed_commits) +
647 ca6e02ac 2020-01-07 stsp ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
648 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits,
652 ca6e02ac 2020-01-07 stsp ncommits = 0;
654 ca6e02ac 2020-01-07 stsp ncommits++;
655 ca6e02ac 2020-01-07 stsp if (ncommits > nallocated) {
656 ca6e02ac 2020-01-07 stsp struct got_object_id *new;
657 ca6e02ac 2020-01-07 stsp nallocated += min_alloc;
658 ca6e02ac 2020-01-07 stsp new = reallocarray(commit_ids, nallocated,
659 ca6e02ac 2020-01-07 stsp sizeof(*commit_ids));
660 ca6e02ac 2020-01-07 stsp if (new == NULL) {
661 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("reallocarray");
664 ca6e02ac 2020-01-07 stsp commit_ids = new;
666 ca6e02ac 2020-01-07 stsp memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
667 ca6e02ac 2020-01-07 stsp SHA1_DIGEST_LENGTH);
669 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(&commit->parent_ids);
670 ca6e02ac 2020-01-07 stsp if (pid == NULL)
673 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx, pid->id);
674 ca6e02ac 2020-01-07 stsp if (idx == -1)
677 ca6e02ac 2020-01-07 stsp err = open_commit(&pcommit, pack, packidx, idx, pid->id,
680 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
682 ca6e02ac 2020-01-07 stsp err = NULL;
686 ca6e02ac 2020-01-07 stsp if (path[0] == '/' && path[1] == '\0') {
687 ca6e02ac 2020-01-07 stsp if (got_object_id_cmp(pcommit->tree_id,
688 ca6e02ac 2020-01-07 stsp commit->tree_id) != 0) {
689 ca6e02ac 2020-01-07 stsp changed = 1;
694 ca6e02ac 2020-01-07 stsp uint8_t *buf = NULL, *pbuf = NULL;
696 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx,
697 ca6e02ac 2020-01-07 stsp commit->tree_id);
698 ca6e02ac 2020-01-07 stsp if (idx == -1)
700 ca6e02ac 2020-01-07 stsp pidx = got_packidx_get_object_idx(packidx,
701 ca6e02ac 2020-01-07 stsp pcommit->tree_id);
702 ca6e02ac 2020-01-07 stsp if (pidx == -1)
705 ca6e02ac 2020-01-07 stsp err = open_tree(&buf, &entries, &nentries, pack,
706 ca6e02ac 2020-01-07 stsp packidx, idx, commit->tree_id, objcache);
709 ca6e02ac 2020-01-07 stsp err = open_tree(&pbuf, &pentries, &pnentries, pack,
710 ca6e02ac 2020-01-07 stsp packidx, pidx, pcommit->tree_id, objcache);
716 ca6e02ac 2020-01-07 stsp err = tree_path_changed(&changed, &buf, &pbuf,
717 ca6e02ac 2020-01-07 stsp &entries, &nentries, &pentries, &pnentries, path,
718 ca6e02ac 2020-01-07 stsp pack, packidx, ibuf, objcache);
720 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&entries);
721 ca6e02ac 2020-01-07 stsp nentries = 0;
723 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&pentries);
724 ca6e02ac 2020-01-07 stsp pnentries = 0;
725 ca6e02ac 2020-01-07 stsp free(pbuf);
727 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
729 ca6e02ac 2020-01-07 stsp err = NULL;
734 ca6e02ac 2020-01-07 stsp if (!changed) {
735 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, pid->id->sha1, SHA1_DIGEST_LENGTH);
736 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
737 ca6e02ac 2020-01-07 stsp commit = pcommit;
738 ca6e02ac 2020-01-07 stsp pcommit = NULL;
740 ca6e02ac 2020-01-07 stsp } while (!changed);
742 ca6e02ac 2020-01-07 stsp if (ncommits > 0) {
743 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits, ibuf);
747 ca6e02ac 2020-01-07 stsp if (changed) {
748 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit(ibuf, commit);
753 e70bf110 2020-03-22 stsp err = send_commit_traversal_done(ibuf);
755 ca6e02ac 2020-01-07 stsp free(commit_ids);
756 ca6e02ac 2020-01-07 stsp if (commit)
757 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
758 ca6e02ac 2020-01-07 stsp if (pcommit)
759 ca6e02ac 2020-01-07 stsp got_object_commit_close(pcommit);
760 ca6e02ac 2020-01-07 stsp if (nentries != 0)
761 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&entries);
762 ca6e02ac 2020-01-07 stsp if (pnentries != 0)
763 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&pentries);
765 f4a881ce 2018-11-17 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
766 f4a881ce 2018-11-17 stsp err = NULL;
768 f4a881ce 2018-11-17 stsp got_privsep_send_error(ibuf, err);
771 f4a881ce 2018-11-17 stsp return err;
774 f4a881ce 2018-11-17 stsp static const struct got_error *
775 59d1e4a0 2021-03-10 stsp raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
776 59d1e4a0 2021-03-10 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
778 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
779 59d1e4a0 2021-03-10 stsp uint8_t *buf = NULL;
780 59d1e4a0 2021-03-10 stsp uint64_t size = 0;
781 59d1e4a0 2021-03-10 stsp FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
782 59d1e4a0 2021-03-10 stsp struct got_imsg_packed_object iobj;
783 59d1e4a0 2021-03-10 stsp struct got_object *obj;
784 59d1e4a0 2021-03-10 stsp struct got_object_id id;
785 59d1e4a0 2021-03-10 stsp size_t datalen;
787 59d1e4a0 2021-03-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
788 59d1e4a0 2021-03-10 stsp if (datalen != sizeof(iobj))
789 59d1e4a0 2021-03-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
790 59d1e4a0 2021-03-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
791 59d1e4a0 2021-03-10 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
793 59d1e4a0 2021-03-10 stsp obj = got_object_cache_get(objcache, &id);
795 59d1e4a0 2021-03-10 stsp obj->refcnt++;
797 59d1e4a0 2021-03-10 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
800 59d1e4a0 2021-03-10 stsp return err;
803 59d1e4a0 2021-03-10 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
805 59d1e4a0 2021-03-10 stsp return err;
806 59d1e4a0 2021-03-10 stsp err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
809 59d1e4a0 2021-03-10 stsp err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
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 if (basefile && fclose(basefile) == EOF && err == NULL)
835 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fclose");
836 59d1e4a0 2021-03-10 stsp if (accumfile && fclose(accumfile) == EOF && err == NULL)
837 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fclose");
838 59d1e4a0 2021-03-10 stsp got_object_close(obj);
839 59d1e4a0 2021-03-10 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
840 59d1e4a0 2021-03-10 stsp got_privsep_send_error(ibuf, err);
842 59d1e4a0 2021-03-10 stsp return err;
847 59d1e4a0 2021-03-10 stsp static const struct got_error *
848 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
850 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
851 876c234b 2018-09-10 stsp struct imsg imsg;
852 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
853 876c234b 2018-09-10 stsp size_t datalen;
854 876c234b 2018-09-10 stsp struct got_packidx *p;
856 876c234b 2018-09-10 stsp *packidx = NULL;
858 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
860 876c234b 2018-09-10 stsp return err;
862 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
863 876c234b 2018-09-10 stsp if (p == NULL) {
864 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
868 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
869 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
873 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
874 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
878 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
879 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
880 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
883 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
885 876c234b 2018-09-10 stsp p->len = ipackidx.len;
886 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
887 876c234b 2018-09-10 stsp if (p->fd == -1) {
888 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
891 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
892 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
896 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
897 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
898 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
899 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
901 c3564dfa 2021-07-15 stsp err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
904 876c234b 2018-09-10 stsp if (imsg.fd != -1)
905 876c234b 2018-09-10 stsp close(imsg.fd);
906 876c234b 2018-09-10 stsp got_packidx_close(p);
908 876c234b 2018-09-10 stsp *packidx = p;
909 876c234b 2018-09-10 stsp imsg_free(&imsg);
910 876c234b 2018-09-10 stsp return err;
913 876c234b 2018-09-10 stsp static const struct got_error *
914 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
916 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
917 876c234b 2018-09-10 stsp struct imsg imsg;
918 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
919 876c234b 2018-09-10 stsp size_t datalen;
920 876c234b 2018-09-10 stsp struct got_pack *pack;
922 876c234b 2018-09-10 stsp *packp = NULL;
924 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
926 876c234b 2018-09-10 stsp return err;
928 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
929 876c234b 2018-09-10 stsp if (pack == NULL) {
930 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
934 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
935 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
939 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
940 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
944 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
945 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
946 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
949 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
951 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
952 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
953 876c234b 2018-09-10 stsp if (pack->fd == -1) {
954 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
957 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
958 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
961 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
962 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
963 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
967 ab2f42e7 2019-11-10 stsp pack->delta_cache = got_delta_cache_alloc(100,
968 ab2f42e7 2019-11-10 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX);
969 ab2f42e7 2019-11-10 stsp if (pack->delta_cache == NULL) {
970 ab2f42e7 2019-11-10 stsp err = got_error_from_errno("got_delta_cache_alloc");
974 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
975 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
976 876c234b 2018-09-10 stsp pack->fd, 0);
977 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
978 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
982 876c234b 2018-09-10 stsp if (imsg.fd != -1)
983 876c234b 2018-09-10 stsp close(imsg.fd);
984 876c234b 2018-09-10 stsp free(pack);
986 876c234b 2018-09-10 stsp *packp = pack;
987 876c234b 2018-09-10 stsp imsg_free(&imsg);
988 876c234b 2018-09-10 stsp return err;
992 876c234b 2018-09-10 stsp main(int argc, char *argv[])
994 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
995 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
996 876c234b 2018-09-10 stsp struct imsg imsg;
997 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
998 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
999 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
1001 876c234b 2018-09-10 stsp //static int attached;
1002 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
1004 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
1006 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1008 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1009 c59b3346 2018-09-11 stsp if (err) {
1010 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_cache_init");
1011 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
1015 2ff12563 2018-09-15 stsp #ifndef PROFILE
1016 876c234b 2018-09-10 stsp /* revoke access to most system calls */
1017 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
1018 638f9024 2019-05-13 stsp err = got_error_from_errno("pledge");
1019 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1024 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
1025 876c234b 2018-09-10 stsp if (err) {
1026 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1030 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
1031 876c234b 2018-09-10 stsp if (err) {
1032 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1037 876c234b 2018-09-10 stsp imsg.fd = -1;
1039 99437157 2018-11-11 stsp if (sigint_received) {
1040 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
1044 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1045 876c234b 2018-09-10 stsp if (err) {
1046 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1047 876c234b 2018-09-10 stsp err = NULL;
1051 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1054 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
1055 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
1056 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
1057 c59b3346 2018-09-11 stsp &objcache);
1059 59d1e4a0 2021-03-10 stsp case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1060 59d1e4a0 2021-03-10 stsp err = raw_object_request(&imsg, &ibuf, pack, packidx,
1061 59d1e4a0 2021-03-10 stsp &objcache);
1063 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
1064 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
1065 7762fe12 2018-11-05 stsp &objcache);
1067 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
1068 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
1069 62d463ca 2020-10-20 naddy &objcache);
1071 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
1072 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
1073 62d463ca 2020-10-20 naddy &objcache);
1075 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG_REQUEST:
1076 f4a881ce 2018-11-17 stsp err = tag_request(&imsg, &ibuf, pack, packidx,
1077 62d463ca 2020-10-20 naddy &objcache);
1079 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1080 ca6e02ac 2020-01-07 stsp err = commit_traversal_request(&imsg, &ibuf, pack,
1081 ca6e02ac 2020-01-07 stsp packidx, &objcache);
1084 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1088 08578a35 2021-01-22 stsp if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
1089 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1090 876c234b 2018-09-10 stsp imsg_free(&imsg);
1095 c59b3346 2018-09-11 stsp if (packidx)
1096 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
1098 c59b3346 2018-09-11 stsp got_pack_close(pack);
1099 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
1100 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
1101 99437157 2018-11-11 stsp if (err) {
1102 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1103 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1104 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
1107 08578a35 2021-01-22 stsp if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
1108 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1109 876c234b 2018-09-10 stsp return err ? 1 : 0;