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);
493 return request_object(obj, repo, obj_fd);
496 static const struct got_error *
497 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
498 int outfd, struct got_repository *repo, int obj_fd)
500 const struct got_error *err;
502 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
503 return request_raw_object(outbuf, size, hdrlen, outfd, repo,
506 err = start_read_object_child(repo);
510 return request_raw_object(outbuf, size, hdrlen, outfd, repo, obj_fd);
513 const struct got_error *
514 got_object_open(struct got_object **obj, struct got_repository *repo,
515 struct got_object_id *id)
517 const struct got_error *err = NULL;
521 *obj = got_repo_get_cached_object(repo, id);
527 err = open_packed_object(obj, id, repo);
528 if (err && err->code != GOT_ERR_NO_OBJ)
532 return got_repo_cache_object(repo, id, *obj);
535 err = got_object_get_path(&path, id, repo);
539 fd = open(path, O_RDONLY | O_NOFOLLOW);
542 err = got_error_no_obj(id);
544 err = got_error_from_errno2("open", path);
547 err = read_object_header_privsep(obj, repo, fd);
550 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
554 err = got_repo_cache_object(repo, id, *obj);
560 const struct got_error *
561 got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo,
562 struct got_object_id *id, size_t blocksize)
564 const struct got_error *err = NULL;
565 struct got_packidx *packidx = NULL;
567 uint8_t *outbuf = NULL;
571 char *path_packfile = NULL;
575 outfd = got_opentempfd();
577 return got_error_from_errno("got_opentempfd");
579 err = got_repo_search_packidx(&packidx, &idx, repo, id);
581 struct got_pack *pack = NULL;
583 err = get_packfile_path(&path_packfile, packidx);
587 pack = got_repo_get_cached_pack(repo, path_packfile);
589 err = got_repo_cache_pack(&pack, repo, path_packfile,
594 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
595 outfd, pack, packidx, idx, id);
596 } else if (err->code == GOT_ERR_NO_OBJ) {
599 err = open_loose_object(&fd, id, repo);
602 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
607 err = got_error(GOT_ERR_BAD_OBJ_HDR);
611 *obj = calloc(1, sizeof(**obj));
613 err = got_error_from_errno("calloc");
617 (*obj)->read_buf = malloc(blocksize);
618 if ((*obj)->read_buf == NULL) {
619 err = got_error_from_errno("malloc");
624 if (close(outfd) == -1) {
625 err = got_error_from_errno("close");
629 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
630 if ((*obj)->f == NULL) {
631 err = got_error_from_errno("fdopen");
634 (*obj)->data = outbuf;
637 if (fstat(outfd, &sb) == -1) {
638 err = got_error_from_errno("fstat");
642 if (sb.st_size != size) {
643 err = got_error(GOT_ERR_PRIVSEP_LEN);
647 (*obj)->f = fdopen(outfd, "r");
648 if ((*obj)->f == NULL) {
649 err = got_error_from_errno("fdopen");
655 (*obj)->hdrlen = hdrlen;
657 (*obj)->blocksize = blocksize;
662 got_object_raw_close(*obj);
673 got_object_raw_rewind(struct got_raw_object *obj)
680 got_object_raw_get_hdrlen(struct got_raw_object *obj)
686 got_object_raw_get_read_buf(struct got_raw_object *obj)
688 return obj->read_buf;
691 const struct got_error *
692 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
696 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
697 if (n == 0 && ferror(obj->f))
698 return got_ferror(obj->f, GOT_ERR_IO);
703 const struct got_error *
704 got_object_raw_close(struct got_raw_object *obj)
706 const struct got_error *err = NULL;
709 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
710 err = got_error_from_errno("fclose");
716 const struct got_error *
717 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
720 struct got_object_id id;
722 if (!got_parse_sha1_digest(id.sha1, id_str))
723 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
725 return got_object_open(obj, repo, &id);
728 const struct got_error *
729 got_object_resolve_id_str(struct got_object_id **id,
730 struct got_repository *repo, const char *id_str)
732 const struct got_error *err = NULL;
733 struct got_object *obj;
735 err = got_object_open_by_id_str(&obj, repo, id_str);
739 *id = got_object_id_dup(got_object_get_id(obj));
740 got_object_close(obj);
742 return got_error_from_errno("got_object_id_dup");
747 static const struct got_error *
748 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
749 int pack_idx, struct got_object_id *id)
751 const struct got_error *err = NULL;
753 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
758 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
762 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
766 static const struct got_error *
767 read_packed_commit_privsep(struct got_commit_object **commit,
768 struct got_pack *pack, struct got_packidx *packidx, int idx,
769 struct got_object_id *id)
771 const struct got_error *err = NULL;
773 if (pack->privsep_child)
774 return request_packed_commit(commit, pack, idx, id);
776 err = start_pack_privsep_child(pack, packidx);
780 return request_packed_commit(commit, pack, idx, id);
783 static const struct got_error *
784 request_commit(struct got_commit_object **commit, struct got_repository *repo,
787 const struct got_error *err = NULL;
788 struct imsgbuf *ibuf;
790 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
792 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
796 return got_privsep_recv_commit(commit, ibuf);
799 static const struct got_error *
800 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
801 struct got_repository *repo)
803 const struct got_error *err;
806 struct imsgbuf *ibuf;
808 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
809 return request_commit(commit, repo, obj_fd);
811 ibuf = calloc(1, sizeof(*ibuf));
813 return got_error_from_errno("calloc");
815 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
816 err = got_error_from_errno("socketpair");
823 err = got_error_from_errno("fork");
828 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
833 if (close(imsg_fds[1]) == -1) {
834 err = got_error_from_errno("close");
838 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
840 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
841 imsg_init(ibuf, imsg_fds[0]);
842 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
844 return request_commit(commit, repo, obj_fd);
848 static const struct got_error *
849 open_commit(struct got_commit_object **commit,
850 struct got_repository *repo, struct got_object_id *id, int check_cache)
852 const struct got_error *err = NULL;
853 struct got_packidx *packidx = NULL;
855 char *path_packfile = NULL;
858 *commit = got_repo_get_cached_commit(repo, id);
859 if (*commit != NULL) {
866 err = got_repo_search_packidx(&packidx, &idx, repo, id);
868 struct got_pack *pack = NULL;
870 err = get_packfile_path(&path_packfile, packidx);
874 pack = got_repo_get_cached_pack(repo, path_packfile);
876 err = got_repo_cache_pack(&pack, repo, path_packfile,
881 err = read_packed_commit_privsep(commit, pack,
883 } else if (err->code == GOT_ERR_NO_OBJ) {
886 err = open_loose_object(&fd, id, repo);
889 err = read_commit_privsep(commit, fd, repo);
894 err = got_repo_cache_commit(repo, id, *commit);
901 const struct got_error *
902 got_object_open_as_commit(struct got_commit_object **commit,
903 struct got_repository *repo, struct got_object_id *id)
905 *commit = got_repo_get_cached_commit(repo, id);
906 if (*commit != NULL) {
911 return open_commit(commit, repo, id, 0);
914 const struct got_error *
915 got_object_commit_open(struct got_commit_object **commit,
916 struct got_repository *repo, struct got_object *obj)
918 return open_commit(commit, repo, got_object_get_id(obj), 1);
921 const struct got_error *
922 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
924 const struct got_error *err = NULL;
926 *qid = calloc(1, sizeof(**qid));
928 return got_error_from_errno("calloc");
930 (*qid)->id = got_object_id_dup(id);
931 if ((*qid)->id == NULL) {
932 err = got_error_from_errno("got_object_id_dup");
933 got_object_qid_free(*qid);
941 static const struct got_error *
942 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
943 int pack_idx, struct got_object_id *id)
945 const struct got_error *err = NULL;
947 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
952 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
955 static const struct got_error *
956 read_packed_tree_privsep(struct got_tree_object **tree,
957 struct got_pack *pack, struct got_packidx *packidx, int idx,
958 struct got_object_id *id)
960 const struct got_error *err = NULL;
962 if (pack->privsep_child)
963 return request_packed_tree(tree, pack, idx, id);
965 err = start_pack_privsep_child(pack, packidx);
969 return request_packed_tree(tree, pack, idx, id);
972 static const struct got_error *
973 request_tree(struct got_tree_object **tree, struct got_repository *repo,
976 const struct got_error *err = NULL;
977 struct imsgbuf *ibuf;
979 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
981 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
985 return got_privsep_recv_tree(tree, ibuf);
988 const struct got_error *
989 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
990 struct got_repository *repo)
992 const struct got_error *err;
995 struct imsgbuf *ibuf;
997 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
998 return request_tree(tree, repo, obj_fd);
1000 ibuf = calloc(1, sizeof(*ibuf));
1002 return got_error_from_errno("calloc");
1004 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1005 err = got_error_from_errno("socketpair");
1012 err = got_error_from_errno("fork");
1016 else if (pid == 0) {
1017 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1022 if (close(imsg_fds[1]) == -1) {
1023 err = got_error_from_errno("close");
1027 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1029 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1030 imsg_init(ibuf, imsg_fds[0]);
1031 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1034 return request_tree(tree, repo, obj_fd);
1037 static const struct got_error *
1038 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1039 struct got_object_id *id, int check_cache)
1041 const struct got_error *err = NULL;
1042 struct got_packidx *packidx = NULL;
1044 char *path_packfile = NULL;
1047 *tree = got_repo_get_cached_tree(repo, id);
1048 if (*tree != NULL) {
1055 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1057 struct got_pack *pack = NULL;
1059 err = get_packfile_path(&path_packfile, packidx);
1063 pack = got_repo_get_cached_pack(repo, path_packfile);
1065 err = got_repo_cache_pack(&pack, repo, path_packfile,
1070 err = read_packed_tree_privsep(tree, pack,
1072 } else if (err->code == GOT_ERR_NO_OBJ) {
1075 err = open_loose_object(&fd, id, repo);
1078 err = read_tree_privsep(tree, fd, repo);
1083 err = got_repo_cache_tree(repo, id, *tree);
1086 free(path_packfile);
1090 const struct got_error *
1091 got_object_open_as_tree(struct got_tree_object **tree,
1092 struct got_repository *repo, struct got_object_id *id)
1094 *tree = got_repo_get_cached_tree(repo, id);
1095 if (*tree != NULL) {
1100 return open_tree(tree, repo, id, 0);
1103 const struct got_error *
1104 got_object_tree_open(struct got_tree_object **tree,
1105 struct got_repository *repo, struct got_object *obj)
1107 return open_tree(tree, repo, got_object_get_id(obj), 1);
1111 got_object_tree_get_nentries(struct got_tree_object *tree)
1113 return tree->nentries;
1116 struct got_tree_entry *
1117 got_object_tree_get_first_entry(struct got_tree_object *tree)
1119 return got_object_tree_get_entry(tree, 0);
1122 struct got_tree_entry *
1123 got_object_tree_get_last_entry(struct got_tree_object *tree)
1125 return got_object_tree_get_entry(tree, tree->nentries - 1);
1128 struct got_tree_entry *
1129 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1131 if (i < 0 || i >= tree->nentries)
1133 return &tree->entries[i];
1137 got_tree_entry_get_mode(struct got_tree_entry *te)
1143 got_tree_entry_get_name(struct got_tree_entry *te)
1145 return &te->name[0];
1148 struct got_object_id *
1149 got_tree_entry_get_id(struct got_tree_entry *te)
1154 const struct got_error *
1155 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1157 const struct got_error *err = NULL;
1158 size_t len, totlen, hdrlen, offset;
1162 hdrlen = got_object_blob_get_hdrlen(blob);
1168 err = got_object_blob_read_block(&len, blob);
1175 totlen += len - hdrlen;
1176 p = realloc(*s, totlen + 1);
1178 err = got_error_from_errno("realloc");
1184 /* Skip blob object header first time around. */
1186 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1191 (*s)[totlen] = '\0';
1195 const struct got_error *
1196 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1197 struct got_repository *repo)
1199 const struct got_error *err = NULL;
1200 struct got_blob_object *blob = NULL;
1202 *link_target = NULL;
1204 if (!got_object_tree_entry_is_symlink(te))
1205 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1207 err = got_object_open_as_blob(&blob, repo,
1208 got_tree_entry_get_id(te), PATH_MAX);
1212 err = got_object_blob_read_to_str(link_target, blob);
1213 got_object_blob_close(blob);
1216 *link_target = NULL;
1222 got_tree_entry_get_index(struct got_tree_entry *te)
1227 struct got_tree_entry *
1228 got_tree_entry_get_next(struct got_tree_object *tree,
1229 struct got_tree_entry *te)
1231 return got_object_tree_get_entry(tree, te->idx + 1);
1234 struct got_tree_entry *
1235 got_tree_entry_get_prev(struct got_tree_object *tree,
1236 struct got_tree_entry *te)
1238 return got_object_tree_get_entry(tree, te->idx - 1);
1241 static const struct got_error *
1242 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1243 struct got_pack *pack, struct got_packidx *packidx, int idx,
1244 struct got_object_id *id)
1246 const struct got_error *err = NULL;
1248 int basefd, accumfd; /* temporary files for delta application */
1250 basefd = got_opentempfd();
1252 return got_error_from_errno("got_opentempfd");
1253 accumfd = got_opentempfd();
1255 return got_error_from_errno("got_opentempfd");
1257 outfd_child = dup(outfd);
1258 if (outfd_child == -1)
1259 return got_error_from_errno("dup");
1261 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1265 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1273 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1280 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1285 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1286 pack->privsep_child->ibuf);
1290 if (lseek(outfd, SEEK_SET, 0) == -1)
1291 err = got_error_from_errno("lseek");
1296 static const struct got_error *
1297 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1298 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1299 struct got_object_id *id)
1301 const struct got_error *err = NULL;
1303 if (pack->privsep_child == NULL) {
1304 err = start_pack_privsep_child(pack, packidx);
1309 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1313 static const struct got_error *
1314 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1315 int infd, struct imsgbuf *ibuf)
1317 const struct got_error *err = NULL;
1320 outfd_child = dup(outfd);
1321 if (outfd_child == -1)
1322 return got_error_from_errno("dup");
1324 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
1328 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1332 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1336 if (lseek(outfd, SEEK_SET, 0) == -1)
1337 return got_error_from_errno("lseek");
1342 static const struct got_error *
1343 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1344 int outfd, int infd, struct got_repository *repo)
1346 const struct got_error *err;
1349 struct imsgbuf *ibuf;
1351 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1352 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1353 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1356 ibuf = calloc(1, sizeof(*ibuf));
1358 return got_error_from_errno("calloc");
1360 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1361 err = got_error_from_errno("socketpair");
1368 err = got_error_from_errno("fork");
1372 else if (pid == 0) {
1373 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1378 if (close(imsg_fds[1]) == -1) {
1379 err = got_error_from_errno("close");
1383 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1385 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1386 imsg_init(ibuf, imsg_fds[0]);
1387 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1389 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1392 static const struct got_error *
1393 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1394 struct got_object_id *id, size_t blocksize)
1396 const struct got_error *err = NULL;
1397 struct got_packidx *packidx = NULL;
1399 char *path_packfile = NULL;
1402 size_t size, hdrlen;
1405 *blob = calloc(1, sizeof(**blob));
1407 return got_error_from_errno("calloc");
1409 outfd = got_opentempfd();
1411 return got_error_from_errno("got_opentempfd");
1413 (*blob)->read_buf = malloc(blocksize);
1414 if ((*blob)->read_buf == NULL) {
1415 err = got_error_from_errno("malloc");
1419 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1421 struct got_pack *pack = NULL;
1423 err = get_packfile_path(&path_packfile, packidx);
1427 pack = got_repo_get_cached_pack(repo, path_packfile);
1429 err = got_repo_cache_pack(&pack, repo, path_packfile,
1434 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1435 pack, packidx, idx, id);
1436 } else if (err->code == GOT_ERR_NO_OBJ) {
1439 err = open_loose_object(&infd, id, repo);
1442 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1448 if (hdrlen > size) {
1449 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1454 if (close(outfd) == -1 && err == NULL)
1455 err = got_error_from_errno("close");
1457 (*blob)->f = fmemopen(outbuf, size, "rb");
1458 if ((*blob)->f == NULL) {
1459 err = got_error_from_errno("fmemopen");
1463 (*blob)->data = outbuf;
1465 if (fstat(outfd, &sb) == -1) {
1466 err = got_error_from_errno("fstat");
1470 if (sb.st_size != size) {
1471 err = got_error(GOT_ERR_PRIVSEP_LEN);
1475 (*blob)->f = fdopen(outfd, "rb");
1476 if ((*blob)->f == NULL) {
1477 err = got_error_from_errno("fdopen");
1484 (*blob)->hdrlen = hdrlen;
1485 (*blob)->blocksize = blocksize;
1486 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1489 free(path_packfile);
1492 got_object_blob_close(*blob);
1494 } else if (outfd != -1)
1500 const struct got_error *
1501 got_object_open_as_blob(struct got_blob_object **blob,
1502 struct got_repository *repo, struct got_object_id *id,
1505 return open_blob(blob, repo, id, blocksize);
1508 const struct got_error *
1509 got_object_blob_open(struct got_blob_object **blob,
1510 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1512 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1515 const struct got_error *
1516 got_object_blob_close(struct got_blob_object *blob)
1518 const struct got_error *err = NULL;
1519 free(blob->read_buf);
1520 if (blob->f && fclose(blob->f) == EOF)
1521 err = got_error_from_errno("fclose");
1528 got_object_blob_rewind(struct got_blob_object *blob)
1535 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1537 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1541 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1543 return blob->hdrlen;
1547 got_object_blob_get_read_buf(struct got_blob_object *blob)
1549 return blob->read_buf;
1552 const struct got_error *
1553 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1557 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1558 if (n == 0 && ferror(blob->f))
1559 return got_ferror(blob->f, GOT_ERR_IO);
1564 const struct got_error *
1565 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1566 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1568 const struct got_error *err = NULL;
1569 size_t n, len, hdrlen;
1572 const int alloc_chunksz = 512;
1574 off_t off = 0, total_len = 0;
1577 *line_offsets = NULL;
1583 hdrlen = got_object_blob_get_hdrlen(blob);
1585 err = got_object_blob_read_block(&len, blob);
1590 buf = got_object_blob_get_read_buf(blob);
1593 if (line_offsets && *line_offsets == NULL) {
1594 /* Have some data but perhaps no '\n'. */
1596 nalloc = alloc_chunksz;
1597 *line_offsets = calloc(nalloc,
1598 sizeof(**line_offsets));
1599 if (*line_offsets == NULL)
1600 return got_error_from_errno("calloc");
1602 /* Skip forward over end of first line. */
1609 /* Scan '\n' offsets in remaining chunk of data. */
1611 if (buf[i] != '\n') {
1616 if (line_offsets && nalloc < *nlines) {
1617 size_t n = *nlines + alloc_chunksz;
1618 off_t *o = recallocarray(*line_offsets,
1619 nalloc, n, sizeof(**line_offsets));
1621 free(*line_offsets);
1622 *line_offsets = NULL;
1623 return got_error_from_errno(
1630 off = total_len + i - hdrlen + 1;
1631 (*line_offsets)[*nlines - 1] = off;
1636 /* Skip blob object header first time around. */
1637 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1638 if (n != len - hdrlen)
1639 return got_ferror(outfile, GOT_ERR_IO);
1640 total_len += len - hdrlen;
1644 if (fflush(outfile) != 0)
1645 return got_error_from_errno("fflush");
1649 *filesize = total_len;
1654 static const struct got_error *
1655 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1656 int pack_idx, struct got_object_id *id)
1658 const struct got_error *err = NULL;
1660 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1665 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1668 static const struct got_error *
1669 read_packed_tag_privsep(struct got_tag_object **tag,
1670 struct got_pack *pack, struct got_packidx *packidx, int idx,
1671 struct got_object_id *id)
1673 const struct got_error *err = NULL;
1675 if (pack->privsep_child)
1676 return request_packed_tag(tag, pack, idx, id);
1678 err = start_pack_privsep_child(pack, packidx);
1682 return request_packed_tag(tag, pack, idx, id);
1685 static const struct got_error *
1686 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1689 const struct got_error *err = NULL;
1690 struct imsgbuf *ibuf;
1692 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1694 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1698 return got_privsep_recv_tag(tag, ibuf);
1701 static const struct got_error *
1702 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1703 struct got_repository *repo)
1705 const struct got_error *err;
1708 struct imsgbuf *ibuf;
1710 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1711 return request_tag(tag, repo, obj_fd);
1713 ibuf = calloc(1, sizeof(*ibuf));
1715 return got_error_from_errno("calloc");
1717 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1718 err = got_error_from_errno("socketpair");
1725 err = got_error_from_errno("fork");
1729 else if (pid == 0) {
1730 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1735 if (close(imsg_fds[1]) == -1) {
1736 err = got_error_from_errno("close");
1740 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1742 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1743 imsg_init(ibuf, imsg_fds[0]);
1744 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1746 return request_tag(tag, repo, obj_fd);
1749 static const struct got_error *
1750 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1751 struct got_object_id *id, int check_cache)
1753 const struct got_error *err = NULL;
1754 struct got_packidx *packidx = NULL;
1756 char *path_packfile = NULL;
1757 struct got_object *obj = NULL;
1758 int obj_type = GOT_OBJ_TYPE_ANY;
1761 *tag = got_repo_get_cached_tag(repo, id);
1769 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1771 struct got_pack *pack = NULL;
1773 err = get_packfile_path(&path_packfile, packidx);
1777 pack = got_repo_get_cached_pack(repo, path_packfile);
1779 err = got_repo_cache_pack(&pack, repo, path_packfile,
1785 /* Beware of "lightweight" tags: Check object type first. */
1786 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1790 obj_type = obj->type;
1791 got_object_close(obj);
1792 if (obj_type != GOT_OBJ_TYPE_TAG) {
1793 err = got_error(GOT_ERR_OBJ_TYPE);
1796 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1797 } else if (err->code == GOT_ERR_NO_OBJ) {
1800 err = open_loose_object(&fd, id, repo);
1803 err = read_object_header_privsep(&obj, repo, fd);
1806 obj_type = obj->type;
1807 got_object_close(obj);
1808 if (obj_type != GOT_OBJ_TYPE_TAG)
1809 return got_error(GOT_ERR_OBJ_TYPE);
1811 err = open_loose_object(&fd, id, repo);
1814 err = read_tag_privsep(tag, fd, repo);
1819 err = got_repo_cache_tag(repo, id, *tag);
1822 free(path_packfile);
1826 const struct got_error *
1827 got_object_open_as_tag(struct got_tag_object **tag,
1828 struct got_repository *repo, struct got_object_id *id)
1830 *tag = got_repo_get_cached_tag(repo, id);
1836 return open_tag(tag, repo, id, 0);
1839 const struct got_error *
1840 got_object_tag_open(struct got_tag_object **tag,
1841 struct got_repository *repo, struct got_object *obj)
1843 return open_tag(tag, repo, got_object_get_id(obj), 1);
1847 got_object_tag_get_name(struct got_tag_object *tag)
1853 got_object_tag_get_object_type(struct got_tag_object *tag)
1855 return tag->obj_type;
1858 struct got_object_id *
1859 got_object_tag_get_object_id(struct got_tag_object *tag)
1865 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1867 return tag->tagger_time;
1871 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1873 return tag->tagger_gmtoff;
1877 got_object_tag_get_tagger(struct got_tag_object *tag)
1883 got_object_tag_get_message(struct got_tag_object *tag)
1888 static struct got_tree_entry *
1889 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1893 /* Note that tree entries are sorted in strncmp() order. */
1894 for (i = 0; i < tree->nentries; i++) {
1895 struct got_tree_entry *te = &tree->entries[i];
1896 int cmp = strncmp(te->name, name, len);
1901 if (te->name[len] == '\0')
1907 struct got_tree_entry *
1908 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1910 return find_entry_by_name(tree, name, strlen(name));
1913 const struct got_error *
1914 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1915 struct got_object_id *commit_id, const char *path)
1917 const struct got_error *err = NULL;
1918 struct got_commit_object *commit = NULL;
1919 struct got_tree_object *tree = NULL;
1920 struct got_tree_entry *te = NULL;
1921 const char *seg, *s;
1926 err = got_object_open_as_commit(&commit, repo, commit_id);
1930 /* Handle opening of root of commit's tree. */
1931 if (got_path_is_root_dir(path)) {
1932 *id = got_object_id_dup(commit->tree_id);
1934 err = got_error_from_errno("got_object_id_dup");
1938 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1948 struct got_tree_object *next_tree;
1957 te = find_entry_by_name(tree, seg, seglen);
1959 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1970 err = got_object_open_as_tree(&next_tree, repo,
1975 got_object_tree_close(tree);
1981 *id = got_object_id_dup(&te->id);
1983 return got_error_from_errno("got_object_id_dup");
1985 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1988 got_object_commit_close(commit);
1990 got_object_tree_close(tree);
1995 * Normalize file mode bits to avoid false positive tree entry differences
1996 * in case tree entries have unexpected mode bits set.
1999 normalize_mode_for_comparison(mode_t mode)
2002 * For directories, the only relevant bit is the IFDIR bit.
2003 * This allows us to detect paths changing from a directory
2004 * to a file and vice versa.
2007 return mode & S_IFDIR;
2010 * For symlinks, the only relevant bit is the IFLNK bit.
2011 * This allows us to detect paths changing from a symlinks
2012 * to a file or directory and vice versa.
2015 return mode & S_IFLNK;
2017 /* For files, the only change we care about is the executable bit. */
2018 return mode & S_IXUSR;
2021 const struct got_error *
2022 got_object_tree_path_changed(int *changed,
2023 struct got_tree_object *tree01, struct got_tree_object *tree02,
2024 const char *path, struct got_repository *repo)
2026 const struct got_error *err = NULL;
2027 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2028 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2029 const char *seg, *s;
2034 /* We not do support comparing the root path. */
2035 if (got_path_is_root_dir(path))
2036 return got_error_path(path, GOT_ERR_BAD_PATH);
2046 struct got_tree_object *next_tree1, *next_tree2;
2047 mode_t mode1, mode2;
2056 te1 = find_entry_by_name(tree1, seg, seglen);
2058 err = got_error(GOT_ERR_NO_OBJ);
2063 te2 = find_entry_by_name(tree2, seg, seglen);
2066 mode1 = normalize_mode_for_comparison(te1->mode);
2067 mode2 = normalize_mode_for_comparison(te2->mode);
2068 if (mode1 != mode2) {
2073 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2079 if (*s == '\0') { /* final path element */
2088 err = got_object_open_as_tree(&next_tree1, repo,
2093 if (tree1 != tree01)
2094 got_object_tree_close(tree1);
2098 err = got_object_open_as_tree(&next_tree2, repo,
2103 if (tree2 != tree02)
2104 got_object_tree_close(tree2);
2107 if (tree2 != tree02)
2108 got_object_tree_close(tree2);
2114 if (tree1 && tree1 != tree01)
2115 got_object_tree_close(tree1);
2116 if (tree2 && tree2 != tree02)
2117 got_object_tree_close(tree2);
2121 const struct got_error *
2122 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2123 struct got_tree_entry *te)
2125 const struct got_error *err = NULL;
2127 *new_te = calloc(1, sizeof(**new_te));
2128 if (*new_te == NULL)
2129 return got_error_from_errno("calloc");
2131 (*new_te)->mode = te->mode;
2132 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2133 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2138 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2140 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2144 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2146 /* S_IFDIR check avoids confusing symlinks with submodules. */
2147 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2150 static const struct got_error *
2151 resolve_symlink(char **link_target, const char *path,
2152 struct got_object_id *commit_id, struct got_repository *repo)
2154 const struct got_error *err = NULL;
2156 char *name, *parent_path = NULL;
2157 struct got_object_id *tree_obj_id = NULL;
2158 struct got_tree_object *tree = NULL;
2159 struct got_tree_entry *te = NULL;
2161 *link_target = NULL;
2163 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2164 return got_error(GOT_ERR_NO_SPACE);
2166 name = basename(buf);
2168 return got_error_from_errno2("basename", path);
2170 err = got_path_dirname(&parent_path, path);
2174 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2177 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2178 /* Display the complete path in error message. */
2179 err = got_error_path(path, err->code);
2184 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2188 te = got_object_tree_find_entry(tree, name);
2190 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2194 if (got_object_tree_entry_is_symlink(te)) {
2195 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2198 if (!got_path_is_absolute(*link_target)) {
2200 if (asprintf(&abspath, "%s/%s", parent_path,
2201 *link_target) == -1) {
2202 err = got_error_from_errno("asprintf");
2206 *link_target = malloc(PATH_MAX);
2207 if (*link_target == NULL) {
2208 err = got_error_from_errno("malloc");
2211 err = got_canonpath(abspath, *link_target, PATH_MAX);
2220 got_object_tree_close(tree);
2223 *link_target = NULL;
2228 const struct got_error *
2229 got_object_resolve_symlinks(char **link_target, const char *path,
2230 struct got_object_id *commit_id, struct got_repository *repo)
2232 const struct got_error *err = NULL;
2233 char *next_target = NULL;
2234 int max_recursion = 40; /* matches Git */
2236 *link_target = NULL;
2239 err = resolve_symlink(&next_target,
2240 *link_target ? *link_target : path, commit_id, repo);
2245 if (--max_recursion == 0) {
2246 err = got_error_path(path, GOT_ERR_RECURSION);
2247 *link_target = NULL;
2250 *link_target = next_target;
2252 } while (next_target);
2257 const struct got_error *
2258 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2259 struct got_object_id *commit_id, const char *path,
2260 struct got_repository *repo)
2262 const struct got_error *err = NULL;
2263 struct got_pack *pack = NULL;
2264 struct got_packidx *packidx = NULL;
2265 char *path_packfile = NULL;
2266 struct got_commit_object *changed_commit = NULL;
2267 struct got_object_id *changed_commit_id = NULL;
2270 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2272 if (err->code != GOT_ERR_NO_OBJ)
2277 err = get_packfile_path(&path_packfile, packidx);
2281 pack = got_repo_get_cached_pack(repo, path_packfile);
2283 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2288 if (pack->privsep_child == NULL) {
2289 err = start_pack_privsep_child(pack, packidx);
2294 err = got_privsep_send_commit_traversal_request(
2295 pack->privsep_child->ibuf, commit_id, idx, path);
2299 err = got_privsep_recv_traversed_commits(&changed_commit,
2300 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2304 if (changed_commit) {
2306 * Cache the commit in which the path was changed.
2307 * This commit might be opened again soon.
2309 changed_commit->refcnt++;
2310 err = got_repo_cache_commit(repo, changed_commit_id,
2312 got_object_commit_close(changed_commit);
2315 free(path_packfile);
2316 free(changed_commit_id);