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/queue.h>
20 #include <sys/uio.h>
21 #include <sys/time.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_object_idset.h"
45 #include "got_lib_privsep.h"
46 #include "got_lib_pack.h"
48 static volatile sig_atomic_t sigint_received;
50 static void
51 catch_sigint(int signo)
52 {
53 sigint_received = 1;
54 }
56 static const struct got_error *
57 open_object(struct got_object **obj, struct got_pack *pack,
58 struct got_packidx *packidx, int idx, struct got_object_id *id,
59 struct got_object_cache *objcache)
60 {
61 const struct got_error *err;
63 err = got_packfile_open_object(obj, pack, packidx, idx, id);
64 if (err)
65 return err;
66 (*obj)->refcnt++;
68 err = got_object_cache_add(objcache, id, *obj);
69 if (err) {
70 if (err->code == GOT_ERR_OBJ_EXISTS ||
71 err->code == GOT_ERR_OBJ_TOO_LARGE)
72 err = NULL;
73 return err;
74 }
75 (*obj)->refcnt++;
76 return NULL;
77 }
79 static const struct got_error *
80 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
81 struct got_packidx *packidx, struct got_object_cache *objcache)
82 {
83 const struct got_error *err = NULL;
84 struct got_imsg_packed_object iobj;
85 struct got_object *obj;
86 struct got_object_id id;
87 size_t datalen;
89 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
90 if (datalen != sizeof(iobj))
91 return got_error(GOT_ERR_PRIVSEP_LEN);
92 memcpy(&iobj, imsg->data, sizeof(iobj));
93 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
95 obj = got_object_cache_get(objcache, &id);
96 if (obj) {
97 obj->refcnt++;
98 } else {
99 err = open_object(&obj, pack, packidx, iobj.idx, &id,
100 objcache);
101 if (err)
102 goto done;
105 err = got_privsep_send_obj(ibuf, obj);
106 done:
107 got_object_close(obj);
108 return err;
111 static const struct got_error *
112 open_commit(struct got_commit_object **commit, struct got_pack *pack,
113 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
114 struct got_object_cache *objcache)
116 const struct got_error *err = NULL;
117 struct got_object *obj = NULL;
118 uint8_t *buf = NULL;
119 size_t len;
121 *commit = NULL;
123 obj = got_object_cache_get(objcache, id);
124 if (obj) {
125 obj->refcnt++;
126 } else {
127 err = open_object(&obj, pack, packidx, obj_idx, id,
128 objcache);
129 if (err)
130 return err;
133 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
134 if (err)
135 goto done;
137 obj->size = len;
139 err = got_object_parse_commit(commit, buf, len);
140 done:
141 got_object_close(obj);
142 free(buf);
143 return err;
146 static const struct got_error *
147 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
148 struct got_packidx *packidx, struct got_object_cache *objcache)
150 const struct got_error *err = NULL;
151 struct got_imsg_packed_object iobj;
152 struct got_commit_object *commit = NULL;
153 struct got_object_id id;
154 size_t datalen;
156 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
157 if (datalen != sizeof(iobj))
158 return got_error(GOT_ERR_PRIVSEP_LEN);
159 memcpy(&iobj, imsg->data, sizeof(iobj));
160 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
162 err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
163 if (err)
164 goto done;
166 err = got_privsep_send_commit(ibuf, commit);
167 done:
168 if (commit)
169 got_object_commit_close(commit);
170 if (err) {
171 if (err->code == GOT_ERR_PRIVSEP_PIPE)
172 err = NULL;
173 else
174 got_privsep_send_error(ibuf, err);
177 return err;
180 static const struct got_error *
181 open_tree(uint8_t **buf, struct got_parsed_tree_entry **entries, int *nentries,
182 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
183 struct got_object_id *id, struct got_object_cache *objcache)
185 const struct got_error *err = NULL;
186 struct got_object *obj = NULL;
187 size_t len;
189 *buf = NULL;
190 *nentries = 0;
192 obj = got_object_cache_get(objcache, id);
193 if (obj) {
194 obj->refcnt++;
195 } else {
196 err = open_object(&obj, pack, packidx, obj_idx, id,
197 objcache);
198 if (err)
199 return err;
202 err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
203 if (err)
204 goto done;
206 obj->size = len;
208 err = got_object_parse_tree(entries, nentries, *buf, len);
209 done:
210 got_object_close(obj);
211 if (err) {
212 free(*buf);
213 *buf = NULL;
215 return err;
218 static const struct got_error *
219 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
220 struct got_packidx *packidx, struct got_object_cache *objcache)
222 const struct got_error *err = NULL;
223 struct got_imsg_packed_object iobj;
224 struct got_parsed_tree_entry *entries = NULL;
225 int nentries = 0;
226 uint8_t *buf = NULL;
227 struct got_object_id id;
228 size_t datalen;
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 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, uint32_t 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 receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
293 struct imsgbuf *ibuf)
295 size_t datalen;
297 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
298 if (datalen != 0)
299 return got_error(GOT_ERR_PRIVSEP_LEN);
301 if (imsg->fd == -1)
302 return got_error(GOT_ERR_PRIVSEP_NO_FD);
304 *f = fdopen(imsg->fd, mode);
305 if (*f == NULL)
306 return got_error_from_errno("fdopen");
307 imsg->fd = -1;
309 return NULL;
312 static const struct got_error *
313 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
314 struct got_packidx *packidx, struct got_object_cache *objcache,
315 FILE *basefile, FILE *accumfile)
317 const struct got_error *err = NULL;
318 struct got_imsg_packed_object iobj;
319 struct got_object *obj = NULL;
320 FILE *outfile = NULL;
321 struct got_object_id id;
322 size_t datalen;
323 uint64_t blob_size;
324 uint8_t *buf = NULL;
326 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
327 if (datalen != sizeof(iobj))
328 return got_error(GOT_ERR_PRIVSEP_LEN);
329 memcpy(&iobj, imsg->data, sizeof(iobj));
330 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
332 obj = got_object_cache_get(objcache, &id);
333 if (obj) {
334 obj->refcnt++;
335 } else {
336 err = open_object(&obj, pack, packidx, iobj.idx, &id,
337 objcache);
338 if (err)
339 return err;
342 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
343 if (err)
344 goto done;
346 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
347 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
348 if (err)
349 goto done;
350 } else
351 blob_size = obj->size;
353 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
354 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
355 obj, pack);
356 else
357 err = got_packfile_extract_object(pack, obj, outfile, basefile,
358 accumfile);
359 if (err)
360 goto done;
362 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
363 done:
364 free(buf);
365 if (outfile && fclose(outfile) == EOF && err == NULL)
366 err = got_error_from_errno("fclose");
367 got_object_close(obj);
368 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
369 got_privsep_send_error(ibuf, err);
371 return err;
374 static const struct got_error *
375 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
376 struct got_packidx *packidx, struct got_object_cache *objcache)
378 const struct got_error *err = NULL;
379 struct got_imsg_packed_object iobj;
380 struct got_object *obj = NULL;
381 struct got_tag_object *tag = NULL;
382 uint8_t *buf = NULL;
383 size_t len;
384 struct got_object_id id;
385 size_t datalen;
387 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
388 if (datalen != sizeof(iobj))
389 return got_error(GOT_ERR_PRIVSEP_LEN);
390 memcpy(&iobj, imsg->data, sizeof(iobj));
391 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
393 obj = got_object_cache_get(objcache, &id);
394 if (obj) {
395 obj->refcnt++;
396 } else {
397 err = open_object(&obj, pack, packidx, iobj.idx, &id,
398 objcache);
399 if (err)
400 return err;
403 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
404 if (err)
405 goto done;
407 obj->size = len;
408 err = got_object_parse_tag(&tag, buf, len);
409 if (err)
410 goto done;
412 err = got_privsep_send_tag(ibuf, tag);
413 done:
414 free(buf);
415 got_object_close(obj);
416 if (tag)
417 got_object_tag_close(tag);
418 if (err) {
419 if (err->code == GOT_ERR_PRIVSEP_PIPE)
420 err = NULL;
421 else
422 got_privsep_send_error(ibuf, err);
425 return err;
428 static struct got_parsed_tree_entry *
429 find_entry_by_name(struct got_parsed_tree_entry *entries, int nentries,
430 const char *name, size_t len)
432 struct got_parsed_tree_entry *pte;
433 int cmp, i;
435 /* Note that tree entries are sorted in strncmp() order. */
436 for (i = 0; i < nentries; i++) {
437 pte = &entries[i];
438 cmp = strncmp(pte->name, name, len);
439 if (cmp < 0)
440 continue;
441 if (cmp > 0)
442 break;
443 if (pte->name[len] == '\0')
444 return pte;
446 return NULL;
449 static const struct got_error *
450 tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
451 struct got_parsed_tree_entry **entries1, int *nentries1,
452 struct got_parsed_tree_entry **entries2, int *nentries2,
453 const char *path, struct got_pack *pack, struct got_packidx *packidx,
454 struct imsgbuf *ibuf, struct got_object_cache *objcache)
456 const struct got_error *err = NULL;
457 struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
458 const char *seg, *s;
459 size_t seglen;
461 *changed = 0;
463 /* We not do support comparing the root path. */
464 if (got_path_is_root_dir(path))
465 return got_error_path(path, GOT_ERR_BAD_PATH);
467 s = path;
468 while (*s == '/')
469 s++;
470 seg = s;
471 seglen = 0;
472 while (*s) {
473 if (*s != '/') {
474 s++;
475 seglen++;
476 if (*s)
477 continue;
480 pte1 = find_entry_by_name(*entries1, *nentries1, seg, seglen);
481 if (pte1 == NULL) {
482 err = got_error(GOT_ERR_NO_OBJ);
483 break;
486 pte2 = find_entry_by_name(*entries2, *nentries2, seg, seglen);
487 if (pte2 == NULL) {
488 *changed = 1;
489 break;
492 if (pte1->mode != pte2->mode) {
493 *changed = 1;
494 break;
497 if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
498 *changed = 0;
499 break;
502 if (*s == '\0') { /* final path element */
503 *changed = 1;
504 break;
507 seg = s + 1;
508 s++;
509 seglen = 0;
510 if (*s) {
511 struct got_object_id id1, id2;
512 int idx;
514 memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
515 idx = got_packidx_get_object_idx(packidx, &id1);
516 if (idx == -1) {
517 err = got_error_no_obj(&id1);
518 break;
520 free(*entries1);
521 *nentries1 = 0;
522 free(*buf1);
523 *buf1 = NULL;
524 err = open_tree(buf1, entries1, nentries1, pack,
525 packidx, idx, &id1, objcache);
526 pte1 = NULL;
527 if (err)
528 break;
530 memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
531 idx = got_packidx_get_object_idx(packidx, &id2);
532 if (idx == -1) {
533 err = got_error_no_obj(&id2);
534 break;
536 free(*entries2);
537 *nentries2 = 0;
538 free(*buf2);
539 *buf2 = NULL;
540 err = open_tree(buf2, entries2, nentries2, pack,
541 packidx, idx, &id2, objcache);
542 pte2 = NULL;
543 if (err)
544 break;
548 return err;
551 static const struct got_error *
552 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
553 struct imsgbuf *ibuf)
555 struct ibuf *wbuf;
556 size_t i;
558 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
559 sizeof(struct got_imsg_traversed_commits) +
560 ncommits * SHA1_DIGEST_LENGTH);
561 if (wbuf == NULL)
562 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
564 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
565 return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
567 for (i = 0; i < ncommits; i++) {
568 struct got_object_id *id = &commit_ids[i];
569 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
570 return got_error_from_errno(
571 "imsg_add TRAVERSED_COMMITS");
575 wbuf->fd = -1;
576 imsg_close(ibuf, wbuf);
578 return got_privsep_flush_imsg(ibuf);
581 static const struct got_error *
582 send_commit_traversal_done(struct imsgbuf *ibuf)
584 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
585 NULL, 0) == -1)
586 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
588 return got_privsep_flush_imsg(ibuf);
591 static const struct got_error *
592 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
593 struct got_pack *pack, struct got_packidx *packidx,
594 struct got_object_cache *objcache)
596 const struct got_error *err = NULL;
597 struct got_imsg_packed_object iobj;
598 struct got_object_qid *pid;
599 struct got_commit_object *commit = NULL, *pcommit = NULL;
600 struct got_parsed_tree_entry *entries = NULL, *pentries = NULL;
601 int nentries = 0, pnentries = 0;
602 struct got_object_id id;
603 size_t datalen, path_len;
604 char *path = NULL;
605 const int min_alloc = 64;
606 int changed = 0, ncommits = 0, nallocated = 0;
607 struct got_object_id *commit_ids = NULL;
609 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
610 if (datalen < sizeof(iobj))
611 return got_error(GOT_ERR_PRIVSEP_LEN);
612 memcpy(&iobj, imsg->data, sizeof(iobj));
613 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
615 path_len = datalen - sizeof(iobj) - 1;
616 if (path_len < 0)
617 return got_error(GOT_ERR_PRIVSEP_LEN);
618 if (path_len > 0) {
619 path = imsg->data + sizeof(iobj);
620 if (path[path_len] != '\0')
621 return got_error(GOT_ERR_PRIVSEP_LEN);
624 nallocated = min_alloc;
625 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
626 if (commit_ids == NULL)
627 return got_error_from_errno("reallocarray");
629 do {
630 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
631 int idx;
633 if (sigint_received) {
634 err = got_error(GOT_ERR_CANCELLED);
635 goto done;
638 if (commit == NULL) {
639 idx = got_packidx_get_object_idx(packidx, &id);
640 if (idx == -1)
641 break;
642 err = open_commit(&commit, pack, packidx,
643 idx, &id, objcache);
644 if (err) {
645 if (err->code != GOT_ERR_NO_OBJ)
646 goto done;
647 err = NULL;
648 break;
652 if (sizeof(struct got_imsg_traversed_commits) +
653 ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
654 err = send_traversed_commits(commit_ids, ncommits,
655 ibuf);
656 if (err)
657 goto done;
658 ncommits = 0;
660 ncommits++;
661 if (ncommits > nallocated) {
662 struct got_object_id *new;
663 nallocated += min_alloc;
664 new = reallocarray(commit_ids, nallocated,
665 sizeof(*commit_ids));
666 if (new == NULL) {
667 err = got_error_from_errno("reallocarray");
668 goto done;
670 commit_ids = new;
672 memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
673 SHA1_DIGEST_LENGTH);
675 pid = STAILQ_FIRST(&commit->parent_ids);
676 if (pid == NULL)
677 break;
679 idx = got_packidx_get_object_idx(packidx, &pid->id);
680 if (idx == -1)
681 break;
683 err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
684 objcache);
685 if (err) {
686 if (err->code != GOT_ERR_NO_OBJ)
687 goto done;
688 err = NULL;
689 break;
692 if (path[0] == '/' && path[1] == '\0') {
693 if (got_object_id_cmp(pcommit->tree_id,
694 commit->tree_id) != 0) {
695 changed = 1;
696 break;
698 } else {
699 int pidx;
700 uint8_t *buf = NULL, *pbuf = NULL;
702 idx = got_packidx_get_object_idx(packidx,
703 commit->tree_id);
704 if (idx == -1)
705 break;
706 pidx = got_packidx_get_object_idx(packidx,
707 pcommit->tree_id);
708 if (pidx == -1)
709 break;
711 err = open_tree(&buf, &entries, &nentries, pack,
712 packidx, idx, commit->tree_id, objcache);
713 if (err)
714 goto done;
715 err = open_tree(&pbuf, &pentries, &pnentries, pack,
716 packidx, pidx, pcommit->tree_id, objcache);
717 if (err) {
718 free(buf);
719 goto done;
722 err = tree_path_changed(&changed, &buf, &pbuf,
723 &entries, &nentries, &pentries, &pnentries, path,
724 pack, packidx, ibuf, objcache);
726 free(entries);
727 entries = NULL;
728 nentries = 0;
729 free(buf);
730 free(pentries);
731 pentries = NULL;
732 pnentries = 0;
733 free(pbuf);
734 if (err) {
735 if (err->code != GOT_ERR_NO_OBJ)
736 goto done;
737 err = NULL;
738 break;
742 if (!changed) {
743 memcpy(id.sha1, pid->id.sha1, SHA1_DIGEST_LENGTH);
744 got_object_commit_close(commit);
745 commit = pcommit;
746 pcommit = NULL;
748 } while (!changed);
750 if (ncommits > 0) {
751 err = send_traversed_commits(commit_ids, ncommits, ibuf);
752 if (err)
753 goto done;
755 if (changed) {
756 err = got_privsep_send_commit(ibuf, commit);
757 if (err)
758 goto done;
761 err = send_commit_traversal_done(ibuf);
762 done:
763 free(commit_ids);
764 if (commit)
765 got_object_commit_close(commit);
766 if (pcommit)
767 got_object_commit_close(pcommit);
768 free(entries);
769 free(pentries);
770 if (err) {
771 if (err->code == GOT_ERR_PRIVSEP_PIPE)
772 err = NULL;
773 else
774 got_privsep_send_error(ibuf, err);
777 return err;
780 static const struct got_error *
781 raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
782 struct got_pack *pack, struct got_packidx *packidx,
783 struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
785 const struct got_error *err = NULL;
786 uint8_t *buf = NULL;
787 uint64_t size = 0;
788 FILE *outfile = NULL;
789 struct got_imsg_packed_object iobj;
790 struct got_object *obj;
791 struct got_object_id id;
792 size_t datalen;
794 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
795 if (datalen != sizeof(iobj))
796 return got_error(GOT_ERR_PRIVSEP_LEN);
797 memcpy(&iobj, imsg->data, sizeof(iobj));
798 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
800 obj = got_object_cache_get(objcache, &id);
801 if (obj) {
802 obj->refcnt++;
803 } else {
804 err = open_object(&obj, pack, packidx, iobj.idx, &id,
805 objcache);
806 if (err)
807 return err;
810 err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
811 if (err)
812 return err;
814 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
815 err = got_pack_get_max_delta_object_size(&size, obj, pack);
816 if (err)
817 goto done;
818 } else
819 size = obj->size;
821 if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
822 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
823 obj, pack);
824 else
825 err = got_packfile_extract_object(pack, obj, outfile, basefile,
826 accumfile);
827 if (err)
828 goto done;
830 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
831 done:
832 free(buf);
833 if (outfile && fclose(outfile) == EOF && err == NULL)
834 err = got_error_from_errno("fclose");
835 got_object_close(obj);
836 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
837 got_privsep_send_error(ibuf, err);
839 return err;
842 static const struct got_error *
843 get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
844 off_t base_offset)
846 const struct got_error *err;
847 int idx;
849 err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
850 if (err)
851 return err;
852 if (idx == -1)
853 return got_error(GOT_ERR_BAD_PACKIDX);
855 return got_packidx_get_object_id(base_id, packidx, idx);
858 static const struct got_error *
859 raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
860 FILE *delta_outfile, struct got_pack *pack,
861 struct got_packidx *packidx)
863 const struct got_error *err = NULL;
864 struct got_imsg_raw_delta_request req;
865 size_t datalen, delta_size, delta_compressed_size;
866 off_t delta_offset;
867 uint8_t *delta_buf = NULL;
868 struct got_object_id id, base_id;
869 off_t base_offset, delta_out_offset = 0;
870 uint64_t base_size = 0, result_size = 0;
871 size_t w;
873 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
874 if (datalen != sizeof(req))
875 return got_error(GOT_ERR_PRIVSEP_LEN);
876 memcpy(&req, imsg->data, sizeof(req));
877 memcpy(id.sha1, req.id, SHA1_DIGEST_LENGTH);
879 imsg->fd = -1;
881 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
882 &delta_compressed_size, &delta_offset, &base_offset, &base_id,
883 &base_size, &result_size, pack, packidx, req.idx);
884 if (err)
885 goto done;
887 /*
888 * If this is an offset delta we must determine the base
889 * object ID ourselves.
890 */
891 if (base_offset != 0) {
892 err = get_base_object_id(&base_id, packidx, base_offset);
893 if (err)
894 goto done;
897 delta_out_offset = ftello(delta_outfile);
898 w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
899 if (w != delta_compressed_size) {
900 err = got_ferror(delta_outfile, GOT_ERR_IO);
901 goto done;
903 if (fflush(delta_outfile) == -1) {
904 err = got_error_from_errno("fflush");
905 goto done;
908 err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
909 delta_size, delta_compressed_size, delta_offset, delta_out_offset,
910 &base_id);
911 done:
912 free(delta_buf);
913 return err;
916 struct search_deltas_arg {
917 struct imsgbuf *ibuf;
918 struct got_packidx *packidx;
919 struct got_pack *pack;
920 struct got_object_idset *idset;
921 FILE *delta_outfile;
922 struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
923 size_t ndeltas;
924 };
926 static const struct got_error *
927 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
929 const struct got_error *err;
930 struct search_deltas_arg *a = arg;
931 int obj_idx;
932 uint8_t *delta_buf = NULL;
933 uint64_t base_size, result_size;
934 size_t delta_size, delta_compressed_size;
935 off_t delta_offset, base_offset;
936 struct got_object_id base_id;
938 if (sigint_received)
939 return got_error(GOT_ERR_CANCELLED);
941 obj_idx = got_packidx_get_object_idx(a->packidx, id);
942 if (obj_idx == -1)
943 return NULL; /* object not present in our pack file */
945 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
946 &delta_compressed_size, &delta_offset, &base_offset, &base_id,
947 &base_size, &result_size, a->pack, a->packidx, obj_idx);
948 if (err) {
949 if (err->code == GOT_ERR_OBJ_TYPE)
950 return NULL; /* object not stored as a delta */
951 return err;
954 /*
955 * If this is an offset delta we must determine the base
956 * object ID ourselves.
957 */
958 if (base_offset != 0) {
959 err = get_base_object_id(&base_id, a->packidx, base_offset);
960 if (err)
961 goto done;
964 if (got_object_idset_contains(a->idset, &base_id)) {
965 struct got_imsg_reused_delta *delta;
966 off_t delta_out_offset = ftello(a->delta_outfile);
967 size_t w;
969 w = fwrite(delta_buf, 1, delta_compressed_size,
970 a->delta_outfile);
971 if (w != delta_compressed_size) {
972 err = got_ferror(a->delta_outfile, GOT_ERR_IO);
973 goto done;
976 delta = &a->deltas[a->ndeltas++];
977 memcpy(&delta->id, id, sizeof(delta->id));
978 memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
979 delta->base_size = base_size;
980 delta->result_size = result_size;
981 delta->delta_size = delta_size;
982 delta->delta_compressed_size = delta_compressed_size;
983 delta->delta_offset = delta_offset;
984 delta->delta_out_offset = delta_out_offset;
986 if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
987 err = got_privsep_send_reused_deltas(a->ibuf,
988 a->deltas, a->ndeltas);
989 if (err)
990 goto done;
991 a->ndeltas = 0;
994 done:
995 free(delta_buf);
996 return err;
999 static const struct got_error *
1000 recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
1002 const struct got_error *err = NULL;
1003 int done = 0;
1004 struct got_object_id *ids;
1005 size_t nids, i;
1007 for (;;) {
1008 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1009 if (err || done)
1010 break;
1011 for (i = 0; i < nids; i++) {
1012 err = got_object_idset_add(idset, &ids[i], NULL);
1013 if (err) {
1014 free(ids);
1015 return err;
1018 free(ids);
1021 return err;
1024 static const struct got_error *
1025 recv_object_id_queue(struct got_object_id_queue *queue, struct imsgbuf *ibuf)
1027 const struct got_error *err = NULL;
1028 int done = 0;
1029 struct got_object_qid *qid;
1030 struct got_object_id *ids;
1031 size_t nids, i;
1033 for (;;) {
1034 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1035 if (err || done)
1036 break;
1037 for (i = 0; i < nids; i++) {
1038 err = got_object_qid_alloc_partial(&qid);
1039 if (err)
1040 return err;
1041 memcpy(&qid->id, &ids[i], sizeof(qid->id));
1042 STAILQ_INSERT_TAIL(queue, qid, entry);
1046 return err;
1049 static const struct got_error *
1050 delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1051 FILE *delta_outfile, struct got_pack *pack, struct got_packidx *packidx)
1053 const struct got_error *err = NULL;
1054 struct got_object_idset *idset;
1055 struct search_deltas_arg sda;
1057 idset = got_object_idset_alloc();
1058 if (idset == NULL)
1059 return got_error_from_errno("got_object_idset_alloc");
1061 err = recv_object_ids(idset, ibuf);
1062 if (err)
1063 return err;
1065 memset(&sda, 0, sizeof(sda));
1066 sda.ibuf = ibuf;
1067 sda.idset = idset;
1068 sda.pack = pack;
1069 sda.packidx = packidx;
1070 sda.delta_outfile = delta_outfile;
1071 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1072 if (err)
1073 goto done;
1075 if (sda.ndeltas > 0) {
1076 err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1077 sda.ndeltas);
1078 if (err)
1079 goto done;
1082 if (fflush(delta_outfile) == -1) {
1083 err = got_error_from_errno("fflush");
1084 goto done;
1087 err = got_privsep_send_reused_deltas_done(ibuf);
1088 done:
1089 got_object_idset_free(idset);
1090 return err;
1093 static const struct got_error *
1094 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1096 const struct got_error *err = NULL;
1097 struct imsg imsg;
1098 struct got_imsg_packidx ipackidx;
1099 size_t datalen;
1100 struct got_packidx *p;
1102 *packidx = NULL;
1104 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1105 if (err)
1106 return err;
1108 p = calloc(1, sizeof(*p));
1109 if (p == NULL) {
1110 err = got_error_from_errno("calloc");
1111 goto done;
1114 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1115 err = got_error(GOT_ERR_PRIVSEP_MSG);
1116 goto done;
1119 if (imsg.fd == -1) {
1120 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1121 goto done;
1124 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1125 if (datalen != sizeof(ipackidx)) {
1126 err = got_error(GOT_ERR_PRIVSEP_LEN);
1127 goto done;
1129 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1131 p->len = ipackidx.len;
1132 p->fd = dup(imsg.fd);
1133 if (p->fd == -1) {
1134 err = got_error_from_errno("dup");
1135 goto done;
1137 if (lseek(p->fd, 0, SEEK_SET) == -1) {
1138 err = got_error_from_errno("lseek");
1139 goto done;
1142 #ifndef GOT_PACK_NO_MMAP
1143 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1144 if (p->map == MAP_FAILED)
1145 p->map = NULL; /* fall back to read(2) */
1146 #endif
1147 err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1148 done:
1149 if (err) {
1150 if (imsg.fd != -1)
1151 close(imsg.fd);
1152 got_packidx_close(p);
1153 } else
1154 *packidx = p;
1155 imsg_free(&imsg);
1156 return err;
1159 static const struct got_error *
1160 send_tree_enumeration_done(struct imsgbuf *ibuf)
1162 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENUMERATION_DONE, 0, 0, -1,
1163 NULL, 0) == -1)
1164 return got_error_from_errno("imsg_compose TREE_ENUMERATION_DONE");
1166 return got_privsep_flush_imsg(ibuf);
1169 struct enumerated_tree {
1170 struct got_object_id id;
1171 char *path;
1172 uint8_t *buf;
1173 struct got_parsed_tree_entry *entries;
1174 int nentries;
1177 static const struct got_error *
1178 enumerate_tree(int *have_all_entries, struct imsgbuf *ibuf, size_t *totlen,
1179 struct got_object_id *tree_id,
1180 const char *path, struct got_pack *pack, struct got_packidx *packidx,
1181 struct got_object_cache *objcache, struct got_object_idset *idset,
1182 struct enumerated_tree **trees, size_t *nalloc, size_t *ntrees)
1184 const struct got_error *err = NULL;
1185 struct got_object_id_queue ids;
1186 struct got_object_qid *qid;
1187 uint8_t *buf = NULL;
1188 struct got_parsed_tree_entry *entries = NULL;
1189 int nentries = 0, i;
1190 struct enumerated_tree *tree;
1192 *ntrees = 0;
1193 *have_all_entries = 1;
1194 STAILQ_INIT(&ids);
1196 err = got_object_qid_alloc_partial(&qid);
1197 if (err)
1198 return err;
1199 memcpy(&qid->id.sha1, tree_id, SHA1_DIGEST_LENGTH);
1200 qid->data = strdup(path);
1201 if (qid->data == NULL) {
1202 err = got_error_from_errno("strdup");
1203 goto done;
1205 STAILQ_INSERT_TAIL(&ids, qid, entry);
1206 qid = NULL;
1208 /* Traverse the tree hierarchy, gather tree object IDs and paths. */
1209 do {
1210 const char *path;
1211 int idx, i;
1213 if (sigint_received) {
1214 err = got_error(GOT_ERR_CANCELLED);
1215 goto done;
1218 qid = STAILQ_FIRST(&ids);
1219 STAILQ_REMOVE_HEAD(&ids, entry);
1220 path = qid->data;
1222 idx = got_packidx_get_object_idx(packidx, &qid->id);
1223 if (idx == -1) {
1224 *have_all_entries = 0;
1225 break;
1228 err = open_tree(&buf, &entries, &nentries,
1229 pack, packidx, idx, &qid->id, objcache);
1230 if (err) {
1231 if (err->code != GOT_ERR_NO_OBJ)
1232 goto done;
1235 err = got_object_idset_add(idset, &qid->id, NULL);
1236 if (err)
1237 goto done;
1239 for (i = 0; i < nentries; i++) {
1240 struct got_object_qid *eqid = NULL;
1241 struct got_parsed_tree_entry *pte = &entries[i];
1242 char *p;
1244 if (!S_ISDIR(pte->mode))
1245 continue;
1247 err = got_object_qid_alloc_partial(&eqid);
1248 if (err)
1249 goto done;
1250 memcpy(eqid->id.sha1, pte->id, sizeof(eqid->id.sha1));
1252 if (got_object_idset_contains(idset, &eqid->id)) {
1253 got_object_qid_free(eqid);
1254 continue;
1257 if (asprintf(&p, "%s%s%s", path,
1258 got_path_is_root_dir(path) ? "" : "/",
1259 pte->name) == -1) {
1260 err = got_error_from_errno("asprintf");
1261 got_object_qid_free(eqid);
1262 goto done;
1264 eqid->data = p;
1265 STAILQ_INSERT_TAIL(&ids, eqid, entry);
1268 if (*ntrees >= *nalloc) {
1269 struct enumerated_tree *new;
1270 new = recallocarray(*trees, *nalloc, *nalloc + 16,
1271 sizeof(*new));
1272 if (new == NULL) {
1273 err = got_error_from_errno("malloc");
1274 goto done;
1276 *trees = new;
1277 *nalloc += 16;
1279 tree = &(*trees)[*ntrees];
1280 (*ntrees)++;
1281 memcpy(&tree->id, &qid->id, sizeof(tree->id));
1282 tree->path = qid->data;
1283 tree->buf = buf;
1284 buf = NULL;
1285 tree->entries = entries;
1286 entries = NULL;
1287 tree->nentries = nentries;
1289 got_object_qid_free(qid);
1290 qid = NULL;
1291 } while (!STAILQ_EMPTY(&ids));
1293 if (*have_all_entries) {
1294 int i;
1296 * We have managed to traverse all entries in the hierarchy.
1297 * Tell the main process what we have found.
1299 for (i = 0; i < *ntrees; i++) {
1300 tree = &(*trees)[i];
1301 err = got_privsep_send_enumerated_tree(totlen,
1302 ibuf, &tree->id, tree->path, tree->entries,
1303 tree->nentries);
1304 if (err)
1305 goto done;
1306 free(tree->buf);
1307 tree->buf = NULL;
1308 free(tree->path);
1309 tree->path = NULL;
1310 free(tree->entries);
1311 tree->entries = NULL;
1313 *ntrees = 0; /* don't loop again below to free memory */
1315 err = send_tree_enumeration_done(ibuf);
1316 } else {
1318 * We can only load fully packed tree hierarchies on
1319 * behalf of the main process, otherwise the main process
1320 * gets a wrong idea about which tree objects have
1321 * already been traversed.
1322 * Indicate a missing entry for the root of this tree.
1323 * The main process should continue by loading this
1324 * entire tree the slow way.
1326 err = got_privsep_send_enumerated_tree(totlen, ibuf,
1327 tree_id, "/", NULL, -1);
1328 if (err)
1329 goto done;
1331 done:
1332 free(buf);
1333 free(entries);
1334 for (i = 0; i < *ntrees; i++) {
1335 tree = &(*trees)[i];
1336 free(tree->buf);
1337 tree->buf = NULL;
1338 free(tree->path);
1339 tree->path = NULL;
1340 free(tree->entries);
1341 tree->entries = NULL;
1343 if (qid)
1344 free(qid->data);
1345 got_object_qid_free(qid);
1346 got_object_id_queue_free(&ids);
1347 if (err) {
1348 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1349 err = NULL;
1350 else
1351 got_privsep_send_error(ibuf, err);
1354 return err;
1357 static const struct got_error *
1358 enumeration_request(struct imsg *imsg, struct imsgbuf *ibuf,
1359 struct got_pack *pack, struct got_packidx *packidx,
1360 struct got_object_cache *objcache)
1362 const struct got_error *err = NULL;
1363 struct got_object_id_queue commit_ids;
1364 const struct got_object_id_queue *parents = NULL;
1365 struct got_object_qid *qid = NULL;
1366 struct got_object *obj = NULL;
1367 struct got_commit_object *commit = NULL;
1368 struct got_object_id *tree_id = NULL;
1369 size_t totlen = 0;
1370 struct got_object_idset *idset;
1371 int i, idx, have_all_entries = 1;
1372 struct enumerated_tree *trees = NULL;
1373 size_t ntrees = 0, nalloc = 16;
1375 STAILQ_INIT(&commit_ids);
1377 trees = calloc(nalloc, sizeof(*trees));
1378 if (trees == NULL)
1379 return got_error_from_errno("calloc");
1381 idset = got_object_idset_alloc();
1382 if (idset == NULL) {
1383 err = got_error_from_errno("got_object_idset_alloc");
1384 goto done;
1387 err = recv_object_id_queue(&commit_ids, ibuf);
1388 if (err)
1389 goto done;
1391 if (STAILQ_EMPTY(&commit_ids)) {
1392 err = got_error(GOT_ERR_PRIVSEP_MSG);
1393 goto done;
1396 err = recv_object_ids(idset, ibuf);
1397 if (err)
1398 goto done;
1400 while (!STAILQ_EMPTY(&commit_ids)) {
1401 if (sigint_received) {
1402 err = got_error(GOT_ERR_CANCELLED);
1403 goto done;
1406 qid = STAILQ_FIRST(&commit_ids);
1407 STAILQ_REMOVE_HEAD(&commit_ids, entry);
1409 if (got_object_idset_contains(idset, &qid->id)) {
1410 got_object_qid_free(qid);
1411 qid = NULL;
1412 continue;
1415 idx = got_packidx_get_object_idx(packidx, &qid->id);
1416 if (idx == -1) {
1417 have_all_entries = 0;
1418 break;
1421 err = open_object(&obj, pack, packidx, idx, &qid->id,
1422 objcache);
1423 if (err)
1424 goto done;
1425 if (obj->type == GOT_OBJ_TYPE_TAG) {
1426 struct got_tag_object *tag;
1427 uint8_t *buf;
1428 size_t len;
1429 err = got_packfile_extract_object_to_mem(&buf,
1430 &len, obj, pack);
1431 if (err)
1432 goto done;
1433 obj->size = len;
1434 err = got_object_parse_tag(&tag, buf, len);
1435 if (err) {
1436 free(buf);
1437 goto done;
1439 idx = got_packidx_get_object_idx(packidx, &tag->id);
1440 if (idx == -1) {
1441 have_all_entries = 0;
1442 break;
1444 err = open_commit(&commit, pack, packidx, idx,
1445 &tag->id, objcache);
1446 got_object_tag_close(tag);
1447 free(buf);
1448 if (err)
1449 goto done;
1450 } else if (obj->type == GOT_OBJ_TYPE_COMMIT) {
1451 err = open_commit(&commit, pack, packidx, idx,
1452 &qid->id, objcache);
1453 if (err)
1454 goto done;
1455 } else {
1456 err = got_error(GOT_ERR_OBJ_TYPE);
1457 goto done;
1459 got_object_close(obj);
1460 obj = NULL;
1462 err = got_privsep_send_enumerated_commit(ibuf, &qid->id,
1463 got_object_commit_get_committer_time(commit));
1464 if (err)
1465 goto done;
1467 tree_id = got_object_commit_get_tree_id(commit);
1468 idx = got_packidx_get_object_idx(packidx, tree_id);
1469 if (idx == -1) {
1470 have_all_entries = 0;
1471 err = got_privsep_send_enumerated_tree(&totlen, ibuf,
1472 tree_id, "/", NULL, -1);
1473 if (err)
1474 goto done;
1475 break;
1478 if (got_object_idset_contains(idset, tree_id)) {
1479 got_object_qid_free(qid);
1480 qid = NULL;
1481 continue;
1484 err = enumerate_tree(&have_all_entries, ibuf, &totlen, tree_id, "/",
1485 pack, packidx, objcache, idset, &trees, &nalloc, &ntrees);
1486 if (err)
1487 goto done;
1489 if (!have_all_entries)
1490 break;
1492 got_object_qid_free(qid);
1493 qid = NULL;
1495 parents = got_object_commit_get_parent_ids(commit);
1496 if (parents) {
1497 struct got_object_qid *pid;
1498 STAILQ_FOREACH(pid, parents, entry) {
1499 if (got_object_idset_contains(idset, &pid->id))
1500 continue;
1501 err = got_object_qid_alloc_partial(&qid);
1502 if (err)
1503 goto done;
1504 memcpy(&qid->id, &pid->id, sizeof(qid->id));
1505 STAILQ_INSERT_TAIL(&commit_ids, qid, entry);
1506 qid = NULL;
1510 got_object_commit_close(commit);
1511 commit = NULL;
1514 if (have_all_entries) {
1515 err = got_privsep_send_object_enumeration_done(ibuf);
1516 if (err)
1517 goto done;
1518 } else {
1519 err = got_privsep_send_object_enumeration_incomplete(ibuf);
1520 if (err)
1521 goto done;
1523 done:
1524 if (obj)
1525 got_object_close(obj);
1526 if (commit)
1527 got_object_commit_close(commit);
1528 got_object_qid_free(qid);
1529 got_object_id_queue_free(&commit_ids);
1530 if (idset)
1531 got_object_idset_free(idset);
1532 for (i = 0; i < ntrees; i++) {
1533 struct enumerated_tree *tree = &trees[i];
1534 free(tree->buf);
1535 free(tree->path);
1536 free(tree->entries);
1538 free(trees);
1539 return err;
1542 static const struct got_error *
1543 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1545 const struct got_error *err = NULL;
1546 struct imsg imsg;
1547 struct got_imsg_pack ipack;
1548 size_t datalen;
1549 struct got_pack *pack;
1551 *packp = NULL;
1553 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1554 if (err)
1555 return err;
1557 pack = calloc(1, sizeof(*pack));
1558 if (pack == NULL) {
1559 err = got_error_from_errno("calloc");
1560 goto done;
1563 if (imsg.hdr.type != GOT_IMSG_PACK) {
1564 err = got_error(GOT_ERR_PRIVSEP_MSG);
1565 goto done;
1568 if (imsg.fd == -1) {
1569 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1570 goto done;
1573 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1574 if (datalen != sizeof(ipack)) {
1575 err = got_error(GOT_ERR_PRIVSEP_LEN);
1576 goto done;
1578 memcpy(&ipack, imsg.data, sizeof(ipack));
1580 pack->filesize = ipack.filesize;
1581 pack->fd = dup(imsg.fd);
1582 if (pack->fd == -1) {
1583 err = got_error_from_errno("dup");
1584 goto done;
1586 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1587 err = got_error_from_errno("lseek");
1588 goto done;
1590 pack->path_packfile = strdup(ipack.path_packfile);
1591 if (pack->path_packfile == NULL) {
1592 err = got_error_from_errno("strdup");
1593 goto done;
1596 err = got_delta_cache_alloc(&pack->delta_cache);
1597 if (err)
1598 goto done;
1600 #ifndef GOT_PACK_NO_MMAP
1601 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1602 pack->fd, 0);
1603 if (pack->map == MAP_FAILED)
1604 pack->map = NULL; /* fall back to read(2) */
1605 #endif
1606 done:
1607 if (err) {
1608 if (imsg.fd != -1)
1609 close(imsg.fd);
1610 free(pack);
1611 } else
1612 *packp = pack;
1613 imsg_free(&imsg);
1614 return err;
1617 int
1618 main(int argc, char *argv[])
1620 const struct got_error *err = NULL;
1621 struct imsgbuf ibuf;
1622 struct imsg imsg;
1623 struct got_packidx *packidx = NULL;
1624 struct got_pack *pack = NULL;
1625 struct got_object_cache objcache;
1626 FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1628 //static int attached;
1629 //while (!attached) sleep(1);
1631 signal(SIGINT, catch_sigint);
1633 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1635 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1636 if (err) {
1637 err = got_error_from_errno("got_object_cache_init");
1638 got_privsep_send_error(&ibuf, err);
1639 return 1;
1642 #ifndef PROFILE
1643 /* revoke access to most system calls */
1644 if (pledge("stdio recvfd", NULL) == -1) {
1645 err = got_error_from_errno("pledge");
1646 got_privsep_send_error(&ibuf, err);
1647 return 1;
1649 #endif
1651 err = receive_packidx(&packidx, &ibuf);
1652 if (err) {
1653 got_privsep_send_error(&ibuf, err);
1654 return 1;
1657 err = receive_pack(&pack, &ibuf);
1658 if (err) {
1659 got_privsep_send_error(&ibuf, err);
1660 return 1;
1663 for (;;) {
1664 imsg.fd = -1;
1666 if (sigint_received) {
1667 err = got_error(GOT_ERR_CANCELLED);
1668 break;
1671 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1672 if (err) {
1673 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1674 err = NULL;
1675 break;
1678 if (imsg.hdr.type == GOT_IMSG_STOP)
1679 break;
1681 switch (imsg.hdr.type) {
1682 case GOT_IMSG_TMPFD:
1683 if (basefile == NULL) {
1684 err = receive_tempfile(&basefile, "w+",
1685 &imsg, &ibuf);
1686 } else if (accumfile == NULL) {
1687 err = receive_tempfile(&accumfile, "w+",
1688 &imsg, &ibuf);
1689 } else
1690 err = got_error(GOT_ERR_PRIVSEP_MSG);
1691 break;
1692 case GOT_IMSG_PACKED_OBJECT_REQUEST:
1693 err = object_request(&imsg, &ibuf, pack, packidx,
1694 &objcache);
1695 break;
1696 case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1697 if (basefile == NULL || accumfile == NULL) {
1698 err = got_error(GOT_ERR_PRIVSEP_MSG);
1699 break;
1701 err = raw_object_request(&imsg, &ibuf, pack, packidx,
1702 &objcache, basefile, accumfile);
1703 break;
1704 case GOT_IMSG_RAW_DELTA_OUTFD:
1705 if (delta_outfile != NULL) {
1706 err = got_error(GOT_ERR_PRIVSEP_MSG);
1707 break;
1709 err = receive_tempfile(&delta_outfile, "w",
1710 &imsg, &ibuf);
1711 break;
1712 case GOT_IMSG_RAW_DELTA_REQUEST:
1713 if (delta_outfile == NULL) {
1714 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1715 break;
1717 err = raw_delta_request(&imsg, &ibuf, delta_outfile,
1718 pack, packidx);
1719 break;
1720 case GOT_IMSG_DELTA_REUSE_REQUEST:
1721 if (delta_outfile == NULL) {
1722 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1723 break;
1725 err = delta_reuse_request(&imsg, &ibuf,
1726 delta_outfile, pack, packidx);
1727 break;
1728 case GOT_IMSG_COMMIT_REQUEST:
1729 err = commit_request(&imsg, &ibuf, pack, packidx,
1730 &objcache);
1731 break;
1732 case GOT_IMSG_TREE_REQUEST:
1733 err = tree_request(&imsg, &ibuf, pack, packidx,
1734 &objcache);
1735 break;
1736 case GOT_IMSG_BLOB_REQUEST:
1737 if (basefile == NULL || accumfile == NULL) {
1738 err = got_error(GOT_ERR_PRIVSEP_MSG);
1739 break;
1741 err = blob_request(&imsg, &ibuf, pack, packidx,
1742 &objcache, basefile, accumfile);
1743 break;
1744 case GOT_IMSG_TAG_REQUEST:
1745 err = tag_request(&imsg, &ibuf, pack, packidx,
1746 &objcache);
1747 break;
1748 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1749 err = commit_traversal_request(&imsg, &ibuf, pack,
1750 packidx, &objcache);
1751 break;
1752 case GOT_IMSG_OBJECT_ENUMERATION_REQUEST:
1753 err = enumeration_request(&imsg, &ibuf, pack,
1754 packidx, &objcache);
1755 break;
1756 default:
1757 err = got_error(GOT_ERR_PRIVSEP_MSG);
1758 break;
1761 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
1762 err = got_error_from_errno("close");
1763 imsg_free(&imsg);
1764 if (err)
1765 break;
1768 if (packidx)
1769 got_packidx_close(packidx);
1770 if (pack)
1771 got_pack_close(pack);
1772 got_object_cache_close(&objcache);
1773 imsg_clear(&ibuf);
1774 if (basefile && fclose(basefile) == EOF && err == NULL)
1775 err = got_error_from_errno("fclose");
1776 if (accumfile && fclose(accumfile) == EOF && err == NULL)
1777 err = got_error_from_errno("fclose");
1778 if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
1779 err = got_error_from_errno("fclose");
1780 if (err) {
1781 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1782 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1783 got_privsep_send_error(&ibuf, err);
1786 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
1787 err = got_error_from_errno("close");
1788 return err ? 1 : 0;