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_id_by_path(struct got_object_id **id, struct got_repository *repo,
1910 struct got_object_id *commit_id, const char *path)
1912 const struct got_error *err = NULL;
1913 struct got_commit_object *commit = NULL;
1914 struct got_tree_object *tree = NULL;
1915 struct got_tree_entry *te = NULL;
1916 const char *seg, *s;
1921 err = got_object_open_as_commit(&commit, repo, commit_id);
1925 /* Handle opening of root of commit's tree. */
1926 if (got_path_is_root_dir(path)) {
1927 *id = got_object_id_dup(commit->tree_id);
1929 err = got_error_from_errno("got_object_id_dup");
1933 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1943 struct got_tree_object *next_tree;
1952 te = find_entry_by_name(tree, seg, seglen);
1954 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1965 err = got_object_open_as_tree(&next_tree, repo,
1970 got_object_tree_close(tree);
1976 *id = got_object_id_dup(&te->id);
1978 return got_error_from_errno("got_object_id_dup");
1980 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1983 got_object_commit_close(commit);
1985 got_object_tree_close(tree);
1990 * Normalize file mode bits to avoid false positive tree entry differences
1991 * in case tree entries have unexpected mode bits set.
1994 normalize_mode_for_comparison(mode_t mode)
1997 * For directories, the only relevant bit is the IFDIR bit.
1998 * This allows us to detect paths changing from a directory
1999 * to a file and vice versa.
2002 return mode & S_IFDIR;
2005 * For symlinks, the only relevant bit is the IFLNK bit.
2006 * This allows us to detect paths changing from a symlinks
2007 * to a file or directory and vice versa.
2010 return mode & S_IFLNK;
2012 /* For files, the only change we care about is the executable bit. */
2013 return mode & S_IXUSR;
2016 const struct got_error *
2017 got_object_tree_path_changed(int *changed,
2018 struct got_tree_object *tree01, struct got_tree_object *tree02,
2019 const char *path, struct got_repository *repo)
2021 const struct got_error *err = NULL;
2022 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2023 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2024 const char *seg, *s;
2029 /* We not do support comparing the root path. */
2030 if (got_path_is_root_dir(path))
2031 return got_error_path(path, GOT_ERR_BAD_PATH);
2041 struct got_tree_object *next_tree1, *next_tree2;
2042 mode_t mode1, mode2;
2051 te1 = find_entry_by_name(tree1, seg, seglen);
2053 err = got_error(GOT_ERR_NO_OBJ);
2058 te2 = find_entry_by_name(tree2, seg, seglen);
2061 mode1 = normalize_mode_for_comparison(te1->mode);
2062 mode2 = normalize_mode_for_comparison(te2->mode);
2063 if (mode1 != mode2) {
2068 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2074 if (*s == '\0') { /* final path element */
2083 err = got_object_open_as_tree(&next_tree1, repo,
2088 if (tree1 != tree01)
2089 got_object_tree_close(tree1);
2093 err = got_object_open_as_tree(&next_tree2, repo,
2098 if (tree2 != tree02)
2099 got_object_tree_close(tree2);
2102 if (tree2 != tree02)
2103 got_object_tree_close(tree2);
2109 if (tree1 && tree1 != tree01)
2110 got_object_tree_close(tree1);
2111 if (tree2 && tree2 != tree02)
2112 got_object_tree_close(tree2);
2116 const struct got_error *
2117 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2118 struct got_tree_entry *te)
2120 const struct got_error *err = NULL;
2122 *new_te = calloc(1, sizeof(**new_te));
2123 if (*new_te == NULL)
2124 return got_error_from_errno("calloc");
2126 (*new_te)->mode = te->mode;
2127 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2128 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2133 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2135 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2139 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2141 /* S_IFDIR check avoids confusing symlinks with submodules. */
2142 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2145 static const struct got_error *
2146 resolve_symlink(char **link_target, const char *path,
2147 struct got_object_id *commit_id, struct got_repository *repo)
2149 const struct got_error *err = NULL;
2151 char *name, *parent_path = NULL;
2152 struct got_object_id *tree_obj_id = NULL;
2153 struct got_tree_object *tree = NULL;
2154 struct got_tree_entry *te = NULL;
2156 *link_target = NULL;
2158 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2159 return got_error(GOT_ERR_NO_SPACE);
2161 name = basename(buf);
2163 return got_error_from_errno2("basename", path);
2165 err = got_path_dirname(&parent_path, path);
2169 err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2172 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2173 /* Display the complete path in error message. */
2174 err = got_error_path(path, err->code);
2179 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2183 te = got_object_tree_find_entry(tree, name);
2185 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2189 if (got_object_tree_entry_is_symlink(te)) {
2190 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2193 if (!got_path_is_absolute(*link_target)) {
2195 if (asprintf(&abspath, "%s/%s", parent_path,
2196 *link_target) == -1) {
2197 err = got_error_from_errno("asprintf");
2201 *link_target = malloc(PATH_MAX);
2202 if (*link_target == NULL) {
2203 err = got_error_from_errno("malloc");
2206 err = got_canonpath(abspath, *link_target, PATH_MAX);
2215 got_object_tree_close(tree);
2218 *link_target = NULL;
2223 const struct got_error *
2224 got_object_resolve_symlinks(char **link_target, const char *path,
2225 struct got_object_id *commit_id, struct got_repository *repo)
2227 const struct got_error *err = NULL;
2228 char *next_target = NULL;
2229 int max_recursion = 40; /* matches Git */
2231 *link_target = NULL;
2234 err = resolve_symlink(&next_target,
2235 *link_target ? *link_target : path, commit_id, repo);
2240 if (--max_recursion == 0) {
2241 err = got_error_path(path, GOT_ERR_RECURSION);
2242 *link_target = NULL;
2245 *link_target = next_target;
2247 } while (next_target);
2252 const struct got_error *
2253 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2254 struct got_object_id *commit_id, const char *path,
2255 struct got_repository *repo)
2257 const struct got_error *err = NULL;
2258 struct got_pack *pack = NULL;
2259 struct got_packidx *packidx = NULL;
2260 char *path_packfile = NULL;
2261 struct got_commit_object *changed_commit = NULL;
2262 struct got_object_id *changed_commit_id = NULL;
2265 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2267 if (err->code != GOT_ERR_NO_OBJ)
2272 err = got_packidx_get_packfile_path(&path_packfile,
2273 packidx->path_packidx);
2277 pack = got_repo_get_cached_pack(repo, path_packfile);
2279 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2284 if (pack->privsep_child == NULL) {
2285 err = start_pack_privsep_child(pack, packidx);
2290 err = got_privsep_send_commit_traversal_request(
2291 pack->privsep_child->ibuf, commit_id, idx, path);
2295 err = got_privsep_recv_traversed_commits(&changed_commit,
2296 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2300 if (changed_commit) {
2302 * Cache the commit in which the path was changed.
2303 * This commit might be opened again soon.
2305 changed_commit->refcnt++;
2306 err = got_repo_cache_commit(repo, changed_commit_id,
2308 got_object_commit_close(changed_commit);
2311 free(path_packfile);
2312 free(changed_commit_id);