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.
18 #include <sys/types.h>
19 #include <sys/queue.h>
37 #include "got_error.h"
38 #include "got_object.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_delta_cache.h"
43 #include "got_lib_object.h"
44 #include "got_lib_object_qid.h"
45 #include "got_lib_object_cache.h"
46 #include "got_lib_object_parse.h"
47 #include "got_lib_object_idset.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_pack.h"
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 static volatile sig_atomic_t sigint_received;
58 catch_sigint(int signo)
63 static const struct got_error *
64 open_object(struct got_object **obj, struct got_pack *pack,
65 struct got_packidx *packidx, int idx, struct got_object_id *id,
66 struct got_object_cache *objcache)
68 const struct got_error *err;
70 err = got_packfile_open_object(obj, pack, packidx, idx, id);
75 err = got_object_cache_add(objcache, id, *obj);
77 if (err->code == GOT_ERR_OBJ_EXISTS ||
78 err->code == GOT_ERR_OBJ_TOO_LARGE)
86 static const struct got_error *
87 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
88 struct got_packidx *packidx, struct got_object_cache *objcache)
90 const struct got_error *err = NULL;
91 struct got_imsg_packed_object iobj;
92 struct got_object *obj;
93 struct got_object_id id;
96 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
97 if (datalen != sizeof(iobj))
98 return got_error(GOT_ERR_PRIVSEP_LEN);
99 memcpy(&iobj, imsg->data, sizeof(iobj));
100 memcpy(&id, &iobj.id, sizeof(id));
102 obj = got_object_cache_get(objcache, &id);
106 err = open_object(&obj, pack, packidx, iobj.idx, &id,
112 err = got_privsep_send_obj(ibuf, obj);
114 got_object_close(obj);
118 static const struct got_error *
119 open_commit(struct got_commit_object **commit, struct got_pack *pack,
120 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
121 struct got_object_cache *objcache)
123 const struct got_error *err = NULL;
124 struct got_object *obj = NULL;
130 obj = got_object_cache_get(objcache, id);
134 err = open_object(&obj, pack, packidx, obj_idx, id,
140 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
146 err = got_object_parse_commit(commit, buf, len);
148 got_object_close(obj);
153 static const struct got_error *
154 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
155 struct got_packidx *packidx, struct got_object_cache *objcache)
157 const struct got_error *err = NULL;
158 struct got_imsg_packed_object iobj;
159 struct got_commit_object *commit = NULL;
160 struct got_object_id id;
163 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
164 if (datalen != sizeof(iobj))
165 return got_error(GOT_ERR_PRIVSEP_LEN);
166 memcpy(&iobj, imsg->data, sizeof(iobj));
167 memcpy(&id, &iobj.id, sizeof(id));
169 err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
173 err = got_privsep_send_commit(ibuf, commit);
176 got_object_commit_close(commit);
178 if (err->code == GOT_ERR_PRIVSEP_PIPE)
181 got_privsep_send_error(ibuf, err);
187 static const struct got_error *
188 open_tree(uint8_t **buf, size_t *len,
189 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
190 struct got_object_id *id, struct got_object_cache *objcache)
192 const struct got_error *err = NULL;
193 struct got_object *obj = NULL;
199 obj = got_object_cache_get(objcache, id);
204 err = open_object(&obj, pack, packidx, obj_idx, id,
210 err = got_packfile_extract_object_to_mem(buf, len, obj, pack);
217 got_object_close(obj);
225 static const struct got_error *
226 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
227 struct got_packidx *packidx, struct got_object_cache *objcache,
228 struct got_parsed_tree_entry **entries, size_t *nentries,
229 size_t *nentries_alloc)
231 const struct got_error *err = NULL;
232 struct got_imsg_packed_object iobj;
235 struct got_object_id id;
238 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
239 if (datalen != sizeof(iobj))
240 return got_error(GOT_ERR_PRIVSEP_LEN);
241 memcpy(&iobj, imsg->data, sizeof(iobj));
242 memcpy(&id, &iobj.id, sizeof(id));
244 err = open_tree(&buf, &len, pack, packidx, iobj.idx, &id, objcache);
248 err = got_object_parse_tree(entries, nentries, nentries_alloc,
253 err = got_privsep_send_tree(ibuf, *entries, *nentries);
255 if (err->code == GOT_ERR_PRIVSEP_PIPE)
258 got_privsep_send_error(ibuf, err);
265 static const struct got_error *
266 receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
268 const struct got_error *err;
273 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
277 if (imsg.hdr.type != imsg_code) {
278 err = got_error(GOT_ERR_PRIVSEP_MSG);
282 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
284 err = got_error(GOT_ERR_PRIVSEP_LEN);
287 fd = imsg_get_fd(&imsg);
289 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
293 *f = fdopen(fd, "w+");
295 err = got_error_from_errno("fdopen");
304 static const struct got_error *
305 receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
306 struct imsgbuf *ibuf)
308 const struct got_error *err;
312 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
314 return got_error(GOT_ERR_PRIVSEP_LEN);
316 fd = imsg_get_fd(imsg);
318 return got_error(GOT_ERR_PRIVSEP_NO_FD);
320 *f = fdopen(fd, mode);
322 err = got_error_from_errno("fdopen");
330 static const struct got_error *
331 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
332 struct got_packidx *packidx, struct got_object_cache *objcache,
333 FILE *basefile, FILE *accumfile)
335 const struct got_error *err = NULL;
336 struct got_imsg_packed_object iobj;
337 struct got_object *obj = NULL;
338 FILE *outfile = NULL;
339 struct got_object_id id;
344 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
345 if (datalen != sizeof(iobj))
346 return got_error(GOT_ERR_PRIVSEP_LEN);
347 memcpy(&iobj, imsg->data, sizeof(iobj));
348 memcpy(&id, &iobj.id, sizeof(id));
350 obj = got_object_cache_get(objcache, &id);
354 err = open_object(&obj, pack, packidx, iobj.idx, &id,
360 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
364 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
365 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
369 blob_size = obj->size;
371 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
372 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
375 err = got_packfile_extract_object(pack, obj, outfile, basefile,
380 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
383 if (outfile && fclose(outfile) == EOF && err == NULL)
384 err = got_error_from_errno("fclose");
385 got_object_close(obj);
386 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
387 got_privsep_send_error(ibuf, err);
392 static const struct got_error *
393 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
394 struct got_packidx *packidx, struct got_object_cache *objcache)
396 const struct got_error *err = NULL;
397 struct got_imsg_packed_object iobj;
398 struct got_object *obj = NULL;
399 struct got_tag_object *tag = NULL;
402 struct got_object_id id;
405 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
406 if (datalen != sizeof(iobj))
407 return got_error(GOT_ERR_PRIVSEP_LEN);
408 memcpy(&iobj, imsg->data, sizeof(iobj));
409 memcpy(&id, &iobj.id, sizeof(id));
411 obj = got_object_cache_get(objcache, &id);
415 err = open_object(&obj, pack, packidx, iobj.idx, &id,
421 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
426 err = got_object_parse_tag(&tag, buf, len);
430 err = got_privsep_send_tag(ibuf, tag);
433 got_object_close(obj);
435 got_object_tag_close(tag);
437 if (err->code == GOT_ERR_PRIVSEP_PIPE)
440 got_privsep_send_error(ibuf, err);
446 static const struct got_error *
447 tree_path_changed(int *changed, uint8_t **buf1, size_t *len1,
448 uint8_t **buf2, size_t *len2, const char *path,
449 struct got_pack *pack, struct got_packidx *packidx,
450 struct imsgbuf *ibuf, struct got_object_cache *objcache)
452 const struct got_error *err = NULL;
453 struct got_parsed_tree_entry pte1, pte2;
456 size_t remain1 = *len1, remain2 = *len2, elen;
457 uint8_t *next_entry1 = *buf1;
458 uint8_t *next_entry2 = *buf2;
460 memset(&pte1, 0, sizeof(pte1));
461 memset(&pte2, 0, sizeof(pte2));
465 /* We not do support comparing the root path. */
466 if (got_path_is_root_dir(path))
467 return got_error_path(path, GOT_ERR_BAD_PATH);
483 * As an optimization we compare entries in on-disk order
484 * rather than in got_path_cmp() order. We only need to
485 * find out if any entries differ. Parsing all entries and
486 * sorting them slows us down significantly when tree objects
487 * have thousands of entries. We can assume that on-disk entry
488 * ordering is stable, as per got_object_tree_create() and
489 * sort_tree_entries_the_way_git_likes_it(). Other orderings
490 * are incompatible with Git and would yield false positives
493 while (remain1 > 0) {
494 err = got_object_parse_tree_entry(&pte1, &elen,
495 next_entry1, remain1);
500 if (strncmp(pte1.name, seg, seglen) != 0 ||
501 pte1.name[seglen] != '\0') {
502 memset(&pte1, 0, sizeof(pte1));
507 if (pte1.name == NULL) {
508 err = got_error(GOT_ERR_NO_OBJ);
517 while (remain2 > 0) {
518 err = got_object_parse_tree_entry(&pte2, &elen,
519 next_entry2, remain2);
524 if (strncmp(pte2.name, seg, seglen) != 0 ||
525 pte2.name[seglen] != '\0') {
526 memset(&pte2, 0, sizeof(pte2));
532 if (pte2.name == NULL) {
537 if (pte1.mode != pte2.mode) {
542 if (memcmp(pte1.id, pte2.id, SHA1_DIGEST_LENGTH) == 0) {
547 if (*s == '\0') { /* final path element */
556 struct got_object_id id1, id2;
559 memcpy(id1.sha1, pte1.id, SHA1_DIGEST_LENGTH);
560 idx = got_packidx_get_object_idx(packidx, &id1);
562 err = got_error_no_obj(&id1);
567 err = open_tree(buf1, len1, pack, packidx, idx, &id1,
569 memset(&pte1, 0, sizeof(pte1));
575 memcpy(id2.sha1, pte2.id, SHA1_DIGEST_LENGTH);
576 idx = got_packidx_get_object_idx(packidx, &id2);
578 err = got_error_no_obj(&id2);
583 err = open_tree(buf2, len2, pack, packidx, idx, &id2,
585 memset(&pte2, 0, sizeof(pte2));
596 static const struct got_error *
597 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
598 struct imsgbuf *ibuf)
603 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
604 sizeof(struct got_imsg_traversed_commits) +
605 ncommits * sizeof(commit_ids[0]));
607 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
609 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
610 return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
612 for (i = 0; i < ncommits; i++) {
613 struct got_object_id *id = &commit_ids[i];
614 if (imsg_add(wbuf, id, sizeof(*id)) == -1) {
615 return got_error_from_errno(
616 "imsg_add TRAVERSED_COMMITS");
620 imsg_close(ibuf, wbuf);
622 return got_privsep_flush_imsg(ibuf);
625 static const struct got_error *
626 send_commit_traversal_done(struct imsgbuf *ibuf)
628 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
630 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
632 return got_privsep_flush_imsg(ibuf);
635 static const struct got_error *
636 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
637 struct got_pack *pack, struct got_packidx *packidx,
638 struct got_object_cache *objcache)
640 const struct got_error *err = NULL;
641 struct got_imsg_commit_traversal_request ctreq;
642 struct got_object_qid *pid;
643 struct got_commit_object *commit = NULL, *pcommit = NULL;
644 struct got_object_id id;
647 const int min_alloc = 64;
648 int changed = 0, ncommits = 0, nallocated = 0;
649 struct got_object_id *commit_ids = NULL;
651 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
652 if (datalen < sizeof(ctreq))
653 return got_error(GOT_ERR_PRIVSEP_LEN);
654 memcpy(&ctreq, imsg->data, sizeof(ctreq));
655 memcpy(&id, &ctreq.iobj.id, sizeof(id));
657 if (datalen != sizeof(ctreq) + ctreq.path_len)
658 return got_error(GOT_ERR_PRIVSEP_LEN);
659 if (ctreq.path_len == 0)
660 return got_error(GOT_ERR_PRIVSEP_LEN);
662 path = strndup(imsg->data + sizeof(ctreq), ctreq.path_len);
664 return got_error_from_errno("strndup");
666 nallocated = min_alloc;
667 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
668 if (commit_ids == NULL)
669 return got_error_from_errno("reallocarray");
672 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
675 if (sigint_received) {
676 err = got_error(GOT_ERR_CANCELLED);
680 if (commit == NULL) {
681 idx = got_packidx_get_object_idx(packidx, &id);
684 err = open_commit(&commit, pack, packidx,
687 if (err->code != GOT_ERR_NO_OBJ)
694 if (sizeof(struct got_imsg_traversed_commits) +
695 ncommits * sizeof(commit_ids[0]) >= max_datalen) {
696 err = send_traversed_commits(commit_ids, ncommits,
703 if (ncommits > nallocated) {
704 struct got_object_id *new;
705 nallocated += min_alloc;
706 new = reallocarray(commit_ids, nallocated,
707 sizeof(*commit_ids));
709 err = got_error_from_errno("reallocarray");
714 memcpy(&commit_ids[ncommits - 1], &id, sizeof(id));
716 pid = STAILQ_FIRST(&commit->parent_ids);
720 idx = got_packidx_get_object_idx(packidx, &pid->id);
724 err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
727 if (err->code != GOT_ERR_NO_OBJ)
733 if (path[0] == '/' && path[1] == '\0') {
734 if (got_object_id_cmp(pcommit->tree_id,
735 commit->tree_id) != 0) {
741 uint8_t *buf = NULL, *pbuf = NULL;
742 size_t len = 0, plen = 0;
744 idx = got_packidx_get_object_idx(packidx,
748 pidx = got_packidx_get_object_idx(packidx,
753 err = open_tree(&buf, &len, pack, packidx, idx,
754 commit->tree_id, objcache);
758 err = open_tree(&pbuf, &plen, pack, packidx, pidx,
759 pcommit->tree_id, objcache);
765 err = tree_path_changed(&changed, &buf, &len,
766 &pbuf, &plen, path, pack, packidx, ibuf,
772 if (err->code != GOT_ERR_NO_OBJ)
780 memcpy(&id, &pid->id, sizeof(id));
781 got_object_commit_close(commit);
788 err = send_traversed_commits(commit_ids, ncommits, ibuf);
793 err = got_privsep_send_commit(ibuf, commit);
798 err = send_commit_traversal_done(ibuf);
803 got_object_commit_close(commit);
805 got_object_commit_close(pcommit);
807 if (err->code == GOT_ERR_PRIVSEP_PIPE)
810 got_privsep_send_error(ibuf, err);
816 static const struct got_error *
817 raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
818 struct got_pack *pack, struct got_packidx *packidx,
819 struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
821 const struct got_error *err = NULL;
824 FILE *outfile = NULL;
825 struct got_imsg_packed_object iobj;
826 struct got_object *obj;
827 struct got_object_id id;
830 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
831 if (datalen != sizeof(iobj))
832 return got_error(GOT_ERR_PRIVSEP_LEN);
833 memcpy(&iobj, imsg->data, sizeof(iobj));
834 memcpy(&id, &iobj.id, sizeof(id));
836 obj = got_object_cache_get(objcache, &id);
840 err = open_object(&obj, pack, packidx, iobj.idx, &id,
846 err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
850 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
851 err = got_pack_get_max_delta_object_size(&size, obj, pack);
857 if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
858 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
861 err = got_packfile_extract_object(pack, obj, outfile, basefile,
866 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
869 if (outfile && fclose(outfile) == EOF && err == NULL)
870 err = got_error_from_errno("fclose");
871 got_object_close(obj);
872 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
873 got_privsep_send_error(ibuf, err);
878 static const struct got_error *
879 get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
882 const struct got_error *err;
885 err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
889 return got_error(GOT_ERR_BAD_PACKIDX);
891 return got_packidx_get_object_id(base_id, packidx, idx);
894 static const struct got_error *
895 raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
896 FILE *delta_outfile, struct got_pack *pack,
897 struct got_packidx *packidx)
899 const struct got_error *err = NULL;
900 struct got_imsg_raw_delta_request req;
901 size_t datalen, delta_size, delta_compressed_size;
902 off_t delta_offset, delta_data_offset;
903 uint8_t *delta_buf = NULL;
904 struct got_object_id id, base_id;
905 off_t base_offset, delta_out_offset = 0;
906 uint64_t base_size = 0, result_size = 0;
909 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
910 if (datalen != sizeof(req))
911 return got_error(GOT_ERR_PRIVSEP_LEN);
912 memcpy(&req, imsg->data, sizeof(req));
913 memcpy(&id, &req.id, sizeof(id));
915 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
916 &delta_compressed_size, &delta_offset, &delta_data_offset,
917 &base_offset, &base_id, &base_size, &result_size,
918 pack, packidx, req.idx);
923 * If this is an offset delta we must determine the base
924 * object ID ourselves.
926 if (base_offset != 0) {
927 err = get_base_object_id(&base_id, packidx, base_offset);
932 delta_out_offset = ftello(delta_outfile);
933 w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
934 if (w != delta_compressed_size) {
935 err = got_ferror(delta_outfile, GOT_ERR_IO);
938 if (fflush(delta_outfile) == -1) {
939 err = got_error_from_errno("fflush");
943 err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
944 delta_size, delta_compressed_size, delta_offset, delta_out_offset,
951 struct search_deltas_arg {
952 struct imsgbuf *ibuf;
953 struct got_packidx *packidx;
954 struct got_pack *pack;
955 struct got_object_idset *idset;
956 struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
960 static const struct got_error *
961 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
963 const struct got_error *err;
964 struct search_deltas_arg *a = arg;
966 uint8_t *delta_buf = NULL;
967 uint64_t base_size, result_size;
968 size_t delta_size, delta_compressed_size;
969 off_t delta_offset, delta_data_offset, base_offset;
970 struct got_object_id base_id;
973 return got_error(GOT_ERR_CANCELLED);
975 obj_idx = got_packidx_get_object_idx(a->packidx, id);
977 return NULL; /* object not present in our pack file */
979 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
980 &delta_compressed_size, &delta_offset, &delta_data_offset,
981 &base_offset, &base_id, &base_size, &result_size,
982 a->pack, a->packidx, obj_idx);
984 if (err->code == GOT_ERR_OBJ_TYPE)
985 return NULL; /* object not stored as a delta */
990 * If this is an offset delta we must determine the base
991 * object ID ourselves.
993 if (base_offset != 0) {
994 err = get_base_object_id(&base_id, a->packidx, base_offset);
999 if (got_object_idset_contains(a->idset, &base_id)) {
1000 struct got_imsg_reused_delta *delta;
1002 delta = &a->deltas[a->ndeltas++];
1003 memcpy(&delta->id, id, sizeof(delta->id));
1004 memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
1005 delta->base_size = base_size;
1006 delta->result_size = result_size;
1007 delta->delta_size = delta_size;
1008 delta->delta_compressed_size = delta_compressed_size;
1009 delta->delta_offset = delta_data_offset;
1011 if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
1012 err = got_privsep_send_reused_deltas(a->ibuf,
1013 a->deltas, a->ndeltas);
1024 static const struct got_error *
1025 recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
1027 const struct got_error *err = NULL;
1029 struct got_object_id *ids;
1033 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1036 for (i = 0; i < nids; i++) {
1037 err = got_object_idset_add(idset, &ids[i], NULL);
1049 static const struct got_error *
1050 recv_object_id_queue(struct got_object_id_queue *queue,
1051 struct got_object_idset *queued_ids, struct imsgbuf *ibuf)
1053 const struct got_error *err = NULL;
1055 struct got_object_qid *qid;
1056 struct got_object_id *ids;
1060 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1063 for (i = 0; i < nids; i++) {
1064 err = got_object_qid_alloc_partial(&qid);
1067 memcpy(&qid->id, &ids[i], sizeof(qid->id));
1068 STAILQ_INSERT_TAIL(queue, qid, entry);
1069 err = got_object_idset_add(queued_ids, &qid->id, NULL);
1078 static const struct got_error *
1079 delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1080 struct got_pack *pack, struct got_packidx *packidx)
1082 const struct got_error *err = NULL;
1083 struct got_object_idset *idset;
1084 struct search_deltas_arg sda;
1086 idset = got_object_idset_alloc();
1088 return got_error_from_errno("got_object_idset_alloc");
1090 err = recv_object_ids(idset, ibuf);
1094 memset(&sda, 0, sizeof(sda));
1098 sda.packidx = packidx;
1099 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1103 if (sda.ndeltas > 0) {
1104 err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1110 err = got_privsep_send_reused_deltas_done(ibuf);
1112 got_object_idset_free(idset);
1116 static const struct got_error *
1117 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1119 const struct got_error *err = NULL;
1121 struct got_imsg_packidx ipackidx;
1123 struct got_packidx *p;
1127 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1131 p = calloc(1, sizeof(*p));
1133 err = got_error_from_errno("calloc");
1137 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1138 err = got_error(GOT_ERR_PRIVSEP_MSG);
1142 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1143 if (datalen != sizeof(ipackidx)) {
1144 err = got_error(GOT_ERR_PRIVSEP_LEN);
1147 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1149 p->fd = imsg_get_fd(&imsg);
1150 p->len = ipackidx.len;
1152 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1155 if (lseek(p->fd, 0, SEEK_SET) == -1) {
1156 err = got_error_from_errno("lseek");
1160 #ifndef GOT_PACK_NO_MMAP
1161 if (p->len > 0 && p->len <= SIZE_MAX) {
1162 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1163 if (p->map == MAP_FAILED)
1164 p->map = NULL; /* fall back to read(2) */
1167 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 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1877 if (datalen != sizeof(ipack)) {
1878 err = got_error(GOT_ERR_PRIVSEP_LEN);
1881 memcpy(&ipack, imsg.data, sizeof(ipack));
1883 pack->filesize = ipack.filesize;
1884 pack->fd = imsg_get_fd(&imsg);
1885 if (pack->fd == -1) {
1886 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1889 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1890 err = got_error_from_errno("lseek");
1893 pack->path_packfile = strdup(ipack.path_packfile);
1894 if (pack->path_packfile == NULL) {
1895 err = got_error_from_errno("strdup");
1899 err = got_delta_cache_alloc(&pack->delta_cache);
1903 #ifndef GOT_PACK_NO_MMAP
1904 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1905 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1907 if (pack->map == MAP_FAILED)
1908 pack->map = NULL; /* fall back to read(2) */
1914 got_pack_close(pack);
1922 main(int argc, char *argv[])
1924 const struct got_error *err = NULL;
1925 struct imsgbuf ibuf;
1927 struct got_packidx *packidx = NULL;
1928 struct got_pack *pack = NULL;
1929 struct got_object_cache objcache;
1930 FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1931 struct got_object_idset *keep = NULL, *drop = NULL, *skip = NULL;
1932 struct got_parsed_tree_entry *entries = NULL;
1933 size_t nentries = 0, nentries_alloc = 0;
1935 //static int attached;
1936 //while (!attached) sleep(1);
1938 signal(SIGINT, catch_sigint);
1940 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1942 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1944 err = got_error_from_errno("got_object_cache_init");
1945 got_privsep_send_error(&ibuf, err);
1950 /* revoke access to most system calls */
1951 if (pledge("stdio recvfd", NULL) == -1) {
1952 err = got_error_from_errno("pledge");
1953 got_privsep_send_error(&ibuf, err);
1958 err = receive_packidx(&packidx, &ibuf);
1960 got_privsep_send_error(&ibuf, err);
1964 err = receive_pack(&pack, &ibuf);
1966 got_privsep_send_error(&ibuf, err);
1971 if (sigint_received) {
1972 err = got_error(GOT_ERR_CANCELLED);
1976 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1978 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1983 if (imsg.hdr.type == GOT_IMSG_STOP) {
1988 switch (imsg.hdr.type) {
1989 case GOT_IMSG_TMPFD:
1990 if (basefile == NULL) {
1991 err = receive_tempfile(&basefile, "w+",
1993 } else if (accumfile == NULL) {
1994 err = receive_tempfile(&accumfile, "w+",
1997 err = got_error(GOT_ERR_PRIVSEP_MSG);
1999 case GOT_IMSG_PACKED_OBJECT_REQUEST:
2000 err = object_request(&imsg, &ibuf, pack, packidx,
2003 case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
2004 if (basefile == NULL || accumfile == NULL) {
2005 err = got_error(GOT_ERR_PRIVSEP_MSG);
2008 err = raw_object_request(&imsg, &ibuf, pack, packidx,
2009 &objcache, basefile, accumfile);
2011 case GOT_IMSG_RAW_DELTA_OUTFD:
2012 if (delta_outfile != NULL) {
2013 err = got_error(GOT_ERR_PRIVSEP_MSG);
2016 err = receive_tempfile(&delta_outfile, "w",
2019 case GOT_IMSG_RAW_DELTA_REQUEST:
2020 if (delta_outfile == NULL) {
2021 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
2024 err = raw_delta_request(&imsg, &ibuf, delta_outfile,
2027 case GOT_IMSG_DELTA_REUSE_REQUEST:
2028 err = delta_reuse_request(&imsg, &ibuf, pack, packidx);
2030 case GOT_IMSG_COMMIT_REQUEST:
2031 err = commit_request(&imsg, &ibuf, pack, packidx,
2034 case GOT_IMSG_TREE_REQUEST:
2035 err = tree_request(&imsg, &ibuf, pack, packidx,
2036 &objcache, &entries, &nentries, &nentries_alloc);
2038 case GOT_IMSG_BLOB_REQUEST:
2039 if (basefile == NULL || accumfile == NULL) {
2040 err = got_error(GOT_ERR_PRIVSEP_MSG);
2043 err = blob_request(&imsg, &ibuf, pack, packidx,
2044 &objcache, basefile, accumfile);
2046 case GOT_IMSG_TAG_REQUEST:
2047 err = tag_request(&imsg, &ibuf, pack, packidx,
2050 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
2051 err = commit_traversal_request(&imsg, &ibuf, pack,
2052 packidx, &objcache);
2054 case GOT_IMSG_OBJECT_ENUMERATION_REQUEST:
2055 err = enumeration_request(&imsg, &ibuf, pack,
2056 packidx, &objcache);
2058 case GOT_IMSG_COMMIT_PAINTING_INIT:
2059 commit_painting_free(&keep, &drop, &skip);
2060 err = commit_painting_init(&ibuf, &keep, &drop, &skip);
2062 case GOT_IMSG_COMMIT_PAINTING_REQUEST:
2063 if (keep == NULL || drop == NULL || skip == NULL) {
2064 err = got_error(GOT_ERR_PRIVSEP_MSG);
2067 err = commit_painting_request(&imsg, &ibuf, pack,
2068 packidx, &objcache, keep, drop, skip);
2070 case GOT_IMSG_COMMIT_PAINTING_DONE:
2071 commit_painting_free(&keep, &drop, &skip);
2074 err = got_error(GOT_ERR_PRIVSEP_MSG);
2084 commit_painting_free(&keep, &drop, &skip);
2086 got_packidx_close(packidx);
2088 got_pack_close(pack);
2089 got_object_cache_close(&objcache);
2091 if (basefile && fclose(basefile) == EOF && err == NULL)
2092 err = got_error_from_errno("fclose");
2093 if (accumfile && fclose(accumfile) == EOF && err == NULL)
2094 err = got_error_from_errno("fclose");
2095 if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
2096 err = got_error_from_errno("fclose");
2098 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
2099 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
2100 got_privsep_send_error(&ibuf, err);
2103 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
2104 err = got_error_from_errno("close");