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>
24 #include <sys/resource.h>
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_repository.h"
45 #include "got_opentemp.h"
48 #include "got_lib_sha1.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_object_idcache.h"
54 #include "got_lib_object_cache.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_repository.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 struct got_object_id *
64 got_object_get_id(struct got_object *obj)
69 const struct got_error *
70 got_object_get_id_str(char **outbuf, struct got_object *obj)
72 return got_object_id_str(outbuf, &obj->id);
75 const struct got_error *
76 got_object_get_type(int *type, struct got_repository *repo,
77 struct got_object_id *id)
79 const struct got_error *err = NULL;
80 struct got_object *obj;
82 err = got_object_open(&obj, repo, id);
87 case GOT_OBJ_TYPE_COMMIT:
88 case GOT_OBJ_TYPE_TREE:
89 case GOT_OBJ_TYPE_BLOB:
90 case GOT_OBJ_TYPE_TAG:
94 err = got_error(GOT_ERR_OBJ_TYPE);
98 got_object_close(obj);
102 const struct got_error *
103 got_object_get_path(char **path, struct got_object_id *id,
104 struct got_repository *repo)
106 const struct got_error *err = NULL;
112 path_objects = got_repo_get_path_objects(repo);
113 if (path_objects == NULL)
114 return got_error_from_errno("got_repo_get_path_objects");
116 err = got_object_id_str(&hex, id);
120 if (asprintf(path, "%s/%.2x/%s", path_objects,
121 id->sha1[0], hex + 2) == -1)
122 err = got_error_from_errno("asprintf");
130 const struct got_error *
131 got_object_open_loose_fd(int *fd, struct got_object_id *id,
132 struct got_repository *repo)
134 const struct got_error *err = NULL;
137 err = got_object_get_path(&path, id, repo);
140 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
142 err = got_error_from_errno2("open", path);
150 static const struct got_error *
151 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
152 struct got_object_id *id)
154 const struct got_error *err = NULL;
155 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
157 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
161 err = got_privsep_recv_obj(obj, ibuf);
165 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
170 /* Create temporary files used during delta application. */
171 static const struct got_error *
172 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
174 const struct got_error *err;
178 * For performance reasons, the child will keep reusing the
179 * same temporary files during every object request.
180 * Opening and closing new files for every object request is
181 * too expensive during operations such as 'gotadmin pack'.
183 if (pack->child_has_tempfiles)
186 basefd = got_opentempfd();
188 return got_error_from_errno("got_opentempfd");
190 err = got_privsep_send_tmpfd(ibuf, basefd);
194 accumfd = got_opentempfd();
196 return got_error_from_errno("got_opentempfd");
198 err = got_privsep_send_tmpfd(ibuf, accumfd);
202 pack->child_has_tempfiles = 1;
206 static const struct got_error *
207 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
208 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
210 const struct got_error *err = NULL;
211 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
214 err = pack_child_send_tempfiles(ibuf, pack);
218 outfd_child = dup(outfd);
219 if (outfd_child == -1)
220 return got_error_from_errno("dup");
222 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
228 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
232 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
240 set_max_datasize(void)
244 if (getrlimit(RLIMIT_DATA, &rl) != 0)
247 rl.rlim_cur = rl.rlim_max;
248 setrlimit(RLIMIT_DATA, &rl);
251 static const struct got_error *
252 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
254 const struct got_error *err = NULL;
257 struct imsgbuf *ibuf;
259 ibuf = calloc(1, sizeof(*ibuf));
261 return got_error_from_errno("calloc");
263 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
264 if (pack->privsep_child == NULL) {
265 err = got_error_from_errno("calloc");
269 pack->child_has_tempfiles = 0;
270 pack->child_has_delta_outfd = 0;
272 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
273 err = got_error_from_errno("socketpair");
279 err = got_error_from_errno("fork");
281 } else if (pid == 0) {
283 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
284 pack->path_packfile);
288 if (close(imsg_fds[1]) == -1)
289 return got_error_from_errno("close");
290 pack->privsep_child->imsg_fd = imsg_fds[0];
291 pack->privsep_child->pid = pid;
292 imsg_init(ibuf, imsg_fds[0]);
293 pack->privsep_child->ibuf = ibuf;
295 err = got_privsep_init_pack_child(ibuf, pack, packidx);
297 const struct got_error *child_err;
298 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
299 child_err = got_privsep_wait_for_child(
300 pack->privsep_child->pid);
301 if (child_err && err == NULL)
307 free(pack->privsep_child);
308 pack->privsep_child = NULL;
313 static const struct got_error *
314 read_packed_object_privsep(struct got_object **obj,
315 struct got_repository *repo, struct got_pack *pack,
316 struct got_packidx *packidx, int idx, struct got_object_id *id)
318 const struct got_error *err = NULL;
320 if (pack->privsep_child == NULL) {
321 err = start_pack_privsep_child(pack, packidx);
326 return request_packed_object(obj, pack, idx, id);
329 static const struct got_error *
330 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
331 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
332 struct got_object_id *id)
334 const struct got_error *err = NULL;
336 if (pack->privsep_child == NULL) {
337 err = start_pack_privsep_child(pack, packidx);
342 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
346 const struct got_error *
347 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
348 struct got_repository *repo)
350 const struct got_error *err = NULL;
351 struct got_pack *pack = NULL;
352 struct got_packidx *packidx = NULL;
356 err = got_repo_search_packidx(&packidx, &idx, repo, id);
360 err = got_packidx_get_packfile_path(&path_packfile,
361 packidx->path_packidx);
365 pack = got_repo_get_cached_pack(repo, path_packfile);
367 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
372 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
380 const struct got_error *
381 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
382 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
383 struct got_repository *repo)
385 return read_packed_object_privsep(obj, repo, pack, packidx,
389 const struct got_error *
390 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
391 off_t *delta_size, off_t *delta_offset, off_t *delta_out_offset,
392 struct got_object_id **base_id, int delta_cache_fd,
393 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
394 struct got_repository *repo)
396 const struct got_error *err = NULL;
397 struct got_pack *pack = NULL;
404 *delta_out_offset = 0;
406 err = got_packidx_get_packfile_path(&path_packfile,
407 packidx->path_packidx);
411 pack = got_repo_get_cached_pack(repo, path_packfile);
413 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
418 if (pack->privsep_child == NULL) {
419 err = start_pack_privsep_child(pack, packidx);
424 if (!pack->child_has_delta_outfd) {
426 outfd_child = dup(delta_cache_fd);
427 if (outfd_child == -1)
428 return got_error_from_errno("dup");
429 err = got_privsep_send_raw_delta_outfd(
430 pack->privsep_child->ibuf, outfd_child);
433 pack->child_has_delta_outfd = 1;
436 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
441 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
442 delta_offset, delta_out_offset, base_id, pack->privsep_child->ibuf);
445 static const struct got_error *
446 request_object(struct got_object **obj, struct got_object_id *id,
447 struct got_repository *repo, int fd)
449 const struct got_error *err = NULL;
450 struct imsgbuf *ibuf;
452 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
454 err = got_privsep_send_obj_req(ibuf, fd, id);
458 return got_privsep_recv_obj(obj, ibuf);
461 static const struct got_error *
462 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
463 struct got_object_id *id, struct got_repository *repo, int infd)
465 const struct got_error *err = NULL;
466 struct imsgbuf *ibuf;
469 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
471 outfd_child = dup(outfd);
472 if (outfd_child == -1)
473 return got_error_from_errno("dup");
475 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
479 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
483 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
486 static const struct got_error *
487 start_read_object_child(struct got_repository *repo)
489 const struct got_error *err = NULL;
492 struct imsgbuf *ibuf;
494 ibuf = calloc(1, sizeof(*ibuf));
496 return got_error_from_errno("calloc");
498 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
499 err = got_error_from_errno("socketpair");
506 err = got_error_from_errno("fork");
511 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
516 if (close(imsg_fds[1]) == -1) {
517 err = got_error_from_errno("close");
522 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
524 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
525 imsg_init(ibuf, imsg_fds[0]);
526 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
531 const struct got_error *
532 got_object_read_header_privsep(struct got_object **obj,
533 struct got_object_id *id, struct got_repository *repo, int obj_fd)
535 const struct got_error *err;
537 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
538 return request_object(obj, id, repo, obj_fd);
540 err = start_read_object_child(repo);
546 return request_object(obj, id, repo, obj_fd);
549 static const struct got_error *
550 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
551 int outfd, struct got_object_id *id, struct got_repository *repo,
554 const struct got_error *err;
556 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
557 return request_raw_object(outbuf, size, hdrlen, outfd, id,
560 err = start_read_object_child(repo);
564 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
568 const struct got_error *
569 got_object_open(struct got_object **obj, struct got_repository *repo,
570 struct got_object_id *id)
572 const struct got_error *err = NULL;
575 *obj = got_repo_get_cached_object(repo, id);
581 err = got_object_open_packed(obj, id, repo);
582 if (err && err->code != GOT_ERR_NO_OBJ)
586 return got_repo_cache_object(repo, id, *obj);
589 err = got_object_open_loose_fd(&fd, id, repo);
591 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
592 err = got_error_no_obj(id);
596 err = got_object_read_header_privsep(obj, id, repo, fd);
600 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
603 return got_repo_cache_object(repo, id, *obj);
606 /* *outfd must be initialized to -1 by caller */
607 const struct got_error *
608 got_object_raw_open(struct got_raw_object **obj, int *outfd,
609 struct got_repository *repo, struct got_object_id *id)
611 const struct got_error *err = NULL;
612 struct got_packidx *packidx = NULL;
614 uint8_t *outbuf = NULL;
617 char *path_packfile = NULL;
619 *obj = got_repo_get_cached_raw_object(repo, id);
626 *outfd = got_opentempfd();
628 return got_error_from_errno("got_opentempfd");
631 err = got_repo_search_packidx(&packidx, &idx, repo, id);
633 struct got_pack *pack = NULL;
635 err = got_packidx_get_packfile_path(&path_packfile,
636 packidx->path_packidx);
640 pack = got_repo_get_cached_pack(repo, path_packfile);
642 err = got_repo_cache_pack(&pack, repo, path_packfile,
647 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
648 *outfd, pack, packidx, idx, id);
651 } else if (err->code == GOT_ERR_NO_OBJ) {
654 err = got_object_open_loose_fd(&fd, id, repo);
657 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
663 *obj = calloc(1, sizeof(**obj));
665 err = got_error_from_errno("calloc");
671 (*obj)->data = outbuf;
674 if (fstat(*outfd, &sb) == -1) {
675 err = got_error_from_errno("fstat");
679 if (sb.st_size != hdrlen + size) {
680 err = got_error(GOT_ERR_PRIVSEP_LEN);
683 #ifndef GOT_PACK_NO_MMAP
684 if (hdrlen + size > 0) {
685 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
686 MAP_PRIVATE, *outfd, 0);
687 if ((*obj)->data == MAP_FAILED) {
688 if (errno != ENOMEM) {
689 err = got_error_from_errno("mmap");
700 (*obj)->f = fdopen(*outfd, "r");
701 if ((*obj)->f == NULL) {
702 err = got_error_from_errno("fdopen");
708 (*obj)->hdrlen = hdrlen;
710 err = got_repo_cache_raw_object(repo, id, *obj);
715 got_object_raw_close(*obj);
724 const struct got_error *
725 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
728 struct got_object_id id;
730 if (!got_parse_sha1_digest(id.sha1, id_str))
731 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
733 return got_object_open(obj, repo, &id);
736 const struct got_error *
737 got_object_resolve_id_str(struct got_object_id **id,
738 struct got_repository *repo, const char *id_str)
740 const struct got_error *err = NULL;
741 struct got_object *obj;
743 err = got_object_open_by_id_str(&obj, repo, id_str);
747 *id = got_object_id_dup(got_object_get_id(obj));
748 got_object_close(obj);
750 return got_error_from_errno("got_object_id_dup");
755 static const struct got_error *
756 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
757 int pack_idx, struct got_object_id *id)
759 const struct got_error *err = NULL;
761 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
766 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
770 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
774 static const struct got_error *
775 read_packed_commit_privsep(struct got_commit_object **commit,
776 struct got_pack *pack, struct got_packidx *packidx, int idx,
777 struct got_object_id *id)
779 const struct got_error *err = NULL;
781 if (pack->privsep_child)
782 return request_packed_commit(commit, pack, idx, id);
784 err = start_pack_privsep_child(pack, packidx);
788 return request_packed_commit(commit, pack, idx, id);
791 static const struct got_error *
792 request_commit(struct got_commit_object **commit, struct got_repository *repo,
793 int fd, struct got_object_id *id)
795 const struct got_error *err = NULL;
796 struct imsgbuf *ibuf;
798 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
800 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
804 return got_privsep_recv_commit(commit, ibuf);
807 static const struct got_error *
808 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
809 struct got_object_id *id, struct got_repository *repo)
811 const struct got_error *err;
814 struct imsgbuf *ibuf;
816 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
817 return request_commit(commit, repo, obj_fd, id);
819 ibuf = calloc(1, sizeof(*ibuf));
821 return got_error_from_errno("calloc");
823 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
824 err = got_error_from_errno("socketpair");
831 err = got_error_from_errno("fork");
836 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
841 if (close(imsg_fds[1]) == -1) {
842 err = got_error_from_errno("close");
846 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
848 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
849 imsg_init(ibuf, imsg_fds[0]);
850 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
852 return request_commit(commit, repo, obj_fd, id);
856 static const struct got_error *
857 open_commit(struct got_commit_object **commit,
858 struct got_repository *repo, struct got_object_id *id, int check_cache)
860 const struct got_error *err = NULL;
861 struct got_packidx *packidx = NULL;
863 char *path_packfile = NULL;
866 *commit = got_repo_get_cached_commit(repo, id);
867 if (*commit != NULL) {
874 err = got_repo_search_packidx(&packidx, &idx, repo, id);
876 struct got_pack *pack = NULL;
878 err = got_packidx_get_packfile_path(&path_packfile,
879 packidx->path_packidx);
883 pack = got_repo_get_cached_pack(repo, path_packfile);
885 err = got_repo_cache_pack(&pack, repo, path_packfile,
890 err = read_packed_commit_privsep(commit, pack,
892 } else if (err->code == GOT_ERR_NO_OBJ) {
895 err = got_object_open_loose_fd(&fd, id, repo);
898 err = read_commit_privsep(commit, fd, id, repo);
903 err = got_repo_cache_commit(repo, id, *commit);
910 const struct got_error *
911 got_object_open_as_commit(struct got_commit_object **commit,
912 struct got_repository *repo, struct got_object_id *id)
914 *commit = got_repo_get_cached_commit(repo, id);
915 if (*commit != NULL) {
920 return open_commit(commit, repo, id, 0);
923 const struct got_error *
924 got_object_commit_open(struct got_commit_object **commit,
925 struct got_repository *repo, struct got_object *obj)
927 return open_commit(commit, repo, got_object_get_id(obj), 1);
930 const struct got_error *
931 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
933 *qid = calloc(1, sizeof(**qid));
935 return got_error_from_errno("calloc");
937 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
941 const struct got_error *
942 got_object_id_queue_copy(const struct got_object_id_queue *src,
943 struct got_object_id_queue *dest)
945 const struct got_error *err;
946 struct got_object_qid *qid;
948 STAILQ_FOREACH(qid, src, entry) {
949 struct got_object_qid *new;
951 * Deep-copy the object ID only. Let the caller deal
952 * with setting up the new->data pointer if needed.
954 err = got_object_qid_alloc(&new, &qid->id);
956 got_object_id_queue_free(dest);
959 STAILQ_INSERT_TAIL(dest, new, entry);
965 static const struct got_error *
966 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
967 int pack_idx, struct got_object_id *id)
969 const struct got_error *err = NULL;
971 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
976 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
979 static const struct got_error *
980 read_packed_tree_privsep(struct got_tree_object **tree,
981 struct got_pack *pack, struct got_packidx *packidx, int idx,
982 struct got_object_id *id)
984 const struct got_error *err = NULL;
986 if (pack->privsep_child)
987 return request_packed_tree(tree, pack, idx, id);
989 err = start_pack_privsep_child(pack, packidx);
993 return request_packed_tree(tree, pack, idx, id);
996 static const struct got_error *
997 request_tree(struct got_tree_object **tree, struct got_repository *repo,
998 int fd, struct got_object_id *id)
1000 const struct got_error *err = NULL;
1001 struct imsgbuf *ibuf;
1003 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1005 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1009 return got_privsep_recv_tree(tree, ibuf);
1012 const struct got_error *
1013 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1014 struct got_object_id *id, struct got_repository *repo)
1016 const struct got_error *err;
1019 struct imsgbuf *ibuf;
1021 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1022 return request_tree(tree, repo, obj_fd, id);
1024 ibuf = calloc(1, sizeof(*ibuf));
1026 return got_error_from_errno("calloc");
1028 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1029 err = got_error_from_errno("socketpair");
1036 err = got_error_from_errno("fork");
1040 else if (pid == 0) {
1041 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1046 if (close(imsg_fds[1]) == -1) {
1047 err = got_error_from_errno("close");
1051 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1053 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1054 imsg_init(ibuf, imsg_fds[0]);
1055 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1058 return request_tree(tree, repo, obj_fd, id);
1061 static const struct got_error *
1062 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1063 struct got_object_id *id, int check_cache)
1065 const struct got_error *err = NULL;
1066 struct got_packidx *packidx = NULL;
1068 char *path_packfile = NULL;
1071 *tree = got_repo_get_cached_tree(repo, id);
1072 if (*tree != NULL) {
1079 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1081 struct got_pack *pack = NULL;
1083 err = got_packidx_get_packfile_path(&path_packfile,
1084 packidx->path_packidx);
1088 pack = got_repo_get_cached_pack(repo, path_packfile);
1090 err = got_repo_cache_pack(&pack, repo, path_packfile,
1095 err = read_packed_tree_privsep(tree, pack,
1097 } else if (err->code == GOT_ERR_NO_OBJ) {
1100 err = got_object_open_loose_fd(&fd, id, repo);
1103 err = read_tree_privsep(tree, fd, id, repo);
1108 err = got_repo_cache_tree(repo, id, *tree);
1111 free(path_packfile);
1115 const struct got_error *
1116 got_object_open_as_tree(struct got_tree_object **tree,
1117 struct got_repository *repo, struct got_object_id *id)
1119 *tree = got_repo_get_cached_tree(repo, id);
1120 if (*tree != NULL) {
1125 return open_tree(tree, repo, id, 0);
1128 const struct got_error *
1129 got_object_tree_open(struct got_tree_object **tree,
1130 struct got_repository *repo, struct got_object *obj)
1132 return open_tree(tree, repo, got_object_get_id(obj), 1);
1136 got_object_tree_get_nentries(struct got_tree_object *tree)
1138 return tree->nentries;
1141 struct got_tree_entry *
1142 got_object_tree_get_first_entry(struct got_tree_object *tree)
1144 return got_object_tree_get_entry(tree, 0);
1147 struct got_tree_entry *
1148 got_object_tree_get_last_entry(struct got_tree_object *tree)
1150 return got_object_tree_get_entry(tree, tree->nentries - 1);
1153 struct got_tree_entry *
1154 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1156 if (i < 0 || i >= tree->nentries)
1158 return &tree->entries[i];
1162 got_tree_entry_get_mode(struct got_tree_entry *te)
1168 got_tree_entry_get_name(struct got_tree_entry *te)
1170 return &te->name[0];
1173 struct got_object_id *
1174 got_tree_entry_get_id(struct got_tree_entry *te)
1179 const struct got_error *
1180 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1182 const struct got_error *err = NULL;
1183 size_t len, totlen, hdrlen, offset;
1187 hdrlen = got_object_blob_get_hdrlen(blob);
1193 err = got_object_blob_read_block(&len, blob);
1200 totlen += len - hdrlen;
1201 p = realloc(*s, totlen + 1);
1203 err = got_error_from_errno("realloc");
1209 /* Skip blob object header first time around. */
1211 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1216 (*s)[totlen] = '\0';
1220 const struct got_error *
1221 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1222 struct got_repository *repo)
1224 const struct got_error *err = NULL;
1225 struct got_blob_object *blob = NULL;
1227 *link_target = NULL;
1229 if (!got_object_tree_entry_is_symlink(te))
1230 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1232 err = got_object_open_as_blob(&blob, repo,
1233 got_tree_entry_get_id(te), PATH_MAX);
1237 err = got_object_blob_read_to_str(link_target, blob);
1238 got_object_blob_close(blob);
1241 *link_target = NULL;
1247 got_tree_entry_get_index(struct got_tree_entry *te)
1252 struct got_tree_entry *
1253 got_tree_entry_get_next(struct got_tree_object *tree,
1254 struct got_tree_entry *te)
1256 return got_object_tree_get_entry(tree, te->idx + 1);
1259 struct got_tree_entry *
1260 got_tree_entry_get_prev(struct got_tree_object *tree,
1261 struct got_tree_entry *te)
1263 return got_object_tree_get_entry(tree, te->idx - 1);
1266 static const struct got_error *
1267 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1268 struct got_pack *pack, struct got_packidx *packidx, int idx,
1269 struct got_object_id *id)
1271 const struct got_error *err = NULL;
1272 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1275 err = pack_child_send_tempfiles(ibuf, pack);
1279 outfd_child = dup(outfd);
1280 if (outfd_child == -1)
1281 return got_error_from_errno("dup");
1283 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1287 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1293 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1294 pack->privsep_child->ibuf);
1298 if (lseek(outfd, SEEK_SET, 0) == -1)
1299 err = got_error_from_errno("lseek");
1304 static const struct got_error *
1305 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1306 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1307 struct got_object_id *id)
1309 const struct got_error *err = NULL;
1311 if (pack->privsep_child == NULL) {
1312 err = start_pack_privsep_child(pack, packidx);
1317 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1321 static const struct got_error *
1322 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1323 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1325 const struct got_error *err = NULL;
1328 outfd_child = dup(outfd);
1329 if (outfd_child == -1)
1330 return got_error_from_errno("dup");
1332 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1336 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1340 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1344 if (lseek(outfd, SEEK_SET, 0) == -1)
1345 return got_error_from_errno("lseek");
1350 static const struct got_error *
1351 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1352 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1354 const struct got_error *err;
1357 struct imsgbuf *ibuf;
1359 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1360 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1361 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1365 ibuf = calloc(1, sizeof(*ibuf));
1367 return got_error_from_errno("calloc");
1369 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1370 err = got_error_from_errno("socketpair");
1377 err = got_error_from_errno("fork");
1381 else if (pid == 0) {
1382 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1387 if (close(imsg_fds[1]) == -1) {
1388 err = got_error_from_errno("close");
1392 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1394 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1395 imsg_init(ibuf, imsg_fds[0]);
1396 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1398 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1401 static const struct got_error *
1402 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1403 struct got_object_id *id, size_t blocksize)
1405 const struct got_error *err = NULL;
1406 struct got_packidx *packidx = NULL;
1408 char *path_packfile = NULL;
1411 size_t size, hdrlen;
1414 *blob = calloc(1, sizeof(**blob));
1416 return got_error_from_errno("calloc");
1418 outfd = got_opentempfd();
1420 return got_error_from_errno("got_opentempfd");
1422 (*blob)->read_buf = malloc(blocksize);
1423 if ((*blob)->read_buf == NULL) {
1424 err = got_error_from_errno("malloc");
1428 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1430 struct got_pack *pack = NULL;
1432 err = got_packidx_get_packfile_path(&path_packfile,
1433 packidx->path_packidx);
1437 pack = got_repo_get_cached_pack(repo, path_packfile);
1439 err = got_repo_cache_pack(&pack, repo, path_packfile,
1444 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1445 pack, packidx, idx, id);
1446 } else if (err->code == GOT_ERR_NO_OBJ) {
1449 err = got_object_open_loose_fd(&infd, id, repo);
1452 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1458 if (hdrlen > size) {
1459 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1464 if (close(outfd) == -1 && err == NULL)
1465 err = got_error_from_errno("close");
1467 (*blob)->f = fmemopen(outbuf, size, "rb");
1468 if ((*blob)->f == NULL) {
1469 err = got_error_from_errno("fmemopen");
1473 (*blob)->data = outbuf;
1475 if (fstat(outfd, &sb) == -1) {
1476 err = got_error_from_errno("fstat");
1480 if (sb.st_size != size) {
1481 err = got_error(GOT_ERR_PRIVSEP_LEN);
1485 (*blob)->f = fdopen(outfd, "rb");
1486 if ((*blob)->f == NULL) {
1487 err = got_error_from_errno("fdopen");
1494 (*blob)->hdrlen = hdrlen;
1495 (*blob)->blocksize = blocksize;
1496 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1499 free(path_packfile);
1502 got_object_blob_close(*blob);
1504 } else if (outfd != -1)
1510 const struct got_error *
1511 got_object_open_as_blob(struct got_blob_object **blob,
1512 struct got_repository *repo, struct got_object_id *id,
1515 return open_blob(blob, repo, id, blocksize);
1518 const struct got_error *
1519 got_object_blob_open(struct got_blob_object **blob,
1520 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1522 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1525 const struct got_error *
1526 got_object_blob_close(struct got_blob_object *blob)
1528 const struct got_error *err = NULL;
1529 free(blob->read_buf);
1530 if (blob->f && fclose(blob->f) == EOF)
1531 err = got_error_from_errno("fclose");
1538 got_object_blob_rewind(struct got_blob_object *blob)
1545 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1547 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1551 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1553 return blob->hdrlen;
1557 got_object_blob_get_read_buf(struct got_blob_object *blob)
1559 return blob->read_buf;
1562 const struct got_error *
1563 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1567 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1568 if (n == 0 && ferror(blob->f))
1569 return got_ferror(blob->f, GOT_ERR_IO);
1574 const struct got_error *
1575 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1576 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1578 const struct got_error *err = NULL;
1579 size_t n, len, hdrlen;
1582 const int alloc_chunksz = 512;
1584 off_t off = 0, total_len = 0;
1587 *line_offsets = NULL;
1593 hdrlen = got_object_blob_get_hdrlen(blob);
1595 err = got_object_blob_read_block(&len, blob);
1600 buf = got_object_blob_get_read_buf(blob);
1603 if (line_offsets && *line_offsets == NULL) {
1604 /* Have some data but perhaps no '\n'. */
1606 nalloc = alloc_chunksz;
1607 *line_offsets = calloc(nalloc,
1608 sizeof(**line_offsets));
1609 if (*line_offsets == NULL)
1610 return got_error_from_errno("calloc");
1612 /* Skip forward over end of first line. */
1619 /* Scan '\n' offsets in remaining chunk of data. */
1621 if (buf[i] != '\n') {
1626 if (line_offsets && nalloc < *nlines) {
1627 size_t n = *nlines + alloc_chunksz;
1628 off_t *o = recallocarray(*line_offsets,
1629 nalloc, n, sizeof(**line_offsets));
1631 free(*line_offsets);
1632 *line_offsets = NULL;
1633 return got_error_from_errno(
1640 off = total_len + i - hdrlen + 1;
1641 (*line_offsets)[*nlines - 1] = off;
1646 /* Skip blob object header first time around. */
1647 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1648 if (n != len - hdrlen)
1649 return got_ferror(outfile, GOT_ERR_IO);
1650 total_len += len - hdrlen;
1654 if (fflush(outfile) != 0)
1655 return got_error_from_errno("fflush");
1659 *filesize = total_len;
1664 static const struct got_error *
1665 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1666 int pack_idx, struct got_object_id *id)
1668 const struct got_error *err = NULL;
1670 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1675 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1678 static const struct got_error *
1679 read_packed_tag_privsep(struct got_tag_object **tag,
1680 struct got_pack *pack, struct got_packidx *packidx, int idx,
1681 struct got_object_id *id)
1683 const struct got_error *err = NULL;
1685 if (pack->privsep_child)
1686 return request_packed_tag(tag, pack, idx, id);
1688 err = start_pack_privsep_child(pack, packidx);
1692 return request_packed_tag(tag, pack, idx, id);
1695 static const struct got_error *
1696 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1697 int fd, struct got_object_id *id)
1699 const struct got_error *err = NULL;
1700 struct imsgbuf *ibuf;
1702 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1704 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1708 return got_privsep_recv_tag(tag, ibuf);
1711 static const struct got_error *
1712 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1713 struct got_object_id *id, struct got_repository *repo)
1715 const struct got_error *err;
1718 struct imsgbuf *ibuf;
1720 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1721 return request_tag(tag, repo, obj_fd, id);
1723 ibuf = calloc(1, sizeof(*ibuf));
1725 return got_error_from_errno("calloc");
1727 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1728 err = got_error_from_errno("socketpair");
1735 err = got_error_from_errno("fork");
1739 else if (pid == 0) {
1740 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1745 if (close(imsg_fds[1]) == -1) {
1746 err = got_error_from_errno("close");
1750 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1752 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1753 imsg_init(ibuf, imsg_fds[0]);
1754 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1756 return request_tag(tag, repo, obj_fd, id);
1759 static const struct got_error *
1760 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1761 struct got_object_id *id, int check_cache)
1763 const struct got_error *err = NULL;
1764 struct got_packidx *packidx = NULL;
1766 char *path_packfile = NULL;
1767 struct got_object *obj = NULL;
1768 int obj_type = GOT_OBJ_TYPE_ANY;
1771 *tag = got_repo_get_cached_tag(repo, id);
1779 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1781 struct got_pack *pack = NULL;
1783 err = got_packidx_get_packfile_path(&path_packfile,
1784 packidx->path_packidx);
1788 pack = got_repo_get_cached_pack(repo, path_packfile);
1790 err = got_repo_cache_pack(&pack, repo, path_packfile,
1796 /* Beware of "lightweight" tags: Check object type first. */
1797 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1801 obj_type = obj->type;
1802 got_object_close(obj);
1803 if (obj_type != GOT_OBJ_TYPE_TAG) {
1804 err = got_error(GOT_ERR_OBJ_TYPE);
1807 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1808 } else if (err->code == GOT_ERR_NO_OBJ) {
1811 err = got_object_open_loose_fd(&fd, id, repo);
1814 err = got_object_read_header_privsep(&obj, id, repo, fd);
1817 obj_type = obj->type;
1818 got_object_close(obj);
1819 if (obj_type != GOT_OBJ_TYPE_TAG)
1820 return got_error(GOT_ERR_OBJ_TYPE);
1822 err = got_object_open_loose_fd(&fd, id, repo);
1825 err = read_tag_privsep(tag, fd, id, repo);
1830 err = got_repo_cache_tag(repo, id, *tag);
1833 free(path_packfile);
1837 const struct got_error *
1838 got_object_open_as_tag(struct got_tag_object **tag,
1839 struct got_repository *repo, struct got_object_id *id)
1841 *tag = got_repo_get_cached_tag(repo, id);
1847 return open_tag(tag, repo, id, 0);
1850 const struct got_error *
1851 got_object_tag_open(struct got_tag_object **tag,
1852 struct got_repository *repo, struct got_object *obj)
1854 return open_tag(tag, repo, got_object_get_id(obj), 1);
1858 got_object_tag_get_name(struct got_tag_object *tag)
1864 got_object_tag_get_object_type(struct got_tag_object *tag)
1866 return tag->obj_type;
1869 struct got_object_id *
1870 got_object_tag_get_object_id(struct got_tag_object *tag)
1876 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1878 return tag->tagger_time;
1882 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1884 return tag->tagger_gmtoff;
1888 got_object_tag_get_tagger(struct got_tag_object *tag)
1894 got_object_tag_get_message(struct got_tag_object *tag)
1899 static struct got_tree_entry *
1900 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1904 /* Note that tree entries are sorted in strncmp() order. */
1905 for (i = 0; i < tree->nentries; i++) {
1906 struct got_tree_entry *te = &tree->entries[i];
1907 int cmp = strncmp(te->name, name, len);
1912 if (te->name[len] == '\0')
1918 struct got_tree_entry *
1919 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1921 return find_entry_by_name(tree, name, strlen(name));
1924 const struct got_error *
1925 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1926 struct got_repository *repo, struct got_tree_object *tree,
1929 const struct got_error *err = NULL;
1930 struct got_tree_object *subtree = NULL;
1931 struct got_tree_entry *te = NULL;
1932 const char *seg, *s;
1944 struct got_tree_object *next_tree;
1953 te = find_entry_by_name(subtree, seg, seglen);
1955 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1966 err = got_object_open_as_tree(&next_tree, repo,
1971 if (subtree != tree)
1972 got_object_tree_close(subtree);
1973 subtree = next_tree;
1978 *id = got_object_id_dup(&te->id);
1980 return got_error_from_errno("got_object_id_dup");
1984 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1986 if (subtree && subtree != tree)
1987 got_object_tree_close(subtree);
1990 const struct got_error *
1991 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1992 struct got_commit_object *commit, const char *path)
1994 const struct got_error *err = NULL;
1995 struct got_tree_object *tree = NULL;
1999 /* Handle opening of root of commit's tree. */
2000 if (got_path_is_root_dir(path)) {
2001 *id = got_object_id_dup(commit->tree_id);
2003 err = got_error_from_errno("got_object_id_dup");
2005 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2008 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2012 got_object_tree_close(tree);
2017 * Normalize file mode bits to avoid false positive tree entry differences
2018 * in case tree entries have unexpected mode bits set.
2021 normalize_mode_for_comparison(mode_t mode)
2024 * For directories, the only relevant bit is the IFDIR bit.
2025 * This allows us to detect paths changing from a directory
2026 * to a file and vice versa.
2029 return mode & S_IFDIR;
2032 * For symlinks, the only relevant bit is the IFLNK bit.
2033 * This allows us to detect paths changing from a symlinks
2034 * to a file or directory and vice versa.
2037 return mode & S_IFLNK;
2039 /* For files, the only change we care about is the executable bit. */
2040 return mode & S_IXUSR;
2043 const struct got_error *
2044 got_object_tree_path_changed(int *changed,
2045 struct got_tree_object *tree01, struct got_tree_object *tree02,
2046 const char *path, struct got_repository *repo)
2048 const struct got_error *err = NULL;
2049 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2050 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2051 const char *seg, *s;
2056 /* We not do support comparing the root path. */
2057 if (got_path_is_root_dir(path))
2058 return got_error_path(path, GOT_ERR_BAD_PATH);
2068 struct got_tree_object *next_tree1, *next_tree2;
2069 mode_t mode1, mode2;
2078 te1 = find_entry_by_name(tree1, seg, seglen);
2080 err = got_error(GOT_ERR_NO_OBJ);
2085 te2 = find_entry_by_name(tree2, seg, seglen);
2088 mode1 = normalize_mode_for_comparison(te1->mode);
2089 mode2 = normalize_mode_for_comparison(te2->mode);
2090 if (mode1 != mode2) {
2095 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2101 if (*s == '\0') { /* final path element */
2110 err = got_object_open_as_tree(&next_tree1, repo,
2115 if (tree1 != tree01)
2116 got_object_tree_close(tree1);
2120 err = got_object_open_as_tree(&next_tree2, repo,
2125 if (tree2 != tree02)
2126 got_object_tree_close(tree2);
2129 if (tree2 != tree02)
2130 got_object_tree_close(tree2);
2136 if (tree1 && tree1 != tree01)
2137 got_object_tree_close(tree1);
2138 if (tree2 && tree2 != tree02)
2139 got_object_tree_close(tree2);
2143 const struct got_error *
2144 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2145 struct got_tree_entry *te)
2147 const struct got_error *err = NULL;
2149 *new_te = calloc(1, sizeof(**new_te));
2150 if (*new_te == NULL)
2151 return got_error_from_errno("calloc");
2153 (*new_te)->mode = te->mode;
2154 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2155 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2160 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2162 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2166 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2168 /* S_IFDIR check avoids confusing symlinks with submodules. */
2169 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2172 static const struct got_error *
2173 resolve_symlink(char **link_target, const char *path,
2174 struct got_commit_object *commit, struct got_repository *repo)
2176 const struct got_error *err = NULL;
2178 char *name, *parent_path = NULL;
2179 struct got_object_id *tree_obj_id = NULL;
2180 struct got_tree_object *tree = NULL;
2181 struct got_tree_entry *te = NULL;
2183 *link_target = NULL;
2185 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2186 return got_error(GOT_ERR_NO_SPACE);
2188 name = basename(buf);
2190 return got_error_from_errno2("basename", path);
2192 err = got_path_dirname(&parent_path, path);
2196 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2199 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2200 /* Display the complete path in error message. */
2201 err = got_error_path(path, err->code);
2206 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2210 te = got_object_tree_find_entry(tree, name);
2212 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2216 if (got_object_tree_entry_is_symlink(te)) {
2217 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2220 if (!got_path_is_absolute(*link_target)) {
2222 if (asprintf(&abspath, "%s/%s", parent_path,
2223 *link_target) == -1) {
2224 err = got_error_from_errno("asprintf");
2228 *link_target = malloc(PATH_MAX);
2229 if (*link_target == NULL) {
2230 err = got_error_from_errno("malloc");
2233 err = got_canonpath(abspath, *link_target, PATH_MAX);
2242 got_object_tree_close(tree);
2245 *link_target = NULL;
2250 const struct got_error *
2251 got_object_resolve_symlinks(char **link_target, const char *path,
2252 struct got_commit_object *commit, struct got_repository *repo)
2254 const struct got_error *err = NULL;
2255 char *next_target = NULL;
2256 int max_recursion = 40; /* matches Git */
2258 *link_target = NULL;
2261 err = resolve_symlink(&next_target,
2262 *link_target ? *link_target : path, commit, repo);
2267 if (--max_recursion == 0) {
2268 err = got_error_path(path, GOT_ERR_RECURSION);
2269 *link_target = NULL;
2272 *link_target = next_target;
2274 } while (next_target);
2279 const struct got_error *
2280 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2281 struct got_object_id *commit_id, const char *path,
2282 struct got_repository *repo)
2284 const struct got_error *err = NULL;
2285 struct got_pack *pack = NULL;
2286 struct got_packidx *packidx = NULL;
2287 char *path_packfile = NULL;
2288 struct got_commit_object *changed_commit = NULL;
2289 struct got_object_id *changed_commit_id = NULL;
2292 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2294 if (err->code != GOT_ERR_NO_OBJ)
2299 err = got_packidx_get_packfile_path(&path_packfile,
2300 packidx->path_packidx);
2304 pack = got_repo_get_cached_pack(repo, path_packfile);
2306 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2311 if (pack->privsep_child == NULL) {
2312 err = start_pack_privsep_child(pack, packidx);
2317 err = got_privsep_send_commit_traversal_request(
2318 pack->privsep_child->ibuf, commit_id, idx, path);
2322 err = got_privsep_recv_traversed_commits(&changed_commit,
2323 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2327 if (changed_commit) {
2329 * Cache the commit in which the path was changed.
2330 * This commit might be opened again soon.
2332 changed_commit->refcnt++;
2333 err = got_repo_cache_commit(repo, changed_commit_id,
2335 got_object_commit_close(changed_commit);
2338 free(path_packfile);
2339 free(changed_commit_id);