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_pack.h"
46 #include "got_lib_path.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_object.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_object_idcache.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_repository.h"
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
62 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
65 struct got_object_id *
66 got_object_id_dup(struct got_object_id *id1)
68 struct got_object_id *id2;
70 id2 = malloc(sizeof(*id2));
73 memcpy(id2, id1, sizeof(*id2));
77 struct got_object_id *
78 got_object_get_id(struct got_object *obj)
83 const struct got_error *
84 got_object_get_id_str(char **outbuf, struct got_object *obj)
86 return got_object_id_str(outbuf, &obj->id);
90 got_object_get_type(struct got_object *obj)
93 case GOT_OBJ_TYPE_COMMIT:
94 case GOT_OBJ_TYPE_TREE:
95 case GOT_OBJ_TYPE_BLOB:
96 case GOT_OBJ_TYPE_TAG:
107 static const struct got_error *
108 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
110 const struct got_error *err = NULL;
112 char *path_objects = got_repo_get_path_objects(repo);
116 if (path_objects == NULL)
117 return got_error_from_errno();
119 err = got_object_id_str(&hex, id);
123 if (asprintf(path, "%s/%.2x/%s", path_objects,
124 id->sha1[0], hex + 2) == -1)
125 err = got_error_from_errno();
133 static const struct got_error *
134 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
136 const struct got_error *err = NULL;
139 err = object_path(&path, &obj->id, repo);
142 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
144 err = got_error_from_errno();
152 static const struct got_error *
153 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
157 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
158 size = strlen(packidx->path_packidx) + 2;
159 if (size < GOT_PACKFILE_NAMELEN + 1)
160 return got_error(GOT_ERR_BAD_PATH);
162 *path_packfile = calloc(size, sizeof(**path_packfile));
163 if (*path_packfile == NULL)
164 return got_error_from_errno();
166 /* Copy up to and excluding ".idx". */
167 if (strlcpy(*path_packfile, packidx->path_packidx,
168 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
169 return got_error(GOT_ERR_NO_SPACE);
171 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
172 return got_error(GOT_ERR_NO_SPACE);
177 static const struct got_error *
178 open_packed_object(struct got_object **obj, struct got_object_id *id,
179 struct got_repository *repo)
181 const struct got_error *err = NULL;
182 struct got_pack *pack = NULL;
183 struct got_packidx *packidx = NULL;
187 err = got_repo_search_packidx(&packidx, &idx, repo, id);
191 err = get_packfile_path(&path_packfile, packidx);
195 pack = got_repo_get_cached_pack(repo, path_packfile);
197 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
202 err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
206 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
212 const struct got_error *
213 got_object_open(struct got_object **obj, struct got_repository *repo,
214 struct got_object_id *id)
216 const struct got_error *err = NULL;
220 *obj = got_repo_get_cached_object(repo, id);
226 err = open_packed_object(obj, id, repo);
227 if (err && err->code != GOT_ERR_NO_OBJ)
231 return got_repo_cache_object(repo, id, *obj);
234 err = object_path(&path, id, repo);
238 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
241 err = got_error(GOT_ERR_NO_OBJ);
243 err = got_error_from_errno();
246 err = got_object_read_header_privsep(obj, repo, fd);
249 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
253 err = got_repo_cache_object(repo, id, *obj);
262 const struct got_error *
263 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
266 struct got_object_id id;
268 if (!got_parse_sha1_digest(id.sha1, id_str))
269 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
271 return got_object_open(obj, repo, &id);
274 static const struct got_error *
275 open_commit(struct got_commit_object **commit,
276 struct got_repository *repo, struct got_object *obj, int check_cache)
278 const struct got_error *err = NULL;
281 *commit = got_repo_get_cached_commit(repo, &obj->id);
282 if (*commit != NULL) {
289 if (obj->type != GOT_OBJ_TYPE_COMMIT)
290 return got_error(GOT_ERR_OBJ_TYPE);
292 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
293 struct got_pack *pack;
294 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
296 err = got_repo_cache_pack(&pack, repo,
297 obj->path_packfile, NULL);
301 err = got_object_read_packed_commit_privsep(commit, obj, pack);
304 err = open_loose_object(&fd, obj, repo);
307 err = got_object_read_commit_privsep(commit, obj, fd, repo);
313 err = got_repo_cache_commit(repo, &obj->id, *commit);
319 const struct got_error *
320 got_object_open_as_commit(struct got_commit_object **commit,
321 struct got_repository *repo, struct got_object_id *id)
323 const struct got_error *err;
324 struct got_object *obj;
326 *commit = got_repo_get_cached_commit(repo, id);
327 if (*commit != NULL) {
332 err = got_object_open(&obj, repo, id);
335 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
336 err = got_error(GOT_ERR_OBJ_TYPE);
340 err = open_commit(commit, repo, obj, 0);
342 got_object_close(obj);
346 const struct got_error *
347 got_object_commit_open(struct got_commit_object **commit,
348 struct got_repository *repo, struct got_object *obj)
350 return open_commit(commit, repo, obj, 1);
353 const struct got_error *
354 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
356 const struct got_error *err = NULL;
358 *qid = calloc(1, sizeof(**qid));
360 return got_error_from_errno();
362 (*qid)->id = got_object_id_dup(id);
363 if ((*qid)->id == NULL) {
364 err = got_error_from_errno();
365 got_object_qid_free(*qid);
373 static const struct got_error *
374 open_tree(struct got_tree_object **tree,
375 struct got_repository *repo, struct got_object *obj, int check_cache)
377 const struct got_error *err = NULL;
380 *tree = got_repo_get_cached_tree(repo, &obj->id);
388 if (obj->type != GOT_OBJ_TYPE_TREE)
389 return got_error(GOT_ERR_OBJ_TYPE);
391 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
392 struct got_pack *pack;
393 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
395 err = got_repo_cache_pack(&pack, repo,
396 obj->path_packfile, NULL);
400 err = got_object_read_packed_tree_privsep(tree, obj, pack);
403 err = open_loose_object(&fd, obj, repo);
406 err = got_object_read_tree_privsep(tree, obj, fd, repo);
412 err = got_repo_cache_tree(repo, &obj->id, *tree);
418 const struct got_error *
419 got_object_open_as_tree(struct got_tree_object **tree,
420 struct got_repository *repo, struct got_object_id *id)
422 const struct got_error *err;
423 struct got_object *obj;
425 *tree = got_repo_get_cached_tree(repo, id);
431 err = got_object_open(&obj, repo, id);
434 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
435 err = got_error(GOT_ERR_OBJ_TYPE);
439 err = open_tree(tree, repo, obj, 0);
441 got_object_close(obj);
445 const struct got_error *
446 got_object_tree_open(struct got_tree_object **tree,
447 struct got_repository *repo, struct got_object *obj)
449 return open_tree(tree, repo, obj, 1);
452 const struct got_tree_entries *
453 got_object_tree_get_entries(struct got_tree_object *tree)
455 return &tree->entries;
458 static const struct got_error *
459 read_packed_blob_privsep(size_t *size, int outfd, struct got_object *obj,
460 struct got_pack *pack)
462 const struct got_error *err = NULL;
464 int basefd, accumfd; /* temporary files for delta application */
466 basefd = got_opentempfd();
468 return got_error_from_errno();
469 accumfd = got_opentempfd();
471 return got_error_from_errno();
473 outfd_child = dup(outfd);
474 if (outfd_child == -1)
475 return got_error_from_errno();
477 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
481 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
487 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
496 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
504 err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
508 if (lseek(outfd, SEEK_SET, 0) == -1)
509 err = got_error_from_errno();
514 const struct got_error *
515 got_object_blob_open(struct got_blob_object **blob,
516 struct got_repository *repo, struct got_object *obj, size_t blocksize)
518 const struct got_error *err = NULL;
523 if (obj->type != GOT_OBJ_TYPE_BLOB)
524 return got_error(GOT_ERR_OBJ_TYPE);
526 if (blocksize < obj->hdrlen)
527 return got_error(GOT_ERR_NO_SPACE);
529 *blob = calloc(1, sizeof(**blob));
531 return got_error_from_errno();
533 outfd = got_opentempfd();
535 return got_error_from_errno();
537 (*blob)->read_buf = malloc(blocksize);
538 if ((*blob)->read_buf == NULL) {
539 err = got_error_from_errno();
542 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
543 struct got_pack *pack;
544 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
546 err = got_repo_cache_pack(&pack, repo,
547 obj->path_packfile, NULL);
551 err = read_packed_blob_privsep(&size, outfd, obj, pack);
558 err = open_loose_object(&infd, obj, repo);
562 err = got_object_read_blob_privsep(&size, outfd, infd, repo);
567 if (size != obj->hdrlen + obj->size) {
568 err = got_error(GOT_ERR_PRIVSEP_LEN);
573 if (fstat(outfd, &sb) == -1) {
574 err = got_error_from_errno();
578 if (sb.st_size != obj->hdrlen + obj->size) {
579 err = got_error(GOT_ERR_PRIVSEP_LEN);
583 (*blob)->f = fdopen(outfd, "rb");
584 if ((*blob)->f == NULL) {
585 err = got_error_from_errno();
590 (*blob)->hdrlen = obj->hdrlen;
591 (*blob)->blocksize = blocksize;
592 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
599 free((*blob)->read_buf);
602 } else if (outfd != -1)
608 const struct got_error *
609 got_object_open_as_blob(struct got_blob_object **blob,
610 struct got_repository *repo, struct got_object_id *id,
613 const struct got_error *err;
614 struct got_object *obj;
618 err = got_object_open(&obj, repo, id);
621 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
622 err = got_error(GOT_ERR_OBJ_TYPE);
626 err = got_object_blob_open(blob, repo, obj, blocksize);
628 got_object_close(obj);
633 got_object_blob_close(struct got_blob_object *blob)
635 free(blob->read_buf);
641 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
643 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
647 got_object_blob_get_hdrlen(struct got_blob_object *blob)
653 got_object_blob_get_read_buf(struct got_blob_object *blob)
655 return blob->read_buf;
658 const struct got_error *
659 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
663 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
664 if (n == 0 && ferror(blob->f))
665 return got_ferror(blob->f, GOT_ERR_IO);
670 const struct got_error *
671 got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
672 FILE *outfile, struct got_blob_object *blob)
674 const struct got_error *err = NULL;
684 hdrlen = got_object_blob_get_hdrlen(blob);
686 err = got_object_blob_read_block(&len, blob);
693 buf = got_object_blob_get_read_buf(blob);
695 for (i = 0; i < len; i++) {
700 /* Skip blob object header first time around. */
701 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
711 static struct got_tree_entry *
712 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
714 struct got_tree_entry *te;
716 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
717 if (strncmp(te->name, name, len) == 0 && te->name[len] == '\0')
723 const struct got_error *
724 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
725 struct got_object_id *commit_id, const char *path)
727 const struct got_error *err = NULL;
728 struct got_commit_object *commit = NULL;
729 struct got_tree_object *tree = NULL;
730 struct got_tree_entry *te = NULL;
732 size_t seglen, len = strlen(path);
736 /* We are expecting an absolute in-repository path. */
738 return got_error(GOT_ERR_NOT_ABSPATH);
740 err = got_object_open_as_commit(&commit, repo, commit_id);
744 /* Handle opening of root of commit's tree. */
745 if (path[1] == '\0') {
746 *id = got_object_id_dup(commit->tree_id);
748 err = got_error_from_errno();
752 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
757 s++; /* skip leading '/' */
762 struct got_tree_object *next_tree;
772 te = find_entry_by_name(tree, seg, seglen);
774 err = got_error(GOT_ERR_NO_OBJ);
786 err = got_object_open_as_tree(&next_tree, repo,
791 got_object_tree_close(tree);
797 *id = got_object_id_dup(te->id);
799 return got_error_from_errno();
801 err = got_error(GOT_ERR_NO_OBJ);
804 got_object_commit_close(commit);
806 got_object_tree_close(tree);
810 const struct got_error *
811 got_object_tree_path_changed(int *changed,
812 struct got_tree_object *tree01, struct got_tree_object *tree02,
813 const char *path, struct got_repository *repo)
815 const struct got_error *err = NULL;
816 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
817 struct got_tree_entry *te1 = NULL, *te2 = NULL;
819 size_t seglen, len = strlen(path);
823 /* We are expecting an absolute in-repository path. */
825 return got_error(GOT_ERR_NOT_ABSPATH);
827 /* We not do support comparing the root path. */
829 return got_error(GOT_ERR_BAD_PATH);
834 s++; /* skip leading '/' */
839 struct got_tree_object *next_tree1, *next_tree2;
849 te1 = find_entry_by_name(tree1, seg, seglen);
851 err = got_error(GOT_ERR_NO_OBJ);
855 te2 = find_entry_by_name(tree2, seg, seglen);
861 if (te1->mode != te2->mode) {
866 if (got_object_id_cmp(te1->id, te2->id) == 0) {
871 if (len == 0) { /* final path element */
881 err = got_object_open_as_tree(&next_tree1, repo,
887 got_object_tree_close(tree1);
890 err = got_object_open_as_tree(&next_tree2, repo,
896 got_object_tree_close(tree2);
901 if (tree1 && tree1 != tree01)
902 got_object_tree_close(tree1);
903 if (tree2 && tree2 != tree02)
904 got_object_tree_close(tree2);
909 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
913 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
914 fprintf(stderr, "%s: %s\n", getprogname(),
918 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
919 fprintf(stderr, "%s: %s\n", getprogname(),
924 if (execl(path, path, repo_path, (char *)NULL) == -1) {
925 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
931 static const struct got_error *
932 request_object(struct got_object **obj, struct got_repository *repo, int fd)
934 const struct got_error *err = NULL;
935 struct imsgbuf *ibuf;
937 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
939 err = got_privsep_send_obj_req(ibuf, fd, NULL);
943 return got_privsep_recv_obj(obj, ibuf);
946 const struct got_error *
947 got_object_read_header_privsep(struct got_object **obj,
948 struct got_repository *repo, int obj_fd)
952 struct imsgbuf *ibuf;
954 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
955 return request_object(obj, repo, obj_fd);
957 ibuf = calloc(1, sizeof(*ibuf));
959 return got_error_from_errno();
961 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
962 return got_error_from_errno();
966 return got_error_from_errno();
968 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
974 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
976 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
977 imsg_init(ibuf, imsg_fds[0]);
978 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
980 return request_object(obj, repo, obj_fd);
983 static const struct got_error *
984 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
985 struct got_object_id *id)
987 const struct got_error *err = NULL;
988 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
990 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
994 err = got_privsep_recv_obj(obj, ibuf);
998 (*obj)->path_packfile = strdup(pack->path_packfile);
999 if ((*obj)->path_packfile == NULL) {
1000 err = got_error_from_errno();
1003 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1008 const struct got_error *
1009 got_object_packed_read_privsep(struct got_object **obj,
1010 struct got_repository *repo, struct got_pack *pack,
1011 struct got_packidx *packidx, int idx, struct got_object_id *id)
1013 const struct got_error *err = NULL;
1016 struct imsgbuf *ibuf;
1018 if (pack->privsep_child)
1019 return request_packed_object(obj, pack, idx, id);
1021 ibuf = calloc(1, sizeof(*ibuf));
1023 return got_error_from_errno();
1025 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
1026 if (pack->privsep_child == NULL) {
1027 err = got_error_from_errno();
1032 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1033 err = got_error_from_errno();
1039 err = got_error_from_errno();
1041 } else if (pid == 0) {
1042 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
1043 pack->path_packfile);
1048 pack->privsep_child->imsg_fd = imsg_fds[0];
1049 pack->privsep_child->pid = pid;
1050 imsg_init(ibuf, imsg_fds[0]);
1051 pack->privsep_child->ibuf = ibuf;
1053 err = got_privsep_init_pack_child(ibuf, pack, packidx);
1055 const struct got_error *child_err;
1056 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
1057 child_err = got_privsep_wait_for_child(
1058 pack->privsep_child->pid);
1059 if (child_err && err == NULL)
1062 free(pack->privsep_child);
1063 pack->privsep_child = NULL;
1070 free(pack->privsep_child);
1071 pack->privsep_child = NULL;
1073 err = request_packed_object(obj, pack, idx, id);
1078 static const struct got_error *
1079 request_commit(struct got_commit_object **commit, struct got_repository *repo,
1080 struct got_object *obj, int fd)
1082 const struct got_error *err = NULL;
1083 struct imsgbuf *ibuf;
1085 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
1087 err = got_privsep_send_obj_req(ibuf, fd, obj);
1091 return got_privsep_recv_commit(commit, ibuf);
1094 const struct got_error *
1095 got_object_read_packed_commit_privsep(struct got_commit_object **commit,
1096 struct got_object *obj, struct got_pack *pack)
1098 const struct got_error *err = NULL;
1100 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1104 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
1107 const struct got_error *
1108 got_object_read_commit_privsep(struct got_commit_object **commit,
1109 struct got_object *obj, int obj_fd, struct got_repository *repo)
1113 struct imsgbuf *ibuf;
1115 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
1116 return request_commit(commit, repo, obj, obj_fd);
1118 ibuf = calloc(1, sizeof(*ibuf));
1120 return got_error_from_errno();
1122 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1123 return got_error_from_errno();
1127 return got_error_from_errno();
1128 else if (pid == 0) {
1129 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
1135 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
1137 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
1138 imsg_init(ibuf, imsg_fds[0]);
1139 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
1141 return request_commit(commit, repo, obj, obj_fd);
1144 static const struct got_error *
1145 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1146 struct got_object *obj, int fd)
1148 const struct got_error *err = NULL;
1149 struct imsgbuf *ibuf;
1151 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1153 err = got_privsep_send_obj_req(ibuf, fd, obj);
1157 return got_privsep_recv_tree(tree, ibuf);
1160 const struct got_error *
1161 got_object_read_tree_privsep(struct got_tree_object **tree,
1162 struct got_object *obj, int obj_fd, struct got_repository *repo)
1166 struct imsgbuf *ibuf;
1168 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1169 return request_tree(tree, repo, obj, obj_fd);
1171 ibuf = calloc(1, sizeof(*ibuf));
1173 return got_error_from_errno();
1175 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1176 return got_error_from_errno();
1180 return got_error_from_errno();
1181 else if (pid == 0) {
1182 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1189 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1191 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1192 imsg_init(ibuf, imsg_fds[0]);
1193 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1196 return request_tree(tree, repo, obj, obj_fd);
1199 const struct got_error *
1200 got_object_read_packed_tree_privsep(struct got_tree_object **tree,
1201 struct got_object *obj, struct got_pack *pack)
1203 const struct got_error *err = NULL;
1205 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1209 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1212 static const struct got_error *
1213 request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
1215 const struct got_error *err = NULL;
1218 outfd_child = dup(outfd);
1219 if (outfd_child == -1)
1220 return got_error_from_errno();
1222 err = got_privsep_send_blob_req(ibuf, infd);
1226 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1232 err = got_privsep_recv_blob(size, ibuf);
1236 if (lseek(outfd, SEEK_SET, 0) == -1)
1237 return got_error_from_errno();
1242 const struct got_error *
1243 got_object_read_blob_privsep(size_t *size, int outfd, int infd,
1244 struct got_repository *repo)
1248 struct imsgbuf *ibuf;
1250 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1251 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1252 return request_blob(size, outfd, infd, ibuf);
1255 ibuf = calloc(1, sizeof(*ibuf));
1257 return got_error_from_errno();
1259 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1260 return got_error_from_errno();
1264 return got_error_from_errno();
1265 else if (pid == 0) {
1266 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1272 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1274 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1275 imsg_init(ibuf, imsg_fds[0]);
1276 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1278 return request_blob(size, outfd, infd, ibuf);