2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
31 #include "got_compat.h"
33 #include "got_error.h"
34 #include "got_object.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_privsep.h"
43 #include "got_lib_pack.h"
45 static volatile sig_atomic_t sigint_received;
48 catch_sigint(int signo)
53 static const struct got_error *
54 open_object(struct got_object **obj, struct got_pack *pack,
55 struct got_packidx *packidx, int idx, struct got_object_id *id,
56 struct got_object_cache *objcache)
58 const struct got_error *err;
60 err = got_packfile_open_object(obj, pack, packidx, idx, id);
65 err = got_object_cache_add(objcache, id, *obj);
67 if (err->code == GOT_ERR_OBJ_EXISTS ||
68 err->code == GOT_ERR_OBJ_TOO_LARGE)
76 static const struct got_error *
77 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
78 struct got_packidx *packidx, struct got_object_cache *objcache)
80 const struct got_error *err = NULL;
81 struct got_imsg_packed_object iobj;
82 struct got_object *obj;
83 struct got_object_id id;
86 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
87 if (datalen != sizeof(iobj))
88 return got_error(GOT_ERR_PRIVSEP_LEN);
89 memcpy(&iobj, imsg->data, sizeof(iobj));
90 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
92 obj = got_object_cache_get(objcache, &id);
96 err = open_object(&obj, pack, packidx, iobj.idx, &id,
102 err = got_privsep_send_obj(ibuf, obj);
104 got_object_close(obj);
108 static const struct got_error *
109 open_commit(struct got_commit_object **commit, struct got_pack *pack,
110 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
111 struct got_object_cache *objcache)
113 const struct got_error *err = NULL;
114 struct got_object *obj = NULL;
120 obj = got_object_cache_get(objcache, id);
124 err = open_object(&obj, pack, packidx, obj_idx, id,
130 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
136 err = got_object_parse_commit(commit, buf, len);
138 got_object_close(obj);
143 static const struct got_error *
144 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
145 struct got_packidx *packidx, struct got_object_cache *objcache)
147 const struct got_error *err = NULL;
148 struct got_imsg_packed_object iobj;
149 struct got_commit_object *commit = NULL;
150 struct got_object_id id;
153 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
154 if (datalen != sizeof(iobj))
155 return got_error(GOT_ERR_PRIVSEP_LEN);
156 memcpy(&iobj, imsg->data, sizeof(iobj));
157 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
159 err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
163 err = got_privsep_send_commit(ibuf, commit);
166 got_object_commit_close(commit);
168 if (err->code == GOT_ERR_PRIVSEP_PIPE)
171 got_privsep_send_error(ibuf, err);
177 static const struct got_error *
178 open_tree(uint8_t **buf, struct got_pathlist_head *entries, int *nentries,
179 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
180 struct got_object_id *id, struct got_object_cache *objcache)
182 const struct got_error *err = NULL;
183 struct got_object *obj = NULL;
189 obj = got_object_cache_get(objcache, id);
193 err = open_object(&obj, pack, packidx, obj_idx, id,
199 err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
205 err = got_object_parse_tree(entries, nentries, *buf, len);
207 got_object_close(obj);
215 static const struct got_error *
216 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
217 struct got_packidx *packidx, struct got_object_cache *objcache)
219 const struct got_error *err = NULL;
220 struct got_imsg_packed_object iobj;
221 struct got_pathlist_head entries;
224 struct got_object_id id;
227 TAILQ_INIT(&entries);
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,
240 err = got_privsep_send_tree(ibuf, &entries, nentries);
241 got_object_parsed_tree_entries_free(&entries);
244 if (err->code == GOT_ERR_PRIVSEP_PIPE)
247 got_privsep_send_error(ibuf, 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;
260 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
264 if (imsg.hdr.type != imsg_code) {
265 err = got_error(GOT_ERR_PRIVSEP_MSG);
269 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
271 err = got_error(GOT_ERR_PRIVSEP_LEN);
275 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
279 *f = fdopen(imsg.fd, "w+");
281 err = got_error_from_errno("fdopen");
290 static const struct got_error *
291 receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
292 struct imsgbuf *ibuf)
296 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
298 return got_error(GOT_ERR_PRIVSEP_LEN);
301 return got_error(GOT_ERR_PRIVSEP_NO_FD);
303 *f = fdopen(imsg->fd, mode);
305 return got_error_from_errno("fdopen");
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;
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);
335 err = open_object(&obj, pack, packidx, iobj.idx, &id,
341 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
345 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
346 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
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,
356 err = got_packfile_extract_object(pack, obj, outfile, basefile,
361 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, 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);
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;
383 struct got_object_id id;
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);
396 err = open_object(&obj, pack, packidx, iobj.idx, &id,
402 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
407 err = got_object_parse_tag(&tag, buf, len);
411 err = got_privsep_send_tag(ibuf, tag);
414 got_object_close(obj);
416 got_object_tag_close(tag);
418 if (err->code == GOT_ERR_PRIVSEP_PIPE)
421 got_privsep_send_error(ibuf, err);
427 static struct got_parsed_tree_entry *
428 find_entry_by_name(struct got_pathlist_head *entries, int nentries,
429 const char *name, size_t len)
431 struct got_pathlist_entry *pe;
433 /* Note that tree entries are sorted in strncmp() order. */
434 TAILQ_FOREACH(pe, entries, entry) {
435 int cmp = strncmp(pe->path, name, len);
440 if (pe->path[len] == '\0')
441 return (struct got_parsed_tree_entry *)pe->data;
446 static const struct got_error *
447 tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
448 struct got_pathlist_head *entries1, int *nentries1,
449 struct got_pathlist_head *entries2, int *nentries2,
450 const char *path, struct got_pack *pack, struct got_packidx *packidx,
451 struct imsgbuf *ibuf, struct got_object_cache *objcache)
453 const struct got_error *err = NULL;
454 struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
460 /* We not do support comparing the root path. */
461 if (got_path_is_root_dir(path))
462 return got_error_path(path, GOT_ERR_BAD_PATH);
477 pte1 = find_entry_by_name(entries1, *nentries1, seg, seglen);
479 err = got_error(GOT_ERR_NO_OBJ);
483 pte2 = find_entry_by_name(entries2, *nentries2, seg, seglen);
489 if (pte1->mode != pte2->mode) {
494 if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
499 if (*s == '\0') { /* final path element */
508 struct got_object_id id1, id2;
511 memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
512 idx = got_packidx_get_object_idx(packidx, &id1);
514 err = got_error_no_obj(&id1);
517 got_object_parsed_tree_entries_free(entries1);
521 err = open_tree(buf1, entries1, nentries1, pack,
522 packidx, idx, &id1, objcache);
527 memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
528 idx = got_packidx_get_object_idx(packidx, &id2);
530 err = got_error_no_obj(&id2);
533 got_object_parsed_tree_entries_free(entries2);
537 err = open_tree(buf2, entries2, nentries2, pack,
538 packidx, idx, &id2, objcache);
548 static const struct got_error *
549 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
550 struct imsgbuf *ibuf)
552 const struct got_error *err;
556 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
557 sizeof(struct got_imsg_traversed_commits) +
558 ncommits * SHA1_DIGEST_LENGTH);
560 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
562 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
563 err = 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 err = got_error_from_errno(
571 "imsg_add TRAVERSED_COMMITS");
578 imsg_close(ibuf, wbuf);
580 return got_privsep_flush_imsg(ibuf);
583 static const struct got_error *
584 send_commit_traversal_done(struct imsgbuf *ibuf)
586 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
588 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
590 return got_privsep_flush_imsg(ibuf);
594 static const struct got_error *
595 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
596 struct got_pack *pack, struct got_packidx *packidx,
597 struct got_object_cache *objcache)
599 const struct got_error *err = NULL;
600 struct got_imsg_packed_object iobj;
601 struct got_object_qid *pid;
602 struct got_commit_object *commit = NULL, *pcommit = NULL;
603 struct got_pathlist_head entries, pentries;
604 int nentries = 0, pnentries = 0;
605 struct got_object_id id;
606 size_t datalen, path_len;
608 const int min_alloc = 64;
609 int changed = 0, ncommits = 0, nallocated = 0;
610 struct got_object_id *commit_ids = NULL;
612 TAILQ_INIT(&entries);
613 TAILQ_INIT(&pentries);
615 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
616 if (datalen < sizeof(iobj))
617 return got_error(GOT_ERR_PRIVSEP_LEN);
618 memcpy(&iobj, imsg->data, sizeof(iobj));
619 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
621 path_len = datalen - sizeof(iobj) - 1;
623 return got_error(GOT_ERR_PRIVSEP_LEN);
625 path = imsg->data + sizeof(iobj);
626 if (path[path_len] != '\0')
627 return got_error(GOT_ERR_PRIVSEP_LEN);
630 nallocated = min_alloc;
631 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
632 if (commit_ids == NULL)
633 return got_error_from_errno("reallocarray");
636 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
639 if (sigint_received) {
640 err = got_error(GOT_ERR_CANCELLED);
644 if (commit == NULL) {
645 idx = got_packidx_get_object_idx(packidx, &id);
648 err = open_commit(&commit, pack, packidx,
651 if (err->code != GOT_ERR_NO_OBJ)
658 if (sizeof(struct got_imsg_traversed_commits) +
659 ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
660 err = send_traversed_commits(commit_ids, ncommits,
667 if (ncommits > nallocated) {
668 struct got_object_id *new;
669 nallocated += min_alloc;
670 new = reallocarray(commit_ids, nallocated,
671 sizeof(*commit_ids));
673 err = got_error_from_errno("reallocarray");
678 memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
681 pid = STAILQ_FIRST(&commit->parent_ids);
685 idx = got_packidx_get_object_idx(packidx, pid->id);
689 err = open_commit(&pcommit, pack, packidx, idx, pid->id,
692 if (err->code != GOT_ERR_NO_OBJ)
698 if (path[0] == '/' && path[1] == '\0') {
699 if (got_object_id_cmp(pcommit->tree_id,
700 commit->tree_id) != 0) {
706 uint8_t *buf = NULL, *pbuf = NULL;
708 idx = got_packidx_get_object_idx(packidx,
712 pidx = got_packidx_get_object_idx(packidx,
717 err = open_tree(&buf, &entries, &nentries, pack,
718 packidx, idx, commit->tree_id, objcache);
721 err = open_tree(&pbuf, &pentries, &pnentries, pack,
722 packidx, pidx, pcommit->tree_id, objcache);
728 err = tree_path_changed(&changed, &buf, &pbuf,
729 &entries, &nentries, &pentries, &pnentries, path,
730 pack, packidx, ibuf, objcache);
732 got_object_parsed_tree_entries_free(&entries);
735 got_object_parsed_tree_entries_free(&pentries);
739 if (err->code != GOT_ERR_NO_OBJ)
747 memcpy(id.sha1, pid->id->sha1, SHA1_DIGEST_LENGTH);
748 got_object_commit_close(commit);
755 err = send_traversed_commits(commit_ids, ncommits, ibuf);
760 err = got_privsep_send_commit(ibuf, commit);
765 err = send_commit_traversal_done(ibuf);
769 got_object_commit_close(commit);
771 got_object_commit_close(pcommit);
773 got_object_parsed_tree_entries_free(&entries);
775 got_object_parsed_tree_entries_free(&pentries);
777 if (err->code == GOT_ERR_PRIVSEP_PIPE)
780 got_privsep_send_error(ibuf, err);
786 static const struct got_error *
787 raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
788 struct got_pack *pack, struct got_packidx *packidx,
789 struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
791 const struct got_error *err = NULL;
794 FILE *outfile = NULL;
795 struct got_imsg_packed_object iobj;
796 struct got_object *obj;
797 struct got_object_id id;
800 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
801 if (datalen != sizeof(iobj))
802 return got_error(GOT_ERR_PRIVSEP_LEN);
803 memcpy(&iobj, imsg->data, sizeof(iobj));
804 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
806 obj = got_object_cache_get(objcache, &id);
810 err = open_object(&obj, pack, packidx, iobj.idx, &id,
816 err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
820 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
821 err = got_pack_get_max_delta_object_size(&size, obj, pack);
827 if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
828 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
831 err = got_packfile_extract_object(pack, obj, outfile, basefile,
836 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
839 if (outfile && fclose(outfile) == EOF && err == NULL)
840 err = got_error_from_errno("fclose");
841 got_object_close(obj);
842 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
843 got_privsep_send_error(ibuf, err);
848 static const struct got_error *
849 get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
852 const struct got_error *err;
855 err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
859 return got_error(GOT_ERR_BAD_PACKIDX);
861 return got_packidx_get_object_id(base_id, packidx, idx);
864 static const struct got_error *
865 raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
866 FILE *delta_outfile, struct got_pack *pack,
867 struct got_packidx *packidx)
869 const struct got_error *err = NULL;
870 struct got_imsg_raw_delta_request req;
871 size_t datalen, delta_size;
873 uint8_t *delta_buf = NULL;
874 struct got_object_id id, base_id;
875 off_t base_offset, delta_out_offset = 0;
876 uint64_t base_size = 0, result_size = 0;
879 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
880 if (datalen != sizeof(req))
881 return got_error(GOT_ERR_PRIVSEP_LEN);
882 memcpy(&req, imsg->data, sizeof(req));
883 memcpy(id.sha1, req.id, SHA1_DIGEST_LENGTH);
887 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
888 &delta_offset, &base_offset, &base_id, &base_size, &result_size,
889 pack, packidx, req.idx);
894 * If this is an offset delta we must determine the base
895 * object ID ourselves.
897 if (base_offset != 0) {
898 err = get_base_object_id(&base_id, packidx, base_offset);
903 delta_out_offset = ftello(delta_outfile);
904 w = fwrite(delta_buf, 1, delta_size, delta_outfile);
905 if (w != delta_size) {
906 err = got_ferror(delta_outfile, GOT_ERR_IO);
909 if (fflush(delta_outfile) == -1) {
910 err = got_error_from_errno("fflush");
914 err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
915 delta_size, delta_offset, delta_out_offset, &base_id);
921 static const struct got_error *
922 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
924 const struct got_error *err = NULL;
926 struct got_imsg_packidx ipackidx;
928 struct got_packidx *p;
932 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
936 p = calloc(1, sizeof(*p));
938 err = got_error_from_errno("calloc");
942 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
943 err = got_error(GOT_ERR_PRIVSEP_MSG);
948 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
952 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
953 if (datalen != sizeof(ipackidx)) {
954 err = got_error(GOT_ERR_PRIVSEP_LEN);
957 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
959 p->len = ipackidx.len;
960 p->fd = dup(imsg.fd);
962 err = got_error_from_errno("dup");
965 if (lseek(p->fd, 0, SEEK_SET) == -1) {
966 err = got_error_from_errno("lseek");
970 #ifndef GOT_PACK_NO_MMAP
971 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
972 if (p->map == MAP_FAILED)
973 p->map = NULL; /* fall back to read(2) */
975 err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
980 got_packidx_close(p);
987 static const struct got_error *
988 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
990 const struct got_error *err = NULL;
992 struct got_imsg_pack ipack;
994 struct got_pack *pack;
998 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1002 pack = calloc(1, sizeof(*pack));
1004 err = got_error_from_errno("calloc");
1008 if (imsg.hdr.type != GOT_IMSG_PACK) {
1009 err = got_error(GOT_ERR_PRIVSEP_MSG);
1013 if (imsg.fd == -1) {
1014 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1018 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1019 if (datalen != sizeof(ipack)) {
1020 err = got_error(GOT_ERR_PRIVSEP_LEN);
1023 memcpy(&ipack, imsg.data, sizeof(ipack));
1025 pack->filesize = ipack.filesize;
1026 pack->fd = dup(imsg.fd);
1027 if (pack->fd == -1) {
1028 err = got_error_from_errno("dup");
1031 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1032 err = got_error_from_errno("lseek");
1035 pack->path_packfile = strdup(ipack.path_packfile);
1036 if (pack->path_packfile == NULL) {
1037 err = got_error_from_errno("strdup");
1041 pack->delta_cache = got_delta_cache_alloc(100,
1042 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
1043 if (pack->delta_cache == NULL) {
1044 err = got_error_from_errno("got_delta_cache_alloc");
1048 #ifndef GOT_PACK_NO_MMAP
1049 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1051 if (pack->map == MAP_FAILED)
1052 pack->map = NULL; /* fall back to read(2) */
1066 main(int argc, char *argv[])
1068 const struct got_error *err = NULL;
1069 struct imsgbuf ibuf;
1071 struct got_packidx *packidx = NULL;
1072 struct got_pack *pack = NULL;
1073 struct got_object_cache objcache;
1074 FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1076 //static int attached;
1077 //while (!attached) sleep(1);
1079 signal(SIGINT, catch_sigint);
1081 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1083 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1085 err = got_error_from_errno("got_object_cache_init");
1086 got_privsep_send_error(&ibuf, err);
1091 /* revoke access to most system calls */
1092 if (pledge("stdio recvfd", NULL) == -1) {
1093 err = got_error_from_errno("pledge");
1094 got_privsep_send_error(&ibuf, err);
1098 /* revoke fs access */
1099 if (landlock_no_fs() == -1) {
1100 err = got_error_from_errno("landlock_no_fs");
1101 got_privsep_send_error(&ibuf, err);
1106 err = receive_packidx(&packidx, &ibuf);
1108 got_privsep_send_error(&ibuf, err);
1112 err = receive_pack(&pack, &ibuf);
1114 got_privsep_send_error(&ibuf, err);
1121 if (sigint_received) {
1122 err = got_error(GOT_ERR_CANCELLED);
1126 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1128 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1133 if (imsg.hdr.type == GOT_IMSG_STOP)
1136 switch (imsg.hdr.type) {
1137 case GOT_IMSG_TMPFD:
1138 if (basefile == NULL) {
1139 err = receive_tempfile(&basefile, "w+",
1141 } else if (accumfile == NULL) {
1142 err = receive_tempfile(&accumfile, "w+",
1145 err = got_error(GOT_ERR_PRIVSEP_MSG);
1147 case GOT_IMSG_PACKED_OBJECT_REQUEST:
1148 err = object_request(&imsg, &ibuf, pack, packidx,
1151 case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1152 if (basefile == NULL || accumfile == NULL) {
1153 err = got_error(GOT_ERR_PRIVSEP_MSG);
1156 err = raw_object_request(&imsg, &ibuf, pack, packidx,
1157 &objcache, basefile, accumfile);
1159 case GOT_IMSG_RAW_DELTA_OUTFD:
1160 if (delta_outfile != NULL) {
1161 err = got_error(GOT_ERR_PRIVSEP_MSG);
1164 err = receive_tempfile(&delta_outfile, "w",
1167 case GOT_IMSG_RAW_DELTA_REQUEST:
1168 if (delta_outfile == NULL) {
1169 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1172 err = raw_delta_request(&imsg, &ibuf, delta_outfile,
1175 case GOT_IMSG_COMMIT_REQUEST:
1176 err = commit_request(&imsg, &ibuf, pack, packidx,
1179 case GOT_IMSG_TREE_REQUEST:
1180 err = tree_request(&imsg, &ibuf, pack, packidx,
1183 case GOT_IMSG_BLOB_REQUEST:
1184 if (basefile == NULL || accumfile == NULL) {
1185 err = got_error(GOT_ERR_PRIVSEP_MSG);
1188 err = blob_request(&imsg, &ibuf, pack, packidx,
1189 &objcache, basefile, accumfile);
1191 case GOT_IMSG_TAG_REQUEST:
1192 err = tag_request(&imsg, &ibuf, pack, packidx,
1195 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1196 err = commit_traversal_request(&imsg, &ibuf, pack,
1197 packidx, &objcache);
1200 err = got_error(GOT_ERR_PRIVSEP_MSG);
1204 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
1205 err = got_error_from_errno("close");
1212 got_packidx_close(packidx);
1214 got_pack_close(pack);
1215 got_object_cache_close(&objcache);
1217 if (basefile && fclose(basefile) == EOF && err == NULL)
1218 err = got_error_from_errno("fclose");
1219 if (accumfile && fclose(accumfile) == EOF && err == NULL)
1220 err = got_error_from_errno("fclose");
1221 if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
1222 err = got_error_from_errno("fclose");
1224 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1225 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1226 got_privsep_send_error(&ibuf, err);
1229 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
1230 err = got_error_from_errno("close");