2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
34 #include "got_error.h"
35 #include "got_object.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_delta_cache.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_cache.h"
42 #include "got_lib_object_parse.h"
43 #include "got_lib_privsep.h"
44 #include "got_lib_pack.h"
46 static volatile sig_atomic_t sigint_received;
49 catch_sigint(int signo)
54 static const struct got_error *
55 open_object(struct got_object **obj, struct got_pack *pack,
56 struct got_packidx *packidx, int idx, struct got_object_id *id,
57 struct got_object_cache *objcache)
59 const struct got_error *err;
61 err = got_packfile_open_object(obj, pack, packidx, idx, id);
66 err = got_object_cache_add(objcache, id, *obj);
68 if (err->code == GOT_ERR_OBJ_EXISTS ||
69 err->code == GOT_ERR_OBJ_TOO_LARGE)
77 static const struct got_error *
78 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
79 struct got_packidx *packidx, struct got_object_cache *objcache)
81 const struct got_error *err = NULL;
82 struct got_imsg_packed_object iobj;
83 struct got_object *obj;
84 struct got_object_id id;
87 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
88 if (datalen != sizeof(iobj))
89 return got_error(GOT_ERR_PRIVSEP_LEN);
90 memcpy(&iobj, imsg->data, sizeof(iobj));
91 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
93 obj = got_object_cache_get(objcache, &id);
97 err = open_object(&obj, pack, packidx, iobj.idx, &id,
103 err = got_privsep_send_obj(ibuf, obj);
105 got_object_close(obj);
109 static const struct got_error *
110 open_commit(struct got_commit_object **commit, struct got_pack *pack,
111 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
112 struct got_object_cache *objcache)
114 const struct got_error *err = NULL;
115 struct got_object *obj = NULL;
121 obj = got_object_cache_get(objcache, id);
125 err = open_object(&obj, pack, packidx, obj_idx, id,
131 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
137 err = got_object_parse_commit(commit, buf, len);
139 got_object_close(obj);
144 static const struct got_error *
145 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
146 struct got_packidx *packidx, struct got_object_cache *objcache)
148 const struct got_error *err = NULL;
149 struct got_imsg_packed_object iobj;
150 struct got_commit_object *commit = NULL;
151 struct got_object_id id;
154 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
155 if (datalen != sizeof(iobj))
156 return got_error(GOT_ERR_PRIVSEP_LEN);
157 memcpy(&iobj, imsg->data, sizeof(iobj));
158 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
160 err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
164 err = got_privsep_send_commit(ibuf, commit);
167 got_object_commit_close(commit);
169 if (err->code == GOT_ERR_PRIVSEP_PIPE)
172 got_privsep_send_error(ibuf, err);
178 static const struct got_error *
179 open_tree(uint8_t **buf, struct got_pathlist_head *entries, int *nentries,
180 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
181 struct got_object_id *id, struct got_object_cache *objcache)
183 const struct got_error *err = NULL;
184 struct got_object *obj = NULL;
190 obj = got_object_cache_get(objcache, id);
194 err = open_object(&obj, pack, packidx, obj_idx, id,
200 err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
206 err = got_object_parse_tree(entries, nentries, *buf, len);
208 got_object_close(obj);
216 static const struct got_error *
217 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
218 struct got_packidx *packidx, struct got_object_cache *objcache)
220 const struct got_error *err = NULL;
221 struct got_imsg_packed_object iobj;
222 struct got_pathlist_head entries;
225 struct got_object_id id;
228 TAILQ_INIT(&entries);
230 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
231 if (datalen != sizeof(iobj))
232 return got_error(GOT_ERR_PRIVSEP_LEN);
233 memcpy(&iobj, imsg->data, sizeof(iobj));
234 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
236 err = open_tree(&buf, &entries, &nentries, pack, packidx, iobj.idx,
241 err = got_privsep_send_tree(ibuf, &entries, nentries);
242 got_object_parsed_tree_entries_free(&entries);
245 if (err->code == GOT_ERR_PRIVSEP_PIPE)
248 got_privsep_send_error(ibuf, err);
254 static const struct got_error *
255 receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
257 const struct got_error *err;
261 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
265 if (imsg.hdr.type != imsg_code) {
266 err = got_error(GOT_ERR_PRIVSEP_MSG);
270 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
272 err = got_error(GOT_ERR_PRIVSEP_LEN);
276 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
280 *f = fdopen(imsg.fd, "w+");
282 err = got_error_from_errno("fdopen");
291 static const struct got_error *
292 receive_tempfile(FILE **basefile, FILE **accumfile, struct imsg *imsg,
293 struct imsgbuf *ibuf)
298 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
300 return got_error(GOT_ERR_PRIVSEP_LEN);
303 return got_error(GOT_ERR_PRIVSEP_NO_FD);
305 if (*basefile == NULL)
307 else if (*accumfile == NULL)
310 return got_error(GOT_ERR_PRIVSEP_MSG);
312 *f = fdopen(imsg->fd, "w+");
314 return got_error_from_errno("fdopen");
320 static const struct got_error *
321 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
322 struct got_packidx *packidx, struct got_object_cache *objcache,
323 FILE *basefile, FILE *accumfile)
325 const struct got_error *err = NULL;
326 struct got_imsg_packed_object iobj;
327 struct got_object *obj = NULL;
328 FILE *outfile = NULL;
329 struct got_object_id id;
334 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
335 if (datalen != sizeof(iobj))
336 return got_error(GOT_ERR_PRIVSEP_LEN);
337 memcpy(&iobj, imsg->data, sizeof(iobj));
338 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
340 obj = got_object_cache_get(objcache, &id);
344 err = open_object(&obj, pack, packidx, iobj.idx, &id,
350 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
354 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
355 err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
359 blob_size = obj->size;
361 if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
362 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
365 err = got_packfile_extract_object(pack, obj, outfile, basefile,
370 err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
373 if (outfile && fclose(outfile) == EOF && err == NULL)
374 err = got_error_from_errno("fclose");
375 got_object_close(obj);
376 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
377 got_privsep_send_error(ibuf, err);
382 static const struct got_error *
383 tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
384 struct got_packidx *packidx, struct got_object_cache *objcache)
386 const struct got_error *err = NULL;
387 struct got_imsg_packed_object iobj;
388 struct got_object *obj = NULL;
389 struct got_tag_object *tag = NULL;
392 struct got_object_id id;
395 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
396 if (datalen != sizeof(iobj))
397 return got_error(GOT_ERR_PRIVSEP_LEN);
398 memcpy(&iobj, imsg->data, sizeof(iobj));
399 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
401 obj = got_object_cache_get(objcache, &id);
405 err = open_object(&obj, pack, packidx, iobj.idx, &id,
411 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
416 err = got_object_parse_tag(&tag, buf, len);
420 err = got_privsep_send_tag(ibuf, tag);
423 got_object_close(obj);
425 got_object_tag_close(tag);
427 if (err->code == GOT_ERR_PRIVSEP_PIPE)
430 got_privsep_send_error(ibuf, err);
436 static struct got_parsed_tree_entry *
437 find_entry_by_name(struct got_pathlist_head *entries, int nentries,
438 const char *name, size_t len)
440 struct got_pathlist_entry *pe;
442 /* Note that tree entries are sorted in strncmp() order. */
443 TAILQ_FOREACH(pe, entries, entry) {
444 int cmp = strncmp(pe->path, name, len);
449 if (pe->path[len] == '\0')
450 return (struct got_parsed_tree_entry *)pe->data;
455 static const struct got_error *
456 tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
457 struct got_pathlist_head *entries1, int *nentries1,
458 struct got_pathlist_head *entries2, int *nentries2,
459 const char *path, struct got_pack *pack, struct got_packidx *packidx,
460 struct imsgbuf *ibuf, struct got_object_cache *objcache)
462 const struct got_error *err = NULL;
463 struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
469 /* We not do support comparing the root path. */
470 if (got_path_is_root_dir(path))
471 return got_error_path(path, GOT_ERR_BAD_PATH);
486 pte1 = find_entry_by_name(entries1, *nentries1, seg, seglen);
488 err = got_error(GOT_ERR_NO_OBJ);
492 pte2 = find_entry_by_name(entries2, *nentries2, seg, seglen);
498 if (pte1->mode != pte2->mode) {
503 if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
508 if (*s == '\0') { /* final path element */
517 struct got_object_id id1, id2;
520 memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
521 idx = got_packidx_get_object_idx(packidx, &id1);
523 err = got_error_no_obj(&id1);
526 got_object_parsed_tree_entries_free(entries1);
530 err = open_tree(buf1, entries1, nentries1, pack,
531 packidx, idx, &id1, objcache);
536 memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
537 idx = got_packidx_get_object_idx(packidx, &id2);
539 err = got_error_no_obj(&id2);
542 got_object_parsed_tree_entries_free(entries2);
546 err = open_tree(buf2, entries2, nentries2, pack,
547 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)
561 const struct got_error *err;
565 wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
566 sizeof(struct got_imsg_traversed_commits) +
567 ncommits * SHA1_DIGEST_LENGTH);
569 return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
571 if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
572 err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
576 for (i = 0; i < ncommits; i++) {
577 struct got_object_id *id = &commit_ids[i];
578 if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
579 err = got_error_from_errno(
580 "imsg_add TRAVERSED_COMMITS");
587 imsg_close(ibuf, wbuf);
589 return got_privsep_flush_imsg(ibuf);
592 static const struct got_error *
593 send_commit_traversal_done(struct imsgbuf *ibuf)
595 if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
597 return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
599 return got_privsep_flush_imsg(ibuf);
603 static const struct got_error *
604 commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
605 struct got_pack *pack, struct got_packidx *packidx,
606 struct got_object_cache *objcache)
608 const struct got_error *err = NULL;
609 struct got_imsg_packed_object iobj;
610 struct got_object_qid *pid;
611 struct got_commit_object *commit = NULL, *pcommit = NULL;
612 struct got_pathlist_head entries, pentries;
613 int nentries = 0, pnentries = 0;
614 struct got_object_id id;
615 size_t datalen, path_len;
617 const int min_alloc = 64;
618 int changed = 0, ncommits = 0, nallocated = 0;
619 struct got_object_id *commit_ids = NULL;
621 TAILQ_INIT(&entries);
622 TAILQ_INIT(&pentries);
624 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
625 if (datalen < sizeof(iobj))
626 return got_error(GOT_ERR_PRIVSEP_LEN);
627 memcpy(&iobj, imsg->data, sizeof(iobj));
628 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
630 path_len = datalen - sizeof(iobj) - 1;
632 return got_error(GOT_ERR_PRIVSEP_LEN);
634 path = imsg->data + sizeof(iobj);
635 if (path[path_len] != '\0')
636 return got_error(GOT_ERR_PRIVSEP_LEN);
639 nallocated = min_alloc;
640 commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
641 if (commit_ids == NULL)
642 return got_error_from_errno("reallocarray");
645 const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
648 if (sigint_received) {
649 err = got_error(GOT_ERR_CANCELLED);
653 if (commit == NULL) {
654 idx = got_packidx_get_object_idx(packidx, &id);
657 err = open_commit(&commit, pack, packidx,
660 if (err->code != GOT_ERR_NO_OBJ)
667 if (sizeof(struct got_imsg_traversed_commits) +
668 ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
669 err = send_traversed_commits(commit_ids, ncommits,
676 if (ncommits > nallocated) {
677 struct got_object_id *new;
678 nallocated += min_alloc;
679 new = reallocarray(commit_ids, nallocated,
680 sizeof(*commit_ids));
682 err = got_error_from_errno("reallocarray");
687 memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
690 pid = STAILQ_FIRST(&commit->parent_ids);
694 idx = got_packidx_get_object_idx(packidx, pid->id);
698 err = open_commit(&pcommit, pack, packidx, idx, pid->id,
701 if (err->code != GOT_ERR_NO_OBJ)
707 if (path[0] == '/' && path[1] == '\0') {
708 if (got_object_id_cmp(pcommit->tree_id,
709 commit->tree_id) != 0) {
715 uint8_t *buf = NULL, *pbuf = NULL;
717 idx = got_packidx_get_object_idx(packidx,
721 pidx = got_packidx_get_object_idx(packidx,
726 err = open_tree(&buf, &entries, &nentries, pack,
727 packidx, idx, commit->tree_id, objcache);
730 err = open_tree(&pbuf, &pentries, &pnentries, pack,
731 packidx, pidx, pcommit->tree_id, objcache);
737 err = tree_path_changed(&changed, &buf, &pbuf,
738 &entries, &nentries, &pentries, &pnentries, path,
739 pack, packidx, ibuf, objcache);
741 got_object_parsed_tree_entries_free(&entries);
744 got_object_parsed_tree_entries_free(&pentries);
748 if (err->code != GOT_ERR_NO_OBJ)
756 memcpy(id.sha1, pid->id->sha1, SHA1_DIGEST_LENGTH);
757 got_object_commit_close(commit);
764 err = send_traversed_commits(commit_ids, ncommits, ibuf);
769 err = got_privsep_send_commit(ibuf, commit);
774 err = send_commit_traversal_done(ibuf);
778 got_object_commit_close(commit);
780 got_object_commit_close(pcommit);
782 got_object_parsed_tree_entries_free(&entries);
784 got_object_parsed_tree_entries_free(&pentries);
786 if (err->code == GOT_ERR_PRIVSEP_PIPE)
789 got_privsep_send_error(ibuf, err);
795 static const struct got_error *
796 raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
797 struct got_pack *pack, struct got_packidx *packidx,
798 struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
800 const struct got_error *err = NULL;
803 FILE *outfile = NULL;
804 struct got_imsg_packed_object iobj;
805 struct got_object *obj;
806 struct got_object_id id;
809 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
810 if (datalen != sizeof(iobj))
811 return got_error(GOT_ERR_PRIVSEP_LEN);
812 memcpy(&iobj, imsg->data, sizeof(iobj));
813 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
815 obj = got_object_cache_get(objcache, &id);
819 err = open_object(&obj, pack, packidx, iobj.idx, &id,
825 err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
829 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
830 err = got_pack_get_max_delta_object_size(&size, obj, pack);
836 if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
837 err = got_packfile_extract_object_to_mem(&buf, &obj->size,
840 err = got_packfile_extract_object(pack, obj, outfile, basefile,
845 err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
848 if (outfile && fclose(outfile) == EOF && err == NULL)
849 err = got_error_from_errno("fclose");
850 got_object_close(obj);
851 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
852 got_privsep_send_error(ibuf, err);
859 static const struct got_error *
860 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
862 const struct got_error *err = NULL;
864 struct got_imsg_packidx ipackidx;
866 struct got_packidx *p;
870 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
874 p = calloc(1, sizeof(*p));
876 err = got_error_from_errno("calloc");
880 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
881 err = got_error(GOT_ERR_PRIVSEP_MSG);
886 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
890 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
891 if (datalen != sizeof(ipackidx)) {
892 err = got_error(GOT_ERR_PRIVSEP_LEN);
895 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
897 p->len = ipackidx.len;
898 p->fd = dup(imsg.fd);
900 err = got_error_from_errno("dup");
903 if (lseek(p->fd, 0, SEEK_SET) == -1) {
904 err = got_error_from_errno("lseek");
908 #ifndef GOT_PACK_NO_MMAP
909 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
910 if (p->map == MAP_FAILED)
911 p->map = NULL; /* fall back to read(2) */
913 err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
918 got_packidx_close(p);
925 static const struct got_error *
926 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
928 const struct got_error *err = NULL;
930 struct got_imsg_pack ipack;
932 struct got_pack *pack;
936 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
940 pack = calloc(1, sizeof(*pack));
942 err = got_error_from_errno("calloc");
946 if (imsg.hdr.type != GOT_IMSG_PACK) {
947 err = got_error(GOT_ERR_PRIVSEP_MSG);
952 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
956 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
957 if (datalen != sizeof(ipack)) {
958 err = got_error(GOT_ERR_PRIVSEP_LEN);
961 memcpy(&ipack, imsg.data, sizeof(ipack));
963 pack->filesize = ipack.filesize;
964 pack->fd = dup(imsg.fd);
965 if (pack->fd == -1) {
966 err = got_error_from_errno("dup");
969 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
970 err = got_error_from_errno("lseek");
973 pack->path_packfile = strdup(ipack.path_packfile);
974 if (pack->path_packfile == NULL) {
975 err = got_error_from_errno("strdup");
979 pack->delta_cache = got_delta_cache_alloc(100,
980 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
981 if (pack->delta_cache == NULL) {
982 err = got_error_from_errno("got_delta_cache_alloc");
986 #ifndef GOT_PACK_NO_MMAP
987 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
989 if (pack->map == MAP_FAILED)
990 pack->map = NULL; /* fall back to read(2) */
1004 main(int argc, char *argv[])
1006 const struct got_error *err = NULL;
1007 struct imsgbuf ibuf;
1009 struct got_packidx *packidx = NULL;
1010 struct got_pack *pack = NULL;
1011 struct got_object_cache objcache;
1012 FILE *basefile = NULL, *accumfile = NULL;
1014 //static int attached;
1015 //while (!attached) sleep(1);
1017 signal(SIGINT, catch_sigint);
1019 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1021 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1023 err = got_error_from_errno("got_object_cache_init");
1024 got_privsep_send_error(&ibuf, err);
1029 /* revoke access to most system calls */
1030 if (pledge("stdio recvfd", NULL) == -1) {
1031 err = got_error_from_errno("pledge");
1032 got_privsep_send_error(&ibuf, err);
1037 err = receive_packidx(&packidx, &ibuf);
1039 got_privsep_send_error(&ibuf, err);
1043 err = receive_pack(&pack, &ibuf);
1045 got_privsep_send_error(&ibuf, err);
1052 if (sigint_received) {
1053 err = got_error(GOT_ERR_CANCELLED);
1057 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1059 if (err->code == GOT_ERR_PRIVSEP_PIPE)
1064 if (imsg.hdr.type == GOT_IMSG_STOP)
1067 switch (imsg.hdr.type) {
1068 case GOT_IMSG_TMPFD:
1069 err = receive_tempfile(&basefile, &accumfile,
1072 case GOT_IMSG_PACKED_OBJECT_REQUEST:
1073 err = object_request(&imsg, &ibuf, pack, packidx,
1076 case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1077 if (basefile == NULL || accumfile == NULL) {
1078 err = got_error(GOT_ERR_PRIVSEP_MSG);
1081 err = raw_object_request(&imsg, &ibuf, pack, packidx,
1082 &objcache, basefile, accumfile);
1084 case GOT_IMSG_COMMIT_REQUEST:
1085 err = commit_request(&imsg, &ibuf, pack, packidx,
1088 case GOT_IMSG_TREE_REQUEST:
1089 err = tree_request(&imsg, &ibuf, pack, packidx,
1092 case GOT_IMSG_BLOB_REQUEST:
1093 if (basefile == NULL || accumfile == NULL) {
1094 err = got_error(GOT_ERR_PRIVSEP_MSG);
1097 err = blob_request(&imsg, &ibuf, pack, packidx,
1098 &objcache, basefile, accumfile);
1100 case GOT_IMSG_TAG_REQUEST:
1101 err = tag_request(&imsg, &ibuf, pack, packidx,
1104 case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1105 err = commit_traversal_request(&imsg, &ibuf, pack,
1106 packidx, &objcache);
1109 err = got_error(GOT_ERR_PRIVSEP_MSG);
1113 if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
1114 err = got_error_from_errno("close");
1121 got_packidx_close(packidx);
1123 got_pack_close(pack);
1124 got_object_cache_close(&objcache);
1126 if (basefile && fclose(basefile) == EOF && err == NULL)
1127 err = got_error_from_errno("fclose");
1128 if (accumfile && fclose(accumfile) == EOF && err == NULL)
1129 err = got_error_from_errno("fclose");
1131 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1132 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1133 got_privsep_send_error(&ibuf, err);
1136 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
1137 err = got_error_from_errno("close");