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/stat.h>
18 #include <sys/types.h>
19 #include <sys/uio.h>
20 #include <sys/time.h>
21 #include <sys/mman.h>
23 #include <limits.h>
24 #include <signal.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <zlib.h>
32 #include "got_compat.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_object_idset.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 static 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 static const struct got_error *
180 open_tree(uint8_t **buf, struct got_parsed_tree_entry **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_parsed_tree_entry *entries = NULL;
224 int nentries = 0;
225 uint8_t *buf = NULL;
226 struct got_object_id id;
227 size_t datalen;
229 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
230 if (datalen != sizeof(iobj))
231 return got_error(GOT_ERR_PRIVSEP_LEN);
232 memcpy(&iobj, imsg->data, sizeof(iobj));
233 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
235 err = open_tree(&buf, &entries, &nentries, pack, packidx, iobj.idx,
236 &id, objcache);
237 if (err)
238 return err;
240 err = got_privsep_send_tree(ibuf, entries, nentries);
241 free(entries);
242 free(buf);
243 if (err) {
244 if (err->code == GOT_ERR_PRIVSEP_PIPE)
245 err = NULL;
246 else
247 got_privsep_send_error(ibuf, err);
250 return err;
253 static const struct got_error *
254 receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
256 const struct got_error *err;
257 struct imsg imsg;
258 size_t datalen;
260 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
261 if (err)
262 return err;
264 if (imsg.hdr.type != imsg_code) {
265 err = got_error(GOT_ERR_PRIVSEP_MSG);
266 goto done;
269 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
270 if (datalen != 0) {
271 err = got_error(GOT_ERR_PRIVSEP_LEN);
272 goto done;
274 if (imsg.fd == -1) {
275 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
276 goto done;
279 *f = fdopen(imsg.fd, "w+");
280 if (*f == NULL) {
281 err = got_error_from_errno("fdopen");
282 close(imsg.fd);
283 goto done;
285 done:
286 imsg_free(&imsg);
287 return err;
290 static const struct got_error *
291 receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
292 struct imsgbuf *ibuf)
294 size_t datalen;
296 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
297 if (datalen != 0)
298 return got_error(GOT_ERR_PRIVSEP_LEN);
300 if (imsg->fd == -1)
301 return got_error(GOT_ERR_PRIVSEP_NO_FD);
303 *f = fdopen(imsg->fd, mode);
304 if (*f == NULL)
305 return got_error_from_errno("fdopen");
306 imsg->fd = -1;
308 return NULL;
311 static const struct got_error *
312 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
313 struct got_packidx *packidx, struct got_object_cache *objcache,
314 FILE *basefile, FILE *accumfile)
316 const struct got_error *err = NULL;
317 struct got_imsg_packed_object iobj;
318 struct got_object *obj = NULL;
319 FILE *outfile = NULL;
320 struct got_object_id id;
321 size_t datalen;
322 uint64_t blob_size;
323 uint8_t *buf = NULL;
325 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
326 if (datalen != sizeof(iobj))
327 return got_error(GOT_ERR_PRIVSEP_LEN);
328 memcpy(&iobj, imsg->data, sizeof(iobj));
329 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
331 obj = got_object_cache_get(objcache, &id);
332 if (obj) {
333 obj->refcnt++;
334 } else {
335 err = open_object(&obj, pack, packidx, iobj.idx, &id,
336 objcache);
337 if (err)
338 return err;
341 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
342 if (err)
343 goto done;
345 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
346 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
347 if (err)
348 goto done;
349 } else
350 blob_size = obj->size;
352 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
353 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
354 obj, pack);
355 else
356 err = got_packfile_extract_object(pack, obj, outfile, basefile,
357 accumfile);
358 if (err)
359 goto done;
361 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
362 done:
363 free(buf);
364 if (outfile && fclose(outfile) == EOF && err == NULL)
365 err = got_error_from_errno("fclose");
366 got_object_close(obj);
367 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
368 got_privsep_send_error(ibuf, err);
370 return err;
373 static const struct got_error *
374 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
375 struct got_packidx *packidx, struct got_object_cache *objcache)
377 const struct got_error *err = NULL;
378 struct got_imsg_packed_object iobj;
379 struct got_object *obj = NULL;
380 struct got_tag_object *tag = NULL;
381 uint8_t *buf = NULL;
382 size_t len;
383 struct got_object_id id;
384 size_t datalen;
386 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
387 if (datalen != sizeof(iobj))
388 return got_error(GOT_ERR_PRIVSEP_LEN);
389 memcpy(&iobj, imsg->data, sizeof(iobj));
390 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
392 obj = got_object_cache_get(objcache, &id);
393 if (obj) {
394 obj->refcnt++;
395 } else {
396 err = open_object(&obj, pack, packidx, iobj.idx, &id,
397 objcache);
398 if (err)
399 return err;
402 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
403 if (err)
404 goto done;
406 obj->size = len;
407 err = got_object_parse_tag(&tag, buf, len);
408 if (err)
409 goto done;
411 err = got_privsep_send_tag(ibuf, tag);
412 done:
413 free(buf);
414 got_object_close(obj);
415 if (tag)
416 got_object_tag_close(tag);
417 if (err) {
418 if (err->code == GOT_ERR_PRIVSEP_PIPE)
419 err = NULL;
420 else
421 got_privsep_send_error(ibuf, err);
424 return err;
427 static struct got_parsed_tree_entry *
428 find_entry_by_name(struct got_parsed_tree_entry *entries, int nentries,
429 const char *name, size_t len)
431 struct got_parsed_tree_entry *pte;
432 int cmp, i;
434 /* Note that tree entries are sorted in strncmp() order. */
435 for (i = 0; i < nentries; i++) {
436 pte = &entries[i];
437 cmp = strncmp(pte->name, name, len);
438 if (cmp < 0)
439 continue;
440 if (cmp > 0)
441 break;
442 if (pte->name[len] == '\0')
443 return pte;
445 return NULL;
448 static const struct got_error *
449 tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
450 struct got_parsed_tree_entry **entries1, int *nentries1,
451 struct got_parsed_tree_entry **entries2, int *nentries2,
452 const char *path, struct got_pack *pack, struct got_packidx *packidx,
453 struct imsgbuf *ibuf, struct got_object_cache *objcache)
455 const struct got_error *err = NULL;
456 struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
457 const char *seg, *s;
458 size_t seglen;
460 *changed = 0;
462 /* We not do support comparing the root path. */
463 if (got_path_is_root_dir(path))
464 return got_error_path(path, GOT_ERR_BAD_PATH);
466 s = path;
467 while (*s == '/')
468 s++;
469 seg = s;
470 seglen = 0;
471 while (*s) {
472 if (*s != '/') {
473 s++;
474 seglen++;
475 if (*s)
476 continue;
479 pte1 = find_entry_by_name(*entries1, *nentries1, seg, seglen);
480 if (pte1 == NULL) {
481 err = got_error(GOT_ERR_NO_OBJ);
482 break;
485 pte2 = find_entry_by_name(*entries2, *nentries2, seg, seglen);
486 if (pte2 == NULL) {
487 *changed = 1;
488 break;
491 if (pte1->mode != pte2->mode) {
492 *changed = 1;
493 break;
496 if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
497 *changed = 0;
498 break;
501 if (*s == '\0') { /* final path element */
502 *changed = 1;
503 break;
506 seg = s + 1;
507 s++;
508 seglen = 0;
509 if (*s) {
510 struct got_object_id id1, id2;
511 int idx;
513 memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
514 idx = got_packidx_get_object_idx(packidx, &id1);
515 if (idx == -1) {
516 err = got_error_no_obj(&id1);
517 break;
519 free(*entries1);
520 *nentries1 = 0;
521 free(*buf1);
522 *buf1 = NULL;
523 err = open_tree(buf1, entries1, nentries1, pack,
524 packidx, idx, &id1, objcache);
525 pte1 = NULL;
526 if (err)
527 break;
529 memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
530 idx = got_packidx_get_object_idx(packidx, &id2);
531 if (idx == -1) {
532 err = got_error_no_obj(&id2);
533 break;
535 free(*entries2);
536 *nentries2 = 0;
537 free(*buf2);
538 *buf2 = NULL;
539 err = open_tree(buf2, entries2, nentries2, pack,
540 packidx, idx, &id2, objcache);
541 pte2 = NULL;
542 if (err)
543 break;
547 return err;
550 static const struct got_error *
551 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
552 struct imsgbuf *ibuf)
554 struct ibuf *wbuf;
555 size_t i;
557 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
558 sizeof(struct got_imsg_traversed_commits) +
559 ncommits * SHA1_DIGEST_LENGTH);
560 if (wbuf == NULL)
561 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
563 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
564 return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
566 for (i = 0; i < ncommits; i++) {
567 struct got_object_id *id = &commit_ids[i];
568 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
569 return got_error_from_errno(
570 "imsg_add TRAVERSED_COMMITS");
574 wbuf->fd = -1;
575 imsg_close(ibuf, wbuf);
577 return got_privsep_flush_imsg(ibuf);
580 static const struct got_error *
581 send_commit_traversal_done(struct imsgbuf *ibuf)
583 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
584 NULL, 0) == -1)
585 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
587 return got_privsep_flush_imsg(ibuf);
590 static const struct got_error *
591 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
592 struct got_pack *pack, struct got_packidx *packidx,
593 struct got_object_cache *objcache)
595 const struct got_error *err = NULL;
596 struct got_imsg_packed_object iobj;
597 struct got_object_qid *pid;
598 struct got_commit_object *commit = NULL, *pcommit = NULL;
599 struct got_parsed_tree_entry *entries = NULL, *pentries = NULL;
600 int nentries = 0, pnentries = 0;
601 struct got_object_id id;
602 size_t datalen, path_len;
603 char *path = NULL;
604 const int min_alloc = 64;
605 int changed = 0, ncommits = 0, nallocated = 0;
606 struct got_object_id *commit_ids = NULL;
608 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
609 if (datalen < sizeof(iobj))
610 return got_error(GOT_ERR_PRIVSEP_LEN);
611 memcpy(&iobj, imsg->data, sizeof(iobj));
612 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
614 path_len = datalen - sizeof(iobj) - 1;
615 if (path_len < 0)
616 return got_error(GOT_ERR_PRIVSEP_LEN);
617 if (path_len > 0) {
618 path = imsg->data + sizeof(iobj);
619 if (path[path_len] != '\0')
620 return got_error(GOT_ERR_PRIVSEP_LEN);
623 nallocated = min_alloc;
624 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
625 if (commit_ids == NULL)
626 return got_error_from_errno("reallocarray");
628 do {
629 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
630 int idx;
632 if (sigint_received) {
633 err = got_error(GOT_ERR_CANCELLED);
634 goto done;
637 if (commit == NULL) {
638 idx = got_packidx_get_object_idx(packidx, &id);
639 if (idx == -1)
640 break;
641 err = open_commit(&commit, pack, packidx,
642 idx, &id, objcache);
643 if (err) {
644 if (err->code != GOT_ERR_NO_OBJ)
645 goto done;
646 err = NULL;
647 break;
651 if (sizeof(struct got_imsg_traversed_commits) +
652 ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
653 err = send_traversed_commits(commit_ids, ncommits,
654 ibuf);
655 if (err)
656 goto done;
657 ncommits = 0;
659 ncommits++;
660 if (ncommits > nallocated) {
661 struct got_object_id *new;
662 nallocated += min_alloc;
663 new = reallocarray(commit_ids, nallocated,
664 sizeof(*commit_ids));
665 if (new == NULL) {
666 err = got_error_from_errno("reallocarray");
667 goto done;
669 commit_ids = new;
671 memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
672 SHA1_DIGEST_LENGTH);
674 pid = STAILQ_FIRST(&commit->parent_ids);
675 if (pid == NULL)
676 break;
678 idx = got_packidx_get_object_idx(packidx, &pid->id);
679 if (idx == -1)
680 break;
682 err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
683 objcache);
684 if (err) {
685 if (err->code != GOT_ERR_NO_OBJ)
686 goto done;
687 err = NULL;
688 break;
691 if (path[0] == '/' && path[1] == '\0') {
692 if (got_object_id_cmp(pcommit->tree_id,
693 commit->tree_id) != 0) {
694 changed = 1;
695 break;
697 } else {
698 int pidx;
699 uint8_t *buf = NULL, *pbuf = NULL;
701 idx = got_packidx_get_object_idx(packidx,
702 commit->tree_id);
703 if (idx == -1)
704 break;
705 pidx = got_packidx_get_object_idx(packidx,
706 pcommit->tree_id);
707 if (pidx == -1)
708 break;
710 err = open_tree(&buf, &entries, &nentries, pack,
711 packidx, idx, commit->tree_id, objcache);
712 if (err)
713 goto done;
714 err = open_tree(&pbuf, &pentries, &pnentries, pack,
715 packidx, pidx, pcommit->tree_id, objcache);
716 if (err) {
717 free(buf);
718 goto done;
721 err = tree_path_changed(&changed, &buf, &pbuf,
722 &entries, &nentries, &pentries, &pnentries, path,
723 pack, packidx, ibuf, objcache);
725 free(entries);
726 entries = NULL;
727 nentries = 0;
728 free(buf);
729 free(pentries);
730 pentries = NULL;
731 pnentries = 0;
732 free(pbuf);
733 if (err) {
734 if (err->code != GOT_ERR_NO_OBJ)
735 goto done;
736 err = NULL;
737 break;
741 if (!changed) {
742 memcpy(id.sha1, pid->id.sha1, SHA1_DIGEST_LENGTH);
743 got_object_commit_close(commit);
744 commit = pcommit;
745 pcommit = NULL;
747 } while (!changed);
749 if (ncommits > 0) {
750 err = send_traversed_commits(commit_ids, ncommits, ibuf);
751 if (err)
752 goto done;
754 if (changed) {
755 err = got_privsep_send_commit(ibuf, commit);
756 if (err)
757 goto done;
760 err = send_commit_traversal_done(ibuf);
761 done:
762 free(commit_ids);
763 if (commit)
764 got_object_commit_close(commit);
765 if (pcommit)
766 got_object_commit_close(pcommit);
767 free(entries);
768 free(pentries);
769 if (err) {
770 if (err->code == GOT_ERR_PRIVSEP_PIPE)
771 err = NULL;
772 else
773 got_privsep_send_error(ibuf, err);
776 return err;
779 static const struct got_error *
780 raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
781 struct got_pack *pack, struct got_packidx *packidx,
782 struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
784 const struct got_error *err = NULL;
785 uint8_t *buf = NULL;
786 uint64_t size = 0;
787 FILE *outfile = NULL;
788 struct got_imsg_packed_object iobj;
789 struct got_object *obj;
790 struct got_object_id id;
791 size_t datalen;
793 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
794 if (datalen != sizeof(iobj))
795 return got_error(GOT_ERR_PRIVSEP_LEN);
796 memcpy(&iobj, imsg->data, sizeof(iobj));
797 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
799 obj = got_object_cache_get(objcache, &id);
800 if (obj) {
801 obj->refcnt++;
802 } else {
803 err = open_object(&obj, pack, packidx, iobj.idx, &id,
804 objcache);
805 if (err)
806 return err;
809 err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
810 if (err)
811 return err;
813 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
814 err = got_pack_get_max_delta_object_size(&size, obj, pack);
815 if (err)
816 goto done;
817 } else
818 size = obj->size;
820 if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
821 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
822 obj, pack);
823 else
824 err = got_packfile_extract_object(pack, obj, outfile, basefile,
825 accumfile);
826 if (err)
827 goto done;
829 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
830 done:
831 free(buf);
832 if (outfile && fclose(outfile) == EOF && err == NULL)
833 err = got_error_from_errno("fclose");
834 got_object_close(obj);
835 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
836 got_privsep_send_error(ibuf, err);
838 return err;
841 static const struct got_error *
842 get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
843 off_t base_offset)
845 const struct got_error *err;
846 int idx;
848 err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
849 if (err)
850 return err;
851 if (idx == -1)
852 return got_error(GOT_ERR_BAD_PACKIDX);
854 return got_packidx_get_object_id(base_id, packidx, idx);
857 static const struct got_error *
858 raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
859 FILE *delta_outfile, struct got_pack *pack,
860 struct got_packidx *packidx)
862 const struct got_error *err = NULL;
863 struct got_imsg_raw_delta_request req;
864 size_t datalen, delta_size, delta_compressed_size;
865 off_t delta_offset;
866 uint8_t *delta_buf = NULL;
867 struct got_object_id id, base_id;
868 off_t base_offset, delta_out_offset = 0;
869 uint64_t base_size = 0, result_size = 0;
870 size_t w;
872 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
873 if (datalen != sizeof(req))
874 return got_error(GOT_ERR_PRIVSEP_LEN);
875 memcpy(&req, imsg->data, sizeof(req));
876 memcpy(id.sha1, req.id, SHA1_DIGEST_LENGTH);
878 imsg->fd = -1;
880 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
881 &delta_compressed_size, &delta_offset, &base_offset, &base_id,
882 &base_size, &result_size, pack, packidx, req.idx);
883 if (err)
884 goto done;
886 /*
887 * If this is an offset delta we must determine the base
888 * object ID ourselves.
889 */
890 if (base_offset != 0) {
891 err = get_base_object_id(&base_id, packidx, base_offset);
892 if (err)
893 goto done;
896 delta_out_offset = ftello(delta_outfile);
897 w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
898 if (w != delta_compressed_size) {
899 err = got_ferror(delta_outfile, GOT_ERR_IO);
900 goto done;
902 if (fflush(delta_outfile) == -1) {
903 err = got_error_from_errno("fflush");
904 goto done;
907 err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
908 delta_size, delta_compressed_size, delta_offset, delta_out_offset,
909 &base_id);
910 done:
911 free(delta_buf);
912 return err;
915 struct search_deltas_arg {
916 struct imsgbuf *ibuf;
917 struct got_packidx *packidx;
918 struct got_pack *pack;
919 struct got_object_idset *idset;
920 FILE *delta_outfile;
921 struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
922 size_t ndeltas;
923 };
925 static const struct got_error *
926 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
928 const struct got_error *err;
929 struct search_deltas_arg *a = arg;
930 int obj_idx;
931 uint8_t *delta_buf = NULL;
932 uint64_t base_size, result_size;
933 size_t delta_size, delta_compressed_size;
934 off_t delta_offset, base_offset;
935 struct got_object_id base_id;
937 if (sigint_received)
938 return got_error(GOT_ERR_CANCELLED);
940 obj_idx = got_packidx_get_object_idx(a->packidx, id);
941 if (obj_idx == -1)
942 return NULL; /* object not present in our pack file */
944 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
945 &delta_compressed_size, &delta_offset, &base_offset, &base_id,
946 &base_size, &result_size, a->pack, a->packidx, obj_idx);
947 if (err) {
948 if (err->code == GOT_ERR_OBJ_TYPE)
949 return NULL; /* object not stored as a delta */
950 return err;
953 /*
954 * If this is an offset delta we must determine the base
955 * object ID ourselves.
956 */
957 if (base_offset != 0) {
958 err = get_base_object_id(&base_id, a->packidx, base_offset);
959 if (err)
960 goto done;
963 if (got_object_idset_contains(a->idset, &base_id)) {
964 struct got_imsg_reused_delta *delta;
965 off_t delta_out_offset = ftello(a->delta_outfile);
966 size_t w;
968 w = fwrite(delta_buf, 1, delta_compressed_size,
969 a->delta_outfile);
970 if (w != delta_compressed_size) {
971 err = got_ferror(a->delta_outfile, GOT_ERR_IO);
972 goto done;
975 delta = &a->deltas[a->ndeltas++];
976 memcpy(&delta->id, id, sizeof(delta->id));
977 memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
978 delta->base_size = base_size;
979 delta->result_size = result_size;
980 delta->delta_size = delta_size;
981 delta->delta_compressed_size = delta_compressed_size;
982 delta->delta_offset = delta_offset;
983 delta->delta_out_offset = delta_out_offset;
985 if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
986 err = got_privsep_send_reused_deltas(a->ibuf,
987 a->deltas, a->ndeltas);
988 if (err)
989 goto done;
990 a->ndeltas = 0;
993 done:
994 free(delta_buf);
995 return err;
998 static const struct got_error *
999 recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
1001 const struct got_error *err = NULL;
1002 int done = 0;
1003 struct got_object_id *ids;
1004 size_t nids, i;
1006 for (;;) {
1007 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1008 if (err || done)
1009 break;
1010 for (i = 0; i < nids; i++) {
1011 err = got_object_idset_add(idset, &ids[i], NULL);
1012 if (err) {
1013 free(ids);
1014 return err;
1017 free(ids);
1020 return err;
1023 static const struct got_error *
1024 recv_object_id_queue(struct got_object_id_queue *queue, struct imsgbuf *ibuf)
1026 const struct got_error *err = NULL;
1027 int done = 0;
1028 struct got_object_qid *qid;
1029 struct got_object_id *ids;
1030 size_t nids, i;
1032 for (;;) {
1033 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1034 if (err || done)
1035 break;
1036 for (i = 0; i < nids; i++) {
1037 err = got_object_qid_alloc_partial(&qid);
1038 if (err)
1039 return err;
1040 memcpy(&qid->id, &ids[i], sizeof(qid->id));
1041 STAILQ_INSERT_TAIL(queue, qid, entry);
1045 return err;
1048 static const struct got_error *
1049 delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1050 FILE *delta_outfile, struct got_pack *pack, struct got_packidx *packidx)
1052 const struct got_error *err = NULL;
1053 struct got_object_idset *idset;
1054 struct search_deltas_arg sda;
1056 idset = got_object_idset_alloc();
1057 if (idset == NULL)
1058 return got_error_from_errno("got_object_idset_alloc");
1060 err = recv_object_ids(idset, ibuf);
1061 if (err)
1062 return err;
1064 memset(&sda, 0, sizeof(sda));
1065 sda.ibuf = ibuf;
1066 sda.idset = idset;
1067 sda.pack = pack;
1068 sda.packidx = packidx;
1069 sda.delta_outfile = delta_outfile;
1070 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1071 if (err)
1072 goto done;
1074 if (sda.ndeltas > 0) {
1075 err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1076 sda.ndeltas);
1077 if (err)
1078 goto done;
1081 if (fflush(delta_outfile) == -1) {
1082 err = got_error_from_errno("fflush");
1083 goto done;
1086 err = got_privsep_send_reused_deltas_done(ibuf);
1087 done:
1088 got_object_idset_free(idset);
1089 return err;
1092 static const struct got_error *
1093 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1095 const struct got_error *err = NULL;
1096 struct imsg imsg;
1097 struct got_imsg_packidx ipackidx;
1098 size_t datalen;
1099 struct got_packidx *p;
1101 *packidx = NULL;
1103 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1104 if (err)
1105 return err;
1107 p = calloc(1, sizeof(*p));
1108 if (p == NULL) {
1109 err = got_error_from_errno("calloc");
1110 goto done;
1113 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1114 err = got_error(GOT_ERR_PRIVSEP_MSG);
1115 goto done;
1118 if (imsg.fd == -1) {
1119 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1120 goto done;
1123 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1124 if (datalen != sizeof(ipackidx)) {
1125 err = got_error(GOT_ERR_PRIVSEP_LEN);
1126 goto done;
1128 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1130 p->len = ipackidx.len;
1131 p->fd = dup(imsg.fd);
1132 if (p->fd == -1) {
1133 err = got_error_from_errno("dup");
1134 goto done;
1136 if (lseek(p->fd, 0, SEEK_SET) == -1) {
1137 err = got_error_from_errno("lseek");
1138 goto done;
1141 #ifndef GOT_PACK_NO_MMAP
1142 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1143 if (p->map == MAP_FAILED)
1144 p->map = NULL; /* fall back to read(2) */
1145 #endif
1146 err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1147 done:
1148 if (err) {
1149 if (imsg.fd != -1)
1150 close(imsg.fd);
1151 got_packidx_close(p);
1152 } else
1153 *packidx = p;
1154 imsg_free(&imsg);
1155 return err;
1158 static const struct got_error *
1159 send_tree_enumeration_done(struct imsgbuf *ibuf)
1161 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENUMERATION_DONE, 0, 0, -1,
1162 NULL, 0) == -1)
1163 return got_error_from_errno("imsg_compose TREE_ENUMERATION_DONE");
1165 return got_privsep_flush_imsg(ibuf);
1168 struct enumerated_tree {
1169 struct got_object_id id;
1170 char *path;
1171 uint8_t *buf;
1172 struct got_parsed_tree_entry *entries;
1173 int nentries;
1176 static const struct got_error *
1177 enumerate_tree(int *have_all_entries, struct imsgbuf *ibuf, size_t *totlen,
1178 struct got_object_id *tree_id,
1179 const char *path, struct got_pack *pack, struct got_packidx *packidx,
1180 struct got_object_cache *objcache, struct got_object_idset *idset,
1181 struct enumerated_tree **trees, size_t *nalloc, size_t *ntrees)
1183 const struct got_error *err = NULL;
1184 struct got_object_id_queue ids;
1185 struct got_object_qid *qid;
1186 uint8_t *buf = NULL;
1187 struct got_parsed_tree_entry *entries = NULL;
1188 int nentries = 0, i;
1189 struct enumerated_tree *tree;
1191 *ntrees = 0;
1192 *have_all_entries = 1;
1193 STAILQ_INIT(&ids);
1195 err = got_object_qid_alloc_partial(&qid);
1196 if (err)
1197 return err;
1198 memcpy(&qid->id.sha1, tree_id, SHA1_DIGEST_LENGTH);
1199 qid->data = strdup(path);
1200 if (qid->data == NULL) {
1201 err = got_error_from_errno("strdup");
1202 goto done;
1204 STAILQ_INSERT_TAIL(&ids, qid, entry);
1205 qid = NULL;
1207 /* Traverse the tree hierarchy, gather tree object IDs and paths. */
1208 do {
1209 const char *path;
1210 int idx, i;
1212 if (sigint_received) {
1213 err = got_error(GOT_ERR_CANCELLED);
1214 goto done;
1217 qid = STAILQ_FIRST(&ids);
1218 STAILQ_REMOVE_HEAD(&ids, entry);
1219 path = qid->data;
1221 idx = got_packidx_get_object_idx(packidx, &qid->id);
1222 if (idx == -1) {
1223 *have_all_entries = 0;
1224 break;
1227 err = open_tree(&buf, &entries, &nentries,
1228 pack, packidx, idx, &qid->id, objcache);
1229 if (err) {
1230 if (err->code != GOT_ERR_NO_OBJ)
1231 goto done;
1234 err = got_object_idset_add(idset, &qid->id, NULL);
1235 if (err)
1236 goto done;
1238 for (i = 0; i < nentries; i++) {
1239 struct got_object_qid *eqid = NULL;
1240 struct got_parsed_tree_entry *pte = &entries[i];
1241 char *p;
1243 if (!S_ISDIR(pte->mode))
1244 continue;
1246 err = got_object_qid_alloc_partial(&eqid);
1247 if (err)
1248 goto done;
1249 memcpy(eqid->id.sha1, pte->id, sizeof(eqid->id.sha1));
1251 if (got_object_idset_contains(idset, &eqid->id)) {
1252 got_object_qid_free(eqid);
1253 continue;
1256 if (asprintf(&p, "%s%s%s", path,
1257 got_path_is_root_dir(path) ? "" : "/",
1258 pte->name) == -1) {
1259 err = got_error_from_errno("asprintf");
1260 got_object_qid_free(eqid);
1261 goto done;
1263 eqid->data = p;
1264 STAILQ_INSERT_TAIL(&ids, eqid, entry);
1267 if (*ntrees >= *nalloc) {
1268 struct enumerated_tree *new;
1269 new = recallocarray(*trees, *nalloc, *nalloc + 16,
1270 sizeof(*new));
1271 if (new == NULL) {
1272 err = got_error_from_errno("malloc");
1273 goto done;
1275 *trees = new;
1276 *nalloc += 16;
1278 tree = &(*trees)[*ntrees];
1279 (*ntrees)++;
1280 memcpy(&tree->id, &qid->id, sizeof(tree->id));
1281 tree->path = qid->data;
1282 tree->buf = buf;
1283 buf = NULL;
1284 tree->entries = entries;
1285 entries = NULL;
1286 tree->nentries = nentries;
1288 got_object_qid_free(qid);
1289 qid = NULL;
1290 } while (!STAILQ_EMPTY(&ids));
1292 if (*have_all_entries) {
1293 int i;
1295 * We have managed to traverse all entries in the hierarchy.
1296 * Tell the main process what we have found.
1298 for (i = 0; i < *ntrees; i++) {
1299 tree = &(*trees)[i];
1300 err = got_privsep_send_enumerated_tree(totlen,
1301 ibuf, &tree->id, tree->path, tree->entries,
1302 tree->nentries);
1303 if (err)
1304 goto done;
1305 free(tree->buf);
1306 tree->buf = NULL;
1307 free(tree->path);
1308 tree->path = NULL;
1309 free(tree->entries);
1310 tree->entries = NULL;
1312 *ntrees = 0; /* don't loop again below to free memory */
1314 err = send_tree_enumeration_done(ibuf);
1315 } else {
1317 * We can only load fully packed tree hierarchies on
1318 * behalf of the main process, otherwise the main process
1319 * gets a wrong idea about which tree objects have
1320 * already been traversed.
1321 * Indicate a missing entry for the root of this tree.
1322 * The main process should continue by loading this
1323 * entire tree the slow way.
1325 err = got_privsep_send_enumerated_tree(totlen, ibuf,
1326 tree_id, "/", NULL, -1);
1327 if (err)
1328 goto done;
1330 done:
1331 free(buf);
1332 free(entries);
1333 for (i = 0; i < *ntrees; i++) {
1334 tree = &(*trees)[i];
1335 free(tree->buf);
1336 tree->buf = NULL;
1337 free(tree->path);
1338 tree->path = NULL;
1339 free(tree->entries);
1340 tree->entries = NULL;
1342 if (qid)
1343 free(qid->data);
1344 got_object_qid_free(qid);
1345 got_object_id_queue_free(&ids);
1346 if (err) {
1347 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1348 err = NULL;
1349 else
1350 got_privsep_send_error(ibuf, err);
1353 return err;
1356 static const struct got_error *
1357 enumeration_request(struct imsg *imsg, struct imsgbuf *ibuf,
1358 struct got_pack *pack, struct got_packidx *packidx,
1359 struct got_object_cache *objcache)
1361 const struct got_error *err = NULL;
1362 struct got_object_id_queue commit_ids;
1363 const struct got_object_id_queue *parents = NULL;
1364 struct got_object_qid *qid = NULL;
1365 struct got_object *obj = NULL;
1366 struct got_commit_object *commit = NULL;
1367 struct got_object_id *tree_id = NULL;
1368 size_t totlen = 0;
1369 struct got_object_idset *idset;
1370 int i, idx, have_all_entries = 1;
1371 struct enumerated_tree *trees = NULL;
1372 size_t ntrees = 0, nalloc = 16;
1374 STAILQ_INIT(&commit_ids);
1376 trees = calloc(nalloc, sizeof(*trees));
1377 if (trees == NULL)
1378 return got_error_from_errno("calloc");
1380 idset = got_object_idset_alloc();
1381 if (idset == NULL) {
1382 err = got_error_from_errno("got_object_idset_alloc");
1383 goto done;
1386 err = recv_object_id_queue(&commit_ids, ibuf);
1387 if (err)
1388 goto done;
1390 if (STAILQ_EMPTY(&commit_ids)) {
1391 err = got_error(GOT_ERR_PRIVSEP_MSG);
1392 goto done;
1395 err = recv_object_ids(idset, ibuf);
1396 if (err)
1397 goto done;
1399 while (!STAILQ_EMPTY(&commit_ids)) {
1400 if (sigint_received) {
1401 err = got_error(GOT_ERR_CANCELLED);
1402 goto done;
1405 qid = STAILQ_FIRST(&commit_ids);
1406 STAILQ_REMOVE_HEAD(&commit_ids, entry);
1408 if (got_object_idset_contains(idset, &qid->id)) {
1409 got_object_qid_free(qid);
1410 qid = NULL;
1411 continue;
1414 idx = got_packidx_get_object_idx(packidx, &qid->id);
1415 if (idx == -1) {
1416 have_all_entries = 0;
1417 break;
1420 err = open_object(&obj, pack, packidx, idx, &qid->id,
1421 objcache);
1422 if (err)
1423 goto done;
1424 if (obj->type == GOT_OBJ_TYPE_TAG) {
1425 struct got_tag_object *tag;
1426 uint8_t *buf;
1427 size_t len;
1428 err = got_packfile_extract_object_to_mem(&buf,
1429 &len, obj, pack);
1430 if (err)
1431 goto done;
1432 obj->size = len;
1433 err = got_object_parse_tag(&tag, buf, len);
1434 if (err) {
1435 free(buf);
1436 goto done;
1438 idx = got_packidx_get_object_idx(packidx, &tag->id);
1439 if (idx == -1) {
1440 have_all_entries = 0;
1441 break;
1443 err = open_commit(&commit, pack, packidx, idx,
1444 &tag->id, objcache);
1445 got_object_tag_close(tag);
1446 free(buf);
1447 if (err)
1448 goto done;
1449 } else if (obj->type == GOT_OBJ_TYPE_COMMIT) {
1450 err = open_commit(&commit, pack, packidx, idx,
1451 &qid->id, objcache);
1452 if (err)
1453 goto done;
1454 } else {
1455 err = got_error(GOT_ERR_OBJ_TYPE);
1456 goto done;
1458 got_object_close(obj);
1459 obj = NULL;
1461 err = got_privsep_send_enumerated_commit(ibuf, &qid->id,
1462 got_object_commit_get_committer_time(commit));
1463 if (err)
1464 goto done;
1466 tree_id = got_object_commit_get_tree_id(commit);
1467 idx = got_packidx_get_object_idx(packidx, tree_id);
1468 if (idx == -1) {
1469 have_all_entries = 0;
1470 err = got_privsep_send_enumerated_tree(&totlen, ibuf,
1471 tree_id, "/", NULL, -1);
1472 if (err)
1473 goto done;
1474 break;
1477 if (got_object_idset_contains(idset, tree_id)) {
1478 got_object_qid_free(qid);
1479 qid = NULL;
1480 continue;
1483 err = enumerate_tree(&have_all_entries, ibuf, &totlen, tree_id, "/",
1484 pack, packidx, objcache, idset, &trees, &nalloc, &ntrees);
1485 if (err)
1486 goto done;
1488 if (!have_all_entries)
1489 break;
1491 got_object_qid_free(qid);
1492 qid = NULL;
1494 parents = got_object_commit_get_parent_ids(commit);
1495 if (parents) {
1496 struct got_object_qid *pid;
1497 STAILQ_FOREACH(pid, parents, entry) {
1498 if (got_object_idset_contains(idset, &pid->id))
1499 continue;
1500 err = got_object_qid_alloc_partial(&qid);
1501 if (err)
1502 goto done;
1503 memcpy(&qid->id, &pid->id, sizeof(qid->id));
1504 STAILQ_INSERT_TAIL(&commit_ids, qid, entry);
1505 qid = NULL;
1509 got_object_commit_close(commit);
1510 commit = NULL;
1513 if (have_all_entries) {
1514 err = got_privsep_send_object_enumeration_done(ibuf);
1515 if (err)
1516 goto done;
1517 } else {
1518 err = got_privsep_send_object_enumeration_incomplete(ibuf);
1519 if (err)
1520 goto done;
1522 done:
1523 if (obj)
1524 got_object_close(obj);
1525 if (commit)
1526 got_object_commit_close(commit);
1527 got_object_qid_free(qid);
1528 got_object_id_queue_free(&commit_ids);
1529 if (idset)
1530 got_object_idset_free(idset);
1531 for (i = 0; i < ntrees; i++) {
1532 struct enumerated_tree *tree = &trees[i];
1533 free(tree->buf);
1534 free(tree->path);
1535 free(tree->entries);
1537 free(trees);
1538 return err;
1541 static const struct got_error *
1542 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1544 const struct got_error *err = NULL;
1545 struct imsg imsg;
1546 struct got_imsg_pack ipack;
1547 size_t datalen;
1548 struct got_pack *pack;
1550 *packp = NULL;
1552 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1553 if (err)
1554 return err;
1556 pack = calloc(1, sizeof(*pack));
1557 if (pack == NULL) {
1558 err = got_error_from_errno("calloc");
1559 goto done;
1562 if (imsg.hdr.type != GOT_IMSG_PACK) {
1563 err = got_error(GOT_ERR_PRIVSEP_MSG);
1564 goto done;
1567 if (imsg.fd == -1) {
1568 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1569 goto done;
1572 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1573 if (datalen != sizeof(ipack)) {
1574 err = got_error(GOT_ERR_PRIVSEP_LEN);
1575 goto done;
1577 memcpy(&ipack, imsg.data, sizeof(ipack));
1579 pack->filesize = ipack.filesize;
1580 pack->fd = dup(imsg.fd);
1581 if (pack->fd == -1) {
1582 err = got_error_from_errno("dup");
1583 goto done;
1585 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1586 err = got_error_from_errno("lseek");
1587 goto done;
1589 pack->path_packfile = strdup(ipack.path_packfile);
1590 if (pack->path_packfile == NULL) {
1591 err = got_error_from_errno("strdup");
1592 goto done;
1595 err = got_delta_cache_alloc(&pack->delta_cache);
1596 if (err)
1597 goto done;
1599 #ifndef GOT_PACK_NO_MMAP
1600 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1601 pack->fd, 0);
1602 if (pack->map == MAP_FAILED)
1603 pack->map = NULL; /* fall back to read(2) */
1604 #endif
1605 done:
1606 if (err) {
1607 if (imsg.fd != -1)
1608 close(imsg.fd);
1609 free(pack);
1610 } else
1611 *packp = pack;
1612 imsg_free(&imsg);
1613 return err;
1616 int
1617 main(int argc, char *argv[])
1619 const struct got_error *err = NULL;
1620 struct imsgbuf ibuf;
1621 struct imsg imsg;
1622 struct got_packidx *packidx = NULL;
1623 struct got_pack *pack = NULL;
1624 struct got_object_cache objcache;
1625 FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1627 //static int attached;
1628 //while (!attached) sleep(1);
1630 signal(SIGINT, catch_sigint);
1632 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1634 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1635 if (err) {
1636 err = got_error_from_errno("got_object_cache_init");
1637 got_privsep_send_error(&ibuf, err);
1638 return 1;
1641 #ifndef PROFILE
1642 /* revoke access to most system calls */
1643 if (pledge("stdio recvfd", NULL) == -1) {
1644 err = got_error_from_errno("pledge");
1645 got_privsep_send_error(&ibuf, err);
1646 return 1;
1649 /* revoke fs access */
1650 if (landlock_no_fs() == -1) {
1651 err = got_error_from_errno("landlock_no_fs");
1652 got_privsep_send_error(&ibuf, err);
1653 return 1;
1655 #endif
1657 err = receive_packidx(&packidx, &ibuf);
1658 if (err) {
1659 got_privsep_send_error(&ibuf, err);
1660 return 1;
1663 err = receive_pack(&pack, &ibuf);
1664 if (err) {
1665 got_privsep_send_error(&ibuf, err);
1666 return 1;
1669 for (;;) {
1670 imsg.fd = -1;
1672 if (sigint_received) {
1673 err = got_error(GOT_ERR_CANCELLED);
1674 break;
1677 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1678 if (err) {
1679 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1680 err = NULL;
1681 break;
1684 if (imsg.hdr.type == GOT_IMSG_STOP)
1685 break;
1687 switch (imsg.hdr.type) {
1688 case GOT_IMSG_TMPFD:
1689 if (basefile == NULL) {
1690 err = receive_tempfile(&basefile, "w+",
1691 &imsg, &ibuf);
1692 } else if (accumfile == NULL) {
1693 err = receive_tempfile(&accumfile, "w+",
1694 &imsg, &ibuf);
1695 } else
1696 err = got_error(GOT_ERR_PRIVSEP_MSG);
1697 break;
1698 case GOT_IMSG_PACKED_OBJECT_REQUEST:
1699 err = object_request(&imsg, &ibuf, pack, packidx,
1700 &objcache);
1701 break;
1702 case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1703 if (basefile == NULL || accumfile == NULL) {
1704 err = got_error(GOT_ERR_PRIVSEP_MSG);
1705 break;
1707 err = raw_object_request(&imsg, &ibuf, pack, packidx,
1708 &objcache, basefile, accumfile);
1709 break;
1710 case GOT_IMSG_RAW_DELTA_OUTFD:
1711 if (delta_outfile != NULL) {
1712 err = got_error(GOT_ERR_PRIVSEP_MSG);
1713 break;
1715 err = receive_tempfile(&delta_outfile, "w",
1716 &imsg, &ibuf);
1717 break;
1718 case GOT_IMSG_RAW_DELTA_REQUEST:
1719 if (delta_outfile == NULL) {
1720 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1721 break;
1723 err = raw_delta_request(&imsg, &ibuf, delta_outfile,
1724 pack, packidx);
1725 break;
1726 case GOT_IMSG_DELTA_REUSE_REQUEST:
1727 if (delta_outfile == NULL) {
1728 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1729 break;
1731 err = delta_reuse_request(&imsg, &ibuf,
1732 delta_outfile, pack, packidx);
1733 break;
1734 case GOT_IMSG_COMMIT_REQUEST:
1735 err = commit_request(&imsg, &ibuf, pack, packidx,
1736 &objcache);
1737 break;
1738 case GOT_IMSG_TREE_REQUEST:
1739 err = tree_request(&imsg, &ibuf, pack, packidx,
1740 &objcache);
1741 break;
1742 case GOT_IMSG_BLOB_REQUEST:
1743 if (basefile == NULL || accumfile == NULL) {
1744 err = got_error(GOT_ERR_PRIVSEP_MSG);
1745 break;
1747 err = blob_request(&imsg, &ibuf, pack, packidx,
1748 &objcache, basefile, accumfile);
1749 break;
1750 case GOT_IMSG_TAG_REQUEST:
1751 err = tag_request(&imsg, &ibuf, pack, packidx,
1752 &objcache);
1753 break;
1754 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1755 err = commit_traversal_request(&imsg, &ibuf, pack,
1756 packidx, &objcache);
1757 break;
1758 case GOT_IMSG_OBJECT_ENUMERATION_REQUEST:
1759 err = enumeration_request(&imsg, &ibuf, pack,
1760 packidx, &objcache);
1761 break;
1762 default:
1763 err = got_error(GOT_ERR_PRIVSEP_MSG);
1764 break;
1767 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
1768 err = got_error_from_errno("close");
1769 imsg_free(&imsg);
1770 if (err)
1771 break;
1774 if (packidx)
1775 got_packidx_close(packidx);
1776 if (pack)
1777 got_pack_close(pack);
1778 got_object_cache_close(&objcache);
1779 imsg_clear(&ibuf);
1780 if (basefile && fclose(basefile) == EOF && err == NULL)
1781 err = got_error_from_errno("fclose");
1782 if (accumfile && fclose(accumfile) == EOF && err == NULL)
1783 err = got_error_from_errno("fclose");
1784 if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
1785 err = got_error_from_errno("fclose");
1786 if (err) {
1787 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1788 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1789 got_privsep_send_error(&ibuf, err);
1792 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
1793 err = got_error_from_errno("close");
1794 return err ? 1 : 0;