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 static const struct got_error *
129 open_loose_object(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 get_packfile_path(char **path_packfile, struct got_packidx *packidx)
153 /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
154 size = strlen(packidx->path_packidx) + 2;
155 if (size < GOT_PACKFILE_NAMELEN + 1)
156 return got_error_path(packidx->path_packidx, GOT_ERR_BAD_PATH);
158 *path_packfile = malloc(size);
159 if (*path_packfile == NULL)
160 return got_error_from_errno("malloc");
162 /* Copy up to and excluding ".idx". */
163 if (strlcpy(*path_packfile, packidx->path_packidx,
164 size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
165 return got_error(GOT_ERR_NO_SPACE);
167 if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
168 return got_error(GOT_ERR_NO_SPACE);
173 static const struct got_error *
174 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
175 struct got_object_id *id)
177 const struct got_error *err = NULL;
178 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
180 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
184 err = got_privsep_recv_obj(obj, ibuf);
188 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
193 static const struct got_error *
194 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
195 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
197 const struct got_error *err = NULL;
198 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
200 int basefd, accumfd; /* temporary files for delta application */
202 basefd = got_opentempfd();
204 return got_error_from_errno("got_opentempfd");
206 accumfd = got_opentempfd();
209 return got_error_from_errno("got_opentempfd");
212 outfd_child = dup(outfd);
213 if (outfd_child == -1) {
214 err = got_error_from_errno("dup");
220 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
228 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
236 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
243 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
248 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
256 set_max_datasize(void)
260 if (getrlimit(RLIMIT_DATA, &rl) != 0)
263 rl.rlim_cur = rl.rlim_max;
264 setrlimit(RLIMIT_DATA, &rl);
267 static const struct got_error *
268 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
270 const struct got_error *err = NULL;
273 struct imsgbuf *ibuf;
275 ibuf = calloc(1, sizeof(*ibuf));
277 return got_error_from_errno("calloc");
279 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
280 if (pack->privsep_child == NULL) {
281 err = got_error_from_errno("calloc");
286 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
287 err = got_error_from_errno("socketpair");
293 err = got_error_from_errno("fork");
295 } else if (pid == 0) {
297 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
298 pack->path_packfile);
302 if (close(imsg_fds[1]) == -1)
303 return got_error_from_errno("close");
304 pack->privsep_child->imsg_fd = imsg_fds[0];
305 pack->privsep_child->pid = pid;
306 imsg_init(ibuf, imsg_fds[0]);
307 pack->privsep_child->ibuf = ibuf;
309 err = got_privsep_init_pack_child(ibuf, pack, packidx);
311 const struct got_error *child_err;
312 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
313 child_err = got_privsep_wait_for_child(
314 pack->privsep_child->pid);
315 if (child_err && err == NULL)
321 free(pack->privsep_child);
322 pack->privsep_child = NULL;
327 static const struct got_error *
328 read_packed_object_privsep(struct got_object **obj,
329 struct got_repository *repo, struct got_pack *pack,
330 struct got_packidx *packidx, int idx, struct got_object_id *id)
332 const struct got_error *err = NULL;
334 if (pack->privsep_child == NULL) {
335 err = start_pack_privsep_child(pack, packidx);
340 return request_packed_object(obj, pack, idx, id);
343 static const struct got_error *
344 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
345 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
346 struct got_object_id *id)
348 const struct got_error *err = NULL;
350 if (pack->privsep_child == NULL) {
351 err = start_pack_privsep_child(pack, packidx);
356 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
360 static const struct got_error *
361 open_packed_object(struct got_object **obj, struct got_object_id *id,
362 struct got_repository *repo)
364 const struct got_error *err = NULL;
365 struct got_pack *pack = NULL;
366 struct got_packidx *packidx = NULL;
370 err = got_repo_search_packidx(&packidx, &idx, repo, id);
374 err = get_packfile_path(&path_packfile, packidx);
378 pack = got_repo_get_cached_pack(repo, path_packfile);
380 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
385 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
393 static const struct got_error *
394 request_object(struct got_object **obj, struct got_repository *repo, int fd)
396 const struct got_error *err = NULL;
397 struct imsgbuf *ibuf;
399 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
401 err = got_privsep_send_obj_req(ibuf, fd);
405 return got_privsep_recv_obj(obj, ibuf);
408 static const struct got_error *
409 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
410 struct got_repository *repo, int infd)
412 const struct got_error *err = NULL;
413 struct imsgbuf *ibuf;
416 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
418 outfd_child = dup(outfd);
419 if (outfd_child == -1)
420 return got_error_from_errno("dup");
422 err = got_privsep_send_raw_obj_req(ibuf, infd);
426 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
430 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
433 static const struct got_error *
434 start_read_object_child(struct got_repository *repo)
436 const struct got_error *err = NULL;
439 struct imsgbuf *ibuf;
441 ibuf = calloc(1, sizeof(*ibuf));
443 return got_error_from_errno("calloc");
445 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
446 err = got_error_from_errno("socketpair");
453 err = got_error_from_errno("fork");
458 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
463 if (close(imsg_fds[1]) == -1) {
464 err = got_error_from_errno("close");
469 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
471 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
472 imsg_init(ibuf, imsg_fds[0]);
473 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
478 static const struct got_error *
479 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
482 const struct got_error *err;
484 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
485 return request_object(obj, repo, obj_fd);
487 err = start_read_object_child(repo);
491 return request_object(obj, repo, obj_fd);
494 static const struct got_error *
495 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
496 int outfd, struct got_repository *repo, int obj_fd)
498 const struct got_error *err;
500 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
501 return request_raw_object(outbuf, size, hdrlen, outfd, repo,
504 err = start_read_object_child(repo);
508 return request_raw_object(outbuf, size, hdrlen, outfd, repo, obj_fd);
511 const struct got_error *
512 got_object_open(struct got_object **obj, struct got_repository *repo,
513 struct got_object_id *id)
515 const struct got_error *err = NULL;
519 *obj = got_repo_get_cached_object(repo, id);
525 err = open_packed_object(obj, id, repo);
526 if (err && err->code != GOT_ERR_NO_OBJ)
530 return got_repo_cache_object(repo, id, *obj);
533 err = got_object_get_path(&path, id, repo);
537 fd = open(path, O_RDONLY | O_NOFOLLOW);
540 err = got_error_no_obj(id);
542 err = got_error_from_errno2("open", path);
545 err = read_object_header_privsep(obj, repo, fd);
548 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
552 err = got_repo_cache_object(repo, id, *obj);
558 const struct got_error *
559 got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo,
560 struct got_object_id *id, size_t blocksize)
562 const struct got_error *err = NULL;
563 struct got_packidx *packidx = NULL;
565 uint8_t *outbuf = NULL;
569 char *path_packfile = NULL;
573 outfd = got_opentempfd();
575 return got_error_from_errno("got_opentempfd");
577 err = got_repo_search_packidx(&packidx, &idx, repo, id);
579 struct got_pack *pack = NULL;
581 err = get_packfile_path(&path_packfile, packidx);
585 pack = got_repo_get_cached_pack(repo, path_packfile);
587 err = got_repo_cache_pack(&pack, repo, path_packfile,
592 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
593 outfd, pack, packidx, idx, id);
594 } else if (err->code == GOT_ERR_NO_OBJ) {
597 err = open_loose_object(&fd, id, repo);
600 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
605 err = got_error(GOT_ERR_BAD_OBJ_HDR);
609 *obj = calloc(1, sizeof(**obj));
611 err = got_error_from_errno("calloc");
615 (*obj)->read_buf = malloc(blocksize);
616 if ((*obj)->read_buf == NULL) {
617 err = got_error_from_errno("malloc");
622 if (close(outfd) == -1) {
623 err = got_error_from_errno("close");
627 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
628 if ((*obj)->f == NULL) {
629 err = got_error_from_errno("fdopen");
632 (*obj)->data = outbuf;
635 if (fstat(outfd, &sb) == -1) {
636 err = got_error_from_errno("fstat");
640 if (sb.st_size != size) {
641 err = got_error(GOT_ERR_PRIVSEP_LEN);
645 (*obj)->f = fdopen(outfd, "r");
646 if ((*obj)->f == NULL) {
647 err = got_error_from_errno("fdopen");
653 (*obj)->hdrlen = hdrlen;
655 (*obj)->blocksize = blocksize;
660 got_object_raw_close(*obj);
671 got_object_raw_rewind(struct got_raw_object *obj)
678 got_object_raw_get_hdrlen(struct got_raw_object *obj)
684 got_object_raw_get_read_buf(struct got_raw_object *obj)
686 return obj->read_buf;
689 const struct got_error *
690 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
694 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
695 if (n == 0 && ferror(obj->f))
696 return got_ferror(obj->f, GOT_ERR_IO);
701 const struct got_error *
702 got_object_raw_close(struct got_raw_object *obj)
704 const struct got_error *err = NULL;
707 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
708 err = got_error_from_errno("fclose");
714 const struct got_error *
715 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
718 struct got_object_id id;
720 if (!got_parse_sha1_digest(id.sha1, id_str))
721 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
723 return got_object_open(obj, repo, &id);
726 const struct got_error *
727 got_object_resolve_id_str(struct got_object_id **id,
728 struct got_repository *repo, const char *id_str)
730 const struct got_error *err = NULL;
731 struct got_object *obj;
733 err = got_object_open_by_id_str(&obj, repo, id_str);
737 *id = got_object_id_dup(got_object_get_id(obj));
738 got_object_close(obj);
740 return got_error_from_errno("got_object_id_dup");
745 static const struct got_error *
746 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
747 int pack_idx, struct got_object_id *id)
749 const struct got_error *err = NULL;
751 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
756 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
760 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
764 static const struct got_error *
765 read_packed_commit_privsep(struct got_commit_object **commit,
766 struct got_pack *pack, struct got_packidx *packidx, int idx,
767 struct got_object_id *id)
769 const struct got_error *err = NULL;
771 if (pack->privsep_child)
772 return request_packed_commit(commit, pack, idx, id);
774 err = start_pack_privsep_child(pack, packidx);
778 return request_packed_commit(commit, pack, idx, id);
781 static const struct got_error *
782 request_commit(struct got_commit_object **commit, struct got_repository *repo,
785 const struct got_error *err = NULL;
786 struct imsgbuf *ibuf;
788 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
790 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
794 return got_privsep_recv_commit(commit, ibuf);
797 static const struct got_error *
798 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
799 struct got_repository *repo)
801 const struct got_error *err;
804 struct imsgbuf *ibuf;
806 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
807 return request_commit(commit, repo, obj_fd);
809 ibuf = calloc(1, sizeof(*ibuf));
811 return got_error_from_errno("calloc");
813 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
814 err = got_error_from_errno("socketpair");
821 err = got_error_from_errno("fork");
826 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
831 if (close(imsg_fds[1]) == -1) {
832 err = got_error_from_errno("close");
836 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
838 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
839 imsg_init(ibuf, imsg_fds[0]);
840 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
842 return request_commit(commit, repo, obj_fd);
846 static const struct got_error *
847 open_commit(struct got_commit_object **commit,
848 struct got_repository *repo, struct got_object_id *id, int check_cache)
850 const struct got_error *err = NULL;
851 struct got_packidx *packidx = NULL;
853 char *path_packfile = NULL;
856 *commit = got_repo_get_cached_commit(repo, id);
857 if (*commit != NULL) {
864 err = got_repo_search_packidx(&packidx, &idx, repo, id);
866 struct got_pack *pack = NULL;
868 err = get_packfile_path(&path_packfile, packidx);
872 pack = got_repo_get_cached_pack(repo, path_packfile);
874 err = got_repo_cache_pack(&pack, repo, path_packfile,
879 err = read_packed_commit_privsep(commit, pack,
881 } else if (err->code == GOT_ERR_NO_OBJ) {
884 err = open_loose_object(&fd, id, repo);
887 err = read_commit_privsep(commit, fd, repo);
892 err = got_repo_cache_commit(repo, id, *commit);
899 const struct got_error *
900 got_object_open_as_commit(struct got_commit_object **commit,
901 struct got_repository *repo, struct got_object_id *id)
903 *commit = got_repo_get_cached_commit(repo, id);
904 if (*commit != NULL) {
909 return open_commit(commit, repo, id, 0);
912 const struct got_error *
913 got_object_commit_open(struct got_commit_object **commit,
914 struct got_repository *repo, struct got_object *obj)
916 return open_commit(commit, repo, got_object_get_id(obj), 1);
919 const struct got_error *
920 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
922 const struct got_error *err = NULL;
924 *qid = calloc(1, sizeof(**qid));
926 return got_error_from_errno("calloc");
928 (*qid)->id = got_object_id_dup(id);
929 if ((*qid)->id == NULL) {
930 err = got_error_from_errno("got_object_id_dup");
931 got_object_qid_free(*qid);
939 static const struct got_error *
940 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
941 int pack_idx, struct got_object_id *id)
943 const struct got_error *err = NULL;
945 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
950 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
953 static const struct got_error *
954 read_packed_tree_privsep(struct got_tree_object **tree,
955 struct got_pack *pack, struct got_packidx *packidx, int idx,
956 struct got_object_id *id)
958 const struct got_error *err = NULL;
960 if (pack->privsep_child)
961 return request_packed_tree(tree, pack, idx, id);
963 err = start_pack_privsep_child(pack, packidx);
967 return request_packed_tree(tree, pack, idx, id);
970 static const struct got_error *
971 request_tree(struct got_tree_object **tree, struct got_repository *repo,
974 const struct got_error *err = NULL;
975 struct imsgbuf *ibuf;
977 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
979 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
983 return got_privsep_recv_tree(tree, ibuf);
986 const struct got_error *
987 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
988 struct got_repository *repo)
990 const struct got_error *err;
993 struct imsgbuf *ibuf;
995 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
996 return request_tree(tree, repo, obj_fd);
998 ibuf = calloc(1, sizeof(*ibuf));
1000 return got_error_from_errno("calloc");
1002 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1003 err = got_error_from_errno("socketpair");
1010 err = got_error_from_errno("fork");
1014 else if (pid == 0) {
1015 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1020 if (close(imsg_fds[1]) == -1) {
1021 err = got_error_from_errno("close");
1025 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1027 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1028 imsg_init(ibuf, imsg_fds[0]);
1029 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1032 return request_tree(tree, repo, obj_fd);
1035 static const struct got_error *
1036 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1037 struct got_object_id *id, int check_cache)
1039 const struct got_error *err = NULL;
1040 struct got_packidx *packidx = NULL;
1042 char *path_packfile = NULL;
1045 *tree = got_repo_get_cached_tree(repo, id);
1046 if (*tree != NULL) {
1053 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1055 struct got_pack *pack = NULL;
1057 err = get_packfile_path(&path_packfile, packidx);
1061 pack = got_repo_get_cached_pack(repo, path_packfile);
1063 err = got_repo_cache_pack(&pack, repo, path_packfile,
1068 err = read_packed_tree_privsep(tree, pack,
1070 } else if (err->code == GOT_ERR_NO_OBJ) {
1073 err = open_loose_object(&fd, id, repo);
1076 err = read_tree_privsep(tree, fd, repo);
1081 err = got_repo_cache_tree(repo, id, *tree);
1084 free(path_packfile);
1088 const struct got_error *
1089 got_object_open_as_tree(struct got_tree_object **tree,
1090 struct got_repository *repo, struct got_object_id *id)
1092 *tree = got_repo_get_cached_tree(repo, id);
1093 if (*tree != NULL) {
1098 return open_tree(tree, repo, id, 0);
1101 const struct got_error *
1102 got_object_tree_open(struct got_tree_object **tree,
1103 struct got_repository *repo, struct got_object *obj)
1105 return open_tree(tree, repo, got_object_get_id(obj), 1);
1109 got_object_tree_get_nentries(struct got_tree_object *tree)
1111 return tree->nentries;
1114 struct got_tree_entry *
1115 got_object_tree_get_first_entry(struct got_tree_object *tree)
1117 return got_object_tree_get_entry(tree, 0);
1120 struct got_tree_entry *
1121 got_object_tree_get_last_entry(struct got_tree_object *tree)
1123 return got_object_tree_get_entry(tree, tree->nentries - 1);
1126 struct got_tree_entry *
1127 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1129 if (i < 0 || i >= tree->nentries)
1131 return &tree->entries[i];
1135 got_tree_entry_get_mode(struct got_tree_entry *te)
1141 got_tree_entry_get_name(struct got_tree_entry *te)
1143 return &te->name[0];
1146 struct got_object_id *
1147 got_tree_entry_get_id(struct got_tree_entry *te)
1152 const struct got_error *
1153 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1155 const struct got_error *err = NULL;
1156 size_t len, totlen, hdrlen, offset;
1160 hdrlen = got_object_blob_get_hdrlen(blob);
1166 err = got_object_blob_read_block(&len, blob);
1173 totlen += len - hdrlen;
1174 p = realloc(*s, totlen + 1);
1176 err = got_error_from_errno("realloc");
1182 /* Skip blob object header first time around. */
1184 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1189 (*s)[totlen] = '\0';
1193 const struct got_error *
1194 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1195 struct got_repository *repo)
1197 const struct got_error *err = NULL;
1198 struct got_blob_object *blob = NULL;
1200 *link_target = NULL;
1202 if (!got_object_tree_entry_is_symlink(te))
1203 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1205 err = got_object_open_as_blob(&blob, repo,
1206 got_tree_entry_get_id(te), PATH_MAX);
1210 err = got_object_blob_read_to_str(link_target, blob);
1211 got_object_blob_close(blob);
1214 *link_target = NULL;
1220 got_tree_entry_get_index(struct got_tree_entry *te)
1225 struct got_tree_entry *
1226 got_tree_entry_get_next(struct got_tree_object *tree,
1227 struct got_tree_entry *te)
1229 return got_object_tree_get_entry(tree, te->idx + 1);
1232 struct got_tree_entry *
1233 got_tree_entry_get_prev(struct got_tree_object *tree,
1234 struct got_tree_entry *te)
1236 return got_object_tree_get_entry(tree, te->idx - 1);
1239 static const struct got_error *
1240 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1241 struct got_pack *pack, struct got_packidx *packidx, int idx,
1242 struct got_object_id *id)
1244 const struct got_error *err = NULL;
1246 int basefd, accumfd; /* temporary files for delta application */
1248 basefd = got_opentempfd();
1250 return got_error_from_errno("got_opentempfd");
1251 accumfd = got_opentempfd();
1253 return got_error_from_errno("got_opentempfd");
1255 outfd_child = dup(outfd);
1256 if (outfd_child == -1)
1257 return got_error_from_errno("dup");
1259 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1263 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1271 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1278 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1283 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1284 pack->privsep_child->ibuf);
1288 if (lseek(outfd, SEEK_SET, 0) == -1)
1289 err = got_error_from_errno("lseek");
1294 static const struct got_error *
1295 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1296 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1297 struct got_object_id *id)
1299 const struct got_error *err = NULL;
1301 if (pack->privsep_child == NULL) {
1302 err = start_pack_privsep_child(pack, packidx);
1307 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1311 static const struct got_error *
1312 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1313 int infd, struct imsgbuf *ibuf)
1315 const struct got_error *err = NULL;
1318 outfd_child = dup(outfd);
1319 if (outfd_child == -1)
1320 return got_error_from_errno("dup");
1322 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
1326 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1330 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1334 if (lseek(outfd, SEEK_SET, 0) == -1)
1335 return got_error_from_errno("lseek");
1340 static const struct got_error *
1341 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1342 int outfd, int infd, struct got_repository *repo)
1344 const struct got_error *err;
1347 struct imsgbuf *ibuf;
1349 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1350 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1351 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1354 ibuf = calloc(1, sizeof(*ibuf));
1356 return got_error_from_errno("calloc");
1358 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1359 err = got_error_from_errno("socketpair");
1366 err = got_error_from_errno("fork");
1370 else if (pid == 0) {
1371 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1376 if (close(imsg_fds[1]) == -1) {
1377 err = got_error_from_errno("close");
1381 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1383 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1384 imsg_init(ibuf, imsg_fds[0]);
1385 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1387 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1390 static const struct got_error *
1391 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1392 struct got_object_id *id, size_t blocksize)
1394 const struct got_error *err = NULL;
1395 struct got_packidx *packidx = NULL;
1397 char *path_packfile = NULL;
1400 size_t size, hdrlen;
1403 *blob = calloc(1, sizeof(**blob));
1405 return got_error_from_errno("calloc");
1407 outfd = got_opentempfd();
1409 return got_error_from_errno("got_opentempfd");
1411 (*blob)->read_buf = malloc(blocksize);
1412 if ((*blob)->read_buf == NULL) {
1413 err = got_error_from_errno("malloc");
1417 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1419 struct got_pack *pack = NULL;
1421 err = get_packfile_path(&path_packfile, packidx);
1425 pack = got_repo_get_cached_pack(repo, path_packfile);
1427 err = got_repo_cache_pack(&pack, repo, path_packfile,
1432 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1433 pack, packidx, idx, id);
1434 } else if (err->code == GOT_ERR_NO_OBJ) {
1437 err = open_loose_object(&infd, id, repo);
1440 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1446 if (hdrlen > size) {
1447 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1452 if (close(outfd) == -1 && err == NULL)
1453 err = got_error_from_errno("close");
1455 (*blob)->f = fmemopen(outbuf, size, "rb");
1456 if ((*blob)->f == NULL) {
1457 err = got_error_from_errno("fmemopen");
1461 (*blob)->data = outbuf;
1463 if (fstat(outfd, &sb) == -1) {
1464 err = got_error_from_errno("fstat");
1468 if (sb.st_size != size) {
1469 err = got_error(GOT_ERR_PRIVSEP_LEN);
1473 (*blob)->f = fdopen(outfd, "rb");
1474 if ((*blob)->f == NULL) {
1475 err = got_error_from_errno("fdopen");
1482 (*blob)->hdrlen = hdrlen;
1483 (*blob)->blocksize = blocksize;
1484 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1487 free(path_packfile);
1490 got_object_blob_close(*blob);
1492 } else if (outfd != -1)
1498 const struct got_error *
1499 got_object_open_as_blob(struct got_blob_object **blob,
1500 struct got_repository *repo, struct got_object_id *id,
1503 return open_blob(blob, repo, id, blocksize);
1506 const struct got_error *
1507 got_object_blob_open(struct got_blob_object **blob,
1508 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1510 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1513 const struct got_error *
1514 got_object_blob_close(struct got_blob_object *blob)
1516 const struct got_error *err = NULL;
1517 free(blob->read_buf);
1518 if (blob->f && fclose(blob->f) == EOF)
1519 err = got_error_from_errno("fclose");
1526 got_object_blob_rewind(struct got_blob_object *blob)
1533 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1535 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1539 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1541 return blob->hdrlen;
1545 got_object_blob_get_read_buf(struct got_blob_object *blob)
1547 return blob->read_buf;
1550 const struct got_error *
1551 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1555 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1556 if (n == 0 && ferror(blob->f))
1557 return got_ferror(blob->f, GOT_ERR_IO);
1562 const struct got_error *
1563 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1564 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1566 const struct got_error *err = NULL;
1567 size_t n, len, hdrlen;
1570 const int alloc_chunksz = 512;
1572 off_t off = 0, total_len = 0;
1575 *line_offsets = NULL;
1581 hdrlen = got_object_blob_get_hdrlen(blob);
1583 err = got_object_blob_read_block(&len, blob);
1588 buf = got_object_blob_get_read_buf(blob);
1591 if (line_offsets && *line_offsets == NULL) {
1592 /* Have some data but perhaps no '\n'. */
1594 nalloc = alloc_chunksz;
1595 *line_offsets = calloc(nalloc,
1596 sizeof(**line_offsets));
1597 if (*line_offsets == NULL)
1598 return got_error_from_errno("calloc");
1600 /* Skip forward over end of first line. */
1607 /* Scan '\n' offsets in remaining chunk of data. */
1609 if (buf[i] != '\n') {
1614 if (line_offsets && nalloc < *nlines) {
1615 size_t n = *nlines + alloc_chunksz;
1616 off_t *o = recallocarray(*line_offsets,
1617 nalloc, n, sizeof(**line_offsets));
1619 free(*line_offsets);
1620 *line_offsets = NULL;
1621 return got_error_from_errno(
1628 off = total_len + i - hdrlen + 1;
1629 (*line_offsets)[*nlines - 1] = off;
1634 /* Skip blob object header first time around. */
1635 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1636 if (n != len - hdrlen)
1637 return got_ferror(outfile, GOT_ERR_IO);
1638 total_len += len - hdrlen;
1642 if (fflush(outfile) != 0)
1643 return got_error_from_errno("fflush");
1647 *filesize = total_len;
1652 static const struct got_error *
1653 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1654 int pack_idx, struct got_object_id *id)
1656 const struct got_error *err = NULL;
1658 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1663 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1666 static const struct got_error *
1667 read_packed_tag_privsep(struct got_tag_object **tag,
1668 struct got_pack *pack, struct got_packidx *packidx, int idx,
1669 struct got_object_id *id)
1671 const struct got_error *err = NULL;
1673 if (pack->privsep_child)
1674 return request_packed_tag(tag, pack, idx, id);
1676 err = start_pack_privsep_child(pack, packidx);
1680 return request_packed_tag(tag, pack, idx, id);
1683 static const struct got_error *
1684 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1687 const struct got_error *err = NULL;
1688 struct imsgbuf *ibuf;
1690 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1692 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1696 return got_privsep_recv_tag(tag, ibuf);
1699 static const struct got_error *
1700 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1701 struct got_repository *repo)
1703 const struct got_error *err;
1706 struct imsgbuf *ibuf;
1708 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1709 return request_tag(tag, repo, obj_fd);
1711 ibuf = calloc(1, sizeof(*ibuf));
1713 return got_error_from_errno("calloc");
1715 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1716 err = got_error_from_errno("socketpair");
1723 err = got_error_from_errno("fork");
1727 else if (pid == 0) {
1728 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1733 if (close(imsg_fds[1]) == -1) {
1734 err = got_error_from_errno("close");
1738 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1740 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1741 imsg_init(ibuf, imsg_fds[0]);
1742 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1744 return request_tag(tag, repo, obj_fd);
1747 static const struct got_error *
1748 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1749 struct got_object_id *id, int check_cache)
1751 const struct got_error *err = NULL;
1752 struct got_packidx *packidx = NULL;
1754 char *path_packfile = NULL;
1755 struct got_object *obj = NULL;
1756 int obj_type = GOT_OBJ_TYPE_ANY;
1759 *tag = got_repo_get_cached_tag(repo, id);
1767 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1769 struct got_pack *pack = NULL;
1771 err = get_packfile_path(&path_packfile, packidx);
1775 pack = got_repo_get_cached_pack(repo, path_packfile);
1777 err = got_repo_cache_pack(&pack, repo, path_packfile,
1783 /* Beware of "lightweight" tags: Check object type first. */
1784 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1788 obj_type = obj->type;
1789 got_object_close(obj);
1790 if (obj_type != GOT_OBJ_TYPE_TAG) {
1791 err = got_error(GOT_ERR_OBJ_TYPE);
1794 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1795 } else if (err->code == GOT_ERR_NO_OBJ) {
1798 err = open_loose_object(&fd, id, repo);
1801 err = read_object_header_privsep(&obj, repo, fd);
1804 obj_type = obj->type;
1805 got_object_close(obj);
1806 if (obj_type != GOT_OBJ_TYPE_TAG)
1807 return got_error(GOT_ERR_OBJ_TYPE);
1809 err = open_loose_object(&fd, id, repo);
1812 err = read_tag_privsep(tag, fd, repo);
1817 err = got_repo_cache_tag(repo, id, *tag);
1820 free(path_packfile);
1824 const struct got_error *
1825 got_object_open_as_tag(struct got_tag_object **tag,
1826 struct got_repository *repo, struct got_object_id *id)
1828 *tag = got_repo_get_cached_tag(repo, id);
1834 return open_tag(tag, repo, id, 0);
1837 const struct got_error *
1838 got_object_tag_open(struct got_tag_object **tag,
1839 struct got_repository *repo, struct got_object *obj)
1841 return open_tag(tag, repo, got_object_get_id(obj), 1);
1845 got_object_tag_get_name(struct got_tag_object *tag)
1851 got_object_tag_get_object_type(struct got_tag_object *tag)
1853 return tag->obj_type;
1856 struct got_object_id *
1857 got_object_tag_get_object_id(struct got_tag_object *tag)
1863 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1865 return tag->tagger_time;
1869 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1871 return tag->tagger_gmtoff;
1875 got_object_tag_get_tagger(struct got_tag_object *tag)
1881 got_object_tag_get_message(struct got_tag_object *tag)
1886 static struct got_tree_entry *
1887 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1891 /* Note that tree entries are sorted in strncmp() order. */
1892 for (i = 0; i < tree->nentries; i++) {
1893 struct got_tree_entry *te = &tree->entries[i];
1894 int cmp = strncmp(te->name, name, len);
1899 if (te->name[len] == '\0')
1905 struct got_tree_entry *
1906 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1908 return find_entry_by_name(tree, name, strlen(name));
1911 const struct got_error *
1912 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1913 struct got_object_id *commit_id, const char *path)
1915 const struct got_error *err = NULL;
1916 struct got_commit_object *commit = NULL;
1917 struct got_tree_object *tree = NULL;
1918 struct got_tree_entry *te = NULL;
1919 const char *seg, *s;
1924 err = got_object_open_as_commit(&commit, repo, commit_id);
1928 /* Handle opening of root of commit's tree. */
1929 if (got_path_is_root_dir(path)) {
1930 *id = got_object_id_dup(commit->tree_id);
1932 err = got_error_from_errno("got_object_id_dup");
1936 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1946 struct got_tree_object *next_tree;
1955 te = find_entry_by_name(tree, seg, seglen);
1957 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1968 err = got_object_open_as_tree(&next_tree, repo,
1973 got_object_tree_close(tree);
1979 *id = got_object_id_dup(&te->id);
1981 return got_error_from_errno("got_object_id_dup");
1983 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1986 got_object_commit_close(commit);
1988 got_object_tree_close(tree);
1993 * Normalize file mode bits to avoid false positive tree entry differences
1994 * in case tree entries have unexpected mode bits set.
1997 normalize_mode_for_comparison(mode_t mode)
2000 * For directories, the only relevant bit is the IFDIR bit.
2001 * This allows us to detect paths changing from a directory
2002 * to a file and vice versa.
2005 return mode & S_IFDIR;
2008 * For symlinks, the only relevant bit is the IFLNK bit.
2009 * This allows us to detect paths changing from a symlinks
2010 * to a file or directory and vice versa.
2013 return mode & S_IFLNK;
2015 /* For files, the only change we care about is the executable bit. */
2016 return mode & S_IXUSR;
2019 const struct got_error *
2020 got_object_tree_path_changed(int *changed,
2021 struct got_tree_object *tree01, struct got_tree_object *tree02,
2022 const char *path, struct got_repository *repo)
2024 const struct got_error *err = NULL;
2025 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2026 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2027 const char *seg, *s;
2032 /* We not do support comparing the root path. */
2033 if (got_path_is_root_dir(path))
2034 return got_error_path(path, GOT_ERR_BAD_PATH);
2044 struct got_tree_object *next_tree1, *next_tree2;
2045 mode_t mode1, mode2;
2054 te1 = find_entry_by_name(tree1, seg, seglen);
2056 err = got_error(GOT_ERR_NO_OBJ);
2061 te2 = find_entry_by_name(tree2, seg, seglen);
2064 mode1 = normalize_mode_for_comparison(te1->mode);
2065 mode2 = normalize_mode_for_comparison(te2->mode);
2066 if (mode1 != mode2) {
2071 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2077 if (*s == '\0') { /* final path element */
2086 err = got_object_open_as_tree(&next_tree1, repo,
2091 if (tree1 != tree01)
2092 got_object_tree_close(tree1);
2096 err = got_object_open_as_tree(&next_tree2, repo,
2101 if (tree2 != tree02)
2102 got_object_tree_close(tree2);
2105 if (tree2 != tree02)
2106 got_object_tree_close(tree2);
2112 if (tree1 && tree1 != tree01)
2113 got_object_tree_close(tree1);
2114 if (tree2 && tree2 != tree02)
2115 got_object_tree_close(tree2);
2119 const struct got_error *
2120 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2121 struct got_tree_entry *te)
2123 const struct got_error *err = NULL;
2125 *new_te = calloc(1, sizeof(**new_te));
2126 if (*new_te == NULL)
2127 return got_error_from_errno("calloc");
2129 (*new_te)->mode = te->mode;
2130 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2131 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2136 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2138 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2142 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2144 /* S_IFDIR check avoids confusing symlinks with submodules. */
2145 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2148 static const struct got_error *
2149 resolve_symlink(char **link_target, const char *path,
2150 struct got_object_id *commit_id, struct got_repository *repo)
2152 const struct got_error *err = NULL;
2154 char *name, *parent_path = NULL;
2155 struct got_object_id *tree_obj_id = NULL;
2156 struct got_tree_object *tree = NULL;
2157 struct got_tree_entry *te = NULL;
2159 *link_target = NULL;
2161 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2162 return got_error(GOT_ERR_NO_SPACE);
2164 name = basename(buf);
2166 return got_error_from_errno2("basename", path);
2168 err = got_path_dirname(&parent_path, path);
2172 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2175 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2176 /* Display the complete path in error message. */
2177 err = got_error_path(path, err->code);
2182 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2186 te = got_object_tree_find_entry(tree, name);
2188 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2192 if (got_object_tree_entry_is_symlink(te)) {
2193 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2196 if (!got_path_is_absolute(*link_target)) {
2198 if (asprintf(&abspath, "%s/%s", parent_path,
2199 *link_target) == -1) {
2200 err = got_error_from_errno("asprintf");
2204 *link_target = malloc(PATH_MAX);
2205 if (*link_target == NULL) {
2206 err = got_error_from_errno("malloc");
2209 err = got_canonpath(abspath, *link_target, PATH_MAX);
2218 got_object_tree_close(tree);
2221 *link_target = NULL;
2226 const struct got_error *
2227 got_object_resolve_symlinks(char **link_target, const char *path,
2228 struct got_object_id *commit_id, struct got_repository *repo)
2230 const struct got_error *err = NULL;
2231 char *next_target = NULL;
2232 int max_recursion = 40; /* matches Git */
2234 *link_target = NULL;
2237 err = resolve_symlink(&next_target,
2238 *link_target ? *link_target : path, commit_id, repo);
2243 if (--max_recursion == 0) {
2244 err = got_error_path(path, GOT_ERR_RECURSION);
2245 *link_target = NULL;
2248 *link_target = next_target;
2250 } while (next_target);
2255 const struct got_error *
2256 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2257 struct got_object_id *commit_id, const char *path,
2258 struct got_repository *repo)
2260 const struct got_error *err = NULL;
2261 struct got_pack *pack = NULL;
2262 struct got_packidx *packidx = NULL;
2263 char *path_packfile = NULL;
2264 struct got_commit_object *changed_commit = NULL;
2265 struct got_object_id *changed_commit_id = NULL;
2268 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2270 if (err->code != GOT_ERR_NO_OBJ)
2275 err = get_packfile_path(&path_packfile, packidx);
2279 pack = got_repo_get_cached_pack(repo, path_packfile);
2281 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2286 if (pack->privsep_child == NULL) {
2287 err = start_pack_privsep_child(pack, packidx);
2292 err = got_privsep_send_commit_traversal_request(
2293 pack->privsep_child->ibuf, commit_id, idx, path);
2297 err = got_privsep_recv_traversed_commits(&changed_commit,
2298 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2302 if (changed_commit) {
2304 * Cache the commit in which the path was changed.
2305 * This commit might be opened again soon.
2307 changed_commit->refcnt++;
2308 err = got_repo_cache_commit(repo, changed_commit_id,
2310 got_object_commit_close(changed_commit);
2313 free(path_packfile);
2314 free(changed_commit_id);