2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
20 #include <sys/socket.h>
22 #include <sys/resource.h>
37 #include "got_compat.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_repository.h"
42 #include "got_opentemp.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_object.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_object_idcache.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_repository.h"
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 struct got_object_id *
61 got_object_get_id(struct got_object *obj)
66 const struct got_error *
67 got_object_get_id_str(char **outbuf, struct got_object *obj)
69 return got_object_id_str(outbuf, &obj->id);
72 const struct got_error *
73 got_object_get_type(int *type, struct got_repository *repo,
74 struct got_object_id *id)
76 const struct got_error *err = NULL;
77 struct got_object *obj;
79 err = got_object_open(&obj, repo, id);
84 case GOT_OBJ_TYPE_COMMIT:
85 case GOT_OBJ_TYPE_TREE:
86 case GOT_OBJ_TYPE_BLOB:
87 case GOT_OBJ_TYPE_TAG:
91 err = got_error(GOT_ERR_OBJ_TYPE);
95 got_object_close(obj);
99 const struct got_error *
100 got_object_get_path(char **path, struct got_object_id *id,
101 struct got_repository *repo)
103 const struct got_error *err = NULL;
109 path_objects = got_repo_get_path_objects(repo);
110 if (path_objects == NULL)
111 return got_error_from_errno("got_repo_get_path_objects");
113 err = got_object_id_str(&hex, id);
117 if (asprintf(path, "%s/%.2x/%s", path_objects,
118 id->sha1[0], hex + 2) == -1)
119 err = got_error_from_errno("asprintf");
127 const struct got_error *
128 got_object_open_loose_fd(int *fd, struct got_object_id *id,
129 struct got_repository *repo)
131 const struct got_error *err = NULL;
134 err = got_object_get_path(&path, id, repo);
137 *fd = open(path, O_RDONLY | O_NOFOLLOW);
139 err = got_error_from_errno2("open", path);
147 static const struct got_error *
148 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
149 struct got_object_id *id)
151 const struct got_error *err = NULL;
152 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
154 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
158 err = got_privsep_recv_obj(obj, ibuf);
162 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
167 static const struct got_error *
168 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
169 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
171 const struct got_error *err = NULL;
172 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
174 int basefd, accumfd; /* temporary files for delta application */
176 basefd = got_opentempfd();
178 return got_error_from_errno("got_opentempfd");
180 accumfd = got_opentempfd();
183 return got_error_from_errno("got_opentempfd");
186 outfd_child = dup(outfd);
187 if (outfd_child == -1) {
188 err = got_error_from_errno("dup");
194 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
202 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
210 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
217 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
222 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
230 set_max_datasize(void)
234 if (getrlimit(RLIMIT_DATA, &rl) != 0)
237 rl.rlim_cur = rl.rlim_max;
238 setrlimit(RLIMIT_DATA, &rl);
241 static const struct got_error *
242 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
244 const struct got_error *err = NULL;
247 struct imsgbuf *ibuf;
249 ibuf = calloc(1, sizeof(*ibuf));
251 return got_error_from_errno("calloc");
253 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
254 if (pack->privsep_child == NULL) {
255 err = got_error_from_errno("calloc");
260 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
261 err = got_error_from_errno("socketpair");
267 err = got_error_from_errno("fork");
269 } else if (pid == 0) {
271 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
272 pack->path_packfile);
276 if (close(imsg_fds[1]) == -1)
277 return got_error_from_errno("close");
278 pack->privsep_child->imsg_fd = imsg_fds[0];
279 pack->privsep_child->pid = pid;
280 imsg_init(ibuf, imsg_fds[0]);
281 pack->privsep_child->ibuf = ibuf;
283 err = got_privsep_init_pack_child(ibuf, pack, packidx);
285 const struct got_error *child_err;
286 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
287 child_err = got_privsep_wait_for_child(
288 pack->privsep_child->pid);
289 if (child_err && err == NULL)
295 free(pack->privsep_child);
296 pack->privsep_child = NULL;
301 static const struct got_error *
302 read_packed_object_privsep(struct got_object **obj,
303 struct got_repository *repo, struct got_pack *pack,
304 struct got_packidx *packidx, int idx, struct got_object_id *id)
306 const struct got_error *err = NULL;
308 if (pack->privsep_child == NULL) {
309 err = start_pack_privsep_child(pack, packidx);
314 return request_packed_object(obj, pack, idx, id);
317 static const struct got_error *
318 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
319 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
320 struct got_object_id *id)
322 const struct got_error *err = NULL;
324 if (pack->privsep_child == NULL) {
325 err = start_pack_privsep_child(pack, packidx);
330 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
334 const struct got_error *
335 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
336 struct got_repository *repo)
338 const struct got_error *err = NULL;
339 struct got_pack *pack = NULL;
340 struct got_packidx *packidx = NULL;
344 err = got_repo_search_packidx(&packidx, &idx, repo, id);
348 err = got_packidx_get_packfile_path(&path_packfile,
349 packidx->path_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_object_id *id,
370 struct got_repository *repo, int fd)
372 const struct got_error *err = NULL;
373 struct imsgbuf *ibuf;
375 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
377 err = got_privsep_send_obj_req(ibuf, fd, id);
381 return got_privsep_recv_obj(obj, ibuf);
384 static const struct got_error *
385 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
386 struct got_object_id *id, struct got_repository *repo, int infd)
388 const struct got_error *err = NULL;
389 struct imsgbuf *ibuf;
392 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
394 outfd_child = dup(outfd);
395 if (outfd_child == -1)
396 return got_error_from_errno("dup");
398 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
402 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
406 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
409 static const struct got_error *
410 start_read_object_child(struct got_repository *repo)
412 const struct got_error *err = NULL;
415 struct imsgbuf *ibuf;
417 ibuf = calloc(1, sizeof(*ibuf));
419 return got_error_from_errno("calloc");
421 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
422 err = got_error_from_errno("socketpair");
429 err = got_error_from_errno("fork");
434 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
439 if (close(imsg_fds[1]) == -1) {
440 err = got_error_from_errno("close");
445 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
447 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
448 imsg_init(ibuf, imsg_fds[0]);
449 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
454 const struct got_error *
455 got_object_read_header_privsep(struct got_object **obj,
456 struct got_object_id *id, struct got_repository *repo, int obj_fd)
458 const struct got_error *err;
460 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
461 return request_object(obj, id, repo, obj_fd);
463 err = start_read_object_child(repo);
469 return request_object(obj, id, repo, obj_fd);
472 static const struct got_error *
473 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
474 int outfd, struct got_object_id *id, struct got_repository *repo,
477 const struct got_error *err;
479 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
480 return request_raw_object(outbuf, size, hdrlen, outfd, id,
483 err = start_read_object_child(repo);
487 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
491 const struct got_error *
492 got_object_open(struct got_object **obj, struct got_repository *repo,
493 struct got_object_id *id)
495 const struct got_error *err = NULL;
498 *obj = got_repo_get_cached_object(repo, id);
504 err = got_object_open_packed(obj, id, repo);
505 if (err && err->code != GOT_ERR_NO_OBJ)
509 return got_repo_cache_object(repo, id, *obj);
512 err = got_object_open_loose_fd(&fd, id, repo);
514 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
515 err = got_error_no_obj(id);
519 err = got_object_read_header_privsep(obj, id, repo, fd);
523 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
526 return got_repo_cache_object(repo, id, *obj);
529 /* *outfd must be initialized to -1 by caller */
530 const struct got_error *
531 got_object_raw_open(struct got_raw_object **obj, int *outfd,
532 struct got_repository *repo, struct got_object_id *id)
534 const struct got_error *err = NULL;
535 struct got_packidx *packidx = NULL;
537 uint8_t *outbuf = NULL;
540 char *path_packfile = NULL;
542 *obj = got_repo_get_cached_raw_object(repo, id);
549 *outfd = got_opentempfd();
551 return got_error_from_errno("got_opentempfd");
554 err = got_repo_search_packidx(&packidx, &idx, repo, id);
556 struct got_pack *pack = NULL;
558 err = got_packidx_get_packfile_path(&path_packfile,
559 packidx->path_packidx);
563 pack = got_repo_get_cached_pack(repo, path_packfile);
565 err = got_repo_cache_pack(&pack, repo, path_packfile,
570 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
571 *outfd, pack, packidx, idx, id);
574 } else if (err->code == GOT_ERR_NO_OBJ) {
577 err = got_object_open_loose_fd(&fd, id, repo);
580 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
586 *obj = calloc(1, sizeof(**obj));
588 err = got_error_from_errno("calloc");
593 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
594 if ((*obj)->f == NULL) {
595 err = got_error_from_errno("fdopen");
598 (*obj)->data = outbuf;
601 if (fstat(*outfd, &sb) == -1) {
602 err = got_error_from_errno("fstat");
606 if (sb.st_size != hdrlen + size) {
607 err = got_error(GOT_ERR_PRIVSEP_LEN);
611 (*obj)->f = fdopen(*outfd, "r");
612 if ((*obj)->f == NULL) {
613 err = got_error_from_errno("fdopen");
619 (*obj)->hdrlen = hdrlen;
621 err = got_repo_cache_raw_object(repo, id, *obj);
626 got_object_raw_close(*obj);
635 const struct got_error *
636 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
639 struct got_object_id id;
641 if (!got_parse_sha1_digest(id.sha1, id_str))
642 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
644 return got_object_open(obj, repo, &id);
647 const struct got_error *
648 got_object_resolve_id_str(struct got_object_id **id,
649 struct got_repository *repo, const char *id_str)
651 const struct got_error *err = NULL;
652 struct got_object *obj;
654 err = got_object_open_by_id_str(&obj, repo, id_str);
658 *id = got_object_id_dup(got_object_get_id(obj));
659 got_object_close(obj);
661 return got_error_from_errno("got_object_id_dup");
666 static const struct got_error *
667 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
668 int pack_idx, struct got_object_id *id)
670 const struct got_error *err = NULL;
672 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
677 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
681 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
685 static const struct got_error *
686 read_packed_commit_privsep(struct got_commit_object **commit,
687 struct got_pack *pack, struct got_packidx *packidx, int idx,
688 struct got_object_id *id)
690 const struct got_error *err = NULL;
692 if (pack->privsep_child)
693 return request_packed_commit(commit, pack, idx, id);
695 err = start_pack_privsep_child(pack, packidx);
699 return request_packed_commit(commit, pack, idx, id);
702 static const struct got_error *
703 request_commit(struct got_commit_object **commit, struct got_repository *repo,
704 int fd, struct got_object_id *id)
706 const struct got_error *err = NULL;
707 struct imsgbuf *ibuf;
709 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
711 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
715 return got_privsep_recv_commit(commit, ibuf);
718 static const struct got_error *
719 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
720 struct got_object_id *id, struct got_repository *repo)
722 const struct got_error *err;
725 struct imsgbuf *ibuf;
727 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
728 return request_commit(commit, repo, obj_fd, id);
730 ibuf = calloc(1, sizeof(*ibuf));
732 return got_error_from_errno("calloc");
734 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
735 err = got_error_from_errno("socketpair");
742 err = got_error_from_errno("fork");
747 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
752 if (close(imsg_fds[1]) == -1) {
753 err = got_error_from_errno("close");
757 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
759 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
760 imsg_init(ibuf, imsg_fds[0]);
761 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
763 return request_commit(commit, repo, obj_fd, id);
767 static const struct got_error *
768 open_commit(struct got_commit_object **commit,
769 struct got_repository *repo, struct got_object_id *id, int check_cache)
771 const struct got_error *err = NULL;
772 struct got_packidx *packidx = NULL;
774 char *path_packfile = NULL;
777 *commit = got_repo_get_cached_commit(repo, id);
778 if (*commit != NULL) {
785 err = got_repo_search_packidx(&packidx, &idx, repo, id);
787 struct got_pack *pack = NULL;
789 err = got_packidx_get_packfile_path(&path_packfile,
790 packidx->path_packidx);
794 pack = got_repo_get_cached_pack(repo, path_packfile);
796 err = got_repo_cache_pack(&pack, repo, path_packfile,
801 err = read_packed_commit_privsep(commit, pack,
803 } else if (err->code == GOT_ERR_NO_OBJ) {
806 err = got_object_open_loose_fd(&fd, id, repo);
809 err = read_commit_privsep(commit, fd, id, repo);
814 err = got_repo_cache_commit(repo, id, *commit);
821 const struct got_error *
822 got_object_open_as_commit(struct got_commit_object **commit,
823 struct got_repository *repo, struct got_object_id *id)
825 *commit = got_repo_get_cached_commit(repo, id);
826 if (*commit != NULL) {
831 return open_commit(commit, repo, id, 0);
834 const struct got_error *
835 got_object_commit_open(struct got_commit_object **commit,
836 struct got_repository *repo, struct got_object *obj)
838 return open_commit(commit, repo, got_object_get_id(obj), 1);
841 const struct got_error *
842 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
844 const struct got_error *err = NULL;
846 *qid = calloc(1, sizeof(**qid));
848 return got_error_from_errno("calloc");
850 (*qid)->id = got_object_id_dup(id);
851 if ((*qid)->id == NULL) {
852 err = got_error_from_errno("got_object_id_dup");
853 got_object_qid_free(*qid);
861 const struct got_error *
862 got_object_id_queue_copy(const struct got_object_id_queue *src,
863 struct got_object_id_queue *dest)
865 const struct got_error *err;
866 struct got_object_qid *qid;
868 STAILQ_FOREACH(qid, src, entry) {
869 struct got_object_qid *new;
871 * Deep-copy the object ID only. Let the caller deal
872 * with setting up the new->data pointer if needed.
874 err = got_object_qid_alloc(&new, qid->id);
876 got_object_id_queue_free(dest);
879 STAILQ_INSERT_TAIL(dest, new, entry);
885 static const struct got_error *
886 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
887 int pack_idx, struct got_object_id *id)
889 const struct got_error *err = NULL;
891 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
896 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
899 static const struct got_error *
900 read_packed_tree_privsep(struct got_tree_object **tree,
901 struct got_pack *pack, struct got_packidx *packidx, int idx,
902 struct got_object_id *id)
904 const struct got_error *err = NULL;
906 if (pack->privsep_child)
907 return request_packed_tree(tree, pack, idx, id);
909 err = start_pack_privsep_child(pack, packidx);
913 return request_packed_tree(tree, pack, idx, id);
916 static const struct got_error *
917 request_tree(struct got_tree_object **tree, struct got_repository *repo,
918 int fd, struct got_object_id *id)
920 const struct got_error *err = NULL;
921 struct imsgbuf *ibuf;
923 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
925 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
929 return got_privsep_recv_tree(tree, ibuf);
932 const struct got_error *
933 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
934 struct got_object_id *id, struct got_repository *repo)
936 const struct got_error *err;
939 struct imsgbuf *ibuf;
941 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
942 return request_tree(tree, repo, obj_fd, id);
944 ibuf = calloc(1, sizeof(*ibuf));
946 return got_error_from_errno("calloc");
948 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
949 err = got_error_from_errno("socketpair");
956 err = got_error_from_errno("fork");
961 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
966 if (close(imsg_fds[1]) == -1) {
967 err = got_error_from_errno("close");
971 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
973 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
974 imsg_init(ibuf, imsg_fds[0]);
975 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
978 return request_tree(tree, repo, obj_fd, id);
981 static const struct got_error *
982 open_tree(struct got_tree_object **tree, struct got_repository *repo,
983 struct got_object_id *id, int check_cache)
985 const struct got_error *err = NULL;
986 struct got_packidx *packidx = NULL;
988 char *path_packfile = NULL;
991 *tree = got_repo_get_cached_tree(repo, id);
999 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1001 struct got_pack *pack = NULL;
1003 err = got_packidx_get_packfile_path(&path_packfile,
1004 packidx->path_packidx);
1008 pack = got_repo_get_cached_pack(repo, path_packfile);
1010 err = got_repo_cache_pack(&pack, repo, path_packfile,
1015 err = read_packed_tree_privsep(tree, pack,
1017 } else if (err->code == GOT_ERR_NO_OBJ) {
1020 err = got_object_open_loose_fd(&fd, id, repo);
1023 err = read_tree_privsep(tree, fd, id, repo);
1028 err = got_repo_cache_tree(repo, id, *tree);
1031 free(path_packfile);
1035 const struct got_error *
1036 got_object_open_as_tree(struct got_tree_object **tree,
1037 struct got_repository *repo, struct got_object_id *id)
1039 *tree = got_repo_get_cached_tree(repo, id);
1040 if (*tree != NULL) {
1045 return open_tree(tree, repo, id, 0);
1048 const struct got_error *
1049 got_object_tree_open(struct got_tree_object **tree,
1050 struct got_repository *repo, struct got_object *obj)
1052 return open_tree(tree, repo, got_object_get_id(obj), 1);
1056 got_object_tree_get_nentries(struct got_tree_object *tree)
1058 return tree->nentries;
1061 struct got_tree_entry *
1062 got_object_tree_get_first_entry(struct got_tree_object *tree)
1064 return got_object_tree_get_entry(tree, 0);
1067 struct got_tree_entry *
1068 got_object_tree_get_last_entry(struct got_tree_object *tree)
1070 return got_object_tree_get_entry(tree, tree->nentries - 1);
1073 struct got_tree_entry *
1074 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1076 if (i < 0 || i >= tree->nentries)
1078 return &tree->entries[i];
1082 got_tree_entry_get_mode(struct got_tree_entry *te)
1088 got_tree_entry_get_name(struct got_tree_entry *te)
1090 return &te->name[0];
1093 struct got_object_id *
1094 got_tree_entry_get_id(struct got_tree_entry *te)
1099 const struct got_error *
1100 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1102 const struct got_error *err = NULL;
1103 size_t len, totlen, hdrlen, offset;
1107 hdrlen = got_object_blob_get_hdrlen(blob);
1113 err = got_object_blob_read_block(&len, blob);
1120 totlen += len - hdrlen;
1121 p = realloc(*s, totlen + 1);
1123 err = got_error_from_errno("realloc");
1129 /* Skip blob object header first time around. */
1131 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1136 (*s)[totlen] = '\0';
1140 const struct got_error *
1141 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1142 struct got_repository *repo)
1144 const struct got_error *err = NULL;
1145 struct got_blob_object *blob = NULL;
1147 *link_target = NULL;
1149 if (!got_object_tree_entry_is_symlink(te))
1150 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1152 err = got_object_open_as_blob(&blob, repo,
1153 got_tree_entry_get_id(te), PATH_MAX);
1157 err = got_object_blob_read_to_str(link_target, blob);
1158 got_object_blob_close(blob);
1161 *link_target = NULL;
1167 got_tree_entry_get_index(struct got_tree_entry *te)
1172 struct got_tree_entry *
1173 got_tree_entry_get_next(struct got_tree_object *tree,
1174 struct got_tree_entry *te)
1176 return got_object_tree_get_entry(tree, te->idx + 1);
1179 struct got_tree_entry *
1180 got_tree_entry_get_prev(struct got_tree_object *tree,
1181 struct got_tree_entry *te)
1183 return got_object_tree_get_entry(tree, te->idx - 1);
1186 static const struct got_error *
1187 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1188 struct got_pack *pack, struct got_packidx *packidx, int idx,
1189 struct got_object_id *id)
1191 const struct got_error *err = NULL;
1193 int basefd, accumfd; /* temporary files for delta application */
1195 basefd = got_opentempfd();
1197 return got_error_from_errno("got_opentempfd");
1198 accumfd = got_opentempfd();
1200 return got_error_from_errno("got_opentempfd");
1202 outfd_child = dup(outfd);
1203 if (outfd_child == -1)
1204 return got_error_from_errno("dup");
1206 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1210 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1218 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1225 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1230 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1231 pack->privsep_child->ibuf);
1235 if (lseek(outfd, SEEK_SET, 0) == -1)
1236 err = got_error_from_errno("lseek");
1241 static const struct got_error *
1242 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1243 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1244 struct got_object_id *id)
1246 const struct got_error *err = NULL;
1248 if (pack->privsep_child == NULL) {
1249 err = start_pack_privsep_child(pack, packidx);
1254 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1258 static const struct got_error *
1259 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1260 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1262 const struct got_error *err = NULL;
1265 outfd_child = dup(outfd);
1266 if (outfd_child == -1)
1267 return got_error_from_errno("dup");
1269 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1273 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1277 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1281 if (lseek(outfd, SEEK_SET, 0) == -1)
1282 return got_error_from_errno("lseek");
1287 static const struct got_error *
1288 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1289 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1291 const struct got_error *err;
1294 struct imsgbuf *ibuf;
1296 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1297 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1298 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1302 ibuf = calloc(1, sizeof(*ibuf));
1304 return got_error_from_errno("calloc");
1306 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1307 err = got_error_from_errno("socketpair");
1314 err = got_error_from_errno("fork");
1318 else if (pid == 0) {
1319 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1324 if (close(imsg_fds[1]) == -1) {
1325 err = got_error_from_errno("close");
1329 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1331 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1332 imsg_init(ibuf, imsg_fds[0]);
1333 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1335 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1338 static const struct got_error *
1339 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1340 struct got_object_id *id, size_t blocksize)
1342 const struct got_error *err = NULL;
1343 struct got_packidx *packidx = NULL;
1345 char *path_packfile = NULL;
1348 size_t size, hdrlen;
1351 *blob = calloc(1, sizeof(**blob));
1353 return got_error_from_errno("calloc");
1355 outfd = got_opentempfd();
1357 return got_error_from_errno("got_opentempfd");
1359 (*blob)->read_buf = malloc(blocksize);
1360 if ((*blob)->read_buf == NULL) {
1361 err = got_error_from_errno("malloc");
1365 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1367 struct got_pack *pack = NULL;
1369 err = got_packidx_get_packfile_path(&path_packfile,
1370 packidx->path_packidx);
1374 pack = got_repo_get_cached_pack(repo, path_packfile);
1376 err = got_repo_cache_pack(&pack, repo, path_packfile,
1381 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1382 pack, packidx, idx, id);
1383 } else if (err->code == GOT_ERR_NO_OBJ) {
1386 err = got_object_open_loose_fd(&infd, id, repo);
1389 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1395 if (hdrlen > size) {
1396 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1401 if (close(outfd) == -1 && err == NULL)
1402 err = got_error_from_errno("close");
1404 (*blob)->f = fmemopen(outbuf, size, "rb");
1405 if ((*blob)->f == NULL) {
1406 err = got_error_from_errno("fmemopen");
1410 (*blob)->data = outbuf;
1412 if (fstat(outfd, &sb) == -1) {
1413 err = got_error_from_errno("fstat");
1417 if (sb.st_size != size) {
1418 err = got_error(GOT_ERR_PRIVSEP_LEN);
1422 (*blob)->f = fdopen(outfd, "rb");
1423 if ((*blob)->f == NULL) {
1424 err = got_error_from_errno("fdopen");
1431 (*blob)->hdrlen = hdrlen;
1432 (*blob)->blocksize = blocksize;
1433 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1436 free(path_packfile);
1439 got_object_blob_close(*blob);
1441 } else if (outfd != -1)
1447 const struct got_error *
1448 got_object_open_as_blob(struct got_blob_object **blob,
1449 struct got_repository *repo, struct got_object_id *id,
1452 return open_blob(blob, repo, id, blocksize);
1455 const struct got_error *
1456 got_object_blob_open(struct got_blob_object **blob,
1457 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1459 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1462 const struct got_error *
1463 got_object_blob_close(struct got_blob_object *blob)
1465 const struct got_error *err = NULL;
1466 free(blob->read_buf);
1467 if (blob->f && fclose(blob->f) == EOF)
1468 err = got_error_from_errno("fclose");
1475 got_object_blob_rewind(struct got_blob_object *blob)
1482 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1484 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1488 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1490 return blob->hdrlen;
1494 got_object_blob_get_read_buf(struct got_blob_object *blob)
1496 return blob->read_buf;
1499 const struct got_error *
1500 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1504 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1505 if (n == 0 && ferror(blob->f))
1506 return got_ferror(blob->f, GOT_ERR_IO);
1511 const struct got_error *
1512 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1513 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1515 const struct got_error *err = NULL;
1516 size_t n, len, hdrlen;
1519 const int alloc_chunksz = 512;
1521 off_t off = 0, total_len = 0;
1524 *line_offsets = NULL;
1530 hdrlen = got_object_blob_get_hdrlen(blob);
1532 err = got_object_blob_read_block(&len, blob);
1537 buf = got_object_blob_get_read_buf(blob);
1540 if (line_offsets && *line_offsets == NULL) {
1541 /* Have some data but perhaps no '\n'. */
1543 nalloc = alloc_chunksz;
1544 *line_offsets = calloc(nalloc,
1545 sizeof(**line_offsets));
1546 if (*line_offsets == NULL)
1547 return got_error_from_errno("calloc");
1549 /* Skip forward over end of first line. */
1556 /* Scan '\n' offsets in remaining chunk of data. */
1558 if (buf[i] != '\n') {
1563 if (line_offsets && nalloc < *nlines) {
1564 size_t n = *nlines + alloc_chunksz;
1565 off_t *o = recallocarray(*line_offsets,
1566 nalloc, n, sizeof(**line_offsets));
1568 free(*line_offsets);
1569 *line_offsets = NULL;
1570 return got_error_from_errno(
1577 off = total_len + i - hdrlen + 1;
1578 (*line_offsets)[*nlines - 1] = off;
1583 /* Skip blob object header first time around. */
1584 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1585 if (n != len - hdrlen)
1586 return got_ferror(outfile, GOT_ERR_IO);
1587 total_len += len - hdrlen;
1591 if (fflush(outfile) != 0)
1592 return got_error_from_errno("fflush");
1596 *filesize = total_len;
1601 static const struct got_error *
1602 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1603 int pack_idx, struct got_object_id *id)
1605 const struct got_error *err = NULL;
1607 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1612 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1615 static const struct got_error *
1616 read_packed_tag_privsep(struct got_tag_object **tag,
1617 struct got_pack *pack, struct got_packidx *packidx, int idx,
1618 struct got_object_id *id)
1620 const struct got_error *err = NULL;
1622 if (pack->privsep_child)
1623 return request_packed_tag(tag, pack, idx, id);
1625 err = start_pack_privsep_child(pack, packidx);
1629 return request_packed_tag(tag, pack, idx, id);
1632 static const struct got_error *
1633 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1634 int fd, struct got_object_id *id)
1636 const struct got_error *err = NULL;
1637 struct imsgbuf *ibuf;
1639 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1641 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1645 return got_privsep_recv_tag(tag, ibuf);
1648 static const struct got_error *
1649 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1650 struct got_object_id *id, struct got_repository *repo)
1652 const struct got_error *err;
1655 struct imsgbuf *ibuf;
1657 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1658 return request_tag(tag, repo, obj_fd, id);
1660 ibuf = calloc(1, sizeof(*ibuf));
1662 return got_error_from_errno("calloc");
1664 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1665 err = got_error_from_errno("socketpair");
1672 err = got_error_from_errno("fork");
1676 else if (pid == 0) {
1677 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1682 if (close(imsg_fds[1]) == -1) {
1683 err = got_error_from_errno("close");
1687 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1689 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1690 imsg_init(ibuf, imsg_fds[0]);
1691 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1693 return request_tag(tag, repo, obj_fd, id);
1696 static const struct got_error *
1697 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1698 struct got_object_id *id, int check_cache)
1700 const struct got_error *err = NULL;
1701 struct got_packidx *packidx = NULL;
1703 char *path_packfile = NULL;
1704 struct got_object *obj = NULL;
1705 int obj_type = GOT_OBJ_TYPE_ANY;
1708 *tag = got_repo_get_cached_tag(repo, id);
1716 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1718 struct got_pack *pack = NULL;
1720 err = got_packidx_get_packfile_path(&path_packfile,
1721 packidx->path_packidx);
1725 pack = got_repo_get_cached_pack(repo, path_packfile);
1727 err = got_repo_cache_pack(&pack, repo, path_packfile,
1733 /* Beware of "lightweight" tags: Check object type first. */
1734 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1738 obj_type = obj->type;
1739 got_object_close(obj);
1740 if (obj_type != GOT_OBJ_TYPE_TAG) {
1741 err = got_error(GOT_ERR_OBJ_TYPE);
1744 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1745 } else if (err->code == GOT_ERR_NO_OBJ) {
1748 err = got_object_open_loose_fd(&fd, id, repo);
1751 err = got_object_read_header_privsep(&obj, id, repo, fd);
1754 obj_type = obj->type;
1755 got_object_close(obj);
1756 if (obj_type != GOT_OBJ_TYPE_TAG)
1757 return got_error(GOT_ERR_OBJ_TYPE);
1759 err = got_object_open_loose_fd(&fd, id, repo);
1762 err = read_tag_privsep(tag, fd, id, repo);
1767 err = got_repo_cache_tag(repo, id, *tag);
1770 free(path_packfile);
1774 const struct got_error *
1775 got_object_open_as_tag(struct got_tag_object **tag,
1776 struct got_repository *repo, struct got_object_id *id)
1778 *tag = got_repo_get_cached_tag(repo, id);
1784 return open_tag(tag, repo, id, 0);
1787 const struct got_error *
1788 got_object_tag_open(struct got_tag_object **tag,
1789 struct got_repository *repo, struct got_object *obj)
1791 return open_tag(tag, repo, got_object_get_id(obj), 1);
1795 got_object_tag_get_name(struct got_tag_object *tag)
1801 got_object_tag_get_object_type(struct got_tag_object *tag)
1803 return tag->obj_type;
1806 struct got_object_id *
1807 got_object_tag_get_object_id(struct got_tag_object *tag)
1813 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1815 return tag->tagger_time;
1819 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1821 return tag->tagger_gmtoff;
1825 got_object_tag_get_tagger(struct got_tag_object *tag)
1831 got_object_tag_get_message(struct got_tag_object *tag)
1836 static struct got_tree_entry *
1837 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1841 /* Note that tree entries are sorted in strncmp() order. */
1842 for (i = 0; i < tree->nentries; i++) {
1843 struct got_tree_entry *te = &tree->entries[i];
1844 int cmp = strncmp(te->name, name, len);
1849 if (te->name[len] == '\0')
1855 struct got_tree_entry *
1856 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1858 return find_entry_by_name(tree, name, strlen(name));
1861 const struct got_error *
1862 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1863 struct got_repository *repo, struct got_tree_object *tree,
1866 const struct got_error *err = NULL;
1867 struct got_tree_object *subtree = NULL;
1868 struct got_tree_entry *te = NULL;
1869 const char *seg, *s;
1881 struct got_tree_object *next_tree;
1890 te = find_entry_by_name(subtree, seg, seglen);
1892 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1903 err = got_object_open_as_tree(&next_tree, repo,
1908 if (subtree != tree)
1909 got_object_tree_close(subtree);
1910 subtree = next_tree;
1915 *id = got_object_id_dup(&te->id);
1917 return got_error_from_errno("got_object_id_dup");
1921 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1923 if (subtree && subtree != tree)
1924 got_object_tree_close(subtree);
1927 const struct got_error *
1928 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1929 struct got_object_id *commit_id, const char *path)
1931 const struct got_error *err = NULL;
1932 struct got_commit_object *commit = NULL;
1933 struct got_tree_object *tree = NULL;
1937 err = got_object_open_as_commit(&commit, repo, commit_id);
1941 /* Handle opening of root of commit's tree. */
1942 if (got_path_is_root_dir(path)) {
1943 *id = got_object_id_dup(commit->tree_id);
1945 err = got_error_from_errno("got_object_id_dup");
1947 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1950 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1954 got_object_commit_close(commit);
1956 got_object_tree_close(tree);
1961 * Normalize file mode bits to avoid false positive tree entry differences
1962 * in case tree entries have unexpected mode bits set.
1965 normalize_mode_for_comparison(mode_t mode)
1968 * For directories, the only relevant bit is the IFDIR bit.
1969 * This allows us to detect paths changing from a directory
1970 * to a file and vice versa.
1973 return mode & S_IFDIR;
1976 * For symlinks, the only relevant bit is the IFLNK bit.
1977 * This allows us to detect paths changing from a symlinks
1978 * to a file or directory and vice versa.
1981 return mode & S_IFLNK;
1983 /* For files, the only change we care about is the executable bit. */
1984 return mode & S_IXUSR;
1987 const struct got_error *
1988 got_object_tree_path_changed(int *changed,
1989 struct got_tree_object *tree01, struct got_tree_object *tree02,
1990 const char *path, struct got_repository *repo)
1992 const struct got_error *err = NULL;
1993 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1994 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1995 const char *seg, *s;
2000 /* We not do support comparing the root path. */
2001 if (got_path_is_root_dir(path))
2002 return got_error_path(path, GOT_ERR_BAD_PATH);
2012 struct got_tree_object *next_tree1, *next_tree2;
2013 mode_t mode1, mode2;
2022 te1 = find_entry_by_name(tree1, seg, seglen);
2024 err = got_error(GOT_ERR_NO_OBJ);
2029 te2 = find_entry_by_name(tree2, seg, seglen);
2032 mode1 = normalize_mode_for_comparison(te1->mode);
2033 mode2 = normalize_mode_for_comparison(te2->mode);
2034 if (mode1 != mode2) {
2039 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2045 if (*s == '\0') { /* final path element */
2054 err = got_object_open_as_tree(&next_tree1, repo,
2059 if (tree1 != tree01)
2060 got_object_tree_close(tree1);
2064 err = got_object_open_as_tree(&next_tree2, repo,
2069 if (tree2 != tree02)
2070 got_object_tree_close(tree2);
2073 if (tree2 != tree02)
2074 got_object_tree_close(tree2);
2080 if (tree1 && tree1 != tree01)
2081 got_object_tree_close(tree1);
2082 if (tree2 && tree2 != tree02)
2083 got_object_tree_close(tree2);
2087 const struct got_error *
2088 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2089 struct got_tree_entry *te)
2091 const struct got_error *err = NULL;
2093 *new_te = calloc(1, sizeof(**new_te));
2094 if (*new_te == NULL)
2095 return got_error_from_errno("calloc");
2097 (*new_te)->mode = te->mode;
2098 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2099 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2104 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2106 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2110 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2112 /* S_IFDIR check avoids confusing symlinks with submodules. */
2113 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2116 static const struct got_error *
2117 resolve_symlink(char **link_target, const char *path,
2118 struct got_object_id *commit_id, struct got_repository *repo)
2120 const struct got_error *err = NULL;
2122 char *name, *parent_path = NULL;
2123 struct got_object_id *tree_obj_id = NULL;
2124 struct got_tree_object *tree = NULL;
2125 struct got_tree_entry *te = NULL;
2127 *link_target = NULL;
2129 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2130 return got_error(GOT_ERR_NO_SPACE);
2132 name = basename(buf);
2134 return got_error_from_errno2("basename", path);
2136 err = got_path_dirname(&parent_path, path);
2140 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2143 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2144 /* Display the complete path in error message. */
2145 err = got_error_path(path, err->code);
2150 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2154 te = got_object_tree_find_entry(tree, name);
2156 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2160 if (got_object_tree_entry_is_symlink(te)) {
2161 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2164 if (!got_path_is_absolute(*link_target)) {
2166 if (asprintf(&abspath, "%s/%s", parent_path,
2167 *link_target) == -1) {
2168 err = got_error_from_errno("asprintf");
2172 *link_target = malloc(PATH_MAX);
2173 if (*link_target == NULL) {
2174 err = got_error_from_errno("malloc");
2177 err = got_canonpath(abspath, *link_target, PATH_MAX);
2186 got_object_tree_close(tree);
2189 *link_target = NULL;
2194 const struct got_error *
2195 got_object_resolve_symlinks(char **link_target, const char *path,
2196 struct got_object_id *commit_id, struct got_repository *repo)
2198 const struct got_error *err = NULL;
2199 char *next_target = NULL;
2200 int max_recursion = 40; /* matches Git */
2202 *link_target = NULL;
2205 err = resolve_symlink(&next_target,
2206 *link_target ? *link_target : path, commit_id, repo);
2211 if (--max_recursion == 0) {
2212 err = got_error_path(path, GOT_ERR_RECURSION);
2213 *link_target = NULL;
2216 *link_target = next_target;
2218 } while (next_target);
2223 const struct got_error *
2224 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2225 struct got_object_id *commit_id, const char *path,
2226 struct got_repository *repo)
2228 const struct got_error *err = NULL;
2229 struct got_pack *pack = NULL;
2230 struct got_packidx *packidx = NULL;
2231 char *path_packfile = NULL;
2232 struct got_commit_object *changed_commit = NULL;
2233 struct got_object_id *changed_commit_id = NULL;
2236 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2238 if (err->code != GOT_ERR_NO_OBJ)
2243 err = got_packidx_get_packfile_path(&path_packfile,
2244 packidx->path_packidx);
2248 pack = got_repo_get_cached_pack(repo, path_packfile);
2250 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2255 if (pack->privsep_child == NULL) {
2256 err = start_pack_privsep_child(pack, packidx);
2261 err = got_privsep_send_commit_traversal_request(
2262 pack->privsep_child->ibuf, commit_id, idx, path);
2266 err = got_privsep_recv_traversed_commits(&changed_commit,
2267 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2271 if (changed_commit) {
2273 * Cache the commit in which the path was changed.
2274 * This commit might be opened again soon.
2276 changed_commit->refcnt++;
2277 err = got_repo_cache_commit(repo, changed_commit_id,
2279 got_object_commit_close(changed_commit);
2282 free(path_packfile);
2283 free(changed_commit_id);