2 * Copyright (c) 2018, 2019, 2022 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.
18 #include <sys/queue.h>
19 #include <sys/types.h>
22 #include <sys/socket.h>
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_repository.h"
39 #include "got_opentemp.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_object.h"
44 #include "got_lib_privsep.h"
45 #include "got_lib_object_cache.h"
46 #include "got_lib_pack.h"
47 #include "got_lib_repository.h"
49 static const struct got_error *
50 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
51 struct got_object_id *id)
53 const struct got_error *err = NULL;
54 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
56 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
60 err = got_privsep_recv_obj(obj, ibuf);
64 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
69 /* Create temporary files used during delta application. */
70 static const struct got_error *
71 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
73 const struct got_error *err;
74 int basefd = -1, accumfd = -1;
77 * For performance reasons, the child will keep reusing the
78 * same temporary files during every object request.
79 * Opening and closing new files for every object request is
80 * too expensive during operations such as 'gotadmin pack'.
82 if (pack->child_has_tempfiles)
85 basefd = dup(pack->basefd);
87 return got_error_from_errno("dup");
89 accumfd = dup(pack->accumfd);
91 err = got_error_from_errno("dup");
95 err = got_privsep_send_tmpfd(ibuf, basefd);
99 err = got_privsep_send_tmpfd(ibuf, accumfd);
107 pack->child_has_tempfiles = 1;
111 static const struct got_error *
112 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
113 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
115 const struct got_error *err = NULL;
116 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
119 err = pack_child_send_tempfiles(ibuf, pack);
123 outfd_child = dup(outfd);
124 if (outfd_child == -1)
125 return got_error_from_errno("dup");
127 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
133 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
137 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
144 static const struct got_error *
145 read_packed_object_privsep(struct got_object **obj,
146 struct got_repository *repo, struct got_pack *pack,
147 struct got_packidx *packidx, int idx, struct got_object_id *id)
149 const struct got_error *err = NULL;
151 if (pack->privsep_child == NULL) {
152 err = got_pack_start_privsep_child(pack, packidx);
157 return request_packed_object(obj, pack, idx, id);
160 static const struct got_error *
161 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
162 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
163 struct got_object_id *id)
165 const struct got_error *err = NULL;
167 if (pack->privsep_child == NULL) {
168 err = got_pack_start_privsep_child(pack, packidx);
173 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
177 const struct got_error *
178 got_object_open_packed(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 = got_packidx_get_packfile_path(&path_packfile,
192 packidx->path_packidx);
196 pack = got_repo_get_cached_pack(repo, path_packfile);
198 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
203 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
211 const struct got_error *
212 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
213 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
214 struct got_repository *repo)
216 return read_packed_object_privsep(obj, repo, pack, packidx,
220 const struct got_error *
221 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
222 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
223 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
224 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
225 struct got_repository *repo)
227 const struct got_error *err = NULL;
228 struct got_pack *pack = NULL;
234 *delta_compressed_size = 0;
236 *delta_out_offset = 0;
238 err = got_packidx_get_packfile_path(&path_packfile,
239 packidx->path_packidx);
243 pack = got_repo_get_cached_pack(repo, path_packfile);
245 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
250 if (pack->privsep_child == NULL) {
251 err = got_pack_start_privsep_child(pack, packidx);
256 if (!pack->child_has_delta_outfd) {
258 outfd_child = dup(delta_cache_fd);
259 if (outfd_child == -1)
260 return got_error_from_errno("dup");
261 err = got_privsep_send_raw_delta_outfd(
262 pack->privsep_child->ibuf, outfd_child);
265 pack->child_has_delta_outfd = 1;
268 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
273 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
274 delta_compressed_size, delta_offset, delta_out_offset, base_id,
275 pack->privsep_child->ibuf);
278 static const struct got_error *
279 request_object(struct got_object **obj, struct got_object_id *id,
280 struct got_repository *repo, int fd)
282 const struct got_error *err = NULL;
283 struct imsgbuf *ibuf;
285 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
287 err = got_privsep_send_obj_req(ibuf, fd, id);
291 return got_privsep_recv_obj(obj, ibuf);
294 static const struct got_error *
295 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
296 struct got_object_id *id, struct got_repository *repo, int infd)
298 const struct got_error *err = NULL;
299 struct imsgbuf *ibuf;
302 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
304 outfd_child = dup(outfd);
305 if (outfd_child == -1)
306 return got_error_from_errno("dup");
308 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
312 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
316 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
319 static const struct got_error *
320 start_child(struct got_repository *repo, int type)
322 const struct got_error *err = NULL;
325 struct imsgbuf *ibuf;
326 const char *prog_path;
329 case GOT_REPO_PRIVSEP_CHILD_OBJECT:
330 prog_path = GOT_PATH_PROG_READ_OBJECT;
332 case GOT_REPO_PRIVSEP_CHILD_TREE:
333 prog_path = GOT_PATH_PROG_READ_TREE;
335 case GOT_REPO_PRIVSEP_CHILD_COMMIT:
336 prog_path = GOT_PATH_PROG_READ_COMMIT;
338 case GOT_REPO_PRIVSEP_CHILD_BLOB:
339 prog_path = GOT_PATH_PROG_READ_BLOB;
341 case GOT_REPO_PRIVSEP_CHILD_TAG:
342 prog_path = GOT_PATH_PROG_READ_TAG;
345 return got_error(GOT_ERR_OBJ_TYPE);
348 ibuf = calloc(1, sizeof(*ibuf));
350 return got_error_from_errno("calloc");
352 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
353 err = got_error_from_errno("socketpair");
360 err = got_error_from_errno("fork");
365 got_privsep_exec_child(imsg_fds, prog_path, repo->path);
369 if (close(imsg_fds[1]) == -1) {
370 err = got_error_from_errno("close");
375 repo->privsep_children[type].imsg_fd = imsg_fds[0];
376 repo->privsep_children[type].pid = pid;
377 imsg_init(ibuf, imsg_fds[0]);
378 repo->privsep_children[type].ibuf = ibuf;
383 const struct got_error *
384 got_object_read_header_privsep(struct got_object **obj,
385 struct got_object_id *id, struct got_repository *repo, int obj_fd)
387 const struct got_error *err;
389 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
390 return request_object(obj, id, repo, obj_fd);
392 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_OBJECT);
396 return request_object(obj, id, repo, obj_fd);
399 static const struct got_error *
400 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
401 int outfd, struct got_object_id *id, struct got_repository *repo,
404 const struct got_error *err;
406 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
407 return request_raw_object(outbuf, size, hdrlen, outfd, id,
410 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_OBJECT);
414 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
418 const struct got_error *
419 got_object_open(struct got_object **obj, struct got_repository *repo,
420 struct got_object_id *id)
422 const struct got_error *err = NULL;
425 *obj = got_repo_get_cached_object(repo, id);
431 err = got_object_open_packed(obj, id, repo);
432 if (err && err->code != GOT_ERR_NO_OBJ)
436 return got_repo_cache_object(repo, id, *obj);
439 err = got_object_open_loose_fd(&fd, id, repo);
441 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
442 err = got_error_no_obj(id);
446 err = got_object_read_header_privsep(obj, id, repo, fd);
450 memcpy(&(*obj)->id, id, sizeof(*id));
453 return got_repo_cache_object(repo, id, *obj);
456 /* *outfd must be initialized to -1 by caller */
457 const struct got_error *
458 got_object_raw_open(struct got_raw_object **obj, int *outfd,
459 struct got_repository *repo, struct got_object_id *id)
461 const struct got_error *err = NULL;
462 struct got_packidx *packidx = NULL;
464 uint8_t *outbuf = NULL;
467 char *path_packfile = NULL;
469 *obj = got_repo_get_cached_raw_object(repo, id);
476 *outfd = got_opentempfd();
478 return got_error_from_errno("got_opentempfd");
481 err = got_repo_search_packidx(&packidx, &idx, repo, id);
483 struct got_pack *pack = NULL;
485 err = got_packidx_get_packfile_path(&path_packfile,
486 packidx->path_packidx);
490 pack = got_repo_get_cached_pack(repo, path_packfile);
492 err = got_repo_cache_pack(&pack, repo, path_packfile,
497 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
498 *outfd, pack, packidx, idx, id);
501 } else if (err->code == GOT_ERR_NO_OBJ) {
504 err = got_object_open_loose_fd(&fd, id, repo);
507 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
513 err = got_object_raw_alloc(obj, outbuf, outfd,
514 GOT_DELTA_RESULT_SIZE_CACHED_MAX, hdrlen, size);
518 err = got_repo_cache_raw_object(repo, id, *obj);
523 got_object_raw_close(*obj);
531 static const struct got_error *
532 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
533 int pack_idx, struct got_object_id *id)
535 const struct got_error *err = NULL;
537 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
542 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
546 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
550 static const struct got_error *
551 read_packed_commit_privsep(struct got_commit_object **commit,
552 struct got_pack *pack, struct got_packidx *packidx, int idx,
553 struct got_object_id *id)
555 const struct got_error *err = NULL;
557 if (pack->privsep_child)
558 return request_packed_commit(commit, pack, idx, id);
560 err = got_pack_start_privsep_child(pack, packidx);
564 return request_packed_commit(commit, pack, idx, id);
567 static const struct got_error *
568 request_commit(struct got_commit_object **commit, struct got_repository *repo,
569 int fd, struct got_object_id *id)
571 const struct got_error *err = NULL;
572 struct imsgbuf *ibuf;
574 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
576 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
580 return got_privsep_recv_commit(commit, ibuf);
583 static const struct got_error *
584 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
585 struct got_object_id *id, struct got_repository *repo)
587 const struct got_error *err;
589 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
590 return request_commit(commit, repo, obj_fd, id);
592 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_COMMIT);
596 return request_commit(commit, repo, obj_fd, id);
599 static const struct got_error *
600 open_commit(struct got_commit_object **commit,
601 struct got_repository *repo, struct got_object_id *id, int check_cache)
603 const struct got_error *err = NULL;
604 struct got_packidx *packidx = NULL;
606 char *path_packfile = NULL;
609 *commit = got_repo_get_cached_commit(repo, id);
610 if (*commit != NULL) {
617 err = got_repo_search_packidx(&packidx, &idx, repo, id);
619 struct got_pack *pack = NULL;
621 err = got_packidx_get_packfile_path(&path_packfile,
622 packidx->path_packidx);
626 pack = got_repo_get_cached_pack(repo, path_packfile);
628 err = got_repo_cache_pack(&pack, repo, path_packfile,
633 err = read_packed_commit_privsep(commit, pack,
635 } else if (err->code == GOT_ERR_NO_OBJ) {
638 err = got_object_open_loose_fd(&fd, id, repo);
641 err = read_commit_privsep(commit, fd, id, repo);
646 err = got_repo_cache_commit(repo, id, *commit);
653 const struct got_error *
654 got_object_open_as_commit(struct got_commit_object **commit,
655 struct got_repository *repo, struct got_object_id *id)
657 *commit = got_repo_get_cached_commit(repo, id);
658 if (*commit != NULL) {
663 return open_commit(commit, repo, id, 0);
666 const struct got_error *
667 got_object_commit_open(struct got_commit_object **commit,
668 struct got_repository *repo, struct got_object *obj)
670 return open_commit(commit, repo, got_object_get_id(obj), 1);
673 static const struct got_error *
674 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
675 int pack_idx, struct got_object_id *id)
677 const struct got_error *err = NULL;
679 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
684 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
687 static const struct got_error *
688 read_packed_tree_privsep(struct got_tree_object **tree,
689 struct got_pack *pack, struct got_packidx *packidx, int idx,
690 struct got_object_id *id)
692 const struct got_error *err = NULL;
694 if (pack->privsep_child)
695 return request_packed_tree(tree, pack, idx, id);
697 err = got_pack_start_privsep_child(pack, packidx);
701 return request_packed_tree(tree, pack, idx, id);
704 static const struct got_error *
705 request_tree(struct got_tree_object **tree, struct got_repository *repo,
706 int fd, struct got_object_id *id)
708 const struct got_error *err = NULL;
709 struct imsgbuf *ibuf;
711 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
713 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
717 return got_privsep_recv_tree(tree, ibuf);
720 static const struct got_error *
721 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
722 struct got_object_id *id, struct got_repository *repo)
724 const struct got_error *err;
726 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
727 return request_tree(tree, repo, obj_fd, id);
729 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_TREE);
733 return request_tree(tree, repo, obj_fd, id);
736 static const struct got_error *
737 open_tree(struct got_tree_object **tree, struct got_repository *repo,
738 struct got_object_id *id, int check_cache)
740 const struct got_error *err = NULL;
741 struct got_packidx *packidx = NULL;
743 char *path_packfile = NULL;
746 *tree = got_repo_get_cached_tree(repo, id);
754 err = got_repo_search_packidx(&packidx, &idx, repo, id);
756 struct got_pack *pack = NULL;
758 err = got_packidx_get_packfile_path(&path_packfile,
759 packidx->path_packidx);
763 pack = got_repo_get_cached_pack(repo, path_packfile);
765 err = got_repo_cache_pack(&pack, repo, path_packfile,
770 err = read_packed_tree_privsep(tree, pack,
772 } else if (err->code == GOT_ERR_NO_OBJ) {
775 err = got_object_open_loose_fd(&fd, id, repo);
778 err = read_tree_privsep(tree, fd, id, repo);
783 err = got_repo_cache_tree(repo, id, *tree);
790 const struct got_error *
791 got_object_open_as_tree(struct got_tree_object **tree,
792 struct got_repository *repo, struct got_object_id *id)
794 *tree = got_repo_get_cached_tree(repo, id);
800 return open_tree(tree, repo, id, 0);
803 const struct got_error *
804 got_object_tree_open(struct got_tree_object **tree,
805 struct got_repository *repo, struct got_object *obj)
807 return open_tree(tree, repo, got_object_get_id(obj), 1);
810 static const struct got_error *
811 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
812 struct got_pack *pack, struct got_packidx *packidx, int idx,
813 struct got_object_id *id)
815 const struct got_error *err = NULL;
816 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
819 err = pack_child_send_tempfiles(ibuf, pack);
823 outfd_child = dup(outfd);
824 if (outfd_child == -1)
825 return got_error_from_errno("dup");
827 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
831 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
837 err = got_privsep_recv_blob(outbuf, size, hdrlen,
838 pack->privsep_child->ibuf);
842 if (lseek(outfd, SEEK_SET, 0) == -1)
843 err = got_error_from_errno("lseek");
848 static const struct got_error *
849 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
850 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
851 struct got_object_id *id)
853 const struct got_error *err = NULL;
855 if (pack->privsep_child == NULL) {
856 err = got_pack_start_privsep_child(pack, packidx);
861 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
865 static const struct got_error *
866 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
867 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
869 const struct got_error *err = NULL;
872 outfd_child = dup(outfd);
873 if (outfd_child == -1)
874 return got_error_from_errno("dup");
876 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
880 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
884 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
888 if (lseek(outfd, SEEK_SET, 0) == -1)
889 return got_error_from_errno("lseek");
894 static const struct got_error *
895 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
896 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
898 const struct got_error *err;
899 struct imsgbuf *ibuf;
901 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
902 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
903 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
907 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_BLOB);
911 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
912 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
915 static const struct got_error *
916 open_blob(struct got_blob_object **blob, struct got_repository *repo,
917 struct got_object_id *id, size_t blocksize, int outfd)
919 const struct got_error *err = NULL;
920 struct got_packidx *packidx = NULL;
922 char *path_packfile = NULL;
927 *blob = calloc(1, sizeof(**blob));
929 return got_error_from_errno("calloc");
931 (*blob)->read_buf = malloc(blocksize);
932 if ((*blob)->read_buf == NULL) {
933 err = got_error_from_errno("malloc");
937 if (ftruncate(outfd, 0L) == -1) {
938 err = got_error_from_errno("ftruncate");
941 if (lseek(outfd, SEEK_SET, 0) == -1) {
942 err = got_error_from_errno("lseek");
946 err = got_repo_search_packidx(&packidx, &idx, repo, id);
948 struct got_pack *pack = NULL;
950 err = got_packidx_get_packfile_path(&path_packfile,
951 packidx->path_packidx);
955 pack = got_repo_get_cached_pack(repo, path_packfile);
957 err = got_repo_cache_pack(&pack, repo, path_packfile,
962 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
963 pack, packidx, idx, id);
964 } else if (err->code == GOT_ERR_NO_OBJ) {
967 err = got_object_open_loose_fd(&infd, id, repo);
970 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
977 err = got_error(GOT_ERR_BAD_OBJ_HDR);
982 (*blob)->f = fmemopen(outbuf, size, "rb");
983 if ((*blob)->f == NULL) {
984 err = got_error_from_errno("fmemopen");
988 (*blob)->data = outbuf;
990 if (fstat(outfd, &sb) == -1) {
991 err = got_error_from_errno("fstat");
995 if (sb.st_size != size) {
996 err = got_error(GOT_ERR_PRIVSEP_LEN);
1002 err = got_error_from_errno("dup");
1006 (*blob)->f = fdopen(dfd, "rb");
1007 if ((*blob)->f == NULL) {
1008 err = got_error_from_errno("fdopen");
1015 (*blob)->hdrlen = hdrlen;
1016 (*blob)->blocksize = blocksize;
1017 memcpy(&(*blob)->id, id, sizeof(*id));
1020 free(path_packfile);
1023 got_object_blob_close(*blob);
1030 const struct got_error *
1031 got_object_open_as_blob(struct got_blob_object **blob,
1032 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
1035 return open_blob(blob, repo, id, blocksize, outfd);
1038 const struct got_error *
1039 got_object_blob_open(struct got_blob_object **blob,
1040 struct got_repository *repo, struct got_object *obj, size_t blocksize,
1043 return open_blob(blob, repo, got_object_get_id(obj), blocksize, outfd);
1046 static const struct got_error *
1047 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1048 int pack_idx, struct got_object_id *id)
1050 const struct got_error *err = NULL;
1052 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1057 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1060 static const struct got_error *
1061 read_packed_tag_privsep(struct got_tag_object **tag,
1062 struct got_pack *pack, struct got_packidx *packidx, int idx,
1063 struct got_object_id *id)
1065 const struct got_error *err = NULL;
1067 if (pack->privsep_child)
1068 return request_packed_tag(tag, pack, idx, id);
1070 err = got_pack_start_privsep_child(pack, packidx);
1074 return request_packed_tag(tag, pack, idx, id);
1077 static const struct got_error *
1078 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1079 int fd, struct got_object_id *id)
1081 const struct got_error *err = NULL;
1082 struct imsgbuf *ibuf;
1084 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1086 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1090 return got_privsep_recv_tag(tag, ibuf);
1093 static const struct got_error *
1094 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1095 struct got_object_id *id, struct got_repository *repo)
1097 const struct got_error *err;
1099 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1100 return request_tag(tag, repo, obj_fd, id);
1102 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_TAG);
1106 return request_tag(tag, repo, obj_fd, id);
1109 static const struct got_error *
1110 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1111 struct got_object_id *id, int check_cache)
1113 const struct got_error *err = NULL;
1114 struct got_packidx *packidx = NULL;
1116 char *path_packfile = NULL;
1117 struct got_object *obj = NULL;
1118 int obj_type = GOT_OBJ_TYPE_ANY;
1121 *tag = got_repo_get_cached_tag(repo, id);
1129 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1131 struct got_pack *pack = NULL;
1133 err = got_packidx_get_packfile_path(&path_packfile,
1134 packidx->path_packidx);
1138 pack = got_repo_get_cached_pack(repo, path_packfile);
1140 err = got_repo_cache_pack(&pack, repo, path_packfile,
1146 /* Beware of "lightweight" tags: Check object type first. */
1147 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1151 obj_type = obj->type;
1152 got_object_close(obj);
1153 if (obj_type != GOT_OBJ_TYPE_TAG) {
1154 err = got_error(GOT_ERR_OBJ_TYPE);
1157 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1158 } else if (err->code == GOT_ERR_NO_OBJ) {
1161 err = got_object_open_loose_fd(&fd, id, repo);
1164 err = got_object_read_header_privsep(&obj, id, repo, fd);
1167 obj_type = obj->type;
1168 got_object_close(obj);
1169 if (obj_type != GOT_OBJ_TYPE_TAG)
1170 return got_error(GOT_ERR_OBJ_TYPE);
1172 err = got_object_open_loose_fd(&fd, id, repo);
1175 err = read_tag_privsep(tag, fd, id, repo);
1180 err = got_repo_cache_tag(repo, id, *tag);
1183 free(path_packfile);
1187 const struct got_error *
1188 got_object_open_as_tag(struct got_tag_object **tag,
1189 struct got_repository *repo, struct got_object_id *id)
1191 *tag = got_repo_get_cached_tag(repo, id);
1197 return open_tag(tag, repo, id, 0);
1200 const struct got_error *
1201 got_object_tag_open(struct got_tag_object **tag,
1202 struct got_repository *repo, struct got_object *obj)
1204 return open_tag(tag, repo, got_object_get_id(obj), 1);
1207 const struct got_error *
1208 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
1209 struct got_object_id *commit_id, const char *path,
1210 struct got_repository *repo)
1212 const struct got_error *err = NULL;
1213 struct got_pack *pack = NULL;
1214 struct got_packidx *packidx = NULL;
1215 char *path_packfile = NULL;
1216 struct got_commit_object *changed_commit = NULL;
1217 struct got_object_id *changed_commit_id = NULL;
1220 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
1222 if (err->code != GOT_ERR_NO_OBJ)
1227 err = got_packidx_get_packfile_path(&path_packfile,
1228 packidx->path_packidx);
1232 pack = got_repo_get_cached_pack(repo, path_packfile);
1234 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1239 if (pack->privsep_child == NULL) {
1240 err = got_pack_start_privsep_child(pack, packidx);
1245 err = got_privsep_send_commit_traversal_request(
1246 pack->privsep_child->ibuf, commit_id, idx, path);
1250 err = got_privsep_recv_traversed_commits(&changed_commit,
1251 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
1255 if (changed_commit) {
1257 * Cache the commit in which the path was changed.
1258 * This commit might be opened again soon.
1260 changed_commit->refcnt++;
1261 err = got_repo_cache_commit(repo, changed_commit_id,
1263 got_object_commit_close(changed_commit);
1266 free(path_packfile);
1267 free(changed_commit_id);
1271 const struct got_error *
1272 got_object_enumerate(int *found_all_objects,
1273 got_object_enumerate_commit_cb cb_commit,
1274 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
1275 struct got_object_id **ours, int nours,
1276 struct got_object_id **theirs, int ntheirs,
1277 struct got_packidx *packidx, struct got_repository *repo)
1279 const struct got_error *err = NULL;
1280 struct got_pack *pack;
1281 char *path_packfile = NULL;
1283 err = got_packidx_get_packfile_path(&path_packfile,
1284 packidx->path_packidx);
1288 pack = got_repo_get_cached_pack(repo, path_packfile);
1290 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1295 if (pack->privsep_child == NULL) {
1296 err = got_pack_start_privsep_child(pack, packidx);
1301 err = got_privsep_send_object_enumeration_request(
1302 pack->privsep_child->ibuf);
1306 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
1310 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
1314 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
1318 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
1322 err = got_privsep_recv_enumerated_objects(found_all_objects,
1323 pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
1325 free(path_packfile);