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 | O_CLOEXEC);
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 /* Create temporary files used during delta application. */
168 static const struct got_error *
169 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
171 const struct got_error *err;
175 * For performance reasons, the child will keep reusing the
176 * same temporary files during every object request.
177 * Opening and closing new files for every object request is
178 * too expensive during operations such as 'gotadmin pack'.
180 if (pack->child_has_tempfiles)
183 basefd = got_opentempfd();
185 return got_error_from_errno("got_opentempfd");
187 err = got_privsep_send_tmpfd(ibuf, basefd);
191 accumfd = got_opentempfd();
193 return got_error_from_errno("got_opentempfd");
195 err = got_privsep_send_tmpfd(ibuf, accumfd);
199 pack->child_has_tempfiles = 1;
203 static const struct got_error *
204 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
205 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
207 const struct got_error *err = NULL;
208 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
211 err = pack_child_send_tempfiles(ibuf, pack);
215 outfd_child = dup(outfd);
216 if (outfd_child == -1)
217 return got_error_from_errno("dup");
219 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
225 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
229 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
237 set_max_datasize(void)
241 if (getrlimit(RLIMIT_DATA, &rl) != 0)
244 rl.rlim_cur = rl.rlim_max;
245 setrlimit(RLIMIT_DATA, &rl);
248 static const struct got_error *
249 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
251 const struct got_error *err = NULL;
254 struct imsgbuf *ibuf;
256 ibuf = calloc(1, sizeof(*ibuf));
258 return got_error_from_errno("calloc");
260 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
261 if (pack->privsep_child == NULL) {
262 err = got_error_from_errno("calloc");
266 pack->child_has_tempfiles = 0;
268 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
269 err = got_error_from_errno("socketpair");
275 err = got_error_from_errno("fork");
277 } else if (pid == 0) {
279 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
280 pack->path_packfile);
284 if (close(imsg_fds[1]) == -1)
285 return got_error_from_errno("close");
286 pack->privsep_child->imsg_fd = imsg_fds[0];
287 pack->privsep_child->pid = pid;
288 imsg_init(ibuf, imsg_fds[0]);
289 pack->privsep_child->ibuf = ibuf;
291 err = got_privsep_init_pack_child(ibuf, pack, packidx);
293 const struct got_error *child_err;
294 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
295 child_err = got_privsep_wait_for_child(
296 pack->privsep_child->pid);
297 if (child_err && err == NULL)
303 free(pack->privsep_child);
304 pack->privsep_child = NULL;
309 static const struct got_error *
310 read_packed_object_privsep(struct got_object **obj,
311 struct got_repository *repo, struct got_pack *pack,
312 struct got_packidx *packidx, int idx, struct got_object_id *id)
314 const struct got_error *err = NULL;
316 if (pack->privsep_child == NULL) {
317 err = start_pack_privsep_child(pack, packidx);
322 return request_packed_object(obj, pack, idx, id);
325 static const struct got_error *
326 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
327 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
328 struct got_object_id *id)
330 const struct got_error *err = NULL;
332 if (pack->privsep_child == NULL) {
333 err = start_pack_privsep_child(pack, packidx);
338 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
342 const struct got_error *
343 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
344 struct got_repository *repo)
346 const struct got_error *err = NULL;
347 struct got_pack *pack = NULL;
348 struct got_packidx *packidx = NULL;
352 err = got_repo_search_packidx(&packidx, &idx, repo, id);
356 err = got_packidx_get_packfile_path(&path_packfile,
357 packidx->path_packidx);
361 pack = got_repo_get_cached_pack(repo, path_packfile);
363 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
368 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
376 static const struct got_error *
377 request_object(struct got_object **obj, struct got_object_id *id,
378 struct got_repository *repo, int fd)
380 const struct got_error *err = NULL;
381 struct imsgbuf *ibuf;
383 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
385 err = got_privsep_send_obj_req(ibuf, fd, id);
389 return got_privsep_recv_obj(obj, ibuf);
392 static const struct got_error *
393 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
394 struct got_object_id *id, struct got_repository *repo, int infd)
396 const struct got_error *err = NULL;
397 struct imsgbuf *ibuf;
400 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
402 outfd_child = dup(outfd);
403 if (outfd_child == -1)
404 return got_error_from_errno("dup");
406 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
410 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
414 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
417 static const struct got_error *
418 start_read_object_child(struct got_repository *repo)
420 const struct got_error *err = NULL;
423 struct imsgbuf *ibuf;
425 ibuf = calloc(1, sizeof(*ibuf));
427 return got_error_from_errno("calloc");
429 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
430 err = got_error_from_errno("socketpair");
437 err = got_error_from_errno("fork");
442 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
447 if (close(imsg_fds[1]) == -1) {
448 err = got_error_from_errno("close");
453 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
455 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
456 imsg_init(ibuf, imsg_fds[0]);
457 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
462 const struct got_error *
463 got_object_read_header_privsep(struct got_object **obj,
464 struct got_object_id *id, struct got_repository *repo, int obj_fd)
466 const struct got_error *err;
468 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
469 return request_object(obj, id, repo, obj_fd);
471 err = start_read_object_child(repo);
477 return request_object(obj, id, repo, obj_fd);
480 static const struct got_error *
481 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
482 int outfd, struct got_object_id *id, struct got_repository *repo,
485 const struct got_error *err;
487 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
488 return request_raw_object(outbuf, size, hdrlen, outfd, id,
491 err = start_read_object_child(repo);
495 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
499 const struct got_error *
500 got_object_open(struct got_object **obj, struct got_repository *repo,
501 struct got_object_id *id)
503 const struct got_error *err = NULL;
506 *obj = got_repo_get_cached_object(repo, id);
512 err = got_object_open_packed(obj, id, repo);
513 if (err && err->code != GOT_ERR_NO_OBJ)
517 return got_repo_cache_object(repo, id, *obj);
520 err = got_object_open_loose_fd(&fd, id, repo);
522 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
523 err = got_error_no_obj(id);
527 err = got_object_read_header_privsep(obj, id, repo, fd);
531 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
534 return got_repo_cache_object(repo, id, *obj);
537 /* *outfd must be initialized to -1 by caller */
538 const struct got_error *
539 got_object_raw_open(struct got_raw_object **obj, int *outfd,
540 struct got_repository *repo, struct got_object_id *id)
542 const struct got_error *err = NULL;
543 struct got_packidx *packidx = NULL;
545 uint8_t *outbuf = NULL;
548 char *path_packfile = NULL;
550 *obj = got_repo_get_cached_raw_object(repo, id);
557 *outfd = got_opentempfd();
559 return got_error_from_errno("got_opentempfd");
562 err = got_repo_search_packidx(&packidx, &idx, repo, id);
564 struct got_pack *pack = NULL;
566 err = got_packidx_get_packfile_path(&path_packfile,
567 packidx->path_packidx);
571 pack = got_repo_get_cached_pack(repo, path_packfile);
573 err = got_repo_cache_pack(&pack, repo, path_packfile,
578 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
579 *outfd, pack, packidx, idx, id);
582 } else if (err->code == GOT_ERR_NO_OBJ) {
585 err = got_object_open_loose_fd(&fd, id, repo);
588 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
594 *obj = calloc(1, sizeof(**obj));
596 err = got_error_from_errno("calloc");
601 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
602 if ((*obj)->f == NULL) {
603 err = got_error_from_errno("fdopen");
606 (*obj)->data = outbuf;
609 if (fstat(*outfd, &sb) == -1) {
610 err = got_error_from_errno("fstat");
614 if (sb.st_size != hdrlen + size) {
615 err = got_error(GOT_ERR_PRIVSEP_LEN);
619 (*obj)->f = fdopen(*outfd, "r");
620 if ((*obj)->f == NULL) {
621 err = got_error_from_errno("fdopen");
627 (*obj)->hdrlen = hdrlen;
629 err = got_repo_cache_raw_object(repo, id, *obj);
634 got_object_raw_close(*obj);
643 const struct got_error *
644 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
647 struct got_object_id id;
649 if (!got_parse_sha1_digest(id.sha1, id_str))
650 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
652 return got_object_open(obj, repo, &id);
655 const struct got_error *
656 got_object_resolve_id_str(struct got_object_id **id,
657 struct got_repository *repo, const char *id_str)
659 const struct got_error *err = NULL;
660 struct got_object *obj;
662 err = got_object_open_by_id_str(&obj, repo, id_str);
666 *id = got_object_id_dup(got_object_get_id(obj));
667 got_object_close(obj);
669 return got_error_from_errno("got_object_id_dup");
674 static const struct got_error *
675 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
676 int pack_idx, struct got_object_id *id)
678 const struct got_error *err = NULL;
680 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
685 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
689 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
693 static const struct got_error *
694 read_packed_commit_privsep(struct got_commit_object **commit,
695 struct got_pack *pack, struct got_packidx *packidx, int idx,
696 struct got_object_id *id)
698 const struct got_error *err = NULL;
700 if (pack->privsep_child)
701 return request_packed_commit(commit, pack, idx, id);
703 err = start_pack_privsep_child(pack, packidx);
707 return request_packed_commit(commit, pack, idx, id);
710 static const struct got_error *
711 request_commit(struct got_commit_object **commit, struct got_repository *repo,
712 int fd, struct got_object_id *id)
714 const struct got_error *err = NULL;
715 struct imsgbuf *ibuf;
717 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
719 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
723 return got_privsep_recv_commit(commit, ibuf);
726 static const struct got_error *
727 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
728 struct got_object_id *id, struct got_repository *repo)
730 const struct got_error *err;
733 struct imsgbuf *ibuf;
735 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
736 return request_commit(commit, repo, obj_fd, id);
738 ibuf = calloc(1, sizeof(*ibuf));
740 return got_error_from_errno("calloc");
742 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
743 err = got_error_from_errno("socketpair");
750 err = got_error_from_errno("fork");
755 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
760 if (close(imsg_fds[1]) == -1) {
761 err = got_error_from_errno("close");
765 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
767 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
768 imsg_init(ibuf, imsg_fds[0]);
769 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
771 return request_commit(commit, repo, obj_fd, id);
775 static const struct got_error *
776 open_commit(struct got_commit_object **commit,
777 struct got_repository *repo, struct got_object_id *id, int check_cache)
779 const struct got_error *err = NULL;
780 struct got_packidx *packidx = NULL;
782 char *path_packfile = NULL;
785 *commit = got_repo_get_cached_commit(repo, id);
786 if (*commit != NULL) {
793 err = got_repo_search_packidx(&packidx, &idx, repo, id);
795 struct got_pack *pack = NULL;
797 err = got_packidx_get_packfile_path(&path_packfile,
798 packidx->path_packidx);
802 pack = got_repo_get_cached_pack(repo, path_packfile);
804 err = got_repo_cache_pack(&pack, repo, path_packfile,
809 err = read_packed_commit_privsep(commit, pack,
811 } else if (err->code == GOT_ERR_NO_OBJ) {
814 err = got_object_open_loose_fd(&fd, id, repo);
817 err = read_commit_privsep(commit, fd, id, repo);
822 err = got_repo_cache_commit(repo, id, *commit);
829 const struct got_error *
830 got_object_open_as_commit(struct got_commit_object **commit,
831 struct got_repository *repo, struct got_object_id *id)
833 *commit = got_repo_get_cached_commit(repo, id);
834 if (*commit != NULL) {
839 return open_commit(commit, repo, id, 0);
842 const struct got_error *
843 got_object_commit_open(struct got_commit_object **commit,
844 struct got_repository *repo, struct got_object *obj)
846 return open_commit(commit, repo, got_object_get_id(obj), 1);
849 const struct got_error *
850 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
852 const struct got_error *err = NULL;
854 *qid = calloc(1, sizeof(**qid));
856 return got_error_from_errno("calloc");
858 (*qid)->id = got_object_id_dup(id);
859 if ((*qid)->id == NULL) {
860 err = got_error_from_errno("got_object_id_dup");
861 got_object_qid_free(*qid);
869 const struct got_error *
870 got_object_id_queue_copy(const struct got_object_id_queue *src,
871 struct got_object_id_queue *dest)
873 const struct got_error *err;
874 struct got_object_qid *qid;
876 STAILQ_FOREACH(qid, src, entry) {
877 struct got_object_qid *new;
879 * Deep-copy the object ID only. Let the caller deal
880 * with setting up the new->data pointer if needed.
882 err = got_object_qid_alloc(&new, qid->id);
884 got_object_id_queue_free(dest);
887 STAILQ_INSERT_TAIL(dest, new, entry);
893 static const struct got_error *
894 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
895 int pack_idx, struct got_object_id *id)
897 const struct got_error *err = NULL;
899 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
904 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
907 static const struct got_error *
908 read_packed_tree_privsep(struct got_tree_object **tree,
909 struct got_pack *pack, struct got_packidx *packidx, int idx,
910 struct got_object_id *id)
912 const struct got_error *err = NULL;
914 if (pack->privsep_child)
915 return request_packed_tree(tree, pack, idx, id);
917 err = start_pack_privsep_child(pack, packidx);
921 return request_packed_tree(tree, pack, idx, id);
924 static const struct got_error *
925 request_tree(struct got_tree_object **tree, struct got_repository *repo,
926 int fd, struct got_object_id *id)
928 const struct got_error *err = NULL;
929 struct imsgbuf *ibuf;
931 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
933 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
937 return got_privsep_recv_tree(tree, ibuf);
940 const struct got_error *
941 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
942 struct got_object_id *id, struct got_repository *repo)
944 const struct got_error *err;
947 struct imsgbuf *ibuf;
949 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
950 return request_tree(tree, repo, obj_fd, id);
952 ibuf = calloc(1, sizeof(*ibuf));
954 return got_error_from_errno("calloc");
956 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
957 err = got_error_from_errno("socketpair");
964 err = got_error_from_errno("fork");
969 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
974 if (close(imsg_fds[1]) == -1) {
975 err = got_error_from_errno("close");
979 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
981 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
982 imsg_init(ibuf, imsg_fds[0]);
983 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
986 return request_tree(tree, repo, obj_fd, id);
989 static const struct got_error *
990 open_tree(struct got_tree_object **tree, struct got_repository *repo,
991 struct got_object_id *id, int check_cache)
993 const struct got_error *err = NULL;
994 struct got_packidx *packidx = NULL;
996 char *path_packfile = NULL;
999 *tree = got_repo_get_cached_tree(repo, id);
1000 if (*tree != NULL) {
1007 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1009 struct got_pack *pack = NULL;
1011 err = got_packidx_get_packfile_path(&path_packfile,
1012 packidx->path_packidx);
1016 pack = got_repo_get_cached_pack(repo, path_packfile);
1018 err = got_repo_cache_pack(&pack, repo, path_packfile,
1023 err = read_packed_tree_privsep(tree, pack,
1025 } else if (err->code == GOT_ERR_NO_OBJ) {
1028 err = got_object_open_loose_fd(&fd, id, repo);
1031 err = read_tree_privsep(tree, fd, id, repo);
1036 err = got_repo_cache_tree(repo, id, *tree);
1039 free(path_packfile);
1043 const struct got_error *
1044 got_object_open_as_tree(struct got_tree_object **tree,
1045 struct got_repository *repo, struct got_object_id *id)
1047 *tree = got_repo_get_cached_tree(repo, id);
1048 if (*tree != NULL) {
1053 return open_tree(tree, repo, id, 0);
1056 const struct got_error *
1057 got_object_tree_open(struct got_tree_object **tree,
1058 struct got_repository *repo, struct got_object *obj)
1060 return open_tree(tree, repo, got_object_get_id(obj), 1);
1064 got_object_tree_get_nentries(struct got_tree_object *tree)
1066 return tree->nentries;
1069 struct got_tree_entry *
1070 got_object_tree_get_first_entry(struct got_tree_object *tree)
1072 return got_object_tree_get_entry(tree, 0);
1075 struct got_tree_entry *
1076 got_object_tree_get_last_entry(struct got_tree_object *tree)
1078 return got_object_tree_get_entry(tree, tree->nentries - 1);
1081 struct got_tree_entry *
1082 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1084 if (i < 0 || i >= tree->nentries)
1086 return &tree->entries[i];
1090 got_tree_entry_get_mode(struct got_tree_entry *te)
1096 got_tree_entry_get_name(struct got_tree_entry *te)
1098 return &te->name[0];
1101 struct got_object_id *
1102 got_tree_entry_get_id(struct got_tree_entry *te)
1107 const struct got_error *
1108 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1110 const struct got_error *err = NULL;
1111 size_t len, totlen, hdrlen, offset;
1115 hdrlen = got_object_blob_get_hdrlen(blob);
1121 err = got_object_blob_read_block(&len, blob);
1128 totlen += len - hdrlen;
1129 p = realloc(*s, totlen + 1);
1131 err = got_error_from_errno("realloc");
1137 /* Skip blob object header first time around. */
1139 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1144 (*s)[totlen] = '\0';
1148 const struct got_error *
1149 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1150 struct got_repository *repo)
1152 const struct got_error *err = NULL;
1153 struct got_blob_object *blob = NULL;
1155 *link_target = NULL;
1157 if (!got_object_tree_entry_is_symlink(te))
1158 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1160 err = got_object_open_as_blob(&blob, repo,
1161 got_tree_entry_get_id(te), PATH_MAX);
1165 err = got_object_blob_read_to_str(link_target, blob);
1166 got_object_blob_close(blob);
1169 *link_target = NULL;
1175 got_tree_entry_get_index(struct got_tree_entry *te)
1180 struct got_tree_entry *
1181 got_tree_entry_get_next(struct got_tree_object *tree,
1182 struct got_tree_entry *te)
1184 return got_object_tree_get_entry(tree, te->idx + 1);
1187 struct got_tree_entry *
1188 got_tree_entry_get_prev(struct got_tree_object *tree,
1189 struct got_tree_entry *te)
1191 return got_object_tree_get_entry(tree, te->idx - 1);
1194 static const struct got_error *
1195 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1196 struct got_pack *pack, struct got_packidx *packidx, int idx,
1197 struct got_object_id *id)
1199 const struct got_error *err = NULL;
1200 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1203 err = pack_child_send_tempfiles(ibuf, pack);
1207 outfd_child = dup(outfd);
1208 if (outfd_child == -1)
1209 return got_error_from_errno("dup");
1211 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1215 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1221 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1222 pack->privsep_child->ibuf);
1226 if (lseek(outfd, SEEK_SET, 0) == -1)
1227 err = got_error_from_errno("lseek");
1232 static const struct got_error *
1233 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1234 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1235 struct got_object_id *id)
1237 const struct got_error *err = NULL;
1239 if (pack->privsep_child == NULL) {
1240 err = start_pack_privsep_child(pack, packidx);
1245 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1249 static const struct got_error *
1250 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1251 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1253 const struct got_error *err = NULL;
1256 outfd_child = dup(outfd);
1257 if (outfd_child == -1)
1258 return got_error_from_errno("dup");
1260 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1264 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1268 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1272 if (lseek(outfd, SEEK_SET, 0) == -1)
1273 return got_error_from_errno("lseek");
1278 static const struct got_error *
1279 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1280 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1282 const struct got_error *err;
1285 struct imsgbuf *ibuf;
1287 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1288 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1289 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1293 ibuf = calloc(1, sizeof(*ibuf));
1295 return got_error_from_errno("calloc");
1297 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1298 err = got_error_from_errno("socketpair");
1305 err = got_error_from_errno("fork");
1309 else if (pid == 0) {
1310 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1315 if (close(imsg_fds[1]) == -1) {
1316 err = got_error_from_errno("close");
1320 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1322 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1323 imsg_init(ibuf, imsg_fds[0]);
1324 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1326 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1329 static const struct got_error *
1330 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1331 struct got_object_id *id, size_t blocksize)
1333 const struct got_error *err = NULL;
1334 struct got_packidx *packidx = NULL;
1336 char *path_packfile = NULL;
1339 size_t size, hdrlen;
1342 *blob = calloc(1, sizeof(**blob));
1344 return got_error_from_errno("calloc");
1346 outfd = got_opentempfd();
1348 return got_error_from_errno("got_opentempfd");
1350 (*blob)->read_buf = malloc(blocksize);
1351 if ((*blob)->read_buf == NULL) {
1352 err = got_error_from_errno("malloc");
1356 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1358 struct got_pack *pack = NULL;
1360 err = got_packidx_get_packfile_path(&path_packfile,
1361 packidx->path_packidx);
1365 pack = got_repo_get_cached_pack(repo, path_packfile);
1367 err = got_repo_cache_pack(&pack, repo, path_packfile,
1372 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1373 pack, packidx, idx, id);
1374 } else if (err->code == GOT_ERR_NO_OBJ) {
1377 err = got_object_open_loose_fd(&infd, id, repo);
1380 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1386 if (hdrlen > size) {
1387 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1392 if (close(outfd) == -1 && err == NULL)
1393 err = got_error_from_errno("close");
1395 (*blob)->f = fmemopen(outbuf, size, "rb");
1396 if ((*blob)->f == NULL) {
1397 err = got_error_from_errno("fmemopen");
1401 (*blob)->data = outbuf;
1403 if (fstat(outfd, &sb) == -1) {
1404 err = got_error_from_errno("fstat");
1408 if (sb.st_size != size) {
1409 err = got_error(GOT_ERR_PRIVSEP_LEN);
1413 (*blob)->f = fdopen(outfd, "rb");
1414 if ((*blob)->f == NULL) {
1415 err = got_error_from_errno("fdopen");
1422 (*blob)->hdrlen = hdrlen;
1423 (*blob)->blocksize = blocksize;
1424 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1427 free(path_packfile);
1430 got_object_blob_close(*blob);
1432 } else if (outfd != -1)
1438 const struct got_error *
1439 got_object_open_as_blob(struct got_blob_object **blob,
1440 struct got_repository *repo, struct got_object_id *id,
1443 return open_blob(blob, repo, id, blocksize);
1446 const struct got_error *
1447 got_object_blob_open(struct got_blob_object **blob,
1448 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1450 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1453 const struct got_error *
1454 got_object_blob_close(struct got_blob_object *blob)
1456 const struct got_error *err = NULL;
1457 free(blob->read_buf);
1458 if (blob->f && fclose(blob->f) == EOF)
1459 err = got_error_from_errno("fclose");
1466 got_object_blob_rewind(struct got_blob_object *blob)
1473 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1475 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1479 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1481 return blob->hdrlen;
1485 got_object_blob_get_read_buf(struct got_blob_object *blob)
1487 return blob->read_buf;
1490 const struct got_error *
1491 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1495 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1496 if (n == 0 && ferror(blob->f))
1497 return got_ferror(blob->f, GOT_ERR_IO);
1502 const struct got_error *
1503 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1504 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1506 const struct got_error *err = NULL;
1507 size_t n, len, hdrlen;
1510 const int alloc_chunksz = 512;
1512 off_t off = 0, total_len = 0;
1515 *line_offsets = NULL;
1521 hdrlen = got_object_blob_get_hdrlen(blob);
1523 err = got_object_blob_read_block(&len, blob);
1528 buf = got_object_blob_get_read_buf(blob);
1531 if (line_offsets && *line_offsets == NULL) {
1532 /* Have some data but perhaps no '\n'. */
1534 nalloc = alloc_chunksz;
1535 *line_offsets = calloc(nalloc,
1536 sizeof(**line_offsets));
1537 if (*line_offsets == NULL)
1538 return got_error_from_errno("calloc");
1540 /* Skip forward over end of first line. */
1547 /* Scan '\n' offsets in remaining chunk of data. */
1549 if (buf[i] != '\n') {
1554 if (line_offsets && nalloc < *nlines) {
1555 size_t n = *nlines + alloc_chunksz;
1556 off_t *o = recallocarray(*line_offsets,
1557 nalloc, n, sizeof(**line_offsets));
1559 free(*line_offsets);
1560 *line_offsets = NULL;
1561 return got_error_from_errno(
1568 off = total_len + i - hdrlen + 1;
1569 (*line_offsets)[*nlines - 1] = off;
1574 /* Skip blob object header first time around. */
1575 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1576 if (n != len - hdrlen)
1577 return got_ferror(outfile, GOT_ERR_IO);
1578 total_len += len - hdrlen;
1582 if (fflush(outfile) != 0)
1583 return got_error_from_errno("fflush");
1587 *filesize = total_len;
1592 static const struct got_error *
1593 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1594 int pack_idx, struct got_object_id *id)
1596 const struct got_error *err = NULL;
1598 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1603 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1606 static const struct got_error *
1607 read_packed_tag_privsep(struct got_tag_object **tag,
1608 struct got_pack *pack, struct got_packidx *packidx, int idx,
1609 struct got_object_id *id)
1611 const struct got_error *err = NULL;
1613 if (pack->privsep_child)
1614 return request_packed_tag(tag, pack, idx, id);
1616 err = start_pack_privsep_child(pack, packidx);
1620 return request_packed_tag(tag, pack, idx, id);
1623 static const struct got_error *
1624 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1625 int fd, struct got_object_id *id)
1627 const struct got_error *err = NULL;
1628 struct imsgbuf *ibuf;
1630 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1632 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1636 return got_privsep_recv_tag(tag, ibuf);
1639 static const struct got_error *
1640 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1641 struct got_object_id *id, struct got_repository *repo)
1643 const struct got_error *err;
1646 struct imsgbuf *ibuf;
1648 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1649 return request_tag(tag, repo, obj_fd, id);
1651 ibuf = calloc(1, sizeof(*ibuf));
1653 return got_error_from_errno("calloc");
1655 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1656 err = got_error_from_errno("socketpair");
1663 err = got_error_from_errno("fork");
1667 else if (pid == 0) {
1668 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1673 if (close(imsg_fds[1]) == -1) {
1674 err = got_error_from_errno("close");
1678 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1680 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1681 imsg_init(ibuf, imsg_fds[0]);
1682 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1684 return request_tag(tag, repo, obj_fd, id);
1687 static const struct got_error *
1688 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1689 struct got_object_id *id, int check_cache)
1691 const struct got_error *err = NULL;
1692 struct got_packidx *packidx = NULL;
1694 char *path_packfile = NULL;
1695 struct got_object *obj = NULL;
1696 int obj_type = GOT_OBJ_TYPE_ANY;
1699 *tag = got_repo_get_cached_tag(repo, id);
1707 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1709 struct got_pack *pack = NULL;
1711 err = got_packidx_get_packfile_path(&path_packfile,
1712 packidx->path_packidx);
1716 pack = got_repo_get_cached_pack(repo, path_packfile);
1718 err = got_repo_cache_pack(&pack, repo, path_packfile,
1724 /* Beware of "lightweight" tags: Check object type first. */
1725 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1729 obj_type = obj->type;
1730 got_object_close(obj);
1731 if (obj_type != GOT_OBJ_TYPE_TAG) {
1732 err = got_error(GOT_ERR_OBJ_TYPE);
1735 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1736 } else if (err->code == GOT_ERR_NO_OBJ) {
1739 err = got_object_open_loose_fd(&fd, id, repo);
1742 err = got_object_read_header_privsep(&obj, id, repo, fd);
1745 obj_type = obj->type;
1746 got_object_close(obj);
1747 if (obj_type != GOT_OBJ_TYPE_TAG)
1748 return got_error(GOT_ERR_OBJ_TYPE);
1750 err = got_object_open_loose_fd(&fd, id, repo);
1753 err = read_tag_privsep(tag, fd, id, repo);
1758 err = got_repo_cache_tag(repo, id, *tag);
1761 free(path_packfile);
1765 const struct got_error *
1766 got_object_open_as_tag(struct got_tag_object **tag,
1767 struct got_repository *repo, struct got_object_id *id)
1769 *tag = got_repo_get_cached_tag(repo, id);
1775 return open_tag(tag, repo, id, 0);
1778 const struct got_error *
1779 got_object_tag_open(struct got_tag_object **tag,
1780 struct got_repository *repo, struct got_object *obj)
1782 return open_tag(tag, repo, got_object_get_id(obj), 1);
1786 got_object_tag_get_name(struct got_tag_object *tag)
1792 got_object_tag_get_object_type(struct got_tag_object *tag)
1794 return tag->obj_type;
1797 struct got_object_id *
1798 got_object_tag_get_object_id(struct got_tag_object *tag)
1804 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1806 return tag->tagger_time;
1810 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1812 return tag->tagger_gmtoff;
1816 got_object_tag_get_tagger(struct got_tag_object *tag)
1822 got_object_tag_get_message(struct got_tag_object *tag)
1827 static struct got_tree_entry *
1828 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1832 /* Note that tree entries are sorted in strncmp() order. */
1833 for (i = 0; i < tree->nentries; i++) {
1834 struct got_tree_entry *te = &tree->entries[i];
1835 int cmp = strncmp(te->name, name, len);
1840 if (te->name[len] == '\0')
1846 struct got_tree_entry *
1847 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1849 return find_entry_by_name(tree, name, strlen(name));
1852 const struct got_error *
1853 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1854 struct got_repository *repo, struct got_tree_object *tree,
1857 const struct got_error *err = NULL;
1858 struct got_tree_object *subtree = NULL;
1859 struct got_tree_entry *te = NULL;
1860 const char *seg, *s;
1872 struct got_tree_object *next_tree;
1881 te = find_entry_by_name(subtree, seg, seglen);
1883 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1894 err = got_object_open_as_tree(&next_tree, repo,
1899 if (subtree != tree)
1900 got_object_tree_close(subtree);
1901 subtree = next_tree;
1906 *id = got_object_id_dup(&te->id);
1908 return got_error_from_errno("got_object_id_dup");
1912 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1914 if (subtree && subtree != tree)
1915 got_object_tree_close(subtree);
1918 const struct got_error *
1919 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1920 struct got_object_id *commit_id, const char *path)
1922 const struct got_error *err = NULL;
1923 struct got_commit_object *commit = NULL;
1924 struct got_tree_object *tree = NULL;
1928 err = got_object_open_as_commit(&commit, repo, commit_id);
1932 /* Handle opening of root of commit's tree. */
1933 if (got_path_is_root_dir(path)) {
1934 *id = got_object_id_dup(commit->tree_id);
1936 err = got_error_from_errno("got_object_id_dup");
1938 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1941 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1945 got_object_commit_close(commit);
1947 got_object_tree_close(tree);
1952 * Normalize file mode bits to avoid false positive tree entry differences
1953 * in case tree entries have unexpected mode bits set.
1956 normalize_mode_for_comparison(mode_t mode)
1959 * For directories, the only relevant bit is the IFDIR bit.
1960 * This allows us to detect paths changing from a directory
1961 * to a file and vice versa.
1964 return mode & S_IFDIR;
1967 * For symlinks, the only relevant bit is the IFLNK bit.
1968 * This allows us to detect paths changing from a symlinks
1969 * to a file or directory and vice versa.
1972 return mode & S_IFLNK;
1974 /* For files, the only change we care about is the executable bit. */
1975 return mode & S_IXUSR;
1978 const struct got_error *
1979 got_object_tree_path_changed(int *changed,
1980 struct got_tree_object *tree01, struct got_tree_object *tree02,
1981 const char *path, struct got_repository *repo)
1983 const struct got_error *err = NULL;
1984 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1985 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1986 const char *seg, *s;
1991 /* We not do support comparing the root path. */
1992 if (got_path_is_root_dir(path))
1993 return got_error_path(path, GOT_ERR_BAD_PATH);
2003 struct got_tree_object *next_tree1, *next_tree2;
2004 mode_t mode1, mode2;
2013 te1 = find_entry_by_name(tree1, seg, seglen);
2015 err = got_error(GOT_ERR_NO_OBJ);
2020 te2 = find_entry_by_name(tree2, seg, seglen);
2023 mode1 = normalize_mode_for_comparison(te1->mode);
2024 mode2 = normalize_mode_for_comparison(te2->mode);
2025 if (mode1 != mode2) {
2030 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2036 if (*s == '\0') { /* final path element */
2045 err = got_object_open_as_tree(&next_tree1, repo,
2050 if (tree1 != tree01)
2051 got_object_tree_close(tree1);
2055 err = got_object_open_as_tree(&next_tree2, repo,
2060 if (tree2 != tree02)
2061 got_object_tree_close(tree2);
2064 if (tree2 != tree02)
2065 got_object_tree_close(tree2);
2071 if (tree1 && tree1 != tree01)
2072 got_object_tree_close(tree1);
2073 if (tree2 && tree2 != tree02)
2074 got_object_tree_close(tree2);
2078 const struct got_error *
2079 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2080 struct got_tree_entry *te)
2082 const struct got_error *err = NULL;
2084 *new_te = calloc(1, sizeof(**new_te));
2085 if (*new_te == NULL)
2086 return got_error_from_errno("calloc");
2088 (*new_te)->mode = te->mode;
2089 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2090 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2095 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2097 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2101 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2103 /* S_IFDIR check avoids confusing symlinks with submodules. */
2104 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2107 static const struct got_error *
2108 resolve_symlink(char **link_target, const char *path,
2109 struct got_object_id *commit_id, struct got_repository *repo)
2111 const struct got_error *err = NULL;
2113 char *name, *parent_path = NULL;
2114 struct got_object_id *tree_obj_id = NULL;
2115 struct got_tree_object *tree = NULL;
2116 struct got_tree_entry *te = NULL;
2118 *link_target = NULL;
2120 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2121 return got_error(GOT_ERR_NO_SPACE);
2123 name = basename(buf);
2125 return got_error_from_errno2("basename", path);
2127 err = got_path_dirname(&parent_path, path);
2131 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2134 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2135 /* Display the complete path in error message. */
2136 err = got_error_path(path, err->code);
2141 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2145 te = got_object_tree_find_entry(tree, name);
2147 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2151 if (got_object_tree_entry_is_symlink(te)) {
2152 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2155 if (!got_path_is_absolute(*link_target)) {
2157 if (asprintf(&abspath, "%s/%s", parent_path,
2158 *link_target) == -1) {
2159 err = got_error_from_errno("asprintf");
2163 *link_target = malloc(PATH_MAX);
2164 if (*link_target == NULL) {
2165 err = got_error_from_errno("malloc");
2168 err = got_canonpath(abspath, *link_target, PATH_MAX);
2177 got_object_tree_close(tree);
2180 *link_target = NULL;
2185 const struct got_error *
2186 got_object_resolve_symlinks(char **link_target, const char *path,
2187 struct got_object_id *commit_id, struct got_repository *repo)
2189 const struct got_error *err = NULL;
2190 char *next_target = NULL;
2191 int max_recursion = 40; /* matches Git */
2193 *link_target = NULL;
2196 err = resolve_symlink(&next_target,
2197 *link_target ? *link_target : path, commit_id, repo);
2202 if (--max_recursion == 0) {
2203 err = got_error_path(path, GOT_ERR_RECURSION);
2204 *link_target = NULL;
2207 *link_target = next_target;
2209 } while (next_target);
2214 const struct got_error *
2215 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2216 struct got_object_id *commit_id, const char *path,
2217 struct got_repository *repo)
2219 const struct got_error *err = NULL;
2220 struct got_pack *pack = NULL;
2221 struct got_packidx *packidx = NULL;
2222 char *path_packfile = NULL;
2223 struct got_commit_object *changed_commit = NULL;
2224 struct got_object_id *changed_commit_id = NULL;
2227 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2229 if (err->code != GOT_ERR_NO_OBJ)
2234 err = got_packidx_get_packfile_path(&path_packfile,
2235 packidx->path_packidx);
2239 pack = got_repo_get_cached_pack(repo, path_packfile);
2241 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2246 if (pack->privsep_child == NULL) {
2247 err = start_pack_privsep_child(pack, packidx);
2252 err = got_privsep_send_commit_traversal_request(
2253 pack->privsep_child->ibuf, commit_id, idx, path);
2257 err = got_privsep_recv_traversed_commits(&changed_commit,
2258 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2262 if (changed_commit) {
2264 * Cache the commit in which the path was changed.
2265 * This commit might be opened again soon.
2267 changed_commit->refcnt++;
2268 err = got_repo_cache_commit(repo, changed_commit_id,
2270 got_object_commit_close(changed_commit);
2273 free(path_packfile);
2274 free(changed_commit_id);