2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <sys/socket.h>
23 #include <sys/resource.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_repository.h"
43 #include "got_opentemp.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_object.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_object_idcache.h"
52 #include "got_lib_object_cache.h"
53 #include "got_lib_object_parse.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_repository.h"
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 struct got_object_id *
62 got_object_get_id(struct got_object *obj)
67 const struct got_error *
68 got_object_get_id_str(char **outbuf, struct got_object *obj)
70 return got_object_id_str(outbuf, &obj->id);
73 const struct got_error *
74 got_object_get_type(int *type, struct got_repository *repo,
75 struct got_object_id *id)
77 const struct got_error *err = NULL;
78 struct got_object *obj;
80 err = got_object_open(&obj, repo, id);
85 case GOT_OBJ_TYPE_COMMIT:
86 case GOT_OBJ_TYPE_TREE:
87 case GOT_OBJ_TYPE_BLOB:
88 case GOT_OBJ_TYPE_TAG:
92 err = got_error(GOT_ERR_OBJ_TYPE);
96 got_object_close(obj);
100 const struct got_error *
101 got_object_get_path(char **path, struct got_object_id *id,
102 struct got_repository *repo)
104 const struct got_error *err = NULL;
110 path_objects = got_repo_get_path_objects(repo);
111 if (path_objects == NULL)
112 return got_error_from_errno("got_repo_get_path_objects");
114 err = got_object_id_str(&hex, id);
118 if (asprintf(path, "%s/%.2x/%s", path_objects,
119 id->sha1[0], hex + 2) == -1)
120 err = got_error_from_errno("asprintf");
128 static const struct got_error *
129 open_loose_object(int *fd, struct got_object_id *id,
130 struct got_repository *repo)
132 const struct got_error *err = NULL;
135 err = got_object_get_path(&path, id, repo);
138 *fd = open(path, O_RDONLY | O_NOFOLLOW);
140 err = got_error_from_errno2("open", path);
148 static const struct got_error *
149 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
150 struct got_object_id *id)
152 const struct got_error *err = NULL;
153 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
155 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
159 err = got_privsep_recv_obj(obj, ibuf);
163 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
168 static const struct got_error *
169 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
170 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
172 const struct got_error *err = NULL;
173 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
175 int basefd, accumfd; /* temporary files for delta application */
177 basefd = got_opentempfd();
179 return got_error_from_errno("got_opentempfd");
181 accumfd = got_opentempfd();
184 return got_error_from_errno("got_opentempfd");
187 outfd_child = dup(outfd);
188 if (outfd_child == -1) {
189 err = got_error_from_errno("dup");
195 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
203 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
211 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
218 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
223 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
231 set_max_datasize(void)
235 if (getrlimit(RLIMIT_DATA, &rl) != 0)
238 rl.rlim_cur = rl.rlim_max;
239 setrlimit(RLIMIT_DATA, &rl);
242 static const struct got_error *
243 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
245 const struct got_error *err = NULL;
248 struct imsgbuf *ibuf;
250 ibuf = calloc(1, sizeof(*ibuf));
252 return got_error_from_errno("calloc");
254 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
255 if (pack->privsep_child == NULL) {
256 err = got_error_from_errno("calloc");
261 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
262 err = got_error_from_errno("socketpair");
268 err = got_error_from_errno("fork");
270 } else if (pid == 0) {
272 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
273 pack->path_packfile);
277 if (close(imsg_fds[1]) == -1)
278 return got_error_from_errno("close");
279 pack->privsep_child->imsg_fd = imsg_fds[0];
280 pack->privsep_child->pid = pid;
281 imsg_init(ibuf, imsg_fds[0]);
282 pack->privsep_child->ibuf = ibuf;
284 err = got_privsep_init_pack_child(ibuf, pack, packidx);
286 const struct got_error *child_err;
287 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
288 child_err = got_privsep_wait_for_child(
289 pack->privsep_child->pid);
290 if (child_err && err == NULL)
296 free(pack->privsep_child);
297 pack->privsep_child = NULL;
302 static const struct got_error *
303 read_packed_object_privsep(struct got_object **obj,
304 struct got_repository *repo, struct got_pack *pack,
305 struct got_packidx *packidx, int idx, struct got_object_id *id)
307 const struct got_error *err = NULL;
309 if (pack->privsep_child == NULL) {
310 err = start_pack_privsep_child(pack, packidx);
315 return request_packed_object(obj, pack, idx, id);
318 static const struct got_error *
319 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
320 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
321 struct got_object_id *id)
323 const struct got_error *err = NULL;
325 if (pack->privsep_child == NULL) {
326 err = start_pack_privsep_child(pack, packidx);
331 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
335 static const struct got_error *
336 open_packed_object(struct got_object **obj, struct got_object_id *id,
337 struct got_repository *repo)
339 const struct got_error *err = NULL;
340 struct got_pack *pack = NULL;
341 struct got_packidx *packidx = NULL;
345 err = got_repo_search_packidx(&packidx, &idx, repo, id);
349 err = got_packidx_get_packfile_path(&path_packfile, 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_repository *repo, int fd)
371 const struct got_error *err = NULL;
372 struct imsgbuf *ibuf;
374 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
376 err = got_privsep_send_obj_req(ibuf, fd);
380 return got_privsep_recv_obj(obj, ibuf);
383 static const struct got_error *
384 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
385 struct got_repository *repo, int infd)
387 const struct got_error *err = NULL;
388 struct imsgbuf *ibuf;
391 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
393 outfd_child = dup(outfd);
394 if (outfd_child == -1)
395 return got_error_from_errno("dup");
397 err = got_privsep_send_raw_obj_req(ibuf, infd);
401 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
405 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
408 static const struct got_error *
409 start_read_object_child(struct got_repository *repo)
411 const struct got_error *err = NULL;
414 struct imsgbuf *ibuf;
416 ibuf = calloc(1, sizeof(*ibuf));
418 return got_error_from_errno("calloc");
420 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
421 err = got_error_from_errno("socketpair");
428 err = got_error_from_errno("fork");
433 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
438 if (close(imsg_fds[1]) == -1) {
439 err = got_error_from_errno("close");
444 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
446 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
447 imsg_init(ibuf, imsg_fds[0]);
448 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
453 static const struct got_error *
454 read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
457 const struct got_error *err;
459 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
460 return request_object(obj, repo, obj_fd);
462 err = start_read_object_child(repo);
468 return request_object(obj, repo, obj_fd);
471 static const struct got_error *
472 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
473 int outfd, struct got_repository *repo, int obj_fd)
475 const struct got_error *err;
477 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
478 return request_raw_object(outbuf, size, hdrlen, outfd, repo,
481 err = start_read_object_child(repo);
485 return request_raw_object(outbuf, size, hdrlen, outfd, repo, obj_fd);
488 const struct got_error *
489 got_object_open(struct got_object **obj, struct got_repository *repo,
490 struct got_object_id *id)
492 const struct got_error *err = NULL;
496 *obj = got_repo_get_cached_object(repo, id);
502 err = open_packed_object(obj, id, repo);
503 if (err && err->code != GOT_ERR_NO_OBJ)
507 return got_repo_cache_object(repo, id, *obj);
510 err = got_object_get_path(&path, id, repo);
514 fd = open(path, O_RDONLY | O_NOFOLLOW);
517 err = got_error_no_obj(id);
519 err = got_error_from_errno2("open", path);
522 err = read_object_header_privsep(obj, repo, fd);
525 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
529 err = got_repo_cache_object(repo, id, *obj);
535 const struct got_error *
536 got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo,
537 struct got_object_id *id, size_t blocksize)
539 const struct got_error *err = NULL;
540 struct got_packidx *packidx = NULL;
542 uint8_t *outbuf = NULL;
546 char *path_packfile = NULL;
550 outfd = got_opentempfd();
552 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, packidx);
562 pack = got_repo_get_cached_pack(repo, path_packfile);
564 err = got_repo_cache_pack(&pack, repo, path_packfile,
569 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
570 outfd, pack, packidx, idx, id);
571 } else if (err->code == GOT_ERR_NO_OBJ) {
574 err = open_loose_object(&fd, id, repo);
577 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
582 err = got_error(GOT_ERR_BAD_OBJ_HDR);
586 *obj = calloc(1, sizeof(**obj));
588 err = got_error_from_errno("calloc");
592 (*obj)->read_buf = malloc(blocksize);
593 if ((*obj)->read_buf == NULL) {
594 err = got_error_from_errno("malloc");
599 if (close(outfd) == -1) {
600 err = got_error_from_errno("close");
604 (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
605 if ((*obj)->f == NULL) {
606 err = got_error_from_errno("fdopen");
609 (*obj)->data = outbuf;
612 if (fstat(outfd, &sb) == -1) {
613 err = got_error_from_errno("fstat");
617 if (sb.st_size != size) {
618 err = got_error(GOT_ERR_PRIVSEP_LEN);
622 (*obj)->f = fdopen(outfd, "r");
623 if ((*obj)->f == NULL) {
624 err = got_error_from_errno("fdopen");
630 (*obj)->hdrlen = hdrlen;
632 (*obj)->blocksize = blocksize;
637 got_object_raw_close(*obj);
648 got_object_raw_rewind(struct got_raw_object *obj)
655 got_object_raw_get_hdrlen(struct got_raw_object *obj)
661 got_object_raw_get_read_buf(struct got_raw_object *obj)
663 return obj->read_buf;
666 const struct got_error *
667 got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
671 n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
672 if (n == 0 && ferror(obj->f))
673 return got_ferror(obj->f, GOT_ERR_IO);
678 const struct got_error *
679 got_object_raw_close(struct got_raw_object *obj)
681 const struct got_error *err = NULL;
684 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
685 err = got_error_from_errno("fclose");
691 const struct got_error *
692 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
695 struct got_object_id id;
697 if (!got_parse_sha1_digest(id.sha1, id_str))
698 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
700 return got_object_open(obj, repo, &id);
703 const struct got_error *
704 got_object_resolve_id_str(struct got_object_id **id,
705 struct got_repository *repo, const char *id_str)
707 const struct got_error *err = NULL;
708 struct got_object *obj;
710 err = got_object_open_by_id_str(&obj, repo, id_str);
714 *id = got_object_id_dup(got_object_get_id(obj));
715 got_object_close(obj);
717 return got_error_from_errno("got_object_id_dup");
722 static const struct got_error *
723 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
724 int pack_idx, struct got_object_id *id)
726 const struct got_error *err = NULL;
728 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
733 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
737 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
741 static const struct got_error *
742 read_packed_commit_privsep(struct got_commit_object **commit,
743 struct got_pack *pack, struct got_packidx *packidx, int idx,
744 struct got_object_id *id)
746 const struct got_error *err = NULL;
748 if (pack->privsep_child)
749 return request_packed_commit(commit, pack, idx, id);
751 err = start_pack_privsep_child(pack, packidx);
755 return request_packed_commit(commit, pack, idx, id);
758 static const struct got_error *
759 request_commit(struct got_commit_object **commit, struct got_repository *repo,
762 const struct got_error *err = NULL;
763 struct imsgbuf *ibuf;
765 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
767 err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
771 return got_privsep_recv_commit(commit, ibuf);
774 static const struct got_error *
775 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
776 struct got_repository *repo)
778 const struct got_error *err;
781 struct imsgbuf *ibuf;
783 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
784 return request_commit(commit, repo, obj_fd);
786 ibuf = calloc(1, sizeof(*ibuf));
788 return got_error_from_errno("calloc");
790 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
791 err = got_error_from_errno("socketpair");
798 err = got_error_from_errno("fork");
803 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
808 if (close(imsg_fds[1]) == -1) {
809 err = got_error_from_errno("close");
813 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
815 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
816 imsg_init(ibuf, imsg_fds[0]);
817 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
819 return request_commit(commit, repo, obj_fd);
823 static const struct got_error *
824 open_commit(struct got_commit_object **commit,
825 struct got_repository *repo, struct got_object_id *id, int check_cache)
827 const struct got_error *err = NULL;
828 struct got_packidx *packidx = NULL;
830 char *path_packfile = NULL;
833 *commit = got_repo_get_cached_commit(repo, id);
834 if (*commit != NULL) {
841 err = got_repo_search_packidx(&packidx, &idx, repo, id);
843 struct got_pack *pack = NULL;
845 err = got_packidx_get_packfile_path(&path_packfile, packidx);
849 pack = got_repo_get_cached_pack(repo, path_packfile);
851 err = got_repo_cache_pack(&pack, repo, path_packfile,
856 err = read_packed_commit_privsep(commit, pack,
858 } else if (err->code == GOT_ERR_NO_OBJ) {
861 err = open_loose_object(&fd, id, repo);
864 err = read_commit_privsep(commit, fd, repo);
869 err = got_repo_cache_commit(repo, id, *commit);
876 const struct got_error *
877 got_object_open_as_commit(struct got_commit_object **commit,
878 struct got_repository *repo, struct got_object_id *id)
880 *commit = got_repo_get_cached_commit(repo, id);
881 if (*commit != NULL) {
886 return open_commit(commit, repo, id, 0);
889 const struct got_error *
890 got_object_commit_open(struct got_commit_object **commit,
891 struct got_repository *repo, struct got_object *obj)
893 return open_commit(commit, repo, got_object_get_id(obj), 1);
896 const struct got_error *
897 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
899 const struct got_error *err = NULL;
901 *qid = calloc(1, sizeof(**qid));
903 return got_error_from_errno("calloc");
905 (*qid)->id = got_object_id_dup(id);
906 if ((*qid)->id == NULL) {
907 err = got_error_from_errno("got_object_id_dup");
908 got_object_qid_free(*qid);
916 static const struct got_error *
917 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
918 int pack_idx, struct got_object_id *id)
920 const struct got_error *err = NULL;
922 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
927 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
930 static const struct got_error *
931 read_packed_tree_privsep(struct got_tree_object **tree,
932 struct got_pack *pack, struct got_packidx *packidx, int idx,
933 struct got_object_id *id)
935 const struct got_error *err = NULL;
937 if (pack->privsep_child)
938 return request_packed_tree(tree, pack, idx, id);
940 err = start_pack_privsep_child(pack, packidx);
944 return request_packed_tree(tree, pack, idx, id);
947 static const struct got_error *
948 request_tree(struct got_tree_object **tree, struct got_repository *repo,
951 const struct got_error *err = NULL;
952 struct imsgbuf *ibuf;
954 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
956 err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
960 return got_privsep_recv_tree(tree, ibuf);
963 const struct got_error *
964 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
965 struct got_repository *repo)
967 const struct got_error *err;
970 struct imsgbuf *ibuf;
972 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
973 return request_tree(tree, repo, obj_fd);
975 ibuf = calloc(1, sizeof(*ibuf));
977 return got_error_from_errno("calloc");
979 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
980 err = got_error_from_errno("socketpair");
987 err = got_error_from_errno("fork");
992 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
997 if (close(imsg_fds[1]) == -1) {
998 err = got_error_from_errno("close");
1002 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1004 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1005 imsg_init(ibuf, imsg_fds[0]);
1006 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1009 return request_tree(tree, repo, obj_fd);
1012 static const struct got_error *
1013 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1014 struct got_object_id *id, int check_cache)
1016 const struct got_error *err = NULL;
1017 struct got_packidx *packidx = NULL;
1019 char *path_packfile = NULL;
1022 *tree = got_repo_get_cached_tree(repo, id);
1023 if (*tree != NULL) {
1030 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1032 struct got_pack *pack = NULL;
1034 err = got_packidx_get_packfile_path(&path_packfile, packidx);
1038 pack = got_repo_get_cached_pack(repo, path_packfile);
1040 err = got_repo_cache_pack(&pack, repo, path_packfile,
1045 err = read_packed_tree_privsep(tree, pack,
1047 } else if (err->code == GOT_ERR_NO_OBJ) {
1050 err = open_loose_object(&fd, id, repo);
1053 err = read_tree_privsep(tree, fd, repo);
1058 err = got_repo_cache_tree(repo, id, *tree);
1061 free(path_packfile);
1065 const struct got_error *
1066 got_object_open_as_tree(struct got_tree_object **tree,
1067 struct got_repository *repo, struct got_object_id *id)
1069 *tree = got_repo_get_cached_tree(repo, id);
1070 if (*tree != NULL) {
1075 return open_tree(tree, repo, id, 0);
1078 const struct got_error *
1079 got_object_tree_open(struct got_tree_object **tree,
1080 struct got_repository *repo, struct got_object *obj)
1082 return open_tree(tree, repo, got_object_get_id(obj), 1);
1086 got_object_tree_get_nentries(struct got_tree_object *tree)
1088 return tree->nentries;
1091 struct got_tree_entry *
1092 got_object_tree_get_first_entry(struct got_tree_object *tree)
1094 return got_object_tree_get_entry(tree, 0);
1097 struct got_tree_entry *
1098 got_object_tree_get_last_entry(struct got_tree_object *tree)
1100 return got_object_tree_get_entry(tree, tree->nentries - 1);
1103 struct got_tree_entry *
1104 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1106 if (i < 0 || i >= tree->nentries)
1108 return &tree->entries[i];
1112 got_tree_entry_get_mode(struct got_tree_entry *te)
1118 got_tree_entry_get_name(struct got_tree_entry *te)
1120 return &te->name[0];
1123 struct got_object_id *
1124 got_tree_entry_get_id(struct got_tree_entry *te)
1129 const struct got_error *
1130 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1132 const struct got_error *err = NULL;
1133 size_t len, totlen, hdrlen, offset;
1137 hdrlen = got_object_blob_get_hdrlen(blob);
1143 err = got_object_blob_read_block(&len, blob);
1150 totlen += len - hdrlen;
1151 p = realloc(*s, totlen + 1);
1153 err = got_error_from_errno("realloc");
1159 /* Skip blob object header first time around. */
1161 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1166 (*s)[totlen] = '\0';
1170 const struct got_error *
1171 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1172 struct got_repository *repo)
1174 const struct got_error *err = NULL;
1175 struct got_blob_object *blob = NULL;
1177 *link_target = NULL;
1179 if (!got_object_tree_entry_is_symlink(te))
1180 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1182 err = got_object_open_as_blob(&blob, repo,
1183 got_tree_entry_get_id(te), PATH_MAX);
1187 err = got_object_blob_read_to_str(link_target, blob);
1188 got_object_blob_close(blob);
1191 *link_target = NULL;
1197 got_tree_entry_get_index(struct got_tree_entry *te)
1202 struct got_tree_entry *
1203 got_tree_entry_get_next(struct got_tree_object *tree,
1204 struct got_tree_entry *te)
1206 return got_object_tree_get_entry(tree, te->idx + 1);
1209 struct got_tree_entry *
1210 got_tree_entry_get_prev(struct got_tree_object *tree,
1211 struct got_tree_entry *te)
1213 return got_object_tree_get_entry(tree, te->idx - 1);
1216 static const struct got_error *
1217 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1218 struct got_pack *pack, struct got_packidx *packidx, int idx,
1219 struct got_object_id *id)
1221 const struct got_error *err = NULL;
1223 int basefd, accumfd; /* temporary files for delta application */
1225 basefd = got_opentempfd();
1227 return got_error_from_errno("got_opentempfd");
1228 accumfd = got_opentempfd();
1230 return got_error_from_errno("got_opentempfd");
1232 outfd_child = dup(outfd);
1233 if (outfd_child == -1)
1234 return got_error_from_errno("dup");
1236 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1240 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1248 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1255 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1260 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1261 pack->privsep_child->ibuf);
1265 if (lseek(outfd, SEEK_SET, 0) == -1)
1266 err = got_error_from_errno("lseek");
1271 static const struct got_error *
1272 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1273 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1274 struct got_object_id *id)
1276 const struct got_error *err = NULL;
1278 if (pack->privsep_child == NULL) {
1279 err = start_pack_privsep_child(pack, packidx);
1284 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1288 static const struct got_error *
1289 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1290 int infd, struct imsgbuf *ibuf)
1292 const struct got_error *err = NULL;
1295 outfd_child = dup(outfd);
1296 if (outfd_child == -1)
1297 return got_error_from_errno("dup");
1299 err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
1303 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1307 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1311 if (lseek(outfd, SEEK_SET, 0) == -1)
1312 return got_error_from_errno("lseek");
1317 static const struct got_error *
1318 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1319 int outfd, int infd, struct got_repository *repo)
1321 const struct got_error *err;
1324 struct imsgbuf *ibuf;
1326 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1327 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1328 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1331 ibuf = calloc(1, sizeof(*ibuf));
1333 return got_error_from_errno("calloc");
1335 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1336 err = got_error_from_errno("socketpair");
1343 err = got_error_from_errno("fork");
1347 else if (pid == 0) {
1348 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1353 if (close(imsg_fds[1]) == -1) {
1354 err = got_error_from_errno("close");
1358 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1360 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1361 imsg_init(ibuf, imsg_fds[0]);
1362 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1364 return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1367 static const struct got_error *
1368 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1369 struct got_object_id *id, size_t blocksize)
1371 const struct got_error *err = NULL;
1372 struct got_packidx *packidx = NULL;
1374 char *path_packfile = NULL;
1377 size_t size, hdrlen;
1380 *blob = calloc(1, sizeof(**blob));
1382 return got_error_from_errno("calloc");
1384 outfd = got_opentempfd();
1386 return got_error_from_errno("got_opentempfd");
1388 (*blob)->read_buf = malloc(blocksize);
1389 if ((*blob)->read_buf == NULL) {
1390 err = got_error_from_errno("malloc");
1394 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1396 struct got_pack *pack = NULL;
1398 err = got_packidx_get_packfile_path(&path_packfile, packidx);
1402 pack = got_repo_get_cached_pack(repo, path_packfile);
1404 err = got_repo_cache_pack(&pack, repo, path_packfile,
1409 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1410 pack, packidx, idx, id);
1411 } else if (err->code == GOT_ERR_NO_OBJ) {
1414 err = open_loose_object(&infd, id, repo);
1417 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1423 if (hdrlen > size) {
1424 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1429 if (close(outfd) == -1 && err == NULL)
1430 err = got_error_from_errno("close");
1432 (*blob)->f = fmemopen(outbuf, size, "rb");
1433 if ((*blob)->f == NULL) {
1434 err = got_error_from_errno("fmemopen");
1438 (*blob)->data = outbuf;
1440 if (fstat(outfd, &sb) == -1) {
1441 err = got_error_from_errno("fstat");
1445 if (sb.st_size != size) {
1446 err = got_error(GOT_ERR_PRIVSEP_LEN);
1450 (*blob)->f = fdopen(outfd, "rb");
1451 if ((*blob)->f == NULL) {
1452 err = got_error_from_errno("fdopen");
1459 (*blob)->hdrlen = hdrlen;
1460 (*blob)->blocksize = blocksize;
1461 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1464 free(path_packfile);
1467 got_object_blob_close(*blob);
1469 } else if (outfd != -1)
1475 const struct got_error *
1476 got_object_open_as_blob(struct got_blob_object **blob,
1477 struct got_repository *repo, struct got_object_id *id,
1480 return open_blob(blob, repo, id, blocksize);
1483 const struct got_error *
1484 got_object_blob_open(struct got_blob_object **blob,
1485 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1487 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1490 const struct got_error *
1491 got_object_blob_close(struct got_blob_object *blob)
1493 const struct got_error *err = NULL;
1494 free(blob->read_buf);
1495 if (blob->f && fclose(blob->f) == EOF)
1496 err = got_error_from_errno("fclose");
1503 got_object_blob_rewind(struct got_blob_object *blob)
1510 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1512 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1516 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1518 return blob->hdrlen;
1522 got_object_blob_get_read_buf(struct got_blob_object *blob)
1524 return blob->read_buf;
1527 const struct got_error *
1528 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1532 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1533 if (n == 0 && ferror(blob->f))
1534 return got_ferror(blob->f, GOT_ERR_IO);
1539 const struct got_error *
1540 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1541 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1543 const struct got_error *err = NULL;
1544 size_t n, len, hdrlen;
1547 const int alloc_chunksz = 512;
1549 off_t off = 0, total_len = 0;
1552 *line_offsets = NULL;
1558 hdrlen = got_object_blob_get_hdrlen(blob);
1560 err = got_object_blob_read_block(&len, blob);
1565 buf = got_object_blob_get_read_buf(blob);
1568 if (line_offsets && *line_offsets == NULL) {
1569 /* Have some data but perhaps no '\n'. */
1571 nalloc = alloc_chunksz;
1572 *line_offsets = calloc(nalloc,
1573 sizeof(**line_offsets));
1574 if (*line_offsets == NULL)
1575 return got_error_from_errno("calloc");
1577 /* Skip forward over end of first line. */
1584 /* Scan '\n' offsets in remaining chunk of data. */
1586 if (buf[i] != '\n') {
1591 if (line_offsets && nalloc < *nlines) {
1592 size_t n = *nlines + alloc_chunksz;
1593 off_t *o = recallocarray(*line_offsets,
1594 nalloc, n, sizeof(**line_offsets));
1596 free(*line_offsets);
1597 *line_offsets = NULL;
1598 return got_error_from_errno(
1605 off = total_len + i - hdrlen + 1;
1606 (*line_offsets)[*nlines - 1] = off;
1611 /* Skip blob object header first time around. */
1612 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1613 if (n != len - hdrlen)
1614 return got_ferror(outfile, GOT_ERR_IO);
1615 total_len += len - hdrlen;
1619 if (fflush(outfile) != 0)
1620 return got_error_from_errno("fflush");
1624 *filesize = total_len;
1629 static const struct got_error *
1630 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1631 int pack_idx, struct got_object_id *id)
1633 const struct got_error *err = NULL;
1635 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1640 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1643 static const struct got_error *
1644 read_packed_tag_privsep(struct got_tag_object **tag,
1645 struct got_pack *pack, struct got_packidx *packidx, int idx,
1646 struct got_object_id *id)
1648 const struct got_error *err = NULL;
1650 if (pack->privsep_child)
1651 return request_packed_tag(tag, pack, idx, id);
1653 err = start_pack_privsep_child(pack, packidx);
1657 return request_packed_tag(tag, pack, idx, id);
1660 static const struct got_error *
1661 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1664 const struct got_error *err = NULL;
1665 struct imsgbuf *ibuf;
1667 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1669 err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1673 return got_privsep_recv_tag(tag, ibuf);
1676 static const struct got_error *
1677 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1678 struct got_repository *repo)
1680 const struct got_error *err;
1683 struct imsgbuf *ibuf;
1685 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1686 return request_tag(tag, repo, obj_fd);
1688 ibuf = calloc(1, sizeof(*ibuf));
1690 return got_error_from_errno("calloc");
1692 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1693 err = got_error_from_errno("socketpair");
1700 err = got_error_from_errno("fork");
1704 else if (pid == 0) {
1705 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1710 if (close(imsg_fds[1]) == -1) {
1711 err = got_error_from_errno("close");
1715 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1717 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1718 imsg_init(ibuf, imsg_fds[0]);
1719 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1721 return request_tag(tag, repo, obj_fd);
1724 static const struct got_error *
1725 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1726 struct got_object_id *id, int check_cache)
1728 const struct got_error *err = NULL;
1729 struct got_packidx *packidx = NULL;
1731 char *path_packfile = NULL;
1732 struct got_object *obj = NULL;
1733 int obj_type = GOT_OBJ_TYPE_ANY;
1736 *tag = got_repo_get_cached_tag(repo, id);
1744 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1746 struct got_pack *pack = NULL;
1748 err = got_packidx_get_packfile_path(&path_packfile, packidx);
1752 pack = got_repo_get_cached_pack(repo, path_packfile);
1754 err = got_repo_cache_pack(&pack, repo, path_packfile,
1760 /* Beware of "lightweight" tags: Check object type first. */
1761 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1765 obj_type = obj->type;
1766 got_object_close(obj);
1767 if (obj_type != GOT_OBJ_TYPE_TAG) {
1768 err = got_error(GOT_ERR_OBJ_TYPE);
1771 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1772 } else if (err->code == GOT_ERR_NO_OBJ) {
1775 err = open_loose_object(&fd, id, repo);
1778 err = read_object_header_privsep(&obj, repo, fd);
1781 obj_type = obj->type;
1782 got_object_close(obj);
1783 if (obj_type != GOT_OBJ_TYPE_TAG)
1784 return got_error(GOT_ERR_OBJ_TYPE);
1786 err = open_loose_object(&fd, id, repo);
1789 err = read_tag_privsep(tag, fd, repo);
1794 err = got_repo_cache_tag(repo, id, *tag);
1797 free(path_packfile);
1801 const struct got_error *
1802 got_object_open_as_tag(struct got_tag_object **tag,
1803 struct got_repository *repo, struct got_object_id *id)
1805 *tag = got_repo_get_cached_tag(repo, id);
1811 return open_tag(tag, repo, id, 0);
1814 const struct got_error *
1815 got_object_tag_open(struct got_tag_object **tag,
1816 struct got_repository *repo, struct got_object *obj)
1818 return open_tag(tag, repo, got_object_get_id(obj), 1);
1822 got_object_tag_get_name(struct got_tag_object *tag)
1828 got_object_tag_get_object_type(struct got_tag_object *tag)
1830 return tag->obj_type;
1833 struct got_object_id *
1834 got_object_tag_get_object_id(struct got_tag_object *tag)
1840 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1842 return tag->tagger_time;
1846 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1848 return tag->tagger_gmtoff;
1852 got_object_tag_get_tagger(struct got_tag_object *tag)
1858 got_object_tag_get_message(struct got_tag_object *tag)
1863 static struct got_tree_entry *
1864 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1868 /* Note that tree entries are sorted in strncmp() order. */
1869 for (i = 0; i < tree->nentries; i++) {
1870 struct got_tree_entry *te = &tree->entries[i];
1871 int cmp = strncmp(te->name, name, len);
1876 if (te->name[len] == '\0')
1882 struct got_tree_entry *
1883 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1885 return find_entry_by_name(tree, name, strlen(name));
1888 const struct got_error *
1889 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1890 struct got_object_id *commit_id, const char *path)
1892 const struct got_error *err = NULL;
1893 struct got_commit_object *commit = NULL;
1894 struct got_tree_object *tree = NULL;
1895 struct got_tree_entry *te = NULL;
1896 const char *seg, *s;
1901 err = got_object_open_as_commit(&commit, repo, commit_id);
1905 /* Handle opening of root of commit's tree. */
1906 if (got_path_is_root_dir(path)) {
1907 *id = got_object_id_dup(commit->tree_id);
1909 err = got_error_from_errno("got_object_id_dup");
1913 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1923 struct got_tree_object *next_tree;
1932 te = find_entry_by_name(tree, seg, seglen);
1934 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1945 err = got_object_open_as_tree(&next_tree, repo,
1950 got_object_tree_close(tree);
1956 *id = got_object_id_dup(&te->id);
1958 return got_error_from_errno("got_object_id_dup");
1960 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1963 got_object_commit_close(commit);
1965 got_object_tree_close(tree);
1970 * Normalize file mode bits to avoid false positive tree entry differences
1971 * in case tree entries have unexpected mode bits set.
1974 normalize_mode_for_comparison(mode_t mode)
1977 * For directories, the only relevant bit is the IFDIR bit.
1978 * This allows us to detect paths changing from a directory
1979 * to a file and vice versa.
1982 return mode & S_IFDIR;
1985 * For symlinks, the only relevant bit is the IFLNK bit.
1986 * This allows us to detect paths changing from a symlinks
1987 * to a file or directory and vice versa.
1990 return mode & S_IFLNK;
1992 /* For files, the only change we care about is the executable bit. */
1993 return mode & S_IXUSR;
1996 const struct got_error *
1997 got_object_tree_path_changed(int *changed,
1998 struct got_tree_object *tree01, struct got_tree_object *tree02,
1999 const char *path, struct got_repository *repo)
2001 const struct got_error *err = NULL;
2002 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2003 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2004 const char *seg, *s;
2009 /* We not do support comparing the root path. */
2010 if (got_path_is_root_dir(path))
2011 return got_error_path(path, GOT_ERR_BAD_PATH);
2021 struct got_tree_object *next_tree1, *next_tree2;
2022 mode_t mode1, mode2;
2031 te1 = find_entry_by_name(tree1, seg, seglen);
2033 err = got_error(GOT_ERR_NO_OBJ);
2038 te2 = find_entry_by_name(tree2, seg, seglen);
2041 mode1 = normalize_mode_for_comparison(te1->mode);
2042 mode2 = normalize_mode_for_comparison(te2->mode);
2043 if (mode1 != mode2) {
2048 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2054 if (*s == '\0') { /* final path element */
2063 err = got_object_open_as_tree(&next_tree1, repo,
2068 if (tree1 != tree01)
2069 got_object_tree_close(tree1);
2073 err = got_object_open_as_tree(&next_tree2, repo,
2078 if (tree2 != tree02)
2079 got_object_tree_close(tree2);
2082 if (tree2 != tree02)
2083 got_object_tree_close(tree2);
2089 if (tree1 && tree1 != tree01)
2090 got_object_tree_close(tree1);
2091 if (tree2 && tree2 != tree02)
2092 got_object_tree_close(tree2);
2096 const struct got_error *
2097 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2098 struct got_tree_entry *te)
2100 const struct got_error *err = NULL;
2102 *new_te = calloc(1, sizeof(**new_te));
2103 if (*new_te == NULL)
2104 return got_error_from_errno("calloc");
2106 (*new_te)->mode = te->mode;
2107 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2108 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2113 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2115 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2119 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2121 /* S_IFDIR check avoids confusing symlinks with submodules. */
2122 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2125 static const struct got_error *
2126 resolve_symlink(char **link_target, const char *path,
2127 struct got_object_id *commit_id, struct got_repository *repo)
2129 const struct got_error *err = NULL;
2131 char *name, *parent_path = NULL;
2132 struct got_object_id *tree_obj_id = NULL;
2133 struct got_tree_object *tree = NULL;
2134 struct got_tree_entry *te = NULL;
2136 *link_target = NULL;
2138 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2139 return got_error(GOT_ERR_NO_SPACE);
2141 name = basename(buf);
2143 return got_error_from_errno2("basename", path);
2145 err = got_path_dirname(&parent_path, path);
2149 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2152 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2153 /* Display the complete path in error message. */
2154 err = got_error_path(path, err->code);
2159 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2163 te = got_object_tree_find_entry(tree, name);
2165 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2169 if (got_object_tree_entry_is_symlink(te)) {
2170 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2173 if (!got_path_is_absolute(*link_target)) {
2175 if (asprintf(&abspath, "%s/%s", parent_path,
2176 *link_target) == -1) {
2177 err = got_error_from_errno("asprintf");
2181 *link_target = malloc(PATH_MAX);
2182 if (*link_target == NULL) {
2183 err = got_error_from_errno("malloc");
2186 err = got_canonpath(abspath, *link_target, PATH_MAX);
2195 got_object_tree_close(tree);
2198 *link_target = NULL;
2203 const struct got_error *
2204 got_object_resolve_symlinks(char **link_target, const char *path,
2205 struct got_object_id *commit_id, struct got_repository *repo)
2207 const struct got_error *err = NULL;
2208 char *next_target = NULL;
2209 int max_recursion = 40; /* matches Git */
2211 *link_target = NULL;
2214 err = resolve_symlink(&next_target,
2215 *link_target ? *link_target : path, commit_id, repo);
2220 if (--max_recursion == 0) {
2221 err = got_error_path(path, GOT_ERR_RECURSION);
2222 *link_target = NULL;
2225 *link_target = next_target;
2227 } while (next_target);
2232 const struct got_error *
2233 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2234 struct got_object_id *commit_id, const char *path,
2235 struct got_repository *repo)
2237 const struct got_error *err = NULL;
2238 struct got_pack *pack = NULL;
2239 struct got_packidx *packidx = NULL;
2240 char *path_packfile = NULL;
2241 struct got_commit_object *changed_commit = NULL;
2242 struct got_object_id *changed_commit_id = NULL;
2245 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2247 if (err->code != GOT_ERR_NO_OBJ)
2252 err = got_packidx_get_packfile_path(&path_packfile, packidx);
2256 pack = got_repo_get_cached_pack(repo, path_packfile);
2258 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2263 if (pack->privsep_child == NULL) {
2264 err = start_pack_privsep_child(pack, packidx);
2269 err = got_privsep_send_commit_traversal_request(
2270 pack->privsep_child->ibuf, commit_id, idx, path);
2274 err = got_privsep_recv_traversed_commits(&changed_commit,
2275 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2279 if (changed_commit) {
2281 * Cache the commit in which the path was changed.
2282 * This commit might be opened again soon.
2284 changed_commit->refcnt++;
2285 err = got_repo_cache_commit(repo, changed_commit_id,
2287 got_object_commit_close(changed_commit);
2290 free(path_packfile);
2291 free(changed_commit_id);