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))
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);
84 got_object_get_type(struct got_object *obj)
87 case GOT_OBJ_TYPE_COMMIT:
88 case GOT_OBJ_TYPE_TREE:
89 case GOT_OBJ_TYPE_BLOB:
90 case GOT_OBJ_TYPE_TAG:
101 static const struct got_error *
102 object_path(char **path, struct got_object_id *id, struct got_repository *repo)
104 const struct got_error *err = NULL;
106 char *path_objects = got_repo_get_path_objects(repo);
110 if (path_objects == NULL)
111 return got_error_from_errno();
113 err = got_object_id_str(&hex, id);
117 if (asprintf(path, "%s/%.2x/%s", path_objects,
118 id->sha1[0], hex + 2) == -1)
119 err = got_error_from_errno();
127 static const struct got_error *
128 open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
130 const struct got_error *err = NULL;
133 err = object_path(&path, &obj->id, repo);
136 *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
138 err = got_error_from_errno();
146 static const struct got_error *
147 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
151 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
152 size = strlen(packidx->path_packidx) + 2;
153 if (size < GOT_PACKFILE_NAMELEN + 1)
154 return got_error(GOT_ERR_BAD_PATH);
156 *path_packfile = malloc(size);
157 if (*path_packfile == NULL)
158 return got_error_from_errno();
160 /* Copy up to and excluding ".idx". */
161 if (strlcpy(*path_packfile, packidx->path_packidx,
162 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
163 return got_error(GOT_ERR_NO_SPACE);
165 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
166 return got_error(GOT_ERR_NO_SPACE);
171 static const struct got_error *
172 open_packed_object(struct got_object **obj, struct got_object_id *id,
173 struct got_repository *repo)
175 const struct got_error *err = NULL;
176 struct got_pack *pack = NULL;
177 struct got_packidx *packidx = NULL;
181 err = got_repo_search_packidx(&packidx, &idx, repo, id);
185 err = get_packfile_path(&path_packfile, packidx);
189 pack = got_repo_get_cached_pack(repo, path_packfile);
191 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
196 err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
200 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
206 const struct got_error *
207 got_object_open(struct got_object **obj, struct got_repository *repo,
208 struct got_object_id *id)
210 const struct got_error *err = NULL;
214 *obj = got_repo_get_cached_object(repo, id);
220 err = open_packed_object(obj, id, repo);
221 if (err && err->code != GOT_ERR_NO_OBJ)
225 return got_repo_cache_object(repo, id, *obj);
228 err = object_path(&path, id, repo);
232 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
235 err = got_error_no_obj(id);
237 err = got_error_from_errno();
240 err = got_object_read_header_privsep(obj, repo, fd);
243 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
247 err = got_repo_cache_object(repo, id, *obj);
256 const struct got_error *
257 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
260 struct got_object_id id;
262 if (!got_parse_sha1_digest(id.sha1, id_str))
263 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
265 return got_object_open(obj, repo, &id);
268 static const struct got_error *
269 open_commit(struct got_commit_object **commit,
270 struct got_repository *repo, struct got_object *obj, int check_cache)
272 const struct got_error *err = NULL;
275 *commit = got_repo_get_cached_commit(repo, &obj->id);
276 if (*commit != NULL) {
283 if (obj->type != GOT_OBJ_TYPE_COMMIT)
284 return got_error(GOT_ERR_OBJ_TYPE);
286 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
287 struct got_pack *pack;
288 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
290 err = got_repo_cache_pack(&pack, repo,
291 obj->path_packfile, NULL);
295 err = got_object_read_packed_commit_privsep(commit, obj, pack);
298 err = open_loose_object(&fd, obj, repo);
301 err = got_object_read_commit_privsep(commit, obj, fd, repo);
307 err = got_repo_cache_commit(repo, &obj->id, *commit);
313 const struct got_error *
314 got_object_open_as_commit(struct got_commit_object **commit,
315 struct got_repository *repo, struct got_object_id *id)
317 const struct got_error *err;
318 struct got_object *obj;
320 *commit = got_repo_get_cached_commit(repo, id);
321 if (*commit != NULL) {
326 err = got_object_open(&obj, repo, id);
329 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
330 err = got_error(GOT_ERR_OBJ_TYPE);
334 err = open_commit(commit, repo, obj, 0);
336 got_object_close(obj);
340 const struct got_error *
341 got_object_commit_open(struct got_commit_object **commit,
342 struct got_repository *repo, struct got_object *obj)
344 return open_commit(commit, repo, obj, 1);
347 const struct got_error *
348 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
350 const struct got_error *err = NULL;
352 *qid = calloc(1, sizeof(**qid));
354 return got_error_from_errno();
356 (*qid)->id = got_object_id_dup(id);
357 if ((*qid)->id == NULL) {
358 err = got_error_from_errno();
359 got_object_qid_free(*qid);
367 static const struct got_error *
368 open_tree(struct got_tree_object **tree,
369 struct got_repository *repo, struct got_object *obj, int check_cache)
371 const struct got_error *err = NULL;
374 *tree = got_repo_get_cached_tree(repo, &obj->id);
382 if (obj->type != GOT_OBJ_TYPE_TREE)
383 return got_error(GOT_ERR_OBJ_TYPE);
385 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
386 struct got_pack *pack;
387 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
389 err = got_repo_cache_pack(&pack, repo,
390 obj->path_packfile, NULL);
394 err = got_object_read_packed_tree_privsep(tree, obj, pack);
397 err = open_loose_object(&fd, obj, repo);
400 err = got_object_read_tree_privsep(tree, obj, fd, repo);
406 err = got_repo_cache_tree(repo, &obj->id, *tree);
412 const struct got_error *
413 got_object_open_as_tree(struct got_tree_object **tree,
414 struct got_repository *repo, struct got_object_id *id)
416 const struct got_error *err;
417 struct got_object *obj;
419 *tree = got_repo_get_cached_tree(repo, id);
425 err = got_object_open(&obj, repo, id);
428 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
429 err = got_error(GOT_ERR_OBJ_TYPE);
433 err = open_tree(tree, repo, obj, 0);
435 got_object_close(obj);
439 const struct got_error *
440 got_object_tree_open(struct got_tree_object **tree,
441 struct got_repository *repo, struct got_object *obj)
443 return open_tree(tree, repo, obj, 1);
446 const struct got_tree_entries *
447 got_object_tree_get_entries(struct got_tree_object *tree)
449 return &tree->entries;
452 static const struct got_error *
453 read_packed_blob_privsep(size_t *size, int outfd, struct got_object *obj,
454 struct got_pack *pack)
456 const struct got_error *err = NULL;
458 int basefd, accumfd; /* temporary files for delta application */
460 basefd = got_opentempfd();
462 return got_error_from_errno();
463 accumfd = got_opentempfd();
465 return got_error_from_errno();
467 outfd_child = dup(outfd);
468 if (outfd_child == -1)
469 return got_error_from_errno();
471 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
475 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
481 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
490 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
498 err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
502 if (lseek(outfd, SEEK_SET, 0) == -1)
503 err = got_error_from_errno();
508 const struct got_error *
509 got_object_blob_open(struct got_blob_object **blob,
510 struct got_repository *repo, struct got_object *obj, size_t blocksize)
512 const struct got_error *err = NULL;
517 if (obj->type != GOT_OBJ_TYPE_BLOB)
518 return got_error(GOT_ERR_OBJ_TYPE);
520 if (blocksize < obj->hdrlen)
521 return got_error(GOT_ERR_NO_SPACE);
523 *blob = calloc(1, sizeof(**blob));
525 return got_error_from_errno();
527 outfd = got_opentempfd();
529 return got_error_from_errno();
531 (*blob)->read_buf = malloc(blocksize);
532 if ((*blob)->read_buf == NULL) {
533 err = got_error_from_errno();
536 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
537 struct got_pack *pack;
538 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
540 err = got_repo_cache_pack(&pack, repo,
541 obj->path_packfile, NULL);
545 err = read_packed_blob_privsep(&size, outfd, obj, pack);
552 err = open_loose_object(&infd, obj, repo);
556 err = got_object_read_blob_privsep(&size, outfd, infd, repo);
561 if (size != obj->hdrlen + obj->size) {
562 err = got_error(GOT_ERR_PRIVSEP_LEN);
567 if (fstat(outfd, &sb) == -1) {
568 err = got_error_from_errno();
572 if (sb.st_size != obj->hdrlen + obj->size) {
573 err = got_error(GOT_ERR_PRIVSEP_LEN);
577 (*blob)->f = fdopen(outfd, "rb");
578 if ((*blob)->f == NULL) {
579 err = got_error_from_errno();
584 (*blob)->hdrlen = obj->hdrlen;
585 (*blob)->blocksize = blocksize;
586 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
593 free((*blob)->read_buf);
596 } else if (outfd != -1)
602 const struct got_error *
603 got_object_open_as_blob(struct got_blob_object **blob,
604 struct got_repository *repo, struct got_object_id *id,
607 const struct got_error *err;
608 struct got_object *obj;
612 err = got_object_open(&obj, repo, id);
615 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
616 err = got_error(GOT_ERR_OBJ_TYPE);
620 err = got_object_blob_open(blob, repo, obj, blocksize);
622 got_object_close(obj);
627 got_object_blob_close(struct got_blob_object *blob)
629 free(blob->read_buf);
635 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
637 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
641 got_object_blob_get_hdrlen(struct got_blob_object *blob)
647 got_object_blob_get_read_buf(struct got_blob_object *blob)
649 return blob->read_buf;
652 const struct got_error *
653 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
657 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
658 if (n == 0 && ferror(blob->f))
659 return got_ferror(blob->f, GOT_ERR_IO);
664 const struct got_error *
665 got_object_blob_dump_to_file(size_t *total_len, int *nlines,
666 FILE *outfile, struct got_blob_object *blob)
668 const struct got_error *err = NULL;
678 hdrlen = got_object_blob_get_hdrlen(blob);
680 err = got_object_blob_read_block(&len, blob);
687 buf = got_object_blob_get_read_buf(blob);
689 for (i = 0; i < len; i++) {
694 /* Skip blob object header first time around. */
695 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
705 static const struct got_error *
706 open_tag(struct got_tag_object **tag,
707 struct got_repository *repo, struct got_object *obj, int check_cache)
709 const struct got_error *err = NULL;
712 *tag = got_repo_get_cached_tag(repo, &obj->id);
720 if (obj->type != GOT_OBJ_TYPE_TAG)
721 return got_error(GOT_ERR_OBJ_TYPE);
723 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
724 struct got_pack *pack;
725 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
727 err = got_repo_cache_pack(&pack, repo,
728 obj->path_packfile, NULL);
732 err = got_object_read_packed_tag_privsep(tag, obj, pack);
735 err = open_loose_object(&fd, obj, repo);
738 err = got_object_read_tag_privsep(tag, obj, fd, repo);
744 err = got_repo_cache_tag(repo, &obj->id, *tag);
750 const struct got_error *
751 got_object_open_as_tag(struct got_tag_object **tag,
752 struct got_repository *repo, struct got_object_id *id)
754 const struct got_error *err;
755 struct got_object *obj;
757 *tag = got_repo_get_cached_tag(repo, id);
763 err = got_object_open(&obj, repo, id);
766 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
767 err = got_error(GOT_ERR_OBJ_TYPE);
771 err = open_tag(tag, repo, obj, 0);
773 got_object_close(obj);
777 const struct got_error *
778 got_object_tag_open(struct got_tag_object **tag,
779 struct got_repository *repo, struct got_object *obj)
781 return open_tag(tag, repo, obj, 1);
784 static struct got_tree_entry *
785 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
787 struct got_tree_entry *te;
789 /* Note that tree entries are sorted in strncmp() order. */
790 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
791 int cmp = strncmp(te->name, name, len);
796 if (te->name[len] == '\0')
802 const struct got_error *
803 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
804 struct got_object_id *commit_id, const char *path)
806 const struct got_error *err = NULL;
807 struct got_commit_object *commit = NULL;
808 struct got_tree_object *tree = NULL;
809 struct got_tree_entry *te = NULL;
815 /* We are expecting an absolute in-repository path. */
817 return got_error(GOT_ERR_NOT_ABSPATH);
819 err = got_object_open_as_commit(&commit, repo, commit_id);
823 /* Handle opening of root of commit's tree. */
824 if (path[1] == '\0') {
825 *id = got_object_id_dup(commit->tree_id);
827 err = got_error_from_errno();
831 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
836 s++; /* skip leading '/' */
840 struct got_tree_object *next_tree;
849 te = find_entry_by_name(tree, seg, seglen);
851 err = got_error(GOT_ERR_NO_TREE_ENTRY);
862 err = got_object_open_as_tree(&next_tree, repo,
867 got_object_tree_close(tree);
873 *id = got_object_id_dup(te->id);
875 return got_error_from_errno();
877 err = got_error(GOT_ERR_NO_TREE_ENTRY);
880 got_object_commit_close(commit);
882 got_object_tree_close(tree);
886 const struct got_error *
887 got_object_tree_path_changed(int *changed,
888 struct got_tree_object *tree01, struct got_tree_object *tree02,
889 const char *path, struct got_repository *repo)
891 const struct got_error *err = NULL;
892 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
893 struct got_tree_entry *te1 = NULL, *te2 = NULL;
899 /* We are expecting an absolute in-repository path. */
901 return got_error(GOT_ERR_NOT_ABSPATH);
903 /* We not do support comparing the root path. */
905 return got_error(GOT_ERR_BAD_PATH);
910 s++; /* skip leading '/' */
914 struct got_tree_object *next_tree1, *next_tree2;
923 te1 = find_entry_by_name(tree1, seg, seglen);
925 err = got_error(GOT_ERR_NO_OBJ);
929 te2 = find_entry_by_name(tree2, seg, seglen);
935 if (te1->mode != te2->mode) {
940 if (got_object_id_cmp(te1->id, te2->id) == 0) {
945 if (*s == '\0') { /* final path element */
954 err = got_object_open_as_tree(&next_tree1, repo,
960 got_object_tree_close(tree1);
963 err = got_object_open_as_tree(&next_tree2, repo,
969 got_object_tree_close(tree2);
974 if (tree1 && tree1 != tree01)
975 got_object_tree_close(tree1);
976 if (tree2 && tree2 != tree02)
977 got_object_tree_close(tree2);
982 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
986 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
987 fprintf(stderr, "%s: %s\n", getprogname(),
991 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
992 fprintf(stderr, "%s: %s\n", getprogname(),
997 if (execl(path, path, repo_path, (char *)NULL) == -1) {
998 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
1004 static const struct got_error *
1005 request_object(struct got_object **obj, struct got_repository *repo, int fd)
1007 const struct got_error *err = NULL;
1008 struct imsgbuf *ibuf;
1010 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
1012 err = got_privsep_send_obj_req(ibuf, fd, NULL);
1016 return got_privsep_recv_obj(obj, ibuf);
1019 const struct got_error *
1020 got_object_read_header_privsep(struct got_object **obj,
1021 struct got_repository *repo, int obj_fd)
1025 struct imsgbuf *ibuf;
1027 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
1028 return request_object(obj, repo, obj_fd);
1030 ibuf = calloc(1, sizeof(*ibuf));
1032 return got_error_from_errno();
1034 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1035 return got_error_from_errno();
1039 return got_error_from_errno();
1040 else if (pid == 0) {
1041 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
1047 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
1049 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
1050 imsg_init(ibuf, imsg_fds[0]);
1051 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
1053 return request_object(obj, repo, obj_fd);
1056 static const struct got_error *
1057 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
1058 struct got_object_id *id)
1060 const struct got_error *err = NULL;
1061 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1063 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
1067 err = got_privsep_recv_obj(obj, ibuf);
1071 (*obj)->path_packfile = strdup(pack->path_packfile);
1072 if ((*obj)->path_packfile == NULL) {
1073 err = got_error_from_errno();
1076 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1081 const struct got_error *
1082 got_object_packed_read_privsep(struct got_object **obj,
1083 struct got_repository *repo, struct got_pack *pack,
1084 struct got_packidx *packidx, int idx, struct got_object_id *id)
1086 const struct got_error *err = NULL;
1089 struct imsgbuf *ibuf;
1091 if (pack->privsep_child)
1092 return request_packed_object(obj, pack, idx, id);
1094 ibuf = calloc(1, sizeof(*ibuf));
1096 return got_error_from_errno();
1098 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
1099 if (pack->privsep_child == NULL) {
1100 err = got_error_from_errno();
1105 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1106 err = got_error_from_errno();
1112 err = got_error_from_errno();
1114 } else if (pid == 0) {
1115 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
1116 pack->path_packfile);
1121 pack->privsep_child->imsg_fd = imsg_fds[0];
1122 pack->privsep_child->pid = pid;
1123 imsg_init(ibuf, imsg_fds[0]);
1124 pack->privsep_child->ibuf = ibuf;
1126 err = got_privsep_init_pack_child(ibuf, pack, packidx);
1128 const struct got_error *child_err;
1129 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
1130 child_err = got_privsep_wait_for_child(
1131 pack->privsep_child->pid);
1132 if (child_err && err == NULL)
1135 free(pack->privsep_child);
1136 pack->privsep_child = NULL;
1143 free(pack->privsep_child);
1144 pack->privsep_child = NULL;
1146 err = request_packed_object(obj, pack, idx, id);
1150 static const struct got_error *
1151 request_commit(struct got_commit_object **commit, struct got_repository *repo,
1152 struct got_object *obj, int fd)
1154 const struct got_error *err = NULL;
1155 struct imsgbuf *ibuf;
1157 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
1159 err = got_privsep_send_obj_req(ibuf, fd, obj);
1163 return got_privsep_recv_commit(commit, ibuf);
1166 const struct got_error *
1167 got_object_read_packed_commit_privsep(struct got_commit_object **commit,
1168 struct got_object *obj, struct got_pack *pack)
1170 const struct got_error *err = NULL;
1172 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1176 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
1179 const struct got_error *
1180 got_object_read_commit_privsep(struct got_commit_object **commit,
1181 struct got_object *obj, int obj_fd, struct got_repository *repo)
1185 struct imsgbuf *ibuf;
1187 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
1188 return request_commit(commit, repo, obj, obj_fd);
1190 ibuf = calloc(1, sizeof(*ibuf));
1192 return got_error_from_errno();
1194 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1195 return got_error_from_errno();
1199 return got_error_from_errno();
1200 else if (pid == 0) {
1201 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
1207 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
1209 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
1210 imsg_init(ibuf, imsg_fds[0]);
1211 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
1213 return request_commit(commit, repo, obj, obj_fd);
1216 static const struct got_error *
1217 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1218 struct got_object *obj, int fd)
1220 const struct got_error *err = NULL;
1221 struct imsgbuf *ibuf;
1223 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1225 err = got_privsep_send_obj_req(ibuf, fd, obj);
1229 return got_privsep_recv_tree(tree, ibuf);
1232 const struct got_error *
1233 got_object_read_tree_privsep(struct got_tree_object **tree,
1234 struct got_object *obj, int obj_fd, struct got_repository *repo)
1238 struct imsgbuf *ibuf;
1240 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1241 return request_tree(tree, repo, obj, obj_fd);
1243 ibuf = calloc(1, sizeof(*ibuf));
1245 return got_error_from_errno();
1247 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1248 return got_error_from_errno();
1252 return got_error_from_errno();
1253 else if (pid == 0) {
1254 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1261 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1263 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1264 imsg_init(ibuf, imsg_fds[0]);
1265 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1268 return request_tree(tree, repo, obj, obj_fd);
1271 const struct got_error *
1272 got_object_read_packed_tree_privsep(struct got_tree_object **tree,
1273 struct got_object *obj, struct got_pack *pack)
1275 const struct got_error *err = NULL;
1277 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1281 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1284 static const struct got_error *
1285 request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
1287 const struct got_error *err = NULL;
1290 outfd_child = dup(outfd);
1291 if (outfd_child == -1)
1292 return got_error_from_errno();
1294 err = got_privsep_send_blob_req(ibuf, infd);
1298 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1304 err = got_privsep_recv_blob(size, ibuf);
1308 if (lseek(outfd, SEEK_SET, 0) == -1)
1309 return got_error_from_errno();
1314 const struct got_error *
1315 got_object_read_blob_privsep(size_t *size, int outfd, int infd,
1316 struct got_repository *repo)
1320 struct imsgbuf *ibuf;
1322 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1323 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1324 return request_blob(size, outfd, infd, ibuf);
1327 ibuf = calloc(1, sizeof(*ibuf));
1329 return got_error_from_errno();
1331 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1332 return got_error_from_errno();
1336 return got_error_from_errno();
1337 else if (pid == 0) {
1338 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1344 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1346 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1347 imsg_init(ibuf, imsg_fds[0]);
1348 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1350 return request_blob(size, outfd, infd, ibuf);
1353 static const struct got_error *
1354 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1355 struct got_object *obj, int fd)
1357 const struct got_error *err = NULL;
1358 struct imsgbuf *ibuf;
1360 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1362 err = got_privsep_send_obj_req(ibuf, fd, obj);
1366 return got_privsep_recv_tag(tag, ibuf);
1369 const struct got_error *
1370 got_object_read_packed_tag_privsep(struct got_tag_object **tag,
1371 struct got_object *obj, struct got_pack *pack)
1373 const struct got_error *err = NULL;
1375 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1379 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1382 const struct got_error *
1383 got_object_read_tag_privsep(struct got_tag_object **tag,
1384 struct got_object *obj, int obj_fd, struct got_repository *repo)
1388 struct imsgbuf *ibuf;
1390 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1391 return request_tag(tag, repo, obj, obj_fd);
1393 ibuf = calloc(1, sizeof(*ibuf));
1395 return got_error_from_errno();
1397 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1398 return got_error_from_errno();
1402 return got_error_from_errno();
1403 else if (pid == 0) {
1404 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1410 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1412 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1413 imsg_init(ibuf, imsg_fds[0]);
1414 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1416 return request_tag(tag, repo, obj, obj_fd);