Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/time.h>
21 #include <sys/syslimits.h>
22 #include <sys/mman.h>
24 #include <limits.h>
25 #include <signal.h>
26 #include <stdint.h>
27 #include <imsg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sha1.h>
32 #include <zlib.h>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_path.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_delta_cache.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_cache.h"
42 #include "got_lib_object_parse.h"
43 #include "got_lib_privsep.h"
44 #include "got_lib_pack.h"
46 static volatile sig_atomic_t sigint_received;
48 static void
49 catch_sigint(int signo)
50 {
51 sigint_received = 1;
52 }
54 static const struct got_error *
55 open_object(struct got_object **obj, struct got_pack *pack,
56 struct got_packidx *packidx, int idx, struct got_object_id *id,
57 struct got_object_cache *objcache)
58 {
59 const struct got_error *err;
61 err = got_packfile_open_object(obj, pack, packidx, idx, id);
62 if (err)
63 return err;
64 (*obj)->refcnt++;
66 err = got_object_cache_add(objcache, id, *obj);
67 if (err) {
68 if (err->code == GOT_ERR_OBJ_EXISTS ||
69 err->code == GOT_ERR_OBJ_TOO_LARGE)
70 err = NULL;
71 return err;
72 }
73 (*obj)->refcnt++;
74 return NULL;
75 }
77 static const struct got_error *
78 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
79 struct got_packidx *packidx, struct got_object_cache *objcache)
80 {
81 const struct got_error *err = NULL;
82 struct got_imsg_packed_object iobj;
83 struct got_object *obj;
84 struct got_object_id id;
85 size_t datalen;
87 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
88 if (datalen != sizeof(iobj))
89 return got_error(GOT_ERR_PRIVSEP_LEN);
90 memcpy(&iobj, imsg->data, sizeof(iobj));
91 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
93 obj = got_object_cache_get(objcache, &id);
94 if (obj) {
95 obj->refcnt++;
96 } else {
97 err = open_object(&obj, pack, packidx, iobj.idx, &id,
98 objcache);
99 if (err)
100 goto done;
103 err = got_privsep_send_obj(ibuf, obj);
104 done:
105 got_object_close(obj);
106 return err;
109 const struct got_error *
110 open_commit(struct got_commit_object **commit, struct got_pack *pack,
111 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
112 struct got_object_cache *objcache)
114 const struct got_error *err = NULL;
115 struct got_object *obj = NULL;
116 uint8_t *buf = NULL;
117 size_t len;
119 *commit = NULL;
121 obj = got_object_cache_get(objcache, id);
122 if (obj) {
123 obj->refcnt++;
124 } else {
125 err = open_object(&obj, pack, packidx, obj_idx, id,
126 objcache);
127 if (err)
128 return err;
131 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
132 if (err)
133 goto done;
135 obj->size = len;
137 err = got_object_parse_commit(commit, buf, len);
138 done:
139 got_object_close(obj);
140 free(buf);
141 return err;
144 static const struct got_error *
145 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
146 struct got_packidx *packidx, struct got_object_cache *objcache)
148 const struct got_error *err = NULL;
149 struct got_imsg_packed_object iobj;
150 struct got_commit_object *commit = NULL;
151 struct got_object_id id;
152 size_t datalen;
154 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
155 if (datalen != sizeof(iobj))
156 return got_error(GOT_ERR_PRIVSEP_LEN);
157 memcpy(&iobj, imsg->data, sizeof(iobj));
158 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
160 err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
161 if (err)
162 goto done;
164 err = got_privsep_send_commit(ibuf, commit);
165 done:
166 if (commit)
167 got_object_commit_close(commit);
168 if (err) {
169 if (err->code == GOT_ERR_PRIVSEP_PIPE)
170 err = NULL;
171 else
172 got_privsep_send_error(ibuf, err);
175 return err;
178 const struct got_error *
179 open_tree(uint8_t **buf, struct got_pathlist_head *entries, int *nentries,
180 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
181 struct got_object_id *id, struct got_object_cache *objcache)
183 const struct got_error *err = NULL;
184 struct got_object *obj = NULL;
185 size_t len;
187 *buf = NULL;
188 *nentries = 0;
190 obj = got_object_cache_get(objcache, id);
191 if (obj) {
192 obj->refcnt++;
193 } else {
194 err = open_object(&obj, pack, packidx, obj_idx, id,
195 objcache);
196 if (err)
197 return err;
200 err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
201 if (err)
202 goto done;
204 obj->size = len;
206 err = got_object_parse_tree(entries, nentries, *buf, len);
207 done:
208 got_object_close(obj);
209 if (err) {
210 free(*buf);
211 *buf = NULL;
213 return err;
216 static const struct got_error *
217 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
218 struct got_packidx *packidx, struct got_object_cache *objcache)
220 const struct got_error *err = NULL;
221 struct got_imsg_packed_object iobj;
222 struct got_pathlist_head entries;
223 int nentries = 0;
224 uint8_t *buf = NULL;
225 struct got_object_id id;
226 size_t datalen;
228 TAILQ_INIT(&entries);
230 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
231 if (datalen != sizeof(iobj))
232 return got_error(GOT_ERR_PRIVSEP_LEN);
233 memcpy(&iobj, imsg->data, sizeof(iobj));
234 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
236 err = open_tree(&buf, &entries, &nentries, pack, packidx, iobj.idx,
237 &id, objcache);
238 if (err)
239 return err;
241 err = got_privsep_send_tree(ibuf, &entries, nentries);
242 got_object_parsed_tree_entries_free(&entries);
243 free(buf);
244 if (err) {
245 if (err->code == GOT_ERR_PRIVSEP_PIPE)
246 err = NULL;
247 else
248 got_privsep_send_error(ibuf, err);
251 return err;
254 static const struct got_error *
255 receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
257 const struct got_error *err;
258 struct imsg imsg;
259 size_t datalen;
261 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
262 if (err)
263 return err;
265 if (imsg.hdr.type != imsg_code) {
266 err = got_error(GOT_ERR_PRIVSEP_MSG);
267 goto done;
270 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
271 if (datalen != 0) {
272 err = got_error(GOT_ERR_PRIVSEP_LEN);
273 goto done;
275 if (imsg.fd == -1) {
276 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
277 goto done;
280 *f = fdopen(imsg.fd, "w+");
281 if (*f == NULL) {
282 err = got_error_from_errno("fdopen");
283 close(imsg.fd);
284 goto done;
286 done:
287 imsg_free(&imsg);
288 return err;
291 static const struct got_error *
292 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
293 struct got_packidx *packidx, struct got_object_cache *objcache)
295 const struct got_error *err = NULL;
296 struct got_imsg_packed_object iobj;
297 struct got_object *obj = NULL;
298 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
299 struct got_object_id id;
300 size_t datalen;
301 uint64_t blob_size;
302 uint8_t *buf = NULL;
304 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
305 if (datalen != sizeof(iobj))
306 return got_error(GOT_ERR_PRIVSEP_LEN);
307 memcpy(&iobj, imsg->data, sizeof(iobj));
308 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
310 obj = got_object_cache_get(objcache, &id);
311 if (obj) {
312 obj->refcnt++;
313 } else {
314 err = open_object(&obj, pack, packidx, iobj.idx, &id,
315 objcache);
316 if (err)
317 return err;
320 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
321 if (err)
322 goto done;
323 err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
324 if (err)
325 goto done;
326 err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
327 if (err)
328 goto done;
330 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
331 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
332 if (err)
333 goto done;
334 } else
335 blob_size = obj->size;
337 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
338 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
339 obj, pack);
340 else
341 err = got_packfile_extract_object(pack, obj, outfile, basefile,
342 accumfile);
343 if (err)
344 goto done;
346 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
347 done:
348 free(buf);
349 if (outfile && fclose(outfile) != 0 && err == NULL)
350 err = got_error_from_errno("fclose");
351 if (basefile && fclose(basefile) != 0 && err == NULL)
352 err = got_error_from_errno("fclose");
353 if (accumfile && fclose(accumfile) != 0 && err == NULL)
354 err = got_error_from_errno("fclose");
355 got_object_close(obj);
356 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
357 got_privsep_send_error(ibuf, err);
359 return err;
362 static const struct got_error *
363 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
364 struct got_packidx *packidx, struct got_object_cache *objcache)
366 const struct got_error *err = NULL;
367 struct got_imsg_packed_object iobj;
368 struct got_object *obj = NULL;
369 struct got_tag_object *tag = NULL;
370 uint8_t *buf = NULL;
371 size_t len;
372 struct got_object_id id;
373 size_t datalen;
375 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
376 if (datalen != sizeof(iobj))
377 return got_error(GOT_ERR_PRIVSEP_LEN);
378 memcpy(&iobj, imsg->data, sizeof(iobj));
379 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
381 obj = got_object_cache_get(objcache, &id);
382 if (obj) {
383 obj->refcnt++;
384 } else {
385 err = open_object(&obj, pack, packidx, iobj.idx, &id,
386 objcache);
387 if (err)
388 return err;
391 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
392 if (err)
393 goto done;
395 obj->size = len;
396 err = got_object_parse_tag(&tag, buf, len);
397 if (err)
398 goto done;
400 err = got_privsep_send_tag(ibuf, tag);
401 done:
402 free(buf);
403 got_object_close(obj);
404 if (tag)
405 got_object_tag_close(tag);
406 if (err) {
407 if (err->code == GOT_ERR_PRIVSEP_PIPE)
408 err = NULL;
409 else
410 got_privsep_send_error(ibuf, err);
413 return err;
416 static struct got_parsed_tree_entry *
417 find_entry_by_name(struct got_pathlist_head *entries, int nentries,
418 const char *name, size_t len)
420 struct got_pathlist_entry *pe;
422 /* Note that tree entries are sorted in strncmp() order. */
423 TAILQ_FOREACH(pe, entries, entry) {
424 int cmp = strncmp(pe->path, name, len);
425 if (cmp < 0)
426 continue;
427 if (cmp > 0)
428 break;
429 if (pe->path[len] == '\0')
430 return (struct got_parsed_tree_entry *)pe->data;
432 return NULL;
435 const struct got_error *
436 tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
437 struct got_pathlist_head *entries1, int *nentries1,
438 struct got_pathlist_head *entries2, int *nentries2,
439 const char *path, struct got_pack *pack, struct got_packidx *packidx,
440 struct imsgbuf *ibuf, struct got_object_cache *objcache)
442 const struct got_error *err = NULL;
443 struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
444 const char *seg, *s;
445 size_t seglen;
447 *changed = 0;
449 /* We are expecting an absolute in-repository path. */
450 if (path[0] != '/')
451 return got_error(GOT_ERR_NOT_ABSPATH);
453 /* We not do support comparing the root path. */
454 if (path[1] == '\0')
455 return got_error(GOT_ERR_BAD_PATH);
457 s = path;
458 s++; /* skip leading '/' */
459 seg = s;
460 seglen = 0;
461 while (*s) {
462 if (*s != '/') {
463 s++;
464 seglen++;
465 if (*s)
466 continue;
469 pte1 = find_entry_by_name(entries1, *nentries1, seg, seglen);
470 if (pte1 == NULL) {
471 err = got_error(GOT_ERR_NO_OBJ);
472 break;
475 pte2 = find_entry_by_name(entries2, *nentries2, seg, seglen);
476 if (pte2 == NULL) {
477 *changed = 1;
478 break;
481 if (pte1->mode != pte2->mode) {
482 *changed = 1;
483 break;
486 if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
487 *changed = 0;
488 break;
491 if (*s == '\0') { /* final path element */
492 *changed = 1;
493 break;
496 seg = s + 1;
497 s++;
498 seglen = 0;
499 if (*s) {
500 struct got_object_id id1, id2;
501 int idx;
503 idx = got_packidx_get_object_idx_sha1(packidx,
504 pte1->id);
505 if (idx == -1) {
506 err = got_error(GOT_ERR_NO_OBJ);
507 break;
509 memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
510 got_object_parsed_tree_entries_free(entries1);
511 *nentries1 = 0;
512 free(*buf1);
513 *buf1 = NULL;
514 err = open_tree(buf1, entries1, nentries1, pack,
515 packidx, idx, &id1, objcache);
516 pte1 = NULL;
517 if (err)
518 break;
520 idx = got_packidx_get_object_idx_sha1(packidx,
521 pte2->id);
522 if (idx == -1) {
523 err = got_error(GOT_ERR_NO_OBJ);
524 break;
526 memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
527 got_object_parsed_tree_entries_free(entries2);
528 *nentries2 = 0;
529 free(*buf2);
530 *buf2 = NULL;
531 err = open_tree(buf2, entries2, nentries2, pack,
532 packidx, idx, &id2, objcache);
533 pte2 = NULL;
534 if (err)
535 break;
539 return err;
542 static const struct got_error *
543 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
544 struct got_pack *pack, struct got_packidx *packidx,
545 struct got_object_cache *objcache)
547 const struct got_error *err = NULL;
548 struct got_imsg_packed_object iobj;
549 struct got_object_qid *pid;
550 struct got_commit_object *commit = NULL, *pcommit = NULL;
551 struct got_pathlist_head entries, pentries;
552 int nentries = 0, pnentries = 0;
553 struct got_object_id id;
554 size_t datalen, path_len;
555 char *path = NULL;
556 const int min_alloc = 64;
557 int changed = 0, ncommits = 0, nallocated = 0;
558 struct got_object_id *commit_ids = NULL;
560 TAILQ_INIT(&entries);
561 TAILQ_INIT(&pentries);
563 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
564 if (datalen < sizeof(iobj))
565 return got_error(GOT_ERR_PRIVSEP_LEN);
566 memcpy(&iobj, imsg->data, sizeof(iobj));
567 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
569 path_len = datalen - sizeof(iobj) - 1;
570 if (path_len < 0)
571 return got_error(GOT_ERR_PRIVSEP_LEN);
572 if (path_len > 0) {
573 path = imsg->data + sizeof(iobj);
574 if (path[path_len] != '\0')
575 return got_error(GOT_ERR_PRIVSEP_LEN);
578 nallocated = min_alloc;
579 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
580 if (commit_ids == NULL)
581 return got_error_from_errno("reallocarray");
583 do {
584 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
585 int idx;
587 if (sigint_received) {
588 err = got_error(GOT_ERR_CANCELLED);
589 goto done;
592 if (commit == NULL) {
593 idx = got_packidx_get_object_idx(packidx, &id);
594 if (idx == -1)
595 break;
596 err = open_commit(&commit, pack, packidx,
597 idx, &id, objcache);
598 if (err) {
599 if (err->code != GOT_ERR_NO_OBJ)
600 goto done;
601 err = NULL;
602 break;
606 if (sizeof(struct got_imsg_traversed_commits) +
607 ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
608 err = got_privsep_send_traversed_commits(commit_ids,
609 ncommits, ibuf);
610 if (err)
611 goto done;
612 ncommits = 0;
614 ncommits++;
615 if (ncommits > nallocated) {
616 struct got_object_id *new;
617 nallocated += min_alloc;
618 new = reallocarray(commit_ids, nallocated,
619 sizeof(*commit_ids));
620 if (new == NULL) {
621 err = got_error_from_errno("reallocarray");
622 goto done;
624 commit_ids = new;
626 memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
627 SHA1_DIGEST_LENGTH);
629 pid = SIMPLEQ_FIRST(&commit->parent_ids);
630 if (pid == NULL)
631 break;
633 idx = got_packidx_get_object_idx(packidx, pid->id);
634 if (idx == -1)
635 break;
637 err = open_commit(&pcommit, pack, packidx, idx, pid->id,
638 objcache);
639 if (err) {
640 if (err->code != GOT_ERR_NO_OBJ)
641 goto done;
642 err = NULL;
643 break;
646 if (path[0] == '/' && path[1] == '\0') {
647 if (got_object_id_cmp(pcommit->tree_id,
648 commit->tree_id) != 0) {
649 changed = 1;
650 break;
652 } else {
653 int pidx;
654 uint8_t *buf = NULL, *pbuf = NULL;
656 idx = got_packidx_get_object_idx(packidx,
657 commit->tree_id);
658 if (idx == -1)
659 break;
660 pidx = got_packidx_get_object_idx(packidx,
661 pcommit->tree_id);
662 if (pidx == -1)
663 break;
665 err = open_tree(&buf, &entries, &nentries, pack,
666 packidx, idx, commit->tree_id, objcache);
667 if (err)
668 goto done;
669 err = open_tree(&pbuf, &pentries, &pnentries, pack,
670 packidx, pidx, pcommit->tree_id, objcache);
671 if (err) {
672 free(buf);
673 goto done;
676 err = tree_path_changed(&changed, &buf, &pbuf,
677 &entries, &nentries, &pentries, &pnentries, path,
678 pack, packidx, ibuf, objcache);
680 got_object_parsed_tree_entries_free(&entries);
681 nentries = 0;
682 free(buf);
683 got_object_parsed_tree_entries_free(&pentries);
684 pnentries = 0;
685 free(pbuf);
686 if (err) {
687 if (err->code != GOT_ERR_NO_OBJ)
688 goto done;
689 err = NULL;
690 break;
694 if (!changed) {
695 memcpy(id.sha1, pid->id->sha1, SHA1_DIGEST_LENGTH);
696 got_object_commit_close(commit);
697 commit = pcommit;
698 pcommit = NULL;
700 } while (!changed);
702 if (ncommits > 0) {
703 err = got_privsep_send_traversed_commits(commit_ids,
704 ncommits, ibuf);
705 if (err)
706 goto done;
708 if (changed) {
709 err = got_privsep_send_commit(ibuf, commit);
710 if (err)
711 goto done;
714 err = got_privsep_send_commit_traversal_done(ibuf);
715 done:
716 free(commit_ids);
717 if (commit)
718 got_object_commit_close(commit);
719 if (pcommit)
720 got_object_commit_close(pcommit);
721 if (nentries != 0)
722 got_object_parsed_tree_entries_free(&entries);
723 if (pnentries != 0)
724 got_object_parsed_tree_entries_free(&pentries);
725 if (err) {
726 if (err->code == GOT_ERR_PRIVSEP_PIPE)
727 err = NULL;
728 else
729 got_privsep_send_error(ibuf, err);
732 return err;
735 static const struct got_error *
736 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
738 const struct got_error *err = NULL;
739 struct imsg imsg;
740 struct got_imsg_packidx ipackidx;
741 size_t datalen;
742 struct got_packidx *p;
744 *packidx = NULL;
746 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
747 if (err)
748 return err;
750 p = calloc(1, sizeof(*p));
751 if (p == NULL) {
752 err = got_error_from_errno("calloc");
753 goto done;
756 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
757 err = got_error(GOT_ERR_PRIVSEP_MSG);
758 goto done;
761 if (imsg.fd == -1) {
762 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
763 goto done;
766 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
767 if (datalen != sizeof(ipackidx)) {
768 err = got_error(GOT_ERR_PRIVSEP_LEN);
769 goto done;
771 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
773 p->len = ipackidx.len;
774 p->fd = dup(imsg.fd);
775 if (p->fd == -1) {
776 err = got_error_from_errno("dup");
777 goto done;
779 if (lseek(p->fd, 0, SEEK_SET) == -1) {
780 err = got_error_from_errno("lseek");
781 goto done;
784 #ifndef GOT_PACK_NO_MMAP
785 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
786 if (p->map == MAP_FAILED)
787 p->map = NULL; /* fall back to read(2) */
788 #endif
789 err = got_packidx_init_hdr(p, 1);
790 done:
791 if (err) {
792 if (imsg.fd != -1)
793 close(imsg.fd);
794 got_packidx_close(p);
795 } else
796 *packidx = p;
797 imsg_free(&imsg);
798 return err;
801 static const struct got_error *
802 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
804 const struct got_error *err = NULL;
805 struct imsg imsg;
806 struct got_imsg_pack ipack;
807 size_t datalen;
808 struct got_pack *pack;
810 *packp = NULL;
812 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
813 if (err)
814 return err;
816 pack = calloc(1, sizeof(*pack));
817 if (pack == NULL) {
818 err = got_error_from_errno("calloc");
819 goto done;
822 if (imsg.hdr.type != GOT_IMSG_PACK) {
823 err = got_error(GOT_ERR_PRIVSEP_MSG);
824 goto done;
827 if (imsg.fd == -1) {
828 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
829 goto done;
832 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
833 if (datalen != sizeof(ipack)) {
834 err = got_error(GOT_ERR_PRIVSEP_LEN);
835 goto done;
837 memcpy(&ipack, imsg.data, sizeof(ipack));
839 pack->filesize = ipack.filesize;
840 pack->fd = dup(imsg.fd);
841 if (pack->fd == -1) {
842 err = got_error_from_errno("dup");
843 goto done;
845 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
846 err = got_error_from_errno("lseek");
847 goto done;
849 pack->path_packfile = strdup(ipack.path_packfile);
850 if (pack->path_packfile == NULL) {
851 err = got_error_from_errno("strdup");
852 goto done;
855 pack->delta_cache = got_delta_cache_alloc(100,
856 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
857 if (pack->delta_cache == NULL) {
858 err = got_error_from_errno("got_delta_cache_alloc");
859 goto done;
862 #ifndef GOT_PACK_NO_MMAP
863 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
864 pack->fd, 0);
865 if (pack->map == MAP_FAILED)
866 pack->map = NULL; /* fall back to read(2) */
867 #endif
868 done:
869 if (err) {
870 if (imsg.fd != -1)
871 close(imsg.fd);
872 free(pack);
873 } else
874 *packp = pack;
875 imsg_free(&imsg);
876 return err;
879 int
880 main(int argc, char *argv[])
882 const struct got_error *err = NULL;
883 struct imsgbuf ibuf;
884 struct imsg imsg;
885 struct got_packidx *packidx = NULL;
886 struct got_pack *pack = NULL;
887 struct got_object_cache objcache;
889 //static int attached;
890 //while (!attached) sleep(1);
892 signal(SIGINT, catch_sigint);
894 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
896 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
897 if (err) {
898 err = got_error_from_errno("got_object_cache_init");
899 got_privsep_send_error(&ibuf, err);
900 return 1;
903 #ifndef PROFILE
904 /* revoke access to most system calls */
905 if (pledge("stdio recvfd", NULL) == -1) {
906 err = got_error_from_errno("pledge");
907 got_privsep_send_error(&ibuf, err);
908 return 1;
910 #endif
912 err = receive_packidx(&packidx, &ibuf);
913 if (err) {
914 got_privsep_send_error(&ibuf, err);
915 return 1;
918 err = receive_pack(&pack, &ibuf);
919 if (err) {
920 got_privsep_send_error(&ibuf, err);
921 return 1;
924 for (;;) {
925 imsg.fd = -1;
927 if (sigint_received) {
928 err = got_error(GOT_ERR_CANCELLED);
929 break;
932 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
933 if (err) {
934 if (err->code == GOT_ERR_PRIVSEP_PIPE)
935 err = NULL;
936 break;
939 if (imsg.hdr.type == GOT_IMSG_STOP)
940 break;
942 switch (imsg.hdr.type) {
943 case GOT_IMSG_PACKED_OBJECT_REQUEST:
944 err = object_request(&imsg, &ibuf, pack, packidx,
945 &objcache);
946 break;
947 case GOT_IMSG_COMMIT_REQUEST:
948 err = commit_request(&imsg, &ibuf, pack, packidx,
949 &objcache);
950 break;
951 case GOT_IMSG_TREE_REQUEST:
952 err = tree_request(&imsg, &ibuf, pack, packidx,
953 &objcache);
954 break;
955 case GOT_IMSG_BLOB_REQUEST:
956 err = blob_request(&imsg, &ibuf, pack, packidx,
957 &objcache);
958 break;
959 case GOT_IMSG_TAG_REQUEST:
960 err = tag_request(&imsg, &ibuf, pack, packidx,
961 &objcache);
962 break;
963 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
964 err = commit_traversal_request(&imsg, &ibuf, pack,
965 packidx, &objcache);
966 break;
967 default:
968 err = got_error(GOT_ERR_PRIVSEP_MSG);
969 break;
972 if (imsg.fd != -1 && close(imsg.fd) != 0 && err == NULL)
973 err = got_error_from_errno("close");
974 imsg_free(&imsg);
975 if (err)
976 break;
979 if (packidx)
980 got_packidx_close(packidx);
981 if (pack)
982 got_pack_close(pack);
983 got_object_cache_close(&objcache);
984 imsg_clear(&ibuf);
985 if (err) {
986 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
987 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
988 got_privsep_send_error(&ibuf, err);
991 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
992 err = got_error_from_errno("close");
993 return err ? 1 : 0;