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/uio.h>
19 #include <sys/time.h>
20 #include <sys/mman.h>
22 #include <limits.h>
23 #include <signal.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <zlib.h>
31 #include "got_compat.h"
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_path.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_delta_cache.h"
39 #include "got_lib_object.h"
40 #include "got_lib_object_cache.h"
41 #include "got_lib_object_parse.h"
42 #include "got_lib_object_idset.h"
43 #include "got_lib_privsep.h"
44 #include "got_lib_pack.h"
46 static volatile sig_atomic_t sigint_received;
48 static void
49 catch_sigint(int signo)
50 {
51 sigint_received = 1;
52 }
54 static const struct got_error *
55 open_object(struct got_object **obj, struct got_pack *pack,
56 struct got_packidx *packidx, int idx, struct got_object_id *id,
57 struct got_object_cache *objcache)
58 {
59 const struct got_error *err;
61 err = got_packfile_open_object(obj, pack, packidx, idx, id);
62 if (err)
63 return err;
64 (*obj)->refcnt++;
66 err = got_object_cache_add(objcache, id, *obj);
67 if (err) {
68 if (err->code == GOT_ERR_OBJ_EXISTS ||
69 err->code == GOT_ERR_OBJ_TOO_LARGE)
70 err = NULL;
71 return err;
72 }
73 (*obj)->refcnt++;
74 return NULL;
75 }
77 static const struct got_error *
78 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
79 struct got_packidx *packidx, struct got_object_cache *objcache)
80 {
81 const struct got_error *err = NULL;
82 struct got_imsg_packed_object iobj;
83 struct got_object *obj;
84 struct got_object_id id;
85 size_t datalen;
87 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
88 if (datalen != sizeof(iobj))
89 return got_error(GOT_ERR_PRIVSEP_LEN);
90 memcpy(&iobj, imsg->data, sizeof(iobj));
91 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
93 obj = got_object_cache_get(objcache, &id);
94 if (obj) {
95 obj->refcnt++;
96 } else {
97 err = open_object(&obj, pack, packidx, iobj.idx, &id,
98 objcache);
99 if (err)
100 goto done;
103 err = got_privsep_send_obj(ibuf, obj);
104 done:
105 got_object_close(obj);
106 return err;
109 static const struct got_error *
110 open_commit(struct got_commit_object **commit, struct got_pack *pack,
111 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
112 struct got_object_cache *objcache)
114 const struct got_error *err = NULL;
115 struct got_object *obj = NULL;
116 uint8_t *buf = NULL;
117 size_t len;
119 *commit = NULL;
121 obj = got_object_cache_get(objcache, id);
122 if (obj) {
123 obj->refcnt++;
124 } else {
125 err = open_object(&obj, pack, packidx, obj_idx, id,
126 objcache);
127 if (err)
128 return err;
131 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
132 if (err)
133 goto done;
135 obj->size = len;
137 err = got_object_parse_commit(commit, buf, len);
138 done:
139 got_object_close(obj);
140 free(buf);
141 return err;
144 static const struct got_error *
145 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
146 struct got_packidx *packidx, struct got_object_cache *objcache)
148 const struct got_error *err = NULL;
149 struct got_imsg_packed_object iobj;
150 struct got_commit_object *commit = NULL;
151 struct got_object_id id;
152 size_t datalen;
154 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
155 if (datalen != sizeof(iobj))
156 return got_error(GOT_ERR_PRIVSEP_LEN);
157 memcpy(&iobj, imsg->data, sizeof(iobj));
158 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
160 err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
161 if (err)
162 goto done;
164 err = got_privsep_send_commit(ibuf, commit);
165 done:
166 if (commit)
167 got_object_commit_close(commit);
168 if (err) {
169 if (err->code == GOT_ERR_PRIVSEP_PIPE)
170 err = NULL;
171 else
172 got_privsep_send_error(ibuf, err);
175 return err;
178 static const struct got_error *
179 open_tree(uint8_t **buf, struct got_parsed_tree_entry **entries, int *nentries,
180 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
181 struct got_object_id *id, struct got_object_cache *objcache)
183 const struct got_error *err = NULL;
184 struct got_object *obj = NULL;
185 size_t len;
187 *buf = NULL;
188 *nentries = 0;
190 obj = got_object_cache_get(objcache, id);
191 if (obj) {
192 obj->refcnt++;
193 } else {
194 err = open_object(&obj, pack, packidx, obj_idx, id,
195 objcache);
196 if (err)
197 return err;
200 err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
201 if (err)
202 goto done;
204 obj->size = len;
206 err = got_object_parse_tree(entries, nentries, *buf, len);
207 done:
208 got_object_close(obj);
209 if (err) {
210 free(*buf);
211 *buf = NULL;
213 return err;
216 static const struct got_error *
217 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
218 struct got_packidx *packidx, struct got_object_cache *objcache)
220 const struct got_error *err = NULL;
221 struct got_imsg_packed_object iobj;
222 struct got_parsed_tree_entry *entries = NULL;
223 int nentries = 0;
224 uint8_t *buf = NULL;
225 struct got_object_id id;
226 size_t datalen;
228 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
229 if (datalen != sizeof(iobj))
230 return got_error(GOT_ERR_PRIVSEP_LEN);
231 memcpy(&iobj, imsg->data, sizeof(iobj));
232 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
234 err = open_tree(&buf, &entries, &nentries, pack, packidx, iobj.idx,
235 &id, objcache);
236 if (err)
237 return err;
239 err = got_privsep_send_tree(ibuf, entries, nentries);
240 free(entries);
241 free(buf);
242 if (err) {
243 if (err->code == GOT_ERR_PRIVSEP_PIPE)
244 err = NULL;
245 else
246 got_privsep_send_error(ibuf, err);
249 return err;
252 static const struct got_error *
253 receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
255 const struct got_error *err;
256 struct imsg imsg;
257 size_t datalen;
259 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
260 if (err)
261 return err;
263 if (imsg.hdr.type != imsg_code) {
264 err = got_error(GOT_ERR_PRIVSEP_MSG);
265 goto done;
268 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
269 if (datalen != 0) {
270 err = got_error(GOT_ERR_PRIVSEP_LEN);
271 goto done;
273 if (imsg.fd == -1) {
274 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
275 goto done;
278 *f = fdopen(imsg.fd, "w+");
279 if (*f == NULL) {
280 err = got_error_from_errno("fdopen");
281 close(imsg.fd);
282 goto done;
284 done:
285 imsg_free(&imsg);
286 return err;
289 static const struct got_error *
290 receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
291 struct imsgbuf *ibuf)
293 size_t datalen;
295 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
296 if (datalen != 0)
297 return got_error(GOT_ERR_PRIVSEP_LEN);
299 if (imsg->fd == -1)
300 return got_error(GOT_ERR_PRIVSEP_NO_FD);
302 *f = fdopen(imsg->fd, mode);
303 if (*f == NULL)
304 return got_error_from_errno("fdopen");
305 imsg->fd = -1;
307 return NULL;
310 static const struct got_error *
311 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
312 struct got_packidx *packidx, struct got_object_cache *objcache,
313 FILE *basefile, FILE *accumfile)
315 const struct got_error *err = NULL;
316 struct got_imsg_packed_object iobj;
317 struct got_object *obj = NULL;
318 FILE *outfile = NULL;
319 struct got_object_id id;
320 size_t datalen;
321 uint64_t blob_size;
322 uint8_t *buf = NULL;
324 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
325 if (datalen != sizeof(iobj))
326 return got_error(GOT_ERR_PRIVSEP_LEN);
327 memcpy(&iobj, imsg->data, sizeof(iobj));
328 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
330 obj = got_object_cache_get(objcache, &id);
331 if (obj) {
332 obj->refcnt++;
333 } else {
334 err = open_object(&obj, pack, packidx, iobj.idx, &id,
335 objcache);
336 if (err)
337 return err;
340 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
341 if (err)
342 goto done;
344 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
345 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
346 if (err)
347 goto done;
348 } else
349 blob_size = obj->size;
351 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
352 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
353 obj, pack);
354 else
355 err = got_packfile_extract_object(pack, obj, outfile, basefile,
356 accumfile);
357 if (err)
358 goto done;
360 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
361 done:
362 free(buf);
363 if (outfile && fclose(outfile) == EOF && err == NULL)
364 err = got_error_from_errno("fclose");
365 got_object_close(obj);
366 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
367 got_privsep_send_error(ibuf, err);
369 return err;
372 static const struct got_error *
373 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
374 struct got_packidx *packidx, struct got_object_cache *objcache)
376 const struct got_error *err = NULL;
377 struct got_imsg_packed_object iobj;
378 struct got_object *obj = NULL;
379 struct got_tag_object *tag = NULL;
380 uint8_t *buf = NULL;
381 size_t len;
382 struct got_object_id id;
383 size_t datalen;
385 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
386 if (datalen != sizeof(iobj))
387 return got_error(GOT_ERR_PRIVSEP_LEN);
388 memcpy(&iobj, imsg->data, sizeof(iobj));
389 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
391 obj = got_object_cache_get(objcache, &id);
392 if (obj) {
393 obj->refcnt++;
394 } else {
395 err = open_object(&obj, pack, packidx, iobj.idx, &id,
396 objcache);
397 if (err)
398 return err;
401 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
402 if (err)
403 goto done;
405 obj->size = len;
406 err = got_object_parse_tag(&tag, buf, len);
407 if (err)
408 goto done;
410 err = got_privsep_send_tag(ibuf, tag);
411 done:
412 free(buf);
413 got_object_close(obj);
414 if (tag)
415 got_object_tag_close(tag);
416 if (err) {
417 if (err->code == GOT_ERR_PRIVSEP_PIPE)
418 err = NULL;
419 else
420 got_privsep_send_error(ibuf, err);
423 return err;
426 static struct got_parsed_tree_entry *
427 find_entry_by_name(struct got_parsed_tree_entry *entries, int nentries,
428 const char *name, size_t len)
430 struct got_parsed_tree_entry *pte;
431 int cmp, i;
433 /* Note that tree entries are sorted in strncmp() order. */
434 for (i = 0; i < nentries; i++) {
435 pte = &entries[i];
436 cmp = strncmp(pte->name, name, len);
437 if (cmp < 0)
438 continue;
439 if (cmp > 0)
440 break;
441 if (pte->name[len] == '\0')
442 return pte;
444 return NULL;
447 static const struct got_error *
448 tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
449 struct got_parsed_tree_entry **entries1, int *nentries1,
450 struct got_parsed_tree_entry **entries2, int *nentries2,
451 const char *path, struct got_pack *pack, struct got_packidx *packidx,
452 struct imsgbuf *ibuf, struct got_object_cache *objcache)
454 const struct got_error *err = NULL;
455 struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
456 const char *seg, *s;
457 size_t seglen;
459 *changed = 0;
461 /* We not do support comparing the root path. */
462 if (got_path_is_root_dir(path))
463 return got_error_path(path, GOT_ERR_BAD_PATH);
465 s = path;
466 while (*s == '/')
467 s++;
468 seg = s;
469 seglen = 0;
470 while (*s) {
471 if (*s != '/') {
472 s++;
473 seglen++;
474 if (*s)
475 continue;
478 pte1 = find_entry_by_name(*entries1, *nentries1, seg, seglen);
479 if (pte1 == NULL) {
480 err = got_error(GOT_ERR_NO_OBJ);
481 break;
484 pte2 = find_entry_by_name(*entries2, *nentries2, seg, seglen);
485 if (pte2 == NULL) {
486 *changed = 1;
487 break;
490 if (pte1->mode != pte2->mode) {
491 *changed = 1;
492 break;
495 if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
496 *changed = 0;
497 break;
500 if (*s == '\0') { /* final path element */
501 *changed = 1;
502 break;
505 seg = s + 1;
506 s++;
507 seglen = 0;
508 if (*s) {
509 struct got_object_id id1, id2;
510 int idx;
512 memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
513 idx = got_packidx_get_object_idx(packidx, &id1);
514 if (idx == -1) {
515 err = got_error_no_obj(&id1);
516 break;
518 free(*entries1);
519 *nentries1 = 0;
520 free(*buf1);
521 *buf1 = NULL;
522 err = open_tree(buf1, entries1, nentries1, pack,
523 packidx, idx, &id1, objcache);
524 pte1 = NULL;
525 if (err)
526 break;
528 memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
529 idx = got_packidx_get_object_idx(packidx, &id2);
530 if (idx == -1) {
531 err = got_error_no_obj(&id2);
532 break;
534 free(*entries2);
535 *nentries2 = 0;
536 free(*buf2);
537 *buf2 = NULL;
538 err = open_tree(buf2, entries2, nentries2, pack,
539 packidx, idx, &id2, objcache);
540 pte2 = NULL;
541 if (err)
542 break;
546 return err;
549 static const struct got_error *
550 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
551 struct imsgbuf *ibuf)
553 struct ibuf *wbuf;
554 size_t i;
556 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
557 sizeof(struct got_imsg_traversed_commits) +
558 ncommits * SHA1_DIGEST_LENGTH);
559 if (wbuf == NULL)
560 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
562 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
563 return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
565 for (i = 0; i < ncommits; i++) {
566 struct got_object_id *id = &commit_ids[i];
567 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
568 return got_error_from_errno(
569 "imsg_add TRAVERSED_COMMITS");
573 wbuf->fd = -1;
574 imsg_close(ibuf, wbuf);
576 return got_privsep_flush_imsg(ibuf);
579 static const struct got_error *
580 send_commit_traversal_done(struct imsgbuf *ibuf)
582 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
583 NULL, 0) == -1)
584 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
586 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 delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1025 FILE *delta_outfile, struct got_pack *pack, struct got_packidx *packidx)
1027 const struct got_error *err = NULL;
1028 struct got_object_idset *idset;
1029 struct search_deltas_arg sda;
1031 idset = got_object_idset_alloc();
1032 if (idset == NULL)
1033 return got_error_from_errno("got_object_idset_alloc");
1035 err = recv_object_ids(idset, ibuf);
1036 if (err)
1037 return err;
1039 memset(&sda, 0, sizeof(sda));
1040 sda.ibuf = ibuf;
1041 sda.idset = idset;
1042 sda.pack = pack;
1043 sda.packidx = packidx;
1044 sda.delta_outfile = delta_outfile;
1045 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1046 if (err)
1047 goto done;
1049 if (sda.ndeltas > 0) {
1050 err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1051 sda.ndeltas);
1052 if (err)
1053 goto done;
1056 if (fflush(delta_outfile) == -1) {
1057 err = got_error_from_errno("fflush");
1058 goto done;
1061 err = got_privsep_send_reused_deltas_done(ibuf);
1062 done:
1063 got_object_idset_free(idset);
1064 return err;
1067 static const struct got_error *
1068 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1070 const struct got_error *err = NULL;
1071 struct imsg imsg;
1072 struct got_imsg_packidx ipackidx;
1073 size_t datalen;
1074 struct got_packidx *p;
1076 *packidx = NULL;
1078 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1079 if (err)
1080 return err;
1082 p = calloc(1, sizeof(*p));
1083 if (p == NULL) {
1084 err = got_error_from_errno("calloc");
1085 goto done;
1088 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1089 err = got_error(GOT_ERR_PRIVSEP_MSG);
1090 goto done;
1093 if (imsg.fd == -1) {
1094 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1095 goto done;
1098 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1099 if (datalen != sizeof(ipackidx)) {
1100 err = got_error(GOT_ERR_PRIVSEP_LEN);
1101 goto done;
1103 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1105 p->len = ipackidx.len;
1106 p->fd = dup(imsg.fd);
1107 if (p->fd == -1) {
1108 err = got_error_from_errno("dup");
1109 goto done;
1111 if (lseek(p->fd, 0, SEEK_SET) == -1) {
1112 err = got_error_from_errno("lseek");
1113 goto done;
1116 #ifndef GOT_PACK_NO_MMAP
1117 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1118 if (p->map == MAP_FAILED)
1119 p->map = NULL; /* fall back to read(2) */
1120 #endif
1121 err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1122 done:
1123 if (err) {
1124 if (imsg.fd != -1)
1125 close(imsg.fd);
1126 got_packidx_close(p);
1127 } else
1128 *packidx = p;
1129 imsg_free(&imsg);
1130 return err;
1133 static const struct got_error *
1134 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1136 const struct got_error *err = NULL;
1137 struct imsg imsg;
1138 struct got_imsg_pack ipack;
1139 size_t datalen;
1140 struct got_pack *pack;
1142 *packp = NULL;
1144 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1145 if (err)
1146 return err;
1148 pack = calloc(1, sizeof(*pack));
1149 if (pack == NULL) {
1150 err = got_error_from_errno("calloc");
1151 goto done;
1154 if (imsg.hdr.type != GOT_IMSG_PACK) {
1155 err = got_error(GOT_ERR_PRIVSEP_MSG);
1156 goto done;
1159 if (imsg.fd == -1) {
1160 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1161 goto done;
1164 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1165 if (datalen != sizeof(ipack)) {
1166 err = got_error(GOT_ERR_PRIVSEP_LEN);
1167 goto done;
1169 memcpy(&ipack, imsg.data, sizeof(ipack));
1171 pack->filesize = ipack.filesize;
1172 pack->fd = dup(imsg.fd);
1173 if (pack->fd == -1) {
1174 err = got_error_from_errno("dup");
1175 goto done;
1177 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1178 err = got_error_from_errno("lseek");
1179 goto done;
1181 pack->path_packfile = strdup(ipack.path_packfile);
1182 if (pack->path_packfile == NULL) {
1183 err = got_error_from_errno("strdup");
1184 goto done;
1187 pack->delta_cache = got_delta_cache_alloc(100,
1188 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
1189 if (pack->delta_cache == NULL) {
1190 err = got_error_from_errno("got_delta_cache_alloc");
1191 goto done;
1194 #ifndef GOT_PACK_NO_MMAP
1195 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1196 pack->fd, 0);
1197 if (pack->map == MAP_FAILED)
1198 pack->map = NULL; /* fall back to read(2) */
1199 #endif
1200 done:
1201 if (err) {
1202 if (imsg.fd != -1)
1203 close(imsg.fd);
1204 free(pack);
1205 } else
1206 *packp = pack;
1207 imsg_free(&imsg);
1208 return err;
1211 int
1212 main(int argc, char *argv[])
1214 const struct got_error *err = NULL;
1215 struct imsgbuf ibuf;
1216 struct imsg imsg;
1217 struct got_packidx *packidx = NULL;
1218 struct got_pack *pack = NULL;
1219 struct got_object_cache objcache;
1220 FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1222 //static int attached;
1223 //while (!attached) sleep(1);
1225 signal(SIGINT, catch_sigint);
1227 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1229 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1230 if (err) {
1231 err = got_error_from_errno("got_object_cache_init");
1232 got_privsep_send_error(&ibuf, err);
1233 return 1;
1236 #ifndef PROFILE
1237 /* revoke access to most system calls */
1238 if (pledge("stdio recvfd", NULL) == -1) {
1239 err = got_error_from_errno("pledge");
1240 got_privsep_send_error(&ibuf, err);
1241 return 1;
1244 /* revoke fs access */
1245 if (landlock_no_fs() == -1) {
1246 err = got_error_from_errno("landlock_no_fs");
1247 got_privsep_send_error(&ibuf, err);
1248 return 1;
1250 #endif
1252 err = receive_packidx(&packidx, &ibuf);
1253 if (err) {
1254 got_privsep_send_error(&ibuf, err);
1255 return 1;
1258 err = receive_pack(&pack, &ibuf);
1259 if (err) {
1260 got_privsep_send_error(&ibuf, err);
1261 return 1;
1264 for (;;) {
1265 imsg.fd = -1;
1267 if (sigint_received) {
1268 err = got_error(GOT_ERR_CANCELLED);
1269 break;
1272 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1273 if (err) {
1274 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1275 err = NULL;
1276 break;
1279 if (imsg.hdr.type == GOT_IMSG_STOP)
1280 break;
1282 switch (imsg.hdr.type) {
1283 case GOT_IMSG_TMPFD:
1284 if (basefile == NULL) {
1285 err = receive_tempfile(&basefile, "w+",
1286 &imsg, &ibuf);
1287 } else if (accumfile == NULL) {
1288 err = receive_tempfile(&accumfile, "w+",
1289 &imsg, &ibuf);
1290 } else
1291 err = got_error(GOT_ERR_PRIVSEP_MSG);
1292 break;
1293 case GOT_IMSG_PACKED_OBJECT_REQUEST:
1294 err = object_request(&imsg, &ibuf, pack, packidx,
1295 &objcache);
1296 break;
1297 case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1298 if (basefile == NULL || accumfile == NULL) {
1299 err = got_error(GOT_ERR_PRIVSEP_MSG);
1300 break;
1302 err = raw_object_request(&imsg, &ibuf, pack, packidx,
1303 &objcache, basefile, accumfile);
1304 break;
1305 case GOT_IMSG_RAW_DELTA_OUTFD:
1306 if (delta_outfile != NULL) {
1307 err = got_error(GOT_ERR_PRIVSEP_MSG);
1308 break;
1310 err = receive_tempfile(&delta_outfile, "w",
1311 &imsg, &ibuf);
1312 break;
1313 case GOT_IMSG_RAW_DELTA_REQUEST:
1314 if (delta_outfile == NULL) {
1315 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1316 break;
1318 err = raw_delta_request(&imsg, &ibuf, delta_outfile,
1319 pack, packidx);
1320 break;
1321 case GOT_IMSG_DELTA_REUSE_REQUEST:
1322 if (delta_outfile == NULL) {
1323 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1324 break;
1326 err = delta_reuse_request(&imsg, &ibuf,
1327 delta_outfile, pack, packidx);
1328 break;
1329 case GOT_IMSG_COMMIT_REQUEST:
1330 err = commit_request(&imsg, &ibuf, pack, packidx,
1331 &objcache);
1332 break;
1333 case GOT_IMSG_TREE_REQUEST:
1334 err = tree_request(&imsg, &ibuf, pack, packidx,
1335 &objcache);
1336 break;
1337 case GOT_IMSG_BLOB_REQUEST:
1338 if (basefile == NULL || accumfile == NULL) {
1339 err = got_error(GOT_ERR_PRIVSEP_MSG);
1340 break;
1342 err = blob_request(&imsg, &ibuf, pack, packidx,
1343 &objcache, basefile, accumfile);
1344 break;
1345 case GOT_IMSG_TAG_REQUEST:
1346 err = tag_request(&imsg, &ibuf, pack, packidx,
1347 &objcache);
1348 break;
1349 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1350 err = commit_traversal_request(&imsg, &ibuf, pack,
1351 packidx, &objcache);
1352 break;
1353 default:
1354 err = got_error(GOT_ERR_PRIVSEP_MSG);
1355 break;
1358 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
1359 err = got_error_from_errno("close");
1360 imsg_free(&imsg);
1361 if (err)
1362 break;
1365 if (packidx)
1366 got_packidx_close(packidx);
1367 if (pack)
1368 got_pack_close(pack);
1369 got_object_cache_close(&objcache);
1370 imsg_clear(&ibuf);
1371 if (basefile && fclose(basefile) == EOF && err == NULL)
1372 err = got_error_from_errno("fclose");
1373 if (accumfile && fclose(accumfile) == EOF && err == NULL)
1374 err = got_error_from_errno("fclose");
1375 if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
1376 err = got_error_from_errno("fclose");
1377 if (err) {
1378 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1379 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1380 got_privsep_send_error(&ibuf, err);
1383 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
1384 err = got_error_from_errno("close");
1385 return err ? 1 : 0;