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>
22 #include <sys/socket.h>
24 #include <sys/resource.h>
39 #include "got_compat.h"
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_repository.h"
44 #include "got_opentemp.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_object.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_object_idcache.h"
53 #include "got_lib_object_cache.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_repository.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 struct got_object_id *
63 got_object_get_id(struct got_object *obj)
68 const struct got_error *
69 got_object_get_id_str(char **outbuf, struct got_object *obj)
71 return got_object_id_str(outbuf, &obj->id);
74 const struct got_error *
75 got_object_get_type(int *type, struct got_repository *repo,
76 struct got_object_id *id)
78 const struct got_error *err = NULL;
79 struct got_object *obj;
81 err = got_object_open(&obj, repo, id);
86 case GOT_OBJ_TYPE_COMMIT:
87 case GOT_OBJ_TYPE_TREE:
88 case GOT_OBJ_TYPE_BLOB:
89 case GOT_OBJ_TYPE_TAG:
93 err = got_error(GOT_ERR_OBJ_TYPE);
97 got_object_close(obj);
101 const struct got_error *
102 got_object_get_path(char **path, struct got_object_id *id,
103 struct got_repository *repo)
105 const struct got_error *err = NULL;
111 path_objects = got_repo_get_path_objects(repo);
112 if (path_objects == NULL)
113 return got_error_from_errno("got_repo_get_path_objects");
115 err = got_object_id_str(&hex, id);
119 if (asprintf(path, "%s/%.2x/%s", path_objects,
120 id->sha1[0], hex + 2) == -1)
121 err = got_error_from_errno("asprintf");
129 const struct got_error *
130 got_object_open_loose_fd(int *fd, struct got_object_id *id,
131 struct got_repository *repo)
133 const struct got_error *err = NULL;
136 err = got_object_get_path(&path, id, repo);
139 *fd = open(path, O_RDONLY | O_NOFOLLOW);
141 err = got_error_from_errno2("open", path);
149 static const struct got_error *
150 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
151 struct got_object_id *id)
153 const struct got_error *err = NULL;
154 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
156 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
160 err = got_privsep_recv_obj(obj, ibuf);
164 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
169 static const struct got_error *
170 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
171 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
173 const struct got_error *err = NULL;
174 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
176 int basefd, accumfd; /* temporary files for delta application */
178 basefd = got_opentempfd();
180 return got_error_from_errno("got_opentempfd");
182 accumfd = got_opentempfd();
185 return got_error_from_errno("got_opentempfd");
188 outfd_child = dup(outfd);
189 if (outfd_child == -1) {
190 err = got_error_from_errno("dup");
196 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
204 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
212 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
219 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
224 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
232 set_max_datasize(void)
236 if (getrlimit(RLIMIT_DATA, &rl) != 0)
239 rl.rlim_cur = rl.rlim_max;
240 setrlimit(RLIMIT_DATA, &rl);
243 static const struct got_error *
244 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
246 const struct got_error *err = NULL;
249 struct imsgbuf *ibuf;
251 ibuf = calloc(1, sizeof(*ibuf));
253 return got_error_from_errno("calloc");
255 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
256 if (pack->privsep_child == NULL) {
257 err = got_error_from_errno("calloc");
262 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
263 err = got_error_from_errno("socketpair");
269 err = got_error_from_errno("fork");
271 } else if (pid == 0) {
273 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
274 pack->path_packfile);
278 if (close(imsg_fds[1]) == -1)
279 return got_error_from_errno("close");
280 pack->privsep_child->imsg_fd = imsg_fds[0];
281 pack->privsep_child->pid = pid;
282 imsg_init(ibuf, imsg_fds[0]);
283 pack->privsep_child->ibuf = ibuf;
285 err = got_privsep_init_pack_child(ibuf, pack, packidx);
287 const struct got_error *child_err;
288 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
289 child_err = got_privsep_wait_for_child(
290 pack->privsep_child->pid);
291 if (child_err && err == NULL)
297 free(pack->privsep_child);
298 pack->privsep_child = NULL;
303 static const struct got_error *
304 read_packed_object_privsep(struct got_object **obj,
305 struct got_repository *repo, struct got_pack *pack,
306 struct got_packidx *packidx, int idx, struct got_object_id *id)
308 const struct got_error *err = NULL;
310 if (pack->privsep_child == NULL) {
311 err = start_pack_privsep_child(pack, packidx);
316 return request_packed_object(obj, pack, idx, id);
319 static const struct got_error *
320 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
321 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
322 struct got_object_id *id)
324 const struct got_error *err = NULL;
326 if (pack->privsep_child == NULL) {
327 err = start_pack_privsep_child(pack, packidx);
332 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
336 const struct got_error *
337 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
338 struct got_repository *repo)
340 const struct got_error *err = NULL;
341 struct got_pack *pack = NULL;
342 struct got_packidx *packidx = NULL;
346 err = got_repo_search_packidx(&packidx, &idx, repo, id);
350 err = got_packidx_get_packfile_path(&path_packfile,
351 packidx->path_packidx);
355 pack = got_repo_get_cached_pack(repo, path_packfile);
357 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
362 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
370 static const struct got_error *
371 request_object(struct got_object **obj, struct got_object_id *id,
372 struct got_repository *repo, int fd)
374 const struct got_error *err = NULL;
375 struct imsgbuf *ibuf;
377 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
379 err = got_privsep_send_obj_req(ibuf, fd, id);
383 return got_privsep_recv_obj(obj, ibuf);
386 static const struct got_error *
387 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
388 struct got_object_id *id, struct got_repository *repo, int infd)
390 const struct got_error *err = NULL;
391 struct imsgbuf *ibuf;
394 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
396 outfd_child = dup(outfd);
397 if (outfd_child == -1)
398 return got_error_from_errno("dup");
400 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
404 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
408 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
411 static const struct got_error *
412 start_read_object_child(struct got_repository *repo)
414 const struct got_error *err = NULL;
417 struct imsgbuf *ibuf;
419 ibuf = calloc(1, sizeof(*ibuf));
421 return got_error_from_errno("calloc");
423 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
424 err = got_error_from_errno("socketpair");
431 err = got_error_from_errno("fork");
436 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
441 if (close(imsg_fds[1]) == -1) {
442 err = got_error_from_errno("close");
447 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
449 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
450 imsg_init(ibuf, imsg_fds[0]);
451 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
456 const struct got_error *
457 got_object_read_header_privsep(struct got_object **obj,
458 struct got_object_id *id, struct got_repository *repo, int obj_fd)
460 const struct got_error *err;
462 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
463 return request_object(obj, id, repo, obj_fd);
465 err = start_read_object_child(repo);
471 return request_object(obj, id, repo, obj_fd);
474 static const struct got_error *
475 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
476 int outfd, struct got_object_id *id, struct got_repository *repo,
479 const struct got_error *err;
481 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
482 return request_raw_object(outbuf, size, hdrlen, outfd, id,
485 err = start_read_object_child(repo);
489 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
493 const struct got_error *
494 got_object_open(struct got_object **obj, struct got_repository *repo,
495 struct got_object_id *id)
497 const struct got_error *err = NULL;
500 *obj = got_repo_get_cached_object(repo, id);
506 err = got_object_open_packed(obj, id, repo);
507 if (err && err->code != GOT_ERR_NO_OBJ)
511 return got_repo_cache_object(repo, id, *obj);
514 err = got_object_open_loose_fd(&fd, id, repo);
516 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
517 err = got_error_no_obj(id);
521 err = got_object_read_header_privsep(obj, id, repo, fd);
525 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
528 return got_repo_cache_object(repo, id, *obj);
531 /* *outfd must be initialized to -1 by caller */
532 const struct got_error *
533 got_object_raw_open(struct got_raw_object **obj, int *outfd,
534 struct got_repository *repo, struct got_object_id *id, size_t blocksize)
536 const struct got_error *err = NULL;
537 struct got_packidx *packidx = NULL;
539 uint8_t *outbuf = NULL;
542 char *path_packfile = NULL;
544 *obj = got_repo_get_cached_raw_object(repo, id);
551 *outfd = got_opentempfd();
553 return got_error_from_errno("got_opentempfd");
556 err = got_repo_search_packidx(&packidx, &idx, repo, id);
558 struct got_pack *pack = NULL;
560 err = got_packidx_get_packfile_path(&path_packfile,
561 packidx->path_packidx);
565 pack = got_repo_get_cached_pack(repo, path_packfile);
567 err = got_repo_cache_pack(&pack, repo, path_packfile,
572 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
573 *outfd, pack, packidx, idx, id);
576 } else if (err->code == GOT_ERR_NO_OBJ) {
579 err = got_object_open_loose_fd(&fd, id, repo);
582 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
588 *obj = calloc(1, sizeof(**obj));
590 err = got_error_from_errno("calloc");
595 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
596 if ((*obj)->f == NULL) {
597 err = got_error_from_errno("fdopen");
600 (*obj)->data = outbuf;
603 if (fstat(*outfd, &sb) == -1) {
604 err = got_error_from_errno("fstat");
608 if (sb.st_size != hdrlen + size) {
609 err = got_error(GOT_ERR_PRIVSEP_LEN);
613 (*obj)->f = fdopen(*outfd, "r");
614 if ((*obj)->f == NULL) {
615 err = got_error_from_errno("fdopen");
621 (*obj)->hdrlen = hdrlen;
623 (*obj)->blocksize = blocksize;
624 err = got_repo_cache_raw_object(repo, id, *obj);
629 got_object_raw_close(*obj);
639 got_object_raw_rewind(struct got_raw_object *obj)
646 got_object_raw_get_hdrlen(struct got_raw_object *obj)
652 got_object_raw_get_read_buf(struct got_raw_object *obj)
654 return obj->read_buf;
657 const struct got_error *
658 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
662 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
663 if (n == 0 && ferror(obj->f))
664 return got_ferror(obj->f, GOT_ERR_IO);
669 const struct got_error *
670 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
673 struct got_object_id id;
675 if (!got_parse_sha1_digest(id.sha1, id_str))
676 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
678 return got_object_open(obj, repo, &id);
681 const struct got_error *
682 got_object_resolve_id_str(struct got_object_id **id,
683 struct got_repository *repo, const char *id_str)
685 const struct got_error *err = NULL;
686 struct got_object *obj;
688 err = got_object_open_by_id_str(&obj, repo, id_str);
692 *id = got_object_id_dup(got_object_get_id(obj));
693 got_object_close(obj);
695 return got_error_from_errno("got_object_id_dup");
700 static const struct got_error *
701 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
702 int pack_idx, struct got_object_id *id)
704 const struct got_error *err = NULL;
706 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
711 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
715 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
719 static const struct got_error *
720 read_packed_commit_privsep(struct got_commit_object **commit,
721 struct got_pack *pack, struct got_packidx *packidx, int idx,
722 struct got_object_id *id)
724 const struct got_error *err = NULL;
726 if (pack->privsep_child)
727 return request_packed_commit(commit, pack, idx, id);
729 err = start_pack_privsep_child(pack, packidx);
733 return request_packed_commit(commit, pack, idx, id);
736 static const struct got_error *
737 request_commit(struct got_commit_object **commit, struct got_repository *repo,
738 int fd, struct got_object_id *id)
740 const struct got_error *err = NULL;
741 struct imsgbuf *ibuf;
743 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
745 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
749 return got_privsep_recv_commit(commit, ibuf);
752 static const struct got_error *
753 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
754 struct got_object_id *id, struct got_repository *repo)
756 const struct got_error *err;
759 struct imsgbuf *ibuf;
761 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
762 return request_commit(commit, repo, obj_fd, id);
764 ibuf = calloc(1, sizeof(*ibuf));
766 return got_error_from_errno("calloc");
768 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
769 err = got_error_from_errno("socketpair");
776 err = got_error_from_errno("fork");
781 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
786 if (close(imsg_fds[1]) == -1) {
787 err = got_error_from_errno("close");
791 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
793 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
794 imsg_init(ibuf, imsg_fds[0]);
795 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
797 return request_commit(commit, repo, obj_fd, id);
801 static const struct got_error *
802 open_commit(struct got_commit_object **commit,
803 struct got_repository *repo, struct got_object_id *id, int check_cache)
805 const struct got_error *err = NULL;
806 struct got_packidx *packidx = NULL;
808 char *path_packfile = NULL;
811 *commit = got_repo_get_cached_commit(repo, id);
812 if (*commit != NULL) {
819 err = got_repo_search_packidx(&packidx, &idx, repo, id);
821 struct got_pack *pack = NULL;
823 err = got_packidx_get_packfile_path(&path_packfile,
824 packidx->path_packidx);
828 pack = got_repo_get_cached_pack(repo, path_packfile);
830 err = got_repo_cache_pack(&pack, repo, path_packfile,
835 err = read_packed_commit_privsep(commit, pack,
837 } else if (err->code == GOT_ERR_NO_OBJ) {
840 err = got_object_open_loose_fd(&fd, id, repo);
843 err = read_commit_privsep(commit, fd, id, repo);
848 err = got_repo_cache_commit(repo, id, *commit);
855 const struct got_error *
856 got_object_open_as_commit(struct got_commit_object **commit,
857 struct got_repository *repo, struct got_object_id *id)
859 *commit = got_repo_get_cached_commit(repo, id);
860 if (*commit != NULL) {
865 return open_commit(commit, repo, id, 0);
868 const struct got_error *
869 got_object_commit_open(struct got_commit_object **commit,
870 struct got_repository *repo, struct got_object *obj)
872 return open_commit(commit, repo, got_object_get_id(obj), 1);
875 const struct got_error *
876 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
878 const struct got_error *err = NULL;
880 *qid = calloc(1, sizeof(**qid));
882 return got_error_from_errno("calloc");
884 (*qid)->id = got_object_id_dup(id);
885 if ((*qid)->id == NULL) {
886 err = got_error_from_errno("got_object_id_dup");
887 got_object_qid_free(*qid);
895 const struct got_error *
896 got_object_id_queue_copy(const struct got_object_id_queue *src,
897 struct got_object_id_queue *dest)
899 const struct got_error *err;
900 struct got_object_qid *qid;
902 STAILQ_FOREACH(qid, src, entry) {
903 struct got_object_qid *new;
905 * Deep-copy the object ID only. Let the caller deal
906 * with setting up the new->data pointer if needed.
908 err = got_object_qid_alloc(&new, qid->id);
910 got_object_id_queue_free(dest);
913 STAILQ_INSERT_TAIL(dest, new, entry);
919 static const struct got_error *
920 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
921 int pack_idx, struct got_object_id *id)
923 const struct got_error *err = NULL;
925 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
930 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
933 static const struct got_error *
934 read_packed_tree_privsep(struct got_tree_object **tree,
935 struct got_pack *pack, struct got_packidx *packidx, int idx,
936 struct got_object_id *id)
938 const struct got_error *err = NULL;
940 if (pack->privsep_child)
941 return request_packed_tree(tree, pack, idx, id);
943 err = start_pack_privsep_child(pack, packidx);
947 return request_packed_tree(tree, pack, idx, id);
950 static const struct got_error *
951 request_tree(struct got_tree_object **tree, struct got_repository *repo,
952 int fd, struct got_object_id *id)
954 const struct got_error *err = NULL;
955 struct imsgbuf *ibuf;
957 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
959 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
963 return got_privsep_recv_tree(tree, ibuf);
966 const struct got_error *
967 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
968 struct got_object_id *id, struct got_repository *repo)
970 const struct got_error *err;
973 struct imsgbuf *ibuf;
975 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
976 return request_tree(tree, repo, obj_fd, id);
978 ibuf = calloc(1, sizeof(*ibuf));
980 return got_error_from_errno("calloc");
982 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
983 err = got_error_from_errno("socketpair");
990 err = got_error_from_errno("fork");
995 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1000 if (close(imsg_fds[1]) == -1) {
1001 err = got_error_from_errno("close");
1005 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1007 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1008 imsg_init(ibuf, imsg_fds[0]);
1009 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1012 return request_tree(tree, repo, obj_fd, id);
1015 static const struct got_error *
1016 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1017 struct got_object_id *id, int check_cache)
1019 const struct got_error *err = NULL;
1020 struct got_packidx *packidx = NULL;
1022 char *path_packfile = NULL;
1025 *tree = got_repo_get_cached_tree(repo, id);
1026 if (*tree != NULL) {
1033 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1035 struct got_pack *pack = NULL;
1037 err = got_packidx_get_packfile_path(&path_packfile,
1038 packidx->path_packidx);
1042 pack = got_repo_get_cached_pack(repo, path_packfile);
1044 err = got_repo_cache_pack(&pack, repo, path_packfile,
1049 err = read_packed_tree_privsep(tree, pack,
1051 } else if (err->code == GOT_ERR_NO_OBJ) {
1054 err = got_object_open_loose_fd(&fd, id, repo);
1057 err = read_tree_privsep(tree, fd, id, repo);
1062 err = got_repo_cache_tree(repo, id, *tree);
1065 free(path_packfile);
1069 const struct got_error *
1070 got_object_open_as_tree(struct got_tree_object **tree,
1071 struct got_repository *repo, struct got_object_id *id)
1073 *tree = got_repo_get_cached_tree(repo, id);
1074 if (*tree != NULL) {
1079 return open_tree(tree, repo, id, 0);
1082 const struct got_error *
1083 got_object_tree_open(struct got_tree_object **tree,
1084 struct got_repository *repo, struct got_object *obj)
1086 return open_tree(tree, repo, got_object_get_id(obj), 1);
1090 got_object_tree_get_nentries(struct got_tree_object *tree)
1092 return tree->nentries;
1095 struct got_tree_entry *
1096 got_object_tree_get_first_entry(struct got_tree_object *tree)
1098 return got_object_tree_get_entry(tree, 0);
1101 struct got_tree_entry *
1102 got_object_tree_get_last_entry(struct got_tree_object *tree)
1104 return got_object_tree_get_entry(tree, tree->nentries - 1);
1107 struct got_tree_entry *
1108 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1110 if (i < 0 || i >= tree->nentries)
1112 return &tree->entries[i];
1116 got_tree_entry_get_mode(struct got_tree_entry *te)
1122 got_tree_entry_get_name(struct got_tree_entry *te)
1124 return &te->name[0];
1127 struct got_object_id *
1128 got_tree_entry_get_id(struct got_tree_entry *te)
1133 const struct got_error *
1134 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1136 const struct got_error *err = NULL;
1137 size_t len, totlen, hdrlen, offset;
1141 hdrlen = got_object_blob_get_hdrlen(blob);
1147 err = got_object_blob_read_block(&len, blob);
1154 totlen += len - hdrlen;
1155 p = realloc(*s, totlen + 1);
1157 err = got_error_from_errno("realloc");
1163 /* Skip blob object header first time around. */
1165 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1170 (*s)[totlen] = '\0';
1174 const struct got_error *
1175 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1176 struct got_repository *repo)
1178 const struct got_error *err = NULL;
1179 struct got_blob_object *blob = NULL;
1181 *link_target = NULL;
1183 if (!got_object_tree_entry_is_symlink(te))
1184 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1186 err = got_object_open_as_blob(&blob, repo,
1187 got_tree_entry_get_id(te), PATH_MAX);
1191 err = got_object_blob_read_to_str(link_target, blob);
1192 got_object_blob_close(blob);
1195 *link_target = NULL;
1201 got_tree_entry_get_index(struct got_tree_entry *te)
1206 struct got_tree_entry *
1207 got_tree_entry_get_next(struct got_tree_object *tree,
1208 struct got_tree_entry *te)
1210 return got_object_tree_get_entry(tree, te->idx + 1);
1213 struct got_tree_entry *
1214 got_tree_entry_get_prev(struct got_tree_object *tree,
1215 struct got_tree_entry *te)
1217 return got_object_tree_get_entry(tree, te->idx - 1);
1220 static const struct got_error *
1221 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1222 struct got_pack *pack, struct got_packidx *packidx, int idx,
1223 struct got_object_id *id)
1225 const struct got_error *err = NULL;
1227 int basefd, accumfd; /* temporary files for delta application */
1229 basefd = got_opentempfd();
1231 return got_error_from_errno("got_opentempfd");
1232 accumfd = got_opentempfd();
1234 return got_error_from_errno("got_opentempfd");
1236 outfd_child = dup(outfd);
1237 if (outfd_child == -1)
1238 return got_error_from_errno("dup");
1240 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1244 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1252 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1259 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1264 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1265 pack->privsep_child->ibuf);
1269 if (lseek(outfd, SEEK_SET, 0) == -1)
1270 err = got_error_from_errno("lseek");
1275 static const struct got_error *
1276 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1277 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1278 struct got_object_id *id)
1280 const struct got_error *err = NULL;
1282 if (pack->privsep_child == NULL) {
1283 err = start_pack_privsep_child(pack, packidx);
1288 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1292 static const struct got_error *
1293 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1294 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1296 const struct got_error *err = NULL;
1299 outfd_child = dup(outfd);
1300 if (outfd_child == -1)
1301 return got_error_from_errno("dup");
1303 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1307 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1311 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1315 if (lseek(outfd, SEEK_SET, 0) == -1)
1316 return got_error_from_errno("lseek");
1321 static const struct got_error *
1322 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1323 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1325 const struct got_error *err;
1328 struct imsgbuf *ibuf;
1330 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1331 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1332 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1336 ibuf = calloc(1, sizeof(*ibuf));
1338 return got_error_from_errno("calloc");
1340 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1341 err = got_error_from_errno("socketpair");
1348 err = got_error_from_errno("fork");
1352 else if (pid == 0) {
1353 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1358 if (close(imsg_fds[1]) == -1) {
1359 err = got_error_from_errno("close");
1363 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1365 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1366 imsg_init(ibuf, imsg_fds[0]);
1367 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1369 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1372 static const struct got_error *
1373 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1374 struct got_object_id *id, size_t blocksize)
1376 const struct got_error *err = NULL;
1377 struct got_packidx *packidx = NULL;
1379 char *path_packfile = NULL;
1382 size_t size, hdrlen;
1385 *blob = calloc(1, sizeof(**blob));
1387 return got_error_from_errno("calloc");
1389 outfd = got_opentempfd();
1391 return got_error_from_errno("got_opentempfd");
1393 (*blob)->read_buf = malloc(blocksize);
1394 if ((*blob)->read_buf == NULL) {
1395 err = got_error_from_errno("malloc");
1399 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1401 struct got_pack *pack = NULL;
1403 err = got_packidx_get_packfile_path(&path_packfile,
1404 packidx->path_packidx);
1408 pack = got_repo_get_cached_pack(repo, path_packfile);
1410 err = got_repo_cache_pack(&pack, repo, path_packfile,
1415 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1416 pack, packidx, idx, id);
1417 } else if (err->code == GOT_ERR_NO_OBJ) {
1420 err = got_object_open_loose_fd(&infd, id, repo);
1423 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1429 if (hdrlen > size) {
1430 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1435 if (close(outfd) == -1 && err == NULL)
1436 err = got_error_from_errno("close");
1438 (*blob)->f = fmemopen(outbuf, size, "rb");
1439 if ((*blob)->f == NULL) {
1440 err = got_error_from_errno("fmemopen");
1444 (*blob)->data = outbuf;
1446 if (fstat(outfd, &sb) == -1) {
1447 err = got_error_from_errno("fstat");
1451 if (sb.st_size != size) {
1452 err = got_error(GOT_ERR_PRIVSEP_LEN);
1456 (*blob)->f = fdopen(outfd, "rb");
1457 if ((*blob)->f == NULL) {
1458 err = got_error_from_errno("fdopen");
1465 (*blob)->hdrlen = hdrlen;
1466 (*blob)->blocksize = blocksize;
1467 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1470 free(path_packfile);
1473 got_object_blob_close(*blob);
1475 } else if (outfd != -1)
1481 const struct got_error *
1482 got_object_open_as_blob(struct got_blob_object **blob,
1483 struct got_repository *repo, struct got_object_id *id,
1486 return open_blob(blob, repo, id, blocksize);
1489 const struct got_error *
1490 got_object_blob_open(struct got_blob_object **blob,
1491 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1493 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1496 const struct got_error *
1497 got_object_blob_close(struct got_blob_object *blob)
1499 const struct got_error *err = NULL;
1500 free(blob->read_buf);
1501 if (blob->f && fclose(blob->f) == EOF)
1502 err = got_error_from_errno("fclose");
1509 got_object_blob_rewind(struct got_blob_object *blob)
1516 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1518 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1522 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1524 return blob->hdrlen;
1528 got_object_blob_get_read_buf(struct got_blob_object *blob)
1530 return blob->read_buf;
1533 const struct got_error *
1534 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1538 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1539 if (n == 0 && ferror(blob->f))
1540 return got_ferror(blob->f, GOT_ERR_IO);
1545 const struct got_error *
1546 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1547 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1549 const struct got_error *err = NULL;
1550 size_t n, len, hdrlen;
1553 const int alloc_chunksz = 512;
1555 off_t off = 0, total_len = 0;
1558 *line_offsets = NULL;
1564 hdrlen = got_object_blob_get_hdrlen(blob);
1566 err = got_object_blob_read_block(&len, blob);
1571 buf = got_object_blob_get_read_buf(blob);
1574 if (line_offsets && *line_offsets == NULL) {
1575 /* Have some data but perhaps no '\n'. */
1577 nalloc = alloc_chunksz;
1578 *line_offsets = calloc(nalloc,
1579 sizeof(**line_offsets));
1580 if (*line_offsets == NULL)
1581 return got_error_from_errno("calloc");
1583 /* Skip forward over end of first line. */
1590 /* Scan '\n' offsets in remaining chunk of data. */
1592 if (buf[i] != '\n') {
1597 if (line_offsets && nalloc < *nlines) {
1598 size_t n = *nlines + alloc_chunksz;
1599 off_t *o = recallocarray(*line_offsets,
1600 nalloc, n, sizeof(**line_offsets));
1602 free(*line_offsets);
1603 *line_offsets = NULL;
1604 return got_error_from_errno(
1611 off = total_len + i - hdrlen + 1;
1612 (*line_offsets)[*nlines - 1] = off;
1617 /* Skip blob object header first time around. */
1618 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1619 if (n != len - hdrlen)
1620 return got_ferror(outfile, GOT_ERR_IO);
1621 total_len += len - hdrlen;
1625 if (fflush(outfile) != 0)
1626 return got_error_from_errno("fflush");
1630 *filesize = total_len;
1635 static const struct got_error *
1636 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1637 int pack_idx, struct got_object_id *id)
1639 const struct got_error *err = NULL;
1641 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1646 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1649 static const struct got_error *
1650 read_packed_tag_privsep(struct got_tag_object **tag,
1651 struct got_pack *pack, struct got_packidx *packidx, int idx,
1652 struct got_object_id *id)
1654 const struct got_error *err = NULL;
1656 if (pack->privsep_child)
1657 return request_packed_tag(tag, pack, idx, id);
1659 err = start_pack_privsep_child(pack, packidx);
1663 return request_packed_tag(tag, pack, idx, id);
1666 static const struct got_error *
1667 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1668 int fd, struct got_object_id *id)
1670 const struct got_error *err = NULL;
1671 struct imsgbuf *ibuf;
1673 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1675 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1679 return got_privsep_recv_tag(tag, ibuf);
1682 static const struct got_error *
1683 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1684 struct got_object_id *id, struct got_repository *repo)
1686 const struct got_error *err;
1689 struct imsgbuf *ibuf;
1691 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1692 return request_tag(tag, repo, obj_fd, id);
1694 ibuf = calloc(1, sizeof(*ibuf));
1696 return got_error_from_errno("calloc");
1698 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1699 err = got_error_from_errno("socketpair");
1706 err = got_error_from_errno("fork");
1710 else if (pid == 0) {
1711 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1716 if (close(imsg_fds[1]) == -1) {
1717 err = got_error_from_errno("close");
1721 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1723 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1724 imsg_init(ibuf, imsg_fds[0]);
1725 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1727 return request_tag(tag, repo, obj_fd, id);
1730 static const struct got_error *
1731 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1732 struct got_object_id *id, int check_cache)
1734 const struct got_error *err = NULL;
1735 struct got_packidx *packidx = NULL;
1737 char *path_packfile = NULL;
1738 struct got_object *obj = NULL;
1739 int obj_type = GOT_OBJ_TYPE_ANY;
1742 *tag = got_repo_get_cached_tag(repo, id);
1750 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1752 struct got_pack *pack = NULL;
1754 err = got_packidx_get_packfile_path(&path_packfile,
1755 packidx->path_packidx);
1759 pack = got_repo_get_cached_pack(repo, path_packfile);
1761 err = got_repo_cache_pack(&pack, repo, path_packfile,
1767 /* Beware of "lightweight" tags: Check object type first. */
1768 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1772 obj_type = obj->type;
1773 got_object_close(obj);
1774 if (obj_type != GOT_OBJ_TYPE_TAG) {
1775 err = got_error(GOT_ERR_OBJ_TYPE);
1778 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1779 } else if (err->code == GOT_ERR_NO_OBJ) {
1782 err = got_object_open_loose_fd(&fd, id, repo);
1785 err = got_object_read_header_privsep(&obj, id, repo, fd);
1788 obj_type = obj->type;
1789 got_object_close(obj);
1790 if (obj_type != GOT_OBJ_TYPE_TAG)
1791 return got_error(GOT_ERR_OBJ_TYPE);
1793 err = got_object_open_loose_fd(&fd, id, repo);
1796 err = read_tag_privsep(tag, fd, id, repo);
1801 err = got_repo_cache_tag(repo, id, *tag);
1804 free(path_packfile);
1808 const struct got_error *
1809 got_object_open_as_tag(struct got_tag_object **tag,
1810 struct got_repository *repo, struct got_object_id *id)
1812 *tag = got_repo_get_cached_tag(repo, id);
1818 return open_tag(tag, repo, id, 0);
1821 const struct got_error *
1822 got_object_tag_open(struct got_tag_object **tag,
1823 struct got_repository *repo, struct got_object *obj)
1825 return open_tag(tag, repo, got_object_get_id(obj), 1);
1829 got_object_tag_get_name(struct got_tag_object *tag)
1835 got_object_tag_get_object_type(struct got_tag_object *tag)
1837 return tag->obj_type;
1840 struct got_object_id *
1841 got_object_tag_get_object_id(struct got_tag_object *tag)
1847 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1849 return tag->tagger_time;
1853 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1855 return tag->tagger_gmtoff;
1859 got_object_tag_get_tagger(struct got_tag_object *tag)
1865 got_object_tag_get_message(struct got_tag_object *tag)
1870 static struct got_tree_entry *
1871 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1875 /* Note that tree entries are sorted in strncmp() order. */
1876 for (i = 0; i < tree->nentries; i++) {
1877 struct got_tree_entry *te = &tree->entries[i];
1878 int cmp = strncmp(te->name, name, len);
1883 if (te->name[len] == '\0')
1889 struct got_tree_entry *
1890 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1892 return find_entry_by_name(tree, name, strlen(name));
1895 const struct got_error *
1896 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1897 struct got_repository *repo, struct got_tree_object *tree,
1900 const struct got_error *err = NULL;
1901 struct got_tree_object *subtree = NULL;
1902 struct got_tree_entry *te = NULL;
1903 const char *seg, *s;
1915 struct got_tree_object *next_tree;
1924 te = find_entry_by_name(subtree, seg, seglen);
1926 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1937 err = got_object_open_as_tree(&next_tree, repo,
1942 if (subtree != tree)
1943 got_object_tree_close(subtree);
1944 subtree = next_tree;
1949 *id = got_object_id_dup(&te->id);
1951 return got_error_from_errno("got_object_id_dup");
1955 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1957 if (subtree && subtree != tree)
1958 got_object_tree_close(subtree);
1961 const struct got_error *
1962 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1963 struct got_object_id *commit_id, const char *path)
1965 const struct got_error *err = NULL;
1966 struct got_commit_object *commit = NULL;
1967 struct got_tree_object *tree = NULL;
1971 err = got_object_open_as_commit(&commit, repo, commit_id);
1975 /* Handle opening of root of commit's tree. */
1976 if (got_path_is_root_dir(path)) {
1977 *id = got_object_id_dup(commit->tree_id);
1979 err = got_error_from_errno("got_object_id_dup");
1981 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1984 err = got_object_tree_find_path(id, NULL, repo, tree, path);
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 = got_packidx_get_packfile_path(&path_packfile,
2278 packidx->path_packidx);
2282 pack = got_repo_get_cached_pack(repo, path_packfile);
2284 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2289 if (pack->privsep_child == NULL) {
2290 err = start_pack_privsep_child(pack, packidx);
2295 err = got_privsep_send_commit_traversal_request(
2296 pack->privsep_child->ibuf, commit_id, idx, path);
2300 err = got_privsep_recv_traversed_commits(&changed_commit,
2301 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2305 if (changed_commit) {
2307 * Cache the commit in which the path was changed.
2308 * This commit might be opened again soon.
2310 changed_commit->refcnt++;
2311 err = got_repo_cache_commit(repo, changed_commit_id,
2313 got_object_commit_close(changed_commit);
2316 free(path_packfile);
2317 free(changed_commit_id);