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>
20 #include <sys/socket.h>
22 #include <sys/resource.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))
61 struct got_object_id *
62 got_object_get_id(struct got_object *obj)
67 const struct got_error *
68 got_object_get_id_str(char **outbuf, struct got_object *obj)
70 return got_object_id_str(outbuf, &obj->id);
73 const struct got_error *
74 got_object_get_type(int *type, struct got_repository *repo,
75 struct got_object_id *id)
77 const struct got_error *err = NULL;
78 struct got_object *obj;
80 err = got_object_open(&obj, repo, id);
85 case GOT_OBJ_TYPE_COMMIT:
86 case GOT_OBJ_TYPE_TREE:
87 case GOT_OBJ_TYPE_BLOB:
88 case GOT_OBJ_TYPE_TAG:
92 err = got_error(GOT_ERR_OBJ_TYPE);
96 got_object_close(obj);
100 const struct got_error *
101 got_object_get_path(char **path, struct got_object_id *id,
102 struct got_repository *repo)
104 const struct got_error *err = NULL;
110 path_objects = got_repo_get_path_objects(repo);
111 if (path_objects == NULL)
112 return got_error_from_errno("got_repo_get_path_objects");
114 err = got_object_id_str(&hex, id);
118 if (asprintf(path, "%s/%.2x/%s", path_objects,
119 id->sha1[0], hex + 2) == -1)
120 err = got_error_from_errno("asprintf");
128 const struct got_error *
129 got_object_open_loose_fd(int *fd, struct got_object_id *id,
130 struct got_repository *repo)
132 const struct got_error *err = NULL;
135 err = got_object_get_path(&path, id, repo);
138 *fd = open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
140 err = got_error_from_errno2("open", path);
148 static const struct got_error *
149 request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
150 struct got_object_id *id)
152 const struct got_error *err = NULL;
153 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
155 err = got_privsep_send_packed_obj_req(ibuf, idx, id);
159 err = got_privsep_recv_obj(obj, ibuf);
163 memcpy(&(*obj)->id, id, sizeof((*obj)->id));
168 /* Create temporary files used during delta application. */
169 static const struct got_error *
170 pack_child_send_tempfiles(struct imsgbuf *ibuf, struct got_pack *pack)
172 const struct got_error *err;
176 * For performance reasons, the child will keep reusing the
177 * same temporary files during every object request.
178 * Opening and closing new files for every object request is
179 * too expensive during operations such as 'gotadmin pack'.
181 if (pack->child_has_tempfiles)
184 basefd = got_opentempfd();
186 return got_error_from_errno("got_opentempfd");
188 err = got_privsep_send_tmpfd(ibuf, basefd);
192 accumfd = got_opentempfd();
194 return got_error_from_errno("got_opentempfd");
196 err = got_privsep_send_tmpfd(ibuf, accumfd);
200 pack->child_has_tempfiles = 1;
204 static const struct got_error *
205 request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
206 int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
208 const struct got_error *err = NULL;
209 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
212 err = pack_child_send_tempfiles(ibuf, pack);
216 outfd_child = dup(outfd);
217 if (outfd_child == -1)
218 return got_error_from_errno("dup");
220 err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
226 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
230 err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
238 set_max_datasize(void)
242 if (getrlimit(RLIMIT_DATA, &rl) != 0)
245 rl.rlim_cur = rl.rlim_max;
246 setrlimit(RLIMIT_DATA, &rl);
249 static const struct got_error *
250 start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
252 const struct got_error *err = NULL;
255 struct imsgbuf *ibuf;
257 ibuf = calloc(1, sizeof(*ibuf));
259 return got_error_from_errno("calloc");
261 pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
262 if (pack->privsep_child == NULL) {
263 err = got_error_from_errno("calloc");
267 pack->child_has_tempfiles = 0;
268 pack->child_has_delta_outfd = 0;
270 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
271 err = got_error_from_errno("socketpair");
277 err = got_error_from_errno("fork");
279 } else if (pid == 0) {
281 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
282 pack->path_packfile);
286 if (close(imsg_fds[1]) == -1)
287 return got_error_from_errno("close");
288 pack->privsep_child->imsg_fd = imsg_fds[0];
289 pack->privsep_child->pid = pid;
290 imsg_init(ibuf, imsg_fds[0]);
291 pack->privsep_child->ibuf = ibuf;
293 err = got_privsep_init_pack_child(ibuf, pack, packidx);
295 const struct got_error *child_err;
296 err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
297 child_err = got_privsep_wait_for_child(
298 pack->privsep_child->pid);
299 if (child_err && err == NULL)
305 free(pack->privsep_child);
306 pack->privsep_child = NULL;
311 static const struct got_error *
312 read_packed_object_privsep(struct got_object **obj,
313 struct got_repository *repo, struct got_pack *pack,
314 struct got_packidx *packidx, int idx, struct got_object_id *id)
316 const struct got_error *err = NULL;
318 if (pack->privsep_child == NULL) {
319 err = start_pack_privsep_child(pack, packidx);
324 return request_packed_object(obj, pack, idx, id);
327 static const struct got_error *
328 read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
329 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
330 struct got_object_id *id)
332 const struct got_error *err = NULL;
334 if (pack->privsep_child == NULL) {
335 err = start_pack_privsep_child(pack, packidx);
340 return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
344 const struct got_error *
345 got_object_open_packed(struct got_object **obj, struct got_object_id *id,
346 struct got_repository *repo)
348 const struct got_error *err = NULL;
349 struct got_pack *pack = NULL;
350 struct got_packidx *packidx = NULL;
354 err = got_repo_search_packidx(&packidx, &idx, repo, id);
358 err = got_packidx_get_packfile_path(&path_packfile,
359 packidx->path_packidx);
363 pack = got_repo_get_cached_pack(repo, path_packfile);
365 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
370 err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
378 const struct got_error *
379 got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
380 struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
381 struct got_repository *repo)
383 return read_packed_object_privsep(obj, repo, pack, packidx,
387 const struct got_error *
388 got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
389 off_t *delta_size, off_t *delta_offset, off_t *delta_out_offset,
390 struct got_object_id **base_id, int delta_cache_fd,
391 struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
392 struct got_repository *repo)
394 const struct got_error *err = NULL;
395 struct got_pack *pack = NULL;
402 *delta_out_offset = 0;
404 err = got_packidx_get_packfile_path(&path_packfile,
405 packidx->path_packidx);
409 pack = got_repo_get_cached_pack(repo, path_packfile);
411 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
416 if (pack->privsep_child == NULL) {
417 err = start_pack_privsep_child(pack, packidx);
422 if (!pack->child_has_delta_outfd) {
424 outfd_child = dup(delta_cache_fd);
425 if (outfd_child == -1)
426 return got_error_from_errno("dup");
427 err = got_privsep_send_raw_delta_outfd(
428 pack->privsep_child->ibuf, outfd_child);
431 pack->child_has_delta_outfd = 1;
434 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
439 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
440 delta_offset, delta_out_offset, base_id, pack->privsep_child->ibuf);
443 static const struct got_error *
444 request_object(struct got_object **obj, struct got_object_id *id,
445 struct got_repository *repo, int fd)
447 const struct got_error *err = NULL;
448 struct imsgbuf *ibuf;
450 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
452 err = got_privsep_send_obj_req(ibuf, fd, id);
456 return got_privsep_recv_obj(obj, ibuf);
459 static const struct got_error *
460 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
461 struct got_object_id *id, struct got_repository *repo, int infd)
463 const struct got_error *err = NULL;
464 struct imsgbuf *ibuf;
467 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
469 outfd_child = dup(outfd);
470 if (outfd_child == -1)
471 return got_error_from_errno("dup");
473 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
477 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
481 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
484 static const struct got_error *
485 start_read_object_child(struct got_repository *repo)
487 const struct got_error *err = NULL;
490 struct imsgbuf *ibuf;
492 ibuf = calloc(1, sizeof(*ibuf));
494 return got_error_from_errno("calloc");
496 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
497 err = got_error_from_errno("socketpair");
504 err = got_error_from_errno("fork");
509 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
514 if (close(imsg_fds[1]) == -1) {
515 err = got_error_from_errno("close");
520 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
522 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
523 imsg_init(ibuf, imsg_fds[0]);
524 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
529 const struct got_error *
530 got_object_read_header_privsep(struct got_object **obj,
531 struct got_object_id *id, struct got_repository *repo, int obj_fd)
533 const struct got_error *err;
535 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
536 return request_object(obj, id, repo, obj_fd);
538 err = start_read_object_child(repo);
544 return request_object(obj, id, repo, obj_fd);
547 static const struct got_error *
548 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
549 int outfd, struct got_object_id *id, struct got_repository *repo,
552 const struct got_error *err;
554 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
555 return request_raw_object(outbuf, size, hdrlen, outfd, id,
558 err = start_read_object_child(repo);
562 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
566 const struct got_error *
567 got_object_open(struct got_object **obj, struct got_repository *repo,
568 struct got_object_id *id)
570 const struct got_error *err = NULL;
573 *obj = got_repo_get_cached_object(repo, id);
579 err = got_object_open_packed(obj, id, repo);
580 if (err && err->code != GOT_ERR_NO_OBJ)
584 return got_repo_cache_object(repo, id, *obj);
587 err = got_object_open_loose_fd(&fd, id, repo);
589 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
590 err = got_error_no_obj(id);
594 err = got_object_read_header_privsep(obj, id, repo, fd);
598 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
601 return got_repo_cache_object(repo, id, *obj);
604 /* *outfd must be initialized to -1 by caller */
605 const struct got_error *
606 got_object_raw_open(struct got_raw_object **obj, int *outfd,
607 struct got_repository *repo, struct got_object_id *id)
609 const struct got_error *err = NULL;
610 struct got_packidx *packidx = NULL;
612 uint8_t *outbuf = NULL;
615 char *path_packfile = NULL;
617 *obj = got_repo_get_cached_raw_object(repo, id);
624 *outfd = got_opentempfd();
626 return got_error_from_errno("got_opentempfd");
629 err = got_repo_search_packidx(&packidx, &idx, repo, id);
631 struct got_pack *pack = NULL;
633 err = got_packidx_get_packfile_path(&path_packfile,
634 packidx->path_packidx);
638 pack = got_repo_get_cached_pack(repo, path_packfile);
640 err = got_repo_cache_pack(&pack, repo, path_packfile,
645 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
646 *outfd, pack, packidx, idx, id);
649 } else if (err->code == GOT_ERR_NO_OBJ) {
652 err = got_object_open_loose_fd(&fd, id, repo);
655 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
661 *obj = calloc(1, sizeof(**obj));
663 err = got_error_from_errno("calloc");
669 (*obj)->data = outbuf;
672 if (fstat(*outfd, &sb) == -1) {
673 err = got_error_from_errno("fstat");
677 if (sb.st_size != hdrlen + size) {
678 err = got_error(GOT_ERR_PRIVSEP_LEN);
681 #ifndef GOT_PACK_NO_MMAP
682 if (hdrlen + size > 0) {
683 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
684 MAP_PRIVATE, *outfd, 0);
685 if ((*obj)->data == MAP_FAILED) {
686 if (errno != ENOMEM) {
687 err = got_error_from_errno("mmap");
698 (*obj)->f = fdopen(*outfd, "r");
699 if ((*obj)->f == NULL) {
700 err = got_error_from_errno("fdopen");
706 (*obj)->hdrlen = hdrlen;
708 err = got_repo_cache_raw_object(repo, id, *obj);
713 got_object_raw_close(*obj);
722 const struct got_error *
723 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
726 struct got_object_id id;
728 if (!got_parse_sha1_digest(id.sha1, id_str))
729 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
731 return got_object_open(obj, repo, &id);
734 const struct got_error *
735 got_object_resolve_id_str(struct got_object_id **id,
736 struct got_repository *repo, const char *id_str)
738 const struct got_error *err = NULL;
739 struct got_object *obj;
741 err = got_object_open_by_id_str(&obj, repo, id_str);
745 *id = got_object_id_dup(got_object_get_id(obj));
746 got_object_close(obj);
748 return got_error_from_errno("got_object_id_dup");
753 static const struct got_error *
754 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
755 int pack_idx, struct got_object_id *id)
757 const struct got_error *err = NULL;
759 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
764 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
768 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
772 static const struct got_error *
773 read_packed_commit_privsep(struct got_commit_object **commit,
774 struct got_pack *pack, struct got_packidx *packidx, int idx,
775 struct got_object_id *id)
777 const struct got_error *err = NULL;
779 if (pack->privsep_child)
780 return request_packed_commit(commit, pack, idx, id);
782 err = start_pack_privsep_child(pack, packidx);
786 return request_packed_commit(commit, pack, idx, id);
789 static const struct got_error *
790 request_commit(struct got_commit_object **commit, struct got_repository *repo,
791 int fd, struct got_object_id *id)
793 const struct got_error *err = NULL;
794 struct imsgbuf *ibuf;
796 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
798 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
802 return got_privsep_recv_commit(commit, ibuf);
805 static const struct got_error *
806 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
807 struct got_object_id *id, struct got_repository *repo)
809 const struct got_error *err;
812 struct imsgbuf *ibuf;
814 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
815 return request_commit(commit, repo, obj_fd, id);
817 ibuf = calloc(1, sizeof(*ibuf));
819 return got_error_from_errno("calloc");
821 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
822 err = got_error_from_errno("socketpair");
829 err = got_error_from_errno("fork");
834 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
839 if (close(imsg_fds[1]) == -1) {
840 err = got_error_from_errno("close");
844 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
846 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
847 imsg_init(ibuf, imsg_fds[0]);
848 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
850 return request_commit(commit, repo, obj_fd, id);
854 static const struct got_error *
855 open_commit(struct got_commit_object **commit,
856 struct got_repository *repo, struct got_object_id *id, int check_cache)
858 const struct got_error *err = NULL;
859 struct got_packidx *packidx = NULL;
861 char *path_packfile = NULL;
864 *commit = got_repo_get_cached_commit(repo, id);
865 if (*commit != NULL) {
872 err = got_repo_search_packidx(&packidx, &idx, repo, id);
874 struct got_pack *pack = NULL;
876 err = got_packidx_get_packfile_path(&path_packfile,
877 packidx->path_packidx);
881 pack = got_repo_get_cached_pack(repo, path_packfile);
883 err = got_repo_cache_pack(&pack, repo, path_packfile,
888 err = read_packed_commit_privsep(commit, pack,
890 } else if (err->code == GOT_ERR_NO_OBJ) {
893 err = got_object_open_loose_fd(&fd, id, repo);
896 err = read_commit_privsep(commit, fd, id, repo);
901 err = got_repo_cache_commit(repo, id, *commit);
908 const struct got_error *
909 got_object_open_as_commit(struct got_commit_object **commit,
910 struct got_repository *repo, struct got_object_id *id)
912 *commit = got_repo_get_cached_commit(repo, id);
913 if (*commit != NULL) {
918 return open_commit(commit, repo, id, 0);
921 const struct got_error *
922 got_object_commit_open(struct got_commit_object **commit,
923 struct got_repository *repo, struct got_object *obj)
925 return open_commit(commit, repo, got_object_get_id(obj), 1);
928 const struct got_error *
929 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
931 *qid = calloc(1, sizeof(**qid));
933 return got_error_from_errno("calloc");
935 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
939 const struct got_error *
940 got_object_id_queue_copy(const struct got_object_id_queue *src,
941 struct got_object_id_queue *dest)
943 const struct got_error *err;
944 struct got_object_qid *qid;
946 STAILQ_FOREACH(qid, src, entry) {
947 struct got_object_qid *new;
949 * Deep-copy the object ID only. Let the caller deal
950 * with setting up the new->data pointer if needed.
952 err = got_object_qid_alloc(&new, &qid->id);
954 got_object_id_queue_free(dest);
957 STAILQ_INSERT_TAIL(dest, new, entry);
963 static const struct got_error *
964 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
965 int pack_idx, struct got_object_id *id)
967 const struct got_error *err = NULL;
969 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
974 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
977 static const struct got_error *
978 read_packed_tree_privsep(struct got_tree_object **tree,
979 struct got_pack *pack, struct got_packidx *packidx, int idx,
980 struct got_object_id *id)
982 const struct got_error *err = NULL;
984 if (pack->privsep_child)
985 return request_packed_tree(tree, pack, idx, id);
987 err = start_pack_privsep_child(pack, packidx);
991 return request_packed_tree(tree, pack, idx, id);
994 static const struct got_error *
995 request_tree(struct got_tree_object **tree, struct got_repository *repo,
996 int fd, struct got_object_id *id)
998 const struct got_error *err = NULL;
999 struct imsgbuf *ibuf;
1001 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1003 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1007 return got_privsep_recv_tree(tree, ibuf);
1010 const struct got_error *
1011 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1012 struct got_object_id *id, struct got_repository *repo)
1014 const struct got_error *err;
1017 struct imsgbuf *ibuf;
1019 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1020 return request_tree(tree, repo, obj_fd, id);
1022 ibuf = calloc(1, sizeof(*ibuf));
1024 return got_error_from_errno("calloc");
1026 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1027 err = got_error_from_errno("socketpair");
1034 err = got_error_from_errno("fork");
1038 else if (pid == 0) {
1039 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1044 if (close(imsg_fds[1]) == -1) {
1045 err = got_error_from_errno("close");
1049 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1051 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1052 imsg_init(ibuf, imsg_fds[0]);
1053 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1056 return request_tree(tree, repo, obj_fd, id);
1059 static const struct got_error *
1060 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1061 struct got_object_id *id, int check_cache)
1063 const struct got_error *err = NULL;
1064 struct got_packidx *packidx = NULL;
1066 char *path_packfile = NULL;
1069 *tree = got_repo_get_cached_tree(repo, id);
1070 if (*tree != NULL) {
1077 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1079 struct got_pack *pack = NULL;
1081 err = got_packidx_get_packfile_path(&path_packfile,
1082 packidx->path_packidx);
1086 pack = got_repo_get_cached_pack(repo, path_packfile);
1088 err = got_repo_cache_pack(&pack, repo, path_packfile,
1093 err = read_packed_tree_privsep(tree, pack,
1095 } else if (err->code == GOT_ERR_NO_OBJ) {
1098 err = got_object_open_loose_fd(&fd, id, repo);
1101 err = read_tree_privsep(tree, fd, id, repo);
1106 err = got_repo_cache_tree(repo, id, *tree);
1109 free(path_packfile);
1113 const struct got_error *
1114 got_object_open_as_tree(struct got_tree_object **tree,
1115 struct got_repository *repo, struct got_object_id *id)
1117 *tree = got_repo_get_cached_tree(repo, id);
1118 if (*tree != NULL) {
1123 return open_tree(tree, repo, id, 0);
1126 const struct got_error *
1127 got_object_tree_open(struct got_tree_object **tree,
1128 struct got_repository *repo, struct got_object *obj)
1130 return open_tree(tree, repo, got_object_get_id(obj), 1);
1134 got_object_tree_get_nentries(struct got_tree_object *tree)
1136 return tree->nentries;
1139 struct got_tree_entry *
1140 got_object_tree_get_first_entry(struct got_tree_object *tree)
1142 return got_object_tree_get_entry(tree, 0);
1145 struct got_tree_entry *
1146 got_object_tree_get_last_entry(struct got_tree_object *tree)
1148 return got_object_tree_get_entry(tree, tree->nentries - 1);
1151 struct got_tree_entry *
1152 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1154 if (i < 0 || i >= tree->nentries)
1156 return &tree->entries[i];
1160 got_tree_entry_get_mode(struct got_tree_entry *te)
1166 got_tree_entry_get_name(struct got_tree_entry *te)
1168 return &te->name[0];
1171 struct got_object_id *
1172 got_tree_entry_get_id(struct got_tree_entry *te)
1177 const struct got_error *
1178 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1180 const struct got_error *err = NULL;
1181 size_t len, totlen, hdrlen, offset;
1185 hdrlen = got_object_blob_get_hdrlen(blob);
1191 err = got_object_blob_read_block(&len, blob);
1198 totlen += len - hdrlen;
1199 p = realloc(*s, totlen + 1);
1201 err = got_error_from_errno("realloc");
1207 /* Skip blob object header first time around. */
1209 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1214 (*s)[totlen] = '\0';
1218 const struct got_error *
1219 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1220 struct got_repository *repo)
1222 const struct got_error *err = NULL;
1223 struct got_blob_object *blob = NULL;
1225 *link_target = NULL;
1227 if (!got_object_tree_entry_is_symlink(te))
1228 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1230 err = got_object_open_as_blob(&blob, repo,
1231 got_tree_entry_get_id(te), PATH_MAX);
1235 err = got_object_blob_read_to_str(link_target, blob);
1236 got_object_blob_close(blob);
1239 *link_target = NULL;
1245 got_tree_entry_get_index(struct got_tree_entry *te)
1250 struct got_tree_entry *
1251 got_tree_entry_get_next(struct got_tree_object *tree,
1252 struct got_tree_entry *te)
1254 return got_object_tree_get_entry(tree, te->idx + 1);
1257 struct got_tree_entry *
1258 got_tree_entry_get_prev(struct got_tree_object *tree,
1259 struct got_tree_entry *te)
1261 return got_object_tree_get_entry(tree, te->idx - 1);
1264 static const struct got_error *
1265 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1266 struct got_pack *pack, struct got_packidx *packidx, int idx,
1267 struct got_object_id *id)
1269 const struct got_error *err = NULL;
1270 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1273 err = pack_child_send_tempfiles(ibuf, pack);
1277 outfd_child = dup(outfd);
1278 if (outfd_child == -1)
1279 return got_error_from_errno("dup");
1281 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1285 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1291 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1292 pack->privsep_child->ibuf);
1296 if (lseek(outfd, SEEK_SET, 0) == -1)
1297 err = got_error_from_errno("lseek");
1302 static const struct got_error *
1303 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1304 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1305 struct got_object_id *id)
1307 const struct got_error *err = NULL;
1309 if (pack->privsep_child == NULL) {
1310 err = start_pack_privsep_child(pack, packidx);
1315 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1319 static const struct got_error *
1320 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1321 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1323 const struct got_error *err = NULL;
1326 outfd_child = dup(outfd);
1327 if (outfd_child == -1)
1328 return got_error_from_errno("dup");
1330 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1334 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1338 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1342 if (lseek(outfd, SEEK_SET, 0) == -1)
1343 return got_error_from_errno("lseek");
1348 static const struct got_error *
1349 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1350 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1352 const struct got_error *err;
1355 struct imsgbuf *ibuf;
1357 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1358 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1359 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1363 ibuf = calloc(1, sizeof(*ibuf));
1365 return got_error_from_errno("calloc");
1367 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1368 err = got_error_from_errno("socketpair");
1375 err = got_error_from_errno("fork");
1379 else if (pid == 0) {
1380 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1385 if (close(imsg_fds[1]) == -1) {
1386 err = got_error_from_errno("close");
1390 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1392 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1393 imsg_init(ibuf, imsg_fds[0]);
1394 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1396 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1399 static const struct got_error *
1400 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1401 struct got_object_id *id, size_t blocksize)
1403 const struct got_error *err = NULL;
1404 struct got_packidx *packidx = NULL;
1406 char *path_packfile = NULL;
1409 size_t size, hdrlen;
1412 *blob = calloc(1, sizeof(**blob));
1414 return got_error_from_errno("calloc");
1416 outfd = got_opentempfd();
1418 return got_error_from_errno("got_opentempfd");
1420 (*blob)->read_buf = malloc(blocksize);
1421 if ((*blob)->read_buf == NULL) {
1422 err = got_error_from_errno("malloc");
1426 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1428 struct got_pack *pack = NULL;
1430 err = got_packidx_get_packfile_path(&path_packfile,
1431 packidx->path_packidx);
1435 pack = got_repo_get_cached_pack(repo, path_packfile);
1437 err = got_repo_cache_pack(&pack, repo, path_packfile,
1442 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1443 pack, packidx, idx, id);
1444 } else if (err->code == GOT_ERR_NO_OBJ) {
1447 err = got_object_open_loose_fd(&infd, id, repo);
1450 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1456 if (hdrlen > size) {
1457 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1462 if (close(outfd) == -1 && err == NULL)
1463 err = got_error_from_errno("close");
1465 (*blob)->f = fmemopen(outbuf, size, "rb");
1466 if ((*blob)->f == NULL) {
1467 err = got_error_from_errno("fmemopen");
1471 (*blob)->data = outbuf;
1473 if (fstat(outfd, &sb) == -1) {
1474 err = got_error_from_errno("fstat");
1478 if (sb.st_size != size) {
1479 err = got_error(GOT_ERR_PRIVSEP_LEN);
1483 (*blob)->f = fdopen(outfd, "rb");
1484 if ((*blob)->f == NULL) {
1485 err = got_error_from_errno("fdopen");
1492 (*blob)->hdrlen = hdrlen;
1493 (*blob)->blocksize = blocksize;
1494 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1497 free(path_packfile);
1500 got_object_blob_close(*blob);
1502 } else if (outfd != -1)
1508 const struct got_error *
1509 got_object_open_as_blob(struct got_blob_object **blob,
1510 struct got_repository *repo, struct got_object_id *id,
1513 return open_blob(blob, repo, id, blocksize);
1516 const struct got_error *
1517 got_object_blob_open(struct got_blob_object **blob,
1518 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1520 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1523 const struct got_error *
1524 got_object_blob_close(struct got_blob_object *blob)
1526 const struct got_error *err = NULL;
1527 free(blob->read_buf);
1528 if (blob->f && fclose(blob->f) == EOF)
1529 err = got_error_from_errno("fclose");
1536 got_object_blob_rewind(struct got_blob_object *blob)
1543 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1545 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1549 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1551 return blob->hdrlen;
1555 got_object_blob_get_read_buf(struct got_blob_object *blob)
1557 return blob->read_buf;
1560 const struct got_error *
1561 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1565 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1566 if (n == 0 && ferror(blob->f))
1567 return got_ferror(blob->f, GOT_ERR_IO);
1572 const struct got_error *
1573 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1574 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1576 const struct got_error *err = NULL;
1577 size_t n, len, hdrlen;
1580 const int alloc_chunksz = 512;
1582 off_t off = 0, total_len = 0;
1585 *line_offsets = NULL;
1591 hdrlen = got_object_blob_get_hdrlen(blob);
1593 err = got_object_blob_read_block(&len, blob);
1598 buf = got_object_blob_get_read_buf(blob);
1601 if (line_offsets && *line_offsets == NULL) {
1602 /* Have some data but perhaps no '\n'. */
1604 nalloc = alloc_chunksz;
1605 *line_offsets = calloc(nalloc,
1606 sizeof(**line_offsets));
1607 if (*line_offsets == NULL)
1608 return got_error_from_errno("calloc");
1610 /* Skip forward over end of first line. */
1617 /* Scan '\n' offsets in remaining chunk of data. */
1619 if (buf[i] != '\n') {
1624 if (line_offsets && nalloc < *nlines) {
1625 size_t n = *nlines + alloc_chunksz;
1626 off_t *o = recallocarray(*line_offsets,
1627 nalloc, n, sizeof(**line_offsets));
1629 free(*line_offsets);
1630 *line_offsets = NULL;
1631 return got_error_from_errno(
1638 off = total_len + i - hdrlen + 1;
1639 (*line_offsets)[*nlines - 1] = off;
1644 /* Skip blob object header first time around. */
1645 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1646 if (n != len - hdrlen)
1647 return got_ferror(outfile, GOT_ERR_IO);
1648 total_len += len - hdrlen;
1652 if (fflush(outfile) != 0)
1653 return got_error_from_errno("fflush");
1657 *filesize = total_len;
1662 static const struct got_error *
1663 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1664 int pack_idx, struct got_object_id *id)
1666 const struct got_error *err = NULL;
1668 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1673 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1676 static const struct got_error *
1677 read_packed_tag_privsep(struct got_tag_object **tag,
1678 struct got_pack *pack, struct got_packidx *packidx, int idx,
1679 struct got_object_id *id)
1681 const struct got_error *err = NULL;
1683 if (pack->privsep_child)
1684 return request_packed_tag(tag, pack, idx, id);
1686 err = start_pack_privsep_child(pack, packidx);
1690 return request_packed_tag(tag, pack, idx, id);
1693 static const struct got_error *
1694 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1695 int fd, struct got_object_id *id)
1697 const struct got_error *err = NULL;
1698 struct imsgbuf *ibuf;
1700 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1702 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1706 return got_privsep_recv_tag(tag, ibuf);
1709 static const struct got_error *
1710 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1711 struct got_object_id *id, struct got_repository *repo)
1713 const struct got_error *err;
1716 struct imsgbuf *ibuf;
1718 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1719 return request_tag(tag, repo, obj_fd, id);
1721 ibuf = calloc(1, sizeof(*ibuf));
1723 return got_error_from_errno("calloc");
1725 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1726 err = got_error_from_errno("socketpair");
1733 err = got_error_from_errno("fork");
1737 else if (pid == 0) {
1738 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1743 if (close(imsg_fds[1]) == -1) {
1744 err = got_error_from_errno("close");
1748 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1750 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1751 imsg_init(ibuf, imsg_fds[0]);
1752 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1754 return request_tag(tag, repo, obj_fd, id);
1757 static const struct got_error *
1758 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1759 struct got_object_id *id, int check_cache)
1761 const struct got_error *err = NULL;
1762 struct got_packidx *packidx = NULL;
1764 char *path_packfile = NULL;
1765 struct got_object *obj = NULL;
1766 int obj_type = GOT_OBJ_TYPE_ANY;
1769 *tag = got_repo_get_cached_tag(repo, id);
1777 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1779 struct got_pack *pack = NULL;
1781 err = got_packidx_get_packfile_path(&path_packfile,
1782 packidx->path_packidx);
1786 pack = got_repo_get_cached_pack(repo, path_packfile);
1788 err = got_repo_cache_pack(&pack, repo, path_packfile,
1794 /* Beware of "lightweight" tags: Check object type first. */
1795 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1799 obj_type = obj->type;
1800 got_object_close(obj);
1801 if (obj_type != GOT_OBJ_TYPE_TAG) {
1802 err = got_error(GOT_ERR_OBJ_TYPE);
1805 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1806 } else if (err->code == GOT_ERR_NO_OBJ) {
1809 err = got_object_open_loose_fd(&fd, id, repo);
1812 err = got_object_read_header_privsep(&obj, id, repo, fd);
1815 obj_type = obj->type;
1816 got_object_close(obj);
1817 if (obj_type != GOT_OBJ_TYPE_TAG)
1818 return got_error(GOT_ERR_OBJ_TYPE);
1820 err = got_object_open_loose_fd(&fd, id, repo);
1823 err = read_tag_privsep(tag, fd, id, repo);
1828 err = got_repo_cache_tag(repo, id, *tag);
1831 free(path_packfile);
1835 const struct got_error *
1836 got_object_open_as_tag(struct got_tag_object **tag,
1837 struct got_repository *repo, struct got_object_id *id)
1839 *tag = got_repo_get_cached_tag(repo, id);
1845 return open_tag(tag, repo, id, 0);
1848 const struct got_error *
1849 got_object_tag_open(struct got_tag_object **tag,
1850 struct got_repository *repo, struct got_object *obj)
1852 return open_tag(tag, repo, got_object_get_id(obj), 1);
1856 got_object_tag_get_name(struct got_tag_object *tag)
1862 got_object_tag_get_object_type(struct got_tag_object *tag)
1864 return tag->obj_type;
1867 struct got_object_id *
1868 got_object_tag_get_object_id(struct got_tag_object *tag)
1874 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1876 return tag->tagger_time;
1880 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1882 return tag->tagger_gmtoff;
1886 got_object_tag_get_tagger(struct got_tag_object *tag)
1892 got_object_tag_get_message(struct got_tag_object *tag)
1897 static struct got_tree_entry *
1898 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1902 /* Note that tree entries are sorted in strncmp() order. */
1903 for (i = 0; i < tree->nentries; i++) {
1904 struct got_tree_entry *te = &tree->entries[i];
1905 int cmp = strncmp(te->name, name, len);
1910 if (te->name[len] == '\0')
1916 struct got_tree_entry *
1917 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1919 return find_entry_by_name(tree, name, strlen(name));
1922 const struct got_error *
1923 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1924 struct got_repository *repo, struct got_tree_object *tree,
1927 const struct got_error *err = NULL;
1928 struct got_tree_object *subtree = NULL;
1929 struct got_tree_entry *te = NULL;
1930 const char *seg, *s;
1942 struct got_tree_object *next_tree;
1951 te = find_entry_by_name(subtree, seg, seglen);
1953 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1964 err = got_object_open_as_tree(&next_tree, repo,
1969 if (subtree != tree)
1970 got_object_tree_close(subtree);
1971 subtree = next_tree;
1976 *id = got_object_id_dup(&te->id);
1978 return got_error_from_errno("got_object_id_dup");
1982 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1984 if (subtree && subtree != tree)
1985 got_object_tree_close(subtree);
1988 const struct got_error *
1989 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1990 struct got_commit_object *commit, const char *path)
1992 const struct got_error *err = NULL;
1993 struct got_tree_object *tree = NULL;
1997 /* Handle opening of root of commit's tree. */
1998 if (got_path_is_root_dir(path)) {
1999 *id = got_object_id_dup(commit->tree_id);
2001 err = got_error_from_errno("got_object_id_dup");
2003 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2006 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2010 got_object_tree_close(tree);
2015 * Normalize file mode bits to avoid false positive tree entry differences
2016 * in case tree entries have unexpected mode bits set.
2019 normalize_mode_for_comparison(mode_t mode)
2022 * For directories, the only relevant bit is the IFDIR bit.
2023 * This allows us to detect paths changing from a directory
2024 * to a file and vice versa.
2027 return mode & S_IFDIR;
2030 * For symlinks, the only relevant bit is the IFLNK bit.
2031 * This allows us to detect paths changing from a symlinks
2032 * to a file or directory and vice versa.
2035 return mode & S_IFLNK;
2037 /* For files, the only change we care about is the executable bit. */
2038 return mode & S_IXUSR;
2041 const struct got_error *
2042 got_object_tree_path_changed(int *changed,
2043 struct got_tree_object *tree01, struct got_tree_object *tree02,
2044 const char *path, struct got_repository *repo)
2046 const struct got_error *err = NULL;
2047 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2048 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2049 const char *seg, *s;
2054 /* We not do support comparing the root path. */
2055 if (got_path_is_root_dir(path))
2056 return got_error_path(path, GOT_ERR_BAD_PATH);
2066 struct got_tree_object *next_tree1, *next_tree2;
2067 mode_t mode1, mode2;
2076 te1 = find_entry_by_name(tree1, seg, seglen);
2078 err = got_error(GOT_ERR_NO_OBJ);
2083 te2 = find_entry_by_name(tree2, seg, seglen);
2086 mode1 = normalize_mode_for_comparison(te1->mode);
2087 mode2 = normalize_mode_for_comparison(te2->mode);
2088 if (mode1 != mode2) {
2093 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2099 if (*s == '\0') { /* final path element */
2108 err = got_object_open_as_tree(&next_tree1, repo,
2113 if (tree1 != tree01)
2114 got_object_tree_close(tree1);
2118 err = got_object_open_as_tree(&next_tree2, repo,
2123 if (tree2 != tree02)
2124 got_object_tree_close(tree2);
2127 if (tree2 != tree02)
2128 got_object_tree_close(tree2);
2134 if (tree1 && tree1 != tree01)
2135 got_object_tree_close(tree1);
2136 if (tree2 && tree2 != tree02)
2137 got_object_tree_close(tree2);
2141 const struct got_error *
2142 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2143 struct got_tree_entry *te)
2145 const struct got_error *err = NULL;
2147 *new_te = calloc(1, sizeof(**new_te));
2148 if (*new_te == NULL)
2149 return got_error_from_errno("calloc");
2151 (*new_te)->mode = te->mode;
2152 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2153 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2158 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2160 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2164 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2166 /* S_IFDIR check avoids confusing symlinks with submodules. */
2167 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2170 static const struct got_error *
2171 resolve_symlink(char **link_target, const char *path,
2172 struct got_commit_object *commit, struct got_repository *repo)
2174 const struct got_error *err = NULL;
2176 char *name, *parent_path = NULL;
2177 struct got_object_id *tree_obj_id = NULL;
2178 struct got_tree_object *tree = NULL;
2179 struct got_tree_entry *te = NULL;
2181 *link_target = NULL;
2183 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2184 return got_error(GOT_ERR_NO_SPACE);
2186 name = basename(buf);
2188 return got_error_from_errno2("basename", path);
2190 err = got_path_dirname(&parent_path, path);
2194 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2197 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2198 /* Display the complete path in error message. */
2199 err = got_error_path(path, err->code);
2204 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2208 te = got_object_tree_find_entry(tree, name);
2210 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2214 if (got_object_tree_entry_is_symlink(te)) {
2215 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2218 if (!got_path_is_absolute(*link_target)) {
2220 if (asprintf(&abspath, "%s/%s", parent_path,
2221 *link_target) == -1) {
2222 err = got_error_from_errno("asprintf");
2226 *link_target = malloc(PATH_MAX);
2227 if (*link_target == NULL) {
2228 err = got_error_from_errno("malloc");
2231 err = got_canonpath(abspath, *link_target, PATH_MAX);
2240 got_object_tree_close(tree);
2243 *link_target = NULL;
2248 const struct got_error *
2249 got_object_resolve_symlinks(char **link_target, const char *path,
2250 struct got_commit_object *commit, struct got_repository *repo)
2252 const struct got_error *err = NULL;
2253 char *next_target = NULL;
2254 int max_recursion = 40; /* matches Git */
2256 *link_target = NULL;
2259 err = resolve_symlink(&next_target,
2260 *link_target ? *link_target : path, commit, repo);
2265 if (--max_recursion == 0) {
2266 err = got_error_path(path, GOT_ERR_RECURSION);
2267 *link_target = NULL;
2270 *link_target = next_target;
2272 } while (next_target);
2277 const struct got_error *
2278 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2279 struct got_object_id *commit_id, const char *path,
2280 struct got_repository *repo)
2282 const struct got_error *err = NULL;
2283 struct got_pack *pack = NULL;
2284 struct got_packidx *packidx = NULL;
2285 char *path_packfile = NULL;
2286 struct got_commit_object *changed_commit = NULL;
2287 struct got_object_id *changed_commit_id = NULL;
2290 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2292 if (err->code != GOT_ERR_NO_OBJ)
2297 err = got_packidx_get_packfile_path(&path_packfile,
2298 packidx->path_packidx);
2302 pack = got_repo_get_cached_pack(repo, path_packfile);
2304 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2309 if (pack->privsep_child == NULL) {
2310 err = start_pack_privsep_child(pack, packidx);
2315 err = got_privsep_send_commit_traversal_request(
2316 pack->privsep_child->ibuf, commit_id, idx, path);
2320 err = got_privsep_recv_traversed_commits(&changed_commit,
2321 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2325 if (changed_commit) {
2327 * Cache the commit in which the path was changed.
2328 * This commit might be opened again soon.
2330 changed_commit->refcnt++;
2331 err = got_repo_cache_commit(repo, changed_commit_id,
2333 got_object_commit_close(changed_commit);
2336 free(path_packfile);
2337 free(changed_commit_id);