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);
182 exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
186 if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
187 fprintf(stderr, "%s: %s\n", getprogname(),
191 if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
192 fprintf(stderr, "%s: %s\n", getprogname(),
197 if (execl(path, path, repo_path, (char *)NULL) == -1) {
198 fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
204 static const struct got_error *
205 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
206 struct got_object_id *id)
208 const struct got_error *err = NULL;
209 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
211 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
215 err = got_privsep_recv_obj(obj, ibuf);
219 (*obj)->path_packfile = strdup(pack->path_packfile);
220 if ((*obj)->path_packfile == NULL) {
221 err = got_error_from_errno();
224 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
229 static const struct got_error *
230 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
232 const struct got_error *err = NULL;
235 struct imsgbuf *ibuf;
237 ibuf = calloc(1, sizeof(*ibuf));
239 return got_error_from_errno();
241 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
242 if (pack->privsep_child == NULL) {
243 err = got_error_from_errno();
248 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
249 err = got_error_from_errno();
255 err = got_error_from_errno();
257 } else if (pid == 0) {
258 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
259 pack->path_packfile);
264 pack->privsep_child->imsg_fd = imsg_fds[0];
265 pack->privsep_child->pid = pid;
266 imsg_init(ibuf, imsg_fds[0]);
267 pack->privsep_child->ibuf = ibuf;
269 err = got_privsep_init_pack_child(ibuf, pack, packidx);
271 const struct got_error *child_err;
272 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
273 child_err = got_privsep_wait_for_child(
274 pack->privsep_child->pid);
275 if (child_err && err == NULL)
281 free(pack->privsep_child);
282 pack->privsep_child = NULL;
287 static const struct got_error *
288 read_packed_object_privsep(struct got_object **obj,
289 struct got_repository *repo, struct got_pack *pack,
290 struct got_packidx *packidx, int idx, struct got_object_id *id)
292 const struct got_error *err = NULL;
294 if (pack->privsep_child)
295 return request_packed_object(obj, pack, idx, id);
297 err = start_pack_privsep_child(pack, packidx);
301 return request_packed_object(obj, pack, idx, id);
305 static const struct got_error *
306 open_packed_object(struct got_object **obj, struct got_object_id *id,
307 struct got_repository *repo)
309 const struct got_error *err = NULL;
310 struct got_pack *pack = NULL;
311 struct got_packidx *packidx = NULL;
315 err = got_repo_search_packidx(&packidx, &idx, repo, id);
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, packidx);
330 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
334 err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
340 const struct got_error *
341 got_object_open(struct got_object **obj, struct got_repository *repo,
342 struct got_object_id *id)
344 const struct got_error *err = NULL;
348 *obj = got_repo_get_cached_object(repo, id);
354 err = open_packed_object(obj, id, repo);
355 if (err && err->code != GOT_ERR_NO_OBJ)
359 return got_repo_cache_object(repo, id, *obj);
362 err = object_path(&path, id, repo);
366 fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
369 err = got_error_no_obj(id);
371 err = got_error_from_errno();
374 err = got_object_read_header_privsep(obj, repo, fd);
377 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
381 err = got_repo_cache_object(repo, id, *obj);
390 const struct got_error *
391 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
394 struct got_object_id id;
396 if (!got_parse_sha1_digest(id.sha1, id_str))
397 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
399 return got_object_open(obj, repo, &id);
402 const struct got_error *
403 got_object_resolve_id_str(struct got_object_id **id,
404 struct got_repository *repo, const char *id_str)
406 const struct got_error *err = NULL;
407 struct got_object *obj;
409 err = got_object_open_by_id_str(&obj, repo, id_str);
413 *id = got_object_id_dup(got_object_get_id(obj));
414 got_object_close(obj);
416 return got_error_from_errno();
421 static const struct got_error *
422 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
423 int pack_idx, struct got_object_id *id)
425 const struct got_error *err = NULL;
427 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
432 return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
435 static const struct got_error *
436 read_packed_commit_privsep(struct got_commit_object **commit,
437 struct got_pack *pack, struct got_packidx *packidx, int idx,
438 struct got_object_id *id)
440 const struct got_error *err = NULL;
442 if (pack->privsep_child)
443 return request_packed_commit(commit, pack, idx, id);
445 err = start_pack_privsep_child(pack, packidx);
449 return request_packed_commit(commit, pack, idx, id);
452 static const struct got_error *
453 open_commit(struct got_commit_object **commit,
454 struct got_repository *repo, struct got_object_id *id, int check_cache)
456 const struct got_error *err = NULL;
457 struct got_packidx *packidx = NULL;
462 *commit = got_repo_get_cached_commit(repo, id);
463 if (*commit != NULL) {
470 err = got_repo_search_packidx(&packidx, &idx, repo, id);
472 struct got_pack *pack = NULL;
474 err = get_packfile_path(&path_packfile, packidx);
478 pack = got_repo_get_cached_pack(repo, path_packfile);
480 err = got_repo_cache_pack(&pack, repo, path_packfile,
485 err = read_packed_commit_privsep(commit, pack,
487 } else if (err->code == GOT_ERR_NO_OBJ) {
490 err = open_loose_object(&fd, id, repo);
493 err = got_object_read_commit_privsep(commit, fd, repo);
499 err = got_repo_cache_commit(repo, id, *commit);
505 const struct got_error *
506 got_object_open_as_commit(struct got_commit_object **commit,
507 struct got_repository *repo, struct got_object_id *id)
509 *commit = got_repo_get_cached_commit(repo, id);
510 if (*commit != NULL) {
515 return open_commit(commit, repo, id, 0);
518 const struct got_error *
519 got_object_commit_open(struct got_commit_object **commit,
520 struct got_repository *repo, struct got_object *obj)
522 return open_commit(commit, repo, got_object_get_id(obj), 1);
525 const struct got_error *
526 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
528 const struct got_error *err = NULL;
530 *qid = calloc(1, sizeof(**qid));
532 return got_error_from_errno();
534 (*qid)->id = got_object_id_dup(id);
535 if ((*qid)->id == NULL) {
536 err = got_error_from_errno();
537 got_object_qid_free(*qid);
545 static const struct got_error *
546 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
547 int pack_idx, struct got_object_id *id)
549 const struct got_error *err = NULL;
551 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
556 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
559 static const struct got_error *
560 read_packed_tree_privsep(struct got_tree_object **tree,
561 struct got_pack *pack, struct got_packidx *packidx, int idx,
562 struct got_object_id *id)
564 const struct got_error *err = NULL;
566 if (pack->privsep_child)
567 return request_packed_tree(tree, pack, idx, id);
569 err = start_pack_privsep_child(pack, packidx);
573 return request_packed_tree(tree, pack, idx, id);
576 static const struct got_error *
577 open_tree(struct got_tree_object **tree, struct got_repository *repo,
578 struct got_object_id *id, int check_cache)
580 const struct got_error *err = NULL;
581 struct got_packidx *packidx = NULL;
586 *tree = got_repo_get_cached_tree(repo, id);
594 err = got_repo_search_packidx(&packidx, &idx, repo, id);
596 struct got_pack *pack = NULL;
598 err = get_packfile_path(&path_packfile, packidx);
602 pack = got_repo_get_cached_pack(repo, path_packfile);
604 err = got_repo_cache_pack(&pack, repo, path_packfile,
609 err = read_packed_tree_privsep(tree, pack,
611 } else if (err->code == GOT_ERR_NO_OBJ) {
614 err = open_loose_object(&fd, id, repo);
617 err = got_object_read_tree_privsep(tree, fd, repo);
623 err = got_repo_cache_tree(repo, id, *tree);
629 const struct got_error *
630 got_object_open_as_tree(struct got_tree_object **tree,
631 struct got_repository *repo, struct got_object_id *id)
633 *tree = got_repo_get_cached_tree(repo, id);
639 return open_tree(tree, repo, id, 0);
642 const struct got_error *
643 got_object_tree_open(struct got_tree_object **tree,
644 struct got_repository *repo, struct got_object *obj)
646 return open_tree(tree, repo, got_object_get_id(obj), 1);
649 const struct got_tree_entries *
650 got_object_tree_get_entries(struct got_tree_object *tree)
652 return &tree->entries;
655 static const struct got_error *
656 read_packed_blob_privsep(size_t *size, int outfd, struct got_object *obj,
657 struct got_pack *pack)
659 const struct got_error *err = NULL;
661 int basefd, accumfd; /* temporary files for delta application */
663 basefd = got_opentempfd();
665 return got_error_from_errno();
666 accumfd = got_opentempfd();
668 return got_error_from_errno();
670 outfd_child = dup(outfd);
671 if (outfd_child == -1)
672 return got_error_from_errno();
674 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
678 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
684 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
693 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
701 err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
705 if (lseek(outfd, SEEK_SET, 0) == -1)
706 err = got_error_from_errno();
711 const struct got_error *
712 got_object_blob_open(struct got_blob_object **blob,
713 struct got_repository *repo, struct got_object *obj, size_t blocksize)
715 const struct got_error *err = NULL;
720 if (obj->type != GOT_OBJ_TYPE_BLOB)
721 return got_error(GOT_ERR_OBJ_TYPE);
723 if (blocksize < obj->hdrlen)
724 return got_error(GOT_ERR_NO_SPACE);
726 *blob = calloc(1, sizeof(**blob));
728 return got_error_from_errno();
730 outfd = got_opentempfd();
732 return got_error_from_errno();
734 (*blob)->read_buf = malloc(blocksize);
735 if ((*blob)->read_buf == NULL) {
736 err = got_error_from_errno();
739 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
740 struct got_pack *pack;
741 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
743 err = got_repo_cache_pack(&pack, repo,
744 obj->path_packfile, NULL);
748 err = read_packed_blob_privsep(&size, outfd, obj, pack);
755 err = open_loose_object(&infd, got_object_get_id(obj), repo);
759 err = got_object_read_blob_privsep(&size, outfd, infd, repo);
764 if (size != obj->hdrlen + obj->size) {
765 err = got_error(GOT_ERR_PRIVSEP_LEN);
770 if (fstat(outfd, &sb) == -1) {
771 err = got_error_from_errno();
775 if (sb.st_size != obj->hdrlen + obj->size) {
776 err = got_error(GOT_ERR_PRIVSEP_LEN);
780 (*blob)->f = fdopen(outfd, "rb");
781 if ((*blob)->f == NULL) {
782 err = got_error_from_errno();
787 (*blob)->hdrlen = obj->hdrlen;
788 (*blob)->blocksize = blocksize;
789 memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
796 free((*blob)->read_buf);
799 } else if (outfd != -1)
805 const struct got_error *
806 got_object_open_as_blob(struct got_blob_object **blob,
807 struct got_repository *repo, struct got_object_id *id,
810 const struct got_error *err;
811 struct got_object *obj;
815 err = got_object_open(&obj, repo, id);
818 if (obj->type != GOT_OBJ_TYPE_BLOB) {
819 err = got_error(GOT_ERR_OBJ_TYPE);
823 err = got_object_blob_open(blob, repo, obj, blocksize);
825 got_object_close(obj);
830 got_object_blob_close(struct got_blob_object *blob)
832 free(blob->read_buf);
838 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
840 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
844 got_object_blob_get_hdrlen(struct got_blob_object *blob)
850 got_object_blob_get_read_buf(struct got_blob_object *blob)
852 return blob->read_buf;
855 const struct got_error *
856 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
860 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
861 if (n == 0 && ferror(blob->f))
862 return got_ferror(blob->f, GOT_ERR_IO);
867 const struct got_error *
868 got_object_blob_dump_to_file(size_t *total_len, int *nlines,
869 FILE *outfile, struct got_blob_object *blob)
871 const struct got_error *err = NULL;
881 hdrlen = got_object_blob_get_hdrlen(blob);
883 err = got_object_blob_read_block(&len, blob);
890 buf = got_object_blob_get_read_buf(blob);
892 for (i = 0; i < len; i++) {
897 /* Skip blob object header first time around. */
898 fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
908 static const struct got_error *
909 read_packed_tag_privsep(struct got_tag_object **tag,
910 struct got_object *obj, struct got_pack *pack)
912 const struct got_error *err = NULL;
914 err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
918 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
922 static const struct got_error *
923 open_tag(struct got_tag_object **tag,
924 struct got_repository *repo, struct got_object *obj, int check_cache)
926 const struct got_error *err = NULL;
929 *tag = got_repo_get_cached_tag(repo, &obj->id);
937 if (obj->type != GOT_OBJ_TYPE_TAG)
938 return got_error(GOT_ERR_OBJ_TYPE);
940 if (obj->flags & GOT_OBJ_FLAG_PACKED) {
941 struct got_pack *pack;
942 pack = got_repo_get_cached_pack(repo, obj->path_packfile);
944 err = got_repo_cache_pack(&pack, repo,
945 obj->path_packfile, NULL);
949 err = read_packed_tag_privsep(tag, obj, pack);
952 err = open_loose_object(&fd, got_object_get_id(obj), repo);
955 err = got_object_read_tag_privsep(tag, obj, fd, repo);
961 err = got_repo_cache_tag(repo, &obj->id, *tag);
967 const struct got_error *
968 got_object_open_as_tag(struct got_tag_object **tag,
969 struct got_repository *repo, struct got_object_id *id)
971 const struct got_error *err;
972 struct got_object *obj;
974 *tag = got_repo_get_cached_tag(repo, id);
980 err = got_object_open(&obj, repo, id);
983 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
984 err = got_error(GOT_ERR_OBJ_TYPE);
988 err = open_tag(tag, repo, obj, 0);
990 got_object_close(obj);
994 const struct got_error *
995 got_object_tag_open(struct got_tag_object **tag,
996 struct got_repository *repo, struct got_object *obj)
998 return open_tag(tag, repo, obj, 1);
1001 static struct got_tree_entry *
1002 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1004 struct got_tree_entry *te;
1006 /* Note that tree entries are sorted in strncmp() order. */
1007 SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1008 int cmp = strncmp(te->name, name, len);
1013 if (te->name[len] == '\0')
1019 const struct got_error *
1020 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1021 struct got_object_id *commit_id, const char *path)
1023 const struct got_error *err = NULL;
1024 struct got_commit_object *commit = NULL;
1025 struct got_tree_object *tree = NULL;
1026 struct got_tree_entry *te = NULL;
1027 const char *seg, *s;
1032 /* We are expecting an absolute in-repository path. */
1034 return got_error(GOT_ERR_NOT_ABSPATH);
1036 err = got_object_open_as_commit(&commit, repo, commit_id);
1040 /* Handle opening of root of commit's tree. */
1041 if (path[1] == '\0') {
1042 *id = got_object_id_dup(commit->tree_id);
1044 err = got_error_from_errno();
1048 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1053 s++; /* skip leading '/' */
1057 struct got_tree_object *next_tree;
1066 te = find_entry_by_name(tree, seg, seglen);
1068 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1079 err = got_object_open_as_tree(&next_tree, repo,
1084 got_object_tree_close(tree);
1090 *id = got_object_id_dup(te->id);
1092 return got_error_from_errno();
1094 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1097 got_object_commit_close(commit);
1099 got_object_tree_close(tree);
1103 const struct got_error *
1104 got_object_tree_path_changed(int *changed,
1105 struct got_tree_object *tree01, struct got_tree_object *tree02,
1106 const char *path, struct got_repository *repo)
1108 const struct got_error *err = NULL;
1109 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1110 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1111 const char *seg, *s;
1116 /* We are expecting an absolute in-repository path. */
1118 return got_error(GOT_ERR_NOT_ABSPATH);
1120 /* We not do support comparing the root path. */
1121 if (path[1] == '\0')
1122 return got_error(GOT_ERR_BAD_PATH);
1127 s++; /* skip leading '/' */
1131 struct got_tree_object *next_tree1, *next_tree2;
1140 te1 = find_entry_by_name(tree1, seg, seglen);
1142 err = got_error(GOT_ERR_NO_OBJ);
1146 te2 = find_entry_by_name(tree2, seg, seglen);
1152 if (te1->mode != te2->mode) {
1157 if (got_object_id_cmp(te1->id, te2->id) == 0) {
1162 if (*s == '\0') { /* final path element */
1171 err = got_object_open_as_tree(&next_tree1, repo,
1176 if (tree1 != tree01)
1177 got_object_tree_close(tree1);
1180 err = got_object_open_as_tree(&next_tree2, repo,
1185 if (tree2 != tree02)
1186 got_object_tree_close(tree2);
1191 if (tree1 && tree1 != tree01)
1192 got_object_tree_close(tree1);
1193 if (tree2 && tree2 != tree02)
1194 got_object_tree_close(tree2);
1198 static const struct got_error *
1199 request_object(struct got_object **obj, struct got_repository *repo, int fd)
1201 const struct got_error *err = NULL;
1202 struct imsgbuf *ibuf;
1204 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
1206 err = got_privsep_send_obj_req(ibuf, fd, NULL);
1210 return got_privsep_recv_obj(obj, ibuf);
1213 const struct got_error *
1214 got_object_read_header_privsep(struct got_object **obj,
1215 struct got_repository *repo, int obj_fd)
1219 struct imsgbuf *ibuf;
1221 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
1222 return request_object(obj, repo, obj_fd);
1224 ibuf = calloc(1, sizeof(*ibuf));
1226 return got_error_from_errno();
1228 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1229 return got_error_from_errno();
1233 return got_error_from_errno();
1234 else if (pid == 0) {
1235 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
1241 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
1243 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
1244 imsg_init(ibuf, imsg_fds[0]);
1245 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
1247 return request_object(obj, repo, obj_fd);
1250 static const struct got_error *
1251 request_commit(struct got_commit_object **commit, struct got_repository *repo,
1254 const struct got_error *err = NULL;
1255 struct imsgbuf *ibuf;
1257 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
1259 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
1263 return got_privsep_recv_commit(commit, ibuf);
1266 const struct got_error *
1267 got_object_read_commit_privsep(struct got_commit_object **commit,
1268 int obj_fd, struct got_repository *repo)
1272 struct imsgbuf *ibuf;
1274 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
1275 return request_commit(commit, repo, obj_fd);
1277 ibuf = calloc(1, sizeof(*ibuf));
1279 return got_error_from_errno();
1281 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1282 return got_error_from_errno();
1286 return got_error_from_errno();
1287 else if (pid == 0) {
1288 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
1294 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
1296 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
1297 imsg_init(ibuf, imsg_fds[0]);
1298 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
1300 return request_commit(commit, repo, obj_fd);
1303 static const struct got_error *
1304 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1307 const struct got_error *err = NULL;
1308 struct imsgbuf *ibuf;
1310 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1312 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
1316 return got_privsep_recv_tree(tree, ibuf);
1319 const struct got_error *
1320 got_object_read_tree_privsep(struct got_tree_object **tree,
1321 int obj_fd, struct got_repository *repo)
1325 struct imsgbuf *ibuf;
1327 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1328 return request_tree(tree, repo, obj_fd);
1330 ibuf = calloc(1, sizeof(*ibuf));
1332 return got_error_from_errno();
1334 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1335 return got_error_from_errno();
1339 return got_error_from_errno();
1340 else if (pid == 0) {
1341 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1348 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1350 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1351 imsg_init(ibuf, imsg_fds[0]);
1352 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1355 return request_tree(tree, repo, obj_fd);
1358 static const struct got_error *
1359 request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
1361 const struct got_error *err = NULL;
1364 outfd_child = dup(outfd);
1365 if (outfd_child == -1)
1366 return got_error_from_errno();
1368 err = got_privsep_send_blob_req(ibuf, infd);
1372 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1378 err = got_privsep_recv_blob(size, ibuf);
1382 if (lseek(outfd, SEEK_SET, 0) == -1)
1383 return got_error_from_errno();
1388 const struct got_error *
1389 got_object_read_blob_privsep(size_t *size, int outfd, int infd,
1390 struct got_repository *repo)
1394 struct imsgbuf *ibuf;
1396 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1397 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1398 return request_blob(size, outfd, infd, ibuf);
1401 ibuf = calloc(1, sizeof(*ibuf));
1403 return got_error_from_errno();
1405 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1406 return got_error_from_errno();
1410 return got_error_from_errno();
1411 else if (pid == 0) {
1412 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1418 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1420 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1421 imsg_init(ibuf, imsg_fds[0]);
1422 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1424 return request_blob(size, outfd, infd, ibuf);
1427 static const struct got_error *
1428 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1429 struct got_object *obj, int fd)
1431 const struct got_error *err = NULL;
1432 struct imsgbuf *ibuf;
1434 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1436 err = got_privsep_send_obj_req(ibuf, fd, obj);
1440 return got_privsep_recv_tag(tag, ibuf);
1443 const struct got_error *
1444 got_object_read_tag_privsep(struct got_tag_object **tag,
1445 struct got_object *obj, int obj_fd, struct got_repository *repo)
1449 struct imsgbuf *ibuf;
1451 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1452 return request_tag(tag, repo, obj, obj_fd);
1454 ibuf = calloc(1, sizeof(*ibuf));
1456 return got_error_from_errno();
1458 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1459 return got_error_from_errno();
1463 return got_error_from_errno();
1464 else if (pid == 0) {
1465 exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1471 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1473 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1474 imsg_init(ibuf, imsg_fds[0]);
1475 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1477 return request_tag(tag, repo, obj, obj_fd);