2 * Copyright (c) 2018, 2019 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>
20 #include <sys/socket.h>
37 #include "got_compat.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_repository.h"
42 #include "got_opentemp.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_delta.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_pack.h"
54 #include "got_lib_repository.h"
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 struct got_object_id *
65 got_object_get_id(struct got_object *obj)
70 const struct got_error *
71 got_object_get_id_str(char **outbuf, struct got_object *obj)
73 return got_object_id_str(outbuf, &obj->id);
76 const struct got_error *
77 got_object_get_type(int *type, struct got_repository *repo,
78 struct got_object_id *id)
80 const struct got_error *err = NULL;
81 struct got_object *obj;
83 err = got_object_open(&obj, repo, id);
88 case GOT_OBJ_TYPE_COMMIT:
89 case GOT_OBJ_TYPE_TREE:
90 case GOT_OBJ_TYPE_BLOB:
91 case GOT_OBJ_TYPE_TAG:
95 err = got_error(GOT_ERR_OBJ_TYPE);
99 got_object_close(obj);
103 const struct got_error *
104 got_object_get_path(char **path, struct got_object_id *id,
105 struct got_repository *repo)
107 const struct got_error *err = NULL;
113 path_objects = got_repo_get_path_objects(repo);
114 if (path_objects == NULL)
115 return got_error_from_errno("got_repo_get_path_objects");
117 err = got_object_id_str(&hex, id);
121 if (asprintf(path, "%s/%.2x/%s", path_objects,
122 id->sha1[0], hex + 2) == -1)
123 err = got_error_from_errno("asprintf");
131 const struct got_error *
132 got_object_open_loose_fd(int *fd, struct got_object_id *id,
133 struct got_repository *repo)
135 const struct got_error *err = NULL;
138 err = got_object_get_path(&path, id, repo);
141 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
143 err = got_error_from_errno2("open", path);
151 static const struct got_error *
152 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
153 struct got_object_id *id)
155 const struct got_error *err = NULL;
156 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
158 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
162 err = got_privsep_recv_obj(obj, ibuf);
166 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
171 /* Create temporary files used during delta application. */
172 static const struct got_error *
173 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
175 const struct got_error *err;
176 int basefd = -1, accumfd = -1;
179 * For performance reasons, the child will keep reusing the
180 * same temporary files during every object request.
181 * Opening and closing new files for every object request is
182 * too expensive during operations such as 'gotadmin pack'.
184 if (pack->child_has_tempfiles)
187 basefd = dup(pack->basefd);
189 return got_error_from_errno("dup");
191 accumfd = dup(pack->accumfd);
193 err = got_error_from_errno("dup");
197 err = got_privsep_send_tmpfd(ibuf, basefd);
201 err = got_privsep_send_tmpfd(ibuf, accumfd);
209 pack->child_has_tempfiles = 1;
213 static const struct got_error *
214 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
215 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
217 const struct got_error *err = NULL;
218 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
221 err = pack_child_send_tempfiles(ibuf, pack);
225 outfd_child = dup(outfd);
226 if (outfd_child == -1)
227 return got_error_from_errno("dup");
229 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
235 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
239 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
246 static const struct got_error *
247 read_packed_object_privsep(struct got_object **obj,
248 struct got_repository *repo, struct got_pack *pack,
249 struct got_packidx *packidx, int idx, struct got_object_id *id)
251 const struct got_error *err = NULL;
253 if (pack->privsep_child == NULL) {
254 err = got_pack_start_privsep_child(pack, packidx);
259 return request_packed_object(obj, pack, idx, id);
262 static const struct got_error *
263 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
264 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
265 struct got_object_id *id)
267 const struct got_error *err = NULL;
269 if (pack->privsep_child == NULL) {
270 err = got_pack_start_privsep_child(pack, packidx);
275 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
279 const struct got_error *
280 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
281 struct got_repository *repo)
283 const struct got_error *err = NULL;
284 struct got_pack *pack = NULL;
285 struct got_packidx *packidx = NULL;
289 err = got_repo_search_packidx(&packidx, &idx, repo, id);
293 err = got_packidx_get_packfile_path(&path_packfile,
294 packidx->path_packidx);
298 pack = got_repo_get_cached_pack(repo, path_packfile);
300 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
305 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
313 const struct got_error *
314 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
315 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
316 struct got_repository *repo)
318 return read_packed_object_privsep(obj, repo, pack, packidx,
322 const struct got_error *
323 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
324 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
325 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
326 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
327 struct got_repository *repo)
329 const struct got_error *err = NULL;
330 struct got_pack *pack = NULL;
336 *delta_compressed_size = 0;
338 *delta_out_offset = 0;
340 err = got_packidx_get_packfile_path(&path_packfile,
341 packidx->path_packidx);
345 pack = got_repo_get_cached_pack(repo, path_packfile);
347 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
352 if (pack->privsep_child == NULL) {
353 err = got_pack_start_privsep_child(pack, packidx);
358 if (!pack->child_has_delta_outfd) {
360 outfd_child = dup(delta_cache_fd);
361 if (outfd_child == -1)
362 return got_error_from_errno("dup");
363 err = got_privsep_send_raw_delta_outfd(
364 pack->privsep_child->ibuf, outfd_child);
367 pack->child_has_delta_outfd = 1;
370 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
375 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
376 delta_compressed_size, delta_offset, delta_out_offset, base_id,
377 pack->privsep_child->ibuf);
380 static const struct got_error *
381 request_object(struct got_object **obj, struct got_object_id *id,
382 struct got_repository *repo, int fd)
384 const struct got_error *err = NULL;
385 struct imsgbuf *ibuf;
387 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
389 err = got_privsep_send_obj_req(ibuf, fd, id);
393 return got_privsep_recv_obj(obj, ibuf);
396 static const struct got_error *
397 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
398 struct got_object_id *id, struct got_repository *repo, int infd)
400 const struct got_error *err = NULL;
401 struct imsgbuf *ibuf;
404 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
406 outfd_child = dup(outfd);
407 if (outfd_child == -1)
408 return got_error_from_errno("dup");
410 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
414 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
418 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
421 static const struct got_error *
422 start_read_object_child(struct got_repository *repo)
424 const struct got_error *err = NULL;
427 struct imsgbuf *ibuf;
429 ibuf = calloc(1, sizeof(*ibuf));
431 return got_error_from_errno("calloc");
433 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
434 err = got_error_from_errno("socketpair");
441 err = got_error_from_errno("fork");
446 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
451 if (close(imsg_fds[1]) == -1) {
452 err = got_error_from_errno("close");
457 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
459 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
460 imsg_init(ibuf, imsg_fds[0]);
461 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
466 const struct got_error *
467 got_object_read_header_privsep(struct got_object **obj,
468 struct got_object_id *id, struct got_repository *repo, int obj_fd)
470 const struct got_error *err;
472 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
473 return request_object(obj, id, repo, obj_fd);
475 err = start_read_object_child(repo);
481 return request_object(obj, id, repo, obj_fd);
484 static const struct got_error *
485 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
486 int outfd, struct got_object_id *id, struct got_repository *repo,
489 const struct got_error *err;
491 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
492 return request_raw_object(outbuf, size, hdrlen, outfd, id,
495 err = start_read_object_child(repo);
499 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
503 const struct got_error *
504 got_object_open(struct got_object **obj, struct got_repository *repo,
505 struct got_object_id *id)
507 const struct got_error *err = NULL;
510 *obj = got_repo_get_cached_object(repo, id);
516 err = got_object_open_packed(obj, id, repo);
517 if (err && err->code != GOT_ERR_NO_OBJ)
521 return got_repo_cache_object(repo, id, *obj);
524 err = got_object_open_loose_fd(&fd, id, repo);
526 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
527 err = got_error_no_obj(id);
531 err = got_object_read_header_privsep(obj, id, repo, fd);
535 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
538 return got_repo_cache_object(repo, id, *obj);
541 /* *outfd must be initialized to -1 by caller */
542 const struct got_error *
543 got_object_raw_open(struct got_raw_object **obj, int *outfd,
544 struct got_repository *repo, struct got_object_id *id)
546 const struct got_error *err = NULL;
547 struct got_packidx *packidx = NULL;
549 uint8_t *outbuf = NULL;
552 char *path_packfile = NULL;
554 *obj = got_repo_get_cached_raw_object(repo, id);
561 *outfd = got_opentempfd();
563 return got_error_from_errno("got_opentempfd");
566 err = got_repo_search_packidx(&packidx, &idx, repo, id);
568 struct got_pack *pack = NULL;
570 err = got_packidx_get_packfile_path(&path_packfile,
571 packidx->path_packidx);
575 pack = got_repo_get_cached_pack(repo, path_packfile);
577 err = got_repo_cache_pack(&pack, repo, path_packfile,
582 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
583 *outfd, pack, packidx, idx, id);
586 } else if (err->code == GOT_ERR_NO_OBJ) {
589 err = got_object_open_loose_fd(&fd, id, repo);
592 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
598 *obj = calloc(1, sizeof(**obj));
600 err = got_error_from_errno("calloc");
606 (*obj)->data = outbuf;
609 if (fstat(*outfd, &sb) == -1) {
610 err = got_error_from_errno("fstat");
614 if (sb.st_size != hdrlen + size) {
615 err = got_error(GOT_ERR_PRIVSEP_LEN);
618 #ifndef GOT_PACK_NO_MMAP
619 if (hdrlen + size > 0) {
620 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
621 MAP_PRIVATE, *outfd, 0);
622 if ((*obj)->data == MAP_FAILED) {
623 if (errno != ENOMEM) {
624 err = got_error_from_errno("mmap");
635 (*obj)->f = fdopen(*outfd, "r");
636 if ((*obj)->f == NULL) {
637 err = got_error_from_errno("fdopen");
643 (*obj)->hdrlen = hdrlen;
645 err = got_repo_cache_raw_object(repo, id, *obj);
650 got_object_raw_close(*obj);
659 const struct got_error *
660 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
663 struct got_object_id id;
665 if (!got_parse_sha1_digest(id.sha1, id_str))
666 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
668 return got_object_open(obj, repo, &id);
671 const struct got_error *
672 got_object_resolve_id_str(struct got_object_id **id,
673 struct got_repository *repo, const char *id_str)
675 const struct got_error *err = NULL;
676 struct got_object *obj;
678 err = got_object_open_by_id_str(&obj, repo, id_str);
682 *id = got_object_id_dup(got_object_get_id(obj));
683 got_object_close(obj);
685 return got_error_from_errno("got_object_id_dup");
690 static const struct got_error *
691 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
692 int pack_idx, struct got_object_id *id)
694 const struct got_error *err = NULL;
696 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
701 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
705 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
709 static const struct got_error *
710 read_packed_commit_privsep(struct got_commit_object **commit,
711 struct got_pack *pack, struct got_packidx *packidx, int idx,
712 struct got_object_id *id)
714 const struct got_error *err = NULL;
716 if (pack->privsep_child)
717 return request_packed_commit(commit, pack, idx, id);
719 err = got_pack_start_privsep_child(pack, packidx);
723 return request_packed_commit(commit, pack, idx, id);
726 static const struct got_error *
727 request_commit(struct got_commit_object **commit, struct got_repository *repo,
728 int fd, struct got_object_id *id)
730 const struct got_error *err = NULL;
731 struct imsgbuf *ibuf;
733 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
735 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
739 return got_privsep_recv_commit(commit, ibuf);
742 static const struct got_error *
743 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
744 struct got_object_id *id, struct got_repository *repo)
746 const struct got_error *err;
749 struct imsgbuf *ibuf;
751 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
752 return request_commit(commit, repo, obj_fd, id);
754 ibuf = calloc(1, sizeof(*ibuf));
756 return got_error_from_errno("calloc");
758 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
759 err = got_error_from_errno("socketpair");
766 err = got_error_from_errno("fork");
771 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
776 if (close(imsg_fds[1]) == -1) {
777 err = got_error_from_errno("close");
781 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
783 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
784 imsg_init(ibuf, imsg_fds[0]);
785 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
787 return request_commit(commit, repo, obj_fd, id);
791 static const struct got_error *
792 open_commit(struct got_commit_object **commit,
793 struct got_repository *repo, struct got_object_id *id, int check_cache)
795 const struct got_error *err = NULL;
796 struct got_packidx *packidx = NULL;
798 char *path_packfile = NULL;
801 *commit = got_repo_get_cached_commit(repo, id);
802 if (*commit != NULL) {
809 err = got_repo_search_packidx(&packidx, &idx, repo, id);
811 struct got_pack *pack = NULL;
813 err = got_packidx_get_packfile_path(&path_packfile,
814 packidx->path_packidx);
818 pack = got_repo_get_cached_pack(repo, path_packfile);
820 err = got_repo_cache_pack(&pack, repo, path_packfile,
825 err = read_packed_commit_privsep(commit, pack,
827 } else if (err->code == GOT_ERR_NO_OBJ) {
830 err = got_object_open_loose_fd(&fd, id, repo);
833 err = read_commit_privsep(commit, fd, id, repo);
838 err = got_repo_cache_commit(repo, id, *commit);
845 const struct got_error *
846 got_object_open_as_commit(struct got_commit_object **commit,
847 struct got_repository *repo, struct got_object_id *id)
849 *commit = got_repo_get_cached_commit(repo, id);
850 if (*commit != NULL) {
855 return open_commit(commit, repo, id, 0);
858 const struct got_error *
859 got_object_commit_open(struct got_commit_object **commit,
860 struct got_repository *repo, struct got_object *obj)
862 return open_commit(commit, repo, got_object_get_id(obj), 1);
865 const struct got_error *
866 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
868 *qid = calloc(1, sizeof(**qid));
870 return got_error_from_errno("calloc");
872 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
876 const struct got_error *
877 got_object_id_queue_copy(const struct got_object_id_queue *src,
878 struct got_object_id_queue *dest)
880 const struct got_error *err;
881 struct got_object_qid *qid;
883 STAILQ_FOREACH(qid, src, entry) {
884 struct got_object_qid *new;
886 * Deep-copy the object ID only. Let the caller deal
887 * with setting up the new->data pointer if needed.
889 err = got_object_qid_alloc(&new, &qid->id);
891 got_object_id_queue_free(dest);
894 STAILQ_INSERT_TAIL(dest, new, entry);
900 static const struct got_error *
901 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
902 int pack_idx, struct got_object_id *id)
904 const struct got_error *err = NULL;
906 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
911 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
914 static const struct got_error *
915 read_packed_tree_privsep(struct got_tree_object **tree,
916 struct got_pack *pack, struct got_packidx *packidx, int idx,
917 struct got_object_id *id)
919 const struct got_error *err = NULL;
921 if (pack->privsep_child)
922 return request_packed_tree(tree, pack, idx, id);
924 err = got_pack_start_privsep_child(pack, packidx);
928 return request_packed_tree(tree, pack, idx, id);
931 static const struct got_error *
932 request_tree(struct got_tree_object **tree, struct got_repository *repo,
933 int fd, struct got_object_id *id)
935 const struct got_error *err = NULL;
936 struct imsgbuf *ibuf;
938 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
940 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
944 return got_privsep_recv_tree(tree, ibuf);
947 static const struct got_error *
948 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
949 struct got_object_id *id, struct got_repository *repo)
951 const struct got_error *err;
954 struct imsgbuf *ibuf;
956 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
957 return request_tree(tree, repo, obj_fd, id);
959 ibuf = calloc(1, sizeof(*ibuf));
961 return got_error_from_errno("calloc");
963 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
964 err = got_error_from_errno("socketpair");
971 err = got_error_from_errno("fork");
976 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
981 if (close(imsg_fds[1]) == -1) {
982 err = got_error_from_errno("close");
986 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
988 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
989 imsg_init(ibuf, imsg_fds[0]);
990 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
993 return request_tree(tree, repo, obj_fd, id);
996 static const struct got_error *
997 open_tree(struct got_tree_object **tree, struct got_repository *repo,
998 struct got_object_id *id, int check_cache)
1000 const struct got_error *err = NULL;
1001 struct got_packidx *packidx = NULL;
1003 char *path_packfile = NULL;
1006 *tree = got_repo_get_cached_tree(repo, id);
1007 if (*tree != NULL) {
1014 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1016 struct got_pack *pack = NULL;
1018 err = got_packidx_get_packfile_path(&path_packfile,
1019 packidx->path_packidx);
1023 pack = got_repo_get_cached_pack(repo, path_packfile);
1025 err = got_repo_cache_pack(&pack, repo, path_packfile,
1030 err = read_packed_tree_privsep(tree, pack,
1032 } else if (err->code == GOT_ERR_NO_OBJ) {
1035 err = got_object_open_loose_fd(&fd, id, repo);
1038 err = read_tree_privsep(tree, fd, id, repo);
1043 err = got_repo_cache_tree(repo, id, *tree);
1046 free(path_packfile);
1050 const struct got_error *
1051 got_object_open_as_tree(struct got_tree_object **tree,
1052 struct got_repository *repo, struct got_object_id *id)
1054 *tree = got_repo_get_cached_tree(repo, id);
1055 if (*tree != NULL) {
1060 return open_tree(tree, repo, id, 0);
1063 const struct got_error *
1064 got_object_tree_open(struct got_tree_object **tree,
1065 struct got_repository *repo, struct got_object *obj)
1067 return open_tree(tree, repo, got_object_get_id(obj), 1);
1071 got_object_tree_get_nentries(struct got_tree_object *tree)
1073 return tree->nentries;
1076 struct got_tree_entry *
1077 got_object_tree_get_first_entry(struct got_tree_object *tree)
1079 return got_object_tree_get_entry(tree, 0);
1082 struct got_tree_entry *
1083 got_object_tree_get_last_entry(struct got_tree_object *tree)
1085 return got_object_tree_get_entry(tree, tree->nentries - 1);
1088 struct got_tree_entry *
1089 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1091 if (i < 0 || i >= tree->nentries)
1093 return &tree->entries[i];
1097 got_tree_entry_get_mode(struct got_tree_entry *te)
1103 got_tree_entry_get_name(struct got_tree_entry *te)
1105 return &te->name[0];
1108 struct got_object_id *
1109 got_tree_entry_get_id(struct got_tree_entry *te)
1114 const struct got_error *
1115 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1117 const struct got_error *err = NULL;
1118 size_t len, totlen, hdrlen, offset;
1122 hdrlen = got_object_blob_get_hdrlen(blob);
1128 err = got_object_blob_read_block(&len, blob);
1135 totlen += len - hdrlen;
1136 p = realloc(*s, totlen + 1);
1138 err = got_error_from_errno("realloc");
1144 /* Skip blob object header first time around. */
1146 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1151 (*s)[totlen] = '\0';
1155 const struct got_error *
1156 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1157 struct got_repository *repo)
1159 const struct got_error *err = NULL;
1160 struct got_blob_object *blob = NULL;
1163 *link_target = NULL;
1165 if (!got_object_tree_entry_is_symlink(te))
1166 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1168 fd = got_opentempfd();
1170 err = got_error_from_errno("got_opentempfd");
1174 err = got_object_open_as_blob(&blob, repo,
1175 got_tree_entry_get_id(te), PATH_MAX, fd);
1179 err = got_object_blob_read_to_str(link_target, blob);
1181 if (fd != -1 && close(fd) == -1 && err == NULL)
1182 err = got_error_from_errno("close");
1184 got_object_blob_close(blob);
1187 *link_target = NULL;
1193 got_tree_entry_get_index(struct got_tree_entry *te)
1198 struct got_tree_entry *
1199 got_tree_entry_get_next(struct got_tree_object *tree,
1200 struct got_tree_entry *te)
1202 return got_object_tree_get_entry(tree, te->idx + 1);
1205 struct got_tree_entry *
1206 got_tree_entry_get_prev(struct got_tree_object *tree,
1207 struct got_tree_entry *te)
1209 return got_object_tree_get_entry(tree, te->idx - 1);
1212 static const struct got_error *
1213 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1214 struct got_pack *pack, struct got_packidx *packidx, int idx,
1215 struct got_object_id *id)
1217 const struct got_error *err = NULL;
1218 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1221 err = pack_child_send_tempfiles(ibuf, pack);
1225 outfd_child = dup(outfd);
1226 if (outfd_child == -1)
1227 return got_error_from_errno("dup");
1229 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1233 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1239 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1240 pack->privsep_child->ibuf);
1244 if (lseek(outfd, SEEK_SET, 0) == -1)
1245 err = got_error_from_errno("lseek");
1250 static const struct got_error *
1251 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1252 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1253 struct got_object_id *id)
1255 const struct got_error *err = NULL;
1257 if (pack->privsep_child == NULL) {
1258 err = got_pack_start_privsep_child(pack, packidx);
1263 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1267 static const struct got_error *
1268 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1269 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1271 const struct got_error *err = NULL;
1274 outfd_child = dup(outfd);
1275 if (outfd_child == -1)
1276 return got_error_from_errno("dup");
1278 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1282 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1286 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1290 if (lseek(outfd, SEEK_SET, 0) == -1)
1291 return got_error_from_errno("lseek");
1296 static const struct got_error *
1297 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1298 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1300 const struct got_error *err;
1303 struct imsgbuf *ibuf;
1305 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1306 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1307 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1311 ibuf = calloc(1, sizeof(*ibuf));
1313 return got_error_from_errno("calloc");
1315 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1316 err = got_error_from_errno("socketpair");
1323 err = got_error_from_errno("fork");
1327 else if (pid == 0) {
1328 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1333 if (close(imsg_fds[1]) == -1) {
1334 err = got_error_from_errno("close");
1338 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1340 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1341 imsg_init(ibuf, imsg_fds[0]);
1342 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1344 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1347 static const struct got_error *
1348 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1349 struct got_object_id *id, size_t blocksize, int outfd)
1351 const struct got_error *err = NULL;
1352 struct got_packidx *packidx = NULL;
1354 char *path_packfile = NULL;
1356 size_t size, hdrlen;
1359 *blob = calloc(1, sizeof(**blob));
1361 return got_error_from_errno("calloc");
1363 (*blob)->read_buf = malloc(blocksize);
1364 if ((*blob)->read_buf == NULL) {
1365 err = got_error_from_errno("malloc");
1369 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1371 struct got_pack *pack = NULL;
1373 err = got_packidx_get_packfile_path(&path_packfile,
1374 packidx->path_packidx);
1378 pack = got_repo_get_cached_pack(repo, path_packfile);
1380 err = got_repo_cache_pack(&pack, repo, path_packfile,
1385 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1386 pack, packidx, idx, id);
1387 } else if (err->code == GOT_ERR_NO_OBJ) {
1390 err = got_object_open_loose_fd(&infd, id, repo);
1393 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1399 if (hdrlen > size) {
1400 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1405 (*blob)->f = fmemopen(outbuf, size, "rb");
1406 if ((*blob)->f == NULL) {
1407 err = got_error_from_errno("fmemopen");
1411 (*blob)->data = outbuf;
1413 if (fstat(outfd, &sb) == -1) {
1414 err = got_error_from_errno("fstat");
1418 if (sb.st_size != size) {
1419 err = got_error(GOT_ERR_PRIVSEP_LEN);
1425 err = got_error_from_errno("dup");
1429 (*blob)->f = fdopen(dfd, "rb");
1430 if ((*blob)->f == NULL) {
1431 err = got_error_from_errno("fdopen");
1438 (*blob)->hdrlen = hdrlen;
1439 (*blob)->blocksize = blocksize;
1440 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1443 free(path_packfile);
1446 got_object_blob_close(*blob);
1453 const struct got_error *
1454 got_object_open_as_blob(struct got_blob_object **blob,
1455 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
1458 return open_blob(blob, repo, id, blocksize, outfd);
1461 const struct got_error *
1462 got_object_blob_open(struct got_blob_object **blob,
1463 struct got_repository *repo, struct got_object *obj, size_t blocksize,
1466 return open_blob(blob, repo, got_object_get_id(obj), blocksize, outfd);
1469 const struct got_error *
1470 got_object_blob_close(struct got_blob_object *blob)
1472 const struct got_error *err = NULL;
1473 free(blob->read_buf);
1474 if (blob->f && fclose(blob->f) == EOF)
1475 err = got_error_from_errno("fclose");
1482 got_object_blob_rewind(struct got_blob_object *blob)
1489 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1491 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1495 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1497 return blob->hdrlen;
1501 got_object_blob_get_read_buf(struct got_blob_object *blob)
1503 return blob->read_buf;
1506 const struct got_error *
1507 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1511 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1512 if (n == 0 && ferror(blob->f))
1513 return got_ferror(blob->f, GOT_ERR_IO);
1518 const struct got_error *
1519 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1520 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1522 const struct got_error *err = NULL;
1523 size_t n, len, hdrlen;
1526 const int alloc_chunksz = 512;
1528 off_t off = 0, total_len = 0;
1531 *line_offsets = NULL;
1537 hdrlen = got_object_blob_get_hdrlen(blob);
1539 err = got_object_blob_read_block(&len, blob);
1544 buf = got_object_blob_get_read_buf(blob);
1547 if (line_offsets && *line_offsets == NULL) {
1548 /* Have some data but perhaps no '\n'. */
1550 nalloc = alloc_chunksz;
1551 *line_offsets = calloc(nalloc,
1552 sizeof(**line_offsets));
1553 if (*line_offsets == NULL)
1554 return got_error_from_errno("calloc");
1556 /* Skip forward over end of first line. */
1563 /* Scan '\n' offsets in remaining chunk of data. */
1565 if (buf[i] != '\n') {
1570 if (line_offsets && nalloc < *nlines) {
1571 size_t n = *nlines + alloc_chunksz;
1572 off_t *o = recallocarray(*line_offsets,
1573 nalloc, n, sizeof(**line_offsets));
1575 free(*line_offsets);
1576 *line_offsets = NULL;
1577 return got_error_from_errno(
1584 off = total_len + i - hdrlen + 1;
1585 (*line_offsets)[*nlines - 1] = off;
1590 /* Skip blob object header first time around. */
1591 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1592 if (n != len - hdrlen)
1593 return got_ferror(outfile, GOT_ERR_IO);
1594 total_len += len - hdrlen;
1598 if (fflush(outfile) != 0)
1599 return got_error_from_errno("fflush");
1603 *filesize = total_len;
1608 static const struct got_error *
1609 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1610 int pack_idx, struct got_object_id *id)
1612 const struct got_error *err = NULL;
1614 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1619 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1622 static const struct got_error *
1623 read_packed_tag_privsep(struct got_tag_object **tag,
1624 struct got_pack *pack, struct got_packidx *packidx, int idx,
1625 struct got_object_id *id)
1627 const struct got_error *err = NULL;
1629 if (pack->privsep_child)
1630 return request_packed_tag(tag, pack, idx, id);
1632 err = got_pack_start_privsep_child(pack, packidx);
1636 return request_packed_tag(tag, pack, idx, id);
1639 static const struct got_error *
1640 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1641 int fd, struct got_object_id *id)
1643 const struct got_error *err = NULL;
1644 struct imsgbuf *ibuf;
1646 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1648 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1652 return got_privsep_recv_tag(tag, ibuf);
1655 static const struct got_error *
1656 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1657 struct got_object_id *id, struct got_repository *repo)
1659 const struct got_error *err;
1662 struct imsgbuf *ibuf;
1664 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1665 return request_tag(tag, repo, obj_fd, id);
1667 ibuf = calloc(1, sizeof(*ibuf));
1669 return got_error_from_errno("calloc");
1671 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1672 err = got_error_from_errno("socketpair");
1679 err = got_error_from_errno("fork");
1683 else if (pid == 0) {
1684 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1689 if (close(imsg_fds[1]) == -1) {
1690 err = got_error_from_errno("close");
1694 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1696 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1697 imsg_init(ibuf, imsg_fds[0]);
1698 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1700 return request_tag(tag, repo, obj_fd, id);
1703 static const struct got_error *
1704 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1705 struct got_object_id *id, int check_cache)
1707 const struct got_error *err = NULL;
1708 struct got_packidx *packidx = NULL;
1710 char *path_packfile = NULL;
1711 struct got_object *obj = NULL;
1712 int obj_type = GOT_OBJ_TYPE_ANY;
1715 *tag = got_repo_get_cached_tag(repo, id);
1723 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1725 struct got_pack *pack = NULL;
1727 err = got_packidx_get_packfile_path(&path_packfile,
1728 packidx->path_packidx);
1732 pack = got_repo_get_cached_pack(repo, path_packfile);
1734 err = got_repo_cache_pack(&pack, repo, path_packfile,
1740 /* Beware of "lightweight" tags: Check object type first. */
1741 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1745 obj_type = obj->type;
1746 got_object_close(obj);
1747 if (obj_type != GOT_OBJ_TYPE_TAG) {
1748 err = got_error(GOT_ERR_OBJ_TYPE);
1751 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1752 } else if (err->code == GOT_ERR_NO_OBJ) {
1755 err = got_object_open_loose_fd(&fd, id, repo);
1758 err = got_object_read_header_privsep(&obj, id, repo, fd);
1761 obj_type = obj->type;
1762 got_object_close(obj);
1763 if (obj_type != GOT_OBJ_TYPE_TAG)
1764 return got_error(GOT_ERR_OBJ_TYPE);
1766 err = got_object_open_loose_fd(&fd, id, repo);
1769 err = read_tag_privsep(tag, fd, id, repo);
1774 err = got_repo_cache_tag(repo, id, *tag);
1777 free(path_packfile);
1781 const struct got_error *
1782 got_object_open_as_tag(struct got_tag_object **tag,
1783 struct got_repository *repo, struct got_object_id *id)
1785 *tag = got_repo_get_cached_tag(repo, id);
1791 return open_tag(tag, repo, id, 0);
1794 const struct got_error *
1795 got_object_tag_open(struct got_tag_object **tag,
1796 struct got_repository *repo, struct got_object *obj)
1798 return open_tag(tag, repo, got_object_get_id(obj), 1);
1802 got_object_tag_get_name(struct got_tag_object *tag)
1808 got_object_tag_get_object_type(struct got_tag_object *tag)
1810 return tag->obj_type;
1813 struct got_object_id *
1814 got_object_tag_get_object_id(struct got_tag_object *tag)
1820 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1822 return tag->tagger_time;
1826 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1828 return tag->tagger_gmtoff;
1832 got_object_tag_get_tagger(struct got_tag_object *tag)
1838 got_object_tag_get_message(struct got_tag_object *tag)
1843 static struct got_tree_entry *
1844 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1848 /* Note that tree entries are sorted in strncmp() order. */
1849 for (i = 0; i < tree->nentries; i++) {
1850 struct got_tree_entry *te = &tree->entries[i];
1851 int cmp = strncmp(te->name, name, len);
1856 if (te->name[len] == '\0')
1862 struct got_tree_entry *
1863 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1865 return find_entry_by_name(tree, name, strlen(name));
1868 const struct got_error *
1869 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1870 struct got_repository *repo, struct got_tree_object *tree,
1873 const struct got_error *err = NULL;
1874 struct got_tree_object *subtree = NULL;
1875 struct got_tree_entry *te = NULL;
1876 const char *seg, *s;
1888 struct got_tree_object *next_tree;
1897 te = find_entry_by_name(subtree, seg, seglen);
1899 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1910 err = got_object_open_as_tree(&next_tree, repo,
1915 if (subtree != tree)
1916 got_object_tree_close(subtree);
1917 subtree = next_tree;
1922 *id = got_object_id_dup(&te->id);
1924 return got_error_from_errno("got_object_id_dup");
1928 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1930 if (subtree && subtree != tree)
1931 got_object_tree_close(subtree);
1934 const struct got_error *
1935 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1936 struct got_commit_object *commit, const char *path)
1938 const struct got_error *err = NULL;
1939 struct got_tree_object *tree = NULL;
1943 /* Handle opening of root of commit's tree. */
1944 if (got_path_is_root_dir(path)) {
1945 *id = got_object_id_dup(commit->tree_id);
1947 err = got_error_from_errno("got_object_id_dup");
1949 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1952 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1956 got_object_tree_close(tree);
1961 * Normalize file mode bits to avoid false positive tree entry differences
1962 * in case tree entries have unexpected mode bits set.
1965 normalize_mode_for_comparison(mode_t mode)
1968 * For directories, the only relevant bit is the IFDIR bit.
1969 * This allows us to detect paths changing from a directory
1970 * to a file and vice versa.
1973 return mode & S_IFDIR;
1976 * For symlinks, the only relevant bit is the IFLNK bit.
1977 * This allows us to detect paths changing from a symlinks
1978 * to a file or directory and vice versa.
1981 return mode & S_IFLNK;
1983 /* For files, the only change we care about is the executable bit. */
1984 return mode & S_IXUSR;
1987 const struct got_error *
1988 got_object_tree_path_changed(int *changed,
1989 struct got_tree_object *tree01, struct got_tree_object *tree02,
1990 const char *path, struct got_repository *repo)
1992 const struct got_error *err = NULL;
1993 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1994 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1995 const char *seg, *s;
2000 /* We not do support comparing the root path. */
2001 if (got_path_is_root_dir(path))
2002 return got_error_path(path, GOT_ERR_BAD_PATH);
2012 struct got_tree_object *next_tree1, *next_tree2;
2013 mode_t mode1, mode2;
2022 te1 = find_entry_by_name(tree1, seg, seglen);
2024 err = got_error(GOT_ERR_NO_OBJ);
2029 te2 = find_entry_by_name(tree2, seg, seglen);
2032 mode1 = normalize_mode_for_comparison(te1->mode);
2033 mode2 = normalize_mode_for_comparison(te2->mode);
2034 if (mode1 != mode2) {
2039 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2045 if (*s == '\0') { /* final path element */
2054 err = got_object_open_as_tree(&next_tree1, repo,
2059 if (tree1 != tree01)
2060 got_object_tree_close(tree1);
2064 err = got_object_open_as_tree(&next_tree2, repo,
2069 if (tree2 != tree02)
2070 got_object_tree_close(tree2);
2073 if (tree2 != tree02)
2074 got_object_tree_close(tree2);
2080 if (tree1 && tree1 != tree01)
2081 got_object_tree_close(tree1);
2082 if (tree2 && tree2 != tree02)
2083 got_object_tree_close(tree2);
2087 const struct got_error *
2088 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2089 struct got_tree_entry *te)
2091 const struct got_error *err = NULL;
2093 *new_te = calloc(1, sizeof(**new_te));
2094 if (*new_te == NULL)
2095 return got_error_from_errno("calloc");
2097 (*new_te)->mode = te->mode;
2098 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2099 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2104 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2106 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2110 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2112 /* S_IFDIR check avoids confusing symlinks with submodules. */
2113 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2116 static const struct got_error *
2117 resolve_symlink(char **link_target, const char *path,
2118 struct got_commit_object *commit, struct got_repository *repo)
2120 const struct got_error *err = NULL;
2122 char *name, *parent_path = NULL;
2123 struct got_object_id *tree_obj_id = NULL;
2124 struct got_tree_object *tree = NULL;
2125 struct got_tree_entry *te = NULL;
2127 *link_target = NULL;
2129 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2130 return got_error(GOT_ERR_NO_SPACE);
2132 name = basename(buf);
2134 return got_error_from_errno2("basename", path);
2136 err = got_path_dirname(&parent_path, path);
2140 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2143 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2144 /* Display the complete path in error message. */
2145 err = got_error_path(path, err->code);
2150 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2154 te = got_object_tree_find_entry(tree, name);
2156 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2160 if (got_object_tree_entry_is_symlink(te)) {
2161 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2164 if (!got_path_is_absolute(*link_target)) {
2166 if (asprintf(&abspath, "%s/%s", parent_path,
2167 *link_target) == -1) {
2168 err = got_error_from_errno("asprintf");
2172 *link_target = malloc(PATH_MAX);
2173 if (*link_target == NULL) {
2174 err = got_error_from_errno("malloc");
2177 err = got_canonpath(abspath, *link_target, PATH_MAX);
2186 got_object_tree_close(tree);
2189 *link_target = NULL;
2194 const struct got_error *
2195 got_object_resolve_symlinks(char **link_target, const char *path,
2196 struct got_commit_object *commit, struct got_repository *repo)
2198 const struct got_error *err = NULL;
2199 char *next_target = NULL;
2200 int max_recursion = 40; /* matches Git */
2202 *link_target = NULL;
2205 err = resolve_symlink(&next_target,
2206 *link_target ? *link_target : path, commit, repo);
2211 if (--max_recursion == 0) {
2212 err = got_error_path(path, GOT_ERR_RECURSION);
2213 *link_target = NULL;
2216 *link_target = next_target;
2218 } while (next_target);
2223 const struct got_error *
2224 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2225 struct got_object_id *commit_id, const char *path,
2226 struct got_repository *repo)
2228 const struct got_error *err = NULL;
2229 struct got_pack *pack = NULL;
2230 struct got_packidx *packidx = NULL;
2231 char *path_packfile = NULL;
2232 struct got_commit_object *changed_commit = NULL;
2233 struct got_object_id *changed_commit_id = NULL;
2236 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2238 if (err->code != GOT_ERR_NO_OBJ)
2243 err = got_packidx_get_packfile_path(&path_packfile,
2244 packidx->path_packidx);
2248 pack = got_repo_get_cached_pack(repo, path_packfile);
2250 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2255 if (pack->privsep_child == NULL) {
2256 err = got_pack_start_privsep_child(pack, packidx);
2261 err = got_privsep_send_commit_traversal_request(
2262 pack->privsep_child->ibuf, commit_id, idx, path);
2266 err = got_privsep_recv_traversed_commits(&changed_commit,
2267 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2271 if (changed_commit) {
2273 * Cache the commit in which the path was changed.
2274 * This commit might be opened again soon.
2276 changed_commit->refcnt++;
2277 err = got_repo_cache_commit(repo, changed_commit_id,
2279 got_object_commit_close(changed_commit);
2282 free(path_packfile);
2283 free(changed_commit_id);
2287 const struct got_error *
2288 got_object_enumerate(int *found_all_objects,
2289 got_object_enumerate_commit_cb cb_commit,
2290 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2291 struct got_object_id **ours, int nours,
2292 struct got_object_id **theirs, int ntheirs,
2293 struct got_packidx *packidx, struct got_repository *repo)
2295 const struct got_error *err = NULL;
2296 struct got_pack *pack;
2297 char *path_packfile = NULL;
2299 err = got_packidx_get_packfile_path(&path_packfile,
2300 packidx->path_packidx);
2304 pack = got_repo_get_cached_pack(repo, path_packfile);
2306 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2311 if (pack->privsep_child == NULL) {
2312 err = got_pack_start_privsep_child(pack, packidx);
2317 err = got_privsep_send_object_enumeration_request(
2318 pack->privsep_child->ibuf);
2322 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2326 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2330 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2334 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2338 err = got_privsep_recv_enumerated_objects(found_all_objects,
2339 pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
2341 free(path_packfile);