2 * Copyright (c) 2018 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>
19 #include <sys/queue.h>
21 #include <sys/socket.h>
23 #include <sys/syslimits.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_repository.h"
41 #include "got_opentemp.h"
43 #include "got_lib_sha1.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_path.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_object.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_object_idcache.h"
50 #include "got_lib_object_cache.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_pack.h"
53 #include "got_lib_repository.h"
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 struct got_object_id *
60 got_object_id_dup(struct got_object_id *id1)
62 struct got_object_id *id2;
64 id2 = malloc(sizeof(*id2));
67 memcpy(id2, id1, sizeof(*id2));
71 struct got_object_id *
72 got_object_get_id(struct got_object *obj)
77 const struct got_error *
78 got_object_get_id_str(char **outbuf, struct got_object *obj)
80 return got_object_id_str(outbuf, &obj->id);
83 const struct got_error *
84 got_object_get_type(int *type, struct got_repository *repo,
85 struct got_object_id *id)
87 const struct got_error *err = NULL;
88 struct got_object *obj;
90 err = got_object_open(&obj, repo, id);
95 case GOT_OBJ_TYPE_COMMIT:
96 case GOT_OBJ_TYPE_TREE:
97 case GOT_OBJ_TYPE_BLOB:
98 case GOT_OBJ_TYPE_TAG:
102 err = got_error(GOT_ERR_OBJ_TYPE);
106 got_object_close(obj);
110 static const struct got_error *
111 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
113 const struct got_error *err = NULL;
115 char *path_objects = got_repo_get_path_objects(repo);
119 if (path_objects == NULL)
120 return got_error_from_errno();
122 err = got_object_id_str(&hex, id);
126 if (asprintf(path, "%s/%.2x/%s", path_objects,
127 id->sha1[0], hex + 2) == -1)
128 err = got_error_from_errno();
136 static const struct got_error *
137 open_loose_object(int *fd, struct got_object_id *id,
138 struct got_repository *repo)
140 const struct got_error *err = NULL;
143 err = object_path(&path, id, repo);
146 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
148 err = got_error_from_errno();
156 static const struct got_error *
157 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
161 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
162 size = strlen(packidx->path_packidx) + 2;
163 if (size < GOT_PACKFILE_NAMELEN + 1)
164 return got_error(GOT_ERR_BAD_PATH);
166 *path_packfile = malloc(size);
167 if (*path_packfile == NULL)
168 return got_error_from_errno();
170 /* Copy up to and excluding ".idx". */
171 if (strlcpy(*path_packfile, packidx->path_packidx,
172 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
173 return got_error(GOT_ERR_NO_SPACE);
175 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
176 return got_error(GOT_ERR_NO_SPACE);
181 static const struct got_error *
182 open_packed_object(struct got_object **obj, struct got_object_id *id,
183 struct got_repository *repo)
185 const struct got_error *err = NULL;
186 struct got_pack *pack = NULL;
187 struct got_packidx *packidx = NULL;
191 err = got_repo_search_packidx(&packidx, &idx, repo, id);
195 err = get_packfile_path(&path_packfile, packidx);
199 pack = got_repo_get_cached_pack(repo, path_packfile);
201 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
206 err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
210 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
216 const struct got_error *
217 got_object_open(struct got_object **obj, struct got_repository *repo,
218 struct got_object_id *id)
220 const struct got_error *err = NULL;
224 *obj = got_repo_get_cached_object(repo, id);
230 err = open_packed_object(obj, id, repo);
231 if (err && err->code != GOT_ERR_NO_OBJ)
235 return got_repo_cache_object(repo, id, *obj);
238 err = object_path(&path, id, repo);
242 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
245 err = got_error_no_obj(id);
247 err = got_error_from_errno();
250 err = got_object_read_header_privsep(obj, repo, fd);
253 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
257 err = got_repo_cache_object(repo, id, *obj);
266 const struct got_error *
267 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
270 struct got_object_id id;
272 if (!got_parse_sha1_digest(id.sha1, id_str))
273 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
275 return got_object_open(obj, repo, &id);
278 const struct got_error *
279 got_object_resolve_id_str(struct got_object_id **id,
280 struct got_repository *repo, const char *id_str)
282 const struct got_error *err = NULL;
283 struct got_object *obj;
285 err = got_object_open_by_id_str(&obj, repo, id_str);
289 *id = got_object_id_dup(got_object_get_id(obj));
290 got_object_close(obj);
292 return got_error_from_errno();
297 static const struct got_error *
298 open_commit(struct got_commit_object **commit,
299 struct got_repository *repo, struct got_object_id *id, int check_cache)
301 const struct got_error *err = NULL;
302 struct got_packidx *packidx = NULL;
307 *commit = got_repo_get_cached_commit(repo, id);
308 if (*commit != NULL) {
315 err = got_repo_search_packidx(&packidx, &idx, repo, id);
317 struct got_pack *pack = NULL;
319 err = get_packfile_path(&path_packfile, packidx);
323 pack = got_repo_get_cached_pack(repo, path_packfile);
325 err = got_repo_cache_pack(&pack, repo, path_packfile,
330 err = got_object_read_packed_commit_privsep(commit, pack,
332 } else if (err->code == GOT_ERR_NO_OBJ) {
335 err = open_loose_object(&fd, id, repo);
338 err = got_object_read_commit_privsep(commit, fd, repo);
344 err = got_repo_cache_commit(repo, id, *commit);
350 const struct got_error *
351 got_object_open_as_commit(struct got_commit_object **commit,
352 struct got_repository *repo, struct got_object_id *id)
354 *commit = got_repo_get_cached_commit(repo, id);
355 if (*commit != NULL) {
360 return open_commit(commit, repo, id, 0);
363 const struct got_error *
364 got_object_commit_open(struct got_commit_object **commit,
365 struct got_repository *repo, struct got_object *obj)
367 return open_commit(commit, repo, got_object_get_id(obj), 1);
370 const struct got_error *
371 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
373 const struct got_error *err = NULL;
375 *qid = calloc(1, sizeof(**qid));
377 return got_error_from_errno();
379 (*qid)->id = got_object_id_dup(id);
380 if ((*qid)->id == NULL) {
381 err = got_error_from_errno();
382 got_object_qid_free(*qid);
390 static const struct got_error *
391 open_tree(struct got_tree_object **tree,
392 struct got_repository *repo, struct got_object *obj, int check_cache)
394 const struct got_error *err = NULL;
397 *tree = got_repo_get_cached_tree(repo, &obj->id);
405 if (obj->type != GOT_OBJ_TYPE_TREE)
406 return got_error(GOT_ERR_OBJ_TYPE);
408 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
409 struct got_pack *pack;
410 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
412 err = got_repo_cache_pack(&pack, repo,
413 obj->path_packfile, NULL);
417 err = got_object_read_packed_tree_privsep(tree, obj, pack);
420 err = open_loose_object(&fd, got_object_get_id(obj), repo);
423 err = got_object_read_tree_privsep(tree, obj, fd, repo);
429 err = got_repo_cache_tree(repo, &obj->id, *tree);
435 const struct got_error *
436 got_object_open_as_tree(struct got_tree_object **tree,
437 struct got_repository *repo, struct got_object_id *id)
439 const struct got_error *err;
440 struct got_object *obj;
442 *tree = got_repo_get_cached_tree(repo, id);
448 err = got_object_open(&obj, repo, id);
451 if (obj->type != GOT_OBJ_TYPE_TREE) {
452 err = got_error(GOT_ERR_OBJ_TYPE);
456 err = open_tree(tree, repo, obj, 0);
458 got_object_close(obj);
462 const struct got_error *
463 got_object_tree_open(struct got_tree_object **tree,
464 struct got_repository *repo, struct got_object *obj)
466 return open_tree(tree, repo, obj, 1);
469 const struct got_tree_entries *
470 got_object_tree_get_entries(struct got_tree_object *tree)
472 return &tree->entries;
475 static const struct got_error *
476 read_packed_blob_privsep(size_t *size, int outfd, struct got_object *obj,
477 struct got_pack *pack)
479 const struct got_error *err = NULL;
481 int basefd, accumfd; /* temporary files for delta application */
483 basefd = got_opentempfd();
485 return got_error_from_errno();
486 accumfd = got_opentempfd();
488 return got_error_from_errno();
490 outfd_child = dup(outfd);
491 if (outfd_child == -1)
492 return got_error_from_errno();
494 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
498 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
504 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
513 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
521 err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
525 if (lseek(outfd, SEEK_SET, 0) == -1)
526 err = got_error_from_errno();
531 const struct got_error *
532 got_object_blob_open(struct got_blob_object **blob,
533 struct got_repository *repo, struct got_object *obj, size_t blocksize)
535 const struct got_error *err = NULL;
540 if (obj->type != GOT_OBJ_TYPE_BLOB)
541 return got_error(GOT_ERR_OBJ_TYPE);
543 if (blocksize < obj->hdrlen)
544 return got_error(GOT_ERR_NO_SPACE);
546 *blob = calloc(1, sizeof(**blob));
548 return got_error_from_errno();
550 outfd = got_opentempfd();
552 return got_error_from_errno();
554 (*blob)->read_buf = malloc(blocksize);
555 if ((*blob)->read_buf == NULL) {
556 err = got_error_from_errno();
559 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
560 struct got_pack *pack;
561 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
563 err = got_repo_cache_pack(&pack, repo,
564 obj->path_packfile, NULL);
568 err = read_packed_blob_privsep(&size, outfd, obj, pack);
575 err = open_loose_object(&infd, got_object_get_id(obj), repo);
579 err = got_object_read_blob_privsep(&size, outfd, infd, repo);
584 if (size != obj->hdrlen + obj->size) {
585 err = got_error(GOT_ERR_PRIVSEP_LEN);
590 if (fstat(outfd, &sb) == -1) {
591 err = got_error_from_errno();
595 if (sb.st_size != obj->hdrlen + obj->size) {
596 err = got_error(GOT_ERR_PRIVSEP_LEN);
600 (*blob)->f = fdopen(outfd, "rb");
601 if ((*blob)->f == NULL) {
602 err = got_error_from_errno();
607 (*blob)->hdrlen = obj->hdrlen;
608 (*blob)->blocksize = blocksize;
609 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
616 free((*blob)->read_buf);
619 } else if (outfd != -1)
625 const struct got_error *
626 got_object_open_as_blob(struct got_blob_object **blob,
627 struct got_repository *repo, struct got_object_id *id,
630 const struct got_error *err;
631 struct got_object *obj;
635 err = got_object_open(&obj, repo, id);
638 if (obj->type != GOT_OBJ_TYPE_BLOB) {
639 err = got_error(GOT_ERR_OBJ_TYPE);
643 err = got_object_blob_open(blob, repo, obj, blocksize);
645 got_object_close(obj);
650 got_object_blob_close(struct got_blob_object *blob)
652 free(blob->read_buf);
658 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
660 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
664 got_object_blob_get_hdrlen(struct got_blob_object *blob)
670 got_object_blob_get_read_buf(struct got_blob_object *blob)
672 return blob->read_buf;
675 const struct got_error *
676 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
680 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
681 if (n == 0 && ferror(blob->f))
682 return got_ferror(blob->f, GOT_ERR_IO);
687 const struct got_error *
688 got_object_blob_dump_to_file(size_t *total_len, int *nlines,
689 FILE *outfile, struct got_blob_object *blob)
691 const struct got_error *err = NULL;
701 hdrlen = got_object_blob_get_hdrlen(blob);
703 err = got_object_blob_read_block(&len, blob);
710 buf = got_object_blob_get_read_buf(blob);
712 for (i = 0; i < len; i++) {
717 /* Skip blob object header first time around. */
718 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
728 static const struct got_error *
729 open_tag(struct got_tag_object **tag,
730 struct got_repository *repo, struct got_object *obj, int check_cache)
732 const struct got_error *err = NULL;
735 *tag = got_repo_get_cached_tag(repo, &obj->id);
743 if (obj->type != GOT_OBJ_TYPE_TAG)
744 return got_error(GOT_ERR_OBJ_TYPE);
746 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
747 struct got_pack *pack;
748 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
750 err = got_repo_cache_pack(&pack, repo,
751 obj->path_packfile, NULL);
755 err = got_object_read_packed_tag_privsep(tag, obj, pack);
758 err = open_loose_object(&fd, got_object_get_id(obj), repo);
761 err = got_object_read_tag_privsep(tag, obj, fd, repo);
767 err = got_repo_cache_tag(repo, &obj->id, *tag);
773 const struct got_error *
774 got_object_open_as_tag(struct got_tag_object **tag,
775 struct got_repository *repo, struct got_object_id *id)
777 const struct got_error *err;
778 struct got_object *obj;
780 *tag = got_repo_get_cached_tag(repo, id);
786 err = got_object_open(&obj, repo, id);
789 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
790 err = got_error(GOT_ERR_OBJ_TYPE);
794 err = open_tag(tag, repo, obj, 0);
796 got_object_close(obj);
800 const struct got_error *
801 got_object_tag_open(struct got_tag_object **tag,
802 struct got_repository *repo, struct got_object *obj)
804 return open_tag(tag, repo, obj, 1);
807 static struct got_tree_entry *
808 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
810 struct got_tree_entry *te;
812 /* Note that tree entries are sorted in strncmp() order. */
813 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
814 int cmp = strncmp(te->name, name, len);
819 if (te->name[len] == '\0')
825 const struct got_error *
826 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
827 struct got_object_id *commit_id, const char *path)
829 const struct got_error *err = NULL;
830 struct got_commit_object *commit = NULL;
831 struct got_tree_object *tree = NULL;
832 struct got_tree_entry *te = NULL;
838 /* We are expecting an absolute in-repository path. */
840 return got_error(GOT_ERR_NOT_ABSPATH);
842 err = got_object_open_as_commit(&commit, repo, commit_id);
846 /* Handle opening of root of commit's tree. */
847 if (path[1] == '\0') {
848 *id = got_object_id_dup(commit->tree_id);
850 err = got_error_from_errno();
854 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
859 s++; /* skip leading '/' */
863 struct got_tree_object *next_tree;
872 te = find_entry_by_name(tree, seg, seglen);
874 err = got_error(GOT_ERR_NO_TREE_ENTRY);
885 err = got_object_open_as_tree(&next_tree, repo,
890 got_object_tree_close(tree);
896 *id = got_object_id_dup(te->id);
898 return got_error_from_errno();
900 err = got_error(GOT_ERR_NO_TREE_ENTRY);
903 got_object_commit_close(commit);
905 got_object_tree_close(tree);
909 const struct got_error *
910 got_object_tree_path_changed(int *changed,
911 struct got_tree_object *tree01, struct got_tree_object *tree02,
912 const char *path, struct got_repository *repo)
914 const struct got_error *err = NULL;
915 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
916 struct got_tree_entry *te1 = NULL, *te2 = NULL;
922 /* We are expecting an absolute in-repository path. */
924 return got_error(GOT_ERR_NOT_ABSPATH);
926 /* We not do support comparing the root path. */
928 return got_error(GOT_ERR_BAD_PATH);
933 s++; /* skip leading '/' */
937 struct got_tree_object *next_tree1, *next_tree2;
946 te1 = find_entry_by_name(tree1, seg, seglen);
948 err = got_error(GOT_ERR_NO_OBJ);
952 te2 = find_entry_by_name(tree2, seg, seglen);
958 if (te1->mode != te2->mode) {
963 if (got_object_id_cmp(te1->id, te2->id) == 0) {
968 if (*s == '\0') { /* final path element */
977 err = got_object_open_as_tree(&next_tree1, repo,
983 got_object_tree_close(tree1);
986 err = got_object_open_as_tree(&next_tree2, repo,
992 got_object_tree_close(tree2);
997 if (tree1 && tree1 != tree01)
998 got_object_tree_close(tree1);
999 if (tree2 && tree2 != tree02)
1000 got_object_tree_close(tree2);
1005 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
1009 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1010 fprintf(stderr, "%s: %s\n", getprogname(),
1014 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1015 fprintf(stderr, "%s: %s\n", getprogname(),
1020 if (execl(path, path, repo_path, (char *)NULL) == -1) {
1021 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
1027 static const struct got_error *
1028 request_object(struct got_object **obj, struct got_repository *repo, int fd)
1030 const struct got_error *err = NULL;
1031 struct imsgbuf *ibuf;
1033 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
1035 err = got_privsep_send_obj_req(ibuf, fd, NULL);
1039 return got_privsep_recv_obj(obj, ibuf);
1042 const struct got_error *
1043 got_object_read_header_privsep(struct got_object **obj,
1044 struct got_repository *repo, int obj_fd)
1048 struct imsgbuf *ibuf;
1050 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
1051 return request_object(obj, repo, obj_fd);
1053 ibuf = calloc(1, sizeof(*ibuf));
1055 return got_error_from_errno();
1057 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1058 return got_error_from_errno();
1062 return got_error_from_errno();
1063 else if (pid == 0) {
1064 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
1070 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
1072 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
1073 imsg_init(ibuf, imsg_fds[0]);
1074 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
1076 return request_object(obj, repo, obj_fd);
1079 static const struct got_error *
1080 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
1081 struct got_object_id *id)
1083 const struct got_error *err = NULL;
1084 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1086 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
1090 err = got_privsep_recv_obj(obj, ibuf);
1094 (*obj)->path_packfile = strdup(pack->path_packfile);
1095 if ((*obj)->path_packfile == NULL) {
1096 err = got_error_from_errno();
1099 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1104 const struct got_error *
1105 got_object_packed_read_privsep(struct got_object **obj,
1106 struct got_repository *repo, struct got_pack *pack,
1107 struct got_packidx *packidx, int idx, struct got_object_id *id)
1109 const struct got_error *err = NULL;
1112 struct imsgbuf *ibuf;
1114 if (pack->privsep_child)
1115 return request_packed_object(obj, pack, idx, id);
1117 ibuf = calloc(1, sizeof(*ibuf));
1119 return got_error_from_errno();
1121 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
1122 if (pack->privsep_child == NULL) {
1123 err = got_error_from_errno();
1128 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1129 err = got_error_from_errno();
1135 err = got_error_from_errno();
1137 } else if (pid == 0) {
1138 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
1139 pack->path_packfile);
1144 pack->privsep_child->imsg_fd = imsg_fds[0];
1145 pack->privsep_child->pid = pid;
1146 imsg_init(ibuf, imsg_fds[0]);
1147 pack->privsep_child->ibuf = ibuf;
1149 err = got_privsep_init_pack_child(ibuf, pack, packidx);
1151 const struct got_error *child_err;
1152 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
1153 child_err = got_privsep_wait_for_child(
1154 pack->privsep_child->pid);
1155 if (child_err && err == NULL)
1158 free(pack->privsep_child);
1159 pack->privsep_child = NULL;
1166 free(pack->privsep_child);
1167 pack->privsep_child = NULL;
1169 err = request_packed_object(obj, pack, idx, id);
1173 static const struct got_error *
1174 request_commit(struct got_commit_object **commit, struct got_repository *repo,
1177 const struct got_error *err = NULL;
1178 struct imsgbuf *ibuf;
1180 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
1182 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
1186 return got_privsep_recv_commit(commit, ibuf);
1189 static const struct got_error *
1190 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
1191 int pack_idx, struct got_object_id *id)
1193 const struct got_error *err = NULL;
1195 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
1200 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
1203 const struct got_error *
1204 got_object_read_packed_commit_privsep(struct got_commit_object **commit,
1205 struct got_pack *pack, struct got_packidx *packidx, int idx,
1206 struct got_object_id *id)
1208 const struct got_error *err = NULL;
1211 struct imsgbuf *ibuf;
1213 if (pack->privsep_child)
1214 return request_packed_commit(commit, pack, idx, id);
1216 ibuf = calloc(1, sizeof(*ibuf));
1218 return got_error_from_errno();
1220 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
1221 if (pack->privsep_child == NULL) {
1222 err = got_error_from_errno();
1227 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1228 err = got_error_from_errno();
1234 err = got_error_from_errno();
1236 } else if (pid == 0) {
1237 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
1238 pack->path_packfile);
1243 pack->privsep_child->imsg_fd = imsg_fds[0];
1244 pack->privsep_child->pid = pid;
1245 imsg_init(ibuf, imsg_fds[0]);
1246 pack->privsep_child->ibuf = ibuf;
1248 err = got_privsep_init_pack_child(ibuf, pack, packidx);
1250 const struct got_error *child_err;
1251 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
1252 child_err = got_privsep_wait_for_child(
1253 pack->privsep_child->pid);
1254 if (child_err && err == NULL)
1257 free(pack->privsep_child);
1258 pack->privsep_child = NULL;
1265 free(pack->privsep_child);
1266 pack->privsep_child = NULL;
1268 err = request_packed_commit(commit, pack, idx, id);
1272 const struct got_error *
1273 got_object_read_commit_privsep(struct got_commit_object **commit,
1274 int obj_fd, struct got_repository *repo)
1278 struct imsgbuf *ibuf;
1280 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
1281 return request_commit(commit, repo, obj_fd);
1283 ibuf = calloc(1, sizeof(*ibuf));
1285 return got_error_from_errno();
1287 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1288 return got_error_from_errno();
1292 return got_error_from_errno();
1293 else if (pid == 0) {
1294 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
1300 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
1302 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
1303 imsg_init(ibuf, imsg_fds[0]);
1304 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
1306 return request_commit(commit, repo, obj_fd);
1309 static const struct got_error *
1310 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1311 struct got_object *obj, int fd)
1313 const struct got_error *err = NULL;
1314 struct imsgbuf *ibuf;
1316 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1318 err = got_privsep_send_obj_req(ibuf, fd, obj);
1322 return got_privsep_recv_tree(tree, ibuf);
1325 const struct got_error *
1326 got_object_read_tree_privsep(struct got_tree_object **tree,
1327 struct got_object *obj, int obj_fd, struct got_repository *repo)
1331 struct imsgbuf *ibuf;
1333 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1334 return request_tree(tree, repo, obj, obj_fd);
1336 ibuf = calloc(1, sizeof(*ibuf));
1338 return got_error_from_errno();
1340 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1341 return got_error_from_errno();
1345 return got_error_from_errno();
1346 else if (pid == 0) {
1347 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1354 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1356 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1357 imsg_init(ibuf, imsg_fds[0]);
1358 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1361 return request_tree(tree, repo, obj, obj_fd);
1364 const struct got_error *
1365 got_object_read_packed_tree_privsep(struct got_tree_object **tree,
1366 struct got_object *obj, struct got_pack *pack)
1368 const struct got_error *err = NULL;
1370 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1374 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1377 static const struct got_error *
1378 request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
1380 const struct got_error *err = NULL;
1383 outfd_child = dup(outfd);
1384 if (outfd_child == -1)
1385 return got_error_from_errno();
1387 err = got_privsep_send_blob_req(ibuf, infd);
1391 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1397 err = got_privsep_recv_blob(size, ibuf);
1401 if (lseek(outfd, SEEK_SET, 0) == -1)
1402 return got_error_from_errno();
1407 const struct got_error *
1408 got_object_read_blob_privsep(size_t *size, int outfd, int infd,
1409 struct got_repository *repo)
1413 struct imsgbuf *ibuf;
1415 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1416 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1417 return request_blob(size, outfd, infd, ibuf);
1420 ibuf = calloc(1, sizeof(*ibuf));
1422 return got_error_from_errno();
1424 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1425 return got_error_from_errno();
1429 return got_error_from_errno();
1430 else if (pid == 0) {
1431 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1437 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1439 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1440 imsg_init(ibuf, imsg_fds[0]);
1441 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1443 return request_blob(size, outfd, infd, ibuf);
1446 static const struct got_error *
1447 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1448 struct got_object *obj, int fd)
1450 const struct got_error *err = NULL;
1451 struct imsgbuf *ibuf;
1453 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1455 err = got_privsep_send_obj_req(ibuf, fd, obj);
1459 return got_privsep_recv_tag(tag, ibuf);
1462 const struct got_error *
1463 got_object_read_packed_tag_privsep(struct got_tag_object **tag,
1464 struct got_object *obj, struct got_pack *pack)
1466 const struct got_error *err = NULL;
1468 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1472 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1475 const struct got_error *
1476 got_object_read_tag_privsep(struct got_tag_object **tag,
1477 struct got_object *obj, int obj_fd, struct got_repository *repo)
1481 struct imsgbuf *ibuf;
1483 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1484 return request_tag(tag, repo, obj, obj_fd);
1486 ibuf = calloc(1, sizeof(*ibuf));
1488 return got_error_from_errno();
1490 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1491 return got_error_from_errno();
1495 return got_error_from_errno();
1496 else if (pid == 0) {
1497 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1503 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1505 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1506 imsg_init(ibuf, imsg_fds[0]);
1507 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1509 return request_tag(tag, repo, obj, obj_fd);