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 static const struct got_error *
320 open_mini_commit(struct got_commit_object_mini **commit,
321 struct got_repository *repo, struct got_object *obj)
323 const struct got_error *err = NULL;
327 if (obj->type != GOT_OBJ_TYPE_COMMIT)
328 return got_error(GOT_ERR_OBJ_TYPE);
330 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
331 struct got_pack *pack;
332 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
334 err = got_repo_cache_pack(&pack, repo,
335 obj->path_packfile, NULL);
339 err = got_object_read_packed_mini_commit_privsep(commit, obj,
343 err = open_loose_object(&fd, obj, repo);
346 err = got_object_read_mini_commit_privsep(commit, obj, fd,
354 const struct got_error *
355 got_object_open_as_commit(struct got_commit_object **commit,
356 struct got_repository *repo, struct got_object_id *id)
358 const struct got_error *err;
359 struct got_object *obj;
361 *commit = got_repo_get_cached_commit(repo, id);
362 if (*commit != NULL) {
367 err = got_object_open(&obj, repo, id);
370 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
371 err = got_error(GOT_ERR_OBJ_TYPE);
375 err = open_commit(commit, repo, obj, 0);
377 got_object_close(obj);
381 const struct got_error *
382 got_object_commit_open(struct got_commit_object **commit,
383 struct got_repository *repo, struct got_object *obj)
385 return open_commit(commit, repo, obj, 1);
388 const struct got_error *
389 got_object_open_mini_commit(struct got_commit_object_mini **commit,
390 struct got_repository *repo, struct got_object_id *id)
392 const struct got_error *err;
393 struct got_object *obj;
395 err = got_object_open(&obj, repo, id);
398 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
399 err = got_error(GOT_ERR_OBJ_TYPE);
403 err = open_mini_commit(commit, repo, obj);
405 got_object_close(obj);
409 const struct got_error *
410 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
412 const struct got_error *err = NULL;
414 *qid = calloc(1, sizeof(**qid));
416 return got_error_from_errno();
418 (*qid)->id = got_object_id_dup(id);
419 if ((*qid)->id == NULL) {
420 err = got_error_from_errno();
421 got_object_qid_free(*qid);
429 static const struct got_error *
430 open_tree(struct got_tree_object **tree,
431 struct got_repository *repo, struct got_object *obj, int check_cache)
433 const struct got_error *err = NULL;
436 *tree = got_repo_get_cached_tree(repo, &obj->id);
444 if (obj->type != GOT_OBJ_TYPE_TREE)
445 return got_error(GOT_ERR_OBJ_TYPE);
447 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
448 struct got_pack *pack;
449 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
451 err = got_repo_cache_pack(&pack, repo,
452 obj->path_packfile, NULL);
456 err = got_object_read_packed_tree_privsep(tree, obj, pack);
459 err = open_loose_object(&fd, obj, repo);
462 err = got_object_read_tree_privsep(tree, obj, fd, repo);
468 err = got_repo_cache_tree(repo, &obj->id, *tree);
474 const struct got_error *
475 got_object_open_as_tree(struct got_tree_object **tree,
476 struct got_repository *repo, struct got_object_id *id)
478 const struct got_error *err;
479 struct got_object *obj;
481 *tree = got_repo_get_cached_tree(repo, id);
487 err = got_object_open(&obj, repo, id);
490 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
491 err = got_error(GOT_ERR_OBJ_TYPE);
495 err = open_tree(tree, repo, obj, 0);
497 got_object_close(obj);
501 const struct got_error *
502 got_object_tree_open(struct got_tree_object **tree,
503 struct got_repository *repo, struct got_object *obj)
505 return open_tree(tree, repo, obj, 1);
508 const struct got_tree_entries *
509 got_object_tree_get_entries(struct got_tree_object *tree)
511 return &tree->entries;
514 static const struct got_error *
515 read_packed_blob_privsep(size_t *size, int outfd, struct got_object *obj,
516 struct got_pack *pack)
518 const struct got_error *err = NULL;
520 int basefd, accumfd; /* temporary files for delta application */
522 basefd = got_opentempfd();
524 return got_error_from_errno();
525 accumfd = got_opentempfd();
527 return got_error_from_errno();
529 outfd_child = dup(outfd);
530 if (outfd_child == -1)
531 return got_error_from_errno();
533 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
537 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
543 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
552 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
560 err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
564 if (lseek(outfd, SEEK_SET, 0) == -1)
565 err = got_error_from_errno();
570 const struct got_error *
571 got_object_blob_open(struct got_blob_object **blob,
572 struct got_repository *repo, struct got_object *obj, size_t blocksize)
574 const struct got_error *err = NULL;
579 if (obj->type != GOT_OBJ_TYPE_BLOB)
580 return got_error(GOT_ERR_OBJ_TYPE);
582 if (blocksize < obj->hdrlen)
583 return got_error(GOT_ERR_NO_SPACE);
585 *blob = calloc(1, sizeof(**blob));
587 return got_error_from_errno();
589 outfd = got_opentempfd();
591 return got_error_from_errno();
593 (*blob)->read_buf = malloc(blocksize);
594 if ((*blob)->read_buf == NULL) {
595 err = got_error_from_errno();
598 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
599 struct got_pack *pack;
600 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
602 err = got_repo_cache_pack(&pack, repo,
603 obj->path_packfile, NULL);
607 err = read_packed_blob_privsep(&size, outfd, obj, pack);
614 err = open_loose_object(&infd, obj, repo);
618 err = got_object_read_blob_privsep(&size, outfd, infd, repo);
623 if (size != obj->hdrlen + obj->size) {
624 err = got_error(GOT_ERR_PRIVSEP_LEN);
629 if (fstat(outfd, &sb) == -1) {
630 err = got_error_from_errno();
634 if (sb.st_size != obj->hdrlen + obj->size) {
635 err = got_error(GOT_ERR_PRIVSEP_LEN);
639 (*blob)->f = fdopen(outfd, "rb");
640 if ((*blob)->f == NULL) {
641 err = got_error_from_errno();
646 (*blob)->hdrlen = obj->hdrlen;
647 (*blob)->blocksize = blocksize;
648 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
655 free((*blob)->read_buf);
658 } else if (outfd != -1)
664 const struct got_error *
665 got_object_open_as_blob(struct got_blob_object **blob,
666 struct got_repository *repo, struct got_object_id *id,
669 const struct got_error *err;
670 struct got_object *obj;
674 err = got_object_open(&obj, repo, id);
677 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
678 err = got_error(GOT_ERR_OBJ_TYPE);
682 err = got_object_blob_open(blob, repo, obj, blocksize);
684 got_object_close(obj);
689 got_object_blob_close(struct got_blob_object *blob)
691 free(blob->read_buf);
697 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
699 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
703 got_object_blob_get_hdrlen(struct got_blob_object *blob)
709 got_object_blob_get_read_buf(struct got_blob_object *blob)
711 return blob->read_buf;
714 const struct got_error *
715 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
719 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
720 if (n == 0 && ferror(blob->f))
721 return got_ferror(blob->f, GOT_ERR_IO);
726 const struct got_error *
727 got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
728 FILE *outfile, struct got_blob_object *blob)
730 const struct got_error *err = NULL;
740 hdrlen = got_object_blob_get_hdrlen(blob);
742 err = got_object_blob_read_block(&len, blob);
749 buf = got_object_blob_get_read_buf(blob);
751 for (i = 0; i < len; i++) {
756 /* Skip blob object header first time around. */
757 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
767 static struct got_tree_entry *
768 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
770 struct got_tree_entry *te;
772 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
773 if (strncmp(te->name, name, len) == 0 && te->name[len] == '\0')
779 const struct got_error *
780 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
781 struct got_object_id *commit_id, const char *path)
783 const struct got_error *err = NULL;
784 struct got_commit_object *commit = NULL;
785 struct got_tree_object *tree = NULL;
786 struct got_tree_entry *te = NULL;
788 size_t seglen, len = strlen(path);
792 /* We are expecting an absolute in-repository path. */
794 return got_error(GOT_ERR_NOT_ABSPATH);
796 err = got_object_open_as_commit(&commit, repo, commit_id);
800 /* Handle opening of root of commit's tree. */
801 if (path[1] == '\0') {
802 *id = got_object_id_dup(commit->tree_id);
804 err = got_error_from_errno();
808 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
813 s++; /* skip leading '/' */
818 struct got_tree_object *next_tree;
828 te = find_entry_by_name(tree, seg, seglen);
830 err = got_error(GOT_ERR_NO_OBJ);
842 err = got_object_open_as_tree(&next_tree, repo,
847 got_object_tree_close(tree);
853 *id = got_object_id_dup(te->id);
855 return got_error_from_errno();
857 err = got_error(GOT_ERR_NO_OBJ);
860 got_object_commit_close(commit);
862 got_object_tree_close(tree);
866 const struct got_error *
867 got_object_tree_path_changed(int *changed,
868 struct got_tree_object *tree01, struct got_tree_object *tree02,
869 const char *path, struct got_repository *repo)
871 const struct got_error *err = NULL;
872 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
873 struct got_tree_entry *te1 = NULL, *te2 = NULL;
875 size_t seglen, len = strlen(path);
879 /* We are expecting an absolute in-repository path. */
881 return got_error(GOT_ERR_NOT_ABSPATH);
883 /* We not do support comparing the root path. */
885 return got_error(GOT_ERR_BAD_PATH);
890 s++; /* skip leading '/' */
895 struct got_tree_object *next_tree1, *next_tree2;
905 te1 = find_entry_by_name(tree1, seg, seglen);
907 err = got_error(GOT_ERR_NO_OBJ);
911 te2 = find_entry_by_name(tree2, seg, seglen);
917 if (te1->mode != te2->mode) {
922 if (got_object_id_cmp(te1->id, te2->id) == 0) {
927 if (len == 0) { /* final path element */
937 err = got_object_open_as_tree(&next_tree1, repo,
943 got_object_tree_close(tree1);
946 err = got_object_open_as_tree(&next_tree2, repo,
952 got_object_tree_close(tree2);
957 if (tree1 && tree1 != tree01)
958 got_object_tree_close(tree1);
959 if (tree2 && tree2 != tree02)
960 got_object_tree_close(tree2);
965 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
969 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
970 fprintf(stderr, "%s: %s\n", getprogname(),
974 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
975 fprintf(stderr, "%s: %s\n", getprogname(),
980 if (execl(path, path, repo_path, (char *)NULL) == -1) {
981 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
987 static const struct got_error *
988 request_object(struct got_object **obj, struct got_repository *repo, int fd)
990 const struct got_error *err = NULL;
991 struct imsgbuf *ibuf;
993 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
995 err = got_privsep_send_obj_req(ibuf, fd, NULL);
999 return got_privsep_recv_obj(obj, ibuf);
1002 const struct got_error *
1003 got_object_read_header_privsep(struct got_object **obj,
1004 struct got_repository *repo, int obj_fd)
1008 struct imsgbuf *ibuf;
1010 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
1011 return request_object(obj, repo, obj_fd);
1013 ibuf = calloc(1, sizeof(*ibuf));
1015 return got_error_from_errno();
1017 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1018 return got_error_from_errno();
1022 return got_error_from_errno();
1023 else if (pid == 0) {
1024 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
1030 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
1032 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
1033 imsg_init(ibuf, imsg_fds[0]);
1034 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
1036 return request_object(obj, repo, obj_fd);
1039 static const struct got_error *
1040 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
1041 struct got_object_id *id)
1043 const struct got_error *err = NULL;
1044 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1046 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
1050 err = got_privsep_recv_obj(obj, ibuf);
1054 (*obj)->path_packfile = strdup(pack->path_packfile);
1055 if ((*obj)->path_packfile == NULL) {
1056 err = got_error_from_errno();
1059 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1064 const struct got_error *
1065 got_object_packed_read_privsep(struct got_object **obj,
1066 struct got_repository *repo, struct got_pack *pack,
1067 struct got_packidx *packidx, int idx, struct got_object_id *id)
1069 const struct got_error *err = NULL;
1072 struct imsgbuf *ibuf;
1074 if (pack->privsep_child)
1075 return request_packed_object(obj, pack, idx, id);
1077 ibuf = calloc(1, sizeof(*ibuf));
1079 return got_error_from_errno();
1081 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
1082 if (pack->privsep_child == NULL) {
1083 err = got_error_from_errno();
1088 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1089 err = got_error_from_errno();
1095 err = got_error_from_errno();
1097 } else if (pid == 0) {
1098 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
1099 pack->path_packfile);
1104 pack->privsep_child->imsg_fd = imsg_fds[0];
1105 pack->privsep_child->pid = pid;
1106 imsg_init(ibuf, imsg_fds[0]);
1107 pack->privsep_child->ibuf = ibuf;
1109 err = got_privsep_init_pack_child(ibuf, pack, packidx);
1111 const struct got_error *child_err;
1112 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
1113 child_err = got_privsep_wait_for_child(
1114 pack->privsep_child->pid);
1115 if (child_err && err == NULL)
1118 free(pack->privsep_child);
1119 pack->privsep_child = NULL;
1126 free(pack->privsep_child);
1127 pack->privsep_child = NULL;
1129 err = request_packed_object(obj, pack, idx, id);
1133 static const struct got_error *
1134 request_commit(struct got_commit_object **commit, struct got_repository *repo,
1135 struct got_object *obj, int fd)
1137 const struct got_error *err = NULL;
1138 struct imsgbuf *ibuf;
1140 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
1142 err = got_privsep_send_obj_req(ibuf, fd, obj);
1146 return got_privsep_recv_commit(commit, ibuf);
1149 static const struct got_error *
1150 request_mini_commit(struct got_commit_object_mini **commit,
1151 struct got_repository *repo, struct got_object *obj, int fd)
1153 const struct got_error *err = NULL;
1154 struct imsgbuf *ibuf;
1156 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
1158 err = got_privsep_send_mini_commit_req(ibuf, fd, obj);
1162 return got_privsep_recv_mini_commit(commit, ibuf);
1165 const struct got_error *
1166 got_object_read_packed_commit_privsep(struct got_commit_object **commit,
1167 struct got_object *obj, struct got_pack *pack)
1169 const struct got_error *err = NULL;
1171 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1175 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
1178 const struct got_error *
1179 got_object_read_packed_mini_commit_privsep(struct got_commit_object_mini **commit,
1180 struct got_object *obj, struct got_pack *pack)
1182 const struct got_error *err = NULL;
1184 err = got_privsep_send_mini_commit_req(pack->privsep_child->ibuf, -1,
1189 return got_privsep_recv_mini_commit(commit, pack->privsep_child->ibuf);
1192 const struct got_error *
1193 got_object_read_commit_privsep(struct got_commit_object **commit,
1194 struct got_object *obj, int obj_fd, struct got_repository *repo)
1198 struct imsgbuf *ibuf;
1200 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
1201 return request_commit(commit, repo, obj, obj_fd);
1203 ibuf = calloc(1, sizeof(*ibuf));
1205 return got_error_from_errno();
1207 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1208 return got_error_from_errno();
1212 return got_error_from_errno();
1213 else if (pid == 0) {
1214 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
1220 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
1222 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
1223 imsg_init(ibuf, imsg_fds[0]);
1224 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
1226 return request_commit(commit, repo, obj, obj_fd);
1229 const struct got_error *
1230 got_object_read_mini_commit_privsep(struct got_commit_object_mini **commit,
1231 struct got_object *obj, int obj_fd, struct got_repository *repo)
1235 struct imsgbuf *ibuf;
1237 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
1238 return request_mini_commit(commit, repo, obj, obj_fd);
1240 ibuf = calloc(1, sizeof(*ibuf));
1242 return got_error_from_errno();
1244 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1245 return got_error_from_errno();
1249 return got_error_from_errno();
1250 else if (pid == 0) {
1251 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
1257 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
1259 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
1260 imsg_init(ibuf, imsg_fds[0]);
1261 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
1263 return request_mini_commit(commit, repo, obj, obj_fd);
1266 static const struct got_error *
1267 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1268 struct got_object *obj, int fd)
1270 const struct got_error *err = NULL;
1271 struct imsgbuf *ibuf;
1273 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1275 err = got_privsep_send_obj_req(ibuf, fd, obj);
1279 return got_privsep_recv_tree(tree, ibuf);
1282 const struct got_error *
1283 got_object_read_tree_privsep(struct got_tree_object **tree,
1284 struct got_object *obj, int obj_fd, struct got_repository *repo)
1288 struct imsgbuf *ibuf;
1290 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1291 return request_tree(tree, repo, obj, obj_fd);
1293 ibuf = calloc(1, sizeof(*ibuf));
1295 return got_error_from_errno();
1297 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1298 return got_error_from_errno();
1302 return got_error_from_errno();
1303 else if (pid == 0) {
1304 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1311 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1313 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1314 imsg_init(ibuf, imsg_fds[0]);
1315 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1318 return request_tree(tree, repo, obj, obj_fd);
1321 const struct got_error *
1322 got_object_read_packed_tree_privsep(struct got_tree_object **tree,
1323 struct got_object *obj, struct got_pack *pack)
1325 const struct got_error *err = NULL;
1327 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1331 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1334 static const struct got_error *
1335 request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
1337 const struct got_error *err = NULL;
1340 outfd_child = dup(outfd);
1341 if (outfd_child == -1)
1342 return got_error_from_errno();
1344 err = got_privsep_send_blob_req(ibuf, infd);
1348 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1354 err = got_privsep_recv_blob(size, ibuf);
1358 if (lseek(outfd, SEEK_SET, 0) == -1)
1359 return got_error_from_errno();
1364 const struct got_error *
1365 got_object_read_blob_privsep(size_t *size, int outfd, int infd,
1366 struct got_repository *repo)
1370 struct imsgbuf *ibuf;
1372 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1373 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1374 return request_blob(size, outfd, infd, ibuf);
1377 ibuf = calloc(1, sizeof(*ibuf));
1379 return got_error_from_errno();
1381 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1382 return got_error_from_errno();
1386 return got_error_from_errno();
1387 else if (pid == 0) {
1388 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1394 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1396 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1397 imsg_init(ibuf, imsg_fds[0]);
1398 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1400 return request_blob(size, outfd, infd, ibuf);