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 const struct got_error *
129 got_object_open_loose_fd(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 const struct got_error *
336 got_object_open_packed(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,
350 packidx->path_packidx);
354 pack = got_repo_get_cached_pack(repo, path_packfile);
356 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
361 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
369 static const struct got_error *
370 request_object(struct got_object **obj, struct got_object_id *id,
371 struct got_repository *repo, int fd)
373 const struct got_error *err = NULL;
374 struct imsgbuf *ibuf;
376 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
378 err = got_privsep_send_obj_req(ibuf, fd, id);
382 return got_privsep_recv_obj(obj, ibuf);
385 static const struct got_error *
386 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
387 struct got_object_id *id, struct got_repository *repo, int infd)
389 const struct got_error *err = NULL;
390 struct imsgbuf *ibuf;
393 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
395 outfd_child = dup(outfd);
396 if (outfd_child == -1)
397 return got_error_from_errno("dup");
399 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
403 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
407 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
410 static const struct got_error *
411 start_read_object_child(struct got_repository *repo)
413 const struct got_error *err = NULL;
416 struct imsgbuf *ibuf;
418 ibuf = calloc(1, sizeof(*ibuf));
420 return got_error_from_errno("calloc");
422 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
423 err = got_error_from_errno("socketpair");
430 err = got_error_from_errno("fork");
435 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
440 if (close(imsg_fds[1]) == -1) {
441 err = got_error_from_errno("close");
446 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
448 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
449 imsg_init(ibuf, imsg_fds[0]);
450 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
455 const struct got_error *
456 got_object_read_header_privsep(struct got_object **obj,
457 struct got_object_id *id, struct got_repository *repo, int obj_fd)
459 const struct got_error *err;
461 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
462 return request_object(obj, id, repo, obj_fd);
464 err = start_read_object_child(repo);
470 return request_object(obj, id, repo, obj_fd);
473 static const struct got_error *
474 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
475 int outfd, struct got_object_id *id, struct got_repository *repo,
478 const struct got_error *err;
480 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
481 return request_raw_object(outbuf, size, hdrlen, outfd, id,
484 err = start_read_object_child(repo);
488 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
492 const struct got_error *
493 got_object_open(struct got_object **obj, struct got_repository *repo,
494 struct got_object_id *id)
496 const struct got_error *err = NULL;
499 *obj = got_repo_get_cached_object(repo, id);
505 err = got_object_open_packed(obj, id, repo);
506 if (err && err->code != GOT_ERR_NO_OBJ)
510 return got_repo_cache_object(repo, id, *obj);
513 err = got_object_open_loose_fd(&fd, id, repo);
515 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
516 err = got_error_no_obj(id);
520 err = got_object_read_header_privsep(obj, id, repo, fd);
524 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
527 return got_repo_cache_object(repo, id, *obj);
530 const struct got_error *
531 got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo,
532 struct got_object_id *id, size_t blocksize)
534 const struct got_error *err = NULL;
535 struct got_packidx *packidx = NULL;
537 uint8_t *outbuf = NULL;
541 char *path_packfile = NULL;
545 outfd = got_opentempfd();
547 return got_error_from_errno("got_opentempfd");
549 err = got_repo_search_packidx(&packidx, &idx, repo, id);
551 struct got_pack *pack = NULL;
553 err = got_packidx_get_packfile_path(&path_packfile,
554 packidx->path_packidx);
558 pack = got_repo_get_cached_pack(repo, path_packfile);
560 err = got_repo_cache_pack(&pack, repo, path_packfile,
565 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
566 outfd, pack, packidx, idx, id);
567 } else if (err->code == GOT_ERR_NO_OBJ) {
570 err = got_object_open_loose_fd(&fd, id, repo);
573 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
577 *obj = calloc(1, sizeof(**obj));
579 err = got_error_from_errno("calloc");
583 (*obj)->read_buf = malloc(blocksize);
584 if ((*obj)->read_buf == NULL) {
585 err = got_error_from_errno("malloc");
590 if (close(outfd) == -1) {
591 err = got_error_from_errno("close");
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;
628 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_raw_close(struct got_raw_object *obj)
672 const struct got_error *err = NULL;
675 if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
676 err = got_error_from_errno("fclose");
682 const struct got_error *
683 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
686 struct got_object_id id;
688 if (!got_parse_sha1_digest(id.sha1, id_str))
689 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
691 return got_object_open(obj, repo, &id);
694 const struct got_error *
695 got_object_resolve_id_str(struct got_object_id **id,
696 struct got_repository *repo, const char *id_str)
698 const struct got_error *err = NULL;
699 struct got_object *obj;
701 err = got_object_open_by_id_str(&obj, repo, id_str);
705 *id = got_object_id_dup(got_object_get_id(obj));
706 got_object_close(obj);
708 return got_error_from_errno("got_object_id_dup");
713 static const struct got_error *
714 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
715 int pack_idx, struct got_object_id *id)
717 const struct got_error *err = NULL;
719 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
724 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
728 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
732 static const struct got_error *
733 read_packed_commit_privsep(struct got_commit_object **commit,
734 struct got_pack *pack, struct got_packidx *packidx, int idx,
735 struct got_object_id *id)
737 const struct got_error *err = NULL;
739 if (pack->privsep_child)
740 return request_packed_commit(commit, pack, idx, id);
742 err = start_pack_privsep_child(pack, packidx);
746 return request_packed_commit(commit, pack, idx, id);
749 static const struct got_error *
750 request_commit(struct got_commit_object **commit, struct got_repository *repo,
751 int fd, struct got_object_id *id)
753 const struct got_error *err = NULL;
754 struct imsgbuf *ibuf;
756 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
758 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
762 return got_privsep_recv_commit(commit, ibuf);
765 static const struct got_error *
766 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
767 struct got_object_id *id, struct got_repository *repo)
769 const struct got_error *err;
772 struct imsgbuf *ibuf;
774 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
775 return request_commit(commit, repo, obj_fd, id);
777 ibuf = calloc(1, sizeof(*ibuf));
779 return got_error_from_errno("calloc");
781 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
782 err = got_error_from_errno("socketpair");
789 err = got_error_from_errno("fork");
794 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
799 if (close(imsg_fds[1]) == -1) {
800 err = got_error_from_errno("close");
804 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
806 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
807 imsg_init(ibuf, imsg_fds[0]);
808 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
810 return request_commit(commit, repo, obj_fd, id);
814 static const struct got_error *
815 open_commit(struct got_commit_object **commit,
816 struct got_repository *repo, struct got_object_id *id, int check_cache)
818 const struct got_error *err = NULL;
819 struct got_packidx *packidx = NULL;
821 char *path_packfile = NULL;
824 *commit = got_repo_get_cached_commit(repo, id);
825 if (*commit != NULL) {
832 err = got_repo_search_packidx(&packidx, &idx, repo, id);
834 struct got_pack *pack = NULL;
836 err = got_packidx_get_packfile_path(&path_packfile,
837 packidx->path_packidx);
841 pack = got_repo_get_cached_pack(repo, path_packfile);
843 err = got_repo_cache_pack(&pack, repo, path_packfile,
848 err = read_packed_commit_privsep(commit, pack,
850 } else if (err->code == GOT_ERR_NO_OBJ) {
853 err = got_object_open_loose_fd(&fd, id, repo);
856 err = read_commit_privsep(commit, fd, id, repo);
861 err = got_repo_cache_commit(repo, id, *commit);
868 const struct got_error *
869 got_object_open_as_commit(struct got_commit_object **commit,
870 struct got_repository *repo, struct got_object_id *id)
872 *commit = got_repo_get_cached_commit(repo, id);
873 if (*commit != NULL) {
878 return open_commit(commit, repo, id, 0);
881 const struct got_error *
882 got_object_commit_open(struct got_commit_object **commit,
883 struct got_repository *repo, struct got_object *obj)
885 return open_commit(commit, repo, got_object_get_id(obj), 1);
888 const struct got_error *
889 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
891 const struct got_error *err = NULL;
893 *qid = calloc(1, sizeof(**qid));
895 return got_error_from_errno("calloc");
897 (*qid)->id = got_object_id_dup(id);
898 if ((*qid)->id == NULL) {
899 err = got_error_from_errno("got_object_id_dup");
900 got_object_qid_free(*qid);
908 const struct got_error *
909 got_object_id_queue_copy(const struct got_object_id_queue *src,
910 struct got_object_id_queue *dest)
912 const struct got_error *err;
913 struct got_object_qid *qid;
915 STAILQ_FOREACH(qid, src, entry) {
916 struct got_object_qid *new;
918 * Deep-copy the object ID only. Let the caller deal
919 * with setting up the new->data pointer if needed.
921 err = got_object_qid_alloc(&new, qid->id);
923 got_object_id_queue_free(dest);
926 STAILQ_INSERT_TAIL(dest, new, entry);
932 static const struct got_error *
933 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
934 int pack_idx, struct got_object_id *id)
936 const struct got_error *err = NULL;
938 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
943 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
946 static const struct got_error *
947 read_packed_tree_privsep(struct got_tree_object **tree,
948 struct got_pack *pack, struct got_packidx *packidx, int idx,
949 struct got_object_id *id)
951 const struct got_error *err = NULL;
953 if (pack->privsep_child)
954 return request_packed_tree(tree, pack, idx, id);
956 err = start_pack_privsep_child(pack, packidx);
960 return request_packed_tree(tree, pack, idx, id);
963 static const struct got_error *
964 request_tree(struct got_tree_object **tree, struct got_repository *repo,
965 int fd, struct got_object_id *id)
967 const struct got_error *err = NULL;
968 struct imsgbuf *ibuf;
970 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
972 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
976 return got_privsep_recv_tree(tree, ibuf);
979 const struct got_error *
980 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
981 struct got_object_id *id, struct got_repository *repo)
983 const struct got_error *err;
986 struct imsgbuf *ibuf;
988 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
989 return request_tree(tree, repo, obj_fd, id);
991 ibuf = calloc(1, sizeof(*ibuf));
993 return got_error_from_errno("calloc");
995 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
996 err = got_error_from_errno("socketpair");
1003 err = got_error_from_errno("fork");
1007 else if (pid == 0) {
1008 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1013 if (close(imsg_fds[1]) == -1) {
1014 err = got_error_from_errno("close");
1018 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1020 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1021 imsg_init(ibuf, imsg_fds[0]);
1022 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1025 return request_tree(tree, repo, obj_fd, id);
1028 static const struct got_error *
1029 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1030 struct got_object_id *id, int check_cache)
1032 const struct got_error *err = NULL;
1033 struct got_packidx *packidx = NULL;
1035 char *path_packfile = NULL;
1038 *tree = got_repo_get_cached_tree(repo, id);
1039 if (*tree != NULL) {
1046 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1048 struct got_pack *pack = NULL;
1050 err = got_packidx_get_packfile_path(&path_packfile,
1051 packidx->path_packidx);
1055 pack = got_repo_get_cached_pack(repo, path_packfile);
1057 err = got_repo_cache_pack(&pack, repo, path_packfile,
1062 err = read_packed_tree_privsep(tree, pack,
1064 } else if (err->code == GOT_ERR_NO_OBJ) {
1067 err = got_object_open_loose_fd(&fd, id, repo);
1070 err = read_tree_privsep(tree, fd, id, repo);
1075 err = got_repo_cache_tree(repo, id, *tree);
1078 free(path_packfile);
1082 const struct got_error *
1083 got_object_open_as_tree(struct got_tree_object **tree,
1084 struct got_repository *repo, struct got_object_id *id)
1086 *tree = got_repo_get_cached_tree(repo, id);
1087 if (*tree != NULL) {
1092 return open_tree(tree, repo, id, 0);
1095 const struct got_error *
1096 got_object_tree_open(struct got_tree_object **tree,
1097 struct got_repository *repo, struct got_object *obj)
1099 return open_tree(tree, repo, got_object_get_id(obj), 1);
1103 got_object_tree_get_nentries(struct got_tree_object *tree)
1105 return tree->nentries;
1108 struct got_tree_entry *
1109 got_object_tree_get_first_entry(struct got_tree_object *tree)
1111 return got_object_tree_get_entry(tree, 0);
1114 struct got_tree_entry *
1115 got_object_tree_get_last_entry(struct got_tree_object *tree)
1117 return got_object_tree_get_entry(tree, tree->nentries - 1);
1120 struct got_tree_entry *
1121 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1123 if (i < 0 || i >= tree->nentries)
1125 return &tree->entries[i];
1129 got_tree_entry_get_mode(struct got_tree_entry *te)
1135 got_tree_entry_get_name(struct got_tree_entry *te)
1137 return &te->name[0];
1140 struct got_object_id *
1141 got_tree_entry_get_id(struct got_tree_entry *te)
1146 const struct got_error *
1147 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1149 const struct got_error *err = NULL;
1150 size_t len, totlen, hdrlen, offset;
1154 hdrlen = got_object_blob_get_hdrlen(blob);
1160 err = got_object_blob_read_block(&len, blob);
1167 totlen += len - hdrlen;
1168 p = realloc(*s, totlen + 1);
1170 err = got_error_from_errno("realloc");
1176 /* Skip blob object header first time around. */
1178 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1183 (*s)[totlen] = '\0';
1187 const struct got_error *
1188 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1189 struct got_repository *repo)
1191 const struct got_error *err = NULL;
1192 struct got_blob_object *blob = NULL;
1194 *link_target = NULL;
1196 if (!got_object_tree_entry_is_symlink(te))
1197 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1199 err = got_object_open_as_blob(&blob, repo,
1200 got_tree_entry_get_id(te), PATH_MAX);
1204 err = got_object_blob_read_to_str(link_target, blob);
1205 got_object_blob_close(blob);
1208 *link_target = NULL;
1214 got_tree_entry_get_index(struct got_tree_entry *te)
1219 struct got_tree_entry *
1220 got_tree_entry_get_next(struct got_tree_object *tree,
1221 struct got_tree_entry *te)
1223 return got_object_tree_get_entry(tree, te->idx + 1);
1226 struct got_tree_entry *
1227 got_tree_entry_get_prev(struct got_tree_object *tree,
1228 struct got_tree_entry *te)
1230 return got_object_tree_get_entry(tree, te->idx - 1);
1233 static const struct got_error *
1234 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1235 struct got_pack *pack, struct got_packidx *packidx, int idx,
1236 struct got_object_id *id)
1238 const struct got_error *err = NULL;
1240 int basefd, accumfd; /* temporary files for delta application */
1242 basefd = got_opentempfd();
1244 return got_error_from_errno("got_opentempfd");
1245 accumfd = got_opentempfd();
1247 return got_error_from_errno("got_opentempfd");
1249 outfd_child = dup(outfd);
1250 if (outfd_child == -1)
1251 return got_error_from_errno("dup");
1253 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1257 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1265 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1272 err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1277 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1278 pack->privsep_child->ibuf);
1282 if (lseek(outfd, SEEK_SET, 0) == -1)
1283 err = got_error_from_errno("lseek");
1288 static const struct got_error *
1289 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1290 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1291 struct got_object_id *id)
1293 const struct got_error *err = NULL;
1295 if (pack->privsep_child == NULL) {
1296 err = start_pack_privsep_child(pack, packidx);
1301 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1305 static const struct got_error *
1306 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1307 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1309 const struct got_error *err = NULL;
1312 outfd_child = dup(outfd);
1313 if (outfd_child == -1)
1314 return got_error_from_errno("dup");
1316 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1320 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1324 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1328 if (lseek(outfd, SEEK_SET, 0) == -1)
1329 return got_error_from_errno("lseek");
1334 static const struct got_error *
1335 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1336 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1338 const struct got_error *err;
1341 struct imsgbuf *ibuf;
1343 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1344 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1345 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1349 ibuf = calloc(1, sizeof(*ibuf));
1351 return got_error_from_errno("calloc");
1353 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1354 err = got_error_from_errno("socketpair");
1361 err = got_error_from_errno("fork");
1365 else if (pid == 0) {
1366 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1371 if (close(imsg_fds[1]) == -1) {
1372 err = got_error_from_errno("close");
1376 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1378 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1379 imsg_init(ibuf, imsg_fds[0]);
1380 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1382 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1385 static const struct got_error *
1386 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1387 struct got_object_id *id, size_t blocksize)
1389 const struct got_error *err = NULL;
1390 struct got_packidx *packidx = NULL;
1392 char *path_packfile = NULL;
1395 size_t size, hdrlen;
1398 *blob = calloc(1, sizeof(**blob));
1400 return got_error_from_errno("calloc");
1402 outfd = got_opentempfd();
1404 return got_error_from_errno("got_opentempfd");
1406 (*blob)->read_buf = malloc(blocksize);
1407 if ((*blob)->read_buf == NULL) {
1408 err = got_error_from_errno("malloc");
1412 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1414 struct got_pack *pack = NULL;
1416 err = got_packidx_get_packfile_path(&path_packfile,
1417 packidx->path_packidx);
1421 pack = got_repo_get_cached_pack(repo, path_packfile);
1423 err = got_repo_cache_pack(&pack, repo, path_packfile,
1428 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1429 pack, packidx, idx, id);
1430 } else if (err->code == GOT_ERR_NO_OBJ) {
1433 err = got_object_open_loose_fd(&infd, id, repo);
1436 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1442 if (hdrlen > size) {
1443 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1448 if (close(outfd) == -1 && err == NULL)
1449 err = got_error_from_errno("close");
1451 (*blob)->f = fmemopen(outbuf, size, "rb");
1452 if ((*blob)->f == NULL) {
1453 err = got_error_from_errno("fmemopen");
1457 (*blob)->data = outbuf;
1459 if (fstat(outfd, &sb) == -1) {
1460 err = got_error_from_errno("fstat");
1464 if (sb.st_size != size) {
1465 err = got_error(GOT_ERR_PRIVSEP_LEN);
1469 (*blob)->f = fdopen(outfd, "rb");
1470 if ((*blob)->f == NULL) {
1471 err = got_error_from_errno("fdopen");
1478 (*blob)->hdrlen = hdrlen;
1479 (*blob)->blocksize = blocksize;
1480 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1483 free(path_packfile);
1486 got_object_blob_close(*blob);
1488 } else if (outfd != -1)
1494 const struct got_error *
1495 got_object_open_as_blob(struct got_blob_object **blob,
1496 struct got_repository *repo, struct got_object_id *id,
1499 return open_blob(blob, repo, id, blocksize);
1502 const struct got_error *
1503 got_object_blob_open(struct got_blob_object **blob,
1504 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1506 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1509 const struct got_error *
1510 got_object_blob_close(struct got_blob_object *blob)
1512 const struct got_error *err = NULL;
1513 free(blob->read_buf);
1514 if (blob->f && fclose(blob->f) == EOF)
1515 err = got_error_from_errno("fclose");
1522 got_object_blob_rewind(struct got_blob_object *blob)
1529 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1531 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1535 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1537 return blob->hdrlen;
1541 got_object_blob_get_read_buf(struct got_blob_object *blob)
1543 return blob->read_buf;
1546 const struct got_error *
1547 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1551 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1552 if (n == 0 && ferror(blob->f))
1553 return got_ferror(blob->f, GOT_ERR_IO);
1558 const struct got_error *
1559 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1560 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1562 const struct got_error *err = NULL;
1563 size_t n, len, hdrlen;
1566 const int alloc_chunksz = 512;
1568 off_t off = 0, total_len = 0;
1571 *line_offsets = NULL;
1577 hdrlen = got_object_blob_get_hdrlen(blob);
1579 err = got_object_blob_read_block(&len, blob);
1584 buf = got_object_blob_get_read_buf(blob);
1587 if (line_offsets && *line_offsets == NULL) {
1588 /* Have some data but perhaps no '\n'. */
1590 nalloc = alloc_chunksz;
1591 *line_offsets = calloc(nalloc,
1592 sizeof(**line_offsets));
1593 if (*line_offsets == NULL)
1594 return got_error_from_errno("calloc");
1596 /* Skip forward over end of first line. */
1603 /* Scan '\n' offsets in remaining chunk of data. */
1605 if (buf[i] != '\n') {
1610 if (line_offsets && nalloc < *nlines) {
1611 size_t n = *nlines + alloc_chunksz;
1612 off_t *o = recallocarray(*line_offsets,
1613 nalloc, n, sizeof(**line_offsets));
1615 free(*line_offsets);
1616 *line_offsets = NULL;
1617 return got_error_from_errno(
1624 off = total_len + i - hdrlen + 1;
1625 (*line_offsets)[*nlines - 1] = off;
1630 /* Skip blob object header first time around. */
1631 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1632 if (n != len - hdrlen)
1633 return got_ferror(outfile, GOT_ERR_IO);
1634 total_len += len - hdrlen;
1638 if (fflush(outfile) != 0)
1639 return got_error_from_errno("fflush");
1643 *filesize = total_len;
1648 static const struct got_error *
1649 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1650 int pack_idx, struct got_object_id *id)
1652 const struct got_error *err = NULL;
1654 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1659 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1662 static const struct got_error *
1663 read_packed_tag_privsep(struct got_tag_object **tag,
1664 struct got_pack *pack, struct got_packidx *packidx, int idx,
1665 struct got_object_id *id)
1667 const struct got_error *err = NULL;
1669 if (pack->privsep_child)
1670 return request_packed_tag(tag, pack, idx, id);
1672 err = start_pack_privsep_child(pack, packidx);
1676 return request_packed_tag(tag, pack, idx, id);
1679 static const struct got_error *
1680 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1681 int fd, struct got_object_id *id)
1683 const struct got_error *err = NULL;
1684 struct imsgbuf *ibuf;
1686 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1688 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1692 return got_privsep_recv_tag(tag, ibuf);
1695 static const struct got_error *
1696 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1697 struct got_object_id *id, struct got_repository *repo)
1699 const struct got_error *err;
1702 struct imsgbuf *ibuf;
1704 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1705 return request_tag(tag, repo, obj_fd, id);
1707 ibuf = calloc(1, sizeof(*ibuf));
1709 return got_error_from_errno("calloc");
1711 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1712 err = got_error_from_errno("socketpair");
1719 err = got_error_from_errno("fork");
1723 else if (pid == 0) {
1724 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1729 if (close(imsg_fds[1]) == -1) {
1730 err = got_error_from_errno("close");
1734 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1736 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1737 imsg_init(ibuf, imsg_fds[0]);
1738 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1740 return request_tag(tag, repo, obj_fd, id);
1743 static const struct got_error *
1744 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1745 struct got_object_id *id, int check_cache)
1747 const struct got_error *err = NULL;
1748 struct got_packidx *packidx = NULL;
1750 char *path_packfile = NULL;
1751 struct got_object *obj = NULL;
1752 int obj_type = GOT_OBJ_TYPE_ANY;
1755 *tag = got_repo_get_cached_tag(repo, id);
1763 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1765 struct got_pack *pack = NULL;
1767 err = got_packidx_get_packfile_path(&path_packfile,
1768 packidx->path_packidx);
1772 pack = got_repo_get_cached_pack(repo, path_packfile);
1774 err = got_repo_cache_pack(&pack, repo, path_packfile,
1780 /* Beware of "lightweight" tags: Check object type first. */
1781 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1785 obj_type = obj->type;
1786 got_object_close(obj);
1787 if (obj_type != GOT_OBJ_TYPE_TAG) {
1788 err = got_error(GOT_ERR_OBJ_TYPE);
1791 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1792 } else if (err->code == GOT_ERR_NO_OBJ) {
1795 err = got_object_open_loose_fd(&fd, id, repo);
1798 err = got_object_read_header_privsep(&obj, id, repo, fd);
1801 obj_type = obj->type;
1802 got_object_close(obj);
1803 if (obj_type != GOT_OBJ_TYPE_TAG)
1804 return got_error(GOT_ERR_OBJ_TYPE);
1806 err = got_object_open_loose_fd(&fd, id, repo);
1809 err = read_tag_privsep(tag, fd, id, repo);
1814 err = got_repo_cache_tag(repo, id, *tag);
1817 free(path_packfile);
1821 const struct got_error *
1822 got_object_open_as_tag(struct got_tag_object **tag,
1823 struct got_repository *repo, struct got_object_id *id)
1825 *tag = got_repo_get_cached_tag(repo, id);
1831 return open_tag(tag, repo, id, 0);
1834 const struct got_error *
1835 got_object_tag_open(struct got_tag_object **tag,
1836 struct got_repository *repo, struct got_object *obj)
1838 return open_tag(tag, repo, got_object_get_id(obj), 1);
1842 got_object_tag_get_name(struct got_tag_object *tag)
1848 got_object_tag_get_object_type(struct got_tag_object *tag)
1850 return tag->obj_type;
1853 struct got_object_id *
1854 got_object_tag_get_object_id(struct got_tag_object *tag)
1860 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1862 return tag->tagger_time;
1866 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1868 return tag->tagger_gmtoff;
1872 got_object_tag_get_tagger(struct got_tag_object *tag)
1878 got_object_tag_get_message(struct got_tag_object *tag)
1883 static struct got_tree_entry *
1884 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1888 /* Note that tree entries are sorted in strncmp() order. */
1889 for (i = 0; i < tree->nentries; i++) {
1890 struct got_tree_entry *te = &tree->entries[i];
1891 int cmp = strncmp(te->name, name, len);
1896 if (te->name[len] == '\0')
1902 struct got_tree_entry *
1903 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1905 return find_entry_by_name(tree, name, strlen(name));
1908 const struct got_error *
1909 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1910 struct got_repository *repo, struct got_tree_object *tree,
1913 const struct got_error *err = NULL;
1914 struct got_tree_object *subtree = NULL;
1915 struct got_tree_entry *te = NULL;
1916 const char *seg, *s;
1928 struct got_tree_object *next_tree;
1937 te = find_entry_by_name(subtree, seg, seglen);
1939 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1950 err = got_object_open_as_tree(&next_tree, repo,
1955 if (subtree != tree)
1956 got_object_tree_close(subtree);
1957 subtree = next_tree;
1962 *id = got_object_id_dup(&te->id);
1964 return got_error_from_errno("got_object_id_dup");
1968 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1970 if (subtree && subtree != tree)
1971 got_object_tree_close(subtree);
1974 const struct got_error *
1975 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1976 struct got_object_id *commit_id, const char *path)
1978 const struct got_error *err = NULL;
1979 struct got_commit_object *commit = NULL;
1980 struct got_tree_object *tree = NULL;
1984 err = got_object_open_as_commit(&commit, repo, commit_id);
1988 /* Handle opening of root of commit's tree. */
1989 if (got_path_is_root_dir(path)) {
1990 *id = got_object_id_dup(commit->tree_id);
1992 err = got_error_from_errno("got_object_id_dup");
1994 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1997 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2001 got_object_commit_close(commit);
2003 got_object_tree_close(tree);
2008 * Normalize file mode bits to avoid false positive tree entry differences
2009 * in case tree entries have unexpected mode bits set.
2012 normalize_mode_for_comparison(mode_t mode)
2015 * For directories, the only relevant bit is the IFDIR bit.
2016 * This allows us to detect paths changing from a directory
2017 * to a file and vice versa.
2020 return mode & S_IFDIR;
2023 * For symlinks, the only relevant bit is the IFLNK bit.
2024 * This allows us to detect paths changing from a symlinks
2025 * to a file or directory and vice versa.
2028 return mode & S_IFLNK;
2030 /* For files, the only change we care about is the executable bit. */
2031 return mode & S_IXUSR;
2034 const struct got_error *
2035 got_object_tree_path_changed(int *changed,
2036 struct got_tree_object *tree01, struct got_tree_object *tree02,
2037 const char *path, struct got_repository *repo)
2039 const struct got_error *err = NULL;
2040 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2041 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2042 const char *seg, *s;
2047 /* We not do support comparing the root path. */
2048 if (got_path_is_root_dir(path))
2049 return got_error_path(path, GOT_ERR_BAD_PATH);
2059 struct got_tree_object *next_tree1, *next_tree2;
2060 mode_t mode1, mode2;
2069 te1 = find_entry_by_name(tree1, seg, seglen);
2071 err = got_error(GOT_ERR_NO_OBJ);
2076 te2 = find_entry_by_name(tree2, seg, seglen);
2079 mode1 = normalize_mode_for_comparison(te1->mode);
2080 mode2 = normalize_mode_for_comparison(te2->mode);
2081 if (mode1 != mode2) {
2086 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2092 if (*s == '\0') { /* final path element */
2101 err = got_object_open_as_tree(&next_tree1, repo,
2106 if (tree1 != tree01)
2107 got_object_tree_close(tree1);
2111 err = got_object_open_as_tree(&next_tree2, repo,
2116 if (tree2 != tree02)
2117 got_object_tree_close(tree2);
2120 if (tree2 != tree02)
2121 got_object_tree_close(tree2);
2127 if (tree1 && tree1 != tree01)
2128 got_object_tree_close(tree1);
2129 if (tree2 && tree2 != tree02)
2130 got_object_tree_close(tree2);
2134 const struct got_error *
2135 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2136 struct got_tree_entry *te)
2138 const struct got_error *err = NULL;
2140 *new_te = calloc(1, sizeof(**new_te));
2141 if (*new_te == NULL)
2142 return got_error_from_errno("calloc");
2144 (*new_te)->mode = te->mode;
2145 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2146 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2151 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2153 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2157 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2159 /* S_IFDIR check avoids confusing symlinks with submodules. */
2160 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2163 static const struct got_error *
2164 resolve_symlink(char **link_target, const char *path,
2165 struct got_object_id *commit_id, struct got_repository *repo)
2167 const struct got_error *err = NULL;
2169 char *name, *parent_path = NULL;
2170 struct got_object_id *tree_obj_id = NULL;
2171 struct got_tree_object *tree = NULL;
2172 struct got_tree_entry *te = NULL;
2174 *link_target = NULL;
2176 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2177 return got_error(GOT_ERR_NO_SPACE);
2179 name = basename(buf);
2181 return got_error_from_errno2("basename", path);
2183 err = got_path_dirname(&parent_path, path);
2187 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2190 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2191 /* Display the complete path in error message. */
2192 err = got_error_path(path, err->code);
2197 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2201 te = got_object_tree_find_entry(tree, name);
2203 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2207 if (got_object_tree_entry_is_symlink(te)) {
2208 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2211 if (!got_path_is_absolute(*link_target)) {
2213 if (asprintf(&abspath, "%s/%s", parent_path,
2214 *link_target) == -1) {
2215 err = got_error_from_errno("asprintf");
2219 *link_target = malloc(PATH_MAX);
2220 if (*link_target == NULL) {
2221 err = got_error_from_errno("malloc");
2224 err = got_canonpath(abspath, *link_target, PATH_MAX);
2233 got_object_tree_close(tree);
2236 *link_target = NULL;
2241 const struct got_error *
2242 got_object_resolve_symlinks(char **link_target, const char *path,
2243 struct got_object_id *commit_id, struct got_repository *repo)
2245 const struct got_error *err = NULL;
2246 char *next_target = NULL;
2247 int max_recursion = 40; /* matches Git */
2249 *link_target = NULL;
2252 err = resolve_symlink(&next_target,
2253 *link_target ? *link_target : path, commit_id, repo);
2258 if (--max_recursion == 0) {
2259 err = got_error_path(path, GOT_ERR_RECURSION);
2260 *link_target = NULL;
2263 *link_target = next_target;
2265 } while (next_target);
2270 const struct got_error *
2271 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2272 struct got_object_id *commit_id, const char *path,
2273 struct got_repository *repo)
2275 const struct got_error *err = NULL;
2276 struct got_pack *pack = NULL;
2277 struct got_packidx *packidx = NULL;
2278 char *path_packfile = NULL;
2279 struct got_commit_object *changed_commit = NULL;
2280 struct got_object_id *changed_commit_id = NULL;
2283 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2285 if (err->code != GOT_ERR_NO_OBJ)
2290 err = got_packidx_get_packfile_path(&path_packfile,
2291 packidx->path_packidx);
2295 pack = got_repo_get_cached_pack(repo, path_packfile);
2297 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2302 if (pack->privsep_child == NULL) {
2303 err = start_pack_privsep_child(pack, packidx);
2308 err = got_privsep_send_commit_traversal_request(
2309 pack->privsep_child->ibuf, commit_id, idx, path);
2313 err = got_privsep_recv_traversed_commits(&changed_commit,
2314 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2318 if (changed_commit) {
2320 * Cache the commit in which the path was changed.
2321 * This commit might be opened again soon.
2323 changed_commit->refcnt++;
2324 err = got_repo_cache_commit(repo, changed_commit_id,
2326 got_object_commit_close(changed_commit);
2329 free(path_packfile);
2330 free(changed_commit_id);