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 not do support comparing the root path. */
450 if (got_path_is_root_dir(path))
451 return got_error_path(path, GOT_ERR_BAD_PATH);
453 s = path;
454 while (*s == '/')
455 s++;
456 seg = s;
457 seglen = 0;
458 while (*s) {
459 if (*s != '/') {
460 s++;
461 seglen++;
462 if (*s)
463 continue;
466 pte1 = find_entry_by_name(entries1, *nentries1, seg, seglen);
467 if (pte1 == NULL) {
468 err = got_error(GOT_ERR_NO_OBJ);
469 break;
472 pte2 = find_entry_by_name(entries2, *nentries2, seg, seglen);
473 if (pte2 == NULL) {
474 *changed = 1;
475 break;
478 if (pte1->mode != pte2->mode) {
479 *changed = 1;
480 break;
483 if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
484 *changed = 0;
485 break;
488 if (*s == '\0') { /* final path element */
489 *changed = 1;
490 break;
493 seg = s + 1;
494 s++;
495 seglen = 0;
496 if (*s) {
497 struct got_object_id id1, id2;
498 int idx;
500 idx = got_packidx_get_object_idx_sha1(packidx,
501 pte1->id);
502 if (idx == -1) {
503 err = got_error(GOT_ERR_NO_OBJ);
504 break;
506 memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
507 got_object_parsed_tree_entries_free(entries1);
508 *nentries1 = 0;
509 free(*buf1);
510 *buf1 = NULL;
511 err = open_tree(buf1, entries1, nentries1, pack,
512 packidx, idx, &id1, objcache);
513 pte1 = NULL;
514 if (err)
515 break;
517 idx = got_packidx_get_object_idx_sha1(packidx,
518 pte2->id);
519 if (idx == -1) {
520 err = got_error(GOT_ERR_NO_OBJ);
521 break;
523 memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
524 got_object_parsed_tree_entries_free(entries2);
525 *nentries2 = 0;
526 free(*buf2);
527 *buf2 = NULL;
528 err = open_tree(buf2, entries2, nentries2, pack,
529 packidx, idx, &id2, objcache);
530 pte2 = NULL;
531 if (err)
532 break;
536 return err;
539 static const struct got_error *
540 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
541 struct imsgbuf *ibuf)
543 const struct got_error *err;
544 struct ibuf *wbuf;
545 int i;
547 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
548 sizeof(struct got_imsg_traversed_commits) +
549 ncommits * SHA1_DIGEST_LENGTH);
550 if (wbuf == NULL)
551 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
553 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
554 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
555 ibuf_free(wbuf);
556 return err;
558 for (i = 0; i < ncommits; i++) {
559 struct got_object_id *id = &commit_ids[i];
560 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
561 err = got_error_from_errno(
562 "imsg_add TRAVERSED_COMMITS");
563 ibuf_free(wbuf);
564 return err;
568 wbuf->fd = -1;
569 imsg_close(ibuf, wbuf);
571 return got_privsep_flush_imsg(ibuf);
574 static const struct got_error *
575 send_commit_traversal_done(struct imsgbuf *ibuf)
577 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
578 NULL, 0) == -1)
579 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
581 return got_privsep_flush_imsg(ibuf);
585 static const struct got_error *
586 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
587 struct got_pack *pack, struct got_packidx *packidx,
588 struct got_object_cache *objcache)
590 const struct got_error *err = NULL;
591 struct got_imsg_packed_object iobj;
592 struct got_object_qid *pid;
593 struct got_commit_object *commit = NULL, *pcommit = NULL;
594 struct got_pathlist_head entries, pentries;
595 int nentries = 0, pnentries = 0;
596 struct got_object_id id;
597 size_t datalen, path_len;
598 char *path = NULL;
599 const int min_alloc = 64;
600 int changed = 0, ncommits = 0, nallocated = 0;
601 struct got_object_id *commit_ids = NULL;
603 TAILQ_INIT(&entries);
604 TAILQ_INIT(&pentries);
606 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
607 if (datalen < sizeof(iobj))
608 return got_error(GOT_ERR_PRIVSEP_LEN);
609 memcpy(&iobj, imsg->data, sizeof(iobj));
610 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
612 path_len = datalen - sizeof(iobj) - 1;
613 if (path_len < 0)
614 return got_error(GOT_ERR_PRIVSEP_LEN);
615 if (path_len > 0) {
616 path = imsg->data + sizeof(iobj);
617 if (path[path_len] != '\0')
618 return got_error(GOT_ERR_PRIVSEP_LEN);
621 nallocated = min_alloc;
622 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
623 if (commit_ids == NULL)
624 return got_error_from_errno("reallocarray");
626 do {
627 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
628 int idx;
630 if (sigint_received) {
631 err = got_error(GOT_ERR_CANCELLED);
632 goto done;
635 if (commit == NULL) {
636 idx = got_packidx_get_object_idx(packidx, &id);
637 if (idx == -1)
638 break;
639 err = open_commit(&commit, pack, packidx,
640 idx, &id, objcache);
641 if (err) {
642 if (err->code != GOT_ERR_NO_OBJ)
643 goto done;
644 err = NULL;
645 break;
649 if (sizeof(struct got_imsg_traversed_commits) +
650 ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
651 err = send_traversed_commits(commit_ids, ncommits,
652 ibuf);
653 if (err)
654 goto done;
655 ncommits = 0;
657 ncommits++;
658 if (ncommits > nallocated) {
659 struct got_object_id *new;
660 nallocated += min_alloc;
661 new = reallocarray(commit_ids, nallocated,
662 sizeof(*commit_ids));
663 if (new == NULL) {
664 err = got_error_from_errno("reallocarray");
665 goto done;
667 commit_ids = new;
669 memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
670 SHA1_DIGEST_LENGTH);
672 pid = SIMPLEQ_FIRST(&commit->parent_ids);
673 if (pid == NULL)
674 break;
676 idx = got_packidx_get_object_idx(packidx, pid->id);
677 if (idx == -1)
678 break;
680 err = open_commit(&pcommit, pack, packidx, idx, pid->id,
681 objcache);
682 if (err) {
683 if (err->code != GOT_ERR_NO_OBJ)
684 goto done;
685 err = NULL;
686 break;
689 if (path[0] == '/' && path[1] == '\0') {
690 if (got_object_id_cmp(pcommit->tree_id,
691 commit->tree_id) != 0) {
692 changed = 1;
693 break;
695 } else {
696 int pidx;
697 uint8_t *buf = NULL, *pbuf = NULL;
699 idx = got_packidx_get_object_idx(packidx,
700 commit->tree_id);
701 if (idx == -1)
702 break;
703 pidx = got_packidx_get_object_idx(packidx,
704 pcommit->tree_id);
705 if (pidx == -1)
706 break;
708 err = open_tree(&buf, &entries, &nentries, pack,
709 packidx, idx, commit->tree_id, objcache);
710 if (err)
711 goto done;
712 err = open_tree(&pbuf, &pentries, &pnentries, pack,
713 packidx, pidx, pcommit->tree_id, objcache);
714 if (err) {
715 free(buf);
716 goto done;
719 err = tree_path_changed(&changed, &buf, &pbuf,
720 &entries, &nentries, &pentries, &pnentries, path,
721 pack, packidx, ibuf, objcache);
723 got_object_parsed_tree_entries_free(&entries);
724 nentries = 0;
725 free(buf);
726 got_object_parsed_tree_entries_free(&pentries);
727 pnentries = 0;
728 free(pbuf);
729 if (err) {
730 if (err->code != GOT_ERR_NO_OBJ)
731 goto done;
732 err = NULL;
733 break;
737 if (!changed) {
738 memcpy(id.sha1, pid->id->sha1, SHA1_DIGEST_LENGTH);
739 got_object_commit_close(commit);
740 commit = pcommit;
741 pcommit = NULL;
743 } while (!changed);
745 if (ncommits > 0) {
746 err = send_traversed_commits(commit_ids, ncommits, ibuf);
747 if (err)
748 goto done;
750 if (changed) {
751 err = got_privsep_send_commit(ibuf, commit);
752 if (err)
753 goto done;
756 err = send_commit_traversal_done(ibuf);
757 done:
758 free(commit_ids);
759 if (commit)
760 got_object_commit_close(commit);
761 if (pcommit)
762 got_object_commit_close(pcommit);
763 if (nentries != 0)
764 got_object_parsed_tree_entries_free(&entries);
765 if (pnentries != 0)
766 got_object_parsed_tree_entries_free(&pentries);
767 if (err) {
768 if (err->code == GOT_ERR_PRIVSEP_PIPE)
769 err = NULL;
770 else
771 got_privsep_send_error(ibuf, err);
774 return err;
777 static const struct got_error *
778 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
780 const struct got_error *err = NULL;
781 struct imsg imsg;
782 struct got_imsg_packidx ipackidx;
783 size_t datalen;
784 struct got_packidx *p;
786 *packidx = NULL;
788 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
789 if (err)
790 return err;
792 p = calloc(1, sizeof(*p));
793 if (p == NULL) {
794 err = got_error_from_errno("calloc");
795 goto done;
798 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
799 err = got_error(GOT_ERR_PRIVSEP_MSG);
800 goto done;
803 if (imsg.fd == -1) {
804 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
805 goto done;
808 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
809 if (datalen != sizeof(ipackidx)) {
810 err = got_error(GOT_ERR_PRIVSEP_LEN);
811 goto done;
813 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
815 p->len = ipackidx.len;
816 p->fd = dup(imsg.fd);
817 if (p->fd == -1) {
818 err = got_error_from_errno("dup");
819 goto done;
821 if (lseek(p->fd, 0, SEEK_SET) == -1) {
822 err = got_error_from_errno("lseek");
823 goto done;
826 #ifndef GOT_PACK_NO_MMAP
827 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
828 if (p->map == MAP_FAILED)
829 p->map = NULL; /* fall back to read(2) */
830 #endif
831 err = got_packidx_init_hdr(p, 1);
832 done:
833 if (err) {
834 if (imsg.fd != -1)
835 close(imsg.fd);
836 got_packidx_close(p);
837 } else
838 *packidx = p;
839 imsg_free(&imsg);
840 return err;
843 static const struct got_error *
844 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
846 const struct got_error *err = NULL;
847 struct imsg imsg;
848 struct got_imsg_pack ipack;
849 size_t datalen;
850 struct got_pack *pack;
852 *packp = NULL;
854 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
855 if (err)
856 return err;
858 pack = calloc(1, sizeof(*pack));
859 if (pack == NULL) {
860 err = got_error_from_errno("calloc");
861 goto done;
864 if (imsg.hdr.type != GOT_IMSG_PACK) {
865 err = got_error(GOT_ERR_PRIVSEP_MSG);
866 goto done;
869 if (imsg.fd == -1) {
870 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
871 goto done;
874 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
875 if (datalen != sizeof(ipack)) {
876 err = got_error(GOT_ERR_PRIVSEP_LEN);
877 goto done;
879 memcpy(&ipack, imsg.data, sizeof(ipack));
881 pack->filesize = ipack.filesize;
882 pack->fd = dup(imsg.fd);
883 if (pack->fd == -1) {
884 err = got_error_from_errno("dup");
885 goto done;
887 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
888 err = got_error_from_errno("lseek");
889 goto done;
891 pack->path_packfile = strdup(ipack.path_packfile);
892 if (pack->path_packfile == NULL) {
893 err = got_error_from_errno("strdup");
894 goto done;
897 pack->delta_cache = got_delta_cache_alloc(100,
898 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
899 if (pack->delta_cache == NULL) {
900 err = got_error_from_errno("got_delta_cache_alloc");
901 goto done;
904 #ifndef GOT_PACK_NO_MMAP
905 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
906 pack->fd, 0);
907 if (pack->map == MAP_FAILED)
908 pack->map = NULL; /* fall back to read(2) */
909 #endif
910 done:
911 if (err) {
912 if (imsg.fd != -1)
913 close(imsg.fd);
914 free(pack);
915 } else
916 *packp = pack;
917 imsg_free(&imsg);
918 return err;
921 int
922 main(int argc, char *argv[])
924 const struct got_error *err = NULL;
925 struct imsgbuf ibuf;
926 struct imsg imsg;
927 struct got_packidx *packidx = NULL;
928 struct got_pack *pack = NULL;
929 struct got_object_cache objcache;
931 //static int attached;
932 //while (!attached) sleep(1);
934 signal(SIGINT, catch_sigint);
936 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
938 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
939 if (err) {
940 err = got_error_from_errno("got_object_cache_init");
941 got_privsep_send_error(&ibuf, err);
942 return 1;
945 #ifndef PROFILE
946 /* revoke access to most system calls */
947 if (pledge("stdio recvfd", NULL) == -1) {
948 err = got_error_from_errno("pledge");
949 got_privsep_send_error(&ibuf, err);
950 return 1;
952 #endif
954 err = receive_packidx(&packidx, &ibuf);
955 if (err) {
956 got_privsep_send_error(&ibuf, err);
957 return 1;
960 err = receive_pack(&pack, &ibuf);
961 if (err) {
962 got_privsep_send_error(&ibuf, err);
963 return 1;
966 for (;;) {
967 imsg.fd = -1;
969 if (sigint_received) {
970 err = got_error(GOT_ERR_CANCELLED);
971 break;
974 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
975 if (err) {
976 if (err->code == GOT_ERR_PRIVSEP_PIPE)
977 err = NULL;
978 break;
981 if (imsg.hdr.type == GOT_IMSG_STOP)
982 break;
984 switch (imsg.hdr.type) {
985 case GOT_IMSG_PACKED_OBJECT_REQUEST:
986 err = object_request(&imsg, &ibuf, pack, packidx,
987 &objcache);
988 break;
989 case GOT_IMSG_COMMIT_REQUEST:
990 err = commit_request(&imsg, &ibuf, pack, packidx,
991 &objcache);
992 break;
993 case GOT_IMSG_TREE_REQUEST:
994 err = tree_request(&imsg, &ibuf, pack, packidx,
995 &objcache);
996 break;
997 case GOT_IMSG_BLOB_REQUEST:
998 err = blob_request(&imsg, &ibuf, pack, packidx,
999 &objcache);
1000 break;
1001 case GOT_IMSG_TAG_REQUEST:
1002 err = tag_request(&imsg, &ibuf, pack, packidx,
1003 &objcache);
1004 break;
1005 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1006 err = commit_traversal_request(&imsg, &ibuf, pack,
1007 packidx, &objcache);
1008 break;
1009 default:
1010 err = got_error(GOT_ERR_PRIVSEP_MSG);
1011 break;
1014 if (imsg.fd != -1 && close(imsg.fd) != 0 && err == NULL)
1015 err = got_error_from_errno("close");
1016 imsg_free(&imsg);
1017 if (err)
1018 break;
1021 if (packidx)
1022 got_packidx_close(packidx);
1023 if (pack)
1024 got_pack_close(pack);
1025 got_object_cache_close(&objcache);
1026 imsg_clear(&ibuf);
1027 if (err) {
1028 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1029 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1030 got_privsep_send_error(&ibuf, err);
1033 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
1034 err = got_error_from_errno("close");
1035 return err ? 1 : 0;