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>
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_cache.h"
43 #include "got_lib_object_parse.h"
44 #include "got_lib_object_idset.h"
45 #include "got_lib_privsep.h"
46 #include "got_lib_pack.h"
49 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 static volatile sig_atomic_t sigint_received;
55 catch_sigint(int signo)
60 static const struct got_error *
61 open_object(struct got_object **obj, struct got_pack *pack,
62 struct got_packidx *packidx, int idx, struct got_object_id *id,
63 struct got_object_cache *objcache)
65 const struct got_error *err;
67 err = got_packfile_open_object(obj, pack, packidx, idx, id);
72 err = got_object_cache_add(objcache, id, *obj);
74 if (err->code == GOT_ERR_OBJ_EXISTS ||
75 err->code == GOT_ERR_OBJ_TOO_LARGE)
83 static const struct got_error *
84 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
85 struct got_packidx *packidx, struct got_object_cache *objcache)
87 const struct got_error *err = NULL;
88 struct got_imsg_packed_object iobj;
89 struct got_object *obj;
90 struct got_object_id id;
93 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
94 if (datalen != sizeof(iobj))
95 return got_error(GOT_ERR_PRIVSEP_LEN);
96 memcpy(&iobj, imsg->data, sizeof(iobj));
97 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
99 obj = got_object_cache_get(objcache, &id);
103 err = open_object(&obj, pack, packidx, iobj.idx, &id,
109 err = got_privsep_send_obj(ibuf, obj);
111 got_object_close(obj);
115 static const struct got_error *
116 open_commit(struct got_commit_object **commit, struct got_pack *pack,
117 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
118 struct got_object_cache *objcache)
120 const struct got_error *err = NULL;
121 struct got_object *obj = NULL;
127 obj = got_object_cache_get(objcache, id);
131 err = open_object(&obj, pack, packidx, obj_idx, id,
137 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
143 err = got_object_parse_commit(commit, buf, len);
145 got_object_close(obj);
150 static const struct got_error *
151 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
152 struct got_packidx *packidx, struct got_object_cache *objcache)
154 const struct got_error *err = NULL;
155 struct got_imsg_packed_object iobj;
156 struct got_commit_object *commit = NULL;
157 struct got_object_id id;
160 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
161 if (datalen != sizeof(iobj))
162 return got_error(GOT_ERR_PRIVSEP_LEN);
163 memcpy(&iobj, imsg->data, sizeof(iobj));
164 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
166 err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
170 err = got_privsep_send_commit(ibuf, commit);
173 got_object_commit_close(commit);
175 if (err->code == GOT_ERR_PRIVSEP_PIPE)
178 got_privsep_send_error(ibuf, err);
184 static const struct got_error *
185 open_tree(uint8_t **buf, struct got_parsed_tree_entry **entries, int *nentries,
186 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
187 struct got_object_id *id, struct got_object_cache *objcache)
189 const struct got_error *err = NULL;
190 struct got_object *obj = NULL;
196 obj = got_object_cache_get(objcache, id);
200 err = open_object(&obj, pack, packidx, obj_idx, id,
206 err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
212 err = got_object_parse_tree(entries, nentries, *buf, len);
214 got_object_close(obj);
222 static const struct got_error *
223 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
224 struct got_packidx *packidx, struct got_object_cache *objcache)
226 const struct got_error *err = NULL;
227 struct got_imsg_packed_object iobj;
228 struct got_parsed_tree_entry *entries = NULL;
231 struct got_object_id id;
234 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
235 if (datalen != sizeof(iobj))
236 return got_error(GOT_ERR_PRIVSEP_LEN);
237 memcpy(&iobj, imsg->data, sizeof(iobj));
238 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
240 err = open_tree(&buf, &entries, &nentries, pack, packidx, iobj.idx,
245 err = got_privsep_send_tree(ibuf, entries, nentries);
249 if (err->code == GOT_ERR_PRIVSEP_PIPE)
252 got_privsep_send_error(ibuf, err);
258 static const struct got_error *
259 receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
261 const struct got_error *err;
265 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
269 if (imsg.hdr.type != imsg_code) {
270 err = got_error(GOT_ERR_PRIVSEP_MSG);
274 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
276 err = got_error(GOT_ERR_PRIVSEP_LEN);
280 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
284 *f = fdopen(imsg.fd, "w+");
286 err = got_error_from_errno("fdopen");
295 static const struct got_error *
296 receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
297 struct imsgbuf *ibuf)
301 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
303 return got_error(GOT_ERR_PRIVSEP_LEN);
306 return got_error(GOT_ERR_PRIVSEP_NO_FD);
308 *f = fdopen(imsg->fd, mode);
310 return got_error_from_errno("fdopen");
316 static const struct got_error *
317 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
318 struct got_packidx *packidx, struct got_object_cache *objcache,
319 FILE *basefile, FILE *accumfile)
321 const struct got_error *err = NULL;
322 struct got_imsg_packed_object iobj;
323 struct got_object *obj = NULL;
324 FILE *outfile = NULL;
325 struct got_object_id id;
330 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
331 if (datalen != sizeof(iobj))
332 return got_error(GOT_ERR_PRIVSEP_LEN);
333 memcpy(&iobj, imsg->data, sizeof(iobj));
334 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
336 obj = got_object_cache_get(objcache, &id);
340 err = open_object(&obj, pack, packidx, iobj.idx, &id,
346 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
350 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
351 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
355 blob_size = obj->size;
357 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
358 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
361 err = got_packfile_extract_object(pack, obj, outfile, basefile,
366 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
369 if (outfile && fclose(outfile) == EOF && err == NULL)
370 err = got_error_from_errno("fclose");
371 got_object_close(obj);
372 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
373 got_privsep_send_error(ibuf, err);
378 static const struct got_error *
379 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
380 struct got_packidx *packidx, struct got_object_cache *objcache)
382 const struct got_error *err = NULL;
383 struct got_imsg_packed_object iobj;
384 struct got_object *obj = NULL;
385 struct got_tag_object *tag = NULL;
388 struct got_object_id id;
391 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
392 if (datalen != sizeof(iobj))
393 return got_error(GOT_ERR_PRIVSEP_LEN);
394 memcpy(&iobj, imsg->data, sizeof(iobj));
395 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
397 obj = got_object_cache_get(objcache, &id);
401 err = open_object(&obj, pack, packidx, iobj.idx, &id,
407 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
412 err = got_object_parse_tag(&tag, buf, len);
416 err = got_privsep_send_tag(ibuf, tag);
419 got_object_close(obj);
421 got_object_tag_close(tag);
423 if (err->code == GOT_ERR_PRIVSEP_PIPE)
426 got_privsep_send_error(ibuf, err);
432 static struct got_parsed_tree_entry *
433 find_entry_by_name(struct got_parsed_tree_entry *entries, int nentries,
434 const char *name, size_t len)
436 struct got_parsed_tree_entry *pte;
439 /* Note that tree entries are sorted in strncmp() order. */
440 for (i = 0; i < nentries; i++) {
442 cmp = strncmp(pte->name, name, len);
447 if (pte->name[len] == '\0')
453 static const struct got_error *
454 tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
455 struct got_parsed_tree_entry **entries1, int *nentries1,
456 struct got_parsed_tree_entry **entries2, int *nentries2,
457 const char *path, struct got_pack *pack, struct got_packidx *packidx,
458 struct imsgbuf *ibuf, struct got_object_cache *objcache)
460 const struct got_error *err = NULL;
461 struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
467 /* We not do support comparing the root path. */
468 if (got_path_is_root_dir(path))
469 return got_error_path(path, GOT_ERR_BAD_PATH);
484 pte1 = find_entry_by_name(*entries1, *nentries1, seg, seglen);
486 err = got_error(GOT_ERR_NO_OBJ);
490 pte2 = find_entry_by_name(*entries2, *nentries2, seg, seglen);
496 if (pte1->mode != pte2->mode) {
501 if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
506 if (*s == '\0') { /* final path element */
515 struct got_object_id id1, id2;
518 memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
519 idx = got_packidx_get_object_idx(packidx, &id1);
521 err = got_error_no_obj(&id1);
528 err = open_tree(buf1, entries1, nentries1, pack,
529 packidx, idx, &id1, objcache);
534 memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
535 idx = got_packidx_get_object_idx(packidx, &id2);
537 err = got_error_no_obj(&id2);
544 err = open_tree(buf2, entries2, nentries2, pack,
545 packidx, idx, &id2, objcache);
555 static const struct got_error *
556 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
557 struct imsgbuf *ibuf)
562 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
563 sizeof(struct got_imsg_traversed_commits) +
564 ncommits * SHA1_DIGEST_LENGTH);
566 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
568 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
569 return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
571 for (i = 0; i < ncommits; i++) {
572 struct got_object_id *id = &commit_ids[i];
573 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
574 return got_error_from_errno(
575 "imsg_add TRAVERSED_COMMITS");
580 imsg_close(ibuf, wbuf);
582 return got_privsep_flush_imsg(ibuf);
585 static const struct got_error *
586 send_commit_traversal_done(struct imsgbuf *ibuf)
588 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
590 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
592 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_parsed_tree_entry *entries = NULL, *pentries = NULL;
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 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
614 if (datalen < sizeof(iobj))
615 return got_error(GOT_ERR_PRIVSEP_LEN);
616 memcpy(&iobj, imsg->data, sizeof(iobj));
617 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
619 path_len = datalen - sizeof(iobj) - 1;
621 return got_error(GOT_ERR_PRIVSEP_LEN);
623 path = imsg->data + sizeof(iobj);
624 if (path[path_len] != '\0')
625 return got_error(GOT_ERR_PRIVSEP_LEN);
628 nallocated = min_alloc;
629 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
630 if (commit_ids == NULL)
631 return got_error_from_errno("reallocarray");
634 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
637 if (sigint_received) {
638 err = got_error(GOT_ERR_CANCELLED);
642 if (commit == NULL) {
643 idx = got_packidx_get_object_idx(packidx, &id);
646 err = open_commit(&commit, pack, packidx,
649 if (err->code != GOT_ERR_NO_OBJ)
656 if (sizeof(struct got_imsg_traversed_commits) +
657 ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
658 err = send_traversed_commits(commit_ids, ncommits,
665 if (ncommits > nallocated) {
666 struct got_object_id *new;
667 nallocated += min_alloc;
668 new = reallocarray(commit_ids, nallocated,
669 sizeof(*commit_ids));
671 err = got_error_from_errno("reallocarray");
676 memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
679 pid = STAILQ_FIRST(&commit->parent_ids);
683 idx = got_packidx_get_object_idx(packidx, &pid->id);
687 err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
690 if (err->code != GOT_ERR_NO_OBJ)
696 if (path[0] == '/' && path[1] == '\0') {
697 if (got_object_id_cmp(pcommit->tree_id,
698 commit->tree_id) != 0) {
704 uint8_t *buf = NULL, *pbuf = NULL;
706 idx = got_packidx_get_object_idx(packidx,
710 pidx = got_packidx_get_object_idx(packidx,
715 err = open_tree(&buf, &entries, &nentries, pack,
716 packidx, idx, commit->tree_id, objcache);
719 err = open_tree(&pbuf, &pentries, &pnentries, pack,
720 packidx, pidx, pcommit->tree_id, objcache);
726 err = tree_path_changed(&changed, &buf, &pbuf,
727 &entries, &nentries, &pentries, &pnentries, path,
728 pack, packidx, ibuf, objcache);
739 if (err->code != GOT_ERR_NO_OBJ)
747 memcpy(id.sha1, pid->id.sha1, SHA1_DIGEST_LENGTH);
748 got_object_commit_close(commit);
755 err = send_traversed_commits(commit_ids, ncommits, ibuf);
760 err = got_privsep_send_commit(ibuf, commit);
765 err = send_commit_traversal_done(ibuf);
769 got_object_commit_close(commit);
771 got_object_commit_close(pcommit);
775 if (err->code == GOT_ERR_PRIVSEP_PIPE)
778 got_privsep_send_error(ibuf, err);
784 static const struct got_error *
785 raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
786 struct got_pack *pack, struct got_packidx *packidx,
787 struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
789 const struct got_error *err = NULL;
792 FILE *outfile = NULL;
793 struct got_imsg_packed_object iobj;
794 struct got_object *obj;
795 struct got_object_id id;
798 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
799 if (datalen != sizeof(iobj))
800 return got_error(GOT_ERR_PRIVSEP_LEN);
801 memcpy(&iobj, imsg->data, sizeof(iobj));
802 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
804 obj = got_object_cache_get(objcache, &id);
808 err = open_object(&obj, pack, packidx, iobj.idx, &id,
814 err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
818 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
819 err = got_pack_get_max_delta_object_size(&size, obj, pack);
825 if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
826 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
829 err = got_packfile_extract_object(pack, obj, outfile, basefile,
834 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
837 if (outfile && fclose(outfile) == EOF && err == NULL)
838 err = got_error_from_errno("fclose");
839 got_object_close(obj);
840 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
841 got_privsep_send_error(ibuf, err);
846 static const struct got_error *
847 get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
850 const struct got_error *err;
853 err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
857 return got_error(GOT_ERR_BAD_PACKIDX);
859 return got_packidx_get_object_id(base_id, packidx, idx);
862 static const struct got_error *
863 raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
864 FILE *delta_outfile, struct got_pack *pack,
865 struct got_packidx *packidx)
867 const struct got_error *err = NULL;
868 struct got_imsg_raw_delta_request req;
869 size_t datalen, delta_size, delta_compressed_size;
871 uint8_t *delta_buf = NULL;
872 struct got_object_id id, base_id;
873 off_t base_offset, delta_out_offset = 0;
874 uint64_t base_size = 0, result_size = 0;
877 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
878 if (datalen != sizeof(req))
879 return got_error(GOT_ERR_PRIVSEP_LEN);
880 memcpy(&req, imsg->data, sizeof(req));
881 memcpy(id.sha1, req.id, SHA1_DIGEST_LENGTH);
885 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
886 &delta_compressed_size, &delta_offset, &base_offset, &base_id,
887 &base_size, &result_size, pack, packidx, req.idx);
892 * If this is an offset delta we must determine the base
893 * object ID ourselves.
895 if (base_offset != 0) {
896 err = get_base_object_id(&base_id, packidx, base_offset);
901 delta_out_offset = ftello(delta_outfile);
902 w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
903 if (w != delta_compressed_size) {
904 err = got_ferror(delta_outfile, GOT_ERR_IO);
907 if (fflush(delta_outfile) == -1) {
908 err = got_error_from_errno("fflush");
912 err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
913 delta_size, delta_compressed_size, delta_offset, delta_out_offset,
920 struct search_deltas_arg {
921 struct imsgbuf *ibuf;
922 struct got_packidx *packidx;
923 struct got_pack *pack;
924 struct got_object_idset *idset;
926 struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
930 static const struct got_error *
931 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
933 const struct got_error *err;
934 struct search_deltas_arg *a = arg;
936 uint8_t *delta_buf = NULL;
937 uint64_t base_size, result_size;
938 size_t delta_size, delta_compressed_size;
939 off_t delta_offset, base_offset;
940 struct got_object_id base_id;
943 return got_error(GOT_ERR_CANCELLED);
945 obj_idx = got_packidx_get_object_idx(a->packidx, id);
947 return NULL; /* object not present in our pack file */
949 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
950 &delta_compressed_size, &delta_offset, &base_offset, &base_id,
951 &base_size, &result_size, a->pack, a->packidx, obj_idx);
953 if (err->code == GOT_ERR_OBJ_TYPE)
954 return NULL; /* object not stored as a delta */
959 * If this is an offset delta we must determine the base
960 * object ID ourselves.
962 if (base_offset != 0) {
963 err = get_base_object_id(&base_id, a->packidx, base_offset);
968 if (got_object_idset_contains(a->idset, &base_id)) {
969 struct got_imsg_reused_delta *delta;
970 off_t delta_out_offset = ftello(a->delta_outfile);
973 w = fwrite(delta_buf, 1, delta_compressed_size,
975 if (w != delta_compressed_size) {
976 err = got_ferror(a->delta_outfile, GOT_ERR_IO);
980 delta = &a->deltas[a->ndeltas++];
981 memcpy(&delta->id, id, sizeof(delta->id));
982 memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
983 delta->base_size = base_size;
984 delta->result_size = result_size;
985 delta->delta_size = delta_size;
986 delta->delta_compressed_size = delta_compressed_size;
987 delta->delta_offset = delta_offset;
988 delta->delta_out_offset = delta_out_offset;
990 if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
991 err = got_privsep_send_reused_deltas(a->ibuf,
992 a->deltas, a->ndeltas);
1003 static const struct got_error *
1004 recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
1006 const struct got_error *err = NULL;
1008 struct got_object_id *ids;
1012 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1015 for (i = 0; i < nids; i++) {
1016 err = got_object_idset_add(idset, &ids[i], NULL);
1028 static const struct got_error *
1029 recv_object_id_queue(struct got_object_id_queue *queue, struct imsgbuf *ibuf)
1031 const struct got_error *err = NULL;
1033 struct got_object_qid *qid;
1034 struct got_object_id *ids;
1038 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1041 for (i = 0; i < nids; i++) {
1042 err = got_object_qid_alloc_partial(&qid);
1045 memcpy(&qid->id, &ids[i], sizeof(qid->id));
1046 STAILQ_INSERT_TAIL(queue, qid, entry);
1053 static const struct got_error *
1054 delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1055 FILE *delta_outfile, struct got_pack *pack, struct got_packidx *packidx)
1057 const struct got_error *err = NULL;
1058 struct got_object_idset *idset;
1059 struct search_deltas_arg sda;
1061 idset = got_object_idset_alloc();
1063 return got_error_from_errno("got_object_idset_alloc");
1065 err = recv_object_ids(idset, ibuf);
1069 memset(&sda, 0, sizeof(sda));
1073 sda.packidx = packidx;
1074 sda.delta_outfile = delta_outfile;
1075 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1079 if (sda.ndeltas > 0) {
1080 err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1086 if (fflush(delta_outfile) == -1) {
1087 err = got_error_from_errno("fflush");
1091 err = got_privsep_send_reused_deltas_done(ibuf);
1093 got_object_idset_free(idset);
1097 static const struct got_error *
1098 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1100 const struct got_error *err = NULL;
1102 struct got_imsg_packidx ipackidx;
1104 struct got_packidx *p;
1108 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1112 p = calloc(1, sizeof(*p));
1114 err = got_error_from_errno("calloc");
1118 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1119 err = got_error(GOT_ERR_PRIVSEP_MSG);
1123 if (imsg.fd == -1) {
1124 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1128 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1129 if (datalen != sizeof(ipackidx)) {
1130 err = got_error(GOT_ERR_PRIVSEP_LEN);
1133 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1135 p->len = ipackidx.len;
1136 p->fd = dup(imsg.fd);
1138 err = got_error_from_errno("dup");
1141 if (lseek(p->fd, 0, SEEK_SET) == -1) {
1142 err = got_error_from_errno("lseek");
1146 #ifndef GOT_PACK_NO_MMAP
1147 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1148 if (p->map == MAP_FAILED)
1149 p->map = NULL; /* fall back to read(2) */
1151 err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1156 got_packidx_close(p);
1163 static const struct got_error *
1164 send_tree_enumeration_done(struct imsgbuf *ibuf)
1166 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENUMERATION_DONE, 0, 0, -1,
1168 return got_error_from_errno("imsg_compose TREE_ENUMERATION_DONE");
1170 return got_privsep_flush_imsg(ibuf);
1173 struct enumerated_tree {
1174 struct got_object_id id;
1177 struct got_parsed_tree_entry *entries;
1181 static const struct got_error *
1182 enumerate_tree(int *have_all_entries, struct imsgbuf *ibuf, size_t *totlen,
1183 struct got_object_id *tree_id,
1184 const char *path, struct got_pack *pack, struct got_packidx *packidx,
1185 struct got_object_cache *objcache, struct got_object_idset *idset,
1186 struct enumerated_tree **trees, size_t *nalloc, size_t *ntrees)
1188 const struct got_error *err = NULL;
1189 struct got_object_id_queue ids;
1190 struct got_object_qid *qid;
1191 uint8_t *buf = NULL;
1192 struct got_parsed_tree_entry *entries = NULL;
1193 int nentries = 0, i;
1194 struct enumerated_tree *tree;
1197 *have_all_entries = 1;
1200 err = got_object_qid_alloc_partial(&qid);
1203 memcpy(&qid->id.sha1, tree_id, SHA1_DIGEST_LENGTH);
1204 qid->data = strdup(path);
1205 if (qid->data == NULL) {
1206 err = got_error_from_errno("strdup");
1209 STAILQ_INSERT_TAIL(&ids, qid, entry);
1212 /* Traverse the tree hierarchy, gather tree object IDs and paths. */
1217 if (sigint_received) {
1218 err = got_error(GOT_ERR_CANCELLED);
1222 qid = STAILQ_FIRST(&ids);
1223 STAILQ_REMOVE_HEAD(&ids, entry);
1226 idx = got_packidx_get_object_idx(packidx, &qid->id);
1228 *have_all_entries = 0;
1232 err = open_tree(&buf, &entries, &nentries,
1233 pack, packidx, idx, &qid->id, objcache);
1235 if (err->code != GOT_ERR_NO_OBJ)
1239 err = got_object_idset_add(idset, &qid->id, NULL);
1243 for (i = 0; i < nentries; i++) {
1244 struct got_object_qid *eqid = NULL;
1245 struct got_parsed_tree_entry *pte = &entries[i];
1248 if (!S_ISDIR(pte->mode))
1251 err = got_object_qid_alloc_partial(&eqid);
1254 memcpy(eqid->id.sha1, pte->id, sizeof(eqid->id.sha1));
1256 if (got_object_idset_contains(idset, &eqid->id)) {
1257 got_object_qid_free(eqid);
1261 if (asprintf(&p, "%s%s%s", path,
1262 got_path_is_root_dir(path) ? "" : "/",
1264 err = got_error_from_errno("asprintf");
1265 got_object_qid_free(eqid);
1269 STAILQ_INSERT_TAIL(&ids, eqid, entry);
1272 if (*ntrees >= *nalloc) {
1273 struct enumerated_tree *new;
1274 new = recallocarray(*trees, *nalloc, *nalloc + 16,
1277 err = got_error_from_errno("malloc");
1283 tree = &(*trees)[*ntrees];
1285 memcpy(&tree->id, &qid->id, sizeof(tree->id));
1286 tree->path = qid->data;
1289 tree->entries = entries;
1291 tree->nentries = nentries;
1293 got_object_qid_free(qid);
1295 } while (!STAILQ_EMPTY(&ids));
1297 if (*have_all_entries) {
1300 * We have managed to traverse all entries in the hierarchy.
1301 * Tell the main process what we have found.
1303 for (i = 0; i < *ntrees; i++) {
1304 tree = &(*trees)[i];
1305 err = got_privsep_send_enumerated_tree(totlen,
1306 ibuf, &tree->id, tree->path, tree->entries,
1314 free(tree->entries);
1315 tree->entries = NULL;
1317 *ntrees = 0; /* don't loop again below to free memory */
1319 err = send_tree_enumeration_done(ibuf);
1322 * We can only load fully packed tree hierarchies on
1323 * behalf of the main process, otherwise the main process
1324 * gets a wrong idea about which tree objects have
1325 * already been traversed.
1326 * Indicate a missing entry for the root of this tree.
1327 * The main process should continue by loading this
1328 * entire tree the slow way.
1330 err = got_privsep_send_enumerated_tree(totlen, ibuf,
1331 tree_id, "/", NULL, -1);
1338 for (i = 0; i < *ntrees; i++) {
1339 tree = &(*trees)[i];
1344 free(tree->entries);
1345 tree->entries = NULL;
1349 got_object_qid_free(qid);
1350 got_object_id_queue_free(&ids);
1352 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1355 got_privsep_send_error(ibuf, err);
1361 static const struct got_error *
1362 enumeration_request(struct imsg *imsg, struct imsgbuf *ibuf,
1363 struct got_pack *pack, struct got_packidx *packidx,
1364 struct got_object_cache *objcache)
1366 const struct got_error *err = NULL;
1367 struct got_object_id_queue commit_ids;
1368 const struct got_object_id_queue *parents = NULL;
1369 struct got_object_qid *qid = NULL;
1370 struct got_object *obj = NULL;
1371 struct got_commit_object *commit = NULL;
1372 struct got_object_id *tree_id = NULL;
1374 struct got_object_idset *idset;
1375 int i, idx, have_all_entries = 1;
1376 struct enumerated_tree *trees = NULL;
1377 size_t ntrees = 0, nalloc = 16;
1379 STAILQ_INIT(&commit_ids);
1381 trees = calloc(nalloc, sizeof(*trees));
1383 return got_error_from_errno("calloc");
1385 idset = got_object_idset_alloc();
1386 if (idset == NULL) {
1387 err = got_error_from_errno("got_object_idset_alloc");
1391 err = recv_object_id_queue(&commit_ids, ibuf);
1395 if (STAILQ_EMPTY(&commit_ids)) {
1396 err = got_error(GOT_ERR_PRIVSEP_MSG);
1400 err = recv_object_ids(idset, ibuf);
1404 while (!STAILQ_EMPTY(&commit_ids)) {
1405 if (sigint_received) {
1406 err = got_error(GOT_ERR_CANCELLED);
1410 qid = STAILQ_FIRST(&commit_ids);
1411 STAILQ_REMOVE_HEAD(&commit_ids, entry);
1413 if (got_object_idset_contains(idset, &qid->id)) {
1414 got_object_qid_free(qid);
1419 idx = got_packidx_get_object_idx(packidx, &qid->id);
1421 have_all_entries = 0;
1425 err = open_object(&obj, pack, packidx, idx, &qid->id,
1429 if (obj->type == GOT_OBJ_TYPE_TAG) {
1430 struct got_tag_object *tag;
1433 err = got_packfile_extract_object_to_mem(&buf,
1438 err = got_object_parse_tag(&tag, buf, len);
1443 idx = got_packidx_get_object_idx(packidx, &tag->id);
1445 have_all_entries = 0;
1448 err = open_commit(&commit, pack, packidx, idx,
1449 &tag->id, objcache);
1450 got_object_tag_close(tag);
1454 } else if (obj->type == GOT_OBJ_TYPE_COMMIT) {
1455 err = open_commit(&commit, pack, packidx, idx,
1456 &qid->id, objcache);
1460 err = got_error(GOT_ERR_OBJ_TYPE);
1463 got_object_close(obj);
1466 err = got_privsep_send_enumerated_commit(ibuf, &qid->id,
1467 got_object_commit_get_committer_time(commit));
1471 tree_id = got_object_commit_get_tree_id(commit);
1472 idx = got_packidx_get_object_idx(packidx, tree_id);
1474 have_all_entries = 0;
1475 err = got_privsep_send_enumerated_tree(&totlen, ibuf,
1476 tree_id, "/", NULL, -1);
1482 if (got_object_idset_contains(idset, tree_id)) {
1483 got_object_qid_free(qid);
1488 err = enumerate_tree(&have_all_entries, ibuf, &totlen,
1489 tree_id, "/", pack, packidx, objcache, idset,
1490 &trees, &nalloc, &ntrees);
1494 if (!have_all_entries)
1497 got_object_qid_free(qid);
1500 parents = got_object_commit_get_parent_ids(commit);
1502 struct got_object_qid *pid;
1503 STAILQ_FOREACH(pid, parents, entry) {
1504 if (got_object_idset_contains(idset, &pid->id))
1506 err = got_object_qid_alloc_partial(&qid);
1509 memcpy(&qid->id, &pid->id, sizeof(qid->id));
1510 STAILQ_INSERT_TAIL(&commit_ids, qid, entry);
1515 got_object_commit_close(commit);
1519 if (have_all_entries) {
1520 err = got_privsep_send_object_enumeration_done(ibuf);
1524 err = got_privsep_send_object_enumeration_incomplete(ibuf);
1530 got_object_close(obj);
1532 got_object_commit_close(commit);
1533 got_object_qid_free(qid);
1534 got_object_id_queue_free(&commit_ids);
1536 got_object_idset_free(idset);
1537 for (i = 0; i < ntrees; i++) {
1538 struct enumerated_tree *tree = &trees[i];
1541 free(tree->entries);
1547 enum findtwixt_color {
1554 static const struct got_error *
1555 paint_commit(struct got_object_qid *qid, intptr_t color)
1557 if (color < 0 || color >= COLOR_MAX)
1558 return got_error(GOT_ERR_RANGE);
1560 qid->data = (void *)color;
1564 static const struct got_error *
1565 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1568 const struct got_error *err;
1569 struct got_object_qid *qid;
1571 err = got_object_qid_alloc_partial(&qid);
1575 memcpy(&qid->id, id, sizeof(qid->id));
1576 STAILQ_INSERT_TAIL(ids, qid, entry);
1577 return paint_commit(qid, color);
1580 static const struct got_error *
1581 paint_commits(struct got_object_id_queue *ids, int *nids,
1582 struct got_object_idset *keep, struct got_object_idset *drop,
1583 struct got_object_idset *skip, struct got_pack *pack,
1584 struct got_packidx *packidx, struct imsgbuf *ibuf,
1585 struct got_object_cache *objcache)
1587 const struct got_error *err = NULL;
1588 struct got_commit_object *commit = NULL;
1589 struct got_object_id_queue painted;
1590 const struct got_object_id_queue *parents;
1591 struct got_object_qid *qid = NULL;
1592 int nqueued = *nids, nskip = 0, npainted = 0;
1594 STAILQ_INIT(&painted);
1596 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1600 if (sigint_received) {
1601 err = got_error(GOT_ERR_CANCELLED);
1605 qid = STAILQ_FIRST(ids);
1606 idx = got_packidx_get_object_idx(packidx, &qid->id);
1612 STAILQ_REMOVE_HEAD(ids, entry);
1614 color = (intptr_t)qid->data;
1615 if (color == COLOR_SKIP)
1618 if (got_object_idset_contains(skip, &qid->id)) {
1619 got_object_qid_free(qid);
1626 if (got_object_idset_contains(keep, &qid->id)) {
1627 got_object_qid_free(qid);
1631 if (got_object_idset_contains(drop, &qid->id)) {
1632 err = paint_commit(qid, COLOR_SKIP);
1636 err = got_object_idset_add(keep, &qid->id, NULL);
1641 if (got_object_idset_contains(drop, &qid->id)) {
1642 got_object_qid_free(qid);
1646 if (got_object_idset_contains(keep, &qid->id)) {
1647 err = paint_commit(qid, COLOR_SKIP);
1651 err = got_object_idset_add(drop, &qid->id, NULL);
1656 if (!got_object_idset_contains(skip, &qid->id)) {
1657 err = got_object_idset_add(skip, &qid->id,
1664 /* should not happen */
1665 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1666 "%s invalid commit color %d", __func__, color);
1670 err = open_commit(&commit, pack, packidx, idx, &qid->id,
1675 parents = got_object_commit_get_parent_ids(commit);
1677 struct got_object_qid *pid;
1678 color = (intptr_t)qid->data;
1679 STAILQ_FOREACH(pid, parents, entry) {
1680 err = queue_commit_id(ids, &pid->id, color);
1684 if (color == COLOR_SKIP)
1689 got_object_commit_close(commit);
1692 STAILQ_INSERT_TAIL(&painted, qid, entry);
1696 err = got_privsep_send_painted_commits(ibuf, &painted,
1702 err = got_privsep_send_painted_commits(ibuf, &painted, &npainted, 1, 1);
1709 got_object_commit_close(commit);
1710 got_object_qid_free(qid);
1715 commit_painting_free(struct got_object_idset **keep,
1716 struct got_object_idset **drop,
1717 struct got_object_idset **skip)
1720 got_object_idset_free(*keep);
1724 got_object_idset_free(*drop);
1728 got_object_idset_free(*skip);
1733 static const struct got_error *
1734 commit_painting_init(struct imsgbuf *ibuf, struct got_object_idset **keep,
1735 struct got_object_idset **drop, struct got_object_idset **skip)
1737 const struct got_error *err = NULL;
1739 *keep = got_object_idset_alloc();
1740 if (*keep == NULL) {
1741 err = got_error_from_errno("got_object_idset_alloc");
1744 *drop = got_object_idset_alloc();
1745 if (*drop == NULL) {
1746 err = got_error_from_errno("got_object_idset_alloc");
1749 *skip = got_object_idset_alloc();
1750 if (*skip == NULL) {
1751 err = got_error_from_errno("got_object_idset_alloc");
1755 err = recv_object_ids(*keep, ibuf);
1758 err = recv_object_ids(*drop, ibuf);
1761 err = recv_object_ids(*skip, ibuf);
1767 commit_painting_free(keep, drop, skip);
1772 static const struct got_error *
1773 commit_painting_request(struct imsg *imsg, struct imsgbuf *ibuf,
1774 struct got_pack *pack, struct got_packidx *packidx,
1775 struct got_object_cache *objcache, struct got_object_idset *keep,
1776 struct got_object_idset *drop, struct got_object_idset *skip)
1778 const struct got_error *err = NULL;
1779 struct got_imsg_commit_painting_request ireq;
1780 struct got_object_id id;
1782 struct got_object_id_queue ids;
1787 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1788 if (datalen != sizeof(ireq))
1789 return got_error(GOT_ERR_PRIVSEP_LEN);
1790 memcpy(&ireq, imsg->data, sizeof(ireq));
1791 memcpy(id.sha1, ireq.id, SHA1_DIGEST_LENGTH);
1793 err = queue_commit_id(&ids, &id, ireq.color);
1798 err = paint_commits(&ids, &nids, keep, drop, skip,
1799 pack, packidx, ibuf, objcache);
1803 err = got_privsep_send_painted_commits(ibuf, &ids, &nids, 0, 1);
1807 err = got_privsep_send_painting_commits_done(ibuf);
1809 got_object_id_queue_free(&ids);
1813 static const struct got_error *
1814 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1816 const struct got_error *err = NULL;
1818 struct got_imsg_pack ipack;
1820 struct got_pack *pack;
1824 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1828 pack = calloc(1, sizeof(*pack));
1830 err = got_error_from_errno("calloc");
1834 if (imsg.hdr.type != GOT_IMSG_PACK) {
1835 err = got_error(GOT_ERR_PRIVSEP_MSG);
1839 if (imsg.fd == -1) {
1840 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1844 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1845 if (datalen != sizeof(ipack)) {
1846 err = got_error(GOT_ERR_PRIVSEP_LEN);
1849 memcpy(&ipack, imsg.data, sizeof(ipack));
1851 pack->filesize = ipack.filesize;
1852 pack->fd = dup(imsg.fd);
1853 if (pack->fd == -1) {
1854 err = got_error_from_errno("dup");
1857 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1858 err = got_error_from_errno("lseek");
1861 pack->path_packfile = strdup(ipack.path_packfile);
1862 if (pack->path_packfile == NULL) {
1863 err = got_error_from_errno("strdup");
1867 err = got_delta_cache_alloc(&pack->delta_cache);
1871 #ifndef GOT_PACK_NO_MMAP
1872 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1874 if (pack->map == MAP_FAILED)
1875 pack->map = NULL; /* fall back to read(2) */
1889 main(int argc, char *argv[])
1891 const struct got_error *err = NULL;
1892 struct imsgbuf ibuf;
1894 struct got_packidx *packidx = NULL;
1895 struct got_pack *pack = NULL;
1896 struct got_object_cache objcache;
1897 FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1898 struct got_object_idset *keep = NULL, *drop = NULL, *skip = NULL;
1900 //static int attached;
1901 //while (!attached) sleep(1);
1903 signal(SIGINT, catch_sigint);
1905 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1907 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1909 err = got_error_from_errno("got_object_cache_init");
1910 got_privsep_send_error(&ibuf, err);
1915 /* revoke access to most system calls */
1916 if (pledge("stdio recvfd", NULL) == -1) {
1917 err = got_error_from_errno("pledge");
1918 got_privsep_send_error(&ibuf, err);
1923 err = receive_packidx(&packidx, &ibuf);
1925 got_privsep_send_error(&ibuf, err);
1929 err = receive_pack(&pack, &ibuf);
1931 got_privsep_send_error(&ibuf, err);
1938 if (sigint_received) {
1939 err = got_error(GOT_ERR_CANCELLED);
1943 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1945 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1950 if (imsg.hdr.type == GOT_IMSG_STOP)
1953 switch (imsg.hdr.type) {
1954 case GOT_IMSG_TMPFD:
1955 if (basefile == NULL) {
1956 err = receive_tempfile(&basefile, "w+",
1958 } else if (accumfile == NULL) {
1959 err = receive_tempfile(&accumfile, "w+",
1962 err = got_error(GOT_ERR_PRIVSEP_MSG);
1964 case GOT_IMSG_PACKED_OBJECT_REQUEST:
1965 err = object_request(&imsg, &ibuf, pack, packidx,
1968 case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1969 if (basefile == NULL || accumfile == NULL) {
1970 err = got_error(GOT_ERR_PRIVSEP_MSG);
1973 err = raw_object_request(&imsg, &ibuf, pack, packidx,
1974 &objcache, basefile, accumfile);
1976 case GOT_IMSG_RAW_DELTA_OUTFD:
1977 if (delta_outfile != NULL) {
1978 err = got_error(GOT_ERR_PRIVSEP_MSG);
1981 err = receive_tempfile(&delta_outfile, "w",
1984 case GOT_IMSG_RAW_DELTA_REQUEST:
1985 if (delta_outfile == NULL) {
1986 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1989 err = raw_delta_request(&imsg, &ibuf, delta_outfile,
1992 case GOT_IMSG_DELTA_REUSE_REQUEST:
1993 if (delta_outfile == NULL) {
1994 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1997 err = delta_reuse_request(&imsg, &ibuf,
1998 delta_outfile, pack, packidx);
2000 case GOT_IMSG_COMMIT_REQUEST:
2001 err = commit_request(&imsg, &ibuf, pack, packidx,
2004 case GOT_IMSG_TREE_REQUEST:
2005 err = tree_request(&imsg, &ibuf, pack, packidx,
2008 case GOT_IMSG_BLOB_REQUEST:
2009 if (basefile == NULL || accumfile == NULL) {
2010 err = got_error(GOT_ERR_PRIVSEP_MSG);
2013 err = blob_request(&imsg, &ibuf, pack, packidx,
2014 &objcache, basefile, accumfile);
2016 case GOT_IMSG_TAG_REQUEST:
2017 err = tag_request(&imsg, &ibuf, pack, packidx,
2020 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
2021 err = commit_traversal_request(&imsg, &ibuf, pack,
2022 packidx, &objcache);
2024 case GOT_IMSG_OBJECT_ENUMERATION_REQUEST:
2025 err = enumeration_request(&imsg, &ibuf, pack,
2026 packidx, &objcache);
2028 case GOT_IMSG_COMMIT_PAINTING_INIT:
2029 commit_painting_free(&keep, &drop, &skip);
2030 err = commit_painting_init(&ibuf, &keep, &drop, &skip);
2032 case GOT_IMSG_COMMIT_PAINTING_REQUEST:
2033 if (keep == NULL || drop == NULL || skip == NULL) {
2034 err = got_error(GOT_ERR_PRIVSEP_MSG);
2037 err = commit_painting_request(&imsg, &ibuf, pack,
2038 packidx, &objcache, keep, drop, skip);
2040 case GOT_IMSG_COMMIT_PAINTING_DONE:
2041 commit_painting_free(&keep, &drop, &skip);
2044 err = got_error(GOT_ERR_PRIVSEP_MSG);
2048 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
2049 err = got_error_from_errno("close");
2055 commit_painting_free(&keep, &drop, &skip);
2057 got_packidx_close(packidx);
2059 got_pack_close(pack);
2060 got_object_cache_close(&objcache);
2062 if (basefile && fclose(basefile) == EOF && err == NULL)
2063 err = got_error_from_errno("fclose");
2064 if (accumfile && fclose(accumfile) == EOF && err == NULL)
2065 err = got_error_from_errno("fclose");
2066 if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
2067 err = got_error_from_errno("fclose");
2069 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
2070 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
2071 got_privsep_send_error(&ibuf, err);
2074 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
2075 err = got_error_from_errno("close");