2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <sys/socket.h>
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_repository.h"
44 #include "got_opentemp.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_object.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_object_idcache.h"
53 #include "got_lib_object_cache.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_repository.h"
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 struct got_object_id *
67 got_object_get_id(struct got_object *obj)
72 const struct got_error *
73 got_object_get_id_str(char **outbuf, struct got_object *obj)
75 return got_object_id_str(outbuf, &obj->id);
78 const struct got_error *
79 got_object_get_type(int *type, struct got_repository *repo,
80 struct got_object_id *id)
82 const struct got_error *err = NULL;
83 struct got_object *obj;
85 err = got_object_open(&obj, repo, id);
90 case GOT_OBJ_TYPE_COMMIT:
91 case GOT_OBJ_TYPE_TREE:
92 case GOT_OBJ_TYPE_BLOB:
93 case GOT_OBJ_TYPE_TAG:
97 err = got_error(GOT_ERR_OBJ_TYPE);
101 got_object_close(obj);
105 const struct got_error *
106 got_object_get_path(char **path, struct got_object_id *id,
107 struct got_repository *repo)
109 const struct got_error *err = NULL;
115 path_objects = got_repo_get_path_objects(repo);
116 if (path_objects == NULL)
117 return got_error_from_errno("got_repo_get_path_objects");
119 err = got_object_id_str(&hex, id);
123 if (asprintf(path, "%s/%.2x/%s", path_objects,
124 id->sha1[0], hex + 2) == -1)
125 err = got_error_from_errno("asprintf");
133 const struct got_error *
134 got_object_open_loose_fd(int *fd, struct got_object_id *id,
135 struct got_repository *repo)
137 const struct got_error *err = NULL;
140 err = got_object_get_path(&path, id, repo);
143 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
145 err = got_error_from_errno2("open", path);
153 static const struct got_error *
154 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
155 struct got_object_id *id)
157 const struct got_error *err = NULL;
158 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
160 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
164 err = got_privsep_recv_obj(obj, ibuf);
168 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
173 /* Create temporary files used during delta application. */
174 static const struct got_error *
175 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
177 const struct got_error *err;
178 int basefd = -1, accumfd = -1;
181 * For performance reasons, the child will keep reusing the
182 * same temporary files during every object request.
183 * Opening and closing new files for every object request is
184 * too expensive during operations such as 'gotadmin pack'.
186 if (pack->child_has_tempfiles)
189 basefd = dup(pack->basefd);
191 return got_error_from_errno("dup");
193 accumfd = dup(pack->accumfd);
195 err = got_error_from_errno("dup");
199 err = got_privsep_send_tmpfd(ibuf, basefd);
203 err = got_privsep_send_tmpfd(ibuf, accumfd);
211 pack->child_has_tempfiles = 1;
215 static const struct got_error *
216 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
217 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
219 const struct got_error *err = NULL;
220 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
223 err = pack_child_send_tempfiles(ibuf, pack);
227 outfd_child = dup(outfd);
228 if (outfd_child == -1)
229 return got_error_from_errno("dup");
231 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
237 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
241 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
248 static const struct got_error *
249 read_packed_object_privsep(struct got_object **obj,
250 struct got_repository *repo, struct got_pack *pack,
251 struct got_packidx *packidx, int idx, struct got_object_id *id)
253 const struct got_error *err = NULL;
255 if (pack->privsep_child == NULL) {
256 err = got_pack_start_privsep_child(pack, packidx);
261 return request_packed_object(obj, pack, idx, id);
264 static const struct got_error *
265 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
266 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
267 struct got_object_id *id)
269 const struct got_error *err = NULL;
271 if (pack->privsep_child == NULL) {
272 err = got_pack_start_privsep_child(pack, packidx);
277 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
281 const struct got_error *
282 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
283 struct got_repository *repo)
285 const struct got_error *err = NULL;
286 struct got_pack *pack = NULL;
287 struct got_packidx *packidx = NULL;
291 err = got_repo_search_packidx(&packidx, &idx, repo, id);
295 err = got_packidx_get_packfile_path(&path_packfile,
296 packidx->path_packidx);
300 pack = got_repo_get_cached_pack(repo, path_packfile);
302 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
307 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
315 const struct got_error *
316 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
317 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
318 struct got_repository *repo)
320 return read_packed_object_privsep(obj, repo, pack, packidx,
324 const struct got_error *
325 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
326 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
327 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
328 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
329 struct got_repository *repo)
331 const struct got_error *err = NULL;
332 struct got_pack *pack = NULL;
338 *delta_compressed_size = 0;
340 *delta_out_offset = 0;
342 err = got_packidx_get_packfile_path(&path_packfile,
343 packidx->path_packidx);
347 pack = got_repo_get_cached_pack(repo, path_packfile);
349 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
354 if (pack->privsep_child == NULL) {
355 err = got_pack_start_privsep_child(pack, packidx);
360 if (!pack->child_has_delta_outfd) {
362 outfd_child = dup(delta_cache_fd);
363 if (outfd_child == -1)
364 return got_error_from_errno("dup");
365 err = got_privsep_send_raw_delta_outfd(
366 pack->privsep_child->ibuf, outfd_child);
369 pack->child_has_delta_outfd = 1;
372 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
377 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
378 delta_compressed_size, delta_offset, delta_out_offset, base_id,
379 pack->privsep_child->ibuf);
382 static const struct got_error *
383 request_object(struct got_object **obj, struct got_object_id *id,
384 struct got_repository *repo, int fd)
386 const struct got_error *err = NULL;
387 struct imsgbuf *ibuf;
389 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
391 err = got_privsep_send_obj_req(ibuf, fd, id);
395 return got_privsep_recv_obj(obj, ibuf);
398 static const struct got_error *
399 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
400 struct got_object_id *id, struct got_repository *repo, int infd)
402 const struct got_error *err = NULL;
403 struct imsgbuf *ibuf;
406 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
408 outfd_child = dup(outfd);
409 if (outfd_child == -1)
410 return got_error_from_errno("dup");
412 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
416 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
420 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
423 static const struct got_error *
424 start_read_object_child(struct got_repository *repo)
426 const struct got_error *err = NULL;
429 struct imsgbuf *ibuf;
431 ibuf = calloc(1, sizeof(*ibuf));
433 return got_error_from_errno("calloc");
435 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
436 err = got_error_from_errno("socketpair");
443 err = got_error_from_errno("fork");
448 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
453 if (close(imsg_fds[1]) == -1) {
454 err = got_error_from_errno("close");
459 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
461 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
462 imsg_init(ibuf, imsg_fds[0]);
463 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
468 const struct got_error *
469 got_object_read_header_privsep(struct got_object **obj,
470 struct got_object_id *id, struct got_repository *repo, int obj_fd)
472 const struct got_error *err;
474 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
475 return request_object(obj, id, repo, obj_fd);
477 err = start_read_object_child(repo);
483 return request_object(obj, id, repo, obj_fd);
486 static const struct got_error *
487 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
488 int outfd, struct got_object_id *id, struct got_repository *repo,
491 const struct got_error *err;
493 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
494 return request_raw_object(outbuf, size, hdrlen, outfd, id,
497 err = start_read_object_child(repo);
501 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
505 const struct got_error *
506 got_object_open(struct got_object **obj, struct got_repository *repo,
507 struct got_object_id *id)
509 const struct got_error *err = NULL;
512 *obj = got_repo_get_cached_object(repo, id);
518 err = got_object_open_packed(obj, id, repo);
519 if (err && err->code != GOT_ERR_NO_OBJ)
523 return got_repo_cache_object(repo, id, *obj);
526 err = got_object_open_loose_fd(&fd, id, repo);
528 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
529 err = got_error_no_obj(id);
533 err = got_object_read_header_privsep(obj, id, repo, fd);
537 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
540 return got_repo_cache_object(repo, id, *obj);
543 /* *outfd must be initialized to -1 by caller */
544 const struct got_error *
545 got_object_raw_open(struct got_raw_object **obj, int *outfd,
546 struct got_repository *repo, struct got_object_id *id)
548 const struct got_error *err = NULL;
549 struct got_packidx *packidx = NULL;
551 uint8_t *outbuf = NULL;
554 char *path_packfile = NULL;
556 *obj = got_repo_get_cached_raw_object(repo, id);
563 *outfd = got_opentempfd();
565 return got_error_from_errno("got_opentempfd");
568 err = got_repo_search_packidx(&packidx, &idx, repo, id);
570 struct got_pack *pack = NULL;
572 err = got_packidx_get_packfile_path(&path_packfile,
573 packidx->path_packidx);
577 pack = got_repo_get_cached_pack(repo, path_packfile);
579 err = got_repo_cache_pack(&pack, repo, path_packfile,
584 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
585 *outfd, pack, packidx, idx, id);
588 } else if (err->code == GOT_ERR_NO_OBJ) {
591 err = got_object_open_loose_fd(&fd, id, repo);
594 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
600 *obj = calloc(1, sizeof(**obj));
602 err = got_error_from_errno("calloc");
608 (*obj)->data = outbuf;
611 if (fstat(*outfd, &sb) == -1) {
612 err = got_error_from_errno("fstat");
616 if (sb.st_size != hdrlen + size) {
617 err = got_error(GOT_ERR_PRIVSEP_LEN);
620 #ifndef GOT_PACK_NO_MMAP
621 if (hdrlen + size > 0) {
622 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
623 MAP_PRIVATE, *outfd, 0);
624 if ((*obj)->data == MAP_FAILED) {
625 if (errno != ENOMEM) {
626 err = got_error_from_errno("mmap");
637 (*obj)->f = fdopen(*outfd, "r");
638 if ((*obj)->f == NULL) {
639 err = got_error_from_errno("fdopen");
645 (*obj)->hdrlen = hdrlen;
647 err = got_repo_cache_raw_object(repo, id, *obj);
652 got_object_raw_close(*obj);
661 const struct got_error *
662 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
665 struct got_object_id id;
667 if (!got_parse_sha1_digest(id.sha1, id_str))
668 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
670 return got_object_open(obj, repo, &id);
673 const struct got_error *
674 got_object_resolve_id_str(struct got_object_id **id,
675 struct got_repository *repo, const char *id_str)
677 const struct got_error *err = NULL;
678 struct got_object *obj;
680 err = got_object_open_by_id_str(&obj, repo, id_str);
684 *id = got_object_id_dup(got_object_get_id(obj));
685 got_object_close(obj);
687 return got_error_from_errno("got_object_id_dup");
692 static const struct got_error *
693 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
694 int pack_idx, struct got_object_id *id)
696 const struct got_error *err = NULL;
698 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
703 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
707 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
711 static const struct got_error *
712 read_packed_commit_privsep(struct got_commit_object **commit,
713 struct got_pack *pack, struct got_packidx *packidx, int idx,
714 struct got_object_id *id)
716 const struct got_error *err = NULL;
718 if (pack->privsep_child)
719 return request_packed_commit(commit, pack, idx, id);
721 err = got_pack_start_privsep_child(pack, packidx);
725 return request_packed_commit(commit, pack, idx, id);
728 static const struct got_error *
729 request_commit(struct got_commit_object **commit, struct got_repository *repo,
730 int fd, struct got_object_id *id)
732 const struct got_error *err = NULL;
733 struct imsgbuf *ibuf;
735 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
737 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
741 return got_privsep_recv_commit(commit, ibuf);
744 static const struct got_error *
745 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
746 struct got_object_id *id, struct got_repository *repo)
748 const struct got_error *err;
751 struct imsgbuf *ibuf;
753 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
754 return request_commit(commit, repo, obj_fd, id);
756 ibuf = calloc(1, sizeof(*ibuf));
758 return got_error_from_errno("calloc");
760 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
761 err = got_error_from_errno("socketpair");
768 err = got_error_from_errno("fork");
773 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
778 if (close(imsg_fds[1]) == -1) {
779 err = got_error_from_errno("close");
783 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
785 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
786 imsg_init(ibuf, imsg_fds[0]);
787 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
789 return request_commit(commit, repo, obj_fd, id);
793 static const struct got_error *
794 open_commit(struct got_commit_object **commit,
795 struct got_repository *repo, struct got_object_id *id, int check_cache)
797 const struct got_error *err = NULL;
798 struct got_packidx *packidx = NULL;
800 char *path_packfile = NULL;
803 *commit = got_repo_get_cached_commit(repo, id);
804 if (*commit != NULL) {
811 err = got_repo_search_packidx(&packidx, &idx, repo, id);
813 struct got_pack *pack = NULL;
815 err = got_packidx_get_packfile_path(&path_packfile,
816 packidx->path_packidx);
820 pack = got_repo_get_cached_pack(repo, path_packfile);
822 err = got_repo_cache_pack(&pack, repo, path_packfile,
827 err = read_packed_commit_privsep(commit, pack,
829 } else if (err->code == GOT_ERR_NO_OBJ) {
832 err = got_object_open_loose_fd(&fd, id, repo);
835 err = read_commit_privsep(commit, fd, id, repo);
840 err = got_repo_cache_commit(repo, id, *commit);
847 const struct got_error *
848 got_object_open_as_commit(struct got_commit_object **commit,
849 struct got_repository *repo, struct got_object_id *id)
851 *commit = got_repo_get_cached_commit(repo, id);
852 if (*commit != NULL) {
857 return open_commit(commit, repo, id, 0);
860 const struct got_error *
861 got_object_commit_open(struct got_commit_object **commit,
862 struct got_repository *repo, struct got_object *obj)
864 return open_commit(commit, repo, got_object_get_id(obj), 1);
867 const struct got_error *
868 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
870 *qid = calloc(1, sizeof(**qid));
872 return got_error_from_errno("calloc");
874 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
878 const struct got_error *
879 got_object_id_queue_copy(const struct got_object_id_queue *src,
880 struct got_object_id_queue *dest)
882 const struct got_error *err;
883 struct got_object_qid *qid;
885 STAILQ_FOREACH(qid, src, entry) {
886 struct got_object_qid *new;
888 * Deep-copy the object ID only. Let the caller deal
889 * with setting up the new->data pointer if needed.
891 err = got_object_qid_alloc(&new, &qid->id);
893 got_object_id_queue_free(dest);
896 STAILQ_INSERT_TAIL(dest, new, entry);
902 static const struct got_error *
903 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
904 int pack_idx, struct got_object_id *id)
906 const struct got_error *err = NULL;
908 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
913 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
916 static const struct got_error *
917 read_packed_tree_privsep(struct got_tree_object **tree,
918 struct got_pack *pack, struct got_packidx *packidx, int idx,
919 struct got_object_id *id)
921 const struct got_error *err = NULL;
923 if (pack->privsep_child)
924 return request_packed_tree(tree, pack, idx, id);
926 err = got_pack_start_privsep_child(pack, packidx);
930 return request_packed_tree(tree, pack, idx, id);
933 static const struct got_error *
934 request_tree(struct got_tree_object **tree, struct got_repository *repo,
935 int fd, struct got_object_id *id)
937 const struct got_error *err = NULL;
938 struct imsgbuf *ibuf;
940 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
942 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
946 return got_privsep_recv_tree(tree, ibuf);
949 static const struct got_error *
950 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
951 struct got_object_id *id, struct got_repository *repo)
953 const struct got_error *err;
956 struct imsgbuf *ibuf;
958 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
959 return request_tree(tree, repo, obj_fd, id);
961 ibuf = calloc(1, sizeof(*ibuf));
963 return got_error_from_errno("calloc");
965 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
966 err = got_error_from_errno("socketpair");
973 err = got_error_from_errno("fork");
978 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
983 if (close(imsg_fds[1]) == -1) {
984 err = got_error_from_errno("close");
988 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
990 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
991 imsg_init(ibuf, imsg_fds[0]);
992 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
995 return request_tree(tree, repo, obj_fd, id);
998 static const struct got_error *
999 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1000 struct got_object_id *id, int check_cache)
1002 const struct got_error *err = NULL;
1003 struct got_packidx *packidx = NULL;
1005 char *path_packfile = NULL;
1008 *tree = got_repo_get_cached_tree(repo, id);
1009 if (*tree != NULL) {
1016 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1018 struct got_pack *pack = NULL;
1020 err = got_packidx_get_packfile_path(&path_packfile,
1021 packidx->path_packidx);
1025 pack = got_repo_get_cached_pack(repo, path_packfile);
1027 err = got_repo_cache_pack(&pack, repo, path_packfile,
1032 err = read_packed_tree_privsep(tree, pack,
1034 } else if (err->code == GOT_ERR_NO_OBJ) {
1037 err = got_object_open_loose_fd(&fd, id, repo);
1040 err = read_tree_privsep(tree, fd, id, repo);
1045 err = got_repo_cache_tree(repo, id, *tree);
1048 free(path_packfile);
1052 const struct got_error *
1053 got_object_open_as_tree(struct got_tree_object **tree,
1054 struct got_repository *repo, struct got_object_id *id)
1056 *tree = got_repo_get_cached_tree(repo, id);
1057 if (*tree != NULL) {
1062 return open_tree(tree, repo, id, 0);
1065 const struct got_error *
1066 got_object_tree_open(struct got_tree_object **tree,
1067 struct got_repository *repo, struct got_object *obj)
1069 return open_tree(tree, repo, got_object_get_id(obj), 1);
1073 got_object_tree_get_nentries(struct got_tree_object *tree)
1075 return tree->nentries;
1078 struct got_tree_entry *
1079 got_object_tree_get_first_entry(struct got_tree_object *tree)
1081 return got_object_tree_get_entry(tree, 0);
1084 struct got_tree_entry *
1085 got_object_tree_get_last_entry(struct got_tree_object *tree)
1087 return got_object_tree_get_entry(tree, tree->nentries - 1);
1090 struct got_tree_entry *
1091 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1093 if (i < 0 || i >= tree->nentries)
1095 return &tree->entries[i];
1099 got_tree_entry_get_mode(struct got_tree_entry *te)
1105 got_tree_entry_get_name(struct got_tree_entry *te)
1107 return &te->name[0];
1110 struct got_object_id *
1111 got_tree_entry_get_id(struct got_tree_entry *te)
1116 const struct got_error *
1117 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1119 const struct got_error *err = NULL;
1120 size_t len, totlen, hdrlen, offset;
1124 hdrlen = got_object_blob_get_hdrlen(blob);
1130 err = got_object_blob_read_block(&len, blob);
1137 totlen += len - hdrlen;
1138 p = realloc(*s, totlen + 1);
1140 err = got_error_from_errno("realloc");
1146 /* Skip blob object header first time around. */
1148 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1153 (*s)[totlen] = '\0';
1157 const struct got_error *
1158 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1159 struct got_repository *repo)
1161 const struct got_error *err = NULL;
1162 struct got_blob_object *blob = NULL;
1165 *link_target = NULL;
1167 if (!got_object_tree_entry_is_symlink(te))
1168 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1170 fd = got_opentempfd();
1172 err = got_error_from_errno("got_opentempfd");
1176 err = got_object_open_as_blob(&blob, repo,
1177 got_tree_entry_get_id(te), PATH_MAX, fd);
1181 err = got_object_blob_read_to_str(link_target, blob);
1183 if (fd != -1 && close(fd) == -1 && err == NULL)
1184 err = got_error_from_errno("close");
1186 got_object_blob_close(blob);
1189 *link_target = NULL;
1195 got_tree_entry_get_index(struct got_tree_entry *te)
1200 struct got_tree_entry *
1201 got_tree_entry_get_next(struct got_tree_object *tree,
1202 struct got_tree_entry *te)
1204 return got_object_tree_get_entry(tree, te->idx + 1);
1207 struct got_tree_entry *
1208 got_tree_entry_get_prev(struct got_tree_object *tree,
1209 struct got_tree_entry *te)
1211 return got_object_tree_get_entry(tree, te->idx - 1);
1214 static const struct got_error *
1215 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1216 struct got_pack *pack, struct got_packidx *packidx, int idx,
1217 struct got_object_id *id)
1219 const struct got_error *err = NULL;
1220 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1223 err = pack_child_send_tempfiles(ibuf, pack);
1227 outfd_child = dup(outfd);
1228 if (outfd_child == -1)
1229 return got_error_from_errno("dup");
1231 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1235 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1241 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1242 pack->privsep_child->ibuf);
1246 if (lseek(outfd, SEEK_SET, 0) == -1)
1247 err = got_error_from_errno("lseek");
1252 static const struct got_error *
1253 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1254 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1255 struct got_object_id *id)
1257 const struct got_error *err = NULL;
1259 if (pack->privsep_child == NULL) {
1260 err = got_pack_start_privsep_child(pack, packidx);
1265 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1269 static const struct got_error *
1270 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1271 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1273 const struct got_error *err = NULL;
1276 outfd_child = dup(outfd);
1277 if (outfd_child == -1)
1278 return got_error_from_errno("dup");
1280 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1284 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1288 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1292 if (lseek(outfd, SEEK_SET, 0) == -1)
1293 return got_error_from_errno("lseek");
1298 static const struct got_error *
1299 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1300 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1302 const struct got_error *err;
1305 struct imsgbuf *ibuf;
1307 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1308 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1309 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1313 ibuf = calloc(1, sizeof(*ibuf));
1315 return got_error_from_errno("calloc");
1317 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1318 err = got_error_from_errno("socketpair");
1325 err = got_error_from_errno("fork");
1329 else if (pid == 0) {
1330 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1335 if (close(imsg_fds[1]) == -1) {
1336 err = got_error_from_errno("close");
1340 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1342 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1343 imsg_init(ibuf, imsg_fds[0]);
1344 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1346 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1349 static const struct got_error *
1350 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1351 struct got_object_id *id, size_t blocksize, int outfd)
1353 const struct got_error *err = NULL;
1354 struct got_packidx *packidx = NULL;
1356 char *path_packfile = NULL;
1358 size_t size, hdrlen;
1361 *blob = calloc(1, sizeof(**blob));
1363 return got_error_from_errno("calloc");
1365 (*blob)->read_buf = malloc(blocksize);
1366 if ((*blob)->read_buf == NULL) {
1367 err = got_error_from_errno("malloc");
1371 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1373 struct got_pack *pack = NULL;
1375 err = got_packidx_get_packfile_path(&path_packfile,
1376 packidx->path_packidx);
1380 pack = got_repo_get_cached_pack(repo, path_packfile);
1382 err = got_repo_cache_pack(&pack, repo, path_packfile,
1387 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1388 pack, packidx, idx, id);
1389 } else if (err->code == GOT_ERR_NO_OBJ) {
1392 err = got_object_open_loose_fd(&infd, id, repo);
1395 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1401 if (hdrlen > size) {
1402 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1407 (*blob)->f = fmemopen(outbuf, size, "rb");
1408 if ((*blob)->f == NULL) {
1409 err = got_error_from_errno("fmemopen");
1413 (*blob)->data = outbuf;
1415 if (fstat(outfd, &sb) == -1) {
1416 err = got_error_from_errno("fstat");
1420 if (sb.st_size != size) {
1421 err = got_error(GOT_ERR_PRIVSEP_LEN);
1425 (*blob)->f = fdopen(outfd, "rb");
1426 if ((*blob)->f == NULL) {
1427 err = got_error_from_errno("fdopen");
1434 (*blob)->hdrlen = hdrlen;
1435 (*blob)->blocksize = blocksize;
1436 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1439 free(path_packfile);
1442 got_object_blob_close(*blob);
1449 const struct got_error *
1450 got_object_open_as_blob(struct got_blob_object **blob,
1451 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
1454 return open_blob(blob, repo, id, blocksize, outfd);
1457 const struct got_error *
1458 got_object_blob_open(struct got_blob_object **blob,
1459 struct got_repository *repo, struct got_object *obj, size_t blocksize,
1462 return open_blob(blob, repo, got_object_get_id(obj), blocksize, outfd);
1465 const struct got_error *
1466 got_object_blob_close(struct got_blob_object *blob)
1468 const struct got_error *err = NULL;
1469 free(blob->read_buf);
1470 if (blob->f && fclose(blob->f) == EOF)
1471 err = got_error_from_errno("fclose");
1478 got_object_blob_rewind(struct got_blob_object *blob)
1485 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1487 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1491 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1493 return blob->hdrlen;
1497 got_object_blob_get_read_buf(struct got_blob_object *blob)
1499 return blob->read_buf;
1502 const struct got_error *
1503 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1507 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1508 if (n == 0 && ferror(blob->f))
1509 return got_ferror(blob->f, GOT_ERR_IO);
1514 const struct got_error *
1515 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1516 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1518 const struct got_error *err = NULL;
1519 size_t n, len, hdrlen;
1522 const int alloc_chunksz = 512;
1524 off_t off = 0, total_len = 0;
1527 *line_offsets = NULL;
1533 hdrlen = got_object_blob_get_hdrlen(blob);
1535 err = got_object_blob_read_block(&len, blob);
1540 buf = got_object_blob_get_read_buf(blob);
1543 if (line_offsets && *line_offsets == NULL) {
1544 /* Have some data but perhaps no '\n'. */
1546 nalloc = alloc_chunksz;
1547 *line_offsets = calloc(nalloc,
1548 sizeof(**line_offsets));
1549 if (*line_offsets == NULL)
1550 return got_error_from_errno("calloc");
1552 /* Skip forward over end of first line. */
1559 /* Scan '\n' offsets in remaining chunk of data. */
1561 if (buf[i] != '\n') {
1566 if (line_offsets && nalloc < *nlines) {
1567 size_t n = *nlines + alloc_chunksz;
1568 off_t *o = recallocarray(*line_offsets,
1569 nalloc, n, sizeof(**line_offsets));
1571 free(*line_offsets);
1572 *line_offsets = NULL;
1573 return got_error_from_errno(
1580 off = total_len + i - hdrlen + 1;
1581 (*line_offsets)[*nlines - 1] = off;
1586 /* Skip blob object header first time around. */
1587 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1588 if (n != len - hdrlen)
1589 return got_ferror(outfile, GOT_ERR_IO);
1590 total_len += len - hdrlen;
1594 if (fflush(outfile) != 0)
1595 return got_error_from_errno("fflush");
1599 *filesize = total_len;
1604 static const struct got_error *
1605 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1606 int pack_idx, struct got_object_id *id)
1608 const struct got_error *err = NULL;
1610 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1615 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1618 static const struct got_error *
1619 read_packed_tag_privsep(struct got_tag_object **tag,
1620 struct got_pack *pack, struct got_packidx *packidx, int idx,
1621 struct got_object_id *id)
1623 const struct got_error *err = NULL;
1625 if (pack->privsep_child)
1626 return request_packed_tag(tag, pack, idx, id);
1628 err = got_pack_start_privsep_child(pack, packidx);
1632 return request_packed_tag(tag, pack, idx, id);
1635 static const struct got_error *
1636 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1637 int fd, struct got_object_id *id)
1639 const struct got_error *err = NULL;
1640 struct imsgbuf *ibuf;
1642 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1644 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1648 return got_privsep_recv_tag(tag, ibuf);
1651 static const struct got_error *
1652 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1653 struct got_object_id *id, struct got_repository *repo)
1655 const struct got_error *err;
1658 struct imsgbuf *ibuf;
1660 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1661 return request_tag(tag, repo, obj_fd, id);
1663 ibuf = calloc(1, sizeof(*ibuf));
1665 return got_error_from_errno("calloc");
1667 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1668 err = got_error_from_errno("socketpair");
1675 err = got_error_from_errno("fork");
1679 else if (pid == 0) {
1680 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1685 if (close(imsg_fds[1]) == -1) {
1686 err = got_error_from_errno("close");
1690 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1692 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1693 imsg_init(ibuf, imsg_fds[0]);
1694 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1696 return request_tag(tag, repo, obj_fd, id);
1699 static const struct got_error *
1700 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1701 struct got_object_id *id, int check_cache)
1703 const struct got_error *err = NULL;
1704 struct got_packidx *packidx = NULL;
1706 char *path_packfile = NULL;
1707 struct got_object *obj = NULL;
1708 int obj_type = GOT_OBJ_TYPE_ANY;
1711 *tag = got_repo_get_cached_tag(repo, id);
1719 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1721 struct got_pack *pack = NULL;
1723 err = got_packidx_get_packfile_path(&path_packfile,
1724 packidx->path_packidx);
1728 pack = got_repo_get_cached_pack(repo, path_packfile);
1730 err = got_repo_cache_pack(&pack, repo, path_packfile,
1736 /* Beware of "lightweight" tags: Check object type first. */
1737 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1741 obj_type = obj->type;
1742 got_object_close(obj);
1743 if (obj_type != GOT_OBJ_TYPE_TAG) {
1744 err = got_error(GOT_ERR_OBJ_TYPE);
1747 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1748 } else if (err->code == GOT_ERR_NO_OBJ) {
1751 err = got_object_open_loose_fd(&fd, id, repo);
1754 err = got_object_read_header_privsep(&obj, id, repo, fd);
1757 obj_type = obj->type;
1758 got_object_close(obj);
1759 if (obj_type != GOT_OBJ_TYPE_TAG)
1760 return got_error(GOT_ERR_OBJ_TYPE);
1762 err = got_object_open_loose_fd(&fd, id, repo);
1765 err = read_tag_privsep(tag, fd, id, repo);
1770 err = got_repo_cache_tag(repo, id, *tag);
1773 free(path_packfile);
1777 const struct got_error *
1778 got_object_open_as_tag(struct got_tag_object **tag,
1779 struct got_repository *repo, struct got_object_id *id)
1781 *tag = got_repo_get_cached_tag(repo, id);
1787 return open_tag(tag, repo, id, 0);
1790 const struct got_error *
1791 got_object_tag_open(struct got_tag_object **tag,
1792 struct got_repository *repo, struct got_object *obj)
1794 return open_tag(tag, repo, got_object_get_id(obj), 1);
1798 got_object_tag_get_name(struct got_tag_object *tag)
1804 got_object_tag_get_object_type(struct got_tag_object *tag)
1806 return tag->obj_type;
1809 struct got_object_id *
1810 got_object_tag_get_object_id(struct got_tag_object *tag)
1816 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1818 return tag->tagger_time;
1822 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1824 return tag->tagger_gmtoff;
1828 got_object_tag_get_tagger(struct got_tag_object *tag)
1834 got_object_tag_get_message(struct got_tag_object *tag)
1839 static struct got_tree_entry *
1840 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1844 /* Note that tree entries are sorted in strncmp() order. */
1845 for (i = 0; i < tree->nentries; i++) {
1846 struct got_tree_entry *te = &tree->entries[i];
1847 int cmp = strncmp(te->name, name, len);
1852 if (te->name[len] == '\0')
1858 struct got_tree_entry *
1859 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1861 return find_entry_by_name(tree, name, strlen(name));
1864 const struct got_error *
1865 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1866 struct got_repository *repo, struct got_tree_object *tree,
1869 const struct got_error *err = NULL;
1870 struct got_tree_object *subtree = NULL;
1871 struct got_tree_entry *te = NULL;
1872 const char *seg, *s;
1884 struct got_tree_object *next_tree;
1893 te = find_entry_by_name(subtree, seg, seglen);
1895 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1906 err = got_object_open_as_tree(&next_tree, repo,
1911 if (subtree != tree)
1912 got_object_tree_close(subtree);
1913 subtree = next_tree;
1918 *id = got_object_id_dup(&te->id);
1920 return got_error_from_errno("got_object_id_dup");
1924 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1926 if (subtree && subtree != tree)
1927 got_object_tree_close(subtree);
1930 const struct got_error *
1931 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1932 struct got_commit_object *commit, const char *path)
1934 const struct got_error *err = NULL;
1935 struct got_tree_object *tree = NULL;
1939 /* Handle opening of root of commit's tree. */
1940 if (got_path_is_root_dir(path)) {
1941 *id = got_object_id_dup(commit->tree_id);
1943 err = got_error_from_errno("got_object_id_dup");
1945 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1948 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1952 got_object_tree_close(tree);
1957 * Normalize file mode bits to avoid false positive tree entry differences
1958 * in case tree entries have unexpected mode bits set.
1961 normalize_mode_for_comparison(mode_t mode)
1964 * For directories, the only relevant bit is the IFDIR bit.
1965 * This allows us to detect paths changing from a directory
1966 * to a file and vice versa.
1969 return mode & S_IFDIR;
1972 * For symlinks, the only relevant bit is the IFLNK bit.
1973 * This allows us to detect paths changing from a symlinks
1974 * to a file or directory and vice versa.
1977 return mode & S_IFLNK;
1979 /* For files, the only change we care about is the executable bit. */
1980 return mode & S_IXUSR;
1983 const struct got_error *
1984 got_object_tree_path_changed(int *changed,
1985 struct got_tree_object *tree01, struct got_tree_object *tree02,
1986 const char *path, struct got_repository *repo)
1988 const struct got_error *err = NULL;
1989 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1990 struct got_tree_entry *te1 = NULL, *te2 = NULL;
1991 const char *seg, *s;
1996 /* We not do support comparing the root path. */
1997 if (got_path_is_root_dir(path))
1998 return got_error_path(path, GOT_ERR_BAD_PATH);
2008 struct got_tree_object *next_tree1, *next_tree2;
2009 mode_t mode1, mode2;
2018 te1 = find_entry_by_name(tree1, seg, seglen);
2020 err = got_error(GOT_ERR_NO_OBJ);
2025 te2 = find_entry_by_name(tree2, seg, seglen);
2028 mode1 = normalize_mode_for_comparison(te1->mode);
2029 mode2 = normalize_mode_for_comparison(te2->mode);
2030 if (mode1 != mode2) {
2035 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2041 if (*s == '\0') { /* final path element */
2050 err = got_object_open_as_tree(&next_tree1, repo,
2055 if (tree1 != tree01)
2056 got_object_tree_close(tree1);
2060 err = got_object_open_as_tree(&next_tree2, repo,
2065 if (tree2 != tree02)
2066 got_object_tree_close(tree2);
2069 if (tree2 != tree02)
2070 got_object_tree_close(tree2);
2076 if (tree1 && tree1 != tree01)
2077 got_object_tree_close(tree1);
2078 if (tree2 && tree2 != tree02)
2079 got_object_tree_close(tree2);
2083 const struct got_error *
2084 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2085 struct got_tree_entry *te)
2087 const struct got_error *err = NULL;
2089 *new_te = calloc(1, sizeof(**new_te));
2090 if (*new_te == NULL)
2091 return got_error_from_errno("calloc");
2093 (*new_te)->mode = te->mode;
2094 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2095 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2100 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2102 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2106 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2108 /* S_IFDIR check avoids confusing symlinks with submodules. */
2109 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2112 static const struct got_error *
2113 resolve_symlink(char **link_target, const char *path,
2114 struct got_commit_object *commit, struct got_repository *repo)
2116 const struct got_error *err = NULL;
2118 char *name, *parent_path = NULL;
2119 struct got_object_id *tree_obj_id = NULL;
2120 struct got_tree_object *tree = NULL;
2121 struct got_tree_entry *te = NULL;
2123 *link_target = NULL;
2125 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2126 return got_error(GOT_ERR_NO_SPACE);
2128 name = basename(buf);
2130 return got_error_from_errno2("basename", path);
2132 err = got_path_dirname(&parent_path, path);
2136 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2139 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2140 /* Display the complete path in error message. */
2141 err = got_error_path(path, err->code);
2146 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2150 te = got_object_tree_find_entry(tree, name);
2152 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2156 if (got_object_tree_entry_is_symlink(te)) {
2157 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2160 if (!got_path_is_absolute(*link_target)) {
2162 if (asprintf(&abspath, "%s/%s", parent_path,
2163 *link_target) == -1) {
2164 err = got_error_from_errno("asprintf");
2168 *link_target = malloc(PATH_MAX);
2169 if (*link_target == NULL) {
2170 err = got_error_from_errno("malloc");
2173 err = got_canonpath(abspath, *link_target, PATH_MAX);
2182 got_object_tree_close(tree);
2185 *link_target = NULL;
2190 const struct got_error *
2191 got_object_resolve_symlinks(char **link_target, const char *path,
2192 struct got_commit_object *commit, struct got_repository *repo)
2194 const struct got_error *err = NULL;
2195 char *next_target = NULL;
2196 int max_recursion = 40; /* matches Git */
2198 *link_target = NULL;
2201 err = resolve_symlink(&next_target,
2202 *link_target ? *link_target : path, commit, repo);
2207 if (--max_recursion == 0) {
2208 err = got_error_path(path, GOT_ERR_RECURSION);
2209 *link_target = NULL;
2212 *link_target = next_target;
2214 } while (next_target);
2219 const struct got_error *
2220 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2221 struct got_object_id *commit_id, const char *path,
2222 struct got_repository *repo)
2224 const struct got_error *err = NULL;
2225 struct got_pack *pack = NULL;
2226 struct got_packidx *packidx = NULL;
2227 char *path_packfile = NULL;
2228 struct got_commit_object *changed_commit = NULL;
2229 struct got_object_id *changed_commit_id = NULL;
2232 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2234 if (err->code != GOT_ERR_NO_OBJ)
2239 err = got_packidx_get_packfile_path(&path_packfile,
2240 packidx->path_packidx);
2244 pack = got_repo_get_cached_pack(repo, path_packfile);
2246 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2251 if (pack->privsep_child == NULL) {
2252 err = got_pack_start_privsep_child(pack, packidx);
2257 err = got_privsep_send_commit_traversal_request(
2258 pack->privsep_child->ibuf, commit_id, idx, path);
2262 err = got_privsep_recv_traversed_commits(&changed_commit,
2263 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2267 if (changed_commit) {
2269 * Cache the commit in which the path was changed.
2270 * This commit might be opened again soon.
2272 changed_commit->refcnt++;
2273 err = got_repo_cache_commit(repo, changed_commit_id,
2275 got_object_commit_close(changed_commit);
2278 free(path_packfile);
2279 free(changed_commit_id);
2283 const struct got_error *
2284 got_object_enumerate(int *found_all_objects,
2285 got_object_enumerate_commit_cb cb_commit,
2286 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2287 struct got_object_id **ours, int nours,
2288 struct got_object_id **theirs, int ntheirs,
2289 struct got_packidx *packidx, struct got_repository *repo)
2291 const struct got_error *err = NULL;
2292 struct got_pack *pack;
2293 char *path_packfile = NULL;
2295 err = got_packidx_get_packfile_path(&path_packfile,
2296 packidx->path_packidx);
2300 pack = got_repo_get_cached_pack(repo, path_packfile);
2302 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2307 if (pack->privsep_child == NULL) {
2308 err = got_pack_start_privsep_child(pack, packidx);
2313 err = got_privsep_send_object_enumeration_request(
2314 pack->privsep_child->ibuf);
2318 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2322 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2326 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2330 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2334 err = got_privsep_recv_enumerated_objects(found_all_objects,
2335 pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
2337 free(path_packfile);