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 <unistd.h>
33 #include <zlib.h>
35 #include "got_error.h"
36 #include "got_object.h"
37 #include "got_path.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_delta_cache.h"
41 #include "got_lib_object.h"
42 #include "got_lib_object_cache.h"
43 #include "got_lib_object_parse.h"
44 #include "got_lib_privsep.h"
45 #include "got_lib_pack.h"
47 static volatile sig_atomic_t sigint_received;
49 static void
50 catch_sigint(int signo)
51 {
52 sigint_received = 1;
53 }
55 static const struct got_error *
56 open_object(struct got_object **obj, struct got_pack *pack,
57 struct got_packidx *packidx, int idx, struct got_object_id *id,
58 struct got_object_cache *objcache)
59 {
60 const struct got_error *err;
62 err = got_packfile_open_object(obj, pack, packidx, idx, id);
63 if (err)
64 return err;
65 (*obj)->refcnt++;
67 err = got_object_cache_add(objcache, id, *obj);
68 if (err) {
69 if (err->code == GOT_ERR_OBJ_EXISTS ||
70 err->code == GOT_ERR_OBJ_TOO_LARGE)
71 err = NULL;
72 return err;
73 }
74 (*obj)->refcnt++;
75 return NULL;
76 }
78 static const struct got_error *
79 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
80 struct got_packidx *packidx, struct got_object_cache *objcache)
81 {
82 const struct got_error *err = NULL;
83 struct got_imsg_packed_object iobj;
84 struct got_object *obj;
85 struct got_object_id id;
86 size_t datalen;
88 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
89 if (datalen != sizeof(iobj))
90 return got_error(GOT_ERR_PRIVSEP_LEN);
91 memcpy(&iobj, imsg->data, sizeof(iobj));
92 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
94 obj = got_object_cache_get(objcache, &id);
95 if (obj) {
96 obj->refcnt++;
97 } else {
98 err = open_object(&obj, pack, packidx, iobj.idx, &id,
99 objcache);
100 if (err)
101 goto done;
104 err = got_privsep_send_obj(ibuf, obj);
105 done:
106 got_object_close(obj);
107 return err;
110 const struct got_error *
111 open_commit(struct got_commit_object **commit, struct got_pack *pack,
112 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
113 struct got_object_cache *objcache)
115 const struct got_error *err = NULL;
116 struct got_object *obj = NULL;
117 uint8_t *buf = NULL;
118 size_t len;
120 *commit = NULL;
122 obj = got_object_cache_get(objcache, id);
123 if (obj) {
124 obj->refcnt++;
125 } else {
126 err = open_object(&obj, pack, packidx, obj_idx, id,
127 objcache);
128 if (err)
129 return err;
132 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
133 if (err)
134 goto done;
136 obj->size = len;
138 err = got_object_parse_commit(commit, buf, len);
139 done:
140 got_object_close(obj);
141 free(buf);
142 return err;
145 static const struct got_error *
146 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
147 struct got_packidx *packidx, struct got_object_cache *objcache)
149 const struct got_error *err = NULL;
150 struct got_imsg_packed_object iobj;
151 struct got_commit_object *commit = NULL;
152 struct got_object_id id;
153 size_t datalen;
155 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
156 if (datalen != sizeof(iobj))
157 return got_error(GOT_ERR_PRIVSEP_LEN);
158 memcpy(&iobj, imsg->data, sizeof(iobj));
159 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
161 err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
162 if (err)
163 goto done;
165 err = got_privsep_send_commit(ibuf, commit);
166 done:
167 if (commit)
168 got_object_commit_close(commit);
169 if (err) {
170 if (err->code == GOT_ERR_PRIVSEP_PIPE)
171 err = NULL;
172 else
173 got_privsep_send_error(ibuf, err);
176 return err;
179 const struct got_error *
180 open_tree(uint8_t **buf, struct got_pathlist_head *entries, int *nentries,
181 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
182 struct got_object_id *id, struct got_object_cache *objcache)
184 const struct got_error *err = NULL;
185 struct got_object *obj = NULL;
186 size_t len;
188 *buf = NULL;
189 *nentries = 0;
191 obj = got_object_cache_get(objcache, id);
192 if (obj) {
193 obj->refcnt++;
194 } else {
195 err = open_object(&obj, pack, packidx, obj_idx, id,
196 objcache);
197 if (err)
198 return err;
201 err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
202 if (err)
203 goto done;
205 obj->size = len;
207 err = got_object_parse_tree(entries, nentries, *buf, len);
208 done:
209 got_object_close(obj);
210 if (err) {
211 free(*buf);
212 *buf = NULL;
214 return err;
217 static const struct got_error *
218 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
219 struct got_packidx *packidx, struct got_object_cache *objcache)
221 const struct got_error *err = NULL;
222 struct got_imsg_packed_object iobj;
223 struct got_pathlist_head entries;
224 int nentries = 0;
225 uint8_t *buf = NULL;
226 struct got_object_id id;
227 size_t datalen;
229 TAILQ_INIT(&entries);
231 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
232 if (datalen != sizeof(iobj))
233 return got_error(GOT_ERR_PRIVSEP_LEN);
234 memcpy(&iobj, imsg->data, sizeof(iobj));
235 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
237 err = open_tree(&buf, &entries, &nentries, pack, packidx, iobj.idx,
238 &id, objcache);
239 if (err)
240 return err;
242 err = got_privsep_send_tree(ibuf, &entries, nentries);
243 got_object_parsed_tree_entries_free(&entries);
244 free(buf);
245 if (err) {
246 if (err->code == GOT_ERR_PRIVSEP_PIPE)
247 err = NULL;
248 else
249 got_privsep_send_error(ibuf, err);
252 return err;
255 static const struct got_error *
256 receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
258 const struct got_error *err;
259 struct imsg imsg;
260 size_t datalen;
262 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
263 if (err)
264 return err;
266 if (imsg.hdr.type != imsg_code) {
267 err = got_error(GOT_ERR_PRIVSEP_MSG);
268 goto done;
271 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
272 if (datalen != 0) {
273 err = got_error(GOT_ERR_PRIVSEP_LEN);
274 goto done;
276 if (imsg.fd == -1) {
277 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
278 goto done;
281 *f = fdopen(imsg.fd, "w+");
282 if (*f == NULL) {
283 err = got_error_from_errno("fdopen");
284 close(imsg.fd);
285 goto done;
287 done:
288 imsg_free(&imsg);
289 return err;
292 static const struct got_error *
293 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
294 struct got_packidx *packidx, struct got_object_cache *objcache)
296 const struct got_error *err = NULL;
297 struct got_imsg_packed_object iobj;
298 struct got_object *obj = NULL;
299 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
300 struct got_object_id id;
301 size_t datalen;
302 uint64_t blob_size;
303 uint8_t *buf = NULL;
305 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
306 if (datalen != sizeof(iobj))
307 return got_error(GOT_ERR_PRIVSEP_LEN);
308 memcpy(&iobj, imsg->data, sizeof(iobj));
309 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
311 obj = got_object_cache_get(objcache, &id);
312 if (obj) {
313 obj->refcnt++;
314 } else {
315 err = open_object(&obj, pack, packidx, iobj.idx, &id,
316 objcache);
317 if (err)
318 return err;
321 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
322 if (err)
323 goto done;
324 err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
325 if (err)
326 goto done;
327 err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
328 if (err)
329 goto done;
331 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
332 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
333 if (err)
334 goto done;
335 } else
336 blob_size = obj->size;
338 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
339 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
340 obj, pack);
341 else
342 err = got_packfile_extract_object(pack, obj, outfile, basefile,
343 accumfile);
344 if (err)
345 goto done;
347 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
348 done:
349 free(buf);
350 if (outfile && fclose(outfile) != 0 && err == NULL)
351 err = got_error_from_errno("fclose");
352 if (basefile && fclose(basefile) != 0 && err == NULL)
353 err = got_error_from_errno("fclose");
354 if (accumfile && fclose(accumfile) != 0 && err == NULL)
355 err = got_error_from_errno("fclose");
356 got_object_close(obj);
357 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
358 got_privsep_send_error(ibuf, err);
360 return err;
363 static const struct got_error *
364 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
365 struct got_packidx *packidx, struct got_object_cache *objcache)
367 const struct got_error *err = NULL;
368 struct got_imsg_packed_object iobj;
369 struct got_object *obj = NULL;
370 struct got_tag_object *tag = NULL;
371 uint8_t *buf = NULL;
372 size_t len;
373 struct got_object_id id;
374 size_t datalen;
376 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
377 if (datalen != sizeof(iobj))
378 return got_error(GOT_ERR_PRIVSEP_LEN);
379 memcpy(&iobj, imsg->data, sizeof(iobj));
380 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
382 obj = got_object_cache_get(objcache, &id);
383 if (obj) {
384 obj->refcnt++;
385 } else {
386 err = open_object(&obj, pack, packidx, iobj.idx, &id,
387 objcache);
388 if (err)
389 return err;
392 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
393 if (err)
394 goto done;
396 obj->size = len;
397 err = got_object_parse_tag(&tag, buf, len);
398 if (err)
399 goto done;
401 err = got_privsep_send_tag(ibuf, tag);
402 done:
403 free(buf);
404 got_object_close(obj);
405 if (tag)
406 got_object_tag_close(tag);
407 if (err) {
408 if (err->code == GOT_ERR_PRIVSEP_PIPE)
409 err = NULL;
410 else
411 got_privsep_send_error(ibuf, err);
414 return err;
417 static struct got_parsed_tree_entry *
418 find_entry_by_name(struct got_pathlist_head *entries, int nentries,
419 const char *name, size_t len)
421 struct got_pathlist_entry *pe;
423 /* Note that tree entries are sorted in strncmp() order. */
424 TAILQ_FOREACH(pe, entries, entry) {
425 int cmp = strncmp(pe->path, name, len);
426 if (cmp < 0)
427 continue;
428 if (cmp > 0)
429 break;
430 if (pe->path[len] == '\0')
431 return (struct got_parsed_tree_entry *)pe->data;
433 return NULL;
436 const struct got_error *
437 tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
438 struct got_pathlist_head *entries1, int *nentries1,
439 struct got_pathlist_head *entries2, int *nentries2,
440 const char *path, struct got_pack *pack, struct got_packidx *packidx,
441 struct imsgbuf *ibuf, struct got_object_cache *objcache)
443 const struct got_error *err = NULL;
444 struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
445 const char *seg, *s;
446 size_t seglen;
448 *changed = 0;
450 /* We not do support comparing the root path. */
451 if (got_path_is_root_dir(path))
452 return got_error_path(path, GOT_ERR_BAD_PATH);
454 s = path;
455 while (*s == '/')
456 s++;
457 seg = s;
458 seglen = 0;
459 while (*s) {
460 if (*s != '/') {
461 s++;
462 seglen++;
463 if (*s)
464 continue;
467 pte1 = find_entry_by_name(entries1, *nentries1, seg, seglen);
468 if (pte1 == NULL) {
469 err = got_error(GOT_ERR_NO_OBJ);
470 break;
473 pte2 = find_entry_by_name(entries2, *nentries2, seg, seglen);
474 if (pte2 == NULL) {
475 *changed = 1;
476 break;
479 if (pte1->mode != pte2->mode) {
480 *changed = 1;
481 break;
484 if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
485 *changed = 0;
486 break;
489 if (*s == '\0') { /* final path element */
490 *changed = 1;
491 break;
494 seg = s + 1;
495 s++;
496 seglen = 0;
497 if (*s) {
498 struct got_object_id id1, id2;
499 int idx;
501 memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
502 idx = got_packidx_get_object_idx(packidx, &id1);
503 if (idx == -1) {
504 err = got_error_no_obj(&id1);
505 break;
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 memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
518 idx = got_packidx_get_object_idx(packidx, &id2);
519 if (idx == -1) {
520 err = got_error_no_obj(&id2);
521 break;
523 got_object_parsed_tree_entries_free(entries2);
524 *nentries2 = 0;
525 free(*buf2);
526 *buf2 = NULL;
527 err = open_tree(buf2, entries2, nentries2, pack,
528 packidx, idx, &id2, objcache);
529 pte2 = NULL;
530 if (err)
531 break;
535 return err;
538 static const struct got_error *
539 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
540 struct imsgbuf *ibuf)
542 const struct got_error *err;
543 struct ibuf *wbuf;
544 int i;
546 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
547 sizeof(struct got_imsg_traversed_commits) +
548 ncommits * SHA1_DIGEST_LENGTH);
549 if (wbuf == NULL)
550 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
552 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
553 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
554 ibuf_free(wbuf);
555 return err;
557 for (i = 0; i < ncommits; i++) {
558 struct got_object_id *id = &commit_ids[i];
559 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
560 err = got_error_from_errno(
561 "imsg_add TRAVERSED_COMMITS");
562 ibuf_free(wbuf);
563 return err;
567 wbuf->fd = -1;
568 imsg_close(ibuf, wbuf);
570 return got_privsep_flush_imsg(ibuf);
573 static const struct got_error *
574 send_commit_traversal_done(struct imsgbuf *ibuf)
576 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
577 NULL, 0) == -1)
578 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
580 return got_privsep_flush_imsg(ibuf);
584 static const struct got_error *
585 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
586 struct got_pack *pack, struct got_packidx *packidx,
587 struct got_object_cache *objcache)
589 const struct got_error *err = NULL;
590 struct got_imsg_packed_object iobj;
591 struct got_object_qid *pid;
592 struct got_commit_object *commit = NULL, *pcommit = NULL;
593 struct got_pathlist_head entries, pentries;
594 int nentries = 0, pnentries = 0;
595 struct got_object_id id;
596 size_t datalen, path_len;
597 char *path = NULL;
598 const int min_alloc = 64;
599 int changed = 0, ncommits = 0, nallocated = 0;
600 struct got_object_id *commit_ids = NULL;
602 TAILQ_INIT(&entries);
603 TAILQ_INIT(&pentries);
605 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
606 if (datalen < sizeof(iobj))
607 return got_error(GOT_ERR_PRIVSEP_LEN);
608 memcpy(&iobj, imsg->data, sizeof(iobj));
609 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
611 path_len = datalen - sizeof(iobj) - 1;
612 if (path_len < 0)
613 return got_error(GOT_ERR_PRIVSEP_LEN);
614 if (path_len > 0) {
615 path = imsg->data + sizeof(iobj);
616 if (path[path_len] != '\0')
617 return got_error(GOT_ERR_PRIVSEP_LEN);
620 nallocated = min_alloc;
621 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
622 if (commit_ids == NULL)
623 return got_error_from_errno("reallocarray");
625 do {
626 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
627 int idx;
629 if (sigint_received) {
630 err = got_error(GOT_ERR_CANCELLED);
631 goto done;
634 if (commit == NULL) {
635 idx = got_packidx_get_object_idx(packidx, &id);
636 if (idx == -1)
637 break;
638 err = open_commit(&commit, pack, packidx,
639 idx, &id, objcache);
640 if (err) {
641 if (err->code != GOT_ERR_NO_OBJ)
642 goto done;
643 err = NULL;
644 break;
648 if (sizeof(struct got_imsg_traversed_commits) +
649 ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
650 err = send_traversed_commits(commit_ids, ncommits,
651 ibuf);
652 if (err)
653 goto done;
654 ncommits = 0;
656 ncommits++;
657 if (ncommits > nallocated) {
658 struct got_object_id *new;
659 nallocated += min_alloc;
660 new = reallocarray(commit_ids, nallocated,
661 sizeof(*commit_ids));
662 if (new == NULL) {
663 err = got_error_from_errno("reallocarray");
664 goto done;
666 commit_ids = new;
668 memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
669 SHA1_DIGEST_LENGTH);
671 pid = SIMPLEQ_FIRST(&commit->parent_ids);
672 if (pid == NULL)
673 break;
675 idx = got_packidx_get_object_idx(packidx, pid->id);
676 if (idx == -1)
677 break;
679 err = open_commit(&pcommit, pack, packidx, idx, pid->id,
680 objcache);
681 if (err) {
682 if (err->code != GOT_ERR_NO_OBJ)
683 goto done;
684 err = NULL;
685 break;
688 if (path[0] == '/' && path[1] == '\0') {
689 if (got_object_id_cmp(pcommit->tree_id,
690 commit->tree_id) != 0) {
691 changed = 1;
692 break;
694 } else {
695 int pidx;
696 uint8_t *buf = NULL, *pbuf = NULL;
698 idx = got_packidx_get_object_idx(packidx,
699 commit->tree_id);
700 if (idx == -1)
701 break;
702 pidx = got_packidx_get_object_idx(packidx,
703 pcommit->tree_id);
704 if (pidx == -1)
705 break;
707 err = open_tree(&buf, &entries, &nentries, pack,
708 packidx, idx, commit->tree_id, objcache);
709 if (err)
710 goto done;
711 err = open_tree(&pbuf, &pentries, &pnentries, pack,
712 packidx, pidx, pcommit->tree_id, objcache);
713 if (err) {
714 free(buf);
715 goto done;
718 err = tree_path_changed(&changed, &buf, &pbuf,
719 &entries, &nentries, &pentries, &pnentries, path,
720 pack, packidx, ibuf, objcache);
722 got_object_parsed_tree_entries_free(&entries);
723 nentries = 0;
724 free(buf);
725 got_object_parsed_tree_entries_free(&pentries);
726 pnentries = 0;
727 free(pbuf);
728 if (err) {
729 if (err->code != GOT_ERR_NO_OBJ)
730 goto done;
731 err = NULL;
732 break;
736 if (!changed) {
737 memcpy(id.sha1, pid->id->sha1, SHA1_DIGEST_LENGTH);
738 got_object_commit_close(commit);
739 commit = pcommit;
740 pcommit = NULL;
742 } while (!changed);
744 if (ncommits > 0) {
745 err = send_traversed_commits(commit_ids, ncommits, ibuf);
746 if (err)
747 goto done;
749 if (changed) {
750 err = got_privsep_send_commit(ibuf, commit);
751 if (err)
752 goto done;
755 err = send_commit_traversal_done(ibuf);
756 done:
757 free(commit_ids);
758 if (commit)
759 got_object_commit_close(commit);
760 if (pcommit)
761 got_object_commit_close(pcommit);
762 if (nentries != 0)
763 got_object_parsed_tree_entries_free(&entries);
764 if (pnentries != 0)
765 got_object_parsed_tree_entries_free(&pentries);
766 if (err) {
767 if (err->code == GOT_ERR_PRIVSEP_PIPE)
768 err = NULL;
769 else
770 got_privsep_send_error(ibuf, err);
773 return err;
776 static const struct got_error *
777 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
779 const struct got_error *err = NULL;
780 struct imsg imsg;
781 struct got_imsg_packidx ipackidx;
782 size_t datalen;
783 struct got_packidx *p;
785 *packidx = NULL;
787 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
788 if (err)
789 return err;
791 p = calloc(1, sizeof(*p));
792 if (p == NULL) {
793 err = got_error_from_errno("calloc");
794 goto done;
797 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
798 err = got_error(GOT_ERR_PRIVSEP_MSG);
799 goto done;
802 if (imsg.fd == -1) {
803 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
804 goto done;
807 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
808 if (datalen != sizeof(ipackidx)) {
809 err = got_error(GOT_ERR_PRIVSEP_LEN);
810 goto done;
812 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
814 p->len = ipackidx.len;
815 p->fd = dup(imsg.fd);
816 if (p->fd == -1) {
817 err = got_error_from_errno("dup");
818 goto done;
820 if (lseek(p->fd, 0, SEEK_SET) == -1) {
821 err = got_error_from_errno("lseek");
822 goto done;
825 #ifndef GOT_PACK_NO_MMAP
826 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
827 if (p->map == MAP_FAILED)
828 p->map = NULL; /* fall back to read(2) */
829 #endif
830 err = got_packidx_init_hdr(p, 1);
831 done:
832 if (err) {
833 if (imsg.fd != -1)
834 close(imsg.fd);
835 got_packidx_close(p);
836 } else
837 *packidx = p;
838 imsg_free(&imsg);
839 return err;
842 static const struct got_error *
843 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
845 const struct got_error *err = NULL;
846 struct imsg imsg;
847 struct got_imsg_pack ipack;
848 size_t datalen;
849 struct got_pack *pack;
851 *packp = NULL;
853 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
854 if (err)
855 return err;
857 pack = calloc(1, sizeof(*pack));
858 if (pack == NULL) {
859 err = got_error_from_errno("calloc");
860 goto done;
863 if (imsg.hdr.type != GOT_IMSG_PACK) {
864 err = got_error(GOT_ERR_PRIVSEP_MSG);
865 goto done;
868 if (imsg.fd == -1) {
869 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
870 goto done;
873 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
874 if (datalen != sizeof(ipack)) {
875 err = got_error(GOT_ERR_PRIVSEP_LEN);
876 goto done;
878 memcpy(&ipack, imsg.data, sizeof(ipack));
880 pack->filesize = ipack.filesize;
881 pack->fd = dup(imsg.fd);
882 if (pack->fd == -1) {
883 err = got_error_from_errno("dup");
884 goto done;
886 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
887 err = got_error_from_errno("lseek");
888 goto done;
890 pack->path_packfile = strdup(ipack.path_packfile);
891 if (pack->path_packfile == NULL) {
892 err = got_error_from_errno("strdup");
893 goto done;
896 pack->delta_cache = got_delta_cache_alloc(100,
897 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
898 if (pack->delta_cache == NULL) {
899 err = got_error_from_errno("got_delta_cache_alloc");
900 goto done;
903 #ifndef GOT_PACK_NO_MMAP
904 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
905 pack->fd, 0);
906 if (pack->map == MAP_FAILED)
907 pack->map = NULL; /* fall back to read(2) */
908 #endif
909 done:
910 if (err) {
911 if (imsg.fd != -1)
912 close(imsg.fd);
913 free(pack);
914 } else
915 *packp = pack;
916 imsg_free(&imsg);
917 return err;
920 int
921 main(int argc, char *argv[])
923 const struct got_error *err = NULL;
924 struct imsgbuf ibuf;
925 struct imsg imsg;
926 struct got_packidx *packidx = NULL;
927 struct got_pack *pack = NULL;
928 struct got_object_cache objcache;
930 //static int attached;
931 //while (!attached) sleep(1);
933 signal(SIGINT, catch_sigint);
935 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
937 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
938 if (err) {
939 err = got_error_from_errno("got_object_cache_init");
940 got_privsep_send_error(&ibuf, err);
941 return 1;
944 #ifndef PROFILE
945 /* revoke access to most system calls */
946 if (pledge("stdio recvfd", NULL) == -1) {
947 err = got_error_from_errno("pledge");
948 got_privsep_send_error(&ibuf, err);
949 return 1;
951 #endif
953 err = receive_packidx(&packidx, &ibuf);
954 if (err) {
955 got_privsep_send_error(&ibuf, err);
956 return 1;
959 err = receive_pack(&pack, &ibuf);
960 if (err) {
961 got_privsep_send_error(&ibuf, err);
962 return 1;
965 for (;;) {
966 imsg.fd = -1;
968 if (sigint_received) {
969 err = got_error(GOT_ERR_CANCELLED);
970 break;
973 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
974 if (err) {
975 if (err->code == GOT_ERR_PRIVSEP_PIPE)
976 err = NULL;
977 break;
980 if (imsg.hdr.type == GOT_IMSG_STOP)
981 break;
983 switch (imsg.hdr.type) {
984 case GOT_IMSG_PACKED_OBJECT_REQUEST:
985 err = object_request(&imsg, &ibuf, pack, packidx,
986 &objcache);
987 break;
988 case GOT_IMSG_COMMIT_REQUEST:
989 err = commit_request(&imsg, &ibuf, pack, packidx,
990 &objcache);
991 break;
992 case GOT_IMSG_TREE_REQUEST:
993 err = tree_request(&imsg, &ibuf, pack, packidx,
994 &objcache);
995 break;
996 case GOT_IMSG_BLOB_REQUEST:
997 err = blob_request(&imsg, &ibuf, pack, packidx,
998 &objcache);
999 break;
1000 case GOT_IMSG_TAG_REQUEST:
1001 err = tag_request(&imsg, &ibuf, pack, packidx,
1002 &objcache);
1003 break;
1004 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1005 err = commit_traversal_request(&imsg, &ibuf, pack,
1006 packidx, &objcache);
1007 break;
1008 default:
1009 err = got_error(GOT_ERR_PRIVSEP_MSG);
1010 break;
1013 if (imsg.fd != -1 && close(imsg.fd) != 0 && err == NULL)
1014 err = got_error_from_errno("close");
1015 imsg_free(&imsg);
1016 if (err)
1017 break;
1020 if (packidx)
1021 got_packidx_close(packidx);
1022 if (pack)
1023 got_pack_close(pack);
1024 got_object_cache_close(&objcache);
1025 imsg_clear(&ibuf);
1026 if (err) {
1027 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1028 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1029 got_privsep_send_error(&ibuf, err);
1032 if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
1033 err = got_error_from_errno("close");
1034 return err ? 1 : 0;