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.
16 #include "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/queue.h>
35 #include "got_error.h"
36 #include "got_object.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_qid.h"
43 #include "got_lib_object_cache.h"
44 #include "got_lib_object_parse.h"
45 #include "got_lib_object_idset.h"
46 #include "got_lib_privsep.h"
47 #include "got_lib_pack.h"
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 static volatile sig_atomic_t sigint_received;
56 catch_sigint(int signo)
61 static const struct got_error *
62 open_object(struct got_object **obj, struct got_pack *pack,
63 struct got_packidx *packidx, int idx, struct got_object_id *id,
64 struct got_object_cache *objcache)
66 const struct got_error *err;
68 err = got_packfile_open_object(obj, pack, packidx, idx, id);
73 err = got_object_cache_add(objcache, id, *obj);
75 if (err->code == GOT_ERR_OBJ_EXISTS ||
76 err->code == GOT_ERR_OBJ_TOO_LARGE)
84 static const struct got_error *
85 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
86 struct got_packidx *packidx, struct got_object_cache *objcache)
88 const struct got_error *err = NULL;
89 struct got_imsg_packed_object iobj;
90 struct got_object *obj;
91 struct got_object_id id;
94 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
95 if (datalen != sizeof(iobj))
96 return got_error(GOT_ERR_PRIVSEP_LEN);
97 memcpy(&iobj, imsg->data, sizeof(iobj));
98 memcpy(&id, &iobj.id, sizeof(id));
100 obj = got_object_cache_get(objcache, &id);
104 err = open_object(&obj, pack, packidx, iobj.idx, &id,
110 err = got_privsep_send_obj(ibuf, obj);
112 got_object_close(obj);
116 static const struct got_error *
117 open_commit(struct got_commit_object **commit, struct got_pack *pack,
118 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
119 struct got_object_cache *objcache)
121 const struct got_error *err = NULL;
122 struct got_object *obj = NULL;
128 obj = got_object_cache_get(objcache, id);
132 err = open_object(&obj, pack, packidx, obj_idx, id,
138 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
144 err = got_object_parse_commit(commit, buf, len);
146 got_object_close(obj);
151 static const struct got_error *
152 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
153 struct got_packidx *packidx, struct got_object_cache *objcache)
155 const struct got_error *err = NULL;
156 struct got_imsg_packed_object iobj;
157 struct got_commit_object *commit = NULL;
158 struct got_object_id id;
161 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
162 if (datalen != sizeof(iobj))
163 return got_error(GOT_ERR_PRIVSEP_LEN);
164 memcpy(&iobj, imsg->data, sizeof(iobj));
165 memcpy(&id, &iobj.id, sizeof(id));
167 err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
171 err = got_privsep_send_commit(ibuf, commit);
174 got_object_commit_close(commit);
176 if (err->code == GOT_ERR_PRIVSEP_PIPE)
179 got_privsep_send_error(ibuf, err);
185 static const struct got_error *
186 open_tree(uint8_t **buf, size_t *len,
187 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
188 struct got_object_id *id, struct got_object_cache *objcache)
190 const struct got_error *err = NULL;
191 struct got_object *obj = NULL;
197 obj = got_object_cache_get(objcache, id);
202 err = open_object(&obj, pack, packidx, obj_idx, id,
208 err = got_packfile_extract_object_to_mem(buf, len, obj, pack);
215 got_object_close(obj);
223 static const struct got_error *
224 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
225 struct got_packidx *packidx, struct got_object_cache *objcache,
226 struct got_parsed_tree_entry **entries, size_t *nentries,
227 size_t *nentries_alloc)
229 const struct got_error *err = NULL;
230 struct got_imsg_packed_object iobj;
233 struct got_object_id id;
236 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
237 if (datalen != sizeof(iobj))
238 return got_error(GOT_ERR_PRIVSEP_LEN);
239 memcpy(&iobj, imsg->data, sizeof(iobj));
240 memcpy(&id, &iobj.id, sizeof(id));
242 err = open_tree(&buf, &len, pack, packidx, iobj.idx, &id, objcache);
246 err = got_object_parse_tree(entries, nentries, nentries_alloc,
251 err = got_privsep_send_tree(ibuf, *entries, *nentries);
253 if (err->code == GOT_ERR_PRIVSEP_PIPE)
256 got_privsep_send_error(ibuf, err);
263 static const struct got_error *
264 receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
266 const struct got_error *err;
270 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
274 if (imsg.hdr.type != imsg_code) {
275 err = got_error(GOT_ERR_PRIVSEP_MSG);
279 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
281 err = got_error(GOT_ERR_PRIVSEP_LEN);
285 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
289 *f = fdopen(imsg.fd, "w+");
291 err = got_error_from_errno("fdopen");
300 static const struct got_error *
301 receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
302 struct imsgbuf *ibuf)
306 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
308 return got_error(GOT_ERR_PRIVSEP_LEN);
311 return got_error(GOT_ERR_PRIVSEP_NO_FD);
313 *f = fdopen(imsg->fd, mode);
315 return got_error_from_errno("fdopen");
321 static const struct got_error *
322 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
323 struct got_packidx *packidx, struct got_object_cache *objcache,
324 FILE *basefile, FILE *accumfile)
326 const struct got_error *err = NULL;
327 struct got_imsg_packed_object iobj;
328 struct got_object *obj = NULL;
329 FILE *outfile = NULL;
330 struct got_object_id id;
335 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
336 if (datalen != sizeof(iobj))
337 return got_error(GOT_ERR_PRIVSEP_LEN);
338 memcpy(&iobj, imsg->data, sizeof(iobj));
339 memcpy(&id, &iobj.id, sizeof(id));
341 obj = got_object_cache_get(objcache, &id);
345 err = open_object(&obj, pack, packidx, iobj.idx, &id,
351 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
355 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
356 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
360 blob_size = obj->size;
362 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
363 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
366 err = got_packfile_extract_object(pack, obj, outfile, basefile,
371 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
374 if (outfile && fclose(outfile) == EOF && err == NULL)
375 err = got_error_from_errno("fclose");
376 got_object_close(obj);
377 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
378 got_privsep_send_error(ibuf, err);
383 static const struct got_error *
384 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
385 struct got_packidx *packidx, struct got_object_cache *objcache)
387 const struct got_error *err = NULL;
388 struct got_imsg_packed_object iobj;
389 struct got_object *obj = NULL;
390 struct got_tag_object *tag = NULL;
393 struct got_object_id id;
396 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
397 if (datalen != sizeof(iobj))
398 return got_error(GOT_ERR_PRIVSEP_LEN);
399 memcpy(&iobj, imsg->data, sizeof(iobj));
400 memcpy(&id, &iobj.id, sizeof(id));
402 obj = got_object_cache_get(objcache, &id);
406 err = open_object(&obj, pack, packidx, iobj.idx, &id,
412 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
417 err = got_object_parse_tag(&tag, buf, len);
421 err = got_privsep_send_tag(ibuf, tag);
424 got_object_close(obj);
426 got_object_tag_close(tag);
428 if (err->code == GOT_ERR_PRIVSEP_PIPE)
431 got_privsep_send_error(ibuf, err);
437 static const struct got_error *
438 tree_path_changed(int *changed, uint8_t **buf1, size_t *len1,
439 uint8_t **buf2, size_t *len2, const char *path,
440 struct got_pack *pack, struct got_packidx *packidx,
441 struct imsgbuf *ibuf, struct got_object_cache *objcache)
443 const struct got_error *err = NULL;
444 struct got_parsed_tree_entry pte1, pte2;
447 size_t remain1 = *len1, remain2 = *len2, elen;
448 uint8_t *next_entry1 = *buf1;
449 uint8_t *next_entry2 = *buf2;
451 memset(&pte1, 0, sizeof(pte1));
452 memset(&pte2, 0, sizeof(pte2));
456 /* We not do support comparing the root path. */
457 if (got_path_is_root_dir(path))
458 return got_error_path(path, GOT_ERR_BAD_PATH);
474 * As an optimization we compare entries in on-disk order
475 * rather than in got_path_cmp() order. We only need to
476 * find out if any entries differ. Parsing all entries and
477 * sorting them slows us down significantly when tree objects
478 * have thousands of entries. We can assume that on-disk entry
479 * ordering is stable, as per got_object_tree_create() and
480 * sort_tree_entries_the_way_git_likes_it(). Other orderings
481 * are incompatible with Git and would yield false positives
484 while (remain1 > 0) {
485 err = got_object_parse_tree_entry(&pte1, &elen,
486 next_entry1, remain1);
491 if (strncmp(pte1.name, seg, seglen) != 0 ||
492 pte1.name[seglen] != '\0') {
493 memset(&pte1, 0, sizeof(pte1));
498 if (pte1.name == NULL) {
499 err = got_error(GOT_ERR_NO_OBJ);
508 while (remain2 > 0) {
509 err = got_object_parse_tree_entry(&pte2, &elen,
510 next_entry2, remain2);
515 if (strncmp(pte2.name, seg, seglen) != 0 ||
516 pte2.name[seglen] != '\0') {
517 memset(&pte2, 0, sizeof(pte2));
523 if (pte2.name == NULL) {
528 if (pte1.mode != pte2.mode) {
533 if (memcmp(pte1.id, pte2.id, SHA1_DIGEST_LENGTH) == 0) {
538 if (*s == '\0') { /* final path element */
547 struct got_object_id id1, id2;
550 memcpy(id1.sha1, pte1.id, SHA1_DIGEST_LENGTH);
551 idx = got_packidx_get_object_idx(packidx, &id1);
553 err = got_error_no_obj(&id1);
558 err = open_tree(buf1, len1, pack, packidx, idx, &id1,
560 memset(&pte1, 0, sizeof(pte1));
566 memcpy(id2.sha1, pte2.id, SHA1_DIGEST_LENGTH);
567 idx = got_packidx_get_object_idx(packidx, &id2);
569 err = got_error_no_obj(&id2);
574 err = open_tree(buf2, len2, pack, packidx, idx, &id2,
576 memset(&pte2, 0, sizeof(pte2));
587 static const struct got_error *
588 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
589 struct imsgbuf *ibuf)
594 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
595 sizeof(struct got_imsg_traversed_commits) +
596 ncommits * sizeof(commit_ids[0]));
598 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
600 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
601 return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
603 for (i = 0; i < ncommits; i++) {
604 struct got_object_id *id = &commit_ids[i];
605 if (imsg_add(wbuf, id, sizeof(*id)) == -1) {
606 return got_error_from_errno(
607 "imsg_add TRAVERSED_COMMITS");
612 imsg_close(ibuf, wbuf);
614 return got_privsep_flush_imsg(ibuf);
617 static const struct got_error *
618 send_commit_traversal_done(struct imsgbuf *ibuf)
620 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
622 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
624 return got_privsep_flush_imsg(ibuf);
627 static const struct got_error *
628 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
629 struct got_pack *pack, struct got_packidx *packidx,
630 struct got_object_cache *objcache)
632 const struct got_error *err = NULL;
633 struct got_imsg_commit_traversal_request ctreq;
634 struct got_object_qid *pid;
635 struct got_commit_object *commit = NULL, *pcommit = NULL;
636 struct got_object_id id;
639 const int min_alloc = 64;
640 int changed = 0, ncommits = 0, nallocated = 0;
641 struct got_object_id *commit_ids = NULL;
643 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
644 if (datalen < sizeof(ctreq))
645 return got_error(GOT_ERR_PRIVSEP_LEN);
646 memcpy(&ctreq, imsg->data, sizeof(ctreq));
647 memcpy(&id, &ctreq.iobj.id, sizeof(id));
649 if (datalen != sizeof(ctreq) + ctreq.path_len)
650 return got_error(GOT_ERR_PRIVSEP_LEN);
651 if (ctreq.path_len == 0)
652 return got_error(GOT_ERR_PRIVSEP_LEN);
654 path = strndup(imsg->data + sizeof(ctreq), ctreq.path_len);
656 return got_error_from_errno("strndup");
658 nallocated = min_alloc;
659 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
660 if (commit_ids == NULL)
661 return got_error_from_errno("reallocarray");
664 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
667 if (sigint_received) {
668 err = got_error(GOT_ERR_CANCELLED);
672 if (commit == NULL) {
673 idx = got_packidx_get_object_idx(packidx, &id);
676 err = open_commit(&commit, pack, packidx,
679 if (err->code != GOT_ERR_NO_OBJ)
686 if (sizeof(struct got_imsg_traversed_commits) +
687 ncommits * sizeof(commit_ids[0]) >= max_datalen) {
688 err = send_traversed_commits(commit_ids, ncommits,
695 if (ncommits > nallocated) {
696 struct got_object_id *new;
697 nallocated += min_alloc;
698 new = reallocarray(commit_ids, nallocated,
699 sizeof(*commit_ids));
701 err = got_error_from_errno("reallocarray");
706 memcpy(&commit_ids[ncommits - 1], &id, sizeof(id));
708 pid = STAILQ_FIRST(&commit->parent_ids);
712 idx = got_packidx_get_object_idx(packidx, &pid->id);
716 err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
719 if (err->code != GOT_ERR_NO_OBJ)
725 if (path[0] == '/' && path[1] == '\0') {
726 if (got_object_id_cmp(pcommit->tree_id,
727 commit->tree_id) != 0) {
733 uint8_t *buf = NULL, *pbuf = NULL;
734 size_t len = 0, plen = 0;
736 idx = got_packidx_get_object_idx(packidx,
740 pidx = got_packidx_get_object_idx(packidx,
745 err = open_tree(&buf, &len, pack, packidx, idx,
746 commit->tree_id, objcache);
750 err = open_tree(&pbuf, &plen, pack, packidx, pidx,
751 pcommit->tree_id, objcache);
757 err = tree_path_changed(&changed, &buf, &len,
758 &pbuf, &plen, path, pack, packidx, ibuf,
764 if (err->code != GOT_ERR_NO_OBJ)
772 memcpy(&id, &pid->id, sizeof(id));
773 got_object_commit_close(commit);
780 err = send_traversed_commits(commit_ids, ncommits, ibuf);
785 err = got_privsep_send_commit(ibuf, commit);
790 err = send_commit_traversal_done(ibuf);
795 got_object_commit_close(commit);
797 got_object_commit_close(pcommit);
799 if (err->code == GOT_ERR_PRIVSEP_PIPE)
802 got_privsep_send_error(ibuf, err);
808 static const struct got_error *
809 raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
810 struct got_pack *pack, struct got_packidx *packidx,
811 struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
813 const struct got_error *err = NULL;
816 FILE *outfile = NULL;
817 struct got_imsg_packed_object iobj;
818 struct got_object *obj;
819 struct got_object_id id;
822 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
823 if (datalen != sizeof(iobj))
824 return got_error(GOT_ERR_PRIVSEP_LEN);
825 memcpy(&iobj, imsg->data, sizeof(iobj));
826 memcpy(&id, &iobj.id, sizeof(id));
828 obj = got_object_cache_get(objcache, &id);
832 err = open_object(&obj, pack, packidx, iobj.idx, &id,
838 err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
842 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
843 err = got_pack_get_max_delta_object_size(&size, obj, pack);
849 if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
850 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
853 err = got_packfile_extract_object(pack, obj, outfile, basefile,
858 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
861 if (outfile && fclose(outfile) == EOF && err == NULL)
862 err = got_error_from_errno("fclose");
863 got_object_close(obj);
864 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
865 got_privsep_send_error(ibuf, err);
870 static const struct got_error *
871 get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
874 const struct got_error *err;
877 err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
881 return got_error(GOT_ERR_BAD_PACKIDX);
883 return got_packidx_get_object_id(base_id, packidx, idx);
886 static const struct got_error *
887 raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
888 FILE *delta_outfile, struct got_pack *pack,
889 struct got_packidx *packidx)
891 const struct got_error *err = NULL;
892 struct got_imsg_raw_delta_request req;
893 size_t datalen, delta_size, delta_compressed_size;
894 off_t delta_offset, delta_data_offset;
895 uint8_t *delta_buf = NULL;
896 struct got_object_id id, base_id;
897 off_t base_offset, delta_out_offset = 0;
898 uint64_t base_size = 0, result_size = 0;
901 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
902 if (datalen != sizeof(req))
903 return got_error(GOT_ERR_PRIVSEP_LEN);
904 memcpy(&req, imsg->data, sizeof(req));
905 memcpy(&id, &req.id, sizeof(id));
909 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
910 &delta_compressed_size, &delta_offset, &delta_data_offset,
911 &base_offset, &base_id, &base_size, &result_size,
912 pack, packidx, req.idx);
917 * If this is an offset delta we must determine the base
918 * object ID ourselves.
920 if (base_offset != 0) {
921 err = get_base_object_id(&base_id, packidx, base_offset);
926 delta_out_offset = ftello(delta_outfile);
927 w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
928 if (w != delta_compressed_size) {
929 err = got_ferror(delta_outfile, GOT_ERR_IO);
932 if (fflush(delta_outfile) == -1) {
933 err = got_error_from_errno("fflush");
937 err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
938 delta_size, delta_compressed_size, delta_offset, delta_out_offset,
945 struct search_deltas_arg {
946 struct imsgbuf *ibuf;
947 struct got_packidx *packidx;
948 struct got_pack *pack;
949 struct got_object_idset *idset;
950 struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
954 static const struct got_error *
955 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
957 const struct got_error *err;
958 struct search_deltas_arg *a = arg;
960 uint8_t *delta_buf = NULL;
961 uint64_t base_size, result_size;
962 size_t delta_size, delta_compressed_size;
963 off_t delta_offset, delta_data_offset, base_offset;
964 struct got_object_id base_id;
967 return got_error(GOT_ERR_CANCELLED);
969 obj_idx = got_packidx_get_object_idx(a->packidx, id);
971 return NULL; /* object not present in our pack file */
973 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
974 &delta_compressed_size, &delta_offset, &delta_data_offset,
975 &base_offset, &base_id, &base_size, &result_size,
976 a->pack, a->packidx, obj_idx);
978 if (err->code == GOT_ERR_OBJ_TYPE)
979 return NULL; /* object not stored as a delta */
984 * If this is an offset delta we must determine the base
985 * object ID ourselves.
987 if (base_offset != 0) {
988 err = get_base_object_id(&base_id, a->packidx, base_offset);
993 if (got_object_idset_contains(a->idset, &base_id)) {
994 struct got_imsg_reused_delta *delta;
996 delta = &a->deltas[a->ndeltas++];
997 memcpy(&delta->id, id, sizeof(delta->id));
998 memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
999 delta->base_size = base_size;
1000 delta->result_size = result_size;
1001 delta->delta_size = delta_size;
1002 delta->delta_compressed_size = delta_compressed_size;
1003 delta->delta_offset = delta_data_offset;
1005 if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
1006 err = got_privsep_send_reused_deltas(a->ibuf,
1007 a->deltas, a->ndeltas);
1018 static const struct got_error *
1019 recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
1021 const struct got_error *err = NULL;
1023 struct got_object_id *ids;
1027 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1030 for (i = 0; i < nids; i++) {
1031 err = got_object_idset_add(idset, &ids[i], NULL);
1043 static const struct got_error *
1044 recv_object_id_queue(struct got_object_id_queue *queue,
1045 struct got_object_idset *queued_ids, struct imsgbuf *ibuf)
1047 const struct got_error *err = NULL;
1049 struct got_object_qid *qid;
1050 struct got_object_id *ids;
1054 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1057 for (i = 0; i < nids; i++) {
1058 err = got_object_qid_alloc_partial(&qid);
1061 memcpy(&qid->id, &ids[i], sizeof(qid->id));
1062 STAILQ_INSERT_TAIL(queue, qid, entry);
1063 err = got_object_idset_add(queued_ids, &qid->id, NULL);
1072 static const struct got_error *
1073 delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1074 struct got_pack *pack, struct got_packidx *packidx)
1076 const struct got_error *err = NULL;
1077 struct got_object_idset *idset;
1078 struct search_deltas_arg sda;
1080 idset = got_object_idset_alloc();
1082 return got_error_from_errno("got_object_idset_alloc");
1084 err = recv_object_ids(idset, ibuf);
1088 memset(&sda, 0, sizeof(sda));
1092 sda.packidx = packidx;
1093 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1097 if (sda.ndeltas > 0) {
1098 err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1104 err = got_privsep_send_reused_deltas_done(ibuf);
1106 got_object_idset_free(idset);
1110 static const struct got_error *
1111 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1113 const struct got_error *err = NULL;
1115 struct got_imsg_packidx ipackidx;
1117 struct got_packidx *p;
1121 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1125 p = calloc(1, sizeof(*p));
1127 err = got_error_from_errno("calloc");
1131 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1132 err = got_error(GOT_ERR_PRIVSEP_MSG);
1136 if (imsg.fd == -1) {
1137 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1141 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1142 if (datalen != sizeof(ipackidx)) {
1143 err = got_error(GOT_ERR_PRIVSEP_LEN);
1146 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1148 p->len = ipackidx.len;
1149 p->fd = dup(imsg.fd);
1151 err = got_error_from_errno("dup");
1154 if (lseek(p->fd, 0, SEEK_SET) == -1) {
1155 err = got_error_from_errno("lseek");
1159 #ifndef GOT_PACK_NO_MMAP
1160 if (p->len > 0 && p->len <= SIZE_MAX) {
1161 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1162 if (p->map == MAP_FAILED)
1163 p->map = NULL; /* fall back to read(2) */
1166 err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1171 got_packidx_close(p);
1178 static const struct got_error *
1179 send_tree_enumeration_done(struct imsgbuf *ibuf)
1181 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENUMERATION_DONE, 0, 0, -1,
1183 return got_error_from_errno("imsg_compose TREE_ENUMERATION_DONE");
1185 return got_privsep_flush_imsg(ibuf);
1188 struct enumerated_tree {
1189 struct got_object_id id;
1192 struct got_parsed_tree_entry *entries;
1196 static const struct got_error *
1197 enumerate_tree(int *have_all_entries, struct imsgbuf *ibuf, size_t *totlen,
1198 struct got_object_id *tree_id,
1199 const char *path, struct got_pack *pack, struct got_packidx *packidx,
1200 struct got_object_cache *objcache, struct got_object_idset *idset,
1201 struct enumerated_tree **trees, size_t *nalloc, size_t *ntrees)
1203 const struct got_error *err = NULL;
1204 struct got_object_id_queue ids;
1205 struct got_object_qid *qid;
1206 uint8_t *buf = NULL;
1208 struct got_parsed_tree_entry *entries = NULL;
1209 size_t nentries = 0, nentries_alloc = 0, i;
1210 struct enumerated_tree *tree;
1213 *have_all_entries = 1;
1216 err = got_object_qid_alloc_partial(&qid);
1219 memcpy(&qid->id, tree_id, sizeof(*tree_id));
1220 qid->data = strdup(path);
1221 if (qid->data == NULL) {
1222 err = got_error_from_errno("strdup");
1225 STAILQ_INSERT_TAIL(&ids, qid, entry);
1228 /* Traverse the tree hierarchy, gather tree object IDs and paths. */
1233 if (sigint_received) {
1234 err = got_error(GOT_ERR_CANCELLED);
1238 qid = STAILQ_FIRST(&ids);
1239 STAILQ_REMOVE_HEAD(&ids, entry);
1242 idx = got_packidx_get_object_idx(packidx, &qid->id);
1244 *have_all_entries = 0;
1248 err = open_tree(&buf, &len, pack, packidx, idx, &qid->id,
1251 if (err->code != GOT_ERR_NO_OBJ)
1255 err = got_object_parse_tree(&entries, &nentries,
1256 &nentries_alloc, buf, len);
1260 err = got_object_idset_add(idset, &qid->id, NULL);
1264 for (i = 0; i < nentries; i++) {
1265 struct got_object_qid *eqid = NULL;
1266 struct got_parsed_tree_entry *pte = &entries[i];
1269 if (!S_ISDIR(pte->mode))
1272 err = got_object_qid_alloc_partial(&eqid);
1275 memcpy(eqid->id.sha1, pte->id, sizeof(eqid->id.sha1));
1277 if (got_object_idset_contains(idset, &eqid->id)) {
1278 got_object_qid_free(eqid);
1282 if (asprintf(&p, "%s%s%s", path,
1283 got_path_is_root_dir(path) ? "" : "/",
1285 err = got_error_from_errno("asprintf");
1286 got_object_qid_free(eqid);
1290 STAILQ_INSERT_TAIL(&ids, eqid, entry);
1293 if (*ntrees >= *nalloc) {
1294 struct enumerated_tree *new;
1295 new = recallocarray(*trees, *nalloc, *nalloc + 16,
1298 err = got_error_from_errno("malloc");
1304 tree = &(*trees)[*ntrees];
1306 memcpy(&tree->id, &qid->id, sizeof(tree->id));
1307 tree->path = qid->data;
1310 tree->entries = entries;
1313 tree->nentries = nentries;
1316 got_object_qid_free(qid);
1318 } while (!STAILQ_EMPTY(&ids));
1320 if (*have_all_entries) {
1323 * We have managed to traverse all entries in the hierarchy.
1324 * Tell the main process what we have found.
1326 for (i = 0; i < *ntrees; i++) {
1327 tree = &(*trees)[i];
1328 err = got_privsep_send_enumerated_tree(totlen,
1329 ibuf, &tree->id, tree->path, tree->entries,
1337 free(tree->entries);
1338 tree->entries = NULL;
1340 *ntrees = 0; /* don't loop again below to free memory */
1342 err = send_tree_enumeration_done(ibuf);
1345 * We can only load fully packed tree hierarchies on
1346 * behalf of the main process, otherwise the main process
1347 * gets a wrong idea about which tree objects have
1348 * already been traversed.
1349 * Indicate a missing entry for the root of this tree.
1350 * The main process should continue by loading this
1351 * entire tree the slow way.
1353 err = got_privsep_send_enumerated_tree(totlen, ibuf,
1354 tree_id, "/", NULL, -1);
1361 for (i = 0; i < *ntrees; i++) {
1362 tree = &(*trees)[i];
1367 free(tree->entries);
1368 tree->entries = NULL;
1372 got_object_qid_free(qid);
1373 got_object_id_queue_free(&ids);
1375 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1378 got_privsep_send_error(ibuf, err);
1384 static const struct got_error *
1385 enumeration_request(struct imsg *imsg, struct imsgbuf *ibuf,
1386 struct got_pack *pack, struct got_packidx *packidx,
1387 struct got_object_cache *objcache)
1389 const struct got_error *err = NULL;
1390 struct got_object_id_queue commit_ids;
1391 const struct got_object_id_queue *parents = NULL;
1392 struct got_object_qid *qid = NULL;
1393 struct got_object *obj = NULL;
1394 struct got_commit_object *commit = NULL;
1395 struct got_object_id *tree_id = NULL;
1397 struct got_object_idset *idset, *queued_ids = NULL;
1398 int i, idx, have_all_entries = 1;
1399 struct enumerated_tree *trees = NULL;
1400 size_t ntrees = 0, nalloc = 16;
1402 STAILQ_INIT(&commit_ids);
1404 trees = calloc(nalloc, sizeof(*trees));
1406 return got_error_from_errno("calloc");
1408 idset = got_object_idset_alloc();
1409 if (idset == NULL) {
1410 err = got_error_from_errno("got_object_idset_alloc");
1414 queued_ids = got_object_idset_alloc();
1415 if (queued_ids == NULL) {
1416 err = got_error_from_errno("got_object_idset_alloc");
1420 err = recv_object_id_queue(&commit_ids, queued_ids, ibuf);
1424 if (STAILQ_EMPTY(&commit_ids)) {
1425 err = got_error(GOT_ERR_PRIVSEP_MSG);
1429 err = recv_object_ids(idset, ibuf);
1433 while (!STAILQ_EMPTY(&commit_ids)) {
1434 if (sigint_received) {
1435 err = got_error(GOT_ERR_CANCELLED);
1439 qid = STAILQ_FIRST(&commit_ids);
1440 STAILQ_REMOVE_HEAD(&commit_ids, entry);
1442 if (got_object_idset_contains(idset, &qid->id)) {
1443 got_object_qid_free(qid);
1448 idx = got_packidx_get_object_idx(packidx, &qid->id);
1450 have_all_entries = 0;
1454 err = open_object(&obj, pack, packidx, idx, &qid->id,
1458 if (obj->type == GOT_OBJ_TYPE_TAG) {
1459 struct got_tag_object *tag;
1462 err = got_packfile_extract_object_to_mem(&buf,
1467 err = got_object_parse_tag(&tag, buf, len);
1472 idx = got_packidx_get_object_idx(packidx, &tag->id);
1474 have_all_entries = 0;
1477 err = open_commit(&commit, pack, packidx, idx,
1478 &tag->id, objcache);
1479 got_object_tag_close(tag);
1483 } else if (obj->type == GOT_OBJ_TYPE_COMMIT) {
1484 err = open_commit(&commit, pack, packidx, idx,
1485 &qid->id, objcache);
1489 err = got_error(GOT_ERR_OBJ_TYPE);
1492 got_object_close(obj);
1495 err = got_privsep_send_enumerated_commit(ibuf, &qid->id,
1496 got_object_commit_get_committer_time(commit));
1500 tree_id = got_object_commit_get_tree_id(commit);
1501 idx = got_packidx_get_object_idx(packidx, tree_id);
1503 have_all_entries = 0;
1504 err = got_privsep_send_enumerated_tree(&totlen, ibuf,
1505 tree_id, "/", NULL, -1);
1511 if (got_object_idset_contains(idset, tree_id)) {
1512 got_object_qid_free(qid);
1514 err = send_tree_enumeration_done(ibuf);
1520 err = enumerate_tree(&have_all_entries, ibuf, &totlen,
1521 tree_id, "/", pack, packidx, objcache, idset,
1522 &trees, &nalloc, &ntrees);
1526 if (!have_all_entries)
1529 got_object_qid_free(qid);
1532 parents = got_object_commit_get_parent_ids(commit);
1534 struct got_object_qid *pid;
1535 STAILQ_FOREACH(pid, parents, entry) {
1536 if (got_object_idset_contains(idset, &pid->id))
1538 if (got_object_idset_contains(queued_ids, &pid->id))
1540 err = got_object_qid_alloc_partial(&qid);
1543 memcpy(&qid->id, &pid->id, sizeof(qid->id));
1544 STAILQ_INSERT_TAIL(&commit_ids, qid, entry);
1549 got_object_commit_close(commit);
1553 if (have_all_entries) {
1554 err = got_privsep_send_object_enumeration_done(ibuf);
1558 err = got_privsep_send_object_enumeration_incomplete(ibuf);
1564 got_object_close(obj);
1566 got_object_commit_close(commit);
1567 got_object_qid_free(qid);
1568 got_object_id_queue_free(&commit_ids);
1570 got_object_idset_free(idset);
1572 got_object_idset_free(queued_ids);
1573 for (i = 0; i < ntrees; i++) {
1574 struct enumerated_tree *tree = &trees[i];
1577 free(tree->entries);
1583 enum findtwixt_color {
1590 static const struct got_error *
1591 paint_commit(struct got_object_qid *qid, intptr_t color)
1593 if (color < 0 || color >= COLOR_MAX)
1594 return got_error(GOT_ERR_RANGE);
1596 qid->data = (void *)color;
1600 static const struct got_error *
1601 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1604 const struct got_error *err;
1605 struct got_object_qid *qid;
1607 err = got_object_qid_alloc_partial(&qid);
1611 memcpy(&qid->id, id, sizeof(qid->id));
1612 STAILQ_INSERT_TAIL(ids, qid, entry);
1613 return paint_commit(qid, color);
1616 static const struct got_error *
1617 paint_commits(struct got_object_id_queue *ids, int *nids,
1618 struct got_object_idset *keep, struct got_object_idset *drop,
1619 struct got_object_idset *skip, struct got_pack *pack,
1620 struct got_packidx *packidx, struct imsgbuf *ibuf,
1621 struct got_object_cache *objcache)
1623 const struct got_error *err = NULL;
1624 struct got_commit_object *commit = NULL;
1625 struct got_object_id_queue painted;
1626 const struct got_object_id_queue *parents;
1627 struct got_object_qid *qid = NULL;
1628 int nqueued = *nids, nskip = 0, npainted = 0;
1630 STAILQ_INIT(&painted);
1632 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1636 if (sigint_received) {
1637 err = got_error(GOT_ERR_CANCELLED);
1641 qid = STAILQ_FIRST(ids);
1642 idx = got_packidx_get_object_idx(packidx, &qid->id);
1648 STAILQ_REMOVE_HEAD(ids, entry);
1650 color = (intptr_t)qid->data;
1651 if (color == COLOR_SKIP)
1654 if (got_object_idset_contains(skip, &qid->id)) {
1655 got_object_qid_free(qid);
1662 if (got_object_idset_contains(keep, &qid->id)) {
1663 got_object_qid_free(qid);
1667 if (got_object_idset_contains(drop, &qid->id)) {
1668 err = paint_commit(qid, COLOR_SKIP);
1672 err = got_object_idset_add(keep, &qid->id, NULL);
1677 if (got_object_idset_contains(drop, &qid->id)) {
1678 got_object_qid_free(qid);
1682 if (got_object_idset_contains(keep, &qid->id)) {
1683 err = paint_commit(qid, COLOR_SKIP);
1687 err = got_object_idset_add(drop, &qid->id, NULL);
1692 if (!got_object_idset_contains(skip, &qid->id)) {
1693 err = got_object_idset_add(skip, &qid->id,
1700 /* should not happen */
1701 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1702 "%s invalid commit color %"PRIdPTR, __func__,
1707 err = open_commit(&commit, pack, packidx, idx, &qid->id,
1712 parents = got_object_commit_get_parent_ids(commit);
1714 struct got_object_qid *pid;
1715 color = (intptr_t)qid->data;
1716 STAILQ_FOREACH(pid, parents, entry) {
1717 err = queue_commit_id(ids, &pid->id, color);
1721 if (color == COLOR_SKIP)
1726 got_object_commit_close(commit);
1729 STAILQ_INSERT_TAIL(&painted, qid, entry);
1733 err = got_privsep_send_painted_commits(ibuf, &painted,
1739 err = got_privsep_send_painted_commits(ibuf, &painted, &npainted, 1, 1);
1746 got_object_commit_close(commit);
1747 got_object_qid_free(qid);
1752 commit_painting_free(struct got_object_idset **keep,
1753 struct got_object_idset **drop,
1754 struct got_object_idset **skip)
1757 got_object_idset_free(*keep);
1761 got_object_idset_free(*drop);
1765 got_object_idset_free(*skip);
1770 static const struct got_error *
1771 commit_painting_init(struct imsgbuf *ibuf, struct got_object_idset **keep,
1772 struct got_object_idset **drop, struct got_object_idset **skip)
1774 const struct got_error *err = NULL;
1776 *keep = got_object_idset_alloc();
1777 if (*keep == NULL) {
1778 err = got_error_from_errno("got_object_idset_alloc");
1781 *drop = got_object_idset_alloc();
1782 if (*drop == NULL) {
1783 err = got_error_from_errno("got_object_idset_alloc");
1786 *skip = got_object_idset_alloc();
1787 if (*skip == NULL) {
1788 err = got_error_from_errno("got_object_idset_alloc");
1792 err = recv_object_ids(*keep, ibuf);
1795 err = recv_object_ids(*drop, ibuf);
1798 err = recv_object_ids(*skip, ibuf);
1804 commit_painting_free(keep, drop, skip);
1809 static const struct got_error *
1810 commit_painting_request(struct imsg *imsg, struct imsgbuf *ibuf,
1811 struct got_pack *pack, struct got_packidx *packidx,
1812 struct got_object_cache *objcache, struct got_object_idset *keep,
1813 struct got_object_idset *drop, struct got_object_idset *skip)
1815 const struct got_error *err = NULL;
1816 struct got_imsg_commit_painting_request ireq;
1817 struct got_object_id id;
1819 struct got_object_id_queue ids;
1824 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1825 if (datalen != sizeof(ireq))
1826 return got_error(GOT_ERR_PRIVSEP_LEN);
1827 memcpy(&ireq, imsg->data, sizeof(ireq));
1828 memcpy(&id, &ireq.id, sizeof(id));
1830 err = queue_commit_id(&ids, &id, ireq.color);
1835 err = paint_commits(&ids, &nids, keep, drop, skip,
1836 pack, packidx, ibuf, objcache);
1840 err = got_privsep_send_painted_commits(ibuf, &ids, &nids, 0, 1);
1844 err = got_privsep_send_painting_commits_done(ibuf);
1846 got_object_id_queue_free(&ids);
1850 static const struct got_error *
1851 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1853 const struct got_error *err = NULL;
1855 struct got_imsg_pack ipack;
1857 struct got_pack *pack;
1861 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1865 pack = calloc(1, sizeof(*pack));
1867 err = got_error_from_errno("calloc");
1871 if (imsg.hdr.type != GOT_IMSG_PACK) {
1872 err = got_error(GOT_ERR_PRIVSEP_MSG);
1876 if (imsg.fd == -1) {
1877 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1881 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1882 if (datalen != sizeof(ipack)) {
1883 err = got_error(GOT_ERR_PRIVSEP_LEN);
1886 memcpy(&ipack, imsg.data, sizeof(ipack));
1888 pack->filesize = ipack.filesize;
1889 pack->fd = dup(imsg.fd);
1890 if (pack->fd == -1) {
1891 err = got_error_from_errno("dup");
1894 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1895 err = got_error_from_errno("lseek");
1898 pack->path_packfile = strdup(ipack.path_packfile);
1899 if (pack->path_packfile == NULL) {
1900 err = got_error_from_errno("strdup");
1904 err = got_delta_cache_alloc(&pack->delta_cache);
1908 #ifndef GOT_PACK_NO_MMAP
1909 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1910 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1912 if (pack->map == MAP_FAILED)
1913 pack->map = NULL; /* fall back to read(2) */
1928 main(int argc, char *argv[])
1930 const struct got_error *err = NULL;
1931 struct imsgbuf ibuf;
1933 struct got_packidx *packidx = NULL;
1934 struct got_pack *pack = NULL;
1935 struct got_object_cache objcache;
1936 FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1937 struct got_object_idset *keep = NULL, *drop = NULL, *skip = NULL;
1938 struct got_parsed_tree_entry *entries = NULL;
1939 size_t nentries = 0, nentries_alloc = 0;
1941 //static int attached;
1942 //while (!attached) sleep(1);
1944 signal(SIGINT, catch_sigint);
1946 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1948 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1950 err = got_error_from_errno("got_object_cache_init");
1951 got_privsep_send_error(&ibuf, err);
1956 /* revoke access to most system calls */
1957 if (pledge("stdio recvfd", NULL) == -1) {
1958 err = got_error_from_errno("pledge");
1959 got_privsep_send_error(&ibuf, err);
1963 /* revoke fs access */
1964 if (landlock_no_fs() == -1) {
1965 err = got_error_from_errno("landlock_no_fs");
1966 got_privsep_send_error(&ibuf, err);
1969 if (cap_enter() == -1) {
1970 err = got_error_from_errno("cap_enter");
1971 got_privsep_send_error(&ibuf, err);
1976 err = receive_packidx(&packidx, &ibuf);
1978 got_privsep_send_error(&ibuf, err);
1982 err = receive_pack(&pack, &ibuf);
1984 got_privsep_send_error(&ibuf, err);
1991 if (sigint_received) {
1992 err = got_error(GOT_ERR_CANCELLED);
1996 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1998 if (err->code == GOT_ERR_PRIVSEP_PIPE)
2003 if (imsg.hdr.type == GOT_IMSG_STOP)
2006 switch (imsg.hdr.type) {
2007 case GOT_IMSG_TMPFD:
2008 if (basefile == NULL) {
2009 err = receive_tempfile(&basefile, "w+",
2011 } else if (accumfile == NULL) {
2012 err = receive_tempfile(&accumfile, "w+",
2015 err = got_error(GOT_ERR_PRIVSEP_MSG);
2017 case GOT_IMSG_PACKED_OBJECT_REQUEST:
2018 err = object_request(&imsg, &ibuf, pack, packidx,
2021 case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
2022 if (basefile == NULL || accumfile == NULL) {
2023 err = got_error(GOT_ERR_PRIVSEP_MSG);
2026 err = raw_object_request(&imsg, &ibuf, pack, packidx,
2027 &objcache, basefile, accumfile);
2029 case GOT_IMSG_RAW_DELTA_OUTFD:
2030 if (delta_outfile != NULL) {
2031 err = got_error(GOT_ERR_PRIVSEP_MSG);
2034 err = receive_tempfile(&delta_outfile, "w",
2037 case GOT_IMSG_RAW_DELTA_REQUEST:
2038 if (delta_outfile == NULL) {
2039 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
2042 err = raw_delta_request(&imsg, &ibuf, delta_outfile,
2045 case GOT_IMSG_DELTA_REUSE_REQUEST:
2046 err = delta_reuse_request(&imsg, &ibuf, pack, packidx);
2048 case GOT_IMSG_COMMIT_REQUEST:
2049 err = commit_request(&imsg, &ibuf, pack, packidx,
2052 case GOT_IMSG_TREE_REQUEST:
2053 err = tree_request(&imsg, &ibuf, pack, packidx,
2054 &objcache, &entries, &nentries, &nentries_alloc);
2056 case GOT_IMSG_BLOB_REQUEST:
2057 if (basefile == NULL || accumfile == NULL) {
2058 err = got_error(GOT_ERR_PRIVSEP_MSG);
2061 err = blob_request(&imsg, &ibuf, pack, packidx,
2062 &objcache, basefile, accumfile);
2064 case GOT_IMSG_TAG_REQUEST:
2065 err = tag_request(&imsg, &ibuf, pack, packidx,
2068 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
2069 err = commit_traversal_request(&imsg, &ibuf, pack,
2070 packidx, &objcache);
2072 case GOT_IMSG_OBJECT_ENUMERATION_REQUEST:
2073 err = enumeration_request(&imsg, &ibuf, pack,
2074 packidx, &objcache);
2076 case GOT_IMSG_COMMIT_PAINTING_INIT:
2077 commit_painting_free(&keep, &drop, &skip);
2078 err = commit_painting_init(&ibuf, &keep, &drop, &skip);
2080 case GOT_IMSG_COMMIT_PAINTING_REQUEST:
2081 if (keep == NULL || drop == NULL || skip == NULL) {
2082 err = got_error(GOT_ERR_PRIVSEP_MSG);
2085 err = commit_painting_request(&imsg, &ibuf, pack,
2086 packidx, &objcache, keep, drop, skip);
2088 case GOT_IMSG_COMMIT_PAINTING_DONE:
2089 commit_painting_free(&keep, &drop, &skip);
2092 err = got_error(GOT_ERR_PRIVSEP_MSG);
2096 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
2097 err = got_error_from_errno("close");
2104 commit_painting_free(&keep, &drop, &skip);
2106 got_packidx_close(packidx);
2108 got_pack_close(pack);
2109 got_object_cache_close(&objcache);
2111 if (basefile && fclose(basefile) == EOF && err == NULL)
2112 err = got_error_from_errno("fclose");
2113 if (accumfile && fclose(accumfile) == EOF && err == NULL)
2114 err = got_error_from_errno("fclose");
2115 if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
2116 err = got_error_from_errno("fclose");
2118 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
2119 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
2120 got_privsep_send_error(&ibuf, err);
2123 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
2124 err = got_error_from_errno("close");