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>
38 #include "got_compat.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))
62 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 struct got_object_id *
66 got_object_get_id(struct got_object *obj)
71 const struct got_error *
72 got_object_get_id_str(char **outbuf, struct got_object *obj)
74 return got_object_id_str(outbuf, &obj->id);
77 const struct got_error *
78 got_object_get_type(int *type, struct got_repository *repo,
79 struct got_object_id *id)
81 const struct got_error *err = NULL;
82 struct got_object *obj;
84 err = got_object_open(&obj, repo, id);
89 case GOT_OBJ_TYPE_COMMIT:
90 case GOT_OBJ_TYPE_TREE:
91 case GOT_OBJ_TYPE_BLOB:
92 case GOT_OBJ_TYPE_TAG:
96 err = got_error(GOT_ERR_OBJ_TYPE);
100 got_object_close(obj);
104 const struct got_error *
105 got_object_get_path(char **path, struct got_object_id *id,
106 struct got_repository *repo)
108 const struct got_error *err = NULL;
114 path_objects = got_repo_get_path_objects(repo);
115 if (path_objects == NULL)
116 return got_error_from_errno("got_repo_get_path_objects");
118 err = got_object_id_str(&hex, id);
122 if (asprintf(path, "%s/%.2x/%s", path_objects,
123 id->sha1[0], hex + 2) == -1)
124 err = got_error_from_errno("asprintf");
132 const struct got_error *
133 got_object_open_loose_fd(int *fd, struct got_object_id *id,
134 struct got_repository *repo)
136 const struct got_error *err = NULL;
139 err = got_object_get_path(&path, id, repo);
142 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
144 err = got_error_from_errno2("open", path);
152 static const struct got_error *
153 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
154 struct got_object_id *id)
156 const struct got_error *err = NULL;
157 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
159 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
163 err = got_privsep_recv_obj(obj, ibuf);
167 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
172 /* Create temporary files used during delta application. */
173 static const struct got_error *
174 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
176 const struct got_error *err;
177 int basefd = -1, accumfd = -1;
180 * For performance reasons, the child will keep reusing the
181 * same temporary files during every object request.
182 * Opening and closing new files for every object request is
183 * too expensive during operations such as 'gotadmin pack'.
185 if (pack->child_has_tempfiles)
188 basefd = dup(pack->basefd);
190 return got_error_from_errno("dup");
192 accumfd = dup(pack->accumfd);
194 err = got_error_from_errno("dup");
198 err = got_privsep_send_tmpfd(ibuf, basefd);
202 err = got_privsep_send_tmpfd(ibuf, accumfd);
210 pack->child_has_tempfiles = 1;
214 static const struct got_error *
215 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
216 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
218 const struct got_error *err = NULL;
219 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
222 err = pack_child_send_tempfiles(ibuf, pack);
226 outfd_child = dup(outfd);
227 if (outfd_child == -1)
228 return got_error_from_errno("dup");
230 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
236 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
240 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
247 static const struct got_error *
248 read_packed_object_privsep(struct got_object **obj,
249 struct got_repository *repo, struct got_pack *pack,
250 struct got_packidx *packidx, int idx, struct got_object_id *id)
252 const struct got_error *err = NULL;
254 if (pack->privsep_child == NULL) {
255 err = got_pack_start_privsep_child(pack, packidx);
260 return request_packed_object(obj, pack, idx, id);
263 static const struct got_error *
264 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
265 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
266 struct got_object_id *id)
268 const struct got_error *err = NULL;
270 if (pack->privsep_child == NULL) {
271 err = got_pack_start_privsep_child(pack, packidx);
276 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
280 const struct got_error *
281 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
282 struct got_repository *repo)
284 const struct got_error *err = NULL;
285 struct got_pack *pack = NULL;
286 struct got_packidx *packidx = NULL;
290 err = got_repo_search_packidx(&packidx, &idx, repo, id);
294 err = got_packidx_get_packfile_path(&path_packfile,
295 packidx->path_packidx);
299 pack = got_repo_get_cached_pack(repo, path_packfile);
301 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
306 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
314 const struct got_error *
315 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
316 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
317 struct got_repository *repo)
319 return read_packed_object_privsep(obj, repo, pack, packidx,
323 const struct got_error *
324 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
325 off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
326 off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
327 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
328 struct got_repository *repo)
330 const struct got_error *err = NULL;
331 struct got_pack *pack = NULL;
337 *delta_compressed_size = 0;
339 *delta_out_offset = 0;
341 err = got_packidx_get_packfile_path(&path_packfile,
342 packidx->path_packidx);
346 pack = got_repo_get_cached_pack(repo, path_packfile);
348 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
353 if (pack->privsep_child == NULL) {
354 err = got_pack_start_privsep_child(pack, packidx);
359 if (!pack->child_has_delta_outfd) {
361 outfd_child = dup(delta_cache_fd);
362 if (outfd_child == -1)
363 return got_error_from_errno("dup");
364 err = got_privsep_send_raw_delta_outfd(
365 pack->privsep_child->ibuf, outfd_child);
368 pack->child_has_delta_outfd = 1;
371 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
376 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
377 delta_compressed_size, delta_offset, delta_out_offset, base_id,
378 pack->privsep_child->ibuf);
381 static const struct got_error *
382 request_object(struct got_object **obj, struct got_object_id *id,
383 struct got_repository *repo, int fd)
385 const struct got_error *err = NULL;
386 struct imsgbuf *ibuf;
388 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
390 err = got_privsep_send_obj_req(ibuf, fd, id);
394 return got_privsep_recv_obj(obj, ibuf);
397 static const struct got_error *
398 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
399 struct got_object_id *id, struct got_repository *repo, int infd)
401 const struct got_error *err = NULL;
402 struct imsgbuf *ibuf;
405 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
407 outfd_child = dup(outfd);
408 if (outfd_child == -1)
409 return got_error_from_errno("dup");
411 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
415 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
419 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
422 static const struct got_error *
423 start_read_object_child(struct got_repository *repo)
425 const struct got_error *err = NULL;
428 struct imsgbuf *ibuf;
430 ibuf = calloc(1, sizeof(*ibuf));
432 return got_error_from_errno("calloc");
434 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
435 err = got_error_from_errno("socketpair");
442 err = got_error_from_errno("fork");
447 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
452 if (close(imsg_fds[1]) == -1) {
453 err = got_error_from_errno("close");
458 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
460 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
461 imsg_init(ibuf, imsg_fds[0]);
462 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
467 const struct got_error *
468 got_object_read_header_privsep(struct got_object **obj,
469 struct got_object_id *id, struct got_repository *repo, int obj_fd)
471 const struct got_error *err;
473 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
474 return request_object(obj, id, repo, obj_fd);
476 err = start_read_object_child(repo);
482 return request_object(obj, id, repo, obj_fd);
485 static const struct got_error *
486 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
487 int outfd, struct got_object_id *id, struct got_repository *repo,
490 const struct got_error *err;
492 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
493 return request_raw_object(outbuf, size, hdrlen, outfd, id,
496 err = start_read_object_child(repo);
500 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
504 const struct got_error *
505 got_object_open(struct got_object **obj, struct got_repository *repo,
506 struct got_object_id *id)
508 const struct got_error *err = NULL;
511 *obj = got_repo_get_cached_object(repo, id);
517 err = got_object_open_packed(obj, id, repo);
518 if (err && err->code != GOT_ERR_NO_OBJ)
522 return got_repo_cache_object(repo, id, *obj);
525 err = got_object_open_loose_fd(&fd, id, repo);
527 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
528 err = got_error_no_obj(id);
532 err = got_object_read_header_privsep(obj, id, repo, fd);
536 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
539 return got_repo_cache_object(repo, id, *obj);
542 /* *outfd must be initialized to -1 by caller */
543 const struct got_error *
544 got_object_raw_open(struct got_raw_object **obj, int *outfd,
545 struct got_repository *repo, struct got_object_id *id)
547 const struct got_error *err = NULL;
548 struct got_packidx *packidx = NULL;
550 uint8_t *outbuf = NULL;
553 char *path_packfile = NULL;
555 *obj = got_repo_get_cached_raw_object(repo, id);
562 *outfd = got_opentempfd();
564 return got_error_from_errno("got_opentempfd");
567 err = got_repo_search_packidx(&packidx, &idx, repo, id);
569 struct got_pack *pack = NULL;
571 err = got_packidx_get_packfile_path(&path_packfile,
572 packidx->path_packidx);
576 pack = got_repo_get_cached_pack(repo, path_packfile);
578 err = got_repo_cache_pack(&pack, repo, path_packfile,
583 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
584 *outfd, pack, packidx, idx, id);
587 } else if (err->code == GOT_ERR_NO_OBJ) {
590 err = got_object_open_loose_fd(&fd, id, repo);
593 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
599 *obj = calloc(1, sizeof(**obj));
601 err = got_error_from_errno("calloc");
607 (*obj)->data = outbuf;
610 if (fstat(*outfd, &sb) == -1) {
611 err = got_error_from_errno("fstat");
615 if (sb.st_size != hdrlen + size) {
616 err = got_error(GOT_ERR_PRIVSEP_LEN);
619 #ifndef GOT_PACK_NO_MMAP
620 if (hdrlen + size > 0) {
621 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
622 MAP_PRIVATE, *outfd, 0);
623 if ((*obj)->data == MAP_FAILED) {
624 if (errno != ENOMEM) {
625 err = got_error_from_errno("mmap");
636 (*obj)->f = fdopen(*outfd, "r");
637 if ((*obj)->f == NULL) {
638 err = got_error_from_errno("fdopen");
644 (*obj)->hdrlen = hdrlen;
646 err = got_repo_cache_raw_object(repo, id, *obj);
651 got_object_raw_close(*obj);
660 const struct got_error *
661 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
664 struct got_object_id id;
666 if (!got_parse_sha1_digest(id.sha1, id_str))
667 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
669 return got_object_open(obj, repo, &id);
672 const struct got_error *
673 got_object_resolve_id_str(struct got_object_id **id,
674 struct got_repository *repo, const char *id_str)
676 const struct got_error *err = NULL;
677 struct got_object *obj;
679 err = got_object_open_by_id_str(&obj, repo, id_str);
683 *id = got_object_id_dup(got_object_get_id(obj));
684 got_object_close(obj);
686 return got_error_from_errno("got_object_id_dup");
691 static const struct got_error *
692 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
693 int pack_idx, struct got_object_id *id)
695 const struct got_error *err = NULL;
697 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
702 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
706 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
710 static const struct got_error *
711 read_packed_commit_privsep(struct got_commit_object **commit,
712 struct got_pack *pack, struct got_packidx *packidx, int idx,
713 struct got_object_id *id)
715 const struct got_error *err = NULL;
717 if (pack->privsep_child)
718 return request_packed_commit(commit, pack, idx, id);
720 err = got_pack_start_privsep_child(pack, packidx);
724 return request_packed_commit(commit, pack, idx, id);
727 static const struct got_error *
728 request_commit(struct got_commit_object **commit, struct got_repository *repo,
729 int fd, struct got_object_id *id)
731 const struct got_error *err = NULL;
732 struct imsgbuf *ibuf;
734 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
736 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
740 return got_privsep_recv_commit(commit, ibuf);
743 static const struct got_error *
744 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
745 struct got_object_id *id, struct got_repository *repo)
747 const struct got_error *err;
750 struct imsgbuf *ibuf;
752 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
753 return request_commit(commit, repo, obj_fd, id);
755 ibuf = calloc(1, sizeof(*ibuf));
757 return got_error_from_errno("calloc");
759 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
760 err = got_error_from_errno("socketpair");
767 err = got_error_from_errno("fork");
772 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
777 if (close(imsg_fds[1]) == -1) {
778 err = got_error_from_errno("close");
782 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
784 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
785 imsg_init(ibuf, imsg_fds[0]);
786 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
788 return request_commit(commit, repo, obj_fd, id);
792 static const struct got_error *
793 open_commit(struct got_commit_object **commit,
794 struct got_repository *repo, struct got_object_id *id, int check_cache)
796 const struct got_error *err = NULL;
797 struct got_packidx *packidx = NULL;
799 char *path_packfile = NULL;
802 *commit = got_repo_get_cached_commit(repo, id);
803 if (*commit != NULL) {
810 err = got_repo_search_packidx(&packidx, &idx, repo, id);
812 struct got_pack *pack = NULL;
814 err = got_packidx_get_packfile_path(&path_packfile,
815 packidx->path_packidx);
819 pack = got_repo_get_cached_pack(repo, path_packfile);
821 err = got_repo_cache_pack(&pack, repo, path_packfile,
826 err = read_packed_commit_privsep(commit, pack,
828 } else if (err->code == GOT_ERR_NO_OBJ) {
831 err = got_object_open_loose_fd(&fd, id, repo);
834 err = read_commit_privsep(commit, fd, id, repo);
839 err = got_repo_cache_commit(repo, id, *commit);
846 const struct got_error *
847 got_object_open_as_commit(struct got_commit_object **commit,
848 struct got_repository *repo, struct got_object_id *id)
850 *commit = got_repo_get_cached_commit(repo, id);
851 if (*commit != NULL) {
856 return open_commit(commit, repo, id, 0);
859 const struct got_error *
860 got_object_commit_open(struct got_commit_object **commit,
861 struct got_repository *repo, struct got_object *obj)
863 return open_commit(commit, repo, got_object_get_id(obj), 1);
866 const struct got_error *
867 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
869 *qid = calloc(1, sizeof(**qid));
871 return got_error_from_errno("calloc");
873 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
877 const struct got_error *
878 got_object_id_queue_copy(const struct got_object_id_queue *src,
879 struct got_object_id_queue *dest)
881 const struct got_error *err;
882 struct got_object_qid *qid;
884 STAILQ_FOREACH(qid, src, entry) {
885 struct got_object_qid *new;
887 * Deep-copy the object ID only. Let the caller deal
888 * with setting up the new->data pointer if needed.
890 err = got_object_qid_alloc(&new, &qid->id);
892 got_object_id_queue_free(dest);
895 STAILQ_INSERT_TAIL(dest, new, entry);
901 static const struct got_error *
902 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
903 int pack_idx, struct got_object_id *id)
905 const struct got_error *err = NULL;
907 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
912 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
915 static const struct got_error *
916 read_packed_tree_privsep(struct got_tree_object **tree,
917 struct got_pack *pack, struct got_packidx *packidx, int idx,
918 struct got_object_id *id)
920 const struct got_error *err = NULL;
922 if (pack->privsep_child)
923 return request_packed_tree(tree, pack, idx, id);
925 err = got_pack_start_privsep_child(pack, packidx);
929 return request_packed_tree(tree, pack, idx, id);
932 static const struct got_error *
933 request_tree(struct got_tree_object **tree, struct got_repository *repo,
934 int fd, struct got_object_id *id)
936 const struct got_error *err = NULL;
937 struct imsgbuf *ibuf;
939 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
941 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
945 return got_privsep_recv_tree(tree, ibuf);
948 static const struct got_error *
949 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
950 struct got_object_id *id, struct got_repository *repo)
952 const struct got_error *err;
955 struct imsgbuf *ibuf;
957 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
958 return request_tree(tree, repo, obj_fd, id);
960 ibuf = calloc(1, sizeof(*ibuf));
962 return got_error_from_errno("calloc");
964 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
965 err = got_error_from_errno("socketpair");
972 err = got_error_from_errno("fork");
977 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
982 if (close(imsg_fds[1]) == -1) {
983 err = got_error_from_errno("close");
987 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
989 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
990 imsg_init(ibuf, imsg_fds[0]);
991 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
994 return request_tree(tree, repo, obj_fd, id);
997 static const struct got_error *
998 open_tree(struct got_tree_object **tree, struct got_repository *repo,
999 struct got_object_id *id, int check_cache)
1001 const struct got_error *err = NULL;
1002 struct got_packidx *packidx = NULL;
1004 char *path_packfile = NULL;
1007 *tree = got_repo_get_cached_tree(repo, id);
1008 if (*tree != NULL) {
1015 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1017 struct got_pack *pack = NULL;
1019 err = got_packidx_get_packfile_path(&path_packfile,
1020 packidx->path_packidx);
1024 pack = got_repo_get_cached_pack(repo, path_packfile);
1026 err = got_repo_cache_pack(&pack, repo, path_packfile,
1031 err = read_packed_tree_privsep(tree, pack,
1033 } else if (err->code == GOT_ERR_NO_OBJ) {
1036 err = got_object_open_loose_fd(&fd, id, repo);
1039 err = read_tree_privsep(tree, fd, id, repo);
1044 err = got_repo_cache_tree(repo, id, *tree);
1047 free(path_packfile);
1051 const struct got_error *
1052 got_object_open_as_tree(struct got_tree_object **tree,
1053 struct got_repository *repo, struct got_object_id *id)
1055 *tree = got_repo_get_cached_tree(repo, id);
1056 if (*tree != NULL) {
1061 return open_tree(tree, repo, id, 0);
1064 const struct got_error *
1065 got_object_tree_open(struct got_tree_object **tree,
1066 struct got_repository *repo, struct got_object *obj)
1068 return open_tree(tree, repo, got_object_get_id(obj), 1);
1072 got_object_tree_get_nentries(struct got_tree_object *tree)
1074 return tree->nentries;
1077 struct got_tree_entry *
1078 got_object_tree_get_first_entry(struct got_tree_object *tree)
1080 return got_object_tree_get_entry(tree, 0);
1083 struct got_tree_entry *
1084 got_object_tree_get_last_entry(struct got_tree_object *tree)
1086 return got_object_tree_get_entry(tree, tree->nentries - 1);
1089 struct got_tree_entry *
1090 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1092 if (i < 0 || i >= tree->nentries)
1094 return &tree->entries[i];
1098 got_tree_entry_get_mode(struct got_tree_entry *te)
1104 got_tree_entry_get_name(struct got_tree_entry *te)
1106 return &te->name[0];
1109 struct got_object_id *
1110 got_tree_entry_get_id(struct got_tree_entry *te)
1115 const struct got_error *
1116 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1118 const struct got_error *err = NULL;
1119 size_t len, totlen, hdrlen, offset;
1123 hdrlen = got_object_blob_get_hdrlen(blob);
1129 err = got_object_blob_read_block(&len, blob);
1136 totlen += len - hdrlen;
1137 p = realloc(*s, totlen + 1);
1139 err = got_error_from_errno("realloc");
1145 /* Skip blob object header first time around. */
1147 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1152 (*s)[totlen] = '\0';
1156 const struct got_error *
1157 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1158 struct got_repository *repo)
1160 const struct got_error *err = NULL;
1161 struct got_blob_object *blob = NULL;
1164 *link_target = NULL;
1166 if (!got_object_tree_entry_is_symlink(te))
1167 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1169 fd = got_opentempfd();
1171 err = got_error_from_errno("got_opentempfd");
1175 err = got_object_open_as_blob(&blob, repo,
1176 got_tree_entry_get_id(te), PATH_MAX, fd);
1180 err = got_object_blob_read_to_str(link_target, blob);
1182 if (fd != -1 && close(fd) == -1 && err == NULL)
1183 err = got_error_from_errno("close");
1185 got_object_blob_close(blob);
1188 *link_target = NULL;
1194 got_tree_entry_get_index(struct got_tree_entry *te)
1199 struct got_tree_entry *
1200 got_tree_entry_get_next(struct got_tree_object *tree,
1201 struct got_tree_entry *te)
1203 return got_object_tree_get_entry(tree, te->idx + 1);
1206 struct got_tree_entry *
1207 got_tree_entry_get_prev(struct got_tree_object *tree,
1208 struct got_tree_entry *te)
1210 return got_object_tree_get_entry(tree, te->idx - 1);
1213 static const struct got_error *
1214 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1215 struct got_pack *pack, struct got_packidx *packidx, int idx,
1216 struct got_object_id *id)
1218 const struct got_error *err = NULL;
1219 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1222 err = pack_child_send_tempfiles(ibuf, pack);
1226 outfd_child = dup(outfd);
1227 if (outfd_child == -1)
1228 return got_error_from_errno("dup");
1230 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1234 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1240 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1241 pack->privsep_child->ibuf);
1245 if (lseek(outfd, SEEK_SET, 0) == -1)
1246 err = got_error_from_errno("lseek");
1251 static const struct got_error *
1252 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1253 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1254 struct got_object_id *id)
1256 const struct got_error *err = NULL;
1258 if (pack->privsep_child == NULL) {
1259 err = got_pack_start_privsep_child(pack, packidx);
1264 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1268 static const struct got_error *
1269 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1270 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1272 const struct got_error *err = NULL;
1275 outfd_child = dup(outfd);
1276 if (outfd_child == -1)
1277 return got_error_from_errno("dup");
1279 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1283 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1287 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1291 if (lseek(outfd, SEEK_SET, 0) == -1)
1292 return got_error_from_errno("lseek");
1297 static const struct got_error *
1298 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1299 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1301 const struct got_error *err;
1304 struct imsgbuf *ibuf;
1306 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1307 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1308 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1312 ibuf = calloc(1, sizeof(*ibuf));
1314 return got_error_from_errno("calloc");
1316 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1317 err = got_error_from_errno("socketpair");
1324 err = got_error_from_errno("fork");
1328 else if (pid == 0) {
1329 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1334 if (close(imsg_fds[1]) == -1) {
1335 err = got_error_from_errno("close");
1339 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1341 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1342 imsg_init(ibuf, imsg_fds[0]);
1343 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1345 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1348 static const struct got_error *
1349 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1350 struct got_object_id *id, size_t blocksize, int outfd)
1352 const struct got_error *err = NULL;
1353 struct got_packidx *packidx = NULL;
1355 char *path_packfile = NULL;
1357 size_t size, hdrlen;
1360 *blob = calloc(1, sizeof(**blob));
1362 return got_error_from_errno("calloc");
1364 (*blob)->read_buf = malloc(blocksize);
1365 if ((*blob)->read_buf == NULL) {
1366 err = got_error_from_errno("malloc");
1370 if (ftruncate(outfd, 0L) == -1) {
1371 err = got_error_from_errno("ftruncate");
1374 if (lseek(outfd, SEEK_SET, 0) == -1) {
1375 err = got_error_from_errno("lseek");
1379 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1381 struct got_pack *pack = NULL;
1383 err = got_packidx_get_packfile_path(&path_packfile,
1384 packidx->path_packidx);
1388 pack = got_repo_get_cached_pack(repo, path_packfile);
1390 err = got_repo_cache_pack(&pack, repo, path_packfile,
1395 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1396 pack, packidx, idx, id);
1397 } else if (err->code == GOT_ERR_NO_OBJ) {
1400 err = got_object_open_loose_fd(&infd, id, repo);
1403 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1409 if (hdrlen > size) {
1410 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1415 (*blob)->f = fmemopen(outbuf, size, "rb");
1416 if ((*blob)->f == NULL) {
1417 err = got_error_from_errno("fmemopen");
1421 (*blob)->data = outbuf;
1423 if (fstat(outfd, &sb) == -1) {
1424 err = got_error_from_errno("fstat");
1428 if (sb.st_size != size) {
1429 err = got_error(GOT_ERR_PRIVSEP_LEN);
1435 err = got_error_from_errno("dup");
1439 (*blob)->f = fdopen(dfd, "rb");
1440 if ((*blob)->f == NULL) {
1441 err = got_error_from_errno("fdopen");
1448 (*blob)->hdrlen = hdrlen;
1449 (*blob)->blocksize = blocksize;
1450 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1453 free(path_packfile);
1456 got_object_blob_close(*blob);
1463 const struct got_error *
1464 got_object_open_as_blob(struct got_blob_object **blob,
1465 struct got_repository *repo, struct got_object_id *id, size_t blocksize,
1468 return open_blob(blob, repo, id, blocksize, outfd);
1471 const struct got_error *
1472 got_object_blob_open(struct got_blob_object **blob,
1473 struct got_repository *repo, struct got_object *obj, size_t blocksize,
1476 return open_blob(blob, repo, got_object_get_id(obj), blocksize, outfd);
1479 const struct got_error *
1480 got_object_blob_close(struct got_blob_object *blob)
1482 const struct got_error *err = NULL;
1483 free(blob->read_buf);
1484 if (blob->f && fclose(blob->f) == EOF)
1485 err = got_error_from_errno("fclose");
1492 got_object_blob_rewind(struct got_blob_object *blob)
1499 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1501 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1505 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1507 return blob->hdrlen;
1511 got_object_blob_get_read_buf(struct got_blob_object *blob)
1513 return blob->read_buf;
1516 const struct got_error *
1517 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1521 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1522 if (n == 0 && ferror(blob->f))
1523 return got_ferror(blob->f, GOT_ERR_IO);
1528 const struct got_error *
1529 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1530 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1532 const struct got_error *err = NULL;
1533 size_t n, len, hdrlen;
1536 const int alloc_chunksz = 512;
1538 off_t off = 0, total_len = 0;
1541 *line_offsets = NULL;
1547 hdrlen = got_object_blob_get_hdrlen(blob);
1549 err = got_object_blob_read_block(&len, blob);
1554 buf = got_object_blob_get_read_buf(blob);
1557 if (line_offsets && *line_offsets == NULL) {
1558 /* Have some data but perhaps no '\n'. */
1560 nalloc = alloc_chunksz;
1561 *line_offsets = calloc(nalloc,
1562 sizeof(**line_offsets));
1563 if (*line_offsets == NULL)
1564 return got_error_from_errno("calloc");
1566 /* Skip forward over end of first line. */
1573 /* Scan '\n' offsets in remaining chunk of data. */
1575 if (buf[i] != '\n') {
1580 if (line_offsets && nalloc < *nlines) {
1581 size_t n = *nlines + alloc_chunksz;
1582 off_t *o = recallocarray(*line_offsets,
1583 nalloc, n, sizeof(**line_offsets));
1585 free(*line_offsets);
1586 *line_offsets = NULL;
1587 return got_error_from_errno(
1594 off = total_len + i - hdrlen + 1;
1595 (*line_offsets)[*nlines - 1] = off;
1600 /* Skip blob object header first time around. */
1601 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1602 if (n != len - hdrlen)
1603 return got_ferror(outfile, GOT_ERR_IO);
1604 total_len += len - hdrlen;
1608 if (fflush(outfile) != 0)
1609 return got_error_from_errno("fflush");
1613 *filesize = total_len;
1618 static const struct got_error *
1619 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1620 int pack_idx, struct got_object_id *id)
1622 const struct got_error *err = NULL;
1624 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1629 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1632 static const struct got_error *
1633 read_packed_tag_privsep(struct got_tag_object **tag,
1634 struct got_pack *pack, struct got_packidx *packidx, int idx,
1635 struct got_object_id *id)
1637 const struct got_error *err = NULL;
1639 if (pack->privsep_child)
1640 return request_packed_tag(tag, pack, idx, id);
1642 err = got_pack_start_privsep_child(pack, packidx);
1646 return request_packed_tag(tag, pack, idx, id);
1649 static const struct got_error *
1650 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1651 int fd, struct got_object_id *id)
1653 const struct got_error *err = NULL;
1654 struct imsgbuf *ibuf;
1656 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1658 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1662 return got_privsep_recv_tag(tag, ibuf);
1665 static const struct got_error *
1666 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1667 struct got_object_id *id, struct got_repository *repo)
1669 const struct got_error *err;
1672 struct imsgbuf *ibuf;
1674 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1675 return request_tag(tag, repo, obj_fd, id);
1677 ibuf = calloc(1, sizeof(*ibuf));
1679 return got_error_from_errno("calloc");
1681 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1682 err = got_error_from_errno("socketpair");
1689 err = got_error_from_errno("fork");
1693 else if (pid == 0) {
1694 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1699 if (close(imsg_fds[1]) == -1) {
1700 err = got_error_from_errno("close");
1704 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1706 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1707 imsg_init(ibuf, imsg_fds[0]);
1708 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1710 return request_tag(tag, repo, obj_fd, id);
1713 static const struct got_error *
1714 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1715 struct got_object_id *id, int check_cache)
1717 const struct got_error *err = NULL;
1718 struct got_packidx *packidx = NULL;
1720 char *path_packfile = NULL;
1721 struct got_object *obj = NULL;
1722 int obj_type = GOT_OBJ_TYPE_ANY;
1725 *tag = got_repo_get_cached_tag(repo, id);
1733 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1735 struct got_pack *pack = NULL;
1737 err = got_packidx_get_packfile_path(&path_packfile,
1738 packidx->path_packidx);
1742 pack = got_repo_get_cached_pack(repo, path_packfile);
1744 err = got_repo_cache_pack(&pack, repo, path_packfile,
1750 /* Beware of "lightweight" tags: Check object type first. */
1751 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1755 obj_type = obj->type;
1756 got_object_close(obj);
1757 if (obj_type != GOT_OBJ_TYPE_TAG) {
1758 err = got_error(GOT_ERR_OBJ_TYPE);
1761 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1762 } else if (err->code == GOT_ERR_NO_OBJ) {
1765 err = got_object_open_loose_fd(&fd, id, repo);
1768 err = got_object_read_header_privsep(&obj, id, repo, fd);
1771 obj_type = obj->type;
1772 got_object_close(obj);
1773 if (obj_type != GOT_OBJ_TYPE_TAG)
1774 return got_error(GOT_ERR_OBJ_TYPE);
1776 err = got_object_open_loose_fd(&fd, id, repo);
1779 err = read_tag_privsep(tag, fd, id, repo);
1784 err = got_repo_cache_tag(repo, id, *tag);
1787 free(path_packfile);
1791 const struct got_error *
1792 got_object_open_as_tag(struct got_tag_object **tag,
1793 struct got_repository *repo, struct got_object_id *id)
1795 *tag = got_repo_get_cached_tag(repo, id);
1801 return open_tag(tag, repo, id, 0);
1804 const struct got_error *
1805 got_object_tag_open(struct got_tag_object **tag,
1806 struct got_repository *repo, struct got_object *obj)
1808 return open_tag(tag, repo, got_object_get_id(obj), 1);
1812 got_object_tag_get_name(struct got_tag_object *tag)
1818 got_object_tag_get_object_type(struct got_tag_object *tag)
1820 return tag->obj_type;
1823 struct got_object_id *
1824 got_object_tag_get_object_id(struct got_tag_object *tag)
1830 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1832 return tag->tagger_time;
1836 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1838 return tag->tagger_gmtoff;
1842 got_object_tag_get_tagger(struct got_tag_object *tag)
1848 got_object_tag_get_message(struct got_tag_object *tag)
1853 static struct got_tree_entry *
1854 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1858 /* Note that tree entries are sorted in strncmp() order. */
1859 for (i = 0; i < tree->nentries; i++) {
1860 struct got_tree_entry *te = &tree->entries[i];
1861 int cmp = strncmp(te->name, name, len);
1866 if (te->name[len] == '\0')
1872 struct got_tree_entry *
1873 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1875 return find_entry_by_name(tree, name, strlen(name));
1878 const struct got_error *
1879 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1880 struct got_repository *repo, struct got_tree_object *tree,
1883 const struct got_error *err = NULL;
1884 struct got_tree_object *subtree = NULL;
1885 struct got_tree_entry *te = NULL;
1886 const char *seg, *s;
1898 struct got_tree_object *next_tree;
1907 te = find_entry_by_name(subtree, seg, seglen);
1909 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1920 err = got_object_open_as_tree(&next_tree, repo,
1925 if (subtree != tree)
1926 got_object_tree_close(subtree);
1927 subtree = next_tree;
1932 *id = got_object_id_dup(&te->id);
1934 return got_error_from_errno("got_object_id_dup");
1938 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1940 if (subtree && subtree != tree)
1941 got_object_tree_close(subtree);
1944 const struct got_error *
1945 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1946 struct got_commit_object *commit, const char *path)
1948 const struct got_error *err = NULL;
1949 struct got_tree_object *tree = NULL;
1953 /* Handle opening of root of commit's tree. */
1954 if (got_path_is_root_dir(path)) {
1955 *id = got_object_id_dup(commit->tree_id);
1957 err = got_error_from_errno("got_object_id_dup");
1959 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1962 err = got_object_tree_find_path(id, NULL, repo, tree, path);
1966 got_object_tree_close(tree);
1971 * Normalize file mode bits to avoid false positive tree entry differences
1972 * in case tree entries have unexpected mode bits set.
1975 normalize_mode_for_comparison(mode_t mode)
1978 * For directories, the only relevant bit is the IFDIR bit.
1979 * This allows us to detect paths changing from a directory
1980 * to a file and vice versa.
1983 return mode & S_IFDIR;
1986 * For symlinks, the only relevant bit is the IFLNK bit.
1987 * This allows us to detect paths changing from a symlinks
1988 * to a file or directory and vice versa.
1991 return mode & S_IFLNK;
1993 /* For files, the only change we care about is the executable bit. */
1994 return mode & S_IXUSR;
1997 const struct got_error *
1998 got_object_tree_path_changed(int *changed,
1999 struct got_tree_object *tree01, struct got_tree_object *tree02,
2000 const char *path, struct got_repository *repo)
2002 const struct got_error *err = NULL;
2003 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2004 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2005 const char *seg, *s;
2010 /* We not do support comparing the root path. */
2011 if (got_path_is_root_dir(path))
2012 return got_error_path(path, GOT_ERR_BAD_PATH);
2022 struct got_tree_object *next_tree1, *next_tree2;
2023 mode_t mode1, mode2;
2032 te1 = find_entry_by_name(tree1, seg, seglen);
2034 err = got_error(GOT_ERR_NO_OBJ);
2039 te2 = find_entry_by_name(tree2, seg, seglen);
2042 mode1 = normalize_mode_for_comparison(te1->mode);
2043 mode2 = normalize_mode_for_comparison(te2->mode);
2044 if (mode1 != mode2) {
2049 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2055 if (*s == '\0') { /* final path element */
2064 err = got_object_open_as_tree(&next_tree1, repo,
2069 if (tree1 != tree01)
2070 got_object_tree_close(tree1);
2074 err = got_object_open_as_tree(&next_tree2, repo,
2079 if (tree2 != tree02)
2080 got_object_tree_close(tree2);
2083 if (tree2 != tree02)
2084 got_object_tree_close(tree2);
2090 if (tree1 && tree1 != tree01)
2091 got_object_tree_close(tree1);
2092 if (tree2 && tree2 != tree02)
2093 got_object_tree_close(tree2);
2097 const struct got_error *
2098 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2099 struct got_tree_entry *te)
2101 const struct got_error *err = NULL;
2103 *new_te = calloc(1, sizeof(**new_te));
2104 if (*new_te == NULL)
2105 return got_error_from_errno("calloc");
2107 (*new_te)->mode = te->mode;
2108 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2109 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2114 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2116 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2120 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2122 /* S_IFDIR check avoids confusing symlinks with submodules. */
2123 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2126 static const struct got_error *
2127 resolve_symlink(char **link_target, const char *path,
2128 struct got_commit_object *commit, struct got_repository *repo)
2130 const struct got_error *err = NULL;
2132 char *name, *parent_path = NULL;
2133 struct got_object_id *tree_obj_id = NULL;
2134 struct got_tree_object *tree = NULL;
2135 struct got_tree_entry *te = NULL;
2137 *link_target = NULL;
2139 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2140 return got_error(GOT_ERR_NO_SPACE);
2142 name = basename(buf);
2144 return got_error_from_errno2("basename", path);
2146 err = got_path_dirname(&parent_path, path);
2150 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2153 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2154 /* Display the complete path in error message. */
2155 err = got_error_path(path, err->code);
2160 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2164 te = got_object_tree_find_entry(tree, name);
2166 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2170 if (got_object_tree_entry_is_symlink(te)) {
2171 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2174 if (!got_path_is_absolute(*link_target)) {
2176 if (asprintf(&abspath, "%s/%s", parent_path,
2177 *link_target) == -1) {
2178 err = got_error_from_errno("asprintf");
2182 *link_target = malloc(PATH_MAX);
2183 if (*link_target == NULL) {
2184 err = got_error_from_errno("malloc");
2187 err = got_canonpath(abspath, *link_target, PATH_MAX);
2196 got_object_tree_close(tree);
2199 *link_target = NULL;
2204 const struct got_error *
2205 got_object_resolve_symlinks(char **link_target, const char *path,
2206 struct got_commit_object *commit, struct got_repository *repo)
2208 const struct got_error *err = NULL;
2209 char *next_target = NULL;
2210 int max_recursion = 40; /* matches Git */
2212 *link_target = NULL;
2215 err = resolve_symlink(&next_target,
2216 *link_target ? *link_target : path, commit, repo);
2221 if (--max_recursion == 0) {
2222 err = got_error_path(path, GOT_ERR_RECURSION);
2223 *link_target = NULL;
2226 *link_target = next_target;
2228 } while (next_target);
2233 const struct got_error *
2234 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2235 struct got_object_id *commit_id, const char *path,
2236 struct got_repository *repo)
2238 const struct got_error *err = NULL;
2239 struct got_pack *pack = NULL;
2240 struct got_packidx *packidx = NULL;
2241 char *path_packfile = NULL;
2242 struct got_commit_object *changed_commit = NULL;
2243 struct got_object_id *changed_commit_id = NULL;
2246 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2248 if (err->code != GOT_ERR_NO_OBJ)
2253 err = got_packidx_get_packfile_path(&path_packfile,
2254 packidx->path_packidx);
2258 pack = got_repo_get_cached_pack(repo, path_packfile);
2260 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2265 if (pack->privsep_child == NULL) {
2266 err = got_pack_start_privsep_child(pack, packidx);
2271 err = got_privsep_send_commit_traversal_request(
2272 pack->privsep_child->ibuf, commit_id, idx, path);
2276 err = got_privsep_recv_traversed_commits(&changed_commit,
2277 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2281 if (changed_commit) {
2283 * Cache the commit in which the path was changed.
2284 * This commit might be opened again soon.
2286 changed_commit->refcnt++;
2287 err = got_repo_cache_commit(repo, changed_commit_id,
2289 got_object_commit_close(changed_commit);
2292 free(path_packfile);
2293 free(changed_commit_id);
2297 const struct got_error *
2298 got_object_enumerate(int *found_all_objects,
2299 got_object_enumerate_commit_cb cb_commit,
2300 got_object_enumerate_tree_cb cb_tree, void *cb_arg,
2301 struct got_object_id **ours, int nours,
2302 struct got_object_id **theirs, int ntheirs,
2303 struct got_packidx *packidx, struct got_repository *repo)
2305 const struct got_error *err = NULL;
2306 struct got_pack *pack;
2307 char *path_packfile = NULL;
2309 err = got_packidx_get_packfile_path(&path_packfile,
2310 packidx->path_packidx);
2314 pack = got_repo_get_cached_pack(repo, path_packfile);
2316 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2321 if (pack->privsep_child == NULL) {
2322 err = got_pack_start_privsep_child(pack, packidx);
2327 err = got_privsep_send_object_enumeration_request(
2328 pack->privsep_child->ibuf);
2332 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2336 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2340 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
2344 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
2348 err = got_privsep_recv_enumerated_objects(found_all_objects,
2349 pack->privsep_child->ibuf, cb_commit, cb_tree, cb_arg, repo);
2351 free(path_packfile);