2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 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/syslimits.h>
22 876c234b 2018-09-10 stsp #include <sys/mman.h>
24 876c234b 2018-09-10 stsp #include <limits.h>
25 99437157 2018-11-11 stsp #include <signal.h>
26 876c234b 2018-09-10 stsp #include <stdint.h>
27 876c234b 2018-09-10 stsp #include <imsg.h>
28 876c234b 2018-09-10 stsp #include <stdio.h>
29 876c234b 2018-09-10 stsp #include <stdlib.h>
30 876c234b 2018-09-10 stsp #include <string.h>
31 876c234b 2018-09-10 stsp #include <sha1.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 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
44 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
46 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
49 99437157 2018-11-11 stsp catch_sigint(int signo)
51 99437157 2018-11-11 stsp sigint_received = 1;
54 876c234b 2018-09-10 stsp static const struct got_error *
55 704b89c4 2019-05-23 stsp open_object(struct got_object **obj, struct got_pack *pack,
56 704b89c4 2019-05-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id,
57 704b89c4 2019-05-23 stsp struct got_object_cache *objcache)
59 704b89c4 2019-05-23 stsp const struct got_error *err;
61 704b89c4 2019-05-23 stsp err = got_packfile_open_object(obj, pack, packidx, idx, id);
64 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
66 704b89c4 2019-05-23 stsp err = got_object_cache_add(objcache, id, *obj);
68 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
69 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
73 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
74 704b89c4 2019-05-23 stsp return NULL;
77 704b89c4 2019-05-23 stsp static const struct got_error *
78 876c234b 2018-09-10 stsp object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
79 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
81 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
82 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
83 876c234b 2018-09-10 stsp struct got_object *obj;
84 106807b4 2018-09-15 stsp struct got_object_id id;
85 876c234b 2018-09-10 stsp size_t datalen;
87 876c234b 2018-09-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
88 876c234b 2018-09-10 stsp if (datalen != sizeof(iobj))
89 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
90 876c234b 2018-09-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
91 106807b4 2018-09-15 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
93 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
95 704b89c4 2019-05-23 stsp obj->refcnt++;
97 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
103 876c234b 2018-09-10 stsp err = got_privsep_send_obj(ibuf, obj);
105 876c234b 2018-09-10 stsp got_object_close(obj);
106 876c234b 2018-09-10 stsp return err;
109 876c234b 2018-09-10 stsp static const struct got_error *
110 876c234b 2018-09-10 stsp commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
111 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
113 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
114 1785f84a 2018-12-23 stsp struct got_imsg_packed_object iobj;
115 cb5e38fd 2019-05-23 stsp struct got_object *obj = NULL;
116 cfd633c2 2018-09-10 stsp struct got_commit_object *commit = NULL;
117 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
118 cfd633c2 2018-09-10 stsp size_t len;
119 1785f84a 2018-12-23 stsp struct got_object_id id;
120 1785f84a 2018-12-23 stsp size_t datalen;
122 1785f84a 2018-12-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
123 1785f84a 2018-12-23 stsp if (datalen != sizeof(iobj))
124 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
125 1785f84a 2018-12-23 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
126 1785f84a 2018-12-23 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
128 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
130 704b89c4 2019-05-23 stsp obj->refcnt++;
132 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
135 704b89c4 2019-05-23 stsp return err;
138 cfd633c2 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
142 cfd633c2 2018-09-10 stsp obj->size = len;
143 cfd633c2 2018-09-10 stsp err = got_object_parse_commit(&commit, buf, len);
147 cfd633c2 2018-09-10 stsp err = got_privsep_send_commit(ibuf, commit);
150 cb5e38fd 2019-05-23 stsp got_object_close(obj);
151 cb5e38fd 2019-05-23 stsp if (commit)
152 cb5e38fd 2019-05-23 stsp got_object_commit_close(commit);
154 7762fe12 2018-11-05 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
155 7762fe12 2018-11-05 stsp err = NULL;
157 7762fe12 2018-11-05 stsp got_privsep_send_error(ibuf, err);
160 7762fe12 2018-11-05 stsp return err;
163 7762fe12 2018-11-05 stsp static const struct got_error *
164 876c234b 2018-09-10 stsp tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
165 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
167 e7885405 2018-09-10 stsp const struct got_error *err = NULL;
168 13c729f7 2018-12-24 stsp struct got_imsg_packed_object iobj;
169 e7885405 2018-09-10 stsp struct got_object *obj = NULL;
170 3022d272 2019-11-14 stsp struct got_pathlist_head entries;
171 3022d272 2019-11-14 stsp int nentries = 0;
172 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
173 e7885405 2018-09-10 stsp size_t len;
174 13c729f7 2018-12-24 stsp struct got_object_id id;
175 13c729f7 2018-12-24 stsp size_t datalen;
177 3022d272 2019-11-14 stsp TAILQ_INIT(&entries);
179 13c729f7 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
180 13c729f7 2018-12-24 stsp if (datalen != sizeof(iobj))
181 13c729f7 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
182 13c729f7 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
183 13c729f7 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
185 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
187 704b89c4 2019-05-23 stsp obj->refcnt++;
189 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
192 704b89c4 2019-05-23 stsp return err;
195 e7885405 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
199 e7885405 2018-09-10 stsp obj->size = len;
200 3022d272 2019-11-14 stsp err = got_object_parse_tree(&entries, &nentries, buf, len);
204 3022d272 2019-11-14 stsp err = got_privsep_send_tree(ibuf, &entries, nentries);
206 3022d272 2019-11-14 stsp got_pathlist_free(&entries);
208 cb5e38fd 2019-05-23 stsp got_object_close(obj);
210 e7885405 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
211 e7885405 2018-09-10 stsp err = NULL;
213 e7885405 2018-09-10 stsp got_privsep_send_error(ibuf, err);
216 e7885405 2018-09-10 stsp return err;
219 876c234b 2018-09-10 stsp static const struct got_error *
220 3840f4c9 2018-09-12 stsp receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
222 3840f4c9 2018-09-12 stsp const struct got_error *err;
223 3840f4c9 2018-09-12 stsp struct imsg imsg;
224 55da3778 2018-09-10 stsp size_t datalen;
226 3840f4c9 2018-09-12 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
228 55da3778 2018-09-10 stsp return err;
230 3840f4c9 2018-09-12 stsp if (imsg.hdr.type != imsg_code) {
231 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
235 3840f4c9 2018-09-12 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
236 55da3778 2018-09-10 stsp if (datalen != 0) {
237 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
240 3840f4c9 2018-09-12 stsp if (imsg.fd == -1) {
241 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
245 3840f4c9 2018-09-12 stsp *f = fdopen(imsg.fd, "w+");
246 3840f4c9 2018-09-12 stsp if (*f == NULL) {
247 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
248 3a6ce05a 2019-02-11 stsp close(imsg.fd);
252 3840f4c9 2018-09-12 stsp imsg_free(&imsg);
253 3840f4c9 2018-09-12 stsp return err;
256 3840f4c9 2018-09-12 stsp static const struct got_error *
257 3840f4c9 2018-09-12 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
258 3840f4c9 2018-09-12 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
260 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
261 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj;
262 3840f4c9 2018-09-12 stsp struct got_object *obj = NULL;
263 3840f4c9 2018-09-12 stsp FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
264 ebc55e2d 2018-12-24 stsp struct got_object_id id;
265 ebc55e2d 2018-12-24 stsp size_t datalen;
266 ac544f8c 2019-01-13 stsp uint64_t blob_size;
267 ac544f8c 2019-01-13 stsp uint8_t *buf = NULL;
269 ebc55e2d 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
270 ebc55e2d 2018-12-24 stsp if (datalen != sizeof(iobj))
271 ebc55e2d 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
272 ebc55e2d 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
273 ebc55e2d 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
275 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
277 704b89c4 2019-05-23 stsp obj->refcnt++;
279 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
282 704b89c4 2019-05-23 stsp return err;
285 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
288 3840f4c9 2018-09-12 stsp err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
291 3840f4c9 2018-09-12 stsp err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
295 ac544f8c 2019-01-13 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
296 42c69117 2019-11-10 stsp err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
300 ac544f8c 2019-01-13 stsp blob_size = obj->size;
302 ac544f8c 2019-01-13 stsp if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
303 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
304 ac544f8c 2019-01-13 stsp obj, pack);
306 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
307 ac544f8c 2019-01-13 stsp accumfile);
311 ac544f8c 2019-01-13 stsp err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
314 fb43ecf1 2019-02-11 stsp if (outfile && fclose(outfile) != 0 && err == NULL)
315 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
316 638f9024 2019-05-13 stsp if (basefile && fclose(basefile) != 0 && err == NULL)
317 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
318 fb43ecf1 2019-02-11 stsp if (accumfile && fclose(accumfile) != 0 && err == NULL)
319 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
320 cb5e38fd 2019-05-23 stsp got_object_close(obj);
321 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
322 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
324 55da3778 2018-09-10 stsp return err;
327 876c234b 2018-09-10 stsp static const struct got_error *
328 f4a881ce 2018-11-17 stsp tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
329 f4a881ce 2018-11-17 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
331 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
332 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj;
333 f4a881ce 2018-11-17 stsp struct got_object *obj = NULL;
334 f4a881ce 2018-11-17 stsp struct got_tag_object *tag = NULL;
335 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
336 f4a881ce 2018-11-17 stsp size_t len;
337 268f7291 2018-12-24 stsp struct got_object_id id;
338 268f7291 2018-12-24 stsp size_t datalen;
340 268f7291 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
341 268f7291 2018-12-24 stsp if (datalen != sizeof(iobj))
342 268f7291 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
343 268f7291 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
344 268f7291 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
346 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
348 704b89c4 2019-05-23 stsp obj->refcnt++;
350 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
353 704b89c4 2019-05-23 stsp return err;
356 f4a881ce 2018-11-17 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
360 f4a881ce 2018-11-17 stsp obj->size = len;
361 f4a881ce 2018-11-17 stsp err = got_object_parse_tag(&tag, buf, len);
365 f4a881ce 2018-11-17 stsp err = got_privsep_send_tag(ibuf, tag);
368 cb5e38fd 2019-05-23 stsp got_object_close(obj);
370 cb5e38fd 2019-05-23 stsp got_object_tag_close(tag);
372 f4a881ce 2018-11-17 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
373 f4a881ce 2018-11-17 stsp err = NULL;
375 f4a881ce 2018-11-17 stsp got_privsep_send_error(ibuf, err);
378 f4a881ce 2018-11-17 stsp return err;
381 f4a881ce 2018-11-17 stsp static const struct got_error *
382 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
384 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
385 876c234b 2018-09-10 stsp struct imsg imsg;
386 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
387 876c234b 2018-09-10 stsp size_t datalen;
388 876c234b 2018-09-10 stsp struct got_packidx *p;
390 876c234b 2018-09-10 stsp *packidx = NULL;
392 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
394 876c234b 2018-09-10 stsp return err;
396 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
397 876c234b 2018-09-10 stsp if (p == NULL) {
398 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
402 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
403 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
407 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
408 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
412 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
413 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
414 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
417 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
419 876c234b 2018-09-10 stsp p->len = ipackidx.len;
420 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
421 876c234b 2018-09-10 stsp if (p->fd == -1) {
422 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
425 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
426 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
430 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
431 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
432 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
433 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
435 876c234b 2018-09-10 stsp err = got_packidx_init_hdr(p, 1);
438 876c234b 2018-09-10 stsp if (imsg.fd != -1)
439 876c234b 2018-09-10 stsp close(imsg.fd);
440 876c234b 2018-09-10 stsp got_packidx_close(p);
442 876c234b 2018-09-10 stsp *packidx = p;
443 876c234b 2018-09-10 stsp imsg_free(&imsg);
444 876c234b 2018-09-10 stsp return err;
447 876c234b 2018-09-10 stsp static const struct got_error *
448 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
450 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
451 876c234b 2018-09-10 stsp struct imsg imsg;
452 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
453 876c234b 2018-09-10 stsp size_t datalen;
454 876c234b 2018-09-10 stsp struct got_pack *pack;
456 876c234b 2018-09-10 stsp *packp = NULL;
458 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
460 876c234b 2018-09-10 stsp return err;
462 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
463 876c234b 2018-09-10 stsp if (pack == NULL) {
464 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
468 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
469 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
473 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
474 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
478 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
479 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
480 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
483 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
485 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
486 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
487 876c234b 2018-09-10 stsp if (pack->fd == -1) {
488 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
491 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
492 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
495 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
496 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
497 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
501 ab2f42e7 2019-11-10 stsp pack->delta_cache = got_delta_cache_alloc(100,
502 ab2f42e7 2019-11-10 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX);
503 ab2f42e7 2019-11-10 stsp if (pack->delta_cache == NULL) {
504 ab2f42e7 2019-11-10 stsp err = got_error_from_errno("got_delta_cache_alloc");
508 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
509 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
510 876c234b 2018-09-10 stsp pack->fd, 0);
511 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
512 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
516 876c234b 2018-09-10 stsp if (imsg.fd != -1)
517 876c234b 2018-09-10 stsp close(imsg.fd);
518 876c234b 2018-09-10 stsp free(pack);
520 876c234b 2018-09-10 stsp *packp = pack;
521 876c234b 2018-09-10 stsp imsg_free(&imsg);
522 876c234b 2018-09-10 stsp return err;
526 876c234b 2018-09-10 stsp main(int argc, char *argv[])
528 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
529 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
530 876c234b 2018-09-10 stsp struct imsg imsg;
531 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
532 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
533 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
535 876c234b 2018-09-10 stsp //static int attached;
536 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
538 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
540 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
542 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
544 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_cache_init");
545 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
549 2ff12563 2018-09-15 stsp #ifndef PROFILE
550 876c234b 2018-09-10 stsp /* revoke access to most system calls */
551 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
552 638f9024 2019-05-13 stsp err = got_error_from_errno("pledge");
553 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
558 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
560 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
564 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
566 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
571 876c234b 2018-09-10 stsp imsg.fd = -1;
573 99437157 2018-11-11 stsp if (sigint_received) {
574 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
578 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
580 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
581 876c234b 2018-09-10 stsp err = NULL;
585 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
588 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
589 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
590 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
591 c59b3346 2018-09-11 stsp &objcache);
593 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
594 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
595 7762fe12 2018-11-05 stsp &objcache);
597 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
598 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
599 c59b3346 2018-09-11 stsp &objcache);
601 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
602 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
603 c59b3346 2018-09-11 stsp &objcache);
605 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG_REQUEST:
606 f4a881ce 2018-11-17 stsp err = tag_request(&imsg, &ibuf, pack, packidx,
607 f4a881ce 2018-11-17 stsp &objcache);
610 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
614 3a6ce05a 2019-02-11 stsp if (imsg.fd != -1 && close(imsg.fd) != 0 && err == NULL)
615 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
616 876c234b 2018-09-10 stsp imsg_free(&imsg);
621 c59b3346 2018-09-11 stsp if (packidx)
622 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
624 c59b3346 2018-09-11 stsp got_pack_close(pack);
625 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
626 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
628 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
629 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
630 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
633 3a6ce05a 2019-02-11 stsp if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
634 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
635 876c234b 2018-09-10 stsp return err ? 1 : 0;