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>
21 #include <sys/socket.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_repository.h"
36 #include "got_opentemp.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_object.h"
41 #include "got_lib_privsep.h"
42 #include "got_lib_object_cache.h"
43 #include "got_lib_pack.h"
44 #include "got_lib_repository.h"
46 static const struct got_error *
47 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
48 struct got_object_id *id)
50 const struct got_error *err = NULL;
51 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
53 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
57 err = got_privsep_recv_obj(obj, ibuf);
61 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
66 /* Create temporary files used during delta application. */
67 static const struct got_error *
68 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
70 const struct got_error *err;
71 int basefd = -1, accumfd = -1;
74 * For performance reasons, the child will keep reusing the
75 * same temporary files during every object request.
76 * Opening and closing new files for every object request is
77 * too expensive during operations such as 'gotadmin pack'.
79 if (pack->child_has_tempfiles)
82 basefd = dup(pack->basefd);
84 return got_error_from_errno("dup");
86 accumfd = dup(pack->accumfd);
88 err = got_error_from_errno("dup");
92 err = got_privsep_send_tmpfd(ibuf, basefd);
96 err = got_privsep_send_tmpfd(ibuf, accumfd);
104 pack->child_has_tempfiles = 1;
108 static const struct got_error *
109 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
110 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
112 const struct got_error *err = NULL;
113 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
116 err = pack_child_send_tempfiles(ibuf, pack);
120 outfd_child = dup(outfd);
121 if (outfd_child == -1)
122 return got_error_from_errno("dup");
124 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
130 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
134 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
141 static const struct got_error *
142 read_packed_object_privsep(struct got_object **obj,
143 struct got_repository *repo, struct got_pack *pack,
144 struct got_packidx *packidx, int idx, struct got_object_id *id)
146 const struct got_error *err = NULL;
148 if (pack->privsep_child == NULL) {
149 err = got_pack_start_privsep_child(pack, packidx);
154 return request_packed_object(obj, pack, idx, id);
157 static const struct got_error *
158 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
159 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
160 struct got_object_id *id)
162 const struct got_error *err = NULL;
164 if (pack->privsep_child == NULL) {
165 err = got_pack_start_privsep_child(pack, packidx);
170 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
174 const struct got_error *
175 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
176 struct got_repository *repo)
178 const struct got_error *err = NULL;
179 struct got_pack *pack = NULL;
180 struct got_packidx *packidx = NULL;
184 err = got_repo_search_packidx(&packidx, &idx, repo, id);
188 err = got_packidx_get_packfile_path(&path_packfile,
189 packidx->path_packidx);
193 pack = got_repo_get_cached_pack(repo, path_packfile);
195 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
200 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
208 const struct got_error *
209 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
210 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
211 struct got_repository *repo)
213 return read_packed_object_privsep(obj, repo, pack, packidx,
217 const struct got_error *
218 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
219 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
220 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
221 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
222 struct got_repository *repo)
224 const struct got_error *err = NULL;
225 struct got_pack *pack = NULL;
231 *delta_compressed_size = 0;
233 *delta_out_offset = 0;
235 err = got_packidx_get_packfile_path(&path_packfile,
236 packidx->path_packidx);
240 pack = got_repo_get_cached_pack(repo, path_packfile);
242 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
247 if (pack->privsep_child == NULL) {
248 err = got_pack_start_privsep_child(pack, packidx);
253 if (!pack->child_has_delta_outfd) {
255 outfd_child = dup(delta_cache_fd);
256 if (outfd_child == -1)
257 return got_error_from_errno("dup");
258 err = got_privsep_send_raw_delta_outfd(
259 pack->privsep_child->ibuf, outfd_child);
262 pack->child_has_delta_outfd = 1;
265 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
270 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
271 delta_compressed_size, delta_offset, delta_out_offset, base_id,
272 pack->privsep_child->ibuf);
275 static const struct got_error *
276 request_object(struct got_object **obj, struct got_object_id *id,
277 struct got_repository *repo, int fd)
279 const struct got_error *err = NULL;
280 struct imsgbuf *ibuf;
282 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
284 err = got_privsep_send_obj_req(ibuf, fd, id);
288 return got_privsep_recv_obj(obj, ibuf);
291 static const struct got_error *
292 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
293 struct got_object_id *id, struct got_repository *repo, int infd)
295 const struct got_error *err = NULL;
296 struct imsgbuf *ibuf;
299 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
301 outfd_child = dup(outfd);
302 if (outfd_child == -1)
303 return got_error_from_errno("dup");
305 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
309 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
313 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
316 static const struct got_error *
317 start_child(struct got_repository *repo, int type)
319 const struct got_error *err = NULL;
322 struct imsgbuf *ibuf;
323 const char *prog_path;
326 case GOT_REPO_PRIVSEP_CHILD_OBJECT:
327 prog_path = GOT_PATH_PROG_READ_OBJECT;
329 case GOT_REPO_PRIVSEP_CHILD_TREE:
330 prog_path = GOT_PATH_PROG_READ_TREE;
332 case GOT_REPO_PRIVSEP_CHILD_COMMIT:
333 prog_path = GOT_PATH_PROG_READ_COMMIT;
335 case GOT_REPO_PRIVSEP_CHILD_BLOB:
336 prog_path = GOT_PATH_PROG_READ_BLOB;
338 case GOT_REPO_PRIVSEP_CHILD_TAG:
339 prog_path = GOT_PATH_PROG_READ_TAG;
342 return got_error(GOT_ERR_OBJ_TYPE);
345 ibuf = calloc(1, sizeof(*ibuf));
347 return got_error_from_errno("calloc");
349 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
350 err = got_error_from_errno("socketpair");
357 err = got_error_from_errno("fork");
362 got_privsep_exec_child(imsg_fds, prog_path, repo->path);
366 if (close(imsg_fds[1]) == -1) {
367 err = got_error_from_errno("close");
372 repo->privsep_children[type].imsg_fd = imsg_fds[0];
373 repo->privsep_children[type].pid = pid;
374 imsg_init(ibuf, imsg_fds[0]);
375 repo->privsep_children[type].ibuf = ibuf;
380 const struct got_error *
381 got_object_read_header_privsep(struct got_object **obj,
382 struct got_object_id *id, struct got_repository *repo, int obj_fd)
384 const struct got_error *err;
386 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
387 return request_object(obj, id, repo, obj_fd);
389 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_OBJECT);
393 return request_object(obj, id, repo, obj_fd);
396 static const struct got_error *
397 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
398 int outfd, struct got_object_id *id, struct got_repository *repo,
401 const struct got_error *err;
403 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
404 return request_raw_object(outbuf, size, hdrlen, outfd, id,
407 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_OBJECT);
411 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
415 const struct got_error *
416 got_object_open(struct got_object **obj, struct got_repository *repo,
417 struct got_object_id *id)
419 const struct got_error *err = NULL;
422 *obj = got_repo_get_cached_object(repo, id);
428 err = got_object_open_packed(obj, id, repo);
429 if (err && err->code != GOT_ERR_NO_OBJ)
433 return got_repo_cache_object(repo, id, *obj);
436 err = got_object_open_loose_fd(&fd, id, repo);
438 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
439 err = got_error_no_obj(id);
443 err = got_object_read_header_privsep(obj, id, repo, fd);
447 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
450 return got_repo_cache_object(repo, id, *obj);
453 /* *outfd must be initialized to -1 by caller */
454 const struct got_error *
455 got_object_raw_open(struct got_raw_object **obj, int *outfd,
456 struct got_repository *repo, struct got_object_id *id)
458 const struct got_error *err = NULL;
459 struct got_packidx *packidx = NULL;
461 uint8_t *outbuf = NULL;
464 char *path_packfile = NULL;
466 *obj = got_repo_get_cached_raw_object(repo, id);
473 *outfd = got_opentempfd();
475 return got_error_from_errno("got_opentempfd");
478 err = got_repo_search_packidx(&packidx, &idx, repo, id);
480 struct got_pack *pack = NULL;
482 err = got_packidx_get_packfile_path(&path_packfile,
483 packidx->path_packidx);
487 pack = got_repo_get_cached_pack(repo, path_packfile);
489 err = got_repo_cache_pack(&pack, repo, path_packfile,
494 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
495 *outfd, pack, packidx, idx, id);
498 } else if (err->code == GOT_ERR_NO_OBJ) {
501 err = got_object_open_loose_fd(&fd, id, repo);
504 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
510 err = got_object_raw_alloc(obj, outbuf, outfd, hdrlen, size);
514 err = got_repo_cache_raw_object(repo, id, *obj);
519 got_object_raw_close(*obj);
527 static const struct got_error *
528 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
529 int pack_idx, struct got_object_id *id)
531 const struct got_error *err = NULL;
533 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
538 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
542 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
546 static const struct got_error *
547 read_packed_commit_privsep(struct got_commit_object **commit,
548 struct got_pack *pack, struct got_packidx *packidx, int idx,
549 struct got_object_id *id)
551 const struct got_error *err = NULL;
553 if (pack->privsep_child)
554 return request_packed_commit(commit, pack, idx, id);
556 err = got_pack_start_privsep_child(pack, packidx);
560 return request_packed_commit(commit, pack, idx, id);
563 static const struct got_error *
564 request_commit(struct got_commit_object **commit, struct got_repository *repo,
565 int fd, struct got_object_id *id)
567 const struct got_error *err = NULL;
568 struct imsgbuf *ibuf;
570 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
572 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
576 return got_privsep_recv_commit(commit, ibuf);
579 static const struct got_error *
580 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
581 struct got_object_id *id, struct got_repository *repo)
583 const struct got_error *err;
585 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
586 return request_commit(commit, repo, obj_fd, id);
588 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_COMMIT);
592 return request_commit(commit, repo, obj_fd, id);
595 static const struct got_error *
596 open_commit(struct got_commit_object **commit,
597 struct got_repository *repo, struct got_object_id *id, int check_cache)
599 const struct got_error *err = NULL;
600 struct got_packidx *packidx = NULL;
602 char *path_packfile = NULL;
605 *commit = got_repo_get_cached_commit(repo, id);
606 if (*commit != NULL) {
613 err = got_repo_search_packidx(&packidx, &idx, repo, id);
615 struct got_pack *pack = NULL;
617 err = got_packidx_get_packfile_path(&path_packfile,
618 packidx->path_packidx);
622 pack = got_repo_get_cached_pack(repo, path_packfile);
624 err = got_repo_cache_pack(&pack, repo, path_packfile,
629 err = read_packed_commit_privsep(commit, pack,
631 } else if (err->code == GOT_ERR_NO_OBJ) {
634 err = got_object_open_loose_fd(&fd, id, repo);
637 err = read_commit_privsep(commit, fd, id, repo);
642 err = got_repo_cache_commit(repo, id, *commit);
649 const struct got_error *
650 got_object_open_as_commit(struct got_commit_object **commit,
651 struct got_repository *repo, struct got_object_id *id)
653 *commit = got_repo_get_cached_commit(repo, id);
654 if (*commit != NULL) {
659 return open_commit(commit, repo, id, 0);
662 const struct got_error *
663 got_object_commit_open(struct got_commit_object **commit,
664 struct got_repository *repo, struct got_object *obj)
666 return open_commit(commit, repo, got_object_get_id(obj), 1);
669 static const struct got_error *
670 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
671 int pack_idx, struct got_object_id *id)
673 const struct got_error *err = NULL;
675 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
680 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
683 static const struct got_error *
684 read_packed_tree_privsep(struct got_tree_object **tree,
685 struct got_pack *pack, struct got_packidx *packidx, int idx,
686 struct got_object_id *id)
688 const struct got_error *err = NULL;
690 if (pack->privsep_child)
691 return request_packed_tree(tree, pack, idx, id);
693 err = got_pack_start_privsep_child(pack, packidx);
697 return request_packed_tree(tree, pack, idx, id);
700 static const struct got_error *
701 request_tree(struct got_tree_object **tree, struct got_repository *repo,
702 int fd, struct got_object_id *id)
704 const struct got_error *err = NULL;
705 struct imsgbuf *ibuf;
707 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
709 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
713 return got_privsep_recv_tree(tree, ibuf);
716 static const struct got_error *
717 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
718 struct got_object_id *id, struct got_repository *repo)
720 const struct got_error *err;
722 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
723 return request_tree(tree, repo, obj_fd, id);
725 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_TREE);
729 return request_tree(tree, repo, obj_fd, id);
732 static const struct got_error *
733 open_tree(struct got_tree_object **tree, struct got_repository *repo,
734 struct got_object_id *id, int check_cache)
736 const struct got_error *err = NULL;
737 struct got_packidx *packidx = NULL;
739 char *path_packfile = NULL;
742 *tree = got_repo_get_cached_tree(repo, id);
750 err = got_repo_search_packidx(&packidx, &idx, repo, id);
752 struct got_pack *pack = NULL;
754 err = got_packidx_get_packfile_path(&path_packfile,
755 packidx->path_packidx);
759 pack = got_repo_get_cached_pack(repo, path_packfile);
761 err = got_repo_cache_pack(&pack, repo, path_packfile,
766 err = read_packed_tree_privsep(tree, pack,
768 } else if (err->code == GOT_ERR_NO_OBJ) {
771 err = got_object_open_loose_fd(&fd, id, repo);
774 err = read_tree_privsep(tree, fd, id, repo);
779 err = got_repo_cache_tree(repo, id, *tree);
786 const struct got_error *
787 got_object_open_as_tree(struct got_tree_object **tree,
788 struct got_repository *repo, struct got_object_id *id)
790 *tree = got_repo_get_cached_tree(repo, id);
796 return open_tree(tree, repo, id, 0);
799 const struct got_error *
800 got_object_tree_open(struct got_tree_object **tree,
801 struct got_repository *repo, struct got_object *obj)
803 return open_tree(tree, repo, got_object_get_id(obj), 1);
806 static const struct got_error *
807 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
808 struct got_pack *pack, struct got_packidx *packidx, int idx,
809 struct got_object_id *id)
811 const struct got_error *err = NULL;
812 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
815 err = pack_child_send_tempfiles(ibuf, pack);
819 outfd_child = dup(outfd);
820 if (outfd_child == -1)
821 return got_error_from_errno("dup");
823 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
827 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
833 err = got_privsep_recv_blob(outbuf, size, hdrlen,
834 pack->privsep_child->ibuf);
838 if (lseek(outfd, SEEK_SET, 0) == -1)
839 err = got_error_from_errno("lseek");
844 static const struct got_error *
845 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
846 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
847 struct got_object_id *id)
849 const struct got_error *err = NULL;
851 if (pack->privsep_child == NULL) {
852 err = got_pack_start_privsep_child(pack, packidx);
857 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
861 static const struct got_error *
862 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
863 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
865 const struct got_error *err = NULL;
868 outfd_child = dup(outfd);
869 if (outfd_child == -1)
870 return got_error_from_errno("dup");
872 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
876 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
880 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
884 if (lseek(outfd, SEEK_SET, 0) == -1)
885 return got_error_from_errno("lseek");
890 static const struct got_error *
891 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
892 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
894 const struct got_error *err;
895 struct imsgbuf *ibuf;
897 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
898 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
899 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
903 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_BLOB);
907 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
908 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
911 static const struct got_error *
912 open_blob(struct got_blob_object **blob, struct got_repository *repo,
913 struct got_object_id *id, size_t blocksize, int outfd)
915 const struct got_error *err = NULL;
916 struct got_packidx *packidx = NULL;
918 char *path_packfile = NULL;
923 *blob = calloc(1, sizeof(**blob));
925 return got_error_from_errno("calloc");
927 (*blob)->read_buf = malloc(blocksize);
928 if ((*blob)->read_buf == NULL) {
929 err = got_error_from_errno("malloc");
933 if (ftruncate(outfd, 0L) == -1) {
934 err = got_error_from_errno("ftruncate");
937 if (lseek(outfd, SEEK_SET, 0) == -1) {
938 err = got_error_from_errno("lseek");
942 err = got_repo_search_packidx(&packidx, &idx, repo, id);
944 struct got_pack *pack = NULL;
946 err = got_packidx_get_packfile_path(&path_packfile,
947 packidx->path_packidx);
951 pack = got_repo_get_cached_pack(repo, path_packfile);
953 err = got_repo_cache_pack(&pack, repo, path_packfile,
958 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
959 pack, packidx, idx, id);
960 } else if (err->code == GOT_ERR_NO_OBJ) {
963 err = got_object_open_loose_fd(&infd, id, repo);
966 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
973 err = got_error(GOT_ERR_BAD_OBJ_HDR);
978 (*blob)->f = fmemopen(outbuf, size, "rb");
979 if ((*blob)->f == NULL) {
980 err = got_error_from_errno("fmemopen");
984 (*blob)->data = outbuf;
986 if (fstat(outfd, &sb) == -1) {
987 err = got_error_from_errno("fstat");
991 if (sb.st_size != size) {
992 err = got_error(GOT_ERR_PRIVSEP_LEN);
998 err = got_error_from_errno("dup");
1002 (*blob)->f = fdopen(dfd, "rb");
1003 if ((*blob)->f == NULL) {
1004 err = got_error_from_errno("fdopen");
1011 (*blob)->hdrlen = hdrlen;
1012 (*blob)->blocksize = blocksize;
1013 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1016 free(path_packfile);
1019 got_object_blob_close(*blob);
1026 const struct got_error *
1027 got_object_open_as_blob(struct got_blob_object **blob,
1028 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
1031 return open_blob(blob, repo, id, blocksize, outfd);
1034 const struct got_error *
1035 got_object_blob_open(struct got_blob_object **blob,
1036 struct got_repository *repo, struct got_object *obj, size_t blocksize,
1039 return open_blob(blob, repo, got_object_get_id(obj), blocksize, outfd);
1042 static const struct got_error *
1043 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1044 int pack_idx, struct got_object_id *id)
1046 const struct got_error *err = NULL;
1048 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1053 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1056 static const struct got_error *
1057 read_packed_tag_privsep(struct got_tag_object **tag,
1058 struct got_pack *pack, struct got_packidx *packidx, int idx,
1059 struct got_object_id *id)
1061 const struct got_error *err = NULL;
1063 if (pack->privsep_child)
1064 return request_packed_tag(tag, pack, idx, id);
1066 err = got_pack_start_privsep_child(pack, packidx);
1070 return request_packed_tag(tag, pack, idx, id);
1073 static const struct got_error *
1074 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1075 int fd, struct got_object_id *id)
1077 const struct got_error *err = NULL;
1078 struct imsgbuf *ibuf;
1080 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1082 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1086 return got_privsep_recv_tag(tag, ibuf);
1089 static const struct got_error *
1090 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1091 struct got_object_id *id, struct got_repository *repo)
1093 const struct got_error *err;
1095 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1096 return request_tag(tag, repo, obj_fd, id);
1098 err = start_child(repo, GOT_REPO_PRIVSEP_CHILD_TAG);
1102 return request_tag(tag, repo, obj_fd, id);
1105 static const struct got_error *
1106 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1107 struct got_object_id *id, int check_cache)
1109 const struct got_error *err = NULL;
1110 struct got_packidx *packidx = NULL;
1112 char *path_packfile = NULL;
1113 struct got_object *obj = NULL;
1114 int obj_type = GOT_OBJ_TYPE_ANY;
1117 *tag = got_repo_get_cached_tag(repo, id);
1125 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1127 struct got_pack *pack = NULL;
1129 err = got_packidx_get_packfile_path(&path_packfile,
1130 packidx->path_packidx);
1134 pack = got_repo_get_cached_pack(repo, path_packfile);
1136 err = got_repo_cache_pack(&pack, repo, path_packfile,
1142 /* Beware of "lightweight" tags: Check object type first. */
1143 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1147 obj_type = obj->type;
1148 got_object_close(obj);
1149 if (obj_type != GOT_OBJ_TYPE_TAG) {
1150 err = got_error(GOT_ERR_OBJ_TYPE);
1153 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1154 } else if (err->code == GOT_ERR_NO_OBJ) {
1157 err = got_object_open_loose_fd(&fd, id, repo);
1160 err = got_object_read_header_privsep(&obj, id, repo, fd);
1163 obj_type = obj->type;
1164 got_object_close(obj);
1165 if (obj_type != GOT_OBJ_TYPE_TAG)
1166 return got_error(GOT_ERR_OBJ_TYPE);
1168 err = got_object_open_loose_fd(&fd, id, repo);
1171 err = read_tag_privsep(tag, fd, id, repo);
1176 err = got_repo_cache_tag(repo, id, *tag);
1179 free(path_packfile);
1183 const struct got_error *
1184 got_object_open_as_tag(struct got_tag_object **tag,
1185 struct got_repository *repo, struct got_object_id *id)
1187 *tag = got_repo_get_cached_tag(repo, id);
1193 return open_tag(tag, repo, id, 0);
1196 const struct got_error *
1197 got_object_tag_open(struct got_tag_object **tag,
1198 struct got_repository *repo, struct got_object *obj)
1200 return open_tag(tag, repo, got_object_get_id(obj), 1);
1203 const struct got_error *
1204 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
1205 struct got_object_id *commit_id, const char *path,
1206 struct got_repository *repo)
1208 const struct got_error *err = NULL;
1209 struct got_pack *pack = NULL;
1210 struct got_packidx *packidx = NULL;
1211 char *path_packfile = NULL;
1212 struct got_commit_object *changed_commit = NULL;
1213 struct got_object_id *changed_commit_id = NULL;
1216 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
1218 if (err->code != GOT_ERR_NO_OBJ)
1223 err = got_packidx_get_packfile_path(&path_packfile,
1224 packidx->path_packidx);
1228 pack = got_repo_get_cached_pack(repo, path_packfile);
1230 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1235 if (pack->privsep_child == NULL) {
1236 err = got_pack_start_privsep_child(pack, packidx);
1241 err = got_privsep_send_commit_traversal_request(
1242 pack->privsep_child->ibuf, commit_id, idx, path);
1246 err = got_privsep_recv_traversed_commits(&changed_commit,
1247 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
1251 if (changed_commit) {
1253 * Cache the commit in which the path was changed.
1254 * This commit might be opened again soon.
1256 changed_commit->refcnt++;
1257 err = got_repo_cache_commit(repo, changed_commit_id,
1259 got_object_commit_close(changed_commit);
1262 free(path_packfile);
1263 free(changed_commit_id);
1267 const struct got_error *
1268 got_object_enumerate(int *found_all_objects,
1269 got_object_enumerate_commit_cb cb_commit,
1270 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
1271 struct got_object_id **ours, int nours,
1272 struct got_object_id **theirs, int ntheirs,
1273 struct got_packidx *packidx, struct got_repository *repo)
1275 const struct got_error *err = NULL;
1276 struct got_pack *pack;
1277 char *path_packfile = NULL;
1279 err = got_packidx_get_packfile_path(&path_packfile,
1280 packidx->path_packidx);
1284 pack = got_repo_get_cached_pack(repo, path_packfile);
1286 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1291 if (pack->privsep_child == NULL) {
1292 err = got_pack_start_privsep_child(pack, packidx);
1297 err = got_privsep_send_object_enumeration_request(
1298 pack->privsep_child->ibuf);
1302 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
1306 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
1310 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
1314 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
1318 err = got_privsep_recv_enumerated_objects(found_all_objects,
1319 pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
1321 free(path_packfile);