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_compressed_size, off_t *delta_offset,
392 off_t *delta_out_offset, 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;
403 *delta_compressed_size = 0;
405 *delta_out_offset = 0;
407 err = got_packidx_get_packfile_path(&path_packfile,
408 packidx->path_packidx);
412 pack = got_repo_get_cached_pack(repo, path_packfile);
414 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
419 if (pack->privsep_child == NULL) {
420 err = start_pack_privsep_child(pack, packidx);
425 if (!pack->child_has_delta_outfd) {
427 outfd_child = dup(delta_cache_fd);
428 if (outfd_child == -1)
429 return got_error_from_errno("dup");
430 err = got_privsep_send_raw_delta_outfd(
431 pack->privsep_child->ibuf, outfd_child);
434 pack->child_has_delta_outfd = 1;
437 err = got_privsep_send_raw_delta_req(pack->privsep_child->ibuf,
442 return got_privsep_recv_raw_delta(base_size, result_size, delta_size,
443 delta_compressed_size, delta_offset, delta_out_offset, base_id,
444 pack->privsep_child->ibuf);
447 static const struct got_error *
448 request_object(struct got_object **obj, struct got_object_id *id,
449 struct got_repository *repo, int fd)
451 const struct got_error *err = NULL;
452 struct imsgbuf *ibuf;
454 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
456 err = got_privsep_send_obj_req(ibuf, fd, id);
460 return got_privsep_recv_obj(obj, ibuf);
463 static const struct got_error *
464 request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
465 struct got_object_id *id, struct got_repository *repo, int infd)
467 const struct got_error *err = NULL;
468 struct imsgbuf *ibuf;
471 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
473 outfd_child = dup(outfd);
474 if (outfd_child == -1)
475 return got_error_from_errno("dup");
477 err = got_privsep_send_raw_obj_req(ibuf, infd, id);
481 err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
485 return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
488 static const struct got_error *
489 start_read_object_child(struct got_repository *repo)
491 const struct got_error *err = NULL;
494 struct imsgbuf *ibuf;
496 ibuf = calloc(1, sizeof(*ibuf));
498 return got_error_from_errno("calloc");
500 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
501 err = got_error_from_errno("socketpair");
508 err = got_error_from_errno("fork");
513 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
518 if (close(imsg_fds[1]) == -1) {
519 err = got_error_from_errno("close");
524 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
526 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
527 imsg_init(ibuf, imsg_fds[0]);
528 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
533 const struct got_error *
534 got_object_read_header_privsep(struct got_object **obj,
535 struct got_object_id *id, struct got_repository *repo, int obj_fd)
537 const struct got_error *err;
539 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
540 return request_object(obj, id, repo, obj_fd);
542 err = start_read_object_child(repo);
548 return request_object(obj, id, repo, obj_fd);
551 static const struct got_error *
552 read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
553 int outfd, struct got_object_id *id, struct got_repository *repo,
556 const struct got_error *err;
558 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
559 return request_raw_object(outbuf, size, hdrlen, outfd, id,
562 err = start_read_object_child(repo);
566 return request_raw_object(outbuf, size, hdrlen, outfd, id, repo,
570 const struct got_error *
571 got_object_open(struct got_object **obj, struct got_repository *repo,
572 struct got_object_id *id)
574 const struct got_error *err = NULL;
577 *obj = got_repo_get_cached_object(repo, id);
583 err = got_object_open_packed(obj, id, repo);
584 if (err && err->code != GOT_ERR_NO_OBJ)
588 return got_repo_cache_object(repo, id, *obj);
591 err = got_object_open_loose_fd(&fd, id, repo);
593 if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
594 err = got_error_no_obj(id);
598 err = got_object_read_header_privsep(obj, id, repo, fd);
602 memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
605 return got_repo_cache_object(repo, id, *obj);
608 /* *outfd must be initialized to -1 by caller */
609 const struct got_error *
610 got_object_raw_open(struct got_raw_object **obj, int *outfd,
611 struct got_repository *repo, struct got_object_id *id)
613 const struct got_error *err = NULL;
614 struct got_packidx *packidx = NULL;
616 uint8_t *outbuf = NULL;
619 char *path_packfile = NULL;
621 *obj = got_repo_get_cached_raw_object(repo, id);
628 *outfd = got_opentempfd();
630 return got_error_from_errno("got_opentempfd");
633 err = got_repo_search_packidx(&packidx, &idx, repo, id);
635 struct got_pack *pack = NULL;
637 err = got_packidx_get_packfile_path(&path_packfile,
638 packidx->path_packidx);
642 pack = got_repo_get_cached_pack(repo, path_packfile);
644 err = got_repo_cache_pack(&pack, repo, path_packfile,
649 err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
650 *outfd, pack, packidx, idx, id);
653 } else if (err->code == GOT_ERR_NO_OBJ) {
656 err = got_object_open_loose_fd(&fd, id, repo);
659 err = read_object_raw_privsep(&outbuf, &size, &hdrlen, *outfd,
665 *obj = calloc(1, sizeof(**obj));
667 err = got_error_from_errno("calloc");
673 (*obj)->data = outbuf;
676 if (fstat(*outfd, &sb) == -1) {
677 err = got_error_from_errno("fstat");
681 if (sb.st_size != hdrlen + size) {
682 err = got_error(GOT_ERR_PRIVSEP_LEN);
685 #ifndef GOT_PACK_NO_MMAP
686 if (hdrlen + size > 0) {
687 (*obj)->data = mmap(NULL, hdrlen + size, PROT_READ,
688 MAP_PRIVATE, *outfd, 0);
689 if ((*obj)->data == MAP_FAILED) {
690 if (errno != ENOMEM) {
691 err = got_error_from_errno("mmap");
702 (*obj)->f = fdopen(*outfd, "r");
703 if ((*obj)->f == NULL) {
704 err = got_error_from_errno("fdopen");
710 (*obj)->hdrlen = hdrlen;
712 err = got_repo_cache_raw_object(repo, id, *obj);
717 got_object_raw_close(*obj);
726 const struct got_error *
727 got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
730 struct got_object_id id;
732 if (!got_parse_sha1_digest(id.sha1, id_str))
733 return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
735 return got_object_open(obj, repo, &id);
738 const struct got_error *
739 got_object_resolve_id_str(struct got_object_id **id,
740 struct got_repository *repo, const char *id_str)
742 const struct got_error *err = NULL;
743 struct got_object *obj;
745 err = got_object_open_by_id_str(&obj, repo, id_str);
749 *id = got_object_id_dup(got_object_get_id(obj));
750 got_object_close(obj);
752 return got_error_from_errno("got_object_id_dup");
757 static const struct got_error *
758 request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
759 int pack_idx, struct got_object_id *id)
761 const struct got_error *err = NULL;
763 err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
768 err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
772 (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
776 static const struct got_error *
777 read_packed_commit_privsep(struct got_commit_object **commit,
778 struct got_pack *pack, struct got_packidx *packidx, int idx,
779 struct got_object_id *id)
781 const struct got_error *err = NULL;
783 if (pack->privsep_child)
784 return request_packed_commit(commit, pack, idx, id);
786 err = start_pack_privsep_child(pack, packidx);
790 return request_packed_commit(commit, pack, idx, id);
793 static const struct got_error *
794 request_commit(struct got_commit_object **commit, struct got_repository *repo,
795 int fd, struct got_object_id *id)
797 const struct got_error *err = NULL;
798 struct imsgbuf *ibuf;
800 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
802 err = got_privsep_send_commit_req(ibuf, fd, id, -1);
806 return got_privsep_recv_commit(commit, ibuf);
809 static const struct got_error *
810 read_commit_privsep(struct got_commit_object **commit, int obj_fd,
811 struct got_object_id *id, struct got_repository *repo)
813 const struct got_error *err;
816 struct imsgbuf *ibuf;
818 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
819 return request_commit(commit, repo, obj_fd, id);
821 ibuf = calloc(1, sizeof(*ibuf));
823 return got_error_from_errno("calloc");
825 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
826 err = got_error_from_errno("socketpair");
833 err = got_error_from_errno("fork");
838 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
843 if (close(imsg_fds[1]) == -1) {
844 err = got_error_from_errno("close");
848 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
850 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
851 imsg_init(ibuf, imsg_fds[0]);
852 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
854 return request_commit(commit, repo, obj_fd, id);
858 static const struct got_error *
859 open_commit(struct got_commit_object **commit,
860 struct got_repository *repo, struct got_object_id *id, int check_cache)
862 const struct got_error *err = NULL;
863 struct got_packidx *packidx = NULL;
865 char *path_packfile = NULL;
868 *commit = got_repo_get_cached_commit(repo, id);
869 if (*commit != NULL) {
876 err = got_repo_search_packidx(&packidx, &idx, repo, id);
878 struct got_pack *pack = NULL;
880 err = got_packidx_get_packfile_path(&path_packfile,
881 packidx->path_packidx);
885 pack = got_repo_get_cached_pack(repo, path_packfile);
887 err = got_repo_cache_pack(&pack, repo, path_packfile,
892 err = read_packed_commit_privsep(commit, pack,
894 } else if (err->code == GOT_ERR_NO_OBJ) {
897 err = got_object_open_loose_fd(&fd, id, repo);
900 err = read_commit_privsep(commit, fd, id, repo);
905 err = got_repo_cache_commit(repo, id, *commit);
912 const struct got_error *
913 got_object_open_as_commit(struct got_commit_object **commit,
914 struct got_repository *repo, struct got_object_id *id)
916 *commit = got_repo_get_cached_commit(repo, id);
917 if (*commit != NULL) {
922 return open_commit(commit, repo, id, 0);
925 const struct got_error *
926 got_object_commit_open(struct got_commit_object **commit,
927 struct got_repository *repo, struct got_object *obj)
929 return open_commit(commit, repo, got_object_get_id(obj), 1);
932 const struct got_error *
933 got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
935 *qid = calloc(1, sizeof(**qid));
937 return got_error_from_errno("calloc");
939 memcpy(&(*qid)->id, id, sizeof((*qid)->id));
943 const struct got_error *
944 got_object_id_queue_copy(const struct got_object_id_queue *src,
945 struct got_object_id_queue *dest)
947 const struct got_error *err;
948 struct got_object_qid *qid;
950 STAILQ_FOREACH(qid, src, entry) {
951 struct got_object_qid *new;
953 * Deep-copy the object ID only. Let the caller deal
954 * with setting up the new->data pointer if needed.
956 err = got_object_qid_alloc(&new, &qid->id);
958 got_object_id_queue_free(dest);
961 STAILQ_INSERT_TAIL(dest, new, entry);
967 static const struct got_error *
968 request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
969 int pack_idx, struct got_object_id *id)
971 const struct got_error *err = NULL;
973 err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
978 return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
981 static const struct got_error *
982 read_packed_tree_privsep(struct got_tree_object **tree,
983 struct got_pack *pack, struct got_packidx *packidx, int idx,
984 struct got_object_id *id)
986 const struct got_error *err = NULL;
988 if (pack->privsep_child)
989 return request_packed_tree(tree, pack, idx, id);
991 err = start_pack_privsep_child(pack, packidx);
995 return request_packed_tree(tree, pack, idx, id);
998 static const struct got_error *
999 request_tree(struct got_tree_object **tree, struct got_repository *repo,
1000 int fd, struct got_object_id *id)
1002 const struct got_error *err = NULL;
1003 struct imsgbuf *ibuf;
1005 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1007 err = got_privsep_send_tree_req(ibuf, fd, id, -1);
1011 return got_privsep_recv_tree(tree, ibuf);
1014 const struct got_error *
1015 read_tree_privsep(struct got_tree_object **tree, int obj_fd,
1016 struct got_object_id *id, struct got_repository *repo)
1018 const struct got_error *err;
1021 struct imsgbuf *ibuf;
1023 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1024 return request_tree(tree, repo, obj_fd, id);
1026 ibuf = calloc(1, sizeof(*ibuf));
1028 return got_error_from_errno("calloc");
1030 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1031 err = got_error_from_errno("socketpair");
1038 err = got_error_from_errno("fork");
1042 else if (pid == 0) {
1043 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1048 if (close(imsg_fds[1]) == -1) {
1049 err = got_error_from_errno("close");
1053 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1055 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1056 imsg_init(ibuf, imsg_fds[0]);
1057 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1060 return request_tree(tree, repo, obj_fd, id);
1063 static const struct got_error *
1064 open_tree(struct got_tree_object **tree, struct got_repository *repo,
1065 struct got_object_id *id, int check_cache)
1067 const struct got_error *err = NULL;
1068 struct got_packidx *packidx = NULL;
1070 char *path_packfile = NULL;
1073 *tree = got_repo_get_cached_tree(repo, id);
1074 if (*tree != NULL) {
1081 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1083 struct got_pack *pack = NULL;
1085 err = got_packidx_get_packfile_path(&path_packfile,
1086 packidx->path_packidx);
1090 pack = got_repo_get_cached_pack(repo, path_packfile);
1092 err = got_repo_cache_pack(&pack, repo, path_packfile,
1097 err = read_packed_tree_privsep(tree, pack,
1099 } else if (err->code == GOT_ERR_NO_OBJ) {
1102 err = got_object_open_loose_fd(&fd, id, repo);
1105 err = read_tree_privsep(tree, fd, id, repo);
1110 err = got_repo_cache_tree(repo, id, *tree);
1113 free(path_packfile);
1117 const struct got_error *
1118 got_object_open_as_tree(struct got_tree_object **tree,
1119 struct got_repository *repo, struct got_object_id *id)
1121 *tree = got_repo_get_cached_tree(repo, id);
1122 if (*tree != NULL) {
1127 return open_tree(tree, repo, id, 0);
1130 const struct got_error *
1131 got_object_tree_open(struct got_tree_object **tree,
1132 struct got_repository *repo, struct got_object *obj)
1134 return open_tree(tree, repo, got_object_get_id(obj), 1);
1138 got_object_tree_get_nentries(struct got_tree_object *tree)
1140 return tree->nentries;
1143 struct got_tree_entry *
1144 got_object_tree_get_first_entry(struct got_tree_object *tree)
1146 return got_object_tree_get_entry(tree, 0);
1149 struct got_tree_entry *
1150 got_object_tree_get_last_entry(struct got_tree_object *tree)
1152 return got_object_tree_get_entry(tree, tree->nentries - 1);
1155 struct got_tree_entry *
1156 got_object_tree_get_entry(struct got_tree_object *tree, int i)
1158 if (i < 0 || i >= tree->nentries)
1160 return &tree->entries[i];
1164 got_tree_entry_get_mode(struct got_tree_entry *te)
1170 got_tree_entry_get_name(struct got_tree_entry *te)
1172 return &te->name[0];
1175 struct got_object_id *
1176 got_tree_entry_get_id(struct got_tree_entry *te)
1181 const struct got_error *
1182 got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1184 const struct got_error *err = NULL;
1185 size_t len, totlen, hdrlen, offset;
1189 hdrlen = got_object_blob_get_hdrlen(blob);
1195 err = got_object_blob_read_block(&len, blob);
1202 totlen += len - hdrlen;
1203 p = realloc(*s, totlen + 1);
1205 err = got_error_from_errno("realloc");
1211 /* Skip blob object header first time around. */
1213 got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1218 (*s)[totlen] = '\0';
1222 const struct got_error *
1223 got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1224 struct got_repository *repo)
1226 const struct got_error *err = NULL;
1227 struct got_blob_object *blob = NULL;
1229 *link_target = NULL;
1231 if (!got_object_tree_entry_is_symlink(te))
1232 return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1234 err = got_object_open_as_blob(&blob, repo,
1235 got_tree_entry_get_id(te), PATH_MAX);
1239 err = got_object_blob_read_to_str(link_target, blob);
1240 got_object_blob_close(blob);
1243 *link_target = NULL;
1249 got_tree_entry_get_index(struct got_tree_entry *te)
1254 struct got_tree_entry *
1255 got_tree_entry_get_next(struct got_tree_object *tree,
1256 struct got_tree_entry *te)
1258 return got_object_tree_get_entry(tree, te->idx + 1);
1261 struct got_tree_entry *
1262 got_tree_entry_get_prev(struct got_tree_object *tree,
1263 struct got_tree_entry *te)
1265 return got_object_tree_get_entry(tree, te->idx - 1);
1268 static const struct got_error *
1269 request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1270 struct got_pack *pack, struct got_packidx *packidx, int idx,
1271 struct got_object_id *id)
1273 const struct got_error *err = NULL;
1274 struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1277 err = pack_child_send_tempfiles(ibuf, pack);
1281 outfd_child = dup(outfd);
1282 if (outfd_child == -1)
1283 return got_error_from_errno("dup");
1285 err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1289 err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1295 err = got_privsep_recv_blob(outbuf, size, hdrlen,
1296 pack->privsep_child->ibuf);
1300 if (lseek(outfd, SEEK_SET, 0) == -1)
1301 err = got_error_from_errno("lseek");
1306 static const struct got_error *
1307 read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1308 int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1309 struct got_object_id *id)
1311 const struct got_error *err = NULL;
1313 if (pack->privsep_child == NULL) {
1314 err = start_pack_privsep_child(pack, packidx);
1319 return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1323 static const struct got_error *
1324 request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1325 int infd, struct got_object_id *id, struct imsgbuf *ibuf)
1327 const struct got_error *err = NULL;
1330 outfd_child = dup(outfd);
1331 if (outfd_child == -1)
1332 return got_error_from_errno("dup");
1334 err = got_privsep_send_blob_req(ibuf, infd, id, -1);
1338 err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1342 err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1346 if (lseek(outfd, SEEK_SET, 0) == -1)
1347 return got_error_from_errno("lseek");
1352 static const struct got_error *
1353 read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1354 int outfd, int infd, struct got_object_id *id, struct got_repository *repo)
1356 const struct got_error *err;
1359 struct imsgbuf *ibuf;
1361 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1362 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1363 return request_blob(outbuf, size, hdrlen, outfd, infd, id,
1367 ibuf = calloc(1, sizeof(*ibuf));
1369 return got_error_from_errno("calloc");
1371 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1372 err = got_error_from_errno("socketpair");
1379 err = got_error_from_errno("fork");
1383 else if (pid == 0) {
1384 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1389 if (close(imsg_fds[1]) == -1) {
1390 err = got_error_from_errno("close");
1394 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1396 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1397 imsg_init(ibuf, imsg_fds[0]);
1398 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1400 return request_blob(outbuf, size, hdrlen, outfd, infd, id, ibuf);
1403 static const struct got_error *
1404 open_blob(struct got_blob_object **blob, struct got_repository *repo,
1405 struct got_object_id *id, size_t blocksize)
1407 const struct got_error *err = NULL;
1408 struct got_packidx *packidx = NULL;
1410 char *path_packfile = NULL;
1413 size_t size, hdrlen;
1416 *blob = calloc(1, sizeof(**blob));
1418 return got_error_from_errno("calloc");
1420 outfd = got_opentempfd();
1422 return got_error_from_errno("got_opentempfd");
1424 (*blob)->read_buf = malloc(blocksize);
1425 if ((*blob)->read_buf == NULL) {
1426 err = got_error_from_errno("malloc");
1430 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1432 struct got_pack *pack = NULL;
1434 err = got_packidx_get_packfile_path(&path_packfile,
1435 packidx->path_packidx);
1439 pack = got_repo_get_cached_pack(repo, path_packfile);
1441 err = got_repo_cache_pack(&pack, repo, path_packfile,
1446 err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1447 pack, packidx, idx, id);
1448 } else if (err->code == GOT_ERR_NO_OBJ) {
1451 err = got_object_open_loose_fd(&infd, id, repo);
1454 err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1460 if (hdrlen > size) {
1461 err = got_error(GOT_ERR_BAD_OBJ_HDR);
1466 if (close(outfd) == -1 && err == NULL)
1467 err = got_error_from_errno("close");
1469 (*blob)->f = fmemopen(outbuf, size, "rb");
1470 if ((*blob)->f == NULL) {
1471 err = got_error_from_errno("fmemopen");
1475 (*blob)->data = outbuf;
1477 if (fstat(outfd, &sb) == -1) {
1478 err = got_error_from_errno("fstat");
1482 if (sb.st_size != size) {
1483 err = got_error(GOT_ERR_PRIVSEP_LEN);
1487 (*blob)->f = fdopen(outfd, "rb");
1488 if ((*blob)->f == NULL) {
1489 err = got_error_from_errno("fdopen");
1496 (*blob)->hdrlen = hdrlen;
1497 (*blob)->blocksize = blocksize;
1498 memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1501 free(path_packfile);
1504 got_object_blob_close(*blob);
1506 } else if (outfd != -1)
1512 const struct got_error *
1513 got_object_open_as_blob(struct got_blob_object **blob,
1514 struct got_repository *repo, struct got_object_id *id,
1517 return open_blob(blob, repo, id, blocksize);
1520 const struct got_error *
1521 got_object_blob_open(struct got_blob_object **blob,
1522 struct got_repository *repo, struct got_object *obj, size_t blocksize)
1524 return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1527 const struct got_error *
1528 got_object_blob_close(struct got_blob_object *blob)
1530 const struct got_error *err = NULL;
1531 free(blob->read_buf);
1532 if (blob->f && fclose(blob->f) == EOF)
1533 err = got_error_from_errno("fclose");
1540 got_object_blob_rewind(struct got_blob_object *blob)
1547 got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1549 return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1553 got_object_blob_get_hdrlen(struct got_blob_object *blob)
1555 return blob->hdrlen;
1559 got_object_blob_get_read_buf(struct got_blob_object *blob)
1561 return blob->read_buf;
1564 const struct got_error *
1565 got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1569 n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1570 if (n == 0 && ferror(blob->f))
1571 return got_ferror(blob->f, GOT_ERR_IO);
1576 const struct got_error *
1577 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1578 off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1580 const struct got_error *err = NULL;
1581 size_t n, len, hdrlen;
1584 const int alloc_chunksz = 512;
1586 off_t off = 0, total_len = 0;
1589 *line_offsets = NULL;
1595 hdrlen = got_object_blob_get_hdrlen(blob);
1597 err = got_object_blob_read_block(&len, blob);
1602 buf = got_object_blob_get_read_buf(blob);
1605 if (line_offsets && *line_offsets == NULL) {
1606 /* Have some data but perhaps no '\n'. */
1608 nalloc = alloc_chunksz;
1609 *line_offsets = calloc(nalloc,
1610 sizeof(**line_offsets));
1611 if (*line_offsets == NULL)
1612 return got_error_from_errno("calloc");
1614 /* Skip forward over end of first line. */
1621 /* Scan '\n' offsets in remaining chunk of data. */
1623 if (buf[i] != '\n') {
1628 if (line_offsets && nalloc < *nlines) {
1629 size_t n = *nlines + alloc_chunksz;
1630 off_t *o = recallocarray(*line_offsets,
1631 nalloc, n, sizeof(**line_offsets));
1633 free(*line_offsets);
1634 *line_offsets = NULL;
1635 return got_error_from_errno(
1642 off = total_len + i - hdrlen + 1;
1643 (*line_offsets)[*nlines - 1] = off;
1648 /* Skip blob object header first time around. */
1649 n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1650 if (n != len - hdrlen)
1651 return got_ferror(outfile, GOT_ERR_IO);
1652 total_len += len - hdrlen;
1656 if (fflush(outfile) != 0)
1657 return got_error_from_errno("fflush");
1661 *filesize = total_len;
1666 static const struct got_error *
1667 request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1668 int pack_idx, struct got_object_id *id)
1670 const struct got_error *err = NULL;
1672 err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1677 return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1680 static const struct got_error *
1681 read_packed_tag_privsep(struct got_tag_object **tag,
1682 struct got_pack *pack, struct got_packidx *packidx, int idx,
1683 struct got_object_id *id)
1685 const struct got_error *err = NULL;
1687 if (pack->privsep_child)
1688 return request_packed_tag(tag, pack, idx, id);
1690 err = start_pack_privsep_child(pack, packidx);
1694 return request_packed_tag(tag, pack, idx, id);
1697 static const struct got_error *
1698 request_tag(struct got_tag_object **tag, struct got_repository *repo,
1699 int fd, struct got_object_id *id)
1701 const struct got_error *err = NULL;
1702 struct imsgbuf *ibuf;
1704 ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1706 err = got_privsep_send_tag_req(ibuf, fd, id, -1);
1710 return got_privsep_recv_tag(tag, ibuf);
1713 static const struct got_error *
1714 read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1715 struct got_object_id *id, struct got_repository *repo)
1717 const struct got_error *err;
1720 struct imsgbuf *ibuf;
1722 if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1723 return request_tag(tag, repo, obj_fd, id);
1725 ibuf = calloc(1, sizeof(*ibuf));
1727 return got_error_from_errno("calloc");
1729 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1730 err = got_error_from_errno("socketpair");
1737 err = got_error_from_errno("fork");
1741 else if (pid == 0) {
1742 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1747 if (close(imsg_fds[1]) == -1) {
1748 err = got_error_from_errno("close");
1752 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1754 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1755 imsg_init(ibuf, imsg_fds[0]);
1756 repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1758 return request_tag(tag, repo, obj_fd, id);
1761 static const struct got_error *
1762 open_tag(struct got_tag_object **tag, struct got_repository *repo,
1763 struct got_object_id *id, int check_cache)
1765 const struct got_error *err = NULL;
1766 struct got_packidx *packidx = NULL;
1768 char *path_packfile = NULL;
1769 struct got_object *obj = NULL;
1770 int obj_type = GOT_OBJ_TYPE_ANY;
1773 *tag = got_repo_get_cached_tag(repo, id);
1781 err = got_repo_search_packidx(&packidx, &idx, repo, id);
1783 struct got_pack *pack = NULL;
1785 err = got_packidx_get_packfile_path(&path_packfile,
1786 packidx->path_packidx);
1790 pack = got_repo_get_cached_pack(repo, path_packfile);
1792 err = got_repo_cache_pack(&pack, repo, path_packfile,
1798 /* Beware of "lightweight" tags: Check object type first. */
1799 err = read_packed_object_privsep(&obj, repo, pack, packidx,
1803 obj_type = obj->type;
1804 got_object_close(obj);
1805 if (obj_type != GOT_OBJ_TYPE_TAG) {
1806 err = got_error(GOT_ERR_OBJ_TYPE);
1809 err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1810 } else if (err->code == GOT_ERR_NO_OBJ) {
1813 err = got_object_open_loose_fd(&fd, id, repo);
1816 err = got_object_read_header_privsep(&obj, id, repo, fd);
1819 obj_type = obj->type;
1820 got_object_close(obj);
1821 if (obj_type != GOT_OBJ_TYPE_TAG)
1822 return got_error(GOT_ERR_OBJ_TYPE);
1824 err = got_object_open_loose_fd(&fd, id, repo);
1827 err = read_tag_privsep(tag, fd, id, repo);
1832 err = got_repo_cache_tag(repo, id, *tag);
1835 free(path_packfile);
1839 const struct got_error *
1840 got_object_open_as_tag(struct got_tag_object **tag,
1841 struct got_repository *repo, struct got_object_id *id)
1843 *tag = got_repo_get_cached_tag(repo, id);
1849 return open_tag(tag, repo, id, 0);
1852 const struct got_error *
1853 got_object_tag_open(struct got_tag_object **tag,
1854 struct got_repository *repo, struct got_object *obj)
1856 return open_tag(tag, repo, got_object_get_id(obj), 1);
1860 got_object_tag_get_name(struct got_tag_object *tag)
1866 got_object_tag_get_object_type(struct got_tag_object *tag)
1868 return tag->obj_type;
1871 struct got_object_id *
1872 got_object_tag_get_object_id(struct got_tag_object *tag)
1878 got_object_tag_get_tagger_time(struct got_tag_object *tag)
1880 return tag->tagger_time;
1884 got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1886 return tag->tagger_gmtoff;
1890 got_object_tag_get_tagger(struct got_tag_object *tag)
1896 got_object_tag_get_message(struct got_tag_object *tag)
1901 static struct got_tree_entry *
1902 find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1906 /* Note that tree entries are sorted in strncmp() order. */
1907 for (i = 0; i < tree->nentries; i++) {
1908 struct got_tree_entry *te = &tree->entries[i];
1909 int cmp = strncmp(te->name, name, len);
1914 if (te->name[len] == '\0')
1920 struct got_tree_entry *
1921 got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1923 return find_entry_by_name(tree, name, strlen(name));
1926 const struct got_error *
1927 got_object_tree_find_path(struct got_object_id **id, mode_t *mode,
1928 struct got_repository *repo, struct got_tree_object *tree,
1931 const struct got_error *err = NULL;
1932 struct got_tree_object *subtree = NULL;
1933 struct got_tree_entry *te = NULL;
1934 const char *seg, *s;
1946 struct got_tree_object *next_tree;
1955 te = find_entry_by_name(subtree, seg, seglen);
1957 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1968 err = got_object_open_as_tree(&next_tree, repo,
1973 if (subtree != tree)
1974 got_object_tree_close(subtree);
1975 subtree = next_tree;
1980 *id = got_object_id_dup(&te->id);
1982 return got_error_from_errno("got_object_id_dup");
1986 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1988 if (subtree && subtree != tree)
1989 got_object_tree_close(subtree);
1992 const struct got_error *
1993 got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1994 struct got_commit_object *commit, const char *path)
1996 const struct got_error *err = NULL;
1997 struct got_tree_object *tree = NULL;
2001 /* Handle opening of root of commit's tree. */
2002 if (got_path_is_root_dir(path)) {
2003 *id = got_object_id_dup(commit->tree_id);
2005 err = got_error_from_errno("got_object_id_dup");
2007 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
2010 err = got_object_tree_find_path(id, NULL, repo, tree, path);
2014 got_object_tree_close(tree);
2019 * Normalize file mode bits to avoid false positive tree entry differences
2020 * in case tree entries have unexpected mode bits set.
2023 normalize_mode_for_comparison(mode_t mode)
2026 * For directories, the only relevant bit is the IFDIR bit.
2027 * This allows us to detect paths changing from a directory
2028 * to a file and vice versa.
2031 return mode & S_IFDIR;
2034 * For symlinks, the only relevant bit is the IFLNK bit.
2035 * This allows us to detect paths changing from a symlinks
2036 * to a file or directory and vice versa.
2039 return mode & S_IFLNK;
2041 /* For files, the only change we care about is the executable bit. */
2042 return mode & S_IXUSR;
2045 const struct got_error *
2046 got_object_tree_path_changed(int *changed,
2047 struct got_tree_object *tree01, struct got_tree_object *tree02,
2048 const char *path, struct got_repository *repo)
2050 const struct got_error *err = NULL;
2051 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2052 struct got_tree_entry *te1 = NULL, *te2 = NULL;
2053 const char *seg, *s;
2058 /* We not do support comparing the root path. */
2059 if (got_path_is_root_dir(path))
2060 return got_error_path(path, GOT_ERR_BAD_PATH);
2070 struct got_tree_object *next_tree1, *next_tree2;
2071 mode_t mode1, mode2;
2080 te1 = find_entry_by_name(tree1, seg, seglen);
2082 err = got_error(GOT_ERR_NO_OBJ);
2087 te2 = find_entry_by_name(tree2, seg, seglen);
2090 mode1 = normalize_mode_for_comparison(te1->mode);
2091 mode2 = normalize_mode_for_comparison(te2->mode);
2092 if (mode1 != mode2) {
2097 if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2103 if (*s == '\0') { /* final path element */
2112 err = got_object_open_as_tree(&next_tree1, repo,
2117 if (tree1 != tree01)
2118 got_object_tree_close(tree1);
2122 err = got_object_open_as_tree(&next_tree2, repo,
2127 if (tree2 != tree02)
2128 got_object_tree_close(tree2);
2131 if (tree2 != tree02)
2132 got_object_tree_close(tree2);
2138 if (tree1 && tree1 != tree01)
2139 got_object_tree_close(tree1);
2140 if (tree2 && tree2 != tree02)
2141 got_object_tree_close(tree2);
2145 const struct got_error *
2146 got_object_tree_entry_dup(struct got_tree_entry **new_te,
2147 struct got_tree_entry *te)
2149 const struct got_error *err = NULL;
2151 *new_te = calloc(1, sizeof(**new_te));
2152 if (*new_te == NULL)
2153 return got_error_from_errno("calloc");
2155 (*new_te)->mode = te->mode;
2156 memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2157 memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2162 got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2164 return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2168 got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2170 /* S_IFDIR check avoids confusing symlinks with submodules. */
2171 return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2174 static const struct got_error *
2175 resolve_symlink(char **link_target, const char *path,
2176 struct got_commit_object *commit, struct got_repository *repo)
2178 const struct got_error *err = NULL;
2180 char *name, *parent_path = NULL;
2181 struct got_object_id *tree_obj_id = NULL;
2182 struct got_tree_object *tree = NULL;
2183 struct got_tree_entry *te = NULL;
2185 *link_target = NULL;
2187 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2188 return got_error(GOT_ERR_NO_SPACE);
2190 name = basename(buf);
2192 return got_error_from_errno2("basename", path);
2194 err = got_path_dirname(&parent_path, path);
2198 err = got_object_id_by_path(&tree_obj_id, repo, commit,
2201 if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2202 /* Display the complete path in error message. */
2203 err = got_error_path(path, err->code);
2208 err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2212 te = got_object_tree_find_entry(tree, name);
2214 err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2218 if (got_object_tree_entry_is_symlink(te)) {
2219 err = got_tree_entry_get_symlink_target(link_target, te, repo);
2222 if (!got_path_is_absolute(*link_target)) {
2224 if (asprintf(&abspath, "%s/%s", parent_path,
2225 *link_target) == -1) {
2226 err = got_error_from_errno("asprintf");
2230 *link_target = malloc(PATH_MAX);
2231 if (*link_target == NULL) {
2232 err = got_error_from_errno("malloc");
2235 err = got_canonpath(abspath, *link_target, PATH_MAX);
2244 got_object_tree_close(tree);
2247 *link_target = NULL;
2252 const struct got_error *
2253 got_object_resolve_symlinks(char **link_target, const char *path,
2254 struct got_commit_object *commit, struct got_repository *repo)
2256 const struct got_error *err = NULL;
2257 char *next_target = NULL;
2258 int max_recursion = 40; /* matches Git */
2260 *link_target = NULL;
2263 err = resolve_symlink(&next_target,
2264 *link_target ? *link_target : path, commit, repo);
2269 if (--max_recursion == 0) {
2270 err = got_error_path(path, GOT_ERR_RECURSION);
2271 *link_target = NULL;
2274 *link_target = next_target;
2276 } while (next_target);
2281 const struct got_error *
2282 got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2283 struct got_object_id *commit_id, const char *path,
2284 struct got_repository *repo)
2286 const struct got_error *err = NULL;
2287 struct got_pack *pack = NULL;
2288 struct got_packidx *packidx = NULL;
2289 char *path_packfile = NULL;
2290 struct got_commit_object *changed_commit = NULL;
2291 struct got_object_id *changed_commit_id = NULL;
2294 err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2296 if (err->code != GOT_ERR_NO_OBJ)
2301 err = got_packidx_get_packfile_path(&path_packfile,
2302 packidx->path_packidx);
2306 pack = got_repo_get_cached_pack(repo, path_packfile);
2308 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2313 if (pack->privsep_child == NULL) {
2314 err = start_pack_privsep_child(pack, packidx);
2319 err = got_privsep_send_commit_traversal_request(
2320 pack->privsep_child->ibuf, commit_id, idx, path);
2324 err = got_privsep_recv_traversed_commits(&changed_commit,
2325 &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2329 if (changed_commit) {
2331 * Cache the commit in which the path was changed.
2332 * This commit might be opened again soon.
2334 changed_commit->refcnt++;
2335 err = got_repo_cache_commit(repo, changed_commit_id,
2337 got_object_commit_close(changed_commit);
2340 free(path_packfile);
2341 free(changed_commit_id);