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 const struct got_error *err = NULL;
933 *qid = calloc(1, sizeof(**qid));
935 return got_error_from_errno("calloc");
937 (*qid)->id = got_object_id_dup(id);
938 if ((*qid)->id == NULL) {
939 err = got_error_from_errno("got_object_id_dup");
940 got_object_qid_free(*qid);
948 const struct got_error *
949 got_object_id_queue_copy(const struct got_object_id_queue *src,
950 struct got_object_id_queue *dest)
952 const struct got_error *err;
953 struct got_object_qid *qid;
955 STAILQ_FOREACH(qid, src, entry) {
956 struct got_object_qid *new;
958 * Deep-copy the object ID only. Let the caller deal
959 * with setting up the new->data pointer if needed.
961 err = got_object_qid_alloc(&new, qid->id);
963 got_object_id_queue_free(dest);
966 STAILQ_INSERT_TAIL(dest, new, entry);
972 static const struct got_error *
973 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
974 int pack_idx, struct got_object_id *id)
976 const struct got_error *err = NULL;
978 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
983 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
986 static const struct got_error *
987 read_packed_tree_privsep(struct got_tree_object **tree,
988 struct got_pack *pack, struct got_packidx *packidx, int idx,
989 struct got_object_id *id)
991 const struct got_error *err = NULL;
993 if (pack->privsep_child)
994 return request_packed_tree(tree, pack, idx, id);
996 err = start_pack_privsep_child(pack, packidx);
1000 return request_packed_tree(tree, pack, idx, id);
1003 static const struct got_error *
1004 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1005 int fd, struct got_object_id *id)
1007 const struct got_error *err = NULL;
1008 struct imsgbuf *ibuf;
1010 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1012 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1016 return got_privsep_recv_tree(tree, ibuf);
1019 const struct got_error *
1020 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1021 struct got_object_id *id, struct got_repository *repo)
1023 const struct got_error *err;
1026 struct imsgbuf *ibuf;
1028 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1029 return request_tree(tree, repo, obj_fd, id);
1031 ibuf = calloc(1, sizeof(*ibuf));
1033 return got_error_from_errno("calloc");
1035 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1036 err = got_error_from_errno("socketpair");
1043 err = got_error_from_errno("fork");
1047 else if (pid == 0) {
1048 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1053 if (close(imsg_fds[1]) == -1) {
1054 err = got_error_from_errno("close");
1058 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1060 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1061 imsg_init(ibuf, imsg_fds[0]);
1062 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1065 return request_tree(tree, repo, obj_fd, id);
1068 static const struct got_error *
1069 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1070 struct got_object_id *id, int check_cache)
1072 const struct got_error *err = NULL;
1073 struct got_packidx *packidx = NULL;
1075 char *path_packfile = NULL;
1078 *tree = got_repo_get_cached_tree(repo, id);
1079 if (*tree != NULL) {
1086 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1088 struct got_pack *pack = NULL;
1090 err = got_packidx_get_packfile_path(&path_packfile,
1091 packidx->path_packidx);
1095 pack = got_repo_get_cached_pack(repo, path_packfile);
1097 err = got_repo_cache_pack(&pack, repo, path_packfile,
1102 err = read_packed_tree_privsep(tree, pack,
1104 } else if (err->code == GOT_ERR_NO_OBJ) {
1107 err = got_object_open_loose_fd(&fd, id, repo);
1110 err = read_tree_privsep(tree, fd, id, repo);
1115 err = got_repo_cache_tree(repo, id, *tree);
1118 free(path_packfile);
1122 const struct got_error *
1123 got_object_open_as_tree(struct got_tree_object **tree,
1124 struct got_repository *repo, struct got_object_id *id)
1126 *tree = got_repo_get_cached_tree(repo, id);
1127 if (*tree != NULL) {
1132 return open_tree(tree, repo, id, 0);
1135 const struct got_error *
1136 got_object_tree_open(struct got_tree_object **tree,
1137 struct got_repository *repo, struct got_object *obj)
1139 return open_tree(tree, repo, got_object_get_id(obj), 1);
1143 got_object_tree_get_nentries(struct got_tree_object *tree)
1145 return tree->nentries;
1148 struct got_tree_entry *
1149 got_object_tree_get_first_entry(struct got_tree_object *tree)
1151 return got_object_tree_get_entry(tree, 0);
1154 struct got_tree_entry *
1155 got_object_tree_get_last_entry(struct got_tree_object *tree)
1157 return got_object_tree_get_entry(tree, tree->nentries - 1);
1160 struct got_tree_entry *
1161 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1163 if (i < 0 || i >= tree->nentries)
1165 return &tree->entries[i];
1169 got_tree_entry_get_mode(struct got_tree_entry *te)
1175 got_tree_entry_get_name(struct got_tree_entry *te)
1177 return &te->name[0];
1180 struct got_object_id *
1181 got_tree_entry_get_id(struct got_tree_entry *te)
1186 const struct got_error *
1187 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1189 const struct got_error *err = NULL;
1190 size_t len, totlen, hdrlen, offset;
1194 hdrlen = got_object_blob_get_hdrlen(blob);
1200 err = got_object_blob_read_block(&len, blob);
1207 totlen += len - hdrlen;
1208 p = realloc(*s, totlen + 1);
1210 err = got_error_from_errno("realloc");
1216 /* Skip blob object header first time around. */
1218 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1223 (*s)[totlen] = '\0';
1227 const struct got_error *
1228 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1229 struct got_repository *repo)
1231 const struct got_error *err = NULL;
1232 struct got_blob_object *blob = NULL;
1234 *link_target = NULL;
1236 if (!got_object_tree_entry_is_symlink(te))
1237 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1239 err = got_object_open_as_blob(&blob, repo,
1240 got_tree_entry_get_id(te), PATH_MAX);
1244 err = got_object_blob_read_to_str(link_target, blob);
1245 got_object_blob_close(blob);
1248 *link_target = NULL;
1254 got_tree_entry_get_index(struct got_tree_entry *te)
1259 struct got_tree_entry *
1260 got_tree_entry_get_next(struct got_tree_object *tree,
1261 struct got_tree_entry *te)
1263 return got_object_tree_get_entry(tree, te->idx + 1);
1266 struct got_tree_entry *
1267 got_tree_entry_get_prev(struct got_tree_object *tree,
1268 struct got_tree_entry *te)
1270 return got_object_tree_get_entry(tree, te->idx - 1);
1273 static const struct got_error *
1274 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1275 struct got_pack *pack, struct got_packidx *packidx, int idx,
1276 struct got_object_id *id)
1278 const struct got_error *err = NULL;
1279 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1282 err = pack_child_send_tempfiles(ibuf, pack);
1286 outfd_child = dup(outfd);
1287 if (outfd_child == -1)
1288 return got_error_from_errno("dup");
1290 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1294 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1300 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1301 pack->privsep_child->ibuf);
1305 if (lseek(outfd, SEEK_SET, 0) == -1)
1306 err = got_error_from_errno("lseek");
1311 static const struct got_error *
1312 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1313 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1314 struct got_object_id *id)
1316 const struct got_error *err = NULL;
1318 if (pack->privsep_child == NULL) {
1319 err = start_pack_privsep_child(pack, packidx);
1324 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1328 static const struct got_error *
1329 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1330 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1332 const struct got_error *err = NULL;
1335 outfd_child = dup(outfd);
1336 if (outfd_child == -1)
1337 return got_error_from_errno("dup");
1339 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1343 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1347 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1351 if (lseek(outfd, SEEK_SET, 0) == -1)
1352 return got_error_from_errno("lseek");
1357 static const struct got_error *
1358 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1359 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1361 const struct got_error *err;
1364 struct imsgbuf *ibuf;
1366 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1367 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1368 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1372 ibuf = calloc(1, sizeof(*ibuf));
1374 return got_error_from_errno("calloc");
1376 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1377 err = got_error_from_errno("socketpair");
1384 err = got_error_from_errno("fork");
1388 else if (pid == 0) {
1389 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1394 if (close(imsg_fds[1]) == -1) {
1395 err = got_error_from_errno("close");
1399 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1401 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1402 imsg_init(ibuf, imsg_fds[0]);
1403 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1405 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1408 static const struct got_error *
1409 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1410 struct got_object_id *id, size_t blocksize)
1412 const struct got_error *err = NULL;
1413 struct got_packidx *packidx = NULL;
1415 char *path_packfile = NULL;
1418 size_t size, hdrlen;
1421 *blob = calloc(1, sizeof(**blob));
1423 return got_error_from_errno("calloc");
1425 outfd = got_opentempfd();
1427 return got_error_from_errno("got_opentempfd");
1429 (*blob)->read_buf = malloc(blocksize);
1430 if ((*blob)->read_buf == NULL) {
1431 err = got_error_from_errno("malloc");
1435 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1437 struct got_pack *pack = NULL;
1439 err = got_packidx_get_packfile_path(&path_packfile,
1440 packidx->path_packidx);
1444 pack = got_repo_get_cached_pack(repo, path_packfile);
1446 err = got_repo_cache_pack(&pack, repo, path_packfile,
1451 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1452 pack, packidx, idx, id);
1453 } else if (err->code == GOT_ERR_NO_OBJ) {
1456 err = got_object_open_loose_fd(&infd, id, repo);
1459 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1465 if (hdrlen > size) {
1466 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1471 if (close(outfd) == -1 && err == NULL)
1472 err = got_error_from_errno("close");
1474 (*blob)->f = fmemopen(outbuf, size, "rb");
1475 if ((*blob)->f == NULL) {
1476 err = got_error_from_errno("fmemopen");
1480 (*blob)->data = outbuf;
1482 if (fstat(outfd, &sb) == -1) {
1483 err = got_error_from_errno("fstat");
1487 if (sb.st_size != size) {
1488 err = got_error(GOT_ERR_PRIVSEP_LEN);
1492 (*blob)->f = fdopen(outfd, "rb");
1493 if ((*blob)->f == NULL) {
1494 err = got_error_from_errno("fdopen");
1501 (*blob)->hdrlen = hdrlen;
1502 (*blob)->blocksize = blocksize;
1503 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1506 free(path_packfile);
1509 got_object_blob_close(*blob);
1511 } else if (outfd != -1)
1517 const struct got_error *
1518 got_object_open_as_blob(struct got_blob_object **blob,
1519 struct got_repository *repo, struct got_object_id *id,
1522 return open_blob(blob, repo, id, blocksize);
1525 const struct got_error *
1526 got_object_blob_open(struct got_blob_object **blob,
1527 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1529 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1532 const struct got_error *
1533 got_object_blob_close(struct got_blob_object *blob)
1535 const struct got_error *err = NULL;
1536 free(blob->read_buf);
1537 if (blob->f && fclose(blob->f) == EOF)
1538 err = got_error_from_errno("fclose");
1545 got_object_blob_rewind(struct got_blob_object *blob)
1552 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1554 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1558 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1560 return blob->hdrlen;
1564 got_object_blob_get_read_buf(struct got_blob_object *blob)
1566 return blob->read_buf;
1569 const struct got_error *
1570 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1574 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1575 if (n == 0 && ferror(blob->f))
1576 return got_ferror(blob->f, GOT_ERR_IO);
1581 const struct got_error *
1582 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1583 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1585 const struct got_error *err = NULL;
1586 size_t n, len, hdrlen;
1589 const int alloc_chunksz = 512;
1591 off_t off = 0, total_len = 0;
1594 *line_offsets = NULL;
1600 hdrlen = got_object_blob_get_hdrlen(blob);
1602 err = got_object_blob_read_block(&len, blob);
1607 buf = got_object_blob_get_read_buf(blob);
1610 if (line_offsets && *line_offsets == NULL) {
1611 /* Have some data but perhaps no '\n'. */
1613 nalloc = alloc_chunksz;
1614 *line_offsets = calloc(nalloc,
1615 sizeof(**line_offsets));
1616 if (*line_offsets == NULL)
1617 return got_error_from_errno("calloc");
1619 /* Skip forward over end of first line. */
1626 /* Scan '\n' offsets in remaining chunk of data. */
1628 if (buf[i] != '\n') {
1633 if (line_offsets && nalloc < *nlines) {
1634 size_t n = *nlines + alloc_chunksz;
1635 off_t *o = recallocarray(*line_offsets,
1636 nalloc, n, sizeof(**line_offsets));
1638 free(*line_offsets);
1639 *line_offsets = NULL;
1640 return got_error_from_errno(
1647 off = total_len + i - hdrlen + 1;
1648 (*line_offsets)[*nlines - 1] = off;
1653 /* Skip blob object header first time around. */
1654 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1655 if (n != len - hdrlen)
1656 return got_ferror(outfile, GOT_ERR_IO);
1657 total_len += len - hdrlen;
1661 if (fflush(outfile) != 0)
1662 return got_error_from_errno("fflush");
1666 *filesize = total_len;
1671 static const struct got_error *
1672 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1673 int pack_idx, struct got_object_id *id)
1675 const struct got_error *err = NULL;
1677 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1682 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1685 static const struct got_error *
1686 read_packed_tag_privsep(struct got_tag_object **tag,
1687 struct got_pack *pack, struct got_packidx *packidx, int idx,
1688 struct got_object_id *id)
1690 const struct got_error *err = NULL;
1692 if (pack->privsep_child)
1693 return request_packed_tag(tag, pack, idx, id);
1695 err = start_pack_privsep_child(pack, packidx);
1699 return request_packed_tag(tag, pack, idx, id);
1702 static const struct got_error *
1703 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1704 int fd, struct got_object_id *id)
1706 const struct got_error *err = NULL;
1707 struct imsgbuf *ibuf;
1709 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1711 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1715 return got_privsep_recv_tag(tag, ibuf);
1718 static const struct got_error *
1719 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1720 struct got_object_id *id, struct got_repository *repo)
1722 const struct got_error *err;
1725 struct imsgbuf *ibuf;
1727 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1728 return request_tag(tag, repo, obj_fd, id);
1730 ibuf = calloc(1, sizeof(*ibuf));
1732 return got_error_from_errno("calloc");
1734 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1735 err = got_error_from_errno("socketpair");
1742 err = got_error_from_errno("fork");
1746 else if (pid == 0) {
1747 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1752 if (close(imsg_fds[1]) == -1) {
1753 err = got_error_from_errno("close");
1757 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1759 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1760 imsg_init(ibuf, imsg_fds[0]);
1761 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1763 return request_tag(tag, repo, obj_fd, id);
1766 static const struct got_error *
1767 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1768 struct got_object_id *id, int check_cache)
1770 const struct got_error *err = NULL;
1771 struct got_packidx *packidx = NULL;
1773 char *path_packfile = NULL;
1774 struct got_object *obj = NULL;
1775 int obj_type = GOT_OBJ_TYPE_ANY;
1778 *tag = got_repo_get_cached_tag(repo, id);
1786 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1788 struct got_pack *pack = NULL;
1790 err = got_packidx_get_packfile_path(&path_packfile,
1791 packidx->path_packidx);
1795 pack = got_repo_get_cached_pack(repo, path_packfile);
1797 err = got_repo_cache_pack(&pack, repo, path_packfile,
1803 /* Beware of "lightweight" tags: Check object type first. */
1804 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1808 obj_type = obj->type;
1809 got_object_close(obj);
1810 if (obj_type != GOT_OBJ_TYPE_TAG) {
1811 err = got_error(GOT_ERR_OBJ_TYPE);
1814 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1815 } else if (err->code == GOT_ERR_NO_OBJ) {
1818 err = got_object_open_loose_fd(&fd, id, repo);
1821 err = got_object_read_header_privsep(&obj, id, repo, fd);
1824 obj_type = obj->type;
1825 got_object_close(obj);
1826 if (obj_type != GOT_OBJ_TYPE_TAG)
1827 return got_error(GOT_ERR_OBJ_TYPE);
1829 err = got_object_open_loose_fd(&fd, id, repo);
1832 err = read_tag_privsep(tag, fd, id, repo);
1837 err = got_repo_cache_tag(repo, id, *tag);
1840 free(path_packfile);
1844 const struct got_error *
1845 got_object_open_as_tag(struct got_tag_object **tag,
1846 struct got_repository *repo, struct got_object_id *id)
1848 *tag = got_repo_get_cached_tag(repo, id);
1854 return open_tag(tag, repo, id, 0);
1857 const struct got_error *
1858 got_object_tag_open(struct got_tag_object **tag,
1859 struct got_repository *repo, struct got_object *obj)
1861 return open_tag(tag, repo, got_object_get_id(obj), 1);
1865 got_object_tag_get_name(struct got_tag_object *tag)
1871 got_object_tag_get_object_type(struct got_tag_object *tag)
1873 return tag->obj_type;
1876 struct got_object_id *
1877 got_object_tag_get_object_id(struct got_tag_object *tag)
1883 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1885 return tag->tagger_time;
1889 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1891 return tag->tagger_gmtoff;
1895 got_object_tag_get_tagger(struct got_tag_object *tag)
1901 got_object_tag_get_message(struct got_tag_object *tag)
1906 static struct got_tree_entry *
1907 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1911 /* Note that tree entries are sorted in strncmp() order. */
1912 for (i = 0; i < tree->nentries; i++) {
1913 struct got_tree_entry *te = &tree->entries[i];
1914 int cmp = strncmp(te->name, name, len);
1919 if (te->name[len] == '\0')
1925 struct got_tree_entry *
1926 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1928 return find_entry_by_name(tree, name, strlen(name));
1931 const struct got_error *
1932 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1933 struct got_repository *repo, struct got_tree_object *tree,
1936 const struct got_error *err = NULL;
1937 struct got_tree_object *subtree = NULL;
1938 struct got_tree_entry *te = NULL;
1939 const char *seg, *s;
1951 struct got_tree_object *next_tree;
1960 te = find_entry_by_name(subtree, seg, seglen);
1962 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1973 err = got_object_open_as_tree(&next_tree, repo,
1978 if (subtree != tree)
1979 got_object_tree_close(subtree);
1980 subtree = next_tree;
1985 *id = got_object_id_dup(&te->id);
1987 return got_error_from_errno("got_object_id_dup");
1991 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1993 if (subtree && subtree != tree)
1994 got_object_tree_close(subtree);
1997 const struct got_error *
1998 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1999 struct got_commit_object *commit, const char *path)
2001 const struct got_error *err = NULL;
2002 struct got_tree_object *tree = NULL;
2006 /* Handle opening of root of commit's tree. */
2007 if (got_path_is_root_dir(path)) {
2008 *id = got_object_id_dup(commit->tree_id);
2010 err = got_error_from_errno("got_object_id_dup");
2012 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2015 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2019 got_object_tree_close(tree);
2024 * Normalize file mode bits to avoid false positive tree entry differences
2025 * in case tree entries have unexpected mode bits set.
2028 normalize_mode_for_comparison(mode_t mode)
2031 * For directories, the only relevant bit is the IFDIR bit.
2032 * This allows us to detect paths changing from a directory
2033 * to a file and vice versa.
2036 return mode & S_IFDIR;
2039 * For symlinks, the only relevant bit is the IFLNK bit.
2040 * This allows us to detect paths changing from a symlinks
2041 * to a file or directory and vice versa.
2044 return mode & S_IFLNK;
2046 /* For files, the only change we care about is the executable bit. */
2047 return mode & S_IXUSR;
2050 const struct got_error *
2051 got_object_tree_path_changed(int *changed,
2052 struct got_tree_object *tree01, struct got_tree_object *tree02,
2053 const char *path, struct got_repository *repo)
2055 const struct got_error *err = NULL;
2056 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2057 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2058 const char *seg, *s;
2063 /* We not do support comparing the root path. */
2064 if (got_path_is_root_dir(path))
2065 return got_error_path(path, GOT_ERR_BAD_PATH);
2075 struct got_tree_object *next_tree1, *next_tree2;
2076 mode_t mode1, mode2;
2085 te1 = find_entry_by_name(tree1, seg, seglen);
2087 err = got_error(GOT_ERR_NO_OBJ);
2092 te2 = find_entry_by_name(tree2, seg, seglen);
2095 mode1 = normalize_mode_for_comparison(te1->mode);
2096 mode2 = normalize_mode_for_comparison(te2->mode);
2097 if (mode1 != mode2) {
2102 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2108 if (*s == '\0') { /* final path element */
2117 err = got_object_open_as_tree(&next_tree1, repo,
2122 if (tree1 != tree01)
2123 got_object_tree_close(tree1);
2127 err = got_object_open_as_tree(&next_tree2, repo,
2132 if (tree2 != tree02)
2133 got_object_tree_close(tree2);
2136 if (tree2 != tree02)
2137 got_object_tree_close(tree2);
2143 if (tree1 && tree1 != tree01)
2144 got_object_tree_close(tree1);
2145 if (tree2 && tree2 != tree02)
2146 got_object_tree_close(tree2);
2150 const struct got_error *
2151 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2152 struct got_tree_entry *te)
2154 const struct got_error *err = NULL;
2156 *new_te = calloc(1, sizeof(**new_te));
2157 if (*new_te == NULL)
2158 return got_error_from_errno("calloc");
2160 (*new_te)->mode = te->mode;
2161 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2162 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2167 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2169 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2173 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2175 /* S_IFDIR check avoids confusing symlinks with submodules. */
2176 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2179 static const struct got_error *
2180 resolve_symlink(char **link_target, const char *path,
2181 struct got_commit_object *commit, struct got_repository *repo)
2183 const struct got_error *err = NULL;
2185 char *name, *parent_path = NULL;
2186 struct got_object_id *tree_obj_id = NULL;
2187 struct got_tree_object *tree = NULL;
2188 struct got_tree_entry *te = NULL;
2190 *link_target = NULL;
2192 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2193 return got_error(GOT_ERR_NO_SPACE);
2195 name = basename(buf);
2197 return got_error_from_errno2("basename", path);
2199 err = got_path_dirname(&parent_path, path);
2203 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2206 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2207 /* Display the complete path in error message. */
2208 err = got_error_path(path, err->code);
2213 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2217 te = got_object_tree_find_entry(tree, name);
2219 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2223 if (got_object_tree_entry_is_symlink(te)) {
2224 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2227 if (!got_path_is_absolute(*link_target)) {
2229 if (asprintf(&abspath, "%s/%s", parent_path,
2230 *link_target) == -1) {
2231 err = got_error_from_errno("asprintf");
2235 *link_target = malloc(PATH_MAX);
2236 if (*link_target == NULL) {
2237 err = got_error_from_errno("malloc");
2240 err = got_canonpath(abspath, *link_target, PATH_MAX);
2249 got_object_tree_close(tree);
2252 *link_target = NULL;
2257 const struct got_error *
2258 got_object_resolve_symlinks(char **link_target, const char *path,
2259 struct got_commit_object *commit, struct got_repository *repo)
2261 const struct got_error *err = NULL;
2262 char *next_target = NULL;
2263 int max_recursion = 40; /* matches Git */
2265 *link_target = NULL;
2268 err = resolve_symlink(&next_target,
2269 *link_target ? *link_target : path, commit, repo);
2274 if (--max_recursion == 0) {
2275 err = got_error_path(path, GOT_ERR_RECURSION);
2276 *link_target = NULL;
2279 *link_target = next_target;
2281 } while (next_target);
2286 const struct got_error *
2287 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2288 struct got_object_id *commit_id, const char *path,
2289 struct got_repository *repo)
2291 const struct got_error *err = NULL;
2292 struct got_pack *pack = NULL;
2293 struct got_packidx *packidx = NULL;
2294 char *path_packfile = NULL;
2295 struct got_commit_object *changed_commit = NULL;
2296 struct got_object_id *changed_commit_id = NULL;
2299 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2301 if (err->code != GOT_ERR_NO_OBJ)
2306 err = got_packidx_get_packfile_path(&path_packfile,
2307 packidx->path_packidx);
2311 pack = got_repo_get_cached_pack(repo, path_packfile);
2313 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2318 if (pack->privsep_child == NULL) {
2319 err = start_pack_privsep_child(pack, packidx);
2324 err = got_privsep_send_commit_traversal_request(
2325 pack->privsep_child->ibuf, commit_id, idx, path);
2329 err = got_privsep_recv_traversed_commits(&changed_commit,
2330 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2334 if (changed_commit) {
2336 * Cache the commit in which the path was changed.
2337 * This commit might be opened again soon.
2339 changed_commit->refcnt++;
2340 err = got_repo_cache_commit(repo, changed_commit_id,
2342 got_object_commit_close(changed_commit);
2345 free(path_packfile);
2346 free(changed_commit_id);