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>
19 #include <sys/queue.h>
21 #include <sys/socket.h>
23 #include <sys/resource.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_repository.h"
43 #include "got_opentemp.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_object.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_object_idcache.h"
52 #include "got_lib_object_cache.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_repository.h"
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 struct got_object_id *
62 got_object_get_id(struct got_object *obj)
67 const struct got_error *
68 got_object_get_id_str(char **outbuf, struct got_object *obj)
70 return got_object_id_str(outbuf, &obj->id);
73 const struct got_error *
74 got_object_get_type(int *type, struct got_repository *repo,
75 struct got_object_id *id)
77 const struct got_error *err = NULL;
78 struct got_object *obj;
80 err = got_object_open(&obj, repo, id);
85 case GOT_OBJ_TYPE_COMMIT:
86 case GOT_OBJ_TYPE_TREE:
87 case GOT_OBJ_TYPE_BLOB:
88 case GOT_OBJ_TYPE_TAG:
92 err = got_error(GOT_ERR_OBJ_TYPE);
96 got_object_close(obj);
100 const struct got_error *
101 got_object_get_path(char **path, struct got_object_id *id,
102 struct got_repository *repo)
104 const struct got_error *err = NULL;
110 path_objects = got_repo_get_path_objects(repo);
111 if (path_objects == NULL)
112 return got_error_from_errno("got_repo_get_path_objects");
114 err = got_object_id_str(&hex, id);
118 if (asprintf(path, "%s/%.2x/%s", path_objects,
119 id->sha1[0], hex + 2) == -1)
120 err = got_error_from_errno("asprintf");
128 const struct got_error *
129 got_object_open_loose_fd(int *fd, struct got_object_id *id,
130 struct got_repository *repo)
132 const struct got_error *err = NULL;
135 err = got_object_get_path(&path, id, repo);
138 *fd = open(path, O_RDONLY | O_NOFOLLOW);
140 err = got_error_from_errno2("open", path);
148 static const struct got_error *
149 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
150 struct got_object_id *id)
152 const struct got_error *err = NULL;
153 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
155 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
159 err = got_privsep_recv_obj(obj, ibuf);
163 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
168 static const struct got_error *
169 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
170 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
172 const struct got_error *err = NULL;
173 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
175 int basefd, accumfd; /* temporary files for delta application */
177 basefd = got_opentempfd();
179 return got_error_from_errno("got_opentempfd");
181 accumfd = got_opentempfd();
184 return got_error_from_errno("got_opentempfd");
187 outfd_child = dup(outfd);
188 if (outfd_child == -1) {
189 err = got_error_from_errno("dup");
195 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
203 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
211 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
218 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
223 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
231 set_max_datasize(void)
235 if (getrlimit(RLIMIT_DATA, &rl) != 0)
238 rl.rlim_cur = rl.rlim_max;
239 setrlimit(RLIMIT_DATA, &rl);
242 static const struct got_error *
243 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
245 const struct got_error *err = NULL;
248 struct imsgbuf *ibuf;
250 ibuf = calloc(1, sizeof(*ibuf));
252 return got_error_from_errno("calloc");
254 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
255 if (pack->privsep_child == NULL) {
256 err = got_error_from_errno("calloc");
261 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
262 err = got_error_from_errno("socketpair");
268 err = got_error_from_errno("fork");
270 } else if (pid == 0) {
272 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
273 pack->path_packfile);
277 if (close(imsg_fds[1]) == -1)
278 return got_error_from_errno("close");
279 pack->privsep_child->imsg_fd = imsg_fds[0];
280 pack->privsep_child->pid = pid;
281 imsg_init(ibuf, imsg_fds[0]);
282 pack->privsep_child->ibuf = ibuf;
284 err = got_privsep_init_pack_child(ibuf, pack, packidx);
286 const struct got_error *child_err;
287 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
288 child_err = got_privsep_wait_for_child(
289 pack->privsep_child->pid);
290 if (child_err && err == NULL)
296 free(pack->privsep_child);
297 pack->privsep_child = NULL;
302 static const struct got_error *
303 read_packed_object_privsep(struct got_object **obj,
304 struct got_repository *repo, struct got_pack *pack,
305 struct got_packidx *packidx, int idx, struct got_object_id *id)
307 const struct got_error *err = NULL;
309 if (pack->privsep_child == NULL) {
310 err = start_pack_privsep_child(pack, packidx);
315 return request_packed_object(obj, pack, idx, id);
318 static const struct got_error *
319 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
320 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
321 struct got_object_id *id)
323 const struct got_error *err = NULL;
325 if (pack->privsep_child == NULL) {
326 err = start_pack_privsep_child(pack, packidx);
331 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
335 static const struct got_error *
336 open_packed_object(struct got_object **obj, struct got_object_id *id,
337 struct got_repository *repo)
339 const struct got_error *err = NULL;
340 struct got_pack *pack = NULL;
341 struct got_packidx *packidx = NULL;
345 err = got_repo_search_packidx(&packidx, &idx, repo, id);
349 err = got_packidx_get_packfile_path(&path_packfile, packidx);
353 pack = got_repo_get_cached_pack(repo, path_packfile);
355 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
360 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
368 static const struct got_error *
369 request_object(struct got_object **obj, struct got_repository *repo, int fd)
371 const struct got_error *err = NULL;
372 struct imsgbuf *ibuf;
374 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
376 err = got_privsep_send_obj_req(ibuf, fd);
380 return got_privsep_recv_obj(obj, ibuf);
383 static const struct got_error *
384 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
385 struct got_repository *repo, int infd)
387 const struct got_error *err = NULL;
388 struct imsgbuf *ibuf;
391 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
393 outfd_child = dup(outfd);
394 if (outfd_child == -1)
395 return got_error_from_errno("dup");
397 err = got_privsep_send_raw_obj_req(ibuf, infd);
401 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
405 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
408 static const struct got_error *
409 start_read_object_child(struct got_repository *repo)
411 const struct got_error *err = NULL;
414 struct imsgbuf *ibuf;
416 ibuf = calloc(1, sizeof(*ibuf));
418 return got_error_from_errno("calloc");
420 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
421 err = got_error_from_errno("socketpair");
428 err = got_error_from_errno("fork");
433 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
438 if (close(imsg_fds[1]) == -1) {
439 err = got_error_from_errno("close");
444 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
446 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
447 imsg_init(ibuf, imsg_fds[0]);
448 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
453 static const struct got_error *
454 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
457 const struct got_error *err;
459 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
460 return request_object(obj, repo, obj_fd);
462 err = start_read_object_child(repo);
468 return request_object(obj, repo, obj_fd);
471 static const struct got_error *
472 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
473 int outfd, struct got_repository *repo, int obj_fd)
475 const struct got_error *err;
477 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
478 return request_raw_object(outbuf, size, hdrlen, outfd, repo,
481 err = start_read_object_child(repo);
485 return request_raw_object(outbuf, size, hdrlen, outfd, repo, obj_fd);
488 const struct got_error *
489 got_object_open(struct got_object **obj, struct got_repository *repo,
490 struct got_object_id *id)
492 const struct got_error *err = NULL;
495 *obj = got_repo_get_cached_object(repo, id);
501 err = open_packed_object(obj, id, repo);
502 if (err && err->code != GOT_ERR_NO_OBJ)
506 return got_repo_cache_object(repo, id, *obj);
509 err = got_object_open_loose_fd(&fd, id, repo);
511 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
512 err = got_error_no_obj(id);
516 err = read_object_header_privsep(obj, repo, fd);
520 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
523 return got_repo_cache_object(repo, id, *obj);
526 const struct got_error *
527 got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo,
528 struct got_object_id *id, size_t blocksize)
530 const struct got_error *err = NULL;
531 struct got_packidx *packidx = NULL;
533 uint8_t *outbuf = NULL;
537 char *path_packfile = NULL;
541 outfd = got_opentempfd();
543 return got_error_from_errno("got_opentempfd");
545 err = got_repo_search_packidx(&packidx, &idx, repo, id);
547 struct got_pack *pack = NULL;
549 err = got_packidx_get_packfile_path(&path_packfile, packidx);
553 pack = got_repo_get_cached_pack(repo, path_packfile);
555 err = got_repo_cache_pack(&pack, repo, path_packfile,
560 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
561 outfd, pack, packidx, idx, id);
562 } else if (err->code == GOT_ERR_NO_OBJ) {
565 err = got_object_open_loose_fd(&fd, id, repo);
568 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
573 err = got_error(GOT_ERR_BAD_OBJ_HDR);
577 *obj = calloc(1, sizeof(**obj));
579 err = got_error_from_errno("calloc");
583 (*obj)->read_buf = malloc(blocksize);
584 if ((*obj)->read_buf == NULL) {
585 err = got_error_from_errno("malloc");
590 if (close(outfd) == -1) {
591 err = got_error_from_errno("close");
595 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
596 if ((*obj)->f == NULL) {
597 err = got_error_from_errno("fdopen");
600 (*obj)->data = outbuf;
603 if (fstat(outfd, &sb) == -1) {
604 err = got_error_from_errno("fstat");
608 if (sb.st_size != size) {
609 err = got_error(GOT_ERR_PRIVSEP_LEN);
613 (*obj)->f = fdopen(outfd, "r");
614 if ((*obj)->f == NULL) {
615 err = got_error_from_errno("fdopen");
621 (*obj)->hdrlen = hdrlen;
623 (*obj)->blocksize = blocksize;
628 got_object_raw_close(*obj);
639 got_object_raw_rewind(struct got_raw_object *obj)
646 got_object_raw_get_hdrlen(struct got_raw_object *obj)
652 got_object_raw_get_read_buf(struct got_raw_object *obj)
654 return obj->read_buf;
657 const struct got_error *
658 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
662 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
663 if (n == 0 && ferror(obj->f))
664 return got_ferror(obj->f, GOT_ERR_IO);
669 const struct got_error *
670 got_object_raw_close(struct got_raw_object *obj)
672 const struct got_error *err = NULL;
675 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
676 err = got_error_from_errno("fclose");
682 const struct got_error *
683 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
686 struct got_object_id id;
688 if (!got_parse_sha1_digest(id.sha1, id_str))
689 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
691 return got_object_open(obj, repo, &id);
694 const struct got_error *
695 got_object_resolve_id_str(struct got_object_id **id,
696 struct got_repository *repo, const char *id_str)
698 const struct got_error *err = NULL;
699 struct got_object *obj;
701 err = got_object_open_by_id_str(&obj, repo, id_str);
705 *id = got_object_id_dup(got_object_get_id(obj));
706 got_object_close(obj);
708 return got_error_from_errno("got_object_id_dup");
713 static const struct got_error *
714 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
715 int pack_idx, struct got_object_id *id)
717 const struct got_error *err = NULL;
719 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
724 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
728 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
732 static const struct got_error *
733 read_packed_commit_privsep(struct got_commit_object **commit,
734 struct got_pack *pack, struct got_packidx *packidx, int idx,
735 struct got_object_id *id)
737 const struct got_error *err = NULL;
739 if (pack->privsep_child)
740 return request_packed_commit(commit, pack, idx, id);
742 err = start_pack_privsep_child(pack, packidx);
746 return request_packed_commit(commit, pack, idx, id);
749 static const struct got_error *
750 request_commit(struct got_commit_object **commit, struct got_repository *repo,
753 const struct got_error *err = NULL;
754 struct imsgbuf *ibuf;
756 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
758 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
762 return got_privsep_recv_commit(commit, ibuf);
765 static const struct got_error *
766 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
767 struct got_repository *repo)
769 const struct got_error *err;
772 struct imsgbuf *ibuf;
774 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
775 return request_commit(commit, repo, obj_fd);
777 ibuf = calloc(1, sizeof(*ibuf));
779 return got_error_from_errno("calloc");
781 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
782 err = got_error_from_errno("socketpair");
789 err = got_error_from_errno("fork");
794 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
799 if (close(imsg_fds[1]) == -1) {
800 err = got_error_from_errno("close");
804 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
806 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
807 imsg_init(ibuf, imsg_fds[0]);
808 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
810 return request_commit(commit, repo, obj_fd);
814 static const struct got_error *
815 open_commit(struct got_commit_object **commit,
816 struct got_repository *repo, struct got_object_id *id, int check_cache)
818 const struct got_error *err = NULL;
819 struct got_packidx *packidx = NULL;
821 char *path_packfile = NULL;
824 *commit = got_repo_get_cached_commit(repo, id);
825 if (*commit != NULL) {
832 err = got_repo_search_packidx(&packidx, &idx, repo, id);
834 struct got_pack *pack = NULL;
836 err = got_packidx_get_packfile_path(&path_packfile, packidx);
840 pack = got_repo_get_cached_pack(repo, path_packfile);
842 err = got_repo_cache_pack(&pack, repo, path_packfile,
847 err = read_packed_commit_privsep(commit, pack,
849 } else if (err->code == GOT_ERR_NO_OBJ) {
852 err = got_object_open_loose_fd(&fd, id, repo);
855 err = read_commit_privsep(commit, fd, repo);
860 err = got_repo_cache_commit(repo, id, *commit);
867 const struct got_error *
868 got_object_open_as_commit(struct got_commit_object **commit,
869 struct got_repository *repo, struct got_object_id *id)
871 *commit = got_repo_get_cached_commit(repo, id);
872 if (*commit != NULL) {
877 return open_commit(commit, repo, id, 0);
880 const struct got_error *
881 got_object_commit_open(struct got_commit_object **commit,
882 struct got_repository *repo, struct got_object *obj)
884 return open_commit(commit, repo, got_object_get_id(obj), 1);
887 const struct got_error *
888 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
890 const struct got_error *err = NULL;
892 *qid = calloc(1, sizeof(**qid));
894 return got_error_from_errno("calloc");
896 (*qid)->id = got_object_id_dup(id);
897 if ((*qid)->id == NULL) {
898 err = got_error_from_errno("got_object_id_dup");
899 got_object_qid_free(*qid);
907 const struct got_error *
908 got_object_id_queue_copy(const struct got_object_id_queue *src,
909 struct got_object_id_queue *dest)
911 const struct got_error *err;
912 struct got_object_qid *qid;
914 SIMPLEQ_FOREACH(qid, src, entry) {
915 struct got_object_qid *new;
917 * Deep-copy the object ID only. Let the caller deal
918 * with setting up the new->data pointer if needed.
920 err = got_object_qid_alloc(&new, qid->id);
922 got_object_id_queue_free(dest);
925 SIMPLEQ_INSERT_TAIL(dest, new, entry);
931 static const struct got_error *
932 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
933 int pack_idx, struct got_object_id *id)
935 const struct got_error *err = NULL;
937 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
942 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
945 static const struct got_error *
946 read_packed_tree_privsep(struct got_tree_object **tree,
947 struct got_pack *pack, struct got_packidx *packidx, int idx,
948 struct got_object_id *id)
950 const struct got_error *err = NULL;
952 if (pack->privsep_child)
953 return request_packed_tree(tree, pack, idx, id);
955 err = start_pack_privsep_child(pack, packidx);
959 return request_packed_tree(tree, pack, idx, id);
962 static const struct got_error *
963 request_tree(struct got_tree_object **tree, struct got_repository *repo,
966 const struct got_error *err = NULL;
967 struct imsgbuf *ibuf;
969 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
971 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
975 return got_privsep_recv_tree(tree, ibuf);
978 const struct got_error *
979 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
980 struct got_repository *repo)
982 const struct got_error *err;
985 struct imsgbuf *ibuf;
987 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
988 return request_tree(tree, repo, obj_fd);
990 ibuf = calloc(1, sizeof(*ibuf));
992 return got_error_from_errno("calloc");
994 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
995 err = got_error_from_errno("socketpair");
1002 err = got_error_from_errno("fork");
1006 else if (pid == 0) {
1007 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1012 if (close(imsg_fds[1]) == -1) {
1013 err = got_error_from_errno("close");
1017 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1019 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1020 imsg_init(ibuf, imsg_fds[0]);
1021 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1024 return request_tree(tree, repo, obj_fd);
1027 static const struct got_error *
1028 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1029 struct got_object_id *id, int check_cache)
1031 const struct got_error *err = NULL;
1032 struct got_packidx *packidx = NULL;
1034 char *path_packfile = NULL;
1037 *tree = got_repo_get_cached_tree(repo, id);
1038 if (*tree != NULL) {
1045 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1047 struct got_pack *pack = NULL;
1049 err = got_packidx_get_packfile_path(&path_packfile, packidx);
1053 pack = got_repo_get_cached_pack(repo, path_packfile);
1055 err = got_repo_cache_pack(&pack, repo, path_packfile,
1060 err = read_packed_tree_privsep(tree, pack,
1062 } else if (err->code == GOT_ERR_NO_OBJ) {
1065 err = got_object_open_loose_fd(&fd, id, repo);
1068 err = read_tree_privsep(tree, fd, repo);
1073 err = got_repo_cache_tree(repo, id, *tree);
1076 free(path_packfile);
1080 const struct got_error *
1081 got_object_open_as_tree(struct got_tree_object **tree,
1082 struct got_repository *repo, struct got_object_id *id)
1084 *tree = got_repo_get_cached_tree(repo, id);
1085 if (*tree != NULL) {
1090 return open_tree(tree, repo, id, 0);
1093 const struct got_error *
1094 got_object_tree_open(struct got_tree_object **tree,
1095 struct got_repository *repo, struct got_object *obj)
1097 return open_tree(tree, repo, got_object_get_id(obj), 1);
1101 got_object_tree_get_nentries(struct got_tree_object *tree)
1103 return tree->nentries;
1106 struct got_tree_entry *
1107 got_object_tree_get_first_entry(struct got_tree_object *tree)
1109 return got_object_tree_get_entry(tree, 0);
1112 struct got_tree_entry *
1113 got_object_tree_get_last_entry(struct got_tree_object *tree)
1115 return got_object_tree_get_entry(tree, tree->nentries - 1);
1118 struct got_tree_entry *
1119 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1121 if (i < 0 || i >= tree->nentries)
1123 return &tree->entries[i];
1127 got_tree_entry_get_mode(struct got_tree_entry *te)
1133 got_tree_entry_get_name(struct got_tree_entry *te)
1135 return &te->name[0];
1138 struct got_object_id *
1139 got_tree_entry_get_id(struct got_tree_entry *te)
1144 const struct got_error *
1145 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1147 const struct got_error *err = NULL;
1148 size_t len, totlen, hdrlen, offset;
1152 hdrlen = got_object_blob_get_hdrlen(blob);
1158 err = got_object_blob_read_block(&len, blob);
1165 totlen += len - hdrlen;
1166 p = realloc(*s, totlen + 1);
1168 err = got_error_from_errno("realloc");
1174 /* Skip blob object header first time around. */
1176 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1181 (*s)[totlen] = '\0';
1185 const struct got_error *
1186 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1187 struct got_repository *repo)
1189 const struct got_error *err = NULL;
1190 struct got_blob_object *blob = NULL;
1192 *link_target = NULL;
1194 if (!got_object_tree_entry_is_symlink(te))
1195 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1197 err = got_object_open_as_blob(&blob, repo,
1198 got_tree_entry_get_id(te), PATH_MAX);
1202 err = got_object_blob_read_to_str(link_target, blob);
1203 got_object_blob_close(blob);
1206 *link_target = NULL;
1212 got_tree_entry_get_index(struct got_tree_entry *te)
1217 struct got_tree_entry *
1218 got_tree_entry_get_next(struct got_tree_object *tree,
1219 struct got_tree_entry *te)
1221 return got_object_tree_get_entry(tree, te->idx + 1);
1224 struct got_tree_entry *
1225 got_tree_entry_get_prev(struct got_tree_object *tree,
1226 struct got_tree_entry *te)
1228 return got_object_tree_get_entry(tree, te->idx - 1);
1231 static const struct got_error *
1232 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1233 struct got_pack *pack, struct got_packidx *packidx, int idx,
1234 struct got_object_id *id)
1236 const struct got_error *err = NULL;
1238 int basefd, accumfd; /* temporary files for delta application */
1240 basefd = got_opentempfd();
1242 return got_error_from_errno("got_opentempfd");
1243 accumfd = got_opentempfd();
1245 return got_error_from_errno("got_opentempfd");
1247 outfd_child = dup(outfd);
1248 if (outfd_child == -1)
1249 return got_error_from_errno("dup");
1251 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1255 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1263 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1270 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1275 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1276 pack->privsep_child->ibuf);
1280 if (lseek(outfd, SEEK_SET, 0) == -1)
1281 err = got_error_from_errno("lseek");
1286 static const struct got_error *
1287 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1288 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1289 struct got_object_id *id)
1291 const struct got_error *err = NULL;
1293 if (pack->privsep_child == NULL) {
1294 err = start_pack_privsep_child(pack, packidx);
1299 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1303 static const struct got_error *
1304 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1305 int infd, struct imsgbuf *ibuf)
1307 const struct got_error *err = NULL;
1310 outfd_child = dup(outfd);
1311 if (outfd_child == -1)
1312 return got_error_from_errno("dup");
1314 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
1318 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1322 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1326 if (lseek(outfd, SEEK_SET, 0) == -1)
1327 return got_error_from_errno("lseek");
1332 static const struct got_error *
1333 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1334 int outfd, int infd, struct got_repository *repo)
1336 const struct got_error *err;
1339 struct imsgbuf *ibuf;
1341 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1342 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1343 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1346 ibuf = calloc(1, sizeof(*ibuf));
1348 return got_error_from_errno("calloc");
1350 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1351 err = got_error_from_errno("socketpair");
1358 err = got_error_from_errno("fork");
1362 else if (pid == 0) {
1363 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1368 if (close(imsg_fds[1]) == -1) {
1369 err = got_error_from_errno("close");
1373 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1375 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1376 imsg_init(ibuf, imsg_fds[0]);
1377 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1379 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1382 static const struct got_error *
1383 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1384 struct got_object_id *id, size_t blocksize)
1386 const struct got_error *err = NULL;
1387 struct got_packidx *packidx = NULL;
1389 char *path_packfile = NULL;
1392 size_t size, hdrlen;
1395 *blob = calloc(1, sizeof(**blob));
1397 return got_error_from_errno("calloc");
1399 outfd = got_opentempfd();
1401 return got_error_from_errno("got_opentempfd");
1403 (*blob)->read_buf = malloc(blocksize);
1404 if ((*blob)->read_buf == NULL) {
1405 err = got_error_from_errno("malloc");
1409 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1411 struct got_pack *pack = NULL;
1413 err = got_packidx_get_packfile_path(&path_packfile, packidx);
1417 pack = got_repo_get_cached_pack(repo, path_packfile);
1419 err = got_repo_cache_pack(&pack, repo, path_packfile,
1424 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1425 pack, packidx, idx, id);
1426 } else if (err->code == GOT_ERR_NO_OBJ) {
1429 err = got_object_open_loose_fd(&infd, id, repo);
1432 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1438 if (hdrlen > size) {
1439 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1444 if (close(outfd) == -1 && err == NULL)
1445 err = got_error_from_errno("close");
1447 (*blob)->f = fmemopen(outbuf, size, "rb");
1448 if ((*blob)->f == NULL) {
1449 err = got_error_from_errno("fmemopen");
1453 (*blob)->data = outbuf;
1455 if (fstat(outfd, &sb) == -1) {
1456 err = got_error_from_errno("fstat");
1460 if (sb.st_size != size) {
1461 err = got_error(GOT_ERR_PRIVSEP_LEN);
1465 (*blob)->f = fdopen(outfd, "rb");
1466 if ((*blob)->f == NULL) {
1467 err = got_error_from_errno("fdopen");
1474 (*blob)->hdrlen = hdrlen;
1475 (*blob)->blocksize = blocksize;
1476 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1479 free(path_packfile);
1482 got_object_blob_close(*blob);
1484 } else if (outfd != -1)
1490 const struct got_error *
1491 got_object_open_as_blob(struct got_blob_object **blob,
1492 struct got_repository *repo, struct got_object_id *id,
1495 return open_blob(blob, repo, id, blocksize);
1498 const struct got_error *
1499 got_object_blob_open(struct got_blob_object **blob,
1500 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1502 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1505 const struct got_error *
1506 got_object_blob_close(struct got_blob_object *blob)
1508 const struct got_error *err = NULL;
1509 free(blob->read_buf);
1510 if (blob->f && fclose(blob->f) == EOF)
1511 err = got_error_from_errno("fclose");
1518 got_object_blob_rewind(struct got_blob_object *blob)
1525 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1527 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1531 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1533 return blob->hdrlen;
1537 got_object_blob_get_read_buf(struct got_blob_object *blob)
1539 return blob->read_buf;
1542 const struct got_error *
1543 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1547 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1548 if (n == 0 && ferror(blob->f))
1549 return got_ferror(blob->f, GOT_ERR_IO);
1554 const struct got_error *
1555 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1556 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1558 const struct got_error *err = NULL;
1559 size_t n, len, hdrlen;
1562 const int alloc_chunksz = 512;
1564 off_t off = 0, total_len = 0;
1567 *line_offsets = NULL;
1573 hdrlen = got_object_blob_get_hdrlen(blob);
1575 err = got_object_blob_read_block(&len, blob);
1580 buf = got_object_blob_get_read_buf(blob);
1583 if (line_offsets && *line_offsets == NULL) {
1584 /* Have some data but perhaps no '\n'. */
1586 nalloc = alloc_chunksz;
1587 *line_offsets = calloc(nalloc,
1588 sizeof(**line_offsets));
1589 if (*line_offsets == NULL)
1590 return got_error_from_errno("calloc");
1592 /* Skip forward over end of first line. */
1599 /* Scan '\n' offsets in remaining chunk of data. */
1601 if (buf[i] != '\n') {
1606 if (line_offsets && nalloc < *nlines) {
1607 size_t n = *nlines + alloc_chunksz;
1608 off_t *o = recallocarray(*line_offsets,
1609 nalloc, n, sizeof(**line_offsets));
1611 free(*line_offsets);
1612 *line_offsets = NULL;
1613 return got_error_from_errno(
1620 off = total_len + i - hdrlen + 1;
1621 (*line_offsets)[*nlines - 1] = off;
1626 /* Skip blob object header first time around. */
1627 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1628 if (n != len - hdrlen)
1629 return got_ferror(outfile, GOT_ERR_IO);
1630 total_len += len - hdrlen;
1634 if (fflush(outfile) != 0)
1635 return got_error_from_errno("fflush");
1639 *filesize = total_len;
1644 static const struct got_error *
1645 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1646 int pack_idx, struct got_object_id *id)
1648 const struct got_error *err = NULL;
1650 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1655 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1658 static const struct got_error *
1659 read_packed_tag_privsep(struct got_tag_object **tag,
1660 struct got_pack *pack, struct got_packidx *packidx, int idx,
1661 struct got_object_id *id)
1663 const struct got_error *err = NULL;
1665 if (pack->privsep_child)
1666 return request_packed_tag(tag, pack, idx, id);
1668 err = start_pack_privsep_child(pack, packidx);
1672 return request_packed_tag(tag, pack, idx, id);
1675 static const struct got_error *
1676 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1679 const struct got_error *err = NULL;
1680 struct imsgbuf *ibuf;
1682 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1684 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1688 return got_privsep_recv_tag(tag, ibuf);
1691 static const struct got_error *
1692 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1693 struct got_repository *repo)
1695 const struct got_error *err;
1698 struct imsgbuf *ibuf;
1700 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1701 return request_tag(tag, repo, obj_fd);
1703 ibuf = calloc(1, sizeof(*ibuf));
1705 return got_error_from_errno("calloc");
1707 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1708 err = got_error_from_errno("socketpair");
1715 err = got_error_from_errno("fork");
1719 else if (pid == 0) {
1720 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1725 if (close(imsg_fds[1]) == -1) {
1726 err = got_error_from_errno("close");
1730 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1732 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1733 imsg_init(ibuf, imsg_fds[0]);
1734 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1736 return request_tag(tag, repo, obj_fd);
1739 static const struct got_error *
1740 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1741 struct got_object_id *id, int check_cache)
1743 const struct got_error *err = NULL;
1744 struct got_packidx *packidx = NULL;
1746 char *path_packfile = NULL;
1747 struct got_object *obj = NULL;
1748 int obj_type = GOT_OBJ_TYPE_ANY;
1751 *tag = got_repo_get_cached_tag(repo, id);
1759 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1761 struct got_pack *pack = NULL;
1763 err = got_packidx_get_packfile_path(&path_packfile, packidx);
1767 pack = got_repo_get_cached_pack(repo, path_packfile);
1769 err = got_repo_cache_pack(&pack, repo, path_packfile,
1775 /* Beware of "lightweight" tags: Check object type first. */
1776 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1780 obj_type = obj->type;
1781 got_object_close(obj);
1782 if (obj_type != GOT_OBJ_TYPE_TAG) {
1783 err = got_error(GOT_ERR_OBJ_TYPE);
1786 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1787 } else if (err->code == GOT_ERR_NO_OBJ) {
1790 err = got_object_open_loose_fd(&fd, id, repo);
1793 err = read_object_header_privsep(&obj, repo, fd);
1796 obj_type = obj->type;
1797 got_object_close(obj);
1798 if (obj_type != GOT_OBJ_TYPE_TAG)
1799 return got_error(GOT_ERR_OBJ_TYPE);
1801 err = got_object_open_loose_fd(&fd, id, repo);
1804 err = read_tag_privsep(tag, fd, repo);
1809 err = got_repo_cache_tag(repo, id, *tag);
1812 free(path_packfile);
1816 const struct got_error *
1817 got_object_open_as_tag(struct got_tag_object **tag,
1818 struct got_repository *repo, struct got_object_id *id)
1820 *tag = got_repo_get_cached_tag(repo, id);
1826 return open_tag(tag, repo, id, 0);
1829 const struct got_error *
1830 got_object_tag_open(struct got_tag_object **tag,
1831 struct got_repository *repo, struct got_object *obj)
1833 return open_tag(tag, repo, got_object_get_id(obj), 1);
1837 got_object_tag_get_name(struct got_tag_object *tag)
1843 got_object_tag_get_object_type(struct got_tag_object *tag)
1845 return tag->obj_type;
1848 struct got_object_id *
1849 got_object_tag_get_object_id(struct got_tag_object *tag)
1855 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1857 return tag->tagger_time;
1861 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1863 return tag->tagger_gmtoff;
1867 got_object_tag_get_tagger(struct got_tag_object *tag)
1873 got_object_tag_get_message(struct got_tag_object *tag)
1878 static struct got_tree_entry *
1879 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1883 /* Note that tree entries are sorted in strncmp() order. */
1884 for (i = 0; i < tree->nentries; i++) {
1885 struct got_tree_entry *te = &tree->entries[i];
1886 int cmp = strncmp(te->name, name, len);
1891 if (te->name[len] == '\0')
1897 struct got_tree_entry *
1898 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1900 return find_entry_by_name(tree, name, strlen(name));
1903 const struct got_error *
1904 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1905 struct got_object_id *commit_id, const char *path)
1907 const struct got_error *err = NULL;
1908 struct got_commit_object *commit = NULL;
1909 struct got_tree_object *tree = NULL;
1910 struct got_tree_entry *te = NULL;
1911 const char *seg, *s;
1916 err = got_object_open_as_commit(&commit, repo, commit_id);
1920 /* Handle opening of root of commit's tree. */
1921 if (got_path_is_root_dir(path)) {
1922 *id = got_object_id_dup(commit->tree_id);
1924 err = got_error_from_errno("got_object_id_dup");
1928 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1938 struct got_tree_object *next_tree;
1947 te = find_entry_by_name(tree, seg, seglen);
1949 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1960 err = got_object_open_as_tree(&next_tree, repo,
1965 got_object_tree_close(tree);
1971 *id = got_object_id_dup(&te->id);
1973 return got_error_from_errno("got_object_id_dup");
1975 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1978 got_object_commit_close(commit);
1980 got_object_tree_close(tree);
1985 * Normalize file mode bits to avoid false positive tree entry differences
1986 * in case tree entries have unexpected mode bits set.
1989 normalize_mode_for_comparison(mode_t mode)
1992 * For directories, the only relevant bit is the IFDIR bit.
1993 * This allows us to detect paths changing from a directory
1994 * to a file and vice versa.
1997 return mode & S_IFDIR;
2000 * For symlinks, the only relevant bit is the IFLNK bit.
2001 * This allows us to detect paths changing from a symlinks
2002 * to a file or directory and vice versa.
2005 return mode & S_IFLNK;
2007 /* For files, the only change we care about is the executable bit. */
2008 return mode & S_IXUSR;
2011 const struct got_error *
2012 got_object_tree_path_changed(int *changed,
2013 struct got_tree_object *tree01, struct got_tree_object *tree02,
2014 const char *path, struct got_repository *repo)
2016 const struct got_error *err = NULL;
2017 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2018 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2019 const char *seg, *s;
2024 /* We not do support comparing the root path. */
2025 if (got_path_is_root_dir(path))
2026 return got_error_path(path, GOT_ERR_BAD_PATH);
2036 struct got_tree_object *next_tree1, *next_tree2;
2037 mode_t mode1, mode2;
2046 te1 = find_entry_by_name(tree1, seg, seglen);
2048 err = got_error(GOT_ERR_NO_OBJ);
2053 te2 = find_entry_by_name(tree2, seg, seglen);
2056 mode1 = normalize_mode_for_comparison(te1->mode);
2057 mode2 = normalize_mode_for_comparison(te2->mode);
2058 if (mode1 != mode2) {
2063 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2069 if (*s == '\0') { /* final path element */
2078 err = got_object_open_as_tree(&next_tree1, repo,
2083 if (tree1 != tree01)
2084 got_object_tree_close(tree1);
2088 err = got_object_open_as_tree(&next_tree2, repo,
2093 if (tree2 != tree02)
2094 got_object_tree_close(tree2);
2097 if (tree2 != tree02)
2098 got_object_tree_close(tree2);
2104 if (tree1 && tree1 != tree01)
2105 got_object_tree_close(tree1);
2106 if (tree2 && tree2 != tree02)
2107 got_object_tree_close(tree2);
2111 const struct got_error *
2112 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2113 struct got_tree_entry *te)
2115 const struct got_error *err = NULL;
2117 *new_te = calloc(1, sizeof(**new_te));
2118 if (*new_te == NULL)
2119 return got_error_from_errno("calloc");
2121 (*new_te)->mode = te->mode;
2122 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2123 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2128 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2130 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2134 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2136 /* S_IFDIR check avoids confusing symlinks with submodules. */
2137 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2140 static const struct got_error *
2141 resolve_symlink(char **link_target, const char *path,
2142 struct got_object_id *commit_id, struct got_repository *repo)
2144 const struct got_error *err = NULL;
2146 char *name, *parent_path = NULL;
2147 struct got_object_id *tree_obj_id = NULL;
2148 struct got_tree_object *tree = NULL;
2149 struct got_tree_entry *te = NULL;
2151 *link_target = NULL;
2153 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2154 return got_error(GOT_ERR_NO_SPACE);
2156 name = basename(buf);
2158 return got_error_from_errno2("basename", path);
2160 err = got_path_dirname(&parent_path, path);
2164 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2167 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2168 /* Display the complete path in error message. */
2169 err = got_error_path(path, err->code);
2174 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2178 te = got_object_tree_find_entry(tree, name);
2180 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2184 if (got_object_tree_entry_is_symlink(te)) {
2185 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2188 if (!got_path_is_absolute(*link_target)) {
2190 if (asprintf(&abspath, "%s/%s", parent_path,
2191 *link_target) == -1) {
2192 err = got_error_from_errno("asprintf");
2196 *link_target = malloc(PATH_MAX);
2197 if (*link_target == NULL) {
2198 err = got_error_from_errno("malloc");
2201 err = got_canonpath(abspath, *link_target, PATH_MAX);
2210 got_object_tree_close(tree);
2213 *link_target = NULL;
2218 const struct got_error *
2219 got_object_resolve_symlinks(char **link_target, const char *path,
2220 struct got_object_id *commit_id, struct got_repository *repo)
2222 const struct got_error *err = NULL;
2223 char *next_target = NULL;
2224 int max_recursion = 40; /* matches Git */
2226 *link_target = NULL;
2229 err = resolve_symlink(&next_target,
2230 *link_target ? *link_target : path, commit_id, repo);
2235 if (--max_recursion == 0) {
2236 err = got_error_path(path, GOT_ERR_RECURSION);
2237 *link_target = NULL;
2240 *link_target = next_target;
2242 } while (next_target);
2247 const struct got_error *
2248 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2249 struct got_object_id *commit_id, const char *path,
2250 struct got_repository *repo)
2252 const struct got_error *err = NULL;
2253 struct got_pack *pack = NULL;
2254 struct got_packidx *packidx = NULL;
2255 char *path_packfile = NULL;
2256 struct got_commit_object *changed_commit = NULL;
2257 struct got_object_id *changed_commit_id = NULL;
2260 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2262 if (err->code != GOT_ERR_NO_OBJ)
2267 err = got_packidx_get_packfile_path(&path_packfile, packidx);
2271 pack = got_repo_get_cached_pack(repo, path_packfile);
2273 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2278 if (pack->privsep_child == NULL) {
2279 err = start_pack_privsep_child(pack, packidx);
2284 err = got_privsep_send_commit_traversal_request(
2285 pack->privsep_child->ibuf, commit_id, idx, path);
2289 err = got_privsep_recv_traversed_commits(&changed_commit,
2290 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2294 if (changed_commit) {
2296 * Cache the commit in which the path was changed.
2297 * This commit might be opened again soon.
2299 changed_commit->refcnt++;
2300 err = got_repo_cache_commit(repo, changed_commit_id,
2302 got_object_commit_close(changed_commit);
2305 free(path_packfile);
2306 free(changed_commit_id);