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>
18 #include <sys/queue.h>
34 #include "got_error.h"
35 #include "got_object.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_delta_cache.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_cache.h"
42 #include "got_lib_object_parse.h"
43 #include "got_lib_privsep.h"
44 #include "got_lib_pack.h"
46 static volatile sig_atomic_t sigint_received;
49 catch_sigint(int signo)
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)
59 const struct got_error *err;
61 err = got_packfile_open_object(obj, pack, packidx, idx, id);
66 err = got_object_cache_add(objcache, id, *obj);
68 if (err->code == GOT_ERR_OBJ_EXISTS ||
69 err->code == GOT_ERR_OBJ_TOO_LARGE)
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)
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;
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);
97 err = open_object(&obj, pack, packidx, iobj.idx, &id,
103 err = got_privsep_send_obj(ibuf, obj);
105 got_object_close(obj);
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;
121 obj = got_object_cache_get(objcache, id);
125 err = open_object(&obj, pack, packidx, obj_idx, id,
131 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
137 err = got_object_parse_commit(commit, buf, len);
139 got_object_close(obj);
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;
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);
164 err = got_privsep_send_commit(ibuf, commit);
167 got_object_commit_close(commit);
169 if (err->code == GOT_ERR_PRIVSEP_PIPE)
172 got_privsep_send_error(ibuf, err);
178 static const struct got_error *
179 open_tree(uint8_t **buf, struct got_pathlist_head *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;
190 obj = got_object_cache_get(objcache, id);
194 err = open_object(&obj, pack, packidx, obj_idx, id,
200 err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
206 err = got_object_parse_tree(entries, nentries, *buf, len);
208 got_object_close(obj);
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_pathlist_head entries;
225 struct got_object_id id;
228 TAILQ_INIT(&entries);
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,
241 err = got_privsep_send_tree(ibuf, &entries, nentries);
242 got_object_parsed_tree_entries_free(&entries);
245 if (err->code == GOT_ERR_PRIVSEP_PIPE)
248 got_privsep_send_error(ibuf, 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;
261 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
265 if (imsg.hdr.type != imsg_code) {
266 err = got_error(GOT_ERR_PRIVSEP_MSG);
270 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
272 err = got_error(GOT_ERR_PRIVSEP_LEN);
276 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
280 *f = fdopen(imsg.fd, "w+");
282 err = got_error_from_errno("fdopen");
291 static const struct got_error *
292 receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
293 struct imsgbuf *ibuf)
297 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
299 return got_error(GOT_ERR_PRIVSEP_LEN);
302 return got_error(GOT_ERR_PRIVSEP_NO_FD);
304 *f = fdopen(imsg->fd, mode);
306 return got_error_from_errno("fdopen");
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;
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);
336 err = open_object(&obj, pack, packidx, iobj.idx, &id,
342 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
346 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
347 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
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,
357 err = got_packfile_extract_object(pack, obj, outfile, basefile,
362 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, 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);
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;
384 struct got_object_id id;
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);
397 err = open_object(&obj, pack, packidx, iobj.idx, &id,
403 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
408 err = got_object_parse_tag(&tag, buf, len);
412 err = got_privsep_send_tag(ibuf, tag);
415 got_object_close(obj);
417 got_object_tag_close(tag);
419 if (err->code == GOT_ERR_PRIVSEP_PIPE)
422 got_privsep_send_error(ibuf, err);
428 static struct got_parsed_tree_entry *
429 find_entry_by_name(struct got_pathlist_head *entries, int nentries,
430 const char *name, size_t len)
432 struct got_pathlist_entry *pe;
434 /* Note that tree entries are sorted in strncmp() order. */
435 TAILQ_FOREACH(pe, entries, entry) {
436 int cmp = strncmp(pe->path, name, len);
441 if (pe->path[len] == '\0')
442 return (struct got_parsed_tree_entry *)pe->data;
447 static const struct got_error *
448 tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
449 struct got_pathlist_head *entries1, int *nentries1,
450 struct got_pathlist_head *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;
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);
478 pte1 = find_entry_by_name(entries1, *nentries1, seg, seglen);
480 err = got_error(GOT_ERR_NO_OBJ);
484 pte2 = find_entry_by_name(entries2, *nentries2, seg, seglen);
490 if (pte1->mode != pte2->mode) {
495 if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
500 if (*s == '\0') { /* final path element */
509 struct got_object_id id1, id2;
512 memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
513 idx = got_packidx_get_object_idx(packidx, &id1);
515 err = got_error_no_obj(&id1);
518 got_object_parsed_tree_entries_free(entries1);
522 err = open_tree(buf1, entries1, nentries1, pack,
523 packidx, idx, &id1, objcache);
528 memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
529 idx = got_packidx_get_object_idx(packidx, &id2);
531 err = got_error_no_obj(&id2);
534 got_object_parsed_tree_entries_free(entries2);
538 err = open_tree(buf2, entries2, nentries2, pack,
539 packidx, idx, &id2, objcache);
549 static const struct got_error *
550 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
551 struct imsgbuf *ibuf)
553 const struct got_error *err;
557 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
558 sizeof(struct got_imsg_traversed_commits) +
559 ncommits * SHA1_DIGEST_LENGTH);
561 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
563 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
564 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
568 for (i = 0; i < ncommits; i++) {
569 struct got_object_id *id = &commit_ids[i];
570 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
571 err = got_error_from_errno(
572 "imsg_add TRAVERSED_COMMITS");
579 imsg_close(ibuf, wbuf);
581 return got_privsep_flush_imsg(ibuf);
584 static const struct got_error *
585 send_commit_traversal_done(struct imsgbuf *ibuf)
587 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
589 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
591 return got_privsep_flush_imsg(ibuf);
595 static const struct got_error *
596 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
597 struct got_pack *pack, struct got_packidx *packidx,
598 struct got_object_cache *objcache)
600 const struct got_error *err = NULL;
601 struct got_imsg_packed_object iobj;
602 struct got_object_qid *pid;
603 struct got_commit_object *commit = NULL, *pcommit = NULL;
604 struct got_pathlist_head entries, pentries;
605 int nentries = 0, pnentries = 0;
606 struct got_object_id id;
607 size_t datalen, path_len;
609 const int min_alloc = 64;
610 int changed = 0, ncommits = 0, nallocated = 0;
611 struct got_object_id *commit_ids = NULL;
613 TAILQ_INIT(&entries);
614 TAILQ_INIT(&pentries);
616 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
617 if (datalen < sizeof(iobj))
618 return got_error(GOT_ERR_PRIVSEP_LEN);
619 memcpy(&iobj, imsg->data, sizeof(iobj));
620 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
622 path_len = datalen - sizeof(iobj) - 1;
624 return got_error(GOT_ERR_PRIVSEP_LEN);
626 path = imsg->data + sizeof(iobj);
627 if (path[path_len] != '\0')
628 return got_error(GOT_ERR_PRIVSEP_LEN);
631 nallocated = min_alloc;
632 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
633 if (commit_ids == NULL)
634 return got_error_from_errno("reallocarray");
637 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
640 if (sigint_received) {
641 err = got_error(GOT_ERR_CANCELLED);
645 if (commit == NULL) {
646 idx = got_packidx_get_object_idx(packidx, &id);
649 err = open_commit(&commit, pack, packidx,
652 if (err->code != GOT_ERR_NO_OBJ)
659 if (sizeof(struct got_imsg_traversed_commits) +
660 ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
661 err = send_traversed_commits(commit_ids, ncommits,
668 if (ncommits > nallocated) {
669 struct got_object_id *new;
670 nallocated += min_alloc;
671 new = reallocarray(commit_ids, nallocated,
672 sizeof(*commit_ids));
674 err = got_error_from_errno("reallocarray");
679 memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
682 pid = STAILQ_FIRST(&commit->parent_ids);
686 idx = got_packidx_get_object_idx(packidx, pid->id);
690 err = open_commit(&pcommit, pack, packidx, idx, pid->id,
693 if (err->code != GOT_ERR_NO_OBJ)
699 if (path[0] == '/' && path[1] == '\0') {
700 if (got_object_id_cmp(pcommit->tree_id,
701 commit->tree_id) != 0) {
707 uint8_t *buf = NULL, *pbuf = NULL;
709 idx = got_packidx_get_object_idx(packidx,
713 pidx = got_packidx_get_object_idx(packidx,
718 err = open_tree(&buf, &entries, &nentries, pack,
719 packidx, idx, commit->tree_id, objcache);
722 err = open_tree(&pbuf, &pentries, &pnentries, pack,
723 packidx, pidx, pcommit->tree_id, objcache);
729 err = tree_path_changed(&changed, &buf, &pbuf,
730 &entries, &nentries, &pentries, &pnentries, path,
731 pack, packidx, ibuf, objcache);
733 got_object_parsed_tree_entries_free(&entries);
736 got_object_parsed_tree_entries_free(&pentries);
740 if (err->code != GOT_ERR_NO_OBJ)
748 memcpy(id.sha1, pid->id->sha1, SHA1_DIGEST_LENGTH);
749 got_object_commit_close(commit);
756 err = send_traversed_commits(commit_ids, ncommits, ibuf);
761 err = got_privsep_send_commit(ibuf, commit);
766 err = send_commit_traversal_done(ibuf);
770 got_object_commit_close(commit);
772 got_object_commit_close(pcommit);
774 got_object_parsed_tree_entries_free(&entries);
776 got_object_parsed_tree_entries_free(&pentries);
778 if (err->code == GOT_ERR_PRIVSEP_PIPE)
781 got_privsep_send_error(ibuf, err);
787 static const struct got_error *
788 raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
789 struct got_pack *pack, struct got_packidx *packidx,
790 struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
792 const struct got_error *err = NULL;
795 FILE *outfile = NULL;
796 struct got_imsg_packed_object iobj;
797 struct got_object *obj;
798 struct got_object_id id;
801 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
802 if (datalen != sizeof(iobj))
803 return got_error(GOT_ERR_PRIVSEP_LEN);
804 memcpy(&iobj, imsg->data, sizeof(iobj));
805 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
807 obj = got_object_cache_get(objcache, &id);
811 err = open_object(&obj, pack, packidx, iobj.idx, &id,
817 err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
821 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
822 err = got_pack_get_max_delta_object_size(&size, obj, pack);
828 if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
829 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
832 err = got_packfile_extract_object(pack, obj, outfile, basefile,
837 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
840 if (outfile && fclose(outfile) == EOF && err == NULL)
841 err = got_error_from_errno("fclose");
842 got_object_close(obj);
843 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
844 got_privsep_send_error(ibuf, err);
849 static const struct got_error *
850 get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
853 const struct got_error *err;
856 err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
860 return got_error(GOT_ERR_BAD_PACKIDX);
862 return got_packidx_get_object_id(base_id, packidx, idx);
865 static const struct got_error *
866 raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
867 FILE *delta_outfile, struct got_pack *pack,
868 struct got_packidx *packidx)
870 const struct got_error *err = NULL;
871 struct got_imsg_raw_delta_request req;
872 size_t datalen, delta_size;
874 uint8_t *delta_buf = NULL;
875 struct got_object_id id, base_id;
876 off_t base_offset, delta_out_offset = 0;
877 uint64_t base_size = 0, result_size = 0;
880 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
881 if (datalen != sizeof(req))
882 return got_error(GOT_ERR_PRIVSEP_LEN);
883 memcpy(&req, imsg->data, sizeof(req));
884 memcpy(id.sha1, req.id, SHA1_DIGEST_LENGTH);
888 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
889 &delta_offset, &base_offset, &base_id, &base_size, &result_size,
890 pack, packidx, req.idx);
895 * If this is an offset delta we must determine the base
896 * object ID ourselves.
898 if (base_offset != 0) {
899 err = get_base_object_id(&base_id, packidx, base_offset);
904 delta_out_offset = ftello(delta_outfile);
905 w = fwrite(delta_buf, 1, delta_size, delta_outfile);
906 if (w != delta_size) {
907 err = got_ferror(delta_outfile, GOT_ERR_IO);
910 if (fflush(delta_outfile) == -1) {
911 err = got_error_from_errno("fflush");
915 err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
916 delta_size, delta_offset, delta_out_offset, &base_id);
922 static const struct got_error *
923 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
925 const struct got_error *err = NULL;
927 struct got_imsg_packidx ipackidx;
929 struct got_packidx *p;
933 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
937 p = calloc(1, sizeof(*p));
939 err = got_error_from_errno("calloc");
943 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
944 err = got_error(GOT_ERR_PRIVSEP_MSG);
949 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
953 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
954 if (datalen != sizeof(ipackidx)) {
955 err = got_error(GOT_ERR_PRIVSEP_LEN);
958 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
960 p->len = ipackidx.len;
961 p->fd = dup(imsg.fd);
963 err = got_error_from_errno("dup");
966 if (lseek(p->fd, 0, SEEK_SET) == -1) {
967 err = got_error_from_errno("lseek");
971 #ifndef GOT_PACK_NO_MMAP
972 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
973 if (p->map == MAP_FAILED)
974 p->map = NULL; /* fall back to read(2) */
976 err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
981 got_packidx_close(p);
988 static const struct got_error *
989 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
991 const struct got_error *err = NULL;
993 struct got_imsg_pack ipack;
995 struct got_pack *pack;
999 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1003 pack = calloc(1, sizeof(*pack));
1005 err = got_error_from_errno("calloc");
1009 if (imsg.hdr.type != GOT_IMSG_PACK) {
1010 err = got_error(GOT_ERR_PRIVSEP_MSG);
1014 if (imsg.fd == -1) {
1015 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1019 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1020 if (datalen != sizeof(ipack)) {
1021 err = got_error(GOT_ERR_PRIVSEP_LEN);
1024 memcpy(&ipack, imsg.data, sizeof(ipack));
1026 pack->filesize = ipack.filesize;
1027 pack->fd = dup(imsg.fd);
1028 if (pack->fd == -1) {
1029 err = got_error_from_errno("dup");
1032 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1033 err = got_error_from_errno("lseek");
1036 pack->path_packfile = strdup(ipack.path_packfile);
1037 if (pack->path_packfile == NULL) {
1038 err = got_error_from_errno("strdup");
1042 pack->delta_cache = got_delta_cache_alloc(100,
1043 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
1044 if (pack->delta_cache == NULL) {
1045 err = got_error_from_errno("got_delta_cache_alloc");
1049 #ifndef GOT_PACK_NO_MMAP
1050 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1052 if (pack->map == MAP_FAILED)
1053 pack->map = NULL; /* fall back to read(2) */
1067 main(int argc, char *argv[])
1069 const struct got_error *err = NULL;
1070 struct imsgbuf ibuf;
1072 struct got_packidx *packidx = NULL;
1073 struct got_pack *pack = NULL;
1074 struct got_object_cache objcache;
1075 FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1077 //static int attached;
1078 //while (!attached) sleep(1);
1080 signal(SIGINT, catch_sigint);
1082 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1084 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1086 err = got_error_from_errno("got_object_cache_init");
1087 got_privsep_send_error(&ibuf, err);
1092 /* revoke access to most system calls */
1093 if (pledge("stdio recvfd", NULL) == -1) {
1094 err = got_error_from_errno("pledge");
1095 got_privsep_send_error(&ibuf, err);
1100 err = receive_packidx(&packidx, &ibuf);
1102 got_privsep_send_error(&ibuf, err);
1106 err = receive_pack(&pack, &ibuf);
1108 got_privsep_send_error(&ibuf, err);
1115 if (sigint_received) {
1116 err = got_error(GOT_ERR_CANCELLED);
1120 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1122 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1127 if (imsg.hdr.type == GOT_IMSG_STOP)
1130 switch (imsg.hdr.type) {
1131 case GOT_IMSG_TMPFD:
1132 if (basefile == NULL) {
1133 err = receive_tempfile(&basefile, "w+",
1135 } else if (accumfile == NULL) {
1136 err = receive_tempfile(&accumfile, "w+",
1139 err = got_error(GOT_ERR_PRIVSEP_MSG);
1141 case GOT_IMSG_PACKED_OBJECT_REQUEST:
1142 err = object_request(&imsg, &ibuf, pack, packidx,
1145 case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1146 if (basefile == NULL || accumfile == NULL) {
1147 err = got_error(GOT_ERR_PRIVSEP_MSG);
1150 err = raw_object_request(&imsg, &ibuf, pack, packidx,
1151 &objcache, basefile, accumfile);
1153 case GOT_IMSG_RAW_DELTA_OUTFD:
1154 if (delta_outfile != NULL) {
1155 err = got_error(GOT_ERR_PRIVSEP_MSG);
1158 err = receive_tempfile(&delta_outfile, "w",
1161 case GOT_IMSG_RAW_DELTA_REQUEST:
1162 if (delta_outfile == NULL) {
1163 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1166 err = raw_delta_request(&imsg, &ibuf, delta_outfile,
1169 case GOT_IMSG_COMMIT_REQUEST:
1170 err = commit_request(&imsg, &ibuf, pack, packidx,
1173 case GOT_IMSG_TREE_REQUEST:
1174 err = tree_request(&imsg, &ibuf, pack, packidx,
1177 case GOT_IMSG_BLOB_REQUEST:
1178 if (basefile == NULL || accumfile == NULL) {
1179 err = got_error(GOT_ERR_PRIVSEP_MSG);
1182 err = blob_request(&imsg, &ibuf, pack, packidx,
1183 &objcache, basefile, accumfile);
1185 case GOT_IMSG_TAG_REQUEST:
1186 err = tag_request(&imsg, &ibuf, pack, packidx,
1189 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1190 err = commit_traversal_request(&imsg, &ibuf, pack,
1191 packidx, &objcache);
1194 err = got_error(GOT_ERR_PRIVSEP_MSG);
1198 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
1199 err = got_error_from_errno("close");
1206 got_packidx_close(packidx);
1208 got_pack_close(pack);
1209 got_object_cache_close(&objcache);
1211 if (basefile && fclose(basefile) == EOF && err == NULL)
1212 err = got_error_from_errno("fclose");
1213 if (accumfile && fclose(accumfile) == EOF && err == NULL)
1214 err = got_error_from_errno("fclose");
1215 if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
1216 err = got_error_from_errno("fclose");
1218 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1219 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1220 got_privsep_send_error(&ibuf, err);
1223 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
1224 err = got_error_from_errno("close");