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>
36 #include "got_error.h"
37 #include "got_object.h"
40 #include "got_lib_delta.h"
41 #include "got_lib_delta_cache.h"
42 #include "got_lib_object.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, struct got_parsed_tree_entry **entries, size_t *nentries,
187 size_t *nentries_alloc, struct got_pack *pack, struct got_packidx *packidx,
188 int obj_idx, 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);
201 err = open_object(&obj, pack, packidx, obj_idx, id,
207 err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
213 err = got_object_parse_tree(entries, nentries, nentries_alloc,
216 got_object_close(obj);
224 static const struct got_error *
225 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
226 struct got_packidx *packidx, struct got_object_cache *objcache,
227 struct got_parsed_tree_entry **entries, size_t *nentries,
228 size_t *nentries_alloc)
230 const struct got_error *err = NULL;
231 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, entries, nentries, nentries_alloc,
243 pack, packidx, iobj.idx, &id, objcache);
247 err = got_privsep_send_tree(ibuf, *entries, *nentries);
250 if (err->code == GOT_ERR_PRIVSEP_PIPE)
253 got_privsep_send_error(ibuf, err);
259 static const struct got_error *
260 receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
262 const struct got_error *err;
266 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
270 if (imsg.hdr.type != imsg_code) {
271 err = got_error(GOT_ERR_PRIVSEP_MSG);
275 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
277 err = got_error(GOT_ERR_PRIVSEP_LEN);
281 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
285 *f = fdopen(imsg.fd, "w+");
287 err = got_error_from_errno("fdopen");
296 static const struct got_error *
297 receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
298 struct imsgbuf *ibuf)
302 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
304 return got_error(GOT_ERR_PRIVSEP_LEN);
307 return got_error(GOT_ERR_PRIVSEP_NO_FD);
309 *f = fdopen(imsg->fd, mode);
311 return got_error_from_errno("fdopen");
317 static const struct got_error *
318 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
319 struct got_packidx *packidx, struct got_object_cache *objcache,
320 FILE *basefile, FILE *accumfile)
322 const struct got_error *err = NULL;
323 struct got_imsg_packed_object iobj;
324 struct got_object *obj = NULL;
325 FILE *outfile = NULL;
326 struct got_object_id id;
331 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
332 if (datalen != sizeof(iobj))
333 return got_error(GOT_ERR_PRIVSEP_LEN);
334 memcpy(&iobj, imsg->data, sizeof(iobj));
335 memcpy(&id, &iobj.id, sizeof(id));
337 obj = got_object_cache_get(objcache, &id);
341 err = open_object(&obj, pack, packidx, iobj.idx, &id,
347 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
351 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
352 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
356 blob_size = obj->size;
358 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
359 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
362 err = got_packfile_extract_object(pack, obj, outfile, basefile,
367 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
370 if (outfile && fclose(outfile) == EOF && err == NULL)
371 err = got_error_from_errno("fclose");
372 got_object_close(obj);
373 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
374 got_privsep_send_error(ibuf, err);
379 static const struct got_error *
380 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
381 struct got_packidx *packidx, struct got_object_cache *objcache)
383 const struct got_error *err = NULL;
384 struct got_imsg_packed_object iobj;
385 struct got_object *obj = NULL;
386 struct got_tag_object *tag = NULL;
389 struct got_object_id id;
392 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
393 if (datalen != sizeof(iobj))
394 return got_error(GOT_ERR_PRIVSEP_LEN);
395 memcpy(&iobj, imsg->data, sizeof(iobj));
396 memcpy(&id, &iobj.id, sizeof(id));
398 obj = got_object_cache_get(objcache, &id);
402 err = open_object(&obj, pack, packidx, iobj.idx, &id,
408 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
413 err = got_object_parse_tag(&tag, buf, len);
417 err = got_privsep_send_tag(ibuf, tag);
420 got_object_close(obj);
422 got_object_tag_close(tag);
424 if (err->code == GOT_ERR_PRIVSEP_PIPE)
427 got_privsep_send_error(ibuf, err);
433 static struct got_parsed_tree_entry *
434 find_entry_by_name(struct got_parsed_tree_entry *entries, int nentries,
435 const char *name, size_t len)
437 struct got_parsed_tree_entry *pte;
440 /* Note that tree entries are sorted in strncmp() order. */
441 for (i = 0; i < nentries; i++) {
443 cmp = strncmp(pte->name, name, len);
448 if (pte->name[len] == '\0')
454 static const struct got_error *
455 tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
456 struct got_parsed_tree_entry **entries1, size_t *nentries1,
457 size_t *nentries_alloc1,
458 struct got_parsed_tree_entry **entries2, size_t *nentries2,
459 size_t *nentries_alloc2,
460 const char *path, struct got_pack *pack, struct got_packidx *packidx,
461 struct imsgbuf *ibuf, struct got_object_cache *objcache)
463 const struct got_error *err = NULL;
464 struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
470 /* We not do support comparing the root path. */
471 if (got_path_is_root_dir(path))
472 return got_error_path(path, GOT_ERR_BAD_PATH);
487 pte1 = find_entry_by_name(*entries1, *nentries1, seg, seglen);
489 err = got_error(GOT_ERR_NO_OBJ);
493 pte2 = find_entry_by_name(*entries2, *nentries2, seg, seglen);
499 if (pte1->mode != pte2->mode) {
504 if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
509 if (*s == '\0') { /* final path element */
518 struct got_object_id id1, id2;
521 memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
522 idx = got_packidx_get_object_idx(packidx, &id1);
524 err = got_error_no_obj(&id1);
530 err = open_tree(buf1, entries1, nentries1,
531 nentries_alloc1, pack, packidx, idx, &id1,
537 memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
538 idx = got_packidx_get_object_idx(packidx, &id2);
540 err = got_error_no_obj(&id2);
546 err = open_tree(buf2, entries2, nentries2,
547 nentries_alloc2, pack, packidx, idx, &id2, objcache);
557 static const struct got_error *
558 send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
559 struct imsgbuf *ibuf)
564 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
565 sizeof(struct got_imsg_traversed_commits) +
566 ncommits * SHA1_DIGEST_LENGTH);
568 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
570 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
571 return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
573 for (i = 0; i < ncommits; i++) {
574 struct got_object_id *id = &commit_ids[i];
575 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
576 return got_error_from_errno(
577 "imsg_add TRAVERSED_COMMITS");
582 imsg_close(ibuf, wbuf);
584 return got_privsep_flush_imsg(ibuf);
587 static const struct got_error *
588 send_commit_traversal_done(struct imsgbuf *ibuf)
590 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
592 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
594 return got_privsep_flush_imsg(ibuf);
597 static const struct got_error *
598 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
599 struct got_pack *pack, struct got_packidx *packidx,
600 struct got_object_cache *objcache)
602 const struct got_error *err = NULL;
603 struct got_imsg_packed_object iobj;
604 struct got_object_qid *pid;
605 struct got_commit_object *commit = NULL, *pcommit = NULL;
606 struct got_parsed_tree_entry *entries = NULL, *pentries = NULL;
607 size_t nentries = 0, nentries_alloc = 0;
608 size_t pnentries = 0, pnentries_alloc = 0;
609 struct got_object_id id;
610 size_t datalen, path_len;
612 const int min_alloc = 64;
613 int changed = 0, ncommits = 0, nallocated = 0;
614 struct got_object_id *commit_ids = NULL;
616 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
617 if (datalen < sizeof(iobj))
618 return got_error(GOT_ERR_PRIVSEP_LEN);
619 memcpy(&iobj, imsg->data, sizeof(iobj));
620 memcpy(&id, &iobj.id, sizeof(id));
622 path_len = datalen - sizeof(iobj) - 1;
624 return got_error(GOT_ERR_PRIVSEP_LEN);
626 path = imsg->data + sizeof(iobj);
627 if (path[path_len] != '\0')
628 return got_error(GOT_ERR_PRIVSEP_LEN);
631 nallocated = min_alloc;
632 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
633 if (commit_ids == NULL)
634 return got_error_from_errno("reallocarray");
637 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
640 if (sigint_received) {
641 err = got_error(GOT_ERR_CANCELLED);
645 if (commit == NULL) {
646 idx = got_packidx_get_object_idx(packidx, &id);
649 err = open_commit(&commit, pack, packidx,
652 if (err->code != GOT_ERR_NO_OBJ)
659 if (sizeof(struct got_imsg_traversed_commits) +
660 ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
661 err = send_traversed_commits(commit_ids, ncommits,
668 if (ncommits > nallocated) {
669 struct got_object_id *new;
670 nallocated += min_alloc;
671 new = reallocarray(commit_ids, nallocated,
672 sizeof(*commit_ids));
674 err = got_error_from_errno("reallocarray");
679 memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
682 pid = STAILQ_FIRST(&commit->parent_ids);
686 idx = got_packidx_get_object_idx(packidx, &pid->id);
690 err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
693 if (err->code != GOT_ERR_NO_OBJ)
699 if (path[0] == '/' && path[1] == '\0') {
700 if (got_object_id_cmp(pcommit->tree_id,
701 commit->tree_id) != 0) {
707 uint8_t *buf = NULL, *pbuf = NULL;
709 idx = got_packidx_get_object_idx(packidx,
713 pidx = got_packidx_get_object_idx(packidx,
718 err = open_tree(&buf, &entries, &nentries,
719 &nentries_alloc, pack, packidx, idx,
720 commit->tree_id, objcache);
723 err = open_tree(&pbuf, &pentries, &pnentries,
724 &pnentries_alloc, pack, packidx, pidx,
725 pcommit->tree_id, objcache);
731 err = tree_path_changed(&changed, &buf, &pbuf,
732 &entries, &nentries, &nentries_alloc,
733 &pentries, &pnentries, &pnentries_alloc,
734 path, pack, packidx, ibuf, objcache);
741 if (err->code != GOT_ERR_NO_OBJ)
749 memcpy(id.sha1, pid->id.sha1, SHA1_DIGEST_LENGTH);
750 got_object_commit_close(commit);
757 err = send_traversed_commits(commit_ids, ncommits, ibuf);
762 err = got_privsep_send_commit(ibuf, commit);
767 err = send_commit_traversal_done(ibuf);
771 got_object_commit_close(commit);
773 got_object_commit_close(pcommit);
777 if (err->code == GOT_ERR_PRIVSEP_PIPE)
780 got_privsep_send_error(ibuf, err);
786 static const struct got_error *
787 raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
788 struct got_pack *pack, struct got_packidx *packidx,
789 struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
791 const struct got_error *err = NULL;
794 FILE *outfile = NULL;
795 struct got_imsg_packed_object iobj;
796 struct got_object *obj;
797 struct got_object_id id;
800 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
801 if (datalen != sizeof(iobj))
802 return got_error(GOT_ERR_PRIVSEP_LEN);
803 memcpy(&iobj, imsg->data, sizeof(iobj));
804 memcpy(&id, &iobj.id, sizeof(id));
806 obj = got_object_cache_get(objcache, &id);
810 err = open_object(&obj, pack, packidx, iobj.idx, &id,
816 err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
820 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
821 err = got_pack_get_max_delta_object_size(&size, obj, pack);
827 if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
828 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
831 err = got_packfile_extract_object(pack, obj, outfile, basefile,
836 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
839 if (outfile && fclose(outfile) == EOF && err == NULL)
840 err = got_error_from_errno("fclose");
841 got_object_close(obj);
842 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
843 got_privsep_send_error(ibuf, err);
848 static const struct got_error *
849 get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
852 const struct got_error *err;
855 err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
859 return got_error(GOT_ERR_BAD_PACKIDX);
861 return got_packidx_get_object_id(base_id, packidx, idx);
864 static const struct got_error *
865 raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
866 FILE *delta_outfile, struct got_pack *pack,
867 struct got_packidx *packidx)
869 const struct got_error *err = NULL;
870 struct got_imsg_raw_delta_request req;
871 size_t datalen, delta_size, delta_compressed_size;
872 off_t delta_offset, delta_data_offset;
873 uint8_t *delta_buf = NULL;
874 struct got_object_id id, base_id;
875 off_t base_offset, delta_out_offset = 0;
876 uint64_t base_size = 0, result_size = 0;
879 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
880 if (datalen != sizeof(req))
881 return got_error(GOT_ERR_PRIVSEP_LEN);
882 memcpy(&req, imsg->data, sizeof(req));
883 memcpy(&id, &req.id, sizeof(id));
887 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
888 &delta_compressed_size, &delta_offset, &delta_data_offset,
889 &base_offset, &base_id, &base_size, &result_size,
890 pack, packidx, req.idx);
895 * If this is an offset delta we must determine the base
896 * object ID ourselves.
898 if (base_offset != 0) {
899 err = get_base_object_id(&base_id, packidx, base_offset);
904 delta_out_offset = ftello(delta_outfile);
905 w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
906 if (w != delta_compressed_size) {
907 err = got_ferror(delta_outfile, GOT_ERR_IO);
910 if (fflush(delta_outfile) == -1) {
911 err = got_error_from_errno("fflush");
915 err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
916 delta_size, delta_compressed_size, delta_offset, delta_out_offset,
923 struct search_deltas_arg {
924 struct imsgbuf *ibuf;
925 struct got_packidx *packidx;
926 struct got_pack *pack;
927 struct got_object_idset *idset;
928 struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
932 static const struct got_error *
933 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
935 const struct got_error *err;
936 struct search_deltas_arg *a = arg;
938 uint8_t *delta_buf = NULL;
939 uint64_t base_size, result_size;
940 size_t delta_size, delta_compressed_size;
941 off_t delta_offset, delta_data_offset, base_offset;
942 struct got_object_id base_id;
945 return got_error(GOT_ERR_CANCELLED);
947 obj_idx = got_packidx_get_object_idx(a->packidx, id);
949 return NULL; /* object not present in our pack file */
951 err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
952 &delta_compressed_size, &delta_offset, &delta_data_offset,
953 &base_offset, &base_id, &base_size, &result_size,
954 a->pack, a->packidx, obj_idx);
956 if (err->code == GOT_ERR_OBJ_TYPE)
957 return NULL; /* object not stored as a delta */
962 * If this is an offset delta we must determine the base
963 * object ID ourselves.
965 if (base_offset != 0) {
966 err = get_base_object_id(&base_id, a->packidx, base_offset);
971 if (got_object_idset_contains(a->idset, &base_id)) {
972 struct got_imsg_reused_delta *delta;
974 delta = &a->deltas[a->ndeltas++];
975 memcpy(&delta->id, id, sizeof(delta->id));
976 memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
977 delta->base_size = base_size;
978 delta->result_size = result_size;
979 delta->delta_size = delta_size;
980 delta->delta_compressed_size = delta_compressed_size;
981 delta->delta_offset = delta_data_offset;
983 if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
984 err = got_privsep_send_reused_deltas(a->ibuf,
985 a->deltas, a->ndeltas);
996 static const struct got_error *
997 recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
999 const struct got_error *err = NULL;
1001 struct got_object_id *ids;
1005 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1008 for (i = 0; i < nids; i++) {
1009 err = got_object_idset_add(idset, &ids[i], NULL);
1021 static const struct got_error *
1022 recv_object_id_queue(struct got_object_id_queue *queue,
1023 struct got_object_idset *queued_ids, struct imsgbuf *ibuf)
1025 const struct got_error *err = NULL;
1027 struct got_object_qid *qid;
1028 struct got_object_id *ids;
1032 err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1035 for (i = 0; i < nids; i++) {
1036 err = got_object_qid_alloc_partial(&qid);
1039 memcpy(&qid->id, &ids[i], sizeof(qid->id));
1040 STAILQ_INSERT_TAIL(queue, qid, entry);
1041 err = got_object_idset_add(queued_ids, &qid->id, NULL);
1050 static const struct got_error *
1051 delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1052 struct got_pack *pack, struct got_packidx *packidx)
1054 const struct got_error *err = NULL;
1055 struct got_object_idset *idset;
1056 struct search_deltas_arg sda;
1058 idset = got_object_idset_alloc();
1060 return got_error_from_errno("got_object_idset_alloc");
1062 err = recv_object_ids(idset, ibuf);
1066 memset(&sda, 0, sizeof(sda));
1070 sda.packidx = packidx;
1071 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1075 if (sda.ndeltas > 0) {
1076 err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1082 err = got_privsep_send_reused_deltas_done(ibuf);
1084 got_object_idset_free(idset);
1088 static const struct got_error *
1089 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1091 const struct got_error *err = NULL;
1093 struct got_imsg_packidx ipackidx;
1095 struct got_packidx *p;
1099 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1103 p = calloc(1, sizeof(*p));
1105 err = got_error_from_errno("calloc");
1109 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1110 err = got_error(GOT_ERR_PRIVSEP_MSG);
1114 if (imsg.fd == -1) {
1115 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1119 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1120 if (datalen != sizeof(ipackidx)) {
1121 err = got_error(GOT_ERR_PRIVSEP_LEN);
1124 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1126 p->len = ipackidx.len;
1127 p->fd = dup(imsg.fd);
1129 err = got_error_from_errno("dup");
1132 if (lseek(p->fd, 0, SEEK_SET) == -1) {
1133 err = got_error_from_errno("lseek");
1137 #ifndef GOT_PACK_NO_MMAP
1138 if (p->len > 0 && p->len <= SIZE_MAX) {
1139 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1140 if (p->map == MAP_FAILED)
1141 p->map = NULL; /* fall back to read(2) */
1144 err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1149 got_packidx_close(p);
1156 static const struct got_error *
1157 send_tree_enumeration_done(struct imsgbuf *ibuf)
1159 if (imsg_compose(ibuf, GOT_IMSG_TREE_ENUMERATION_DONE, 0, 0, -1,
1161 return got_error_from_errno("imsg_compose TREE_ENUMERATION_DONE");
1163 return got_privsep_flush_imsg(ibuf);
1166 struct enumerated_tree {
1167 struct got_object_id id;
1170 struct got_parsed_tree_entry *entries;
1174 static const struct got_error *
1175 enumerate_tree(int *have_all_entries, struct imsgbuf *ibuf, size_t *totlen,
1176 struct got_object_id *tree_id,
1177 const char *path, struct got_pack *pack, struct got_packidx *packidx,
1178 struct got_object_cache *objcache, struct got_object_idset *idset,
1179 struct enumerated_tree **trees, size_t *nalloc, size_t *ntrees)
1181 const struct got_error *err = NULL;
1182 struct got_object_id_queue ids;
1183 struct got_object_qid *qid;
1184 uint8_t *buf = NULL;
1185 struct got_parsed_tree_entry *entries = NULL;
1186 size_t nentries = 0, nentries_alloc = 0, i;
1187 struct enumerated_tree *tree;
1190 *have_all_entries = 1;
1193 err = got_object_qid_alloc_partial(&qid);
1196 memcpy(&qid->id.sha1, tree_id, SHA1_DIGEST_LENGTH);
1197 qid->data = strdup(path);
1198 if (qid->data == NULL) {
1199 err = got_error_from_errno("strdup");
1202 STAILQ_INSERT_TAIL(&ids, qid, entry);
1205 /* Traverse the tree hierarchy, gather tree object IDs and paths. */
1210 if (sigint_received) {
1211 err = got_error(GOT_ERR_CANCELLED);
1215 qid = STAILQ_FIRST(&ids);
1216 STAILQ_REMOVE_HEAD(&ids, entry);
1219 idx = got_packidx_get_object_idx(packidx, &qid->id);
1221 *have_all_entries = 0;
1225 err = open_tree(&buf, &entries, &nentries, &nentries_alloc,
1226 pack, packidx, idx, &qid->id, objcache);
1228 if (err->code != GOT_ERR_NO_OBJ)
1232 err = got_object_idset_add(idset, &qid->id, NULL);
1236 for (i = 0; i < nentries; i++) {
1237 struct got_object_qid *eqid = NULL;
1238 struct got_parsed_tree_entry *pte = &entries[i];
1241 if (!S_ISDIR(pte->mode))
1244 err = got_object_qid_alloc_partial(&eqid);
1247 memcpy(eqid->id.sha1, pte->id, sizeof(eqid->id.sha1));
1249 if (got_object_idset_contains(idset, &eqid->id)) {
1250 got_object_qid_free(eqid);
1254 if (asprintf(&p, "%s%s%s", path,
1255 got_path_is_root_dir(path) ? "" : "/",
1257 err = got_error_from_errno("asprintf");
1258 got_object_qid_free(eqid);
1262 STAILQ_INSERT_TAIL(&ids, eqid, entry);
1265 if (*ntrees >= *nalloc) {
1266 struct enumerated_tree *new;
1267 new = recallocarray(*trees, *nalloc, *nalloc + 16,
1270 err = got_error_from_errno("malloc");
1276 tree = &(*trees)[*ntrees];
1278 memcpy(&tree->id, &qid->id, sizeof(tree->id));
1279 tree->path = qid->data;
1282 tree->entries = entries;
1285 tree->nentries = nentries;
1288 got_object_qid_free(qid);
1290 } while (!STAILQ_EMPTY(&ids));
1292 if (*have_all_entries) {
1295 * We have managed to traverse all entries in the hierarchy.
1296 * Tell the main process what we have found.
1298 for (i = 0; i < *ntrees; i++) {
1299 tree = &(*trees)[i];
1300 err = got_privsep_send_enumerated_tree(totlen,
1301 ibuf, &tree->id, tree->path, tree->entries,
1309 free(tree->entries);
1310 tree->entries = NULL;
1312 *ntrees = 0; /* don't loop again below to free memory */
1314 err = send_tree_enumeration_done(ibuf);
1317 * We can only load fully packed tree hierarchies on
1318 * behalf of the main process, otherwise the main process
1319 * gets a wrong idea about which tree objects have
1320 * already been traversed.
1321 * Indicate a missing entry for the root of this tree.
1322 * The main process should continue by loading this
1323 * entire tree the slow way.
1325 err = got_privsep_send_enumerated_tree(totlen, ibuf,
1326 tree_id, "/", NULL, -1);
1333 for (i = 0; i < *ntrees; i++) {
1334 tree = &(*trees)[i];
1339 free(tree->entries);
1340 tree->entries = NULL;
1344 got_object_qid_free(qid);
1345 got_object_id_queue_free(&ids);
1347 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1350 got_privsep_send_error(ibuf, err);
1356 static const struct got_error *
1357 enumeration_request(struct imsg *imsg, struct imsgbuf *ibuf,
1358 struct got_pack *pack, struct got_packidx *packidx,
1359 struct got_object_cache *objcache)
1361 const struct got_error *err = NULL;
1362 struct got_object_id_queue commit_ids;
1363 const struct got_object_id_queue *parents = NULL;
1364 struct got_object_qid *qid = NULL;
1365 struct got_object *obj = NULL;
1366 struct got_commit_object *commit = NULL;
1367 struct got_object_id *tree_id = NULL;
1369 struct got_object_idset *idset, *queued_ids = NULL;
1370 int i, idx, have_all_entries = 1;
1371 struct enumerated_tree *trees = NULL;
1372 size_t ntrees = 0, nalloc = 16;
1374 STAILQ_INIT(&commit_ids);
1376 trees = calloc(nalloc, sizeof(*trees));
1378 return got_error_from_errno("calloc");
1380 idset = got_object_idset_alloc();
1381 if (idset == NULL) {
1382 err = got_error_from_errno("got_object_idset_alloc");
1386 queued_ids = got_object_idset_alloc();
1387 if (queued_ids == NULL) {
1388 err = got_error_from_errno("got_object_idset_alloc");
1392 err = recv_object_id_queue(&commit_ids, queued_ids, ibuf);
1396 if (STAILQ_EMPTY(&commit_ids)) {
1397 err = got_error(GOT_ERR_PRIVSEP_MSG);
1401 err = recv_object_ids(idset, ibuf);
1405 while (!STAILQ_EMPTY(&commit_ids)) {
1406 if (sigint_received) {
1407 err = got_error(GOT_ERR_CANCELLED);
1411 qid = STAILQ_FIRST(&commit_ids);
1412 STAILQ_REMOVE_HEAD(&commit_ids, entry);
1414 if (got_object_idset_contains(idset, &qid->id)) {
1415 got_object_qid_free(qid);
1420 idx = got_packidx_get_object_idx(packidx, &qid->id);
1422 have_all_entries = 0;
1426 err = open_object(&obj, pack, packidx, idx, &qid->id,
1430 if (obj->type == GOT_OBJ_TYPE_TAG) {
1431 struct got_tag_object *tag;
1434 err = got_packfile_extract_object_to_mem(&buf,
1439 err = got_object_parse_tag(&tag, buf, len);
1444 idx = got_packidx_get_object_idx(packidx, &tag->id);
1446 have_all_entries = 0;
1449 err = open_commit(&commit, pack, packidx, idx,
1450 &tag->id, objcache);
1451 got_object_tag_close(tag);
1455 } else if (obj->type == GOT_OBJ_TYPE_COMMIT) {
1456 err = open_commit(&commit, pack, packidx, idx,
1457 &qid->id, objcache);
1461 err = got_error(GOT_ERR_OBJ_TYPE);
1464 got_object_close(obj);
1467 err = got_privsep_send_enumerated_commit(ibuf, &qid->id,
1468 got_object_commit_get_committer_time(commit));
1472 tree_id = got_object_commit_get_tree_id(commit);
1473 idx = got_packidx_get_object_idx(packidx, tree_id);
1475 have_all_entries = 0;
1476 err = got_privsep_send_enumerated_tree(&totlen, ibuf,
1477 tree_id, "/", NULL, -1);
1483 if (got_object_idset_contains(idset, tree_id)) {
1484 got_object_qid_free(qid);
1486 err = send_tree_enumeration_done(ibuf);
1492 err = enumerate_tree(&have_all_entries, ibuf, &totlen,
1493 tree_id, "/", pack, packidx, objcache, idset,
1494 &trees, &nalloc, &ntrees);
1498 if (!have_all_entries)
1501 got_object_qid_free(qid);
1504 parents = got_object_commit_get_parent_ids(commit);
1506 struct got_object_qid *pid;
1507 STAILQ_FOREACH(pid, parents, entry) {
1508 if (got_object_idset_contains(idset, &pid->id))
1510 if (got_object_idset_contains(queued_ids, &pid->id))
1512 err = got_object_qid_alloc_partial(&qid);
1515 memcpy(&qid->id, &pid->id, sizeof(qid->id));
1516 STAILQ_INSERT_TAIL(&commit_ids, qid, entry);
1521 got_object_commit_close(commit);
1525 if (have_all_entries) {
1526 err = got_privsep_send_object_enumeration_done(ibuf);
1530 err = got_privsep_send_object_enumeration_incomplete(ibuf);
1536 got_object_close(obj);
1538 got_object_commit_close(commit);
1539 got_object_qid_free(qid);
1540 got_object_id_queue_free(&commit_ids);
1542 got_object_idset_free(idset);
1544 got_object_idset_free(queued_ids);
1545 for (i = 0; i < ntrees; i++) {
1546 struct enumerated_tree *tree = &trees[i];
1549 free(tree->entries);
1555 enum findtwixt_color {
1562 static const struct got_error *
1563 paint_commit(struct got_object_qid *qid, intptr_t color)
1565 if (color < 0 || color >= COLOR_MAX)
1566 return got_error(GOT_ERR_RANGE);
1568 qid->data = (void *)color;
1572 static const struct got_error *
1573 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1576 const struct got_error *err;
1577 struct got_object_qid *qid;
1579 err = got_object_qid_alloc_partial(&qid);
1583 memcpy(&qid->id, id, sizeof(qid->id));
1584 STAILQ_INSERT_TAIL(ids, qid, entry);
1585 return paint_commit(qid, color);
1588 static const struct got_error *
1589 paint_commits(struct got_object_id_queue *ids, int *nids,
1590 struct got_object_idset *keep, struct got_object_idset *drop,
1591 struct got_object_idset *skip, struct got_pack *pack,
1592 struct got_packidx *packidx, struct imsgbuf *ibuf,
1593 struct got_object_cache *objcache)
1595 const struct got_error *err = NULL;
1596 struct got_commit_object *commit = NULL;
1597 struct got_object_id_queue painted;
1598 const struct got_object_id_queue *parents;
1599 struct got_object_qid *qid = NULL;
1600 int nqueued = *nids, nskip = 0, npainted = 0;
1602 STAILQ_INIT(&painted);
1604 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1608 if (sigint_received) {
1609 err = got_error(GOT_ERR_CANCELLED);
1613 qid = STAILQ_FIRST(ids);
1614 idx = got_packidx_get_object_idx(packidx, &qid->id);
1620 STAILQ_REMOVE_HEAD(ids, entry);
1622 color = (intptr_t)qid->data;
1623 if (color == COLOR_SKIP)
1626 if (got_object_idset_contains(skip, &qid->id)) {
1627 got_object_qid_free(qid);
1634 if (got_object_idset_contains(keep, &qid->id)) {
1635 got_object_qid_free(qid);
1639 if (got_object_idset_contains(drop, &qid->id)) {
1640 err = paint_commit(qid, COLOR_SKIP);
1644 err = got_object_idset_add(keep, &qid->id, NULL);
1649 if (got_object_idset_contains(drop, &qid->id)) {
1650 got_object_qid_free(qid);
1654 if (got_object_idset_contains(keep, &qid->id)) {
1655 err = paint_commit(qid, COLOR_SKIP);
1659 err = got_object_idset_add(drop, &qid->id, NULL);
1664 if (!got_object_idset_contains(skip, &qid->id)) {
1665 err = got_object_idset_add(skip, &qid->id,
1672 /* should not happen */
1673 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1674 "%s invalid commit color %"PRIdPTR, __func__,
1679 err = open_commit(&commit, pack, packidx, idx, &qid->id,
1684 parents = got_object_commit_get_parent_ids(commit);
1686 struct got_object_qid *pid;
1687 color = (intptr_t)qid->data;
1688 STAILQ_FOREACH(pid, parents, entry) {
1689 err = queue_commit_id(ids, &pid->id, color);
1693 if (color == COLOR_SKIP)
1698 got_object_commit_close(commit);
1701 STAILQ_INSERT_TAIL(&painted, qid, entry);
1705 err = got_privsep_send_painted_commits(ibuf, &painted,
1711 err = got_privsep_send_painted_commits(ibuf, &painted, &npainted, 1, 1);
1718 got_object_commit_close(commit);
1719 got_object_qid_free(qid);
1724 commit_painting_free(struct got_object_idset **keep,
1725 struct got_object_idset **drop,
1726 struct got_object_idset **skip)
1729 got_object_idset_free(*keep);
1733 got_object_idset_free(*drop);
1737 got_object_idset_free(*skip);
1742 static const struct got_error *
1743 commit_painting_init(struct imsgbuf *ibuf, struct got_object_idset **keep,
1744 struct got_object_idset **drop, struct got_object_idset **skip)
1746 const struct got_error *err = NULL;
1748 *keep = got_object_idset_alloc();
1749 if (*keep == NULL) {
1750 err = got_error_from_errno("got_object_idset_alloc");
1753 *drop = got_object_idset_alloc();
1754 if (*drop == NULL) {
1755 err = got_error_from_errno("got_object_idset_alloc");
1758 *skip = got_object_idset_alloc();
1759 if (*skip == NULL) {
1760 err = got_error_from_errno("got_object_idset_alloc");
1764 err = recv_object_ids(*keep, ibuf);
1767 err = recv_object_ids(*drop, ibuf);
1770 err = recv_object_ids(*skip, ibuf);
1776 commit_painting_free(keep, drop, skip);
1781 static const struct got_error *
1782 commit_painting_request(struct imsg *imsg, struct imsgbuf *ibuf,
1783 struct got_pack *pack, struct got_packidx *packidx,
1784 struct got_object_cache *objcache, struct got_object_idset *keep,
1785 struct got_object_idset *drop, struct got_object_idset *skip)
1787 const struct got_error *err = NULL;
1788 struct got_imsg_commit_painting_request ireq;
1789 struct got_object_id id;
1791 struct got_object_id_queue ids;
1796 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1797 if (datalen != sizeof(ireq))
1798 return got_error(GOT_ERR_PRIVSEP_LEN);
1799 memcpy(&ireq, imsg->data, sizeof(ireq));
1800 memcpy(id.sha1, ireq.id, SHA1_DIGEST_LENGTH);
1802 err = queue_commit_id(&ids, &id, ireq.color);
1807 err = paint_commits(&ids, &nids, keep, drop, skip,
1808 pack, packidx, ibuf, objcache);
1812 err = got_privsep_send_painted_commits(ibuf, &ids, &nids, 0, 1);
1816 err = got_privsep_send_painting_commits_done(ibuf);
1818 got_object_id_queue_free(&ids);
1822 static const struct got_error *
1823 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1825 const struct got_error *err = NULL;
1827 struct got_imsg_pack ipack;
1829 struct got_pack *pack;
1833 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1837 pack = calloc(1, sizeof(*pack));
1839 err = got_error_from_errno("calloc");
1843 if (imsg.hdr.type != GOT_IMSG_PACK) {
1844 err = got_error(GOT_ERR_PRIVSEP_MSG);
1848 if (imsg.fd == -1) {
1849 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1853 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1854 if (datalen != sizeof(ipack)) {
1855 err = got_error(GOT_ERR_PRIVSEP_LEN);
1858 memcpy(&ipack, imsg.data, sizeof(ipack));
1860 pack->filesize = ipack.filesize;
1861 pack->fd = dup(imsg.fd);
1862 if (pack->fd == -1) {
1863 err = got_error_from_errno("dup");
1866 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1867 err = got_error_from_errno("lseek");
1870 pack->path_packfile = strdup(ipack.path_packfile);
1871 if (pack->path_packfile == NULL) {
1872 err = got_error_from_errno("strdup");
1876 err = got_delta_cache_alloc(&pack->delta_cache);
1880 #ifndef GOT_PACK_NO_MMAP
1881 if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1882 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1884 if (pack->map == MAP_FAILED)
1885 pack->map = NULL; /* fall back to read(2) */
1900 main(int argc, char *argv[])
1902 const struct got_error *err = NULL;
1903 struct imsgbuf ibuf;
1905 struct got_packidx *packidx = NULL;
1906 struct got_pack *pack = NULL;
1907 struct got_object_cache objcache;
1908 FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1909 struct got_object_idset *keep = NULL, *drop = NULL, *skip = NULL;
1910 struct got_parsed_tree_entry *entries = NULL;
1911 size_t nentries = 0, nentries_alloc = 0;
1913 //static int attached;
1914 //while (!attached) sleep(1);
1916 signal(SIGINT, catch_sigint);
1918 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1920 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1922 err = got_error_from_errno("got_object_cache_init");
1923 got_privsep_send_error(&ibuf, err);
1928 /* revoke access to most system calls */
1929 if (pledge("stdio recvfd", NULL) == -1) {
1930 err = got_error_from_errno("pledge");
1931 got_privsep_send_error(&ibuf, err);
1936 err = receive_packidx(&packidx, &ibuf);
1938 got_privsep_send_error(&ibuf, err);
1942 err = receive_pack(&pack, &ibuf);
1944 got_privsep_send_error(&ibuf, err);
1951 if (sigint_received) {
1952 err = got_error(GOT_ERR_CANCELLED);
1956 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1958 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1963 if (imsg.hdr.type == GOT_IMSG_STOP)
1966 switch (imsg.hdr.type) {
1967 case GOT_IMSG_TMPFD:
1968 if (basefile == NULL) {
1969 err = receive_tempfile(&basefile, "w+",
1971 } else if (accumfile == NULL) {
1972 err = receive_tempfile(&accumfile, "w+",
1975 err = got_error(GOT_ERR_PRIVSEP_MSG);
1977 case GOT_IMSG_PACKED_OBJECT_REQUEST:
1978 err = object_request(&imsg, &ibuf, pack, packidx,
1981 case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1982 if (basefile == NULL || accumfile == NULL) {
1983 err = got_error(GOT_ERR_PRIVSEP_MSG);
1986 err = raw_object_request(&imsg, &ibuf, pack, packidx,
1987 &objcache, basefile, accumfile);
1989 case GOT_IMSG_RAW_DELTA_OUTFD:
1990 if (delta_outfile != NULL) {
1991 err = got_error(GOT_ERR_PRIVSEP_MSG);
1994 err = receive_tempfile(&delta_outfile, "w",
1997 case GOT_IMSG_RAW_DELTA_REQUEST:
1998 if (delta_outfile == NULL) {
1999 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
2002 err = raw_delta_request(&imsg, &ibuf, delta_outfile,
2005 case GOT_IMSG_DELTA_REUSE_REQUEST:
2006 err = delta_reuse_request(&imsg, &ibuf, pack, packidx);
2008 case GOT_IMSG_COMMIT_REQUEST:
2009 err = commit_request(&imsg, &ibuf, pack, packidx,
2012 case GOT_IMSG_TREE_REQUEST:
2013 err = tree_request(&imsg, &ibuf, pack, packidx,
2014 &objcache, &entries, &nentries, &nentries_alloc);
2016 case GOT_IMSG_BLOB_REQUEST:
2017 if (basefile == NULL || accumfile == NULL) {
2018 err = got_error(GOT_ERR_PRIVSEP_MSG);
2021 err = blob_request(&imsg, &ibuf, pack, packidx,
2022 &objcache, basefile, accumfile);
2024 case GOT_IMSG_TAG_REQUEST:
2025 err = tag_request(&imsg, &ibuf, pack, packidx,
2028 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
2029 err = commit_traversal_request(&imsg, &ibuf, pack,
2030 packidx, &objcache);
2032 case GOT_IMSG_OBJECT_ENUMERATION_REQUEST:
2033 err = enumeration_request(&imsg, &ibuf, pack,
2034 packidx, &objcache);
2036 case GOT_IMSG_COMMIT_PAINTING_INIT:
2037 commit_painting_free(&keep, &drop, &skip);
2038 err = commit_painting_init(&ibuf, &keep, &drop, &skip);
2040 case GOT_IMSG_COMMIT_PAINTING_REQUEST:
2041 if (keep == NULL || drop == NULL || skip == NULL) {
2042 err = got_error(GOT_ERR_PRIVSEP_MSG);
2045 err = commit_painting_request(&imsg, &ibuf, pack,
2046 packidx, &objcache, keep, drop, skip);
2048 case GOT_IMSG_COMMIT_PAINTING_DONE:
2049 commit_painting_free(&keep, &drop, &skip);
2052 err = got_error(GOT_ERR_PRIVSEP_MSG);
2056 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
2057 err = got_error_from_errno("close");
2064 commit_painting_free(&keep, &drop, &skip);
2066 got_packidx_close(packidx);
2068 got_pack_close(pack);
2069 got_object_cache_close(&objcache);
2071 if (basefile && fclose(basefile) == EOF && err == NULL)
2072 err = got_error_from_errno("fclose");
2073 if (accumfile && fclose(accumfile) == EOF && err == NULL)
2074 err = got_error_from_errno("fclose");
2075 if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
2076 err = got_error_from_errno("fclose");
2078 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
2079 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
2080 got_privsep_send_error(&ibuf, err);
2083 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
2084 err = got_error_from_errno("close");